From 38e5452beed4254bf93c4451a069175963619fd0 Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Tue, 16 Jul 2024 18:02:21 -0600 Subject: [PATCH] fix(NetworkManager): Fix network activator Reload NetworkManager configuration rather than bringing up interfaces. This enables NetworkManager to configure interfaces which are not yet available in userspace, and fixes a race that was previously worked around in a service file. --- cloudinit/net/activators.py | 24 ++++++++++++++++++++++++ systemd/cloud-final.service.tmpl | 8 +------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/cloudinit/net/activators.py b/cloudinit/net/activators.py index b9f27cff9..812568b43 100644 --- a/cloudinit/net/activators.py +++ b/cloudinit/net/activators.py @@ -184,6 +184,30 @@ class NetworkManagerActivator(NetworkActivator): cmd = ["nmcli", "device", "disconnect", device_name] return _alter_interface(cmd, device_name) + @classmethod + def bring_up_interfaces(cls, device_names: Iterable[str]) -> bool: + """Activate network + + Return True on success + """ + state = subp.subp( + [ + "systemctl", + "show", + "--property=SubState", + "NetworkManager.service", + ] + ).stdout + if "SubState=running" != state: + LOG.warning( + "Expected NetworkManager SubState=running, but detected: %s", + state, + ) + return _alter_interface( + ["systemctl", "reload-or-try-restart", "NetworkManager.service"], + "all", + ) + class NetplanActivator(NetworkActivator): NETPLAN_CMD = ["netplan", "apply"] diff --git a/systemd/cloud-final.service.tmpl b/systemd/cloud-final.service.tmpl index b66533643..11958483f 100644 --- a/systemd/cloud-final.service.tmpl +++ b/systemd/cloud-final.service.tmpl @@ -19,13 +19,7 @@ ExecStart=/usr/bin/cloud-init modules --mode=final RemainAfterExit=yes TimeoutSec=0 KillMode=process -{% if variant in ["almalinux", "cloudlinux", "rhel"] %} -# Restart NetworkManager if it is present and running. -ExecStartPost=/bin/sh -c 'u=NetworkManager.service; \ - out=$(systemctl show --property=SubState $u) || exit; \ - [ "$out" = "SubState=running" ] || exit 0; \ - systemctl reload-or-try-restart $u' -{% else %} +{% if variant not in ["almalinux", "cloudlinux", "rhel"] %} TasksMax=infinity {% endif %} -- 2.43.0