On 07/09/15 at 11:18am, Minfei Huang wrote:
Introduce two lib functiones to make kdump get target string
conveniently.
Another lib function is used to strip the characters around the ip
address or hostname.
In order to setup the network in 2nd kernel, we need the network device
and target ip address. we use the command "ip route" to get the route
info.
Signed-off-by: Minfei Huang <mhuang(a)redhat.com>
---
dracut-module-setup.sh | 17 +++--------
kdump-lib.sh | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 83 insertions(+), 13 deletions(-)
diff --git a/dracut-module-setup.sh b/dracut-module-setup.sh
index ca871fd..ea03edb 100755
--- a/dracut-module-setup.sh
+++ b/dracut-module-setup.sh
@@ -297,24 +297,15 @@ kdump_install_net() {
local _server _netdev _srcaddr
local config_val="$1"
- _server=`echo $config_val | sed 's/.*@//' | cut -d':' -f1`
+ _server=$(get_remote_host $config_val)
is_hostname $_server && _server=`getent ahosts $_server | head -1 | cut
-d' ' -f1`
- _netdev=`/sbin/ip route get to $_server 2>&1`
+ _netdev=`/sbin/ip route get to $_server 2>&1 | head -n 1`
The variable name is bad, please use something like _route
[ $? != 0 ] && echo "Bad kdump location:
$config_val" && exit 1
- #the field in the ip output changes if we go to another subnet
- if [ -n "`echo $_netdev | grep via`" ]
- then
- # we are going to a different subnet
- _srcaddr=`echo $_netdev|awk '{print $7}'|head -n 1`
- _netdev=`echo $_netdev|awk '{print $5;}'|head -n 1`
- else
- # we are on the same subnet
- _srcaddr=`echo $_netdev|awk '{print $5}'|head -n 1`
- _netdev=`echo $_netdev|awk '{print $3}'|head -n 1`
- fi
+ _srcaddr=$(get_next_string 0 "$_netdev" "src")
+ _netdev=$(get_prev_string 0 "$_netdev" "src")
kdump_setup_netdev "${_netdev}" "${_srcaddr}"
"${_server}"
diff --git a/kdump-lib.sh b/kdump-lib.sh
index d12b7c9..5462874 100755
--- a/kdump-lib.sh
+++ b/kdump-lib.sh
@@ -207,6 +207,21 @@ is_ipv6_address()
echo $1 | grep -q ":"
}
+# get ip address or hostname from nfs/ssh config value
+get_remote_host()
+{
+ local _config_val=$1
+
+ # in ipv6, the _config_val format is [xxxx:xxxx::xxxx%eth0]:/mnt/nfs or
+ # username at xxxx:xxxx::xxxx%eth0. what we need is just xxxx:xxxx::xxxx
+ _config_val=${_config_val#*@}
+ _config_val=${_config_val%:/*}
+ _config_val=${_config_val#[}
+ _config_val=${_config_val%]}
+ _config_val=${_config_val%\%*}
+ echo $_config_val
+}
+
# if the passed parameter is hostname/domain, it will be satisfied with
# condition which it is without colon ":", and contains the [a-zA-Z]
# $1: hostname/ip address
@@ -219,3 +234,67 @@ is_hostname()
fi
echo $1 | grep -q "[a-zA-Z]"
}
+
+# get the next string after the keyword
+# $1: find the fisrt/last keyword in the string
+# $2: target string
+# $3: keyword
+get_next_string()
+{
+ local _is_first=$1 _str=$2 _keyword=$3 _num=0 _key_num=0
+
+ if [ -z "$_str" ]; then
+ return
+ fi
+
+ for _s in $_str;
+ do
+ _num=$(($_num+1))
+ if [[ $_s = $_keyword ]]; then
+ _key_num=$_num
+ if [ $_is_first = 1 ]; then
+ break
+ fi
+ fi
+ done
+
+ if [ $_key_num = 0 ]; then
+ return
+ fi
+
+ _key_num=$(($_key_num+1))
+ _str=`echo $_str | awk '{print $_key_num}' _key_num="$_key_num"`
+ echo $_str
+}
Do we need a function so comlicated? How about below:
#$1: `ip route` output string
#$2: the field name we want
#output: the field value we want
get_ip_route_field()
{
echo ${1##*$2}|cut -d ' ' -f1
}
You can call the function like below
_route=`ip route ...`
_src=$(get_ip_route_field $_route "src")
_dev=$(get_ip_route_field $_route "dev")
+
+# get the previous string before the keyword
+# $1: find the fisrt/last keyword in the string
+# $2: target string
+# $3: keyword
+get_prev_string()
+{
+ local _is_first=$1 _str=$2 _keyword=$3 _num=0 _key_num=0
+
+ if [ -z "$_str" ]; then
+ return
+ fi
+
+ for _s in $_str;
+ do
+ _num=$(($_num+1))
+ if [[ $_s = $_keyword ]]; then
+ _key_num=$_num
+ if [ $_is_first = 1 ]; then
+ break
+ fi
+ fi
+ done
+
+ if [ $_key_num = 0 ]; then
+ return
+ fi
+
+ _key_num=$(($_key_num-1))
+ _str=`echo $_str | awk '{print $_key_num}' _key_num="$_key_num"`
+ echo $_str
+}
--
2.1.0