On Sat, Oct 7, 2023 at 4:12 PM Minglei Liu <minglei.liu@smartx.com> wrote:
From: "minglei.liu" <minglei.liu@smartx.com>

In commit 1328a83, the 'media=cdrom' attribute was removed from -drive.
However, this attribute is still essential for usb cdrom and is still
supported in qemu 8.1.1. Therefore, we need to reintroduce this attribute
for usb cdrom.
From the results on qemu-kvm-8.1.1-1.fc39.x86_6, the parameter 'media' is not supported for -blockdev:
qemu-kvm -blockdev '{"driver":"file","filename":"/tmp/vdb","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap","media":"cdrom"}'
qemu-kvm: -blockdev {"driver":"file","filename":"/tmp/vdb","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap","media":"cdrom"}: Parameter 'media' is unexpected
---
 src/qemu/qemu_command.c                                    | 7 +++++++
 .../disk-cdrom-bus-other.x86_64-latest.args                | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 8a7b80719f..42f3f8f740 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1705,6 +1705,13 @@ qemuBuildDriveStr(virDomainDiskDef *disk)

     virBufferAsprintf(&opt, "if=sd,index=%d", virDiskNameToIndex(disk->dst));

+    /* While this is a frontend attribute, it only makes sense to be used when
+     * legacy -drive is used. In modern qemu the 'ide-cd' or 'scsi-cd' are used.
+     * currently only usb cdrom need this attribute */
+    if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM &&
+        disk->bus == VIR_DOMAIN_DISK_BUS_USB)
+        virBufferAddLit(&opt, ",media=cdrom");
+
     if (disk->src->readonly)
         virBufferAddLit(&opt, ",readonly=on");

diff --git a/tests/qemuxml2argvdata/disk-cdrom-bus-other.x86_64-latest.args b/tests/qemuxml2argvdata/disk-cdrom-bus-other.x86_64-latest.args
index de5fa083d8..38093423cf 100644
--- a/tests/qemuxml2argvdata/disk-cdrom-bus-other.x86_64-latest.args
+++ b/tests/qemuxml2argvdata/disk-cdrom-bus-other.x86_64-latest.args
@@ -27,7 +27,7 @@ XDG_CONFIG_HOME=/var/lib/libvirt/qemu/domain--1-QEMUGuest1/.config \
 -no-shutdown \
 -boot strict=on \
 -device '{"driver":"piix3-usb-uhci","id":"usb","bus":"pci.0","addr":"0x1.0x2"}' \
--blockdev '{"driver":"file","filename":"/root/boot.iso","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"}' \
+-blockdev '{"driver":"file","filename":"/root/boot.iso","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap","media":"cdrom"}' \
 -blockdev '{"node-name":"libvirt-2-format","read-only":true,"driver":"raw","file":"libvirt-2-storage"}' \
 -device '{"driver":"usb-storage","bus":"usb.0","port":"1","drive":"libvirt-2-format","id":"usb-disk0","removable":false}' \
 -device '{"driver":"usb-storage","bus":"usb.0","port":"2","id":"usb-disk1","removable":false}' \
--
2.41.0