How to install GRUB on a USB stick

Q I have a laptop with the default OS installed on hda1. I installed Debian on hda2 but installed Grub on hda2 instead of the MBR. I wanted to take this opportunity to learn how to install Grub on to a USB stick instead of just re-installing Linux, but after much Googling, I am still unable to do it. Is it possible to get Grub installed on to a USB stick, without also installing Linux to it?

A There are at least three ways to get your Grub setup working. You could modify the existing (Windows?) bootloader to chainload Grub; you could install Grub to the MBR or you could set up Grub on a removable device, like a floppy disk or USB stick. To chainload Grub from Windows NTLDR (New Technology Loader), you need a copy of your Grub boot sector on the Windows drive. Do this while in Debian

dd if=/dev/hda2 of=lin-boot.img bs=512 count=1

This creates a file called lin-boot.img (the name is unimportant) that contains the first 512 bytes of the partition containing Grub. Copy this file to the Windows C: drive, either by mounting your Windows partition in Debian or copying the file to a USB stick and then copying from that in Windows. Now reboot into Windows, copy lin-boot.img into C: if you haven't already done so, and edit C:\boot.ini in Notepad to add this line to the end

C:\lin-boot-img="Debian GNU/Linux"

Now the Windows bootloader will have two options, with the second passing control to Grub from hda2. The second option, and the one I prefer, is to let Grub handle everything. Run grub as root to open the Grub shell and install it to the MBR of the first disk with

root (hd0,1)
setup (hd0)
quit

This installs Grub to the first disk, (hd0), after telling it to look for its files in hda2 (hd1,1) remember that Grub counts from zero. Now you need to modify /boot/grub/menu.lst (some distros use /boot/grub/grub.conf) to add the Windows menu entry, like so

title Windows
rootnoverify (hd0,0)
chainloader +1

which simply tells Grub to pass control of the boot process to the bootloader found at /dev/hda1, which is where Windows keeps its bootloader (this is why Windows needs the first partition to be marked bootable, because the bootloader is on the partition, not the MBR). The final option is to place Grub on a removable device, such as a floppy disk or USB stick. This will be slower than using Grub files on the hard disk, but it does provide a useful backup should the MBR bootloader become corrupted. To do this, copy the /boot/grub directory from your hard disk to the removable device (it must still be boot/grub on that device). Now you need to set up Grub on the device. To make it easier to find the correct device number in Grub, first do

touch /media/sda1/findme

replacing sda1 with the actual mountpoint. Now run grub and do

find /findme

which will return the Grub designation for your device, say (hd1,0), then do

root (hd1,0)
setup (hd1)
quit

Grub is now set up and ready to boot on the USB stick, so try it. It is possible you may get a 'File not found' error when trying to boot Linux from the new boot menu. This is caused by the BIOS switching the hard disk and USB stick around when booting from the stick. The cure is to replace all calls to hd0 in menu.list with hd1, or remove all absolute paths and put root (hd1,1) at the top of the file. Note that USB booting is a bit of a black art - not all BIOSes and USB sticks cooperate, so if it refuses to boot at all, you may need to experiment with BIOS boot settings or try a different stick. If you decide to place Grub on the MBR of the disk and then follow the above steps to put it on a USB device, you will then have to repeat the procedure for the hard disk with the USB device removed, otherwise your hard disk boot will no longer work.

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar