Wednesday, April 30, 2008

Installing GRUB on a USB drive

About a month ago, I finally bought a flash drive (a 4GB one) for myself, to replace the antique 256MB MP3 player. As this was intended to be a pen drive and not an MP3 player, it supported USB booting. Unfortunately, each time I plugged it in, the flash drive became the default hard disk, since it apparently had priority over SATA disks for booting. Now you might say I could simply set the SATA drive as the preferred hard disk for booting, but once you remove the drive, and the BIOS detects that it is missing, it no longer keeps a track of the flash drive, plug it in another time and it tries to boot from the flash drive. I did try looking for a way to disable USB booting, but to no avail. I couldn't locate it neither in the motherboard manual, nor in the BIOS itself. I could have probed the matter a bit more, but then I decided to install a GRUB in my pen drive. After all, it takes a mere 170KB, I could install a regular OS in this thing (4GB pen drive). Okay so here's how I did it.
Plug in the drive and make sure it has a file system, format it if it doesn't.
Make a folder called boot in the root directory of the pen drive. Or simply execute the following command in your terminal.
sudo mkdir -p /media/drive_name/boot
Of course, needless to say, you'd have to replace drive_name with the name your drive is mounted with.
Now you have to copy your grub to the boot directory you created. Use the following command to do so.
sudo cp -r /boot/grub /media/drive_name/boot
Now, there is a need to activate the GRUB prompt, to do so simply type
sudo grub
You will notice that the GRUB prompt (GRUB>) appears. Now we need to find where all the GRUB exists. In order to do so, type in the following in the GRUB prompt.
find /boot/grub/stage1
What this command did was that it looked for a file called stage1 (which I know exists in the grub directory) and reports where all it found it. This is what the output should look like,
Now to check out which of the hard-disks is the pen drive. Type in
geometry (hd0)
and it displays something like
drive 0x80: C/H/S = 3841/33/63, The number of sectors = 7987200, /dev/sdb
Partition num: 0, Filesystem type is fat, partition type 0xb
Okay, since I have set my pendrive as number hdd1, this is it. If you see more than one partitions, chances are this is not your flash drive, check out the next one that showed up, until you have found the one that you are sure is your pendrive. Now you don't want to rewrite your MBR when there is not the need to do so, do you?
Now that I know the hdd and partition name (hd0,0) in my case which would be different in your case, we need to specify the grub we need to install in the MBR by:
root (hd0,0)
Now to write the stage1 of the GRUB onto the boot sector of the USB, which would be different in your case
setup (hd0)
which would give a message like
setup (hd0)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/fat_stage1_5" exists... yes
Running "embed /boot/grub/fat_stage1_5 (hd0)"... 17 sectors are embedded.
Running "install /boot/grub/stage1 (hd0) (hd0)1+17 p (hd0,0)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded
Do I need to tell you that you are done once it says done? Well actually, I wasn't quite done here, but if you weren't facing the problem I was, this is your exit. Type
If you were, then I'm afraid you'd have to read on, since this is the main reason for writing this post. You see, each time you plug in your pendrive, the hd0 and the hd1 get messed up, because the boot order changes. So now, if your pendrive was hd1 like it was initially in my case, it becomes hd0 when it becomes the primary hdd. So now you need to change the grub in your pendrive to modify make this grub run. In order to allow you to make changes to all its files, type in this command in the terminal
sudo chmod -R 777 /media/usbdisk/boot/grub
Disclaimer : What ever I did from here were my own instincts, you could follow the steps since it worked for me, even if the steps don't work, they worst case scenario would be that you mess up the GRUB you copied, that didn't ever work (But make sure it doesn't work before you mess with it)
Now in the grub folder, in the boot folder you made in your pendrive, you will find a file called I simply interchanged the assignment of sda (the hard drive) with the sdb (the pen drive). Then in the menu.lst file, replace the drive numbers with their appropriate ones, like in my case, hd(0,9) was replaced with hd(1,9) in the case of linux booting, and hd(0,0) was replaced by hd(1,0) for the Windows Vista bootloader. But don't let this confuse you, my boot procedure is really messed up. I have a windows vista bootloader which manages XP by default and I added Linux to it using easy BCD. Let me finish my exams n I will sort things out. Instead of replacing the drive number in each place, you could add something like
map (hd0) (hd1)
map (hd1) (hd0)
which simply changes the mapping dynamically. It works, but you have to put it before each root instruction in the menu.lst Any way I guess that is it. In case anybody reading this needs any help, I am pretty sure I'd be proficient with stuff like this by then (going by the number of people who visit this blog). Though it would be a nice idea to join a good community, and ask your doubts there, because there are a lotta knowledgeable people out there, willing to help you with your problems.

Do visit my Linux blog

1 comment:

rax said...

hi,thanx for sharing this information.usb boot drive