Installing Linux on USB – Part 6: Create a DOS and Linux bootable USB flash drive
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 (I will provide an image for this)
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
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.
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:
Now, similarly create a Linux partition (/dev/sdc2) type 83 following the above steps.
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 hd1grub> root (hd1,1)
Filesystem type is ext2fs, partition type 0×83grub> 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 and format exes) 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.
In case if you do not have necessary tools to create a MS-DOS bootable floppy drive, please download this image on any Linux system and give the following command:
# dd if=/root/msdos_bootable_floppy.image of=/dev/fd0
The above command will clone the image on your floppy drive. Now reboot your system and boot from the floppy drive and you should be able to boot into DOS.
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










Free Email Subscription









February 11th, 2009 at 12:20 am
[...] Part 6: Create a DOS and Linux bootable USB flash drive [...]
February 26th, 2009 at 7:03 pm
Great article,
i have an old usb drive its capacity is 256 mb and hence i couldnt install lenny the one you have provided here.
So i was wondering if you could suggest some other smaller distribution that i could install similarly, doesnt need to be brand new maybe one with a command line interface alone will do too.(one with X is better though).
Also i think using freedos would be better than dos 6.22
what say you
Reply to this comment
February 26th, 2009 at 11:20 pm
you can usé debootstrap(available in debian and maybe ubuntu) to install a minimal system in a directory(~100mb)+xfce or maybe dsl(damn small linux), puppy,etc..
Great article, very nice to read!
Ps:there is a duplicate in the last fstab
Reply to this comment
Bean Counter Reply:
May 24th, 2009 at 5:40 pm
The second instance is for booting into runlevel 1, or single mode, a minimal single-user shell, generally for rescue purposes.
Reply to this comment
February 27th, 2009 at 12:44 am
Rescue usb created : 18mb!
Copy top level dirs ,full copy of /boot,/bin. Add /etc/fstab, /sbin/reboot, for each binary use ldd to figure out libs needed to be copied.Around 150 files to have a basic system.
Reply to this comment
nikhil Reply:
March 3rd, 2009 at 9:29 am
i wasn’t able to get the size down to 18 mb
it was about 100 mb.
but i got it to work
perhaps you can tell me exactly how you made it so small
Reply to this comment
February 27th, 2009 at 2:19 pm
so now i’ve copied all the directories as suggested by Nx and installed grub. let me give it a try to see if it works.
Reply to this comment
March 2nd, 2009 at 1:11 pm
I was just wondering if you missed out bunzipping after this in Step 4:
debian:~# mount /dev/sdc2 /mnt/
debian:~# cd /mnt
debian:/mnt# tar -xjvf /root/debian-lenny-fresh-install.tar.bz2
’cause you seem to use this in Step 6:
debian:~# mount /dev/sdc2 /mnt
debian:~# nano /mnt/boot/grub/menu.lst
Or is it possible to boot from a bizipped image?
Reply to this comment
Kushal Reply:
March 2nd, 2009 at 6:30 pm
Hi Xaero,
Thank you for your comment! I don’t think any step is missing. The “tar” commands does the unzipping part and then you make changes to the menu.lst file and you should be able to boot from your device.
It is not possible to boot directly from the bzipped image – at least I am not aware of any method.
Are you failing at any step?
Reply to this comment
March 3rd, 2009 at 4:19 am
Oh you have not mentioned the bzipping step, I was thinking that you missed the bunzipping. I was wondering if by any chance you knew why do some distros that use squashfs keep searching for cdroms. It causes a problem when installing from USB drives.
Secondly if you install grub on a usb disk and copy lets say a fedora image that has a folder named isolinux with the boot related files and LiveOS with the squashfs image, how do you point the kernel to use the squashfs image, because the original isolinux.cfg/syslinux.cfg doesn’t have any path mentioned, how does the kernel find it?
Reply to this comment
March 8th, 2009 at 6:03 pm
[...] Part 6 [...]
August 16th, 2009 at 8:51 am
there can only be three primary partitions and an extended partition
Reply to this comment
October 18th, 2009 at 11:43 pm
I use a USB jump drive to act as the primary boot device for all my systems. Most computer manufacturers are reluctant to recommend installing grub to their systems because it interferes with their system recovery option. So I have grub installed on the jump drive with a full working distro ( I use Ubuntu). I use Bios setup to select USB flash as the first boot device and then grub boots allowing me to select the windows or linux distribution on the hard drive. The advantage is that the primary windows partition on the PCs primary hard drive does not have to be altered with grub.
I find that I can even run compiz quite well on the USB. It means that we can leave the SYSTEMS ALONE.
Reply to this comment
January 2nd, 2010 at 4:02 pm
Kushal: your “Installing Linux on USB” is great, the best I’ve seen. I’ve been looking around for quite a while, trying to find some insight on how to get Linux on a USB flashdrive (I’m running a brand new diskless dual-core Atom small form factor PC). And I’m a new Linux user by the way.
I am able to get Kubuntu on a flashdrive using the USB wizard, but it appears to be an ISO (e.g, CD image), not the individual files. What I end up with is one great big 3gb file — persistent, but a bit slow, and if it crashes you lose your settings (which happened to me). But up to know that method is the only one I saw (all the tutorials seem to be all about getting a CD image on a flashdrive).
Your method appears to actually install the individual files. I have two questions: would your method as outlined in Part 5 and Part 6 work for Kunbuntu? Or am I missing the point of your tutorial?
Second question: in part 6, you give instructions for creating DOS 6 on a flashdrive (coincidentally something else that I need!). However I don’t have a floppy drive. I have 2 machines, one running windows, one running Linux (the Atom). No floppy. Is there another way to get the image you supplied on to the flash drive?
By the way your comments about life-span of USB flashdrives is well taken, thanks!
Many Thanks and Best Regards,
Scott Charles
PlumbBob Research
Reply to this comment
January 3rd, 2010 at 5:36 am
^^ Here is the ultimate usb booting thread
http://hak5.org/forums/index.php?showtopic=14179
Reply to this comment
Scott Charles Reply:
January 3rd, 2010 at 12:26 pm
Haider: appreciate your reply. And the hak5.org is a good reference. Doesn’t quite help me, but thanks anyway. What I’m trying to do is install Linux on a USB flash drive, as Kushal has outlined. I’m just trying to figure out how to do it in Kubuntu.
FYI I just tried the cfdisk command on one of my extra flashdrives, got an error saying the drive was read only. No doubt simple to fix if you know Linux, but if you don’t, it’s a brick wall.
Thanks,
Scott
Reply to this comment
February 17th, 2010 at 4:56 am
You may wish to mention the occasional need to use the undocumented /mbr switch when fdisking the flash drive. If there’s crud on the master boot record, something can’t grok the boot block and the boot process will die with NON-System disk or somesuch.
–jake
Reply to this comment