SUSECON
  • Understanding LVM snapshots and how to use them.

    In this article we will look at some basic concepts and provide some simple commands to help you take advantage of this powerful tool.

    Before discussing snapshots you have to understand a bit about logical volumes (LVs) since a snapshot is, itself, a logical volume. The SLES documentation does a very good job of explaining that. It's a quick read and not at all complicated so have a look. There is no point in my simply repeating what is already well explained. The same applies to Snapshots.

    Storage Administration Guide - 4.0 LVM Configuration

    Storage Administration Guide - 17.0 Volume Snapshots

    Free space in the form of a disk (sda) or partition (sdb1) is assigned to a volume group (VG) to form a pool of free space. LVs, including their snapshots, are created from the free space available in the VG.

    A snapshot is just that. When it is created, it provides a view of the source LV that is frozen in time, even as the source LV continues to be modified. Applications that access the source LV may be completely unaware that a snapshot exists. If you mount the snapshot LV, what you will see is everything that was in the source LV when the snapshot was created. This is especially useful for making consistent backups of a changing LV. Once you are finished, you can simply remove the snapshot. The source LV is unaffected and life goes on.

    How does it work? ... just like magic. When it is initially created, the snapshot is empty. It definitely is not a full copy of the source LV. As the source LV is modified, the "before image" of the modified data blocks is copied to the snapshot LV. When viewing a mounted snapshot it is these blocks of data that are seen and not the modified ones. The more a source LV is modified, the more the snapshot LV begins to grow in size as additional "before images" are transferred to the snapshot LV. The initial size you choose for the snapshot LV will depend upon how long a snapshot is needed and how heavily the source LV is modified. Often this can be five or ten percent of the size of the source LV.

    There is another feature of snapshots that, in my opinion, makes them even more useful: you can merge all the blocks in the snapshot LV back into the source LV. This will undo all the changes made to the source LV since the snapshot was created. Let me provide an example of just how useful this can be.

    I have a number of virtual machines running in a Xen environment. When I created my DomUs I chose to use a block storage device instead of file backed storage. Rather than assigning a drive (sdb) or a partition (sdb1) to the DomU, I chose to use an LV. In my Dom0, that LV remains unformatted and unmounted. Now, before doing maintenance on my DomU and especially before upgrading to a new version, I can create a snapshot in Dom0. If all goes well, I'll remove the snapshot when I'm done but when things go terribly wrong, as they can sometimes do, I simply shutdown my DomU and merge the snapshot back into the source LV and I'm ready to try again.

    Let's look at an example:
    My Dom0 is "oak". I have an existing VG "vg100" that has some free space. I have already created a "test" LV and have assigned it to my DomU "birch". It is mounted at /mnt/test. In this example, it won't be the primary block device used by the DomU but will serve to show the effect of merging a snapshot.

    Lets begin by creating three files (file1, file2, and file3) in our new "test" LV.
    Code:
    birch:~ # cd /mnt/test
    birch:/mnt/test # touch file1
    birch:/mnt/test # touch file2
    birch:/mnt/test # touch file3
    birch:/mnt/test # ls
    .Trash-0  file1  file2  file3
    birch:/mnt/test #
    I shutdown the DomU before creating my snapshot to insure a consistent state:
    Code:
    birch:/mnt/test # shutdown -h now
    In the following section, only the lvcreate command is required. The rest is to show what other objects are created when setting up a snapshot. The "-s" specifies that this will be a snapshot. The snapshot will be 100 megabytes, it will be named "test-snap", the source LV is "/dev/vg100/test", and the snapshot LV will be created in the same VG as the source LV.
    Code:
    oak:~ # ls /dev/vg100/*test*
    /dev/vg100/test
    oak:~ # ls /dev/mapper/*test*
    /dev/mapper/vg100-test
    oak:~ # lvcreate -s -L 100M -n test-snap /dev/vg100/test
      Logical volume "test-snap" created
    oak:~ # ls /dev/vg100/*test*
    /dev/vg100/test  /dev/vg100/test-snap
    oak:~ # ls /dev/mapper/*test*
    /dev/mapper/vg100-test        /dev/mapper/vg100-test--snap-cow
    /dev/mapper/vg100-test--snap  /dev/mapper/vg100-test-real
    oak:~ #
    Let's start the DomU and continue.
    Code:
    oak:~ # xm start birch
    oak:~ #
    Let's verify the three files we created are still there.
    Code:
    birch:~ # cd /mnt/test
    birch:/mnt/test # ls
    .Trash-0  file1  file2  file3
    birch:/mnt/test #
    Now, we'll delete file3 and verify it is gone.
    Code:
    birch:/mnt/test # rm file3
    birch:/mnt/test # ls
    .Trash-0  file1  file2
    birch:/mnt/test #
    Shutdown the DomU
    Code:
    birch:/mnt/test # shutdown -h now
    When the DomU has shut down, we will use the lvconvert command with the --merge option to undo the changes made to our LV "test".
    Code:
    oak:~ # lvconvert --merge -v /dev/vg100/test-snap
        Using logical volume(s) on command line
        Archiving volume group "vg100" metadata (seqno 63).
        Found volume group "vg100"
        Found volume group "vg100"
        Loading vg100-test-real table (253:10)
        Suppressed vg100-test-real identical table reload.
        Loading vg100-test--snap-cow table (253:12)
        Suppressed vg100-test--snap-cow identical table reload.
        Loading vg100-test table (253:13)
        Loading vg100-test--snap table (253:9)
        Not monitoring vg100/snapshot0
        Suspending vg100-test (253:13) with filesystem sync with device flush
        Suspending vg100-test--snap (253:9) with filesystem sync with device flush
        Suspending vg100-test-real (253:10) with filesystem sync with device flush
        Suspending vg100-test--snap-cow (253:12) with filesystem sync with device flush
        Found volume group "vg100"
        Loading vg100-test-real table (253:10)
        Suppressed vg100-test-real identical table reload.
        Loading vg100-test--snap-cow table (253:12)
        Suppressed vg100-test--snap-cow identical table reload.
        Resuming vg100-test-real (253:10)
        Resuming vg100-test--snap-cow (253:12)
        Resuming vg100-test (253:13)
        Resuming vg100-test--snap (253:9)
      Merging of volume test-snap started.
        Creating volume group backup "/etc/lvm/backup/vg100" (seqno 64).
        Checking progress before waiting every 15 seconds
      test: Merged: 0.0%
      test: Merged: 0.0%
      Merge of snapshot into logical volume test has finished.
        Archiving volume group "vg100" metadata (seqno 64).
        Removing snapshot test-snap
        Found volume group "vg100"
        Found volume group "vg100"
        Loading vg100-test table (253:13)
        Suspending vg100-test (253:13) with device flush
        Suspending vg100-test--snap (253:9) with device flush
        Suspending vg100-test--snap-cow (253:12) with device flush
        Suspending vg100-test-real (253:10) with device flush
        Found volume group "vg100"
        Resuming vg100-test--snap-cow (253:12)
        Resuming vg100-test-real (253:10)
        Resuming vg100-test (253:13)
        Removing vg100-test-real (253:10)
        Found volume group "vg100"
        Removing vg100-test--snap (253:9)
        Removing vg100-test--snap-cow (253:12)
        Releasing logical volume "test-snap"
        Creating volume group backup "/etc/lvm/backup/vg100" (seqno 66).
      Logical volume "test-snap" successfully removed
    oak:~ #
    Let's just verify all the objects that were created along with the snapshot LV are gone... and they are.
    Code:
    oak:~ # ls /dev/vg100/*test*
    /dev/vg100/test
    oak:~ # ls /dev/mapper/*test*
    /dev/mapper/vg100-test
    oak:~ #
    Start the DomU one last time.
    Code:
    oak:~ # xm start birch
    oak:~ #
    Here we can see that the lvconvert --merge has restored file3 which we had deleted.
    Code:
    birch:~ # cd /mnt/test
    birch:/mnt/test # ls
    .Trash-0  file1  file2  file3
    birch:/mnt/test #
    This has been a very simple example to show what can be done with snapshots. I encourage you to do your own research and explore other uses. If you have questions or need help, please visit the SUSE forums for further assistance.
    https://forums.suse.com/forumdisplay...ure-Administer

    One last point, and this is very important: Once a snapshot has served its purpose, it must be removed using "lvremove" or merged back into it's source LV using "lvconvert --merge". If you do not do this, it will continue to grow until all its allocated space is used. When the snapshot is full, it is disabled, which prevents you from remounting the source logical volume.