TMCnet News

How To Run Linux From A USB Flash Drive
[May 21, 2008]

How To Run Linux From A USB Flash Drive


(TechwebNews.com Via Acquire Media NewsEdge) From Installation CD To Bootable Flash Drive

Most Linux distributions, such as Ubuntu, are capable of installing directly to a USB drive as if it were just another hard drive, but some distributions treat USB flash drives as a slightly special case. DSL, for instance, has an installer specifically for creating a USB-drive installation of the OS. Puppy can also install itself directly to a flash drive through the built-in Universal Installer utility. However, the default options did not work in my case; I had to explicitly use the "SysLinux" option in its installer menu to make the flash drive bootable.



In all cases, a USB flash drive will be mounted and recognized as a virtual SCSI device, such as /dev/sda or /dev/sdb. If you're performing the installation on a system that already has a hard drive present, pay close attention to the devices listed in the partition manager, and make sure you're installing to the right device. Otherwise you might accidentally wipe the hard drive -- although the device size should be a big tipoff! Also make sure that the installer makes the target device bootable and writes a proper master boot record to the drive, although most of the time this should happen automatically.

From Live .ISO To Bootable Flash Drive


Open source QEMU lets you run Linux in a virtual machine on top of another OS.

(click for image gallery)

If the edition of Linux you're eyeing has a live CD distribution (and most of them do), it's also possible to mount the .ISO for the live CD on a flash drive and run it from there. The advantages of doing this should be plain: it's a lot faster to run a live distribution from a flash drive than from a CD, and almost any machine that can boot from a CD can also boot from a flash drive as well.

The disadvantage of this type of setup is that, by default, a live CD distribution is generally not persistent. Any changes made to the system will be lost the next time you reboot, unless you make specific provisions to save user data. Some live distributions allow this, but it's not always guaranteed. On the plus side, if you want a live distribution that doesn't leave behind any traces -- for instance, for secure browsing or system recovery -- this is a perfect choice.

The folks at PenDriveLinux have created scripts to automate the process for a number of popular distributions such as Ubuntu 8.04 and PCLinuxOS. Their trick (and it's a clever one!) is to use the open-source 7-Zip archiving application to browse the .ISO and extract key files from it to allow SysLinux to make the drive bootable.

The same basic techniques can be applied to any Linux that runs from the ISOLINUX live file system. To that end, if you want to make a bootable flash drive from a live CD distribution's .ISO, follow these steps:

Format your flash drive. You may need to first use DISKPART CLEAN as described above. For the sake of compatibility, use FAT or FAT32 as the file system. (FAT seems to work best.)

Use 7-Zip or another .ISO reading tool to unpack the entire contents of the .ISO onto the flash drive. Make sure the root directory of the .ISO corresponds to the root directory of the flash drive; don't unpack the contents into a subfolder on the flash drive.

Use SYSLINUX -ma <driveletter>: (in an admin console, if you're using Vista) to write the system files, with <driveletter> being the drive letter for the flash drive. If you're running SysLinux from something other than Windows, the -ma switches aren't needed.

With many live distributions, there will be a file in the root directory of the drive labeled ISOLINUX.CFG. This may need to be renamed to SYSLINUX.CFG to make things work correctly.

Unmount the flash drive and try to boot it on your target system.

Some live Linux distributions will have a \boot directory off the root directory in the .ISO, usually with an \isolinux directory in it as well. For such distributions, you'll need to move the contents of \boot\isolinux to the root directory of the flash drive before renaming files and then applying SysLinux. The order of the events is crucial.

Most of the time, Linux is run from either an installation on a hard drive or a live CD/DVD distribution. The first is fast, but not very portable; the second can be run anywhere you have a computer and a CD drive with boot access, but typically isn't very fast. Over the last few years, though, we've seen the emergence of something that combines the speed of a hard drive install with the convenience of a live CD: running Linux from a USB flash drive.

While flash memory prices are still high enough that a flash-based 100-GB hard drive is still way out of the realm of what would be affordable for most people, 2-GB and 4-GB flash drives are cheap enough to make a streamlined Linux installation practical.

You won't be able to pack your MP3 collection and your gigs of vacation photos to go with you -- at least, not yet! -- but you will be able to run Linux with most of the applications you need, and bring the more crucial of your data with you as well. What's more, there are ways to run Linux from a flash drive that don't even require an OS reboot, especially if you're running Windows.

What You Need

Puppy Linux's universal installer supports everything from flash drives to compact flash cards.

(click for image gallery)

To install a given Linux distribution on a flash drive and run it, you'll need the following things:

A Linux distribution that supports installing to and running from USB. The good news is that just about any recent distribution you could name will do this, although the exact steps for getting it running may vary between distros. Some distributions have the ability to do this natively.

A USB flash drive, or other USB-attached external media. All flash drives are not created equally, however, which is something I'll be discussing.

A computer that can boot from a USB port. Again, this can be slightly thorny depending on the PC -- most current PCs support booting from USB, but there are exceptions and quirks.

If you're setting up a Linux installation that will run from within Windows itself, then having USB boot support isn't required, just access to storage devices mounted from USB ports. Note that machines in some environments -- for instance, in a corporate setting -- may have administrative controls in place to prevent users from mounting storage devices on USB ports.

Which Distribution?

Your choice of Linux distribution is going to depend on a couple of things: what size flash drive you're using, which Linux distros you're familiar/comfortable with, and what features you want to support.

The size of the drive is a factor because of the amount of space the distribution will need, but that may actually be the least of your worries, depending on your budget. Flash drives have plummeted in price; just this past week, I was able to pick up a 1-GB drive for a mere $10, and a decently fast one at that. A 1-GB drive can be used to hold the ISO for a live distribution, with some room left over for persistent user data in a dedicated partition.

Most of the time you'll want to start with a distribution that is designed to run small and light. Two of my favorites in this regard are DamnSmallLinux -- probably the grand-daddy of all "tiny" distributions -- and Puppy Linux, which jams an unbelievable amount of day-to-day functionality into only a couple of hundred megabytes.

Generally, you'll need 2 to 4 GB of space for an actual fully functional installation of a distribution on the order of Ubuntu or Fedora, and if there isn't enough room on the drive for the installation, you'll generally get a warning from the installer. If you have the space for a big distro, use one, as you'll have that many more apps and that much more feature support right out of the box.

Which Flash Drive?

Flash drives may all seem alike from the outside, but believe me, they're not -- the differences in speeds between any two drives can be startling. What's even more frustrating is that up until very recently there wasn't much word from the manufacturers about the relative speeds of different flash drives.

Thankfully, this has changed, and drive manufacturers are getting savvier about including speed statistics with their drives. A drive that's rated for "100x" speed or higher should work nicely, and most any drive that claims support for Vista's ReadyBoost feature should also work.

One thing that most manufactures have not made terribly clear is the drive's performance with regard to sequential vs. random access. You might think there shouldn't be a difference -- after all, the whole idea with a flash drive is that every byte on the drive has the same access time, right? Not true. Sometimes the manufacturer will mix memory speeds as a cost-saving measure, using a small block of fast flash memory in conjunction with a large block of slower memory. This means heavy variations in performance depending on what's actually being done on the drive. To that end, you might see major differences in performance between two drives that have identical or near-identical manufacturer's ratings.

DamnSmallLinux's "pen drive" installer.

(click for image gallery)

Another interesting thing to note is that flash drives aren't the only removable media that may work -- compact flash cards or digital cameras and music players that can mount as mass storage devices will also work. Again, whether or not Linux or your PC will support booting and running from such a device will depend on how the device advertises itself to the computer, and whether or not the Linux distribution you're using has drivers set up to handle it. Puppy, for instance, can boot from compact flash cards and even has an installer option to specifically allow it.

Enable USB Boot Support

Most computers shipped within the last couple of years will support booting from a USB device. That said, this isn't always enabled by default, and it isn't always enabled for all USB ports on the system.

One of the first places to look for information about USB boot support, apart from the manufacturer's own specifications, is the system BIOS. In my own Dell XPS, for instance, the default USB handling option is to enable booting from attached USB devices -- but I need to press F12 at boot time to select something other than the default boot device, normally the hard drive.

Another important thing is that some of the USB ports in the XPS can be manually disabled from being polled for boot devices. This is done so that you can attach an external USB drive (whether flash or an actual hard drive), and leave it attached between reboots without worrying about it hijacking the computer's boot sequence.

The folks at the PenDriveLinux Web site -- a fantastic resource for all things related to running Linux on USB -- have a quick way to test USB boot support on a given USB drive and computer. They use the SysLinux utility to make the drive bootable and add a copy of Memtest86+ a quick way to see if boot support works. Note that if you're doing this on Vista, open an admin command prompt to run the makeboot.bat file or it won't work.

SysLinux is used widely to make flash drives bootable for Linux distributions, so it's a tool that we'll be coming back to. Note that if you get a "boot error" or other error message when you try to boot a drive processed with SysLinux, you may need to run the DISKPART CLEAN command on the disk (again, from an admin command line) to remove any previous master boot record information, reformat the drive, and then try again.

QEMU Linux

Some readers are probably familiar with using a virtual machine to run Linux on top of another operating system -- usually as a way to try out a given Linux distribution within Windows without creating a dual-boot scenario.

The same thing can be accomplished with a flash-drive edition of Linux, courtesy of an open source machine emulation package named QEMU.

It's small enough to fit on most flash drives along with whatever distribution you're using and can be started by executing a simple batch file. You'll even have networking support courtesy of whatever network is running on the host. Here's how to do this.

MokaFive lets you create bootable Flash drives that run any OS in emulation.

(click for image gallery)

Format the flash drive as FAT or FAT32 (for the sake of compatibility).

Copy the .ISO file you want to use to the root directory of the flash drive.

Unpack the QEMU executable into a directory named \QEMU.

Use the following command line to run QEMU from the root of the drive (you can turn this into a batch file):

.\qemu\qemu.exe -L .\qemu -cdrom <name_of_ISO>

There are a number of other options that can be passed. If you run QEMU without any command-line switches you'll see the full range of options available, so you can tune it to your needs.

Run QEMU from within Windows. The emulated machine will appear in a Window and you can free the cursor by pressing the left Ctrl+Alt keys.

Keep in mind you're not going to see the kind of performance you'd get if you booted the system natively. QEMU does have a Windows-specific driver to allow some performance acceleration, but you can run it without that as well if speed isn't a priority.

If you want to create persistent storage on the flash drive, there are a couple of ways to do this. QEMU lets you mount any file as a hard drive, so one possible trick is just to create a large file on the flash drive that will hold your user data, use the -hd QEMU option to mount it as a hard drive, format the drive from within your live Linux session, and store your user data there between sessions.

The second option is more or less the same as the first, except that instead of just storing data on that drive, you actually install Linux on it and then set QEMU to boot the hard disk image instead of the .ISO. Note that the best way to do this would probably be to prep all the files on your PC first, then copy the hard disk image and the QEMU executables to the flash drive.

Finally, the folks at PenDriveLinux have created a package that you can use which contains QEMU and a batch file with some common options. The batch file can be customized with the command-line options you want to use, and you can make any other changes you see fit as described above.

MokaFive

Yet another way to run a Linux installation -- or, in fact, most any operating system -- from a flash drive is through MokaFive. This is a commercial application that lets you package virtual machines for redistribution across a network or -- you guessed it -- a flash drive. MokaFive uses VMware to perform machine emulation, which generally runs better than QEMU out of the box, and also allows for some fairly sophisticated lockdown techniques as well. The basic edition of MokaFive is free (although not open source), but will do nicely for this kind of work. You can even access hardware devices on the host computer, like scanners and printers (provided you have permission to do so).

The MokaFive toolkit includes the BareMetal player, a tool which lets you install a MokaFive VM to a flash drive, boot from it, and run it natively. A device with at least 2 GB of storage has to be available to make this work. Note that because the BareMetal player is protected against tampering, the only way to get a virtual machine into it is by loading one in through the BareMetal player's own interface.

Buy Linux Pre-Loaded

There's been speculation that you might eventually buy music on flash drives, and while that hasn't panned out the way conventional MP3 downloads have (barring the odd Nine Inch Nails flash drive left somewhere surreptitiously!), some Linux distributions have started to turn up on flash drives as preloads. Dragon Technology offers Ubuntu on a 4-GB flash drive for ?21.95 (about U.S. $43), and the LinuxUK site has a range of USB Linux products at various prices. I've gone back and forth about whether or not we'll see software products routinely provided on flash drives -- the raw cost versus plain old CDs or DVDs is a lot higher, but in my mind the added versatility of the flash drive makes up for it.

Copyright ? 2008 CMP Media LLC

[ Back To TMCnet.com's Homepage ]