From f0f5d69182b48efa379ebdf4c6a1ded0db469e5b Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 17 Sep 2024 10:19:30 +0100 Subject: [PATCH 1/2] generator: use keep-configuration for NM Network Manager will create external connection profiles for existing interfaces. There are cases where it will happen, in particular with virtual interfaces, during "netplan apply" even for interfaces that have profiles. "keep-configuration" forces NM to use the most appropriate profile for existing interfaces. --- src/generate.c | 13 ++++++++++++- src/nm.c | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/generate.c b/src/generate.c index aa5232437..16bbe6c0b 100644 --- a/src/generate.c +++ b/src/generate.c @@ -304,9 +304,20 @@ int main(int argc, char** argv) /* Disable /usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf * (which restricts NM to wifi and wwan) if "renderer: NetworkManager" is used anywhere */ - if (netplan_state_get_backend(np_state) == NETPLAN_BACKEND_NM || any_nm) + if (netplan_state_get_backend(np_state) == NETPLAN_BACKEND_NM || any_nm) { _netplan_g_string_free_to_file(g_string_new(NULL), rootdir, "/run/NetworkManager/conf.d/10-globally-managed-devices.conf", NULL); + /* + * Always try to match existing interfaces with connections. + * Because "netplan apply" is too aggressive with Network Manager connections it might + * create in-memory profiles for existing virtual interfaces. + * keep-configuration forces NM to use existing connection profiles for these interfaces avoiding + * this problem. + */ + _netplan_g_string_free_to_file(g_string_new("[device]\nkeep-configuration=no\n"), rootdir, + "/run/NetworkManager/conf.d/10-keep-configuration.conf", NULL); + } + gboolean enable_wait_online = FALSE; if (any_networkd) enable_wait_online = _netplan_networkd_write_wait_online(np_state, rootdir); diff --git a/src/nm.c b/src/nm.c index 3dbb85bfc..d36b9f219 100644 --- a/src/nm.c +++ b/src/nm.c @@ -1185,8 +1185,11 @@ _netplan_nm_cleanup(const char* rootdir) "/run/NetworkManager/conf.d/netplan.conf", NULL); g_autofree char* global_manage_path = g_strjoin(NULL, rootdir != NULL ? rootdir : "", "/run/NetworkManager/conf.d/10-globally-managed-devices.conf", NULL); + g_autofree char* keep_configuration_path = g_strjoin(NULL, rootdir != NULL ? rootdir : "", + "/run/NetworkManager/conf.d/10-keep-configuration.conf", NULL); unlink(confpath); unlink(global_manage_path); + unlink(keep_configuration_path); _netplan_unlink_glob(rootdir, "/run/NetworkManager/system-connections/netplan-*"); return TRUE; } From 3f2b33db9cbe20a1625f85d82a760b95d20f4d86 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 17 Sep 2024 10:22:51 +0100 Subject: [PATCH 2/2] apply: stop flushing addresses when NM is used With the introduction of keep-configuration it shouldn't be necessary anymore. --- netplan_cli/cli/commands/apply.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/netplan_cli/cli/commands/apply.py b/netplan_cli/cli/commands/apply.py index f6b94cdcb..b1049f33d 100644 --- a/netplan_cli/cli/commands/apply.py +++ b/netplan_cli/cli/commands/apply.py @@ -290,11 +290,7 @@ def command_apply(self, run_generate=True, sync=False, exit_on_error=True, state # 2nd: start all other services utils.systemctl('start', netplan_wpa + netplan_ovs, sync=True) if restart_nm: - # Flush all IP addresses of NM managed interfaces, to avoid NM creating - # new, non netplan-* connection profiles, using the existing IPs. nm_interfaces = utils.nm_interfaces(restart_nm_glob, devices) - for iface in nm_interfaces: - utils.ip_addr_flush(iface) # clear NM state, especially the [device].managed=true config, as that might have been # re-set via an udev rule setting "NM_UNMANAGED=1" shutil.rmtree('/run/NetworkManager/devices', ignore_errors=True)