Tuesday, May 17, 2011

How to recover Grub2 for Ubuntu 9.10 / 10.04

Recover Grub2 For Ubuntu 9.10 (It is also so far successful in 10.04).


IconsPage/cd.png
Reboot your computer and boot into a live session.
Mount your Ubuntu partition from Places menu. If you have multiple Ubuntu partitions (e.g., /boot, /home, /), you need to mount the /boot partition.
Screenshot-Mount_Disk.png

Are you using Grub or Grub 2?


IconsPage/query.png
Skip this section if you are sure about your version of Grub

Go into boot/grub directory and check for the existence of the files menu.lst and grub.cfg.
Screenshot-grub-file-browser.png
  • 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.
If you downloaded Ubuntu Karmic 9.10 Live CD, but your installed version of Ubuntu uses Grub Legacy, please use this guide

Overwriting the Master Boot Record


IconsPage/IconWarning3.png
Existing MBR will be lost. In order to dual-boot your computer, we will be creating a new Master Boot Record to include all operating systems, including Ubuntu Linux and Microsoft Windows. This is to be expected.

Your Ubuntu partition is now mounted. You will need to continue Using the Terminal.
Verify if your partition is correct.
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.
IconsPage/IconHint2.png Use Tab Completion in Terminal to complete the path. Hitting the key will automatically finish file names, directory locations, and other long or hard to type file names.
To make sure this is indeed the Ubuntu boot partition, run
ls /media/0d104aff-ec8c-44c8-b811-92b993823444/boot

But substitute the example's UUID, 0d104aff-ec8c-44c8-b811-92b993823444, with your volume's UUID which you found earlier. If your boot partition was a separate partition which you mounted in a previous step, use this instead
ls /media/0d104aff-ec8c-44c8-b811-92b993823444

In either case, the output should be something like this:
config-2.6.18-3-686      initrd.img-2.6.18-3-686.bak  System.map-2.6.18-3-686 grub                     lost+found                   vmlinuz-2.6.18-3-686 initrd.img-2.6.18-3-686  memtest86+.bin

If what you have is not similar, unmount it and try another partition.
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

IconsPage/warning.png 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

Reboot, making sure to boot to your hard drive and not to the live CD. Grub should be installed and both Ubuntu and Windows should have been automatically detected and listed in the menu.
The Master Boot Record will execute Grub as the initial boot-loader. The Windows boot-loader is contained within the Windows partition and will then be chainloaded by the Grub boot-loader.