If you have accidentally removed a logical volume (LV) or LVM meta data got corrupted or damaged in some way, it can be easily restored without losing the data using the ‘vgcfgrestore’ command.
In this guide, we’ll show you how to restore accidentally deleted a logical volume in Linux.
By default, LVM automatically takes backup of it’s meta data whenever a configuration change occurs on all LVM devices using the ‘vgcfgbackup’ command.
- /etc/lvm/backup : It contains a backup of the metadata, which taken after executing a command.
- /etc/lvm/archive : It contains metadata archives that were taken before executing a command.
Please go through the below articles, if you want to know more about LVM:
- Part-1: How to Create/Configure LVM (Logical Volume Management) in Linux
- Part-2: How to Extend/Increase LVM’s (Logical Volume Resize) in Linux
- Part-3: How to Reduce/Shrink LVM’s (Logical Volume Resize) in Linux
- Part-4: How to Remove Physical Volume from a Volume Group in LVM
- Part-5: How to Remove LVM (Logical) Volume in Linux
The vgcfgbackup command takes the configuration metadata from the LVM header of the disk and stores it in the default file location.
The vgcfgrestore command restores the metadata of a volume group (VG) from the latest archive backup.
vgcfgrestore [-f] [backup_file] vg_name
Checking existing LVMs
Existing logical volumes (LV) can be listed using the blow command:
To list available logical volumes, run:
# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert home rhel -wi-ao---- 12.95g root rhel -wi-ao---- 50.05g swap rhel -wi-ao---- 7.96g lv001 vg01 -wi-ao---- 10.00g lv002 vg01 -wi-ao---- 10.00g
Removing Logical Volume (LV)
We will remove the
'lv001' logical volume for demonstration purposes, which is the active logical volume listed in the above output.
# lvremove /dev/mapper/vg01-lv001 Do you really want to remove active logical volume "lv001"? [y/n]: y Logical volume "lv001" successfully removed
Recovering Logical Volume (LV)
Before restoring the deleted LVM, let’s identify the latest archive that contains the metadata of removed LVM. To do so, run the following command. It lists all VG metadata backups & archives.
# vgcfgrestore ---list vg01 File: /etc/lvm/archive/vg01_00000-381245830.vg VG name: vg01 Description: Created before executing 'vgcreate vg01 /dev/sdb /dev/sdc /dev/sdd' Backup Time: Sun Jul 19 04:01:03 2021 File: /etc/lvm/archive/vg01_00001-459034274.vg VG name: vg01 Description: Created before executing 'lvcreate -L 10G -n lv001 vg01' Backup Time: Sun Jul 19 04:03:15 2021 File: /etc/lvm/archive/vg01_00002-1063902356.vg VG name: vg01 Description: Created before executing 'lvcreate -L 10G -n lv002 vg01' Backup Time: Sun Jul 19 04:04:45 2021 File: /etc/lvm/archive/vg01_00003-586203914.vg VG name: vg01 Description: Created before executing 'lvremove /dev/mapper/vg01-lv001' Backup Time: Sun Jul 19 04:25:52 2021 File: /etc/lvm/backup/vg01 VG name: vg01 Description: Created after executing 'lvremove /dev/mapper/vg01-lv001' Backup Time: Sun Jul 19 04:25:52 2021
You can check if the archive back up doesn’t broken before performing the actual restore using ‘–test’ options as shown below:
# vgcfgrestore vg01 --test -f /etc/lvm/archive/vg_data_00003-586203914.vg TEST MODE: Metadata will NOT be updated and volumes will not be (de)activated. Volume group vg01 has active volume: lv001. WARNING: Found 1 active volume(s) in volume group "vg01". Restoring VG with active LVs, may cause mismatch with its metadata. Do you really want to proceed with restore of volume group "vg01", while 1 volume(s) are active? [y/n]: y Restored volume group vg01.
If dry run is successful, then perform the actual restore. This recover the deleted logical volume.
# vgcfgrestore vg01 -f /etc/lvm/archive/vg_data_00003-586203914.vg Volume group vg01 has active volume: lv001. WARNING: Found 1 active volume(s) in volume group "vg01". Restoring VG with active LVs, may cause mismatch with its metadata. Do you really want to proceed with restore of volume group "vg01", while 1 volume(s) are active? [y/n]: y Restored volume group vg01.
Check if the deleted volume is successfully restored using the ‘lvscan’ command. Yes, it was restored but it’s in inactive state.
# lvscan inactive '/dev/vg01/lv001' [10.00 GiB] inherit ACTIVE '/dev/vg01/lv002' [10.00 GiB] inherit
To activate logical volume, run:
# lvchange -a y /dev/vg01/lv001
Finally, mount the logical volume and verify the data:
# mount /dev/mapper/vg01-lv001 /lvmtest
Yes, i can see all our data:
# ls -lh /lvmtest
In this guide, we’ve shown you how to restore accidentally deleted a logical volume in Linux using the vgcfgrestore command.
If you have any questions or feedback, feel free to comment below.