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);
+ }
+ }
+ }
+ }
+
if (virDomainDeleteConfig(driver->configDir, driver->autostartDir, vm) < 0)
goto cleanup;
--
1.7.6