-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual.subr
438 lines (361 loc) · 12.5 KB
/
virtual.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
if [ ! "$_CBSD_VIRTUAL_SUBR" ]; then
_CBSD_VIRTUAL_SUBR=1
###
check_iso_sha256sum()
{
local _sha256sum_new _sha256sum
local _file _sha256sum_passed
while getopts "f:m:" opt; do
case "$opt" in
f) _file="${OPTARG}" ;;
m) _sha256sum="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
if [ -n "${_sha256sum}" -a "${_sha256sum}" != "0" ]; then
printf "${MAGENTA}Checking checksum: ${GREEN}${_sha256sum}${MAGENTA}...${NORMAL}"
_sha256sum_new=$( /sbin/sha256 -q ${_file} )
if [ "${_sha256sum}" = "${_sha256sum_new}" ]; then
${ECHO} "${GREEN}Passed${NORMAL}"
_sha256sum_passed=1
else
${ECHO} "${MAGENTA}Invalid CRC: ${GREEN}${_sha256sum_new}${NORMAL}"
${ECHO} "${MAGENTA}See more details about CRC: ${GREEN}https://www.bsdstore.ru/en/cbsd_fetch.html${NORMAL}"
_sha256sum_passed=0
fi
else
${ECHO} "${LRED}Check CRC sum skipped due to settings. ${MAGENTA}See more: ${GREEN}https://www.bsdstore.ru/en/cbsd_fetch.html${NORMAL}"
_sha256sum_passed=1
fi
return ${_sha256sum_passed}
}
# generic/shared func for custom virtuals
# $sha256sum - optional - check for sha256sum
init_iso()
{
local sha256sum_new
local sha256sum_passed
local _sorted_mirror_list _sorted_mirror_list2
local _sorted_mirror_list_rate
local _fetch_attempt=0
local _mirror_num=0 _mirror_cur=0
readconf fetch.conf
if [ -z "${iso_img}" ]; then
${ECHO} "no cd: iso_img and register_iso_as is empty"
return 1
fi
# test for media database
local media_db
if [ "${CBSD_ISO_SKIP_CHECKSUM}" = "yes" -o "${sha256sum}" = "0" ]; then
sha256sum_passed=1
sha256sum=0
else
sha256sum_passed=0
fi
if [ -n "${vm_iso_path}" ]; then
${ECHO} "${MAGENTA}vm_iso_path: ${GREEN}${vm_iso_path}${NORMAL}"
media_db=$( cbsdsql storage_media SELECT path FROM media WHERE name=\"${vm_iso_path}\" AND path LIKE \"%${register_iso_name}%\" LIMIT 1 )
if [ -n "${media_db}" -a -r "${media_db}" ]; then
${ECHO} "${MAGENTA}media found: ${GREEN}${vm_iso_path}${MAGENTA} --> ${GREEN}${media_db}${NORMAL}"
iso_img="${media_db}"
return 0
fi
fi
local _mydir="${srcdir}/iso"
local _myfile="${_mydir}/${iso_img}"
local i
[ ! -d "${_mydir}" -o ! -h "${_mydir}" ] && /bin/mkdir -p ${_mydir}
[ -z "${iso_img_dist}" ] && iso_img_dist="${iso_img}"
# Unessesary when we boot not from cd
[ "${vm_boot}" != "cd" ] && iso_img="${_myfile}" && return 0
if [ -z "${register_iso_name}" ]; then
_myfile="${_mydir}/${iso_img}"
${ECHO} "${MAGENTA}Warning: empty register_iso_name. Use iso_img: ${_myfile}"
fi
_myfile=$( media mode=get name="${register_iso_name}" path='' )
[ -z "${_myfile}" ] && _myfile="${_mydir}/${register_iso_name}"
if [ -r "${_myfile}" ]; then
iso_img="${_myfile}"
return 0
fi
_mirror_num=$( echo "${iso_site} ${cbsd_iso_mirrors}" | /usr/bin/wc -w | /usr/bin/awk {'printf $1'} )
if [ ! -f "${_myfile}" -a "${fetch}" = "1" ]; then
[ -z "${iso_site}" -a -z "${cbsd_iso_mirrors}" ] && ${ECHO} "${MAGENTA}No such iso: ${GREEN}${_myfile}${MAGENTA} and ${GREEN}iso_site/cbsd_iso_mirrors${MAGENTA} variable not found" && return 1
${ECHO} "${MAGENTA}No such media: ${GREEN}${_myfile}${MAGENTA} in ${GREEN}${_mydir}${NORMAL}"
ALWAYS_YES=1
if [ "${iso_auto_fetch}" = "1" ]; then
ointer="${inter}"
inter=0
fi
if getyesno "Shall i download it from: ${iso_site}?"; then
${ECHO} "${MAGENTA}Download to: ${GREEN}${_myfile}${NORMAL}"
if [ -n "${iso_extract}" ]; then
outfile="${_mydir}/${iso_img_dist}"
else
outfile="${_myfile}"
fi
local win_mirror=
local win_rate=0
local t_size=0
local url=
if [ ${_mirror_num} -gt 1 ]; then
${ECHO} "${MAGENTA}Scanning for fastest mirror...${NORMAL}"
_sorted_mirror_list=$( for i in ${iso_site} ${cbsd_iso_mirrors}; do echo ${i}; done | /usr/bin/sort -u )
_sorted_mirror_list_rate=
# for broken_crc_fetch_order="cbsd_mirror"
_sorted_mirror_list2=$( for i in ${cbsd_iso_mirrors} ${iso_site}; do echo ${i}; done | /usr/bin/sort -u )
for i in ${_sorted_mirror_list}; do
_mirror_cur=$(( _mirror_cur + 1 ))
url="${i}${iso_img_dist}"
printf " ${WHITE}* ${LCYAN}[${_mirror_cur}/${_mirror_num}] ${GREEN}${i}${NORMAL}: "
t_size=$( /usr/bin/timeout -s HUP 3 ${bindir}/cfetch -s 1 -o /dev/null -u ${url} 2>/dev/null )
if [ -z "${t_size}" ]; then
echo "failed"
t_size=0
else
echo "${t_size}"
if ! is_number ${t_size}; then
if [ ${t_size} -gt ${win_rate} ]; then
win_rate=${t_size}
win_mirror="${i}"
fi
else
t_size=0
echo "failed, no size"
fi
fi
_sorted_mirror_list_rate="${_sorted_mirror_list_rate}${t_size} ${i}\n"
done
_sorted_mirror_list_rate=$( printf "${_sorted_mirror_list_rate}" |/usr/bin/sort -k1 -n -r )
_sorted_mirror_list=$( printf "${_sorted_mirror_list_rate}" |/usr/bin/awk {'printf $2" "'} )
# sorted list "<rate> <mirror>":
#echo "${_sorted_mirror_list_rate}"
[ -n "${win_mirror}" ] && ${ECHO} "${LYELLOW} Winner: ${GREEN}${win_mirror}${NORMAL}"
iso_site="${_sorted_mirror_list}"
else
# single site without mirrors
_sorted_mirror_list2=
fi
for i in ${iso_site}; do
sha256sum_passed=0
${ECHO} "${MAGENTA}Processing: ${GREEN}${i}${LGREEN}${iso_img_dist}${NORMAL}"
[ -s "${_myfile}" ] && continue # skip all other mirror when file exist
jswmode jname=${jname} mode=maintenance comment='Fetching_ISO'
fetchme -o ${outfile} -u ${i}${iso_img_dist}
jswmode jname=${jname} mode=master comment='Starting'
check_iso_sha256sum -f ${outfile} -m ${sha256sum}
sha256sum_passed=$?
[ -r ${outfile} -a ${sha256sum_passed} -eq 1 ] && break
[ -r ${outfile} ] && /bin/rm -f ${outfile}
_fetch_attempt=$(( _fetch_attempt + 1 ))
if [ "${broken_crc_fetch_order}" = "cbsd_mirror" -a ${_fetch_attempt} -eq 1 ]; then
# reset mirror list
for x in ${_sorted_mirror_list2}; do
${ECHO} "${MAGENTA}Processing from CBSD Mirrors: ${GREEN}${x}${NORMAL}"
fetchme -o ${outfile} -u ${x}${iso_img_dist}
check_iso_sha256sum -f ${outfile} -m ${sha256sum}
sha256sum_passed=$?
[ -r ${outfile} -a ${sha256sum_passed} -eq 1 ] && break
done
break
fi
done
if [ ${sha256sum_passed} -ne 1 ]; then
${ECHO} "${MAGENTA}SHA256 sum wrong. Please update profiles or use ${GREEN}CBSD_ISO_SKIP_CHECKSUM=yes${MAGENTA} variable"
${ECHO} "via env(1) or ${emulator}-default-default.conf config file to disable sha256sum validating${NORMAL}"
fi
fi
ALWAYS_YES=0
# restore inter value
if [ "{iso_auto_fetch}" = "1" ]; then
inter="${ointer}"
fi
fi
if [ -n "${iso_extract}" -a -f "${outfile}" ]; then
${ECHO} "${MAGENTA}Extracting...${NORMAL}"
set -o xtrace
cd ${_mydir}
${iso_extract} ${outfile}
set +o xtrace
[ -f "${outfile}" ] && /bin/rm -f ${outfile}
[ -f "${_mydir}/${iso_img}" ] && /bin/mv ${_mydir}/${iso_img} ${_myfile}
fi
if [ ! -s "${_myfile}" ]; then
${ECHO} "${MAGENTA}No such ISO image. Try to download it manually and put into ${GREEN}${_myfile}${NORMAL}"
err 1 "${MAGENTA}Or register (via cbsd media mode=register) it with vm_iso_path name: ${GREEN}${vm_iso_path}${NORMAL}"
fi
if [ -r "${_myfile}" ]; then
${ECHO} "${MAGENTA}Automatically register iso as: ${GREEN}${register_iso_name}${NORMAL}"
local reg_name
if [ -n "${register_iso_as}" ]; then
reg_name="${register_iso_as}"
else
reg_name="${register_iso_name}"
fi
media mode=register name="${reg_name}" path="${_myfile}" type="iso" jname="${jname}"
fi
# Final check for ISO existance
# if symlink - store realpath from symlink
[ -h ${_myfile} ] && _myfile=$( /usr/bin/readlink ${_myfile} )
if [ -r "${_myfile}" ]; then
iso_img="${_myfile}"
else
iso_img=
fi
return 0
}
check_for_empty_hdd()
{
local _err
local dsk_size=0
local tmp_zvol=
[ "${vm_boot}" != "hdd" ] && return 0
# check for empty disk
local checkres
if [ -h "${data}/${MDFILE}" ]; then
. ${zfstool}
if is_getzvol ${data}/${MDFILE}; then
dsk_size=$( /sbin/zfs get -Hp -o value refer ${is_zvol} 2>/dev/null )
else
checkres=$( /usr/bin/readlink ${data}/${MDFILE} )
[ -r ${checkres} ] && dsk_size=$( /usr/bin/du -sk ${checkres} 2>/dev/null |/usr/bin/awk '{printf $1}' )
fi
else
[ -r ${checkres} ] && dsk_size=$( /usr/bin/du -sk ${data}/${MDFILE} 2>/dev/null |/usr/bin/awk '{printf $1}' )
fi
[ -z "${dsk_size}" ] && err 1 "${MAGENTA}Unable to determine disk size for ${data}/${MDFILE}, empty result for dsk_size${NORMAL}"
if is_number ${dsk_size}; then
err 1 "${MAGENTA}Unable to determine disk size for ${data}/${MDFILE}, result: [${dsk_size}]${NORMAL}"
fi
if [ ${dsk_size} -lt 327680 ]; then
${ECHO} "${MAGENTA}Looks like ${GREEN}${data}/${MDFILE}${MAGENTA} is empty.${NORMAL}"
ALWAYS_YES=1
if getyesno "May be you want to boot from CD? "; then
${ECHO} "${MAGENTA}Temporary boot device: ${GREEN}cd${NORMAL}"
cd_boot_once=1
vm_boot="cd";
fi
ALWAYS_YES=0
else
# Eject CD/ISO ?
media_auto_eject=$( cbsdsql ${jailsysdir}/${jname}/local.sqlite 'SELECT media_auto_eject FROM settings ORDER BY (created) DESC LIMIT 1;' 2>/dev/null )
if [ "${media_auto_eject}" = "1" ]; then
media mode=eject jname=${jname}
cbsdsql ${jailsysdir}/${jname}/local.sqlite "UPDATE settings SET media_auto_eject='0'"
fi
fi
}
# print next free vnc/rdp port
# $1 - start port
get_next_rd_port()
{
local _start=5900
[ -n "${1}" ] && _start="$1"
for _rd_port in $( /usr/bin/seq ${_start} 6000 ); do
/usr/bin/nc -w 2 -z 127.0.0.1 ${_rd_port} > /dev/null 2>&1 || break
done
echo ${_rd_port}
}
# check 1 when DMAR detected
# check 0 if not
check_dmar()
{
local _dmar_test=$( /usr/sbin/acpidump -t | /usr/bin/grep DMAR )
[ -z "${_dmar_test}" ] && return 0
return 1
}
init_systap()
{
local _i _iface _desc _mytap _sys_uplink _ret
_iface=$( /sbin/ifconfig -l )
_mytap=
for _i in ${_iface}; do
_desc=$( /sbin/ifconfig ${_i} 2>/dev/null | /usr/bin/awk '/description:/{print $2}' )
[ "${_desc}" = "CBSDSYSTEM0" ] && return 0
done
. ${workdir}/initenv.subr
update_netinfo
if [ "${node_ip6_active}" = "1" ]; then
_sys_uplink="${CBSD_UPLINK_IFACE6}"
else
_sys_uplink="${CBSD_UPLINK_IFACE4}"
fi
# no uplink interface detected, skip system tap
[ -z "${_sys_uplink}" ] && return 0
_is_phys=$( ${toolsdir}/nic_info --nic=${_sys_uplink} --phys --quiet 2>/dev/null )
# if interface not physical, skipp init systap
[ "${_is_phys}" != "1" ] && return 0
. ${vimageconf}
if ! get_vm_uplink_interface ${_sys_uplink}; then
${ECHO} "${MAGENTA}bstart init_systap warning: error get_vm_uplink_interface ${_sys_uplink}${NORMAL}"
return 1
fi
if ! mybridge=$( get_my_device bridge ${interface} ); then
${ECHO} "${MAGENTA}bstart init_systap warning: errmsg=\"Error:get_my_device_bridge_for_${interface}: ${mybridge}\""
return 1
fi
_mytap=$( get_my_tap ${mybridge} )
_ret=$?
if [ ${_ret} -eq 1 ]; then
${ECHO} "${MAGENTA}bstart init_systap warning: errmsg=\"Error:get_my_tap_for_${mybridge}: ${mybridge}\""
return 1
fi
/sbin/ifconfig ${_mytap} description CBSDSYSTEM0
}
# export ${dsk_size},${dsk_bsize},${dsk_realsize} variable
# $dsk_size can in form: "100M/200G"
# $dsk_bsize - size in bytes
# require: dsk_path as "dsk1.vhd" or $1 as full path
# require . tools
populate_dsk_size()
{
local dsk_fullpath
local tmp_zvol _zvol_pref _is_zvol
if [ -n "${1}" ]; then
dsk_fullpath="${1}"
else
dsk_fullpath="${jaildatadir}/${jname}-${jaildatapref}/${dsk_path}"
fi
dsk_size=0
dsk_bsize=0
dsk_realsize=
if [ -f "${dsk_fullpath}" ]; then
dsk_realsize=$( /usr/bin/du -sh ${dsk_fullpath} | /usr/bin/awk '{printf $1}' 2>/dev/null )
dsk_bsize=$( /usr/bin/stat -f "%z" ${dsk_fullpath} 2>/dev/null )
elif [ -h "${dsk_fullpath}" ]; then
tmp_zvol=$( /usr/bin/readlink ${dsk_fullpath} 2>/dev/null )
_zvol_pref=$( substr --pos=0 --len=10 --str=${tmp_zvol} )
# not started with /dev/zvol ?
[ "${_zvol_pref}" != "/dev/zvol/" ] && return 0
_is_zvol=$( echo ${tmp_zvol} |/usr/bin/sed s:/dev/zvol/::g )
dsk_bsize=$( /sbin/zfs get -Hp -o value volsize ${_is_zvol} )
else
# no such file or symlink
return 0
fi
if conv2human "${dsk_bsize}"; then
dsk_size=${convval}
fi
if [ -z "${dsk_size}" ]; then
dsk_size=0
return 0
fi
[ -n "${dsk_realsize}" ] && dsk_size="${dsk_realsize}/${dsk_size}"
}
# print to stdout core count by topology
# $1 - ${_sockets}
# $2 - ${_cores}
# $3 ${_threads}
get_vm_cores_by_topology()
{
local _sockets _cores _threads _vm_cpus
_sockets="${1}"
_cores="${2}"
_threads="${3}"
[ -z "${_sockets}" -o -z "${_cores}" -o -z "${_threads}" ] && return 1
_vm_cpus=$(( _sockets * _cores * _threads ))
printf "${_vm_cpus}"
return 0
}
###
fi