Lost Partition Table

DateReadtime 4 minutes Tags

Step 1: Losing the Data

We will start this post off with a tip...

Never ever ever re-install a distribution when you are tired.

Long story short, I was re-installing Ubuntu at way to early in the morning and clicked "Erase Entire disk" about 2 seconds later I panicked and pulled out the battery on my laptop. After restarting, the installer had not yet managed to do any damage that I knew of, but it had removed my partition table.

Step 2: Panic

I am now wide awake and really angry at myself. I put in a liveusb and begin the process of getting my data back. The best thing I did at this point was go to bed. I was way to wired to work on recovering my data and it was far to important not to screw up. Take your time, rushing is probably what got you in this mess.

Step 3: Recover partition table

After a few seconds of search and a few hours of research, I found a seemly universally liked program called testdisk. I highly recommend testdisk.

"Test disk is a free data recovery utility. It is primarily designed to help recover lost data storage partitions and/or make non-booting disks bootable again when these symptoms are caused by faulty software, certain types of viruses or human error (such as accidentally erasing a partition table). TestDisk can be used to collect detailed information about a corrupted drive which can then be sent to a tech for further analysis." - wikipedia

Using testdisk, I was able to really easily scan my hard disk. The program allowed me to view files in partition and do some navigation through the directories. I was also able recover most of my partition table.

Testdisk correctly (sort of) found four partitions: Boot, recovery, windows, and linux. However, testdisk incorrectly placed the linux partition with "unallocated" space on either side. More to come on that later.

Step 4: Recover lvm2 configuration file

LVM stores a copy of the lvm configuration file in the front of each physical volume. Using dd to read the first few sectors of the lvm partition to a file, I should have been able to find the config file. I did not. Now, this should be a red flag to you that something is wrong with my partition table but at the time i over looked this detail.

Next, I used photorec (another program in the testdisk suite) and a custom signature to scan the disk for my lvm config file. All lvm config files should begin with something like "# Generated by LVM2" so by creating a signature for that type of file I was able to recover my configuration file. Step 4.5: Fix the partition table

After trying many different times and processes to restore the lvm config file and failing each time, I determined that testdisk had incorrectly recovered my partitions. I was able to confirm this by comparing the size of sda4 with the size of the physical volume in the configuration file. The lvm physical volume was the entire rest of the disk after the third partition not just a chunk in the middle (duh) after the third. To change my partition table I used gparted. I think that partitions aren't worth screwing up on the command line but to each their own. Now, and this is important I deleted the existing partition and made a new one. I did not move and resize the partition. If you move the partition most programs will move the data to the new start sector of the partition which would erase some of your data. So again delete and recreate do not move.

Step 5: Restore lvm config file

Now that the partition table was correct the restore went smoothly. First I had to use "pvcreate" to turn sda4 back into a valid lvm physical volume, then I used "vgcfgrestore" to restore the configuration file.

Epilogue:

As ever person who has ever used a computer knows... back up your data. But actually do it. Because I promise you that you will be super angry just like me.

I am sorry, I do not not still have the actual commands that I ran to do the recovery it has been a couple of months and I have since forgotten all of the correct arguments and such.

If you want more information about any of the programs used... I found all of this on the internet there are a few really good sites that explain testdisk as well as some lvm restoration troubleshooting.