Shit Happens. (Or how to recover a lost parition)

Few days ago I decided I wanted to re-install Windows. There was nothing particularly wrong with my system, just that I prefer a squeaky clean machine (thanks to my OCD) and also I couldn't get Xilinx to run correctly, so I figured doing a fresh install might help.

It didn't.

The point being, anyone who's installed Windows on a machine that already has Ubuntu, would know that it erases the Master Boot Record (MBR). In other words, you won't be presented with the GRUB menu, so you won't be able to boot into Ubuntu again. This is a very well documented issue, and the simple workaround is to pop in a Live CD and reinstall GRUB. You can find out more about that here or here.

But turns out the guys at Microsoft have worse things in store.

So I log into a live session, trying to locate the exact partition where Ubuntu was installed, BUT, it's not to be found! There's no ext4 partition listed in the Partition Table, but just a chunk of "Unused" space, the exact size of the partition that held Ubuntu. (What a co-incidence).

My immediate reaction was to panic, reformat and re-install, but thankfully I didn't.

I guessed that my data was still in place, but it wasn't detecting the drive for some reason. I started googling various permutations of 'recover lost parition linux' and came across two possible solutions. One of them worked, one didn't. Turns out what had happened was that my partition table was messed up and needed to be rewritten, but I had no idea why.

The first fix I came across was a utility called gpart (No, it's not the same as gparted) and scan the hard drive.
sudo apt-get install gpart               --Installs gpart
sudo gpart /dev/sda                 --Scans your hard drive for paritions 
sudo -W /dev/sda /dev/sda         --Rewrites the partition table

(WARNING : Verify the output of the second command before using the last one)

Not only did this take an excruciating amount of time and get me really nervous, but it also didn't work. It couldn't locate the lost partition. Fail.

The second fix (the one which worked) was a program called 'testdisk'. All you need to do is :

sudo apt-get install testdisk
sudo testdisk

It took me only about a second, and I was presented with a text-based utility that displayed the "corrected" partition table and it was restored in all its glory. I reinstalled GRUB, and life was back to normal again :D


  1. The exact same thing happened to me! But I re-installed Ubuntu in that unused space :P Then my CMOS battery died. I wanted to kill myself.

Post a Comment