Weekend Project: Create a Live USB Key Linux Distribution

1001

Until world domination is complete, you simply cannot count on Linux being available everywhere you travel. Thus, when you’re vacationing or on a business trip and need access to a Linux environment — or get the inevitable PC troubleshooting help request — you’ll have to have a bootable distribution with you. A variety of tools can help you tailor a live USB distro to your liking, including data persistence and encryption, so grab an unused USB flash drive from the desk drawer and make exactly what you need this weekend.

The first question you need to answer for yourself is what distribution you wish to use as the basis for your live USB key. Most of the popular desktop distributions provide live CD images for testing or installation purposes, and include a utility to install such an image onto other media, including external hard disk storage or a flash drive. With the rise in popularity of optical-drive-less netbooks and tablets, providing USB as an installation path guarantees that the distributions will continue to make booting from flash a priority. If you are simply interested in building a portable environment like the one you use at home, you should start by looking for such a utility within your distro.

Branching out a little, there are a handful of good multi-distribution live USB creation utilities you should consider if, for some reason, your distribution of choice does not provide the options you need. It is also possible to install a bootable image onto a flash drive manually, and there are several good step-by-step guides to help you through this process. Last but certainly not least, it is important to know what the options are for creating a bootable Linux USB key from within Windows or another operating system — if you find yourself in that situation, familiarity with only the Linux-based tools places you in an uncomfortable chicken-and-egg situation.

 

Distribution Tools

The distribution-specific tools all vary slightly, both in how they work and in what they require of the installation medium. In most cases, you will need to download the live CD image ahead of time in order to copy it to a USB stick or flash card, and unless otherwise specified, installing a bootable Linux environment on a USB key will mean erasing its existing contents.

The openSUSE utility is called SUSE Studio Image Writer. It is a GUI tool that allows you to drag-and-drop an ISO image into its window, and it will automatically partition and install a bootable live image onto any attached USB storage key. The distribution recommends 1GB minimum for the latest release, 11.3.

Fedora’s utility is called Fedora LiveUSB Creator. The distro recommends 1GB minimum of unused storage, formatted as VFAT. The LiveUSB Creator will install an image non-destructively on a key that already has files on it, as long as there is sufficient free space. The GUI can also download a supported live CD ISO image for you in a background process, although you may want to perform the download manually if you intend to build from a non-standard release, such as an Alpha or a Rawhide testing build. It also allows you to set up a persistent storage area on the key in whatever size you choose from the remaining available space.

Ubuntu’s utility (also available in Kubuntu and derivatives) is called Startup Disk Creator. It requires you to download the desired ISO image before getting started, but it does give you several install-time options. You can choose to install the image non-destructively or to re-format the USB key, and you can select between creating a persistent storage area or leaving it off entirely.

The Debian project has a GUI tool called Debian Live Magic, with which you can create a bootable USB key using a wide array of options, including distribution (stable, unstable, or testing), hardware architecture, environment (including non-X environments), localization, and package selection. Unlike the other tools already discussed, Live Magic does not simply copy an ISO image onto the USB drive and make it bootable; it downloads current Debian packages and creates a custom virtual filesystem. This takes considerably longer, but is very flexible.

Knoppix, of course, is a distribution designed to run as a live CD out-of-the-box. It has a USB installation tool called flash-knoppix that can install the current live CD image onto a flash drive. It requires FAT formatted media, and does not do repartitioning. As you might expect, however, Knoppix’s built-in persistent storage overlay works automatically.

Multi-distro Tools

The above tools certainly do not cover everyone, and they don’t really address some of the special-purpose needs that you may be interested in building a bootable live USB key to handle.

The Universal Netboot Installer (UNetbootin) can install a range of different Linux-based systems onto USB storage. The list includes popular distributions (including those listed in the previous section, as well as CentOS, Mandriva, Slackware, Gentoo, and more), various BSD OSes, and any live CD ISO image you have on hand. For the pre-fab distribution options, the application downloads an ISO image automatically.

It also provides a way to build a completely custom installation by uploading a kernel image, initrd file, and specifying command-line boot options. Obviously, in order to have a complete system, you will also need to install the rest of the distribution as well, but this feature of UNetbootin does also enable you to install multiple kernels onto a single USB key, which could come in handy if you are building a hardware diagnosis image and encounter driver woes. There is also a derivative of UNetbootin called FUSBi that, for philosophical reasons, restricts the pre-selected distro image list to only those projects endorsed by the Free Software Foundation.

Multiboot LiveUSB is another Linux-based application for installing a variety of different distributions onto USB storage, but with one important difference. Unlike UNetbootin, Multiboot’s main goal is to install multiple bootable images onto a single drive. You can use it to install any combination of distributions from its supported list, which currently includes Ubuntu, Fedora, Debian, MeeGo, Gentoo, and many, many more, including some helpful utility distros such as Clonezilla and Ophcrack Live. Persistence is only available for a few of these, however.

Finally, Portable Linux is a basic, no-frills live USB disk creator. It is approximately the equivalent of one of the drag-and-drop tools from the distribution-specific list above, but places no requirements on what ISO images are supported. UNetbootin and Multiboot offer more options, including multi-distro-installation, but Portable Linux is a good backup option if neither of the above work out for you. At least with USB storage, unlike optical media, you can afford to experiment a few times.

Windows and Web-based Tools

If you get stuck on a Windows machine and need to build a live USB key, you have fewer options. UNetbootin, mentioned above, has a Windows build. The same features are available as on the Linux version, and the same distributions are supported.

The Linux Live USB Creator (LiLi) is a Windows-only GUI tool that allows you to create live USB images from a fixed list of supported distro ISOs that includes several generations of Ubuntu, Debian, Fedora, PCLinuxOS, and Gentoo distributions, including derivatives, and several security and rescue CD distributions. It supports persistence and non-destructive installation, but requires FAT or FAT32 as the drive filesystem. One unusual feature is that LiLi can “hide” files from Windows by setting the system (S) and hidden (H) filesystem attributes. They will, of course, remain visible to other operating systems.

Mandriva offers its own Windows live USB creator called Mandriva Seed, released alongside the ISOs for each new version of Mandriva. It is designed to work with the latest Mandriva release, although any Linux ISO should be supported.

Finally, the Slax project is a special case. The distribution itself is based on Slackware, but Slax provides a web-based interface for configuring and building a live USB image. The online build tool breaks the distribution into package modules, including the OS core, X, KDE, KOffice, development tools, and around fifteen different application bundles. As you build the configuration you want, the site gives you a running total of how much drive space your ISO will require.

The Cowboy Way

As you can see, there is an abundance of tools to help you simply load an ISO image onto a USB stick and make it bootable. But if you want to bypass the point-and-drool process and completely customize the software you install onto your drive, you can do that, too.

Naturally, if you need to partition and format the USB key before starting, you can use the standard fdisk and mkfs tools on a flash drive just like you would any other storage medium. You should use a FAT filesystem on bootable USB keys, and therefore create the filesystem with mkdisfs -F 16 /dev/sde1, substituting the appropriate partition name. Note that for drives of 4GB capacity or higher, you will have to use FAT32, a la mkdisfs -F 32 /dev/sde1.

Underneath the hood, what the GUI live USB-creation tools do is copy the raw contents of the ISO file to the USB device. You can do this manually with dd, for example, by running dd if=/home/nate/NateLinux_1.0.32bit.iso of=/dev/sde bs=8M. In that command, if specifies the input file, of specifies the device name (note that this is not the partition name, which would be /dev/sde1 or something similar), and bs specifies a block size. This raw copy preserves low-level information from the ISO — including the Master Boot Record (MBR) that makes the volume bootable — that would be overlooked by simply copying file contents from one device to another.

Where different distributions vary is in what additional steps you need to take to ensure that the bootloader copied from the CD ISO will work when transferred to the USB stick. This depends on the bootloader (Syslinux, GRUB, GRUB2, etc.) and choices made by the distribution. The best plan of attack is to consult a distribution-specific guide. GRUB is very forgiving of different configuration options; essentially if you can place a vmlinuz file somewhere on the drive, you can probably get it to boot. Syslinux is not so kind. OpenSUSE, Fedora, Ubuntu, and Gentoo all provide detailed guides with such low-level instructions.

Whichever approach you take, the USB boot options supported by the host machine’s BIOS can throw you for a loop. PCs have been able to boot off of USB media for several generations, but some BIOSes can have settings that keep the computer from looking at USB media — or at least from looking at it before hard disks. Worse yet, Intel-based Mac hardware may not be able to boot Linux from USB media at all, due to discrepancies between Apple’s EFI firmware and “legacy” mode BIOS.

Understanding Persistence and Encryption Options

Data persistence of live USB distributions is accomplished by creating a second filesystem that is “stacked” on top of the existing (usually compressed) filesystem with Unionfs. The second, writable filesystem is not simply mounted into the first, they are overlayed on top of one another — any file that exists in the original filesystem can be changed by a new version saved in the second. This allows you not only to store data files in your home directory, but to alter system configuration files anywhere on the filesystem. It is a slick idea, but it comes with two big side effects.

The first is that your flash media gets exposed to far more write cycles than it normally would when used just for file transport. This can significantly shorten the lifespan of the device. The second is that you cannot use an encrypting filesystem such as eCryptfs to secure your home directory on the key: eCryptfs cannot (currently) be stacked.

Thus, if you need to secure some data on your live USB stick, you have to turn to alternative tools like Truecrypt, which creates a large encrypted file that you use as a container for other files, and decrypt after logging in. It is not an ideal solution, perhaps, but then again you are more likely to accidentally misplace a USB key than an internal hard drive, so caution is the word of the day.

But even if you do not use encryption or persistence to achieve secure, portable Linux nirvana, a live USB key is a better totable computing environment than a live CD for a number of reasons. It is less fragile, smaller, fits into almost any device (unlike “business card” CDs), and on top of everything else, its read times are significantly faster.