/
srptools.init
124 lines (105 loc) · 2.78 KB
/
srptools.init
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
#!/bin/bash
### BEGIN INIT INFO
# Provides: srptools
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Discovers SRP scsi targets.
# Description: Discovers SRP scsi over infiniband targets.
### END INIT INFO
DAEMON=/usr/sbin/srp_daemon
IBDIR=/sys/class/infiniband
PORTS=""
RETRIES=""
RETRIES_DEFAULT=60
LOG=""
LOG_DEFAULT=/var/log/srp_daemon.log
[ -x $DAEMON ] || exit 0
. /lib/lsb/init-functions
[ -f /etc/default/srptools ] && . /etc/default/srptools
max() {
echo $(($1 > $2 ? $1 : $2))
}
run_daemon() {
# srp does not background itself; using the start-stop-daemon background
# function causes us to lose stdout, which is where it logs to
nohup start-stop-daemon --start --quiet -m \
--pidfile "/var/run/srp_daemon.${HCA_ID}.${PORT}" \
--exec $DAEMON -- -e -c -n \
-i "${HCA_ID}" -p "${PORT}" -R "${RETRIES:-${RETRIES_DEFAULT}}" \
>> "${LOG:-${LOG_DEFAULT}}" 2>&1 &
RETVAL=$(max "$RETVAL" $?)
}
# Evaluate shell command $1 for every port in $PORTS
for_all_ports() {
local cmd=$1 p
if [ "$PORTS" = "ALL" ]; then
for p in ${IBDIR}/*/ports/*; do
[ -e "$p" ] || continue
PORT=$(basename "$p")
HCA_ID=$(basename "$(dirname "$(dirname "$p")")")
eval "$cmd"
done
else
for ADAPTER in $PORTS; do
HCA_ID=${ADAPTER%%:*}
PORT=${ADAPTER#${HCA_ID}:}
[ -n "$HCA_ID" ] && [ -n "$PORT" ] && eval "$cmd"
done
fi
}
start_daemon() {
local RETVAL=0
if [ "$PORTS" = "NONE" ] ; then
echo "srptools disabled."
exit 0
fi
for_all_ports run_daemon
case $RETVAL in
0) log_success_msg "started $DAEMON";;
*) log_failure_msg "failed to start $DAEMON";;
esac
return $RETVAL
}
stop_daemon() {
local RETVAL=0 PORTS=ALL
for_all_ports 'start-stop-daemon --stop --quiet --oknodo -m --pidfile "/var/run/srp_daemon.${HCA_ID}.${PORT}"; RETVAL=$(max $RETVAL $?)'
case $RETVAL in
0) log_success_msg "stopped $DAEMON";;
*) log_failure_msg "failed to stop $DAEMON";;
esac
return $RETVAL
}
check_status() {
local pidfile=$1 pid
[ -e "$pidfile" ] || return 3 # not running
pid=$(<"$pidfile")
[ -n "$pid" ] || return 3 # not running
[ -d "/proc/$pid" ] || return 1 # not running and pid file exists
return 0 # running
}
daemon_status() {
local RETVAL=0
for_all_ports 'check_status /var/run/srp_daemon.${HCA_ID}.${PORT} $DAEMON; RETVAL=$(max $RETVAL $?)'
case $RETVAL in
0) log_success_msg "$DAEMON is running";;
*) log_failure_msg "$DAEMON is not running";;
esac
return $RETVAL
}
case "$1" in
start)
start_daemon
;;
stop)
stop_daemon
;;
status)
daemon_status
;;
restart | reload | force-reload )
stop_daemon
start_daemon
;;
esac