Friday, August 04, 2006

Linux on USB flash drive

There are many sites providing USB pendrive distros, most notably PendriveLinux. But what I was unable to find was a step-by-step procedure. So began experimentation and I created a Linux installation on a 128 MB USB flash drive. And its pretty simple.
So here goes...
You need :
  • A linux box, with sources, gcc & binutils.
  • ExtLinux bootloader.
  • A USB pen drive (also called USB stick, USB key, USB etc etc).
  • Busybox
Configure the kernel and compile it. Especially, USB support must be part of the kernel and not a module. In my configuration, all h/w support was part of the kernel image. Nothing was included as a module.
Also, this means that we need to specially customize the kernel for the target hardware on which the USB installation will be used. So we have all the necessary support without increasing the kernel image size.

The USB drive needs to be partitioned and formatted as an ext3 partition. I created a single bootable ext3 partition.
Next is to install extlinux on the USB device. Extlinux is a bootloader that can boot linux of an ext2/3 partition. The steps to do this are in the syslinux site.

Once this is done, copy the bzImage and the initrd file to the boot directory on the USB drive and also create the extlinux.conf file. The initrd file gets created in /boot directory of the linux box when 'make install' is run. Once can choose to create their own initrd too.

I extracted the initrd and then copied the directories created onto the USB drive. This had the directories bin, lib, proc, root, sbin, sys, tmp and a init shell script. Linux needs an init (or a linuxrc) which it executes as the first process.

Now that the files are copied, reboot the box and get it to boot from the USB disk. :-).

Busybox can be used instead of the various individual binaries. Busybox is a lightweight console app that provides for the various binaries needed on Linux.

Next experiment: get X server & Window Maker on the USB drive.

1 comment:

xed said...

Thanks for this! I was trying to do a very similar thing with no luck. The hint you provided was "bootable" partition. Stupid really, but you need to set the bootable flag apparently.