[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] PATCH: Support setting QEMU disk driver type



The domain XML has long supported ability to set the disk driver type
but we only used it in Xen so far. 

This patch adds support for QEMU usage. In this usage the driver name
is always going to be 'qemu' since all backends are in QEMU, no alternate
kernelspace / helper daemons as with Xen. The driver type will be one of
the valid QEMU disk formats.

eg

  <driver name='qemu' type='qcow2'/>


The snprintf() stuff for building the disk parameter string was getting
rather out of hand, so I took the opportunity to switch it over to use
the virBuffer routines.

 src/qemu_conf.c                                              |   49 ++++++++---
 tests/qemuxml2argvdata/qemuxml2argv-disk-drive-fmt-qcow.args |    1 
 tests/qemuxml2argvdata/qemuxml2argv-disk-drive-fmt-qcow.xml  |   29 ++++++
 tests/qemuxml2argvtest.c                                     |    2 
 tests/qemuxml2xmltest.c                                      |    1 
 5 files changed, 69 insertions(+), 13 deletions(-)


Daniel


diff --git a/src/qemu_conf.c b/src/qemu_conf.c
--- a/src/qemu_conf.c
+++ b/src/qemu_conf.c
@@ -48,6 +48,8 @@
 #include "xml.h"
 #include "nodeinfo.h"
 
+#define VIR_FROM_THIS VIR_FROM_QEMU
+
 VIR_ENUM_DECL(virDomainDiskQEMUBus)
 VIR_ENUM_IMPL(virDomainDiskQEMUBus, VIR_DOMAIN_DISK_BUS_LAST,
               "ide",
@@ -862,6 +864,18 @@ int qemudBuildCommandLine(virConnectPtr 
         ADD_ARG_LIT(vm->def->os.bootloader);
     }
 
+    for (i = 0 ; i < vm->def->ndisks ; i++) {
+        virDomainDiskDefPtr disk = vm->def->disks[i];
+
+        if (disk->driverName != NULL &&
+            !STREQ(disk->driverName, "qemu")) {
+            qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                             _("unsupported driver name '%s' for disk '%s'"),
+                             disk->driverName, disk->src);
+            goto error;
+        }
+    }
+
     /* If QEMU supports -drive param instead of old -hda, -hdb, -cdrom .. */
     if (qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE) {
         int bootCD = 0, bootFloppy = 0, bootDisk = 0;
@@ -884,8 +898,8 @@ int qemudBuildCommandLine(virConnectPtr 
         }
 
         for (i = 0 ; i < vm->def->ndisks ; i++) {
-            char opt[PATH_MAX];
-            const char *media = NULL;
+            virBuffer opt = VIR_BUFFER_INITIALIZER;
+            char *optstr;
             int bootable = 0;
             virDomainDiskDefPtr disk = vm->def->disks[i];
             int idx = virDiskNameToIndex(disk->dst);
@@ -912,7 +926,6 @@ int qemudBuildCommandLine(virConnectPtr 
             case VIR_DOMAIN_DISK_DEVICE_CDROM:
                 bootable = bootCD;
                 bootCD = 0;
-                media = "media=cdrom,";
                 break;
             case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
                 bootable = bootFloppy;
@@ -924,18 +937,28 @@ int qemudBuildCommandLine(virConnectPtr 
                 break;
             }
 
-            snprintf(opt, PATH_MAX, "file=%s,if=%s,%sindex=%d%s%s",
-                     disk->src ? disk->src : "", bus,
-                     media ? media : "",
-                     idx,
-                     bootable &&
-                     disk->device == VIR_DOMAIN_DISK_DEVICE_DISK
-                     ? ",boot=on" : "",
-                     disk->shared && ! disk->readonly
-                     ? ",cache=off" : "");
+            virBufferVSprintf(&opt, "file=%s", disk->src ? disk->src : "");
+            virBufferVSprintf(&opt, ",if=%s", bus);
+            if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
+                virBufferAddLit(&opt, ",media=cdrom");
+            virBufferVSprintf(&opt, ",index=%d", idx);
+            if (bootable &&
+                disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
+                virBufferAddLit(&opt, ",boot=on");
+            if (disk->shared && !disk->readonly)
+                virBufferAddLit(&opt, ",cache=off");
+            if (disk->driverType)
+                virBufferVSprintf(&opt, ",fmt=%s", disk->driverType);
+
+            if (virBufferError(&opt)) {
+                virReportOOMError(conn);
+                goto error;
+            }
+
+            optstr = virBufferContentAndReset(&opt);
 
             ADD_ARG_LIT("-drive");
-            ADD_ARG_LIT(opt);
+            ADD_ARG(optstr);
         }
     } else {
         for (i = 0 ; i < vm->def->ndisks ; i++) {
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-fmt-qcow.args b/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-fmt-qcow.args
new file mode 100644
--- /dev/null
+++ b/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-fmt-qcow.args
@@ -0,0 +1,1 @@
+LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test /usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic -monitor pty -pidfile /nowhere/QEMUGuest1.pid -no-acpi -boot c -drive file=/dev/HostVG/QEMUGuest1,if=ide,index=0,boot=on,fmt=qcow2 -drive file=/dev/HostVG/QEMUGuest2,if=ide,media=cdrom,index=2,fmt=raw -net none -serial none -parallel none -usb
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-fmt-qcow.xml b/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-fmt-qcow.xml
new file mode 100644
--- /dev/null
+++ b/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-fmt-qcow.xml
@@ -0,0 +1,29 @@
+<domain type='qemu'>
+  <name>QEMUGuest1</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory>219200</memory>
+  <currentMemory>219200</currentMemory>
+  <vcpu>1</vcpu>
+  <os>
+    <type arch='i686' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <emulator>/usr/bin/qemu</emulator>
+    <disk type='block' device='disk'>
+      <driver name='qemu' type='qcow2'/>
+      <source dev='/dev/HostVG/QEMUGuest1'/>
+      <target dev='hda' bus='ide'/>
+    </disk>
+    <disk type='block' device='cdrom'>
+      <driver name='qemu' type='raw'/>
+      <source dev='/dev/HostVG/QEMUGuest2'/>
+      <target dev='hdc' bus='ide'/>
+      <readonly/>
+    </disk>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -194,6 +194,8 @@ mymain(int argc, char **argv)
             QEMUD_CMD_FLAG_DRIVE_BOOT);
     DO_TEST("disk-drive-boot-cdrom", QEMUD_CMD_FLAG_DRIVE |
             QEMUD_CMD_FLAG_DRIVE_BOOT);
+    DO_TEST("disk-drive-fmt-qcow", QEMUD_CMD_FLAG_DRIVE |
+            QEMUD_CMD_FLAG_DRIVE_BOOT);
     DO_TEST("disk-usb", 0);
     DO_TEST("graphics-vnc", 0);
     DO_TEST("graphics-sdl", 0);
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
--- a/tests/qemuxml2xmltest.c
+++ b/tests/qemuxml2xmltest.c
@@ -97,6 +97,7 @@ mymain(int argc, char **argv)
     DO_TEST("disk-many");
     DO_TEST("disk-xenvbd");
     DO_TEST("disk-usb");
+    DO_TEST("disk-drive-fmt-qcow");
     DO_TEST("graphics-vnc");
     DO_TEST("graphics-sdl");
     DO_TEST("graphics-sdl-fullscreen");

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]