[pve-devel] [PATCH container] setup: template_fixup: remove bogus LXC_NAME line from /etc/hosts

Stoiko Ivanov s.ivanov at proxmox.com
Fri Jun 9 19:36:27 CEST 2023


Templates not built directly by us, but redistributed from
images.linuxcontainer.org contain the following line in /etc/hosts:
`127.0.1.1       LXC_NAME`

While it is mostly cosmetic - LXC_NAME, contains an underscore and is
not a valid DNS name.

Simply drop it for newly created containers in template_fixup.

We ship Debian, Devuan ourselves, without this line - so no need for
modification there.

Currently the nixos container-template (see [0]) also does not seem to
contain the line.

Noticed the issue with a fresh fedora container, where systemd-resolved
logs a warning upon boot.

[0] https://nixos.wiki/wiki/Proxmox_Linux_Container

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
 src/PVE/LXC/Setup/Alpine.pm    |  2 ++
 src/PVE/LXC/Setup/ArchLinux.pm |  2 ++
 src/PVE/LXC/Setup/Base.pm      | 11 +++++++++++
 src/PVE/LXC/Setup/CentOS.pm    |  2 ++
 src/PVE/LXC/Setup/Fedora.pm    |  2 ++
 src/PVE/LXC/Setup/Gentoo.pm    |  2 ++
 src/PVE/LXC/Setup/SUSE.pm      |  2 ++
 7 files changed, 23 insertions(+)

diff --git a/src/PVE/LXC/Setup/Alpine.pm b/src/PVE/LXC/Setup/Alpine.pm
index 87d72be..8672f3c 100644
--- a/src/PVE/LXC/Setup/Alpine.pm
+++ b/src/PVE/LXC/Setup/Alpine.pm
@@ -40,6 +40,8 @@ sub template_fixup {
     $self->ct_symlink('/etc/init.d/savecache', '/etc/runlevels/shutdown/savecache');
 
     $self->setup_securetty($conf);
+
+    $self->remove_lxc_name_from_etc_hosts();
 }
 
 sub setup_init {
diff --git a/src/PVE/LXC/Setup/ArchLinux.pm b/src/PVE/LXC/Setup/ArchLinux.pm
index 60118ef..e379ea1 100644
--- a/src/PVE/LXC/Setup/ArchLinux.pm
+++ b/src/PVE/LXC/Setup/ArchLinux.pm
@@ -40,6 +40,8 @@ sub template_fixup {
 
     # edit /etc/securetty (enable login on console)
     $self->setup_securetty($conf);
+
+    $self->remove_lxc_name_from_etc_hosts();
 }
 
 sub setup_init {
diff --git a/src/PVE/LXC/Setup/Base.pm b/src/PVE/LXC/Setup/Base.pm
index 5534397..2d14599 100644
--- a/src/PVE/LXC/Setup/Base.pm
+++ b/src/PVE/LXC/Setup/Base.pm
@@ -841,4 +841,15 @@ 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) = @_;
+    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;
diff --git a/src/PVE/LXC/Setup/CentOS.pm b/src/PVE/LXC/Setup/CentOS.pm
index 1d31cee..e603d6c 100644
--- a/src/PVE/LXC/Setup/CentOS.pm
+++ b/src/PVE/LXC/Setup/CentOS.pm
@@ -119,6 +119,8 @@ sub template_fixup {
 
     # always call so root can login, if /etc/securetty doesn't exists it's a no-op
     $self->setup_securetty($conf);
+
+    $self->remove_lxc_name_from_etc_hosts();
 }
 
 sub setup_init {
diff --git a/src/PVE/LXC/Setup/Fedora.pm b/src/PVE/LXC/Setup/Fedora.pm
index 17ae7da..6e54a9e 100644
--- a/src/PVE/LXC/Setup/Fedora.pm
+++ b/src/PVE/LXC/Setup/Fedora.pm
@@ -24,6 +24,8 @@ sub template_fixup {
     my ($self, $conf) = @_;
     $self->setup_securetty($conf);
     $self->ct_unlink('/etc/systemd/system/getty at .service');
+
+    $self->remove_lxc_name_from_etc_hosts();
 }
 
 sub setup_init {
diff --git a/src/PVE/LXC/Setup/Gentoo.pm b/src/PVE/LXC/Setup/Gentoo.pm
index 6dc8e9f..501db54 100644
--- a/src/PVE/LXC/Setup/Gentoo.pm
+++ b/src/PVE/LXC/Setup/Gentoo.pm
@@ -35,6 +35,8 @@ sub devttydir {
 sub template_fixup {
     my ($self, $conf) = @_;
     $self->setup_securetty($conf);
+
+    $self->remove_lxc_name_from_etc_hosts();
 }
 
 sub setup_init {
diff --git a/src/PVE/LXC/Setup/SUSE.pm b/src/PVE/LXC/Setup/SUSE.pm
index 7863b50..3c5fc68 100644
--- a/src/PVE/LXC/Setup/SUSE.pm
+++ b/src/PVE/LXC/Setup/SUSE.pm
@@ -52,6 +52,8 @@ sub template_fixup {
     # temporary fix for systemd-firstboot
     $self->ct_file_set_contents('/etc/locale.conf', "LANG=C.utf8") if !$self->ct_file_exists('/etc/locale.conf');
     $self->ct_symlink('/usr/share/zoneinfo/UTC', '/etc/localtime') if !$self->ct_file_exists('/etc/localtime');
+
+    $self->remove_lxc_name_from_etc_hosts();
 }
 
 sub setup_init {
-- 
2.30.2






More information about the pve-devel mailing list