Being able to run Linux straight out of a USB drive is a great way to enjoy the liveCD experience, letting you run Linux from any computer you want. And it saves you from the trouble of carrying a CD along all the time, with your USB drive easily tucked away in your pocket.
Here’s a simple tutorial that shows you how to install Ubuntu on a USB drive. Even though this tutorial uses Ubuntu as its base distribution, you can virtually use any type of Linux liveCD distribution.
What you need
In order to be able to execute this simple tutorial, you will need:
- Ubuntu liveCD
- USB drive of at least 1 GB capacity
- Running Linux operating system.
Step 1: Finding the device
First of all, plug in your USB drive and check which device it is associated under. To find out that out, run:
$ sudo fdisk –l
On my system, the device appears as being /dev/sdb; therefore, we’ll be using /dev/sdb as a reference for this tutorial. Please make the replacement according to your system (for example, sda, sdc, etc)
Once the device has been located, the next step is creating the partitions.
Using the wrong device name might be fatal to your system partitions, so make sure you double check.
Step 2: Creating the partitions
Before you continue, make sure that you un-mount ALL your mounted partitions. To do so, run:
$sudo umount /dev/sdb1
Now launch fdisk, a tool to edit partition under Linux.
sudo fdisk /dev/sdb
What we are going to do now is delete all the partitions, and then create 2 new partitions: one FAT partition of 750 MB to host the files from the liveCD iso, and the other for the rest of the files.
At fdisk prompt, type d x, where x denotes the partition number (you can simply type d if you have only one partition).
- n to create a new partition
- p to make it primary
- 1 so that it is the first primary partition
Accept the default or type 1 to start from the first cylinder
- +750M to make it 750 Meg big
- a to toggle the partition active for boot
- 1 to choose the 1 partition
- 6 to set it to FAT16
Our first partition is now set up. Let’s move onto the next one:
- n to create a new partition
- p to make it primary
- 2 to be the second partition
- Accept the default by hitting Enter
- Accept the default to make your partition as big as possible
- Finally, type w to write the change to your USB drive
- Partition creation process is now complete. The next step is formatting them.
Step 3: Formatting the partitions
We are going to format the first partition as a FAT file system of size 16 and we are then going to attribute it the label “liveusb”.
$ sudo mkfs.vfat -F 16 -n liveusb /dev/sdb1
The second partition is going to be of type ext2, with a blocksize of 4096 bytes and the label casper-rw. Make sure that the label is casper-rw; otherwise the tutorial will not work.
$ sudo mkfs.ext2 -b 4096 -L casper-rw /dev/sdb2
And now, our USB drive is ready to host the liveCD image. The next step is copying the files to the USB stick.
Step 4: Mounting Ubuntu liveCD image
First off, we need to mount our Ubuntu iso. Depending on you having the .iso file or the CD, there are two different ways of doing it.
Mounting from the CD:
People using Ubuntu or any other user-friendly distribution might just have to stick the CD in, and it will be mounted automatically. If that doesn’t happen, the following command should mount it:
$ sudo mount /media/cdrom
Mounting from an .iso image file:
You will have to create a temporary directory, for example let’s use /tmp/ubuntu-livecd, and then mount your iso. (We will be using a feisty fawn iso).
- $ mkdir /tmp/ubuntu-livecd
- $ sudo mount -o loop /path/to/feisty-desktop-i386.iso /tmp/ubuntu-livecd
Once the CD image is ready, it is time to mount the newly created USB stick partitions.
Step 5: Mounting the USB stick partitions
Like we experienced with mounting the Ubuntu liveCD image directly from the CD, you might be able to get both partitions by simply re-plugging the USB drive. Partitions might appear as /media/liveusb and /media/casper-rw. If that doesn’t happen, you will have to mount them manually:
$ mkdir /tmp/liveusb
$ sudo mount /dev/sdb1 /tmp/liveusb
All the partitions we need are now mounted. Now lets move on to the next step.
Step 6: Copying the files to the USB stick
Access the CD image directory (/tmp/ubuntu-livecd in my case, but it might be /media/cdrom) and copy at the root of your USB first partition:
the directories: “casper”, “disctree”, “dists”, “install”, “pics”, “pool”, “preseed”, “.disk”
The content of directory “isolinux”
and files “md5sum.txt”, “README.diskdefines”, “ubuntu.ico”
as well as files: “casper/vmlinuz”, “casper/initrd.gz” and “install/mt86plus”
$ cd /tmp/ubuntu-livecd
$ sudo cp -rf casper disctree dists install pics pool preseed .disk isolinux/* md5sum.txt README.diskdefines ubuntu.ico casper/vmlinuz casper/initrd.gz install/mt86plus /tmp/liveusb/
Ignore any complaints your screen shows about symbolic links not being able to create.
Now let’s go to the first partition of your USB drive and rename isolinux.cfg to syslinux.cfg
$ cd /tmp/liveusb
$ sudo mv isolinux.cfg syslinux.cfg
Change /tmp/liveusb according to your settings.
Edit syslinux.cfg so that it looks like this:
APPEND file=preseed/ubuntu.seed boot=casper initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
menu label ^Start Ubuntu in persistent mode
append file=preseed/ubuntu.seed boot=casper persistent initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
menu label ^Start or install Ubuntu
append file=preseed/ubuntu.seed boot=casper initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
menu label Start Ubuntu in safe ^graphics mode
append file=preseed/ubuntu.seed boot=casper xforcevesa initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
menu label ^Check CD for defects
append boot=casper integrity-check initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
menu label ^Memory test
menu label ^Boot from first hard disk
Finally, we have our USB drive almost usable. Which brings us to our final step.
Step: 6: Making the USB stick bootable
In order to do this, we need to install syslinux and mtools:
$ sudo apt-get install syslinux mtools
And finally, unmount /dev/sdb1 and make it bootable:
$ sudo umount /tmp/liveusb
$ sudo syslinux -f /dev/sdb1
There you are!
Reboot, set your BIOS to boot from the USB drive and enjoy Ubuntu Linux from a flash drive.
If you run into trouble booting on the USB stick, this could be due to your MBR being corrupted. In order to fix this, you can use lilo.
$ lilo -M /dev/sdb
This should fix the MBR on device /dev/sdb.