Anything can go here, in any language... except my native language Sinhala. Be cool... anybody is warmly welcomed! :)

How I Prepared My GRUB-Bootable USB Flash Drive

Again... the GNU GRUB. I really can't forget such an interesting software that I can study. Today, I'm going to put the English version of my another Sinhala blog post. It's about how I made my flash drive a bootable one.

Now you might say,.. "That's pretty easy stuff with Windows... Just right click and Format...."; wait.........! I'm not going to talk about the DOS/ Windows bootloader. DOS/ Windows bootloader is nothing compared to the GNU GRUB. What I'm going to put here is how to put the GRUB + kernel into your flash drive.

First, I would like to give a small introduction on the GNU GRUB. Wikipedia got a whole lot of information,.. but I'll also explain. Simply, GRUB is a bootloader. A bootloader is the program loads the operating system when your computer boots. Have you ever seen the "NTLDR is missing" error on a Windows XP installed system? Yes, that NTLDR is the bootloader of Windows NT based operating systems. There are two most used types of bootloaders with Linux. One is LILO (LInux LOader) and the other is GRUB (GRand Unified Bootloader). Due to numerous reasons the most popular bootloader among the two is the GRUB. I'm not going to list them here,.. but believe me.. the GRUB is really a 'thing' to study!

So,.. we are bit off the topic.Let's get back on it. How would it be if I install the GRUB on a USB flash drive? That's what came into my mind when I was reading the posts on UCSC LMS (I'm a student). Yes, it would be great! There are several advantages I can think of.
  • If I boot a Live CD, it takes some time. But, here as it is a bootloader only system, it takes a less time to boot up.
  • I can study further,.. (I'm not a Linux expert)
  • I can hijack Linux systems,... :D
  • I can check the RAM using memtest+86.bin kernel

I have a 4 GB flash drive. So here's how I did with it:

1. Backed up all the data on the flash drive as I'm gonna partition it.

2. Divided the drive into two primary partitions. It doesn't matter whether it is primary or logical, but as I didn't need any more partitions, I set it as this.

# fdisk /dev/sdb

One partition is a FAT-32 one to keep my personal files,which I might need to use with both Windows and Linux. It should be the first partition on the flash drive. Unless, Windows will spoil up everything. I left 50 MB at the end of the partition table as unpartitioned/ free space which I'm going to use for the boot partition.

As I've mentioned earlier, Windows can not identify the partitions other than the physically first partition on the flash drive partition table. That means, even if the partition number is not 1, it should exist first to be identified by Windows.

The second one, is nearly 50 MB, and is ext-2 type. It holds the GRUB's files and the kernel.

Formatted the partitions using the following commands:
# mkdosfs /dev/sdb1 -v -F 32 -n LEONIDAS_4G
# mke2fs /dev/sdb2 -L boot

Now mounted them,
# mkdir /media/data; mount -t vfat /dev/sdb1 /media/data

# mkdir /media/boot; mount /dev/sdb2 /media/boot

3. Well, there could be a systematic way to do this. But, I'm not a Linux expert. So, please don't laugh at me. This is how I did it:

# cd /
# cp -rfv boot /media/boot

Now, the GRUB installation, the thing I recently got to know from our LMS forum.

# grub
grub> find /media/boot/boot/grub/stage1

Now it gives the following output:

According to the notation, the second partition on the flash drive is (hd1,1). The next step is;

grub> root (hd1,1)
grub> setup (hd1)
grub> quit

Now, the GRUB has been installed on the flash drive.

4. Now I carefully examined the boot partition on the flash drive. There were two versions of the kernal and related files. This might be probably due to a kernal update on my system. It doesn't matter. I kept the most recent version and deleted the other.

5. Finally I edited the menu.lst, which caontains the initial configuration of the GRUB, when it boots. Here's the entire menu.lst file:

title    Linux kernel 2.6.27-14-generic
root     (hd0,1)
kernel   /boot/vmlinuz-2.6.27-14-generic root=UUID=d1670f8e-eb3f-4dba-bba5-e00f0437e2a2 ro single
initrd   /boot/initrd.img-2.6.27-14-generic

title    memtest86+
root     (hd0,1)
kernel   /boot/memtest86+.bin

There's another few important things to state here. Althoug the flash drive is (hd1) here, it becomes (hd0) when booting. The reason is almost obvious, when you set it as the first boot device on the BIOS, it becomes the first device.

The next thing is the UUID. UUID is sent to the kernel as a parameter. You can find the UUID using this:

# vol_id --uuid /dev/sdb2

6. Okey,.. I'm almost done. The final step is to boot. The GRUB loading can bee seen and then the boot menu. You can either edit the menu and boot the kernel on the hard disk or boot the kernel on the flash drive.

There is no init on the flash drive because I didn't put it on the drive. So when booting, it show an error message, and drops into a BusyBox shell. It works on the RAM (initramfs). In simple terms, there is a shell = we can make use of the kernel. :)

7. Restore the backup (personal data/ files) onto the FAT 32 partition on the flash drive. If you plug the flash drive inta a Windows machine, the whole thing may look like this:

Do not delete any of the partitions using the Windows Disk Management Console. If you want to format your data drive, you can go through My Computer and format. Do not touch the other one as it could ruin the entire filesystem on the flash drive which we have built so far. Also if you want to adjust the partition table, the safe option is to do it with Linux.

Finally, I have to say is I've experimented and learnt something new. And I wanted to share my experience here!

Thanks for reading.

Comments (2)


December 18, 2009 at 3:50 PM

Very nice post, thanks.

I'm about to install debian lenny but do not want to install grub on MBR.
Wish me luck :)

ශාකුන්තල | Shaakunthala

December 18, 2009 at 8:40 PM

Lenny is very flexible than Ubuntu as I think. As you prefer you can have either GRUB, GRUB2, or LILO as bootloader, or you can have none of them.

Watch your way carefully, and you'll definetely make it. So, good luck! :-)


Subscription Options

 Subscribe in a reader


Enter your email address:

to inscrease

and make me happy. :)

Other Blogs by the Author

The Author
ශාකුන්තල | Shaakunthala's items Go to ශාකුන්තල | Shaakunthala's photostream
free counters
Related Posts with Thumbnails