Currently, we are using a combination of Puppet and Foreman to deploy virtual machines to our environment. Allowing Foreman to deploy the VM guest and then having PXE install the OS is awesome, a great hands-off system after the initial setup and configuration is done.
One of the limitations of Foreman, however, is it only deploys the guest as an image file in RAW format or QCOW2 format. I wanted a solution to backup the guest without pausing or stopping it, while at the same time not requiring any changes with how Foreman deploys our VMs. I solved this by using the ‘qemu-img’ command.
Just a heads up, we no longer use this tactic because it’s against best practices. Execute at your own risk.
1. Create a snapshot of the running VM, named “date”. This will not pause or shutdown the guest.
qemu-img snapshot -c “date” /var/lib/libvirt/images/beta-test-disk1
2. Create a usable disk file from the snapshot.
qemu-img convert -f qcow2 -O qcow2 -s “date” /var/lib/libvirt/images/beta-test-disk1 /var/lib/libvirt/images/beta-test-disk1-“date”
3. Remove the snapshot.
qemu-img snapshot -d “date” /var/lib/libvirt/images/beta-test-disk1
Now you have file/var/lib/libvirt/images/beta-test-disk1-“date” to copy to a new location or archive. Personally, I take the file and bunzip2 it to conserve space.
A few items to consider are that this works on a per disk level, so if your guest has multiple disks, you’ll need to snapshot per disk. I use /usr/bin/virsh domblklist “vm name” to help determine the disks attached and their actual file name. Secondly, if you’re running a database, index, or something similar, you’ll need to make sure to flush any info out of RAM to the disk.
Overall, this process is easy to use, very flexible, has almost no downtime with the part of the guest.