Thursday, April 16, 2009

Restoring the Grub after reinstalling Windows

After reinstalling windows or fixing MBR(Master Boot Record) you always loose your grub and you cannot boot to your linux. So this post is about the methods of restoring the grub. There are three methods I use. I will explain all three.

All three methods need a live cd of a linux. You need to boot from the live cd and take the terminal and do the following:
  1. First method is using grub command. We initially go into the grub mode interface using the command
    sudo grub
    This will get you a "grub>" prompt (i.e. the grub shell). At grub>. enter these commands
    find /boot/grub/stage1
    If the find returned (hd0,1) then you would enter
    root (hd0,1)
    Now the root drive where the boot folder is mounted.Now we needed to setup the drive to boot to show the grub. We do this by
    setup (hd0)
    Finally exit the grub shell
    quit
    And then Reboot to and try if you are getting the grub.
    If not try the next method.
  2. If couldn't get grub to find the stage1 file or even recognize the drive. Use this method.
    sudo mkdir /mnt/root

    sudo mount -t ext3 /dev/sda6 /mnt/root

    Then you have to mount the proc subsystem and udev inside /mnt/root also:

    sudo mount -t proc none /mnt/root/proc

    sudo mount -o bind /dev /mnt/root/dev

    Doing this allows grub to discover your drives. Next you have to chroot:

    sudo chroot /mnt/root /bin/bash

    Now that you're chrooted into your drive as root everything should work.

    sudo grub
    And then follow the 1st method to install the grub.
  3. In lastest Linux you can use the command grub-install.The usage of this command is as
    sudo grub-install /dev/sda
    It might be hda if the hard disk is IDE and sda if disk is SATA.
So try it out the next time you loose your grub and tell me how it went.