Recover partition table on Linux just after destroying it

At the time I want to repartition a disk connected to USB, I destroyed by inattention my system disk one. Testdisk save my life.

The killing error :

cfdisk /dev/sda => modification => write.

well, as it should never be the case before changing partitions. I didn’t saved my partitions before !! :)

Include forecd option that save incrementally the partition table in (c)fdisk sould be nice, this only take few octets and could save lot of datas.

For information, you can do it with sfdisk, that create a simple text file, to be kept in protected (from deletion) place.

sfdisk /dev/sda -d sda.txt

To restore him :

sfdisk /dev/sda <sda.txt

Happily, I understood my mistake before restarting the OS (but should be the same just a little bit longer), with a rescue CD like (systemrescuecd or some system cd disks.

I seen tutorials that speak about old unmaintained gpart, tried, but take tenth of minutes (on an SSD), to find nothing.

I then tried the tool testdisk (command line with nice and easy menu text interface, included in most Linux distributions), and it found immediatly (less than 1 second) the good result. Perhaps the fact that partitions where following in sequence without hole help it ?

Usage

* If it’s not installed you still can install it without risk for your partition table, the system still use the old table as they are mounted: sudo apt-get install testdisk
* Launch testdisk in a terminal with the command sudo testdisk, anything else will be made using textmode semi-graphical menus.
* This start biwth the choice of the testdisk.log you can choos what seems better to you, I myself choose [No log] for a first test, that was good enough for the whole recovery process.
* Select a media: (for me:) Disk /dev/sda – 240 GB / 223 GiB – OCZ-VERTEX2
* Partition table type : typically on a PC : >[Intel ] Intel/PC partition (recent computer preinstalled with windows (like laptop or lowend pre-made computers) use generally EFI GPT since few time. There is also other choices (Sunos, Mac, xbox…), I wonder that if you destroyed your table, you know which kind of table it was.
* Choose the tool: >[ Analyse ] Analyse current partition structure and search for lost partitions. There are other interesting tools too.
* Quick search : Et voilà !!! It found the good table (well, that was my case). Warning, this table is still not restored. You can use a terminal and type df -h to see if this match the former table (as this should be mounted). You can eventually compare with previoulsy done fdisk -l. If everything is OK, cotinue by press enter.
* Write (or deeper search if that’s not good) : Write partition table, confirm ? (Y/N) type y et voilà, this time, that’s done.

Leave a Reply