all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH container v2] fix #7388: prefer container DNS config over host-managed DHCP
Date: Mon, 30 Nov 2026 14:32:16 +0100	[thread overview]
Message-ID: <20261130133226.90647-1-f.schauer@proxmox.com> (raw)

For host-managed container network interfaces using DHCP, dhclient may
overwrite /etc/resolv.conf with DNS information supplied by the DHCP
server, causing any explicitly configured nameserver/searchdomain to be
silently ignored.

Fix this with a custom dhclient.conf that ensures the container config
takes precedence over any DNS settings offered by the DHCP server.

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
Changed since v1:
* Rebased onto pve-container 6.1.29

 src/PVE/LXC.pm | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 237b298..59d0ca0 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1349,12 +1349,41 @@ sub get_interfaces {
 }
 
 sub manage_dhclient {
-    my ($action, $vmid, $ipversion, $eth, $rootdir) = @_;
+    my ($action, $vmid, $conf, $ipversion, $eth, $rootdir) = @_;
 
     File::Path::make_path("/var/lib/lxc/$vmid/hook") if $action eq 'start';
     my $pidfile = "/var/lib/lxc/$vmid/hook/dhclient$ipversion-$eth.pid";
     my $leasefile = "/var/lib/lxc/$vmid/hook/dhclient$ipversion-$eth.leases";
     my $scriptfile = '/usr/share/lxc/hooks/dhclient-script';
+
+    # Keep configfh open until dhclient has opened the file
+    my $configfh;
+    my @config_opts;
+
+    if ($action eq 'start') {
+        my $config = "";
+        my $base = '/etc/dhcp/dhclient.conf';
+        if (-f $base) {
+            $config = PVE::Tools::file_get_contents($base) . "\n";
+        }
+
+        my $ns_re = $ipversion == 6 ? qr/^$IPV6RE$/ : qr/^$IPV4RE$/;
+        if (my @nameservers = grep { $_ =~ $ns_re } PVE::Tools::split_list($conf->{nameserver})) {
+            my $option = $ipversion == 6 ? "dhcp6.name-servers" : "domain-name-servers";
+            $config .= "supersede $option " . join(', ', @nameservers) . ";\n";
+        }
+
+        if (my @searchdomains = PVE::Tools::split_list($conf->{searchdomain})) {
+            $config .= "supersede " . (($ipversion == 6) ? "dhcp6." : "") . "domain-search ";
+            $config .= join(", ", map { "\"$_\"" } @searchdomains) . ";\n";
+        }
+
+        open($configfh, '+>', undef) or die "failed to create dhclient config: $!\n";
+        print $configfh $config;
+        $configfh->flush();
+        @config_opts = ('-cf', "/proc/$$/fd/" . fileno($configfh));
+    }
+
     run_in_ct_namespaces(
         $vmid,
         ['net', 'uts'],
@@ -1373,6 +1402,7 @@ sub manage_dhclient {
             "ROOTFS=$rootdir",
             '-sf',
             $scriptfile,
+            @config_opts,
             $eth,
         ],
     );
@@ -1428,10 +1458,10 @@ sub update_ipconfig {
         my $is_real_ip = ($newip && $newip !~ /^(?:auto|dhcp|manual)$/);
         if ($change_ip) {
             if ($newnet->{'host-managed'} && $newip && $newip eq 'dhcp') {
-                manage_dhclient('start', $vmid, $ipversion, $eth, $rootdir);
+                manage_dhclient('start', $vmid, $conf, $ipversion, $eth, $rootdir);
             }
             if ($optdata->{'host-managed'} && $oldip && $oldip eq 'dhcp') {
-                manage_dhclient('stop', $vmid, $ipversion, $eth, $rootdir);
+                manage_dhclient('stop', $vmid, $conf, $ipversion, $eth, $rootdir);
             }
 
             if ($is_real_ip) {
@@ -1442,7 +1472,7 @@ sub update_ipconfig {
                 }
             }
         } elsif ($optdata->{'host-managed'} && !$newnet->{'host-managed'}) {
-            manage_dhclient('stop', $vmid, $ipversion, $eth, $rootdir);
+            manage_dhclient('stop', $vmid, $conf, $ipversion, $eth, $rootdir);
         }
 
         # step 2: replace gateway
@@ -3319,12 +3349,12 @@ sub vm_start {
     my $rootdir = "/proc/$pid/root";
 
     for my $eth (@managed_dhcpv4_interfaces) {
-        eval { manage_dhclient('start', $vmid, 4, $eth, $rootdir) };
+        eval { manage_dhclient('start', $vmid, $conf, 4, $eth, $rootdir) };
         PVE::RESTEnvironment::log_warn("DHCP failed - $@") if $@;
     }
 
     for my $eth (@managed_dhcpv6_interfaces) {
-        eval { manage_dhclient('start', $vmid, 6, $eth, $rootdir) };
+        eval { manage_dhclient('start', $vmid, $conf, 6, $eth, $rootdir) };
         PVE::RESTEnvironment::log_warn("DHCP failed - $@") if $@;
     }
 
-- 
2.47.3





             reply	other threads:[~2026-11-30 13:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 13:32 Filip Schauer [this message]
2026-11-30 13:45 ` [PATCH container v2] fix #7388: prefer container DNS config over host-managed DHCP Filip Schauer
2026-12-01 11:36 ` Elias Huhsovitz
2026-12-01 15:37   ` superseded: " Filip Schauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130133226.90647-1-f.schauer@proxmox.com \
    --to=f.schauer@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal