blob: 5ddc5fc21d6c01203f48bc6ff2884cdefe2c9cbd (
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
|
#!/bin/sh
# PROVIDE: vboxheadless
# REQUIRE: LOGIN vboxnet
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf[.local] to enable vboxheadless
#
# vboxheadless_enable (bool): Set to "NO" by default.
# Set it to "YES" to enable vboxheadless.
# vboxheadless_machines (str): Space separated list of machines
# vboxheadless_user (str): Default user account to run with.
# (default: %%VBOXUSER%%)
# vboxheadless_stop (str): Default stop cmd for VBoxManage controlvm.
# (default: savestate)
# vboxheadless_delay (int): Default startup/shutdown delay in seconds.
# (default: 0)
# vboxheadless_<machine>_name (str): Virtualbox machine name or UUID.
# vboxheadless_<machine>_user (str): User account to run with.
# vboxheadless_<machine>_flags (str): Additional flags for VBoxHeadless.
# vboxheadless_<machine>_stop (str): Stop command for VBoxManage controlvm.
# vboxheadless_<machine>_delay (int): Startup and shutdown delay in seconds.
. /etc/rc.subr
name="vboxheadless"
rcvar=vboxheadless_enable
rc_fast="YES"
command="%%VBOXDIR%%/VBoxHeadless"
pidbase="/var/run/${name}"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"
vboxheadless_start()
{
local machine mpidfile pid vmname vmuser vmflags vmdelay
echo "Starting Virtual Machines:"
for machine in ${vboxheadless_machines}; do
mpidfile="${pidbase}_${machine}.pid"
pid=$(check_pidfile $mpidfile $command)
eval vmname="\${vboxheadless_${machine}_name:-${machine}}"
eval vmuser="\${vboxheadless_${machine}_user:-${vboxheadless_user}}"
eval vmflags="\${vboxheadless_${machine}_flags:-}"
eval vmdelay="\${vboxheadless_${machine}_delay:-${vboxheadless_delay}}"
HOME=$(/usr/sbin/pw usershow -7 -n "${vmuser}" | /usr/bin/cut -d: -f6)
/usr/bin/printf "%25s " "${vmname}"
/usr/bin/su ${vmuser} -c "%%VBOXDIR%%/VBoxManage showvminfo '${vmname}' >/dev/null" 2>/dev/null
if [ $? != 0 ]; then
echo "Unknown machine"
continue
fi
if [ -n "${pid}" ]; then
echo "Already running? (pid=${pid})"
continue
fi
/bin/sleep ${vmdelay}
/usr/bin/install -o ${vmuser} -g %%VBOXGROUP%% -m 644 /dev/null ${mpidfile}
/usr/sbin/daemon -f -p ${mpidfile} -u ${vmuser} ${command} --startvm "${vmname}" ${vmflags}
echo "Started"
done
}
vboxheadless_stop()
{
local machine mpidfile pid pids vmname vmuser vmstop vmdelay
echo "Saving states for Virtual Machines:"
for machine in ${vboxheadless_machines}; do
mpidfile="${pidbase}_${machine}.pid"
pid=$(check_pidfile $mpidfile $command)
eval vmname="\${vboxheadless_${machine}_name:-${machine}}"
eval vmuser="\${vboxheadless_${machine}_user:-${vboxheadless_user}}"
eval vmstop="\${vboxheadless_${machine}_stop:-${vboxheadless_stop}}"
eval vmdelay="\${vboxheadless_${machine}_delay:-${vboxheadless_delay}}"
/usr/bin/printf "%25s " "${vmname}"
if [ -n "${pid}" ]; then
pids="${pids} ${pid}"
/bin/sleep ${vmdelay}
/usr/bin/su ${vmuser} -c "%%VBOXDIR%%/VBoxManage controlvm '${vmname}' ${vmstop} >/dev/null &" 2>/dev/null
fi
done
if [ -n "${pids}" ]; then
wait_for_pids $pids >/dev/null
echo "Stopped"
else
echo "Not running?"
fi
}
vboxheadless_status()
{
local machine mpidfile pid vmname vmuser
/usr/bin/printf "%25s %s\n" "Machine" "Status"
/usr/bin/printf "%25s %s\n" "-------------------------" "------------"
for machine in ${vboxheadless_machines}; do
mpidfile="${pidbase}_${machine}.pid"
pid=$(check_pidfile $mpidfile $command)
eval vmname="\${vboxheadless_${machine}_name:-${machine}}"
eval vmuser="\${vboxheadless_${machine}_user:-${vboxheadless_user}}"
/usr/bin/su ${vmuser} -c "%%VBOXDIR%%/VBoxManage showvminfo '${vmname}' >/dev/null" 2>/dev/null
if [ $? != 0 ]; then
/usr/bin/printf "%20s %s\n" "${vmname}" "Unknown Machine"
elif [ -n "${pid}" ]; then
/usr/bin/printf "%25s %s\n" "${vmname}" "Running"
else
/usr/bin/printf "%25s %s\n" "${vmname}" "Powered Off"
fi
done
}
load_rc_config $name
: ${vboxheadless_enable="NO"}
: ${vboxheadless_user="%%VBOXUSER%%"}
: ${vboxheadless_stop="savestate"}
: ${vboxheadless_delay="0"}
cmd_arg="$1" ; shift
if [ -n "$*" ]; then
vboxheadless_machines="$*"
fi
run_rc_command "${cmd_arg}"
|