KVM/QEMU Notes

This page not complete yet

I've been lately playing around with KVM and Virt-Manager (on Debian Sid).
Virt-Manager is much nicer than raw command-line kvm (or qemu) but there're some important nuisances. I'm writing some random tidbits
here both for my own reference and for anyone arriving here 🙂

Stuff here only deals with KVM as an hypervisor, not with Xen.

For the examples I'll be running an instance of kvm (instead of plain qemu) with pfSense and one with Fedora Core.
I'll be working over the Fedora virtual machine.

Note most tips here deal with ways to save space in dynamically-growable disk images, which I guess any serious deployment won't be using anyway for performance reasons.

COW images

Using a COW (Copy-On-Write) image linked to another image seems the best
option: once the system is installed, create a new image backed on the older, e.g.:

  • install e.g. in disk.qcow2
  • do any cleanup (see below)
  • optionally compress the image to get save space (requires the creation of a new image):
    $ qemu-img convert -c disk.qcow2 -O qcow2 new_disk.qcow2
    $ mv new_disk.qcow2 disk.qcow2
    • new_disk.qcow2 should be smaller than disk.qcow2, use "qemu-img info" or "du -hs" to compare)
  • create a COW "layer" backed on the previous image:
    $ mv disk.qcow2 base_disk.qcow2
    $ qemu-img create -c -b base_disk.qcow2 -f qcow2 delta_disk.qcow2
    $ ln -s delta_disk.qcow2 disk.qcow2
    • base_disk.qcow2 must be kept, it won't be changed anymore; any changes will be
      stored in delta_disk.qcow2, but it will only contain the changes —i.e. initially it won't take any
      space—.
    • a base image could be used by more than a VM?
    • the initial state can be restored by simply removing/recreating the delta image

Note that converting a delta image will create a new image with the base and delta image merged.

Note that compression only works on the initial write (when a compressed sector is modified, it is stored uncompressed) that's why re-creating an image potentially saves lots of space.

Base image/install preparation

OS-independent

  • Apply any vital patches (e.g. security fixes in Linux, SPs in XP, …)
  • Clean any cruft (browser caches, downloaded patches, trash cans, temporary dirs…)
  • Zero the disk (should the last step before re-creating), see below
  • Re-create the image once the install is complete to make it more compact.

Windows

  • Temporarily disable hibernation (removes the "C:\hiberfil.sys" file, same size as RAM).
    Might not really save space if not used yet?
  • Temporarily disable the paging/swap file
  • Defragmenting ↔ Might improve performance; will make the image grow unless it's zeroed after the defragmentation.
    With no swap nor hibernation file probably there won't be any unmovable files.
  • Possibly install the Win2K/XP/Vista virtio network drivers.
    Even if the current Virt-Manager can't support it yet, it should soon enough (libvirt already does so the virtio network model can be enabled by hand-editing the xml files).

Linux/UNIX family

  • Temporarily disable any swap files
  • Temporarily disable any swap partitions and zero them
  • Virtio devies are support from linux 2.6.25 onwards, upgrade if desired

Zeroing

Since any deleted files (or phisically moved, e.g. by defrag) will still be in the some place in the image; and since the image is going to be re-compressed, these removed files would make the compression less efficient.
A workaround is to fill the empty space in the disk with zeroes (or any other value). A stream of repeated values compresses better than some random files' contents :).
I use a very rude, yet efficient, approach: create a file with all-zeroes filling the disk (in some filesystems this would require multiple files due to file-size limit, but I try to avoid such systems).

In UNIX systems that's as easy as:

# dd if=/dev/zero of=bigfile
# sync
# rm bigfile
# sync

And shutting down the virtual machine

In Windows I don't know, there's certainly some program out there to do that. Colin D Bennett suggests SDelete in the comments below. I've written a simple program in Java you could use too.

KVM/QEMU Notes

Leave a Reply to Attributor Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.