]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
nspawn: Assume unified cgroup hierarchy if there's no systemd in the image
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 13 Aug 2024 09:59:51 +0000 (11:59 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 13 Aug 2024 17:09:26 +0000 (19:09 +0200)
If there's no systemd installation in the image, assume the unified
cgroup hierarchy.

NEWS
src/nspawn/nspawn-util.c
src/nspawn/nspawn.c
src/nspawn/test-nspawn-util.c

diff --git a/NEWS b/NEWS
index e772f08c2bbbb3f2955eb3cbb40b9bd6c540844d..947b831001d13ebf58962ea7237aca24e0e2e5cc 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -42,6 +42,11 @@ CHANGES WITH 257 in spe:
           and 'block-weak' inhibitor modes were added, if taken they will make
           the inhibitor lock work as in the previous versions.
 
+        * systemd-nspawn will now mount the unified cgroup hierarchy into a
+          container if no systemd installation is found in a container's root
+          filesystem. `$SYSTEMD_NSPAWN_UNIFIED_HIERARCHY=0` can be used to override
+          this behavior.
+
         — <place>, <date>
 
 CHANGES WITH 256:
index 6c3848916d2ec3cc96eb489bae743a52aaf2f63b..2cd73fe87c95e7a1261a78957e3f64d54d7ddc38 100644 (file)
@@ -9,6 +9,7 @@
 #include "string-util.h"
 
 int systemd_installation_has_version(const char *root, const char *minimal_version) {
+        bool found = false;
         int r;
 
         /* Try to guess if systemd installation is later than the specified version. This
@@ -63,6 +64,8 @@ int systemd_installation_has_version(const char *root, const char *minimal_versi
                                 continue;
                         *t2 = '\0';
 
+                        found = true;
+
                         r = strverscmp_improved(t, minimal_version);
                         log_debug("Found libsystemd shared at \"%s.so\", version %s (%s).",
                                   *name, t,
@@ -72,5 +75,5 @@ int systemd_installation_has_version(const char *root, const char *minimal_versi
                 }
         }
 
-        return false;
+        return !found ? -ENOENT : false;
 }
index e7d96821ea3b31336d4506d3a09c06eae5bc4f72..d11868844878e6d400c8dabab364448eecc8d74f 100644 (file)
@@ -530,23 +530,25 @@ static int detect_unified_cgroup_hierarchy_from_image(const char *directory) {
                 return log_error_errno(r, "Failed to determine whether we are in all unified mode.");
         if (r > 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. */
+                 * routine only detects 231, so we'll have a false negative here for 230. If there is no
+                 * systemd installation in the container, we use the unified cgroup hierarchy. */
                 r = systemd_installation_has_version(directory, "230");
-                if (r < 0)
+                if (r < 0 && r != -ENOENT)
                         return log_error_errno(r, "Failed to determine systemd version in container: %m");
-                if (r > 0)
-                        arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
-                else
+                if (r == 0)
                         arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
+                else
+                        arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
         } else if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0) {
-                /* Mixed cgroup hierarchy support was added in 233 */
+                /* Mixed cgroup hierarchy support was added in 233. If there is no systemd installation in
+                 * the container, we use the unified cgroup hierarchy. */
                 r = systemd_installation_has_version(directory, "233");
-                if (r < 0)
+                if (r < 0 && r != -ENOENT)
                         return log_error_errno(r, "Failed to determine systemd version in container: %m");
-                if (r > 0)
-                        arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_SYSTEMD;
-                else
+                if (r == 0)
                         arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
+                else
+                        arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_SYSTEMD;
         } else
                 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
 
index 533edde5aaa0bd21d233f8eab455d44a8b3ee478..45fc8ad927afcb0571145f0cadb547496f956669 100644 (file)
@@ -1,19 +1,28 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include "nspawn-util.h"
-#include "string-util.h"
+#include "rm-rf.h"
 #include "strv.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 
 TEST(systemd_installation_has_version) {
         int r;
 
         FOREACH_STRING(version, "0", "231", PROJECT_VERSION_FULL, "999") {
                 r = systemd_installation_has_version(saved_argv[1], version);
-                assert_se(r >= 0);
+                /* The build environment may not have a systemd installation. */
+                if (r == -ENOENT)
+                        continue;
+                ASSERT_OK(r);
                 log_info("%s has systemd >= %s: %s",
                          saved_argv[1] ?: "Current installation", version, yes_no(r));
         }
+
+        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
+        ASSERT_OK(mkdtemp_malloc(NULL, &t));
+
+        ASSERT_ERROR(systemd_installation_has_version(t, PROJECT_VERSION_FULL), ENOENT);
 }
 
 /* This program can be called with a path to an installation root.