[libvirt-users] Changing 'source file' on running VM.

Eric Blake eblake at redhat.com
Tue Nov 18 16:27:10 UTC 2014

On 10/22/2014 03:39 PM, mathew.moon at 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 created

so this changes your chain from the old:



/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 deleted

This 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 -delete

Ouch.  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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 539 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvirt-users/attachments/20141118/832940a9/attachment.sig>

More information about the libvirt-users mailing list