Preparing the USB drive

Making partitions

Caution: Be careful using any disk utility to partition or format dives as a small error could damage your operating system or overwrite a whole partition of files. Remove any external drives, cameras or other storage devices connected to your computer (except for the USB drive you wish to repartition).

Use 'disk utility' or 'gparted' to unmount the USB device, and then to delete any existing partition(s). Create a 750 MB FAT partition named 'ubuntu' (or whatever); then an ext2 partition named 'casper-rw' (exactly) on the remaining space. Select the 750 MB ubuntu partition, click "edit partition" and check "bootable". If you were successful, skip down to #Installing Ubuntu on the USB drive. If you prefer to work with 'fdisk' on the command line, keep reading below. The preparation of the drive is explained below using 'fdisk'. You could use 'gparted' or 'disk utility' as well.

Plug in the drive and check its device name:


Assuming you have your Ubuntu set up to automatically mount your USB stick, this should show you what /media/drive or whatever it's called corresponds to on the device level.

Alternatively, you could run sudo fdisk -l but if you have multiple devices on your system, it will print a fair amount of fairly technical information.

We are going to suppose that its name is /dev/sdX where X is the disk device such as /dev/sda or /dev/sdc. Please be sure to substitute the correct letter for X which matches the device name you find for your pen drive. Double check it! [Note: on newer computers, which use serial ATA disks, /dev/sda is typically the main hard drive; do not overwrite this!

Note: on some usb-sticks fdisk says "Note: sector size is 2048 (not 512)", in which case you may very well be out of luck trying to boot from it, see mailing list thread: Problem with 2GB USB Pendrive

Now we are going to create 2 partitions: 1 partition of 750 MB with a fat32 filesystem and 1 partition with the rest of the drive space with volume name "casper-rw" and ext2 filesystem. The first partition will be made active.

Open 'terminal' and unmount the USB drive (replace 'sdX1' with the appropriate partition name):

sudo umount /dev/sdX1

Fire up 'fdisk' for the drive:

sudo fdisk /dev/sdX

This will give you the fdisk prompt. Look to what you have on the drive. Check if you have to backup the data you have on it. 'p' will print the content of /dev/sdX. If you have partitions on it remove them ('d', 'partition number').

Make partition 1:

1. 'n' for new partition

2. 'p' to make it a primary partition

3. '1' to call it partition number 1

4. Then just press enter to accept the proposed starting cylinder

5. '+750M' to make its size 750 MB

6. Then 'a' to make it the active partition

Set the file system of partition 1 to FAT:

1. 't'

2. then '1'

3. then '6'

(I already had partition 1 selected so for me it was just 't' then '6' -Jordan_U)

there seems to be an incongruity: when you change the filesystem to FAT using option "t" the filesystem option "6" is FAT16. w95/fat32 is option "b". Which file system needs to be used? -rebelcat1

Make partition 2:

1. 'n' for new partition

2. 'p' to make it a primary partition

3. '2' to call it partition 2

4. then just press enter to accept the proposed starting cylinder

5. then press enter to accept the proposed ending cylinder

6. Save and quit fdisk with 'w' to write the new settings.

I had an issue with this that every time I saved my partition setup using 'w', I'd get:

WARNING: Re-reading the partition table failed with error22: Invalid argument.
The kernel still uses old table.
The new table will be available at the next reboot.

If this happens, and then checking with 'sudo fdisk -l' doesn't show the new partitions - here's a fix if you have a Windows OS, though I don't know why it works:

Do nothing else with the drive. Insert into Windows (mine was XP) and format with Disk Management and use FAT32. Now, 'sudo fdisk -l' should see the two partitions under the drive. -neverard

I had the same error, using gparted instead of fdisk worked for me -Jordan_U

Check the result:

sudo fdisk -l

You should see the 2 partitions with the first marked with a * because it is active.

If ubuntu mounted the partitions again you should unmount them:

sudo umount /dev/sdX1
sudo umount /dev/sdX2

make sure (via fdisk -l) you created a w95/fat and not a linux partition, otherwise i was unable to boot from it!! (i had to use gparted to achieve this) - bernstein

fdisk worked for me however - booted off CD - krishnan

Alternate partition layout

Please note that Windows will only mount the first partition of any removable media - which includes USB flash drives. While you can get around this, it requires some highly kludgy driver procedures, which you are unlikely to be able to pull off on any machine where you are not an admin.

In short, having the Ubuntu Live as your first partition reduces the utility of your USB stick for what is statistically it's most likely task - transferring files to and from Windows machines.

Partition 1 : FAT32, full size of stick minus 750M

Partition 2 : FAT16, 750M (FAT16 because older versions of syslinux don't like FAT32)

Follow the instructions, but use /dev/sdX2 instead of /dev/sdX1, and make sure you make /dev/sdX2 bootable in fdisk.

For persistence, follow the instructions to create a loopback file at LiveCDPersistence. This feature works fine from a loopback file inside a FAT32 filesystem, just as long as the file contains a supported unionfs filesystem like ext2.

Now you have (mostly) the best of both worlds.

You can transfer files between Windows machines on the FAT32 partition
You have a persistent Ubuntu thumb
You can mount the FAT32 partition from a persistent Ubuntu boot (manually)
You can enlarge or shrink your casper-rw partition without having to repartition your thumb, just make a new loopback filesystem, mount them both, and copy the files across.
If the persistent boot automatically mounted the root of the FAT32 filesystem as well as the casper-rw filesystem, it would be ideal.

Formatting the partitions

We will now format the partitions by putting a filesystem on and giving them the name 'ubuntu' (or any other name you want) and 'casper-rw' (this name is MANDATORY, do not change other names and do not use capital letters):

sudo mkfs.vfat -F 32 -n ubuntu /dev/sdX1
sudo mkfs.ext2 -b 4096 -L casper-rw /dev/sdX2

Mount the partitions on the drive. Take out the drive and put it back. Ubuntu Dapper or Edgy will mount the drive automatically if you have checked this option under System -->Preferences--> Removable media. Otherwise use 'mount':

sudo mount /dev/sdX1 /media/mountpointyouhave1
sudo mount /dev/sdX2 /media/mountpointyouhave2

On recent Ubuntu versions, the USB stick's partitions should simply appear under /media/ubuntu and /media/casper-rw if you have followed the proposed naming convention.

Installing Ubuntu on the USB drive

Download the Ubuntu image (for example, ubuntu-11.04-desktop-i386.iso) or put in an Ubuntu install CD if you have it.

If you use the downloaded image you can mount it to make it accessible as follows, using 'terminal' (supposing that the iso image is in the current directory):

mkdir ubuntuCD

sudo mount ubuntu-11.04-desktop-i386.iso ubuntuCD -o loop

You can now access the files via the 'ubuntuCD' folder.

Open the CD's top-level folder and make sure that you see the hidden files too. (verify that View-->Show Hidden Files is checked).

Copy the following folders and files to the 750 MB partition of your USB drive:

Copy folders: '.disk', 'casper', 'dists', 'install', 'pics', 'pool' and 'preseed' (skip the 'boot' folder);
Copy the folder 'isolinux' to the drive and rename it to 'syslinux';
Copy the file 'README.diskdefines', and optionally, the 'usb-creator.exe' and 'wubi.exe' files (skip the 'autorun.inf' and 'md5sum.txt' files);
Rename the file 'syslinux/isolinux.cfg' to 'syslinux/syslinux.cfg' on the USB drive by right clicking on it and selecting 'rename';
Rename the file 'syslinux/isolinux.bin' to 'syslinux/syslinux.bin'.

Alternately, you can run the following commands in 'terminal'. If you copy/paste, take care to make your browser window large enough to not have accidental line breaks inserted. And remember to replace /media/ubuntu with /media/whatmountpointyouhave1 if you used a different name for the mount point.

Add the -v option to cp to get some progress indication. The casper/filesystem.squashfs file in particular is large, and takes a long time to copy.

cd ubuntuCD
cp -rf casper dists install pics pool preseed .disk /media/ubuntu
cp -rf isolinux /media/ubuntu/syslinux
cd /media/ubuntu/syslinux
mv isolinux.cfg syslinux.cfg
mv isolinux.bin syslinux.bin

The FAT32 file system will not allow symbolic links to be copied over. You will get a warning about this, but it is harmless.

Unmount the iso image that you mounted above, using 'terminal'.

sudo umount ubuntuCD

Making the drive bootable

We are going to install the linux bootloader 'syslinux'. This bootloader gets its menu from 'syslinux/txt.cfg'. We will modify this file. Open 'syslinux/txt.cfg' from the USB drive with your favorite text editor. For instance:

cd /media/ubuntu/syslinux

gedit txt.cfg

The following edits need to be made:

Search and replace '/cdrom' with nothing;

Copy the first 'label' section, paste it in before itself, and make the following edits to this new section:

The 'label' argument should be changed to something unique, here we use 'persistent';

The 'menu label' should be changed to describe the option; '^Use Ubuntu in persistent mode' would seem suitable, with a caret before the "U" to make it a shortcut key for this option;

The word 'persistent' should be added into the 'append' line (this is the crucial point!);

Optionally, change the 'default' to the label name used above (e.g. 'persistent').

The following is an example of what you might end up with:

default persistent
label persistent
menu label ^Use Ubuntu in persistent mode
kernel /casper/vmlinuz
append file=/preseed/ubuntu.seed boot=casper persistent initrd=/casper/initrd.lz quiet splash --
label live
menu label ^Try Ubuntu without installing
kernel /casper/vmlinuz
append file=/preseed/ubuntu.seed boot=casper initrd=/casper/initrd.lz quiet splash --
label live-install
menu label ^Install Ubuntu
kernel /casper/vmlinuz
append file=/preseed/ubuntu.seed boot=casper only-ubiquity initrd=/casper/initrd.lz quiet splash --
label check
menu label ^Check disc for defects
kernel /casper/vmlinuz
append boot=casper integrity-check initrd=/casper/initrd.lz quiet splash --
label memtest
menu label Test ^memory
kernel /install/mt86plus
label hd
menu label ^Boot from first hard disk
localboot 0x80

The menu choice called 'persistent' was made the default so that when booting up you can just hit 'Enter' to start your customized session.

Install bootloader

The last thing to do is install the bootloader. Make sure you have the 'syslinux' and 'mtools' packages installed. If not, open 'synaptic' and select them from the package list to mark them to be installed, then "apply" the changes.

Or use 'aptitude' or 'apt-get' from the command line. For example, the following command should be harmless if they are already installed.

sudo aptitude install syslinux mtools

First you need to unmount your usb stick using 'disk utility', or use 'terminal' to enter the following commands (replacing 'sdX1' with the appropriate partition name for the 750 MB ubuntu partition on your USB device):

cd ~
sudo umount /dev/sdX1

This command will install the bootloader on your drive:

syslinux /dev/sdX1

don't know why, but i had to use the -f switch, (syslinux -f /dev/sdc1) - bernstein

same here - I had to use the -f switch - krishnan

syslinux complained it couldn't create LDLINUX.SYS, but it worked in the Windows version (see below) - JanSchlüter

According to this page1 syslinux 3.35 should be used to get support for fat32, edgy ships with 3.11. According to the changelog 3.35 only seems to introduce support for subdirectories.

If you get the error:

sh: mcopy: command not found
syslinux: failed to create ldlinux.sys

then you will need to install the 'mtools' package.

Eject the drive:

eject /dev/sdX

Boot on the USB drive, change some settings, reboot again and check if the changes are kept (persistence check).

Do not forget to check if the computer you are going to use, boots USB devices first (look in BIOS settings).

So that is it. Enjoy (k)ubuntu where ever you go.

If you boot the drive and persistency works but when you log in to gnome you get a message saying that it couldn't write to .ICEauthority, then a simple fix is deleting .ICEauthority. You can automate that by doing this:

echo >> .bashrc
echo "# This makes GNOME work" >> .bashrc
echo "rm .ICEauthority 1>/dev/null 2>/dev/null" >> .bashrc

Those using Lucid (10.04) to make a pen drive to boot Precise (12.04) that is to say, using the previous LTS release to go to the next one, will need to be aware of this incompatibility between the syslinux versions involved:

there are a couple of work-arounds (eg, typing 'help' at the prompt and then hitting 'enter') but the bug has been triaged and labeled WONTFIX since Maverick and as late as March 2012.