LVM2: Adding Secondary HDD to Volume Group and Using the Space for Snapshots

Case: OS: Solus 4.1 installed with LVM; drive: 250GB SSD taken over by the installation. Freeing up space for point-in-time snapshots/backups is required.

Solution

LVM Caveats

  1. Created snapshot increases in size with time, because it’s copy-on-write, not an archive. So it’s not good as a long-term backup solution.
  2. Snapshot must be configured to auto-increase size or it will become damaged if it runs out of preallocated space.
  3. Snapshot is removed once merged with the original logical volume.
  4. Snapshot must be recreated immediately after every restore.

LVM Snapshots Compared to Backup

  1. LVM snapshots allow you to capture a logical volume at a certain point in time and preserve it while with backup you copy and archive the content of a partition or logical volume.
  2. LVM Snapshot size is independent of the source logical volume size, as the LVM snapshot takes very less size while a backup would take almost equivalent size compared to the source logical volume (based on the compression type).
  3. There is a possibility that if the source logical volume continues to get filled then the LVM snapshot gets corrupted but a backup will stay intact on a safe location and is independent of the source logical volume usage.
  4. Hence a LVM snapshot must be created but should not be kept for a long time as it will end up eating a lot of space when source logical volume is used. In such cases backup is the preferred option instead of snapshot.

src: See Referecences, reference #4

Some Utility Commands

  • lvs – display logical volumes
  • lvscan – scan all drives for logical volumes
  • pvs – display physical volumes
  • pvscan – scan all drives for physical volumes
  • lvremove – remove logical volume from volume group
  • lvdisplay VolumeGroup/LogicalVolume – displayed detailed usage for the specified LV

Environment

LVS Output

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
Root SolusSystem -wi-ao---- <228.68g 
Swap SolusSystem -wi-ao---- <3.73g

PVS Output

PV VG Fmt Attr PSize PFree 
/dev/sda2 SolusSystem lvm2 a-- 232.40g 0

Adding a Physical Volume to Volume Groups

Add a physical volume from a second HDD (must be pre-formatted to LVM)

vgextend SolusSystem /dev/sdb1

Creating a Snapshot

Create a logical volume to the newly added sdb1. The LV is configured as a snapshot of root.

sudo lvcreate -s -n 2020-12-03T2054 -L 10g SolusSystem/Root

Flags:

  • -s – snapshot
  • -n – name
  • -L – size

Note: Snapshot name is the timestamp.

Merging/Restoring Snapshot

/!\ Create a new snapshot immediately after restoring as the old one is lost.

Specify the snapshot to be restored and it will automatically match its origin logical volume.

sudo lvconvert --merge SolusSystem/2020-12-03T2054

/!\ Create a new snapshot immediately after restoring as the old one is lost.

References

  1. Lvm – Ubuntu Wiki
  2. Managing LVM Snapshots in LVM2 – The Urban Penguin
  3. How to Shrink an LVM Volume Safely on Linux
  4. Linux lvm snapshot backup and restore tutorial RHEL/CentOS 7/8 | GoLinuxCloud
  5. A Linux user’s guide to Logical Volume Management | Opensource.com

Was this post helpful?