Recovering Accidentally Formatted ext4 Partition / Fixing Superblock

Today, I made the silly mistake of accidentally formatting one of the ext4 partitions in my Ubuntu 16.04 machine to NTFS instead of formatting the pen drive, which I was hoping to format. So if you are reading this most probably you have done something similar or perhaps someone you know has gone down that path and you maybe trying to help him/her.

Fortunately I was able to recover my partition completely and in this post I’ll go through the few things that helped me recover my partition. There is still hope my friend. 🙂

First I must thank Shane for this askubuntu answer, the author of this blog post for giving pointers and my friend Janaka for helping me out. Have a look at those two posts as they are very helpful.

If you accidentally formatted your partition (or in any case of lost partitions or data), the most important thing is avoiding any data writes on that partition. Another important thing is creating a backup image of the messed up disk.

Quoting Shane,

If your messed up drive is sda, and you wanted to store the image in yourname’s home directory for instance: dd if=/dev/sda of=/home/yourname/sda.img.bak bs=512

to restore the image after a failed recovery attempt: dd if=/home/yourname/sda.img.bak of=/dev/sda bs=512

You could of course use /dev/sda1 if you are only interested in the first partition, but as some of these utilities alter the partition table, it is perhaps a better idea to image the whole disk..

Also, if you are using dd for a large operation, it is very helpful to see a progress bar, for which you can use a utility called pv which reports progress of data through a pipeline

for instance: pv -tpreb /dev/sda | dd of=/home/yourname/sda.img.bak bs=512

First of all I tried TestDisk tool. I was able to get some files recovered but I wasn’t able to find a way to recover the whole partition using TestDisk tool.

Then I started following the other blog post I shared above. In their first thing was identifying the affected partition. I already knew mine, however if you want to get info on that you can run sudo fdisk -l command. Output for me was as follows.

fdisk-output

Now the idea for the next step is that, since I did not write anything on the formatted disk, my previous ext file system data should be still there. In ext, file system data are kept in a record called Superblock which keeps the characteristics of a filesystem, including its size, the block size, the empty and the filled blocks and their respective counts, the size and location of the inode tables, the disk block map and usage information, and the size of the block groups. (You can read more about it here if you are interested). So what what we are trying to do here is to fix the ext file system.

In my case, I was able to do it with the following file system check command. (Note that the same command is there for ext2 and ext3 as well). Before you run the command, make sure the partition is unmounted.

sudo fsck.ext4 -v /dev/xxx

First part of the output for me was as follows. I was able to see the original partition name (“Personal”) which I had previously given to my ext4 partition and it gave me a slight relief. So if you are able to see the same, hopefully things will turn out better.

fcsk-output-part1

At the bottom of the above screen capture, you could see a prompt asking it it is okay to fix certain blocks count. I went with yes here and it was actually the only option for me to go as well. There were few more prompts of the similar manner and seemed like a lot more is coming. So I stopped the command and went with the following which basically says yes to all prompts.

sudo fsck.ext4 -vy /dev/sda10

Then after a number of fix prompts, I got the following output.

fsck-output-success

When I mount the partition everything was back to normal. Hopefully you will be able to recover yours as well. Please note that this might not be the recovery method for all the scenarios. I’m just noting this down hoping that this will help someone else like me. Make sure you understand the steps well before doing these.

EDIT (03/09/2017):

If you have a dual boot set-up, once you boot up the other OS (e.g. Windows) you might lose the partition again since in the other OS the partitioning might be different. So it’s better to make sure to get a backup once the recovery is done.

Further even if you lose the partition again once you login to the other OS, you can still recover using the fcsk.ext4 command.

Cheers! 🙂

~ Rajind Ruparathna

20 thoughts on “Recovering Accidentally Formatted ext4 Partition / Fixing Superblock

  1. Excellent post Rajind, congratulations. I made the same mistake, but worse because I formatted the partition ext4 in NTFS. So, when I try to run fsck.ext4 it tells me that:

    sudo fsck.ext4 -v /dev/sdb3
    e2fsck 1.42.13 (17-May-2015)
    ext2fs_open2: Bad magic number in super-block
    fsck.ext4: Superblock invalid, trying backup blocks…
    fsck.ext4: Bad magic number in super-block while trying to open /dev/sdb3

    The superblock could not be read or does not describe a valid ext2/ext3/ext4
    filesystem. If the device is valid and it really contains an ext2/ext3/ext4
    filesystem (and not swap or ufs or something else), then the superblock
    is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193
    or
    e2fsck -b 32768

    Should I reformat again in ext4? And try again or it will be worse?

    Can I have some hope?

    Liked by 1 person

  2. Well, I was in the process of hooking some old IDE hard drives to a USB interface card to test them. The ones that would spin up and I could read with gparted I was formatting into NTFS with the plans to give them to a friend of mine with a Windows 7 system. My second drive in my Linux system was being used as a public share for others and I accidentally selected it without realizing and…. I unmounted, deleted the single EXT4 partition, and then formated NTFS. I panicked because I had my accounting files on it…. after searching, trying testdisk, etc I found this blog posting. I tried it. A couple of times…. and then it worked. I got it all back as if nothing had happened.

    A BIG THANK-YOU for this information.

    Liked by 1 person

  3. It’s work ext4 to LVM and back to ext4, but the problem now is the flag and type of hardive is always flag for LVM.

    Like

  4. Thank you so much. After installing paragon ext4 and initializing the disk, Windows ruined the partition. I tried TestDisk and other software, nothing worked. I have been trying for more than a year until today I came across your solution. Thanks to you I recovered a full 6TB HDD.

    Liked by 1 person

  5. This has helped me immensely too, in recovering a partition that i though i had lost for good 🙂 I was wondering whether you accept donations, as a form of ‘thank you’.

    Liked by 1 person

  6. I’ve accidentally turned 2 linux ext4 disks from MBR to GPT in my dual boot Windows Machine and the same as you, testdisk does not help and there is no clue after fixing the blocks with e2fsck -b 32768 (picking this by chance only).

    Afterwards, I try your command fsck.ext4 -vy /dev/sdb and recovered the data.

    However, before making the silly mistalk, when using lsblk the system shows:
    sdb
    |
    -sdb1 … … /media/$USER/DiskLabel

    After the rescue, one become
    sdb … … /media/$USER/DiskLabel1

    and the others simply shows
    sdc (no mount information show)

    Anyway, the previous label shows up again in my file manager (dolphin of kde, Kubuntu 20.04) and all data recovered.

    I am checking but seems nothing missed.

    Thank you very much for your notes, Sir.

    Liked by 1 person

  7. Hi Rajind, I accidentally formatted my NTFS drive D in my Windows to ext4 while installing dual boot Ubuntu alongside with my Windows 10. Is there any chance to get my data back? Thank you so much!

    Like

  8. Rajind, great write up, do you think this will work on a QNAP drive, which uses UBUNTU ? Problem I have is that I have an array of drives in a QNAP home server, however I accidentally formatted the number one drive to NTFS, long story, which now won’t allow access to the rest of the drives, no I did not have a back up, I’m a bit green when it comes to RAID levels, so the drives were set up as JBOD with no redundancy, but I pulled all the drives before any information was written to them, so am confident of the data being still there.
    I can also see the original partitions in ext2/3/4 using Testdisk and other software.
    Ultimately the language QNAP use is UBUNTU, so am hopeful something like the above will work, what do you think, is there any additional advice you can offer please ?

    Like

  9. I accidentally clicked yes when windows 10 said it wanted to initialize a drive, I thought it was my newly installed m.2 drive, but it took my 6tb ext4 partition with over 2 years of work on it.
    Testdisk didn’t work this time even after 12 hours of analyzing it.
    So I tried this which worked great! Though it becomes bad again after reboot, so I’ll have to get a drive I can backup too. That is a minor problem though. Files seems to not be corrupt.
    Thanks a lot! I am so happy I can recover the files. I almost had a heart attack.

    Like

Leave a comment