[pve-devel] [RFC PATCH v2 container] /etc/hosts handling rewritten

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Feb 10 13:43:42 CET 2016


Now using comment markers to mark the /etc/hosts section
managed by PVE. The rest of the file is left untouched.

If a localhost entry is missing it'll be included as part of
the managed section (and can be moved out by the user).

If no section is found then one will be inserted after the
last 127.0.0.1 or ::1 entry in the file, or on top if no
such line was found either.
---
The original version still modified the existing data as that's what
we've been doing before.
This version simply manages a comment-marked section of the file.

NOTE:
The disadvantage is that updating from the old version to this one
will cause the old host line to be duplicated to inside the newly
added managed section. (Newly created containers do not have this
problem.)
A possible solution to this would be a mixed behavior: If the section
exists touch only it, otherwise delete the lines the old code would
have replaced before inserting the section.

# --- BEGIN PVE HOSTS FILE ---
everything in here is replaced at startup
# --- END PVE HOSTS FILE ---

The user can move this section around and move the main 127.0.0.1 and
::1 entry into and out of this section.

Anything else will be left untouched.

 src/PVE/LXC/Setup/Base.pm                | 105 +++++++++++++------------------
 src/test/test-centos6-001/etc/hosts.exp  |   3 +
 src/test/test-debian-001/etc/hosts.exp   |   3 +
 src/test/test-debian-002/etc/hosts.exp   |   3 +
 src/test/test-debian-003/etc/hosts.exp   |   3 +
 src/test/test-debian-004/etc/hosts.exp   |   3 +
 src/test/test-debian-005/etc/hosts.exp   |   3 +
 src/test/test-debian-006/etc/hosts.exp   |   3 +
 src/test/test-debian-007/etc/hosts.exp   |   3 +
 src/test/test-debian-008/etc/hosts.exp   |   4 ++
 src/test/test-debian-009/etc/hosts.exp   |   3 +
 src/test/test-debian-010/etc/hosts.exp   |   3 +
 src/test/test-debian-013/etc/hosts.exp   |   3 +
 src/test/test-opensuse-001/etc/hosts.exp |   3 +
 14 files changed, 84 insertions(+), 61 deletions(-)

diff --git a/src/PVE/LXC/Setup/Base.pm b/src/PVE/LXC/Setup/Base.pm
index 8991b29..ffa2579 100644
--- a/src/PVE/LXC/Setup/Base.pm
+++ b/src/PVE/LXC/Setup/Base.pm
@@ -58,82 +58,65 @@ sub update_etc_hosts {
 
     my $done = 0;
 
-    my @lines;
-
     my $namepart = ($newname =~ s/\..*$//r);
 
-    my $all_names = '';
+    # First prepare all the new hostnames we want to use:
+    my @all_names = ();
     if ($newname =~ /\./) {
-	$all_names .= "$newname $namepart";
+	push @all_names, $newname, $namepart;
     } else {
 	foreach my $domain (PVE::Tools::split_list($searchdomains)) {
-	    $all_names .= ' ' if $all_names;
-	    $all_names .= "$newname.$domain";
+	    push @all_names, "$newname.$domain";
 	}
-	$all_names .= ' ' if $all_names;
-	$all_names .= $newname;
+	push @all_names, $newname;
     }
-    
-    foreach my $line (split(/\n/, $etc_hosts_data)) {
-	if ($line =~ m/^#/ || $line =~ m/^\s*$/) {
-	    push @lines, $line;
-	    next;
-	}
-
-	my ($ip, @names) = split(/\s+/, $line);
-	if (($ip eq '127.0.0.1') || ($ip eq '::1')) {
-	    push @lines, $line;
-	    next;
-	}
 
-	my $found = 0;
-	foreach my $name (@names) {
-	    if ($name eq $oldname || $name eq $newname) {
-		$found = 1;
-	    } else {
-		# fixme: record extra names?
-	    }
-	}
-	$found = 1 if defined($hostip) && ($ip eq $hostip);
-	
-	if ($found) {
-	    if (!$done) {
-		if (defined($hostip)) {
-		    push @lines, "$hostip $all_names";
-		} else {
-		    push @lines, "127.0.1.1 $namepart";
-		}
-		$done = 1;
-	    }
-	    next;
-	} else {
-	    push @lines, $line;
+    # Now modify the file:
+    my $lines = [split(/\n/, $etc_hosts_data)];
+
+    # Look for markers and localhost entries
+    my ($begin, $end, $found_lo4, $found_lo6);
+    foreach my $i (0..(@$lines-1)) {
+	my $line = $lines->[$i];
+	$begin = $i if !defined($begin) && $line =~ /^#\s*---\s*BEGIN\s*PVE/i;
+	$end = $i if !defined($end) && defined($begin) && $line =~ /^#\s*---\s*END\s*PVE/i;
+	if (!defined($begin) || defined($end)) {
+	    $found_lo4 = $i if $line =~ /^127\.0\.0\.1\s/;
+	    $found_lo6 = $i if $line =~ /^::1\s/;
 	}
     }
 
-    if (!$done) {
-	if (defined($hostip)) {
-	    push @lines, "$hostip $all_names";
-	} else {
-	    push @lines, "127.0.1.1 $namepart";
-	}	
+    # Prepare the PVE host section:
+    my @entry_lines = ('# --- BEGIN PVE HOSTS FILE ---');
+    push @entry_lines, "127.0.0.1 localhost.localnet localhost" if !defined($found_lo4);
+    push @entry_lines, "::1 localhost.localnet localhost" if !defined($found_lo6);
+    if (defined($hostip)) {
+	push @entry_lines, "$hostip " . join(' ', @all_names);
+    } else {
+	push @entry_lines, "127.0.1.1 $namepart";
     }
-
-    my $found_localhost = 0;
-    foreach my $line (@lines) {
-	if ($line =~ m/^127.0.0.1\s/) {
-	    $found_localhost = 1;
-	    last;
+    push @entry_lines, '# --- END PVE HOSTS FILE ---';
+
+    # Figure out where to put it:
+    if (!defined($begin)) {
+	# No begin found:
+	if (defined($found_lo4) || defined($found_lo6)) {
+	    # We have localhost sections, insert after them
+	    $found_lo4 = 0 if !defined($found_lo4);
+	    $found_lo6 = 0 if !defined($found_lo6);
+	    $begin = ($found_lo4 > $found_lo6 ? $found_lo4 : $found_lo6)+1;
+	} elsif (defined($end)) {
+	    $begin = $end;
+	} else {
+	    $begin = 0;
 	}
     }
+    $end = $begin-1 if !defined($end);
 
-    if (!$found_localhost) {
-	unshift @lines, "127.0.0.1 localhost.localnet localhost";
-    }
-    
-    $etc_hosts_data = join("\n", @lines) . "\n";
-    
-    return $etc_hosts_data;
+    # Now insert/replace PVE data:
+    splice @$lines, $begin, $end-$begin+1, @entry_lines;
+
+    return join("\n", @$lines) . "\n";
 }
 
 sub template_fixup {
diff --git a/src/test/test-centos6-001/etc/hosts.exp b/src/test/test-centos6-001/etc/hosts.exp
index 0791e77..3460c34 100644
--- a/src/test/test-centos6-001/etc/hosts.exp
+++ b/src/test/test-centos6-001/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 test1.proxmox.com test1
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-001/etc/hosts.exp b/src/test/test-debian-001/etc/hosts.exp
index 7f3910c..f986a01 100644
--- a/src/test/test-debian-001/etc/hosts.exp
+++ b/src/test/test-debian-001/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-002/etc/hosts.exp b/src/test/test-debian-002/etc/hosts.exp
index 0791e77..3460c34 100644
--- a/src/test/test-debian-002/etc/hosts.exp
+++ b/src/test/test-debian-002/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 test1.proxmox.com test1
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-003/etc/hosts.exp b/src/test/test-debian-003/etc/hosts.exp
index 9e3fb59..7eb24ec 100644
--- a/src/test/test-debian-003/etc/hosts.exp
+++ b/src/test/test-debian-003/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test3
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-004/etc/hosts.exp b/src/test/test-debian-004/etc/hosts.exp
index cd65bdf..e9e6c39 100644
--- a/src/test/test-debian-004/etc/hosts.exp
+++ b/src/test/test-debian-004/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.2 test3.use-fqdn.com test3
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-005/etc/hosts.exp b/src/test/test-debian-005/etc/hosts.exp
index 7f3910c..f986a01 100644
--- a/src/test/test-debian-005/etc/hosts.exp
+++ b/src/test/test-debian-005/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-006/etc/hosts.exp b/src/test/test-debian-006/etc/hosts.exp
index 7f3910c..f986a01 100644
--- a/src/test/test-debian-006/etc/hosts.exp
+++ b/src/test/test-debian-006/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-007/etc/hosts.exp b/src/test/test-debian-007/etc/hosts.exp
index 7f3910c..f986a01 100644
--- a/src/test/test-debian-007/etc/hosts.exp
+++ b/src/test/test-debian-007/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-008/etc/hosts.exp b/src/test/test-debian-008/etc/hosts.exp
index 58c7bba..bff08c9 100644
--- a/src/test/test-debian-008/etc/hosts.exp
+++ b/src/test/test-debian-008/etc/hosts.exp
@@ -1,2 +1,6 @@
 127.0.0.1 localhost.localdomain localhost
+# --- BEGIN PVE HOSTS FILE ---
+::1 localhost.localnet localhost
 192.168.3.102 CT102.proxmox.com CT102
+# --- END PVE HOSTS FILE ---
+127.0.1.1 CT102
diff --git a/src/test/test-debian-009/etc/hosts.exp b/src/test/test-debian-009/etc/hosts.exp
index 41eed1f..51fef7a 100644
--- a/src/test/test-debian-009/etc/hosts.exp
+++ b/src/test/test-debian-009/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test9
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-010/etc/hosts.exp b/src/test/test-debian-010/etc/hosts.exp
index 00a9546..fb707d0 100644
--- a/src/test/test-debian-010/etc/hosts.exp
+++ b/src/test/test-debian-010/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test10
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-debian-013/etc/hosts.exp b/src/test/test-debian-013/etc/hosts.exp
index bd9bcd7..34c5c76 100644
--- a/src/test/test-debian-013/etc/hosts.exp
+++ b/src/test/test-debian-013/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 localhost
+# --- END PVE HOSTS FILE ---
diff --git a/src/test/test-opensuse-001/etc/hosts.exp b/src/test/test-opensuse-001/etc/hosts.exp
index c0113b8..3629050 100644
--- a/src/test/test-opensuse-001/etc/hosts.exp
+++ b/src/test/test-opensuse-001/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS FILE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 pvesuse1.proxmox.com pvesuse1
+# --- END PVE HOSTS FILE ---
-- 
2.1.4





More information about the pve-devel mailing list