From b07ce8846d358baa0d851b45f641c11a08cc5d30 Mon Sep 17 00:00:00 2001 From: luxend Date: Mon, 16 Nov 2026 16:46:21 +0100 Subject: [PATCH] debiface: read dns_nameservers and nameservers set on the loopback Proxmox VE (configdrive2) writes the nameservers once, with an underscore, on the loopback stanza: auto lo iface lo inet loopback dns_nameservers 192.0.2.53 192.0.2.54 dns_search example.com auto eth0 iface eth0 inet static ... The DNSNS regex only matches "dns-nameservers" and the loopback block is never related to the other interfaces, so instances get no DNS servers at all. Accept both "dns-nameservers" and "dns_nameservers", and use the nameservers found on the loopback stanza for every NIC that has none of its own. The loopback block no longer produces a NetworkDetails entry with only nameservers in it. dns_search is still ignored, as NetworkDetails has no field for search domains. Fixes: https://github.com/cloudbase/cloudbase-init/issues/56 Signed-off-by: luxend --- cloudbaseinit/tests/utils/test_debiface.py | 41 ++++++++++++++++++++++ cloudbaseinit/utils/debiface.py | 13 ++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/cloudbaseinit/tests/utils/test_debiface.py b/cloudbaseinit/tests/utils/test_debiface.py index 0b6fdca00..27c498b9c 100644 --- a/cloudbaseinit/tests/utils/test_debiface.py +++ b/cloudbaseinit/tests/utils/test_debiface.py @@ -83,5 +83,46 @@ def test_nothing_to_parse(self): self.data = data self._test_parse_nics(no_nics=True) + def _test_parse_loopback_dns(self, lines, expected_dns): + nics = debiface.parse("\n".join(lines)) + + self.assertEqual(expected_dns, [nic.dnsnameservers for nic in nics]) + + def test_parse_loopback_dns(self): + # As written by Proxmox VE + # (PVE::QemuServer::Cloudinit::configdrive2_network). + self._test_parse_loopback_dns([ + "auto lo", + "iface lo inet loopback", + "", + " dns_nameservers 192.0.2.53 192.0.2.54", + " dns_search example.com", + "auto eth0", + "iface eth0 inet static", + " address 192.0.2.10", + " netmask 255.255.255.0", + " gateway 192.0.2.1", + "auto eth1", + "iface eth1 inet static", + " address 198.51.100.10", + " netmask 255.255.255.0", + ], [["192.0.2.53", "192.0.2.54"], ["192.0.2.53", "192.0.2.54"]]) + + def test_parse_loopback_dns_iface_dns_wins(self): + self._test_parse_loopback_dns([ + "auto lo", + "iface lo inet loopback", + " dns-nameservers 192.0.2.53", + "auto eth0", + "iface eth0 inet static", + " address 192.0.2.10", + " netmask 255.255.255.0", + " dns-nameservers 198.51.100.53", + "auto eth1", + "iface eth1 inet static", + " address 198.51.100.10", + " netmask 255.255.255.0", + ], [["198.51.100.53"], ["192.0.2.53"]]) + def test_parse(self): self._test_parse_nics() diff --git a/cloudbaseinit/utils/debiface.py b/cloudbaseinit/utils/debiface.py index 6ef5cb2b2..db6352dfe 100644 --- a/cloudbaseinit/utils/debiface.py +++ b/cloudbaseinit/utils/debiface.py @@ -52,8 +52,10 @@ r"(?P<{}>\S+)".format(GATEWAY)), GATEWAY6: re.compile(r"post-up ip -6 route add default via " r"(?P<{}>.+) dev".format(GATEWAY6)), - DNSNS: re.compile(r"dns-nameservers\s+(?P<{}>.+)".format(DNSNS)) + # Proxmox VE writes "dns_nameservers". + DNSNS: re.compile(r"dns[-_]nameservers\s+(?P<{}>.+)".format(DNSNS)) } +LOOPBACK = re.compile(r"iface\s+\S+\s+inet\s+loopback") IFACE_TEMPLATE = dict.fromkeys(FIELDS.keys()) # Map IPv6 availability by value index under `NetworkDetails`. V6_PROXY = { @@ -114,6 +116,7 @@ def parse(data): LOG.info("Parsing Debian config...\n%s", data) nics = [] # list of NetworkDetails objects + lo_dnsns = None # nameservers set on the loopback stanza for lines_pair in _get_iface_blocks(data): iface = IFACE_TEMPLATE.copy() for lines, use_proxy in zip(lines_pair, (False, True)): @@ -125,6 +128,14 @@ def parse(data): continue func = DETAIL_PREPROCESS.get(field, lambda value: value) iface[field] = func(value) if value != "None" else None + if LOOPBACK.match(lines_pair[0][0]): + # Proxmox VE puts the nameservers on the loopback stanza, + # they apply to every NIC without nameservers of its own. + lo_dnsns = iface[DNSNS] or lo_dnsns + continue _add_nic(iface, nics) + if lo_dnsns: + nics = [nic if nic.dnsnameservers else + nic._replace(dnsnameservers=lo_dnsns) for nic in nics] return nics