Sunday, November 21, 2010

Grub Bootloader Recovery

In this method that I am going to describe Grub boot-loader will be installed into the Master Boot Record (MBR) of the main computer hard drive. I have Ubuntu & OpenSUSE dual boot. I first installed Ubuntu and then OpenSUSE. So OpenSUSE's bootloader overwrites Ubuntu's. But the new bootloader couldn't load Ubuntu. So I had to reinstall the Ubuntu bootloader and then able to load Ubuntu. It has no problem loading OpenSUSE .


At first you have to determine whether Ubuntu Operating System installed on your computer was configured for Grub or Grub 2.
- If you fresh-installed Ubuntu Karmic 9.10 or newer, you are running Grub2.
- If you ran previous version of Ubuntu and upgraded to Ubuntu Karmic 9.10, you are running Grub Legacy by default; unless you have executed upgrade-from-grub-legacy, then you are running Grub2.
- If you are running Ubuntu Jaunty or Intrepid, you are running Grub Legacy.
I only here describe how to recover Grub 2.

To start the recovering process you need an Ubuntu live CD. Boot the computer with the live CD.

You can check the current version installed on your system via Synaptic or from the command line. Open a Terminal and enter the following:
$ sudo grub-install -v
grub-install (GNU GRUB 1.98-1ubuntu7)
GRUB 2 should display a version number of 1.96 or later. Legacy GRUB is version 0.97.

Another way to discover whether you use grub legacy or grub 2 is to check it's configuration file extenction. To do this go to /boot/grub.
- Grub Legacy uses /boot/grub/menu.lst.
- Grub 2 uses uses /boot/grub/grub.cfg.
If you are still not sure, view the file and check whether syntax follows Grub2 conventions.

Caution: Your existing MBR will be lost. A new Master Boot Record will be created (and all other OS will be included if present). This is to be expected.

Mount your Ubuntu partition from 'Places' menu.

Your Ubuntu partition is now mounted. You can verify if your partition is correct using the Terminal.

$ sudo mount | tail -1
You should see output similar to this:
/dev/sda2 on /media/0d104aff-ec8c-44c8-b811-92b993823444 type ext4 (rw,nosuid,nodev,uhelper=devkit)

Note the designation for the disk /dev/sda which you will be using later, and the directory in /media.

Now that everything is mounted, we just need to reinstall GRUB by specifying the correct directory and the correct drive name:
$ sudo grub-install --root-directory=/media/0d104aff-ec8c-44c8-b811-92b993823444 /dev/sda

If you get BIOS warnings try:
$ sudo grub-install --root-directory=/media/0d104aff-ec8c-44c8-b811-92b993823444 /dev/sda --recheck
Replace /dev/sda with the location you want to install GRUB on.

If all went well, you should see something like this:

Installation finished. No error reported.
This is the contents of the device map /boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.


(hd0)   /dev/sda

You can also use 'grub-setup -d' command instead of 'grub-install'. This will reinstall the Grub2 files on the mounted partition to the proper location and to the MBR of the designated device. I haven't tried this method. But it will work.
$ sudo grub-setup -d /media/0d104aff-ec8c-44c8-b811-92b993823444/boot/grub /dev/sda
If you get an "error: Cannot open /boot/grub/device.map" message, add the -m switch and path to the device.map to the command as follows:
$ sudo grub-setup -d /media/0d104aff-ec8c-44c8-b811-92b993823444/boot/grub -m /media/0d104aff-ec8c-44c8-b811-92b993823444/boot/grub/device.map /dev/sda
If you get a repeated "error: Cannot open /boot/grub/device.map" message. If this happens try rebooting. The UUID method may actually work but throws an error message falsely. Using the label name does not seem to evoke the same repeated error message.

Reboot and make sure to boot to your hard drive and not to the live CD.

Updating Grub 2
Since I use OpenSUSE with Ububutu in a dual boot environment, whenever I update OpenSUSE kernel, it fails to load. It's because OpenSUSE is in new kernel but Ubuntu's Grub is with the old one.
The Solution is very simple. All I do is load the computer with Ubuntu and then from the terminal type the following
$ sudo update-grub2
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.32-25-generic
Found initrd image: /boot/initrd.img-2.6.32-25-generic
Found memtest86+ image: /boot/memtest86+.bin
Found openSUSE 11.3 (i586) on /dev/sda9
done
That's all. Now all I have to do is reboot and select OpenSUSE from Grub to load the OS.

Other Grub Commands
The following commands may prove useful in locating and/or fixing a problem.
To look for the bootloader location.
$ sudo grub-probe -t device /boot/grub
To install GRUB 2 to the sdX partition's MBR (sda, sdb, etc.)
$ sudo grub-install /dev/sdX
To recheck the installation. (sda, sdb, etc.)
$ sudo grub-install --recheck /dev/sdX

Reference
This article describes how to recover both grub legacy and grub 2 in various methods and has in depth technical details.
This article describes several methods about how to recover Grub2 from Ubuntu live CD.

No comments:

Post a Comment