Linux – Recover partition table for lvm

fdisklinuxlvmpartition

I managed to run fdisk on the wrong disk. However I was able to save a log of the terminal session.

Fdisk before the accident:

~$ sudo fdisk -l /dev/sda

Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x72fc82e8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       36473   292969341   8e  Linux LVM
/dev/sda2           36474       38913    19599300    b  W95 FAT32

Later in the log I found:

$ sudo fdisk -u /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c').

Command (m for help): p

Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x72fc82e8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1              64   550000000   274999968+  83  Linux
/dev/sda2       550000001   625142447    37571223+  83  Linux

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x72fc82e8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1              64   550000000   274999968+  8e  Linux LVM
/dev/sda2       550000001   625142447    37571223+  83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Das Gerät oder die Ressource ist belegt.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

The system kept on running fine. I partioned /dev/sdb as I wanted correctly and finally run kpartx on /dev/sdb. I suppose this is why the volumes on sda behaved correctly until reboot.

I noticed the error and tried to recover the partition table on /dev/sda

However I did not manage to recover the original partition table. The number of blocks displayed for /dev/sda1 is always 292969348, thus +7 compared to the original. I remember upgrading fdisk before partitioning sdb, maybe it's because of the newer version?

Nevertheless I considered the system safe as I did not mind the fat32 partition which is unused. After a reboot lvm finds the physical volume but no volume groups. I had defined a single volume group consisting only of sda1 and some logical volumes on this group.

Current system status:

~# fdisk -c -l /dev/sda

Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x72fc82e8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       36473   292968348+  8e  Linux LVM
/dev/sda2           36474       38913    19599300    b  W95 FAT32

~# pvdisplay 
  "/dev/sda1" is a new physical volume of "279,40 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda1
  VG Name               
  PV Size               279,40 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ur3J4Y-cwvC-lji2-RPk0-39xb-ltzW-ogN4Sd

~# vgscan 
  Reading all physical volumes.  This may take a while...
  No volume groups found

~# pvck -v /dev/sda1
    Scanning /dev/sda1
  Found label on /dev/sda1, sector 1, type=LVM2 001
  Found text metadata area: offset=4096, size=192512
    Found LVM2 metadata record at offset=168448, size=28160, offset2=0 size2=0
    Found LVM2 metadata record at offset=67584, size=100864, offset2=0 size2=0

Why can't I restore the original partition table with the exact blocks count?
Why doesn't lvm find any volume groups?
What are my options now? Can the multiple lvm metadata be the rescue? How would I use them?

Best Answer

You should break out the backup of your partition table and dd it back into place. Or run a partition recovery tool (The first hit on Google for "recover partitions Linux" looks interesting). But I'd stick with the partition table backup.

Related Topic