Sunday, July 22, 2007

Re-install GRUB after Windows install deletes it

Dual booting Windows and Linux gone wrong


When websites have tutorials about dual-booting Windows and Linux, they usually say to install Windows first and then install Linux after it. The reason for this is because Linux's bootloader detects your Windows partition and allows you to boot into it whereas Windows overwrites Linux's bootloader and replaces it with its own which only allows you to boot Windows. So that's why I install Windows first.

However, my Windows installation started acting unstable and I decided to reinstall it. Afterwards, I couldn't boot into Ubuntu (and Windows couldn't connect to the Internet, have the right resolution, etc. because I needed to reinstall the drivers...and everyone always talks about how everything "just works" after an install).

Reinstall GRUB


So this little tutorial tells you how to reinstall GRUB so you could choose your OS instead of being locked into one (and so you could reinstall Windows as necessary without worrying about breaking Linux).

If you use LILO or another bootloader, you could probably just google it. However, some suggestions don't work if you're repairing the MBR (Master Boot Record) from a LiveCD.

The Code


The following steps worked when I booted off the Ubuntu 6.10 LiveCD to repair the MBR but they should work on any CD that has GRUB.

Open a terminal and type the following in. The "#" is the prompt for root.

# grub
> find /boot/grub/stage1
   (hd0,1)

> root (hd0, 1)
> setup (hd0)

   ...   

> quit


How it works


The grub command takes you into the grub shell

  • the first line finds out on which hard drive and partition grub is installed on. In my case (hd0, 1), grub is installed on the first (and only) hd0 hard drive on the second partition (0 is the first)

  • the second line switches to the hdd and partition where grub is installed

  • setup tells grub to install itself onto the MBR so that grub becomes your default bootloader when booting off that hard drive


[via ubuntuforums.org]

No comments: