Snapshotting VMs before they go away


I need to be able to take a disk snapshot of a VM before it is shutdown or deleted. This is for security reasons.

What I have at the moment is a trigger which creates a deferred disk snapshot when the VM is created. This is not ideal as it means that we cannot snapshot VMs without killing them.

I have been experimenting with different combinations of State and LCMState to try and get a combination where I can run a disk snapshot but have been unable to so far

Does anyone have any ideas on this?



To create consistent snapshot on live VM you must instruct the kernel of the VM to flush its buffers and stop issuing new write requests to the block device. It is possible by using fsfreeze but to trigger it from the hypervisor you must have qemu-guest-agent configured and running inside the VM.
So the procedure looks something like:

  1. configure and run qemu-guest-agent on VM and configure libvirt to use the agent (libvirt Wiki)
  2. issue fsfreeze via libvirt
  3. snapshot the root disk (*)
  4. issue fsunfreeze via libvirt

(*) the possibility to create snapshots and how fast it is complete depends on the driver that is used for the block device

Probably there are other solutions but I am thinking for something like that for VM backups. IMHO your case is rather close to the backups too.

Best Regards,
Anton Todorov

That isnt a huge problem, we can ensure that the VMs are consistent.

We just need a way to trigger the snapshots at the right time