A LVM snapshot is a special logical volume(LV) that when created is a copy of another LV at that point in time. A sysadmin can use this facility to make quick backups of LVs even if they are mounted and being used by a running system. This does require that the snapshot be made when the data on the LV is a consistent state. Thanks to the VFS-lock patch in LVM1, many filesystems (including ext3) can allow consistent snapshots to be taken at anytime. However do check if your filesystem allows this or require extra steps before a consistent snapshot can be taken. The XFS filesystem for example maybe require the sysadmin to do a xfs_freeze to pause the filesystem first.
Here’s some things that I’ve observed about snap shot volumes
1) Snapshot volumes consumes free LVM extents.
# vgdisplay vg1
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 700.00 MB
PE Size 4.00 MB
Total PE 175
Alloc PE / Size 125 / 500.00 MB
Free PE / Size 50 / 200.00 MB
VG UUID IE77Fv-Lr7z-2H8c-JsMR-fPNG-jx4s-wLNtck
# lvcreate -s -n snaptest1 /dev/vg1/test_lv -L 100M
Logical volume "snaptest1" created
# vgdisplay vg1
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 16
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 700.00 MB
PE Size 4.00 MB
Total PE 175
Alloc PE / Size 150 / 600.00 MB
Free PE / Size 25 / 100.00 MB
VG UUID IE77Fv-Lr7z-2H8c-JsMR-fPNG-jx4s-wLNtck
Here I am creating a 100M snapshot named “snapshot1” on test_lv (500M). Both of them reside on the volume group “vg1“. snapshot1 is a 100MB snapshot1 and so requires 100M from vg1. There does not seems to be anyway to specify another volume group for this which implies that a snapshot must be created in the same volume group as the LV that it is based on.
2) LVM2 snapshot volumes can be mounted read/write.
It will appear to be the same as the target volume at the point in time it was created.
# mount /dev/vg1/snaptest1 /mnt/test2
# ls /mnt/test*
/mnt/test:
lost+found testfile.txt
/mnt/test2:
lost+found testfile.txt
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg1-test_lv
485M 17M 443M 4% /mnt/test
/dev/mapper/vg1-snaptest1
485M 17M 443M 4% /mnt/test2
# cat /mnt/test/testfile.txt
this is a test file
# cat /mnt/test2/testfile.txt
this is a test file
Writing (reading too but only a tiny amount) to a snapshot volume will consume snapshot disk space, see next point. Note that LVM1 snapshot volumes are read-only.
3) Block level changes consume snapshot space
# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
opt_lv vg0 -wi-ao 1.00G
root_lv vg0 -wi-ao 6.00G
swap_lv vg0 -wi-ao 1.00G
snaptest1 vg1 swi-a- 100.00M test_lv 0.01
test_lv vg1 owi-ao 500.00M
# dd if=/dev/zero of=/mnt/test/test1.img bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0414562 s, 506 MB/s
# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
opt_lv vg0 -wi-ao 1.00G
root_lv vg0 -wi-ao 6.00G
swap_lv vg0 -wi-ao 1.00G
snaptest1 vg1 swi-a- 100.00M test_lv 20.19
test_lv vg1 owi-ao 500.00M
Creating a 20MB file on test_lv uses up 20% of snapshot1’s extents (see the Snap% column). This works up to 20MB.
Writing to the snapshot volume also uses up snapshot space.
# dd if=/dev/zero of=/mnt/test2/test1_2.img bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0216117 s, 485 MB/s
# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
opt_lv vg0 -wi-ao 1.00G
root_lv vg0 -wi-ao 6.00G
swap_lv vg0 -wi-ao 1.00G
snaptest1 vg1 swi-ao 100.00M test_lv 30.27
test_lv vg1 owi-ao 500.00M
Here creating a 10M file on the mounted snapshot volume used up 10MB worth of snapshot space.
However deletion only uses a tiny bit of snapshot space.
# rm /mnt/test/test1.img
# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
opt_lv vg0 -wi-ao 1.00G
root_lv vg0 -wi-ao 6.00G
swap_lv vg0 -wi-ao 1.00G
snaptest1 vg1 swi-ao 100.00M test_lv 30.29
test_lv vg1 owi-ao 500.00M
Snapshot LVs store changes at the block level only. Creating a 20MB file will result in 20MB worth of block level changes and hence require 20MB of snapshot space . A deletion only involves the wiping of a file inode which changes only a few blocks.
Interestingly, other seemingly non-modifying operations such as mounting/unmounting, file listing with the ls command will also consume a bit of snapshot space each time they are performed. These operations modify the internal filesystem structures invisibly to the user.
4) Once a snapshot LV is full, it is no longer usable.
# dd if=/dev/zero of=/mnt/test/test2.img bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.217156 s, 483 MB/s
# lvs
/dev/dm-4: read failed after 0 of 4096 at 0: Input/output error
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
opt_lv vg0 -wi-ao 1.00G
root_lv vg0 -wi-ao 6.00G
swap_lv vg0 -wi-ao 1.00G
snaptest1 vg1 Swi-Io 100.00M test_lv 100.00
test_lv vg1 owi-ao 500.00M
Here after creating a 100MB file on test_lv, snapshot1 is now 100% full (see the Snap% column). The snapshot LV is now not usable and a new snapshot should be created to replace it.
What does it mean for a snapshot to be unusable?
If it is still mounted you might still be able to read from it.
# cat /mnt/test2/testfile.txt
this is a test file
But you won’t be able to write to it
# touch /mnt/test2/testfile2.txt
touch: cannot touch `/mnt/test2/testfile2.txt': Read-only file system
And if you won’t be able to mount it again.
# mount -t ext3 /dev/vg1/snaptest1 /mnt/test2
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg1-snaptest1,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so
Uses of snapshots
Snapshot volumes are great for making quick copies of logical volumes for backups or experiments. For example, you can make nightly backups of the home directories of your users so that when they lose important data, you can restore them easily. Another use might be to make a quick copy of your virtual machines’ file backends for experimentation.
Performance
Active snapshot volumes do incur extra I/O operations and may cause performance degradation. This is something to be aware of.