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.
This commit is contained in:
Thomas Haller 2023-06-15 18:28:54 +02:00
parent c1f2616618
commit e9426e6d07
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
5 changed files with 36 additions and 2 deletions

2
NEWS
View file

@ -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

View file

@ -554,6 +554,21 @@ no-auto-default=*
</para>
</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>

View file

@ -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, ),
},
{

View file

@ -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);

View file

@ -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"