Loading....
Recent Article links:

Archive for May, 2006

How I broke my partition table — and recovered it (with TestDisk)

Yesterday, I decided I’d install the latest version of Kubuntu, since I had a few problems with my current installation, and I wanted to try something new. Unfortunately, Kubuntu wouldn’t read my partition table, so I couldn’t install it.

It turned out the trouble was my partition table was pretty screwed, and had been for some time, so the Parted partition editor (which KUbuntu uses) downright refused to read it. The trouble was that for some reason, my Linux documents partition, while located inside my extended partition, was listed as a primary partition. My disk layout was something like this:

  • 1-10168: hda1, Windows NTFS
  • 10169-79408: hda2, extended
    • 10169-11205: hda5, Linux swap
    • 25532-29628: hda6, Linux data
    • 29628-38824: hda7, Windows NTFS data
    • 38824-49231: hda8, Linux data
    • 49232-61486: hda9, Linux root (somehow displayed as hda11 in Linux itself)
    • 61487-79408: hda10, Linux data for MP3s
  • 11205-25532: hda3, Linux /home

Apart from Parted choking on this there were some other problems as well: Windows also refused to boot from Lilo, probably because of the strange partitioning, and Windows calculated the total hard drive as 80GB in the partition editor, whereas it was actually 40GB.

Anyway, I decided to play around somewhat with Parted, hoping to try out its “guess partition” functionality to see whether it could find my partitions. So I cleared my partition table, and found it didn’t work. What I didn’t realize was that Parted directly applies your commands instead of waiting for a write command like fdisk. So I was left with a completely empty partition table. Also, I did know the cylinder numbers but feared that wouldn’t help me either because each time I start fdisk, it starts complaining how partitions are not aligned “at cylinder boundaries”. Also after my re-formatting, fdisk displayed the partition table in completely different units then before, so I couldn’t even try to enter the old information (yes, I did try the u command).

There were two documents that came to my rescue: the Partition-Rescue HOWTO, and this Dutch article called “Partitietabellenblues” (partition table blues). At first I was unlucky. As mentioned, Parted wouldn’t detect enything apart from my primary Windows partition, which wasn’t really useful. Gpart which was also on the KUbuntu rescue disc did a much better job, and managed to detect my primary Windows partition, and everything in the Extended partition. Gpart gives back exact locations, so you can then with fdisk create primary partitions for each location one at a time, and then recover what’s on that partition. Which was quite a relief.

Still, my documents partition hda3, which was my main concern, was nowhere to be found. I now had a range of a few MB where my hda3 was approximately starting, so I actually started looking for ext2 filesystem magic with a hex editor. Without much luck as I couldn’t really find anything. The Dutch article did describe the format of partition tables, and it was quite fun to see that if you just do a hexedit /dev/hda, you can actually directly see (and edit…) the partition table.

Finally, I stumbled across TestDisk, an open-source program that was unfortunately not included on my Ubuntu rescue disk. Luckily I had a second hard disk on which I could install Ubuntu to run TestDisk from (I could have also done it from a recovered partition, but it felt a bit unsafe to meddle around with the disk too much).

Much to my surprise, TestDisk completed the job and managed to find all of my partitions, including my documents partition! Which was quite a relief and also a surprise since neither of the above documents mentioned the program at all. It has a bit of a strange user interface, but it did the job very well, so I was very happy with TestDisk. Brilliant program.

Anyway, after a long night of filesystem crisis, I was able to completely recover my filesystem, and adjust it in a way to eliminate the filesystem overlap. The moral of this story: being careful with partitioning saves you a lot of time!