Welcome to the part 6 of “Installing Linux on USB” series. In this part we will learn how to create a USB flash drive which can boot into Linux and DOS both using GRUB.

Purpose: In this post we will try to create a handy USB flash drive which has MS-DOS partition and a Linux ext2 partition with GRUB installed. Using GRUB we can boot into either DOS partition or Linux partition.

What you will need:

1. A Linux box with basic utilities
2. A USB flash drive (blank/used/formatted anything will do)
3. A MS-DOS 6.22 bootable floppy drive with “format” and “fdisk” utility.
4. A “tar” image of any existing Linux box filesystem (I will provide this too)

Step 1: Boot your Linux box with USB flash drive attached

Attached your USB flash drive (which is going to be formatted) to your Linux machine and boot from your hard drive into Linux. I am using Debian Lenny 5.0 with kernel 2.6.26-1-686.

Step 2: Create partitions on your  USB flash drive

The goal of this step is to create two primary partitions. Let’s assume that our USB flash drive gets detect as /dev/sdc, then:

/dev/sdc1 – Primary partition 1 – Bootable Flag set – DOS/FAT16 partition

/dev/sdc2 – Primary partition 2 -Linux ext2 partition

Advanced users: You can use any of your favorite formatting tool like fdisk, gparted, parted, qtparted to create this partition structure. Once you create the partition structure you can proceed to Step 3.

New Linux users: You can follow the steps mentioned below to create the partition structure using cfdisk. Give the following command:
# cfdisk /dev/sdc

cfdisk partition

cfdisk partition

cfdisk-Create Primary partition

cfdisk-Create Primary partition

cfdisk - Enter the size of partition

cfdisk - Enter the size of partition

My USB flash drive is of 2GB capacity so I am allocating 0.5 GB for DOS partition and 1.5 GB for my Linux partition. You can vary the size as per your needs. I recommend at least 0.5GB of space for your Linux partition.

cfdisk - Position of partition - Always at the beginning

cfdisk - Position of partition - Always at the beginning

cfdisk - After primary partition 1 is created

cfdisk - After primary partition 1 is created

Now, by default, cfdisk creates a Linux partition as you can see from the above image. We need to change that to a DOS/FAT16 partition as shown below:

cfdisk - Select Type and look for FAT16

cfdisk - Select Type and look for FAT16

cfdisk - Select "06" as partition type number

cfdisk - Select "06" as partition type number

cfdisk - DOS/FAT16 partition created

cfdisk - DOS/FAT16 partition created

Now, similarly create a Linux partition (/dev/sdc2) type 83 following the above steps.

cfdisk - Final partition structure

cfdisk - Final partition structure

Once you create all the partitions, you need to make sdc1 “Bootable” by selected the the option in the left bottom corner of the screen as shown in the above image (the highlighted part). Finally you need to “Write” the partition table that you just created by selecting the “Write” option. After the partition table has been written your final structure should look like the above image.

Note: Even after writing the partition table you may find that devices /dev/sdc1 and /dev/sdc2 are not being populated. If that is the case, then just un-plug and plug back in your USB flash drive and now the Linux kernel should be able to recognize those partitions. When you un-plug and plug back in your USB devices, chances are that the device name, /dev/sdc, might get changed to something like /dev/sdd. If that happens then you need to substitute the proper device name for the rest of the entry.

Step 3: Format your Linux partition

Now we need to format our Linux partition (/dev/sdc2) that we created above as Ext2 partition. Why Ext2? Please read my previous post for more details on this topic.

debian:~# mkfs.ext2 /dev/sdc2
mke2fs 1.41.3 (12-Oct-2008)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
95424 inodes, 381424 blocks
19071 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=394264576
12 block groups
32768 blocks per group, 32768 fragments per group
7952 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
debian:~#

Step 4: Extract a filesystem image on your USB flash drive

This is the only step in which you will have to arrange something on your own. We need to put a Linux filesystem on /dev/sdc2 so that we can boot into Linux from our USB flash drive.  For this, I simply tar’ed an entire partition (the “/”) of one of my hard drive which had regular Debian Linux on it and called it as “debian-lenny-fresh-install.tar.bz2″. Make sure that the size of the Linux install partition that you are going to tar is less than the size of Linux partition that you created in the above steps. I used a basic Debian install with 2.6.26-1-686 kernel (no GUI) for this purpose. You can download the filesystem that I am using in this post from here.

Assuming you have a tar image of any working Linux filesystem either from above or your own, do this:

debian:~# mount /dev/sdc2 /mnt/
debian:~# cd /mnt
debian:/mnt# tar -xjvf /root/debian-lenny-fresh-install.tar.bz2 .

This will extract the entire filesystem on your USB flash drive’s primary partition 2.

Step 5: Install GRUB bootloader

Now we need to install GRUB bootloader so that we can finally boot from our USB flash drive. Give the following command:
debian:~# grub --no-floppy

GNU GRUB  version 0.97  (640K lower / 3072K upper memory)

[ Minimal BASH-like line editing is supported.   For
the   first   word,  TAB  lists  possible  command
completions.  Anywhere else TAB lists the possible
completions of a device/filename. ]

grub> root (hd
Possible disks are:  hd0 hd1

grub> root (hd1,1)
Filesystem type is ext2fs, partition type 0×83

grub> setup (hd1)
Checking if “/boot/grub/stage1″ exists… yes
Checking if “/boot/grub/stage2″ exists… yes
Checking if “/boot/grub/e2fs_stage1_5″ exists… yes
Running “embed /boot/grub/e2fs_stage1_5 (hd1)”…  17 sectors are embedded.
succeeded
Running “install /boot/grub/stage1 (hd1) (hd1)1+17 p (hd1,1)/boot/grub/stage2
/boot/grub/menu.lst”… succeeded
Done.

grub>  quit

Note: If you only  have a hard drive and a USB Flash drive attached to your system, then most likely your USB flash drive will be (hd1). This is because GRUB’s device naming convention (hd0) is different from udev/linux kernel (hda). There are many websites which talks about this issue. So do a search if you are confused or contact me. I will try to provide some links on this issue.

Step 6: Edit menu.lst and fstab files

Since in Step 4 we took a Linux filesystem from an existing installation we need to edit certain configuration files to reflect the fact that we will be booting from a USB device instead of an IDE/PATA device on which the extracted filesystem was installed.

debian:~# mount /dev/sdc2 /mnt
debian:~# nano /mnt/boot/grub/menu.lst
title           Debian GNU/Linux, kernel 2.6.26-1-686
root            (hd0,1)
kernel          /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro quiet rootdelay=5
initrd          /boot/initrd.img-2.6.26-1-686

title           Debian GNU/Linux, kernel 2.6.26-1-686 (single-user mode)
root            (hd0,1)
kernel          /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro single rootdelay=5
initrd          /boot/initrd.img-2.6.26-1-686

Save the file and quit.

Note: The text in red is what we need to take care of. I have “sda1″ as my root filesystem instead of “hda1″ which I had in there previously. Also instead of  “(hd0,0)”, I now have “(hd0,1)” because our Linux partition is Primary partition 2. In GRUB’s terminology:

(hd0,0) – Primary partition 1

(hd0,1) – Primary partition 2

(hd0,2) – Primary partition 3

(hd0,3) – Primary partition 4

(hd0,4) – Extended Secondary partition 1

(hd0,5) – Extended Secondary partition 2

and so on. Also make sure that your kernel version on your extracted filesystem matches with the entry as shown above.

Now similarly we will edit the fstab file also to reflect the proper changes:

debian:~# nano /mnt/test/etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
/dev/sda1 /              ext2    noatime,errors=remount-ro 0       1

Save the file and quit.

Step 7: Boot into your Linux partition on your USB flash drive

Finally if everything went fine, you are now ready to boot from your USB flash drive. Reboot your computer and enter CMOS and change the setting to boot from USB device. You should be able to see the GRUB menu screen and the system should automatically boot into Linux. Congratulations!!!

Step 8: Get hold of a MS-DOS bootable floppy drive

Insert a MS-DOS 6.22 bootable floppy (with fdisk.exe and format.exe) into the floppy drive and boot from the floppy drive. Mostly likely you will boot to A:\>

Note: Remove/Disable all the drives from your system such that only USB flash drive and the Floppy driver are attached or activated on your system. This is essential because when we boot from DOS floppy we need to make sure that DOS detects the USB flash drive’s first primary partition as “C:” drive. AFAIK, in order to make any storage device DOS bootable, it needs to be detected as C: drive only, otherwise chances are you will most likely run into problems.

Step 9:  Preparing your primary DOS partition to boot DOS

A:\>format /s C:

You should be able to see something this like:

WARNING, ALL DATA ON NON-REMOVABLE DISK
DRIVE LIFE: WILL BE LOST!
PROCEED WITH FORMAT <Y/N>?y

Formatting 476.59M

Format complete

System transferred

Note: In case if you get “Drive not ready” or some similar error make sure that your USB drive is detected by DOS. For this you will need to use the “fdisk” command and re-create a new Primary DOS partition. You can follow the steps as mentioned here.

Step 10: Boot into your Linux partition of your USB flash drive

You are still not ready to boot into DOS from your USB flash drive because it we haven’t added any entry into the menu.lst file. Now, just as in Step 7, boot into your Linux partition on your USB flash drive.

Step 11: Create an entry into menu.lst file

Now we need to add an entry for DOS into GRUB’s menu.lst file so that we can boot from DOS.
debian:~# nano /boot/grub/menu.lst

Add the following lines at the end of the menu.lst file:
title=DOS 6.22
rootnoverify (hd0,0)
makeactive
chainloader +1

So your overall entries in the menu.lst file should look something like this:

title           Debian GNU/Linux, kernel 2.6.26-1-686
root            (hd0,1)
kernel          /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro quiet rootdelay=5
initrd          /boot/initrd.img-2.6.26-1-686

title           Debian GNU/Linux, kernel 2.6.26-1-686 (single-user mode)
root            (hd0,1)
kernel          /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro single rootdelay=5
initrd          /boot/initrd.img-2.6.26-1-686

### END DEBIAN AUTOMAGIC KERNELS LIST

title=DOS 6.22
rootnoverify (hd0,0)
makeactive
chainloader +1

Save the file and quit.

Step 12: Reboot your system and select “DOS 6.22″  from GRUB menu

Finally we now have a USB flash drive which can boot into DOS and Linux using GRUB. Try booting into DOS by selecting the “DOS 6.22″ option.

Note: This post is a little bit advanced from the previous ones and hence there are chances that you might not be able to boot successfully in the first time. Please leave a comment (preferred) or contact me in case if you encounter any errors.

As usual, please leave a comment/feedback if you have any. Comments encourages bloggers to post more and keep their spirits high.

Also don’t forget to rate this post below.

Part 7: Install Debian Linux from USB drives

Be Sociable, Share!