[libvirt] [PATCH 23/34] Convert audio devices over to -device syntax

Daniel P. Berrange berrange at redhat.com
Fri Jan 8 17:23:19 UTC 2010


The current syntax for audio devices is a horrible multiplexed
arg

    -soundhw sb16,pcspk,ac97

The new syntax is

    -device sb16,id=sound0

or

    -device AC97,id=sound1,addr=<PCI SLOT>

NB, pcspk still uses the old -soundhw syntax
---
 src/qemu/qemu_conf.c                               |   97 ++++++++++++++++----
 .../qemuxml2argv-sound-device.args                 |    1 +
 .../qemuxml2argvdata/qemuxml2argv-sound-device.xml |   26 +++++
 tests/qemuxml2argvtest.c                           |    1 +
 4 files changed, 106 insertions(+), 19 deletions(-)
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-sound-device.args
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-sound-device.xml

diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index fa16f8c..067fe42 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -2052,7 +2052,44 @@ error:
     return NULL;
 }
 
-/* this function outputs a -chardev command line option which describes only the
+
+static char *
+qemuBuildSoundDevStr(virDomainSoundDefPtr sound)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    const char *model = virDomainSoundModelTypeToString(sound->model);
+
+    if (!model) {
+        qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                         "%s", _("invalid sound model"));
+        goto error;
+    }
+
+    /* Hack for 2 wierdly unusal devices name in QEMU */
+    if (STREQ(model, "es1370"))
+        model = "ES1370";
+    else if (STREQ(model, "ac97"))
+        model = "AC97";
+
+    virBufferVSprintf(&buf, "%s", model);
+    virBufferVSprintf(&buf, ",id=%s", sound->info.alias);
+    if (qemuBuildDeviceAddressStr(&buf, &sound->info) < 0)
+        goto error;
+
+    if (virBufferError(&buf)) {
+        virReportOOMError(NULL);
+        goto error;
+    }
+
+    return virBufferContentAndReset(&buf);
+
+error:
+    virBufferFreeAndReset(&buf);
+    return NULL;
+}
+
+
+/* This function outputs a -chardev command line option which describes only the
  * host side of the character device */
 static void qemudBuildCommandLineChrDevChardevStr(virDomainChrDefPtr dev,
                                                   virBufferPtr buf)
@@ -3121,27 +3158,49 @@ int qemudBuildCommandLine(virConnectPtr conn,
 
     /* Add sound hardware */
     if (def->nsounds) {
-        int size = 100;
-        char *modstr;
-        if (VIR_ALLOC_N(modstr, size+1) < 0)
-            goto no_memory;
+        if (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE) {
+            for (i = 0 ; i < def->nsounds ; i++) {
+                virDomainSoundDefPtr sound = def->sounds[i];
+                char *str = NULL;
+
+                /* Sadly pcspk device doesn't use -device syntax. Fortunately
+                 * we don't need to set any PCI address on it, so we don't
+                 * mind too much */
+                if (sound->model == VIR_DOMAIN_SOUND_MODEL_PCSPK) {
+                    ADD_ARG_LIT("-soundhw");
+                    ADD_ARG_LIT("pcspk");
+                } else {
+                    ADD_ARG_LIT("-device");
 
-        for (i = 0 ; i < def->nsounds && size > 0 ; i++) {
-            virDomainSoundDefPtr sound = def->sounds[i];
-            const char *model = virDomainSoundModelTypeToString(sound->model);
-            if (!model) {
-                VIR_FREE(modstr);
-                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
-                                 "%s", _("invalid sound model"));
-                goto error;
+                    if (!(str = qemuBuildSoundDevStr(sound)))
+                        goto error;
+
+                    ADD_ARG(str);
+                }
+            }
+        } else {
+            int size = 100;
+            char *modstr;
+            if (VIR_ALLOC_N(modstr, size+1) < 0)
+                goto no_memory;
+
+            for (i = 0 ; i < def->nsounds && size > 0 ; i++) {
+                virDomainSoundDefPtr sound = def->sounds[i];
+                const char *model = virDomainSoundModelTypeToString(sound->model);
+                if (!model) {
+                    VIR_FREE(modstr);
+                    qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                                     "%s", _("invalid sound model"));
+                    goto error;
+                }
+                strncat(modstr, model, size);
+                size -= strlen(model);
+                if (i < (def->nsounds - 1))
+                    strncat(modstr, ",", size--);
             }
-            strncat(modstr, model, size);
-            size -= strlen(model);
-            if (i < (def->nsounds - 1))
-               strncat(modstr, ",", size--);
+            ADD_ARG_LIT("-soundhw");
+            ADD_ARG(modstr);
         }
-        ADD_ARG_LIT("-soundhw");
-        ADD_ARG(modstr);
     }
 
     /* Add watchdog hardware */
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-sound-device.args b/tests/qemuxml2argvdata/qemuxml2argv-sound-device.args
new file mode 100644
index 0000000..31ac0ee
--- /dev/null
+++ b/tests/qemuxml2argvdata/qemuxml2argv-sound-device.args
@@ -0,0 +1 @@
+LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test /usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic -nodefaults -monitor unix:/tmp/test-monitor,server,nowait -no-acpi -boot c -hda /dev/HostVG/QEMUGuest1 -usb -soundhw pcspk -device ES1370,id=sound1 -device sb16,id=sound2 -device AC97,id=sound3
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-sound-device.xml b/tests/qemuxml2argvdata/qemuxml2argv-sound-device.xml
new file mode 100644
index 0000000..8c33e6c
--- /dev/null
+++ b/tests/qemuxml2argvdata/qemuxml2argv-sound-device.xml
@@ -0,0 +1,26 @@
+<domain type='qemu'>
+  <name>QEMUGuest1</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory>219200</memory>
+  <currentMemory>219200</currentMemory>
+  <vcpu>1</vcpu>
+  <os>
+    <type arch='i686' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <emulator>/usr/bin/qemu</emulator>
+    <disk type='block' device='disk'>
+      <source dev='/dev/HostVG/QEMUGuest1'/>
+      <target dev='hda' bus='ide'/>
+    </disk>
+    <sound model='pcspk'/>
+    <sound model='es1370'/>
+    <sound model='sb16'/>
+    <sound model='ac97'/>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 6bfc217..55e7d58 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -293,6 +293,7 @@ mymain(int argc, char **argv)
     DO_TEST("watchdog", 0);
     DO_TEST("watchdog-device", QEMUD_CMD_FLAG_DEVICE);
     DO_TEST("sound", 0);
+    DO_TEST("sound-device", QEMUD_CMD_FLAG_DEVICE);
 
     DO_TEST("hostdev-usb-product", 0);
     DO_TEST("hostdev-usb-address", 0);
-- 
1.6.5.2




More information about the libvir-list mailing list