[41999] | 1 | #!/bin/sh
|
---|
[69246] | 2 | # $Id: vboxautostart-service.sh 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
| 3 | ## @file
|
---|
[41999] | 4 | # VirtualBox autostart service init script.
|
---|
| 5 | #
|
---|
[69246] | 6 |
|
---|
| 7 | #
|
---|
[98103] | 8 | # Copyright (C) 2012-2023 Oracle and/or its affiliates.
|
---|
[41999] | 9 | #
|
---|
[96407] | 10 | # This file is part of VirtualBox base platform packages, as
|
---|
| 11 | # available from https://www.virtualbox.org.
|
---|
[41999] | 12 | #
|
---|
[96407] | 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 | #
|
---|
[41999] | 28 |
|
---|
[56752] | 29 | # chkconfig: 345 35 65
|
---|
[41999] | 30 | # description: VirtualBox autostart service
|
---|
| 31 | #
|
---|
| 32 | ### BEGIN INIT INFO
|
---|
| 33 | # Provides: vboxautostart-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 autostart service
|
---|
| 39 | ### END INIT INFO
|
---|
| 40 |
|
---|
| 41 | PATH=$PATH:/bin:/sbin:/usr/sbin
|
---|
[58326] | 42 | SCRIPTNAME=vboxautostart-service.sh
|
---|
[41999] | 43 |
|
---|
[43365] | 44 | [ -f /etc/debian_release -a -f /lib/lsb/init-functions ] || NOLSB=yes
|
---|
[41999] | 45 | [ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
|
---|
| 46 |
|
---|
| 47 | if [ -n "$INSTALL_DIR" ]; then
|
---|
| 48 | binary="$INSTALL_DIR/VBoxAutostart"
|
---|
| 49 | else
|
---|
[43365] | 50 | binary="/usr/lib/virtualbox/VBoxAutostart"
|
---|
[41999] | 51 | fi
|
---|
| 52 |
|
---|
| 53 | # silently exit if the package was uninstalled but not purged,
|
---|
[43365] | 54 | # applies to Debian packages only (but shouldn't hurt elsewhere)
|
---|
| 55 | [ ! -f /etc/debian_release -o -x $binary ] || exit 0
|
---|
[41999] | 56 |
|
---|
[43365] | 57 | [ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
|
---|
[41999] | 58 |
|
---|
[57945] | 59 | # Preamble for Gentoo
|
---|
| 60 | if [ "`which $0`" = "/sbin/rc" ]; then
|
---|
| 61 | shift
|
---|
[41999] | 62 | fi
|
---|
| 63 |
|
---|
[57945] | 64 | begin_msg()
|
---|
| 65 | {
|
---|
| 66 | test -n "${2}" && echo "${SCRIPTNAME}: ${1}."
|
---|
[58326] | 67 | logger -t "${SCRIPTNAME}" "${1}."
|
---|
[57945] | 68 | }
|
---|
[41999] | 69 |
|
---|
[57945] | 70 | succ_msg()
|
---|
| 71 | {
|
---|
[58326] | 72 | logger -t "${SCRIPTNAME}" "${1}."
|
---|
[57945] | 73 | }
|
---|
[41999] | 74 |
|
---|
[57945] | 75 | fail_msg()
|
---|
| 76 | {
|
---|
| 77 | echo "${SCRIPTNAME}: failed: ${1}." >&2
|
---|
[58326] | 78 | logger -t "${SCRIPTNAME}" "failed: ${1}."
|
---|
[57945] | 79 | }
|
---|
[41999] | 80 |
|
---|
[57945] | 81 | start_daemon() {
|
---|
| 82 | usr="$1"
|
---|
| 83 | shift
|
---|
| 84 | su - $usr -c "$*"
|
---|
| 85 | }
|
---|
[41999] | 86 |
|
---|
[57985] | 87 | if which start-stop-daemon >/dev/null 2>&1; then
|
---|
[41999] | 88 | start_daemon() {
|
---|
| 89 | usr="$1"
|
---|
| 90 | shift
|
---|
| 91 | bin="$1"
|
---|
| 92 | shift
|
---|
[66074] | 93 | start-stop-daemon --chuid $usr --start --exec $bin -- $@
|
---|
[41999] | 94 | }
|
---|
| 95 | fi
|
---|
| 96 |
|
---|
| 97 | vboxdrvrunning() {
|
---|
| 98 | lsmod | grep -q "vboxdrv[^_-]"
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[88524] | 101 | valid_db_entry() {
|
---|
| 102 |
|
---|
| 103 | entry="$1"
|
---|
| 104 | [ -z "$entry" ] && return 1
|
---|
| 105 |
|
---|
| 106 | user="$2"
|
---|
| 107 | [ -z "$user" ] && return 1
|
---|
| 108 |
|
---|
| 109 | user_name=$(id -n -u "$user" 2>/dev/null)
|
---|
| 110 | [ -z "$user_name" ] && return 1
|
---|
| 111 |
|
---|
| 112 | user_id=$(id -u "$user" 2>/dev/null)
|
---|
| 113 |
|
---|
| 114 | # Verify that @user identifies a user *by name* (i.e. not a numeric id).
|
---|
| 115 | # Careful, all numeric user names are legal.
|
---|
| 116 | if [ "$user_id" = "$user" ] && [ "$user_name" != "$user" ]; then
|
---|
| 117 | return 1
|
---|
| 118 | fi
|
---|
| 119 |
|
---|
| 120 | # Verify whether file name is the same as file owner name.
|
---|
| 121 | [ -z "$(find "$entry" -user "$user" -type f 2>/dev/null)" ] && return 1
|
---|
| 122 |
|
---|
| 123 | return 0
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[41999] | 126 | start() {
|
---|
| 127 | [ -z "$VBOXAUTOSTART_DB" ] && exit 0
|
---|
| 128 | [ -z "$VBOXAUTOSTART_CONFIG" ] && exit 0
|
---|
[57945] | 129 | begin_msg "Starting VirtualBox VMs configured for autostart" console;
|
---|
[41999] | 130 | vboxdrvrunning || {
|
---|
| 131 | fail_msg "VirtualBox kernel module not loaded!"
|
---|
| 132 | exit 0
|
---|
| 133 | }
|
---|
[42338] | 134 | PARAMS="--background --start --config $VBOXAUTOSTART_CONFIG"
|
---|
[41999] | 135 |
|
---|
| 136 | # prevent inheriting this setting to VBoxSVC
|
---|
| 137 | unset VBOX_RELEASE_LOG_DEST
|
---|
| 138 |
|
---|
[88524] | 139 | for entry in "$VBOXAUTOSTART_DB"/*.start
|
---|
[41999] | 140 | do
|
---|
[88524] | 141 | user=$(basename "$entry" .start)
|
---|
| 142 | [ "$user" = "*" ] && break
|
---|
| 143 | valid_db_entry "$entry" "$user" || continue
|
---|
| 144 |
|
---|
| 145 | start_daemon "$user" "$binary" $PARAMS > /dev/null 2>&1
|
---|
[41999] | 146 | done
|
---|
| 147 |
|
---|
| 148 | return $RETVAL
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | stop() {
|
---|
| 152 | [ -z "$VBOXAUTOSTART_DB" ] && exit 0
|
---|
| 153 | [ -z "$VBOXAUTOSTART_CONFIG" ] && exit 0
|
---|
| 154 |
|
---|
[43656] | 155 | PARAMS="--stop --config $VBOXAUTOSTART_CONFIG"
|
---|
[42550] | 156 |
|
---|
[41999] | 157 | # prevent inheriting this setting to VBoxSVC
|
---|
[43656] | 158 | unset VBOX_RELEASE_LOG_DEST
|
---|
[41999] | 159 |
|
---|
[88524] | 160 | for entry in "$VBOXAUTOSTART_DB"/*.stop
|
---|
[43656] | 161 | do
|
---|
[88524] | 162 | user=$(basename "$entry" .stop)
|
---|
| 163 | [ "$user" = "*" ] && break
|
---|
| 164 | valid_db_entry "$entry" "$user" || continue
|
---|
| 165 |
|
---|
| 166 | start_daemon "$user" "$binary" $PARAMS > /dev/null 2>&1
|
---|
[43656] | 167 | done
|
---|
[41999] | 168 |
|
---|
[43656] | 169 | return $RETVAL
|
---|
[41999] | 170 | }
|
---|
| 171 |
|
---|
| 172 | case "$1" in
|
---|
| 173 | start)
|
---|
| 174 | start
|
---|
| 175 | ;;
|
---|
| 176 | stop)
|
---|
| 177 | stop
|
---|
| 178 | ;;
|
---|
| 179 | *)
|
---|
| 180 | echo "Usage: $0 {start|stop}"
|
---|
| 181 | exit 1
|
---|
| 182 | esac
|
---|
| 183 |
|
---|
| 184 | exit $RETVAL
|
---|