VirtualBox

Ticket #9482: vboxdrv

File vboxdrv, 12.4 KB (added by Alexander Bierbaumer, 13 years ago)
Line 
1#! /bin/sh
2# Oracle VM VirtualBox
3# Linux kernel module init script
4
5#
6# Copyright (C) 2006-2010 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: 35 30 70
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
29PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
30DEVICE=/dev/vboxdrv
31LOG="/var/log/vbox-install.log"
32NOLSB=
33DEBIAN=yes
34MODPROBE=/sbin/modprobe
35
36if $MODPROBE -c | grep -q '^allow_unsupported_modules *0'; then
37 MODPROBE="$MODPROBE --allow-unsupported-modules"
38fi
39
40[ -f /lib/lsb/init-functions ] || NOLSB=yes
41[ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
42
43if [ -n "$INSTALL_DIR" ]; then
44 VBOXMANAGE="$INSTALL_DIR/VBoxManage"
45 DODKMS="$INSTALL_DIR/src/vboxhost/do_dkms"
46 BUILDVBOXDRV="$INSTALL_DIR/src/vboxhost/vboxdrv/build_in_tmp"
47 BUILDVBOXNETFLT="$INSTALL_DIR/src/vboxhost/vboxnetflt/build_in_tmp"
48 BUILDVBOXNETADP="$INSTALL_DIR/src/vboxhost/vboxnetadp/build_in_tmp"
49# BUILDVBOXPCI="$INSTALL_DIR/src/vboxhost/vboxpci/build_in_tmp"
50else
51 VBOXMANAGE="/usr/lib/virtualbox/VBoxManage"
52 DODKMS="/usr/share/virtualbox/src/vboxhost/do_dkms"
53 BUILDVBOXDRV="/usr/share/virtualbox/src/vboxhost/vboxdrv/build_in_tmp"
54 BUILDVBOXNETFLT="/usr/share/virtualbox/src/vboxhost/vboxnetflt/build_in_tmp"
55 BUILDVBOXNETADP="/usr/share/virtualbox/src/vboxhost/vboxnetadp/build_in_tmp"
56# BUILDVBOXPCI="/usr/share/virtualbox/src/vboxhost/vboxpci/build_in_tmp"
57fi
58
59# silently exit if the package was uninstalled but not purged,
60# applies to Debian packages only
61[ -z "$DEBIAN" -o -x $VBOXMANAGE -a -x $BUILDVBOXDRV ] || exit 0
62
63if [ -n "$NOLSB" ]; then
64 if [ -f /etc/redhat-release ]; then
65 system=redhat
66 elif [ -f /etc/SuSE-release ]; then
67 system=suse
68 elif [ -f /etc/gentoo-release ]; then
69 system=gentoo
70 fi
71fi
72
73[ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
74
75if [ -z "$NOLSB" ]; then
76 . /lib/lsb/init-functions
77 fail_msg() {
78 echo ""
79 log_failure_msg "$1"
80 }
81 succ_msg() {
82 log_end_msg 0
83 }
84 begin_msg() {
85 log_daemon_msg "$@"
86 }
87else
88 if [ "$system" = "redhat" ]; then
89 . /etc/init.d/functions
90 fail_msg() {
91 echo -n " "
92 echo_failure
93 echo
94 echo " ($1)"
95 }
96 succ_msg() {
97 echo -n " "
98 echo_success
99 echo
100 }
101 elif [ "$system" = "suse" ]; then
102 . /etc/rc.status
103 fail_msg() {
104 rc_failed 1
105 rc_status -v
106 echo " ($1)"
107 }
108 succ_msg() {
109 rc_reset
110 rc_status -v
111 }
112 elif [ "$system" = "gentoo" ]; then
113 if [ -f /sbin/functions.sh ]; then
114 . /sbin/functions.sh
115 elif [ -f /etc/init.d/functions.sh ]; then
116 . /etc/init.d/functions.sh
117 fi
118 fail_msg() {
119 eerror "$1"
120 }
121 succ_msg() {
122 eend "$?"
123 }
124 begin_msg() {
125 ebegin "$1"
126 }
127 if [ "`which $0`" = "/sbin/rc" ]; then
128 shift
129 fi
130 else
131 fail_msg() {
132 echo " ...failed!"
133 echo " ($1)"
134 }
135 succ_msg() {
136 echo " ...done."
137 }
138 fi
139 if [ "$system" != "gentoo" ]; then
140 begin_msg() {
141 [ -z "${1:-}" ] && return 1
142 if [ -z "${2:-}" ]; then
143 echo -n "$1"
144 else
145 echo -n "$1: $2"
146 fi
147 }
148 fi
149fi
150
151failure()
152{
153 fail_msg "$1"
154 exit 0
155}
156
157running()
158{
159 lsmod | grep -q "$1[^_-]"
160}
161
162start()
163{
164 exit 0
165 begin_msg "Starting VirtualBox kernel modules"
166 if [ -d /proc/xen ]; then
167 failure "Running VirtualBox in a Xen environment is not supported"
168 fi
169 if ! running vboxdrv; then
170 if ! rm -f $DEVICE; then
171 failure "Cannot remove $DEVICE"
172 fi
173 if ! $MODPROBE vboxdrv > /dev/null 2>&1; then
174 failure "modprobe vboxdrv failed. Please use 'dmesg' to find out why"
175 fi
176 sleep .2
177 fi
178 # ensure the character special exists
179 if [ ! -c $DEVICE ]; then
180 MAJOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/devices`
181 if [ ! -z "$MAJOR" ]; then
182 MINOR=0
183 else
184 MINOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/misc`
185 if [ ! -z "$MINOR" ]; then
186 MAJOR=10
187 fi
188 fi
189 if [ -z "$MAJOR" ]; then
190 rmmod vboxdrv 2>/dev/null
191 failure "Cannot locate the VirtualBox device"
192 fi
193 if ! mknod -m 0660 $DEVICE c $MAJOR $MINOR 2>/dev/null; then
194 rmmod vboxdrv 2>/dev/null
195 failure "Cannot create device $DEVICE with major $MAJOR and minor $MINOR"
196 fi
197 fi
198 # ensure permissions
199 if ! chown :root $DEVICE 2>/dev/null; then
200# rmmod vboxpci 2>/dev/null
201 rmmod vboxnetadp 2>/dev/null
202 rmmod vboxnetflt 2>/dev/null
203 rmmod vboxdrv 2>/dev/null
204 failure "Cannot change group root for device $DEVICE"
205 fi
206 if ! $MODPROBE vboxnetflt > /dev/null 2>&1; then
207 failure "modprobe vboxnetflt failed. Please use 'dmesg' to find out why"
208 fi
209 if ! $MODPROBE vboxnetadp > /dev/null 2>&1; then
210 failure "modprobe vboxnetadp failed. Please use 'dmesg' to find out why"
211 fi
212# if ! $MODPROBE vboxpci > /dev/null 2>&1; then
213# failure "modprobe vboxpci failed. Please use 'dmesg' to find out why"
214# fi
215 # Create the /dev/vboxusb directory if the host supports that method
216 # of USB access. The USB code checks for the existance of that path.
217 if grep -q usb_device /proc/devices; then
218 mkdir -p -m 0750 /dev/vboxusb 2>/dev/null
219 chown root:vboxusers /dev/vboxusb 2>/dev/null
220 fi
221 succ_msg
222}
223
224stop()
225{
226 begin_msg "Stopping VirtualBox kernel modules"
227# if running vboxpci; then
228# if ! rmmod vboxpci 2>/dev/null; then
229# failure "Cannot unload module vboxpci"
230# fi
231# fi
232 if running vboxnetadp; then
233 if ! rmmod vboxnetadp 2>/dev/null; then
234 failure "Cannot unload module vboxnetadp"
235 fi
236 fi
237 if running vboxdrv; then
238 if running vboxnetflt; then
239 if ! rmmod vboxnetflt 2>/dev/null; then
240 failure "Cannot unload module vboxnetflt"
241 fi
242 fi
243 if ! rmmod vboxdrv 2>/dev/null; then
244 failure "Cannot unload module vboxdrv"
245 fi
246 if ! rm -f $DEVICE; then
247 failure "Cannot unlink $DEVICE"
248 fi
249 fi
250 succ_msg
251}
252
253# enter the following variables in /etc/default/virtualbox:
254# SHUTDOWN_USERS="foo bar"
255# check for running VMs of user foo and user bar
256# SHUTDOWN=poweroff
257# SHUTDOWN=acpibutton
258# SHUTDOWN=savestate
259# select one of these shutdown methods for running VMs
260stop_vms()
261{
262 wait=0
263 for i in $SHUTDOWN_USERS; do
264 # don't create the ipcd directory with wrong permissions!
265 if [ -d /tmp/.vbox-$i-ipc ]; then
266 export VBOX_IPC_SOCKETID="$i"
267 VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
268 if [ -n "$VMS" ]; then
269 if [ "$SHUTDOWN" = "poweroff" ]; then
270 begin_msg "Powering off remaining VMs"
271 for v in $VMS; do
272 $VBOXMANAGE --nologo controlvm $v poweroff
273 done
274 succ_msg
275 elif [ "$SHUTDOWN" = "acpibutton" ]; then
276 begin_msg "Sending ACPI power button event to remaining VMs"
277 for v in $VMS; do
278 $VBOXMANAGE --nologo controlvm $v acpipowerbutton
279 wait=30
280 done
281 succ_msg
282 elif [ "$SHUTDOWN" = "savestate" ]; then
283 begin_msg "Saving state of remaining VMs"
284 for v in $VMS; do
285 $VBOXMANAGE --nologo controlvm $v savestate
286 done
287 succ_msg
288 fi
289 fi
290 fi
291 done
292 # wait for some seconds when doing ACPI shutdown
293 if [ "$wait" -ne 0 ]; then
294 begin_msg "Waiting for $wait seconds for VM shutdown"
295 sleep $wait
296 succ_msg
297 fi
298}
299
300# setup_script
301setup()
302{
303 stop
304 begin_msg "Uninstalling old VirtualBox DKMS kernel modules"
305 $DODKMS uninstall > $LOG
306 succ_msg
307# if find /lib/modules/`uname -r` -name "vboxpci\.*" 2>/dev/null|grep -q vboxpci; then
308# begin_msg "Removing old VirtualBox pci kernel module"
309# find /lib/modules/`uname -r` -name "vboxpci\.*" 2>/dev/null|xargs rm -f 2>/dev/null
310# succ_msg
311# fi
312 if find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|grep -q vboxnetadp; then
313 begin_msg "Removing old VirtualBox netadp kernel module"
314 find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|xargs rm -f 2>/dev/null
315 succ_msg
316 fi
317 if find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|grep -q vboxnetflt; then
318 begin_msg "Removing old VirtualBox netflt kernel module"
319 find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|xargs rm -f 2>/dev/null
320 succ_msg
321 fi
322 if find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
323 begin_msg "Removing old VirtualBox kernel module"
324 find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|xargs rm -f 2>/dev/null
325 succ_msg
326 fi
327 begin_msg "Trying to register the VirtualBox kernel modules using DKMS"
328 if ! $DODKMS install >> $LOG; then
329 fail_msg "Failed, trying without DKMS"
330 begin_msg "Recompiling VirtualBox kernel modules"
331 if ! $BUILDVBOXDRV \
332 --save-module-symvers /tmp/vboxdrv-Module.symvers \
333 --no-print-directory install >> $LOG 2>&1; then
334 failure "Look at $LOG to find out what went wrong"
335 fi
336 if ! $BUILDVBOXNETFLT \
337 --use-module-symvers /tmp/vboxdrv-Module.symvers \
338 --no-print-directory install >> $LOG 2>&1; then
339 failure "Look at $LOG to find out what went wrong"
340 fi
341 if ! $BUILDVBOXNETADP \
342 --use-module-symvers /tmp/vboxdrv-Module.symvers \
343 --no-print-directory install >> $LOG 2>&1; then
344 failure "Look at $LOG to find out what went wrong"
345 fi
346 if ! $BUILDVBOXPCI \
347 --use-module-symvers /tmp/vboxdrv-Module.symvers \
348 --no-print-directory install >> $LOG 2>&1; then
349 failure "Look at $LOG to find out what went wrong"
350 fi
351 fi
352 rm -f /etc/vbox/module_not_compiled
353 succ_msg
354 start
355}
356
357dmnstatus()
358{
359 if running vboxdrv; then
360 str="vboxdrv"
361 if running vboxnetflt; then
362 str="$str, vboxnetflt"
363 if running vboxnetadp; then
364 str="$str, vboxnetadp"
365 fi
366 fi
367# if running vboxpci; then
368# str="$str, vboxpci"
369# fi
370 echo "VirtualBox kernel modules ($str) are loaded."
371 for i in $SHUTDOWN_USERS; do
372 # don't create the ipcd directory with wrong permissions!
373 if [ -d /tmp/.vbox-$i-ipc ]; then
374 export VBOX_IPC_SOCKETID="$i"
375 VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
376 if [ -n "$VMS" ]; then
377 echo "The following VMs are currently running:"
378 for v in $VMS; do
379 echo " $v"
380 done
381 fi
382 fi
383 done
384 else
385 echo "VirtualBox kernel module is not loaded."
386 fi
387}
388
389case "$1" in
390start)
391 start
392 ;;
393stop)
394 stop_vms
395 stop
396 ;;
397stop_vms)
398 stop_vms
399 ;;
400restart)
401 stop && start
402 ;;
403force-reload)
404 stop
405 start
406 ;;
407setup)
408 setup
409 ;;
410status)
411 dmnstatus
412 ;;
413*)
414 echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status|setup}"
415 exit 1
416esac
417
418exit 0

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy