[PATCH v2 container 1/1] pct gentoo plugin fix for systemd

Jorge Ventura jorge.araujo.ventura at gmail.com
Mon May 8 15:49:46 CEST 2023


---
Create variable 'inittype' in the class Gentoo to establish 'openrc' or 'systemd' procedure and implement in self_init the logic for 'inittype' based on 'systemd' in the '/sbin/init' symlink.
For self->{inittype} = 'openrc', the previous logic is preserved; but for self->{inittype} = 'systemd', the following was implemented:    
   1. In set_hostname function, the content removed the key 'hostname='.    
   2. In setup_network function, the logic configure '/etc/systemd/network/<eth>.network' file as defined for systemd standard.    
The v2 just fix the 'open' parameter to FH to remove warning.    
    
Jorge Ventura (1):    
  pct gentoo plugin fix for systemd    

 src/PVE/LXC/Setup/Gentoo.pm | 89 ++++++++++++++++++++++++++++---------
 1 file changed, 68 insertions(+), 21 deletions(-)

diff --git a/src/PVE/LXC/Setup/Gentoo.pm b/src/PVE/LXC/Setup/Gentoo.pm
index 6dc8e9f..99717a0 100644
--- a/src/PVE/LXC/Setup/Gentoo.pm
+++ b/src/PVE/LXC/Setup/Gentoo.pm
@@ -20,7 +20,7 @@ sub new {
 	die "unrecognized gentoo version: $version\n";
     }
 
-    my $self = { conf => $conf, rootdir => $rootdir, version => 0 };
+    my $self = { conf => $conf, rootdir => $rootdir, version => 0, inittype => 'openrc' };
 
     $conf->{ostype} = "gentoo";
 
@@ -40,6 +40,13 @@ sub template_fixup {
 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);
+	$self->{inittype} = 'systemd';
+	return;
+    }
+
     my $filename = "/etc/inittab";
     my $ttycount =  PVE::LXC::Config->get_tty_count($conf);
     my $inittab = $self->ct_file_get_contents($filename);
@@ -77,12 +84,13 @@ sub setup_network {
     my $data = '';
     my %up;
 
-    my $filename = "/etc/conf.d/net";
+    my $filename = '';
+    my $name = '';
 
     foreach my $k (keys %$conf) {
 	next if $k !~ m/^net(\d+)$/;
 	my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k});
-	my $name = $d->{name};
+	$name = $d->{name};
 	next if !$name;
 
 	my $has_ipv4 = 0;
@@ -140,17 +148,41 @@ sub setup_network {
 
 	chomp $config;
 	chomp $routes;
-	$data .= "config_$name=\"$config\"\n" if $config;
-	$data .= "routes_$name=\"$routes\"\n" if $routes;
+
+	if ($self->{inittype} eq 'systemd') {
+	    $data .= "[Match]\nName=$name\n\n" if $config;
+	    if ($d->{ip} eq "dhcp") {
+               $data .= "[Network]\nDHCP=ipv4\n";
+   	    } else {
+               $data .= "[Netowrk]\nAddress=$d->{ip}\n";
+	       $data .= "Gateway=$d->{gw}";
+   	    }
+	} else {
+	    $data .= "config_$name=\"$config\"\n" if $config;
+	    $data .= "routes_$name=\"$routes\"\n" if $routes;
+	}
     }
 
-    $data = "modules=\"\$modules " . join(' ', sort keys %modules) . "\"\n" . $data;
+    if ($self->{inittype} eq 'systemd') {
+        $filename = "/etc/systemd/network/$name.network";
+
+	if (!$self->ct_file_exists($filename)) {
+	    open(FH, '>', $filename) or die $!;
+	    close(FH);
+	}
+       
+	$self->ct_modify_file($filename, $data, replace => 1);
+    } else {
+        $filename = "/etc/conf.d/net";
+        
+	$data = "modules=\"\$modules " . join(' ', sort keys %modules) . "\"\n" . $data;
 
-    # We replace the template's default file...
-    $self->ct_modify_file($filename, $data, replace => 1);
+        # We replace the template's default file...
+        $self->ct_modify_file($filename, $data, replace => 1);
 
-    foreach my $iface (keys %up) {
-	$self->ct_symlink("net.lo", "/etc/init.d/net.$iface");
+        foreach my $iface (keys %up) {
+            $self->ct_symlink("net.lo", "/etc/init.d/net.$iface");
+        }
     }
 }
 
@@ -160,20 +192,31 @@ sub set_hostname {
     my $hostname = $conf->{hostname} || 'localhost';
 
     my $namepart = ($hostname =~ s/\..*$//r);
+    
+    my $hostname_fn = '';
 
-    my $hostname_fn = "/etc/conf.d/hostname";
+    if ($self->{inittype} eq 'systemd') {
+	$hostname_fn = "/etc/hostname";
+	open(FH, '>', $hostname_fn) or die $!;
+	close(FH);
+    } else {
+        $hostname_fn = "/etc/conf.d/hostname";
+    }
 
     my $oldname = 'localhost';
-    my $fh = $self->ct_open_file_read($hostname_fn);
-    while (defined(my $line = <$fh>)) {
-	chomp $line;
-	next if $line =~ /^\s*(#.*)?$/;
-	if ($line =~ /^\s*hostname=("[^"]*"|'[^']*'|\S*)\s*$/) {
-	    $oldname = $1;
-	    last;
-	}
+
+    if ($self->{inittype} eq 'openrc') {
+        my $fh = $self->ct_open_file_read($hostname_fn);
+        while (defined(my $line = <$fh>)) {
+            chomp $line;
+            next if $line =~ /^\s*(#.*)?$/;
+            if ($line =~ /^\s*hostname=("[^"]*"|'[^']*'|\S*)\s*$/) {
+                $oldname = $1;
+                last;
+            }
+        }
+        $fh->close();
     }
-    $fh->close();
 
     my ($ipv4, $ipv6) = PVE::LXC::get_primary_ips($conf);
     my $hostip = $ipv4 || $ipv6;
@@ -184,7 +227,11 @@ sub set_hostname {
 
     # This is supposed to contain only the hostname, so we just replace the
     # file.
-    $self->ct_file_set_contents($hostname_fn, "hostname=\"$namepart\"\n");
+    if ($self->{inittype} eq 'systemd') {
+    	$self->ct_file_set_contents($hostname_fn, "$namepart\n");
+    } else {
+    	$self->ct_file_set_contents($hostname_fn, "hostname=\"$namepart\"\n");
+    }
 }
 
 1;
-- 
2.39.3





More information about the pve-devel mailing list