Reinstaling Grub for Dual Boot After Reinstallation of Windows

Finally back to technical stuff. Dual boot is very common these days with people having Linux and windows both at the same time unleashing the power of both the OS at the same time.

If something goes wrong with Linux, then its pretty straight forward. Re-installation does the work automatically. But when the windows goes wrong, then is something problematic.

So, how to preserve the linux when windows is re-installed? To know how to do this, you need to first understand whats happening with your system during the whole process.

When Linux is installed as a dual boot on windows, some boot loader is installed to support both the operating systems. So, the Master Boot Record(MBR) is re-written to point to the linux boot loader(LILO or GRUB) when the Linux is installed on top of windows. Windows is booted as chainloading process.

Now, when you re-install the windows, the MBR is written by windows to directly boot the windows. So, despite that you have all the settings preserved for linux boot loader and the main operating system, you cannot boot into it.

So, what you have to fix is - you have to re-write the MBR to point to your boot loader which takes charge of the booting options.

Okay, too much theory now. Lets get the hands dirty. I am using Ubuntu 8.10 for this purpose. A complete process with multiple options is given at the Ubuntu Community Help.

Things you need
- Live CD for the version of Operating System you are fixing. (As I said, I am using Ubuntu 8.10).


Step 1
Boot using the Live CD and open the console.


Step 2.
Now, you need to first identify the partition which is your root(/) partition. So you need to type
$ sudo 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/sda1 1 8 64228+ 83 Linux
/dev/sda2 9 1224 9767520 83 Linux
/dev/sda3 * 1225 2440 9767520 a5 FreeBSD
/dev/sda4 2441 14593 97618972+ 5 Extended
/dev/sda5 14532 14593 498015 82 Linux swap / Solaris
/dev/sda6 2441 14530 97112862 83 Linux

Partition table entries are not in disk order

Here /dev/sda1 is /boot partition of your Linux installation. Also, /dev/sda2 is your /(root) partition.


Step 3
Once identifying the required partitions, now you need to mount them. You type -
$ sudo mount /dev/sda2 /mnt
$ sudo mount /dev/sda1 /mnt/boot

Make sure /mnt/boot contains the grub files like it should have vmlinuz, a folder named grub etc.


Step 4
Now you are ready. Because you already had windows listed in the GRUB menu entry, there is nothing you have to do apart from re-installing the GRUB. So you type
$ sudo grub-install --root-directory=/mnt /dev/sda

You should get following message for successful install.

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/sda


Step 5
You are done. No more step. Reboot and find you GRUB as it was earlier. Enjoy and use windows carefully. Better option...use Linux often... ;)

Comments

Popular posts from this blog

Personal Contact Manager

An apt quote

Alone