From cf68314dd14e23459d04cf7a185fb4697866791a Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 20 May 2017 20:26:36 -0400 Subject: nspawn: Clarify detect_unified_cgroup_hierarchy() --- src/basic/cgroup-util.c | 11 +++++++++++ src/basic/cgroup-util.h | 1 + src/nspawn/nspawn.c | 39 ++++++++++++++++++++++++--------------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index cb38f0431d..5bf68105f2 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/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/basic/cgroup-util.h b/src/basic/cgroup-util.h index 0aa27c4cd7..d730f3490c 100644 --- a/src/basic/cgroup-util.h +++ b/src/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/nspawn/nspawn.c b/src/nspawn/nspawn.c index 6aee8bb740..570b170b16 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -322,7 +322,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"); @@ -338,17 +339,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) - return log_error_errno(all_unified < 0 ? all_unified : systemd_unified, - "Failed to determine whether the unified cgroups hierarchy is used: %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. */ + r = cg_version(&outer); + if (r < 0) + return log_error_errno(r, "Failed to determine whether the unified cgroups hierarchy is used: %m"); + + /* 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-sd */ + 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 determine systemd version in container: %m"); @@ -356,7 +364,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-sd232 */ /* Mixed cgroup hierarchy support was added in 232 */ r = systemd_installation_has_version(directory, 232); if (r < 0) @@ -365,8 +374,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