From 56ef82a95ef1e1d001a0e1494324284f673b4cc3 Mon Sep 17 00:00:00 2001 From: luxend Date: Mon, 16 Nov 2026 16:45:38 +0100 Subject: [PATCH] debiface: merge an inet6 stanza into the inet stanza of the same iface Proxmox VE (configdrive2) writes the IPv6 settings of an interface as a separate stanza after the IPv4 one: iface eth0 inet static address 192.0.2.10 ... iface eth0 inet6 static address 2001:db8:10::10 ... _get_iface_blocks() starts a new block on every iface line, so this yields two NetworkDetails named eth0. Both resolve to the same MAC and NetworkConfigPlugin keeps only the last one per MAC, so the IPv4 address, netmask and gateway are never configured. When an inet6 stanza has the same name as the current static inet stanza, feed its lines into the v6 half of that block, where V6_PROXY already maps address, netmask and gateway to their v6 fields. inet6 stanzas of other interfaces (or after a non static inet stanza) still start their own block, as before. Signed-off-by: luxend --- cloudbaseinit/tests/utils/test_debiface.py | 42 ++++++++++++++++++++++ cloudbaseinit/utils/debiface.py | 26 ++++++++++---- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/cloudbaseinit/tests/utils/test_debiface.py b/cloudbaseinit/tests/utils/test_debiface.py index 0b6fdca00..994d59a76 100644 --- a/cloudbaseinit/tests/utils/test_debiface.py +++ b/cloudbaseinit/tests/utils/test_debiface.py @@ -20,6 +20,31 @@ from cloudbaseinit.tests import testutils from cloudbaseinit.utils import debiface +# As written by Proxmox VE (PVE::QemuServer::Cloudinit::configdrive2_network) +# for "ipconfig0: ip=...,gw=...,ip6=...,gw6=..." and "ipconfig1: ip=dhcp, +# ip6=...": the IPv6 settings of an interface get their own inet6 stanza. +PROXMOX_DUAL_STACK = """\ +auto lo +iface lo inet loopback + + dns_nameservers 192.0.2.53 2001:db8::53 + 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 +iface eth0 inet6 static + address 2001:db8:10::10 + netmask 64 + gateway 2001:db8:10::1 +auto eth1 +iface eth1 inet dhcp +iface eth1 inet6 static + address 2001:db8:11::10 + netmask 64 +""" + class TestInterfacesParser(unittest.TestCase): @@ -85,3 +110,20 @@ def test_nothing_to_parse(self): def test_parse(self): self._test_parse_nics() + + def test_parse_inet6_stanza_same_iface(self): + nics = debiface.parse(PROXMOX_DUAL_STACK) + + fields = ("name", "mac", "address", "netmask", "gateway", + "address6", "netmask6", "gateway6") + found = [tuple(getattr(nic, field) for field in fields) + for nic in nics] + self.assertEqual([ + # The inet6 stanza of eth0 is the IPv6 half of eth0, not a + # second eth0 which would replace the IPv4 settings. + ("eth0", None, "192.0.2.10", "255.255.255.0", "192.0.2.1", + "2001:db8:10::10", "64", "2001:db8:10::1"), + # No static IPv4 to merge into: unchanged behaviour. + ("eth1", None, "2001:db8:11::10", "64", None, + None, None, None), + ], found) diff --git a/cloudbaseinit/utils/debiface.py b/cloudbaseinit/utils/debiface.py index 6ef5cb2b2..9ed852cc3 100644 --- a/cloudbaseinit/utils/debiface.py +++ b/cloudbaseinit/utils/debiface.py @@ -67,24 +67,38 @@ MAC: lambda value: value.upper(), DNSNS: lambda value: value.strip().split() } +IFACE_STANZA = re.compile(r"iface\s+(?P\S+)\s+(?Pinet6?)" + r"\s+(?P\S+)") def _get_iface_blocks(data): """"Yield interface blocks as pairs of v4 and v6 halves.""" lines, lines6 = [], [] crt_lines = lines + static_name = None # name of the current static IPv4 stanza for line in data.splitlines(): line = line.strip() if not line or line.startswith("#"): continue if "iface" in line: - if "inet6" in line: + stanza = IFACE_STANZA.match(line) + if (stanza and static_name and + stanza.group("family") == "inet6" and + stanza.group("name") == static_name): + # IPv6 stanza of an interface that already has a static + # IPv4 one (e.g. Proxmox VE): it is the v6 half of the + # same NIC, not a new NIC. crt_lines = lines6 - if lines: - yield lines, lines6 - lines[:] = [] - lines6[:] = [] - crt_lines = lines + else: + if lines: + yield lines, lines6 + lines[:] = [] + lines6[:] = [] + crt_lines = lines + static_name = None + if (stanza and stanza.group("family", "method") == + ("inet", "static")): + static_name = stanza.group("name") crt_lines.append(line) if lines: yield lines, lines6