Debian Codegraph @

sbin/drbl-all-service

raw symbols SCIP
1 #!/bin/bash
2 # Authors: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw>
3 # License: GPL
4
5 # Load DRBL setting and functions
6 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
7
8 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
9 [ -e /etc/drbl/drbl-ocs.conf ] && . /etc/drbl/drbl-ocs.conf
10 [ -e $DRBL_SCRIPT_PATH/sbin/ocs-functions ] && . $DRBL_SCRIPT_PATH/sbin/ocs-functions
11
12 # main
13 USAGE() {
14 echo "Usage:"
15 echo "$0 {start|stop|restart|on|off}"
16 echo "start Start all DRBL-related services now"
17 echo "stop Stop all DRBL-related services now"
18 echo "restart Restart all DRBL-related services now"
19 echo "add Add all DRBL-related services"
20 echo "del Delete all DRBL-related services"
21 echo "Example: To start all DRBL-related services in this DRBL server"
22 echo "$0 start"
23 }
24
25 #
26 drbl_startup_service() {
27 local srv="$1"
28 local act="$2"
29 local ret
30 [ -z "$srv" ] && echo "No srv in function drbl_startup_service!" && exit 1
31 [ -z "$act" ] && echo "No act in function drbl_startup_service!" && exit 1
32 # Traditional service can be started or restart by "/etc/init.d/$srv (start|restart)
33 # Upstart service (from 0.6.3 in Ubuntu 9.04), we should use: restart $srv
34 if is_systemd_init && [ -e "/lib/systemd/system/$srv.service" ]; then
35 _real_service_name="$(basename $(readlink -f /lib/systemd/system/$srv.service) ".service")"
36 if [ "$_real_service_name" = "null" ]; then
37 echo "Service $srv.service is masked. Skiping $act it."
38 else
39 echo "Now $act $_real_service_name service by systemd command..."
40 systemctl $act $_real_service_name.service
41 ret=$?
42 fi
43 elif dpkg -L upstart 2>/dev/null | grep -F "/sbin/start" &>/dev/null && \
44 [ -e /etc/init/${srv}.conf ] && ! is_systemd_init; then
45 # This is upstart case. Especially we need to make sure /sbin/init is not linked to systemd. Since on Ubuntu 15.04, both upstart and systemd packages are installed. But the file /sbin/init is the key point.
46 if [ "$act" = "restart" ]; then
47 # When action is restart, we check the status first, and if it's already stopped, we use "start" instead of "restart", otherwise the service won't be staretd at all. "restart $srv" will just show us: "restart: Unknown instance:"
48 [ -n "$(LC_ALL=C status "$srv" | grep -Ei "stop")" ] && act="start"
49 fi
50 # A workaround to avoid portmap restarting issue. We must make sure portmap is not running. Ref: https://bugs.launchpad.net/ubuntu/+source/portmap/+bug/688550
51 if [ "$srv" = "portmap" -a "$act" = "start" ]; then
52 to_wait=""
53 while [ -z "$to_wait" ]; do
54 if [ -z "$(LC_ALL=C pidof portmap)" ]; then
55 to_wait="no"
56 else
57 sleep 0.5
58 fi
59 done
60 fi
61 initctl $act $srv
62 ret=$?
63 # A workaround to avoid portmap restarting issue. We must make sure portmap is started successfully.
64 # It might due to the "respawn" in /etc/init/portmap.conf, which causes it to be restarted in the background, and the start action we issue might conflict with that. For example in the /var/log/syslog on Ubuntu 11.04 with portmap 6.0.0-2ubuntu5 installed:
65 # May 3 08:24:18 natty portmap[795]: cannot bind tcp: Address already in use
66 # May 3 08:24:18 natty init: portmap main process (795) terminated with status 1
67 # May 3 08:24:18 natty init: portmap main process ended, respawning
68 # May 3 08:24:18 natty portmap: Removing stale lockfile for pid 795
69 # May 3 08:24:18 natty portmap[803]: cannot bind tcp: Address already in use
70 # May 3 08:24:18 natty init: portmap main process (803) terminated with status 1
71 # May 3 08:24:18 natty init: portmap main process ended, respawning
72 # May 3 08:24:18 natty portmap: Removing stale lockfile for pid 803
73 # May 3 08:24:18 natty portmap[811]: cannot bind tcp: Address already in use
74 # May 3 08:24:18 natty init: portmap main process (811) terminated with status 1
75 # May 3 08:24:18 natty init: portmap main process ended, respawning
76 # May 3 08:24:18 natty portmap: Removing stale lockfile for pid 811
77 # May 3 08:24:18 natty portmap[819]: cannot bind tcp: Address already in use
78 # May 3 08:24:18 natty init: portmap main process (819) terminated with status 1
79 # May 3 08:24:18 natty init: portmap main process ended, respawning
80 # May 3 08:24:18 natty portmap: Removing stale lockfile for pid 819
81 # May 3 08:24:18 natty portmap[827]: cannot bind tcp: Address already in use
82 # May 3 08:24:18 natty init: portmap main process (827) terminated with status 1
83 # May 3 08:24:18 natty init: portmap main process ended, respawning
84 # May 3 08:24:18 natty portmap: Removing stale lockfile for pid 827
85 # May 3 08:24:18 natty portmap[835]: cannot bind tcp: Address already in use
86 # May 3 08:24:18 natty init: portmap main process (835) terminated with status 1
87 # May 3 08:24:18 natty init: portmap respawning too fast, stopped
88 if [ "$ret" -ne 0 ]; then
89 if [ "$srv" = "portmap" -a "$act" = "start" ]; then
90 to_wait=""
91 echo -n "Waiting for portmap to be started successfully..."
92 while [ -z "$to_wait" ]; do
93 if [ -z "$(LC_ALL=C pidof portmap)" ]; then
94 initctl start portmap &>/dev/null
95 ret=$?
96 echo -n "."
97 sleep 0.5
98 else
99 to_wait="no"
100 fi
101 done
102 echo " done!"
103 fi
104 fi
105 else
106 echo "Now $act $srv service by SysV command..."
107 service $srv $act
108 ret=$?
109 fi
110 return $ret
111 } # end of drbl_startup_service
112
113 #
114 switch=$1
115
116 #
117 # $drbl_server_service_chklist is loaded from conf/drbl.conf
118 drbl_service=""
119 # check if the service listed exists
120 for iser in $drbl_server_service_chklist; do
121 # For SuSE, the NFS server name is nfsserver, not nfs (which is nfs client).
122 # Exclude nfs.
123 [ -e /etc/SuSE-release -a "${iser}" = "nfs" ] && continue
124 # 3 types of service path, /etc/init.d/ (sysv-init), /etc/init/ (upstart), and /lib/systemd/system/ (systemd)
125 if [ -e "/etc/init.d/${iser}" -o \
126 -e "/etc/init/${iser}.conf" -o \
127 -e "/lib/systemd/system/${iser}.service" ]; then
128 # For Ubuntu 15.04, nfs-common provides both rpc-statd.service for systemd and statd for sysvinit:
129 # /etc/init/statd.conf
130 # /lib/systemd/system/rpc-statd.service
131 # For systemd, we only need rpc-statd.service
132 if is_systemd_init && [ "${iser}" = statd ]; then
133 continue
134 fi
135 drbl_service="$drbl_service $iser"
136 fi
137 done
138 # When nfs-kernel-server and nfs-server co-exist in drbl_service, it means systemd covers both of them. Just keep one of them.
139 if [ -n "$(echo $drbl_service | grep -Ew nfs-kernel-server)" -a \
140 -n "$(echo $drbl_service | grep -Ew nfs-server)" ]; then
141 drbl_service="$(echo $drbl_service | sed -r -e "s|[[:space:]]*nfs-kernel-server||")"
142 fi
143 # tftp is a pseudo service (depending on xinetd) in CentOS 7
144 if [ -n "$(echo $drbl_service | grep -Ew "[[:space:]]*tftp" |\
145 grep -Ew "[[:space:]]*xinetd")" ]; then
146 drbl_service="$(echo $drbl_service | sed -r -e "s|[[:space:]]*tftp||")"
147 fi
148
149 # Final decision for the services
150 if [ "$use_existing_dhcp_srv" = "yes" ]; then
151 # Use dnsmasq to provide dhcp and tftp services.
152 drbl_service="$(echo "$drbl_service" | sed -e "s/$DHCP_SRV_NAME//g" \
153 -e "s/tftpd-hpa//")"
154 # Disable conflicting services, while later dnsmasq will be enabled.
155 systemctl disable $DHCP_SRV_NAME
156 service $DHCP_SRV_NAME stop
157 if [ -e /etc/debian_version ]; then
158 systemctl disable tftpd-hpa
159 service tftpd-hpa stop
160 else
161 # RH-like & SuSE
162 perl -p -i -e "s/disable.*=.*/disable = yes/g" /etc/xinetd.d/tftp
163 service xinetd restart
164 fi
165 else
166 # Use dhcpd and tftpd
167 # Disable conflicting service, while later dhcpd and tftpd will be enabled.
168 drbl_service="$(echo "$drbl_service" | sed -e "s/dnsmasq//g")"
169 systemctl disable dnsmasq
170 service dnsmasq stop
171 fi
172
173 #
174 check_if_root
175
176 #
177 if [ $# -ne 1 ]; then
178 USAGE
179 exit 1
180 fi
181
182 # store the orig LC_ALL, then set it as C
183 LC_ALL_org="$LC_ALL"
184 export LC_ALL=C
185
186 case "$switch" in
187 "start"|"restart")
188 # add them first
189 $0 add
190 # rm /var/lib/nfs/rmtab to avoid a long time try when restart NFS
191 [ -f /var/lib/nfs/rmtab ] && rm -f /var/lib/nfs/rmtab
192
193 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
194 echo "Now start the service: $drbl_service"
195 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
196 ret=""
197 for serv_st in $drbl_service; do
198 if [ -e /etc/debian_version -o -e /etc/SuSE-release ] ; then
199 # It's Debian/SuSE... no /var/lock/subsys/$serv_st tag, so we just restart it always.
200 to_restart=yes
201 else
202 # CentOS...
203 # Since not all the tag file in /var/lock/subsys is the same with service name, we have to convert it here.
204 # Ref: http://bugs.centos.org/view.php?id=5604
205 case "$serv_st" in
206 nfs)
207 if [ -n "$(grep -Ew /var/lock/subsys/nfsd /etc/init.d/nfs 2>/dev/null)" ]; then
208 serv_st_d="nfsd"
209 else
210 serv_st_d="nfs"
211 fi
212 ;;
213 nfslock) serv_st_d="rpc.statd";;
214 *) serv_st_d="$serv_st";;
215 esac
216 # NOT debian, it's RH-like, we can use the /var/lock/subsys/$serv_st
217 if [ -e /var/lock/subsys/$serv_st_d -o -e /var/run/$serv_st_d.pid ]; then
218 to_restart=yes
219 elif [ "$serv_st_d" = "nfs-server" ] ; then
220 to_restart=yes
221 else
222 to_restart=no
223 fi
224 fi
225
226 if [ "$to_restart" = "yes" ] ; then
227 # service is running
228 # Better not to use restart:
229 # drbl_startup_service $serv_st restart
230 # Otherwise it might give false status. E.g rpcbind 0.2.0-6 on Debian:
231 # /etc/init.d/rpcbind restart
232 # Stopping rpcbind daemon....
233 # Starting rpcbind daemon...Already running.. <--
234 # root@debian:/tmp# pidof rpcbind
235 # root@debian:/tmp# <-- Actually it's not running.
236 drbl_startup_service $serv_st stop
237 sleep 1
238 drbl_startup_service $serv_st start
239 RETVAL=$?
240 else
241 # service is stopped
242 #/etc/init.d/$serv_st start
243 drbl_startup_service $serv_st start
244 RETVAL=$?
245 fi
246 if [ "$RETVAL" -gt 0 ]; then
247 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
248 echo "XXXXXXX XXXXXXX XXXXXXX"
249 echo "Failed to start service $serv_st !!!"
250 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
251 fi
252 ret=$[$ret + $RETVAL]
253 done
254 turn_on_ipv4_forward
255 ;;
256 "add")
257 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
258 echo "Now add the service: $drbl_service"
259 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
260 for serv in $drbl_service; do
261 # Check if systemd service first.
262 if is_systemd_init && [ -e "/lib/systemd/system/${serv}.service" ]; then
263 # For systemd, systemctl can't en/disable if service name be a alias name, ex: nfslock, due to 'nfslock' -> 'nfs-lock'
264 _real_unit_file=$(readlink -f /lib/systemd/system/${serv}.service)
265 _real_service_name=$(basename "$_real_unit_file" ".service")
266 if [ "$_real_service_name" = "null" ]; then
267 continue
268 fi
269 # Only enable it if it has [Install] section. Otherwise systemctl will complain.
270 # We check this to avoid the warning message:
271 # "The unit files have no installation config... This means they are not meant to be enabled or disabled using systemctl."
272 if grep -qE "^\[Install\]" "$_real_unit_file" 2>/dev/null; then
273 # If it's linked in /etc/systemd/system/, systemctl enable might fail with "Refusing to operate on linked unit file".
274 # We should remove it if it's a link to /lib/systemd/system/.
275 if [ -L "/etc/systemd/system/${_real_service_name}.service" ]; then
276 if [ "$(readlink -f "/etc/systemd/system/${_real_service_name}.service")" = "$_real_unit_file" ]; then
277 rm -f "/etc/systemd/system/${_real_service_name}.service"
278 fi
279 fi
280 echo "Force to add $_real_service_name service by systemd command..."
281 if [ "$_real_service_name" = 'iptables' -a "$(systemctl is-enabled firewalld.service 2>/dev/null)" = 'enable' ]; then
282 # On FC18, firewalld could conflict with iptables, so disable firewalld from now on
283 echo "Stopping and disabling firewalld service by: 'systemctl stop/disable firewalld.service'..."
284 systemctl stop firewalld.service
285 systemctl disable firewalld.service
286 fi
287 systemctl enable $_real_service_name.service
288 fi
289 elif [ -e /etc/debian_version ]; then
290 # Debian-like
291 echo "Force to add $serv service by SysV command (insserv)..."
292 insserv $serv &>/dev/null
293 elif [ -e /etc/SuSE-release ]; then
294 # SuSE
295 echo "Force to add $serv service by SysV command (insserv)..."
296 insserv $serv &>/dev/null
297 else
298 # RH-like
299 echo "Force to add $serv service by SysV command (chkconfig)..."
300 chkconfig $serv on
301 fi
302 done
303 ;;
304 "del")
305 # stop them first
306 $0 stop
307 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
308 echo "Now delete the service: $drbl_service"
309 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
310 for serv in $drbl_service; do
311 if [ -e /etc/debian_version ]; then
312 # Debian-like
313 echo "Force to delete $serv service by SysV command (insserv)..."
314 insserv -r $serv &>/dev/null
315 elif [ -e /etc/SuSE-release ]; then
316 # SuSE
317 echo "Force to delete $serv service by SysV command (insserv)..."
318 insserv -f -r $serv &>/dev/null
319 elif is_systemd_init && [ -e "/lib/systemd/system/${serv}.service" ]; then
320 # For systemd, systemctl can't en/disable if service name be a alias name, ex: nfslock, due to 'nfslock' -> 'nfs-lock'
321 _real_unit_file=$(readlink -f /lib/systemd/system/${serv}.service)
322 _real_service_name=$(basename "$_real_unit_file" ".service")
323 if [ "$_real_service_name" = "null" ]; then
324 continue
325 fi
326 # Only disable it if it has [Install] section. Otherwise systemctl will complain.
327 if grep -qE "^\[Install\]" "$_real_unit_file" 2>/dev/null; then
328 echo "Force to del $_real_service_name service by systemd command..."
329 systemctl disable $_real_service_name.service
330 # If it's linked in /etc/systemd/system/, systemctl disable might not remove it if it was linked by path.
331 if [ -L "/etc/systemd/system/${_real_service_name}.service" ]; then
332 if [ "$(readlink -f "/etc/systemd/system/${_real_service_name}.service")" = "$_real_unit_file" ]; then
333 rm -f "/etc/systemd/system/${_real_service_name}.service"
334 fi
335 fi
336 fi
337 else
338 # RH-like
339 echo "Force to delete $serv service by SysV command (chkconfig)..."
340 chkconfig --del $serv
341 fi
342 done
343 ;;
344 "stop")
345 ret=""
346 for serv_st in $drbl_service; do
347 if [ -e /etc/debian_version -o -e /etc/SuSE-release ] ; then
348 # It's Debian... no /var/lock/subsys/$serv_st tag, so we just stop it always.
349 # (or SuSE ? not sure... since SuSE uses lock file differs from that service name).
350 to_stop=yes
351 else
352 # CentOS...
353 # We can use the /var/lock/subsys/$serv_st
354 # Since not all the tag file in /var/lock/subsys is the same with service name, we have to convert it here.
355 # Ref: http://bugs.centos.org/view.php?id=5604
356 case "$serv_st" in
357 nfs)
358 if [ -n "$(grep -Ew /var/lock/subsys/nfsd /etc/init.d/nfs 2>/dev/null)" ]; then
359 serv_st_d="nfsd"
360 else
361 serv_st_d="nfs"
362 fi
363 ;;
364 nfslock) serv_st_d="rpc.statd";;
365 *) serv_st_d="$serv_st";;
366 esac
367 if [ -e /var/lock/subsys/$serv_st_d -o -e /var/run/$serv_st_d.pid ]; then
368 to_stop=yes
369 elif [ "$serv_st_d" = "nfs-server" ] ; then
370 to_stop=yes
371 else
372 to_stop=no
373 fi
374 fi
375 if [ "$to_stop" = "yes" ] ; then
376 # service is running
377 RETVAL=0
378 #/etc/init.d/$serv_st stop
379 drbl_startup_service $serv_st stop
380 RETVAL=$?
381 if [ "$RETVAL" -gt 0 ]; then
382 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
383 echo "XXXXXXX XXXXXXX XXXXXXX"
384 echo "Failed to stop service $serv_st !!!"
385 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
386 fi
387 ret=$[$ret + $RETVAL]
388 fi
389 done
390 ;;
391 *)
392 USAGE
393 ret=1
394 ;;
395 esac
396
397 #restore the old LC_ALL
398 export LC_ALL=$LC_ALL_org
399
400 exit $ret