1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-24 06:04:05 +03:00

nspawn: Assume unified cgroup hierarchy if there's no systemd in the image

If there's no systemd installation in the image, assume the unified
cgroup hierarchy.
This commit is contained in:
Daan De Meyer 2024-08-13 11:59:51 +02:00
parent 74cc5e2041
commit d89ee0fcf9
4 changed files with 32 additions and 13 deletions

5
NEWS
View File

@ -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:

View 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;
}

View 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)
if (r == 0)
arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
else
arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
else
arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
} 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;

View 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.