Recover corrupted GPT partition table with gdisk

I’m going to share an awful experience I had last week. I was dual-booting with Windows 10 and Arch Linux. The bootloader was UEFI, configured with a dedicated EFI block that contains both of the operating systems’ boot information. One fine morning, I tried to boot w10 and I got an error message, no clue what happened. Later, tried to fsck, and surprisingly turned out the superblock got damaged with an error message “BAD SUPER BLOCK: MAGIC NUMBER WRONG“.

To fix the problem, I accidentally ran dd if=/dev/zero of=/dev/sda. 30 seconds later, I felt something is wrong with this command and I immediately terminate the process by Cltr + C. It was a mistake and the worst possible case would be an empty unallocated space of my HDD size. To verify, I ran cfdisk /dev/sda and it shows exactly what I was afraid of. The dd has destroyed my partition table.

The first 2 partitions were a 300 MB fat32 block and a 146.xx GB NTFS block containing w10, I wouldn’t mind if dd replace the first 2 partition blocks with zero but the rest of the partitions were crucial.

recover corrupted GPT partition table.
TestDisk 7.1, Data Recovery Utility

I’ve tried many tools to recover data, etc. but I was not satisfied cause I believe the partition table got corrupted but the partitions remain intact.

One tool worth mentioning, which is built for rescue data from the damaged partition table, TestDisk. So I started to play with it. It’s a very nice and user-friendly command-line interface application. TestDisk scans the whole HDD blocks and shows results to recover lost partitions. I will write a detailed post about it later.

Surprisingly the advanced option shows all of the partitions exists. After backing-up important data using TestDisk, I realized that I need to find a way to restore the GPT table.

I used a small yet powerful disk management utility many years ago to convert MBR to GPT and vice-versa without losing data. It’s called gdisk. So, If you accidentally got your GPT partition table corrupted you should follow these steps to recover corrupted GPT partition table with gdisk.

Step 1: Spin a live Linux distro:

You can pick any major distribution like Ubuntu or Arch.

Step 2: Install gdisk:

Ubuntu/Debian Users can install gdisk:

sudo apt installl gdisk

Arch linux users:

sudo pacman -Sy gdisk

Step 3: Run gdisk with your partition block

In my case, It was

sudo gdisk /dev/sda


After launching gdisk, it shows that my main partition table got corrupted and a backup GPT header is present. Type ? and press Enter, you’ll see a list of possible actions that gdisk can perform.

recover corrupted GPT partition table.
gdisk

Step 4: Recover corrupted GPT partition table

From the above list, type r and press enter then type ? and press enter to see the possible commands on the recovery/transformation option.

recover corrupted GPT partition table.
gdisk

As gdisk shows my backup partition header remains untouched, I pressed b which uses backup GPT header to rebuild the main header.

It successfully completed the task and suggest to perform a reboot before mounting any partition. So I pressed w to write table to disk and exit.

After rebooting I got my partitions back as they were before.

Conclusion

I spend almost a day with TestDisk but it couldn’t able to help me, surely it’s a very powerful tool but one thing that was very annoying, when it searches the entire disk cylinder for lost partition table, it takes 7-10 mins and from the search results, I pick first entry, got 3 partition, now I cannot go back and select the second entry from the search result, it again starts searching the whole disk which doesn’t makes sense to me.

Finally, gdisk was able to help. So if you face a similar problem and accidentally got your GPT partition table corrupted (you’ll see unallocated space for HDD size), you should use gdisk to rebuild the GPT header.

4 Comments

  1. I really like your blog.. very nice colors & theme. Did you create
    this website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to create my own blog and would like to know where u
    got this from. cheers

  2. You’re a life saver! Thank you so much. I only know enough to be dangerous, and you’ve helped me out a lot here.

Leave a Reply

Your email address will not be published. Required fields are marked *