1 | #!/bin/sh
|
---|
2 | # $Id: VirtualBoxStartup.sh 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
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-2023 Oracle and/or its affiliates.
|
---|
10 | #
|
---|
11 | # This file is part of VirtualBox base platform packages, as
|
---|
12 | # available from https://www.virtualbox.org.
|
---|
13 | #
|
---|
14 | # This program is free software; you can redistribute it and/or
|
---|
15 | # modify it under the terms of the GNU General Public License
|
---|
16 | # as published by the Free Software Foundation, in version 3 of the
|
---|
17 | # License.
|
---|
18 | #
|
---|
19 | # This program is distributed in the hope that it will be useful, but
|
---|
20 | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | # General Public License for more details.
|
---|
23 | #
|
---|
24 | # You should have received a copy of the GNU General Public License
|
---|
25 | # along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | #
|
---|
27 | # SPDX-License-Identifier: GPL-3.0-only
|
---|
28 | #
|
---|
29 |
|
---|
30 | if false; then
|
---|
31 | . /etc/rc.common
|
---|
32 | else
|
---|
33 | # Fake the startup item functions we're using.
|
---|
34 |
|
---|
35 | ConsoleMessage()
|
---|
36 | {
|
---|
37 | if [ "$1" != "-f" ]; then
|
---|
38 | echo "$@"
|
---|
39 | else
|
---|
40 | shift
|
---|
41 | echo "Fatal error: $@"
|
---|
42 | exit 1;
|
---|
43 | fi
|
---|
44 | }
|
---|
45 |
|
---|
46 | RunService()
|
---|
47 | {
|
---|
48 | case "$1" in
|
---|
49 | "start")
|
---|
50 | StartService
|
---|
51 | exit $?;
|
---|
52 | ;;
|
---|
53 | "stop")
|
---|
54 | StopService
|
---|
55 | exit $?;
|
---|
56 | ;;
|
---|
57 | "restart")
|
---|
58 | RestartService
|
---|
59 | exit $?;
|
---|
60 | ;;
|
---|
61 | "launchd")
|
---|
62 | if RestartService; then
|
---|
63 | while true;
|
---|
64 | do
|
---|
65 | sleep 3600
|
---|
66 | done
|
---|
67 | fi
|
---|
68 | exit $?;
|
---|
69 | ;;
|
---|
70 | **)
|
---|
71 | echo "Error: Unknown action '$1'"
|
---|
72 | exit 1;
|
---|
73 | esac
|
---|
74 | }
|
---|
75 | fi
|
---|
76 |
|
---|
77 |
|
---|
78 | StartService()
|
---|
79 | {
|
---|
80 | VBOX_RC=0
|
---|
81 | VBOXDRV="VBoxDrv"
|
---|
82 | MACOS_VERSION_MAJOR=$(sw_vers -productVersion | /usr/bin/sed -e 's/^\([0-9]*\).*$/\1/')
|
---|
83 |
|
---|
84 | #
|
---|
85 | # Check that all the directories exist first.
|
---|
86 | #
|
---|
87 | if [ ! -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" ]; then
|
---|
88 | ConsoleMessage "Error: /Library/Application Support/VirtualBox/${VBOXDRV}.kext is missing"
|
---|
89 | VBOX_RC=1
|
---|
90 | fi
|
---|
91 | if [ ! -d "/Library/Application Support/VirtualBox/VBoxNetFlt.kext" ]; then
|
---|
92 | ConsoleMessage "Error: /Library/Application Support/VirtualBox/VBoxNetFlt.kext is missing"
|
---|
93 | VBOX_RC=1
|
---|
94 | fi
|
---|
95 | if [ ! -d "/Library/Application Support/VirtualBox/VBoxNetAdp.kext" ]; then
|
---|
96 | ConsoleMessage "Error: /Library/Application Support/VirtualBox/VBoxNetAdp.kext is missing"
|
---|
97 | VBOX_RC=1
|
---|
98 | fi
|
---|
99 |
|
---|
100 | #
|
---|
101 | # Check that no drivers are currently running.
|
---|
102 | # (Try stop the service if this is the case.)
|
---|
103 | #
|
---|
104 | if [ $VBOX_RC -eq 0 ]; then
|
---|
105 | if [[ ${MACOS_VERSION_MAJOR} -lt 11 ]]; then
|
---|
106 | if kextstat -lb org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
|
---|
107 | ConsoleMessage "Error: ${VBOXDRV}.kext is already loaded"
|
---|
108 | VBOX_RC=1
|
---|
109 | fi
|
---|
110 | if kextstat -lb org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
|
---|
111 | ConsoleMessage "Error: VBoxNetFlt.kext is already loaded"
|
---|
112 | VBOX_RC=1
|
---|
113 | fi
|
---|
114 | if kextstat -lb org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
|
---|
115 | ConsoleMessage "Error: VBoxNetAdp.kext is already loaded"
|
---|
116 | VBOX_RC=1
|
---|
117 | fi
|
---|
118 | else
|
---|
119 | #
|
---|
120 | # Use kmutil directly on BigSur or grep will erroneously trigger because kextstat dumps the kmutil
|
---|
121 | # invocation to stdout...
|
---|
122 | #
|
---|
123 | if kmutil showloaded --list-only -b org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
|
---|
124 | ConsoleMessage "Error: ${VBOXDRV}.kext is already loaded"
|
---|
125 | VBOX_RC=1
|
---|
126 | fi
|
---|
127 | if kmutil showloaded --list-only -b org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
|
---|
128 | ConsoleMessage "Error: VBoxNetFlt.kext is already loaded"
|
---|
129 | VBOX_RC=1
|
---|
130 | fi
|
---|
131 | if kmutil showloaded --list-only -b org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
|
---|
132 | ConsoleMessage "Error: VBoxNetAdp.kext is already loaded"
|
---|
133 | VBOX_RC=1
|
---|
134 | fi
|
---|
135 | fi
|
---|
136 | fi
|
---|
137 |
|
---|
138 | #
|
---|
139 | # Load the drivers.
|
---|
140 | #
|
---|
141 | if [ $VBOX_RC -eq 0 ]; then
|
---|
142 | if [[ ${MACOS_VERSION_MAJOR} -lt 11 ]]; then
|
---|
143 | ConsoleMessage "Loading ${VBOXDRV}.kext"
|
---|
144 | if ! kextload "/Library/Application Support/VirtualBox/${VBOXDRV}.kext"; then
|
---|
145 | ConsoleMessage "Error: Failed to load /Library/Application Support/VirtualBox/${VBOXDRV}.kext"
|
---|
146 | VBOX_RC=1
|
---|
147 | fi
|
---|
148 |
|
---|
149 | ConsoleMessage "Loading VBoxNetFlt.kext"
|
---|
150 | if ! kextload -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" "/Library/Application Support/VirtualBox/VBoxNetFlt.kext"; then
|
---|
151 | ConsoleMessage "Error: Failed to load /Library/Application Support/VirtualBox/VBoxNetFlt.kext"
|
---|
152 | VBOX_RC=1
|
---|
153 | fi
|
---|
154 |
|
---|
155 | ConsoleMessage "Loading VBoxNetAdp.kext"
|
---|
156 | if ! kextload -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" "/Library/Application Support/VirtualBox/VBoxNetAdp.kext"; then
|
---|
157 | ConsoleMessage "Error: Failed to load /Library/Application Support/VirtualBox/VBoxNetAdp.kext"
|
---|
158 | VBOX_RC=1
|
---|
159 | fi
|
---|
160 | else
|
---|
161 | #
|
---|
162 | # On BigSur we can only load by bundle ID because the drivers are baked into a kext collection image
|
---|
163 | # and the real path is never loaded actually.
|
---|
164 | #
|
---|
165 | ConsoleMessage "Loading ${VBOXDRV}.kext"
|
---|
166 | if ! kmutil load -b org.virtualbox.kext.VBoxDrv; then
|
---|
167 | ConsoleMessage "Error: Failed to load org.virtualbox.kext.VBoxDrv"
|
---|
168 | VBOX_RC=1
|
---|
169 | fi
|
---|
170 |
|
---|
171 | ConsoleMessage "Loading VBoxNetFlt.kext"
|
---|
172 | if ! kmutil load -b org.virtualbox.kext.VBoxNetFlt; then
|
---|
173 | ConsoleMessage "Error: Failed to load org.virtualbox.kext.VBoxNetFlt"
|
---|
174 | VBOX_RC=1
|
---|
175 | fi
|
---|
176 |
|
---|
177 | ConsoleMessage "Loading VBoxNetAdp.kext"
|
---|
178 | if ! kmutil load -b org.virtualbox.kext.VBoxNetAdp; then
|
---|
179 | ConsoleMessage "Error: Failed to load org.virtualbox.kext.VBoxNetAdp"
|
---|
180 | VBOX_RC=1
|
---|
181 | fi
|
---|
182 | fi
|
---|
183 |
|
---|
184 | if [ $VBOX_RC -ne 0 ]; then
|
---|
185 | # unload the drivers (ignoring failures)
|
---|
186 | kextunload -b org.virtualbox.kext.VBoxNetAdp
|
---|
187 | kextunload -b org.virtualbox.kext.VBoxNetFlt
|
---|
188 | kextunload -b org.virtualbox.kext.VBoxDrv
|
---|
189 | fi
|
---|
190 | fi
|
---|
191 |
|
---|
192 | #
|
---|
193 | # Set the error on failure.
|
---|
194 | #
|
---|
195 | if [ "$VBOX_RC" -ne "0" ]; then
|
---|
196 | ConsoleMessage -f VirtualBox
|
---|
197 | exit $VBOX_RC
|
---|
198 | fi
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | StopService()
|
---|
203 | {
|
---|
204 | VBOX_RC=0
|
---|
205 | VBOXDRV="VBoxDrv"
|
---|
206 | VBOXUSB="VBoxUSB"
|
---|
207 | MACOS_VERSION_MAJOR=$(sw_vers -productVersion | /usr/bin/sed -e 's/^\([0-9]*\).*$/\1/')
|
---|
208 |
|
---|
209 | if [[ ${MACOS_VERSION_MAJOR} -lt 11 ]]; then
|
---|
210 | if kextstat -lb org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
|
---|
211 | ConsoleMessage "Unloading VBoxNetFlt.kext"
|
---|
212 | if ! kextunload -m org.virtualbox.kext.VBoxNetFlt; then
|
---|
213 | ConsoleMessage "Error: Failed to unload VBoxNetFlt.kext"
|
---|
214 | VBOX_RC=1
|
---|
215 | fi
|
---|
216 | fi
|
---|
217 |
|
---|
218 | if kextstat -lb org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
|
---|
219 | ConsoleMessage "Unloading VBoxNetAdp.kext"
|
---|
220 | if ! kextunload -m org.virtualbox.kext.VBoxNetAdp; then
|
---|
221 | ConsoleMessage "Error: Failed to unload VBoxNetAdp.kext"
|
---|
222 | VBOX_RC=1
|
---|
223 | fi
|
---|
224 | fi
|
---|
225 |
|
---|
226 | # This must come last because of dependencies.
|
---|
227 | if kextstat -lb org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
|
---|
228 | ConsoleMessage "Unloading ${VBOXDRV}.kext"
|
---|
229 | if ! kextunload -m org.virtualbox.kext.VBoxDrv; then
|
---|
230 | ConsoleMessage "Error: Failed to unload VBoxDrv.kext"
|
---|
231 | VBOX_RC=1
|
---|
232 | fi
|
---|
233 | fi
|
---|
234 | else
|
---|
235 | if kmutil showloaded --list-only -b org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
|
---|
236 | ConsoleMessage "Unloading VBoxNetFlt.kext"
|
---|
237 | if ! kmutil unload -b org.virtualbox.kext.VBoxNetFlt; then
|
---|
238 | ConsoleMessage "Error: Failed to unload VBoxNetFlt.kext"
|
---|
239 | VBOX_RC=1
|
---|
240 | fi
|
---|
241 | fi
|
---|
242 |
|
---|
243 | if kmutil showloaded --list-only -b org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
|
---|
244 | ConsoleMessage "Unloading VBoxNetAdp.kext"
|
---|
245 | if ! kmutil unload -b org.virtualbox.kext.VBoxNetAdp; then
|
---|
246 | ConsoleMessage "Error: Failed to unload VBoxNetAdp.kext"
|
---|
247 | VBOX_RC=1
|
---|
248 | fi
|
---|
249 | fi
|
---|
250 |
|
---|
251 | # This must come last because of dependencies.
|
---|
252 | if kmutil showloaded --list-only -b org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
|
---|
253 | ConsoleMessage "Unloading ${VBOXDRV}.kext"
|
---|
254 | if ! kmutil unload -b org.virtualbox.kext.VBoxDrv; then
|
---|
255 | ConsoleMessage "Error: Failed to unload VBoxDrv.kext"
|
---|
256 | VBOX_RC=1
|
---|
257 | fi
|
---|
258 | fi
|
---|
259 | fi
|
---|
260 |
|
---|
261 | # Set the error on failure.
|
---|
262 | if [ "$VBOX_RC" -ne "0" ]; then
|
---|
263 | ConsoleMessage -f VirtualBox
|
---|
264 | exit $VBOX_RC
|
---|
265 | fi
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | RestartService()
|
---|
270 | {
|
---|
271 | StopService
|
---|
272 | StartService
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | RunService "$1"
|
---|
277 |
|
---|