This is a common problem you will face often. Even thought it was my first encounter with grub2 it wasn’t as simple as I expected. May be its because things are handled a bit differently in grub2 as compared to the old grub. Anyways, in this article I will tell you how to recover grub2 when you have lost it after windows installation, got some error while installing linux or somehow messed it up while changing its configuration. The below is good example.
If you have a live CD, using it boot into a live session and follow the following steps (don’t skip any unless you are know you are doing).
1) Open a terminal (Applications->accessories->terminal) and run this command
[ubuntu]$ sudo fdisk -l
2) The above command will list out the partition table of your hard disk. After identifying your linux installed partition. Run this command, I will use the partition as /dev/sda1 you will have to replace it with yours.
[ubuntu]$ sudo mount /dev/sda1 /mnt
3) If you have /boot on a different partition other then you need to mount it too. After identifying your /boot partition run this command (i am assuming it to be /dev/sda2)
[ubuntu]$ sudo mount /dev/sda2 /mnt/boot
4) Now, mount the rest of the devices
[ubuntu]$ sudo mount –bind /dev /mnt/dev/
5) Now, using chroot we will be allowed to run commands considering a specified root directory.
[ubuntu]$ sudo chroot /mnt
Now, we will be chrooted as root considering /mnt as the root directory. So, from now we won’t need sudo to execute commands as root
6) If you need to make some changes to /etc/default/grub, open the file in your favourite editor ( i will use vim)
[root]$ vi /etc/default/grub
7) Make whatever changes you want to make and run update-grub to create the configuration file.
8) If you want to install grub2 to MBR run this command.
[root]$ grub-install /dev/sda
If you want to install it to some partition then use that partition name instead of /dev/sda e.g. /dev/sda1 for the second partition etc. Or if you want to install grub2 on another drive then you may use that name e.g. /dev/sdb for an external hard disk.
You will have to resolve the names using fdisk -l command. (be careful while doing that)
9) If you faced some error while installing grub2 then you may want to run it with –recheck attribute.
[root]$ grub-install –recheck /dev/sda
10) Once you are done with this either press Ctrl-D to exit chroot or type exit.
11) And then umount the partition you have mounted starting from /dev
[ubuntu]$ sudo umount /mnt/dev
[ubuntu]$ sudo umount /mnt