diff options
author | Luca Boccassi <luca.boccassi@gmail.com> | 2024-11-21 01:47:39 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-11-23 00:14:20 +0100 |
commit | 193bf42ab0721384570bb6913e5f38385a9105e7 (patch) | |
tree | 2dfc120887550aa11e149d709caf8576c35032ef /src/basic | |
parent | namespace-util: add generic namespace_is_init() call (diff) | |
download | systemd-193bf42ab0721384570bb6913e5f38385a9105e7.tar.xz systemd-193bf42ab0721384570bb6913e5f38385a9105e7.zip |
detect-virt: check the inode number of the pid namespace
The indoe number of root pid namespace is hardcoded in the kernel to
0xEFFFFFFC since 3.8, so check the inode number of our pid namespace
if all else fails. If it's not 0xEFFFFFFC then we are in a pid
namespace, hence a container environment.
Fixes https://github.com/systemd/systemd/issues/35249
[Reworked by Lennart, to make use of namespace_is_init()]
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/virt.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/basic/virt.c b/src/basic/virt.c index fd0c353791..7792d64f16 100644 --- a/src/basic/virt.c +++ b/src/basic/virt.c @@ -645,6 +645,16 @@ static int running_in_cgroupns(void) { } } +static int running_in_pidns(void) { + int r; + + r = namespace_is_init(NAMESPACE_PID); + if (r < 0) + return log_debug_errno(r, "Failed to test if in root PID namespace, ignoring: %m"); + + return !r; +} + static Virtualization detect_container_files(void) { static const struct { const char *file_path; @@ -790,12 +800,21 @@ check_files: r = running_in_cgroupns(); if (r > 0) { + log_debug("Running in a cgroup namespace, assuming unknown container manager."); v = VIRTUALIZATION_CONTAINER_OTHER; goto finish; } if (r < 0) log_debug_errno(r, "Failed to detect cgroup namespace: %m"); + /* Finally, the root pid namespace has an hardcoded inode number of 0xEFFFFFFC since kernel 3.8, so + * if all else fails we can check the inode number of our pid namespace and compare it. */ + if (running_in_pidns() > 0) { + log_debug("Running in a pid namespace, assuming unknown container manager."); + v = VIRTUALIZATION_CONTAINER_OTHER; + goto finish; + } + /* If none of that worked, give up, assume no container manager. */ v = VIRTUALIZATION_NONE; goto finish; |