diff --git a/cloudbaseinit/tests/utils/test_debiface.py b/cloudbaseinit/tests/utils/test_debiface.py index 0b6fdca0..994d59a7 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 6ef5cb2b..9ed852cc 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