From: Lucas Barros <lucasbarros.kam@gmail.com>
To: pve-devel@lists.proxmox.com
Cc: t.lamprecht@proxmox.com, Lucas Barros <lucasbarros.kam@gmail.com>
Subject: [PATCH common v3 1/2] inotify: resolv.conf: add reader that keeps the whole search list
Date: Mon, 16 Nov 2026 10:31:07 -0300 [thread overview]
Message-ID: <20261116133000.6357-1-lucasbarros.kam@gmail.com> (raw)
In-Reply-To: <20261116133000.6357-0-lucasbarros.kam@gmail.com>
The 'resolvconf' parser only keeps the first domain of the 'search'
directive. Some users rely on that, for example to build the FQDN of
the node certificate, both in PVE and PMG, so it stays as it is.
Add read_resolv_conf_with_search_list(), which returns all domains of
the 'search' directive, separated by a single space, for the node DNS
API. As resolv.conf(5) documents that the obsolete 'domain' directive
only takes one domain, only its first word is used in both readers.
Add a test for both readers.
Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>
---
changes since v2:
* keep read_file('resolvconf') as it was and add a new reader for the
node DNS API, as PMG uses the old one too (node certificate,
get_host_dns_info(), pmg-smtp-filter)
* only use the first word of the obsolete 'domain' directive, as
documented in resolv.conf(5), with a regression test for it
This patch was prepared with the help of an LLM based coding assistant; I
reviewed the change and ran the tests from the cover letter myself.
src/PVE/INotify.pm | 29 ++++++++++--
test/Makefile | 1 +
test/resolv_conf_test.pl | 97 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 124 insertions(+), 3 deletions(-)
create mode 100755 test/resolv_conf_test.pl
diff --git a/src/PVE/INotify.pm b/src/PVE/INotify.pm
index 53b4e21..ea9b069 100644
--- a/src/PVE/INotify.pm
+++ b/src/PVE/INotify.pm
@@ -601,15 +601,17 @@ sub write_etc_hosts {
register_file('etchosts', "/etc/hosts", \&read_etc_hosts, \&write_etc_hosts);
-sub read_etc_resolv_conf {
- my ($filename, $fh) = @_;
+my sub parse_resolv_conf {
+ my ($fh, $all_search_domains) = @_;
my $res = {};
my $nscount = 0;
while (my $line = <$fh>) {
chomp $line;
- if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
+ if ($all_search_domains && $line =~ m/^search\s+(\S.*)$/) {
+ $res->{search} = join(' ', split(/\s+/, $1));
+ } elsif ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
$res->{search} = $2;
} elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) {
$nscount++;
@@ -622,6 +624,27 @@ sub read_etc_resolv_conf {
return $res;
}
+# only returns the first search domain, as callers use it to build an FQDN
+sub read_etc_resolv_conf {
+ my ($filename, $fh) = @_;
+
+ return parse_resolv_conf($fh, 0);
+}
+
+# like read_file('resolvconf'), but returns all domains of a 'search' directive, separated by a
+# single space. the obsolete 'domain' directive only takes one domain, see resolv.conf(5).
+sub read_resolv_conf_with_search_list {
+ my ($filename) = @_;
+
+ $filename //= '/etc/resolv.conf';
+
+ open(my $fh, '<', $filename) or return undef;
+ my $res = parse_resolv_conf($fh, 1);
+ close($fh);
+
+ return $res;
+}
+
sub update_etc_resolv_conf {
my ($filename, $fh, $resolv, @args) = @_;
diff --git a/test/Makefile b/test/Makefile
index 9caba10..416bc0c 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -19,6 +19,7 @@ TESTS = lock_file.test \
ticket_test.test \
check_pem_test.test \
split_pem_test.test \
+ resolv_conf_test.test \
all:
diff --git a/test/resolv_conf_test.pl b/test/resolv_conf_test.pl
new file mode 100755
index 0000000..00b7f68
--- /dev/null
+++ b/test/resolv_conf_test.pl
@@ -0,0 +1,97 @@
+#!/usr/bin/perl
+
+use v5.36;
+
+use lib '../src';
+
+use File::Temp;
+use Test::More;
+
+use PVE::INotify;
+
+my $tests = [
+ {
+ name => 'single search domain',
+ input => "search example.com\nnameserver 192.0.2.1\n",
+ first => { search => 'example.com', dns1 => '192.0.2.1' },
+ list => { search => 'example.com', dns1 => '192.0.2.1' },
+ },
+ {
+ name => 'multiple search domains',
+ input => "search a.example b.example c.example\nnameserver 192.0.2.1\n",
+ first => { search => 'a.example', dns1 => '192.0.2.1' },
+ list => { search => 'a.example b.example c.example', dns1 => '192.0.2.1' },
+ },
+ {
+ name => 'multiple search domains with extra whitespace',
+ input => "search\ta.example b.example \t\nnameserver 192.0.2.1\n",
+ first => { search => 'a.example', dns1 => '192.0.2.1' },
+ list => { search => 'a.example b.example', dns1 => '192.0.2.1' },
+ },
+ {
+ name => 'domain directive',
+ input => "domain example.com\nnameserver 192.0.2.1\n",
+ first => { search => 'example.com', dns1 => '192.0.2.1' },
+ list => { search => 'example.com', dns1 => '192.0.2.1' },
+ },
+ {
+ name => 'domain directive with trailing words',
+ input => "domain a.example b.example\nnameserver 192.0.2.1\n",
+ first => { search => 'a.example', dns1 => '192.0.2.1' },
+ list => { search => 'a.example', dns1 => '192.0.2.1' },
+ },
+ {
+ name => 'last of search and domain wins',
+ input => "search a.example b.example\ndomain c.example d.example\n",
+ first => { search => 'c.example' },
+ list => { search => 'c.example' },
+ },
+ {
+ name => 'only the first three name servers',
+ input => "nameserver 192.0.2.1\nnameserver 192.0.2.2\n"
+ . "nameserver 2001:db8::1\nnameserver 192.0.2.4\n",
+ first => { dns1 => '192.0.2.1', dns2 => '192.0.2.2', dns3 => '2001:db8::1' },
+ list => { dns1 => '192.0.2.1', dns2 => '192.0.2.2', dns3 => '2001:db8::1' },
+ },
+];
+
+for my $test ($tests->@*) {
+ open(my $fh, '<', \$test->{input}) or die "failed to open in-memory file - $!\n";
+ my $res = PVE::INotify::read_etc_resolv_conf('/etc/resolv.conf', $fh);
+ is_deeply($res, $test->{first}, "first search domain: $test->{name}");
+
+ my $tmp = File::Temp->new();
+ print $tmp $test->{input};
+ close($tmp);
+ $res = PVE::INotify::read_resolv_conf_with_search_list($tmp->filename);
+ is_deeply($res, $test->{list}, "search list: $test->{name}");
+}
+
+is(
+ PVE::INotify::read_resolv_conf_with_search_list('/nonexistent/resolv.conf'),
+ undef,
+ 'search list: missing file',
+);
+
+{
+ my $input = "search old.example\nnameserver 192.0.2.1\noptions rotate\n";
+ open(my $fh, '<', \$input) or die "failed to open in-memory file - $!\n";
+ my $data = PVE::INotify::update_etc_resolv_conf(
+ '/etc/resolv.conf',
+ $fh,
+ { search => 'a.example b.example', dns1 => '192.0.2.2' },
+ );
+ is(
+ $data,
+ "search a.example b.example\nnameserver 192.0.2.2\noptions rotate\n",
+ 'update: write multiple search domains',
+ );
+
+ my $tmp = File::Temp->new();
+ print $tmp $data;
+ close($tmp);
+ my $res = PVE::INotify::read_resolv_conf_with_search_list($tmp->filename);
+ is($res->{search}, 'a.example b.example', 'update: read back multiple search domains');
+}
+
+done_testing();
--
2.50.1 (Apple Git-155)
next prev parent reply other threads:[~2026-11-16 13:31 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 13:31 [PATCH common/manager v3 0/2] fix #6357: show all search domains in the node DNS settings Lucas Barros
2026-11-16 13:31 ` Lucas Barros [this message]
2026-11-16 13:31 ` [PATCH manager v3 2/2] fix #6357: api: node dns: return all search domains Lucas Barros
2026-11-18 1:42 ` partially-applied: [PATCH common/manager v3 0/2] fix #6357: show all search domains in the node DNS settings Thomas Lamprecht
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=20261116133000.6357-1-lucasbarros.kam@gmail.com \
--to=lucasbarros.kam@gmail.com \
--cc=pve-devel@lists.proxmox.com \
--cc=t.lamprecht@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox