From a042275a396ed5fc338bfdd464b76de2215a8058 Mon Sep 17 00:00:00 2001 From: Martin Kletzander Date: Tue, 26 Apr 2016 08:43:40 +0200 Subject: [PATCH] Unify domain name shortening Add virDomainObjGetShortName() and use it. For now that's used in one place, but we should expose it so that future patches can use it. Signed-off-by: Martin Kletzander --- src/conf/domain_conf.c | 20 ++++++++++++++++++++ src/conf/domain_conf.h | 2 ++ src/libvirt_private.syms | 1 + src/qemu/qemu_domain.c | 24 ++++++++---------------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 2c6244aaf3..b46cd2a64f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -24307,3 +24307,23 @@ virDomainDefHasMemballoon(const virDomainDef *def) return def->memballoon && def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_NONE; } + + +/** + * virDomainObjGetShortName: + * @vm: Machine for which to get a name + * @unique: Make sure the name is unique (use id as well) + * + * Shorten domain name to avoid possible path length limitations. + */ +char * +virDomainObjGetShortName(virDomainObjPtr vm) +{ + const int dommaxlen = 20; + char *ret = NULL; + + ignore_value(virAsprintf(&ret, "%d-%.*s", + vm->def->id, dommaxlen, vm->def->name)); + + return ret; +} diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index fd540ed80e..ee66e6d6aa 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3162,4 +3162,6 @@ int virDomainDefGetVcpuPinInfoHelper(virDomainDefPtr def, bool virDomainDefHasMemballoon(const virDomainDef *def) ATTRIBUTE_NONNULL(1); +char *virDomainObjGetShortName(virDomainObjPtr vm); + #endif /* __DOMAIN_CONF_H */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ad5c382609..0de35efb25 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -400,6 +400,7 @@ virDomainObjGetDefs; virDomainObjGetMetadata; virDomainObjGetOneDef; virDomainObjGetPersistentDef; +virDomainObjGetShortName; virDomainObjGetState; virDomainObjNew; virDomainObjParseNode; diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index b95ba7e781..a2f9810439 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -787,39 +787,31 @@ qemuDomainSetPrivatePathsOld(virQEMUDriverPtr driver, } -/* - * The newer version uses a magic number for one reason. The thing is - * that we need a bit shorter name in order to be able to connect to - * it using UNIX sockets which have path length limitation. Since the - * length is not guaranteed to be constant and similarly the lib - * directory is configurable and so on, we need to rather choose an - * arbitrary maximum length of the domain name that will be used. - * Thanks to the fact that we are now saving it in the status XML, we - * can change it later on whenever we feel like so. - */ int qemuDomainSetPrivatePaths(virQEMUDriverPtr driver, virDomainObjPtr vm) { virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver); qemuDomainObjPrivatePtr priv = vm->privateData; - const int dommaxlen = 20; + char *domname = virDomainObjGetShortName(vm); int ret = -1; + if (!domname) + goto cleanup; + if (!priv->libDir && - virAsprintf(&priv->libDir, "%s/domain-%d-%.*s", - cfg->libDir, vm->def->id, dommaxlen, vm->def->name) < 0) + virAsprintf(&priv->libDir, "%s/domain-%s", cfg->libDir, domname) < 0) goto cleanup; if (!priv->channelTargetDir && - virAsprintf(&priv->channelTargetDir, "%s/domain-%d-%.*s", - cfg->channelTargetDir, vm->def->id, - dommaxlen, vm->def->name) < 0) + virAsprintf(&priv->channelTargetDir, "%s/domain-%s", + cfg->channelTargetDir, domname) < 0) goto cleanup; ret = 0; cleanup: virObjectUnref(cfg); + VIR_FREE(domname); return ret; }