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

[libvirt] [PATCH 08/12] Split code for building QEMU -drive arg in separate method



To enable it to be called from multiple locations, split out
the code for building the -drive arg string

* src/qemu/qemu_conf.c, src/qemu/qemu_conf.h: Add qemuBuildDriveStr
  for building -drive arg string
---
 src/qemu/qemu_conf.c |  188 ++++++++++++++++++++++++++++----------------------
 src/qemu/qemu_conf.h |    7 ++
 2 files changed, 112 insertions(+), 83 deletions(-)

diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index 39d9314..27faa3f 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1313,6 +1313,110 @@ qemuAssignNetNames(virDomainDefPtr def,
     return 0;
 }
 
+#define QEMU_SERIAL_PARAM_ACCEPTED_CHARS \
+  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
+
+static int
+qemuSafeSerialParamValue(virConnectPtr conn,
+                         const char *value)
+{
+    if (strspn(value, QEMU_SERIAL_PARAM_ACCEPTED_CHARS) != strlen (value)) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                         _("driver serial '%s' contains unsafe characters"),
+                         value);
+        return -1;
+    }
+
+    return 0;
+}
+
+
+int
+qemuBuildDriveStr(virConnectPtr conn,
+                  virDomainDiskDefPtr disk,
+                  int bootable,
+                  int qemuCmdFlags,
+                  char **str)
+{
+    virBuffer opt = VIR_BUFFER_INITIALIZER;
+    const char *bus = virDomainDiskQEMUBusTypeToString(disk->bus);
+    int idx = virDiskNameToIndex(disk->dst);
+    char *optstr = NULL;
+
+    if (idx < 0) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                         _("unsupported disk type '%s'"), disk->dst);
+        goto error;
+    }
+
+    if (disk->src) {
+        if (disk->type == VIR_DOMAIN_DISK_TYPE_DIR) {
+            /* QEMU only supports magic FAT format for now */
+            if (disk->driverType &&
+                STRNEQ(disk->driverType, "fat")) {
+                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                                 _("unsupported disk driver type for '%s'"),
+                                 disk->driverType);
+                goto error;
+            }
+            if (!disk->readonly) {
+                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
+                                 _("cannot create virtual FAT disks in read-write mode"));
+                goto error;
+            }
+            if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
+                virBufferVSprintf(&opt, "file=fat:floppy:%s,", disk->src);
+            else
+                virBufferVSprintf(&opt, "file=fat:%s,", disk->src);
+        } else {
+            virBufferVSprintf(&opt, "file=%s,", 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->driverType &&
+        disk->type != VIR_DOMAIN_DISK_TYPE_DIR &&
+        qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE_FORMAT)
+        virBufferVSprintf(&opt, ",format=%s", disk->driverType);
+    if (disk->serial &&
+        (qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE_SERIAL)) {
+        if (qemuSafeSerialParamValue(conn, disk->serial) < 0)
+            goto error;
+        virBufferVSprintf(&opt, ",serial=%s", disk->serial);
+    }
+
+    if (disk->cachemode) {
+        const char *mode =
+            (qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE_CACHE_V2) ?
+            qemuDiskCacheV2TypeToString(disk->cachemode) :
+            qemuDiskCacheV1TypeToString(disk->cachemode);
+
+        virBufferVSprintf(&opt, ",cache=%s", mode);
+    } else if (disk->shared && !disk->readonly) {
+        virBufferAddLit(&opt, ",cache=off");
+    }
+
+    if (virBufferError(&opt)) {
+        virReportOOMError(conn);
+        goto error;
+    }
+
+    *str = virBufferContentAndReset(&opt);
+    return 0;
+
+error:
+    optstr = virBufferContentAndReset(&opt);
+    VIR_FREE(optstr);
+    *str = NULL;
+    return -1;
+}
+
+
 int
 qemuBuildNicStr(virConnectPtr conn,
                 virDomainNetDefPtr net,
@@ -1562,23 +1666,6 @@ static void qemudBuildCommandLineChrDevStr(virDomainChrDefPtr dev,
     }
 }
 
-#define QEMU_SERIAL_PARAM_ACCEPTED_CHARS \
-  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
-
-static int
-qemuSafeSerialParamValue(virConnectPtr conn,
-                         const char *value)
-{
-    if (strspn(value, QEMU_SERIAL_PARAM_ACCEPTED_CHARS) != strlen (value)) {
-        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
-                         _("driver serial '%s' contains unsafe characters"),
-                         value);
-        return -1;
-    }
-
-    return 0;
-}
-
 /*
  * Constructs a argv suitable for launching qemu with config defined
  * for a given virtual machine.
@@ -1960,12 +2047,9 @@ int qemudBuildCommandLine(virConnectPtr conn,
         }
 
         for (i = 0 ; i < def->ndisks ; i++) {
-            virBuffer opt = VIR_BUFFER_INITIALIZER;
             char *optstr;
             int bootable = 0;
             virDomainDiskDefPtr disk = def->disks[i];
-            int idx = virDiskNameToIndex(disk->dst);
-            const char *bus = virDomainDiskQEMUBusTypeToString(disk->bus);
 
             if (disk->bus == VIR_DOMAIN_DISK_BUS_USB) {
                 if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
@@ -1980,12 +2064,6 @@ int qemudBuildCommandLine(virConnectPtr conn,
 
             ADD_ARG_SPACE;
 
-            if (idx < 0) {
-                qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
-                                 _("unsupported disk type '%s'"), disk->dst);
-                goto error;
-            }
-
             switch (disk->device) {
             case VIR_DOMAIN_DISK_DEVICE_CDROM:
                 bootable = bootCD;
@@ -2001,64 +2079,8 @@ int qemudBuildCommandLine(virConnectPtr conn,
                 break;
             }
 
-            if (disk->src) {
-                if (disk->type == VIR_DOMAIN_DISK_TYPE_DIR) {
-                    /* QEMU only supports magic FAT format for now */
-                    if (disk->driverType &&
-                        STRNEQ(disk->driverType, "fat")) {
-                        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
-                                         _("unsupported disk driver type for '%s'"),
-                                         disk->driverType);
-                        goto error;
-                    }
-                    if (!disk->readonly) {
-                        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
-                                         _("cannot create virtual FAT disks in read-write mode"));
-                        goto error;
-                    }
-                    if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
-                        virBufferVSprintf(&opt, "file=fat:floppy:%s,", disk->src);
-                    else
-                        virBufferVSprintf(&opt, "file=fat:%s,", disk->src);
-                } else {
-                    virBufferVSprintf(&opt, "file=%s,", 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->driverType &&
-                disk->type != VIR_DOMAIN_DISK_TYPE_DIR &&
-                qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE_FORMAT)
-                virBufferVSprintf(&opt, ",format=%s", disk->driverType);
-            if (disk->serial &&
-                (qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE_SERIAL)) {
-                if (qemuSafeSerialParamValue(conn, disk->serial) < 0)
-                    goto error;
-                virBufferVSprintf(&opt, ",serial=%s", disk->serial);
-            }
-
-            if (disk->cachemode) {
-                const char *mode =
-                    (qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE_CACHE_V2) ?
-                    qemuDiskCacheV2TypeToString(disk->cachemode) :
-                    qemuDiskCacheV1TypeToString(disk->cachemode);
-
-                virBufferVSprintf(&opt, ",cache=%s", mode);
-            } else if (disk->shared && !disk->readonly) {
-                virBufferAddLit(&opt, ",cache=off");
-            }
-
-            if (virBufferError(&opt)) {
-                virReportOOMError(conn);
+            if (qemuBuildDriveStr(conn, disk, bootable, qemuCmdFlags, &optstr) < 0)
                 goto error;
-            }
-
-            optstr = virBufferContentAndReset(&opt);
 
             if ((qargv[qargc++] = strdup("-drive")) == NULL) {
                 VIR_FREE(optstr);
diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index 248677a..cd59d4c 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -191,6 +191,13 @@ int         qemuBuildNicStr             (virConnectPtr conn,
                                          int vlan,
                                          char **str);
 
+
+int         qemuBuildDriveStr           (virConnectPtr conn,
+                                         virDomainDiskDefPtr disk,
+                                         int bootable,
+                                         int qemuCmdFlags,
+                                         char **str);
+
 int         qemudNetworkIfaceConnect    (virConnectPtr conn,
                                          struct qemud_driver *driver,
                                          virDomainNetDefPtr net,
-- 
1.6.5.2


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