VirtualBox

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

Last change on this file since 8712 was 8712, checked in by vboxsync, 17 years ago

Linux kernel module: Removed default runlevel, trust the host OS when choosing the runlevels

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1#! /bin/sh
2# Sun xVM VirtualBox
3# Linux kernel module init script
4
5#
6# Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 60
18# description: VirtualBox Linux kernel module
19#
20### BEGIN INIT INFO
21# Provides: vboxdrv
22# Required-Start: $syslog
23# Required-Stop:
24# Short-Description: VirtualBox Linux kernel module
25### END INIT INFO
26
27PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
28DEVICE=/dev/vboxdrv
29MODNAME=vboxdrv
30GROUPNAME=vboxusers
31LOG="/var/log/vbox-install.log"
32NOLSB=%NOLSB%
33
34[ -f /lib/lsb/init-functions ] || NOLSB=yes
35[ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
36
37if [ -n "$INSTALL_DIR" ]; then
38 VBOXMANAGE="$INSTALL_DIR/VBoxManage"
39 BUILDINTMP="$INSTALL_DIR/src/build_in_tmp"
40else
41 VBOXMANAGE="/usr/lib/virtualbox/VBoxManage"
42 BUILDINTMP="/usr/share/virtualbox/src/build_in_tmp"
43fi
44
45if [ -n "$NOLSB" ]; then
46 if [ -f /etc/redhat-release ]; then
47 system=redhat
48 elif [ -f /etc/SuSE-release ]; then
49 system=suse
50 elif [ -f /etc/gentoo-release ]; then
51 system=gentoo
52 fi
53fi
54
55[ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
56
57if [ -z "$NOLSB" ]; then
58 . /lib/lsb/init-functions
59 fail_msg() {
60 echo ""
61 log_failure_msg "$1"
62 }
63 succ_msg() {
64 log_success_msg " done."
65 }
66 begin_msg() {
67 log_daemon_msg "$@"
68 }
69else
70 if [ "$system" = "redhat" ]; then
71 . /etc/init.d/functions
72 fail_msg() {
73 echo -n " "
74 echo_failure
75 echo
76 echo " ($1)"
77 }
78 succ_msg() {
79 echo -n " "
80 echo_success
81 echo
82 }
83 elif [ "$system" = "suse" ]; then
84 . /etc/rc.status
85 fail_msg() {
86 rc_failed 1
87 rc_status -v
88 echo " ($1)"
89 }
90 succ_msg() {
91 rc_reset
92 rc_status -v
93 }
94 elif [ "$system" = "gentoo" ]; then
95 . /sbin/functions.sh
96 fail_msg() {
97 eerror "$1"
98 }
99 succ_msg() {
100 eend "$?"
101 }
102 begin_msg() {
103 ebegin "$1"
104 }
105 if [ "`which $0`" = "/sbin/rc" ]; then
106 shift
107 fi
108 else
109 fail_msg() {
110 echo " ...failed!"
111 echo " ($1)"
112 }
113 succ_msg() {
114 echo " ...done."
115 }
116 fi
117 if [ "$system" != "gentoo" ]; then
118 begin_msg() {
119 [ -z "${1:-}" ] && return 1
120 if [ -z "${2:-}" ]; then
121 echo -n "$1"
122 else
123 echo -n "$1: $2"
124 fi
125 }
126 fi
127fi
128
129failure()
130{
131 fail_msg "$1"
132 exit 0
133}
134
135running()
136{
137 lsmod | grep -q "$MODNAME[^_-]"
138}
139
140start()
141{
142 begin_msg "Starting VirtualBox kernel module"
143 if ! running; then
144 if ! find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
145 failure "No suitable module for running kernel found"
146 fi
147 if ! rm -f $DEVICE; then
148 failure "Cannot remove $DEVICE"
149 fi
150 if ! modprobe $MODNAME > /dev/null 2>&1; then
151 failure "modprobe $MODNAME failed. Please use 'dmesg' to find out why"
152 fi
153 sleep .2
154 fi
155 # ensure the character special exists
156 if [ ! -c $DEVICE ]; then
157 MAJOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/devices`
158 if [ ! -z "$MAJOR" ]; then
159 MINOR=0
160 else
161 MINOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/misc`
162 if [ ! -z "$MINOR" ]; then
163 MAJOR=10
164 fi
165 fi
166 if [ -z "$MAJOR" ]; then
167 rmmod $MODNAME 2>/dev/null
168 failure "Cannot locate the VirtualBox device"
169 fi
170 if ! mknod -m 0660 $DEVICE c $MAJOR $MINOR 2>/dev/null; then
171 rmmod $MODNAME 2>/dev/null
172 failure "Cannot create device $DEVICE with major $MAJOR and minor $MINOR"
173 fi
174 fi
175 # ensure permissions
176 if ! chown :$GROUPNAME $DEVICE 2>/dev/null; then
177 rmmod $MODNAME 2>/dev/null
178 failure "Cannot change owner $GROUPNAME for device $DEVICE"
179 fi
180 succ_msg
181}
182
183stop()
184{
185 begin_msg "Stopping VirtualBox kernel module"
186 if running; then
187 if ! rmmod $MODNAME 2>/dev/null; then
188 failure "Cannot unload module $MODNAME"
189 fi
190 if ! rm -f $DEVICE; then
191 failure "Cannot unlink $DEVICE"
192 fi
193 fi
194 succ_msg
195}
196
197# enter the following variables in /etc/default/virtualbox:
198# SHUTDOWN_USERS="foo bar"
199# check for running VMs of user foo and user bar
200# SHUTDOWN=poweroff
201# SHUTDOWN=acpibutton
202# SHUTDOWN=savestate
203# select one of these shutdown methods for running VMs
204stop_vms()
205{
206 wait=0
207 for i in $SHUTDOWN_USERS; do
208 # don't create the ipcd directory with wrong permissions!
209 if [ -d /tmp/.vbox-$i-ipc ]; then
210 export VBOX_IPC_SOCKETID="$i"
211 VMS=`$VBOXMANAGE -nologo list runningvms 2>/dev/null`
212 if [ -n "$VMS" ]; then
213 if [ "$SHUTDOWN" = "poweroff" ]; then
214 begin_msg "Powering off remaining VMs"
215 for v in $VMS; do
216 $VBOXMANAGE -nologo controlvm $v poweroff
217 done
218 succ_msg
219 elif [ "$SHUTDOWN" = "acpibutton" ]; then
220 begin_msg "Sending ACPI power button event to remaining VMs"
221 for v in $VMS; do
222 $VBOXMANAGE -nologo controlvm $v acpipowerbutton
223 wait=30
224 done
225 succ_msg
226 elif [ "$SHUTDOWN" = "savestate" ]; then
227 begin_msg "Saving state of remaining VMs"
228 for v in $VMS; do
229 $VBOXMANAGE -nologo controlvm $v savestate
230 done
231 succ_msg
232 fi
233 fi
234 fi
235 done
236 # wait for some seconds when doing ACPI shutdown
237 if [ "$wait" -ne 0 ]; then
238 begin_msg "Waiting for $wait seconds for VM shutdown"
239 sleep $wait
240 succ_msg
241 fi
242}
243
244setup()
245{
246 stop
247 if find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
248 begin_msg "Removing old VirtualBox kernel module"
249 find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|xargs rm -f 2>/dev/null
250 succ_msg
251 fi
252 begin_msg "Recompiling VirtualBox kernel module"
253 if ! $BUILDINTMP install > $LOG 2>&1; then
254 failure "Look at $LOG to find out what went wrong"
255 fi
256 succ_msg
257 start
258}
259
260dmnstatus()
261{
262 if running; then
263 echo "VirtualBox kernel module is loaded."
264 for i in $SHUTDOWN_USERS; do
265 # don't create the ipcd directory with wrong permissions!
266 if [ -d /tmp/.vbox-$i-ipc ]; then
267 export VBOX_IPC_SOCKETID="$i"
268 VMS=`$VBOXMANAGE -nologo list runningvms 2>/dev/null`
269 if [ -n "$VMS" ]; then
270 echo "The following VMs are currently running:"
271 for v in $VMS; do
272 echo " $v"
273 done
274 fi
275 fi
276 done
277 else
278 echo "VirtualBox kernel module is not loaded."
279 fi
280}
281
282case "$1" in
283start)
284 start
285 ;;
286stop)
287 stop_vms
288 stop
289 ;;
290stop_vms)
291 stop_vms
292 ;;
293restart)
294 stop && start
295 ;;
296force-reload)
297 stop
298 start
299 ;;
300setup)
301 setup
302 ;;
303status)
304 dmnstatus
305 ;;
306*)
307 echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status|setup}"
308 exit 1
309esac
310
311exit 0
Note: See TracBrowser for help on using the repository browser.

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