A Guide on LVM Snapshots with ext4 on CentOS 7.0

LVM is a logical volume manager for the Linux kernel that manages disk drives and similar mass-storage devices. These logical volumes can be composed of different physical hard disks but will appear as one partition on which a file system can be installed.

LVM snapshot can be used as a complete backup of your volume. It will allow you to restore the full volume in case something goes wrong. Centos 7 release made major changes on snapshots to make it more perfect. With Logical Volume snapshots, you are able to freeze a current state of any of your logical volumes. It is a special type of volume that presents all the data that was in the volume at the time the snapshot was created. This means that you can very easily create a backup and once needed rollback to an original logical volume state. This method is very similar to what you already know from using Virtualization where you can simply take a snapshot of the entire virtual machine and revert back in case something went wrong.

1. Creating the snapshot

A snapshot volume can be as large or a small as you like but it must be large enough to hold all the changes that are likely to happen to the original volume during the lifetime of the snapshot.

check LV information before

To create a new snapshot you need to indicate the VG and the LV corresponding which we are going to create a snapshot with

# lvcreate --size 1G --snapshot --name nameofthesnapshot /dev/nameofVG/nameofLV

The LVM snapshots don't contain any data initially, it will only store changes you make to the original volume, this will save a lot of space. Because of the snapshot increases in size as the origin volume changes, it is important to monitor the percentage of the snapshot volume regularly with the lvs command to be sure it does not fill. A snapshot that is 100% full is lost completely, as a write to unchanged parts of the origin would be unable to succeed without corrupting the snapshot.

If a snapshot runs out of space, the whole snapshot is dropped and cannot be used. However if you notice the snapshot is running out of space (check this using the lvdisplay or lvs commands), you can increase its size just like any other logical volume. After you create the snapshot it will be displayed in the output of the lvdisplay command like this:

2. Using the LVM snapshot

You can now use the lvm snapshot as a normal partition, you can mount it in a folder, make changes to it and if you are satisfied with the changes you can merge the changes to the original volume.

To mount it you just need to create a folder and use the mount command like this:

# mkdir /mnt/snap1
# mount /dev/test/snap1 /mnt/snap1

Now you can test anything you wish on the /mnt/snap1 folder without changing the original system.

If you are using XFS as the filesystem you will need to add the nouuid option to the mount command

# mount /dev/test/snap1 /mnt/snap1 -o nouuid,ro

When you mount the snapshot, the meta-data written when the snapshot was taken allows the mapping of snapshot pool blocks over changed blocks in the volume (or higher level snapshot). This way when an access comes for a specific block, LVM knows which block access. As far as the filesystem on that volume is concerned, there are no snapshots. As soon as you create a snapshot, LVM creates a pool of blocks. This pool also contains a full copy of the LVM metadata of the volume. When writes happen to the main volume such as updating an inode, the block being overwritten is copied to this new pool and the new block is written to the main volume. This is the copy-on-write. Because of this, the more data that gets changed between when a snapshot was taken and the current state of the main volume, the more space will get consumed by that snapshot pool.

3. Create a backup of our snapshot

Now we can create a backup of the snapshot on a disk or a partition we have created. You can choose a disk, a LUN, whatever you like to save it. Snapshots only grow if data on the original logical volume changes or if you write to the snapshot yourself (more about the write aspect later). So if we forget about us manually writing to snapshots for the moment, the only data in the snapshot are chunks of original volume data which have changed on the original volume since the snapshot was taken

You just need an emplacement to save the snapshot.

# tar -pczf /media/backup/snap1.tar.gz /mnt/snap1

or you can use

# dd if=/mnt/snap1 of=/media/backup/snap1.img

4. Merge the snapshot

You can only use a snapshot to revert an original logical volume if the original still exists. So snapshots are a fantastic feature but they are not backups in the conventional sense of having a complete copy of all the original data. If you want a completely independent separate copy of your data at the time of a snapshot, you can take a copy in the same way you would for any other device, e.g. using the cp or dd commands.

If you are satisfied with the changes you have made to the snapshot and wish to merge them in the original volume.  The merge will start immediately if the filesystems in the original volume and in the snapshot are unmounted.

This operation can take time because data needs to be copied from the snapshot to the original volume. As soon as the merge begins, any read-write operation to the original volume is transparently redirected to the snapshot that is in the process of being merged. Therefore, the operation is transparent to the user who thinks he’s using the merged volume. This means that as soon as the merge begins, users interact with a volume that contains the data at the time of the creation of the snapshot (+ data that has eventually been written to the snapshot since then).

You can use the lvconvert --merge command like in the example below:

If the original volume is mounted, you will have to reboot the system for the changes to take effect. Now the original logical volume has been reverted/merged to the state of the snapshot. Remember this will require a reboot if one of the volumes is mounted

Note that once you've used a snapshot to revert/merge, the snapshot disappears from your system.

5. Remove the snapshot

You can also remove the snapshot if you don't need the changes in it or don't have any more uses for it. If the logical volume is currently mounted, unmount the volume before removing it. In addition, in a clustered environment you must deactivate a logical volume before it can be removed. All data that has eventually been written to the snapshot is lost.

To remove the snapshot, use the following command:

lvremove /dev/test/snap1

As you can see using LVM snapshots is a great way to test and experiment different changes to the system in total safety.

Conclusion

The usefulness of logical volume snapshots is enormous and it will sure help you with your tasks whether you are a system administrator or a developer. LVM is the only Partition method in which we can expand more and have many features.

About Adrian Dinu

I’ve been a Linux enthusiast since 1998 and been working as a Linux Admin in the past 8 years with a lot of love for computers and networks.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

2 Comments

  1. hi,

    its article !!!
    Its a first blog where i got information of LVM on centos 7.0

    Could I get all info about LVM ?

    Keep posting such a great article...

    1. Thanks Medhansh. We have posted a link inside this article where you can find the lvm configuration.