VirtualBox

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

Last change on this file since 35273 was 35206, checked in by vboxsync, 14 years ago

Installer/linux: /dev/vboxdrv should belong to the group vboxusers in non-hardened builds

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