Install Gentoo on a bootable USB stick/ko

Booting Gentoo from a USB stick is really quite simple. The main challenge is to setup an initial ramdisk and to include the drivers for all the machines it is going to boot. This article will describe how to install Gentoo onto a USB stick that can be booted on any computer. This installation will be just like a regular desktop installation and changes will be persistent.

Preparation
Start by installing a couple of necessary packages. Most importantly these are a kernel and the package:

To add package-specific useflags, create a new file in :

Next install the package:

Compiling the kernel
Make the required changes to the kernel and compile it. Please see Configuring the Linux kernel and the kernel configuration guide 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.

During the configuration, it is of course possible to compile modules as these will be included in the initramfs and loaded at boot.

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

Preparing the USB stick
Create 2 partitions on the drive (assuming the installation will have a and a  (root) partition). We assume that the USB stick is at (run dmesg immediately after plugging in the USB stick to see which device is being used).

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

Now format the new partitions. In the example we use the ext2 file system but another file system for the partition can be used if it is supported in the kernel. Notice that during the formatting operation, a label is assigned to the partitions. This is important because it will be how the root file system is detected later as the USB stick can be booted on systems where the drive letter allocation is completely different. This is related to the  option of the genkernel command line earlier.

Gentoo stage 3 installation
With the kernel, ramdisk and file systems ready, we now need the minimal installation to be performed. This includes a fresh stage3 as well as a Portage tree snapshot.


 * current-stage3
 * portage-latest

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

The sections below cover those aspects of a Gentoo Linux installation that are specific to installing on an USB stick. It is recommended to use the Gentoo handbook instructions while keeping an eye on this article.

Bootloader
In order to boot the new system, install a bootloader on the USB stick.

The below sections give example configurations for lilo and syslinux.

lilo
Emerge lilo (from within the chroot):

Edit so that it has the correct information in it.

The lines after image are all easy but the append line contains some interesting options.


 * 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  option, as USB devices need a little time to be detected by the kernel; that is what this option is for. When there are lots of modules booting takes a long time so it probably does not matter, but if there are only a few modules loaded then it is important, because the system could have booted before the kernel detects the USB device. Also, on newer machines with USB3 controllers it is necessary to add the xhci_hcd driver (built into the kernel or as a module).

Now install LILO (with the configuration) on the USB stick:

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

syslinux
Put the boot sector onto the USB stick.

Now to configure the bootloader.

Next install syslinux onto the USB stick. The  option is not absolutely necessary though.

Tips and tricks
Although it is possible to use the system just made as a standard Gentoo system, it might be worthwhile to compile binary packages on a host and then installing them on the USB stick. Or if the system has enough memory just mount to a tmpfs, as compilation will be much quicker that way anyway!