-
Notifications
You must be signed in to change notification settings - Fork 7
/
vboxcontrol
executable file
·225 lines (195 loc) · 5.79 KB
/
vboxcontrol
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
#! /bin/bash
### BEGIN INIT INFO
# Provides: virtualbox_vms
# Required-Start: $local_fs $syslog $remote_fs
# Required-Stop: $local_fs $syslog $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Control VirtualBox Virtual Machine instances
### END INIT INFO
#
# Version 2008051100 by Jochem Kossen <[email protected]>
# http://farfewertoes.com
#
# Released in the public domain
#
# This file came with a README file containing the instructions on how
# to use this script.
#
#testado no ubuntu server 12.04 64 bits
#por Flávio Oliveira
#https://github.com/oliveiradeflavio
#
. /lib/lsb/init-functions
# Are we running from init?
run_by_init() {
([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
}
################################################################################
# INITIAL CONFIGURATION
VBOXDIR="/etc/virtualbox"
VM_USER="root"
USE_NAT="yes"
export PATH="${PATH:+$PATH:}/bin:/usr/bin:/usr/sbin:/sbin"
if [ -f $VBOXDIR/config ]; then
. $VBOXDIR/config
else
echo "ERROR: $VBOXDIR/config does not exist. Exiting."
exit 1
fi
SU="su $VM_USER -c"
VBOXMANAGE="VBoxManage -nologo"
################################################################################
# FUNCTIONS
# Determine if USE_NAT is set to "yes"
use_nat() {
if [ "$USE_NAT" = "yes" ]; then
return `true`
else
return `false`
fi
}
# Bring up the bridge interface
enable_bridge() {
# If NAT is enabled, don't do anything
use_nat && return
# Load the tun module
if [ ! -e /dev/net/tun ]; then
modprobe tun
fi
brctl addbr br0 || /bin/true
# Disable $HOST_IF; host will use br0 instead
ifdown $HOST_IF
ifconfig $HOST_IF 0.0.0.0 promisc
brctl addif br0 $HOST_IF
# Bring up br0
ifup br0
# Answer ARP requests for $HOST_IP (which now come on br0) with the MAC
# address of $HOST_IF
arp -Ds $HOST_IP $HOST_IF pub
}
# Bring down the bridge interface
disable_bridge() {
# If NAT is enabled, don't do anything
use_nat && return
ifdown br0
brctl delbr br0
ifup $HOST_IF
}
# Activate tap interfaces
enable_taps() {
# If NAT is enabled, don't do anything
use_nat && return
for TAP in $TAPS; do
# Check if $TAP is configured already
ifconfig $TAP > /dev/null 2>&1
if [ $? != 0 ]; then
tunctl -t $TAP -u $VM_USER
brctl addif br0 $TAP
# Disable tap interfaces for host; guest will activate them for themselves
ifconfig $TAP 0.0.0.0 promisc
# Enable proxy_arp so that ARP requests can be answered correctly
# by the host
echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp
# Add a route for the tap device
route add -host $HOST_IP dev $TAP
else
log_failure_msg "Interface $TAP already configured"
fi
done
}
# Disable/deconfigure tap interfaces
disable_taps() {
# If NAT is enabled, don't do anything
use_nat && return
for TAP in $TAPS; do
route del -host $HOST_IP dev $TAP
brctl delif br0 $TAP
tunctl -d $TAP
done
}
# Check for running machines every few seconds; return when all machines are
# down
wait_for_closing_machines() {
RUNNING_MACHINES=`$SU "$VBOXMANAGE list runningvms" | wc -l`
if [ $RUNNING_MACHINES != 0 ]; then
sleep 5
wait_for_closing_machines
fi
}
################################################################################
# RUN
case "$1" in
start)
if [ -f /etc/virtualbox/machines_enabled ]; then
if [ ! `use_nat` ]; then
################## ###
# enable_bridge
# enable_taps
##################
chown root.vboxusers /dev/net/tun
chmod 0660 /dev/net/tun
fi
cat /etc/virtualbox/machines_enabled | while read VM; do
log_action_msg "Starting VM: $VM ..."
$SU "$VBOXMANAGE startvm \"$VM\" -type vrdp"
done
fi
;;
stop)
# NOTE: this stops all running VM's. Not just the ones listed in the
# config
search="\""
$SU "$VBOXMANAGE list runningvms" | while read VM; do
vmNameAux=${VM:1}
endVMName=`expr index "$vmNameAux" $search`-1
vmName=${vmNameAux:0:endVMName}
log_action_msg "Shutting down VM: $vmName - $VM ..."
$SU "$VBOXMANAGE controlvm $vmName savestate"
echo $SU "$VBOXMANAGE controlvm $vmName acpipowerbutton"
done
wait_for_closing_machines
################# ###
#if [ ! `use_nat` ]; then
# disable_taps
# disable_bridge
#fi
#################
;;
bridge-up)
enable_bridge
;;
bridge-down)
disable_bridge
;;
taps-up)
enable_taps
;;
taps-down)
disable_taps
;;
start-vm)
log_action_msg "Starting VM: $2 ..."
$SU "$VBOXMANAGE startvm \"$2\" -type vrdp"
;;
stop-vm)
log_action_msg "Stopping VM: $2 ..."
$SU "$VBOXMANAGE controlvm \"$2\" acpipowerbutton"
;;
poweroff-vm)
log_action_msg "Powering off VM: $2 ..."
$SU "$VBOXMANAGE controlvm \"$2\" poweroff"
;;
status)
log_action_msg "The following virtual machines are currently running:"
$SU "$VBOXMANAGE list runningvms" | while read VM; do
echo -n "$VM ("
echo -n `$SU "VBoxManage showvminfo $VM|grep Name:|sed -e 's/^Name:\s*//g'"`
echo ')'
done
;;
*)
log_failure_msg "Usage: $0 {start|stop|status|start-vm <VM name>|stop-vm <VM name>|poweroff-vm <VM name>|bridge-up|bridge-down|taps-up|taps-down}"
exit 3
esac
exit 0