One of the annoying things about dual booting is that a fresh windows install will always overwrite the master boot record (MBR) on the first hard drive thus overwriting Grub (or Lilo etc). As a result, after restarting the machine you’ll find yourself booting into Windows without the option of using any other operating system that may be installed. This can be pretty daunting for new users that find themselves unable to boot into their Linux system. This is especially problematic when you consider how often some people reinstall Windows!
The good news is that restoring Grub is really quite easy. However, it’s one of those things I always forget how to do, so I thought that I’d record it. In this example I am using Ubuntu Edgy. In order to restore Grub you will first need to boot into some sort of Linux live or rescue system. I chose the Ubuntu live CD since it was the closest to hand but Knoppix the father of all live distros would be another good choice. Simply inserting the CD and restart. If your computer doesn’t boot from the CD check that the BIOS is configured to allow booting from the CD drive and try again.
Once the boot options screen comes up, press enter to choose “Start or install Ubuntu”. This will start up the live system. Once your desktop comes up. ro restore Grub type the following at the command line:
sudo grub
You should now see a prompt like this:
grub>
Type the following:
find /boot/grub/stage2
This should return something like:
(hd0,0)
The find command searches for the file /boot/grub/stage2 on all partitions and prints the list of devices which contain the file. This file should be on the partition that your Linux system is installed on. The notation (hd0,0) means the first partition on the first disk, (hd1,2) would be the third partition on the second disk. To set this up as the boot partition type:
root (hd0,0)
being sure to replace (hd0,0) with whichever partition was returned by the find command. Finally, to setup grub type:
setup (hd0)
now exit grub by typing:
quit
You have now restored Grub to the MBR and you should now be able to boot back into your Linux system.
However, if you have installed Windows on a separate partition this only solves half the problem. To be able to boot into Windows you need to add it to the Grub menu. Boot back into your Linux system and follow the following steps. Once you’ve rebooted, you need to work out which partition you installed Windows on if you don’t know already. To get a list of all partitions with a bit of useful information type:
sudo fdisk -l
What you’re looking for is a partition with a windows filesystem type, ie NTFS (or maybe FAT32). You can pipe the output of the last command through egrep to narrow it down:
sudo fdisk -l |egrep -i “(ntfs|fat32)”
This gives me the output:
/dev/hda3 * 1367 4864 28097685 7 HPFS/NTFS
So I know that the only NTFS or FAT32 partition I have is on /dec/hda3. Now it’s just a matter of converting the /dev/hda3 notation to the Grub notation. In this case, /dev/hda3 refers to the third partition on the first hard disk. So in Grub’s notation it would be (hd0,2). Another example is /dev/hdb1 which refers to the first partition on the second disk which translates as (hd1,0).
Now all we have to do is add the appropriate entry to our Grub menu. Open the file /boot/grub/menu.lst as root in your favourite text editor, backing it up first as a precaution:
sudo cp /boot/grub/menu.lst /boot/grub/menu.list.bak
sudo gedit /boot/grub/menu.lst
Append the following to the end of the file:
title Windows
root (hd0,2)
savedefault
makeactive
chainloader +1
Remember to replace (hd0,2) with the appropriate disk/partition for your system. The title line here is arbitrary so feel to replace it with anything you want (eg Windows XP Pro). Once you’re done editing, save the file and restarting your computer. If all has gone well you should be able to boot into either Linux or Windows via Grub. I hope this has helped.
Cheers for the use of Worm Power RedGlow82!