[libvirt] [PATCH 06/24] src/xenxs: Refactor code parsing xm disk config

Jim Fehlig jfehlig at suse.com
Fri Aug 8 21:26:52 UTC 2014


Kiarie Kahurani wrote:
> introduce function
>   xenParseXMDisk(virConfPtr conf, ........);
> which parses xm disk config instead
>
> signed-off-by: Kiarie Kahurani <davidkiarie4 at gmail.com>
> Signed-off-by: Kiarie Kahurani <davidkiarie4 at gmail.com>
> ---
>  src/xenxs/xen_xm.c | 322 ++++++++++++++++++++++++++++-------------------------
>  1 file changed, 168 insertions(+), 154 deletions(-)
>
> diff --git a/src/xenxs/xen_xm.c b/src/xenxs/xen_xm.c
> index e75842f..f4bb37d 100644
> --- a/src/xenxs/xen_xm.c
> +++ b/src/xenxs/xen_xm.c
> @@ -500,136 +500,15 @@ 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)
>   

Whitespace off a bit.

>  {
> -    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;
> -
> -    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;
> +    int hvm = STREQ(def->os.type, "hvm");
> +    virConfValuePtr list = virConfGetValue(conf, "disk");
>  
> -    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) {
> @@ -641,9 +520,8 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
>              if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
>                  goto skipdisk;
>              head = list->str;
> -
>              if (!(disk = virDomainDiskDefNew()))
> -                goto cleanup;
> +                return -1;
>  
>              /*
>               * Disks have 3 components, SOURCE,DEST-DEVICE,MODE
> @@ -662,32 +540,35 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
>                  ignore_value(virDomainDiskSetSource(disk, NULL));
>              } else {
>                  if (VIR_STRNDUP(tmp, head, offset - head) < 0)
> -                    goto cleanup;
> +                    return -1;
>   

'disk' could be leaked here and below where we return error from the
function without cleanup.  The function should have a cleanup label
similar to the vfb parsing code in 7/24.

> +
>                  if (virDomainDiskSetSource(disk, tmp) < 0) {
>                      VIR_FREE(tmp);
> -                    goto cleanup;
> +                    return -1;
>                  }
> +
>                  VIR_FREE(tmp);
>              }
> -            head = offset + 1;
>  
> +            head = offset + 1;
>              /* Remove legacy ioemu: junk */
>              if (STRPREFIX(head, "ioemu:"))
>                  head = head + 6;
> -
>              /* Extract the dest device name */
>              if (!(offset = strchr(head, ',')))
>                  goto skipdisk;
> +
>              if (VIR_ALLOC_N(disk->dst, (offset - head) + 1) < 0)
> -                goto cleanup;
> +                return -1;
> +
>              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;
> +                return -1;
>              }
> -            head = offset + 1;
>  
> +            head = offset + 1;
>              /* Extract source driver type */
>              src = virDomainDiskGetSource(disk);
>              if (src) {
> @@ -696,29 +577,31 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
>                  if ((tmp = strchr(src, ':')) != NULL) {
>                      len = tmp - src;
>                      if (VIR_STRNDUP(tmp, src, len) < 0)
> -                        goto cleanup;
> +                        return -1;
> +
>                      if (virDomainDiskSetDriver(disk, tmp) < 0) {
>                          VIR_FREE(tmp);
> -                        goto cleanup;
> +                        return -1;
>                      }
> +
>                      VIR_FREE(tmp);
>  
>                      /* Strip the prefix we found off the source file name */
>                      if (virDomainDiskSetSource(disk, src + len + 1) < 0)
> -                        goto cleanup;
> +                        return -1;
> +
>                      src = virDomainDiskGetSource(disk);
>                  }
>  
>                  /* And the sub-type for tap:XXX: type */
>                  if (STREQ_NULLABLE(virDomainDiskGetDriver(disk), "tap")) {
>                      char *driverType;
> -
>                      if (!(tmp = strchr(src, ':')))
>                          goto skipdisk;
>                      len = tmp - src;
> -
>                      if (VIR_STRNDUP(driverType, src, len) < 0)
> -                        goto cleanup;
> +                        return -1;
> +
>                      if (STREQ(driverType, "aio"))
>                          virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
>                      else
> @@ -729,12 +612,12 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
>                          virReportError(VIR_ERR_INTERNAL_ERROR,
>                                         _("Unknown driver type %s"),
>                                         src);
> -                        goto cleanup;
> +                        return -1;
>                      }
>  
>                      /* Strip the prefix we found off the source file name */
>                      if (virDomainDiskSetSource(disk, src + len + 1) < 0)
> -                        goto cleanup;
> +                        return -1;
>                      src = virDomainDiskGetSource(disk);
>                  }
>              }
> @@ -742,15 +625,13 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
>              /* No source, or driver name, so fix to phy: */
>              if (!virDomainDiskGetDriver(disk) &&
>                  virDomainDiskSetDriver(disk, "phy") < 0)
> -                goto cleanup;
> -
> +                return -1;
>  
>              /* phy: type indicates a block device */
>              virDomainDiskSetType(disk,
>                                   STREQ(virDomainDiskGetDriver(disk), "phy") ?
>                                   VIR_STORAGE_TYPE_BLOCK :
>                                   VIR_STORAGE_TYPE_FILE);
> -
>              /* Check for a :cdrom/:disk postfix */
>              disk->device = VIR_DOMAIN_DISK_DEVICE_DISK;
>              if ((tmp = strchr(disk->dst, ':')) != NULL) {
> @@ -773,10 +654,9 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
>              else if ((STREQ(head, "w!")) ||
>                       (STREQ(head, "!")))
>                  disk->src->shared = true;
> -
>              /* Maintain list in sorted order according to target device name */
>              if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
> -                goto cleanup;
> +                return -1;
>  
>              skipdisk:
>              list = list->next;
> @@ -786,27 +666,161 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
>  
>      if (hvm && xendConfigVersion == XEND_CONFIG_VERSION_3_0_2) {
>          if (xenXMConfigGetString(conf, "cdrom", &str, NULL) < 0)
> -            goto cleanup;
> +            return -1;
>          if (str) {
>              if (!(disk = virDomainDiskDefNew()))
> -                goto cleanup;
> +                return -1;
>  
>              virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
>              disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
>              if (virDomainDiskSetDriver(disk, "file") < 0)
> -                goto cleanup;
> +                return -1;
>              if (virDomainDiskSetSource(disk, str) < 0)
> -                goto cleanup;
> +                return -1;
>              if (VIR_STRDUP(disk->dst, "hdc") < 0)
> -                goto cleanup;
> +                return -1;
>              disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
>              disk->src->readonly = true;
>  
>              if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
> +                return -1;
> +        }
> +    }
> +
> +    return 0;
> +}
> +#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;
> +    virDomainDiskDefPtr disk = NULL;
>   

'disk' is no longer used in this function and can be removed.

ACK with the below squashed in.

Regards,
Jim


diff --git a/src/xenxs/xen_xm.c b/src/xenxs/xen_xm.c
index 88b63ac..855eceb 100644
--- a/src/xenxs/xen_xm.c
+++ b/src/xenxs/xen_xm.c
@@ -505,8 +505,7 @@ xenParseXMCPUFeatures(virConfPtr conf,
virDomainDefPtr def)
 
 
 static int
-xenParseXMDisk(virConfPtr conf, virDomainDefPtr def,
-                   int xendConfigVersion)
+xenParseXMDisk(virConfPtr conf, virDomainDefPtr def, int xendConfigVersion)
 {
     const char *str = NULL;
     virDomainDiskDefPtr disk = NULL;
@@ -544,13 +544,12 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def,
                 ignore_value(virDomainDiskSetSource(disk, NULL));
             } else {
                 if (VIR_STRNDUP(tmp, head, offset - head) < 0)
-                    return -1;
+                    goto cleanup;
 
                 if (virDomainDiskSetSource(disk, tmp) < 0) {
                     VIR_FREE(tmp);
-                    return -1;
+                    goto cleanup;
                 }
-
                 VIR_FREE(tmp);
             }
 
@@ -563,13 +562,13 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def,
                 goto skipdisk;
 
             if (VIR_ALLOC_N(disk->dst, (offset - head) + 1) < 0)
-                return -1;
+                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);
-                return -1;
+                goto cleanup;
             }
 
             head = offset + 1;
@@ -581,18 +580,17 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def,
                 if ((tmp = strchr(src, ':')) != NULL) {
                     len = tmp - src;
                     if (VIR_STRNDUP(tmp, src, len) < 0)
-                        return -1;
+                        goto cleanup;
 
                     if (virDomainDiskSetDriver(disk, tmp) < 0) {
                         VIR_FREE(tmp);
-                        return -1;
+                        goto cleanup;
                     }
-
                     VIR_FREE(tmp);
 
                     /* Strip the prefix we found off the source file
name */
                     if (virDomainDiskSetSource(disk, src + len + 1) < 0)
-                        return -1;
+                        goto cleanup;
 
                     src = virDomainDiskGetSource(disk);
                 }
@@ -604,7 +602,7 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def,
                         goto skipdisk;
                     len = tmp - src;
                     if (VIR_STRNDUP(driverType, src, len) < 0)
-                        return -1;
+                        goto cleanup;
 
                     if (STREQ(driverType, "aio"))
                         virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
@@ -616,12 +614,12 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def,
                         virReportError(VIR_ERR_INTERNAL_ERROR,
                                        _("Unknown driver type %s"),
                                        src);
-                        return -1;
+                        goto cleanup;
                     }
 
                     /* Strip the prefix we found off the source file
name */
                     if (virDomainDiskSetSource(disk, src + len + 1) < 0)
-                        return -1;
+                        goto cleanup;
                     src = virDomainDiskGetSource(disk);
                 }
             }
@@ -629,7 +627,7 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def,
             /* No source, or driver name, so fix to phy: */
             if (!virDomainDiskGetDriver(disk) &&
                 virDomainDiskSetDriver(disk, "phy") < 0)
-                return -1;
+               goto cleanup;
 
             /* phy: type indicates a block device */
             virDomainDiskSetType(disk,
@@ -660,7 +658,7 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def,
                 disk->src->shared = true;
             /* Maintain list in sorted order according to target device
name */
             if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
-                return -1;
+                goto cleanup;
 
             skipdisk:
             list = list->next;
@@ -670,29 +668,35 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def,
 
     if (hvm && xendConfigVersion == XEND_CONFIG_VERSION_3_0_2) {
         if (xenXMConfigGetString(conf, "cdrom", &str, NULL) < 0)
-            return -1;
+            goto cleanup;
         if (str) {
             if (!(disk = virDomainDiskDefNew()))
-                return -1;
+                goto cleanup;
 
             virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
             disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
             if (virDomainDiskSetDriver(disk, "file") < 0)
-                return -1;
+                goto cleanup;
             if (virDomainDiskSetSource(disk, str) < 0)
-                return -1;
+                goto cleanup;
             if (VIR_STRDUP(disk->dst, "hdc") < 0)
-                return -1;
+                goto cleanup;
             disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
             disk->src->readonly = true;
 
             if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
-                return -1;
+                goto cleanup;
         }
     }
 
    return 0;
+
+ cleanup:
+    virDomainDiskDefFree(disk);
+    return -1;
 }
+
+
 #define MAX_VFB 1024
 
 /*
@@ -708,7 +712,6 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
     int val;
     virConfValuePtr list;
     virDomainDefPtr def = NULL;
-    virDomainDiskDefPtr disk = NULL;
     virDomainNetDefPtr net = NULL;
     virDomainGraphicsDefPtr graphics = NULL;
     size_t i;
@@ -1235,7 +1238,6 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
  cleanup:
     virDomainGraphicsDefFree(graphics);
     virDomainNetDefFree(net);
-    virDomainDiskDefFree(disk);
     virDomainDefFree(def);
     VIR_FREE(script);
     VIR_FREE(listenAddr);




More information about the libvir-list mailing list