1 | #!/bin/sh
|
---|
2 | # $Id: vboxweb-service.sh 69498 2017-10-28 15:07:25Z vboxsync $
|
---|
3 | ## @file
|
---|
4 | # VirtualBox web service API daemon init script.
|
---|
5 | #
|
---|
6 |
|
---|
7 | #
|
---|
8 | # Copyright (C) 2006-2015 Oracle Corporation
|
---|
9 | #
|
---|
10 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | # available from http://www.virtualbox.org. This file is free software;
|
---|
12 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | # General Public License (GPL) as published by the Free Software
|
---|
14 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | #
|
---|
18 |
|
---|
19 | # chkconfig: 345 35 65
|
---|
20 | # description: VirtualBox web service API
|
---|
21 | #
|
---|
22 | ### BEGIN INIT INFO
|
---|
23 | # Provides: vboxweb-service
|
---|
24 | # Required-Start: vboxdrv
|
---|
25 | # Required-Stop: vboxdrv
|
---|
26 | # Default-Start: 2 3 4 5
|
---|
27 | # Default-Stop: 0 1 6
|
---|
28 | # Description: VirtualBox web service API
|
---|
29 | ### END INIT INFO
|
---|
30 |
|
---|
31 | PATH=$PATH:/bin:/sbin:/usr/sbin
|
---|
32 | SCRIPTNAME=vboxweb-service.sh
|
---|
33 |
|
---|
34 | [ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
|
---|
35 |
|
---|
36 | if [ -n "$INSTALL_DIR" ]; then
|
---|
37 | binary="$INSTALL_DIR/vboxwebsrv"
|
---|
38 | vboxmanage="$INSTALL_DIR/VBoxManage"
|
---|
39 | else
|
---|
40 | binary="/usr/lib/virtualbox/vboxwebsrv"
|
---|
41 | vboxmanage="/usr/lib/virtualbox/VBoxManage"
|
---|
42 | fi
|
---|
43 |
|
---|
44 | # silently exit if the package was uninstalled but not purged,
|
---|
45 | # applies to Debian packages only (but shouldn't hurt elsewhere)
|
---|
46 | [ ! -f /etc/debian_release -o -x $binary ] || exit 0
|
---|
47 |
|
---|
48 | [ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
|
---|
49 |
|
---|
50 | PIDFILE="/var/run/${SCRIPTNAME}"
|
---|
51 |
|
---|
52 | # Preamble for Gentoo
|
---|
53 | if [ "`which $0`" = "/sbin/rc" ]; then
|
---|
54 | shift
|
---|
55 | fi
|
---|
56 |
|
---|
57 | begin_msg()
|
---|
58 | {
|
---|
59 | test -n "${2}" && echo "${SCRIPTNAME}: ${1}."
|
---|
60 | logger -t "${SCRIPTNAME}" "${1}."
|
---|
61 | }
|
---|
62 |
|
---|
63 | succ_msg()
|
---|
64 | {
|
---|
65 | logger -t "${SCRIPTNAME}" "${1}."
|
---|
66 | }
|
---|
67 |
|
---|
68 | fail_msg()
|
---|
69 | {
|
---|
70 | echo "${SCRIPTNAME}: failed: ${1}." >&2
|
---|
71 | logger -t "${SCRIPTNAME}" "failed: ${1}."
|
---|
72 | }
|
---|
73 |
|
---|
74 | start_daemon() {
|
---|
75 | usr="$1"
|
---|
76 | shift
|
---|
77 | su - $usr -c "$*"
|
---|
78 | }
|
---|
79 |
|
---|
80 | killproc() {
|
---|
81 | killall $1
|
---|
82 | rm -f $PIDFILE
|
---|
83 | }
|
---|
84 |
|
---|
85 | if which start-stop-daemon >/dev/null 2>&1; then
|
---|
86 | start_daemon() {
|
---|
87 | usr="$1"
|
---|
88 | shift
|
---|
89 | bin="$1"
|
---|
90 | shift
|
---|
91 | start-stop-daemon --background --chuid $usr --start --exec $bin -- $@
|
---|
92 | }
|
---|
93 |
|
---|
94 | killproc() {
|
---|
95 | start-stop-daemon --stop --exec $@
|
---|
96 | }
|
---|
97 | fi
|
---|
98 |
|
---|
99 | vboxdrvrunning() {
|
---|
100 | lsmod | grep -q "vboxdrv[^_-]"
|
---|
101 | }
|
---|
102 |
|
---|
103 | check_single_user() {
|
---|
104 | if [ -n "$2" ]; then
|
---|
105 | fail_msg "VBOXWEB_USER must not contain multiple users!"
|
---|
106 | exit 1
|
---|
107 | fi
|
---|
108 | }
|
---|
109 |
|
---|
110 | start() {
|
---|
111 | if ! test -f $PIDFILE; then
|
---|
112 | [ -z "$VBOXWEB_USER" ] && exit 0
|
---|
113 | begin_msg "Starting VirtualBox web service" console;
|
---|
114 | check_single_user $VBOXWEB_USER
|
---|
115 | vboxdrvrunning || {
|
---|
116 | fail_msg "VirtualBox kernel module not loaded!"
|
---|
117 | exit 0
|
---|
118 | }
|
---|
119 | PARAMS="--background"
|
---|
120 | [ -n "$VBOXWEB_HOST" ] && PARAMS="$PARAMS -H $VBOXWEB_HOST"
|
---|
121 | [ -n "$VBOXWEB_PORT" ] && PARAMS="$PARAMS -p $VBOXWEB_PORT"
|
---|
122 | [ -n "$VBOXWEB_SSL_KEYFILE" ] && PARAMS="$PARAMS -s -K $VBOXWEB_SSL_KEYFILE"
|
---|
123 | [ -n "$VBOXWEB_SSL_PASSWORDFILE" ] && PARAMS="$PARAMS -a $VBOXWEB_SSL_PASSWORDFILE"
|
---|
124 | [ -n "$VBOXWEB_SSL_CACERT" ] && PARAMS="$PARAMS -c $VBOXWEB_SSL_CACERT"
|
---|
125 | [ -n "$VBOXWEB_SSL_CAPATH" ] && PARAMS="$PARAMS -C $VBOXWEB_SSL_CAPATH"
|
---|
126 | [ -n "$VBOXWEB_SSL_DHFILE" ] && PARAMS="$PARAMS -D $VBOXWEB_SSL_DHFILE"
|
---|
127 | [ -n "$VBOXWEB_SSL_RANDFILE" ] && PARAMS="$PARAMS -r $VBOXWEB_SSL_RANDFILE"
|
---|
128 | [ -n "$VBOXWEB_TIMEOUT" ] && PARAMS="$PARAMS -t $VBOXWEB_TIMEOUT"
|
---|
129 | [ -n "$VBOXWEB_CHECK_INTERVAL" ] && PARAMS="$PARAMS -i $VBOXWEB_CHECK_INTERVAL"
|
---|
130 | [ -n "$VBOXWEB_THREADS" ] && PARAMS="$PARAMS -T $VBOXWEB_THREADS"
|
---|
131 | [ -n "$VBOXWEB_KEEPALIVE" ] && PARAMS="$PARAMS -k $VBOXWEB_KEEPALIVE"
|
---|
132 | [ -n "$VBOXWEB_AUTHENTICATION" ] && PARAMS="$PARAMS -A $VBOXWEB_AUTHENTICATION"
|
---|
133 | [ -n "$VBOXWEB_LOGFILE" ] && PARAMS="$PARAMS -F $VBOXWEB_LOGFILE"
|
---|
134 | [ -n "$VBOXWEB_ROTATE" ] && PARAMS="$PARAMS -R $VBOXWEB_ROTATE"
|
---|
135 | [ -n "$VBOXWEB_LOGSIZE" ] && PARAMS="$PARAMS -S $VBOXWEB_LOGSIZE"
|
---|
136 | [ -n "$VBOXWEB_LOGINTERVAL" ] && PARAMS="$PARAMS -I $VBOXWEB_LOGINTERVAL"
|
---|
137 | # set authentication method + password hash
|
---|
138 | if [ -n "$VBOXWEB_AUTH_LIBRARY" ]; then
|
---|
139 | su - "$VBOXWEB_USER" -c "$vboxmanage setproperty websrvauthlibrary \"$VBOXWEB_AUTH_LIBRARY\""
|
---|
140 | if [ $? -ne 0 ]; then
|
---|
141 | fail_msg "Error $? setting webservice authentication library to $VBOXWEB_AUTH_LIBRARY"
|
---|
142 | fi
|
---|
143 | fi
|
---|
144 | if [ -n "$VBOXWEB_AUTH_PWHASH" ]; then
|
---|
145 | su - "$VBOXWEB_USER" -c "$vboxmanage setextradata global \"VBoxAuthSimple/users/$VBOXWEB_USER\" \"$VBOXWEB_AUTH_PWHASH\""
|
---|
146 | if [ $? -ne 0 ]; then
|
---|
147 | fail_msg "Error $? setting webservice password hash"
|
---|
148 | fi
|
---|
149 | fi
|
---|
150 | # prevent inheriting this setting to VBoxSVC
|
---|
151 | unset VBOX_RELEASE_LOG_DEST
|
---|
152 | start_daemon $VBOXWEB_USER $binary $PARAMS > /dev/null 2>&1
|
---|
153 | # ugly: wait until the final process has forked
|
---|
154 | sleep .1
|
---|
155 | PID=`pidof $binary 2>/dev/null`
|
---|
156 | if [ -n "$PID" ]; then
|
---|
157 | echo "$PID" > $PIDFILE
|
---|
158 | RETVAL=0
|
---|
159 | succ_msg "VirtualBox web service started"
|
---|
160 | else
|
---|
161 | RETVAL=1
|
---|
162 | fail_msg "VirtualBox web service failed to start"
|
---|
163 | fi
|
---|
164 | fi
|
---|
165 | return $RETVAL
|
---|
166 | }
|
---|
167 |
|
---|
168 | stop() {
|
---|
169 | if test -f $PIDFILE; then
|
---|
170 | begin_msg "Stopping VirtualBox web service" console;
|
---|
171 | killproc $binary
|
---|
172 | RETVAL=$?
|
---|
173 | # Be careful: wait 1 second, making sure that everything is cleaned up.
|
---|
174 | sleep 1
|
---|
175 | if ! pidof $binary > /dev/null 2>&1; then
|
---|
176 | rm -f $PIDFILE
|
---|
177 | succ_msg "VirtualBox web service stopped"
|
---|
178 | else
|
---|
179 | fail_msg "VirtualBox web service failed to stop"
|
---|
180 | fi
|
---|
181 | fi
|
---|
182 | return $RETVAL
|
---|
183 | }
|
---|
184 |
|
---|
185 | restart() {
|
---|
186 | stop && start
|
---|
187 | }
|
---|
188 |
|
---|
189 | status() {
|
---|
190 | echo -n "Checking for VBox Web Service"
|
---|
191 | if [ -f $PIDFILE ]; then
|
---|
192 | echo " ...running"
|
---|
193 | else
|
---|
194 | echo " ...not running"
|
---|
195 | fi
|
---|
196 | }
|
---|
197 |
|
---|
198 | case "$1" in
|
---|
199 | start)
|
---|
200 | start
|
---|
201 | ;;
|
---|
202 | stop)
|
---|
203 | stop
|
---|
204 | ;;
|
---|
205 | restart)
|
---|
206 | restart
|
---|
207 | ;;
|
---|
208 | force-reload)
|
---|
209 | restart
|
---|
210 | ;;
|
---|
211 | status)
|
---|
212 | status
|
---|
213 | ;;
|
---|
214 | setup)
|
---|
215 | ;;
|
---|
216 | cleanup)
|
---|
217 | ;;
|
---|
218 | *)
|
---|
219 | echo "Usage: $0 {start|stop|restart|status}"
|
---|
220 | exit 1
|
---|
221 | esac
|
---|
222 |
|
---|
223 | exit $RETVAL
|
---|