Linux Commands

LVM: How to List and Selectively Remove Snapshots

The lvm tool is used to create, manage, and visualize logical volumes, file systems, and snapshots in a system. In this tutorial, we will use the lvm tool to display detailed information of the logical volumes, volume groups, and snapshots and remove snapshots.

Listing Snapshots

Snapshots are created from logical volumes, and the logical volumes are the partitions on the volume groups. In the previous tutorials, we have discussed the steps of creating logical volumes, volume groups, and file systems. Also, we have discussed how we can create snapshots from logical volumes. First, list all the available volume groups on your physical volumes using the vgs, vgscan, or vgdisplay command.

ubuntu@ubuntu:~$ sudo vgs

OR

ubuntu@ubuntu:~$ sudo vgdisplay

OR

ubuntu@ubuntu:~$ sudo vgscan

Currently, there is only one volume group that is vg01. The #PV column shows the number of physical volumes spanned by the volume group. The volume group vg01 is spanning over one physical volume. The #LV column indicates the number of logical volumes on top of this volume group, and there are four logical volumes on top of the volume group vg01. The size of this volume group is less than 30GB, and there is a free space greater than 12GB available on the physical volume.

Next, use any one of the lvs, lvscan, or lvdisplay commands to list all the logical volumes created on top of the volume group. The lvs command will not only display all the logical volumes, but it will also display all the snapshots created from the logical volumes.

ubuntu@ubuntu:~$ sudo lvs

OR

ubuntu@ubuntu:~$ sudo lvdisplay

OR

ubuntu@ubuntu:~$ sudo lvscan

There are four logical volumes on top of the volume group vg01. Two logical volumes, lv01 and lv02, are the original logical volumes, whereas the other two lv01_snap and lv02_snap are snapshots of the logical volumes lv01 and lv02, respectively. The lvs command also shows the size of the logical volumes and snapshots and the origin of the snapshots. The lvscan command displays the type of logical volume, whether it is the original logical volume or snapshot, along with the mount point of the logical volumes. The lvscan command also shows the status of the logical volume, whether it is in an active state or not.

In order to list the physical volume on which all these logical volumes and volume groups are created, use the lsblk command in the terminal:

ubuntu@ubuntu:~$ sudo lsblk

The lsblk command shows the complete structure of the logical volumes and volume groups on top of physical volume and the size of each block.

Removing Snapshots

Before removing a snapshot, first, make sure it is not mounted on the system. List all the mounted file systems using the df command:

ubuntu@ubuntu:~$ sudo df -Th

The df command shows that both the snapshots are mounted, and the file system of these snapshots is ext4. Unmount the filesystem:

ubuntu@ubuntu:~$ sudo umount /dev/vg01/lv01_snap

NOTE: Make sure you have a backup of the snapshot before removing it from the volume group, as you will not be able to access the data after you remove it.

The snapshot has been unmounted. Now, remove the snapshot from the volume group using the lvremove command:

ubuntu@ubuntu:~$ sudo lvremove /dev/vg01/lv01_snap

After removing the snapshot, list all the logical volumes on volume group vg01 using the terminal:

ubuntu@ubuntu:~$ sudo lvs

The removed snapshot is not showing up in the list of logical volumes. If the snapshot is mounted on the system, the lvremove command will not remove the snapshot from the volume group:

Listing and Removing Snapshots using GUI Tool

In the previous sections, listing and removing the snapshots operation has been demonstrated using the command line interface. For experienced Linux users, it is easier to use the command line tool, but for beginners, it is much more challenging to use the command line tool. The kvpm tool provides a nice graphical user interface to interact with physical volumes, volume groups, and logical volumes. The installation process for this tool has been described in the previous article ( How to Create Logical Volumes and Filesystems). Open the kvpm tool by running the following command in the terminal:

ubuntu@ubuntu:~$ sudo kvpm

On the main page, in the “Storage Devices” tab, the tool shows all the block devices or physical volumes available on the system. Along with all the block devices, the kvpm shows the other details of the physical volumes like the capacity, free space, volume groups, and mount point.

In the “Group:vg01” tab, the kvpm shows all the details of the volume group vg01. It displays the total size, used space, available space, allocatable space, and other information of the volume group vg01. Below this information, the kvpm also shows all the available logical volumes and snapshots, with their size, type, and state.

Before deleting a snapshot, make sure you do not have any critical data or you have a backup of all the data available in the snapshot. In order to delete a snapshot using the graphical user interface tool, select the snapshot to be deleted, and click on the “delete” button:

Now, it will ask for the confirmation before deleting the snapshot:

Click on “Yes”, and the snapshot will be deleted. After deleting the snapshot, use the lvs, lvscan, or lvdisplay command in the terminal to check whether the snapshot has been deleted or not.

ubuntu@ubuntu:~$ sudo lvs

OR

ubuntu@ubuntu:~$ sudo lvdisplay

OR

ubuntu@ubuntu:~$ sudo lvscan

Only two logical volumes, lv01 and lv02, are available on the volume group vg01 and the snapshot has been deleted.

Conclusion

This tutorial used the command line and the graphical user interface to list and remove snapshots. The lvm tool is used to interact with logical volumes and snapshots using command line interface, while the kvpm tool provides a nice graphical user interface to interact with the logical volumes and volume groups.

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