Install Gentoo on a bootable USB stick

Booting Gentoo from a USB stick is really quite simple, to make it work properly though you need to setup an initial ramdisk and you will need to make sure you have drivers for all the machines you are going to boot with it. This article will describe how to install Gentoo onto a USB stick that can be booted in any computer. This installation will be just like your desktop installation and changes will be persistent.

Preparation
Before one begins, some packages are needed. It is assumed that you are running on a Gentoo system to create this bootable USB stick. Most importantly a kernel and the package are needed.

To add package-specific useflags, you may need to make a new file in :

Compile the kernel
Make the required changes to your kernel and compile it. Please see section 7 of the handbook and Kernel/Configuration for more detail on this step. Importantly remember that a general kernel is better here because the more general it is the more machines will boot with it. You can still compile modules as these will be included in the initramfs and loaded at boot.

genkernel --firmware --busybox --disklabel --bootdir=/tmp/boot --no-symlink --all-ramdisk-modules --install all

The above command will take some time as it will build the default kernel and create a ramdisk then copy them into.

Prepare the USB stick
Create 2 partitions on the drive (you can probably do it with one but it is normal practice to create a and a  (root) partition. It is assumed that your USB stick is . The USB stick could also be . If you run dmesg immediately after you plug in the USB Stick you should see a message which shows the device letter.  Adjust as necessary.

Make sure that the first partition is bootable (a command toggles the boot flag).

Now format the new partitions to the ext2 file system (but you can use another file system for the / partition if it has support in your kernel). Notice that we have given the drives a label, this is important because it will be how we detect the root file system later, you also notice --disklabel on the genkernel command line earlier.

Stage 3 installation
The kernel is ready, the initial ramdisk is ready and our file systems have been created and labelled, now we need some shell utilities and such. This is all contained in the stage3 tarball available from your nearest Gentoo mirror... and portage snapshot.


 * current-stage3
 * portage-latest

For this example and  were downloaded. This next bit is the same as in the handbook for installing Gentoo.

Chrooting
Now we are going to chroot into the Gentoo environment...

Bootloader
So that you can boot the new system you must install a bootloader onto your USB stick. You have many options for this, but here I will just run through lilo and syslinux.

lilo Bootloader Configuration
Emerge lilo.

Edit so that it has the correct information in it now.

The lines after image are all easy but the append line contains some interesting options. REAL_ROOT=LABEL=GENTOO_USB_ROOT will use the label of the disk instead of which is important because these device numbers move around depending on the number of hard disks in the computer or number of USB disk drives. Also important for USB booting is the scandelay, as USB devices need a little time to be detected by the kernel; that is what this option is for. When you have lots of modules booting takes a long time so it probably does not matter, but if you have only a few it is important, because the system could have booted before the kernel detects the USB device. Also, on newer machines with USB3 controllers you will need the xhci_hcd driver (built into the kernel or as a module).

The first warning is to be expected as we are not installing the boot loader onto the hard disk in your computer. The second warning will only cause a problem on machines that are old, if it is a problem for you try cutting down the kernel by removing modules.

syslinux Bootloader Configuration
Put the boot sector onto your USB stick.

Now to configure the bootloader, to do this we use a text editor:

Then to install syslinux onto your USB stick, device is not absolutely necessary but will throw an error if you make a mistake (not that we ever make mistakes ;-) ).

fstab Configuration
Set the password for the root user and that is it. Exit the chroot environment and unmount all the mounts you made before going into it.

Setting the root password
The stage3 tarball we used comes with a password... I don't now what that password is and you should change it anyway, before leaving your chroot make sure to set it to something you know so that you can login to your new system.

Locale and other configurations
The locale configuration is left up to you, it is quite easy and covered in the handbook. But don't skip this step!

Little tips
Although you can use the system we just made as a standard Gentoo system, if you are using a USB stick rather than a hard drive it might be worthwhile compiling binary packages on a host and then installing then pulling them on in binary form... or if you have enough memory just mount to a tmpfs, as compilation will be quicker that way anyway!

That is all
You should now be able to boot your shiny new Gentoo system from the USB drive.