Monday, August 06, 2007

Installing A Linux Distro to An USB Drive

Let me begin with emphasizing what is an USB device. Generally, it will be an USB stick, but it could be as well an ordinary MP3 player, an iPod, an external HDD, a Smartphone, a PDA or who knows what else. The important thing is that it acts as a storage device.

It is important that you understand how the install procedure works, because you might encounter situations where things are a little bit different, and a good understanding of the process will allow you to get over this kind of situations. In general, this way you can also install Linux on a hard drive.

The first thing you should do is create an ext3 partition on your stick. The larger this partition is, the more Linux applications can be installed on your stick. This one will hold your Linux files. I still recommend creating a small vfat partition, because you will encounter situations when you'll want to transfer some files on the pendrive and booting Linux is not the best thing to do just for transferring a file. Regarding the SWAP partition, I don't recommend creating one, since the space on the USB drive is limited.

For partitioning the USB drive you can use any software you want, but, if you are not very experienced, I recommend GParted or QtParted, because they are very user friendly. If you don't have those installed and you don't want to install them in any Linux distro, you have fdisk and you can use that.

Note: Your USB drive will most probably be sda.

After the partitions are created, you'll have to copy the Linux files to the ext3 partition. There are several techniques for doing that, but I will only explain the one that is the easiest to use. The reason why some techniques are harder to use than the one that follows is that you have to rebuild or, in some unfortunate cases, modify the initrd. An example of how to make a USB compatible initrd is this command:

mkinitrd --with-usb --preload=usb-storage --preload=ehci-hcd --preload=scsi_mod --preload=sd_mod ./usbinitrd-`uname -r` `uname -r`

...and this should work most of the times.

The easiest way is to use a Live CD. First of all, you can copy the contents of the CD on the stick and then install a boot loader. This has the advantage of preserving space, because the LiveCD files are compressed. Secondly, you can boot your favorite LiveCD, mount your flash drive, copy some the files and directories from the / and create some directories using this commands:

cp --preserve -R /{bin,dev,etc,home,lib,root,sbin,usr,var} /mnt/sda1
mkdir /mnt/sda1/{boot,mnt,proc,sys,tmp}

After all this files are copied, you'll have to take the vmlinuz file from the CD and put it in the exact same path on the memory stick. Next, you'll just have to install a boot loader, a step which will be explained later.

The other way is to install your favorite distro directly on the USB memory stick, but this will be more than a little tricky so, since I can't guaranty it will work, I will not describe how this should be done. As an advice, I can tell you that if your distro has an expert mode, that might be the key.

If you still want to put a full featured distro on the stick, you should install it on the HDD, keeping in mind that the size of the installation shouldn't be larger than your ext3 partition on the stick. After your distro is installed, you'll have to copy all the files on the stick, just as you did in the case of the LiveCD. This option would be the best, but it will also be the most time consuming. If you decide to go with this one, don't forget to create a new initrd as shown above.

Installing the boot loader correctly is crucial. I recommend using syslinux or GRUB. For installing syslinux on the stick, you should execute the command syslinux /dev/sda1 After this, you should create a syslinux config file in the root directory. For learning how to configure it, go to the syslinux website or look at some examples on the net, including the one I've posted below.

Hopefully, after you follow all these steps, you'll be the satisfied owner of a Linux on a stick!

Pinpoint customers who are looking for what you sell.