Monday, June 21, 2010

Restore Grub 2.0 after Windows 7 Install (Ubuntu 10.04 or 9.10)

Ubuntu 9.10 is already installed on a machine and you want to dual boot it with Windows 7. Dual booting with Windows 7 already installed is easy as described here. The reverse is not that difficult either. You first resize your Ubuntu partition, then create a Windows NTFS partition, you can do so using Ubuntu Live CD or a Gparted Live CD. Then boot with Windows DVD and install it. The problem is that the Grub would be gone and you won’t be able to boot into Ubuntu anymore.

This post discusses the easy way of restoring Grub2 using Ubuntu Live CD. Boot with the Ubuntu Live CD. Now mount the partition which has Ubuntu installed on it. If you had a separate partition for /boot then you would have to mount that too. Mounting the partitions is super easy. Just go to Places in the top Gnome menu and try browsing to your corresponding partition by clicking on it. It would automatically get mounted and open in nautilus.

Now just run the following command to install Grub2 in the Master Boot Record (MBR) of your first booting hard drive.

sudo grub-setup -d /media/XXXXX/boot/grub -m /media/YYYYY/boot/grub/device.map /dev/sda

Where XXXXX represents the name or UUID of the partition that you have mounted. If your /boot partition is separate from your / partition then YYYYY above would be different from XXXXX, else both of them would be identical. The command above assumes that /dev/sda is your primary or first device in hard disk boot order. If not then replace it with /dev/sdb or sd_ fill the blank with appropriate letter.

After you have run the command, reboot and you should be able to boot in Ubuntu but not in Windows, to fix it run the following command in a terminal and Grub boot list would be updated to include Windows in it. Also if you want to change boot order read this.

sudo update-grub

Now reboot and you should have the choice to boot in both Windows and Ubuntu. For more information about Grub2 setup see this. Source: http://www.hackourlives.com