maxoidos - Fotolia

Manage Learn to apply best practices and optimize your operations.

How to manage and reset a KVM virtual classroom

A simple KVM deployment can get your virtual classroom up and running, but you also need a strategy for managing and restoring VMs after a course ends.

In the first part of this two-part series, you learned how to set up an affordable KVM-based virtual classroom environment and how to use Kernel Samepage Merging to efficiently share memory. In this article, you'll learn how to manage your classroom and easily roll back a VM to its original state.

At the start of each virtual class, you normally want to prepare a clean classroom environment for the course participants. By the end of a class, each participant has normally modified many facets of the VMs. To prepare for the next class, it's important to be able to revert to the original state as fast as possible. Logical Volume Manager snapshots may help with that.

Logical Volume Manager (LVM) is a proven approach that has been used in Linux for many years and that provides a flexible way of dealing with storage. Instead of working with fixed storage allocation units, LVM works with versatile volumes that can easily be enlarged or shrunk. These volumes offer other benefits as well, including snapshots.

After creating an LVM snapshot, there will be two volumes referring to the same data. The original volume keeps recording changes, while the LVM volume keeps the original state of the volume at the moment the snapshot was taken. When changes are written to the original volume after the snapshot was taken, the original data blocks are copied to the snapshot volume.

The metadata in the snapshot points to two different locations when accessing data blocks. Data blocks that haven't been changed since the creation of the snapshot are still on the original volume, while the original version of changed data blocks is kept in the snapshot. That allows you to use the snapshot to access the state of the volume as it was at the moment the snapshot was taken (see Figure 1).

An LVM snapshot records metadata and maintains original storage blocks for later recovery

KVM and LVM snapshots

If a KVM VM is installed using all default values, the storage back end of the VM's disk is in a file. If you create an LVM logical volume before starting the installation of the VM, this logical volume can be used as the storage back end of the VM. An LVM disk is referred to from the KVM XML code in the configuration file in /etc/libvirt/qemu.

<disk type='block' device='disk'>
   <driver name='qemu' type='raw' cache='none' io='native'/>
   <source dev='/dev/centos/lvvm0'/>
   <target dev='vda' bus='virtio'/>
   <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

After creating a VM that is based on an LVM volume, you'll need to shut it down to create a reliable snapshot. Once it is created, a snapshot can be created using a command similar to lvcreate -s -n lvvm0-snap -L 2G /dev/centos/lvvm0. The result is an LVM snapshot volume with a size of 2 GB.

While creating the snapshot, you should consider the size you want. A snapshot doesn't have to be of the same size as the original volume. In fact, generally speaking, 10% of its original size is enough. If you want to be sure you won't run out of space, make it 20%. This allows the snapshot to store all changes that are normally expected during the course of a virtual classroom session. You can set it higher if you expect more changes. While the course participant is working on the VM, the snapshot will increase in size.

At the end of the course, you can easily revert to the snapshot, using the lvconvert command. If the name of the snapshot volume is lvvm0-snap and you want to merge it back into the original volume, use the following command: lvconvert --merge /dev/centos/lvvm-snap. This removes the snapshot and restores the original state of the volume. If you have created all snapshot volumes with a name that contains the word "snap," you could even automate this conversion for many snapshot volumes simultaneously, using a simple sell loop, like: fir i in $(lvs | awk '{ print $1 }' | grep -v snap | grep lv); do lvconvert --merge /dev/centos/$i; done. After running this command, all VMs will have reverted to their original state and be ready for the next course participants to use.

Preventing trouble

This approach works very well, but it's important to keep track of the snapshot size. A snapshot volume that has completely filled up has become damaged. If that happens in your environment, there's one solution only: Remove the logical volumes, and perform a new installation of the VMs.

Dig Deeper on Open source virtualization

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

very good description of the power of KVM, but what could you say about " Kernel Samepage Merging (KSM)" and the possible security issues linked to it?
In the linux kernel source, 3.18-rc2, there is a new parameter "merge_across_nodes" that could help or adress the problem.
What's your opinion, please ?
PS: is it true that Vmware is planning to not use "KSM" or will leave the "user" to choose ?