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 we begin we need some packages, I assume that you are running on a Gentoo system to create this bootable USB stick.

Most importantly we need a kernel and we need the sys-kernel/genkernel package.

Using the new package.use layout we need to make a new file in /etc/portage/package.use/ (If you would prefer the old layout then just add this line to /etc/portage/package.use/package.use.file)

Compile The Kernel
Make the required changes to your kernel and compile it. I will not go into detail on how to do this, 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 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 /tmp/boot.

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 /boot and a / partition. I assume that your USB stick is /dev/sdb.

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

Now Format the new partitions to the ext2 file system (I recommend you use ext2 for the /boot partition 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</tt> 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 I have downloaded stage3-amd64-20120621.tar.bz2</tt> and portage-latest.tar.xv</tt>. This next bit is the same as in the handbook for installing Gentoo.

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

Get LILO Installed on your USB stick
You must now emerge lilo...

Final Configuration Steps
Edit /etc/lilo.conf</tt> 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</tt> will use the label of the disk instead of /dev/sdb2</tt> 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</tt>, 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</tt> 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.

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.

Locale and Other Configurations
I will leave the locale configuration to you, it is quite easy and covered in the handbook.

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 /var/tmp</tt> to a tmpfs, 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.