Linux VG丢失恢复
一、功能需求
Linux主机在执行vgs后,无法获取vg信息,也无法激活vg,根据系统LVM的备份尝试恢复。
二、配置步骤
1、检查现有PV、VG、LV信息,vg01由3块LUN组成,并创建了2个LV,lv01和lv02分别挂载/data01和/data02,上传文件test.bin(MD5:230f9c3c6f9075da2ed62d7e9ec09789)。
[root@server01 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/mapper/mpatha vg01 lvm2 a-- 16.00g 16.00g
/dev/mapper/mpathb vg01 lvm2 a-- 32.00g 16.00g
/dev/mapper/mpathc vg01 lvm2 a-- 64.00g 32.00g
/dev/sda2 cl lvm2 a-- 15.00g 0
[root@server01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz--n- 15.00g 0
vg01 3 2 0 wz--n- 111.99g 63.99g
[root@server01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- 13.39g
swap cl -wi-ao---- 1.60g
lv01 vg01 -wi-ao---- 16.00g
lv02 vg01 -wi-ao---- 32.00g
[root@server01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 14G 3.7G 9.7G 28% /
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 84K 2.0G 1% /dev/shm
tmpfs 2.0G 17M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 1014M 155M 860M 16% /boot
tmpfs 396M 16K 396M 1% /run/user/42
tmpfs 396M 0 396M 0% /run/user/0
/dev/mapper/vg01-lv01 16G 85M 15G 1% /data01
/dev/mapper/vg01-lv02 32G 89M 30G 1% /data02
[root@server01 ~]# pvdisplay |grep UUID
PV UUID DFshFb-8A6P-sHif-xjtJ-BroZ-zhf5-mf56ag
PV UUID 1BGQXh-B2ud-Loui-aUMh-Sy4n-Ddov-iG1TQj
PV UUID nKbLtC-ls6T-uouN-H9PY-Vyc6-S24K-XJ5drP
PV UUID q0I3fc-y64W-xlLv-hPOd-lDaT-Wb9u-lmeg5U
2、手动卸载LV,关闭VG,清除PV,并重新创建PV,模拟VG不可用。
[root@server01 ~]# umount /data01
[root@server01 ~]# umount /data02
[root@server01 ~]# vgchange -an vg01 (关闭VG)
[root@server01 ~]# pvremove -ff /dev/mapper/mpatha (清除PV)
[root@server01 ~]# pvremove -ff /dev/mapper/mpathb (清除PV)
[root@server01 ~]# pvremove -ff /dev/mapper/mpathc (清除PV)
[root@server01 ~]# pvcreate /dev/mapper/mpath{a..c} (重新分配UUID)
Physical volume "/dev/mapper/mpatha" successfully created.
Physical volume "/dev/mapper/mpathb" successfully created.
Physical volume "/dev/mapper/mpathc" successfully created.
[root@server01 ~]# pvdisplay |grep UUID (新的UUID)
PV UUID DFshFb-8A6P-sHif-xjtJ-BroZ-zhf5-mf56ag
PV UUID P3YtRc-zpl6-jr7f-vNoG-Ca3X-GRzN-kcZ8KC
PV UUID zovSfg-rTm1-5SWG-LXFn-dJ1B-jwmA-F4DVsx
PV UUID WMk19i-of1c-ICz5-Ozhe-Lvsz-CS1M-a7aJz9
再次检查pvs、vgs,无法发现vg01,也无法激活vg01,VG不可用
[root@server01 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/mapper/mpatha lvm2 --- 16.00g 16.00g
/dev/mapper/mpathb lvm2 --- 32.00g 32.00g
/dev/mapper/mpathc lvm2 --- 64.00g 64.00g
/dev/sda2 cl lvm2 a-- 15.00g 0
[root@server01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz--n- 15.00g 0
[root@server01 ~]# vgchange -ay vg01
Volume group "vg01" not found
Cannot process volume group vg01
3、恢复PV
Linux的LVM会默认存储用户对PV/VG/LV的每一步操作,并自动把当前的VG的信息备份到一个文件里面,位置是/etc/lvm/backup/VG名, 里面也包括了对于恢复VG信息至关重要的PV UUID,我们就是通过这个文件来恢复整个VG的信息的。
先获取备份文件中的PV-UUID
[root@server01 ~]# grep mpatha /etc/lvm/backup/vg01 -B1
id = "nKbLtC-ls6T-uouN-H9PY-Vyc6-S24K-XJ5drP"
device = "/dev/mapper/mpatha" # Hint only
[root@server01 ~]# grep mpathb /etc/lvm/backup/vg01 -B1
id = "q0I3fc-y64W-xlLv-hPOd-lDaT-Wb9u-lmeg5U"
device = "/dev/mapper/mpathb" # Hint only
[root@server01 ~]# grep mpathc /etc/lvm/backup/vg01 -B1
id = "1BGQXh-B2ud-Loui-aUMh-Sy4n-Ddov-iG1TQj"
device = "/dev/mapper/mpathc" # Hint only
重新创建PV,并用backup文件中的UUID
[root@server01 ~]# pvcreate /dev/mapper/mpatha -u nKbLtC-ls6T-uouN-H9PY-Vyc6-S24K-XJ5drP --restorefile /etc/lvm/backup/vg01
[root@server01 ~]# pvcreate /dev/mapper/mpathb -u q0I3fc-y64W-xlLv-hPOd-lDaT-Wb9u-lmeg5U --restorefile /etc/lvm/backup/vg01
[root@server01 ~]# pvcreate /dev/mapper/mpathc -u 1BGQXh-B2ud-Loui-aUMh-Sy4n-Ddov-iG1TQj --restorefile /etc/lvm/backup/vg01
检查是否恢复
[root@server01 ~]# pvdisplay |grep UUID (与抹除PV之前一致)
PV UUID DFshFb-8A6P-sHif-xjtJ-BroZ-zhf5-mf56ag
PV UUID 1BGQXh-B2ud-Loui-aUMh-Sy4n-Ddov-iG1TQj
PV UUID nKbLtC-ls6T-uouN-H9PY-Vyc6-S24K-XJ5drP
PV UUID q0I3fc-y64W-xlLv-hPOd-lDaT-Wb9u-lmeg5U
4、恢复VG
[root@servert01 ~]# vgcfgrestore vg01 (恢复vg01)
Restored volume group vg01
[root@server01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz--n- 15.00g 0
vg01 3 2 0 wz--n- 111.99g 63.99g
[root@server01 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/mapper/mpatha vg01 lvm2 a-- 16.00g 16.00g
/dev/mapper/mpathb vg01 lvm2 a-- 32.00g 16.00g
/dev/mapper/mpathc vg01 lvm2 a-- 64.00g 32.00g
/dev/sda2 cl lvm2 a-- 15.00g 0
5、恢复LV
[root@server01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- 13.39g
swap cl -wi-ao---- 1.60g
lv01 vg01 -wi-a----- 16.00g
lv02 vg01 -wi-a----- 32.00g
如果发现lv的属性没有a,则需要激活lv
[root@server01 ~]# lvchange -ay /dev/vg01/lv01
[root@server01 ~]# lvchange -ay /dev/vg01/lv02
然后挂载LV,检查里边的数据。
[root@server01 ~]# mount /dev/mapper/vg01-lv01 /data01
[root@server01 ~]# mount /dev/mapper/vg01-lv02 /data02
[root@server01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- 13.39g
swap cl -wi-ao---- 1.60g
lv01 vg01 -wi-ao---- 16.00g
lv02 vg01 -wi-ao---- 32.00g
[root@server01 ~]# md5sum /data01/test.bin /data02/test.bin
230f9c3c6f9075da2ed62d7e9ec09789 /data01/test.bin
230f9c3c6f9075da2ed62d7e9ec09789 /data02/test.bin
四、配置关键点
恢复过程使用/etc/lvm/backup/VG_NAME文件,通过VG_NAME里边PV的UUID恢复PV,进而恢复VG。