mathew.moon@vipaar.com
2014-Oct-22 21:39 UTC
[libvirt-users] Changing 'source file' on running VM.
So I notice that some operations by libvirt such as creating a snapshot actually change the source of the active disk image for a domain without requiring a restart of the vm. How can this be achieved manually? There are instances where I am manipulating files with qemu-img or virsh commands and afterward need to change what file the vm is using as its disk. One example is when deleting a snapshot. Please see example below: ==============================================================root >#virsh snapshot-create-as --domain $DOMAIN $SNAP $DESC --disk-only --diskspec vda,snapshot=external,file=/var/lib/libvirt/images/${SNAPFILE} --atomic Domain snapshot ballyclient-1.snap1 created root >#virsh dumpxml $DOMAIN | grep 'source file' root >#virsh snapshot-delete --metadata $DOMAIN $SNAP Domain snapshot ballyclient-1.snap1 deleted root >#find /var/lib/libvirt/images/ -type f -name $SNAPFILE -delete root >#virsh dumpxml $DOMAIN |grep 'source file' root >#virsh dumpxml $DOMAIN | sed 's/snap1\.//g' >/tmp/$DOMAIN.xml && virsh define /tmp/$DOMAIN.xml && virsh destroy $DOMAIN && virsh start $DOMAIN && virsh dumpxml $DOMAIN | grep 'source file' Domain ballyclient-1 defined from /tmp/ballyclient-1.xml Domain ballyclient-1 destroyed Domain ballyclient-1 started root ># ============================================================== So how could I have told libvirt to go back to using the original base image as 'source file' after deleting the snapshot? I am guessing this is something trivial but `man virsh` has not helped me on this one. Mathew Moon Systems Administrator Cell: (205) 514-5058
Eric Blake
2014-Nov-18 16:27 UTC
Re: [libvirt-users] Changing 'source file' on running VM.
On 10/22/2014 03:39 PM, mathew.moon@vipaar.com wrote:> So I notice that some operations by libvirt such as creating a snapshot actually > change the source of the active disk image for a domain without requiring a > restart of the vm. How can this be achieved manually? There are instances where > I am manipulating files with qemu-img or virsh commands and afterward need to > change what file the vm is using as its disk. One example is when deleting a > snapshot. Please see example below:Qemu does not yet provide a way to randomly change out a disk of a running guest; it can only change the active disk on specific operations: active block commit and snapshot creation. If you broaden the scope and allow migration, then you can change the disk any time you migrate the guest between machines, or when you save the guest state to file then restore the guest. Saving to file (which really is just a special case of migration that doesn't need a second host) has some mandatory downtime. But depending on the modification you want to make, it may already be supported. Let's see...> > ==============================================================> root >#virsh snapshot-create-as --domain $DOMAIN $SNAP $DESC --disk-only > --diskspec vda,snapshot=external,file=/var/lib/libvirt/images/${SNAPFILE} --atomic > Domain snapshot ballyclient-1.snap1 createdso this changes your chain from the old: /var/lib/libvirt/images/src to: /var/lib/libvirt/images/src <- /var/lib/libvirt/images/snapfile where src is now read-only, so you can copy it off to some other location as a backup.> root >#virsh dumpxml $DOMAIN | grep 'source file' > <source file='/var/lib/libvirt/images/ballyclient-1.snap1.qcow2'/> > root >#virsh snapshot-delete --metadata $DOMAIN $SNAP > Domain snapshot ballyclient-1.snap1 deletedThis forgets that a snapshot was taken (you can also skip this step by adding --no-metadata to the earlier snapshot-create-as command, if you know you are just using it for the side effect of creating a temporary overlay file).> > root >#find /var/lib/libvirt/images/ -type f -name $SNAPFILE -deleteOuch. This is very dangerous. You are unlink'ing a file that is in use by qemu. If your guest were to stop operation, you'd be corrupting the guest's data. You really don't want to do this until AFTER you have committed the data from that image into your original file.> root >#virsh dumpxml $DOMAIN |grep 'source file' > <source file='/var/lib/libvirt/images/ballyclient-1.snap1.qcow2'/> > root >#virsh dumpxml $DOMAIN | sed 's/snap1\.//g' >/tmp/$DOMAIN.xml && virsh > define /tmp/$DOMAIN.xml && virsh destroy $DOMAIN && virsh start $DOMAIN && > virsh dumpxml $DOMAIN | grep 'source file'That changes what the guest will boot with on the next boot, but unless you ALSO commit the overlay back into the base image, it means that your next guest boot will appear to jump backwards in time (it will lose all modifications made in the guest since when you created the snapshot). What you REALLY want to do is an active commit.> Domain ballyclient-1 defined from /tmp/ballyclient-1.xml > Domain ballyclient-1 destroyed > Domain ballyclient-1 started > <source file='/var/lib/libvirt/images/ballyclient-1.qcow2'/> > root ># > ==============================================================> > So how could I have told libvirt to go back to using the original base image as > 'source file' after deleting the snapshot? I am guessing this is something > trivial but `man virsh` has not helped me on this one.virsh blockcommit $DOMAIN /var/lib/libvirt/images/$SNAPSHOT \ --shallow --active --verbose --pivot does what you want; after you have done the active commit, then dumpxml will be automatically updated to point back to the original image, and you can then safely delete the overlay that is no longer in use. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Maybe Matching Threads
- Re: Help with understanding and solving snapshot problem
- Revert to internal snapshot - XML error: Non-empty feature list specified without CPU model
- Help with understanding and solving snapshot problem
- After a 'virsh blockpull', 'virsh snapshot-list --tree' o/p does not reflect reality
- Re: virsh can't support VM offline blockcommit