Saturday, August 9, 2008

How to Install Ubuntu Linux on a USB Flash Drive so it Saves Changes


I walk you through all of the steps to create a persistent USB live install



Linux Rocks! as any techie will tell you, but now it's not just for techies. It has now been made easy. No more complex commands. It works much better than Windows and it is virus proof. Here is how to install it on your USB so you can take it with you and use it on any computer that can boot USB devices.

This article is also posted to Squidoo.

Linux Grows Up

Linux has finally hit the big time with the latest Ubuntu release. You no longer need a teenager to make it work. In fact it's much easier than Windows now. It automatically knows about all of your hardware and its truly plug-and-play. The only gap left is printer support, but if you stick with HP printers, that is covered too.

You can try it yourself without installing any software. Just download the CD image and boot it up. If you like it, which I am sure most will, go ahead and install it. If you want to take it with you, you can burn it on a USB flash drive. I tell you how below.

Buy One From Me

I am selling these already built as described below for $25 including shipping for US customers. Contact me for international orders. Send payment to marcdonovan100 at gmail dot com on PayPal. I usually ship within 4 days first-class mail. This is for a 2GB flash drive. If you want a larger drive size, email me and we can discuss.

Burn a Linux CD Image

Go to http://www.ubuntu.com/getubuntu/download and download the latest copy that matches your system. You want the desktop version and you can choose 32 bit or 64 bit depending upon your hardware.

You need to burn the iso file to a CD ROM. There are a lot of free iso burners out there. Just search for "iso burner" on Google.

If you don't have a CD burner, you can order a free CD (including free shipping!) from ShipIt. This can take a while to get delivered though. If you want faster service, order a CD from Canonical.

Once you have the CD, boot it up in live mode. You may need to adjust your bios settings to get it to boot. Do this by changing the boot order in your computer setup utility. When you turn on the computer you will see a message stating how to start the setup program - usually del, esc or f2. Change the boot order to boot from usb first, then cd, then your hard drive.
Running Ubuntu
Write down the url for this lens so you can come back here after you have booted Ubuntu.

If you have problems booting, the most common error is burning the iso file on the CD instead of burning it as a CD image. If you can see the iso file in Windows Explorer, then you have not burned it correctly. Another problem is incorrect video settings. You can select safe mode video from the startup menu (F4 key) in order to get going. Later, you can easily download and install drivers for your video card.

When you boot the CD, there are options to install on your hard drive. You can do this if you want, but be careful since one option is to erase your hard drive prior to installing!

Once you have the live CD running, you will want to poke around and see what this system is all about. Try the programs you find on the pull-down menus. You will be quite surprised to see how much stuff you get for free, already installed and ready to go.

Partitioning the USB

OK, so the CD version is great and it has loads of cool stuff on there, but one thing you will notice is that every time you boot the live CD, you start from a fresh install. None of your settings and files are saved from one session to the next. That's not very productive, so we need to put this on the USB so it can hold your settings, downloaded software and user files.

The first step is to format the USB stick. Insert the stick and open a terminal window (Application/Accessories/Terminal) and type:

sudo fdisk -l

The sudo command gives you root (super-user) privileges to run commands that may be destructive, so be careful with sudo commands and make sure you type things exactly as shown.

The fdisk -l command runs the fdisk program, which is a program that partitions disks. The -l means list. This will display a list of your disks that are visible to Linux. One of these will be the USB stick and you can tell which one it is by the size listed

Here is an example listing from a 2GB USB stick:

Disk /dev/sdb: 2020 MB, 2020539904 bytes
32 heads, 63 sectors/track, 1957 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes
Disk identifier: 0x35f00388

2020 MB is 2 GB. To the left of that is the drive path /dev/sdb. This is what we need to use going forward. Your drive path may be different depending upon your installed drives. The main point here is don't format your internal drive by mistake. Here is the listing for my internal drive:

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xaa2caa2c

Notice this is not 2GB, so it's not the USB stick. This means I should NOT use /dev/sda! You have been warned. If you format the wrong drive, I don't want to know about it.

OK, so now that you know which disk is the USB, you need to partition it into something we can use for Linux.

sudo fdisk /dev/sdb

Of course use the proper /dev/sdX

Hit the m key to see all of the commands. All of these are one key commands. Hit p for print. You should see one partition that is probably a FAT16 partition. Hit d to delete this. On the off chance there is more than one, it will ask your to type a number so hit 1, then d to delete the rest. Hit p to double check there are no partitions. Now hit n for a new partition. Hit p for primary. Hit 1 for partition one. Hit "enter" to select first cylinder, Type +770M to make the first partition 770 MB. Hit a to make this bootable, hit 1 to make the first partition bootable. Hit t to change the partition type. Hit 6 to select FAT16. Hit p to print the partition table. You should see something like this:

Command (m for help): p

Disk /dev/sdb: 2020 MB, 2020539904 bytes
32 heads, 63 sectors/track, 1957 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes
Disk identifier: 0x35f00388

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 747 752944+ 6 FAT16

Command (m for help):

Note that the * is in the boot column and the FAT16 is set as the type.

Now hit n and p and 2 to make the second partition. Hit enter twice to select the defaults to take the rest of the space. Hit p to verify what you have:

Command (m for help): p

Disk /dev/sdb: 2020 MB, 2020539904 bytes
32 heads, 63 sectors/track, 1957 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes
Disk identifier: 0x35f00388

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 747 752944+ 6 FAT16
/dev/sdb2 748 1957 1219680 83 Linux

Yours may have different start and end blocks, but it should look very similar. Hit w to write the changes to the stick and quit fdisk.

You can verify that the stick has the proper partitions by again typing:

sudo fdisk -l

You should see something like this:

Disk /dev/sdb: 2020 MB, 2020539904 bytes
32 heads, 63 sectors/track, 1957 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes
Disk identifier: 0x35f00388

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 747 752944+ 6 FAT16
/dev/sdb2 748 1957 1219680 83 Linux
ubuntu@ubuntu:~$

Format the Partitions

OK, so now the partitions are ready to be formatted. Type the following:

sudo umount /dev/sdb1
sudo mkfs.vfat -F 16 -n ubuntu /dev/sdb1

where /dev/sdb1 is the name of your first USB partition as shown in the fdisk -l list. Careful here and don't format your Windows drive! The umount command unmounts the disk if it was mounted.

The result should be:

mkfs.vfat 2.11 (12 Mar 2005)

This puts a DOS-type filesystem on your first partition.

Next, format the second partition:

sudo umount /dev/sdb2
sudo mkfs.ext3 -b 4096 -L casper-rw /dev/sdb2

result should be:

mke2fs 1.40.8 (13-Mar-2008)
Filesystem label=casper-rw
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
76480 inodes, 304920 blocks
15246 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=314572800
10 block groups
32768 blocks per group, 32768 fragments per group
7648 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

This puts a Linux ext3 filesystem on the second partition. This type of filesystem is an advanced self-recovering filesystem with automatic journaling and recovery.

Your USB is now ready for loading. Remove the stick and plug it back in and you will get two File Browser windows. One labeled ubuntu and one labeled casper-rw.

Put Ubuntu on the Stick

Your new filesystems are mounted on /media. You can verify with this command:

ls /media

You should see:
casper-rw ubuntu

Now we need another Ubuntu CD image file to install on the stick since the one on the CD is locked and in use. There is not enough space on the virtual disk to hold the entire iso file, so you need to change the download file location in Firefox over to the casper-rw disk. But first we need a folder on there that is owned by the ubuntu user. The following commands will do the trick:

sudo mkdir /media/casper-rw/install
sudo chown ubuntu:ubuntu /media/casper-rw/install
ls /media/casper-rw

That last command should display this:

drwxr-xr-x 2 ubuntu ubuntu 4096 2008-08-09 15:32 install
drwx------ 2 root root 16384 2008-08-09 01:09 lost+found

Now start Firefox (the orange button on the menu) and reset the download location. Go to Edit/Preferences, in the download section, Click on "Save Files To" browse button. Select casper-rw then install and click "Open".

Now you need to download the iso image again. This should save the iso image to your casper-rw partition on the stick.

You can verify with this command:

ls /media/casper-rw/install
Mounting the Linux Image on the USB
Find the filename with this command:

ls /media/casper-rw/install

You should see a filename very similar to this:

ubuntu-8.04.1-desktop-i386.iso

Now you need to mount the image so you can get the files.

sudo mkdir /mnt/ubuntu
sudo mount -o loop /media/casper-rw/install/ubuntu-8.04.1-desktop-i386.iso /mnt/ubuntu

Note that your filename may be slightly different if you have a later version than me.

Now you can see the files mounted:

ls /mnt/ubuntu

autorun.inf dists isolinux pics preseed ubuntu wubi.exe
casper install md5sum.txt pool README.diskdefines umenu.exe

Load the System Files on the Stick

Type the following:

cd /mnt/ubuntu
sudo cp -rf * .disk isolinux/* /media/ubuntu

This loads the boot files on the first partition. It can take some time to run. Ignore the messages about sym-links.

Next you need some software packages from the Internet:

sudo apt-get install syslinux
sudo apt-get install lilo

Answer the questions with a y. Ignore the message about running a program. Next run these two programs remembering to use your device names. Don't mess up your Windows disk with these! If you have any doubt run sudo fdisk -l to be sure.

sudo syslinux -f /dev/sdb1
sudo lilo -M /dev/sdb

Now you need to move and edit a file:

cd /media/ubuntu
mv isolinux.cfg syslinux.cfg
gedit syslinux.cfg

First, remove any text with /cdrom, then alter the Live section so it looks like this:

LABEL live
menu label ^Run Ubuntu from USB, saving changes to the USB device
kernel /casper/vmlinuz
append file=/preseed/ubuntu.seed boot=casper initrd=/casper/initrd.gz quiet splash persistent --

Changes include changing the menu text, removing /cdrom and adding persistent to the append line

Fixing a Script Bug

There is a bug in the startup scripts (at least in the version that I have). The following commands will unpack the scripts so you can fix the bug:

mkdir /media/casper-rw/install/bug
cd /media/casper-rw/install/bug
gunzip < /media/ubuntu/casper/initrd.gz | cpio -i --make-directories
cd scripts
gedit casper

You should now be editing the bad script. Use the find button and put in 755 to search. You are looking for a line that looks like this:

mount ${cowdevice} -t ${cow_fstype} -o rw,noatime,mode=755 /cow || panic "Can not mount $cowdevice on /cow"

Remove the ,mode=755 so it looks like this:

mount ${cowdevice} -t ${cow_fstype} -o rw,noatime /cow || panic "Can not mount $cowdevice on /cow"

Now save and exit the gedit program. You repack the startup fies with the following commands:

cd ..
find ./ | cpio -H newc -o > initrd
gzip < initrd > /media/ubuntu/casper/initrd.gz

Now you just need to delete the install directory:

cd /media/casper-rw
sudo rm -Rf install

If you have followed the directions you now have a working bootable USB. Restart your computer and make sure your system is setup to boot from the USB device first. You should be able to see your changes saved between sessions.

The first thing you should do is get updated system files. You will see an orange button on the top-right. Click that and it will download any updates.

When you shut down the system with the USB mode, you will get a message that says "remove the CD and hit enter" At that point, just hit enter and wait for your computer to turn off. Dont pull the stick until it is off completely.