From 94f8eed57335254d2f22b1ef9f5f02380a033838 Mon Sep 17 00:00:00 2001 From: Coiby Xu Date: Wed, 23 Nov 2022 09:42:18 +0800 Subject: [PATCH] Add functions to copy NetworkManage connection profiles to the initramfs Resolves: bz2076416 Upstream: Fedora Conflict: None commit d25b1ee31c0c7eee43362c96d5e76f6fb0e90a25 Author: Coiby Xu Date: Thu Sep 9 11:35:52 2021 +0800 Add functions to copy NetworkManage connection profiles to the initramfs Each network interface is manged by a NM connection. Given a list of network interface names, copy the NetworkManager (NM) connection profiles i.e. .nmconnection files to the kdump initramfs. Before copying a connection file, clone it to automatically convert a legacy ifcfg-*[1] file to a .nmconnection file and for the convenience of editing the connection profile. [1] https://fedoraproject.org/wiki/Changes/NetworkManager_keyfile_instead_of_ifcfg_rh Signed-off-by: Coiby Xu Reviewed-by: Thomas Haller Reviewed-by: Philipp Rudo Signed-off-by: Coiby Xu --- dracut-module-setup.sh | 77 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/dracut-module-setup.sh b/dracut-module-setup.sh index 608e032..e74a026 100755 --- a/dracut-module-setup.sh +++ b/dracut-module-setup.sh @@ -1,10 +1,21 @@ #!/bin/bash +_DRACUT_KDUMP_NM_TMP_DIR="/tmp/$$-DRACUT_KDUMP_NM" + +cleanup() { + rm -rf "$_DRACUT_KDUMP_NM_TMP_DIR" +} + +# shellcheck disable=SC2154 # known issue of shellcheck https://github.com/koalaman/shellcheck/issues/1299 +trap 'ret=$?; cleanup; exit $ret;' EXIT + kdump_module_init() { if ! [[ -d "${initdir}/tmp" ]]; then mkdir -p "${initdir}/tmp" fi + mkdir -p "$_DRACUT_KDUMP_NM_TMP_DIR" + . /lib/kdump/kdump-lib.sh } @@ -354,6 +365,71 @@ kdump_setup_ifname() { echo "$_ifname" } +_clone_nmconnection() { + local _clone_output _name _unique_id + + _unique_id=$1 + _name=$(nmcli --get-values connection.id connection show "$_unique_id") + if _clone_output=$(nmcli connection clone --temporary uuid "$_unique_id" "$_name"); then + sed -E -n "s/.* \(.*\) cloned as.*\((.*)\)\.$/\1/p" <<< "$_clone_output" + return 0 + fi + + return 1 +} + +# Clone and modify NM connection profiles +# +# This function makes use of "nmcli clone" to automatically convert ifcfg-* +# files to Networkmanager .nmconnection connection profiles and also modify the +# properties of .nmconnection if necessary. +clone_and_modify_nmconnection() { + local _dev _cloned_nmconnection_file_path _tmp_nmconnection_file_path _old_uuid _uuid + + _dev=$1 + _nmconnection_file_path=$2 + + _old_uuid=$(nmcli --get-values connection.uuid connection show filename "$_nmconnection_file_path") + + if ! _uuid=$(_clone_nmconnection "$_old_uuid"); then + derror "Failed to clone $_old_uuid" + exit 1 + fi + + _cloned_nmconnection_file_path=$(nmcli --get-values UUID,FILENAME connection show | sed -n "s/^${_uuid}://p") + _tmp_nmconnection_file_path=$_DRACUT_KDUMP_NM_TMP_DIR/$(basename "$_nmconnection_file_path") + cp "$_cloned_nmconnection_file_path" "$_tmp_nmconnection_file_path" + # change uuid back to old value in case it's refered by other connection + # profile e.g. connection.master could be interface name of the master + # device or UUID of the master connection. + sed -i -E "s/(^uuid=).*$/\1${_old_uuid}/g" "$_tmp_nmconnection_file_path" + nmcli connection del "$_uuid" &> >(ddebug) + echo -n "$_tmp_nmconnection_file_path" +} + +_install_nmconnection() { + local _src _nmconnection_name _dst + + _src=$1 + _nmconnection_name=$(basename "$_src") + _dst="/etc/NetworkManager/system-connections/$_nmconnection_name" + inst "$_src" "$_dst" +} + +kdump_install_nmconnections() { + local _netif _nm_conn_path _cloned_nm_path + + while IFS=: read -r _netif _nm_conn_path; do + [[ -v "unique_netifs[$_netif]" ]] || continue + if _cloned_nm_path=$(clone_and_modify_nmconnection "$_netif" "$_nm_conn_path"); then + _install_nmconnection "$_cloned_nm_path" + else + derror "Failed to install the .nmconnection for $_netif" + exit 1 + fi + done <<< "$(nmcli -t -f device,filename connection show --active)" +} + kdump_setup_bridge() { local _netdev=$1 local _brif _dev _mac _kdumpdev @@ -1028,6 +1104,7 @@ remove_cpu_online_rule() { } install() { + declare -A unique_netifs local arch kdump_module_init