A better way to create a customized USB drive with Ubuntu Live on it

published Jan 09, 2009, last modified Jun 26, 2013

Got a thumb USB drive or an SD card, and you'd like to put Linux on it? Did the other HOWTOs fail you? This is the real deal, through and through. This trick also works out-of-the-box with Kubuntu and other Ubuntu variants as well. Moreover, you can adapt it with very few changes to any other live CD Linux distributions out there.

Note that these instructions have been superseded by the Portable Linux creator.  Visit that link to find out how to create your own live Linux distribution in minutes, and how to run memtest86+ or MS-DOS from your USB stick easily as well.

Many modern Linux distributions (Ubuntu included) provide a Live CD that you can download, burn and run directly without installing Linux on your computer.

But what if your computer doesn't have a working CD drive?  Or perhaps you'd like to carry your Linux around in your pocket?  Well, no problem.  Ubuntu and many other distributions can also boot and install themselves from an USB thumb drive or an SD card (henceforth, for simplicity's sake, "USB drive").  All you need is a drive that is 1GB or bigger.

Problem is, not everybody can use the famous liveusb program to create bootable live USB drives.  Some of us are using Fedora on our workstations.  Others  might actually want to run Knoppix or another live distribution, not Ubuntu.  Yet others might not like the way liveusb partitions the disk space in your USB drive, or might want extra functionality not provided by it.

So we'll cover those bases, using Ubuntu as the distribution (and tips for other distributions).

After following it:

  1. Your USB drive will allow you to boot Live Ubuntu Linux on any computer you plug it into.  Just reboot, select USB boot, and off you go.
  2. Any customizations or files you create or change within your Linux will be preserved even after rebooting the computer or using the USB drive in another machine.
  3. You will be able to use your USB drive to install your Linux system on your friends' computers, or (if you know how) even use it to recover Linux installations that don't boot correctly anymore.
  4. (With some extra steps) you will even be able to use the drive to boot Memtest+ (to test hardware) and many other legacy programs such as MS-DOS (for those pesky BIOS updates).
  5. You will have learned valuable Linux knowledge.
  6. Best thing is, only a small portion of your USB drive will be used for this tutorial, so you get to keep the rest to do what you normally do: save music, share movies, move files around, either in Windows or in Linux.

So here's what you need to do, explained step by step.  On each step, colored insets will show you example console commands you can run to accomplish the step; you're free to do each step in the way you feel the most comfortable, and referring to the commands as merely guidelines.

Creating the bootable USB drive

Get the utilities we'll be using in this tutorial

Install the following programs (that might not already be installed) on your computer:

  1. dosfstools
  2. cfdisk

Use your distribution's software installation tool do to this job.

Get Ubuntu (or your favorite Linux distribution)

What you need to do first is get Ubuntu.  You can use either a CD or an ISO CD image downloaded from the Internet.

If you have an ISO image ready, great.  Verify the image using the MD5 sums generally available on the place you downloaded it -- trust me, you don't want to burn a malfunctioning image on your USB drive, it happened to me and it's very frustrating.    If, conversely, you have a CD, extract an ISO image from it:

This and all subsequent examples assume that your distribution uses sudo to run administrative commands.  If your distribution does not use sudo, become an administrator with the command su, then ignore the sudo prefix in all examples that follow.

sudo dd if=/dev/cdrom of=/path/to/iso/image.iso

Adjust the path of the of= parameter to point to the directory where you want to save the ISO image.  For the rest of this tutorial, /path/to/iso/image.iso will represent the path to your ISO image.

Plug your USB drive in

Now plug your USB drive and ensure it is not mounted.  The system log (accessible by issuing the command dmesg) will tell you what the device file corresponding to the USB drive is (it is usually /dev/sdb or /dev/sdc).  For the purposes of this tutorial, we'll assume that the device file is named /dev/sdc.

Repartition your USB drive

Be very, very careful to specify the correct device to any partitioning tool -- a mistake can cost you all your data.  And don't forget to back up the files in your USB drive -- this procedure will destroy any files present on the drive.

What we'll do now is simple.  We'll create two partitions:

  1. The first partition will contain a regular Windows FAT32 filesystem where you can put whatever files you want, wherever you go.  It will also store whatever customizations you perform in your Linux installation.
  2. The second partition will hold the contents of your CD's ISO image.  It will be hidden from view.

So the real trick here is to get the second partition to be just a tiny bit larger than the ISO image (which is just under 700 MB in Ubuntu). We'll use cfdisk to do this job.

sudo cfdisk /dev/sdc

cfdisk is pretty easy to use: you select partitions by using the up/down arrows, and you select functions by using the left/right arrows plus the ENTER key.  Here's what you'll do:

  1. Delete any existing partitions.
  2. Create a new, primary partition.  Make it the entire size of your USB drive minus 730 MB or so.
  3. Create a new, primary partition, covering the 730 MB you left out.
  4. Make the first partition bootable.
  5. Change the type of the first partition to C (Windows FAT32).
  6. Write the new partition table.
  7. Quit the program.

After doing this, your USB drive will be divided in two partitions.  One partition will be visible on My Computer, and you'll be able to store files just fine.  The other one will contain the Linux live CD, and will be invisible.

If you did it right, you should see no errors and the partition table in your USB drive has been re-read.  If not (probably because you forgot to unmount any old partitions in the drive), unplug it, plug it again, and check that the device file name did not change.  As long as no partitions on your USB drive are mounted, you do not need to reboot or unplug your USB drive for the partition table to be re-read by Linux.

Linux automatically names partitions with the device file name and a number at the end, so /dev/sdc1 represents the first partition we created, and /dev/sdc2 represents the second, hidden one.

Verify that your USB drive does not have bad blocks

Before you commit to using the USB drive, you need to know whether the USB drive has sectors that have gone bad -- a common occurrence in USB drives.  If a bad block damages the area where the live CD image is stored, your USB drive may malfunction or cause you to lose data.

sudo badblocks /dev/sdc2

This command will tell you if your USB drive is not usable as a live USB drive.

Format the newly created partition for Windows to use

Now it's time to create a brand new, Windows file system on the recently created partition (the first one) in your USB drive.

sudo mkfs.vfat -F 32 -c /dev/sdc1
sudo dosfslabel /dev/sdc1 UBUNTULIVE

This creates the FAT32 file system and labels it UBUNTULIVE.  The formatting process will take some time because the command tries to detect bad sectors and mark them as unusable.

Mount the Windows partition

sudo mkdir -p /media/USBDRIVE
sudo mount /dev/sdc1 /media/USBDRIVE

This mounts your USB drive in the folder /media/USBDRIVE.  From now on, we'll assume that's where it's mounted.

Install GRUB on the Windows partition of your USB drive

GRUB is the GRand Unified Bootloader, and its job is to pick up from where the BIOS of your computer left off and continue loading the operating system.  We'll use GRUB to bootstrap Ubuntu.  The reason we use GRUB is because GRUB is much more resilient and easier to set up than other boot managers like Syslinux.

sudo grub-install --no-floppy --root-directory=/media/USBDRIVE /dev/sdc

This command installs GRUB on the /media/USBDRIVE directory where your USB drive is mounted -- more accurately, on a boot/grub subdirectory inside it.

Copy the kernel and initial RAM disk from the ISO image to your GRUB directory

Now we're going to copy two key files from your ISO image to the boot/grub directory in the Windows partition of your USB drive (not the one in the root of your Linux file system). GRUB needs to load these two files successfully; since apparently GRUB cannot "see" what's within an ISO image, we're simply going to copy these files from the ISO to the GRUB directory.

sudo mkdir -p /mnt/isoimage
sudo mount -o loop /path/to/iso/image.iso /mnt/isoimage
sudo cp /mnt/isoimage/casper/{vmlinuz,initrd.gz} /media/USBDRIVE/boot/
sudo umount /mnt/isoimage
sudo rmdir /mnt/isoimage

This example assumes your ISO image is Ubuntu or Kubuntu -- in those ISO images, the kernel and RAM disk are in the /casper directory within the disc.  You may adjust accordingly for the live distribution you're going to run.

Now you should have two new files in the GRUB directory, named vmlinuz and initrd.gz.

Create a persistent file system to store data and customizations

Now we're going to create a small file.  This file will contain a Linux file system which will be automatically used by Ubuntu and other similar distributions to store your data, configuration, updated and extra software you install on it.  This step is not required if you do not want your data and customizations be remembered after rebooting your computer.

dd if=/dev/zero of=/media/USBDRIVE/casper-rw bs=1M count=256
mkfs.ext3 -F /media/USBDRIVE/casper-rw

This example creates a file 256 MB large, named casper-rw, in the Windows partition of your USB drive.  Adjust the number 256 to make it bigger or smaller (try not to make it smaller, though -- 256 MB is pretty small).

Write the ISO image to the hidden partition

Now we need to make a byte-by-byte copy of your Ubuntu ISO image to the hidden partition (the second one we created on the step before this one).  For that, we can use the dd command:

sudo dd if=/path/to/iso/image.iso of=/dev/sdc2

Be very careful when running the dd command, and double-check the of= parameter before hitting ENTER.  One typo can cost you all of your data.

This will take a long time.  If everything went well, you will be returned to the prompt with a small message noting how many bytes were copied in total, but no error messages.

Create a nice GRUB boot menu

Because GRUB is so flexible, you can use any live CD distribution instead of Ubuntu on your USB drive.

Refer to your favorite distribution's documentation to find out where you may obtain the appropriate vmlinuz and initrd.gz files (usually in the ISO image of your favorite distribution) and what parameters to specify in the kernel line of the GRUB configuration file.

Once you've done that, you can create the GRUB boot menu that will let you select and boot Ubuntu when the computer boots up.  We're going to create a file named grub.conf within the /media/USBDRIVE/boot/grub directory we created earlier using grub-install.

sudo nano /media/USBDRIVE/boot/grub/grub.conf

That command will launch the nano text editor ready to edit the file.  Put the following contents inside that brand new file:

default         0
timeout         10

title           Ubuntu (Live)
root            (hd0,0)
kernel          /boot/vmlinuz boot=casper file=/preseed/ubuntu.seed persistent
initrd          /boot/initrd.gz

If you're using a Kubuntu image, you need to make a small tweak: instead of ubuntu.seed, type kubuntu.seed.  Other Ubuntu flavors may need similar tweaks.

Save the file and exit the text editor (if you're using nano, hit Ctrl+X, answer Yes to the answer to save the file, hit ENTER, and you're done).

Unmount the Windows partition of your USB drive

Once you're done with the file, you can safely unmount the partition containing those files.  Close any text editors, terminals or folder windows you might have open with the contents of the USB drive, then run:

sudo umount /media/USBDRIVE
sudo rmdir /media/USBDRIVE

That's it.  Now your USB drive has only one folder named boot inside it, with some pretty important files.  Don't touch them from now on, and don't use a disk defragmenter on your USB drive either.

Sync up, then unplug

sync writes any pending data to all the disk drives in your computer.  This guarantees that everything is safely stored on disk before unplugging any device.  Normally, when you click the Eject icon on your desktop, sync is run for you automatically.

Now that you've done all of these steps, run:

sync

Once sync returns you to the prompt, it's safe to unplug the USB drive.

Now you're done.

Using your USB drive

Starting up your Linux system

Wherever you go, all you have to do (in this order) is:

Some BIOSes let you press a key (usually F8) to give you a boot menu.  Using this boot menu, you can temporarily choose to boot from your USB drive without changing any BIOS configuration permanently.
  1. Plug your USB drive in.
  2. Reboot the computer.
  3. As soon as the computer starts up, repeatedly press DEL, F1, F2 or F10 until you get to the BIOS configuration menu.  The correct key is usually briefly flashed across the screen as soon as the computer powers up.
  4. On the BIOS configuration menu, locate the Boot order options (usually under a Boot or an Advanced options menu)
  5. Select as first boot device your USB drive.  Ssometimes you need to select Hard Disk, and within Hard Disk, your USB flash or thumb drive.  On other BIOSes, you might need to first select Hard Disk, then go to a separate submenu to select which hard disk.
  6. Save the changes and reboot.

That's it.  If you did it right, next time the computer boots, your USB drive will be used as a "hard disk", and your Live Linux will come to life right away.

Accessing your Windows partition from within your Linux system

If you want to access the files stored on your Windows partition on your Linux system:

  1. Add the following commands to the /etc/rc.local file (before the exit 0 line):
    /sbin/losetup /dev/loop2 /dev/disk/by-label/UBUNTULIVE
    mkdir -p /media/windows
    mount -t vfat -o umask=000,noatime /dev/loop2 /media/windows
    
  2. reboot

From this point on, you can simply browse to /media/windows to see your files.  The partition mounts automatically on boot.

Care and feeding of your USB drive

Upgrading your USB drive to the latest Linux

We've got, what, six months between Linux releases?  Well, you don't want to be caught running an old Linux distribution, do you?

Fortunately, upgrading is extremely easy.  All you have to do is:

  1. download the latest Linux release, then
  2. re-run the step named Write the ISO image to the hidden partition, then
  3. re-run the step named Copy the kernel and initial RAM disk from the ISO image to your GRUB directory, then

If you've already performed software installations or upgrades, you might want to remove them from your persistent casper-rw file, because they will overshadow files in your new Linux distribution.  To do that, mount the file (using the loopback driver) on an installed Linux system, then remove /var, /usr, /bin, /sbin and /lib from it.  Note that you will have to reinstall any third-party packages that do not ship with your new Linux distribution.

Periodic file system checks

The following tests you need to run as an administrator on a Linux computer every once in a while, or if you experience malfunctions while using your USB drive.  You can't run these tests while running the live distribution on the USB drive.

The first test checks whether the area where the live CD is stored has gone bad and is no longer usable:

badblocks /dev/sdc2

The second test checks and repairs bad sectors in the Windows partition where you store files and the persistent file system is located:

dosfsck -atv /dev/sdc1

The third test checks whether there is any corruption in the persistent file system itself (if you are using this feature):

mkdir -p /media/USBDRIVE
mount /dev/sdc1 /media/USBDRIVE
dosfsck -atv /media/USBDRIVE/casper-rw
umount /dev/sdc1
rmdir /media/USBDRIVE

Dealing with low disk space on your persistent file system

You might find that the 128 MB we assigned earlier for your persistent file system turn out to be too small.  You can fix this by making the file larger without deleting your customizations.

  1. Plug the USB drive in a computer that has Linux, but do not boot from it.  Mount the Windows partition on your USB drive.  Become an administrator to run the subsequent steps.
  2. Create an empty temporary file using the dd command:
    dd if=/dev/zero of=/tmp/tempfile bs=1M count=256
    This example command creates a 256 MB file.  That's a bit small, but it should suffice to install a few programs -- like multimedia codecs and VLC in case you want to roll around with an all-purpose multimedia machine.
  3. Now tack that file onto the end of your casper-rw persistent file  You can do that with the command:
    cat /tmp/tempfile >> /media/USBDRIVE/casper-rw
  4. Finally, enlarge the file system to encompass the added disk space:
    e2fsck -f /media/USBDRIVE/casper-rw
    resize2fs /media/USBDRIVE/casper-rw
  5. Now unmount your USB drive.