[Libvir] PATCH: Check for /usr/bin/qemu* binaries before starting guest

Daniel P. Berrange berrange at redhat.com
Sun Apr 15 20:59:35 UTC 2007


The attached patch fixes two issues

 - It explicitly checks to see if the requested /usr/bin/qemu* binary
   actually exists before fork()/exec()'ing it. While its technically
   possible to catch the execve() failure of ENOENT, its a real pain to
   feed this error back to the qemu daemon because we're in a sub-process
   at that point. The obvious & easy solution is to thus check to see if
   the binary exists before trying to fork.

 - It only passes the -no-kqemu flag if we're running matching arch to
   the host. Previously it would pass the -no-kqemu flag even if running
   an x86_64 guest on i686 platform - QEMU then exited with error because
   the -no-kqemu flag is not recognised on x86_64 when running on i686 host.


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 --------------
Index: conf.c
===================================================================
RCS file: /data/cvs/libvirt/qemud/conf.c,v
retrieving revision 1.48
diff -u -p -r1.48 conf.c
--- conf.c	10 Apr 2007 23:17:46 -0000	1.48
+++ conf.c	15 Apr 2007 20:53:10 -0000
@@ -33,7 +33,7 @@
 #include <fcntl.h>
 #include <sys/wait.h>
 #include <arpa/inet.h>
-
+#include <sys/utsname.h>
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
@@ -299,6 +299,7 @@ static int qemudExtractVersionInfo(const
 
 int qemudExtractVersion(struct qemud_server *server) {
     char *binary = NULL;
+    struct stat sb;
 
     if (server->qemuVersion > 0)
         return 0;
@@ -306,6 +307,14 @@ int qemudExtractVersion(struct qemud_ser
     if (!(binary = qemudLocateBinaryForArch(server, QEMUD_VIRT_QEMU, "i686")))
         return -1;
 
+    if (stat(binary, &sb) < 0) {
+        qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
+                         "Cannot find QEMU binary %s: %s", binary,
+                         strerror(errno));
+        free(binary);
+        return -1;
+    }
+
     if (qemudExtractVersionInfo(binary, &server->qemuVersion, &server->qemuCmdFlags) < 0) {
         free(binary);
         return -1;
@@ -1163,16 +1172,48 @@ int qemudBuildCommandLine(struct qemud_s
     char memory[50];
     char vcpus[50];
     char boot[QEMUD_MAX_BOOT_DEVS+1];
+    struct stat sb;
     struct qemud_vm_disk_def *disk = vm->def->disks;
     struct qemud_vm_net_def *net = vm->def->nets;
+    struct utsname ut;
+    int disableKQEMU = 0;
 
     if (qemudExtractVersion(server) < 0)
         return -1;
 
+    uname(&ut);
+
+    /* Nasty hack make i?86 look like i386 to simplify next comparison */
+    if (ut.machine[0] == 'i' &&
+        ut.machine[2] == '8' &&
+        ut.machine[3] == '6' &&
+        !ut.machine[4])
+        ut.machine[1] = '3';
+
+    /* Need to explicitly disable KQEMU if
+     * 1. Arch matches host arch
+     * 2. Guest is 'qemu'
+     * 3. The qemu binary has the -no-kqemu flag
+     */
+    if ((server->qemuCmdFlags & QEMUD_CMD_FLAG_KQEMU) &&
+        !strcmp(ut.machine, vm->def->os.arch) &&
+        vm->def->virtType == QEMUD_VIRT_QEMU)
+        disableKQEMU = 1;
+
+    /* Make sure the binary we are about to try exec'ing exists.
+     * Technically we could catch the exec() failure, but that's
+     * in a sub-process so its hard to feed back a useful error
+     */
+    if (stat(vm->def->os.binary, &sb) < 0) {
+        qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
+                         "Cannot find QEMU binary %s: %s", vm->def->os.binary,
+                         strerror(errno));
+        return -1;
+    }
+
     len = 1 + /* qemu */
         2 + /* machine type */
-        (((server->qemuCmdFlags & QEMUD_CMD_FLAG_KQEMU) &&
-          (vm->def->virtType == QEMUD_VIRT_QEMU)) ? 1 : 0) + /* Disable kqemu */
+        disableKQEMU + /* Disable kqemu */
         2 * vm->def->ndisks + /* disks*/
         (vm->def->nnets > 0 ? (4 * vm->def->nnets) : 2) + /* networks */
         2 + /* memory*/
@@ -1197,8 +1238,7 @@ int qemudBuildCommandLine(struct qemud_s
         goto no_memory;
     if (!((*argv)[++n] = strdup(vm->def->os.machine)))
         goto no_memory;
-    if ((server->qemuCmdFlags & QEMUD_CMD_FLAG_KQEMU) && 
-        (vm->def->virtType == QEMUD_VIRT_QEMU)) {
+    if (disableKQEMU) {
         if (!((*argv)[++n] = strdup("-no-kqemu")))
             goto no_memory;
     }


More information about the libvir-list mailing list