]> git.proxmox.com Git - pve-container.git/commitdiff
fix #4192: revamp check for systemd version
authorLeo Nunner <l.nunner@proxmox.com>
Thu, 15 Sep 2022 11:52:28 +0000 (13:52 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 8 Nov 2022 17:17:26 +0000 (18:17 +0100)
Instead of iterating through several folders, it might just be easier to
check the objdump output of /sbin/init and getting the version from there.
Resolving the /sbin/init symlink happens inside the chroot, but the
objdump from the host system is used, as to not run any untrusted
executables.

Signed-off-by: Leo Nunner <l.nunner@proxmox.com>
src/PVE/LXC/Setup.pm
src/PVE/LXC/Setup/Alpine.pm
src/PVE/LXC/Setup/Base.pm
src/PVE/LXC/Setup/Devuan.pm
src/PVE/LXC/Setup/Plugin.pm
src/PVE/LXC/Setup/Unmanaged.pm

index b72a18e0d5ae066040898c44d46c0c681bd3eaa3..fe6f0db256f5d6df5ccaa1b26dd3422e564f6e40 100644 (file)
@@ -285,7 +285,7 @@ sub post_create_hook {
 sub unified_cgroupv2_support {
     my ($self) = @_;
 
-    return $self->protected_call(sub { $self->{plugin}->unified_cgroupv2_support() });
+    return $self->{plugin}->unified_cgroupv2_support($self->get_ct_init_path());
 }
 
 # os-release(5):
@@ -335,4 +335,20 @@ sub get_ct_os_release {
     return &$parse_os_release($data);
 }
 
+# Checks whether /sbin/init is a symlink, and if it is, 
+# resolves it to the actual binary
+sub get_ct_init_path { 
+    my ($self) = @_;
+
+    my $init = $self->protected_call(sub {
+       my $init_path = "/sbin/init";
+       if($self->{plugin}->ct_is_symlink($init_path)) {
+           $init_path = $self->{plugin}->ct_readlink($init_path);
+       }
+       return $init_path;
+    });
+
+    return $init;
+}
+
 1;
index b56d8956ea5a8846455f259e20a7c768dcadc8e2..87d72beb49bc1467ac7b7715d32c2d98abfa097c 100644 (file)
@@ -102,7 +102,7 @@ sub setup_network {
 
 # non systemd based containers work with pure cgroupv2
 sub unified_cgroupv2_support {
-    my ($self) = @_;
+    my ($self, $init) = @_;
 
     return 1;
 }
index cc1291462663c7659e606496b8d15517bbfed038..09155cf98f0938c9ebff4162f8d8855d7899e561 100644 (file)
@@ -514,40 +514,42 @@ sub clear_machine_id {
     }
 }
 
-# tries to guess the systemd (major) version based on the existence of
-# (/usr)?/lib/systemd/libsystemd-shared<version>.so. It was introduced in v231.
+# tries to guess the systemd (major) version based on the
+# libsystemd-shared<version>.so linked with /sbin/init
 sub get_systemd_version {
-    my ($self) = @_;
+    my ($self, $init) = @_;
 
-    my $sd_lib_dir = $self->ct_is_directory("/lib/systemd") ?
-       "/lib/systemd" : "/usr/lib/systemd";
-    my $libsd = PVE::Tools::dir_glob_regex($sd_lib_dir, "libsystemd-shared-.+\.so");
-    if (defined($libsd) && $libsd =~ /libsystemd-shared-(\d+)(?:\..*)?\.so/) {
-       return $1;
-    }
+    my $version = undef;
+    PVE::Tools::run_command(
+       ['objdump', '-p', $self->{rootdir}.$init],
+       outfunc => sub {
+           my $line = shift;
+           if ($line =~ /libsystemd-shared-(\d+)(?:\.[a-zA-Z0-9]*)?\.so:$/) {
+               $version = $1;
+           }},
+       errmsg => "objdump on $init failed",
+    );
 
-    return undef;
+    return $version;
 }
 
 sub unified_cgroupv2_support {
-    my ($self) = @_;
+    my ($self, $init) = @_;
 
     # https://www.freedesktop.org/software/systemd/man/systemd.html
     # systemd is installed as symlink to /sbin/init
-    my $systemd = $self->ct_readlink('/sbin/init');
-
     # assume non-systemd init will run with unified cgroupv2
-    if (!defined($systemd) || $systemd !~ m@/systemd$@) {
+    if (!defined($init) || $init !~ m@/systemd$@) {
        return 1;
     }
 
     # systemd version 232 (e.g. debian stretch) supports the unified hierarchy
-    my $sdver = $self->get_systemd_version();
+    my $sdver = $self->get_systemd_version($init);
     if (!defined($sdver) || $sdver < 232) {
        return 0;
     }
 
-    return 1
+    return 1;
 }
 
 sub ssh_host_key_types_to_generate {
index 3e15bb26719543fd90f80846ce017b5402dace81..059f14574574f51fec69820e039e9ad56c736793 100644 (file)
@@ -42,7 +42,7 @@ sub new {
 
 # non systemd based containers work with pure cgroupv2
 sub unified_cgroupv2_support {
-    my ($self) = @_;
+    my ($self, $init) = @_;
 
     return 1;
 }
index 8458ad87dbe351aa50e8f9978a839e03f63ff583..7024856e27dcb8ed72ecbc4e754900da46c09345 100644 (file)
@@ -48,7 +48,7 @@ sub set_user_password {
 }
 
 sub unified_cgroupv2_support {
-    my ($self) = @_;
+    my ($self, $init) = @_;
     croak "implement me in sub-class\n";
 }
 
index 3b9febf24ef96a21b9fc28e4316aafa825ea917c..280af04fccdb156d55ffdcead248269c2e4dad37 100644 (file)
@@ -45,7 +45,7 @@ sub set_user_password {
 }
 
 sub unified_cgroupv2_support {
-    my ($self) = @_;
+    my ($self, $init) = @_;
     return 1; # faking it won't normally hurt ;-)
 }