Creating LVM Snapshots
A very useful feature of LVM is its ability to take what is known as a snapshot of a Logical Volume. This means that you make a copy or a point in time copy of an existing Logical Volume. Snapshots are an easy way to quickly copy another Logical Volume. Snapshots give you the ability to restore a Logical Volume back to a recorded point in time. For example, if you are about to make some changes to your system, you could create a snapshot. This would then allow you to roll the changes back if needed.
To create a Snapshot you would use the "lvcreate" command with the "-s" parameter.
Example Snapshot Command
lvcreate -n lvcopy -s /dev/vg01/lv01 -L 100M
The above command would create a new Logical Volume called "lvcopy". This Logical Volume would be a copy of the Logical Volume "lv01". The snapshot size in the example above is 100MB. Although this is a lot smaller then the size of the Logical Volume "lv01", this is perfectly OK as a snapshot only needs to be around 10% - 20% of the size of the original Logical Volume. Although the snapshot is smaller, it will appear to be the same size as the original Logical Volume when it is mounted. A snapshot only contains changes made to the original Logical Volume. In other words the bulk of your data is still held in the original Logical volume and only deviations from this are held in the snapshot.
Example of a Snapshot
Before we make any changes or issue the snapshot command, it is always a good idea to look at the current state of your Physical Volumes, Logical Volumes and Volume Groups. To do this we can issue the "pvs", "vgs" and "lvs" commands:
# pvs PV VG Fmt Attr PSize PFree /dev/sda2 system lvm2 a- 15.84g 1.68g /dev/sdb2 lint01vg lvm2 a- 40.00g 1020.00m /dev/sdb5 vg01 lvm2 a- 968.00m 168.00m # vgs VG #PV #LV #SN Attr VSize VFree lint01vg 1 1 0 wz--n- 40.00g 1020.00m system 1 4 0 wz--n- 15.84g 1.68g vg01 1 1 0 wz--n- 968.00m 168.00m # lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lint01lv01 lint01vg -wi-ao 39.00g home system -wi-ao 5.00g root system -wi-ao 6.73g tst system -wi-ao 1000.00m swap system -wi-ao 1.46g lv01 vg01 -wi-ao 800.00m
The Logical Volume we will take a snapshot of is called "lv01" and it is associated with the Volume Group "vg01".
To determine where our mount point is for our device "/dev/vg01/lv01" we can issue the mount command to list currently mounted filesystems:
/dev/mapper/vg01-lv01 on /john type ext3 (rw)
From the output taken from the mount command we can see that the mount point "/john" is used with "/dev/mapper/vg01-lv01".
For our example, we are going to navigate to the directory "/john" by issuing the command "cd /john".
From this directory, we are now going to copy some files across for our test:
# cp -R /home/john . # ls -l total 20 drwxr-xr-x 5 root root 4096 Apr 15 09:44 john drwx------ 2 root root 16384 Apr 15 09:38 lost+found
For our example I have copied the directory /home/john to our current directory "/john". Our next step is to create a snapshot.
Create a Snapshot
Next we need to issue our command to create the snapshot: lvcreate -n lvcopy -s /dev/vg01/lv01 -L 100M
# lvcreate -n lvcopy -s /dev/vg01/lv01 -L 100M Logical volume "lvcopy" created
To view our new Logical Volume "lvcopy", we can issue the command "lvs":
# lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lint01lv01 lint01vg -wi-ao 39.00g home system -wi-ao 5.00g root system -wi-ao 6.73g tst system -wi-ao 1000.00m swap system -wi-ao 1.46g lv01 vg01 owi-ao 800.00m lvcopy vg01 swi-a- 100.00m lv01 0.01
From the above output we can see our snapshot "lv01" along with how much space is being used under the heading "Snap%".
If we wanted to view the contents of our snapshot, then we can mount it in the normal manner. In the example below, we created a mount point called "snapshot":
# mkdir snapshot # mount /dev/vg01/lvcopy /snapshot # df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/system-root 6.7G 4.8G 1.6G 76% / devtmpfs 2.0G 180K 2.0G 1% /dev tmpfs 2.0G 188K 2.0G 1% /dev/shm /dev/sda1 152M 36M 109M 25% /boot /dev/mapper/system-home 4.0G 153M 3.6G 4% /home /dev/mapper/lint01vg-lint01lv01 39G 437M 37G 2% /MySQL /dev/mapper/system-tst 985M 18M 918M 2% /tst /dev/mapper/vg01-lv01 788M 17M 731M 3% /john /dev/mapper/vg01-lvcopy 788M 17M 731M 3% /snapshot
If you look at the output from the "df -h" command, we can see that "/john" and "/snapshot" are identical in size.
Now lets make some change to the original Logical Volume area "/john".
# ls -l total 92 -rw------- 1 root root 1633 Apr 15 09:44 .bash_history -rw-r--r-- 1 root root 1187 Apr 15 09:44 .bashrc -rw-r--r-- 1 root root 1637 Apr 15 09:44 .emacs drwxr-xr-x 2 root root 4096 Apr 15 09:44 .fonts -rw-r--r-- 1 root root 18251 Apr 15 09:44 .gnu-emacs -rw-r--r-- 1 root root 861 Apr 15 09:44 .inputrc drwxr-xr-x 2 root root 4096 Apr 15 09:44 .mozilla -rw-r--r-- 1 root root 6043 Apr 15 09:44 .muttrc -rw-r--r-- 1 root root 1028 Apr 15 09:44 .profile -rw------- 1 root root 3173 Apr 15 09:44 .viminfo -rw-r--r-- 1 root root 849 Apr 15 09:44 .vimrc -rw-r--r-- 1 root root 1940 Apr 15 09:44 .xim.template -rwxr-xr-x 1 root root 1446 Apr 15 09:44 .xinitrc.template drwxr-xr-x 2 root root 4096 Apr 15 09:44 bin -rw-r--r-- 1 root root 28 Apr 15 09:44 list.txt -rwxr-xr-x 1 root root 132 Apr 15 09:44 test.sh -rw-r--r-- 1 root root 1660 Apr 15 09:44 test1.txt -rw-r--r-- 1 root root 1660 Apr 15 09:44 test2.txt # rm -fr bin
Unmount Original LV
From the directory path "/john/john" we have deleted the "bin" directory with the command "rm -fr bin". If we require this directory to be reinstated, we can now use our snapshot.
First, we need to unmount the target Logical Volume with the command "umount /dev/vg01/lv01".
# umount /dev/vg01/lv01
Now we can reinstate our original Logical Volume to our point in time snapshot. To accomplish this we use the command lvconvert with the "--merge" option.
Command to be issued: lvconvert --merge /dev/vg01/lvcopy
# lvconvert --merge /dev/vg01/lvcopy Merging of volume lvcopy started. lv01: Merged: 0.1% lv01: Merged: 0.0% Merge of snapshot into logical volume lv01 has finished. Logical volume "lvcopy" successfully removed
After the "lvconvert" has run, our snapshot is automatically removed. Our Logical Volume "lv01" should now be back to its state when we ran our snapshot. We can now mount our LV again with the command "mount /dev/vg01/lv01 /john".
Our missing directory "/john/john/bin" will now be back.
If you see an error "Can't merge over open original volume", then you may need to issue the following commands to deactivate and reactivate your LV:
# lvchange -an /dev/VolumeGroup/LogicalVolume # lvchange -ay /dev/VolumeGroup/LogicalVolume