VirtualBox

source: vbox/trunk/src/VBox/Installer/linux/vboxdrv.sh.in@ 57986

Last change on this file since 57986 was 57986, checked in by vboxsync, 9 years ago

Installers/linux: try to rebuild the kernel modules if they cannot be loaded.

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

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