Monday 3 December 2012

Recover grub using ubuntu live CD


Sometimes when you have both windows and other linux it is possible that windows somehow replaces  the bootloader with its own bootloader like NTLDR or MBR and this does not detect other operating systems. For example when you partition your hard drive using windows your bootloader is also replaced. Today I will show you how to recover grub using ubuntu live CD.
The key here is to mount the linux file system to a particular location and mounting all the required devices, virutal files..etc to the mount point and then changing the working root to the mount point and updating the grub file (grub.cfg or menu.lst)

  1. Boot with your ubuntu live CD and select try ubuntu.
  2. After loading ubuntu, open Terminal
  3. Now say your linux file system exists in /dev/sda1 (use any application like 'Disks', 'Gparted', 'fdisk' to find it)
    Mount all the required folders to your linux file system.

    $ sudo mount /dev/sda1 /mnt
    $ sudo mount --bind /proc /mnt/proc
    $ sudo mount --bind /sys /mnt/sys
    $ sudo mount --bind /dev /mnt/dev
    $ sudo mount --bind /usr /mnt/usr
  4. The key here is to change the working root to the one in your installed linux
    $ sudo chroot /mnt

    If you get any error here like '/bin/bash not found.' or something, it maybe because /bin and /lib are not there. So type the following commands to bind the existing /bin and /lib with the ones at /mnt and retry chrooting to /mnt (type the above command again):
    $ sudo mount --bind /bin /mnt/bin
    $ sudo mount --bind /lib /mnt/lib

    If the above problem still persist try mounting /dev/pts also (it has something to do with signature keys)
  5. Now simply update and install grub to replace the existing bootloader

    $ grub-install /dev/sda
    $ update-grub or $ update-grub2
    If you get any error in the above step try this:
    $ grub-mkconfig -o /boot/grub/grub.cfg

  6. Now reboot the system
Note: Sometimes it may happen that the version of the grub installed might be old. So to replace it with the original version simply boot using your linux and repeat step 5 with root permission.

Update:
Some people say that they get this error when the do update-grub :
/etc/grub.d/00_header: 28: .: Can't open /usr/share/grub/grub-mkconfig_lib

The reason is that some ubuntus have the required file for updating grub in a different place. So copy it to correct location:
$ sudo cp /usr/lib/grub/grub-mkconfig_lib /usr/share/grub/
and then update-grub

5 comments:

  1. Instead you can change the boot directory with --boot-directory option in grub-install

    ReplyDelete
  2. Yeah you can do that. But sometimes it wont work..!

    ReplyDelete
  3. Thank you for this, but "grub-install /dev/sda" instead of "grub-install"? Because that's how mine worked, the former gave me an error...

    ReplyDelete