-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcconf.subr
230 lines (201 loc) · 6.93 KB
/
rcconf.subr
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
#v10.0.4
# this is a temporary wrapper for version 9.2.2 is intended to make the transition to sqlite3 version if rc.conf more painless
# required:
# strings.subr
#
# load data from local bhyve settings
# $1 - jname
init_bhyve_rcconf()
{
local jname="${1}"
[ -z "${jname}" ] && return 0
local sqldelimer="|"
local A
A=$( cbsdsql ${jailsysdir}/${jname}/local.sqlite "SELECT astart,vm_cpus,vm_ram,vm_os_type,vm_boot,vm_os_profile,vm_vnc_port,virtio_type,bhyve_vnc_tcp_bind,bhyve_vnc_resolution,cd_vnc_wait,protected,hidden,maintenance,ip4_addr,vnc_password,vm_hostbridge,vm_iso_path,vm_console,vm_efi,bhyve_generate_acpi,bhyve_wire_memory,bhyve_rts_keeps_utc,bhyve_force_msi_irq,bhyve_x2apic_mode,bhyve_mptable_gen,bhyve_ignore_msr_acc,bhyve_vnc_vgaconf,vm_cpu_topology FROM settings ORDER BY (created) DESC LIMIT 1;" )
if [ -n "${A}" ]; then
OIFS="${IFS}"
IFS="|"
astart=
vm_cpus=
vm_ram=
vm_os_type=
vm_boot=
vm_os_profile=
vm_vnc_port=
virtio_type=
bhyve_vnc_tcp_bind=
bhyve_vnc_resolution=
cd_vnc_wait=
protected=
hidden=
maintenance=
ip4_addr=
vnc_password=
vm_hostbridge=
vm_iso_path=
vm_console=
vm_efi=
bhyve_generate_acpi=
bhyve_wire_memory=
bhyve_rts_keeps_utc=
bhyve_force_msi_irq=
bhyve_x2apic_mode=
bhyve_mptable_gen=
bhyve_ignore_msr_acc=
bhyve_vnc_vgaconf=
vm_cpu_topology=
sqllist "${A}" astart vm_cpus vm_ram vm_os_type vm_boot vm_os_profile vm_vnc_port virtio_type bhyve_vnc_tcp_bind bhyve_vnc_resolution cd_vnc_wait protected hidden maintenance ip4_addr vnc_password vm_hostbridge vm_iso_path vm_console vm_efi bhyve_generate_acpi bhyve_wire_memory bhyve_rts_keeps_utc bhyve_force_msi_irq bhyve_x2apic_mode bhyve_mptable_gen bhyve_ignore_msr_acc bhyve_vnc_vgaconf vm_cpu_topology
IFS="${OIFS}"
else
err 1 "${MAGENTA}Unable to fetch vm data from: ${GREEN}${jailsysdir}/${jname}/local.sqlite${NORMAL}"
fi
}
# load data from local XEN settings
# $jname
init_xen_rcconf()
{
local jname="${1}"
[ -z "${jname}" ] && return 0
local sqldelimer="|"
local A
A=$( cbsdsql ${jailsysdir}/${jname}/local.sqlite "SELECT vm_cpus,vm_ram,vm_os_type,vm_boot,vm_os_profile,vm_vnc_port,xen_vnc_tcp_bind,xen_vnc_resolution,cd_vnc_wait,protected,hidden,maintenance,ip4_addr,vnc_password FROM settings ORDER BY (created) DESC LIMIT 1;" )
if [ -n "${A}" ]; then
OIFS="${IFS}"
IFS="|"
vm_cpus=
vm_ram=
vm_os_type=
vm_boot=
vm_os_profile=
vm_vnc_port=
xen_vnc_tcp_bind=
xen_vnc_resolution=
cd_vnc_wait=
protected=
hidden=
maintenance=
ip4_addr=
vnc_password=
sqllist "${A}" vm_cpus vm_ram vm_os_type vm_boot vm_os_profile vm_vnc_port xen_vnc_tcp_bind xen_vnc_resolution cd_vnc_wait protected hidden maintenance ip4_addr vnc_password
IFS="${OIFS}"
else
err 1 "${MAGENTA}Unable to fetch vm data from: ${GREEN}${jailsysdir}/${jname}/local.sqlite${NORMAL}"
fi
}
# init_rcconf ( $jname must be not empty )
# ( $sqlfile - specify sqlfile in dbdir for searching data. If empty - local file is prefered )
init_rcconf()
{
local _JCOL _BCOL _A _ALLJCOL _ALLBCOL jname _sqlfile _datafile="local" i
[ -n "${2}" ] && _datafile="${2}"
_sqlfile="${dbdir}/${_datafile}.sqlite"
[ ! -f "${_sqlfile}" ] && return 0
. ${sharedir}/local-jails.schema
_JCOL="${MYCOL}"
#clean sqlite3 schema variables
for i in ${_JCOL}; do
unset ${i}
done
_ALLJCOL=$( echo ${_JCOL}| /usr/bin/tr " " "," )
# empty string on remote node if cbsd version is mistmatch ( diff. jail schema )
_A=$( 2>/dev/null env sqldelimer="|" ${miscdir}/sqlcli ${_sqlfile} "SELECT ${_ALLJCOL} FROM jails WHERE jname=\"$1\" " )
[ -z "${_A}" ] && return 1
. ${strings}
sqllist "${_A}" ${_JCOL}
case "${emulator}" in
qemu-arm-static|qemu-mips64-static)
local source_emulator="${emulator}"
;;
qemu-aarch64-static)
local source_emulator="${emulator}"
arch="arm64"
target_arch="aarch64"
;;
esac
#skip for not my emulator
if [ -n "${source_emulator}" ]; then
[ "${source_emulator}" != "${emulator}" ] && continue
fi
. ${strings}
sqllist "${_A}" ${_JCOL}
case "${emulator}" in
bhyve|virtualbox|xen)
. ${sharedir}/${emulator}.conf
_BCOL="${MYCOL}"
jid=-1
_ALLBCOL=$( echo ${_BCOL}|/usr/bin/tr " " "," )
_A=$( 2>/dev/null env sqldelimer="|" ${miscdir}/sqlcli ${_sqlfile} "SELECT ${_ALLBCOL} FROM ${emulator} WHERE jname=\"$1\"" )
sqllist "${_A}" ${_BCOL}
;;
esac
init_jail_path
# sanity check that jid and status are similar to the real state, for localhost only
if [ "${_datafile}" = "local" ]; then
get_jid
case "${emulator}" in
"bhyve")
if [ -e "/dev/vmm/${jname}" ]; then
[ ${status} -ne 3 ] && status="1" # if not in maintenance, update status to online
myjid=$( cbsdsql local SELECT jid FROM jails WHERE jname=\"${jname}\" 2>/dev/null )
# temp work-around for CBSD 11.1.0 wherei PID was introduced and where old machine
# still run with 0 jid: change jid to non zero to proper status
[ -z "${myjid}" -o "${myjid}" = "0" ] && myjid=1
jid="${myjid}"
fi
;;
"virtualbox")
. ${workdir}/virtualbox.subr
_state=$( ${VBOX_MGMT_CMD} showvminfo ${jname}| /usr/bin/awk '/^State: /{print $2}' )
if [ "${_state}" = "running" ]; then
[ ${status} -ne 3 ] && status="1" # if not in maintenance, update status to online
myjid="1"
jid="1"
else
[ ${status} -ne 3 ] && status="0" # if not in maintenance, update status to offline
myjid="0"
jid="0"
fi
;;
"xen")
. ${workdir}/xen.subr
_state=$( ${XL_CMD} list ${jname} > /dev/null 2>&1 )
if [ $? -eq 0 ]; then
[ ${status} -ne 3 ] && status="1" # if not in maintenance, update status to online
myjid="1"
jid="1"
else
[ ${status} -ne 3 ] && status="0" # if not in maintenance, update status to offline
myjid="0"
jid="0"
fi
;;
esac
# check jid when sqlite information is wrong and update it
[ ${myjid} -ne ${jid} ] && jid=${myjid} && ${miscdir}/sqlcli ${dbdir}/local.sqlite "UPDATE jails SET jid=\"${myjid}\" WHERE jname=\"${1}\""
# fix for status only for 0,1, not for slave or maintenace
if [ ${status} -eq 0 -o ${status} -eq 1 ]; then
if [ ${myjid} -ne 0 -a ${status} -eq 0 ]; then
status=1
${miscdir}/sqlcli ${dbdir}/local.sqlite "UPDATE jails SET status=\"1\" WHERE jname=\"${1}\""
fi
if [ ${myjid} -eq 0 -a ${status} -ne 0 -a ${status} -ne 2 ]; then
status=0
${miscdir}/sqlcli ${dbdir}/local.sqlite "UPDATE jails SET status=\"0\" WHERE jname=\"${1}\""
fi
fi
fi
# clean some variables if equal 0
[ "${basename}" = "0" ] && unset basename
[ "${slavenode}" = "0" ] && unset slavenode
[ "${masterhost}" = "0" ] && unset masterhost
[ "${exec_stop}" = "0" ] && unset ${exec_stop}
[ "${exec_poststart}" = "0" ] && unset ${exec_poststart}
[ "${exec_poststop}" = "0" ] && unset ${exec_poststop}
[ "${exec_prestart}" = "0" ] && unset ${exec_prestart}
[ "${exec_prestop}" = "0" ] && unset ${exec_prestop}
case "${emulator}" in
bhyve|xen)
init_${emulator}_rcconf ${1}
esac
}
init_rcconf ${jname} ${sqlfile}