Notice that this article contains many useful yet dangerous tools. You should always NOT use them unless you have to and you know what your are doing.
Yesterday, I was going to make a new Linux LVM partition on the hard disk with a Debian system and a Windows installed. I tried parted (GNU Parted) and everything went well until the misleading command
which I thought of labeling the partition, but which actually erase your partition table. This command also gives a warning like
mklabel LABEL-TYPE create a new disklabel (partition table)
and if you ignored it, you would regret.
Warning: Partition(s) on /dev/sda are being used.
So the partition table was wiped out. However, the good news was that only the partition table was lost. Parted provided a rescue command which is useless. Let's just skip the boring part of trying 'rescue 0 20GB' a hundred times. After several search queries, I came to gpart (not GParted). It turned out to be of use. With
I found an NTFS partition and a Linux swap partition. The gpart did not recognize ext3 and lvm2. Then I found the last release dated back to 2001, and it certainly won't support these new formats.
Whenever you are to give up, the savior comes.
Testdisk came in and became handy. Comparatively, it is user-friendly. Testdisk analyzed the disk and gave out a list of partitions it found. I modified some attributes (boot flag etc) and write the partition table back.
It should marked the end of the partition table recovery, but things were a little more complicated than expected.
The partition was not ordered correctly. It is not a problem of testdisk though. Testdisk ordered the partitions by cylinder number, but the partition table on the disk used to be in random order. The reason for that is when I was installing debian and made partitions using partman, the ordered was determined by the operation order. I had to change the partition order back manually. I chose fdisk this time. It never modify the partition table before the w (write) command.
- First, list the partitions using fdisk and record them somewhere.
fdisk -l /dev/sda (list by cylinders)
fdisk -ul /dev/sda (list by sectors, it will be of use)
fdisk -u /dev/sda (using a sector mode)
- Second, delete those partitions not in the expected order with command d
- Third, add primary partitions using the expected number. (command n).
Add an extended partition that will contain all logical partitions.
Add logical partitions from number 5. Note that you can't choose the partition number for logical partitions.
- [Very Important] Fourth, verify that the sectors you entered are EXACTLY as the listed ones.
Set the boot flag on the boot partition.
Set partition types (LVM, NTFS, FAT etc.).
Make sure that everything is correct and you know what you are doing.
- Finally, write back and quit with w. [Dangerous]
I verified that the partition table is correct with testdisk.
And re-install grub on MBR. With
Now the most exciting part. Reboot...
Everything went and the system was not affected.