「LVM」- 数据恢复

  CREATED BY JENKINSBOT

问题描述

虽然需要恢复数据,但并非应 LVM 经常故障,而是人为原因导致数据丢失。

该笔记将记录:在 Linux 中,如何从 LVM 中恢复数据,以及常见故障场景的处理方法。

解决方案

我们并非数据恢复专家,很多内容都参考自网络:
1)Recovering a Lost LVM Volume Disk

场景一、物理卷被格式化,且无元数据备份

将显示 unknown device 信息:

# pvs
  WARNING: Couldn't find device with uuid A11H2H-99zO-Urtx-NUkn-uRE3-umXU-P1hIWj.
  WARNING: VG dt is missing PV A11H2H-99zO-Urtx-NUkn-uRE3-umXU-P1hIWj (last written to /dev/nvme0n1p4).
  PV         VG        Fmt  Attr PSize  PFree
  /dev/sda3  ubuntu-vg lvm2 a--  <9.00g     0
  /dev/sdb10 dt        lvm2 a--  48.82g     0
  /dev/sdb11 dt        lvm2 a--  48.82g     0
  /dev/sdb12 dt        lvm2 a--  39.32g <9.94g
  /dev/sdb5  dt        lvm2 a--  48.82g <1.18g
  /dev/sdb6  dt        lvm2 a--  48.82g     0
  /dev/sdb7  dt        lvm2 a--  48.82g     0
  /dev/sdb8  dt        lvm2 a--  48.82g     0
  /dev/sdb9  dt        lvm2 a--  48.82g 48.82g
  [unknown]  dt        lvm2 a-m  48.82g     0

结合 lsblk 输出:

# lsblk /dev/sdb
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb       8:16   0  477G  0 disk
├─sdb1    8:17   0  300M  0 part
├─sdb2    8:18   0  128M  0 part
├─sdb3    8:19   0 46.6G  0 part /mnt/fix/rootfs
├─sdb4    8:20   0 48.8G  0 part
├─sdb5    8:21   0 48.8G  0 part
├─sdb6    8:22   0 48.8G  0 part
├─sdb7    8:23   0 48.8G  0 part
├─sdb8    8:24   0 48.8G  0 part
├─sdb9    8:25   0 48.8G  0 part
├─sdb10   8:26   0 48.8G  0 part
├─sdb11   8:27   0 48.8G  0 part
└─sdb12   8:28   0 39.3G  0 part

我们得知磁盘 /dev/sdb4 被格式化,因此使用它重新创建 PV 实例

好在 LVM 以纯文本形式存储相当大的头信息,包括多数据的多个备份:

# strings /dev/sdb5 | less                    // 查看这些信息
...

# vgcfgbackup dt                              // 所以虽然 PV 损坏,但依旧能够完成备份;
  WARNING: PV /dev/sdb4 in VG dt is missing the used flag in PV header.
  Volume group "dt" successfully backed up.

# pvcreate --uuid A11H2H-99zO-Urtx-NUkn-uRE3-umXU-P1hIWj -ff --restorefile /etc/lvm/backup/dt /dev/sdb4
  WARNING: Couldn't find device with uuid A11H2H-99zO-Urtx-NUkn-uRE3-umXU-P1hIWj.
  WARNING: Couldn't find device with uuid ARQRI6-whVg-o2B2-O1qK-rh8c-Qdo0-XmhWjO.
  WARNING: Couldn't find device with uuid s5FrBC-QhyM-h7DR-qzvE-F6Wj-JiG0-zEK1R4.
  WARNING: Couldn't find device with uuid XHt33t-lOEO-mzQ9-XT66-sWdo-S0HD-NWjkQA.
  WARNING: Couldn't find device with uuid FIAWwN-t3cx-mbgA-zdVX-ayJ3-Xfu7-1dJJtj.
  WARNING: Couldn't find device with uuid JlkM1S-LcL2-g6VO-3e30-j6gM-z82f-cUNELP.
  WARNING: Couldn't find device with uuid DLdq59-rQ1w-3jow-U1Nz-CcZL-3oPG-q3IJ92.
  WARNING: Couldn't find device with uuid 8s2vFL-gJ3c-kbcD-kAHR-l1vC-9eUQ-neRSWE.
  WARNING: Couldn't find device with uuid oiBwX0-GFxZ-qApn-ZrcI-ARPN-rDGU-3cGwRY.
  WARNING: PV /dev/sdb4 in VG dt is missing the used flag in PV header.
Really INITIALIZE physical volume "/dev/sdb4" of volume group "dt" [y/n]? y
  WARNING: Forcing physical volume creation on /dev/sdb4 of volume group "dt"
  WARNING: PV /dev/sdb4 in VG dt is missing the used flag in PV header.
  Physical volume "/dev/sdb4" successfully created.

# vgcfgrestore dt
  Restored volume group dt.
  
# vgscan
  Found volume group "dt" using metadata type lvm2
  Found volume group "ubuntu-vg" using metadata type lvm2
  
# vgchange -ay dt
  4 logical volume(s) in volume group "dt" now active

文件系统修复:

# e2fsck -y /dev/dt/opt
...

至此已经完成大部分工作。但是:
1)由于磁盘已经格式化,所以当命令 e2fsck 执行结束后,依旧会丢失部分数据;
2)如果被格式化的分区是文件系统最开始的部分,即元数据丢失,则只能借助其他工具恢复文件;

参考文献

Recovering a Lost LVM Volume Disk
can you recover lvm metadata without access to /etc/lvm/?
How to recover deleted Logical volume (LV) in LVM using vgcfgrestore