How to recover lost GRUB bootloader in Linux


Grub 1


There are various reason one’s MBR can get corrupted, or probably and most commonly being rewritten by Windows installer, causing installed Linux and other operating systems unbootable. The following example uses the Ubuntu Live CD restore the MBR from a Linux installation.

Once booted and everything is loaded, go to the console to mount necessary filesystem, assuming Linux is installed in /dev/sda1

ubuntu@ubuntu:~$ sudo mount -o ro /dev/sda1 /media/
ubuntu@ubuntu:~$ sudo mount -o bind /dev/ /media/dev/
ubuntu@ubuntu:~$ sudo mount -o bind /proc/ /media/proc/

The next step is to chroot into the existing Linux installation;

ubuntu@ubuntu:~$ sudo chroot /media/

and run grub from there

bash:~# grub

Assuming Linux is installed in the first partition of the first harddisk, run the following actions would restore the MBR. If the the installation is done at some other partition or harddisk, change the (hd0,0) and (hd0) accordingly.

    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename.]

grub> root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83

grub> setup (hd0)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd0)"...  16 sectors are embedded.
 Running "install /boot/grub/stage1 (hd0) (hd0)1+16 p (hd0,0)/boot/grub/stage2
/boot/grub/grub.conf"... succeeded

grub> quit

Before rebooting the system to see if it worked, it might be good to unmount whatever mounted before;

ubuntu@ubuntu:~$ sudo umount /media/proc/
ubuntu@ubuntu:~$ sudo umount /media/dev/
ubuntu@ubuntu:~$ sudo umount /media

Grub 2