qemu: hotplug: consolidate media change code paths

Use qemuDomainAttachDeviceDiskLive to change the media in
qemuDomainChangeDiskLive as the former function already does all the
necessary steps to prepare the new medium.

This also allows us to turn qemuDomainChangeEjectableMedia static.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2018-08-13 14:05:12 +02:00
parent 8b5c8ecc0b
commit 663b1d55de
4 changed files with 20 additions and 29 deletions

View File

@ -7599,7 +7599,7 @@ qemuDomainAttachDeviceLive(virDomainObjPtr vm,
switch ((virDomainDeviceType)dev->type) {
case VIR_DOMAIN_DEVICE_DISK:
qemuDomainObjCheckDiskTaint(driver, vm, dev->data.disk, NULL);
ret = qemuDomainAttachDeviceDiskLive(driver, vm, dev);
ret = qemuDomainAttachDeviceDiskLive(driver, vm, dev, false);
if (!ret) {
alias = dev->data.disk->info.alias;
dev->data.disk = NULL;
@ -7850,12 +7850,6 @@ qemuDomainChangeDiskLive(virDomainObjPtr vm,
virDomainDeviceDef oldDev = { .type = dev->type };
int ret = -1;
if (virDomainDiskTranslateSourcePool(disk) < 0)
goto cleanup;
if (qemuDomainDetermineDiskChain(driver, vm, disk, true) < 0)
goto cleanup;
if (!(orig_disk = virDomainDiskFindByBusAndDst(vm->def,
disk->bus, disk->dst))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
@ -7884,18 +7878,8 @@ qemuDomainChangeDiskLive(virDomainObjPtr vm,
goto cleanup;
}
/* Add the new disk src into shared disk hash table */
if (qemuAddSharedDevice(driver, dev, vm->def->name) < 0)
if (qemuDomainAttachDeviceDiskLive(driver, vm, dev, force) < 0)
goto cleanup;
if (qemuDomainChangeEjectableMedia(driver, vm, orig_disk,
dev->data.disk->src, force) < 0) {
ignore_value(qemuRemoveSharedDisk(driver, dev->data.disk,
vm->def->name));
goto cleanup;
}
dev->data.disk->src = NULL;
}
orig_disk->startupPolicy = dev->data.disk->startupPolicy;

View File

@ -586,7 +586,7 @@ qemuHotplugDiskSourceRemove(qemuMonitorPtr mon,
*
* Returns 0 on success, -1 on error and reports libvirt error
*/
int
static int
qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainDiskDefPtr disk,
@ -909,10 +909,22 @@ qemuDomainAttachUSBMassStorageDevice(virQEMUDriverPtr driver,
}
/**
* qemuDomainAttachDeviceDiskLive:
* @driver: qemu driver struct
* @vm: domain object
* @dev: device to attach (expected type is DISK)
* @forceMediaChange: Forcibly open the drive if changing media
*
* Attach a new disk or in case of cdroms/floppies change the media in the drive.
* This function handles all the necessary steps to attach a new storage source
* to the VM. If @forceMediaChange is true the drive is opened forcibly.
*/
int
qemuDomainAttachDeviceDiskLive(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainDeviceDefPtr dev)
virDomainDeviceDefPtr dev,
bool forceMediaChange)
{
size_t i;
virDomainDiskDefPtr disk = dev->data.disk;
@ -946,7 +958,7 @@ qemuDomainAttachDeviceDiskLive(virQEMUDriverPtr driver,
}
if (qemuDomainChangeEjectableMedia(driver, vm, orig_disk,
disk->src, false) < 0)
disk->src, forceMediaChange) < 0)
goto cleanup;
disk->src = NULL;

View File

@ -28,12 +28,6 @@
# include "qemu_domain.h"
# include "domain_conf.h"
int qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainDiskDefPtr disk,
virStorageSourcePtr newsrc,
bool force);
void qemuDomainDelTLSObjects(virQEMUDriverPtr driver,
virDomainObjPtr vm,
qemuDomainAsyncJob asyncJob,
@ -60,7 +54,8 @@ int qemuDomainAttachControllerDevice(virQEMUDriverPtr driver,
virDomainControllerDefPtr controller);
int qemuDomainAttachDeviceDiskLive(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainDeviceDefPtr dev);
virDomainDeviceDefPtr dev,
bool forceMediaChange);
int qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainNetDefPtr net);

View File

@ -120,7 +120,7 @@ testQemuHotplugAttach(virDomainObjPtr vm,
/* conn in only used for storage pool and secrets lookup so as long
* as we don't use any of them, passing NULL should be safe
*/
ret = qemuDomainAttachDeviceDiskLive(&driver, vm, dev);
ret = qemuDomainAttachDeviceDiskLive(&driver, vm, dev, false);
break;
case VIR_DOMAIN_DEVICE_CHR:
ret = qemuDomainAttachChrDevice(&driver, vm, dev->data.chr);