e4b5ba
From 31479ae566d887ce4b864ef84fee99305041aaf1 Mon Sep 17 00:00:00 2001
e4b5ba
From: Cole Robinson <crobinso@redhat.com>
e4b5ba
Date: Wed, 6 May 2015 18:59:46 -0400
e4b5ba
Subject: [PATCH] caps: Don't default to i686 of KVM on x86_64
e4b5ba
e4b5ba
My commit 747761a79 (v1.2.15 only) dropped this bit of logic when filling
e4b5ba
in a default arch in the XML:
e4b5ba
e4b5ba
-    /* First try to find one matching host arch */
e4b5ba
-    for (i = 0; i < caps->nguests; i++) {
e4b5ba
-        if (caps->guests[i]->ostype == ostype) {
e4b5ba
-            for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
e4b5ba
-                if (caps->guests[i]->arch.domains[j]->type == domain &&
e4b5ba
-                    caps->guests[i]->arch.id == caps->host.arch)
e4b5ba
-                    return caps->guests[i]->arch.id;
e4b5ba
-            }
e4b5ba
-        }
e4b5ba
-    }
e4b5ba
e4b5ba
That attempt to match host.arch is important, otherwise we end up
e4b5ba
defaulting to i686 on x86_64 host for KVM, which is not intended.
e4b5ba
Duplicate it in the centralized CapsLookup function.
e4b5ba
e4b5ba
Additionally add some testcases that would have caught this.
e4b5ba
e4b5ba
https://bugzilla.redhat.com/show_bug.cgi?id=1219191
e4b5ba
(cherry picked from commit 67b3c04b139d697e4aab01c0f676e356bbef5c52)
e4b5ba
---
e4b5ba
 src/conf/capabilities.c                            | 63 +++++++++++++++-------
e4b5ba
 .../qemuxml2argv-default-kvm-host-arch.args        |  4 ++
e4b5ba
 .../qemuxml2argv-default-kvm-host-arch.xml         | 11 ++++
e4b5ba
 tests/qemuxml2argvtest.c                           |  1 +
e4b5ba
 .../qemuxml2xmlout-default-kvm-host-arch.xml       | 21 ++++++++
e4b5ba
 tests/qemuxml2xmltest.c                            |  1 +
e4b5ba
 tests/testutilsqemu.c                              | 12 +++++
e4b5ba
 7 files changed, 94 insertions(+), 19 deletions(-)
e4b5ba
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-default-kvm-host-arch.args
e4b5ba
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-default-kvm-host-arch.xml
e4b5ba
 create mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-default-kvm-host-arch.xml
e4b5ba
e4b5ba
diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
e4b5ba
index 922741f..c43bfb3 100644
e4b5ba
--- a/src/conf/capabilities.c
e4b5ba
+++ b/src/conf/capabilities.c
e4b5ba
@@ -607,25 +607,13 @@ virCapsDomainDataCompare(virCapsGuestPtr guest,
e4b5ba
     return true;
e4b5ba
 }
e4b5ba
 
e4b5ba
-/**
e4b5ba
- * virCapabilitiesDomainDataLookup:
e4b5ba
- * @caps: capabilities to query
e4b5ba
- * @ostype: guest operating system type, of enum VIR_DOMAIN_OSTYPE
e4b5ba
- * @arch: Architecture to search for
e4b5ba
- * @domaintype: domain type to search for, of enum VIR_DOMAIN_VIRT
e4b5ba
- * @emulator: Emulator path to search for
e4b5ba
- * @machinetype: Machine type to search for
e4b5ba
- *
e4b5ba
- * Search capabilities for the passed values, and if found return
e4b5ba
- * virCapabilitiesDomainDataLookup filled in with the default values
e4b5ba
- */
e4b5ba
-virCapsDomainDataPtr
e4b5ba
-virCapabilitiesDomainDataLookup(virCapsPtr caps,
e4b5ba
-                                int ostype,
e4b5ba
-                                virArch arch,
e4b5ba
-                                int domaintype,
e4b5ba
-                                const char *emulator,
e4b5ba
-                                const char *machinetype)
e4b5ba
+static virCapsDomainDataPtr
e4b5ba
+virCapabilitiesDomainDataLookupInternal(virCapsPtr caps,
e4b5ba
+                                        int ostype,
e4b5ba
+                                        virArch arch,
e4b5ba
+                                        int domaintype,
e4b5ba
+                                        const char *emulator,
e4b5ba
+                                        const char *machinetype)
e4b5ba
 {
e4b5ba
     virCapsGuestPtr foundguest = NULL;
e4b5ba
     virCapsGuestDomainPtr founddomain = NULL;
e4b5ba
@@ -730,6 +718,43 @@ virCapabilitiesDomainDataLookup(virCapsPtr caps,
e4b5ba
     return ret;
e4b5ba
 }
e4b5ba
 
e4b5ba
+/**
e4b5ba
+ * virCapabilitiesDomainDataLookup:
e4b5ba
+ * @caps: capabilities to query
e4b5ba
+ * @ostype: guest operating system type, of enum VIR_DOMAIN_OSTYPE
e4b5ba
+ * @arch: Architecture to search for
e4b5ba
+ * @domaintype: domain type to search for, of enum VIR_DOMAIN_VIRT
e4b5ba
+ * @emulator: Emulator path to search for
e4b5ba
+ * @machinetype: Machine type to search for
e4b5ba
+ *
e4b5ba
+ * Search capabilities for the passed values, and if found return
e4b5ba
+ * virCapabilitiesDomainDataLookup filled in with the default values
e4b5ba
+ */
e4b5ba
+virCapsDomainDataPtr
e4b5ba
+virCapabilitiesDomainDataLookup(virCapsPtr caps,
e4b5ba
+                                int ostype,
e4b5ba
+                                virArch arch,
e4b5ba
+                                int domaintype,
e4b5ba
+                                const char *emulator,
e4b5ba
+                                const char *machinetype)
e4b5ba
+{
e4b5ba
+    virCapsDomainDataPtr ret;
e4b5ba
+
e4b5ba
+    if (arch == VIR_ARCH_NONE) {
e4b5ba
+        /* Prefer host arch if its available */
e4b5ba
+        ret = virCapabilitiesDomainDataLookupInternal(caps, ostype,
e4b5ba
+                                                      caps->host.arch,
e4b5ba
+                                                      domaintype,
e4b5ba
+                                                      emulator, machinetype);
e4b5ba
+        if (ret)
e4b5ba
+            return ret;
e4b5ba
+    }
e4b5ba
+
e4b5ba
+    return virCapabilitiesDomainDataLookupInternal(caps, ostype,
e4b5ba
+                                                   arch, domaintype,
e4b5ba
+                                                   emulator, machinetype);
e4b5ba
+}
e4b5ba
+
e4b5ba
 static int
e4b5ba
 virCapabilitiesFormatNUMATopology(virBufferPtr buf,
e4b5ba
                                   size_t ncells,
e4b5ba
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-default-kvm-host-arch.args b/tests/qemuxml2argvdata/qemuxml2argv-default-kvm-host-arch.args
e4b5ba
new file mode 100644
e4b5ba
index 0000000..102691f
e4b5ba
--- /dev/null
e4b5ba
+++ b/tests/qemuxml2argvdata/qemuxml2argv-default-kvm-host-arch.args
e4b5ba
@@ -0,0 +1,4 @@
e4b5ba
+LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test QEMU_AUDIO_DRV=none \
e4b5ba
+/usr/bin/kvm -S -machine pc,accel=kvm -m 4096 -smp 4 -nographic \
e4b5ba
+-monitor unix:/tmp/test-monitor,server,nowait -no-acpi -boot c -usb -net none \
e4b5ba
+-serial none -parallel none
e4b5ba
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-default-kvm-host-arch.xml b/tests/qemuxml2argvdata/qemuxml2argv-default-kvm-host-arch.xml
e4b5ba
new file mode 100644
e4b5ba
index 0000000..66dead0
e4b5ba
--- /dev/null
e4b5ba
+++ b/tests/qemuxml2argvdata/qemuxml2argv-default-kvm-host-arch.xml
e4b5ba
@@ -0,0 +1,11 @@
e4b5ba
+<domain type='kvm'>
e4b5ba
+  <name>kvm</name>
e4b5ba
+  <uuid>d091ea82-29e6-2e34-3005-f02617b36e87</uuid>
e4b5ba
+  <memory unit='KiB'>4194304</memory>
e4b5ba
+  <currentMemory unit='KiB'>4194304</currentMemory>
e4b5ba
+  <vcpu placement='static'>4</vcpu>
e4b5ba
+  <os>
e4b5ba
+    <type>hvm</type>
e4b5ba
+    <boot dev='hd'/>
e4b5ba
+  </os>
e4b5ba
+</domain>
e4b5ba
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
e4b5ba
index 4acaa11..df4c9f5 100644
e4b5ba
--- a/tests/qemuxml2argvtest.c
e4b5ba
+++ b/tests/qemuxml2argvtest.c
e4b5ba
@@ -595,6 +595,7 @@ mymain(void)
e4b5ba
     DO_TEST("machine-usb-opt", QEMU_CAPS_MACHINE_OPT,
e4b5ba
             QEMU_CAPS_MACHINE_USB_OPT);
e4b5ba
     DO_TEST("kvm", QEMU_CAPS_MACHINE_OPT);
e4b5ba
+    DO_TEST("default-kvm-host-arch", QEMU_CAPS_MACHINE_OPT);
e4b5ba
     DO_TEST("boot-cdrom", NONE);
e4b5ba
     DO_TEST("boot-network", NONE);
e4b5ba
     DO_TEST("boot-floppy", NONE);
e4b5ba
diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-default-kvm-host-arch.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-default-kvm-host-arch.xml
e4b5ba
new file mode 100644
e4b5ba
index 0000000..30fa66d
e4b5ba
--- /dev/null
e4b5ba
+++ b/tests/qemuxml2xmloutdata/qemuxml2xmlout-default-kvm-host-arch.xml
e4b5ba
@@ -0,0 +1,21 @@
e4b5ba
+<domain type='kvm'>
e4b5ba
+  <name>kvm</name>
e4b5ba
+  <uuid>d091ea82-29e6-2e34-3005-f02617b36e87</uuid>
e4b5ba
+  <memory unit='KiB'>4194304</memory>
e4b5ba
+  <currentMemory unit='KiB'>4194304</currentMemory>
e4b5ba
+  <vcpu placement='static'>4</vcpu>
e4b5ba
+  <os>
e4b5ba
+    <type arch='x86_64' machine='pc'>hvm</type>
e4b5ba
+    <boot dev='hd'/>
e4b5ba
+  </os>
e4b5ba
+  <clock offset='utc'/>
e4b5ba
+  <on_poweroff>destroy</on_poweroff>
e4b5ba
+  <on_reboot>restart</on_reboot>
e4b5ba
+  <on_crash>destroy</on_crash>
e4b5ba
+  <devices>
e4b5ba
+    <emulator>/usr/bin/kvm</emulator>
e4b5ba
+    <controller type='usb' index='0'/>
e4b5ba
+    <controller type='pci' index='0' model='pci-root'/>
e4b5ba
+    <memballoon model='virtio'/>
e4b5ba
+  </devices>
e4b5ba
+</domain>
e4b5ba
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
e4b5ba
index b611afd..a21a299 100644
e4b5ba
--- a/tests/qemuxml2xmltest.c
e4b5ba
+++ b/tests/qemuxml2xmltest.c
e4b5ba
@@ -346,6 +346,7 @@ mymain(void)
e4b5ba
     DO_TEST("minimal");
e4b5ba
     DO_TEST("machine-core-on");
e4b5ba
     DO_TEST("machine-core-off");
e4b5ba
+    DO_TEST_DIFFERENT("default-kvm-host-arch");
e4b5ba
     DO_TEST("boot-cdrom");
e4b5ba
     DO_TEST("boot-network");
e4b5ba
     DO_TEST("boot-floppy");
e4b5ba
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
e4b5ba
index 14743be..d067bca 100644
e4b5ba
--- a/tests/testutilsqemu.c
e4b5ba
+++ b/tests/testutilsqemu.c
e4b5ba
@@ -354,6 +354,18 @@ virCapsPtr testQemuCapsInit(void)
e4b5ba
                                       NULL) == NULL)
e4b5ba
         goto cleanup;
e4b5ba
 
e4b5ba
+    if ((machines = testQemuAllocMachines(&nmachines)) == NULL)
e4b5ba
+        goto cleanup;
e4b5ba
+
e4b5ba
+    if (virCapabilitiesAddGuestDomain(guest,
e4b5ba
+                                      VIR_DOMAIN_VIRT_KVM,
e4b5ba
+                                      "/usr/bin/qemu-kvm",
e4b5ba
+                                      NULL,
e4b5ba
+                                      nmachines,
e4b5ba
+                                      machines) == NULL)
e4b5ba
+        goto cleanup;
e4b5ba
+    machines = NULL;
e4b5ba
+
e4b5ba
     if ((machines = testQemuAllocNewerMachines(&nmachines)) == NULL)
e4b5ba
         goto cleanup;
e4b5ba
 
e4b5ba
-- 
e4b5ba
2.3.1
e4b5ba