This guide teaches you how to install/re-install GRUB2 in Linux. GRUB2 is a boot loader that is used to install Windows and Linux on the same PC.

What is chroot ?

chroot is a tool that allows you to run a command or interactive shell with a different root directory.

The Scenario

Partition Table

Partition table of my PC

The picture above shows my machine’s partition. As you can see, I have installed Ubuntu GRUB2 in the MBR (Master Boot Record). In the first partition, I have Windows 7, in the next partition Ubuntu 11.10, then comes the Swap partition, and lastly in the fourth partition, I have Fedora 16.

Install GRUB2 in Boot Partition

As an example, I will show you how  to install GRUB2 in the boot partition of Fedora which in my case is /dev/sda4.

These are the steps to follow :

Step 1 : Boot into Ubuntu

Step 2 : Mount the partition containing Fedora 16 (/dev/sda4)

$ sudo mount -t ext4 -o rw /dev/sda4 /media

The above command will mount the /dev/sda4 partition into the /media directory in Ubuntu and will give you read write access.

Step 3 : Next you have to mount the following paths in the respective locations in the /media namely – /dev, /sys, proc, and devpts.

$ sudo mount -t proc none /media/proc
$ sudo mount -o bind /dev /media/dev
$ sudo mount -o bind /sys /media/sys
$ sudo mount -t devpts devpts /media/dev/pts

Step 4 : chroot into the Fedora partition

$ sudo chroot /media

Now you get access to the root shell of the Fedora partition. From now on, any command you execute in this shell will be run in relation to the Fedora distro and not Ubuntu.

Step 5 : Install GRUB2 in the boot partition of Fedora

# grub2-install --force /dev/sda4

Note: You have to use the --force flag to force the installation of GRUB2 in the boot partition. Or else it gives an error.

Step 6 : Lastly come out of the chrooted shell and un-mount the /media directory.

# exit

Back in Ubuntu environment, un-mount the previously mounted directories by running the following series of commands.

$ sudo umount /media/proc
$ sudo umount /media/dev/pts
$ sudo umount /media/sys
$ sudo umount /media/dev
$ sudo umount /media

Finally, GRUB2 has been installed in the boot partition of Fedora 16.

Note : The above procedure can be used to fix MBR as well. The only difference being, instead of initially booting into a Linux distribution installed on your machine, you use a liveCD.