提交 bd773e74 编写于 作者: C Cédric Bosdonnat 提交者: Daniel P. Berrange

LXC: workaround machined uncleaned data with containers running systemd.

The problem is described by [0] but its effect on libvirt is that
starting a container with a full distro running systemd after having
stopped it simply fails.

The container cleanup now calls the machined Terminate function to make
sure that everything is in order for the next run.

 [0]: https://bugs.freedesktop.org/show_bug.cgi?id=68370
上级 edc61bf6
...@@ -1947,8 +1947,10 @@ virSysinfoSetup; ...@@ -1947,8 +1947,10 @@ virSysinfoSetup;
# util/virsystemd.h # util/virsystemd.h
virSystemdCreateMachine; virSystemdCreateMachine;
virSystemdMakeMachineName;
virSystemdMakeScopeName; virSystemdMakeScopeName;
virSystemdMakeSliceName; virSystemdMakeSliceName;
virSystemdTerminateMachine;
# util/virthread.h # util/virthread.h
......
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
#include "virstring.h" #include "virstring.h"
#include "viratomic.h" #include "viratomic.h"
#include "virprocess.h" #include "virprocess.h"
#include "virsystemd.h"
#define VIR_FROM_THIS VIR_FROM_LXC #define VIR_FROM_THIS VIR_FROM_LXC
...@@ -210,6 +211,13 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, ...@@ -210,6 +211,13 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
virCgroupFree(&priv->cgroup); virCgroupFree(&priv->cgroup);
} }
/* Get machined to terminate the machine as it may not have cleaned it
* properly. See https://bugs.freedesktop.org/show_bug.cgi?id=68370 for
* the bug we are working around here.
*/
virSystemdTerminateMachine(vm->def->name, "lxc", true);
/* now that we know it's stopped call the hook if present */ /* now that we know it's stopped call the hook if present */
if (virHookPresent(VIR_HOOK_DRIVER_LXC)) { if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
char *xml = virDomainDefFormat(vm->def, 0); char *xml = virDomainDefFormat(vm->def, 0);
......
...@@ -116,6 +116,27 @@ char *virSystemdMakeSliceName(const char *partition) ...@@ -116,6 +116,27 @@ char *virSystemdMakeSliceName(const char *partition)
return virBufferContentAndReset(&buf); return virBufferContentAndReset(&buf);
} }
char *virSystemdMakeMachineName(const char *name,
const char *drivername,
bool privileged)
{
char *machinename = NULL;
char *username = NULL;
if (privileged) {
if (virAsprintf(&machinename, "%s-%s", drivername, name) < 0)
goto cleanup;
} else {
if (!(username = virGetUserName(geteuid())))
goto cleanup;
if (virAsprintf(&machinename, "%s-%s-%s", username, drivername, name) < 0)
goto cleanup;
}
cleanup:
VIR_FREE(username);
return machinename;
}
/** /**
* virSystemdCreateMachine: * virSystemdCreateMachine:
...@@ -142,7 +163,6 @@ int virSystemdCreateMachine(const char *name, ...@@ -142,7 +163,6 @@ int virSystemdCreateMachine(const char *name,
DBusConnection *conn; DBusConnection *conn;
char *machinename = NULL; char *machinename = NULL;
char *creatorname = NULL; char *creatorname = NULL;
char *username = NULL;
char *slicename = NULL; char *slicename = NULL;
ret = virDBusIsServiceEnabled("org.freedesktop.machine1"); ret = virDBusIsServiceEnabled("org.freedesktop.machine1");
...@@ -152,15 +172,8 @@ int virSystemdCreateMachine(const char *name, ...@@ -152,15 +172,8 @@ int virSystemdCreateMachine(const char *name,
conn = virDBusGetSystemBus(); conn = virDBusGetSystemBus();
ret = -1; ret = -1;
if (privileged) { if (!(machinename = virSystemdMakeMachineName(name, drivername, privileged)))
if (virAsprintf(&machinename, "%s-%s", drivername, name) < 0) goto cleanup;
goto cleanup;
} else {
if (!(username = virGetUserName(geteuid())))
goto cleanup;
if (virAsprintf(&machinename, "%s-%s-%s", username, drivername, name) < 0)
goto cleanup;
}
if (virAsprintf(&creatorname, "libvirt-%s", drivername) < 0) if (virAsprintf(&creatorname, "libvirt-%s", drivername) < 0)
goto cleanup; goto cleanup;
...@@ -236,9 +249,54 @@ int virSystemdCreateMachine(const char *name, ...@@ -236,9 +249,54 @@ int virSystemdCreateMachine(const char *name,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(username);
VIR_FREE(creatorname); VIR_FREE(creatorname);
VIR_FREE(machinename); VIR_FREE(machinename);
VIR_FREE(slicename); VIR_FREE(slicename);
return ret; return ret;
} }
int virSystemdTerminateMachine(const char *name,
const char *drivername,
bool privileged)
{
int ret;
DBusConnection *conn;
char *machinename = NULL;
ret = virDBusIsServiceEnabled("org.freedesktop.machine1");
if (ret < 0)
return ret;
conn = virDBusGetSystemBus();
ret = -1;
if (!(machinename = virSystemdMakeMachineName(name, drivername, privileged)))
goto cleanup;
/*
* The systemd DBus API we're invoking has the
* following signature
*
* TerminateMachine(in s name);
*
* @name a host unique name for the machine. shows up
* in 'ps' listing & similar
*/
VIR_DEBUG("Attempting to terminate machine via systemd");
if (virDBusCallMethod(conn,
NULL,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"TerminateMachine",
"s",
machinename) < 0)
goto cleanup;
ret = 0;
cleanup:
VIR_FREE(machinename);
return ret;
}
...@@ -29,6 +29,10 @@ char *virSystemdMakeScopeName(const char *name, ...@@ -29,6 +29,10 @@ char *virSystemdMakeScopeName(const char *name,
const char *slicename); const char *slicename);
char *virSystemdMakeSliceName(const char *partition); char *virSystemdMakeSliceName(const char *partition);
char *virSystemdMakeMachineName(const char *name,
const char *drivername,
bool privileged);
int virSystemdCreateMachine(const char *name, int virSystemdCreateMachine(const char *name,
const char *drivername, const char *drivername,
bool privileged, bool privileged,
...@@ -38,4 +42,8 @@ int virSystemdCreateMachine(const char *name, ...@@ -38,4 +42,8 @@ int virSystemdCreateMachine(const char *name,
bool iscontainer, bool iscontainer,
const char *partition); const char *partition);
int virSystemdTerminateMachine(const char *name,
const char *drivername,
bool privileged);
#endif /* __VIR_SYSTEMD_H__ */ #endif /* __VIR_SYSTEMD_H__ */
...@@ -54,6 +54,18 @@ static int testCreateContainer(const void *opaque ATTRIBUTE_UNUSED) ...@@ -54,6 +54,18 @@ static int testCreateContainer(const void *opaque ATTRIBUTE_UNUSED)
return 0; return 0;
} }
static int testTerminateContainer(const void *opaque ATTRIBUTE_UNUSED)
{
if (virSystemdTerminateMachine("demo",
"lxc",
true) < 0) {
fprintf(stderr, "%s", "Failed to terminate LXC machine\n");
return -1;
}
return 0;
}
static int testCreateMachine(const void *opaque ATTRIBUTE_UNUSED) static int testCreateMachine(const void *opaque ATTRIBUTE_UNUSED)
{ {
unsigned char uuid[VIR_UUID_BUFLEN] = { unsigned char uuid[VIR_UUID_BUFLEN] = {
...@@ -77,6 +89,18 @@ static int testCreateMachine(const void *opaque ATTRIBUTE_UNUSED) ...@@ -77,6 +89,18 @@ static int testCreateMachine(const void *opaque ATTRIBUTE_UNUSED)
return 0; return 0;
} }
static int testTerminateMachine(const void *opaque ATTRIBUTE_UNUSED)
{
if (virSystemdTerminateMachine("demo",
"qemu",
false) < 0) {
fprintf(stderr, "%s", "Failed to terminate KVM machine\n");
return -1;
}
return 0;
}
static int testCreateNoSystemd(const void *opaque ATTRIBUTE_UNUSED) static int testCreateNoSystemd(const void *opaque ATTRIBUTE_UNUSED)
{ {
unsigned char uuid[VIR_UUID_BUFLEN] = { unsigned char uuid[VIR_UUID_BUFLEN] = {
...@@ -184,8 +208,12 @@ mymain(void) ...@@ -184,8 +208,12 @@ mymain(void)
if (virtTestRun("Test create container ", 1, testCreateContainer, NULL) < 0) if (virtTestRun("Test create container ", 1, testCreateContainer, NULL) < 0)
ret = -1; ret = -1;
if (virtTestRun("Test terminate container ", 1, testTerminateContainer, NULL) < 0)
ret = -1;
if (virtTestRun("Test create machine ", 1, testCreateMachine, NULL) < 0) if (virtTestRun("Test create machine ", 1, testCreateMachine, NULL) < 0)
ret = -1; ret = -1;
if (virtTestRun("Test terminate machine ", 1, testTerminateMachine, NULL) < 0)
ret = -1;
if (virtTestRun("Test create no systemd ", 1, testCreateNoSystemd, NULL) < 0) if (virtTestRun("Test create no systemd ", 1, testCreateNoSystemd, NULL) < 0)
ret = -1; ret = -1;
if (virtTestRun("Test create bad systemd ", 1, testCreateBadSystemd, NULL) < 0) if (virtTestRun("Test create bad systemd ", 1, testCreateBadSystemd, NULL) < 0)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册