[libvirt] [PATCH 05/10] Convert disk drive over to use -device where available

Daniel P. Berrange berrange at redhat.com
Tue Dec 15 15:14:45 UTC 2009


The current preferred syntax for disk drives uses

  -drive file=/vms/plain.qcow,if=virtio,index=0,boot=on,format=qcow

The new syntax splits this up into a pair of linked args

  -drive file=/vms/plain.qcow,if=none,id=virtio-0,index=0,format=qcow2
  -device virtio-blk-pci,drive=virtio-0

NB, the 'index=0' bit here in the new args is technically wrong.
This will be fixed when the disk-controller /addressing patches
merge with this.
---
 src/qemu/qemu_conf.c |   83 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 74 insertions(+), 9 deletions(-)

diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index 8c44a93..2480df4 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1373,10 +1373,16 @@ qemuBuildDriveStr(virConnectPtr conn,
             virBufferVSprintf(&opt, "file=%s,", disk->src);
         }
     }
-    virBufferVSprintf(&opt, "if=%s", bus);
+    if (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE) {
+        virBufferAddLit(&opt, "if=none,");
+        virBufferVSprintf(&opt, "id=%s-%d", bus, idx);
+    } else {
+        virBufferVSprintf(&opt, "if=%s", bus);
+    }
     if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
         virBufferAddLit(&opt, ",media=cdrom");
-    virBufferVSprintf(&opt, ",index=%d", idx);
+    if (!(qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE))
+        virBufferVSprintf(&opt, ",index=%d", idx);
     if (bootable &&
         disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
         virBufferAddLit(&opt, ",boot=on");
@@ -1416,6 +1422,48 @@ error:
     return -1;
 }
 
+static int
+qemuBuildDriveDevStr(virConnectPtr conn,
+                     virDomainDiskDefPtr disk,
+                     char **str)
+{
+    virBuffer opt = VIR_BUFFER_INITIALIZER;
+    const char *bus = virDomainDiskQEMUBusTypeToString(disk->bus);
+    int idx = virDiskNameToIndex(disk->dst);
+
+    if (idx < 0) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                         _("unsupported disk type '%s'"), disk->dst);
+        goto error;
+    }
+
+    switch (disk->bus) {
+    case VIR_DOMAIN_DISK_BUS_IDE:
+        virBufferAddLit(&opt, "ide-drive");
+        break;
+    case VIR_DOMAIN_DISK_BUS_SCSI:
+        virBufferAddLit(&opt, "scsi-disk");
+        break;
+    case VIR_DOMAIN_DISK_BUS_VIRTIO:
+        virBufferAddLit(&opt, "virtio-blk-pci");
+        break;
+
+    default:
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                         _("unsupported disk bus '%s' with device setup"), bus);
+        goto error;
+    }
+    virBufferVSprintf(&opt, ",drive=%s-%d", bus, idx);
+
+    *str = virBufferContentAndReset(&opt);
+    return 0;
+
+error:
+    virBufferFreeAndReset(&opt);
+    *str = NULL;
+    return -1;
+}
+
 
 int
 qemuBuildNicStr(virConnectPtr conn,
@@ -2104,6 +2152,7 @@ int qemudBuildCommandLine(virConnectPtr conn,
             char *optstr;
             int bootable = 0;
             virDomainDiskDefPtr disk = def->disks[i];
+            int withDeviceArg = 0;
 
             if (disk->bus == VIR_DOMAIN_DISK_BUS_USB) {
                 if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
@@ -2116,8 +2165,6 @@ int qemudBuildCommandLine(virConnectPtr conn,
                 continue;
             }
 
-            ADD_ARG_SPACE;
-
             switch (disk->device) {
             case VIR_DOMAIN_DISK_DEVICE_CDROM:
                 bootable = bootCD;
@@ -2133,14 +2180,32 @@ int qemudBuildCommandLine(virConnectPtr conn,
                 break;
             }
 
-            if (qemuBuildDriveStr(conn, disk, bootable, qemuCmdFlags, &optstr) < 0)
+            /* Unfortunately it is nt possible to use
+               -device for floppys, or Xen paravirt
+               devices. Fortunately, those don't need
+               static PCI addresses, so we don't really
+               care that we can't use -device */
+            if ((qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE) &&
+                (disk->bus != VIR_DOMAIN_DISK_BUS_FDC) &&
+                (disk->bus != VIR_DOMAIN_DISK_BUS_XEN))
+                withDeviceArg = 1;
+
+            ADD_ARG_LIT("-drive");
+
+            if (qemuBuildDriveStr(conn, disk, bootable,
+                                  (withDeviceArg ? qemuCmdFlags :
+                                   (qemuCmdFlags & ~QEMUD_CMD_FLAG_DEVICE)),
+                                  &optstr) < 0)
                 goto error;
+            ADD_ARG(optstr);
 
-            if ((qargv[qargc++] = strdup("-drive")) == NULL) {
-                VIR_FREE(optstr);
-                goto no_memory;
+            if (withDeviceArg) {
+                ADD_ARG_LIT("-device");
+
+                if (qemuBuildDriveDevStr(conn, disk, &optstr) < 0)
+                    goto error;
+                ADD_ARG(optstr);
             }
-            ADD_ARG(optstr);
         }
     } else {
         for (i = 0 ; i < def->ndisks ; i++) {
-- 
1.6.5.2




More information about the libvir-list mailing list