diff --git a/src/core/main.c b/src/core/main.c index b294313d870..dc93c42f175 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -3302,6 +3302,23 @@ int main(int argc, char *argv[]) { log_execution_mode(&first_boot); + r = cg_has_legacy(); + if (r < 0) { + error_message = "Failed to check cgroup hierarchy"; + goto finish; + } + if (r > 0) { + r = log_full_errno(LOG_EMERG, SYNTHETIC_ERRNO(EPROTO), + "Detected cgroup v1 hierarchy at /sys/fs/cgroup/, which is no longer supported by current version of systemd.\n" + "Please instruct your initrd to mount cgroup v2 (unified) hierarchy,\n" + "possibly by removing any stale kernel command line options, such as:\n" + " systemd.legacy_systemd_cgroup_controller=1\n" + " systemd.unified_cgroup_hierarchy=0"); + + error_message = "Detected unsupported legacy cgroup hierarchy, refusing execution"; + goto finish; + } + r = initialize_runtime(skip_setup, first_boot, &saved_rlimit_nofile, diff --git a/src/shared/cgroup-setup.c b/src/shared/cgroup-setup.c index b94d0b00a66..0ca630b05a6 100644 --- a/src/shared/cgroup-setup.c +++ b/src/shared/cgroup-setup.c @@ -8,6 +8,7 @@ #include "fd-util.h" #include "fileio.h" #include "fs-util.h" +#include "missing_magic.h" #include "mkdir.h" #include "parse-util.h" #include "path-util.h" @@ -951,3 +952,29 @@ int cg_uninstall_release_agent(const char *controller) { return 0; } + +int cg_has_legacy(void) { + struct statfs fs; + + /* Checks if any legacy controller/hierarchy is mounted. */ + + if (statfs("/sys/fs/cgroup/", &fs) < 0) { + if (errno == ENOENT) /* sysfs not mounted? */ + return false; + + return log_debug_errno(errno, "Failed to statfs /sys/fs/cgroup/: %m"); + } + + if (is_fs_type(&fs, CGROUP2_SUPER_MAGIC) || + is_fs_type(&fs, SYSFS_MAGIC)) /* not mounted yet */ + return false; + + if (is_fs_type(&fs, TMPFS_MAGIC)) { + log_debug("Found tmpfs on /sys/fs/cgroup/, assuming legacy hierarchy."); + return true; + } + + return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM), + "Unknown filesystem type %llx mounted on /sys/fs/cgroup/.", + (unsigned long long) fs.f_type); +} diff --git a/src/shared/cgroup-setup.h b/src/shared/cgroup-setup.h index 6eecb9be101..7f0a8c73bb2 100644 --- a/src/shared/cgroup-setup.h +++ b/src/shared/cgroup-setup.h @@ -40,3 +40,5 @@ int cg_trim_v1_controllers(CGroupMask supported, CGroupMask mask, const char *pa int cg_install_release_agent(const char *controller, const char *agent); int cg_uninstall_release_agent(const char *controller); + +int cg_has_legacy(void);