[Libvir] PATCH: Support <on_reboot>poweroff</on_reboot> in QEMU/KVM

Daniel P. Berrange berrange at redhat.com
Wed May 2 15:27:44 UTC 2007


Currently in the QEMU driver we don't support any of the customizable 
shutdown operation behaviour elements in the XML. ie

  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>

This was principally because we don't particularly have a good way to actually
implement some of them. Well, QEMU 0.9.0 added a -no-reboot flag which could
be used to force a 'reboot' inside the guest to behaviour like a shutdown.
So we can now support at least the <on_reboot> element with 'destroy' or
'restart' behaviours.

This turns out to be critical for provisioning guests, because the initial
install uses a different VM configs, from later runtime, eg boot from ISO,
vs boot from Disk.

The attached patch adds support for accepting <on_reboot> when defining new
domain configs.  When dumping XML we will also add <on_poweroff> and
<on_crash> elements to at least indicate what our behaviour is, though the
user can't customize them.

In doing this patch I had to change the way we read data from QEMU. We
were over the 4096 buffer size, and also in some cases we were getting a
short read which meant we lost the end of the help message. We now have
a buffer of 8k  (max QEMU help is 5k currently), and read in a loop until
EOF or 8k is hit.

Dan.
-- 
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
-------------- next part --------------
diff -rup libvirt-0.2.2/qemud/conf.c libvirt-0.2.2.new/qemud/conf.c
--- libvirt-0.2.2/qemud/conf.c	2007-04-17 04:34:42.000000000 -0400
+++ libvirt-0.2.2.new/qemud/conf.c	2007-05-01 18:16:46.000000000 -0400
@@ -245,21 +245,25 @@ static int qemudExtractVersionInfo(const
     cleanup1:
         _exit(-1); /* Just in case */
     } else { /* Parent */
-        char help[4096]; /* Ought to be enough to hold QEMU help screen */
+        char help[8192]; /* Ought to be enough to hold QEMU help screen */
         int got, ret = -1;
         int major, minor, micro;
 
         if (close(newstdout[1]) < 0)
             goto cleanup2;
 
-    reread:
-        if ((got = read(newstdout[0], help, sizeof(help)-1)) < 0) {
-            if (errno == EINTR)
-                goto reread;
-            goto cleanup2;
+        while (got < (sizeof(help)-1)) {
+            int len;
+            if ((len = read(newstdout[0], help+got, sizeof(help)-got-1)) <= 0) {
+                if (!len)
+                    break;
+                if (errno == EINTR)
+                    continue;
+                goto cleanup2;
+            }
+            got += len;
         }
         help[got] = '\0';
-
         if (sscanf(help, "QEMU PC emulator version %d.%d.%d", &major,&minor, &micro) != 3) {
             goto cleanup2;
         }
@@ -267,6 +271,8 @@ static int qemudExtractVersionInfo(const
         *version = (major * 1000 * 1000) + (minor * 1000) + micro;
         if (strstr(help, "-no-kqemu"))
             *flags |= QEMUD_CMD_FLAG_KQEMU;
+        if (strstr(help, "-no-reboot"))
+            *flags |= QEMUD_CMD_FLAG_NO_REBOOT;
         if (*version >= 9000)
             *flags |= QEMUD_CMD_FLAG_VNC_COLON;
         ret = 0;
@@ -858,6 +864,22 @@ static struct qemud_vm_def *qemudParseXM
     }
     xmlXPathFreeObject(obj);
 
+
+    /* See if we disable reboots */
+    obj = xmlXPathEval(BAD_CAST "string(/domain/on_reboot)", ctxt);
+    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
+        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
+        def->noReboot = 0;
+    } else {
+        if (!strcmp((char*)obj->stringval, "destroy"))
+            def->noReboot = 1;
+        else
+            def->noReboot = 0;
+    }
+    if (obj)
+        xmlXPathFreeObject(obj);
+
+
     /* Extract OS type info */
     obj = xmlXPathEval(BAD_CAST "string(/domain/os/type[1])", ctxt);
     if ((obj == NULL) || (obj->type != XPATH_STRING) ||
@@ -1220,6 +1242,8 @@ int qemudBuildCommandLine(struct qemud_s
         2 + /* cpus */
         2 + /* boot device */
         2 + /* monitor */
+        (server->qemuCmdFlags & QEMUD_CMD_FLAG_NO_REBOOT &&
+         vm->def->noReboot ? 1 : 0) + /* no-reboot */
         (vm->def->features & QEMUD_FEATURE_ACPI ? 0 : 1) + /* acpi */
         (vm->def->os.kernel[0] ? 2 : 0) + /* kernel */
         (vm->def->os.initrd[0] ? 2 : 0) + /* initrd */
@@ -1255,6 +1279,11 @@ int qemudBuildCommandLine(struct qemud_s
         goto no_memory;
     if (!((*argv)[++n] = strdup("pty")))
         goto no_memory;
+    if (server->qemuCmdFlags & QEMUD_CMD_FLAG_NO_REBOOT &&
+        vm->def->noReboot) {
+        if (!((*argv)[++n] = strdup("-no-reboot")))
+            goto no_memory;
+    }
 
     if (!(vm->def->features & QEMUD_FEATURE_ACPI)) {
         if (!((*argv)[++n] = strdup("-no-acpi")))
@@ -2517,6 +2546,17 @@ char *qemudGenerateXML(struct qemud_serv
             goto no_memory;
     }
 
+    if (bufferAdd(buf, "  <on_poweroff>destroy</on_poweroff>\n", -1) < 0)
+        goto no_memory;
+    if (def->noReboot) {
+        if (bufferAdd(buf, "  <on_reboot>destroy</on_reboot>\n", -1) < 0)
+            goto no_memory;
+    } else {
+        if (bufferAdd(buf, "  <on_reboot>restart</on_reboot>\n", -1) < 0)
+            goto no_memory;
+    }
+    if (bufferAdd(buf, "  <on_crash>destroy</on_crash>\n", -1) < 0)
+        goto no_memory;
 
     if (bufferAdd(buf, "  <devices>\n", -1) < 0)
         goto no_memory;
diff -rup libvirt-0.2.2/qemud/internal.h libvirt-0.2.2.new/qemud/internal.h
--- libvirt-0.2.2/qemud/internal.h	2007-04-11 10:13:36.000000000 -0400
+++ libvirt-0.2.2.new/qemud/internal.h	2007-05-01 17:53:42.000000000 -0400
@@ -161,6 +161,7 @@ enum qemud_vm_grapics_type {
 enum qemud_cmd_flags {
     QEMUD_CMD_FLAG_KQEMU = 1,
     QEMUD_CMD_FLAG_VNC_COLON = 2,
+    QEMUD_CMD_FLAG_NO_REBOOT = 4,
 };
 
 
@@ -191,6 +192,8 @@ struct qemud_vm_def {
     int maxmem;
     int vcpus;
 
+    int noReboot;
+
     struct qemud_vm_os_def os;
 
     int features;
Only in libvirt-0.2.2.new/qemud: libvirt-0.2.2-qemu-noreboot.patch


More information about the libvir-list mailing list