]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/LXC/Setup.pm
backup: log errors from rsync
[pve-container.git] / src / PVE / LXC / Setup.pm
index f6cf43017a1b7e7212b2e49345528c27f5fad7b0..5c9114cd1bcbc612af15feb876f11f00742967d5 100644 (file)
@@ -17,6 +17,8 @@ use PVE::LXC::Setup::Fedora;
 use PVE::LXC::Setup::Gentoo;
 use PVE::LXC::Setup::SUSE;
 use PVE::LXC::Setup::Ubuntu;
+use PVE::LXC::Setup::NixOS;
+use PVE::LXC::Setup::Unmanaged;
 
 my $plugins = {
     alpine    => 'PVE::LXC::Setup::Alpine',
@@ -28,6 +30,8 @@ my $plugins = {
     gentoo    => 'PVE::LXC::Setup::Gentoo',
     opensuse  => 'PVE::LXC::Setup::SUSE',
     ubuntu    => 'PVE::LXC::Setup::Ubuntu',
+    nixos     => 'PVE::LXC::Setup::NixOS',
+    unmanaged => 'PVE::LXC::Setup::Unmanaged',
 };
 
 # a map to allow supporting related distro flavours
@@ -73,6 +77,8 @@ my $autodetect_type = sub {
        return "alpine";
     } elsif (-f  "$rootdir/etc/gentoo-release") {
        return "gentoo";
+    } elsif (-d  "$rootdir/nix/store") {
+       return "nixos";
     } elsif (-f "$rootdir/etc/os-release") {
        die "unable to detect OS distribution\n";
     } else {
@@ -91,7 +97,7 @@ sub new {
     my $os_release = $self->get_ct_os_release();
 
     if ($conf->{ostype} && $conf->{ostype} eq 'unmanaged') {
-       return $self;
+       $type = 'unmanaged';
     } elsif (!defined($type)) {
        # try to autodetect type
        $type = &$autodetect_type($self, $rootdir, $os_release);
@@ -104,11 +110,6 @@ sub new {
        }
     }
 
-    if ($type eq 'unmanaged') {
-       $conf->{ostype} = $type;
-       return $self;
-    }
-
     my $plugin_class = $plugins->{$type} || die "no such OS type '$type'\n";
 
     my $plugin = $plugin_class->new($conf, $rootdir, $os_release);
@@ -117,14 +118,35 @@ sub new {
 
     # Cache some host files we need access to:
     $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf');
-    $plugin->{host_localtime} = abs_path('/etc/localtime');
+    $plugin->{host_timezone} = PVE::INotify::read_file('timezone');
+
+    abs_path('/etc/localtime') =~ m|^(/.+)| or die "invalid /etc/localtime\n"; # untaint
+    $plugin->{host_localtime} = $1;
 
     # pass on user namespace information:
-    my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
+    my ($id_map, $root_uid, $root_gid) = PVE::LXC::parse_id_maps($conf);
     if (@$id_map) {
        $plugin->{id_map} = $id_map;
-       $plugin->{rootuid} = $rootuid;
-       $plugin->{rootgid} = $rootgid;
+       $plugin->{root_uid} = $root_uid;
+       $plugin->{root_gid} = $root_gid;
+    }
+
+    # if arch is unset, we try to autodetect it
+    if (!defined($conf->{arch})) {
+       my $arch = eval { $self->protected_call(sub { $plugin->detect_architecture() }) };
+
+       if (my $err = $@) {
+           warn "Architecture detection failed: $err" if $err;
+       }
+
+       if (!defined($arch)) {
+           $arch = 'amd64';
+           print "Falling back to $arch.\nUse `pct set VMID --arch ARCH` to change.\n";
+       } else {
+           print "Detected container architecture: $arch\n";
+       }
+
+       $conf->{arch} = $arch;
     }
 
     return $self;
@@ -174,57 +196,36 @@ sub protected_call {
 
 sub template_fixup {
     my ($self) = @_;
-
-    return if !$self->{plugin}; # unmanaged
-
     $self->protected_call(sub { $self->{plugin}->template_fixup($self->{conf}) });
 }
+
 sub setup_network {
     my ($self) = @_;
-
-    return if !$self->{plugin}; # unmanaged
-
     $self->protected_call(sub { $self->{plugin}->setup_network($self->{conf}) });
 }
 
 sub set_hostname {
     my ($self) = @_;
-
-    return if !$self->{plugin}; # unmanaged
-
     $self->protected_call(sub { $self->{plugin}->set_hostname($self->{conf}) });
 }
 
 sub set_dns {
     my ($self) = @_;
-
-    return if !$self->{plugin}; # unmanaged
-
     $self->protected_call(sub { $self->{plugin}->set_dns($self->{conf}) });
 }
 
 sub set_timezone {
     my ($self) = @_;
-
-    return if !$self->{plugin}; # unmanaged
-
     $self->protected_call(sub { $self->{plugin}->set_timezone($self->{conf}) });
 }
 
 sub setup_init {
     my ($self) = @_;
-
-    return if !$self->{plugin}; # unmanaged
-
     $self->protected_call(sub { $self->{plugin}->setup_init($self->{conf}) });
 }
 
 sub set_user_password {
     my ($self, $user, $pw) = @_;
-
-    return if !$self->{plugin}; # unmanaged
-
     $self->protected_call(sub { $self->{plugin}->set_user_password($self->{conf}, $user, $pw) });
 }
 
@@ -252,8 +253,6 @@ my sub generate_ssh_key { # create temporary key in hosts' /run, then read and u
 sub rewrite_ssh_host_keys {
     my ($self) = @_;
 
-    return if !$self->{plugin}; # unmanaged
-
     my $plugin = $self->{plugin};
 
     my $keynames = $plugin->ssh_host_key_types_to_generate();
@@ -295,8 +294,6 @@ sub post_clone_hook {
 sub post_create_hook {
     my ($self, $root_password, $ssh_keys) = @_;
 
-    return if !$self->{plugin}; # unmanaged
-
     $self->protected_call(sub {
        $self->{plugin}->post_create_hook($self->{conf}, $root_password, $ssh_keys);
     });
@@ -306,7 +303,7 @@ sub post_create_hook {
 sub unified_cgroupv2_support {
     my ($self) = @_;
 
-    $self->protected_call(sub { $self->{plugin}->unified_cgroupv2_support() });
+    return $self->{plugin}->unified_cgroupv2_support($self->get_ct_init_path());
 }
 
 # os-release(5):
@@ -356,4 +353,15 @@ 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 {
+       return $self->{plugin}->get_ct_init_path();
+    });
+
+    return $init;
+}
+
 1;