conf: Move virDomainGenerateMachineName to hypervisor/

The virDomainGenerateMachineName() function doesn't belong in
src/conf/ really, because it has nothing to do with domain XML
parsing. It landed there because of lack of better place in the
past. But now that we have src/hypervisor/ the function should
live there. At the same time, the function name is changed to
match new location.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Michal Privoznik 2020-03-20 18:14:22 +01:00
parent 34a59fb570
commit 8b9488d929
8 changed files with 91 additions and 86 deletions

View File

@ -62,7 +62,6 @@
#include "virdomainsnapshotobjlist.h"
#include "virdomaincheckpointobjlist.h"
#include "virutil.h"
#include "vircrypto.h"
#define VIR_FROM_THIS VIR_FROM_DOMAIN
@ -31066,77 +31065,6 @@ virDomainDiskSetBlockIOTune(virDomainDiskDefPtr disk,
return 0;
}
#define HOSTNAME_CHARS \
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
static void
virDomainMachineNameAppendValid(virBufferPtr buf,
const char *name)
{
bool skip = true;
for (; *name; name++) {
if (strlen(virBufferCurrentContent(buf)) >= 64)
break;
if (*name == '.' || *name == '-') {
if (!skip)
virBufferAddChar(buf, *name);
skip = true;
continue;
}
skip = false;
if (!strchr(HOSTNAME_CHARS, *name))
continue;
virBufferAddChar(buf, *name);
}
/* trailing dashes or dots are not allowed */
virBufferTrimChars(buf, "-.");
}
#undef HOSTNAME_CHARS
char *
virDomainGenerateMachineName(const char *drivername,
const char *root,
int id,
const char *name,
bool privileged)
{
virBuffer buf = VIR_BUFFER_INITIALIZER;
virBufferAsprintf(&buf, "%s-", drivername);
if (root) {
g_autofree char *hash = NULL;
/* When two embed drivers start two domains with the same @name and @id
* we would generate a non-unique name. Include parts of hashed @root
* which guarantees uniqueness. The first 8 characters of SHA256 ought
* to be enough for anybody. */
if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0)
return NULL;
virBufferAsprintf(&buf, "embed-%.8s-", hash);
} else if (!privileged) {
g_autofree char *username = NULL;
if (!(username = virGetUserName(geteuid()))) {
virBufferFreeAndReset(&buf);
return NULL;
}
virBufferAsprintf(&buf, "%s-", username);
}
virBufferAsprintf(&buf, "%d-", id);
virDomainMachineNameAppendValid(&buf, name);
return virBufferContentAndReset(&buf);
}
/**
* virDomainNetTypeSharesHostView:

View File

@ -3662,13 +3662,6 @@ virDomainGetBlkioParametersAssignFromDef(virDomainDefPtr def,
int virDomainDiskSetBlockIOTune(virDomainDiskDefPtr disk,
virDomainBlockIoTuneInfo *info);
char *
virDomainGenerateMachineName(const char *drivername,
const char *root,
int id,
const char *name,
bool privileged);
bool
virDomainNetTypeSharesHostView(const virDomainNetDef *net);

View File

@ -23,10 +23,84 @@
#include "domain_driver.h"
#include "viralloc.h"
#include "virstring.h"
#include "vircrypto.h"
#include "virutil.h"
#define VIR_FROM_THIS VIR_FROM_DOMAIN
#define HOSTNAME_CHARS \
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
static void
virDomainMachineNameAppendValid(virBufferPtr buf,
const char *name)
{
bool skip = true;
for (; *name; name++) {
if (strlen(virBufferCurrentContent(buf)) >= 64)
break;
if (*name == '.' || *name == '-') {
if (!skip)
virBufferAddChar(buf, *name);
skip = true;
continue;
}
skip = false;
if (!strchr(HOSTNAME_CHARS, *name))
continue;
virBufferAddChar(buf, *name);
}
/* trailing dashes or dots are not allowed */
virBufferTrimChars(buf, "-.");
}
#undef HOSTNAME_CHARS
char *
virDomainDriverGenerateMachineName(const char *drivername,
const char *root,
int id,
const char *name,
bool privileged)
{
virBuffer buf = VIR_BUFFER_INITIALIZER;
virBufferAsprintf(&buf, "%s-", drivername);
if (root) {
g_autofree char *hash = NULL;
/* When two embed drivers start two domains with the same @name and @id
* we would generate a non-unique name. Include parts of hashed @root
* which guarantees uniqueness. The first 8 characters of SHA256 ought
* to be enough for anybody. */
if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0)
return NULL;
virBufferAsprintf(&buf, "embed-%.8s-", hash);
} else if (!privileged) {
g_autofree char *username = NULL;
if (!(username = virGetUserName(geteuid()))) {
virBufferFreeAndReset(&buf);
return NULL;
}
virBufferAsprintf(&buf, "%s-", username);
}
virBufferAsprintf(&buf, "%d-", id);
virDomainMachineNameAppendValid(&buf, name);
return virBufferContentAndReset(&buf);
}
/* Modify dest_array to reflect all blkio device changes described in
* src_array. */
int

View File

@ -22,6 +22,13 @@
#include "domain_conf.h"
char *
virDomainDriverGenerateMachineName(const char *drivername,
const char *root,
int id,
const char *name,
bool privileged);
int virDomainDriverMergeBlkioDevice(virBlkioDevicePtr *dest_array,
size_t *dest_size,
virBlkioDevicePtr src_array,

View File

@ -406,7 +406,6 @@ virDomainFSTypeFromString;
virDomainFSTypeToString;
virDomainFSWrpolicyTypeFromString;
virDomainFSWrpolicyTypeToString;
virDomainGenerateMachineName;
virDomainGetBlkioParametersAssignFromDef;
virDomainGetFilesystemForTarget;
virDomainGraphicsAuthConnectedTypeFromString;
@ -1403,6 +1402,7 @@ virDomainCgroupSetupMemtune;
# hypervisor/domain_driver.h
virDomainDriverGenerateMachineName;
virDomainDriverMergeBlkioDevice;
virDomainDriverParseBlkioDeviceStr;
virDomainDriverSetupPersistentDefBlkioParams;

View File

@ -31,6 +31,7 @@
#include "virtime.h"
#include "virsystemd.h"
#include "virinitctl.h"
#include "domain_driver.h"
#define VIR_FROM_THIS VIR_FROM_LXC
@ -406,7 +407,7 @@ virLXCDomainGetMachineName(virDomainDefPtr def, pid_t pid)
}
if (!ret)
ret = virDomainGenerateMachineName("lxc", NULL, def->id, def->name, true);
ret = virDomainDriverGenerateMachineName("lxc", NULL, def->id, def->name, true);
return ret;
}

View File

@ -45,6 +45,7 @@
#include "virfile.h"
#include "domain_addr.h"
#include "domain_capabilities.h"
#include "domain_driver.h"
#include "domain_event.h"
#include "virtime.h"
#include "virnetdevopenvswitch.h"
@ -12989,9 +12990,9 @@ qemuDomainGetMachineName(virDomainObjPtr vm)
}
if (!ret)
ret = virDomainGenerateMachineName("qemu", cfg->root,
vm->def->id, vm->def->name,
driver->privileged);
ret = virDomainDriverGenerateMachineName("qemu", cfg->root,
vm->def->id, vm->def->name,
driver->privileged);
return ret;
}

View File

@ -34,6 +34,7 @@
# include "virlog.h"
# include "virmock.h"
# include "rpc/virnetsocket.h"
# include "domain_driver.h"
# define VIR_FROM_THIS VIR_FROM_NONE
VIR_LOG_INIT("tests.systemdtest");
@ -414,8 +415,8 @@ testMachineName(const void *opaque)
int ret = -1;
char *actual = NULL;
if (!(actual = virDomainGenerateMachineName("qemu", data->root,
data->id, data->name, true)))
if (!(actual = virDomainDriverGenerateMachineName("qemu", data->root,
data->id, data->name, true)))
goto cleanup;
if (STRNEQ(actual, data->expected)) {