[libvirt] [PATCH RFC 2/3] undefine: QEMU driver - remove storage associated with a domain while undefining

Daniel P. Berrange berrange at redhat.com
Thu Jul 21 17:03:46 UTC 2011


On Thu, Jul 21, 2011 at 05:12:15PM +0200, Peter Krempa wrote:
> qemu specific code to remove associated storage to domains being undefined
> with the virDomainUndefineFlags function.
> 
> Storage devices marked as shared aren't removed.
> 
> The code checks path of domain's storage devices in the storage pools and acts
> accordingly to flags passed to the undefine function.
> ---
>  src/qemu/qemu_driver.c |   32 +++++++++++++++++++++++++++++++-
>  1 files changed, 31 insertions(+), 1 deletions(-)
> 
> diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
> index cd65bce..03a079f 100644
> --- a/src/qemu/qemu_driver.c
> +++ b/src/qemu/qemu_driver.c
> @@ -4320,8 +4320,14 @@ qemuDomainUndefineFlags(virDomainPtr dom,
>      virDomainEventPtr event = NULL;
>      char *name = NULL;
>      int ret = -1;
> +    int i = 0;
> +    virDomainDiskDefPtr disk = NULL;
> +    virConnectPtr conn = NULL;
> +    virStorageVolPtr volume = NULL;
> 
> -    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE, -1);
> +    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
> +                  VIR_DOMAIN_UNDEFINE_DISK_FILE |
> +                  VIR_DOMAIN_UNDEFINE_DISK_BACKEND , -1);
> 
>      qemuDriverLock(driver);
>      vm = virDomainFindByUUID(&driver->domains, dom->uuid);
> @@ -4366,6 +4372,30 @@ qemuDomainUndefineFlags(virDomainPtr dom,
>          }
>      }
> 
> +    // remove disk images while undefinig the domain
> +    if ((flags & VIR_DOMAIN_UNDEFINE_DISK_FILE || flags & VIR_DOMAIN_UNDEFINE_DISK_BACKEND) &&
> +        ((conn = virDomainGetConnect(dom)) != NULL)) {
> +
> +        for (i = 0; i < vm->def->ndisks; i++) {
> +            disk = (vm->def->disks)[i];
> +
> +            // lookup the device in the storage pool
> +            if ((volume = virStorageVolLookupByPath(conn, disk->src)) == NULL) {
> +                if (disk->type == VIR_DOMAIN_DISK_TYPE_FILE && !disk->shared) {
> +                    if (unlink(disk->src) < 0)
> +                        VIR_WARN("Couldn't remove disk image file: '%s' while undefining domain '%s'", disk->src, vm->def->name);
> +                    else
> +                        VIR_INFO("Disk image '%s' removed.", disk->src);
> +                }
> +            } else {
> +                if (!disk->shared) {
> +                    if(virStorageVolDelete(volume, 0) == -1)
> +                        VIR_WARN("Couldn't remove storage volume with path '%s' while undefining domain '%s'", disk->src, vm->def->name);
> +                }
> +            }
> +        }
> +    }

The way this code deals with errors deleting disks is the pefect example
of why adding these flags to virDomainUndefine is a bad idea. Any serious
user of these APIs, now needs to use all the virStorageVol APIs anyway
to check if the disks for the VM really were deleted.

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list