From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.ivanov@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 166409F9A4
 for <pve-devel@lists.proxmox.com>; Fri,  9 Jun 2023 19:36:49 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id EB67431784
 for <pve-devel@lists.proxmox.com>; Fri,  9 Jun 2023 19:36:48 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Fri,  9 Jun 2023 19:36:47 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 10D0542B74
 for <pve-devel@lists.proxmox.com>; Fri,  9 Jun 2023 19:36:47 +0200 (CEST)
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Fri,  9 Jun 2023 19:36:27 +0200
Message-Id: <20230609173627.3252569-1-s.ivanov@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.051 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [centos.pm, suse.pm, linuxcontainer.org, nixos.wiki, fedora.pm,
 archlinux.pm, alpine.pm, linuxcontainers.org, gentoo.pm, base.pm]
 URIBL_SBL_A 0.1 Contains URL's A record listed in the Spamhaus SBL blocklist
 [185.83.214.222]
Subject: [pve-devel] [PATCH container] setup: template_fixup: remove bogus
 LXC_NAME line from /etc/hosts
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 09 Jun 2023 17:36:49 -0000

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@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@.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