By creating a Logical Volume snapshots you are able to freeze a current state of any of your logical volumes. This means that you can very easily create a backup and once needed rollback to a original logical volume state. This method is very similar to what you already know from using Virtualization software such as Virtualbox or VMware where you can simply take a snapshot of entire virtual machine and revert back in case something went wrong etc. Therefore, using LVM snapshots allows you to take a control of your system's logical volumes whether it is your personal laptop or server. This tutorial is self-contained as no previous experience with Logical Volume Manager is required.
In this article we will explain how to manually create and restore logical volume snapshots. Since we do not assume any previous experience with Logical Volume Manager we will start from a scratch using a dummy physical hard drive /dev/sdb with size of 1073 MB. Here are all steps in nutshell:
· 1First we will create two partitions on our /dev/sdb drive. These partitions will be of "8e Linux LVM" type and will be used to create a physical volumes
· 2Once both partitions are created we use pvcreate command to create physical volumes
· 3In this step we create a new Logical Volume Group and a single 300MB in size logical volume using ext4 filesystem
· 4Mount our new logical volume and create some sample data
· 5Take a snapshot and remove sample data
· 6Rollback logical volume snapshot
3. Creating a Logical Volume
3.1. Logical Volume Manager Basics
Here is a quick start definition of logical volume manager:
Logical volume manager allows you to create a Logical group consisting of multiple physical volumes. Physical volumes can be entire hard-drives or separate partitions. Physical volumes can reside on a single or multiple hard-drives, partitions , USBs, SAN's etc. To increase a Logical Volume size you can add additional physical volumes. Once you create Logical volume group you can then create multiple Logical volumes and at the same time completely disregard a physical volume layer. Logical volume group can be resized at any time by adding more physical volumes so new logical volumes can created or resized.
3.2. Create a partitions
First, we need to create a partitions and mark them as physical volumes. Here is our physical disk we are going to work with:
Let's create two primary partitions. Here we are using fdisk to do tis job. Feel free to use any other partitioning tool to do this job such as cfdisk, parted etc.
All command are highlighted in bold:
If you followed the above steps, your new partition table on the disk /dev/sdb will now look similar to the one below:
3.3. Create Physical Volumes
At this point we mark both partitions as physical volumes. Please note that you do not have to follow the same pattern as in this tutorial. For example you could simply partition entire disk with a single partition instead of two. Use pvcreate to create physical volumes:
3.4. Create Volume Group
Now it is time to create a Volume Group. For this we use tool vgcreate. The new Volume Group will have a name "volume_group".
After execution of the above command you will have a new volume group created named "volume_group". This new volume group will consist of two physical volumes:
You can see the stats of your new volume group using vgdisplay command:
3.5. Creating Logical Volumes
If all went smoothly we can now finally create a logical volume. The size of the logical volume must not exceed the size of your logical group. Let's create new logical volume called "volume1" of size 200 MB and format it with ext4 filesystem.
You can see a definition of your new logical volume using lvdisplay command. Take a note of the LV Path value as you will need it when creating a filesystem on your new h"volume1" logical volume.
Now you can create an ext4 filesystem on your logical volume:
4. Logical Volume Snapshot
Finally, we have come to the point where we can take a snapshot of our logical volume created in previous section. For this we will also need some sample data on our Logical Volume "volume1" so once we revert from the snapshot we can confirm entire process by comparing original data with data recovered from the snapshot.
4.1. Understanding Snaphosts
In order to understand how snapshots work we first need to understand what logical volume consists of and how data are stored. This concept is similar to well known symbolic links. When you create a symbolic link to a file you are not creating a copy of the actual file but instead you simply create only a reference to it. Logical volume stores data in a similar fashion and it consists of two essential parts:
· metadata pointers
· data block
When a snapshot is created Logical Volume Manager simply creates a copy of all Metadata pointers to a separate logical volume. Metadata do not consume much space and therefore your are able to create snapshot of let's say 2GB logical volume to 5MB snapshot volume. The snapshot volume only starts grow once you start altering data of the original logical volume. Which means, that every time you remove or edit file on the original logical volume a copy of that file ( data ) is created on snapshot volume. For a simple changes you may need to create a snapshot volume of around 5-10% of the logical volume original size. If you are prepared to make many changes on your original logical volume then you will need lot more than 10%. Let's get started:
4.2. Sample Data
First, create a new mount point directory for "volume1" and mount it :
Enter "volume1" mount point and copy some sample data:
Using previous commands we have copied entire /sbin directory into /mnt/volume1. The size of /mnt/volume1/sbin/ is currently 8264 KB.
Creating a Snapshot
Now we are going to create a snapshot of logical volume "volume1". In the process Logical Volume Manager will create a new separate logical volume. This new logical volume will have size of 20MB and will be called "volume1_snapshot":
Execute lvs command to confirm that new volume snapshot has been created:
Now that the snapshot has been created we can start altering data on "volume1" for example by removing the entire content:
After this operation you can consult again lvs command and see that Data% on the volume1_snap is now increased. If you want to, you can now mount your snapshot volume to confirm that the original data from "volume1" still exists.
4.3. Revert Logical Volume Snapshot
Before we revert our logical volume snapshot, let's first confirm that our /mnt/volume1/sbin data are still missing:
Recovering a Logical Volume snapshots consists of two steps:
· scheduling a snapshot recovery after next logical volume activation
· deactivate and activate logical volume
To schedule a snapshot rollback execute a following command:
After execution of the above command the logical volume "volume1" will rollback once it is activated. Therefore, what needs to be done next is to re-activate "volume1". First, make sure that you unmount your "volume1"
Deactivate and activate you volume:
As a last step mount again your logical volume "volume1" and confirm that data all has been recovered:
The above was a basic example of snapshot manipulation using Logical Volume Manager. The usefulness of logical volume snapshots is enormous and it will sure help you with your tasks whether you are system administrator or a developer. Although you can use the setup above to create a multiple snapshots for a backup recovery you also need to know the you backup will find its limits on within you Logical Volume Group therefore any low level physical volume problems may render your snapshot useless.