Home / How to create LVM snapshot for system maintenance?

How to create LVM snapshot for system maintenance?

This article shows step-by-step instructions for getting a CentOS system ready and making LVM snapshots for system maintenance or software upgrades. LVM snapshots are meant to be used as a restoration point if anything goes wrong during the upgrade process.

The suggestion is to run this procedure in a test environment first. Also, one of the best things to do is to back up server data before any upgrades or maintenance, which can be related to data changes.

Find the file systems (mount points) that have production data using the mount command or look at the /etc/fstab configuration file. Check which disk devices the LVM volumes are using..

 lvs -a -o+devices

Logical volume “lv_test” is a member of volume group “vg00” and is located on disk “/dev/vda2”

$ lvs -a -o+devices

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert Devices 
lv_test vg00 -wi-ao---- 1.00g /dev/vda2(2048)

Identify all LVM physical volumes, their membership to volume groups, capacity, and available storage (PE).

pvs -v

In the below example, only one device “/dev/vda2” is a member of volume group “vg00”:

$ pvs -v

PV VG Fmt Attr PSize PFree DevSize PV UUID /dev/vda2 vg00 lvm2 a--u 9.50g 508.00m 9.50g Ov0Hu8-vWtW-t34A-GW7S-eGcY-NfYa-S3A4NA

A volume group can have one or more physical volumes. The “PFree” column shows how much physical storage space is free. Check the details of the volume groups and the available space, which are needed for an LVM snapshot.

# vgs -v

In the example below, the “VFree” column shows how much space is available in the volume group.

$ vgs -v

VG Attr Ext #PV #LV #SN VSize VFree VG UUID VProfile
vg00 wz--n- 4.00m 1 4 0 9.50g 508.00m iYed7D-w1hN-cfnB-cYPY-sSwp-R33U-p1T2E0

Add a new physical disk to the server and identify it as follows.

cat /proc/partitions

Create an LVM physical volume (PV) and extend the proper volume group (VG) using a new disk.

pvcreate <NEW DISK>
vgextend <VG NAME> <NEW DISK>

In the below example, /dev/vdb has been added to vg00 volume group. Check the new disk device

$ cat /proc/partitions | grep -i vdb

252 16 10485760 vdb

Create PV.

$ pvcreate /dev/vdb 
Physical volume "/dev/vdb" successfully created

Find info about the new PV as follows.

$ pvs | grep -i vdb

PV VG Fmt Attr PSize PFree 
/dev/vdb lvm2 ---- 10.00g 10.00g

Extend VG.

$ vgextend vg00 /dev/vdb

Volume group "vg00" successfully extended

Display information about PVs. Notice that /dev/vdb is a member of vg00 now.

$ pvs -v

PV VG Fmt Attr PSize PFree DevSize PV UUID 
/dev/vda2 vg00 lvm2 a--u 9.50g 508.00m 9.50g Ov0Hu8-vWtW-t34A-GW7S-eGcY-NfYa-S3A4NA
/dev/vdb vg00 lvm2 a--u 10.00g 10.00g 10.00g JeyBEP-1H5F-L1ls-Lntj-PzDz-FrU1-nHoSxY

Display information about VG. Notice that available storage (VFree) for vg00 is extended now by /dev/vdb size

$ vgs

VG #PV #LV #SN Attr VSize VFree 
vg00 2 4 0 wz--n- 19.49g 10.49g

Make LVM snapshots for all logical volumes in production. Before that operation, you should stop all applications and other writing processes to source LVM logical volumes that will need a snapshot. The snapshot size should be based on how much data will change on the source volume. Source logical volume capacity is the largest size that can be used.

lvcreate -L <snapshot size> -s -n <snapshot name> <source logical volume name> <device used for snapshot>

In the below example, it was created 1GB lv_test_snapshot for device lv_test. All snapshot data are being stored on device /dev/vdb.

$ lvcreate -L 1G -s -n lv_test_snapshot /dev/vg00/lv_test /dev/vdb

Logical volume "lv_test_snapshot" created.

The below output shows information about the source logical volume and its snapshot volume

$ lvs -a -o+devices

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert Devices 
lv_test vg00 owi-aos--- 1.00g /dev/vda2(2048)
lv_test_snapshot vg00 swi-a-s--- 1.00g lv_test 0.00 /dev/vdb(0)

If all the required snapshots are made successfully, system maintenance or software upgrades can be done. When the upgrade is successful, the snapshots can be deleted.

lvremove <snapshot LV device>

The original data on logical volumes can be returned if something goes wrong. The application must be shut down, and the filesystem must be unmounted.

<stop application using systemctl or another startup script>
umount <application filesystem>
lvconvert --merge <snapshot logical volume name>

The operation will start immediately if LV devices are not open (mounted). If not reboot will be required.

In the below example, the file system was not mounted, and lv_test has been restored from snapshot /dev/vg00/lv_test_snapshot. When a source volume lv_test was mounted again, previous data was visible and accessible.

$ lvconvert --merge /dev/vg00/lv_test_snapshot

Merging of volume lv_test_snapshot started.
lv_test: Merged: 90.6%
lv_test: Merged: 100.0%
Merge of snapshot into logical volume lv_test has finished.
Logical volume "lv_test_snapshot" successfully removed

Leave a Reply