/
vboxheadless
236 lines (202 loc) · 5.9 KB
/
vboxheadless
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
#! /bin/sh
# REQUIRE: LOGIN vboxnet
# PROVIDE: vboxheadless
# KEYWORD: nojail shutdown
#
# vboxheadless Startup script for VirtualBox Virtual Machines
#
# description: Manages VirtualBox VMs
# processname: vboxheadless
#
#
# Version 20101023 by Packy Anderson based on:
# Version 20090301 by Kevin Swanson <kswan.info> based on:
# Version 2008051100 by Jochem Kossen <jochem.kossen@gmail.com>
# http://farfewertoes.com
#
# Released in the public domain
#
# Source function library.
. /etc/rc.subr
###############################################################################
# rc.d configuration
name="vboxheadless"
rcvar=`set_rcvar`
pidfile="/var/run/vboxheadless/vboxheadless.pid"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
vmstart_cmd="${name}_vmstart"
vmstop_cmd="${name}_vmstop"
vmpower_cmd="${name}_vmpower"
status_cmd="${name}_status"
extra_commands="vmstart vmstop vmpower status"
load_rc_config $name
eval "${rcvar}=\${${rcvar}:-'NO'}"
###############################################################################
# internal variables
VBOXDIR="/etc/virtualbox"
VM_USER="vbox"
export PATH="${PATH:+$PATH:}/bin:/usr/bin:/usr/sbin:/sbin"
SU="su $VM_USER -c"
VBOXMANAGE="VBoxManage -nologo"
VBOXHEADLESS="VBoxHeadless"
AVAHI="avahi-daemon"
NOHUP="nohup"
###############################################################################
ts() { # timestamp format
date "+%Y-%m-%d %H:%M:%S - "
}
###############################################################################
logfile() {
local VM
VM=$1
echo "/var/log/vbox.$VM.log"
}
###############################################################################
do_log() {
local VM
local MSG
VM=$1
MSG=$2
LOG=$(logfile "$VM")
echo "$MSG"
echo -n $(ts) >> $LOG
echo " $MSG" >> $LOG
chown $VM_USER $LOG
}
###############################################################################
# if there's a vnc port for the VM defined in the vboxheadless_vnc_ports
# variable, return the port number. otherwise, return nothing
vboxheadless_getport() {
local VM
VM=$1
PORT=$(perl -e '
my($vm, $portlist) = @ARGV;
my %ports = split /\s+/, $portlist;
if (exists $ports{$vm}) {
print $ports{$vm};
}
' "$VM" "$vboxheadless_vnc_ports")
echo $PORT
}
vboxheadless_avahi_file() {
local VM
VM=$1
echo "/usr/local/etc/avahi/services/rfb-$VM.service"
}
vboxheadless_register_avahi() {
local VM
VM=$1
if checkyesno avahi_daemon_enable ; then
PORT=$(vboxheadless_getport "$VM")
FILE=$(vboxheadless_avahi_file "$VM")
do_log "$VM" "Registering vnc port $PORT for $VM with Avahi"
cat <<SERVICE >$FILE
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="no">$VM</name>
<service>
<type>_rfb._tcp</type>
<port>$PORT</port>
</service>
</service-group>
SERVICE
$AVAHI --reload
fi
}
vboxheadless_deregister_avahi() {
local VM
VM=$1
if checkyesno avahi_daemon_enable ; then
FILE=$(vboxheadless_avahi_file "$VM")
if [ -f $FILE ] ; then
PORT=$(vboxheadless_getport "$VM")
do_log "$VM" "Deregistering vnc port $PORT for $VM with Avahi"
rm -f $FILE
$AVAHI --reload
fi
fi
}
###############################################################################
vboxheadless_vmstart() {
local VM
VM=$1
PORT=$(vboxheadless_getport "$VM")
LOG=$(logfile "$VM")
OUTPUT=">>$LOG 2>&1"
if [ "$PORT" != "" ]; then
do_log "$VM" "Starting VM: $VM (vnc port $PORT)..."
$SU "$NOHUP $VBOXHEADLESS --vnc --vncport $PORT --startvm \"$VM\" $OUTPUT &"
vboxheadless_register_avahi "$VM"
else
echo "Starting VM: $VM (no vnc)..."
$SU "$NOHUP $VBOXHEADLESS --startvm \"$VM\" $OUTPUT &"
fi
}
###############################################################################
vboxheadless_vmstop() {
local VM
VM=$1
do_log "$VM" "Stopping VM: $VM ..."
$SU "$VBOXMANAGE controlvm \"$VM\" acpipowerbutton"
vboxheadless_deregister_avahi "$VM"
}
###############################################################################
vboxheadless_vmpower() {
local VM
VM=$1
do_log "$VM" "Powering off VM: $VM ..."
$SU "$VBOXMANAGE controlvm \"$VM\" poweroff"
}
###############################################################################
vboxheadless_start() {
if [ "$vboxheadless_vms" != "" ]; then
for VM in $vboxheadless_vms; do
vboxheadless_vmstart "$VM"
done
fi
}
###############################################################################
vboxheadless_running_vmlist() {
$SU "$VBOXMANAGE list runningvms" | perl -ne 's/\s+\{.*\}//; s/"//g; print "$_";'
}
###############################################################################
vboxheadless_stop() {
# NOTE: this stops all running VM's. Not just the ones listed in
# vboxheadless_vms
vboxheadless_running_vmlist | while read VM; do
vboxheadless_vmstop "$VM"
done
RUNNING_MACHINES=`vboxheadless_running_vmlist | wc -l`
while [ $RUNNING_MACHINES != 0 ]; do
sleep 5
RUNNING_MACHINES=`vboxheadless_running_vmlist | wc -l`
done
}
###############################################################################
vboxheadless_status() {
echo "The following virtual machines are currently running:"
vboxheadless_running_vmlist |\
while read UUID; do
$SU "$VBOXMANAGE showvminfo ${UUID%% *}"|PORTS="$vboxheadless_vnc_ports" \
perl -e '
my($name,$state,$uuid,$guest);
while (my $line = <>) {
($name) = $line =~ /^Name:\s*(.+)$/ unless ($name);
($guest) = $line =~ /^Guest OS:\s*(.+)$/ unless ($guest);
($state) = $line =~ /^State:\s*(.+)$/ unless ($state);
($uuid) = $line =~ /^UUID:\s*(.+)$/ unless ($uuid);
}
print " $name, $guest (uuid $uuid)\n";
print " $state\n";
my %ports = split /\s+/, $ENV{PORTS};
if (exists $ports{$name}) {
print " vnc port $ports{$name}\n";
}
'
done
}
###############################################################################
load_rc_config $name
run_rc_command "$@"