USB Portable Install

Nope, not a live CD variant, but the real thing. Your whole environment in your pocket. And you can boot it on any box, even virtual box. Easier to carry than laptop. And of course, you can still use a part of it as a normal pendrive and use it to transfer files or plug it into multimedia player.

Kernel configuration
Depending on how many drives are detected on boot, USB drive can be named sda, sdb, etc. So if we want to make it work on any machine, without extra per machine configuration, we need to use UUIDs or labels. We are humans, not machines, so let's use labels. In any case we need initramfs for that. Genkernel will do that for us.

Now edit the /etc/genkernel.conf file. I have changed the folowing settings:

/etc/genkernel.conf

And I also added these:

/etc/genkernel.conf

now run genkernel:

and configure the kernel. Select all the hardware you might need. I just went through all the options and select to compile as module wherever possible - less thinking, less time, but longer compile time. Computer time is cheaper than yours. However, there are couple of options that you need to be more careful about. If you want to boot on different x86 processors, then enable generic x86 support. That is what i did:

We also need support for USB mass storage build into kernel:

And if you want to boot in VirtualBox:

It is also a good idea to select some other common chipset, just in case you will want to copy your system back to internal drive someday or if you are using some other VM software that emulates other hardware.

Partitions
If you want to use your device as conventional pendrive, than you need one FAT32 partition. To avoid having problems with stupid devices (like my car radio) let it be the first partition. Make sure that it is the first in partition table (it's name should be /dev/sdx1).

It is also a good idea to have swap space. So make a reasonable sized swap partition, according to your needs and space available. Keep in mind that you can create swap file anytime later if you run out of swap space. Or maybe box on which you are booting the key has its own disk with swap partition (autoswap script).

Obviously, you need a partition for linux. I am a boring guy, so I use ext3.

Check your current partitions usage to get an idea of how big your partitions should be.

For example, let's say we have 16GB key, and we partition it like this:


 * 8GB FAT32 partition labeled as "share"
 * 1GB swap partition labeled as "swap"
 * 7GB root partition labeled as "gentoo"

If you have anything on the key than do a backup, so you can repartition from scratch. Resizing of partitions can be very time-consuming. If you don't have your favourite tool for editing partitions, then try gparted:

Working with gparted should be straightforward. Don't forget to set labels to your partitions.

Move to USB
We will mount current root partition again on /mnt/src, so we also copy the initial contents of /dev and exclude other filesystems mounted elsewhere.

If you have separate /boot or /home partition don't forget to handle that also. Now edit the new fstab:

and replace current root and swap with that:

/mnt/dest/etc/fstab

We also need to install grub to your key. We assume that the key is known to your system as sdb:

Now check the grub.conf file:

You should already have a similar entry which was added by genkernel before. Make sure that root line is correct. You need to set real_root parameter to point to your partition. Also scandelay option is needed for USB key to be detected before booting. Two seconds were enough for me. /mnt/dest/boot/grub/grub.conf

Configure BIOS and boot
Reboot your box and enter BIOS to set boot device. Some BIOSes are a bit tricky to set to boot from USB. On some boxes you need to enable some legacy stuff. On others you find your key beetwen disk drives.

That's it! You can start using your key. However there are some optional tricks coming up to improve your experience.

Backup
It's easy to lose or destroy an USB key. So it is a good idea to have a backup. It's always a good idea to have a backup. I am using rdiff-backup. This way I always have most recent backup available as plain filesystem and all older backups still available with rdiff-backup tool. I have a symlink to the folowing script in /etc/cron.daily/. It backups to a disk in my home box. So it needs to check if it is available (.backup_sign). After backup it sends summary via email.

/etc/cron.daily/backup.cron

Don't forget to make script executable:

Boot with non-USB BIOS
It might seem obvious, but I couldn't think of that at the first moment. You need an internal disk that doesn't even have to work perfectly. You only need to transfer grub files to this disk. So if you connect this disk to some other box that you boot from your key this would look like (assuming that this disk is named sdc):

Now put that drive into problematic box and boot from it. Keep in mind that if you update kernel or grub on your USB key, this does not affect that drive.

Portage performance tips
USB keys has way faster access times comparing to mechanical disks. This improves boot and load times (~10% according to my tests). Sequential read/write is also fine (up to 30MB/s). But dealing with lots of small files is disastrous. And I don't have a no-name key. It's a 32GB OCZ Rally2. For example, when syncing portage or emerging packages, CPU is waiting for I/O nearly all the time. It is not even possible to surf smoothly since browser constantly writes some information to disk.

I was playing with /proc/sys/vm/* but couldn't get much out of that. I have also seen suggestions to use elevator=noop as I/O scheduler, but according to my tests anticipatory scheduler still gives best results. So I gave up tweaking.

I usually emerge on box at home, which already have disk for backup. So I put /var/tmp/portage, /usr/portage and /usr/src onto this disk.

You may also consider using tmpfs for /var/tmp/portage.