]> git.proxmox.com Git - pve-container.git/commitdiff
add setup_systemd_preset helper, disable networkd for debian 12+
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 16 Jun 2023 07:24:56 +0000 (09:24 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 16 Jun 2023 08:17:41 +0000 (10:17 +0200)
Since we use ifupdown by defaul there and systemd-networkd
would also pull systemd-networkd-wait-online.service which
fails.

We expect this to have to be used for more initial service
setups in the future, so a helper is added to `Base`.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/PVE/LXC/Setup/Base.pm
src/PVE/LXC/Setup/Debian.pm
src/PVE/LXC/Setup/Fedora.pm

index 040b79375014a879a7e3d410d9610f20c08bd4b3..b8f07ea104a0b24aeefdb24cbe1b113a89a45bf7 100644 (file)
@@ -308,6 +308,33 @@ DATA
     }
 }
 
+# At first boot, 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.
+#
+# `$preset` should map service names to a bool-ish.
+sub setup_systemd_preset {
+    my ($self, $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) = @_;
 
index 76d336a12e69c3ad70c03bd4f0e37bc3105e3bbb..b3ba76ec972b79c768aee09896713c9774736590 100644 (file)
@@ -51,12 +51,34 @@ sub devttydir {
     return '';
 }
 
+my sub at_least : prototype($$$) {
+    my ($str, $want_maj, $want_min) = @_;
+    return if !defined($str) || !defined($want_maj);
+
+    my ($maj, $min) = $str =~ /^(\d+)(?:\.(\d+))?/;
+    return if !defined($maj);
+
+    return $want_maj < $maj || $want_maj == $maj && (
+       (!defined($min) && $want_min == 0) || (defined($min) && $want_min <= $min)
+    );
+}
+
 sub setup_init {
     my ($self, $conf) = @_;
 
     my $systemd = $self->ct_readlink('/sbin/init');
     if (defined($systemd) && $systemd =~ m@/systemd$@) {
        $self->setup_container_getty_service($conf);
+
+       my $version = $self->{version};
+       if (at_least($version, 12, 0)) {
+           # this only affects the first-boot (if no /etc/machien-id exists).
+           $self->setup_systemd_preset({
+               # systemd-networkd gets enabled by default, disable it, debian uses
+               # ifupdown
+               'systemd-networkd.service' => 0,
+           });
+       }
     }
 
     my $filename = "/etc/inittab";
@@ -76,7 +98,6 @@ sub setup_init {
 
     $inittab .= "p0::powerfail:/sbin/init 0\n";
 
-    my $version = $self->{version};
     for (my $id = 1; $id <= $ttycount; $id++) {
        next if $id == 7; # reserved for X11
        my $levels = ($id == 1) ? '2345' : '23';
@@ -146,18 +167,6 @@ sub make_gateway_scripts {
 SCRIPTS
 }
 
-my sub at_least : prototype($$$) {
-    my ($str, $want_maj, $want_min) = @_;
-    return if !defined($str) || !defined($want_maj);
-
-    my ($maj, $min) = $str =~ /^(\d+)(?:\.(\d+))?/;
-    return if !defined($maj);
-
-    return $want_maj < $maj || $want_maj == $maj && (
-       (!defined($min) && $want_min == 0) || (defined($min) && $want_min <= $min)
-    );
-}
-
 # NOTE: this is re-used by Alpine Linux, please have that in mind when changing things.
 sub setup_network {
     my ($self, $conf) = @_;
index 6e54a9eef12f2e7ea99ab0ed3779e0305c48c95c..80ee85faa9ad671c94a3b02a47cbb199f4a56697 100644 (file)
@@ -35,13 +35,12 @@ sub setup_init {
     my $version = $self->{version};
 
     if ($version >= 37) {
-       # systemd-networkd is disabled by the preset in >=37, reenable it
        # this only affects the first-boot (if no /etc/machien-id exists).
-       $self->ct_mkdir('/etc/systemd/system-preset', 0755);
-       $self->ct_file_set_contents(
-           '/etc/systemd/system-preset/00-pve.preset',
-           "enable systemd-networkd.service\n",
-       );
+       $self->setup_systemd_preset({
+           # systemd-networkd is disabled by the preset in >=37 in favor of
+           # NetworkManager, reenable it, since we make use of it.
+           'systemd-networkd.service' => 0,
+       });
     }
 }