]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/LXC/Setup/Base.pm
setup: handle getty services also via systemd-preset
[pve-container.git] / src / PVE / LXC / Setup / Base.pm
index fffaf47e5731423d5c677abdf3eae35cf8b0141e..e136a436b5ac8181ce365edea3eba3630862a08c 100644 (file)
@@ -19,6 +19,8 @@ use PVE::Tools;
 use PVE::Network;
 
 use PVE::LXC::Setup::Plugin;
+use PVE::LXC::Tools;
+
 use base qw(PVE::LXC::Setup::Plugin);
 
 sub new {
@@ -261,7 +263,7 @@ DATA
        my ($has_ipv4, $has_ipv6);
 
        # DHCP bitflags:
-       my @DHCPMODES = ('no', 'v4', 'v6', 'yes');
+       my @DHCPMODES = ('no', 'ipv4', 'ipv6', 'yes');
        my ($NONE, $DHCP4, $DHCP6, $BOTH) = (0, 1, 2, 3);
        my $dhcp = $NONE;
        my $accept_ra = 'false';
@@ -308,6 +310,46 @@ DATA
     }
 }
 
+# At first boot (/etc/machine-id not existing), systemd goes through a set of `presets` to bring the
+# enable/disable state of services to a default.
+# Meaning for newer templates we should use this instead of manually creating enable/disable
+# symlinks.
+#
+# Disables some common problematic and/or useless units by default.
+#
+# `$extra_preset` should map service names to a bool-ish. It can also hold overrides for the default
+# presets.
+sub setup_systemd_preset {
+    my ($self, $extra_preset) = @_;
+
+    # some don't make sense in CTs, child-plugins can still override through extra_presets
+    my $preset = {
+       'sys-kernel-config.mount' => 0,
+       'sys-kernel-debug.mount' => 0,
+       'getty@.service' => 0,
+       'container-getty@.service' => 1,
+    };
+
+    if (defined($extra_preset)) {
+       $preset->{$_} = $extra_preset->{$_} for keys $extra_preset->%*;
+    }
+
+    my $preset_data = "# Added by PVE at create-time for first-boot configuration.\n";
+    for my $service (sort keys %$preset) {
+       if ($preset->{$service}) {
+           $preset_data .= "enable $service\n";
+       } else {
+           $preset_data .= "disable $service\n";
+       }
+    }
+
+    $self->ct_mkdir('/etc/systemd/system-preset', 0755);
+    $self->ct_file_set_contents(
+       '/etc/systemd/system-preset/00-pve.preset',
+       $preset_data,
+    );
+}
+
 sub setup_securetty {
     my ($self, $conf, @add) = @_;
 
@@ -506,11 +548,18 @@ sub clear_machine_id {
         $self->ct_unlink($dbus_machine_id_path);
     }
 
-    # truncate on clone to avoid that FirstBoot condition is set
-    if ($clone && ($uses_systemd || $machine_id_existed)) {
-       $self->ct_file_set_contents($machine_id_path, "\n");
-    } elsif (!$clone && $machine_id_existed) {
-       $self->ct_unlink($machine_id_path);
+    if ($machine_id_existed) {
+       # truncate exiting ones on clone to avoid FirstBoot condition. admins can override this by
+       # removing the machine-id file or setting it to uninitialized before creating a template, or
+       # cloning a guest - as per machine-id(5) man page. TODO: add explicit switch to API?
+       if ($clone) {
+           my $old_machine_id = $self->ct_file_read_firstline($machine_id_path) // '';
+           if ($uses_systemd && $old_machine_id ne 'uninitialized') {
+               $self->ct_file_set_contents($machine_id_path, "\n") if $uses_systemd;
+           }
+       } else {
+           $self->ct_unlink($machine_id_path);
+       }
     }
 }
 
@@ -524,7 +573,7 @@ sub get_systemd_version {
        ['objdump', '-p', $self->{rootdir}.$init],
        outfunc => sub {
            my $line = shift;
-           if ($line =~ /libsystemd-shared-(\d+)(?:\.[a-zA-Z0-9]*)?\.so:$/) {
+           if ($line =~ /libsystemd-shared-(\d+)(?:[-.][a-zA-Z0-9]+)*\.so:?$/) {
                $version = $1;
            }
        },
@@ -553,6 +602,16 @@ sub unified_cgroupv2_support {
     return 1;
 }
 
+sub get_ct_init_path {
+    my ($self) = @_;
+
+    my $init_path = "/sbin/init";
+    if ($self->ct_is_symlink($init_path)) {
+       $init_path = $self->ct_readlink_recursive($init_path);
+    }
+    return $init_path;
+}
+
 sub ssh_host_key_types_to_generate {
     my ($self) = @_;
 
@@ -564,6 +623,13 @@ sub ssh_host_key_types_to_generate {
     };
 }
 
+sub detect_architecture {
+    my ($self) = @_;
+
+    # '/bin/sh' is POSIX mandatory
+    return PVE::LXC::Tools::detect_elf_architecture('/bin/sh');
+}
+
 sub pre_start_hook {
     my ($self, $conf) = @_;
 
@@ -824,4 +890,18 @@ sub remove_pve_sections {
     return $data =~ s/^\h*\Q$head\E.*^\h*\Q$tail\E.*?$//rgms;
 }
 
+# templates from images.linuxcontainers.org have a bogus LXC_NAME line in /etc/hosts
+sub remove_lxc_name_from_etc_hosts {
+    my ($self) = @_;
+
+    return if ! -e '/etc/hosts';
+
+    my $hosts = $self->ct_file_get_contents('/etc/hosts');
+    my @lines = grep { !/^127.0.1.1\s+LXC_NAME$/ } split(/\n/, $hosts);
+
+    $hosts = join("\n", @lines). "\n";
+
+    $self->ct_file_set_contents('/etc/hosts', $hosts);
+}
+
 1;