ITADN

How to add route-metric support in the NetworkManager renderer, not by static config

#6854Openchallvy 创建于 2026-04-29
C
challvycommented
On any OS using NetworkManager, such as RHEL 10, when a new network interface is added without a static network configuration, the default metric values assigned are typically 101 and 102 and so on. However, some cloud vendors (e.g., ECS, EC2) have implemented logic in cloudinit/sources/DataSourceEc2.py and cloudinit/sources/helpers/aliyun.py to increment the metric values of new interfaces by 100. ```python for nic_idx, nic_name in enumerate(orderd_nic_name_list): nic_mac = nic_name_2_mac_map[nic_name] nic_metadata = macs_metadata.get(nic_mac) dhcp_override = {"route-metric": (nic_idx + 1) * 100} dev_config = { "dhcp4": True, "dhcp4-overrides": dhcp_override, "dhcp6": False, "match": {"macaddress": nic_mac.lower()}, "set-name": nic_name, } if nic_metadata.get("ipv6s"): # Any IPv6 addresses configured dev_config["dhcp6"] = True dev_config["dhcp6-overrides"] = dhcp_override ``` But in practice, I have found that this implementation does not seem to take effect. <img width="612" height="155" alt="Image" src="https://github.com/user-attachments/assets/5b802653-e804-4983-a768-13319737ac2c" /> My initial idea is to add the following code to cloudinit/net/network_manager.py. Preliminary testing suggests this approach is feasible: ```python @@ -434,6 +455,9 @@ class NMConnection: self.config[family]["gateway"] = subnet["gateway"] for route in subnet["routes"]: self._add_route(route) + # metric may apply to both dhcp and static config + if "metric" in subnet: + self.config[family]["route-metric"] = str(subnet["metric"]) # Add subnet-level DNS if "dns_nameservers" in subnet: ``` After testing, it does work (a system reboot is required). <img width="788" height="105" alt="Image" src="https://github.com/user-attachments/assets/d2916f7b-f5b8-47d9-8c26-3ecd9104efea" /> Could you please help evaluate this further? Thanks!
0 条评论