Copy-on-write with KVM

While KVM/QEMU snapshoting is still a dirty feature (quite slow), I love copy-on-write disc image format of the QEMU. It is actually the best way how to quiclky provision pre-installed systems for testing and development. The idea is easy:

  • Install a base operating system, configure it for your specific purposes.
  • Stop it. You wont use it anymore, only copy-on-write "children". You may start it to do some additional configuration when you forgot something for example.
  • Provision a new VM with qcow disk image format based on the one from above.
  • Do it multiple times. You can run more than one instances simultaneously.

The key command is qemu-img create which creates new image. You need to provide -f qcow2 parameter specifying the correct image format, and -b /path/to/the/base.img. New VM is provisioned in few seconds since it only needs to create very small image (few kilobytes) that will grow as it copies on write.

I have modified a cool script (karma goes to Jason Dobies, Shannon Hughes and all the others I forgot) for fast image provision. It is tuned for Fedora/Red Hat systems, but it's a piece to modify it for other distributions or systems. It works either with Windows. Simple session:

# snap-guest
Usage: BASE_IMAGE_NAME TARGET_IMAGE_NAME [MEMORY IN MB]

Available base images:
f12-x64-base
f13-x64-base
f14-x64-base
el6_0-x64-base
el6_1-x64-base

# snap-guest el6_0-x64-base testing_el6 1024
...
Provisioning guest

#


The script also connect to the newly created image and configure it (MAC address, hostname). This part needs to be tuned to support non-redhat distros. If you want to provision Windows system - you can - you need to find other way to do it.

It is also possible to modify the script to use LVM or physical volumes to get fastest possible speed for the base image. VM provisioning couldn't be easier.

Please note my script now lives on github.com - fetch the most recent version with even more features.

28 August 2011 | bash | tip | fedora | rhel | kvm
twitter.com linkedin.com
google.com/+ facebook.com
flickr.com youtube.com