summaryrefslogtreecommitdiffstats
path: root/debian/virtualbox.init
blob: 1136b86660129c862c3ba66fb867e50360909b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#! /bin/sh
# Sun VirtualBox
# Linux kernel module init script

#
# Copyright (C) 2006-2009 Sun Microsystems, Inc.
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License (GPL) as published by the Free Software
# Foundation, in version 2 as it comes in the "COPYING" file of the
# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
#

# chkconfig: 35 30 70
# description: VirtualBox Linux kernel module
#
### BEGIN INIT INFO
# Provides:       virtualbox
# Required-Start: $remote_fs $network
# Required-Stop:  $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: VirtualBox Linux kernel module
### END INIT INFO

. /lib/lsb/init-functions

test -d /usr/share/doc/virtualbox -a -x /usr/bin/VBoxHeadless || exit 0

# Include virtualbox defaults if available
[ -r /etc/default/virtualbox ] && . /etc/default/virtualbox

# set list of all active users if asked to do so
if [ "$SHUTDOWN_USERS" = "all" ]; then
	SHUTDOWN_USERS=""
	for i in /tmp/.vbox-*-ipc; do
		SHUTDOWN_USERS="$SHUTDOWN_USERS $(echo $i|cut -d'-' -f2)"
	done
fi

if [ "$LOAD_VBOXDRV_MODULE" != 1 ]; then
	log_success_msg "virtualbox disabled; edit /etc/default/virtualbox"
	exit 0
fi

running()
{
    lsmod | grep -q "$1[^_-]"
}

start()
{
	log_begin_msg "Loading VirtualBox kernel modules..."
	
	if ! running vboxdrv; then
		if ! modprobe vboxdrv > /dev/null 2>&1; then
			if ! find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
				log_failure_msg "No suitable module for running kernel found"
			else
				log_failure_msg "modprobe vboxdrv failed. Please use 'dmesg' to find out why"
			fi
			log_end_msg 1
			return 1
		else
			log_progress_msg "vboxdrv"
		fi
	fi
	
	if ! running vboxnetflt; then
		if ! modprobe vboxnetflt > /dev/null 2>&1; then
			if ! find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|grep -q vboxnetflt; then
				log_failure_msg "No suitable vboxnetflt module for running kernel found"
			else
				log_failure_msg "modprobe vboxnetflt failed. Please use 'dmesg' to find out why"
			fi
			log_end_msg 1
			return 1
		else
			log_progress_msg "vboxnetflt"
		fi
	fi
	
	if ! running vboxnetadp; then
		if ! modprobe vboxnetadp > /dev/null 2>&1; then
			if ! find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|grep -q vboxnetadp; then
				log_failure_msg "No suitable vboxnetadp module for running kernel found"
			else
				log_failure_msg "modprobe vboxnetadp failed. Please use 'dmesg' to find out why"
			fi
			log_end_msg 1
			return 1
		else
			log_progress_msg "vboxnetadp"
		fi
	fi
	
	log_end_msg 0
}

stop()
{
	log_begin_msg "Unloading VirtualBox kernel modules..."
	
	if running vboxnetadp; then
		if ! rmmod vboxnetadp 2>/dev/null; then
			log_failure_msg "Cannot unload module vboxnetadp"
			log_end_msg 1
			return 1
		else
			log_progress_msg "vboxnetadp"
		fi
	fi
	
	if running vboxnetflt; then
		if ! rmmod vboxnetflt 2>/dev/null; then
			log_failure_msg "Cannot unload module vboxnetflt"
			log_end_msg 1
			return 1
		else
			log_progress_msg "vboxnetflt"
		fi
	fi
	
	if running vboxpci; then
		if ! rmmod vboxpci 2>/dev/null; then
			log_failure_msg "Cannot unload module vboxpci"
			log_end_msg 1
			return 1
		else
			log_progress_msg "vboxpci"
		fi
	fi
	
	if running vboxdrv; then
		if ! rmmod vboxdrv 2>/dev/null; then
			log_failure_msg "Cannot unload module vboxdrv"
			log_end_msg 1
			return 1
		else
			log_progress_msg "vboxdrv"
		fi
	fi
	
	log_end_msg 0
}

# enter the following variables in /etc/default/virtualbox:
#   SHUTDOWN_USERS="foo bar"  
#     check for running VMs of user foo and user bar
#   SHUTDOWN=poweroff
#   SHUTDOWN=acpibutton
#   SHUTDOWN=savestate
#     select one of these shutdown methods for running VMs
stop_vms()
{
	if ! pidof VBoxSVC > /dev/null; then
		return 0
	fi
	
	wait=0
	
	for i in $SHUTDOWN_USERS; do
		if [ -d /tmp/.vbox-$i-ipc ]; then
			export VBOX_IPC_SOCKETID="$i"
			VMS=`VBoxManage --nologo list runningvms 2>/dev/null`
			if [ $? -eq 0 -a -n "$VMS" ]; then
				VMS=`echo "$VMS" | sed -e 's/^".*".*{\(.*\)}/\1/'`
				if [ "$SHUTDOWN" = "poweroff" ]; then
					log_action_msg "Powering off remaining VMs from user $i"
					for v in $VMS; do
						VBoxManage --nologo controlvm $v poweroff
						wait=10
					done
				elif [ "$SHUTDOWN" = "acpibutton" ]; then
					log_action_msg "Sending ACPI power button event to remaining VMs from user $i"
					for v in $VMS; do
						VBoxManage --nologo controlvm $v acpipowerbutton
						wait=30
					done
				elif [ "$SHUTDOWN" = "savestate" ]; then
					log_action_msg "Saving state of remaining VMs from user $i"
					for v in $VMS; do
						VBoxManage --nologo controlvm $v savestate
						wait=30
					done
				fi
			fi
		fi
	done
	
	# wait for some seconds when doing ACPI shutdown
	if [ "$wait" -ne 0 ]; then
		log_action_begin_msg "Waiting for $wait seconds for VM shutdown"
		sleep $wait
		log_action_end_msg 0
	fi
	return 0
}

dmnstatus()
{
	if running vboxdrv && running vboxnetflt && running vboxnetadp && running vboxpci; then
		echo "VirtualBox kernel modules are loaded."
		
		for i in $SHUTDOWN_USERS; do
			if [ -d /tmp/.vbox-$i-ipc ]; then
				export VBOX_IPC_SOCKETID="$i"
				VMS=`VBoxManage --nologo list runningvms 2>/dev/null`
				if [ $? -eq 0 -a -n "$VMS" ]; then
					VMS=`echo "$VMS" | sed -e 's/^".*".*{\(.*\)}/\1/'`
					echo "The following VMs are currently running:"
					for v in $VMS; do
						echo "  $v"
					done
				fi
			fi
		done
		return 0
	else
		echo "VirtualBox kernel module is not loaded."
		return 3
	fi
}

case "$1" in
start)
	start
	;;
stop)
	stop_vms && stop
	;;
stop_vms)
	stop_vms
	;;
restart|force-reload)
	stop_vms && stop && start
	;;
status)
	dmnstatus
	;;
*)
	echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status}"
	exit 1
esac