summaryrefslogtreecommitdiff
path: root/utils/iscsi-gen-initiatorname.sh.template
blob: 3fa8dccf35cff68156b62c774343a0f8c4eb6c8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
#
# iscsi-gen-initiatorname
#
# Generate a default iSCSI Initiatorname for SUSE installations.
#
# Copyright (c) 2022 Hannes Reinecke, SUSE Labs
# This script is licensed under the GPL.
#

NAME="$0"
INAME_FILE="@HOMEDIR@/initiatorname.iscsi"
IQN_PREFIX="iqn.1996-04.de.suse:01"

IBFT_COMMENTS="\
##
## iSCSI Initiatorname taken from iBFT BIOS tables.
##
## DO NOT EDIT OR REMOVE THIS FILE! 
## If you remove this file, the iSCSI daemon will not start.
## Any change here will not be reflected to the iBFT BIOS tables.
## If a different initiatorname is required please change the 
## initiatorname in the BIOS setup and call
## @SBINDIR@/iscsi-gen-initiatorname -f
## to recreate an updated version of this file.
##"

NORMAL_COMMENTS="\
##
## Default iSCSI Initiatorname.
##
## DO NOT EDIT OR REMOVE THIS FILE! 
## If you remove this file, the iSCSI daemon will not start.
## If you change the InitiatorName, existing access control lists
## may reject this initiator.  The InitiatorName must be unique
## for each iSCSI initiator.  Do NOT duplicate iSCSI InitiatorNames."

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 "   -p IQN-PRE  set the prefix for the IQN generated (default $IQN_PREFIX)"
    exit $xit_val
}

while getopts "hfp:" o ; do
    case "${o}" in
	h) usage_and_exit 0 ;;
	f) FORCE=1 ;;
	p) IQN_PREFIX=${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 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 $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."
	    exit 1
	fi
    fi
fi

# if we have an initiator name from iBFT or from
# an existing initiator name file, use it
if [ "$iSCSI_INITIATOR_NAME" ] ; then
    echo "##" > $INAME_FILE || exit 1
    echo "## $INAME_FILE" >> $INAME_FILE
    echo "$IBFT_COMMENTS" >> $INAME_FILE
    echo "InitiatorName=$iSCSI_INITIATOR_NAME" >> $INAME_FILE
    chmod 0600 $INAME_FILE
fi

# 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
    echo "$NORMAL_COMMENTS" >> $INAME_FILE
    # create a unique initiator name using iscsi-iname
    INAME=$(@SBINDIR@/iscsi-iname -p "$IQN_PREFIX")
    echo "InitiatorName=$INAME" >> $INAME_FILE
    chmod 0600 $INAME_FILE
fi