Search This Blog

Saturday, August 21, 2010

recover an accidentally overwritten partition table

shorter version:

Q. I accidentally overwrote the partition table on my computer. Now it does not boot. Is there any way to restore it back?
A. use the testdisk program. Debian users can install it by doing

# apt-get install testdisk

To run it, use the command

# testdisk

This tool is very powerful, easy to use and interactive. It can be run from a live CD. I used it to recover the ext3, Linux swap partition information.


Long story:

My favorite tool for partitioning is qtparted. I got hooked to it couple of years back when I was trying out a knoppix live CD.

The hard drive of my laptop is /dev/sda. I recently bought a 500 GB external USB hard drive. When I connected the USB hard drive, it was recognized as /dev/sdb. I used the qtparted program to partition the /dev/sdb.



The partitioning process went smoothly. But when I click on the /dev/sdb link in the left tab of qtparted window, there were error messages on my konsole

$sudo qtparted
No Implementation: Support for opening ntfs file systems is not implemented yet.
Error: File system has an incompatible feature enabled. Compatible features are has_journal, dir_index, filetype, sparse_super and large_file. Use tune2fs or debugfs to remove features.
Error: File system has an incompatible feature enabled. Compatible features are has_journal, dir_index, filetype, sparse_super and large_file. Use tune2fs or debugfs to remove features.

Not sure of what it means, I resorted to cfdisk (a Curses based tool for partitioning).


My intention was to delete all the partitions on /dev/sdb and start afresh. However, I ended up deleting all the ext3, swap partitions on /dev/sda by accident. Notice how cfdisk only lists /dev/sda and nothing about /dev/sdb?

When the changes are committed, it gave an error. It asked me to reboot the machine in order to fix the error.

When the laptop is rebooted, Grub does not load. IIRC, the error code was 17. My computer is now officially in a half baked state with the wrong partition table (all because of my stupidity).

Kumar Appiah suggested me to try testdisk. He used it before to recover some FAT32, NTFS partitions.

So, I popped in an old Ubuntu (hoary) live CD to see if it has testdisk. It does not. The Hoary was released a while back. The official repositories were removed. So a simple

# sudo apt-get update

did not work. I modifed the /etc/apt/sources.list to point to dapper instead of hoary. Then did an apt-get update, apt-get install testdisk.

In order to install testdisk from dapper onto hoary, apt-get wanted to install a new kernal and remove the current kernel. I let the upgrade process run and gave it a 'no' when it wanted to remove the current kernel during the post configure stage. This exited the apt-get process abruptly.

Then, I did a "sudo apt-get -f install" to fix the inconsistent state. I then had to do another "sudo apt-get install testdisk". This time it proceeded without any difficulty.

Once testdisk is installed in the RAM, running it was easy. Just execute

# testdisk

The instructions in testdisk screens are very clear. At the end, I was able to recover my original partition table. Kudos to the authors, Debian maintainers of testdisk and to Kumar for suggesting it.


Useful links:
. websites of testdisk , qtparted
. links to debian packages testdisk, qtparted, e2fsprogs

Followers