Reinstall, Restore and Repair Dual-boot GRUB over Windows
(Rev 1.2 updated 5/2011)

© 2006-2011 Brian Mork - Fall 2006
Increa Technology

Site Index  •  Wiki  • Blog

GRUB and Master Boot Record

GRUB logo

Web lore indicates that Windows is not friendly when installed after/over Linux. Linux should normally installed after Windows in a dual-boot system, because it's nice enough to leave the system set up to dual boot into both.  Installing Windows second sort of destroys Linux boot capability.

This document describes how to put GRUB (GRand Unified Bootloader) back on a hard drive that has been taken over by a Windows 2000 install, which itself was installed after Linux, which was installed after Windows XP.  The critical concept is how to put GRUB back on the Master Boot Record, which is taken over during a Windows install. Bibliography and additional references are at the end.

Mandriva 2007 logo  Mandriva Linux vs. Windows XP

My daughter's laptop computer at college crashed.  I had the Windows XP install CD-ROM, and the plan was to send it to her and talk her through the installation.  To be sure I had the step-by-step instructions memorized, I used the CD to install Windows XP on a local hard drive first, while writing down each step.  I also wanted to include a Mandriva Linux Operating System on the same hard drive, so I directed the Windows XP Pro install process to only use the first 10 GB of the hard drive.

During the install process, Windows XP refused to fully register. When it contacted Microsoft over the Internet, their database properly knew that this operating system was licensed to run only on my daughter's laptop.  So, I knew I only had 30 days to use XP.  But that was no problem because the main reason to install it was accomplished. I intended normal work to be done on this system using Linux, allowing the first 10 GB to be a play partition where I could re-install Windows XP if I wanted, or eventually let Linux have it.

In order to put Linux on the rest of the drive (beyond the first 10 GB), I had to get install disks.  Normally, a person could download the Mandriva Install CD-ROM Image(s) and install from a CD-ROM.  Interested in doing something different with my 250 Kbit/s Internet link, I decided to download the small, bootable Mandriva Network Install CD-ROM.  Doing the network install downloads the applications only if you select them.  Of course, the down-side is that you have to have a reasonably fast internet available during the installation procedure. The 2006 Mandriva Network Install CD-ROM image for a normal PC computer is only 26 MB, so it fits nicely on a small 50 MB CD-ROM, and initiates the install process.

During install, the Mandriva sequence allowed me to create partitions on the four primary partition table entries of /dev/hda:

0 10 GB Window (leave it as is)
00 6 GB Linux root filesystem
400 MB Linux swap space
0 60 GB Linux home filesystem

I chose the GRUB boot program and the Gnome deskstop, and have been pleased with both.

The GRUB loader is nice because the piece of code that is actually written on either the Master Boot Record of the drive or on the Boot Sector of a partition, is static.  It's smart enough to reach onto a Linux filesystem and read the rest of the files it needs to operate.  One of these files is the the /boot/grub/menu.lst file, which lets GRUB know what operating systems are available to boot.

LILO LogoThe GRUB boot loader is often compared to the LILO boot loader, which reads its configuration file at install time, and puts a piece of code on the disk drive that goes directly to the right files based on lower-level sector and track mapping.  LILO is a little more efficient, and in theory lets you pull a boot program off a hard drive formatted in any file system or not formatted at all.  In practice, most people pull the boot program from a Linux file system anyhow, so they use and enjoy the addition run-time options of GRUB.  I've used LILO since 1994, and have just started using GRUB in the last year or so.  Here's the GRUB configuration file left on my computer after the Mandriva install procedure:

timeout 7
color black/cyan yellow/cyan
default 0

kernel (hd0,1)/boot/vmlinuz root=/dev/hda2 resume=/dev/hda3
splash=verbose vga=788
initrd (hd0,1)/boot/initrd.img

kernel (hd0,1)/boot/vmlinuz root=/dev/hda2 resume=/dev/hda3
initrd (hd0,1)/boot/initrd.img

kernel (hd0,1)/boot/vmlinuz root=/dev/hda2 failsafe resume=/dev/hda3
initrd (hd0,1)/boot/initrd.img

root (hd0,0)
chainloader +1

Even to the unitiated, it's probably obvious that there are four boot options, accessing partitions 0 or 1 on the first hard drive.  The first three boot into Linux with various options, and the fourth boots into Windows.

Windows Writes over Linux Boot Sectors

My dual boot Windows XP / Linux system I created worked great for a month.  I knew it was time to make some changes when I tried to boot into Windows XP, and as predicted, it refused to run because I had not registered the copy.

So, I got out the original Windows 2000 disks for this computer, and did an install onto the 10 GB first partition of the hard drive.  As expected from the non-friendly Windows, GRUB was written over, and Windows would boot only to Windows.

Putting Back Dual Boot: Mandriva Linux & Windows 2000

Gentoo logotomsrtbt logoWindows 2000 and Mandriva Linux were both on the disk in their respective partitions.  However, to put back the dual boot capability you'll have to boot Linux with something unrelated to this story.  I used a bootable Gentoo Linux Minimal/Install CD-ROM for the Intel x86 architecture, but I think I could have also used Tom's Root Boot Linux, which comes on a single bootable 1.44 MB floppy.  Research on the the Internet until you're comfortable acquiring and booting one of those.

Once inside of the Gentoo boot Linux, mount up the Mandriva root partition:

cd /mnt
mkdir mount-location
mount -t ext3fs /dev/hda2 /mnt/mount-location

In my Linux distribution there was an install script (named, of all things) left in place that runs GRUB, which automates three grub commands.  Here's a copy of the script file:

grub --device-map=/boot/grub/ --batch <<EOF
root (hd0,1)
setup --stage2=/boot/grub/stage2 (hd0)

Drop into the GRUB directory previously created during the Mandriva install and run the script, which will cause GRUB to re-write over the Master Boot Record:

chroot /mnt/mount-location /bin/bash
cd /boot/grub

The chroot command changes the root directory of the file system being used.  This allows me access to the GRUB program, the install scripts, and the other configuration files left on the Mandriva filesystem rather than be stuck in the context of the Gentoo boot environment which knows nothing of what I'm trying to do.  Also, the "./" might be new to Windows users.  For security and historical reasons, Linux will not automatically search the current directory for a program to run.  The "." directory explicitly directs the command processor to use the script found in the current directory.

Running the script should kick out a few lines of information. Poof!  The Master Boot Record is re-written with GRUB, and because the menu.lst file mentioned above is already in place on the Linux partition file system, full dual-boot capability has been restored.  Here is the output I received from the script when it ran GRUB:

    GNU GRUB  version 0.97-19mdv2007.0  (640K lower / 3072K upper memory)

       [ 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> root (hd0,1)
 Filesystem type is ext2fs, partition type 0x83
grub> setup --stage2=/boot/grub/stage2 (hd0)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd0)"...  15 sectors are embedded.
 Running "install --stage2=/boot/grub/stage2 /boot/grub/stage1 (hd0) (hd0)1+15 p (hd0,1)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded
grub> quit

I rebooted the computer using the exit and reboot commands.  The Mandriva/GRUB boot selection screen I remembered appeared. All is back to normal.

BTW, where the Windows 2000 Master Boot Record went? When running LILO, it saves a copy of the 512 byte sector it's about to write over into a file called boot.0300 (assuming the MBR on the first disk) in the /boot directory. I believe GRUB does not do this because I removed the boot.0300 file first, and it did not re-appear after running the script.  I think the Windows 2000 512-byte Master Boot Record was lost.

Bibliography / Additional Resources

How to recover from 'grub-install /dev/hda1' - Reverse problem. Having accidently slapped GRUB onto a boot sector of a partition, this explains how to put the Windows boot sector back. I was able to do this trivially with the dd program when I used LILO, because LILO saves a copy of the original boot sector before it writes into it. In my opinion, GRUB should do this, too. Emphasizes the difference between hda (MBR) and hda1 (boot sector of a partition).
Windows reinstall messes up grub - After re-installing Windows, this user wanted to get back a Ubuntu Linux boot option.
Recovering Ubuntu After Installing Windows - Uses Ubuntu specific procedure, rather than generic procedure documented in the page you're reading.
How to Repair a Corrupt MBR and boot into Linux - Same accomplishment, but slanted toward a Fedora Linux system. Includes LILO instructions. Includes Knoppix instructions.

nvu logoValid HTML 4.01 Transitional

gnome logoabiword logoopenoffice logoRevision 1.1 - This document was originally created using AbiWord under the Gnome desktop. AbiWord has a lot smaller footprint than Open Office and still does Word compatible files. One problem: I saved the file as an .html file and when I went back to read it in, AbiWord could not read it. Hmm... I did subsequent editing with the Nvu web page editor and the TextPad text editor.