LVM Snapshots

From Thomas-Krenn-Wiki
Jump to: navigation, search

LVM Snapshots simplifies Point-In-Time copies of logical volumes (LVs).[1] However, snapshots are not true copies of the reference LVs. If the original LV has been changed after the creation of the snapshot then the original data in the snapshot will be copied into the snapshot first, Copy-on-Write.

Functional Approach and Applications

Snapshots do not allocate disk space themselves like their associated original LVs, because the snapshot only requires space when the original has been changed. What is important is keeping an eye on the free space for the snapshot volume, since a fully consumed snapshot volume will be unusable, because changes in the original will not be able to be logged[2]. The consumption of a snapshot volume can be checked by means of the lvs command. It should be emphasized that snapshots themselves are not intended for use as a means of making backups, since only the changes will be saved. The following would be a typical backup scenario:

  1. Create the snapshot
  2. Create a backup of the snapshot data; the original LV can continue to run and be "online".
  3. Delete the snapshot again, since otherwise the changes would continue to be tracked.

Doing this can avoid, for example, having to shutdown the server to make a backup, since the snapshot makes all of the data available for the backup[3].

An additional application of snapshot might be for experimental tests, which should not be performed on the original file system. For such purposes, a snapshot can be created, mounted and the tests then performed. Using this approach, only the snapshot would be changed and the original file system would remain unmodifed[4].

Example Configuration

In order to create a snapshot there has to bo unallocated, free space in the volume group. If it is the case that there is no more space left the volume group can be extended with vgextend (linux.die.net).

In the following volume group are still 2GB left being used for a snapshot:

root@ubuntu:~# vgdisplay
  --- Volume group ---
  VG Name               vg00
[...]
  VG Size               7.98 GiB
  PE Size               4.00 MiB
  Total PE              2044
  Alloc PE / Size       1533 / 5.99 GiB
  Free  PE / Size       511 / 2.00 GiB
  VG UUID               YTEj9f-9LCT-EOP5-JBEA-YHSz-c0R1-TMzVmy

The existing space of 2 GiB on vg00 can be used as a snapshot volume.

root@ubuntu:~# lvcreate -l100%FREE -s -n data_snap /dev/vg00/data
  Logical volume "data_snap" created

The entire 2 GiB snapshot LV is now available for the LV "data". If the 2 GiB should now be partitioned, in order to use it as a snapshot LV for several LVs, data_snap would first have to be deleted.

root@ubuntu:~# lvremove /dev/vg00/data_snap
  Do you really want to remove active logical volume data_snap? [y/n]: y
  Logical volume "data_snap" successfully removed

The existing space is available again and can be divided.

root@ubuntu:~# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  vg00   4   2   0 wz--n- 7.98g 2.00g
root@ubuntu:~# lvcreate -l50%FREE -s -n data_snap /dev/vg00/data
  Logical volume "data_snap" created
root@ubuntu:~# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  vg00   4   3   1 wz--n- 7.98g 1.00g
root@ubuntu:~# lvcreate -l100%FREE -s -n data_snap1 /dev/vg00/data1
  Logical volume "data_snap1" created

If data in the file system is now changed as part of ongoing operations then its original content will first be copied into the snapshot. This block will also be flagged as "copied" in the exception table. Since the lvm2-format, snapshots are created as read/write by default. If a snapshot is accessed, the changed blocks will be flagged as "used" in the exception table and will never be copied from that point on. Regarding the size of the snapshot, a maximum of 1 GiB of "data" may be changed in the original volume, so that the snapshot will remain useful. If more data is changed on the original volume, the snapshot will be destroyed and lost.[5]

For creating a backup, the snapshot can be mounted as usual and backed up afterwards. In the example above, there was "data" located on the LV from the time point of the creation of "data_snap", based on which only a snapshot existed. This file should now be deleted and its existence in the snapshot verified.

root@ubuntu:~# mount /dev/vg00/data data
root@ubuntu:~# mount /dev/vg00/data_snap data_snap
root@ubuntu:~# cd data
root@ubuntu:~/data# l
  file  lost+found/
root@ubuntu:~/data# rm file
root@ubuntu:~/data# cd ..
root@ubuntu:~# cd data_snap/
root@ubuntu:~/data_snap# l
  file  lost+found/

The deleted file in the snapshot will remain available and can be also be backed up, or even restored, for that reason.

References

  1. LVM HOWTO Section 3.8. Snapshots
  2. http://tldp.org/HOWTO/LVM-HOWTO/snapshotintro.html
  3. http://tldp.org/HOWTO/LVM-HOWTO/snapshots_backup.html
  4. http://tldp.org/HOWTO/LVM-HOWTO/snapshotintro.html
  5. https://www.redhat.com/archives/linux-lvm/2007-August/msg00089.html

Additional Information


Foto Werner Fischer.jpg

Author: Werner Fischer

Werner Fischer, working in the Web Operations & Knowledge Transfer team at Thomas-Krenn, completed his studies of Computer and Media Security at FH Hagenberg in Austria. He is a regular speaker at many conferences like LinuxTag, OSMC, OSDC, LinuxCon, and author for various IT magazines. In his spare time he enjoys playing the piano and training for a good result at the annual Linz marathon relay.


Related articles

Increasing a Logical Volume
Partition Alignment