Previously, we assumed the ifcfg of a network "interface" is
"/etc/sysconfig/network-scripts/ifcfg-<interface>", but actually
it is not the case.
For example, for network interface "enp0s25", we are able to
generate like "/etc/sysconfig/network-scripts/ifcfg-enp0s25-test"
for it via network-manger.
The "suffix" in "ifcfg-<suffix>" is actually a
"configuration"
name not a "interface" name, though normally we use "interface"
name as its "configuration" name. You can refer to "man ifup"
for the detail.
So, this patch adds some assistant functions to acquire the right
ifcfg file for an interface. Get some help from the following script:
"/etc/sysconfig/network-scripts/network-functions"
Signed-off-by: Xunlei Pang <xlpang(a)redhat.com>
---
kdump-lib.sh | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 93 insertions(+)
diff --git a/kdump-lib.sh b/kdump-lib.sh
index 4d34206..5277966 100755
--- a/kdump-lib.sh
+++ b/kdump-lib.sh
@@ -230,3 +230,96 @@ is_hostname()
fi
echo $1 | grep -q "[a-zA-Z]"
}
+
+get_hwaddr()
+{
+ if [ -f /sys/class/net/${1}/address ]; then
+ awk '{ print toupper($0) }' < /sys/class/net/${1}/address
+ elif [ -d "/sys/class/net/${1}" ]; then
+ ip -o link show ${1} 2>/dev/null | \
+ awk '{ print toupper(gensub(/.*link\/[^ ]* ([[:alnum:]:]*).*/,
+ "\\1", 1)); }'
+ fi
+}
+
+get_config_by_device()
+{
+ grep -E -i -l "^[[:space:]]*DEVICE=${1}" \
+ /etc/sysconfig/network-scripts/ifcfg-* | head -1
+}
+
+get_config_by_hwaddr()
+{
+ grep -E -i -l "^[[:space:]]*HWADDR=${1}" \
+ /etc/sysconfig/network-scripts/ifcfg-* | head -1
+}
+
+get_config_by_name()
+{
+ grep -E -i -l "^[[:space:]]*NAME=${1}" \
+ /etc/sysconfig/network-scripts/ifcfg-* | head -1
+}
+
+is_nm_running ()
+{
+ [ "$(LANG=C nmcli -t --fields running general status 2>/dev/null)" =
"running" ]
+}
+
+# $1: netdev name
+get_ifcfg_nmcli()
+{
+ local nm_name
+ local ifcfg_file
+
+ # Get the active nmcli config name of $1
+ if [[ -x /usr/bin/nmcli && is_nm_running ]]; then
+ # The configuration name generated by nm is wrote to the ifcfg file
+ # as "NAME=<nm_name>", if <nm_name> contains whitespaces, a
pair of
+ # quote will also be added(i.e. NAME="My Test").
+ nm_name=$(nmcli d show $1 2>/dev/null |grep GENERAL.CONNECTION \
+ |sed -e 's/GENERAL.CONNECTION: *//')
+ if [[ ${nm_name} && ${nm_name} != "--" ]]; then
+ # Add quota for nm name that contains any whitespace
+ $(echo $"${nm_name}" | grep -q " ") &&
nm_name="\"${nm_name}\""
+ ifcfg_file=$(get_config_by_name ${nm_name})
+ fi
+ fi
+
+ echo -n ${ifcfg_file}
+}
+
+# $1: netdev name
+get_ifcfg_legacy()
+{
+ local ifcfg_file
+
+ ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-${1}"
+ [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}"
&& return
+
+ ifcfg_file=$(get_config_by_name "${1}")
+ [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}"
&& return
+
+ local hwaddr=$(get_hwaddr ${1})
+ if [ -n "$hwaddr" ]; then
+ ifcfg_file=$(get_config_by_hwaddr ${hwaddr})
+ [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}"
&& return
+ fi
+
+ ifcfg_file=$(get_config_by_device ${1})
+
+ echo -n "${ifcfg_file}"
+}
+
+# $1: netdev name
+# Return the ifcfg file whole name(including the path) of $1 if any.
+get_ifcfg_filename() {
+ local ifcfg_file
+
+ ifcfg_file=$(get_ifcfg_nmcli $1)
+ if [ -z ${ifcfg_file} ]; then
+ ifcfg_file=$(get_ifcfg_legacy $1)
+ fi
+
+ echo -n "${ifcfg_file}"
+}
+
--
1.8.3.1