Creating a Bootable Linux USB Drive PDF Print E-mail
Monday, 05 February 2007 09:30

I just bought a new 4GB USB thumb drive, and I want to put linux on it so I can walk up to about any computer and install or run linux on it for repair purposes or to quickly dissiminate Linux propaganda.

I haven't been able to find any substantial how-to's on 'how to' do this so this is my attempt to do it on my own with the limited knowledge in my brain on this topic.

Partitioning and Formatting

So the general idea is I want to be able to use the USB drive as a normal portable disk to be used on all operating systems, but also with the added ability to boot off of the drive.  I will create a partition for my file space and another for the linux files.

Here is my partition table via fdisk 

# fdisk /dev/sdb  

Command (m for help): p

Disk /dev/sdb: 4127 MB, 4127194624 bytes
255 heads, 63 sectors/track, 501 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         374     3004123+   b  W95 FAT32
/dev/sdb2             375         501     1020127+  83  Linux

Then I format the partitions to FAT32 and ext2, respectively

# mkfs.msdos /dev/sdb1

and then 

# mkfs.ext2 /dev/sdb2

GRUB on a USB Disk

Now I'm going to attempt to install grub onto this baby. 

I copied the grub files from the grub directory on my computer to the disk(mounted at /mnt/usb2):

# cp -R /boot/grub /mnt/usb2/ 

Just type grub to enter the program.

# grub  

Probing devices to guess BIOS drives. This may take a long time.

Now, while in grub we want to setup grub from the grub files on the Linux(ext2) partition that we just created. The hard drive shows up as hd2 on my system(I have 2 other hard drives-- remember grub numbers start from 0).  The partition shows up as partition 1.  Use the autocomplete to double check everything(good practice). Below the autocomplete is shown as well as the actual command.

 grub> root (hd2,
 Possible partitions are:
   Partition num: 0,  Filesystem type is fat, partition type 0xb
   Partition num: 1,  Filesystem type is ext2fs, partition type 0x83

grub> root (hd2,1)
 Filesystem type is ext2fs, partition type 0x83

Then, we want to install grub on the MBR of the USB disk. We use the setup command to do this, and tell it what disk to install itself onto(hd2 in our case).

grub> setup (hd2)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes
Checking if "/grub/e2fs_stage1_5" exists... yes
Running "embed /grub/e2fs_stage1_5 (hd2)"...  16 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd2) (hd2)1+16 p (hd2,1)/grub/stage2
/grub/menu.lst"... succeeded
Done.

Type quit to exit, we are done here

grub> quit 

Now grub should be installed on the disk, we can edit the grub.conf file to specify the boot options.  I'm going to run a test to see if it boots and I should be able to boot my current OS from the grub on the usb disk.

Update!

Sorry I haven't been able to successfully complete this project, it seems I've gotten too busy with other things. I'll finish this when I'm interested in it again.  I just left the two partitions on my USB thumb drive, I was running into problems getting the kernel to load(I don't know much about how ubuntu's bootloader works, I'll probably try with gentoo first). If anyone has any good ideas I'm open to them.

Update! April 4, 2007

I regained some interest lately, and I've done more research.  Instead of trying directly to go with ubuntu, I needed to learn more about initrd and the specific boot process of the kernel.  I was(am) running into kernel errors where the kernel cannot locate the USB drive because the usb modules are not loaded.

So I need to make an initrd file which will load the modules the kernel needs before it boots.  This is a big headache but I think will be worth it.  I've got an external USB drive instead of my thumb drive to work with.  That way I don't have to worry about size for now.

I am using gentoo to develop this boot process, because I am more familiar with it, and it is just so easy to use.  Here I go to explore the mysteries of the kernel boot sequence. 

 

Book Wish List

StackOverflow