Wednesday, April 1, 2009

How to fix Grub using a Live CD

A common problem for users of multiple operating systems is the frequency with which other boot loaders break Grub. Whereas Grub is happy to leave other operating systems’ data well enough alone, the Windows installer offers few options as to placement of the boot loader.
Luckily repairing Grub doesn’t require an OS reinstall, and is easily accomplished using the command line.

Before you fix Grub, you need to know 2 things:

1) The hard drive you’d like to install Grub to (usually your main boot drive)
2) The partition that contains your /boot folder.

The way in which Grub numbers disks is different from Linux, so care needs to be taken when configuring it. Whereas Linux starts its labeling with a for disks and 1 for partitions (so that /dev/sda1 is the first partition of the first disk), Grub starts at 0 for both (so that hd(0,0) would be the first partition of the first disk)

With this in mind, we can use Grub to provide us with some of this information.

Assuming you’ve booted into the Live CD, open terminal and launch Grub by typing:

sudo grub

This will should give you a command line that looks as follows:

grub>


Now, we will use the find command within Grub to tell us on which partition the /boot directory is located.

find /boot/grub/stage1


In my case, Grub returned the following:

(hd0,1)


I know this is correct, because my Linux partition is on my first disk (0) and is the second partition (1).

Next, tell Grub that this is where it needs to look to find its configuration data by using the root command.

root (hd0,1)


Next, reconfigure Grub to install to your boot partition. While Grub can’t tell us what drive this is, you can use clues such as the partition lay out to tell you which drive is which. In my case, the drive Grub sees as the first disk is my boot drive, so I issue the following command:

setup (hd0)


This tells Grub to install to the Master Boot Record of the first hard drive, hd0.
And that should get you back up and running. Grub will automatically create your boot menu using any kernels you have installed in your /boot directory and any modifications you’ve made to your /boot/grub/menu.lst in the past.

Restart your computer, cross your fingers, and boot as usual. If you encounter any errors make sure that installed Grub to the right drive, or that you pointed it at the right partition to find /boot.

Coming up tomorrow: Modifying menu.lst and forcing Grub to meet your demands.

0 comments:

Post a Comment