This horrible piece of spaghetti code is copy-past(ae)d in the SCSI and USB disk hotplug code with minimal changes. Extract it for further reuse. --- src/qemu/qemu_hotplug.c | 50 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-)
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index f00d4f546..f251af904 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -345,21 +345,24 @@ qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver, } +/** + * qemuDomainAttachDiskGeneric: + * + * Attaches disk to a VM. This function aggregates common code for all bus types. + * In cases when the VM crashed while adding the disk, -2 is returned. */ static int -qemuDomainAttachVirtioDiskDevice(virConnectPtr conn, - virQEMUDriverPtr driver, - virDomainObjPtr vm, - virDomainDiskDefPtr disk) +qemuDomainAttachDiskGeneric(virConnectPtr conn, + virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainDiskDefPtr disk) { int ret = -1; int rv; qemuDomainObjPrivatePtr priv = vm->privateData; - virDomainDeviceDef dev = { VIR_DOMAIN_DEVICE_DISK, { .disk = disk } }; virErrorPtr orig_err; char *devstr = NULL; char *drivestr = NULL; char *drivealias = NULL; - bool releaseaddr = false; bool driveAdded = false; bool secobjAdded = false; bool encobjAdded = false; @@ -373,9 +376,6 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn, if (qemuDomainPrepareDisk(driver, vm, disk, NULL, false) < 0) goto cleanup; - if (qemuDomainEnsureVirtioAddress(&releaseaddr, vm, &dev, disk->dst) < 0) - goto error; - if (qemuAssignDeviceDiskAlias(vm->def, disk, priv->qemuCaps) < 0) goto error; @@ -441,7 +441,7 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn, goto exit_monitor; if (qemuDomainObjExitMonitor(driver, vm) < 0) { - releaseaddr = false; + ret = -2; goto error; } @@ -471,22 +471,42 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn, if (encobjAdded) ignore_value(qemuMonitorDelObject(priv->mon, encinfo->s.aes.alias)); if (qemuDomainObjExitMonitor(driver, vm) < 0) - releaseaddr = false; + ret = -2; virErrorRestore(&orig_err); virDomainAuditDisk(vm, NULL, disk->src, "attach", false); error: qemuDomainDelDiskSrcTLSObject(driver, vm, disk->src); - - if (releaseaddr) - qemuDomainReleaseDeviceAddress(vm, &disk->info, disk->dst); - ignore_value(qemuDomainPrepareDisk(driver, vm, disk, NULL, true)); goto cleanup; } +static int +qemuDomainAttachVirtioDiskDevice(virConnectPtr conn, + virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainDiskDefPtr disk) +{ + virDomainDeviceDef dev = { VIR_DOMAIN_DEVICE_DISK, { .disk = disk } }; + bool releaseaddr = false; + int rv; + + if (qemuDomainEnsureVirtioAddress(&releaseaddr, vm, &dev, disk->dst) < 0) + return -1; + + if ((rv = qemuDomainAttachDiskGeneric(conn, driver, vm, disk)) < 0) { + if (rv == -1 && releaseaddr) + qemuDomainReleaseDeviceAddress(vm, &disk->info, disk->dst); + + return -1; + } + + return 0; +} + + int qemuDomainAttachControllerDevice(virQEMUDriverPtr driver, virDomainObjPtr vm, virDomainControllerDefPtr controller) -- 2.14.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list