summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorLee Duncan <lduncan@suse.com>2021-12-03 09:55:13 -0800
committerLee Duncan <lduncan@suse.com>2021-12-03 09:55:13 -0800
commit6a631b538c721bbadd2889d3bc609c0e172aeced (patch)
tree33e32f51e2705f63f1ae955562162e7590cc5443 /utils
parenta2158a7bb73950cc378a99df45fb2e504dab0d90 (diff)
downloadopen-iscsi-6a631b538c721bbadd2889d3bc609c0e172aeced.tar.gz
Update the iscsi-gen-initiatorname script: harden and generalize
The iscsi-gen-initiatorname script seems only to be used by the SUSE distrubution, so it has never been very "general". This update makes the script more resiliant, with better error messages if a problem occurs, as well as more generic, allowing the caller to set the "base IQN" value, though the default contains to be the same. Changes: - Added use of getopts to parse options - Added a "-h" option for help - Added an option to supply base IQN - Added better checking of options and params - Added comments to the script itself - Now check to see if iname file can be written to (handling read-only mounts better) - Fixed initiator name listed in file (it was wrong) - Removed use of printf(1) - Fixed iBFT initiator name setting in general - Now set iname file mode to 0600 even when it comes from iBFT - Cleaned up the script to use variables instead of repeating file pathnames or other values
Diffstat (limited to 'utils')
-rwxr-xr-xutils/iscsi-gen-initiatorname72
1 files changed, 50 insertions, 22 deletions
diff --git a/utils/iscsi-gen-initiatorname b/utils/iscsi-gen-initiatorname
index 3e30465..a375968 100755
--- a/utils/iscsi-gen-initiatorname
+++ b/utils/iscsi-gen-initiatorname
@@ -8,23 +8,47 @@
# This script is licensed under the GPL.
#
-if [ "$1" ] ; then
- if [ "$1" = "-f" ] ; then
- FORCE=1
- else
- echo "Invalid option $1"
- echo "Usage: $0 [-f]"
- exit 1
- fi
+NAME="$0"
+INAME_FILE="/etc/iscsi/initiatorname.iscsi"
+IQN_BASE="iqn.1996-04.de.suse:01"
+
+usage_and_exit()
+{
+ xit_val=$1
+
+ echo "Usage: $NAME [OPTIONS] -- generate an iSCSI initiatorname"
+ echo "Where OPTIONS are from:"
+ echo " -h print usage and exit"
+ echo " -f overwrite existing initiator name, if any"
+ echo " -b IQN-BASE use IQN-BASE for the IQN generated (default $IQN_BASE)"
+ exit $xit_val
+}
+
+while getopts "hfb:" o ; do
+ case "${o}" in
+ h) usage_and_exit 0 ;;
+ f) FORCE=1 ;;
+ b) IQN_BASE=${OPTARG} ;;
+ ?) usage_and_exit 1 ;;
+ esac
+done
+shift $(($OPTIND-1))
+
+if [ "$#" -gt 0 ] ; then
+ echo "Invalid argument(s): $*"
+ usage_and_exit
fi
+# use the iBFT initiator name, if present
if [ -d /sys/firmware/ibft/initiator ] ; then
read iSCSI_INITIATOR_NAME < /sys/firmware/ibft/initiator/initiator-name
fi
-if [ -f /etc/iscsi/initiatorname.iscsi -a -z "$FORCE" ] ; then
+# if we have an iBFT initiator name and an initiator name
+# file, they had better match, unless "force" is set
+if [ -f $INAME_FILE -a -z "$FORCE" ] ; then
if [ "$iSCSI_INITIATOR_NAME" ] ; then
- eval $(cat /etc/iscsi/initiatorname.iscsi | sed -e '/^#/d')
+ eval $(cat $INAME_FILE | sed -e '/^#/d')
if [ "$iSCSI_INITIATOR_NAME" != "$InitiatorName" ] ; then
echo "iSCSI Initiatorname from iBFT is different from the current setting."
echo "Please call '@SBINDIR@/iscsi-gen-initiatorname -f' to update the iSCSI Initiatorname."
@@ -33,10 +57,12 @@ if [ -f /etc/iscsi/initiatorname.iscsi -a -z "$FORCE" ] ; then
fi
fi
+# if we have an initiator name from iBFT or from
+# an existing initiator name file, use it
if [ "$iSCSI_INITIATOR_NAME" ] ; then
- cat << EOF >> /etc/iscsi/initiatorname.iscsi
-##
-## /etc/iscsi/iscsi.initiatorname
+ echo "##" > $INAME_FILE || exit 1
+ echo "## $INAME_FILE" >> $INAME_FILE
+ cat << EOF >> $INAME_FILE
##
## iSCSI Initiatorname taken from iBFT BIOS tables.
##
@@ -48,14 +74,16 @@ if [ "$iSCSI_INITIATOR_NAME" ] ; then
## @SBINDIR@/iscsi-gen-initiatorname -f
## to recreate an updated version of this file.
##
-InitiatorName=$iSCSI_INITIATOR_NAME
EOF
+ echo "InitiatorName=$iSCSI_INITIATOR_NAME" >> $INAME_FILE
+ chmod 0600 $INAME_FILE
fi
-if [ ! -f /etc/iscsi/initiatorname.iscsi ] ; then
- cat << EOF >> /etc/iscsi/initiatorname.iscsi
-##
-## /etc/iscsi/iscsi.initiatorname
+# if we still do not have an initiator name, create one
+if [ ! -f $INAME_FILE ] ; then
+ echo "##" > $INAME_FILE || exit 1
+ echo "## $INAME_FILE" >> $INAME_FILE
+ cat << EOF >> $INAME_FILE
##
## Default iSCSI Initiatorname.
##
@@ -65,9 +93,9 @@ if [ ! -f /etc/iscsi/initiatorname.iscsi ] ; then
## may reject this initiator. The InitiatorName must be unique
## for each iSCSI initiator. Do NOT duplicate iSCSI InitiatorNames.
EOF
- ISSUEDATE="1996-04"
- INAME=$(@SBINDIR@/iscsi-iname -p iqn.$ISSUEDATE.de.suse:01)
- printf "InitiatorName=$INAME\n" >>/etc/iscsi/initiatorname.iscsi
- chmod 0600 /etc/iscsi/initiatorname.iscsi
+ # create a unique initiator name using iscsi-iname
+ INAME=$(@SBINDIR@/iscsi-iname -p "$IQN_BASE")
+ echo "InitiatorName=$INAME" >> $INAME_FILE
+ chmod 0600 $INAME_FILE
fi