]> git.proxmox.com Git - pve-container.git/commitdiff
fix #4460: setup: centos: create /etc/hostname if it does not exist
authorFriedrich Weber <f.weber@proxmox.com>
Mon, 16 Jan 2023 16:52:34 +0000 (17:52 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 17 Jan 2023 08:58:29 +0000 (09:58 +0100)
Previously, the CentOS setup only wrote to /etc/hostname if the file
already existed. Many CT templates of Redhat-derived distros do not
contain that file, so the containers ended up without /etc/hostname.
This caused systemd-hostnamed to report the "static hostname" to be
empty. If networking is handled by NetworkManager, the empty static
hostname caused DHCP requests to be sent without the "Hostname"
field, as reported in #4460.

With this fix, the CentOS setup module creates /etc/hostname if it
does not exist, so NetworkManager correctly reads the hostname and
includes it in DHCP requests.

Manually tested with the following CT templates (checking that
/etc/hostname exists and DHCP requests include the hostname):

- Distros using NetworkManager:

  - Alma Linux 9 (almalinux-9-default_20221108_amd64.tar.xz)
  - CentOS 8 (centos-8-default_20201210_amd64.tar.xz)
  - CentOS 9 Stream (centos-9-stream-default_20221109_amd64.tar.xz)
  - Rocky Linux 9 (rockylinux-9-default_20221109_amd64.tar.xz)

- Distros using network-scripts (here, DHCP requests already
  contained the hostname without this fix, as network-scripts does
  not rely on systemd-hostnamed):

  - Alma Linux 8 (almalinux-8-default_20210928_amd64.tar.xz)
  - CentOS 7 (centos-7-default_20190926_amd64.tar.xz)
  - CentOS 8 Stream (centos-8-stream-default_20220327_amd64.tar.xz)
  - Rocky Linux 8 (rockylinux-8-default_20210929_amd64.tar.xz)

Signed-off-by: Friedrich Weber <f.weber@proxmox.com>
 [ T: slightly touch up of commit message format / wording ]
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/LXC/Setup/CentOS.pm
src/test/test-centos6-001/etc/hostname.exp [new file with mode: 0644]
src/test/test-centos6-002/etc/hostname.exp [new file with mode: 0644]
src/test/test-centos8-001/etc/hostname.exp [new file with mode: 0644]

index 00fecc666908da2cfa54fb0103938e40cff4775a..1d31ceed2189f32ea28491b1f968a68e3f6cf24c 100644 (file)
@@ -157,9 +157,8 @@ sub set_hostname {
 
     $self->update_etc_hosts($hostip, $oldname, $hostname, $searchdomains);
 
-    if ($self->ct_file_exists($hostname_fn)) {
-       $self->ct_file_set_contents($hostname_fn, "$hostname\n");
-    }
+    # Always write /etc/hostname, even if it does not exist yet
+    $self->ct_file_set_contents($hostname_fn, "$hostname\n");
 
     if ($self->ct_file_exists($sysconfig_network)) {
        my $data = $self->ct_file_get_contents($sysconfig_network);
diff --git a/src/test/test-centos6-001/etc/hostname.exp b/src/test/test-centos6-001/etc/hostname.exp
new file mode 100644 (file)
index 0000000..a5bce3f
--- /dev/null
@@ -0,0 +1 @@
+test1
diff --git a/src/test/test-centos6-002/etc/hostname.exp b/src/test/test-centos6-002/etc/hostname.exp
new file mode 100644 (file)
index 0000000..180cf83
--- /dev/null
@@ -0,0 +1 @@
+test2
diff --git a/src/test/test-centos8-001/etc/hostname.exp b/src/test/test-centos8-001/etc/hostname.exp
new file mode 100644 (file)
index 0000000..a5bce3f
--- /dev/null
@@ -0,0 +1 @@
+test1