Currently get_nmcli_connection_show_cmd_by_ifname returns multiple
nmcli params in a single variable, and depend on shell word splitting to
split the words when calling nmcli. But this is very fragile and break
easily when there are any special character in the connection path.
This function is only introduced to get and cache the nmcli command
which contains the "connection name".
Actually only cache the "connection path" is enough. Callers should
just call get_nmcli_connection_apath_by_ifname to cache the path, and
a new helper get_nmcli_field_by_conpath is introduced here to get value
from nmcli. This way "connection path" can contain any character.
Also get rid of another nmcli_cmd usage in
get_nmcli_connection_apath_by_ifname which stores multiple params in a
single bash variable separated by space.
Signed-off-by: Kairui Song <kasong(a)redhat.com>
---
dracut-module-setup.sh | 37 ++++++++++++++++++-------------------
kdump-lib.sh | 28 ++++++++++------------------
2 files changed, 28 insertions(+), 37 deletions(-)
diff --git a/dracut-module-setup.sh b/dracut-module-setup.sh
index 3a8b0e65..bc627832 100755
--- a/dracut-module-setup.sh
+++ b/dracut-module-setup.sh
@@ -84,14 +84,13 @@ source_ifcfg_file() {
fi
}
-# $1: nmcli connection show output
kdump_setup_dns() {
local _netdev="$1"
- local _nm_show_cmd="$2"
+ local _conpath="$2"
local _nameserver _dns _tmp array
local _dnsfile=${initdir}/etc/cmdline.d/42dns.conf
- _tmp=$(get_nmcli_value_by_field "IP4.DNS" "$_nm_show_cmd")
+ _tmp=$(get_nmcli_field_by_conpath "IP4.DNS" "$_conpath")
# shellcheck disable=SC2206
array=(${_tmp//|/ })
if [[ ${array[*]} ]]; then
@@ -354,7 +353,7 @@ kdump_setup_bridge() {
[[ -e $_dev ]] || continue
_kdumpdev=$_dev
if kdump_is_bond "$_dev"; then
- $(kdump_setup_bond "$_dev"
"$(get_nmcli_connection_show_cmd_by_ifname "$_dev")") || exit 1
+ $(kdump_setup_bond "$_dev"
"$(get_nmcli_connection_apath_by_ifname "$_dev")") || exit 1
elif kdump_is_team "$_dev"; then
kdump_setup_team "$_dev"
elif kdump_is_vlan "$_dev"; then
@@ -374,7 +373,7 @@ kdump_setup_bridge() {
# bond=bond0:eth0,eth1:mode=balance-rr
kdump_setup_bond() {
local _netdev="$1"
- local _nm_show_cmd="$2"
+ local _conpath="$2"
local _dev _mac _slaves _kdumpdev _bondoptions
for _dev in $(cat "/sys/class/net/$_netdev/bonding/slaves"); do
_mac=$(kdump_get_perm_addr "$_dev")
@@ -384,7 +383,7 @@ kdump_setup_bond() {
done
echo -n " bond=$_netdev:${_slaves%,}" >>
"${initdir}/etc/cmdline.d/42bond.conf"
- _bondoptions=$(get_nmcli_value_by_field "bond.options"
"$_nm_show_cmd")
+ _bondoptions=$(get_nmcli_field_by_conpath "bond.options"
"$_conpath")
if [[ -z $_bondoptions ]]; then
dwarning "Failed to get bond configuration via nmlci output. Now try
sourcing ifcfg script."
@@ -435,7 +434,7 @@ kdump_setup_vlan() {
derror "Vlan over bridge is not supported!"
exit 1
elif kdump_is_bond "$_phydev"; then
- $(kdump_setup_bond "$_phydev"
"$(get_nmcli_connection_show_cmd_by_ifname "$_phydev")") || exit 1
+ $(kdump_setup_bond "$_phydev"
"$(get_nmcli_connection_apath_by_ifname "$_phydev")") || exit 1
echo " vlan=$(kdump_setup_ifname "$_netdev"):$_phydev" >
"${initdir}/etc/cmdline.d/43vlan.conf"
else
_kdumpdev="$(kdump_setup_ifname "$_phydev")"
@@ -473,18 +472,18 @@ find_online_znet_device() {
# setup s390 znet cmdline
# $1: netdev (ifname)
-# $2: nmcli connection show output
+# $2: nmcli connection path
kdump_setup_znet() {
local _netdev="$1"
- local _nmcli_cmd="$2"
+ local _conpath="$2"
local s390_prefix="802-3-ethernet.s390-"
local _options=""
local NETTYPE
local SUBCHANNELS
- NETTYPE=$(get_nmcli_value_by_field "${s390_prefix}nettype"
"$_nmcli_cmd")
- SUBCHANNELS=$(get_nmcli_value_by_field "${s390_prefix}subchannels"
"$_nmcli_cmd")
- _options=$(get_nmcli_value_by_field "${s390_prefix}options"
"$_nmcli_cmd")
+ NETTYPE=$(get_nmcli_field_by_conpath "${s390_prefix}nettype"
"$_conpath")
+ SUBCHANNELS=$(get_nmcli_field_by_conpath "${s390_prefix}subchannels"
"$_conpath")
+ _options=$(get_nmcli_field_by_conpath "${s390_prefix}options"
"$_conpath")
if [[ -z $NETTYPE || -z $SUBCHANNELS || -z $_options ]]; then
dwarning "Failed to get znet configuration via nmlci output. Now try
sourcing ifcfg script."
@@ -531,22 +530,22 @@ kdump_get_remote_ip() {
# initramfs accessing giving destination
# $1: destination host
kdump_install_net() {
- local _destaddr _srcaddr _route _netdev _nm_show_cmd kdumpnic
+ local _destaddr _srcaddr _route _netdev _conpath kdumpnic
local _static _proto _ip_conf _ip_opts _ifname_opts
- local _znet_netdev _nm_show_cmd_znet
+ local _znet_netdev _znet_conpath
_destaddr=$(kdump_get_remote_ip "$1")
_route=$(kdump_get_ip_route "$_destaddr")
_srcaddr=$(kdump_get_ip_route_field "$_route" "src")
_netdev=$(kdump_get_ip_route_field "$_route" "dev")
- _nm_show_cmd=$(get_nmcli_connection_show_cmd_by_ifname "$_netdev")
+ _conpath=$(get_nmcli_connection_apath_by_ifname "$_netdev")
_netmac=$(kdump_get_mac_addr "$_netdev")
kdumpnic=$(kdump_setup_ifname "$_netdev")
_znet_netdev=$(find_online_znet_device)
if [[ -n $_znet_netdev ]]; then
- _nm_show_cmd_znet=$(get_nmcli_connection_show_cmd_by_ifname
"$_znet_netdev")
- if ! $(kdump_setup_znet "$_znet_netdev"
"$_nm_show_cmd_znet"); then
+ _znet_conpath=$(get_nmcli_connection_apath_by_ifname "$_znet_netdev")
+ if ! $(kdump_setup_znet "$_znet_netdev" "$_znet_conpath");
then
derror "Failed to set up znet"
exit 1
fi
@@ -576,7 +575,7 @@ kdump_install_net() {
if kdump_is_bridge "$_netdev"; then
kdump_setup_bridge "$_netdev"
elif kdump_is_bond "$_netdev"; then
- $(kdump_setup_bond "$_netdev" "$_nm_show_cmd") || exit 1
+ $(kdump_setup_bond "$_netdev" "$_conpath") || exit 1
elif kdump_is_team "$_netdev"; then
kdump_setup_team "$_netdev"
elif kdump_is_vlan "$_netdev"; then
@@ -586,7 +585,7 @@ kdump_install_net() {
echo "$_ifname_opts" >> "$_ip_conf"
fi
- kdump_setup_dns "$_netdev" "$_nm_show_cmd"
+ kdump_setup_dns "$_netdev" "$_conpath"
if [[ ! -f ${initdir}/etc/cmdline.d/50neednet.conf ]]; then
# network-manager module needs this parameter
diff --git a/kdump-lib.sh b/kdump-lib.sh
index 2e003dd8..125423f5 100755
--- a/kdump-lib.sh
+++ b/kdump-lib.sh
@@ -289,6 +289,15 @@ get_nmcli_value_by_field()
LANG=C nmcli --get-values "$@"
}
+# Get nmcli field value of an connection apath (a D-Bus active connection path)
+# Usage: get_nmcli_field_by_apath <field> <apath>
+get_nmcli_field_by_conpath()
+{
+ local _field=$1 _apath=$2
+
+ get_nmcli_value_by_field "$_field" connection show "$_apath"
+}
+
# Get nmcli connection apath (a D-Bus active connection path ) by ifname
#
# apath is used for nmcli connection operations, e.g.
@@ -296,25 +305,8 @@ get_nmcli_value_by_field()
get_nmcli_connection_apath_by_ifname()
{
local _ifname=$1
- local _nm_show_cmd="device show $_ifname"
-
- local _apath=$(get_nmcli_value_by_field "GENERAL.CON-PATH"
"$_nm_show_cmd")
-
- echo -n "$_apath"
-}
-
-# Get nmcli connection show cmd by ifname
-#
-# "$_apath" is supposed to not contain any chracter that
-# need to be escapded, e.g. space. Otherwise get_nmcli_value_by_field
-# would fail.
-get_nmcli_connection_show_cmd_by_ifname()
-{
- local _ifname="$1"
- local _apath=$(get_nmcli_connection_apath_by_ifname "$_ifname")
- local _nm_show_cmd="connection show $_apath"
- echo -n "$_nm_show_cmd"
+ get_nmcli_value_by_field "GENERAL.CON-PATH" device show
"$_ifname"
}
get_ifcfg_by_device()
--
2.31.1