Using LVM2 Snapshots to provide rollback functionality for Xen

Using LVM2 Snapshots to provide rollback functionality for Xen Domain loopback images

LVM2 is a set of tools which work with the device-mapper drivers of the linux kernel to provide volume management. The provide a convenient set of tools for creating,resizing and deleting volumes which can span or be striped over multiple disks. One of the convenient pieces of functionality provided by LVM is the ability to take a snapshot of an LVM volume and make it visible as another partition. This is typically useful when you want to take consistent backups. For example if you know your database is in a consistent state, you can lock it for a moment, create a volume snapshot which takes just a few seconds, and then unlock the database. You can then backup the snapshot volume to slow media or whatever. What has this got to do with [Xen http://www.xen.org/] the visualization mechanism? Well it turns out that you can write to an LVM2 snapshot, and the original volume is unaffected. This means you can create a snapshot of a volume containing lvm loopback images in your desired state, then run the virtual machine on the snapshot images instead. To rollback, you simply power off the machine even with xm destroy unmount and remove the snapshot, and then recreate the snapshot and reboot the virtual machine. A process that takes only a few seconds. Here below is how its done in practice:

1) Create the snapshot of /dev/vga/domU with enough space for 6GB of alterations

lvcreate -L6G -s -n domU.S /dev/vga/domU 

(the snapshot doesnt have to be the same size as the original, it only has to have enough space to store the differences)

2) Create a mount point to mount the snapshot:

mkdir -p /workarea/xen/domains.S

3) mount the snapshot:

mount -t auto /dev/vga/domU.S /workarea/xen/domains.S/

4) Start the virtual machine

xm create -c /etc/xen/myvirtual-S.cfg 

The configuration file will need to have had the paths to the loopback images updated so that they point to the versions on the snapshot

4) After the machine has finished unmount and remove the snapshot volumes:

umount /workarea/xen/domains.S  
lvremove -f /dev/mapper/vga-domU.S

5) rinse and repeat

When you create a new snapshot its condition will of course be the original version of the virtual machine provided no changes have been made to it in the mean time.