[pve-devel] [PATCH pve-container] Redhat: handle /etc/sysconfig/network better

Wolfgang Bumiller w.bumiller at proxmox.com
Mon Nov 23 15:19:13 CET 2015


The NETWORKING and NETWORKING_IPV6 variables are now setup
in setup_network instead of set_hostname, which now only
sets the hostname.

This changes the variable order so the testcase had to be
adapted.

Note that the HOSTNAME update s// now uses \h instead of \s
for horizontal spaces so it doesn't eat up newlines at the
end of file (caught by the testcase).
---
 src/PVE/LXC/Setup/Redhat.pm                        | 44 ++++++++++++----------
 .../test-centos6-001/etc/sysconfig/network.exp     |  2 +-
 2 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/src/PVE/LXC/Setup/Redhat.pm b/src/PVE/LXC/Setup/Redhat.pm
index adc771d..04c3372 100644
--- a/src/PVE/LXC/Setup/Redhat.pm
+++ b/src/PVE/LXC/Setup/Redhat.pm
@@ -153,29 +153,13 @@ sub set_hostname {
 
     if ($self->ct_file_exists($hostname_fn)) {
 	$self->ct_file_set_contents($hostname_fn, "$hostname\n");
-    } else {
+    }
+
+    if ($self->ct_file_exists($sysconfig_network)) {
 	my $data = $self->ct_file_get_contents($sysconfig_network);
-	if ($data !~ s/^HOSTNAME=\s*(\S+)\s*$/HOSTNAME=$hostname/m) {
+	if ($data !~ s/^HOSTNAME=\h*(\S+)\h*$/HOSTNAME=$hostname/m) {
 	    $data .= "HOSTNAME=$hostname\n";
 	}
-	my ($has_ipv4, $has_ipv6);
-	foreach my $k (keys %$conf) {
-	    next if $k !~ m/^net(\d+)$/;
-	    my $d = PVE::LXC::parse_lxc_network($conf->{$k});
-	    next if !$d->{name};
-	    $has_ipv4 = 1 if defined($d->{ip});
-	    $has_ipv6 = 1 if defined($d->{ip6});
-	}
-	if ($has_ipv4) {
-	    if ($data !~ s/(NETWORKING)=\S+/$1=yes/) {
-		$data .= "NETWORKING=yes\n";
-	    }
-	}
-	if ($has_ipv6) {
-	    if ($data !~ s/(NETWORKING_IPV6)=\S+/$1=yes/) {
-		$data .= "NETWORKING_IPV6=yes\n";
-	    }
-	}
 	$self->ct_file_set_contents($sysconfig_network, $data);
     }
 
@@ -189,10 +173,14 @@ sub setup_network {
 
     $self->ct_make_path('/etc/sysconfig/network-scripts');
 
+    my ($has_ipv4, $has_ipv6);
+
     foreach my $k (keys %$conf) {
 	next if $k !~ m/^net(\d+)$/;
 	my $d = PVE::LXC::parse_lxc_network($conf->{$k});
 	next if !$d->{name};
+	$has_ipv4 = 1 if defined($d->{ip});
+	$has_ipv6 = 1 if defined($d->{ip6});
 
 	my $filename = "/etc/sysconfig/network-scripts/ifcfg-$d->{name}";
 	my $routefile = "/etc/sysconfig/network-scripts/route-$d->{name}";
@@ -265,6 +253,22 @@ sub setup_network {
 	    $self->ct_file_set_contents($routefile, $routes);
 	}
     }
+
+    my $sysconfig_network = "/etc/sysconfig/network";
+    if ($self->ct_file_exists($sysconfig_network)) {
+	my $data = $self->ct_file_get_contents($sysconfig_network);
+	if ($has_ipv4) {
+	    if ($data !~ s/(NETWORKING)=\S+/$1=yes/) {
+		$data .= "NETWORKING=yes\n";
+	    }
+	}
+	if ($has_ipv6) {
+	    if ($data !~ s/(NETWORKING_IPV6)=\S+/$1=yes/) {
+		$data .= "NETWORKING_IPV6=yes\n";
+	    }
+	}
+	$self->ct_file_set_contents($sysconfig_network, $data);
+    }
 }
 
 1;
diff --git a/src/test/test-centos6-001/etc/sysconfig/network.exp b/src/test/test-centos6-001/etc/sysconfig/network.exp
index 5c819dc..03d42fd 100644
--- a/src/test/test-centos6-001/etc/sysconfig/network.exp
+++ b/src/test/test-centos6-001/etc/sysconfig/network.exp
@@ -1,2 +1,2 @@
-HOSTNAME=test1
 NETWORKING=yes
+HOSTNAME=test1
-- 
2.1.4





More information about the pve-devel mailing list