aboutsummaryrefslogtreecommitdiffstats
path: root/main/open-iscsi/iscsid.initd
blob: f48eacc0e5fcbe8669c5a9ee8d5973e714a517d0 (plain) (blame)
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
103
104
105
106
#!/sbin/openrc-run
# Copyright 1999-2015 Gentoo Foundation, Inc.
# Distributed under the terms of the GNU General Public License, v2 or later

command=/sbin/iscsid
command_args="${OPTS}"
start_stop_daemon_args="-w 100" # iscsid might fail e.g. when the iSCSI kernel modules aren't available
pidfile=${PIDFILE:-/var/run/${SVCNAME}.pid}

extra_started_commands="starttargets stoptargets"
extra_commands="restarttargets"

ISCSIADM=/sbin/iscsiadm

depend() {
	after modules multipath
	use net
}

checkconfig() {
	if [ ! -e /etc/conf.d/${SVCNAME} ]; then
		eerror "Config file /etc/conf.d/${SVCNAME} does not exist!"
		return 1
	fi
	if [ ! -e "${CONFIG_FILE}" ]; then
		eerror "Config file ${CONFIG_FILE} does not exist!"
		return 1
	fi

	if [ -e ${INITIATORNAME_FILE} ]; then
		. ${INITIATORNAME_FILE}
	fi
	if [ ! -e ${INITIATORNAME_FILE} -o -z "${InitiatorName}" ]; then
		ewarn "${INITIATORNAME_FILE} should contain a string with your initiatior name."
		local IQN=$(/sbin/iscsi-iname)
		ebegin "Creating InitiatorName ${IQN} in ${INITIATORNAME_FILE}"
		echo "InitiatorName=${IQN}" >> "${INITIATORNAME_FILE}"
		eend $?
	fi
}

starttargets() {
	ebegin "Setting up iSCSI targets"
	$ISCSIADM -m node --loginall=automatic
	local ret=$?
	eend $ret
	return $ret
}

stoptargets() {
	ebegin "Disconnecting iSCSI targets"
	sync
	$ISCSIADM -m node --logoutall=all
	local ret=$?

	if [ $ret -eq 21 ]; then
		# See man iscsiadm(8)
		einfo "No active sessions to disconnect"
		eend 0
		return 0
	fi

	eend $ret
	return $ret
}

restarttargets() {
        stoptargets
        starttargets
}

status() {
	ebegin "Showing current active iSCSI sessions"
	$ISCSIADM -m session
}


start_pre() {
	local ret=1

	ebegin "Checking Open-iSCSI configuration"
	checkconfig
	ret=$?
	if [ $ret -ne 0 ]; then
		eend 1
		return 1
	fi
	eend 0
}

start_post() {
	# Start automatic targets when iscsid is started
	if [ "${AUTOSTARTTARGETS}" = "yes" ]; then
		starttargets
		local ret=$?
		if [ "${AUTOSTART}" = "strict" -a $ret -ne 0 ]; then
			stop
			return $ret
		fi
	fi
	return 0
}

stop_pre() {
	stoptargets
}