From 0d1a220b7bb0a853e5d7f5f2111f1689ed8327c3 Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Sat, 10 Apr 2021 12:18:23 +0200 Subject: [PATCH] bundles/systemd-networkd: generate unique mac address for vlan interfaces --- bundles/systemd-networkd/files/template-iface-vlan.netdev | 1 + bundles/systemd-networkd/items.py | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bundles/systemd-networkd/files/template-iface-vlan.netdev b/bundles/systemd-networkd/files/template-iface-vlan.netdev index 9f71a17..0dfd45b 100644 --- a/bundles/systemd-networkd/files/template-iface-vlan.netdev +++ b/bundles/systemd-networkd/files/template-iface-vlan.netdev @@ -1,6 +1,7 @@ [NetDev] Name=${interface} Kind=vlan +MACAddress=${mac} [VLAN] Id=${vlan} diff --git a/bundles/systemd-networkd/items.py b/bundles/systemd-networkd/items.py index 14565b7..f5f54cb 100644 --- a/bundles/systemd-networkd/items.py +++ b/bundles/systemd-networkd/items.py @@ -25,6 +25,9 @@ directories = { }, } +mac_host_prefix = '%04x' % (node.magic_number % 65534) +generated_mac = f'52:54:00:{mac_host_prefix[0:2]}:{mac_host_prefix[2:4]}:{{}}' + # Don't use .get() here. We might end up with a node without a network # config! for interface, config in node.metadata['interfaces'].items(): @@ -36,12 +39,15 @@ for interface, config in node.metadata['interfaces'].items(): template = 'template-iface-nodhcp.network' if '.' in interface: + vlan_id = int(interface.split('.')[1]) + vlan_hex = '%02x' % (vlan_id % 255) files[f'/etc/systemd/network/{interface}.netdev'] = { 'source': 'template-iface-vlan.netdev', 'content_type': 'mako', 'context': { 'interface': interface, - 'vlan': interface.split('.')[1], + 'vlan': vlan_id, + 'mac': generated_mac.format(vlan_hex) }, 'needed_by': { 'svc_systemd:systemd-networkd',