[PATCH] dracut-module-setup: Add new lib function write_config_once

Minfei Huang mhuang at redhat.com
Sat Sep 19 06:03:18 UTC 2015


To make code more clear, add a new wrapper function to let config write
into file once, if kdump calls it repeatly with the same argument.

Make cleanup in function kdump_setup_iscsi_device.

Signed-off-by: Minfei Huang <mhuang at redhat.com>
---
 dracut-module-setup.sh | 37 ++++++++++++-------------------------
 kdump-lib.sh           |  9 +++++++++
 2 files changed, 21 insertions(+), 25 deletions(-)

diff --git a/dracut-module-setup.sh b/dracut-module-setup.sh
index 9b398eb..0bf189b 100755
--- a/dracut-module-setup.sh
+++ b/dracut-module-setup.sh
@@ -497,15 +497,12 @@ kdump_get_iscsi_initiator() {
 # No ibft handling yet.
 kdump_setup_iscsi_device() {
     local path=$1
-    local tgt_name; local tgt_ipaddr;
-    local username; local password; local userpwd_str;
-    local username_in; local password_in; local userpwd_in_str;
-    local netdev
-    local srcaddr
-    local idev
-    local netroot_str ; local initiator_str;
+    local tgt_name tgt_ipaddr
+    local username password userpwd_str
+    local username_in password_in userpwd_in_str
+    local netdev srcaddr
+    local netroot_str initiator_str
     local netroot_conf="${initdir}/etc/cmdline.d/50iscsi.conf"
-    local initiator_conf="/etc/iscsi/initiatorname.iscsi"
 
     dinfo "Found iscsi component $1"
 
@@ -542,32 +539,22 @@ kdump_setup_iscsi_device() {
 
     kdump_setup_netdev $netdev $srcaddr
 
-    # prepare netroot= command line
-    # FIXME: Do we need to parse and set other parameters like protocol, port
-    #        iscsi_iface_name, netdev_name, LUN etc.
+    [[ -f $netroot_conf ]] || touch $netroot_conf
 
     if is_ipv6_address $tgt_ipaddr; then
         tgt_ipaddr="[$tgt_ipaddr]"
     fi
-    netroot_str="netroot=iscsi:${userpwd_str}${userpwd_in_str}@$tgt_ipaddr::::$tgt_name"
-
-    [[ -f $netroot_conf ]] || touch $netroot_conf
 
-    # If netroot target does not exist already, append.
-    if ! grep -q $netroot_str $netroot_conf; then
-         echo $netroot_str >> $netroot_conf
-         dinfo "Appended $netroot_str to $netroot_conf"
-    fi
+    # prepare netroot= command line
+    # FIXME: Do we need to parse and set other parameters like protocol, port
+    #        iscsi_iface_name, netdev_name, LUN etc.
+    netroot_str="netroot=iscsi:${userpwd_str}${userpwd_in_str}@$tgt_ipaddr::::$tgt_name"
+    write_config_once "$netroot_str" "$netroot_conf"
 
     # Setup initator
     initiator_str=$(kdump_get_iscsi_initiator)
     [ $? -ne "0" ] && derror "Failed to get initiator name" && return 1
-
-    # If initiator details do not exist already, append.
-    if ! grep -q "$initiator_str" $netroot_conf; then
-         echo "$initiator_str" >> $netroot_conf
-         dinfo "Appended "$initiator_str" to $netroot_conf"
-    fi
+    write_config_once "$netroot_str" "$netroot_conf"
 }
 
 kdump_check_iscsi_targets () {
diff --git a/kdump-lib.sh b/kdump-lib.sh
index 4d34206..f6217a0 100755
--- a/kdump-lib.sh
+++ b/kdump-lib.sh
@@ -230,3 +230,12 @@ is_hostname()
     fi
     echo $1 | grep -q "[a-zA-Z]"
 }
+
+# $1: content to be appended to file
+# $2: target file
+write_config_once()
+{
+    if ! grep -q "$1" "$2"; then
+        echo "$1" >> "$2"
+    fi
+}
-- 
2.1.0



More information about the kexec mailing list