File Management

LVM: How to Snapshot a Volume or Directory

Snapshots are the copy of a filesystem, partition or a volume at a specific time. When a snapshot is captured, it copies all the data from a filesystem. In case of data loss, this snapshot can also be used to recover data. In this tutorial, we will use the lvm tool to snapshot a volume or a directory.

How to Snapshot a Volume

In the previous tutorial (How to Create Logical Volumes and Filesystems), we have created two logical volumes (lv01, lv02) on top of a volume group (vg01). We also created filesystems on top of these logical volumes. Now in this tutorial, we will write some data in lv01 logical volume and then we will take a snapshot of this logical volume. After this, we will verify whether the snapshot contains the same data as lv01’s logical volume.

First of all, use the lsblk command to display all the block devices containing volume groups and logical volumes.

Now create a directory ‘/home/$USER/lv02’ and mount the lv02 filesystem on this directory.

[email protected]:~$ sudo mkdir /media/$USER/lv02
[email protected]:~$ sudo mount /dev/vg01/lv02 /media/$USER/lv02

Copy some data in this filesystem.

[email protected]:~$ echo “This is lv02 filesystem” > /media/$USER/lv02/file.txt

The above command will create a file in the lv02 filesystem. Verify if the file is present in the logical volume using the cat command.

[email protected]:~$ cat /media/$USER/lv02/file.txt

Check for the free space in the volume group and the size of the filesystem on top of logical volume. Use the vgs command to check free space in the volume group. Similarly, use the lvs command to check the size of the filesystem.

[email protected]:~$ sudo vgs

[email protected]:~$ sudo lvs

The size of lv02 logical volume is 5 GB and the free space in the volume group vg01 is almost 15 GB. For testing purposes, we will create a snapshot of only 1GB. Before creating a snapshot of a logical volume, always make sure you have enough space to create the snapshot. Use the lvcreate command along with the -s flag to create the snapshot of the logical volume.

[email protected]:~$ sudo lvcreate -L 1GB -s -n lv02_snap /dev/vg01/lv02

OR

[email protected]:~$ sudo lvcreate --size 1GB --snapshot --name lv02_snap /dev/vg01/lv02

After creating the snapshot, check the logical volumes using the lvs command in the terminal.

[email protected]:~$ sudo lvs

It is showing the snapshot of the logical volume lv02 in the volume group vg01 and the size of the snapshot that is 1GB. The Origin column shows the origin of the snapshot that is lv02. Currently, the Data% for lv02_snap is 0.01. We will check it again after copying some data to the original logical volume lv02.

Mount the lv02_snap logical volume on the system using the mount command.

[email protected]:~$ sudo mkdir /media/$USER/lv02_snap

[email protected]:~$ sudo mount /dev/vg01/lv02_snap /media/$USER/lv02_snap

List all the filesystems mounted on the system using the df command in the terminal.

It is showing the original filesystem as well as the snapshot mounted on the system. Use the cat command to verify whether the file is present in this snapshot of the logical volume lv02 or not.

[email protected]:~$ cat /media/$USER/lv02_snap/file.txt

The file can be accessed through the snapshot. Copy some more data to the original logical volume lv02.

Again, display all the logical volumes using the lvs command in the terminal.

[email protected]:~$ sudo lvs

Previously the Data% was 0.01 and now it is 5.53. The data from the logical volume lv02 has been copied to the snapshot successfully.

Snapshot a Volume Using GUI Tool

So far, we have been using the command line interface to create a snapshot of logical volumes. Now, we will use the kvpm which is a GUI tool to manage logical volumes to create a snapshot of logical volume. We’ve already discussed the installation procedure of the kvpm in the previous tutorial (How to Create Logical Volumes and Filesystems). Open kvpm form the command line using the following command.

[email protected]:~$ sudo kvpm

From the top, go to the vg01 volume group tab.

It will display all the logical volumes from the volume group. In order to create a snapshot of the volume group lv01, select the lv01 logical volume and click on ‘Snapshot’.

It will ask for snapshot details. Provide the snapshot name and the size and click on ‘OK’.

It will create a snapshot of the logical volume lv01.

Verify from the command line whether the snapshot has been created or not using the lvs command.

[email protected]:~$ sudo lvs

Snapshot of size 1GB from logical volume lv01 has been created. Currently the Data% in the lv01_snap is 0. Now, mount the snapshot on the system using the mount command.

[email protected]:~$ sudo mkdir /media/$USER/lv01_snap

[email protected]:~$ sudo mount /dev/vg01/lv01_snap /media/$USER/lv01_snap

Copy some data to the logical volume lv01 and check from the GUI tool whether the snapshot of the lv01 occupies the space or not.

[email protected]:~$ sudo cp video.mp4 /media/$USER/lv01/

[email protected]:~$ ls /media/$USER/lv01/

Check from the kvpm whether the data from the logical volume lv01 has been copied to the snapshot or not.

How to Remove a Snapshot

In order to remove a snapshot of a logical volume properly, unmount the snapshot using the umount command first then use the lvremove command to remove the snapshot.

[email protected]:~$ sudo umount /dev/vg01/lv01_snap

[email protected]:~$ sudo lvremove /dev/vg01/lv01_snap

Using the GUI tool, select the snapshot, click on ‘unmount fs’ then click on delete to remove the snapshot.

Conclusion

Having backups of your data on a daily basis is one of the best strategies to avoid data loss. System administrators use different techniques to generate snapshots of volumes. In this tutorial, we used the lvm tool to create snapshots of the logical volume which can be restored in case of data loss.

About the author

Usama Azad

A security enthusiast who loves Terminal and Open Source. My area of expertise is Python, Linux (Debian), Bash, Penetration testing, and Firewalls. I’m born and raised in Wazirabad, Pakistan and currently doing Undergraduation from National University of Science and Technology (NUST). On Twitter i go by @UsamaAzad14