]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/main: refuse bootup with legacy cgroup hierarchy
authorMike Yuan <me@yhndnzj.com>
Wed, 5 Mar 2025 17:55:41 +0000 (18:55 +0100)
committerMike Yuan <me@yhndnzj.com>
Sun, 16 Mar 2025 14:30:39 +0000 (15:30 +0100)
First step towards a unified-only future (rework of internals
coming soon (TM))

src/core/main.c
src/shared/cgroup-setup.c
src/shared/cgroup-setup.h

index b294313d8706f7c14e51b4f57a77c0d67579e770..dc93c42f1751591efee8c4ec49324d3f401357af 100644 (file)
@@ -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,
index b94d0b00a665094fbd2d493f1cec86b63b3cecf1..76658df41af14089fda85593e2b013d214d5324c 100644 (file)
@@ -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_error_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_info("Found tmpfs on /sys/fs/cgroup/, assuming legacy hierarchy.");
+                return true;
+        }
+
+        return log_error_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
+                               "Unknown filesystem type %llx mounted on /sys/fs/cgroup/.",
+                               (unsigned long long) fs.f_type);
+}
index 6eecb9be10118744f261b016413991c71e0863a2..7f0a8c73bb2b42672096ada5fc43d23036100cad 100644 (file)
@@ -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);