diff options
author | Thomas Haller <thaller@redhat.com> | 2023-06-15 18:28:54 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2023-06-29 14:05:27 +0200 |
commit | e9426e6d07597cb886af7b2495de40ed0a6d6234 (patch) | |
tree | 79a3fda802c86b69572801de28215e4cd11edffc | |
parent | c1f2616618701f888c7a4e89ec1393e6e68dd373 (diff) |
keyfile: add a NetworkManager.conf option "keyfile.rename"
The default behavior is not to rename profiles. I guess, that makes
sense, as renaming a file when changing the "connection.id" could break
users who rely on the name.
My use case is the following. When I connect a Wi-Fi hotspot I use
`nmcli device wifi connect $SSID`, which -- as expected -- persists the
profile to "/etc/NetworkManager/system-connections/$SSID.nmconnection".
Later, I always update the profile's name to "w_$SSID" so I can see on
the name that this is wireless profile. I also want the filename to
reflect that change of name.
Add a configuration option for that. All the infrastructure
("force_rename" parameter) already exists.
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | man/NetworkManager.conf.xml | 15 | ||||
-rw-r--r-- | src/core/nm-config.c | 1 | ||||
-rw-r--r-- | src/core/settings/plugins/keyfile/nms-keyfile-plugin.c | 19 | ||||
-rw-r--r-- | src/libnm-base/nm-config-base.h | 1 |
5 files changed, 36 insertions, 2 deletions
@@ -12,6 +12,8 @@ USE AT YOUR OWN RISK. NOT RECOMMENDED FOR PRODUCTION USE! be identical to the built-in default value when the stable-id is not set. * When configuring hostnames in non-public TLD (like "example.local"), use the TLD as default search domain instead of the full hostname. +* Add a "[keyfile].rename" option to NetworkManager.conf to force renaming + profiles on disk when their name changes. ============================================= NetworkManager-1.42 diff --git a/man/NetworkManager.conf.xml b/man/NetworkManager.conf.xml index 8ac982d5fc..40aa51d137 100644 --- a/man/NetworkManager.conf.xml +++ b/man/NetworkManager.conf.xml @@ -555,6 +555,21 @@ no-auto-default=* </listitem> </varlistentry> <varlistentry> + <term><varname>rename</varname></term> + <listitem> + <para> + NetworkManager automatically chooses a filename when storing + a new profile to disk. That name depends on the profile's name + (connection.id). When updating a profile's name, the file is + not renamed to not break scripts that rely on the filename + for the profile. + By setting this option to "true", NetworkManager renames + the keyfile on update of the profile, to follow the profile's + name. This defaults to "false". + </para> + </listitem> + </varlistentry> + <varlistentry> <term><varname>unmanaged-devices</varname></term> <listitem> <para>Set devices that should be ignored by NetworkManager. diff --git a/src/core/nm-config.c b/src/core/nm-config.c index bcbc41cb49..a53c6a07bc 100644 --- a/src/core/nm-config.c +++ b/src/core/nm-config.c @@ -878,6 +878,7 @@ static const ConfigGroup config_groups[] = { .group = NM_CONFIG_KEYFILE_GROUP_KEYFILE, .keys = NM_MAKE_STRV(NM_CONFIG_KEYFILE_KEY_KEYFILE_HOSTNAME, NM_CONFIG_KEYFILE_KEY_KEYFILE_PATH, + NM_CONFIG_KEYFILE_KEY_KEYFILE_RENAME, NM_CONFIG_KEYFILE_KEY_KEYFILE_UNMANAGED_DEVICES, ), }, { diff --git a/src/core/settings/plugins/keyfile/nms-keyfile-plugin.c b/src/core/settings/plugins/keyfile/nms-keyfile-plugin.c index 7409391987..1679cab674 100644 --- a/src/core/settings/plugins/keyfile/nms-keyfile-plugin.c +++ b/src/core/settings/plugins/keyfile/nms-keyfile-plugin.c @@ -891,6 +891,7 @@ nms_keyfile_plugin_update_connection(NMSKeyfilePlugin *self, gboolean reread_same; const char *uuid; char strbuf[100]; + NMTernary force_rename2; _nm_assert_storage(self, storage, TRUE); nm_assert(NM_IS_CONNECTION(connection)); @@ -910,6 +911,20 @@ nms_keyfile_plugin_update_connection(NMSKeyfilePlugin *self, previous_filename = nms_keyfile_storage_get_filename(storage); uuid = nms_keyfile_storage_get_uuid(storage); + if (force_rename) + force_rename2 = NM_TERNARY_TRUE; + else { + /* If the caller does not force a rename, we honor [keyfile].rename + * setting, and (if enabled) we rename by following the preferred name + * as necessary. That's indicated with NM_TERNARY_DEFAULT. */ + force_rename2 = nm_config_data_get_value_boolean(NM_CONFIG_GET_DATA, + NM_CONFIG_KEYFILE_GROUP_KEYFILE, + NM_CONFIG_KEYFILE_KEY_KEYFILE_RENAME, + FALSE) + ? NM_TERNARY_DEFAULT + : NM_TERNARY_FALSE; + } + if (!nms_keyfile_writer_connection( connection, is_nm_generated, @@ -922,7 +937,7 @@ nms_keyfile_plugin_update_connection(NMSKeyfilePlugin *self, _get_plugin_dir(priv), previous_filename, FALSE, - !!force_rename, + force_rename2, nm_sett_util_allow_filename_cb, NM_SETT_UTIL_ALLOW_FILENAME_DATA(&priv->storages, previous_filename), &full_filename, @@ -939,7 +954,7 @@ nms_keyfile_plugin_update_connection(NMSKeyfilePlugin *self, } nm_assert(full_filename); - nm_assert(force_rename || nm_streq(full_filename, previous_filename)); + nm_assert(force_rename2 != NM_TERNARY_FALSE || nm_streq(full_filename, previous_filename)); if (!reread || reread_same) nm_g_object_ref_set(&reread, connection); diff --git a/src/libnm-base/nm-config-base.h b/src/libnm-base/nm-config-base.h index c413e867ea..8d6e60a807 100644 --- a/src/libnm-base/nm-config-base.h +++ b/src/libnm-base/nm-config-base.h @@ -50,6 +50,7 @@ #define NM_CONFIG_KEYFILE_KEY_KEYFILE_PATH "path" #define NM_CONFIG_KEYFILE_KEY_KEYFILE_UNMANAGED_DEVICES "unmanaged-devices" #define NM_CONFIG_KEYFILE_KEY_KEYFILE_HOSTNAME "hostname" +#define NM_CONFIG_KEYFILE_KEY_KEYFILE_RENAME "rename" #define NM_CONFIG_KEYFILE_KEY_IFUPDOWN_MANAGED "managed" |