qemu: hotplug: Adjust error path for attach virtio disk

Adjust error path logic to make it clearer how to undo the failed add.

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2016-04-11 09:16:54 -04:00
parent b0e002fcfd
commit 843ae77896

View File

@ -334,6 +334,7 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
{ {
int ret = -1; int ret = -1;
qemuDomainObjPrivatePtr priv = vm->privateData; qemuDomainObjPrivatePtr priv = vm->privateData;
virErrorPtr orig_err;
char *devstr = NULL; char *devstr = NULL;
char *drivestr = NULL; char *drivestr = NULL;
char *drivealias = NULL; char *drivealias = NULL;
@ -384,34 +385,24 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0)
goto error; goto error;
/* Attach the device - 2 step process */
qemuDomainObjEnterMonitor(driver, vm); qemuDomainObjEnterMonitor(driver, vm);
ret = qemuMonitorAddDrive(priv->mon, drivestr);
if (ret == 0) { if (qemuMonitorAddDrive(priv->mon, drivestr) < 0)
ret = qemuMonitorAddDevice(priv->mon, devstr); goto failadddrive;
if (ret < 0) {
virErrorPtr orig_err = virSaveLastError(); if (qemuMonitorAddDevice(priv->mon, devstr) < 0)
if (qemuMonitorDriveDel(priv->mon, drivealias) < 0) { goto failadddevice;
VIR_WARN("Unable to remove drive %s (%s) after failed "
"qemuMonitorAddDevice", drivealias, drivestr);
}
if (orig_err) {
virSetError(orig_err);
virFreeError(orig_err);
}
}
}
if (qemuDomainObjExitMonitor(driver, vm) < 0) { if (qemuDomainObjExitMonitor(driver, vm) < 0) {
releaseaddr = false; releaseaddr = false;
ret = -1; goto failexitmonitor;
goto error;
} }
virDomainAuditDisk(vm, NULL, disk->src, "attach", ret >= 0); virDomainAuditDisk(vm, NULL, disk->src, "attach", true);
if (ret < 0)
goto error;
virDomainDiskInsertPreAlloced(vm->def, disk); virDomainDiskInsertPreAlloced(vm->def, disk);
ret = 0;
cleanup: cleanup:
qemuDomainSecretDiskDestroy(disk); qemuDomainSecretDiskDestroy(disk);
@ -421,6 +412,24 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
virObjectUnref(cfg); virObjectUnref(cfg);
return ret; return ret;
failadddevice:
orig_err = virSaveLastError();
if (qemuMonitorDriveDel(priv->mon, drivealias) < 0) {
VIR_WARN("Unable to remove drive %s (%s) after failed "
"qemuMonitorAddDevice", drivealias, drivestr);
}
if (orig_err) {
virSetError(orig_err);
virFreeError(orig_err);
}
failadddrive:
if (qemuDomainObjExitMonitor(driver, vm) < 0)
releaseaddr = false;
failexitmonitor:
virDomainAuditDisk(vm, NULL, disk->src, "attach", false);
error: error:
if (releaseaddr) if (releaseaddr)
qemuDomainReleaseDeviceAddress(vm, &disk->info, src); qemuDomainReleaseDeviceAddress(vm, &disk->info, src);