Skip to content

Commit

Permalink
cgroup: try creating a temporary directory after mounting `/sys/fs/cg…
Browse files Browse the repository at this point in the history
…roup/unified`

It's possible for `systemd` inside an unprivileged user namespace container
to be able to mount `cgroup2` on `/sys/fs/cgroup/unified` without being able
to create directories there.  When this happens, `systemd` fails to boot, making
it impossible to reexecute itself without restarting the container runtime.

In this patch the issue is avoided by trying creating a temporary directory
after mounting `cgroup2` and falling back to `v1` if `mkdir` fails.

Closes systemd#6408 and lxc/lxc#1678.
  • Loading branch information
evverx committed Nov 21, 2017
1 parent d8bd96a commit ce7b4d9
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/core/mount-setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "cgroup-util.h"
#include "dev-setup.h"
#include "efivars.h"
#include "fileio.h"
#include "fs-util.h"
#include "label.h"
#include "log.h"
Expand All @@ -49,6 +50,7 @@ typedef enum MountMode {
MNT_NONE = 0,
MNT_FATAL = 1 << 0,
MNT_IN_CONTAINER = 1 << 1,
MNT_CHECK_MKDIR = 1 << 2,
} MountMode;

typedef struct MountPoint {
Expand Down Expand Up @@ -103,9 +105,9 @@ static const MountPoint mount_table[] = {
{ "tmpfs", "/sys/fs/cgroup", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME,
cg_is_legacy_wanted, MNT_FATAL|MNT_IN_CONTAINER },
{ "cgroup", "/sys/fs/cgroup/unified", "cgroup2", "nsdelegate", MS_NOSUID|MS_NOEXEC|MS_NODEV,
cg_is_hybrid_wanted, MNT_IN_CONTAINER },
cg_is_hybrid_wanted, MNT_IN_CONTAINER|MNT_CHECK_MKDIR },
{ "cgroup", "/sys/fs/cgroup/unified", "cgroup2", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
cg_is_hybrid_wanted, MNT_IN_CONTAINER },
cg_is_hybrid_wanted, MNT_IN_CONTAINER|MNT_CHECK_MKDIR },
{ "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd,xattr", MS_NOSUID|MS_NOEXEC|MS_NODEV,
cg_is_legacy_wanted, MNT_IN_CONTAINER },
{ "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV,
Expand Down Expand Up @@ -202,6 +204,16 @@ static int mount_one(const MountPoint *p, bool relabel) {
if (relabel)
(void) label_fix(p->where, false, false);

if (p->mode & MNT_CHECK_MKDIR) {
_cleanup_(rmdir_and_freep) char *tempdir = NULL;

r = mkdtemp_malloc(strjoina(p->where, "/systemd-temporary-XXXXXX"), &tempdir);
if (r < 0) {
(void) umount(p->where);
return (p->mode & MNT_FATAL) ? r : 0;
}
}

return 1;
}

Expand Down

0 comments on commit ce7b4d9

Please sign in to comment.