LVM Recovery
LVM Recovery
Advertisement
Create Volume Group
Next create a new Volume Group, we will name this VG as test_vg.
[root@centos-8 ~]# vgcreate test_vg /dev/sdb
List the available volume groups using vgs. I currently have two volume groups
wherein rhel volume group contains my system LVM2 partitions
[root@centos-8 ~]# vgs
Create File System on the Logical Volume
Create ext4 file system on this new logical volume
[root@centos-8 ~]# mkfs.ext4 /dev/mapper/test_vg-test_lv1
List the available volume groups along with the mapped storage device. Here as you see test_vg is
mapped to /dev/sdb
[root@centos-8 ~]# vgs -o+devices
Similarly you can see the new logical volume test_lv1 is mapped to /dev/sdb device
[root@centos-8 ~]# lvs -o+devices
Create a dummy file and note down the md5sum value of this file
[root@centos-8 ~]# touch /test/file
d41d8cd98f00b204e9800998ecf8427e /test/file
In this example we will use wipefs to delete LVM metadata from /dev/sdb device. Since the device
in question /dev/sdb is in use by Volume Group, we have to use -f to forcefully wipe the LVM
metadata
[root@centos-8 ~]# wipefs --all --backup -f /dev/sdb
We have used --backup so that before deleting the LVM metadata, wipefs will create a backup of
the ext4 signature containing LVM metadata under the home folder of the user who is executing the
command. Since we used root user, our LVM metadata backup is stored under root user's home
folder.
[root@centos-8 ~]# ls -l /root/wipefs-sdb-0x00000218.bak
HINT:
To restore lvm metadata stored in the file system signature from the backup we can use dd
if=~/wipefs-sdb-0x00000218.bak of=/dev/sdb seek=$((0x00000218)) bs=1
conv=notrunc
Next you can verify that all the logical volumes, volume groups and physical volume part
of /dev/sdb is missing from the Linux server
[root@centos-8 ~]# lvs -o+devices
Similarly with lsblk also we can verify that there are no LVM2 partitions under /dev/sdb
[root@centos-8 ~]# lsblk
To list the available backups of LVM metadata use vgcfgrestore --list. Currently we have
three backup stages where the last backup was taken after we created test_lv1 logical volume.
[root@centos-8 ~]# vgcfgrestore --list test_vg
File: /etc/lvm/archive/test_vg_00000-1327770182.vg
VG name: test_vg
File: /etc/lvm/archive/test_vg_00001-1359568949.vg
VG name: test_vg
File: /etc/lvm/backup/test_vg
VG name: test_vg
So we will use the last backup i.e. /etc/lvm/backup/test_vg to restore LVM metadata till the
stage where test_lv1 was created.
pv0 {
id = "SBJi2o-jG2O-TfWb-3pyQ-Fh6k-fK6A-AslOg1"
status = ["ALLOCATABLE"]
flags = []
pe_start = 2048
Next again it is important that you test the physical volume restore. We use --test mode to verify
the operation. With --test commands will not update LVM metadata. This is implemented by
disabling all metadata writing but nevertheless returning success to the calling function.
So here I have provided the same UUID of /dev/sdb as we collected earlier, followed by the
backup file we want to use to restore PV and then the device name using which we will perform
pvcreate. The pvcreate command overwrites only the LVM metadata areas and does not affect the
existing data areas.
[root@centos-8 ~]# pvcreate --test --uuid "SBJi2o-jG2O-TfWb-3pyQ-Fh6k-
fK6A-AslOg1" --restorefile /etc/lvm/backup/test_vg /dev/sdb
TEST MODE: Metadata will NOT be updated and volumes will not be
(de)activated.
With --test mode we know that the command execution is successful. So we will run the same
command without --test to restore PV in real.
[root@centos-8 ~]# pvcreate --uuid "SBJi2o-jG2O-TfWb-3pyQ-Fh6k-fK6A-
AslOg1" --restorefile /etc/lvm/backup/test_vg /dev/sdb
As we see that the command execution in --test mode was successful so now we can safely
execute our command to restore VG and recover LVM2 partition in Linux using vgcfgrestore.
[root@centos-8 ~]# vgcfgrestore -f /etc/lvm/backup/test_vg test_vg
Next verify the if you were able to restore deleted lvm and recover LVM2 partition using lvs.
[root@centos-8 ~]# lvs
Step 4: Activate the Volume Group
Next activate the volume group test_vg
[root@centos-8 ~]# vgchange -ay test_vg
If we are able to mount the logical volume so it means our ext4 file system signature is intact and
not lost or else the mount would fail.
[root@centos-8 ~]# ls -l /test/
total 16
Our test file exists and the md5sum matches the value of what we had taken before deleting the LVM
metadata
[root@centos-8 ~]# md5sum /test/file
So overall restore PV, restore VG, restore LVM metadata and recover LVM2 partition was
successful.