1 | #! /bin/sh
|
---|
2 | # Oracle VM VirtualBox
|
---|
3 | # Linux kernel module init script
|
---|
4 |
|
---|
5 | #
|
---|
6 | # Copyright (C) 2006-2015 Oracle Corporation
|
---|
7 | #
|
---|
8 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | # available from http://www.virtualbox.org. This file is free software;
|
---|
10 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | # General Public License (GPL) as published by the Free Software
|
---|
12 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | #
|
---|
16 |
|
---|
17 | # chkconfig: 345 20 80
|
---|
18 | # description: VirtualBox Linux kernel module
|
---|
19 | #
|
---|
20 | ### BEGIN INIT INFO
|
---|
21 | # Provides: vboxdrv
|
---|
22 | # Required-Start: $syslog
|
---|
23 | # Required-Stop:
|
---|
24 | # Default-Start: 2 3 4 5
|
---|
25 | # Default-Stop: 0 1 6
|
---|
26 | # Short-Description: VirtualBox Linux kernel module
|
---|
27 | ### END INIT INFO
|
---|
28 |
|
---|
29 | PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
|
---|
30 | DEVICE=/dev/vboxdrv
|
---|
31 | LOG="/var/log/vbox-install.log"
|
---|
32 | MODPROBE=/sbin/modprobe
|
---|
33 | SCRIPTNAME=vboxdrv
|
---|
34 |
|
---|
35 | if $MODPROBE -c | grep -q '^allow_unsupported_modules *0'; then
|
---|
36 | MODPROBE="$MODPROBE --allow-unsupported-modules"
|
---|
37 | fi
|
---|
38 |
|
---|
39 | [ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
|
---|
40 | export BUILD_TYPE
|
---|
41 | export USERNAME
|
---|
42 | export USER=$USERNAME
|
---|
43 |
|
---|
44 | if [ -n "$INSTALL_DIR" ]; then
|
---|
45 | VIRTUALBOX="$INSTALL_DIR/VirtualBox"
|
---|
46 | VBOXMANAGE="$INSTALL_DIR/VBoxManage"
|
---|
47 | MODULE_SRC="$INSTALL_DIR/src/vboxhost"
|
---|
48 | else
|
---|
49 | VIRTUALBOX="/usr/lib/virtualbox/VirtualBox"
|
---|
50 | VBOXMANAGE="/usr/lib/virtualbox/VBoxManage"
|
---|
51 | MODULE_SRC="/usr/share/virtualbox/src/vboxhost"
|
---|
52 | fi
|
---|
53 | BUILDINTMP="$MODULE_SRC/build_in_tmp"
|
---|
54 | if test -u "${VIRTUALBOX}"; then
|
---|
55 | GROUP=root
|
---|
56 | else
|
---|
57 | GROUP=vboxusers
|
---|
58 | fi
|
---|
59 |
|
---|
60 | # silently exit if the package was uninstalled but not purged,
|
---|
61 | # applies to Debian packages only (but shouldn't hurt elsewhere)
|
---|
62 | [ ! -f /etc/debian_release -o -x $VBOXMANAGE -a -x $BUILDINTMP ] || exit 0
|
---|
63 |
|
---|
64 | [ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
|
---|
65 |
|
---|
66 | # Preamble for Gentoo
|
---|
67 | if [ "`which $0`" = "/sbin/rc" ]; then
|
---|
68 | shift
|
---|
69 | fi
|
---|
70 |
|
---|
71 | begin_msg()
|
---|
72 | {
|
---|
73 | test -n "${2}" && echo "${SCRIPTNAME}: ${1}."
|
---|
74 | logger "${SCRIPTNAME}: ${1}."
|
---|
75 | }
|
---|
76 |
|
---|
77 | succ_msg()
|
---|
78 | {
|
---|
79 | logger "${SCRIPTNAME}: done."
|
---|
80 | }
|
---|
81 |
|
---|
82 | fail_msg()
|
---|
83 | {
|
---|
84 | echo "${SCRIPTNAME}: failed: ${1}." >&2
|
---|
85 | logger "${SCRIPTNAME}: failed: ${1}."
|
---|
86 | }
|
---|
87 |
|
---|
88 | failure()
|
---|
89 | {
|
---|
90 | fail_msg "$1"
|
---|
91 | exit 0
|
---|
92 | }
|
---|
93 |
|
---|
94 | running()
|
---|
95 | {
|
---|
96 | lsmod | grep -q "$1[^_-]"
|
---|
97 | }
|
---|
98 |
|
---|
99 | start()
|
---|
100 | {
|
---|
101 | begin_msg "Starting VirtualBox kernel modules" console
|
---|
102 | if [ -d /proc/xen ]; then
|
---|
103 | failure "Running VirtualBox in a Xen environment is not supported"
|
---|
104 | fi
|
---|
105 | if ! running vboxdrv; then
|
---|
106 | if ! rm -f $DEVICE; then
|
---|
107 | failure "Cannot remove $DEVICE"
|
---|
108 | fi
|
---|
109 | if ! $MODPROBE vboxdrv > /dev/null 2>&1; then
|
---|
110 | setup
|
---|
111 | if ! $MODPROBE vboxdrv > /dev/null 2>&1; then
|
---|
112 | failure "modprobe vboxdrv failed. Please use 'dmesg' to find out why"
|
---|
113 | fi
|
---|
114 | fi
|
---|
115 | sleep .2
|
---|
116 | fi
|
---|
117 | # ensure the character special exists
|
---|
118 | if [ ! -c $DEVICE ]; then
|
---|
119 | MAJOR=`sed -n 's;\([0-9]\+\) vboxdrv$;\1;p' /proc/devices`
|
---|
120 | if [ ! -z "$MAJOR" ]; then
|
---|
121 | MINOR=0
|
---|
122 | else
|
---|
123 | MINOR=`sed -n 's;\([0-9]\+\) vboxdrv$;\1;p' /proc/misc`
|
---|
124 | if [ ! -z "$MINOR" ]; then
|
---|
125 | MAJOR=10
|
---|
126 | fi
|
---|
127 | fi
|
---|
128 | if [ -z "$MAJOR" ]; then
|
---|
129 | rmmod vboxdrv 2>/dev/null
|
---|
130 | failure "Cannot locate the VirtualBox device"
|
---|
131 | fi
|
---|
132 | if ! mknod -m 0660 $DEVICE c $MAJOR $MINOR 2>/dev/null; then
|
---|
133 | rmmod vboxdrv 2>/dev/null
|
---|
134 | failure "Cannot create device $DEVICE with major $MAJOR and minor $MINOR"
|
---|
135 | fi
|
---|
136 | fi
|
---|
137 | # ensure permissions
|
---|
138 | if ! chown :"${GROUP}" $DEVICE 2>/dev/null; then
|
---|
139 | rmmod vboxpci 2>/dev/null
|
---|
140 | rmmod vboxnetadp 2>/dev/null
|
---|
141 | rmmod vboxnetflt 2>/dev/null
|
---|
142 | rmmod vboxdrv 2>/dev/null
|
---|
143 | failure "Cannot change group ${GROUP} for device $DEVICE"
|
---|
144 | fi
|
---|
145 | if ! $MODPROBE vboxnetflt > /dev/null 2>&1; then
|
---|
146 | failure "modprobe vboxnetflt failed. Please use 'dmesg' to find out why"
|
---|
147 | fi
|
---|
148 | if ! $MODPROBE vboxnetadp > /dev/null 2>&1; then
|
---|
149 | failure "modprobe vboxnetadp failed. Please use 'dmesg' to find out why"
|
---|
150 | fi
|
---|
151 | if ! $MODPROBE vboxpci > /dev/null 2>&1; then
|
---|
152 | failure "modprobe vboxpci failed. Please use 'dmesg' to find out why"
|
---|
153 | fi
|
---|
154 | # Create the /dev/vboxusb directory if the host supports that method
|
---|
155 | # of USB access. The USB code checks for the existance of that path.
|
---|
156 | if grep -q usb_device /proc/devices; then
|
---|
157 | mkdir -p -m 0750 /dev/vboxusb 2>/dev/null
|
---|
158 | chown root:vboxusers /dev/vboxusb 2>/dev/null
|
---|
159 | fi
|
---|
160 | succ_msg
|
---|
161 | }
|
---|
162 |
|
---|
163 | stop()
|
---|
164 | {
|
---|
165 | begin_msg "Stopping VirtualBox kernel modules" console
|
---|
166 | if running vboxpci; then
|
---|
167 | if ! rmmod vboxpci 2>/dev/null; then
|
---|
168 | failure "Cannot unload module vboxpci"
|
---|
169 | fi
|
---|
170 | fi
|
---|
171 | if running vboxnetadp; then
|
---|
172 | if ! rmmod vboxnetadp 2>/dev/null; then
|
---|
173 | failure "Cannot unload module vboxnetadp"
|
---|
174 | fi
|
---|
175 | fi
|
---|
176 | if running vboxdrv; then
|
---|
177 | if running vboxnetflt; then
|
---|
178 | if ! rmmod vboxnetflt 2>/dev/null; then
|
---|
179 | failure "Cannot unload module vboxnetflt"
|
---|
180 | fi
|
---|
181 | fi
|
---|
182 | if ! rmmod vboxdrv 2>/dev/null; then
|
---|
183 | failure "Cannot unload module vboxdrv"
|
---|
184 | fi
|
---|
185 | if ! rm -f $DEVICE; then
|
---|
186 | failure "Cannot unlink $DEVICE"
|
---|
187 | fi
|
---|
188 | fi
|
---|
189 | succ_msg
|
---|
190 | }
|
---|
191 |
|
---|
192 | # enter the following variables in /etc/default/virtualbox:
|
---|
193 | # SHUTDOWN_USERS="foo bar"
|
---|
194 | # check for running VMs of user foo and user bar
|
---|
195 | # SHUTDOWN=poweroff
|
---|
196 | # SHUTDOWN=acpibutton
|
---|
197 | # SHUTDOWN=savestate
|
---|
198 | # select one of these shutdown methods for running VMs
|
---|
199 | stop_vms()
|
---|
200 | {
|
---|
201 | wait=0
|
---|
202 | for i in $SHUTDOWN_USERS; do
|
---|
203 | # don't create the ipcd directory with wrong permissions!
|
---|
204 | if [ -d /tmp/.vbox-$i-ipc ]; then
|
---|
205 | export VBOX_IPC_SOCKETID="$i"
|
---|
206 | VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
|
---|
207 | if [ -n "$VMS" ]; then
|
---|
208 | if [ "$SHUTDOWN" = "poweroff" ]; then
|
---|
209 | begin_msg "Powering off remaining VMs"
|
---|
210 | for v in $VMS; do
|
---|
211 | $VBOXMANAGE --nologo controlvm $v poweroff
|
---|
212 | done
|
---|
213 | succ_msg
|
---|
214 | elif [ "$SHUTDOWN" = "acpibutton" ]; then
|
---|
215 | begin_msg "Sending ACPI power button event to remaining VMs"
|
---|
216 | for v in $VMS; do
|
---|
217 | $VBOXMANAGE --nologo controlvm $v acpipowerbutton
|
---|
218 | wait=30
|
---|
219 | done
|
---|
220 | succ_msg
|
---|
221 | elif [ "$SHUTDOWN" = "savestate" ]; then
|
---|
222 | begin_msg "Saving state of remaining VMs"
|
---|
223 | for v in $VMS; do
|
---|
224 | $VBOXMANAGE --nologo controlvm $v savestate
|
---|
225 | done
|
---|
226 | succ_msg
|
---|
227 | fi
|
---|
228 | fi
|
---|
229 | fi
|
---|
230 | done
|
---|
231 | # wait for some seconds when doing ACPI shutdown
|
---|
232 | if [ "$wait" -ne 0 ]; then
|
---|
233 | begin_msg "Waiting for $wait seconds for VM shutdown"
|
---|
234 | sleep $wait
|
---|
235 | succ_msg
|
---|
236 | fi
|
---|
237 | }
|
---|
238 |
|
---|
239 | # setup_script
|
---|
240 | setup()
|
---|
241 | {
|
---|
242 | begin_msg "Building VirtualBox kernel modules" console
|
---|
243 | stop >/dev/null
|
---|
244 | if find /lib/modules/`uname -r` -name "vboxpci\.*" 2>/dev/null|grep -q vboxpci; then
|
---|
245 | begin_msg "Removing old VirtualBox pci kernel module"
|
---|
246 | find /lib/modules/`uname -r` -name "vboxpci\.*" 2>/dev/null|xargs rm -f 2>/dev/null
|
---|
247 | succ_msg
|
---|
248 | fi
|
---|
249 | if find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|grep -q vboxnetadp; then
|
---|
250 | begin_msg "Removing old VirtualBox netadp kernel module"
|
---|
251 | find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|xargs rm -f 2>/dev/null
|
---|
252 | succ_msg
|
---|
253 | fi
|
---|
254 | if find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|grep -q vboxnetflt; then
|
---|
255 | begin_msg "Removing old VirtualBox netflt kernel module"
|
---|
256 | find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|xargs rm -f 2>/dev/null
|
---|
257 | succ_msg
|
---|
258 | fi
|
---|
259 | if find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
|
---|
260 | begin_msg "Removing old VirtualBox kernel module"
|
---|
261 | find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|xargs rm -f 2>/dev/null
|
---|
262 | succ_msg
|
---|
263 | fi
|
---|
264 | begin_msg "Recompiling VirtualBox kernel modules"
|
---|
265 | if ! $BUILDINTMP \
|
---|
266 | --save-module-symvers /tmp/vboxdrv-Module.symvers \
|
---|
267 | --module-source "$MODULE_SRC/vboxdrv" \
|
---|
268 | --no-print-directory install >> $LOG 2>&1; then
|
---|
269 | failure "Look at $LOG to find out what went wrong"
|
---|
270 | fi
|
---|
271 | if ! $BUILDINTMP \
|
---|
272 | --use-module-symvers /tmp/vboxdrv-Module.symvers \
|
---|
273 | --module-source "$MODULE_SRC/vboxnetflt" \
|
---|
274 | --no-print-directory install >> $LOG 2>&1; then
|
---|
275 | failure "Look at $LOG to find out what went wrong"
|
---|
276 | fi
|
---|
277 | if ! $BUILDINTMP \
|
---|
278 | --use-module-symvers /tmp/vboxdrv-Module.symvers \
|
---|
279 | --module-source "$MODULE_SRC/vboxnetadp" \
|
---|
280 | --no-print-directory install >> $LOG 2>&1; then
|
---|
281 | failure "Look at $LOG to find out what went wrong"
|
---|
282 | fi
|
---|
283 | if ! $BUILDINTMP \
|
---|
284 | --use-module-symvers /tmp/vboxdrv-Module.symvers \
|
---|
285 | --module-source "$MODULE_SRC/vboxpci" \
|
---|
286 | --no-print-directory install >> $LOG 2>&1; then
|
---|
287 | failure "Look at $LOG to find out what went wrong"
|
---|
288 | fi
|
---|
289 | rm -f /etc/vbox/module_not_compiled
|
---|
290 | succ_msg
|
---|
291 | }
|
---|
292 |
|
---|
293 | dmnstatus()
|
---|
294 | {
|
---|
295 | if running vboxdrv; then
|
---|
296 | str="vboxdrv"
|
---|
297 | if running vboxnetflt; then
|
---|
298 | str="$str, vboxnetflt"
|
---|
299 | if running vboxnetadp; then
|
---|
300 | str="$str, vboxnetadp"
|
---|
301 | fi
|
---|
302 | fi
|
---|
303 | if running vboxpci; then
|
---|
304 | str="$str, vboxpci"
|
---|
305 | fi
|
---|
306 | echo "VirtualBox kernel modules ($str) are loaded."
|
---|
307 | for i in $SHUTDOWN_USERS; do
|
---|
308 | # don't create the ipcd directory with wrong permissions!
|
---|
309 | if [ -d /tmp/.vbox-$i-ipc ]; then
|
---|
310 | export VBOX_IPC_SOCKETID="$i"
|
---|
311 | VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
|
---|
312 | if [ -n "$VMS" ]; then
|
---|
313 | echo "The following VMs are currently running:"
|
---|
314 | for v in $VMS; do
|
---|
315 | echo " $v"
|
---|
316 | done
|
---|
317 | fi
|
---|
318 | fi
|
---|
319 | done
|
---|
320 | else
|
---|
321 | echo "VirtualBox kernel module is not loaded."
|
---|
322 | fi
|
---|
323 | }
|
---|
324 |
|
---|
325 | case "$1" in
|
---|
326 | start)
|
---|
327 | start
|
---|
328 | ;;
|
---|
329 | stop)
|
---|
330 | stop_vms
|
---|
331 | stop
|
---|
332 | ;;
|
---|
333 | stop_vms)
|
---|
334 | stop_vms
|
---|
335 | ;;
|
---|
336 | restart)
|
---|
337 | stop && start
|
---|
338 | ;;
|
---|
339 | force-reload)
|
---|
340 | stop
|
---|
341 | start
|
---|
342 | ;;
|
---|
343 | status)
|
---|
344 | dmnstatus
|
---|
345 | ;;
|
---|
346 | *)
|
---|
347 | echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status}"
|
---|
348 | exit 1
|
---|
349 | esac
|
---|
350 |
|
---|
351 | exit 0
|
---|