Add test for qemu broken -machine option (RHBZ#748266).
[libguestfs.git] / src / launch.c
index 234f42e..6c7a833 100644 (file)
@@ -105,9 +105,9 @@ free_regexps (void)
 }
 #endif
 
-/* Add a string to the current command line. */
+/* Functions to add a string to the current command line. */
 static void
-incr_cmdline_size (guestfs_h *g)
+alloc_cmdline (guestfs_h *g)
 {
   if (g->cmdline == NULL) {
     /* g->cmdline[0] is reserved for argv[0], set in guestfs_launch. */
@@ -115,7 +115,12 @@ incr_cmdline_size (guestfs_h *g)
     g->cmdline = safe_malloc (g, sizeof (char *));
     g->cmdline[0] = NULL;
   }
+}
 
+static void
+incr_cmdline_size (guestfs_h *g)
+{
+  alloc_cmdline (g);
   g->cmdline_size++;
   g->cmdline = safe_realloc (g, g->cmdline, sizeof (char *) * g->cmdline_size);
 }
@@ -160,11 +165,7 @@ guestfs__debug_cmdline (guestfs_h *g)
   size_t i;
   char **r;
 
-  if (g->cmdline == NULL) {
-    r = safe_malloc (g, sizeof (char *) * 1);
-    r[0] = NULL;
-    return r;
-  }
+  alloc_cmdline (g);
 
   r = safe_malloc (g, sizeof (char *) * (g->cmdline_size + 1));
   r[0] = safe_strdup (g, g->qemu); /* g->cmdline[0] is always NULL */
@@ -512,6 +513,12 @@ launch_appliance (guestfs_h *g)
     /* Set up the full command line.  Do this in the subprocess so we
      * don't need to worry about cleaning up.
      */
+
+    /* Set g->cmdline[0] to the name of the qemu process.  However
+     * it is possible that no g->cmdline has been allocated yet so
+     * we must do that first.
+     */
+    alloc_cmdline (g);
     g->cmdline[0] = g->qemu;
 
     if (qemu_supports (g, "-nodefconfig"))
@@ -523,7 +530,19 @@ launch_appliance (guestfs_h *g)
      */
     if (qemu_supports (g, "-machine")) {
       add_cmdline (g, "-machine");
+#if QEMU_MACHINE_TYPE_IS_BROKEN
+      /* Workaround for qemu 0.15: We have to add the '[type=]pc'
+       * since there is no default.  This is not a permanent solution
+       * because this only works on PC-like hardware.  Other platforms
+       * like ppc would need a different machine type.
+       *
+       * This bug is fixed in qemu commit 2645c6dcaf6ea2a51a, and was
+       * not a problem in qemu < 0.15.
+       */
+      add_cmdline (g, "pc,accel=kvm:tcg");
+#else
       add_cmdline (g, "accel=kvm:tcg");
+#endif
     } else {
       /* qemu sometimes needs this option to enable hardware
        * virtualization, but some versions of 'qemu-kvm' will use KVM
@@ -607,6 +626,7 @@ launch_appliance (guestfs_h *g)
     "console=ttyS0 "   /* serial console */                            \
     "udevtimeout=300 " /* good for very slow systems (RHBZ#480319) */  \
     "noapic "          /* workaround for RHBZ#502058 - ok if not SMP */ \
+    "no_timer_check "  /* fix for RHBZ#502058 */                        \
     "acpi=off "        /* we don't need ACPI, turn it off */           \
     "printk.time=1 "   /* display timestamp before kernel messages */   \
     "cgroup_disable=memory " /* saves us about 5 MB of RAM */
@@ -1058,6 +1078,7 @@ print_qemu_command_line (guestfs_h *g, char **argv)
   }
 }
 
+static int test_qemu_cmd (guestfs_h *g, const char *cmd, char **ret);
 static int read_all (guestfs_h *g, FILE *fp, char **ret);
 
 /* Test qemu binary (or wrapper) runs, and do 'qemu -help' and
@@ -1070,38 +1091,49 @@ test_qemu (guestfs_h *g)
   char cmd[1024];
   FILE *fp;
 
+  free (g->qemu_help);
+  g->qemu_help = NULL;
+  free (g->qemu_version);
+  g->qemu_version = NULL;
+
   snprintf (cmd, sizeof cmd, "LC_ALL=C '%s' -nographic -help", g->qemu);
 
-  fp = popen (cmd, "r");
   /* qemu -help should always work (qemu -version OTOH wasn't
    * supported by qemu 0.9).  If this command doesn't work then it
    * probably indicates that the qemu binary is missing.
    */
-  if (!fp) {
-    /* XXX This error is never printed, even if the qemu binary
-     * doesn't exist.  Why?
-     */
-  error:
-    perrorf (g, _("%s: command failed: If qemu is located on a non-standard path, try setting the LIBGUESTFS_QEMU environment variable."), cmd);
+  if (test_qemu_cmd (g, cmd, &g->qemu_help) == -1) {
+    error (g, _("command failed: %s\n\nIf qemu is located on a non-standard path, try setting the LIBGUESTFS_QEMU\nenvironment variable.  There may also be errors printed above."),
+           cmd);
     return -1;
   }
 
-  if (read_all (g, fp, &g->qemu_help) == -1)
-    goto error;
-
-  if (pclose (fp) == -1)
-    goto error;
-
   snprintf (cmd, sizeof cmd, "LC_ALL=C '%s' -nographic -version 2>/dev/null",
             g->qemu);
 
+  /* Intentionally ignore errors from qemu -version. */
+  ignore_value (test_qemu_cmd (g, cmd, &g->qemu_version));
+
+  return 0;
+}
+
+static int
+test_qemu_cmd (guestfs_h *g, const char *cmd, char **ret)
+{
+  FILE *fp;
+
   fp = popen (cmd, "r");
-  if (fp) {
-    /* Intentionally ignore errors. */
-    read_all (g, fp, &g->qemu_version);
+  if (fp == NULL)
+    return -1;
+
+  if (read_all (g, fp, ret) == -1) {
     pclose (fp);
+    return -1;
   }
 
+  if (pclose (fp) != 0)
+    return -1;
+
   return 0;
 }