导读 本文中将详细介绍如何从已删除的物理卷或故障磁盘场景中恢复。
系统环境

Centos8

现有的文件系统

下面使用了/dev/sd[a-c]三块磁盘,将他们划入一个卷组,创建两个逻辑卷:

# Create pv
[root@localhost ~]# pvcreate /dev/sda
[root@localhost ~]# pvcreate /dev/sdb
[root@localhost ~]# pvcreate /dev/sdc
# Create VG
[root@localhost ~]# vgcreate vg_data /dev/sd[a-c]
# Create LV
[root@localhost ~]# lvcreate -L 25G -n lv_data vg_data
[root@localhost ~]# lvcreate -L 25G -n lv_log vg_data

下面列出逻辑卷的分配使用信息:

[root@localhost ~]# pvs
  PV             VG      Fmt  Attr PSize   PFree 
  /dev/nvme0n1p2 cl      lvm2 a--  <19.00g <2.00g
  /dev/sda       vg_data lvm2 a--  <20.00g     0 
  /dev/sdb       vg_data lvm2 a--  <20.00g <9.99g
  /dev/sdc       vg_data lvm2 a--  <20.00g     0 
[root@localhost ~]# vgs
  VG      #PV #LV #SN Attr   VSize   VFree 
  cl        1   2   0 wz--n- <19.00g <2.00g
  vg_data   3   2   0 wz--n- <59.99g <9.99g
[root@localhost ~]# lvs -a -o +devices
  LV      VG      Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert Devices            
  root    cl      -wi-ao---- 15.00g                                                     /dev/nvme0n1p2(512)
  swap    cl      -wi-ao----  2.00g                                                     /dev/nvme0n1p2(0)  
  lv_data vg_data -wi-a----- 25.00g                                                     /dev/sda(0)        
  lv_data vg_data -wi-a----- 25.00g                                                     /dev/sdb(0)        
  lv_log  vg_data -wi-a----- 25.00g                                                     /dev/sdc(0)        
  lv_log  vg_data -wi-a----- 25.00g                                                     /dev/sdb(1281)     


在根目录创建两个文件夹/data和/log,格式化逻辑卷,挂载逻辑卷,并存放一些数据:

[root@localhost ~]# mkdir /data /logs
[root@localhost ~]# mkfs.xfs /dev/vg_data/lv_data 
[root@localhost ~]# mkfs.xfs /dev/vg_data/lv_log 
[root@localhost ~]# mount /dev/vg_data/lv_data /data
[root@localhost ~]# mount /dev/vg_data/lv_log /data
[root@localhost ~]# df -h
Filesystem                  Size  Used Avail Use% Mounted on
devtmpfs                    880M     0  880M   0% /dev
tmpfs                       897M     0  897M   0% /dev/shm
tmpfs                       897M  8.7M  888M   1% /run
tmpfs                       897M     0  897M   0% /sys/fs/cgroup
/dev/mapper/cl-root          15G  1.9G   14G  13% /
/dev/nvme0n1p1              976M  183M  726M  21% /boot
tmpfs                       180M     0  180M   0% /run/user/0
/dev/mapper/vg_data-lv_log   25G  211M   25G   1% /data
[root@localhost ~]# touch /data/file{1..10}.txt
[root@localhost ~]# touch /logs/text{1..10}.log
将磁盘设置为故障或删除磁盘

这两种情况是意外删除的物理卷或发生故障的磁盘,通过运行pvremove删除了任何一个物理卷,或者从虚拟机中移除某一块物理磁盘。

下面从虚拟机中移除一块物理磁盘:

重启系统之后,发现不能挂载lv_data和lv_log逻辑卷了,/dev目录中找不到vg_data卷组。

如果该逻辑卷设置为开机自动启动了,磁盘发生故障后,由于两个逻辑卷无法挂载文件系统,因此重新引导不能进入系统。只能通过进入单用户模式,并注释fstab配置文件里面和该逻辑卷相关的条目。

添加新物理硬盘

下面从虚拟机中添加一个新的物理磁盘,新添加的磁盘会变成/dev/sdc

[root@localhost ~]# lsblk -S
NAME HCTL       TYPE VENDOR   MODEL             REV TRAN
sda  6:0:0:0    disk VMware,  VMware Virtual S 1.0  spi
sdb  6:0:2:0    disk VMware,  VMware Virtual S 1.0  spi
sdc  6:0:3:0    disk VMware,  VMware Virtual S 1.0  spi
sr0  3:0:0:0    rom  NECVMWar VMware SATA CD01 1.00 sata

恢复已删除物理卷的元数据

现在让我们开始为已删除的物理卷恢复元数据。当使用pvs,vgs,lvs时,它将警告缺少xxxxx UUID的设备之一。

只需复制UUID并使用grep查看存档和备份。在进行重新引导之前,报告的UUID指的是/dev/sdb设备。

[root@localhost ~]# cat /etc/lvm/archive/vg_data_00002-1587647360.vg |grep -B 2 -A 9 "v6wYGZ-0dGA-8ecB-kec8-r2zv-gCJi-gulSAD"

pv1 {
id = "v6wYGZ-0dGA-8ecB-kec8-r2zv-gCJi-gulSAD"
device = "/dev/sdb" # Hint only

status = ["ALLOCATABLE"]
flags = []
dev_size = 41943040 # 20 Gigabytes
pe_start = 2048
pe_count = 5119 # 19.9961 Gigabytes
}
[root@localhost ~]# cat /etc/lvm/backup/vg_data |grep -B 2 -A 9 "v6wYGZ-0dGA-8ecB-kec8-r2zv-gCJi-gulSAD"

pv1 {
id = "v6wYGZ-0dGA-8ecB-kec8-r2zv-gCJi-gulSAD"
device = "/dev/sdb" # Hint only

status = ["ALLOCATABLE"]
flags = []
dev_size = 41943040 # 20 Gigabytes
pe_start = 2048
pe_count = 5119 # 19.9961 Gigabytes
}


让我们先使用pvcreate --test进行试运行:

[root@localhost ~]# pvcreate --test --uuid "v6wYGZ-0dGA-8ecB-kec8-r2zv-gCJi-gulSAD" --restorefile /etc/lvm/archive/vg_data_00002-1587647360.vg /dev/sdc 
  TEST MODE: Metadata will NOT be updated and volumes will not be (de)activated.
  Couldn't find device with uuid l5rSrt-SRfc-KQNw-Tm29-gx4D-ihBw-Xzum3L.
  Couldn't find device with uuid v6wYGZ-0dGA-8ecB-kec8-r2zv-gCJi-gulSAD.
  Couldn't find device with uuid UYyDyH-vc6D-XbQ7-ga1X-DKeM-ObLe-tdBLxR.
  Physical volume "/dev/sdc" successfully created.

  • --test 试运行
  • --uuid 用于指定新创建的物理卷的uuid值。如果没有此选项,将生成随机UUID。在本实验中指定的uuid值是之前删除物理卷的uuid值。
  • --restorefile 读取由vgcfgbackup生成的存档文件

去掉--test选项,正式创建物理卷:

[root@localhost ~]# pvcreate --uuid "v6wYGZ-0dGA-8ecB-kec8-r2zv-gCJi-gulSAD" --restorefile /etc/lvm/archive/vg_data_00002-1587647360.vg /dev/sdc 
  Couldn't find device with uuid l5rSrt-SRfc-KQNw-Tm29-gx4D-ihBw-Xzum3L.
  Couldn't find device with uuid v6wYGZ-0dGA-8ecB-kec8-r2zv-gCJi-gulSAD.
  Couldn't find device with uuid UYyDyH-vc6D-XbQ7-ga1X-DKeM-ObLe-tdBLxR.
  Physical volume "/dev/sdc" successfully created.
[root@localhost ~]# pvs
  PV             VG      Fmt  Attr PSize   PFree 
  /dev/nvme0n1p2 cl      lvm2 a--  <19.00g <2.00g
  /dev/sda       vg_data lvm2 a--  <20.00g     0 
  /dev/sdb       vg_data lvm2 a--  <20.00g     0 
  /dev/sdc       vg_data lvm2 a--  <20.00g <9.99g
[root@localhost ~]# vgs
  VG      #PV #LV #SN Attr   VSize   VFree 
  cl        1   2   0 wz--n- <19.00g <2.00g
  vg_data   3   2   0 wz--n- <59.99g <9.99g
[root@localhost ~]# lvs -o +devices
  LV      VG      Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert Devices            
  root    cl      -wi-ao---- 15.00g                                                     /dev/nvme0n1p2(512)
  swap    cl      -wi-ao----  2.00g                                                     /dev/nvme0n1p2(0)  
  lv_data vg_data -wi-a----- 25.00g                                                     /dev/sda(0)        
  lv_data vg_data -wi-a----- 25.00g                                                     /dev/sdc(0)        
  lv_log  vg_data -wi-a----- 25.00g                                                     /dev/sdb(0)        
  lv_log  vg_data -wi-a----- 25.00g                                                     /dev/sdc(1281)     

恢复卷组

然后使用lvm的备份还原卷组。首先加入--test试运行一下:

[root@localhost ~]# vgcfgrestore --test -f /etc/lvm/backup/vg_data vg_data
  TEST MODE: Metadata will NOT be updated and volumes will not be (de)activated.
  Volume group vg_data has active volume: lv_log.
  Volume group vg_data has active volume: lv_data.
  WARNING: Found 2 active volume(s) in volume group "vg_data".
  Restoring VG with active LVs, may cause mismatch with its metadata.
Do you really want to proceed with restore of volume group "vg_data", while 2 volume(s) are active? [y/n]: y
  Restored volume group vg_data.


下面正式运行一下,去掉--test选项:

[root@localhost ~]# vgcfgrestore -f /etc/lvm/backup/vg_data vg_data
  Volume group vg_data has active volume: lv_log.
  Volume group vg_data has active volume: lv_data.
  WARNING: Found 2 active volume(s) in volume group "vg_data".
  Restoring VG with active LVs, may cause mismatch with its metadata.
Do you really want to proceed with restore of volume group "vg_data", while 2 volume(s) are active? [y/n]: y
  Restored volume group vg_data.


如果卷组和逻辑卷未激活,请使用下面命令激活:

# 扫描卷组
[root@localhost ~]# vgscan 
  Reading all physical volumes.  This may take a while...
  Found volume group "cl" using metadata type lvm2
  Found volume group "vg_data" using metadata type lvm2
# 激活卷组vg_data
[root@localhost ~]# vgchange -ay vg_data
  2 logical volume(s) in volume group "vg_data" now active
# 扫描逻辑卷
[root@localhost ~]# lvscan 
  ACTIVE            '/dev/cl/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/cl/root' [15.00 GiB] inherit
  ACTIVE            '/dev/vg_data/lv_data' [25.00 GiB] inherit
  ACTIVE            '/dev/vg_data/lv_log' [25.00 GiB] inherit

总结

在lvm中,需要执行一些步骤来恢复已删除的物理卷,方法是添加新磁盘并用现有UUID指向新磁盘。

本文原创地址://gulass.cn/lvm-restore-pv.html编辑:逄增宝,审核员:逄增宝