VirtualBox

Ticket #19117: virtualbox

File virtualbox, 6.3 KB (added by Harry M, 5 years ago)
Line 
1#! /bin/sh
2# Sun VirtualBox
3# Linux kernel module init script
4
5#
6# Copyright (C) 2006-2009 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 70
18# description: VirtualBox Linux kernel module
19#
20### BEGIN INIT INFO
21# Provides: virtualbox
22# Required-Start: $remote_fs $network
23# Required-Stop: $remote_fs
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. /lib/lsb/init-functions
30
31test -d /usr/share/doc/virtualbox -a -x /usr/bin/VBoxHeadless || exit 0
32
33# Include virtualbox defaults if available
34[ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
35
36# set list of all active users if asked to do so
37if [ "$SHUTDOWN_USERS" = "all" ]; then
38 SHUTDOWN_USERS=""
39 for i in /tmp/.vbox-*-ipc; do
40 SHUTDOWN_USERS="$SHUTDOWN_USERS $(echo $i|cut -d'-' -f2)"
41 done
42fi
43
44if [ "$LOAD_VBOXDRV_MODULE" != 1 ]; then
45 log_success_msg "virtualbox disabled; edit /etc/default/virtualbox"
46 exit 0
47fi
48
49running()
50{
51 lsmod | grep -q "$1[^_-]"
52}
53
54start()
55{
56 log_begin_msg "Loading VirtualBox kernel modules..."
57
58 if ! running vboxdrv; then
59 if ! modprobe vboxdrv > /dev/null 2>&1; then
60 if ! find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
61 log_failure_msg "No suitable module for running kernel found"
62 else
63 log_failure_msg "modprobe vboxdrv failed. Please use 'dmesg' to find out why"
64 fi
65 log_end_msg 1
66 return 1
67 else
68 log_progress_msg "vboxdrv"
69 fi
70 fi
71
72 if ! running vboxnetflt; then
73 if ! modprobe vboxnetflt > /dev/null 2>&1; then
74 if ! find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|grep -q vboxnetflt; then
75 log_failure_msg "No suitable vboxnetflt module for running kernel found"
76 else
77 log_failure_msg "modprobe vboxnetflt failed. Please use 'dmesg' to find out why"
78 fi
79 log_end_msg 1
80 return 1
81 else
82 log_progress_msg "vboxnetflt"
83 fi
84 fi
85
86 if ! running vboxnetadp; then
87 if ! modprobe vboxnetadp > /dev/null 2>&1; then
88 if ! find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|grep -q vboxnetadp; then
89 log_failure_msg "No suitable vboxnetadp module for running kernel found"
90 else
91 log_failure_msg "modprobe vboxnetadp failed. Please use 'dmesg' to find out why"
92 fi
93 log_end_msg 1
94 return 1
95 else
96 log_progress_msg "vboxnetadp"
97 fi
98 fi
99
100 if ! running vboxpci; then
101 if ! modprobe vboxpci > /dev/null 2>&1; then
102 if ! find /lib/modules/`uname -r` -name "vboxpci\.*" 2>/dev/null|grep -q vboxpci; then
103 log_failure_msg "No suitable vboxpci module for running kernel found"
104 else
105 log_failure_msg "modprobe vboxpci failed. Please use 'dmesg' to find out why"
106 fi
107 log_end_msg 1
108 return 1
109 else
110 log_progress_msg "vboxpci"
111 fi
112 fi
113
114 log_end_msg 0
115}
116
117stop()
118{
119 log_begin_msg "Unloading VirtualBox kernel modules..."
120
121 if running vboxnetadp; then
122 if ! rmmod vboxnetadp 2>/dev/null; then
123 log_failure_msg "Cannot unload module vboxnetadp"
124 log_end_msg 1
125 return 1
126 else
127 log_progress_msg "vboxnetadp"
128 fi
129 fi
130
131 if running vboxnetflt; then
132 if ! rmmod vboxnetflt 2>/dev/null; then
133 log_failure_msg "Cannot unload module vboxnetflt"
134 log_end_msg 1
135 return 1
136 else
137 log_progress_msg "vboxnetflt"
138 fi
139 fi
140
141 if running vboxpci; then
142 if ! rmmod vboxpci 2>/dev/null; then
143 log_failure_msg "Cannot unload module vboxpci"
144 log_end_msg 1
145 return 1
146 else
147 log_progress_msg "vboxpci"
148 fi
149 fi
150
151 if running vboxdrv; then
152 if ! rmmod vboxdrv 2>/dev/null; then
153 log_failure_msg "Cannot unload module vboxdrv"
154 log_end_msg 1
155 return 1
156 else
157 log_progress_msg "vboxdrv"
158 fi
159 fi
160
161 log_end_msg 0
162}
163
164# enter the following variables in /etc/default/virtualbox:
165# SHUTDOWN_USERS="foo bar"
166# check for running VMs of user foo and user bar
167# SHUTDOWN=poweroff
168# SHUTDOWN=acpibutton
169# SHUTDOWN=savestate
170# select one of these shutdown methods for running VMs
171stop_vms()
172{
173 if ! pidof VBoxSVC > /dev/null; then
174 return 0
175 fi
176
177 wait=0
178
179 for i in $SHUTDOWN_USERS; do
180 if [ -d /tmp/.vbox-$i-ipc ]; then
181 export VBOX_IPC_SOCKETID="$i"
182 VMS=`VBoxManage --nologo list runningvms 2>/dev/null`
183 if [ $? -eq 0 -a -n "$VMS" ]; then
184 VMS=`echo "$VMS" | sed -e 's/^".*".*{\(.*\)}/\1/'`
185 if [ "$SHUTDOWN" = "poweroff" ]; then
186 log_action_msg "Powering off remaining VMs from user $i"
187 for v in $VMS; do
188 VBoxManage --nologo controlvm $v poweroff
189 wait=10
190 done
191 elif [ "$SHUTDOWN" = "acpibutton" ]; then
192 log_action_msg "Sending ACPI power button event to remaining VMs from user $i"
193 for v in $VMS; do
194 VBoxManage --nologo controlvm $v acpipowerbutton
195 wait=30
196 done
197 elif [ "$SHUTDOWN" = "savestate" ]; then
198 log_action_msg "Saving state of remaining VMs from user $i"
199 for v in $VMS; do
200 VBoxManage --nologo controlvm $v savestate
201 wait=30
202 done
203 fi
204 fi
205 fi
206 done
207
208 # wait for some seconds when doing ACPI shutdown
209 if [ "$wait" -ne 0 ]; then
210 log_action_begin_msg "Waiting for $wait seconds for VM shutdown"
211 sleep $wait
212 log_action_end_msg 0
213 fi
214 return 0
215}
216
217dmnstatus()
218{
219 if running vboxdrv && running vboxnetflt && running vboxnetadp && running vboxpci; then
220 echo "VirtualBox kernel modules are loaded."
221
222 for i in $SHUTDOWN_USERS; do
223 if [ -d /tmp/.vbox-$i-ipc ]; then
224 export VBOX_IPC_SOCKETID="$i"
225 VMS=`VBoxManage --nologo list runningvms 2>/dev/null`
226 if [ $? -eq 0 -a -n "$VMS" ]; then
227 VMS=`echo "$VMS" | sed -e 's/^".*".*{\(.*\)}/\1/'`
228 echo "The following VMs are currently running:"
229 for v in $VMS; do
230 echo " $v"
231 done
232 fi
233 fi
234 done
235 return 0
236 else
237 echo "VirtualBox kernel module is not loaded."
238 return 3
239 fi
240}
241
242case "$1" in
243start)
244 start
245 ;;
246stop)
247 stop_vms && stop
248 ;;
249stop_vms)
250 stop_vms
251 ;;
252restart|force-reload)
253 stop_vms && stop && start
254 ;;
255status)
256 dmnstatus
257 ;;
258*)
259 echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status}"
260 exit 1
261esac

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