LVM Snapshots

by on under Technical
1 minute read

When working with virtual machines, LVM snapshots are a handy feature (assuming the VMs are stored as LVM volumes).

Snapshots are Copy-on-Write (COW) logical volumes. Each time the original volume is written to, the snapshot volume grows/uses more space, to still represent the state when the snapshot was created. LVM can be configured to auto-extend the snapshot volume, if it runs out of space.
Snapshots can be read-only or read-write. Read-only are ideal for backup-porposes. Read-write snapshots, could be used to rapidly create new VMs from existing volumes.

Note: pvmove is unable to handle snapshots when moving volumes. I learned this the hard way, when trying to replace a physical volume. Fortunatly all my snapshots were not that important, so they could be discarded.

Snapshots as backup

lvcreate --snapshot --permission r --name vm-backup -L1g /dev/vg/vm  

Be sure to set the following in /etc/lvm/lvm.conf to enable auto extend of the snapshot volume. If auto extend is not desired, the snapshots size should be set up to the original volumes size.

snapshot_autoextend_threshold = 80  
snapshot_autoextend_percent = 20  

Restoring a backup is equivilant to merging the snapshot back into the original volume. Following command is used for this task:

lvconvert --merge /dev/vg/vm-backup  

The lvconvert –merge command removes the snapshot after it was merged. Be sure to recreate the snapshot after a successful merge.

Snapshots as VMs

lvcreate --snapshot --permission rw --name centos -L1g /dev/vg/centos-base

Using snapshots as VMs is propably not the best idea. LVM does not support snapshots of snapshots (tested version: 2.02.88). Thus, snapshot VMs can not be backed-up and restored using snapshots.
A better approach would be to use thin provisioning (available since kernel 3.2).

Linux, LVM