LiveUSB

From Gentoo Wiki
Jump to:navigation Jump to:search

This article explains how to create a Gentoo LiveUSB or, in other words, how to emulate a x86 or amd64 Gentoo LiveCD using a USB drive. This is particularly useful for installing Gentoo on a modern laptop with no CD-ROM drive.

Although the instructions found in this document aim at emulating a Gentoo LiveCD using a USB drive, they should work for any arbitrary block device as long as the device names are adjusted accordingly.

Many other methods available on different operating systems should work for creating bootable LiveUSB drives for Gentoo installation.

Note
This article covers creation of a simple bootable drive, from an image file, that can be useful for installing Gentoo to a hard drive, for example. For instructions on installation of a complete, functional, Gentoo system onto a USB drive, see the Install Gentoo on a bootable USB stick article.
Note
unetbootin provides an easy, GUI, alternative to create a bootable USB sticks from a bootable CD image, in Gentoo.

Prerequisites

In order to boot a LiveUSB, the following will be needed:

  • Bootable USB drive with enough space to write the image to. When using the Gentoo Minimal Installation CD, at least 1GB. (Generally, the bigger the better.)
  • A computer with support for


Access to the following is needed for creating a LiveUSB:

  • A computer running Gentoo (or alternatively another Linux distribution)
  • A computer running Microsoft Windows with the appropriate software (see the Windows section below)
  • A computer able to run the dd command (e.g. MacOS)

Choosing an installation media for Gentoo

Good choices are:

  • The architecture appropriate Gentoo Minimal Installation CD
  • SystemRescueCd 5.3.2, which is Gentoo-based. Both 32-bit and 64-bit setups are supported by the same ISO. As of version 6.0.0, SystemRescueCd is based on Arch Linux and dropped 32-bit support (i.e. x86)[1], so in time this alternative might no longer be suitable.


With that being said, almost any modern LiveCD should work for installing Gentoo.

When downloading, adjust the architecture (x86, amd64, arm, sparc, etc.) portion of the URL to match the system's CPU. Gentoo mirrors are here.

Note
Gentoo Minimal Installation CD images support both UEFI and legacy boot.

Convert the ISO image to hybrid mode

Most modern LiveCD's, like Gentoo are already in hybrid mode. If the LiveUSB does not boot, then it may be that the image will have to be converted to hybrid mode. Hybrid mode means image will enable the ISO to boot from both a CD-ROM device or a USB drive.

Convert the ISO with the following command:

root #isohybrid filename.iso

The isohybrid command comes as part of the sys-boot/syslinux package.

Using dd to write the ISO image to a USB drive

Warning
Incorrect use of the dd command can wipe any drive connected to the system. Always backup all important data.
Important
The dd command will wipe all data from the destination drive. Always backup all important data.

When using the Gentoo Installation CD ISOs, it is sufficient to directly copy the ISO contents onto the USB device. The dd command can be used to accomplish this. For instance, assuming the USB device is at /dev/sdc:

root #dd if=/path/to/image.iso of=/dev/sdc bs=8192k

The dd utility will not return any progress output (it can provide some progress output, but not without some finagling). The command will exit without any errors when it completes the transfer successfully (i.e. with no output at all). Depending on the size of the ISO image and the speed of the USB device, this process could some time. Be patient!

Once complete, the USB drive should be bootable.

On Windows, the dd command is also available through various projects, such as Cygwin, GNUWin32, or Chrysocome.

The remainder of this document explains how to setup a vfat partition that is writable using the syslinux bootloader instead of isolinux.

Creating bootable LiveUSB drives from Linux systems

Automatic drive-wide installation script

Warning
This script will erase all data from the USB drive. Make sure to backup any pre-existing data first, and always backup all important data.
Important
This article assumes that the supplied device node corresponds to the USB drive. If other SCSI-like devices exist be sure to use the correct device node. Major data loss could occur if the wrong device node is selected!

This script will assist in writing a LiveUSB stick suitable for both BIOS and UEFI systems.

FILE gentoo-usb.sh
#!/bin/bash
set -e
image=${1:?Supply the .iso image of a Gentoo installation medium}
target=${2:?Supply the target device}

echo Checking for the necessary tools presence...
which syslinux
which sfdisk
which mkfs.vfat

echo Mounting Gentoo CD image...
cdmountpoint=/mnt/gentoo-cd
mkdir -p "$cdmountpoint"
trap 'echo Unmounting Gentoo CD image...; umount "$cdmountpoint"' EXIT
mount -o loop,ro "$image" "$cdmountpoint"

echo Creating a disk-wide EFI FAT partition on "$target"...
echo ',,U,*' | sfdisk --wipe always "$target"

echo Installing syslinux MBR on "$target"...
dd if=/usr/share/syslinux/mbr.bin of="$target"
sleep 1

echo Creating file system on "$target"1...
mkfs.vfat "$target"1 -n GENTOO

echo Mounting file system...
mountpoint=/mnt/gentoo-usb
mkdir -p "$mountpoint"
mount "$target"1 "$mountpoint"

echo Copying files...
cp -r "$cdmountpoint"/* "$mountpoint"/
mv "$mountpoint"/isolinux/* "$mountpoint"
mv "$mountpoint"/isolinux.cfg "$mountpoint"/syslinux.cfg
rm -rf "$mountpoint"/isolinux*
mv "$mountpoint"/memtest86 "$mountpoint"/memtest
sed -i -e "s:cdroot:cdroot slowusb:" -e "s:kernel memtest86:kernel memtest:" "$mountpoint"/syslinux.cfg

echo Unmounting file system...
umount "$mountpoint"

echo Installing syslinux on "$target"1
syslinux "$target"1

echo Syncing...
sync

echo 'Done!'

Manually preparing a LiveUSB drive

Preparing the USB drive

Partitioning the drive
Warning
These instructions will erase all data from the USB drive. Make sure to backup any pre-existing data first, and always backup all important data.
Important
This article assumes that the /dev/sdc device node corresponds to the USB drive. If other SCSI-like devices exist be sure to use the correct device node. Major data loss could occur if the wrong device node is selected!

Create a FAT16 partition on the USB drive and mark it bootable using fdisk. An example partitioning scheme can be seen below:

Note
If the USB drive is 4GB or larger, use partition type b (W95 FAT32).
root #fdisk -l /dev/sdc
Disk /dev/sdc: 2063 MB, 2063597056 bytes
255 heads, 63 sectors/track, 250 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
  
   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1   *           1         250     2008124+   6  FAT16
Creating the filesystem

Create a FAT16 filesystem on the USB drive using mkfs.fat:

Note
If the drive is 4GB or larger, use -F 32 to create a FAT32 filesystem.
root #emerge --ask sys-fs/dosfstools
root #mkfs.fat -F 16 /dev/sdc1
mkfs.fat 3.0.22 (2013-07-19)
Installing a Master Boot Record (MBR)

Install the precompiled Master Boot Record (MBR) from syslinux on the USB drive:

root #emerge --ask --verbose sys-boot/syslinux
root #dd if=/usr/share/syslinux/mbr.bin of=/dev/sdc
0+1 records in
0+1 records out
440 bytes (440 B) copied, 0.00522668 s, 84.2 kB/s

Copying the files

Mounting the Gentoo Minimal Installation CD

Download a Gentoo Minimal Installation CD for the system's architecture from a the main site's download page and mount the ISO image on /mnt/cdrom as shown below:

root #mkdir -p /mnt/cdrom
root #mount -o loop,ro -t iso9660 /path/to/isofile.iso /mnt/cdrom

Adjust the /path/to/isofile.iso as necessary to the location of the downloaded Minimal Installation CD ISO.

Note
If a "Could not find any loop device" error message is displayed when mounting the ISO, a kernel recompile may be required. Verify the Loopback device support option in the kernel configuration has been enabled. For more information on kernel configuration see the kernel configuration article.
Mounting the LiveUSB

Mount the newly formatted USB drive on /mnt/usb as shown below:

root #mkdir -p /mnt/usb
root #mount -t vfat /dev/sdc1 /mnt/usb
Copying the files

Copy the files from the Minimal Installation CD to the LiveUSB. The files need to be reordered since syslinux will be used as the bootloader instead of isolinux:

root #cp -r /mnt/cdrom/* /mnt/usb
root #mv /mnt/usb/isolinux/* /mnt/usb
root #mv /mnt/usb/isolinux.cfg /mnt/usb/syslinux.cfg
root #rm -rf /mnt/usb/isolinux*
root #mv /mnt/usb/memtest86 /mnt/usb/memtest

Unmount the ISO image:

root #umount /mnt/cdrom
Adjusting the bootloader configuration

Adjust the syslinux configuration file using sed as shown below. The slowusb parameter will introduce some extra delays before attempting to mount the filesystem. This is needed to allow the USB drive to settle upon detection.

root #sed -i -e "s:cdroot:cdroot slowusb:" -e "s:kernel memtest86:kernel memtest:" /mnt/usb/syslinux.cfg

Installing a bootloader

Unmounting the drive

Make sure the USB drive has been unmounted before installing the bootloader:

root #umount /mnt/usb
Installing syslinux

Finally install the syslinux bootloader on the USB drive:

root #syslinux /dev/sdc1

Creating bootable LiveUSB drives under Windows

Rufus

Rufus is a free and open source project created to write images to USB drives, with a variety of operating systems. It tends to be faster than the Universal USB installer (see in the next section).

Rufus can be downloaded from the project's homepage.

Rufus is easy to use and should be mostly self explanatory. For more information, see Rufus' FAQ page.

Universal USB Installer

Universal USB installer is one of the oldest Linux-capable LiveUSB creators for Windows systems. It supports most Linux distributions, and has a simple, helpful wizard for selecting the Linux distribution. For Gentoo Minimal Installation CDs, however, select the Try Unlisted Linux ISO which is at the very bottom of the list. Select the proper USB drive to format and extract Linux to, and click Create.

Universal USB installer can be downloaded from its homepage.

Booting

Insert the USB drive and turn on the computer, make sure the BIOS (or EFI firmware) has been set to boot from USB. If all goes well,a standard syslinux prompt should appear on the screen.

When attempting to boot from a USB device, be sure to select or enable USB drives as bootable devices in the system's firmware interface. If the ISO is UEFI capable, it may be necessary to enable booting UEFI devices as well. This is typically performed via toggle options in the BIOS or UEFI firmware interface.

The boot order may need to be adjusted in the system's firmware for USB devices to boot first, although it is usually easier to hit the appropriate key (commonly either F2 or Delete) and manually select the USB device as a one-time boot option from the list of bootable devices.

If installing Gentoo, follow the generic installation instructions found in the Gentoo Handbook appropriate to the system's architecture from here on!

External resources

References

  1. SystemRescueCd change log, February 2nd, 2019. Retrieved on March 6th, 2019.

This page is based on a document formerly found on our main website gentoo.org.
The following people contributed to the original document: brix, neysx
They are listed here because wiki history does not allow for any external attribution. If you edit the wiki article, please do not add yourself here; your contributions are recorded on each article's associated history page.