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