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

Re: [libvirt] [PATCH 01/10] Split code for building QEMU -drive arg in separate method



On Tue, Dec 15, 2009 at 03:14:41PM +0000, Daniel P. Berrange wrote:
> 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, 111 insertions(+), 84 deletions(-)
> 
> diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
> index 86172c6..61e719d 100644
> --- a/src/qemu/qemu_conf.c
> +++ b/src/qemu/qemu_conf.c
> @@ -1313,6 +1313,108 @@ 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);
> +
> +    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:
> +    virBufferFreeAndReset(&opt);
> +    *str = NULL;
> +    return -1;
> +}
> +
> +
>  int
>  qemuBuildNicStr(virConnectPtr conn,
>                  virDomainNetDefPtr net,
> @@ -1563,23 +1665,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.
> @@ -1983,12 +2068,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) {
> @@ -2003,12 +2085,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;
> @@ -2024,64 +2100,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)) {
> -                virBufferFreeAndReset(&opt);
> -                goto no_memory;
> -            }
> -
> -            optstr = virBufferContentAndReset(&opt);
> +            if (qemuBuildDriveStr(conn, disk, bootable, qemuCmdFlags, &optstr) < 0)
> +                goto error;
>  
>              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,

  ACK

Daniel

-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
daniel veillard com  | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library  http://libvirt.org/


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