1 | #!/bin/sh
|
---|
2 | # $Id: vboxautostart-service.sh 69498 2017-10-28 15:07:25Z vboxsync $
|
---|
3 | ## @file
|
---|
4 | # VirtualBox autostart service init script.
|
---|
5 | #
|
---|
6 |
|
---|
7 | #
|
---|
8 | # Copyright (C) 2012-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 autostart service
|
---|
21 | #
|
---|
22 | ### BEGIN INIT INFO
|
---|
23 | # Provides: vboxautostart-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 autostart service
|
---|
29 | ### END INIT INFO
|
---|
30 |
|
---|
31 | PATH=$PATH:/bin:/sbin:/usr/sbin
|
---|
32 | SCRIPTNAME=vboxautostart-service.sh
|
---|
33 |
|
---|
34 | [ -f /etc/debian_release -a -f /lib/lsb/init-functions ] || NOLSB=yes
|
---|
35 | [ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
|
---|
36 |
|
---|
37 | if [ -n "$INSTALL_DIR" ]; then
|
---|
38 | binary="$INSTALL_DIR/VBoxAutostart"
|
---|
39 | else
|
---|
40 | binary="/usr/lib/virtualbox/VBoxAutostart"
|
---|
41 | fi
|
---|
42 |
|
---|
43 | # silently exit if the package was uninstalled but not purged,
|
---|
44 | # applies to Debian packages only (but shouldn't hurt elsewhere)
|
---|
45 | [ ! -f /etc/debian_release -o -x $binary ] || exit 0
|
---|
46 |
|
---|
47 | [ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
|
---|
48 |
|
---|
49 | # Preamble for Gentoo
|
---|
50 | if [ "`which $0`" = "/sbin/rc" ]; then
|
---|
51 | shift
|
---|
52 | fi
|
---|
53 |
|
---|
54 | begin_msg()
|
---|
55 | {
|
---|
56 | test -n "${2}" && echo "${SCRIPTNAME}: ${1}."
|
---|
57 | logger -t "${SCRIPTNAME}" "${1}."
|
---|
58 | }
|
---|
59 |
|
---|
60 | succ_msg()
|
---|
61 | {
|
---|
62 | logger -t "${SCRIPTNAME}" "${1}."
|
---|
63 | }
|
---|
64 |
|
---|
65 | fail_msg()
|
---|
66 | {
|
---|
67 | echo "${SCRIPTNAME}: failed: ${1}." >&2
|
---|
68 | logger -t "${SCRIPTNAME}" "failed: ${1}."
|
---|
69 | }
|
---|
70 |
|
---|
71 | start_daemon() {
|
---|
72 | usr="$1"
|
---|
73 | shift
|
---|
74 | su - $usr -c "$*"
|
---|
75 | }
|
---|
76 |
|
---|
77 | if which start-stop-daemon >/dev/null 2>&1; then
|
---|
78 | start_daemon() {
|
---|
79 | usr="$1"
|
---|
80 | shift
|
---|
81 | bin="$1"
|
---|
82 | shift
|
---|
83 | start-stop-daemon --chuid $usr --start --exec $bin -- $@
|
---|
84 | }
|
---|
85 | fi
|
---|
86 |
|
---|
87 | vboxdrvrunning() {
|
---|
88 | lsmod | grep -q "vboxdrv[^_-]"
|
---|
89 | }
|
---|
90 |
|
---|
91 | start() {
|
---|
92 | [ -z "$VBOXAUTOSTART_DB" ] && exit 0
|
---|
93 | [ -z "$VBOXAUTOSTART_CONFIG" ] && exit 0
|
---|
94 | begin_msg "Starting VirtualBox VMs configured for autostart" console;
|
---|
95 | vboxdrvrunning || {
|
---|
96 | fail_msg "VirtualBox kernel module not loaded!"
|
---|
97 | exit 0
|
---|
98 | }
|
---|
99 | PARAMS="--background --start --config $VBOXAUTOSTART_CONFIG"
|
---|
100 |
|
---|
101 | # prevent inheriting this setting to VBoxSVC
|
---|
102 | unset VBOX_RELEASE_LOG_DEST
|
---|
103 |
|
---|
104 | for user in `ls $VBOXAUTOSTART_DB/*.start`
|
---|
105 | do
|
---|
106 | start_daemon `basename $user | sed -ne "s/\(.*\).start/\1/p"` $binary $PARAMS > /dev/null 2>&1
|
---|
107 | done
|
---|
108 |
|
---|
109 | return $RETVAL
|
---|
110 | }
|
---|
111 |
|
---|
112 | stop() {
|
---|
113 | [ -z "$VBOXAUTOSTART_DB" ] && exit 0
|
---|
114 | [ -z "$VBOXAUTOSTART_CONFIG" ] && exit 0
|
---|
115 |
|
---|
116 | PARAMS="--stop --config $VBOXAUTOSTART_CONFIG"
|
---|
117 |
|
---|
118 | # prevent inheriting this setting to VBoxSVC
|
---|
119 | unset VBOX_RELEASE_LOG_DEST
|
---|
120 |
|
---|
121 | for user in `ls $VBOXAUTOSTART_DB/*.stop`
|
---|
122 | do
|
---|
123 | start_daemon `basename $user | sed -ne "s/\(.*\).stop/\1/p"` $binary $PARAMS > /dev/null 2>&1
|
---|
124 | done
|
---|
125 |
|
---|
126 | return $RETVAL
|
---|
127 | }
|
---|
128 |
|
---|
129 | case "$1" in
|
---|
130 | start)
|
---|
131 | start
|
---|
132 | ;;
|
---|
133 | stop)
|
---|
134 | stop
|
---|
135 | ;;
|
---|
136 | *)
|
---|
137 | echo "Usage: $0 {start|stop}"
|
---|
138 | exit 1
|
---|
139 | esac
|
---|
140 |
|
---|
141 | exit $RETVAL
|
---|