Lyte's Blog

Bad code, bad humour and bad hair.

Oops I Needed That Partition Table...

I just lost the partition table on a drive with a non-standard layout and managed to get the data back… phew.

I originally had something like:

1
2
3
4
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      208813+  83  Linux
/dev/sda2              27        3943    31463302+  8e  Linux LVM
/dev/sda3            3944        5221    10265535    7  HPFS/NTFS

Note: partition table above is from a VM I spun up to write this entry.

That is to say I had a drive dual booting Linux and Windows, with Windows at the end of drive (because Russel sort of implied it was better).

The person who owns the drive in question wanted all the space claimed by Linux reallocated back to Windows.

After confirming backups of critical documents I fired up gparted in a live CD… which all seems swimmingly ok until the Laptop powered off (because the power plug had dropped out and the battery went flat).

After trying to boot up I discovered a corrupted partition table.

This would probably be ok (given the backups of documents) if not for the fact that I had no appropriate Windows installation media and the owner of the Laptop couldn’t find the original installation disks.

Oh noes! (I scream loudly inside my head so as not to alarm the owner of the Laptop)

After booting back in to the live CD I have a partition table that looks more like this:

1
2
3
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      208813+  83  Linux
/dev/sda3              27        1304    10265535    7  HPFS/NTFS

“Ok” I hear you saying “just rewrite the partition table with the output you saved from "fdisk -l” before starting. “Oh you didn’t save the output before starting…”

After a bit of hair pulling (and googling) I come across gpart.

It found the old partition without breaking a sweat:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@ubuntu:~# gpart /dev/sda

Begin scan...
Possible partition(Linux ext2), size(203mb), offset(0mb)
Possible partition(Windows NT/W2K FS), size(10024mb), offset(203mb)
Possible partition(Windows NT/W2K FS), size(10024mb), offset(30929mb)
End scan.
[...]
Primary partition(3)
   type: 007(0x07)(OS/2 HPFS, NTFS, QNX or Advanced UNIX)
   size: 10024mb #s(20531070) s(63344295-83875364)
   chs:  (1023/254/63)-(1023/254/63)d (3943/0/1)-(5220/254/63)r
[...]

After talking to fdisk nicely I was able to convince it to create the partition table that gpart was suggesting and subsequently able to mount, resize properly and validate the partition with Windows chkdsk tool… phew, crisis averted.

Next time I’m dding the disk to a USB drive first, no matter how long it takes.

Comments