diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index aeec44068bea5c846afed672da5a38d468648a5b..daf4dd74fa74ec62c46c3032cfbaacbbe0ca4e9e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1993,6 +1993,7 @@ virStorageSourceInitChainElement; virStorageSourceIsEmpty; virStorageSourceIsLocalStorage; virStorageSourceNewFromBacking; +virStorageSourceParseRBDColonString; virStorageSourcePoolDefFree; virStorageSourcePoolModeTypeFromString; virStorageSourcePoolModeTypeToString; diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 641f85d332b54e75988c6347ed67183ee3cab4ae..21618b29cc8259e7d5af3403891e023cfe2cf13d 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -2535,137 +2535,20 @@ qemuGetSecretString(virConnectPtr conn, } -static int qemuAddRBDHost(virDomainDiskDefPtr disk, char *hostport) +static int +qemuParseRBDString(virDomainDiskDefPtr disk) { - char *port; - size_t skip; - char **parts; - - if (VIR_EXPAND_N(disk->src->hosts, disk->src->nhosts, 1) < 0) - return -1; - - if ((port = strchr(hostport, ']'))) { - /* ipv6, strip brackets */ - hostport += 1; - skip = 3; - } else { - port = strstr(hostport, "\\:"); - skip = 2; - } - - if (port) { - *port = '\0'; - port += skip; - if (VIR_STRDUP(disk->src->hosts[disk->src->nhosts - 1].port, port) < 0) - goto error; - } else { - if (VIR_STRDUP(disk->src->hosts[disk->src->nhosts - 1].port, "6789") < 0) - goto error; - } - - parts = virStringSplit(hostport, "\\:", 0); - if (!parts) - goto error; - disk->src->hosts[disk->src->nhosts-1].name = virStringJoin((const char **)parts, ":"); - virStringFreeList(parts); - if (!disk->src->hosts[disk->src->nhosts-1].name) - goto error; + char *source = disk->src->path; + int ret; - disk->src->hosts[disk->src->nhosts-1].transport = VIR_STORAGE_NET_HOST_TRANS_TCP; - disk->src->hosts[disk->src->nhosts-1].socket = NULL; + disk->src->path = NULL; - return 0; + ret = virStorageSourceParseRBDColonString(source, disk->src); - error: - VIR_FREE(disk->src->hosts[disk->src->nhosts-1].port); - VIR_FREE(disk->src->hosts[disk->src->nhosts-1].name); - return -1; + VIR_FREE(source); + return ret; } -/* disk->src initially has everything after the rbd: prefix */ -static int qemuParseRBDString(virDomainDiskDefPtr disk) -{ - char *options = NULL; - char *p, *e, *next; - virStorageAuthDefPtr authdef = NULL; - - p = strchr(disk->src->path, ':'); - if (p) { - if (VIR_STRDUP(options, p + 1) < 0) - goto error; - *p = '\0'; - } - - /* options */ - if (!options) - return 0; /* all done */ - - p = options; - while (*p) { - /* find : delimiter or end of string */ - for (e = p; *e && *e != ':'; ++e) { - if (*e == '\\') { - e++; - if (*e == '\0') - break; - } - } - if (*e == '\0') { - next = e; /* last kv pair */ - } else { - next = e + 1; - *e = '\0'; - } - - if (STRPREFIX(p, "id=")) { - const char *secrettype; - /* formulate authdef for disk->src->auth */ - if (VIR_ALLOC(authdef) < 0) - goto error; - - if (VIR_STRDUP(authdef->username, p + strlen("id=")) < 0) - goto error; - secrettype = virSecretUsageTypeToString(VIR_SECRET_USAGE_TYPE_CEPH); - if (VIR_STRDUP(authdef->secrettype, secrettype) < 0) - goto error; - disk->src->auth = authdef; - authdef = NULL; - - /* Cannot formulate a secretType (eg, usage or uuid) given - * what is provided. - */ - } - if (STRPREFIX(p, "mon_host=")) { - char *h, *sep; - - h = p + strlen("mon_host="); - while (h < e) { - for (sep = h; sep < e; ++sep) { - if (*sep == '\\' && (sep[1] == ',' || - sep[1] == ';' || - sep[1] == ' ')) { - *sep = '\0'; - sep += 2; - break; - } - } - if (qemuAddRBDHost(disk, h) < 0) - goto error; - - h = sep; - } - } - - p = next; - } - VIR_FREE(options); - return 0; - - error: - VIR_FREE(options); - virStorageAuthDefFree(authdef); - return -1; -} static int qemuParseDriveURIString(virDomainDiskDefPtr def, virURIPtr uri, diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index 8ca2d8d9f95db37ce08df92c5018100a29ce98aa..0659c9fd7d5b90183da45ebffcf7a4336dae48a2 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -2208,6 +2208,151 @@ virStorageSourceParseBackingURI(virStorageSourcePtr src, } +static int +virStorageSourceRBDAddHost(virStorageSourcePtr src, + char *hostport) +{ + char *port; + size_t skip; + char **parts; + + if (VIR_EXPAND_N(src->hosts, src->nhosts, 1) < 0) + return -1; + + if ((port = strchr(hostport, ']'))) { + /* ipv6, strip brackets */ + hostport += 1; + skip = 3; + } else { + port = strstr(hostport, "\\:"); + skip = 2; + } + + if (port) { + *port = '\0'; + port += skip; + if (VIR_STRDUP(src->hosts[src->nhosts - 1].port, port) < 0) + goto error; + } else { + if (VIR_STRDUP(src->hosts[src->nhosts - 1].port, "6789") < 0) + goto error; + } + + parts = virStringSplit(hostport, "\\:", 0); + if (!parts) + goto error; + src->hosts[src->nhosts-1].name = virStringJoin((const char **)parts, ":"); + virStringFreeList(parts); + if (!src->hosts[src->nhosts-1].name) + goto error; + + src->hosts[src->nhosts-1].transport = VIR_STORAGE_NET_HOST_TRANS_TCP; + src->hosts[src->nhosts-1].socket = NULL; + + return 0; + + error: + VIR_FREE(src->hosts[src->nhosts-1].port); + VIR_FREE(src->hosts[src->nhosts-1].name); + return -1; +} + + +int +virStorageSourceParseRBDColonString(const char *rbdstr, + virStorageSourcePtr src) +{ + char *options = NULL; + char *p, *e, *next; + virStorageAuthDefPtr authdef = NULL; + + /* optionally skip the "rbd:" prefix if provided */ + if (STRPREFIX(rbdstr, "rbd:")) + rbdstr += strlen("rbd:"); + + if (VIR_STRDUP(src->path, rbdstr) < 0) + goto error; + + p = strchr(src->path, ':'); + if (p) { + if (VIR_STRDUP(options, p + 1) < 0) + goto error; + *p = '\0'; + } + + /* options */ + if (!options) + return 0; /* all done */ + + p = options; + while (*p) { + /* find : delimiter or end of string */ + for (e = p; *e && *e != ':'; ++e) { + if (*e == '\\') { + e++; + if (*e == '\0') + break; + } + } + if (*e == '\0') { + next = e; /* last kv pair */ + } else { + next = e + 1; + *e = '\0'; + } + + if (STRPREFIX(p, "id=")) { + /* formulate authdef for src->auth */ + if (VIR_ALLOC(authdef) < 0) + goto error; + + if (VIR_STRDUP(authdef->username, p + strlen("id=")) < 0) + goto error; + + if (VIR_STRDUP(authdef->secrettype, + virStorageAuthTypeToString(VIR_STORAGE_AUTH_TYPE_CEPHX)) < 0) + goto error; + src->auth = authdef; + authdef = NULL; + + /* Cannot formulate a secretType (eg, usage or uuid) given + * what is provided. + */ + } + if (STRPREFIX(p, "mon_host=")) { + char *h, *sep; + + h = p + strlen("mon_host="); + while (h < e) { + for (sep = h; sep < e; ++sep) { + if (*sep == '\\' && (sep[1] == ',' || + sep[1] == ';' || + sep[1] == ' ')) { + *sep = '\0'; + sep += 2; + break; + } + } + + if (virStorageSourceRBDAddHost(src, h) < 0) + goto error; + + h = sep; + } + } + + p = next; + } + VIR_FREE(options); + return 0; + + error: + VIR_FREE(options); + virStorageAuthDefFree(authdef); + return -1; +} + + static int virStorageSourceParseBackingColon(virStorageSourcePtr src, const char *path) diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index 209036f6b8046d01e85a15de7dc6824a301dee26..cbff60c1d6d7962231a8b1342c549773bebb37fb 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -363,6 +363,10 @@ virStorageSourcePtr virStorageSourceCopy(const virStorageSource *src, bool backingChain) ATTRIBUTE_NONNULL(1); +int virStorageSourceParseRBDColonString(const char *rbdstr, + virStorageSourcePtr src) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + typedef int (*virStorageFileSimplifyPathReadlinkCallback)(const char *path, char **link,