VirtualBox

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

Last change on this file since 103011 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

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