| 1 | #!/bin/sh
|
|---|
| 2 | # $Id: VirtualBoxStartup.sh 90723 2013-11-18 20:25:07Z fmehnert $
|
|---|
| 3 | ## @file
|
|---|
| 4 | # Startup service for loading the kernel extensions and select the set of VBox
|
|---|
| 5 | # binaries that matches the kernel architecture.
|
|---|
| 6 | #
|
|---|
| 7 |
|
|---|
| 8 | #
|
|---|
| 9 | # Copyright (C) 2007-2014 Oracle Corporation
|
|---|
| 10 | #
|
|---|
| 11 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
|---|
| 12 | # available from http://www.virtualbox.org. This file is free software;
|
|---|
| 13 | # you can redistribute it and/or modify it under the terms of the GNU
|
|---|
| 14 | # General Public License (GPL) as published by the Free Software
|
|---|
| 15 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
|---|
| 16 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
|---|
| 17 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
|---|
| 18 | #
|
|---|
| 19 |
|
|---|
| 20 | if false; then
|
|---|
| 21 | . /etc/rc.common
|
|---|
| 22 | else
|
|---|
| 23 | # Fake the startup item functions we're using.
|
|---|
| 24 |
|
|---|
| 25 | ConsoleMessage()
|
|---|
| 26 | {
|
|---|
| 27 | #if [ "$1" != "-f" ]; then
|
|---|
| 28 | # echo "$@"
|
|---|
| 29 | #else
|
|---|
| 30 | # shift
|
|---|
| 31 | # echo "Fatal error: $@"
|
|---|
| 32 | # exit 1;
|
|---|
| 33 | #fi
|
|---|
| 34 | echo "$@"
|
|---|
| 35 | echo "$@" >> /tmp/vboxdebug.txt
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | RunService()
|
|---|
| 39 | {
|
|---|
| 40 | case "$1" in
|
|---|
| 41 | "start")
|
|---|
| 42 | StartService
|
|---|
| 43 | exit $?;
|
|---|
| 44 | ;;
|
|---|
| 45 | "stop")
|
|---|
| 46 | StopService
|
|---|
| 47 | exit $?;
|
|---|
| 48 | ;;
|
|---|
| 49 | "restart")
|
|---|
| 50 | RestartService
|
|---|
| 51 | exit $?;
|
|---|
| 52 | ;;
|
|---|
| 53 | "launchd")
|
|---|
| 54 | if RestartService; then
|
|---|
| 55 | while true;
|
|---|
| 56 | do
|
|---|
| 57 | sleep 3600
|
|---|
| 58 | done
|
|---|
| 59 | fi
|
|---|
| 60 | exit $?;
|
|---|
| 61 | ;;
|
|---|
| 62 | **)
|
|---|
| 63 | ConsoleMessage "Error: Unknown action '$1'"
|
|---|
| 64 | exit 100;
|
|---|
| 65 | esac
|
|---|
| 66 | }
|
|---|
| 67 | fi
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | StartService()
|
|---|
| 71 | {
|
|---|
| 72 | VBOX_RC=0
|
|---|
| 73 | VBOXDRV="VBoxDrv"
|
|---|
| 74 | VBOXUSB="VBoxUSB"
|
|---|
| 75 |
|
|---|
| 76 | #
|
|---|
| 77 | # Switch the binaries to the right architecture.
|
|---|
| 78 | #
|
|---|
| 79 | VBOX_ARCH=`uname -m`
|
|---|
| 80 | if test "$VBOX_ARCH" = "x86_64"; then
|
|---|
| 81 | VBOX_ARCH="amd64"
|
|---|
| 82 | else
|
|---|
| 83 | VBOX_ARCH="x86"
|
|---|
| 84 | fi
|
|---|
| 85 | for VBOX_TRG in `ls /Applications/VirtualBox.app/Contents/MacOS/*-${VBOX_ARCH}`;
|
|---|
| 86 | do
|
|---|
| 87 | VBOX_LINKNAME=`echo "$VBOX_TRG" | sed -e 's|-'"${VBOX_ARCH}"'$||' `
|
|---|
| 88 | if test "$VBOX_LINKNAME" != "$VBOX_TRG"; then
|
|---|
| 89 | rm -f "$VBOX_LINKNAME"
|
|---|
| 90 | if ! /bin/ln -vh "$VBOX_TRG" "$VBOX_LINKNAME"; then
|
|---|
| 91 | ConsoleMessage "Error: /bin/ln -vh $VBOX_TRG $VBOX_LINKNAME failed"
|
|---|
| 92 | VBOX_RC=2
|
|---|
| 93 | fi
|
|---|
| 94 | else
|
|---|
| 95 | ConsoleMessage "Error: Script error VBOX_TRG=$VBOX_TRG"
|
|---|
| 96 | VBOX_RC=3
|
|---|
| 97 | fi
|
|---|
| 98 | done
|
|---|
| 99 |
|
|---|
| 100 | #
|
|---|
| 101 | # Check that all the directories exist first.
|
|---|
| 102 | #
|
|---|
| 103 | if [ ! -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" ]; then
|
|---|
| 104 | ConsoleMessage "Error: /Library/Application Support/VirtualBox/${VBOXDRV}.kext is missing"
|
|---|
| 105 | VBOX_RC=4
|
|---|
| 106 | fi
|
|---|
| 107 | if [ ! -d "/Library/Application Support/VirtualBox/${VBOXUSB}.kext" ]; then
|
|---|
| 108 | ConsoleMessage "Error: /Library/Application Support/VirtualBox/${VBOXUSB}.kext is missing"
|
|---|
| 109 | VBOX_RC=5
|
|---|
| 110 | fi
|
|---|
| 111 | if [ ! -d "/Library/Application Support/VirtualBox/VBoxNetFlt.kext" ]; then
|
|---|
| 112 | ConsoleMessage "Error: /Library/Application Support/VirtualBox/VBoxNetFlt.kext is missing"
|
|---|
| 113 | VBOX_RC=6
|
|---|
| 114 | fi
|
|---|
| 115 | if [ ! -d "/Library/Application Support/VirtualBox/VBoxNetAdp.kext" ]; then
|
|---|
| 116 | ConsoleMessage "Error: /Library/Application Support/VirtualBox/VBoxNetAdp.kext is missing"
|
|---|
| 117 | VBOX_RC=7
|
|---|
| 118 | fi
|
|---|
| 119 |
|
|---|
| 120 | #
|
|---|
| 121 | # Check that no drivers are currently running.
|
|---|
| 122 | # (Try stop the service if this is the case.)
|
|---|
| 123 | #
|
|---|
| 124 | if [ $VBOX_RC -eq 0 ]; then
|
|---|
| 125 | if kextstat -lb org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
|
|---|
| 126 | ConsoleMessage "Error: ${VBOXDRV}.kext is already loaded"
|
|---|
| 127 | VBOX_RC=8
|
|---|
| 128 | fi
|
|---|
| 129 | if kextstat -lb org.virtualbox.kext.VBoxUSB 2>&1 | grep -q org.virtualbox.kext.VBoxUSB; then
|
|---|
| 130 | ConsoleMessage "Error: ${VBOXUSB}.kext is already loaded"
|
|---|
| 131 | VBOX_RC=9
|
|---|
| 132 | fi
|
|---|
| 133 | if kextstat -lb org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
|
|---|
| 134 | ConsoleMessage "Error: VBoxNetFlt.kext is already loaded"
|
|---|
| 135 | VBOX_RC=10
|
|---|
| 136 | fi
|
|---|
| 137 | if kextstat -lb org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
|
|---|
| 138 | ConsoleMessage "Error: VBoxNetAdp.kext is already loaded"
|
|---|
| 139 | VBOX_RC=11
|
|---|
| 140 | fi
|
|---|
| 141 | fi
|
|---|
| 142 |
|
|---|
| 143 | #
|
|---|
| 144 | # Load the drivers.
|
|---|
| 145 | #
|
|---|
| 146 | if [ $VBOX_RC -eq 0 ]; then
|
|---|
| 147 | ConsoleMessage "Loading ${VBOXDRV}.kext"
|
|---|
| 148 | if ! kextload "/Library/Application Support/VirtualBox/${VBOXDRV}.kext"; then
|
|---|
| 149 | ConsoleMessage "Error: Failed to load /Library/Application Support/VirtualBox/${VBOXDRV}.kext"
|
|---|
| 150 | VBOX_RC=12
|
|---|
| 151 | fi
|
|---|
| 152 |
|
|---|
| 153 | ConsoleMessage "Loading ${VBOXUSB}.kext"
|
|---|
| 154 | if ! kextload -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" "/Library/Application Support/VirtualBox/${VBOXUSB}.kext"; then
|
|---|
| 155 | ConsoleMessage "Error: Failed to load /Library/Application Support/VirtualBox/${VBOXUSB}.kext"
|
|---|
| 156 | VBOX_RC=13
|
|---|
| 157 | fi
|
|---|
| 158 |
|
|---|
| 159 | ConsoleMessage "Loading VBoxNetFlt.kext"
|
|---|
| 160 | if ! kextload -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" "/Library/Application Support/VirtualBox/VBoxNetFlt.kext"; then
|
|---|
| 161 | ConsoleMessage "Error: Failed to load /Library/Application Support/VirtualBox/VBoxNetFlt.kext"
|
|---|
| 162 | VBOX_RC=14
|
|---|
| 163 | fi
|
|---|
| 164 |
|
|---|
| 165 | ConsoleMessage "Loading VBoxNetAdp.kext"
|
|---|
| 166 | if ! kextload -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" "/Library/Application Support/VirtualBox/VBoxNetAdp.kext"; then
|
|---|
| 167 | ConsoleMessage "Error: Failed to load /Library/Application Support/VirtualBox/VBoxNetAdp.kext"
|
|---|
| 168 | VBOX_RC=15
|
|---|
| 169 | fi
|
|---|
| 170 |
|
|---|
| 171 | if [ $VBOX_RC -ne 0 ]; then
|
|---|
| 172 | # unload the drivers (ignoring failures)
|
|---|
| 173 | kextunload -b org.virtualbox.kext.VBoxNetAdp
|
|---|
| 174 | kextunload -b org.virtualbox.kext.VBoxNetFlt
|
|---|
| 175 | kextunload -b org.virtualbox.kext.VBoxUSB
|
|---|
| 176 | kextunload -b org.virtualbox.kext.VBoxDrv
|
|---|
| 177 | fi
|
|---|
| 178 | fi
|
|---|
| 179 |
|
|---|
| 180 | #
|
|---|
| 181 | # Set the error on failure.
|
|---|
| 182 | #
|
|---|
| 183 | if [ "$VBOX_RC" -ne "0" ]; then
|
|---|
| 184 | ConsoleMessage -f VirtualBox
|
|---|
| 185 | exit $VBOX_RC
|
|---|
| 186 | fi
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | StopService()
|
|---|
| 191 | {
|
|---|
| 192 | VBOX_RC=0
|
|---|
| 193 | VBOXDRV="VBoxDrv"
|
|---|
| 194 | VBOXUSB="VBoxUSB"
|
|---|
| 195 |
|
|---|
| 196 | if kextstat -lb org.virtualbox.kext.VBoxUSB 2>&1 | grep -q org.virtualbox.kext.VBoxUSB; then
|
|---|
| 197 | ConsoleMessage "Unloading ${VBOXUSB}.kext"
|
|---|
| 198 | if ! kextunload -m org.virtualbox.kext.VBoxUSB; then
|
|---|
| 199 | ConsoleMessage "Error: Failed to unload VBoxUSB.kext"
|
|---|
| 200 | VBOX_RC=16
|
|---|
| 201 | fi
|
|---|
| 202 | fi
|
|---|
| 203 |
|
|---|
| 204 | if kextstat -lb org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
|
|---|
| 205 | ConsoleMessage "Unloading VBoxNetFlt.kext"
|
|---|
| 206 | if ! kextunload -m org.virtualbox.kext.VBoxNetFlt; then
|
|---|
| 207 | ConsoleMessage "Error: Failed to unload VBoxNetFlt.kext"
|
|---|
| 208 | VBOX_RC=17
|
|---|
| 209 | fi
|
|---|
| 210 | fi
|
|---|
| 211 |
|
|---|
| 212 | if kextstat -lb org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
|
|---|
| 213 | ConsoleMessage "Unloading VBoxNetAdp.kext"
|
|---|
| 214 | if ! kextunload -m org.virtualbox.kext.VBoxNetAdp; then
|
|---|
| 215 | ConsoleMessage "Error: Failed to unload VBoxNetAdp.kext"
|
|---|
| 216 | VBOX_RC=18
|
|---|
| 217 | fi
|
|---|
| 218 | fi
|
|---|
| 219 |
|
|---|
| 220 | # This must come last because of dependencies.
|
|---|
| 221 | if kextstat -lb org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
|
|---|
| 222 | ConsoleMessage "Unloading ${VBOXDRV}.kext"
|
|---|
| 223 | if ! kextunload -m org.virtualbox.kext.VBoxDrv; then
|
|---|
| 224 | ConsoleMessage "Error: Failed to unload VBoxDrv.kext"
|
|---|
| 225 | VBOX_RC=19
|
|---|
| 226 | fi
|
|---|
| 227 | fi
|
|---|
| 228 |
|
|---|
| 229 | # Set the error on failure.
|
|---|
| 230 | if [ "$VBOX_RC" -ne "0" ]; then
|
|---|
| 231 | ConsoleMessage -f VirtualBox
|
|---|
| 232 | exit $VBOX_RC
|
|---|
| 233 | fi
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | RestartService()
|
|---|
| 238 | {
|
|---|
| 239 | StopService
|
|---|
| 240 | StartService
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 | RunService "$1"
|
|---|
| 245 |
|
|---|