public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [PATCH container v2] fix #7388: prefer container DNS config over host-managed DHCP
@ 2026-11-30 13:32 Filip Schauer
  2026-11-30 13:45 ` Filip Schauer
  2026-12-01 11:36 ` Elias Huhsovitz
  0 siblings, 2 replies; 4+ messages in thread
From: Filip Schauer @ 2026-11-30 13:32 UTC (permalink / raw)
  To: pve-devel

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





^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH container v2] fix #7388: prefer container DNS config over host-managed DHCP
  2026-11-30 13:32 [PATCH container v2] fix #7388: prefer container DNS config over host-managed DHCP Filip Schauer
@ 2026-11-30 13:45 ` Filip Schauer
  2026-12-01 11:36 ` Elias Huhsovitz
  1 sibling, 0 replies; 4+ messages in thread
From: Filip Schauer @ 2026-11-30 13:45 UTC (permalink / raw)
  To: pve-devel

On 30/11/2026 14:32, Filip Schauer wrote:
> Changed since v1:
> * Rebased onto pve-container 6.1.29

I forgot to list out all changes:

* Anchor $IPV4RE $IPV6RE regexes
* Remove redundant `sysseek` call
* Only generate the config on 'start', not on 'stop'
* Use the host's /etc/dhcp/dhclient.conf as the base config




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH container v2] fix #7388: prefer container DNS config over host-managed DHCP
  2026-11-30 13:32 [PATCH container v2] fix #7388: prefer container DNS config over host-managed DHCP Filip Schauer
  2026-11-30 13:45 ` Filip Schauer
@ 2026-12-01 11:36 ` Elias Huhsovitz
  2026-12-01 15:37   ` superseded: " Filip Schauer
  1 sibling, 1 reply; 4+ messages in thread
From: Elias Huhsovitz @ 2026-12-01 11:36 UTC (permalink / raw)
  To: Filip Schauer, pve-devel

Gave this a quick test with a small dnsmasq DHCP server that provides
dns-server & domain-name options.

The DHCP server starts using:
dnsmasq --no-daemon \
  --port=0 \
  --interface=vmbr88 \
  --bind-interfaces \
  --dhcp-range=10.0.88.100,10.0.88.150,12h \
  --dhcp-option=option:router,10.0.88.10 \
  --dhcp-option=option:dns-server,10.0.88.10 \
  --dhcp-option=option:domain-name,lan.example

Container config:
nameserver: 9.9.9.9
net0: name=eth0,bridge=vmbr88,firewall=1,host-managed=1,hwaddr=BC:24:11:D0:32:EA,ip=dhcp,type=veth
searchdomain: corp.example

The resulting resolv.conf:
domain lan.example
search lan.example corp.example.
nameserver 9.9.9.9

If I understand the goal of this patch correctly, resolv.conf should
contain:
search corp.example
nameserver 9.9.9.9

Since we want to completely disregard the settings provided by the
DHCP. 

Comments inline.

On Mon Nov 30, 2026 at 2:32 PM CET, Filip Schauer wrote:
> 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$/;

Anchoring the regex now causes IPv6 address with a zone-id
to be rejected by the $IPV6RE regex. e.g. 'fe80::1%eth0'. (Even though
this is a valid config in PVE).

I guess the $conf->{nameserver} has to be pre-processed. Or perhaps the
unanchored regex is actually the right call here, since we valide the
config beforehand anyways, when changes are made.

> +        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";

This doesnt not get applied correctly when the DHCP provides option 15
(domain-name).

You can try something like 

if ($ipversion == 4) {
    $config .= "supersede domain-name \"\";\n";
}

So that in /usr/share/lxc/hooks/dhclient-script:make_resolv_conf() the
variable $new_domain_search is empty.

But I just used this appraoch to confirmed that make_resolv_conf() lines
49-70 are the cause. Perhaps we can think of a better long-term
solution.


> +        }
> +
> +        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 $@;
>      }
>  





^ permalink raw reply	[flat|nested] 4+ messages in thread

* superseded: [PATCH container v2] fix #7388: prefer container DNS config over host-managed DHCP
  2026-12-01 11:36 ` Elias Huhsovitz
@ 2026-12-01 15:37   ` Filip Schauer
  0 siblings, 0 replies; 4+ messages in thread
From: Filip Schauer @ 2026-12-01 15:37 UTC (permalink / raw)
  To: Elias Huhsovitz, pve-devel

Superseded-by:
https://lore.proxmox.com/pve-devel/20261201153346.163922-1-f.schauer@proxmox.com

On 01/12/2026 12:36, Elias Huhsovitz wrote:
> Gave this a quick test with a small dnsmasq DHCP server that provides
> dns-server & domain-name options.
> 
> The DHCP server starts using:
> dnsmasq --no-daemon \
>    --port=0 \
>    --interface=vmbr88 \
>    --bind-interfaces \
>    --dhcp-range=10.0.88.100,10.0.88.150,12h \
>    --dhcp-option=option:router,10.0.88.10 \
>    --dhcp-option=option:dns-server,10.0.88.10 \
>    --dhcp-option=option:domain-name,lan.example
> 
> Container config:
> nameserver: 9.9.9.9
> net0: name=eth0,bridge=vmbr88,firewall=1,host-managed=1,hwaddr=BC:24:11:D0:32:EA,ip=dhcp,type=veth
> searchdomain: corp.example
> 
> The resulting resolv.conf:
> domain lan.example
> search lan.example corp.example.
> nameserver 9.9.9.9
> 
> If I understand the goal of this patch correctly, resolv.conf should
> contain:
> search corp.example
> nameserver 9.9.9.9
> 
> Since we want to completely disregard the settings provided by the
> DHCP.
> 
> Comments inline.
> 
> On Mon Nov 30, 2026 at 2:32 PM CET, Filip Schauer wrote:
>> 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$/;
> 
> Anchoring the regex now causes IPv6 address with a zone-id
> to be rejected by the $IPV6RE regex. e.g. 'fe80::1%eth0'. (Even though
> this is a valid config in PVE).
> 
> I guess the $conf->{nameserver} has to be pre-processed. Or perhaps the
> unanchored regex is actually the right call here, since we valide the
> config beforehand anyways, when changes are made.
> 
>> +        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";
> 
> This doesnt not get applied correctly when the DHCP provides option 15
> (domain-name).
> 
> You can try something like
> 
> if ($ipversion == 4) {
>      $config .= "supersede domain-name \"\";\n";
> }
> 
> So that in /usr/share/lxc/hooks/dhclient-script:make_resolv_conf() the
> variable $new_domain_search is empty.
> 
> But I just used this appraoch to confirmed that make_resolv_conf() lines
> 49-70 are the cause. Perhaps we can think of a better long-term
> solution.
> 
> 
>> +        }
>> +
>> +        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 $@;
>>       }
>>   
> 





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 15:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 13:32 [PATCH container v2] fix #7388: prefer container DNS config over host-managed DHCP Filip Schauer
2026-11-30 13:45 ` Filip Schauer
2026-12-01 11:36 ` Elias Huhsovitz
2026-12-01 15:37   ` superseded: " Filip Schauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal