[PATCH 1/3] qemuValidateDomainDeviceDefDiskFrontend: Refactor validation of <disk type='lun'>

Peter Krempa posted 3 patches 4 months ago
[PATCH 1/3] qemuValidateDomainDeviceDefDiskFrontend: Refactor validation of <disk type='lun'>
Posted by Peter Krempa 4 months ago
Use a switch statement for checks based on the disk bus.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/qemu/qemu_validate.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/qemu/qemu_validate.c b/src/qemu/qemu_validate.c
index 95af93d606..fe190cea36 100644
--- a/src/qemu/qemu_validate.c
+++ b/src/qemu/qemu_validate.c
@@ -2901,23 +2901,34 @@ qemuValidateDomainDeviceDefDiskFrontend(const virDomainDiskDef *disk,
     }

     if (disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
-        /* make sure that both the bus supports type='lun' (SG_IO). */
-        if (disk->bus != VIR_DOMAIN_DISK_BUS_VIRTIO &&
-            disk->bus != VIR_DOMAIN_DISK_BUS_SCSI) {
+
+        switch (disk->bus) {
+        case VIR_DOMAIN_DISK_BUS_SCSI:
+            if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_BLOCK)) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                               _("This QEMU doesn't support scsi-block for lun passthrough"));
+                return -1;
+            }
+            break;
+
+        case VIR_DOMAIN_DISK_BUS_VIRTIO:
+            break;
+
+        case VIR_DOMAIN_DISK_BUS_NONE:
+        case VIR_DOMAIN_DISK_BUS_IDE:
+        case VIR_DOMAIN_DISK_BUS_FDC:
+        case VIR_DOMAIN_DISK_BUS_XEN:
+        case VIR_DOMAIN_DISK_BUS_USB:
+        case VIR_DOMAIN_DISK_BUS_UML:
+        case VIR_DOMAIN_DISK_BUS_SATA:
+        case VIR_DOMAIN_DISK_BUS_SD:
+        case VIR_DOMAIN_DISK_BUS_LAST:
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("disk device='lun' is not supported for bus='%1$s'"),
                            virDomainDiskBusTypeToString(disk->bus));
             return -1;
         }

-        if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI &&
-            !virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_BLOCK)) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                           _("This QEMU doesn't support scsi-block for lun passthrough"));
-            return -1;
-        }
-
-
         if (disk->copy_on_read == VIR_TRISTATE_SWITCH_ON) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("copy_on_read is not compatible with 'lun' disk '%1$s'"),
-- 
2.45.2
Re: [PATCH 1/3] qemuValidateDomainDeviceDefDiskFrontend: Refactor validation of <disk type='lun'>
Posted by Ján Tomko 4 months ago
On a Tuesday in 2024, Peter Krempa wrote:
>Use a switch statement for checks based on the disk bus.
>
>Signed-off-by: Peter Krempa <pkrempa@redhat.com>
>---
> src/qemu/qemu_validate.c | 33 ++++++++++++++++++++++-----------
> 1 file changed, 22 insertions(+), 11 deletions(-)
>
>diff --git a/src/qemu/qemu_validate.c b/src/qemu/qemu_validate.c
>index 95af93d606..fe190cea36 100644
>--- a/src/qemu/qemu_validate.c
>+++ b/src/qemu/qemu_validate.c
>@@ -2901,23 +2901,34 @@ qemuValidateDomainDeviceDefDiskFrontend(const virDomainDiskDef *disk,
>     }
>
>     if (disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
>-        /* make sure that both the bus supports type='lun' (SG_IO). */
>-        if (disk->bus != VIR_DOMAIN_DISK_BUS_VIRTIO &&
>-            disk->bus != VIR_DOMAIN_DISK_BUS_SCSI) {
>+
>+        switch (disk->bus) {
>+        case VIR_DOMAIN_DISK_BUS_SCSI:
>+            if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_BLOCK)) {
>+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
>+                               _("This QEMU doesn't support scsi-block for lun passthrough"));
>+                return -1;
>+            }
>+            break;
>+
>+        case VIR_DOMAIN_DISK_BUS_VIRTIO:
>+            break;
>+
>+        case VIR_DOMAIN_DISK_BUS_NONE:
>+        case VIR_DOMAIN_DISK_BUS_IDE:
>+        case VIR_DOMAIN_DISK_BUS_FDC:
>+        case VIR_DOMAIN_DISK_BUS_XEN:
>+        case VIR_DOMAIN_DISK_BUS_USB:
>+        case VIR_DOMAIN_DISK_BUS_UML:
>+        case VIR_DOMAIN_DISK_BUS_SATA:
>+        case VIR_DOMAIN_DISK_BUS_SD:
>+        case VIR_DOMAIN_DISK_BUS_LAST:

Please use virReportEnumRangeError for the _LAST case.

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano
>             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
>                            _("disk device='lun' is not supported for bus='%1$s'"),
>                            virDomainDiskBusTypeToString(disk->bus));
>             return -1;
>         }
>