return 0;
}
+
+#define HOSTNAME_CHARS \
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
+
+static void
+virDomainMachineNameAppendValid(virBufferPtr buf,
+ const char *name)
+{
+ bool skip_dot = false;
+
+ for (; *name; name++) {
+ if (virBufferError(buf))
+ break;
+ if (strlen(virBufferCurrentContent(buf)) >= 64)
+ break;
+
+ if (*name == '.') {
+ if (!skip_dot)
+ virBufferAddChar(buf, *name);
+ skip_dot = true;
+ continue;
+ }
+
+ skip_dot = false;
+
+ if (!strchr(HOSTNAME_CHARS, *name))
+ continue;
+
+ virBufferAddChar(buf, *name);
+ }
+}
+
+#undef HOSTNAME_CHARS
+
+char *
+virDomainGenerateMachineName(const char *drivername,
+ int id,
+ const char *name,
+ bool privileged)
+{
+ char *machinename = NULL;
+ char *username = NULL;
+ virBuffer buf = VIR_BUFFER_INITIALIZER;
+
+ if (privileged) {
+ virBufferAsprintf(&buf, "%s-", drivername);
+ } else {
+ if (!(username = virGetUserName(geteuid())))
+ goto cleanup;
+
+ virBufferAsprintf(&buf, "%s-%s-", username, drivername);
+ }
+
+ virBufferAsprintf(&buf, "%d-", id);
+ virDomainMachineNameAppendValid(&buf, name);
+
+ machinename = virBufferContentAndReset(&buf);
+ cleanup:
+ VIR_FREE(username);
+
+ return machinename;
+}
int virDomainDiskSetBlockIOTune(virDomainDiskDefPtr disk,
virDomainBlockIoTuneInfo *info);
+char *
+virDomainGenerateMachineName(const char *drivername,
+ int id,
+ const char *name,
+ bool privileged);
#endif /* __DOMAIN_CONF_H */
virDomainFSTypeToString;
virDomainFSWrpolicyTypeFromString;
virDomainFSWrpolicyTypeToString;
+virDomainGenerateMachineName;
virDomainGetFilesystemForTarget;
virDomainGraphicsAuthConnectedTypeFromString;
virDomainGraphicsAuthConnectedTypeToString;
virSystemdCreateMachine;
virSystemdGetMachineNameByPID;
virSystemdHasMachinedResetCachedValue;
-virSystemdMakeMachineName;
virSystemdMakeScopeName;
virSystemdMakeSliceName;
virSystemdNotifyStartup;
int *nicindexes)
{
virCgroupPtr cgroup = NULL;
- char *machineName = virSystemdMakeMachineName("lxc",
- def->id,
- def->name,
- true);
+ char *machineName = virLXCDomainGetMachineName(def, 0);
if (!machineName)
goto cleanup;
#include "virutil.h"
#include "virfile.h"
#include "virtime.h"
+#include "virsystemd.h"
#define VIR_FROM_THIS VIR_FROM_LXC
#define LXC_NAMESPACE_HREF "http://libvirt.org/schemas/domain/lxc/1.0"
.domainPostParseCallback = virLXCDomainDefPostParse,
.devicesPostParseCallback = virLXCDomainDeviceDefPostParse,
};
+
+
+char *
+virLXCDomainGetMachineName(virDomainDefPtr def, pid_t pid)
+{
+ char *ret = NULL;
+
+ if (pid) {
+ ret = virSystemdGetMachineNameByPID(pid);
+ if (!ret)
+ virResetLastError();
+ }
+
+ if (!ret)
+ ret = virDomainGenerateMachineName("lxc", def->id, def->name, true);
+
+ return ret;
+}
virDomainObjPtr obj);
+char *
+virLXCDomainGetMachineName(virDomainDefPtr def, pid_t pid);
+
#endif /* __LXC_DOMAIN_H__ */
* the bug we are working around here.
*/
virCgroupTerminateMachine(priv->machineName);
+ VIR_FREE(priv->machineName);
/* The "release" hook cleans up additional resources */
if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
goto cleanup;
}
+ priv->machineName = virLXCDomainGetMachineName(vm->def, vm->pid);
+ if (!priv->machineName)
+ goto cleanup;
+
/* We know the cgroup must exist by this synchronization
* point so lets detect that first, since it gives us a
* more reliable way to kill everything off if something
* goes wrong from here onwards ... */
if (virCgroupNewDetectMachine(vm->def->name, "lxc",
- vm->def->id, true,
- vm->pid, -1, &priv->cgroup) < 0)
+ vm->pid, -1, priv->machineName,
+ &priv->cgroup) < 0)
goto cleanup;
if (!priv->cgroup) {
goto cleanup;
}
- /* Get the machine name so we can properly delete it through
- * systemd later */
- if (!(priv->machineName = virSystemdGetMachineNameByPID(vm->pid)))
- virResetLastError();
-
/* And we can get the first monitor connection now too */
if (!(priv->monitor = virLXCProcessConnectMonitor(driver, vm))) {
/* Intentionally overwrite the real monitor error message,
if (!(priv->monitor = virLXCProcessConnectMonitor(driver, vm)))
goto error;
- if (virCgroupNewDetectMachine(vm->def->name, "lxc", vm->def->id, true,
- vm->pid, -1, &priv->cgroup) < 0)
+ priv->machineName = virLXCDomainGetMachineName(vm->def, vm->pid);
+ if (!priv->machineName)
+ goto cleanup;
+
+ if (virCgroupNewDetectMachine(vm->def->name, "lxc", vm->pid, -1,
+ priv->machineName, &priv->cgroup) < 0)
goto error;
if (!priv->cgroup) {
goto error;
}
- if (!(priv->machineName = virSystemdGetMachineNameByPID(vm->pid)))
- virResetLastError();
-
if (virLXCUpdateActiveUSBHostdevs(driver, vm->def) < 0)
goto error;
goto cleanup;
}
- /*
- * We need to do this because of systemd-machined, because
- * CreateMachine requires the name to be a valid hostname.
- */
- priv->machineName = virSystemdMakeMachineName("qemu",
- vm->def->id,
- vm->def->name,
- virQEMUDriverIsPrivileged(driver));
- if (!priv->machineName)
- goto cleanup;
-
if (virCgroupNewMachine(priv->machineName,
"qemu",
vm->def->uuid,
if (!virCgroupAvailable())
goto done;
+ priv->machineName = qemuDomainGetMachineName(vm);
+ if (!priv->machineName)
+ goto cleanup;
+
virCgroupFree(&priv->cgroup);
if (virCgroupNewDetectMachine(vm->def->name,
"qemu",
- vm->def->id,
- virQEMUDriverIsPrivileged(driver),
vm->pid,
cfg->cgroupControllers,
+ priv->machineName,
&priv->cgroup) < 0)
goto cleanup;
- priv->machineName = virSystemdGetMachineNameByPID(vm->pid);
- if (!priv->machineName)
- virResetLastError();
-
qemuRestoreCgroupState(vm);
done:
VIR_DEBUG("Failed to terminate cgroup for %s", vm->def->name);
}
- VIR_FREE(priv->machineName);
-
return virCgroupRemove(priv->cgroup);
}
#include "viratomic.h"
#include "virprocess.h"
#include "vircrypto.h"
+#include "virsystemd.h"
#include "secret_util.h"
#include "logging/log_manager.h"
#include "locking/domain_lock.h"
return 0;
}
+
+char *
+qemuDomainGetMachineName(virDomainObjPtr vm)
+{
+ qemuDomainObjPrivatePtr priv = vm->privateData;
+ virQEMUDriverPtr driver = priv->driver;
+ char *ret = NULL;
+
+ if (vm->pid) {
+ ret = virSystemdGetMachineNameByPID(vm->pid);
+ if (!ret)
+ virResetLastError();
+ }
+
+ if (!ret)
+ ret = virDomainGenerateMachineName("qemu", vm->def->id, vm->def->name,
+ virQEMUDriverIsPrivileged(driver));
+
+ return ret;
+}
virCPUDefPtr cpu,
virCPUDefPtr *origCPU);
+char *
+qemuDomainGetMachineName(virDomainObjPtr vm);
+
#endif /* __QEMU_DOMAIN_H__ */
if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
goto cleanup;
+ priv->machineName = qemuDomainGetMachineName(vm);
+ if (!priv->machineName)
+ goto cleanup;
+
if (!(flags & VIR_QEMU_PROCESS_START_PRETEND)) {
/* If you are using a SecurityDriver with dynamic labelling,
then generate a security label for isolation */
}
}
+ VIR_FREE(priv->machineName);
+
vm->taint = 0;
vm->pid = -1;
virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason);
virCgroupValidateMachineGroup(virCgroupPtr group,
const char *name,
const char *drivername,
- int id,
- bool privileged,
- bool stripEmulatorSuffix)
+ bool stripEmulatorSuffix,
+ char *machinename)
{
size_t i;
bool valid = false;
char *partname = NULL;
char *scopename_old = NULL;
char *scopename_new = NULL;
- char *machinename = virSystemdMakeMachineName(drivername, id,
- name, privileged);
char *partmachinename = NULL;
if (virAsprintf(&partname, "%s.libvirt-%s",
int
virCgroupNewDetectMachine(const char *name,
const char *drivername,
- int id,
- bool privileged,
pid_t pid,
int controllers,
+ char *machinename,
virCgroupPtr *group)
{
if (virCgroupNewDetect(pid, controllers, group) < 0) {
}
if (!virCgroupValidateMachineGroup(*group, name, drivername,
- id, privileged, true)) {
+ true, machinename)) {
VIR_DEBUG("Failed to validate machine name for '%s' driver '%s'",
name, drivername);
virCgroupFree(group);
int
virCgroupNewDetectMachine(const char *name ATTRIBUTE_UNUSED,
const char *drivername ATTRIBUTE_UNUSED,
- int id ATTRIBUTE_UNUSED,
- bool privileged ATTRIBUTE_UNUSED,
pid_t pid ATTRIBUTE_UNUSED,
int controllers ATTRIBUTE_UNUSED,
+ char *machinename ATTRIBUTE_UNUSED,
virCgroupPtr *group ATTRIBUTE_UNUSED)
{
virReportSystemError(ENXIO, "%s",
int controllers,
virCgroupPtr *group);
-int virCgroupNewDetectMachine(const char *name,
- const char *drivername,
- int id,
- bool privileged,
- pid_t pid,
- int controllers,
- virCgroupPtr *group)
+int
+virCgroupNewDetectMachine(const char *name,
+ const char *drivername,
+ pid_t pid,
+ int controllers,
+ char *machinename,
+ virCgroupPtr *group)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
int virCgroupNewMachine(const char *name,
return virBufferContentAndReset(&buf);
}
-#define HOSTNAME_CHARS \
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
-
-static void
-virSystemdAppendValidMachineName(virBufferPtr buf,
- const char *name)
-{
- bool skip_dot = false;
-
- for (; *name; name++) {
- if (virBufferError(buf))
- break;
- if (strlen(virBufferCurrentContent(buf)) >= 64)
- break;
-
- if (*name == '.') {
- if (!skip_dot)
- virBufferAddChar(buf, *name);
- skip_dot = true;
- continue;
- }
-
- skip_dot = false;
-
- if (!strchr(HOSTNAME_CHARS, *name))
- continue;
-
- virBufferAddChar(buf, *name);
- }
-}
-
-#undef HOSTNAME_CHARS
-
-char *
-virSystemdMakeMachineName(const char *drivername,
- int id,
- const char *name,
- bool privileged)
-{
- char *machinename = NULL;
- char *username = NULL;
- virBuffer buf = VIR_BUFFER_INITIALIZER;
-
- if (privileged) {
- virBufferAsprintf(&buf, "%s-", drivername);
- } else {
- if (!(username = virGetUserName(geteuid())))
- goto cleanup;
-
- virBufferAsprintf(&buf, "%s-%s-", username, drivername);
- }
-
- virBufferAsprintf(&buf, "%d-", id);
- virSystemdAppendValidMachineName(&buf, name);
-
- machinename = virBufferContentAndReset(&buf);
- cleanup:
- VIR_FREE(username);
-
- return machinename;
-}
-
static int virSystemdHasMachinedCachedValue = -1;
/* Reset the cache from tests for testing the underlying dbus calls
bool legacy_behaviour);
char *virSystemdMakeSliceName(const char *partition);
-char *virSystemdMakeMachineName(const char *drivername,
- int id,
- const char *name,
- bool privileged);
-
int virSystemdCreateMachine(const char *name,
const char *drivername,
const unsigned char *uuid,
int ret = -1;
char *actual = NULL;
- if (!(actual = virSystemdMakeMachineName("qemu", data->id,
- data->name, true)))
+ if (!(actual = virDomainGenerateMachineName("qemu", data->id,
+ data->name, true)))
goto cleanup;
if (STRNEQ(actual, data->expected)) {