Reinstall Ubuntu Grub Bootloader After Windows Wipes it Out

If you run a dual-boot system with Linux and Windows, this has happened to you. You had to do your monthly reinstall of Windows, and now you don't see the linux bootloader anymore, so you can't boot into Ubuntu or whatever flavor of linux you prefer.

Here's the quick and easy way to re-enable Grub.

This option will use the Desktop/Live CD to install Grub into your MBR (Master Boot Record). This option will overwrite your Windows Boot Loader. It is OK to do this, in fact that is the goal of this how to (in order to boot Ubuntu) B)

1. Boot the Desktop/Live CD. (Use Ubuntu 8.04 or later)

2. Open a terminal (Applications -> Accessories -> Terminal)

3. Start grub as root with the following command

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. If not, then adjust accordingly.


4. You will get a grub prompt (see below) which we will use to find the root partition and install grub to the MBR (hd0)

> sudo grub


         [ Minimal BASH-like line editing is supported.   For
the first word, TAB lists possible command
completions. Anywhere else TAB lists the possible
completions of a device/filename. ]

grub>

Type the following and press enter: find /boot/grub/stage1


> root (hd0,0)

> setup (hd0)

> exit

Reboot (removing the livecd), and your boot menu should be back.

Only read below if Windows is now missing from the boot menu

If you installed Ubuntu before you installed Windows, then Ubuntu will not have anything in the grub configuration for Windows. This is where you'll have to do a bit of manual editing to the grub boot menu file.

If you open the file /boot/grub/menu.lst with the following command:

sudo gedit /boot/grub/menu.lst

You'll see a sample section for Windows, which you'll want to uncomment and add to the boot menu list in whatever position you want it in. (uncomment by removing the #'s)

# title Windows 95/98/NT/2000
# root (hd0,0)
# makeactive
# chainloader +1

Note that you should also verify that hd0,0 is the correct location for Windows. If you had installed Windows on the 4th partition on the drive, then you should change it to (hd0,3)


===============================================================

===============================================================



Type the following and press enter:

find /boot/grub/stage1
If you get "Error 15: File not found", try the following:
find /grub/stage1
Using this information, set the root device (fill in X,Y with whatever the find command returned):
grub> root (hdX,Y)
Install Grub:
grub> setup (hd0)

(In case you want to install GRUB to another drive (like hdX) use the above command as 'setup (hdX)' and then continue)

  • Exit Grub:
    grub> quit

5. Reboot (to hard drive). Grub should be installed and both Ubuntu and Windows should have been automatically detected.

6. If, after installing grub, Windows will not boot you may need to edit /boot/grub/menu.lst (That is a small "L" and not the number 1 in menu.lst)

  • Open a terminal and enter :
     gksu gedit /boot/grub/menu.lst
    Or, in Kubuntu:
     kdesu kate /boot/grub/menu.lst
    Your Windows stanza should look something like this :
     title Windows XP/Vista # You can use any title you wish, this will appear on your grub boot menu
    rootnoverify (hd0,0) #(hd0,0) will be most common, you may need to adjust accordingly
    makeactive
    chainloader +1
    Note: Put your Windows stanza before or after AUTOMAGIC KERNEL LIST in the menu.lst

Overwriting the Windows bootloader

Boot from a Live CD and open a terminal. You'll need to run a few commands as root so you can use sudo -i to get a root shell and run them normally instead of using sudo on each of them. Be extra careful when running a root shell, especially for typos !

We'll need to find which partition your Ubuntu system is installed on. Type the command fdisk -l. It will output a list of all your partitions, for example :

fdisk -l

Disk /dev/hda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 1 8 64228+ 83 Linux
/dev/hda2 9 1224 9767520 83 Linux
/dev/hda3 * 1225 2440 9767520 a5 FreeBSD
/dev/hda4 2441 14593 97618972+ 5 Extended
/dev/hda5 14532 14593 498015 82 Linux swap / Solaris
/dev/hda6 2441 14530 97112862 83 Linux

Partition table entries are not in disk order

Here I have three Linux partitions. /dev/hda2 is my root partition, /dev/hda1 is my /boot partition and /dev/hda6 is my /home partitions. If you only have one, obviously this is the one your Ubuntu system is installed on. If you have more than one and you don't know which one your Ubuntu is installed on, we'll look for it later. First, create a mountpoint for your partition, for example :

mkdir /mnt/root

Then mount your partition in it. If you don't know which one it is, then mount any of them, we'll se if it's the correct one.

mount -t ext3 /dev/hda2 /mnt/root

Of course, replace /dev/hda2 with the correct name of your partition. You can check if it's the correct one by running ls /mnt/root, which should output something like this :

bin    dev      home        lib    mnt   root     srv  usr
boot etc initrd lib64 opt sbin sys var
cdrom initrd.img media proc selinux tmp vmlinuz

If what you have looks not at all like this, you didn't mount the correct partition. Do umount /mnt/root to unmount it and try another one. You also need to mount your /boot partition if you made one, like this :

mount -t ext3 /dev/hda1 /mnt/root/boot

To make sure it was the correct one, run ls /mnt/root/boot, which sould output something like this :

config-2.6.18-3-686      initrd.img-2.6.18-3-686.bak  System.map-2.6.18-3-686
grub lost+found vmlinuz-2.6.18-3-686
initrd.img-2.6.18-3-686 memtest86+.bin

Once again, if what you have doesn't fit, unmount it and try another partition.

Now that everything is mounted, we just need to reinstall GRUB :

sudo grub-install --root-directory=/mnt/root /dev/hda

If you got BIOS warnings try:

sudo grub-install --root-directory=/mnt/root /dev/hda --recheck

Of course, replace /dev/hda with the location you want to install GRUB on. If all went well, you should see something like this :

Installation finished. No error reported.
This is the contents of the device map /boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(hd0) /dev/hda
Now you can reboot and the GRUB menu should appear. If you see a warning message regarding XFS filesystem, you can ignore it.

Comments

Popular posts from this blog

PostgreSQL bytea and oid

Microservices Architecture with Spring Boot in 15mins

Adding MySQL datasource to JBOSS AS 7