summaryrefslogtreecommitdiff
path: root/etc/initd/initd.suse
blob: 54d0d84aedc0a67c5e326869d9577f72d24d786b (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/bin/bash
#
# /etc/init.d/iscsi
#
### BEGIN INIT INFO
# Provides:          iscsi
# Required-Start:    $network
# Should-Start:      iscsitarget multipathd
# Required-Stop:     $network
# Should-Stop:       multipathd
# Default-Start:     3 5
# Default-Stop:      
# Short-Description: iSCSI initiator daemon
# Description:       The iSCSI initator is used to create and
#                    manage iSCSI connections to an iSCSI Target.
#
### END INIT INFO

CONFIG_FILE=/etc/iscsi/iscsid.conf
DAEMON=/sbin/iscsid
ISCSIADM=/sbin/iscsiadm
BRCM_ISCSIUIO=/sbin/brcm_iscsiuio
ARGS="-c $CONFIG_FILE -n"

# Source LSB init functions
. /etc/rc.status

# Reset status of this service
rc_reset

DM_MAJOR=$(sed -n 's/\(.*\) device-mapper/\1/p' /proc/devices)

iscsi_login_all_nodes()
{
	echo -n "Setting up iSCSI targets: "
	$ISCSIADM -m node --loginall=automatic 2> /dev/null
	if [ $? == 21 ] ; then
	    rc_failed 6
	fi
	rc_status -v
}

#
# Try to load all required modules prior to startup
#
iscsi_load_transport_modules()
{
    loaded=$(sed -n "/^iscsi_tcp/p" /proc/modules)
    if [ -z "$loaded" ] ; then
	modprobe iscsi_tcp
	if [ $? = 0 ] ; then
	    echo -n " tcp"
	fi
    fi

    for iface in /etc/iscsi/ifaces/*; do
	[ -f "$iface" ] || continue
	[ "$iface" = "iface.example" ] && continue
	# Check if the iface has been configured
	result=$(sed '/#.*/D;/iface.iscsi_ifacename/D;/iface.hwaddress/D;/iface.transport_name/D' $iface)
	if [ "$result" ] ; then
	    mod=$(sed -n 's/iface.transport_name *= *\(.*\)/\1/p' $iface)
	    loaded=$(sed -n "/^$mod/p" /proc/modules)
	    if [ -z "$loaded" ] ; then
		modprobe $mod
		if [ $? = 0 ] ; then
		    echo -n " $mod"
		fi
	    fi
	fi
    done
}

#
# Set a temporary startmode for ifdown
#
iscsi_modify_if_startmode()
{
    local ifname=$1
    local tmp_ifcfg=/dev/.sysconfig/network/if-$ifname

    if [ -e "$tmp_ifcfg" ] ; then
	. $tmp_ifcfg
	if [ "$startmode" ] ; then
	    return
	fi
    fi
    : disabling shutdown on $ifname
    echo "startmode=nfsroot" >> $tmp_ifcfg
}

iscsi_get_ifacename_from_session()
{
    local session=$1
    local ifacename

    ifacename=$(iscsiadm -m session -r ${session##.*/session} 2> /dev/null | \
	sed -n 's/iface.iscsi_ifacename = \(.*\)/\1/p')
    if [ -z "$ifacename" ] ; then
	# Check for iBFT
	ifacename=$(iscsiadm -m fw 2> /dev/null)
	if [ -n "$ifacename" ] ; then
	    ifacename="fw"
	fi
    fi
    echo $ifacename
}

iscsi_get_hwaddress_from_iface()
{
    local iface=$1
    local hwaddress

    hwaddress=$(iscsiadm -m iface -I "$iface" 2> /dev/null | sed -n 's/iface.hwaddress = \(.*\)/\1/p')
    [ "$hwaddress" = "<empty>" ] && hwaddress=

    echo $hwaddress
}

iscsi_get_ifname_from_iface()
{
    local iface=$1
    local ifname

    ifname=$(iscsiadm -m iface -I "$iface" 2> /dev/null | sed -n 's/iface.net_ifacename = \(.*\)/\1/p')
    [ "$ifname" = "<empty>" ] && ifname=

    echo $ifname
}

iscsi_get_ipaddr_from_iface()
{
    local iface=$1
    local ipaddr

    ipaddr=$(iscsiadm -m iface -I "$iface" 2> /dev/null | sed -n 's/iface.ipaddress = \(.*\)/\1/p')
    [ "$ipaddr" = "<empty>" ] && ipaddr=

    echo $ipaddr
}

iscsi_get_ifname_from_firmware()
{
    local hwaddress

    hwaddress=$(iscsiadm -m fw 2> /dev/null | sed -n 's/iface.net_ifacename = \(.*\)/\1/p')

    echo $hwaddress
}

#
# cxgb3i is using the HWAddress to select
# the correct interface
#
iscsi_get_ifname_from_hwaddress()
{
    local hwaddress=$1

    for if in /sys/class/net/*; do
	[ -e "$if" ] || continue
	read mac < $if/address
	[ "$mac" = "$hwaddress" ] || continue
	echo ${if##*/}
	break
    done
}

iscsi_get_ifname_from_ipaddr()
{
    local ipaddr=$1
    local ifname

    ifname=$(ip addr show to $ipaddr | sed -n 's/[0-9]*: \([^ :]*\): .*/\1/p')
    return $ifname
}

#
# Handle 'default' interface:
# It is basically impossible to determine via which
# interface the iSCSI traffic will flow, so we take
# the easy option and ignore _all_ active interfaces
# during shutdown
#
iscsi_modify_all_interfaces()
{
    ip link show up | sed -n '/.*LOOPBACK.*/d;s/[0-9]*: \(.*\): .*/\1/p' | while read ifname; do
	iscsi_modify_if_startmode $ifname
    done
}

#
# Check iface setting and disable
# affected network interfaces
#
iscsi_check_interface()
{
    local session=$1
    local i h n

    i=$(iscsi_get_ifacename_from_session $session)
    [ -z "$i" ] && continue
    if [ "$i" = "default" ] ; then
	iscsi_modify_all_interfaces
    elif [ "$i" = "fw" ] ; then
	n=$(iscsi_get_ifname_from_firmware)
    else
	n=$(iscsi_get_ifname_from_iface $i)
	if [ -z "$n" ] ; then
	    h=$(iscsi_get_hwaddress_from_iface $i)
	    if [ -n "$h" ] ; then
		n=$(iscsi_get_ifname_from_hwaddress $h)
	    fi
	fi
	if [ -z "$n" ] ; then
	    h=$(iscsi_get_ipaddr_from_iface $i)
	    if [ -n "$h" ] ; then
		n=$(iscsi_get_ifname_from_ipaddr $h)
	    fi
	fi
    fi
    if [ "$n" ] ; then
	iscsi_modify_if_startmode $n
    fi
}

#
# Check if device 'dev' is mounted
# Returns the mount point on success
#
iscsi_check_if_mounted()
{
    local dev=$1
    local d m t o x p

    cat /proc/mounts | sed -ne '/^\/dev\/.*/p' | while read d m t o x; do 
	if [ -L "$d" ] ; then
	    d=$(readlink -f $d)
	fi
	[ -b "$d" ] || continue

	b=$(ls -l $d | sed -n 's/.* \([0-9]*\), \([0-9]*\) .*/\1:\2/p')
	p=$(cd -P /sys/dev/block/$b ; echo $PWD)

	if [ -z "$p" ] ; then
	    d=${d##/dev}
	    p="/sys/block${d%%[0-9]*}"
	fi

	[ ! -d ${p} ] && continue

	if [ -e $p/partition ] ; then
	    p=$(cd -P $p/../; echo $PWD)
	fi
	if [ "$dev" = "${p##*/}" ] ; then
	    echo $m
	fi
    done
}

#
# Unwind block device stack
# 
# Stops unwinding if either no more 'holders'
# are found or if a device is mounted
# 
# Unmounts top-level device and deconfigures
# all devices down the stack
#
# Root fs is not unmounted
#
iscsi_unwind_stack()
{
    local p=$1
    local d=${p##*/}
    local u
    local m

    if [ ! -d ${p} ] ; then
	return;
    fi

    m=$(iscsi_check_if_mounted $d)
    if [ -z "$m" ] ; then
	for s in $p/holders/* ; do
	    [ -e $s ] || continue
	    p=$(cd -P $s; echo $PWD)
	    u=$(iscsi_unwind_stack $p)
	    if [ "$u" ] ; then
		echo -n "$u "
	    fi
	done
    else
	if [ "$m" = "/" ] ; then
	    echo -n "$d "
	    return 1
	fi
	if ! umount $m ; then
	    echo -n "$d "
	    return 1
	fi
    fi

    if [ "${d#dm-}" != "$d" ] ; then
	if ! dmsetup remove -j $DM_MAJOR -m ${d#dm-} 2> /dev/null ; then
	    echo -n "$d "
	    return 1
	fi
    fi

    if [ "${d#md}" != "$d" ] ; then
	if ! mdadm --manage /dev/$d --stop 2> /dev/null ; then
	    echo -n "$d "
	    return 1
	fi
    fi
    return 0
}

#
# Return all targets for a given session
#
iscsi_get_target()
{
    local session=$1
    local d

    for d in $session/device/target* ; do
	[ -e "$d" ] || continue
	echo "$d"
    done
}

#
# Checks all devices presented by a target
# and tries to umount them.
# Skip unmounting for the root fs.
# Stops on the first device which could not be unmounted
# and returns the mount device of that device.
#
iscsi_check_target()
{
    local t=$1
    local d b m

    for d in $t/* ; do
	[ -d $d/block ] || continue
	for b in $d/block/sd* ; do
	    [ -d "$b" ] || continue
	    m=$(iscsi_unwind_stack $b)
	    if [ -n "$m" ] ; then
		echo $m
		return 1
	    fi
	done
    done
}

#
# Check all sessions for mounted devices
# and shutdown the session if the affected
# devices could be umounted cleanly.
# If umount fails disable shutdown on all
# affected network interfaces
#
iscsi_stop_sessions()
{
    local t m s i

    i=0
    for session in /sys/class/iscsi_session/session* ; do
	[ -e "$session" ] || continue;
	[ -e $session/device ] || continue
	t=$(iscsi_get_target $session)
	m=$(iscsi_check_target $t)
	s=${session##*/session}
	if [ -z "$m" ] ; then
	    iscsiadm -m session -r ${s} -u
	    i=$(( $i + 1 ))
	else
	    iscsi_check_interface $s
	fi
    done
    echo $i
}

iscsi_list_all_nodes()
{
    # Check for active sessions
    if $ISCSIADM -m session > /dev/null; then
	return 0
    fi
    echo "Active connections:"
    $ISCSIADM -m session | while read proto num PORTAL TARGET ; do
	PORTAL=${PORTAL%,*}
	echo -e "\t$TARGET at $PORTAL"
    done
}

case "$1" in
    start)
	if checkproc $DAEMON ; then
	    RETVAL=0
	else
	    echo -n "Starting iSCSI initiator service: "
	    iscsi_load_transport_modules
	    if grep -q bnx2i /proc/modules && [ -x $BRCM_ISCSIUIO ] ; then
		startproc $BRCM_ISCSIUIO
	    fi
	    startproc $DAEMON $ARGS
	    RETVAL=$?
	    rc_status -v
	fi
	if [ "$RETVAL" == "0" ]; then
	    iscsi_login_all_nodes
	fi
	;;
    stop)
	n=$(iscsi_stop_sessions)
	echo -n "Stopping iSCSI initiator service: "
	if [ "$n" ] && [ "$n" != "0" ] ; then
	    m=$(iscsiadm -m session 2> /dev/null)
	    if [ -z "$m" ] ; then
		killproc -KILL $DAEMON
		RETVAL=$?
		if grep -q bnx2i /proc/modules && [ -x $BRCM_ISCSIUIO ]; then
		    killproc -KILL $BRCM_ISCSIUIO
		fi
 		RETVAL=$?
	    else
		RETVAL=1
	    fi
	    rc_failed $RETVAL
	    rc_status -v
	else
	    # umounting failed, leave initiator running
	    rc_status -s
	fi
	;;
    status)
	echo -n "Checking for iSCSI initiator service: "
	if checkproc $DAEMON ; then
	    rc_status -v
	    iscsi_list_all_nodes
	else
	    rc_failed 3
	    rc_status -v
	fi
	;;
    restart|reload)
	$0 stop
	RETVAL=$?
	if [ "$RETVAL" != "0" ]; then
		echo "Stopping iSCSI initiator service failed, not starting"
		exit $RETVAL
	fi
	sleep 1
	$0 start
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart|reload}"
	exit 1
	;;
esac
rc_exit