Checking if a device is a CDROM or floppy disk is a common verification that is made a few times in the code, and next patch is going to add one more. Let's put it into a helper to enhance readability and spare some lines of code. Signed-off-by: Daniel Henrique Barboza <danielhb413@xxxxxxxxx> --- src/conf/domain_conf.c | 8 ++++++++ src/conf/domain_conf.h | 2 ++ src/libvirt_private.syms | 1 + src/qemu/qemu_command.c | 3 +-- src/qemu/qemu_driver.c | 3 +-- src/qemu/qemu_hotplug.c | 6 ++---- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index bbde3788a6..b6be1e730d 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -16708,6 +16708,14 @@ void virDomainDiskInsertPreAlloced(virDomainDefPtr def, } +bool +virDomainDiskIsCdromOrFloppy(virDomainDiskDefPtr disk) +{ + return disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM || + disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY; +} + + virDomainDiskDefPtr virDomainDiskRemove(virDomainDefPtr def, size_t i) { diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 12eb71c197..2a1c6fd18b 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3579,4 +3579,6 @@ virDomainGraphicsGetRenderNode(const virDomainGraphicsDef *graphics); bool virDomainGraphicsNeedsAutoRenderNode(const virDomainGraphicsDef *graphics); +bool +virDomainDiskIsCdromOrFloppy(virDomainDiskDefPtr disk); #endif /* LIBVIRT_DOMAIN_CONF_H */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6e64e77839..817d9bc28d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -342,6 +342,7 @@ virDomainDiskInsert; virDomainDiskInsertPreAlloced; virDomainDiskIoTypeFromString; virDomainDiskIoTypeToString; +virDomainDiskIsCdromOrFloppy; virDomainDiskMirrorStateTypeFromString; virDomainDiskMirrorStateTypeToString; virDomainDiskModelTypeFromString; diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index a3e845a848..2c663c9009 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -1666,8 +1666,7 @@ qemuBuildDriveSourceStr(virDomainDiskDefPtr disk, /* nothing to format if the drive is empty */ if (!(source || srcprops) || - ((disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY || - disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) && + (virDomainDiskIsCdromOrFloppy(disk) && disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN)) { ret = 0; goto cleanup; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index fe2c586274..599c0f175f 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -8021,8 +8021,7 @@ qemuDomainChangeDiskLive(virDomainObjPtr vm, if (!virStorageSourceIsSameLocation(disk->src, orig_disk->src)) { /* Disk source can be changed only for removable devices */ - if (disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM && - disk->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY) { + if (!virDomainDiskIsCdromOrFloppy(disk)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("disk source can be changed only in removable " "drives")); diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index ef14b1977c..0ba1cc48c9 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -1230,8 +1230,7 @@ qemuDomainAttachDeviceDiskLiveInternal(virQEMUDriverPtr driver, virDomainDiskDefPtr disk = dev->data.disk; int ret = -1; - if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM || - disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) { + if (virDomainDiskIsCdromOrFloppy(disk)) { virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", _("cdrom/floppy device hotplug isn't supported")); return -1; @@ -1314,8 +1313,7 @@ qemuDomainAttachDeviceDiskLive(virQEMUDriverPtr driver, /* this API overloads media change semantics on disk hotplug * for devices supporting media changes */ - if ((disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM || - disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) && + if (virDomainDiskIsCdromOrFloppy(disk) && (orig_disk = virDomainDiskFindByBusAndDst(vm->def, disk->bus, disk->dst))) { if (qemuDomainChangeEjectableMedia(driver, vm, orig_disk, disk->src, false) < 0) -- 2.20.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list