]> xenbits.xensource.com Git - libvirt.git/commitdiff
src/xenxs: Refactor code parsing xm disk config
authorKiarie Kahurani <davidkiarie4@gmail.com>
Thu, 7 Aug 2014 18:32:55 +0000 (21:32 +0300)
committerJim Fehlig <jfehlig@suse.com>
Fri, 8 Aug 2014 21:20:41 +0000 (15:20 -0600)
introduce function
  xenParseXMDisk(virConfPtr conf, ........);
which parses xm disk config instead

Signed-off-by: Kiarie Kahurani <davidkiarie4@gmail.com>
src/xenxs/xen_xm.c

index ff192ccfa356a437407a329f2cdb2282dd8dce1c..3659b5fadd20ad5faf99bf3d5250389647744fbc 100644 (file)
@@ -504,136 +504,14 @@ xenParseXMCPUFeatures(virConfPtr conf, virDomainDefPtr def)
 }
 
 
-#define MAX_VFB 1024
-
-/*
- * Turn a config record into a lump of XML describing the
- * domain, suitable for later feeding for virDomainCreateXML
- */
-virDomainDefPtr
-xenParseXM(virConfPtr conf, int xendConfigVersion,
-           virCapsPtr caps)
+static int
+xenParseXMDisk(virConfPtr conf, virDomainDefPtr def, int xendConfigVersion)
 {
-    const char *str;
-    int hvm = 0;
-    int val;
-    virConfValuePtr list;
-    virDomainDefPtr def = NULL;
+    const char *str = NULL;
     virDomainDiskDefPtr disk = NULL;
-    virDomainNetDefPtr net = NULL;
-    virDomainGraphicsDefPtr graphics = NULL;
-    size_t i;
-    const char *defaultMachine;
-    char *script = NULL;
-    char *listenAddr = NULL;
-
-    if (VIR_ALLOC(def) < 0)
-        return NULL;
-
-    def->virtType = VIR_DOMAIN_VIRT_XEN;
-    def->id = -1;
+    int hvm = STREQ(def->os.type, "hvm");
+    virConfValuePtr list = virConfGetValue(conf, "disk");
 
-    if (xenXMConfigCopyString(conf, "name", &def->name) < 0)
-        goto cleanup;
-    if (xenXMConfigGetUUID(conf, "uuid", def->uuid) < 0)
-        goto cleanup;
-
-
-    if ((xenXMConfigGetString(conf, "builder", &str, "linux") == 0) &&
-        STREQ(str, "hvm"))
-        hvm = 1;
-
-    if (VIR_STRDUP(def->os.type, hvm ? "hvm" : "xen") < 0)
-        goto cleanup;
-
-    def->os.arch =
-        virCapabilitiesDefaultGuestArch(caps,
-                                        def->os.type,
-                                        virDomainVirtTypeToString(def->virtType));
-    if (!def->os.arch) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("no supported architecture for os type '%s'"),
-                       def->os.type);
-        goto cleanup;
-    }
-
-    defaultMachine = virCapabilitiesDefaultGuestMachine(caps,
-                                                        def->os.type,
-                                                        def->os.arch,
-                                                        virDomainVirtTypeToString(def->virtType));
-    if (defaultMachine != NULL) {
-        if (VIR_STRDUP(def->os.machine, defaultMachine) < 0)
-            goto cleanup;
-    }
-
-    if (hvm) {
-        const char *boot;
-        if (xenXMConfigCopyString(conf, "kernel", &def->os.loader) < 0)
-            goto cleanup;
-
-        if (xenXMConfigGetString(conf, "boot", &boot, "c") < 0)
-            goto cleanup;
-
-        for (i = 0; i < VIR_DOMAIN_BOOT_LAST && boot[i]; i++) {
-            switch (*boot) {
-            case 'a':
-                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_FLOPPY;
-                break;
-            case 'd':
-                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_CDROM;
-                break;
-            case 'n':
-                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_NET;
-                break;
-            case 'c':
-            default:
-                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_DISK;
-                break;
-            }
-            def->os.nBootDevs++;
-        }
-    } else {
-        const char *extra, *root;
-
-        if (xenXMConfigCopyStringOpt(conf, "bootloader", &def->os.bootloader) < 0)
-            goto cleanup;
-        if (xenXMConfigCopyStringOpt(conf, "bootargs", &def->os.bootloaderArgs) < 0)
-            goto cleanup;
-
-        if (xenXMConfigCopyStringOpt(conf, "kernel", &def->os.kernel) < 0)
-            goto cleanup;
-        if (xenXMConfigCopyStringOpt(conf, "ramdisk", &def->os.initrd) < 0)
-            goto cleanup;
-        if (xenXMConfigGetString(conf, "extra", &extra, NULL) < 0)
-            goto cleanup;
-        if (xenXMConfigGetString(conf, "root", &root, NULL) < 0)
-            goto cleanup;
-
-        if (root) {
-            if (virAsprintf(&def->os.cmdline, "root=%s %s", root, extra) < 0)
-                goto cleanup;
-        } else {
-            if (VIR_STRDUP(def->os.cmdline, extra) < 0)
-                goto cleanup;
-        }
-    }
-
-    if (xenParseXMMem(conf, def) < 0)
-        goto cleanup;
-
-    if (xenParseXMEventsActions(conf, def) < 0)
-        goto cleanup;
-
-    if (xenParseXMCPUFeatures(conf, def) < 0)
-        goto cleanup;
-
-    if (xenParseXMTimeOffset(conf, def, xendConfigVersion) < 0)
-        goto cleanup;
-
-    if (xenXMConfigCopyStringOpt(conf, "device_model", &def->emulator) < 0)
-        goto cleanup;
-
-    list = virConfGetValue(conf, "disk");
     if (list && list->type == VIR_CONF_LIST) {
         list = list->list;
         while (list) {
@@ -644,10 +522,10 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
 
             if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
                 goto skipdisk;
-            head = list->str;
 
+            head = list->str;
             if (!(disk = virDomainDiskDefNew()))
-                goto cleanup;
+                return -1;
 
             /*
              * Disks have 3 components, SOURCE,DEST-DEVICE,MODE
@@ -667,14 +545,15 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
             } else {
                 if (VIR_STRNDUP(tmp, head, offset - head) < 0)
                     goto cleanup;
+
                 if (virDomainDiskSetSource(disk, tmp) < 0) {
                     VIR_FREE(tmp);
                     goto cleanup;
                 }
                 VIR_FREE(tmp);
             }
-            head = offset + 1;
 
+            head = offset + 1;
             /* Remove legacy ioemu: junk */
             if (STRPREFIX(head, "ioemu:"))
                 head = head + 6;
@@ -682,16 +561,18 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
             /* Extract the dest device name */
             if (!(offset = strchr(head, ',')))
                 goto skipdisk;
+
             if (VIR_ALLOC_N(disk->dst, (offset - head) + 1) < 0)
                 goto cleanup;
+
             if (virStrncpy(disk->dst, head, offset - head,
                            (offset - head) + 1) == NULL) {
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("Dest file %s too big for destination"), head);
                 goto cleanup;
             }
-            head = offset + 1;
 
+            head = offset + 1;
             /* Extract source driver type */
             src = virDomainDiskGetSource(disk);
             if (src) {
@@ -701,6 +582,7 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
                     len = tmp - src;
                     if (VIR_STRNDUP(tmp, src, len) < 0)
                         goto cleanup;
+
                     if (virDomainDiskSetDriver(disk, tmp) < 0) {
                         VIR_FREE(tmp);
                         goto cleanup;
@@ -710,6 +592,7 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
                     /* Strip the prefix we found off the source file name */
                     if (virDomainDiskSetSource(disk, src + len + 1) < 0)
                         goto cleanup;
+
                     src = virDomainDiskGetSource(disk);
                 }
 
@@ -723,6 +606,7 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
 
                     if (VIR_STRNDUP(driverType, src, len) < 0)
                         goto cleanup;
+
                     if (STREQ(driverType, "aio"))
                         virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
                     else
@@ -748,7 +632,6 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
                 virDomainDiskSetDriver(disk, "phy") < 0)
                 goto cleanup;
 
-
             /* phy: type indicates a block device */
             virDomainDiskSetType(disk,
                                  STREQ(virDomainDiskGetDriver(disk), "phy") ?
@@ -811,6 +694,145 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
         }
     }
 
+    return 0;
+
+ cleanup:
+    virDomainDiskDefFree(disk);
+    return -1;
+}
+
+
+#define MAX_VFB 1024
+
+/*
+ * Turn a config record into a lump of XML describing the
+ * domain, suitable for later feeding for virDomainCreateXML
+ */
+virDomainDefPtr
+xenParseXM(virConfPtr conf, int xendConfigVersion,
+           virCapsPtr caps)
+{
+    const char *str;
+    int hvm = 0;
+    int val;
+    virConfValuePtr list;
+    virDomainDefPtr def = NULL;
+    virDomainNetDefPtr net = NULL;
+    virDomainGraphicsDefPtr graphics = NULL;
+    size_t i;
+    const char *defaultMachine;
+    char *script = NULL;
+    char *listenAddr = NULL;
+
+    if (VIR_ALLOC(def) < 0)
+        return NULL;
+
+    def->virtType = VIR_DOMAIN_VIRT_XEN;
+    def->id = -1;
+
+    if (xenXMConfigCopyString(conf, "name", &def->name) < 0)
+        goto cleanup;
+    if (xenXMConfigGetUUID(conf, "uuid", def->uuid) < 0)
+        goto cleanup;
+
+
+    if ((xenXMConfigGetString(conf, "builder", &str, "linux") == 0) &&
+        STREQ(str, "hvm"))
+        hvm = 1;
+
+    if (VIR_STRDUP(def->os.type, hvm ? "hvm" : "xen") < 0)
+        goto cleanup;
+
+    def->os.arch =
+        virCapabilitiesDefaultGuestArch(caps,
+                                        def->os.type,
+                                        virDomainVirtTypeToString(def->virtType));
+    if (!def->os.arch) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("no supported architecture for os type '%s'"),
+                       def->os.type);
+        goto cleanup;
+    }
+
+    defaultMachine = virCapabilitiesDefaultGuestMachine(caps,
+                                                        def->os.type,
+                                                        def->os.arch,
+                                                        virDomainVirtTypeToString(def->virtType));
+    if (defaultMachine != NULL) {
+        if (VIR_STRDUP(def->os.machine, defaultMachine) < 0)
+            goto cleanup;
+    }
+
+    if (hvm) {
+        const char *boot;
+        if (xenXMConfigCopyString(conf, "kernel", &def->os.loader) < 0)
+            goto cleanup;
+
+        if (xenXMConfigGetString(conf, "boot", &boot, "c") < 0)
+            goto cleanup;
+
+        for (i = 0; i < VIR_DOMAIN_BOOT_LAST && boot[i]; i++) {
+            switch (*boot) {
+            case 'a':
+                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_FLOPPY;
+                break;
+            case 'd':
+                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_CDROM;
+                break;
+            case 'n':
+                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_NET;
+                break;
+            case 'c':
+            default:
+                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_DISK;
+                break;
+            }
+            def->os.nBootDevs++;
+        }
+    } else {
+        const char *extra, *root;
+
+        if (xenXMConfigCopyStringOpt(conf, "bootloader", &def->os.bootloader) < 0)
+            goto cleanup;
+        if (xenXMConfigCopyStringOpt(conf, "bootargs", &def->os.bootloaderArgs) < 0)
+            goto cleanup;
+
+        if (xenXMConfigCopyStringOpt(conf, "kernel", &def->os.kernel) < 0)
+            goto cleanup;
+        if (xenXMConfigCopyStringOpt(conf, "ramdisk", &def->os.initrd) < 0)
+            goto cleanup;
+        if (xenXMConfigGetString(conf, "extra", &extra, NULL) < 0)
+            goto cleanup;
+        if (xenXMConfigGetString(conf, "root", &root, NULL) < 0)
+            goto cleanup;
+
+        if (root) {
+            if (virAsprintf(&def->os.cmdline, "root=%s %s", root, extra) < 0)
+                goto cleanup;
+        } else {
+            if (VIR_STRDUP(def->os.cmdline, extra) < 0)
+                goto cleanup;
+        }
+    }
+
+    if (xenParseXMMem(conf, def) < 0)
+        goto cleanup;
+
+    if (xenParseXMEventsActions(conf, def) < 0)
+        goto cleanup;
+
+    if (xenParseXMCPUFeatures(conf, def) < 0)
+        goto cleanup;
+
+    if (xenParseXMTimeOffset(conf, def, xendConfigVersion) < 0)
+        goto cleanup;
+
+    if (xenParseXMDisk(conf, def, xendConfigVersion) < 0)
+        goto cleanup;
+
+    if (xenXMConfigCopyStringOpt(conf, "device_model", &def->emulator) < 0)
+        goto cleanup;
+
     list = virConfGetValue(conf, "vif");
     if (list && list->type == VIR_CONF_LIST) {
         list = list->list;
@@ -1221,7 +1243,6 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
  cleanup:
     virDomainGraphicsDefFree(graphics);
     virDomainNetDefFree(net);
-    virDomainDiskDefFree(disk);
     virDomainDefFree(def);
     VIR_FREE(script);
     VIR_FREE(listenAddr);