From 6e20ab45f9c40a6d59b29741baad5c068df6edc3 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 20 May 2017 20:26:36 -0400 Subject: systemd-nspawn: Clarify detect_unified_cgroup_hierarchy(). --- .../include/systemd-basic/cgroup-util.h | 1 + src/libsystemd-basic/src/cgroup-util.c | 11 +++++++ src/systemd-nspawn/nspawn.c | 34 ++++++++++++++-------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/libsystemd-basic/include/systemd-basic/cgroup-util.h b/src/libsystemd-basic/include/systemd-basic/cgroup-util.h index 0aa27c4cd7..d730f3490c 100644 --- a/src/libsystemd-basic/include/systemd-basic/cgroup-util.h +++ b/src/libsystemd-basic/include/systemd-basic/cgroup-util.h @@ -242,6 +242,7 @@ bool cg_ns_supported(void); int cg_all_unified(void); int cg_unified(const char *controller); +int cg_version(CGroupUnified *ver); void cg_unified_flush(void); bool cg_is_unified_wanted(void); diff --git a/src/libsystemd-basic/src/cgroup-util.c b/src/libsystemd-basic/src/cgroup-util.c index cf9c682599..929101e558 100644 --- a/src/libsystemd-basic/src/cgroup-util.c +++ b/src/libsystemd-basic/src/cgroup-util.c @@ -2311,6 +2311,17 @@ int cg_all_unified(void) { return cg_unified(NULL); } +int cg_version(CGroupUnified *ver) { + int r; + + r = cg_update_unified(); + if (r < 0) + return r; + + *ver = unified_cache; + return 0; +} + void cg_unified_flush(void) { unified_cache = CGROUP_UNIFIED_UNKNOWN; } diff --git a/src/systemd-nspawn/nspawn.c b/src/systemd-nspawn/nspawn.c index 5f5e21c0f4..7c9b32fbc2 100644 --- a/src/systemd-nspawn/nspawn.c +++ b/src/systemd-nspawn/nspawn.c @@ -323,7 +323,8 @@ static int custom_mounts_prepare(void) { static int detect_unified_cgroup_hierarchy(const char *directory) { const char *e; - int r, all_unified, systemd_unified; + int r; + CGroupUnified outer; /* Allow the user to control whether the unified hierarchy is used */ e = getenv("UNIFIED_CGROUP_HIERARCHY"); @@ -339,16 +340,24 @@ static int detect_unified_cgroup_hierarchy(const char *directory) { return 0; } - all_unified = cg_all_unified(); - systemd_unified = cg_unified(SYSTEMD_CGROUP_CONTROLLER); - - if (all_unified < 0 || systemd_unified < 0) + r = cg_version(&outer); + if (r < 0) return log_error_errno(r, "Failed to decide cgroup version to use: Failed to determine what the host system uses: %m"); - /* Otherwise inherit the default from the host system */ - if (all_unified > 0) { - /* Unified cgroup hierarchy support was added in 230. Unfortunately the detection - * routine only detects 231, so we'll have a false negative here for 230. */ + /* Otherwise inherit the default from the host system, unless + * the container doesn't have a new enough systemd (detected + * by checking libsystemd-shared). */ + switch (outer) { + case CGROUP_UNIFIED_UNKNOWN: + assert_not_reached("Unknown host cgroup version"); + break; + case CGROUP_UNIFIED_NONE: /* cgroup v1 */ + arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE; + break; + case CGROUP_UNIFIED_ALL: /* cgroup v2 */ + /* Unified cgroup hierarchy support was added in 230. Unfortunately libsystemd-shared, + * which we use to sniff the systemd version, was only added in 231, so we'll have a + * false negative here for 230. */ r = systemd_installation_has_version(directory, 230); if (r < 0) return log_error_errno(r, "Failed to decide cgroup version to use: Failed to determine systemd version in container: %m"); @@ -356,7 +365,8 @@ static int detect_unified_cgroup_hierarchy(const char *directory) { arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL; else arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE; - } else if (systemd_unified > 0) { + break; + case CGROUP_UNIFIED_SYSTEMD: /* cgroup v1 & v2 mixed; but v2 for systemd */ /* Mixed cgroup hierarchy support was added in 232 */ r = systemd_installation_has_version(directory, 232); if (r < 0) @@ -365,8 +375,8 @@ static int detect_unified_cgroup_hierarchy(const char *directory) { arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_SYSTEMD; else arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE; - } else - arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE; + break; + } return 0; } -- cgit v1.2.3-54-g00ecf