Libvirt and qemu/kvm -snapshot option

kvm-virtualizationlibvirtqemusnapshot

Using libvirt, I want to use QEMU's '-snapshot' option. That way, when the machine shuts down, all of the disk changes that were made get destroyed.

Here is the manual for the -snapshot option:

Write to temporary files instead of disk image files. In this case,
the raw disk image you use is not written back. You can however
force the write back by pressing C-a s.

Similar abilities exist that almost do what I want (qcow2 overlay images, libvirt backing store), but the difference is that I want a fresh environment every time I boot up the VM. Is this possible?

Best Answer

The only way to add command-line switches that libvirt doesn't support yet is to create a wrapper script and change your VM's configuration to use it instead. For example,

# cat >/usr/local/bin/qemu-snapshot <<'END'
#!/bin/sh
exec /usr/bin/qemu "$@" -snapshot
END
# chmod +x /usr/local/bin/qemu-snapshot
# virsh -c qemu:///system edit my_vm
change
    <emulator>/usr/bin/qemu</emulator>
to
    <emulator>/usr/local/bin/qemu-snapshot</emulator>

(It might be /usr/bin/kvm or something like that for you.)