VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testboxscript/linux/testboxscript-service.sh@ 82844

Last change on this file since 82844 was 82844, checked in by vboxsync, 5 years ago

ValidationKit/linux/testboxscript-service.sh: use vanilla "type" for detecting screen/tmux presence, "type -t" (and actually all other options, too) is a bashism which isn't allowed for files run by /bin/sh

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
1#!/bin/sh
2## @file
3# VirtualBox Validation Kit - TestBoxScript service init script.
4#
5
6#
7# Copyright (C) 2006-2019 Oracle Corporation
8#
9# This file is part of VirtualBox Open Source Edition (OSE), as
10# available from http://www.virtualbox.org. This file is free software;
11# you can redistribute it and/or modify it under the terms of the GNU
12# General Public License (GPL) as published by the Free Software
13# Foundation, in version 2 as it comes in the "COPYING" file of the
14# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16#
17# The contents of this file may alternatively be used under the terms
18# of the Common Development and Distribution License Version 1.0
19# (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20# VirtualBox OSE distribution, in which case the provisions of the
21# CDDL are applicable instead of those of the GPL.
22#
23# You may elect to license modified versions of this file under the
24# terms and conditions of either the GPL or the CDDL or both.
25#
26
27# chkconfig: 35 35 65
28# description: TestBoxScript service
29#
30### BEGIN INIT INFO
31# Provides: testboxscript-service
32# Required-Start: $network
33# Required-Stop:
34# Default-Start: 2 3 4 5
35# Default-Stop: 0 1 6
36# Description: TestBoxScript service
37### END INIT INFO
38
39
40PATH=$PATH:/bin:/sbin:/usr/sbin
41
42#
43# Load config and set up defaults.
44#
45service_name="testboxscript"
46
47[ -r /etc/default/${service_name} ] && . /etc/default/${service_name}
48[ -r /etc/conf.d/${service_name} ] && . /etc/conf.d/${service_name}
49
50if [ -z "${TESTBOXSCRIPT_DIR}" ]; then
51 TESTBOXSCRIPT_DIR="/opt/testboxscript"
52fi
53if [ -z "${TESTBOXSCRIPT_USER}" ]; then
54 TESTBOXSCRIPT_USER="vbox"
55fi
56binary="${TESTBOXSCRIPT_DIR}/testboxscript/testboxscript.py"
57binary_real="${TESTBOXSCRIPT_DIR}/testboxscript/testboxscript_real.py"
58
59
60
61#
62# Detect and abstract distro
63#
64[ -f /etc/debian_release -a -f /lib/lsb/init-functions ] || NOLSB=yes
65
66system=unknown
67if [ -f /etc/redhat-release ]; then
68 system=redhat
69 PIDFILE="/var/run/${service_name}-service.pid"
70elif [ -f /etc/SuSE-release ]; then
71 system=suse
72 PIDFILE="/var/lock/subsys/${service_name}-service"
73elif [ -f /etc/debian_version ]; then
74 system=debian
75 PIDFILE="/var/run/${service_name}-service"
76elif [ -f /etc/gentoo-release ]; then
77 system=gentoo
78 PIDFILE="/var/run/${service_name}-service"
79elif [ -f /etc/arch-release ]; then
80 system=arch
81 PIDFILE="/var/run/${service_name}-service"
82elif [ -f /etc/slackware-version ]; then
83 system=slackware
84 PIDFILE="/var/run/${service_name}-service"
85elif [ -f /etc/lfs-release ]; then
86 system=lfs
87 PIDFILE="/var/run/${service_name}-service.pid"
88else
89 system=other
90 if [ -d /var/run -a -w /var/run ]; then
91 PIDFILE="/var/run/${service_name}-service"
92 fi
93fi
94
95
96#
97# Generic implementation.
98#
99
100## Query daemon status.
101# $1 = daemon-user; $2 = binary name
102# returns 0 if running, 1 if started but no longer running, 3 if not started.
103# When 0 is return the pid variable contains a list of relevant pids.
104my_query_status() {
105 a_USER="$1";
106 a_BINARY="$2";
107 pid="";
108 if [ -f "${PIDFILE}" -a -s "${PIDFILE}" ]; then
109 MY_LINE="";
110 read MY_LINE < "${PIDFILE}";
111 for MY_PID in `echo $MY_LINE | sed -e 's/[^0123456789 ]/ /g'`;
112 do
113 if [ "`stat -c '%U' /proc/$MY_PID 2> /dev/null `" = "$a_USER" ]; then
114 pid="${pid} ${MY_PID}";
115 fi
116 done
117 if [ -n "${pid}" ]; then
118 RETVAL=0;
119 else
120 RETVAL=1;
121 fi
122 else
123 RETVAL=3
124 fi
125 return $RETVAL;
126}
127
128## Starts detached daeamon in screen or tmux.
129# $1 = daemon-user; $2+ = daemon and its arguments
130my_start_daemon() {
131 a_USER="$1"
132 shift
133 if touch "${PIDFILE}" && chown "${a_USER}" -- "${PIDFILE}"; then
134 ARGS=""
135 while [ $# -gt 0 ];
136 do
137 ARGS="$ARGS '$1'";
138 shift
139 done
140 ARGS="$ARGS --pidfile '$PIDFILE'";
141 if type screen > /dev/null; then
142 su - "${a_USER}" -c "screen -S ${service_name} -d -m ${ARGS}";
143 elif type tmux > /dev/null; then
144 su - "${a_USER}" -c "tmux new-session -AdD -s ${service_name} ${ARGS}";
145 else
146 echo "Need screen or tmux, please install!"
147 exit 1
148 fi
149 RETVAL=$?;
150 if [ $RETVAL -eq 0 ]; then
151 sleep 0.6;
152 if [ ! -s "$PIDFILE" ]; then sleep 1; fi
153 if [ ! -s "$PIDFILE" ]; then sleep 2; fi
154 if [ ! -s "$PIDFILE" ]; then sleep 3; fi
155 if [ -s "$PIDFILE" ]; then
156 RETVAL=0;
157 else
158 RETVAL=1;
159 fi
160 else
161 fail_msg "su failed with exit code $RETVAL";
162 fi
163 else
164 fail_msg "Failed to create pid file and change it's ownership to ${a_USER}."
165 RETVAL=1;
166 fi
167 return $RETVAL;
168}
169
170## Stops the daemon.
171# $1 = daemon-user; $2 = binary name
172my_stop_daemon() {
173 a_USER="$1";
174 a_BINARY="$2";
175 my_query_status "$a_USER" "$a_BINARY"
176 RETVAL=$?
177 if [ $RETVAL -eq 0 -a -n "$pid" ]; then
178 kill $pid;
179 fi
180 sleep 0.6
181 if my_query_status "$a_USER" "$a_BINARY"; then sleep 1; fi
182 if my_query_status "$a_USER" "$a_BINARY"; then sleep 2; fi
183 if my_query_status "$a_USER" "$a_BINARY"; then sleep 3; fi
184 if ! my_query_status "$a_USER" "$a_BINARY"; then
185 rm -f -- "${PIDFILE}"
186 return 0;
187 fi
188 return 1;
189}
190
191if [ -z "$NOLSB" ]; then
192 . /lib/lsb/init-functions
193 fail_msg() {
194 echo ""
195 log_failure_msg "$1"
196 }
197 succ_msg() {
198 log_success_msg " done."
199 }
200 begin_msg() {
201 log_daemon_msg "$@"
202 }
203else
204 fail_msg() {
205 echo " ...fail!"
206 echo "$@"
207 }
208 succ_msg() {
209 echo " ...done."
210 }
211 begin_msg() {
212 echo -n "$1"
213 }
214fi
215
216#
217# System specific overrides.
218#
219
220if [ "$system" = "redhat" ]; then
221 . /etc/init.d/functions
222 if [ -n "$NOLSB" ]; then
223 fail_msg() {
224 echo_failure
225 echo
226 }
227 succ_msg() {
228 echo_success
229 echo
230 }
231 begin_msg() {
232 echo -n "$1"
233 }
234 fi
235fi
236
237if [ "$system" = "suse" ]; then
238 . /etc/rc.status
239 if [ -n "$NOLSB" ]; then
240 fail_msg() {
241 rc_failed 1
242 rc_status -v
243 }
244 succ_msg() {
245 rc_reset
246 rc_status -v
247 }
248 begin_msg() {
249 echo -n "$1"
250 }
251 fi
252fi
253
254if [ "$system" = "debian" ]; then
255 # Share my_start_daemon and my_stop_daemon with gentoo
256 if [ -n "$NOLSB" ]; then
257 fail_msg() {
258 echo " ...fail!"
259 }
260 succ_msg() {
261 echo " ...done."
262 }
263 begin_msg() {
264 echo -n "$1"
265 }
266 fi
267fi
268
269if [ "$system" = "gentoo" ]; then
270 if [ -f /sbin/functions.sh ]; then
271 . /sbin/functions.sh
272 elif [ -f /etc/init.d/functions.sh ]; then
273 . /etc/init.d/functions.sh
274 fi
275 # Share my_start_daemon and my_stop_daemon with debian.
276 if [ -n "$NOLSB" ]; then
277 if [ "`which $0`" = "/sbin/rc" ]; then
278 shift
279 fi
280 fi
281fi
282
283if [ "$system" = "debian" -o "$system" = "gentoo" ]; then
284 #my_start_daemon() {
285 # usr="$1"
286 # shift
287 # bin="$1"
288 # shift
289 # echo usr=$usr
290 # start-stop-daemon --start --background --pidfile "${PIDFILE}" --make-pidfile --chuid "${usr}" --user "${usr}" \
291 # --exec $bin -- $@
292 #}
293 my_stop_daemon() {
294 a_USER="$1"
295 a_BINARY="$2"
296 start-stop-daemon --stop --user "${a_USER}" --pidfile "${PIDFILE}"
297 RETVAL=$?
298 rm -f "${PIDFILE}"
299 return $RETVAL
300 }
301fi
302
303if [ "$system" = "arch" ]; then
304 USECOLOR=yes
305 . /etc/rc.d/functions
306 if [ -n "$NOLSB" ]; then
307 fail_msg() {
308 stat_fail
309 }
310 succ_msg() {
311 stat_done
312 }
313 begin_msg() {
314 stat_busy "$1"
315 }
316 fi
317fi
318
319if [ "$system" = "lfs" ]; then
320 . /etc/rc.d/init.d/functions
321 if [ -n "$NOLSB" ]; then
322 fail_msg() {
323 echo_failure
324 }
325 succ_msg() {
326 echo_ok
327 }
328 begin_msg() {
329 echo $1
330 }
331 fi
332fi
333
334#
335# Implement the actions.
336#
337check_single_user() {
338 if [ -n "$2" ]; then
339 fail_msg "TESTBOXSCRIPT_USER must not contain multiple users!"
340 exit 1
341 fi
342}
343
344#
345# Open ports at the firewall:
346# 6000..6100 / TCP for VRDP
347# 5000..5032 / TCP for netperf
348# 5000..5032 / UDP for netperf
349#
350set_iptables() {
351 if [ -x /sbin/iptables ]; then
352 I="/sbin/iptables -j ACCEPT -A INPUT -m state --state NEW"
353 if ! /sbin/iptables -L INPUT | grep -q "testsuite vrdp"; then
354 $I -m tcp -p tcp --dport 6000:6100 -m comment --comment "testsuite vrdp"
355 fi
356 if ! /sbin/iptables -L INPUT | grep -q "testsuite perftcp"; then
357 $I -m tcp -p tcp --dport 5000:5032 -m comment --comment "testsuite perftcp"
358 fi
359 if ! /sbin/iptables -L INPUT | grep -q "testsuite perfudp"; then
360 $I -m udp -p udp --dport 5000:5032 -m comment --comment "testsuite perfudp"
361 fi
362 fi
363}
364
365
366start() {
367 if [ ! -f "${PIDFILE}" ]; then
368 begin_msg "Starting TestBoxScript";
369
370 #
371 # Verify config and installation.
372 #
373 if [ ! -d "$TESTBOXSCRIPT_DIR" -o ! -r "$binary" -o ! -r "$binary_real" ]; then
374 fail_msg "Cannot find TestBoxScript installation under '$TESTBOXSCRIPT_DIR'!"
375 exit 0;
376 fi
377 ## @todo check ownership (for upgrade purposes)
378 check_single_user $TESTBOXSCRIPT_USER
379
380 #
381 # Open some ports in the firewall
382 # Allows to access VMs remotely by VRDP, netperf
383 #
384 set_iptables
385
386 #
387 # Set execute bits to make installation (unzip) easier.
388 #
389 chmod a+x > /dev/null 2>&1 \
390 "${binary}" \
391 "${binary_real}" \
392 "${TESTBOXSCRIPT_DIR}/linux/amd64/TestBoxHelper" \
393 "${TESTBOXSCRIPT_DIR}/linux/x86/TestBoxHelper"
394
395 #
396 # Start the daemon as the specified user.
397 #
398 PARAMS=""
399 if [ "${TESTBOXSCRIPT_HWVIRT}" = "yes" ]; then PARAMS="${PARAMS} --hwvirt"; fi
400 if [ "${TESTBOXSCRIPT_HWVIRT}" = "no" ]; then PARAMS="${PARAMS} --no-hwvirt"; fi
401 if [ "${TESTBOXSCRIPT_NESTED_PAGING}" = "yes" ]; then PARAMS="${PARAMS} --nested-paging"; fi
402 if [ "${TESTBOXSCRIPT_NESTED_PAGING}" = "no" ]; then PARAMS="${PARAMS} --no-nested-paging"; fi
403 if [ "${TESTBOXSCRIPT_IOMMU}" = "yes" ]; then PARAMS="${PARAMS} --io-mmu"; fi
404 if [ "${TESTBOXSCRIPT_IOMMU}" = "no" ]; then PARAMS="${PARAMS} --no-io-mmu"; fi
405 if [ -n "${TESTBOXSCRIPT_SYSTEM_UUID}" ]; then PARAMS="${PARAMS} --system-uuid '${TESTBOXSCRIPT_SYSTEM_UUID}'"; fi
406 if [ -n "${TESTBOXSCRIPT_TEST_MANAGER}" ]; then PARAMS="${PARAMS} --test-manager '${TESTBOXSCRIPT_TEST_MANAGER}'"; fi
407 if [ -n "${TESTBOXSCRIPT_SCRATCH_ROOT}" ]; then PARAMS="${PARAMS} --scratch-root '${TESTBOXSCRIPT_SCRATCH_ROOT}'"; fi
408
409 if [ -n "${TESTBOXSCRIPT_BUILDS_PATH}" ]; then PARAMS="${PARAMS} --builds-path '${TESTBOXSCRIPT_BUILDS_PATH}'"; fi
410 if [ -n "${TESTBOXSCRIPT_BUILDS_TYPE}" ]; then PARAMS="${PARAMS} --builds-server-type '${TESTBOXSCRIPT_BUILDS_TYPE}'"; fi
411 if [ -n "${TESTBOXSCRIPT_BUILDS_NAME}" ]; then PARAMS="${PARAMS} --builds-server-name '${TESTBOXSCRIPT_BUILDS_NAME}'"; fi
412 if [ -n "${TESTBOXSCRIPT_BUILDS_SHARE}" ]; then PARAMS="${PARAMS} --builds-server-share '${TESTBOXSCRIPT_BUILDS_SHARE}'"; fi
413 if [ -n "${TESTBOXSCRIPT_BUILDS_USER}" ]; then PARAMS="${PARAMS} --builds-server-user '${TESTBOXSCRIPT_BUILDS_USER}'"; fi
414 if [ -n "${TESTBOXSCRIPT_BUILDS_PASSWD}" ]; then PARAMS="${PARAMS} --builds-server-passwd '${TESTBOXSCRIPT_BUILDS_PASSWD}'"; fi
415 if [ -n "${TESTBOXSCRIPT_BUILDS_MOUNTOPT}" ]; then PARAMS="${PARAMS} --builds-server-mountopt '${TESTBOXSCRIPT_BUILDS_MOUNTOPT}'"; fi
416 if [ -n "${TESTBOXSCRIPT_TESTRSRC_PATH}" ]; then PARAMS="${PARAMS} --testrsrc-path '${TESTBOXSCRIPT_TESTRSRC_PATH}'"; fi
417 if [ -n "${TESTBOXSCRIPT_TESTRSRC_TYPE}" ]; then PARAMS="${PARAMS} --testrsrc-server-type '${TESTBOXSCRIPT_TESTRSRC_TYPE}'"; fi
418 if [ -n "${TESTBOXSCRIPT_TESTRSRC_NAME}" ]; then PARAMS="${PARAMS} --testrsrc-server-name '${TESTBOXSCRIPT_TESTRSRC_NAME}'"; fi
419 if [ -n "${TESTBOXSCRIPT_TESTRSRC_SHARE}" ]; then PARAMS="${PARAMS} --testrsrc-server-share '${TESTBOXSCRIPT_TESTRSRC_SHARE}'"; fi
420 if [ -n "${TESTBOXSCRIPT_TESTRSRC_USER}" ]; then PARAMS="${PARAMS} --testrsrc-server-user '${TESTBOXSCRIPT_TESTRSRC_USER}'"; fi
421 if [ -n "${TESTBOXSCRIPT_TESTRSRC_PASSWD}" ]; then PARAMS="${PARAMS} --testrsrc-server-passwd '${TESTBOXSCRIPT_TESTRSRC_PASSWD}'"; fi
422 if [ -n "${TESTBOXSCRIPT_TESTRSRC_MOUNTOPT}" ]; then PARAMS="${PARAMS} --testrsrc-server-mountopt '${TESTBOXSCRIPT_TESTRSRC_MOUNTOPT}'"; fi
423
424 if [ -n "${TESTBOXSCRIPT_PYTHON}" ]; then
425 my_start_daemon "${TESTBOXSCRIPT_USER}" "${TESTBOXSCRIPT_PYTHON}" "${binary}" ${PARAMS}
426 else
427 my_start_daemon "${TESTBOXSCRIPT_USER}" "${binary}" ${PARAMS}
428 fi
429 RETVAL=$?
430
431 if [ $RETVAL -eq 0 ]; then
432 succ_msg
433 else
434 fail_msg
435 fi
436 else
437 succ_msg "Already running."
438 RETVAL=0
439 fi
440 return $RETVAL
441}
442
443stop() {
444 if [ -f "${PIDFILE}" ]; then
445 begin_msg "Stopping TestBoxScript";
446 my_stop_daemon "${TESTBOXSCRIPT_USER}" "${binary}"
447 RETVAL=$?
448 if [ $RETVAL -eq 0 ]; then
449 succ_msg
450 else
451 fail_msg
452 fi
453 else
454 RETVAL=0
455 fi
456 return $RETVAL
457}
458
459restart() {
460 stop && sleep 1 && start
461}
462
463status() {
464 echo -n "Checking for TestBoxScript"
465 my_query_status "${TESTBOXSCRIPT_USER}" "${binary}"
466 RETVAL=$?
467 if [ ${RETVAL} -eq 0 ]; then
468 echo " ...running"
469 elif [ ${RETVAL} -eq 3 ]; then
470 echo " ...stopped"
471 elif [ ${RETVAL} -eq 1 ]; then
472 echo " ...started but not running"
473 else
474 echo " ...unknown status '${RETVAL}'"
475 fi
476}
477
478
479#
480# main().
481#
482case "$1" in
483 start)
484 start
485 ;;
486 stop)
487 stop
488 ;;
489 restart)
490 restart
491 ;;
492 force-reload)
493 restart
494 ;;
495 status)
496 status
497 ;;
498 setup)
499 ;;
500 cleanup)
501 ;;
502 *)
503 echo "Usage: $0 {start|stop|restart|status}"
504 exit 1
505esac
506
507exit $RETVAL
508
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette