Lines 1-124
Link Here
|
1 |
#!/sbin/openrc-run |
1 |
#!/sbin/openrc-run |
2 |
# Copyright 1999-2015 Gentoo Foundation |
2 |
# Copyright 1999-2019 Gentoo Authors |
3 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# Distributed under the terms of the GNU General Public License v2 |
4 |
|
4 |
|
5 |
TGTD_CONFIG=/etc/tgt/targets.conf |
5 |
# shellcheck shell=sh |
6 |
|
6 |
|
7 |
TASK=$1 |
7 |
# Default configuration fike |
|
|
8 |
: "${tgtd_conf:=/etc/tgt/targets.conf}" |
9 |
|
10 |
pidfile="/var/run/${RC_SVCNAME}.pid" |
11 |
command="/usr/sbin/tgtd" |
12 |
command_args_background="--pid-file ${pidfile}" |
13 |
extra_commands="forcedstop" |
14 |
extra_started_commands="forcedreload reload" |
8 |
|
15 |
|
9 |
depend() { |
16 |
depend() { |
10 |
need net |
17 |
use net |
11 |
} |
18 |
} |
12 |
|
19 |
|
13 |
start() { |
20 |
start_post() { |
14 |
ebegin "Starting target framework daemon" |
21 |
# We need to wait for 1 second before do anything with tgtd. |
15 |
ebegin "Starting ${SVCNAME}" |
22 |
sleep 1 |
16 |
# Start tgtd first. |
23 |
# Put tgtd into "offline" state until all the targets are configured. |
17 |
start-stop-daemon --start --quiet \ |
|
|
18 |
--name tgtd \ |
19 |
--exec /usr/sbin/tgtd -- \ |
20 |
${TGTD_OPTS} |
21 |
RETVAL=$? |
22 |
if [ "$RETVAL" -ne 0 ] ; then |
23 |
echo "Could not start tgtd (is tgtd already running?)" |
24 |
exit 1 |
25 |
fi |
26 |
# We need to wait for 1 second before do anything with tgtd |
27 |
sleep 1 |
28 |
# Put tgtd into "offline" state until all the targets are configured. |
29 |
# We don't want initiators to (re)connect and fail the connection |
24 |
# We don't want initiators to (re)connect and fail the connection |
30 |
# if it's not ready |
25 |
# if configuration is not ready. |
31 |
tgtadm --op update --mode sys --name State -v offline |
26 |
tgtadm --op update --mode sys --name State -v offline |
|
|
27 |
|
32 |
# Configure the targets. |
28 |
# Configure the targets. |
33 |
tgt-admin --update ALL -c $TGTD_CONFIG |
29 |
if [ ! -r "${tgtd_conf}" ]; then |
34 |
# Put tgtd into "ready" state. |
30 |
ewarn "Configuration file '${tgtd_conf}' not found!" |
35 |
tgtadm --op update --mode sys --name State -v ready |
31 |
ewarn "Leaving ${SVCNAME} running in 'offline' state." |
36 |
eend $? |
32 |
eend 0 |
|
|
33 |
else |
34 |
ebegin "Loading target configuration" |
35 |
tgt-admin --update ALL -c "${tgtd_conf}" |
36 |
retval=$? |
37 |
if [ $retval -ne 0 ]; then |
38 |
eerror "Could not load configuration!" |
39 |
stop |
40 |
exit $? |
41 |
fi |
42 |
eend $retval |
43 |
|
44 |
# Put tgtd into "ready" state. |
45 |
ebegin "Onlining targets. Accepting connections" |
46 |
tgtadm --op update --mode sys --name State -v ready |
47 |
eend $? |
48 |
fi |
37 |
} |
49 |
} |
38 |
|
50 |
|
39 |
stop() { |
51 |
stop() { |
40 |
ebegin "Stopping ${SVCNAME}" |
52 |
ebegin "Stopping ${SVCNAME}" |
41 |
ebegin "Stopping target framework daemon" |
53 |
# We need to force shutdown if system is restarting |
42 |
# start-stop-daemon --stop --exec /usr/sbin/tgtd --quiet |
54 |
# or shutting down. |
43 |
if [ "$RUNLEVEL" = 0 -o "$RUNLEVEL" = 6 ] ; then |
55 |
if [ "$RC_RUNLEVEL" = "shutdown" ] ; then |
44 |
forcedstop |
56 |
forcedstop |
45 |
fi |
57 |
else |
46 |
# Remove all targets. It only removes targets which are not in use. |
58 |
# Remove all targets. Only remove targets which are not in use. |
47 |
tgt-admin --update ALL -c /dev/null >/dev/null 2>&1 |
59 |
tgt-admin --update ALL -c /dev/null >/dev/null 2>&1 |
48 |
# tgtd will exit if all targets were removed |
60 |
retval=$? |
49 |
tgtadm --op delete --mode system >/dev/null 2>&1 |
61 |
if [ $retval -eq 107 ] ; then |
50 |
RETVAL=$? |
62 |
einfo "tgtd is not running" |
51 |
if [ "$RETVAL" -eq 107 ] ; then |
63 |
else |
52 |
echo "tgtd is not running" |
64 |
# tgtd will exit if all targets were removed. |
53 |
if [ "$TASK" != "restart" ] ; then |
65 |
tgtadm --op delete --mode system >/dev/null 2>&1 |
54 |
exit 1 |
66 |
retval=$? |
|
|
67 |
if [ $retval -ne 0 ] ; then |
68 |
eerror "WARNING: Some initiators are still connected - could not stop tgtd" |
69 |
fi |
70 |
fi |
55 |
fi |
71 |
fi |
56 |
elif [ "$RETVAL" -ne 0 ] ; then |
72 |
eend $retval |
57 |
echo "Some initiators are still connected - could not stop tgtd" |
|
|
58 |
exit 2 |
59 |
fi |
60 |
# echo -n |
61 |
eend $? |
62 |
} |
73 |
} |
63 |
|
74 |
|
64 |
forcedstop() { |
75 |
forcedstop() { |
65 |
# NOTE: Forced shutdown of the iscsi target may cause data corruption |
76 |
# NOTE: Forced shutdown of the iscsi target may cause data corruption |
66 |
# for initiators that are connected. |
77 |
# for initiators that are connected. |
67 |
echo "Force-stopping target framework daemon" |
78 |
ewarn "WARNING: Force-stopping target framework daemon" |
|
|
79 |
for i in 5 4 3 2 1; do |
80 |
einfo "Continuing in $i seconds..." |
81 |
sleep 1 |
82 |
done |
83 |
|
68 |
# Offline everything first. May be needed if we're rebooting, but |
84 |
# Offline everything first. May be needed if we're rebooting, but |
69 |
# expect the initiators to reconnect cleanly when we boot again |
85 |
# expect the initiators to reconnect cleanly when we boot again |
70 |
# (i.e. we don't want them to reconnect to a tgtd which is still |
86 |
# (i.e. we don't want them to reconnect to a tgtd which is still |
71 |
# working, but the target is gone). |
87 |
# onlineg, but the target is gone). |
72 |
tgtadm --op update --mode sys --name State -v offline >/dev/null 2>&1 |
88 |
tgtadm --op update --mode sys --name State -v offline >/dev/null 2>&1 |
73 |
RETVAL=$? |
89 |
retval=$? |
74 |
if [ "$RETVAL" -eq 107 ] ; then |
90 |
if [ $retval -eq 107 ] ; then |
75 |
echo "tgtd is not running" |
91 |
einfo "tgtd is not running" |
76 |
if [ "$TASK" != "restart" ] ; then |
|
|
77 |
exit 1 |
78 |
fi |
79 |
else |
92 |
else |
|
|
93 |
# Offline all targets |
80 |
tgt-admin --offline ALL |
94 |
tgt-admin --offline ALL |
|
|
95 |
|
81 |
# Remove all targets, even if they are still in use. |
96 |
# Remove all targets, even if they are still in use. |
82 |
tgt-admin --update ALL -c /dev/null -f |
97 |
tgt-admin --update ALL -c /dev/null -f |
83 |
# It will shut down tgtd only after all targets were removed. |
98 |
|
|
|
99 |
# tgtd shuts down after all targets are removed. |
84 |
tgtadm --op delete --mode system |
100 |
tgtadm --op delete --mode system |
85 |
RETVAL=$? |
101 |
retval=$? |
86 |
if [ "$RETVAL" -ne 0 ] ; then |
102 |
if [ $retval -ne 0 ] ; then |
87 |
echo "Failed to shutdown tgtd" |
103 |
eerror "Failed to shutdown tgtd" |
88 |
exit 1 |
104 |
eend 1 |
89 |
fi |
105 |
fi |
90 |
fi |
106 |
fi |
91 |
echo -n |
107 |
eend $retval |
92 |
} |
108 |
} |
93 |
|
109 |
|
94 |
reload() { |
110 |
reload() { |
95 |
echo "Updating target framework daemon configuration" |
111 |
ebegin "Updating target framework daemon configuration" |
96 |
# Update configuration for targets. Only targets which |
112 |
# Update configuration for targets. Only targets which |
97 |
# are not in use will be updated. |
113 |
# are not in use will be updated. |
98 |
tgt-admin --update ALL -c $TGTD_CONFIG >/dev/null 2>&1 |
114 |
tgt-admin --update ALL -c "${tgtd_conf}" >/dev/null 2>&1 |
99 |
RETVAL=$? |
115 |
retval=$? |
100 |
if [ "$RETVAL" -eq 107 ] ; then |
116 |
if [ $retval -eq 107 ]; then |
101 |
echo "tgtd is not running" |
117 |
ewarn "WARNING: tgtd is not running" |
102 |
exit 1 |
118 |
fi |
103 |
fi |
119 |
eend $retval |
104 |
} |
120 |
} |
105 |
|
121 |
|
106 |
forcedreload() { |
122 |
forcedreload() { |
107 |
echo "Force-updating target framework daemon configuration" |
123 |
ebegin "Updating target framework daemon configuration" |
108 |
# Update configuration for targets, even those in use. |
124 |
ewarn "WARNING: Force-updating running configuration!" |
109 |
tgt-admin --update ALL -f -c $TGTD_CONFIG >/dev/null 2>&1 |
125 |
# Update configuration for targets, even those in use. |
110 |
RETVAL=$? |
126 |
tgt-admin --update ALL -f -c "${tgtd_conf}" >/dev/null 2>&1 |
111 |
if [ "$RETVAL" -eq 107 ] ; then |
127 |
retval=$? |
112 |
echo "tgtd is not running" |
128 |
if [ $retval -eq 107 ]; then |
113 |
exit 1 |
129 |
ewarn "WARNING: tgtd is not running" |
114 |
fi |
130 |
fi |
|
|
131 |
eend $retval |
115 |
} |
132 |
} |
116 |
|
133 |
|
117 |
status() { |
134 |
status_post() { |
118 |
TGTD_PROC=$(pidof -c -o $$ -o %PPID tgtd) |
135 |
einfo "Run 'tgt-admin -s' to see detailed target info." |
119 |
if [ -n "$TGTD_PROC" ] ; then |
|
|
120 |
echo "tgtd is running. Run 'tgt-admin -s' to see detailed target info." |
121 |
else |
122 |
echo "tgtd is NOT running." |
123 |
fi |
124 |
} |
136 |
} |