How to lose and recover your partition table

7 Dec 2002

Dramatis Personae

Prologue

Nandini: Chetan, my Windows is not booting.
Me: What does it say?
Nandini: It stops at the first progress bar, giving some weird error.
Me: OK. Let's see.

The error was something like "Could not read file WinNT/system32/config/system". We booted into Linux and mounted the Windows partition. Tried opening the file; vi said "Read Errors". It was a binary file. There was another file called "system.???". Renamed it. Tried booting. Same error.
Again booted into Linux. I asked her if she has anything important on Windows. Some stuff was backed up on the Linux partitions. Then I deleted the damn file. "Na rahega baans, na bajegi baansoori."
Windows booted like a dream! But on starting some programs it gave errors like "Could not save C:\...\asa1212.tmp" etc. Repeatedly. One after another. Hmmm. Maybe this partition was read-only. How the hell does that happen in Windows?
Anyway, Nandini said she wanted to reinstall Windows. OK. She had the Win2K CD. I booted into Linux and copied the first sector of the hard disk to a file called "MBR.grub" in root's home dir. This was in anticipation of Windows overwriting the boot sector. She installed Win2K. Now on booting, it went directly to Windows, as expected.

This is where things get interesting.

The (fateful) sequence of events

  1. Booted from RedHat 7.3 CDROM, entered rescue mode. It recognised the existing linux partitions. Copied the first sector of /dev/hda to /root/MBR.win, using dd. Then copied the file /root/MBR.grub (saved earlier) to /dev/hda.
  2. Try booting. Grub appears. Select Linux - boots Linux properly. But when Windows is selected in grub, it pops up Grub again.
  3. Again booted using RedHat CDROM. Copied /root/MBR.win to /dev/hda.
  4. Try booting. Nothing. Just a blank screen. No error message. Nothing!
  5. Panic.
  6. Try booting using RedHat CDROM. Now it doesn't find any Linux partitions. Only the raw device /dev/hda available.
  7. Panic!

Assessment

According to me, whatever I did should have worked; but it didn't. Satyajit said I should have run grub again to write the new MBR instead of using dd directly. Hmmm.

Anyway, the condition was that the partition table was lost. Sachin said it may be possible to restore it using DOS's fdisk (fdisk /mbr). But how to get a DOS command line. No floppy available. Sachin booted from the Win2K CDROM and got into a repair command line mode. "dir" gave errors. There were commands like FIXMBR and FIXBOOT available in the repair mode. Tried them both. "dir" started working. But it showed nothing in C:\. Noticed that the free space is all wrong. Realised that this is all rubbish. Hmmm.

Talked with Nandini. Any backups? "No". All assignments. All downloaded papers etc. A semester's worth of customizations. Hmmmmm.

Ray of hope

Searched on the net. Found a utility called "gpart". What it does is read the entire hard disk, block by block, and tries to find partition begin patterns. In this way it "guesses" your partition table. Hmmm. Decided to give it a try.
Download. Compile. Tried running on my machine. Started scanning. Ajay got a floppy from somewhere. Booted Nandini's machine in Linux rescue mode. Copied the gpart binary using the floppy. Ran it. It seemed like it would take some time.
Sachin and me went to StaffC. Nandini went to her hostel to sleep. (It was 3:15am)
On returning, we saw that gpart had done its job. We examined the results. Hmmm. We decided to fight with this in the morning.

Woke up at 11am. After reading up some more documentation (which didn't raise my spirits), I tried to make sense of the gpart output. Hmmm.
After about an hour of this "study", I tried writing my own partition table using Linux's fdisk. Tried booting. Nothing. Same blank screen. Hmmmm.

Happy ending

After "studying" the gpart stuff some more, I gave up on it. Frustrated, I decided to give the partition table "guessed" by it a try (although it looked very wrong to me). What the hell - can't get any worse.
Wrote the partition table and rebooted using the Linux CD. This time, it said it has found some Linux partitions but there appear to be some errors. I said go ahead and mount them. After some more warnings, it mounted them and threw me in the shell. It was there! Everything! I heaved a sigh of relief.

Now, to get a working system, Satyajit suggested I use lilo instead of grub. We edited a simple lilo.conf and ran lilo. Reboot. It works. Like a dream! No problems!

Morals of the story

Related links

Back to personal home