In this tutorial you wil learn how to add create LVM Snapshots and revert them back. The logical volume manager (LVM) software allows you to take a snapshot of a volume that has been configured using the LVM tools.

Snapshots are used to take a point in time copy of a volume, this is useful is you are upgrading software and need an easy and quick way to revert back changes in case something goes wrong.

Requirements

  • You will need root access to a Linux distribution
  • The server will need an LVM volume and some free space

Before you begin

For this tutorial, we will be using Debian 11, you can use other versions of Linux such as CentOS or AlmaLinux. If you require a server for testing, please visit our website.

Create An LVM Snapshot

When you take a snapshot, use the lvcreate command along with the “-s” or –snapshot option and remember not to use any “-” (i.e. dash/hyphen) in the name of the snapshot as some distributions will have trouble using it.

Issue the following command replacing vgroup with your existing volume group;

lvcreate --size 2G -s -n serversnapshot /dev/vgroup/original_lv

The above command would create a logical volume called serversnapshot which is a snapshot of original_lv (in the volume group called vgroup).

When you access data in the snapshot volume, you are effectively accessing the original logical volume’s data unless the following conditions are met;

  • the original volume’s data has changed, in which case you access a copy of the original data that is stored in the snapshot volume
  • you have written to the snapshot, which in this case the modified data resides in the snapshot volume.

You can create a snapshot of 50GB in size but will only a use a few Gigabytes, this is because most of the “snapshot” data will still resides on the original volume.

Mounting An LVM Snapshot

You can mount a snapshot which allows you to carry out tasks such as installing software on the volume without altering the original files.

This is useful if you want to install a peice of software and to test it operates as it should. If you are happy wit the test, you can carry out the same proceedure on the orginal volume.

You can also merge the snapshot into the original volume, this is useful if you have spent a long time configuring the software on the snapshot, but need to commit the changes quickly.

To mount a snapshot, create a new directory for the snaphot to be mounted to by running the following command:

mkdir /mnt/serversnapshot

Next, for our snapshot example, lets mount the snapshot to the mount point by running the following command:

mount /dev/vgroup/serversnapshot /mnt/serversnapshot

Then change into the directory where the snapshot is mounted:

cd /mnt/serversnapshot

Reverting And Merging The LVM Snapshot

Once you have a snapshot, you can update the original logical volume to the state of that snapshot should you need to.

Before changing the state of the logical volume to that of it’s snapshot, you need to unmount that logical volume if you want the change to happen immediately.

Alternatively, if you can’t unmount the volume, you can carry out the command but the changes will not take effect until you do a reboot.

In order to revert to the snapshot, you would simply use the lvconvert command using the -merge parameter as shown below

lvconvert --merge /dev/vgroup/serversnapshot

Now the original logical volume has been reverted/merged to the state of the snapshot. As previous mentioned, the server will need a reboot if you are unable to unmount the original volume first.

When you have used a snapshot to revert or merge, the snapshot will disappear from the system.

Removing An LVM Snapshot

If you no longer need to use a snapshot, you can simply remove it by issuing the following command:

lvremove /dev/vgroup/serversnapshot

You have successfully learned how to create LVM snapshots and revert back. Snapshots are very useful, and we recommend using them where possible before making changes to production systems.

Share.
Leave A Reply