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 <mkletzan@redhat.com>
This commit is contained in:
Martin Kletzander 2016-04-26 08:43:40 +02:00
parent d3d4fb4b18
commit a042275a39
4 changed files with 31 additions and 16 deletions

View File

@ -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;
}

View File

@ -3162,4 +3162,6 @@ int virDomainDefGetVcpuPinInfoHelper(virDomainDefPtr def,
bool virDomainDefHasMemballoon(const virDomainDef *def) ATTRIBUTE_NONNULL(1);
char *virDomainObjGetShortName(virDomainObjPtr vm);
#endif /* __DOMAIN_CONF_H */

View File

@ -400,6 +400,7 @@ virDomainObjGetDefs;
virDomainObjGetMetadata;
virDomainObjGetOneDef;
virDomainObjGetPersistentDef;
virDomainObjGetShortName;
virDomainObjGetState;
virDomainObjNew;
virDomainObjParseNode;

View File

@ -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;
}