xen_xm: Split the per-disk logic from xenParseXMDisk()

xenParseXMDisk() does a lot of stuff and, in order to make things
cleaner, let's split it in two new functions:
- xenParseXMDisk(): it's a new function that keeps the old name. It's
responsible for the whole per-disk logic from the old xenParseXMDisk();
- xenParseXMDiskList(): it's basically the old xenParseXMDisk(), but
now it just iterates over the list of disks, calling xenParseXMDisk()
per each disk.

This patch is basically preparing the ground for the future when
typesafe virConf acessors will be used.

Signed-off-by: Fabiano Fidêncio <fabiano@fidencio.org>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Fabiano Fidêncio 2018-05-28 00:28:20 +02:00 committed by Ján Tomko
parent 057a78ea31
commit 5d358df82a

View File

@ -107,28 +107,19 @@ xenParseXMOS(virConfPtr conf, virDomainDefPtr def)
}
static int
xenParseXMDisk(virConfPtr conf, virDomainDefPtr def)
static virDomainDiskDefPtr
xenParseXMDisk(char *entry, int hvm)
{
virDomainDiskDefPtr disk = NULL;
int hvm = def->os.type == VIR_DOMAIN_OSTYPE_HVM;
virConfValuePtr list = virConfGetValue(conf, "disk");
if (list && list->type == VIR_CONF_LIST) {
list = list->list;
while (list) {
char *head;
char *offset;
char *tmp;
const char *src;
if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
goto skipdisk;
head = list->str;
if (!(disk = virDomainDiskDefNew(NULL)))
return -1;
return NULL;
head = entry;
/*
* Disks have 3 components, SOURCE,DEST-DEVICE,MODE
* eg, phy:/dev/HostVG/XenGuest1,xvda,w
@ -139,18 +130,18 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def)
/* Extract the source file path*/
if (!(offset = strchr(head, ',')))
goto skipdisk;
goto error;
if (offset == head) {
/* No source file given, eg CDROM with no media */
ignore_value(virDomainDiskSetSource(disk, NULL));
} else {
if (VIR_STRNDUP(tmp, head, offset - head) < 0)
goto cleanup;
goto error;
if (virDomainDiskSetSource(disk, tmp) < 0) {
VIR_FREE(tmp);
goto cleanup;
goto error;
}
VIR_FREE(tmp);
}
@ -162,16 +153,16 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def)
/* Extract the dest device name */
if (!(offset = strchr(head, ',')))
goto skipdisk;
goto error;
if (VIR_ALLOC_N(disk->dst, (offset - head) + 1) < 0)
goto cleanup;
goto error;
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;
goto error;
}
head = offset + 1;
@ -183,17 +174,17 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def)
if ((tmp = strchr(src, ':')) != NULL) {
len = tmp - src;
if (VIR_STRNDUP(tmp, src, len) < 0)
goto cleanup;
goto error;
if (virDomainDiskSetDriver(disk, tmp) < 0) {
VIR_FREE(tmp);
goto cleanup;
goto error;
}
VIR_FREE(tmp);
/* Strip the prefix we found off the source file name */
if (virDomainDiskSetSource(disk, src + len + 1) < 0)
goto cleanup;
goto error;
src = virDomainDiskGetSource(disk);
}
@ -204,11 +195,11 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def)
char *driverType;
if (!(tmp = strchr(src, ':')))
goto skipdisk;
goto error;
len = tmp - src;
if (VIR_STRNDUP(driverType, src, len) < 0)
goto cleanup;
goto error;
if (STREQ(driverType, "aio"))
virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
@ -220,12 +211,12 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def)
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unknown driver type %s"),
src);
goto cleanup;
goto error;
}
/* Strip the prefix we found off the source file name */
if (virDomainDiskSetSource(disk, src + len + 1) < 0)
goto cleanup;
goto error;
src = virDomainDiskGetSource(disk);
}
}
@ -233,7 +224,7 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def)
/* No source, or driver name, so fix to phy: */
if (!virDomainDiskGetDriver(disk) &&
virDomainDiskSetDriver(disk, "phy") < 0)
goto cleanup;
goto error;
/* phy: type indicates a block device */
virDomainDiskSetType(disk,
@ -249,37 +240,54 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def)
tmp[0] = '\0';
}
if (STRPREFIX(disk->dst, "xvd") || !hvm) {
if (STRPREFIX(disk->dst, "xvd") || !hvm)
disk->bus = VIR_DOMAIN_DISK_BUS_XEN;
} else if (STRPREFIX(disk->dst, "sd")) {
else if (STRPREFIX(disk->dst, "sd"))
disk->bus = VIR_DOMAIN_DISK_BUS_SCSI;
} else {
else
disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
}
if (STREQ(head, "r") ||
STREQ(head, "ro"))
if (STREQ(head, "r") || STREQ(head, "ro"))
disk->src->readonly = true;
else if ((STREQ(head, "w!")) ||
(STREQ(head, "!")))
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 disk;
skipdisk:
list = list->next;
error:
virDomainDiskDefFree(disk);
disk = NULL;
return NULL;
}
static int
xenParseXMDiskList(virConfPtr conf, virDomainDefPtr def)
{
int hvm = def->os.type == VIR_DOMAIN_OSTYPE_HVM;
virConfValuePtr list = virConfGetValue(conf, "disk");
if (!list || list->type != VIR_CONF_LIST)
return 0;
for (list = list->list; list; list = list->next) {
virDomainDiskDefPtr disk;
int rc;
if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
continue;
if (!(disk = xenParseXMDisk(list->str, hvm)))
continue;
/* Maintain list in sorted order according to target device name */
rc = VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk);
virDomainDiskDefFree(disk);
if (rc < 0)
return -1;
}
return 0;
cleanup:
virDomainDiskDefFree(disk);
return -1;
}
@ -457,7 +465,7 @@ xenParseXM(virConfPtr conf,
if (xenParseXMOS(conf, def) < 0)
goto cleanup;
if (xenParseXMDisk(conf, def) < 0)
if (xenParseXMDiskList(conf, def) < 0)
goto cleanup;
if (xenParseXMInputDevs(conf, def) < 0)