From 75ea7130586243c500d6e27d26942facf516226f Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Wed, 14 Aug 2013 15:08:04 +0100
Subject: [PATCH] launch: direct: Don't use -cpu host on TCG.
qemu -cpu \? documents this as:
host KVM processor with all supported host features (only available in KVM mode)
And indeed if you try it with TCG you'll get this error:
Unable to find CPU definition: host
This fixes commit 038ed0a08eaed33e62a27c9f91780a25de0bc08c.
(cherry picked from commit c53b459fdd7fc340f48a76496b7e7e18256a2d64)
---
src/launch-direct.c | 39 ++++++++++++++++++++++++++-------------
1 file changed, 26 insertions(+), 13 deletions(-)
diff --git a/src/launch-direct.c b/src/launch-direct.c
index 90d0df9..299a3d9 100644
--- a/src/launch-direct.c
+++ b/src/launch-direct.c
@@ -20,6 +20,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
@@ -258,6 +259,7 @@ launch_direct (guestfs_h *g, const char *arg)
char buf[256];
int virtio_scsi = qemu_supports_virtio_scsi (g);
struct qemu_param *qp;
+ bool has_kvm;
/* Set up the full command line. Do this in the subprocess so we
* don't need to worry about cleaning up.
@@ -290,6 +292,14 @@ launch_direct (guestfs_h *g, const char *arg)
add_cmdline (g, "-nographic");
+ /* Try to guess if KVM is available. We are just checking that
+ * /dev/kvm is openable. That's not reliable, since /dev/kvm
+ * might be openable by qemu but not by us (think: SELinux) in
+ * which case the user would not get hardware virtualization,
+ * although at least shouldn't fail.
+ */
+ has_kvm = is_openable (g, "/dev/kvm", O_RDWR|O_CLOEXEC);
+
/* The qemu -machine option (added 2010-12) is a bit more sane
* since it falls back through various different acceleration
* modes, so try that first (thanks Markus Armbruster).
@@ -301,23 +311,26 @@ launch_direct (guestfs_h *g, const char *arg)
/* qemu sometimes needs this option to enable hardware
* virtualization, but some versions of 'qemu-kvm' will use KVM
* regardless (even where this option appears in the help text).
- * It is rumoured that there are versions of qemu where supplying
- * this option when hardware virtualization is not available will
- * cause qemu to fail, so we we have to check at least that
- * /dev/kvm is openable. That's not reliable, since /dev/kvm
- * might be openable by qemu but not by us (think: SELinux) in
- * which case the user would not get hardware virtualization,
- * although at least shouldn't fail. A giant clusterfuck with the
- * qemu command line, again.
+ * It is rumoured that there are versions of qemu where
+ * supplying this option when hardware virtualization is not
+ * available will cause qemu to fail. A giant clusterfuck with
+ * the qemu command line, again.
*/
- if (qemu_supports (g, "-enable-kvm") &&
- is_openable (g, "/dev/kvm", O_RDWR|O_CLOEXEC))
+ if (qemu_supports (g, "-enable-kvm") && has_kvm)
add_cmdline (g, "-enable-kvm");
}
- /* Specify the host CPU for speed, and kvmclock for stability. */
- add_cmdline (g, "-cpu");
- add_cmdline (g, "host,+kvmclock");
+ /* -cpu host only works if KVM is available. */
+ if (has_kvm) {
+ /* Specify the host CPU for speed, and kvmclock for stability. */
+ add_cmdline (g, "-cpu");
+ add_cmdline (g, "host,+kvmclock");
+ } else {
+ /* Specify default CPU for speed, and kvmclock for stability. */
+ snprintf (buf, sizeof buf, "qemu%d,+kvmclock", SIZEOF_LONG*8);
+ add_cmdline (g, "-cpu");
+ add_cmdline (g, buf);
+ }
if (g->smp > 1) {
snprintf (buf, sizeof buf, "%d", g->smp);
--
1.8.3.1