Restore deleted logical volumes in LVM

Time:2021-11-30

This article describes how to recover a deleted Logical Volume. If you want to perform a recovery, you will use the vgcfgrestore command.
When performing any tasks related to logical volume management, if the logical volume is accidentally deleted, its data restore function can be used to restore the logical volume. In the following steps, we will demonstrate how to delete any logical volume in the volume group and restore the deleted Logical Volume.

System environment
Centos8

Existing file system
Before starting this article, you have created a logical volume and stored some files in it.

[[email protected] ~]# 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
[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz–n- <19.00g <2.00g
vg_data 2 1 0 wz–n- 39.99g 0
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao—- 15.00g
swap cl -wi-ao—- 2.00g
lv_data vg_data -wi-ao—- 39.99g
[[email protected] ~]# df -h /data
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_data-lv_data 40G 318M 40G 1% /data
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
/The files in the data directory are as follows:
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM

Unexpected deletion of logical volume
For demonstration purposes, delete the logical volume Lv_ data。

[[email protected] ~]# umount /data
[[email protected] ~]# lvremove -v /dev/vg_data/lv_data
Do you really want to remove active logical volume vg_data/lv_data? [y/n]: y

Accepted input: [y]
Removing vg_data-lv_data (253:2)
Archiving volume group "vg_data" metadata (seqno 2).
Releasing logical volume "lv_data"
Creating volume group backup "/etc/lvm/backup/vg_data" (seqno 3).

Logical volume “lv_data” successfully removed
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
Use the LVS command to print and confirm that we have deleted the logical volume Lv_ data:
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
Due to LV_ The data volume has been deleted and you can see the volume group VG_ The remaining space of data is 40g.

[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz–n- <19.00g <2.00g
vg_data 2 0 0 wz–n- 39.99g 39.99g
Start restoring logical volumes
Now let’s look at how to restore a deleted Logical Volume. Whenever we perform any task related to LVM (e.g., backup), all contents will be recorded under / etc / LVM / archive. And use the vgcfgbackup command to back up the volume group configuration and store it under / etc / LVM / backup.

[[email protected] ~]# ll /etc/lvm/backup/
total 8
-rw——-. 1 root root 1746 Mar 9 16:27 cl
-rw——-. 1 root root 1147 Apr 8 10:20 vg_data
[[email protected] ~]# ll /etc/lvm/archive/
total 24
-rw——-. 1 root root 1740 Dec 29 2019 cl_00000-1531402016.vg
-rw——-. 1 root root 1740 Mar 9 16:27 cl_00001-790744555.vg
-rw——-. 1 root root 1747 Mar 9 16:27 cl_00002-1369752011.vg
-rw——-. 1 root root 1150 Apr 8 09:52 vg_data_00000-919482485.vg
-rw——-. 1 root root 1155 Apr 8 09:54 vg_data_00001-1289102393.vg
-rw——-. 1 root root 1763 Apr 8 10:20 vg_data_00002-1932879749.vg
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
In the above step to delete a logical volume, use lvremove – V / dev / VG_ data/lv_ When using the data command, you can see the output details, including archiving and create backup.
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
The following lists and checks how many volume group metadata backups are available:

[[email protected] ~]# vgcfgrestore –list vg_data

File: /etc/lvm/archive/vg_data_00000-919482485.vg
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.
VG name: vg_data
Description: Created before executing ‘vgcreate vg_data /dev/sda /dev/sdb’
Backup Time: Thu Apr 8 09:52:49 2021

File: /etc/lvm/archive/vg_data_00001-1289102393.vg
VG name: vg_data
Description: Created before executing ‘lvcreate -l 100%FREE -n lv_data vg_data’
Backup Time: Thu Apr 8 09:54:45 2021

File: /etc/lvm/archive/vg_data_00002-1932879749.vg
VG name: vg_data
Description: Created before executing ‘lvremove -v /dev/vg_data/lv_data’
Backup Time: Thu Apr 8 10:20:53 2021

File: /etc/lvm/backup/vg_data
VG name: vg_data
Description: Created after executing ‘lvremove -v /dev/vg_data/lv_data’
Backup Time: Thu Apr 8 10:20:53 2021
–The list option lists the metadata backup and archive files related to VG.
What we want to restore is to delete the archive before the volume group, so select / etc / LVM / archive / VG circled below_ data_ 00002-1932879749.vg file.
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
Before restoring, use the — Test option to try the following:

[[email protected] ~]# vgcfgrestore vg_data -f /etc/lvm/archive/vg_data_00002-1932879749.vg –test
TEST MODE: Metadata will NOT be updated and volumes will not be (de)activated.
Restored volume group vg_data.
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
After the test run is OK, remove the — Test option, and then execute it again:

[[email protected] ~]# vgcfgrestore vg_data -f /etc/lvm/archive/vg_data_00002-1932879749.vg
Restored volume group vg_data.
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
You can see a successful recovery of the volume group VG_ data。

-The f option reads the metadata file created by vgcfgbackup.

Using LVS to view LVS_ Whether the data logical volume has been recovered, and use lvscan to check whether the logical volume is activated:

[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao—- 15.00g
swap cl -wi-ao—- 2.00g
lv_data vg_data -wi——- 39.99g
[[email protected] ~]# lvscan
ACTIVE ‘/dev/cl/swap’ [2.00 GiB] inherit
ACTIVE ‘/dev/cl/root’ [15.00 GiB] inherit
inactive ‘/dev/vg_data/lv_data’ [39.99 GiB] inherit
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
Visible LV_ Data has been restored, but the status is inactive. The next step is to activate the logical volume.

Activate logical volume
Activate the logical volume using the lvchange command:

[[email protected] ~]# lvchange -ay /dev/vg_data/lv_data
[[email protected] ~]# lvscan
ACTIVE ‘/dev/cl/swap’ [2.00 GiB] inherit
ACTIVE ‘/dev/cl/root’ [15.00 GiB] inherit
ACTIVE ‘/dev/vg_data/lv_data’ [39.99 GiB] inherit
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao—- 15.00g
swap cl -wi-ao—- 2.00g
lv_data vg_data -wi-a—– 39.99g
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
As shown in the figure above, you can see that the “/ dev / vg_data / lv_data” logical volume has been activated.

Mount and inspect
Mount file system:

[[email protected] ~]# mount /dev/vg_data/lv_data /data/
[[email protected] ~]# df -h /data
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_data-lv_data 40G 318M 40G 1% /data
Restore deleted logical volumes in LVM restore deleted logical volumes in LVM
You can see that the files already exist.

summary
This article has successfully recovered the deleted logical volumes from the metadata of the volume group.