Wednesday, December 17, 2008

HOW TO: Re-install GRUB after windows wipes it out

Problem:
You have installed Ubuntu and then realise that you need to dual-boot with windows. Unfortunately, after you install windows, in a separate partition, GRUB (Grand Unified Boot loader – the thing that tells ubuntu to load) gets erased! How do you successfully dual-boot?


Solution:

1. Boot off the Ubuntu LiveCD


2. Open a Terminal and type in the following commands, noting that the first command will put you into the GRUB "prompt", and the next 3 commands will be executed there; also note that hd0,0 implies the first hard drive and the first partition on that drive, which is where you probably installed GRUB to during installation:

$sudo grub
$ root (hd0,0)
$ setup (hd0)
$ exit
3. Reboot (removing the LiveCD), and your boot menu should be back.

Putting windows into the GRUB file, so that you can dual-boot:
1. Open the GRUB file:
$sudo gedit /boot/grub/menu.lst
2. Scroll to the bottom and add the following:
title Windows XP
root (hd0,0)
makeactive
chainloader +1
Note that you should also verify that hd0,0 is the correct location for Windows. E.g. if you had installed Windows on the 4th partition on the drive, then you should change it to (hd0,3).

Now when you restart your computer, hold down ESC and you will enter the GRUB menu

4 comments:

  1. In Step 2
    >find /boot/grub/stage1
    Depending on the result (ex: (hd0,4))
    >root (hd0,4)
    >setup (hd0)
    >quit

    ReplyDelete
  2. Thanks for pointing that out, but I don't want to over-complicate the guide. If this does not work for someone then they are welcome to comment on it. Again thanks for pointing it out!

    ReplyDelete
  3. You have to type
    quit
    to exit from grub prompt...

    ReplyDelete
  4. Before reinstalling Windows, just copy the Boot Sector in Linux and put if back afterwards.
    1. dd if=/dev/sda of=boot.bin bs=512 count=1
    2. then install windows
    3. use a Linux live CD to boot linux again
    4. dd if=boot.bin of/dev/sda bs=512 count=1
    5. reboot

    ReplyDelete