Install Gentoo on a bootable USB stick
Preparation[edit | edit source]
The guide assumes that the current platform is a Gentoo Linux one. The majority of steps however can be performed from any distribution, just translate the Gentoo-specific way of doing things (such as installing software) to the other distribution's practices.
Start by installing a couple of necessary packages. Most importantly these are a kernel and the sys-kernel/genkernel package:
root #emerge --ask sys-kernel/gentoo-sources sys-kernel/genkernelTo add package-specific useflags, create a new file in /etc/portage/package.use/:
/etc/portage/package.use/sys-appssys-apps/busybox static
Next install the sys-apps/busybox package:
root #emerge --ask sys-apps/busyboxCompiling the kernel[edit | edit source]
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.
root #mkdir /tmp/boot
root #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, create a ramdisk and then copy all files into /tmp/boot.
root #ls /tmp/boot/
initramfs-genkernel-x86_64-3.5.2-gentoo kernel-genkernel-x86_64-3.5.2-gentoo System.map-genkernel-x86_64-3.5.2-gentoo
Preparing the USB stick[edit | edit source]
Create 2 partitions on the drive (assuming the installation will have a /boot and a / (root) partition). We assume that the USB stick is at /dev/sdb (run dmesg immediately after plugging in the USB stick to see which device is being used).
root #fdisk /dev/sdbCommand (m for help): d
Selected partition 1
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4001759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4001759, default 4001759): +100M
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (1-4, default 2):
Using default value 2
First sector (206848-4001759, default 206848):
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-4001759, default 4001759):
Using default value 4001759
Command (m for help): a
Partition number (1-4): 1
Command (m for help): p
Disk /dev/sdb: 2048 MB, 2048901120 bytes
255 heads, 63 sectors/track, 249 cylinders, total 4001760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x001663df
Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 206847 102400 83 Linux
/dev/sdb2 206848 4001759 1897456 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
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 --disklabel option of the genkernel command line earlier.
root #mkfs.ext2 -L GENTOO_USB_BOOT /dev/sdb1
root #mkfs.ext2 -L GENTOO_USB_ROOT /dev/sdb2Gentoo stage 3 installation[edit | edit source]
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.
For this example stage3-amd64-20120621.tar.bz2 and portage-latest.tar.xz are used. This next bit is the same as in the handbook for installing Gentoo.
root #mount /dev/sdb2 /mnt/gentoo
root #cd /mnt/gentoo
root #tar -xpf ~/Download/stage3-amd64-20120621.tar.bz2
root #cd usr
root #tar -xpf ~/Download/portage-latest.tar.xz
root #mount /dev/sdb1 /mnt/gentoo/boot
root #cp /tmp/boot/* /mnt/gentoo/bootThe 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[edit | edit source]
In order to boot the new system, install a bootloader on the USB stick.
The below sections give example configurations for either using grub or using lilo together with syslinux.
grub[edit | edit source]
This method only applies to UEFI booting!
Emerge grub (from within the chroot):
(chroot) #emerge sys-boot/grubInstall grub to corresponding place with your platform information. Suppose you are running x86_64 machine with EFI partition mounted to /boot/efi, the following command will install grub into the USB stick:
(chroot) #grub-install --efi-directory=/boot/efi --target=x86_64-efi --removableNote that the
--removable option is very important. It indicates that the target location is removable device. Don't forget it otherwise you may mess up your own system!If nothing bad happens, run grub-mkconfig to generate grub configuration.
(chroot) #grub-mkconfig -o /boot/grub/grub.cfgChange the target directory accordingly if you use a different grub config directory.
lilo[edit | edit source]
Emerge lilo (from within the chroot):
(chroot) #emerge sys-boot/liloEdit etc/lilo.conf so that it has the correct information in it.
These settings are very important and will be discussed line by line.
/etc/lilo.confConfigure lilo.confboot=/dev/sdb # The location of the USB Stick (currently)
lba32 # use lba32 addressing (ignore)
compact # boot quickly by loading lots of blocks
# remove when there are problems with booting
prompt # Prompt for user input
timeout=20 # Time to wait before default selection
default="Gentoo-352" # Default selection after timeout
image=/boot/kernel-genkernel-x86_64-3.5.2-gentoo
label="Gentoo-352"
read-only
root=/dev/ram0
append="root=LABEL=GENTOO_USB_ROOT scandelay=5"
initrd=/boot/initramfs-genkernel-x86_64-3.5.2-gentoo
The lines after image are all easy but the append line contains some interesting options.
root=LABEL=GENTOO_USB_ROOTwill use the label of the disk instead of /dev/sdb2 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
scandelayoption, 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 thexhci_hcddriver (built into the kernel or as a module).
Now install LILO (with the configuration) on the USB stick:
(chroot) #liloWarning: /dev/sdb is not on the first disk Warning: The initial RAM disk is too big to fit between the kernel and the 15M-16M memory hole. It will be loaded in the highest memory as though the configuration file specified "large-memory" and it will be assumed that the BIOS supports memory moves above 16M. Added Gentoo-352 ? * 2 warnings were issued.
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[edit | edit source]
syslinux currently only works with ext{2,3,4}, btrfs, ntfs and fat filesystems.
(chroot) #emerge syslinuxPut the boot sector onto the USB stick.
(chroot) #dd bs=440 count=1 conv=notrunc if=/usr/share/syslinux/mbr.bin of=/dev/sdbNow to configure the bootloader.
(chroot) #mkdir /boot/syslinux(chroot) #nano -w /boot/syslinux/syslinux.cfg/boot/syslinux/syslinux.cfgPROMPT 1
TIMEOUT 50
DEFAULT gentoo
LABEL gentoo
LINUX ../kernel-genkernel-x86_64-3.5.2-gentoo
APPEND root=LABEL=GENTOO_USB_ROOT scandelay=3 ro
INITRD ../initramfs-genkernel-x86_64-3.5.2-gentoo
The
ro in the above APPEND line will cause the root partition to be mounted read-only (which is usually preferred on USB sticks). Replace ro with rw to allow the system to be modified from the boot onwards (instead of having the user mount the file system as read-write later).Next install syslinux onto the USB stick. The --device /dev/sdb1 option is not absolutely necessary though.
(chroot) #extlinux --device /dev/sdb1 --install /boot/syslinuxfstab[edit | edit source]
/etc/fstabConfigure the fstab file to work with labelsLABEL=GENTOO_USB_BOOT /boot ext2 noauto,noatime 1 2 LABEL=GENTOO_USB_ROOT / ext2 noatime 0 1 /dev/SWAP none swap sw 0 0 /dev/cdrom /mnt/cdrom auto noauto,ro 0 0 /dev/fd0 /mnt/floppy auto noauto 0 0
Tips and tricks[edit | edit source]
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 /var/tmp to a tmpfs, as compilation will be much quicker that way anyway!