-
Notifications
You must be signed in to change notification settings - Fork 0
/
bhyve.subr
1709 lines (1426 loc) · 50.9 KB
/
bhyve.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if [ ! "$_CBSD_BHYVE_SUBR" ]; then
_CBSD_BHYVE_SUBR=1
###
# generic function for bhyve
# required:
# strings.subr
#
. ${distdir}/virtual.subr
# test environment for bhyveing
init_bhyve()
{
local _i _res
[ "${skip_bhyve_check_env}" = "1" ] && return 0 # all fine!
if [ ${freebsdhostversion} -lt 1201499 ]; then
local _required_kmods="vmm if_tap if_bridge nmdm"
else
local _required_kmods="vmm if_tuntap if_bridge nmdm"
fi
if [ "${skip_bhyve_init_warning}" != "1" ]; then
_res=$( ${SU_CMD} -m cbsd -c ${miscdir}/popcnttest > /dev/null 2>&1 )
if [ $? -ne 0 ]; then
# work-around for Alex (CBSD Telegram) issue: this is not clean check for EPT existance:
# sometimes errcode = 16 but POPCNT feature present in CPU
# So, just print warning about this. Need for deep investigation
${ECHO} "${N1_COLOR}Warning: It seems like your CPU does not support ${N2_COLOR}EPT${N0_COLOR}"
${ECHO} "${N1_COLOR}But I will try to run bhyve anyway...${N0_COLOR}"
echo
${ECHO} "${N1_COLOR}To disable this warning, please set in ${workdir}/etc/bhyve-default-default.conf: ${N2_COLOR}skip_bhyve_init_warning${N1_COLOR} to: ${N2_COLOR}1${N0_COLOR}"
${ECHO} "${N1_COLOR}Pause for 10 seconds${N0_COLOR}"
sleep 10
fi
fi
if [ "${skip_bhyve_init_virtual_warning}" != "1" ]; then
if [ "${is_virtual}" != "physical" ]; then
${ECHO} "${N1_COLOR}Warning: Your current environment is not physical. This is a requirement for a working bhyve${N0_COLOR}"
${ECHO} "${N1_COLOR}Virtual engine detected: ${N2_COLOR}${is_virtual}${N0_COLOR}"
${ECHO} "${N1_COLOR}Please refer to this page for more information: ${N2_COLOR}https://wiki.freebsd.org/bhyve${N0_COLOR}"
${ECHO} "${N1_COLOR}Try to run bhyve anyway...${N0_COLOR}"
${ECHO} "${N1_COLOR}To disable this warning, please set in ${workdir}/etc/bhyve-default-default.conf: ${N2_COLOR}skip_bhyve_init_virtual_warning${N1_COLOR} to: ${N2_COLOR}1${N0_COLOR}"
${ECHO} "${N1_COLOR}Pause for 10 seconds${N0_COLOR}"
sleep 10
fi
fi
for _i in ${_required_kmods}; do
if ! ${KLDSTAT_CMD} -qm ${_i} >/dev/null 2>&1; then
${ECHO} "${N1_COLOR}No kldloaded module: ${N2_COLOR}${_i}${N0_COLOR}"
${ECHO} "${N1_COLOR}Please add ${N2_COLOR}vmm_load=\"YES\"${N1_COLOR} to /boot/loader.conf and${N1_COLOR}"
${ECHO} "${N1_COLOR}put ${N2_COLOR}kld_list=\"${_required_kmods}\"${N1_COLOR} into your ${N2_COLOR}/etc/rc.conf${N1_COLOR} then reboot the host.${N0_COLOR}"
[ -n "${DIALOG}" ] && ${ECHO} "${N1_COLOR}Press any key...${N0_COLOR}" && read p
if [ "${mod_cbsd_queue_enabled}" = "YES" -a -z "${MOD_CBSD_QUEUE_DISABLED}" ]; then
readconf cbsd_queue.conf
if [ -z "${cbsd_queue_backend}" ]; then
MOD_CBSD_QUEUE_DISABLED="1"
else
[ -n "${cbsd_bhyve_queue_name}" ] && ${cbsd_queue_backend} cbsd_queue_name=${cbsd_bhyve_queue_name} id=${jname} cmd=bstart status=2 data_status=1
fi
fi
exit 1
fi
done
TMUX_CMD=$( which tmux )
[ -z "${TMUX_CMD}" -o ! -x "${TMUX_CMD}" ] && err 1 "${N1_COLOR}The current version requires ${N2_COLOR}tmux${N1_COLOR}\nPlease run ${N2_COLOR}pkg install tmux ${N1_COLOR} or ${N2_COLOR}make -C /usr/ports/sysutils/tmux install${N1_COLOR} it.${N0_COLOR}"
tmuxcmd="${TMUX_CMD} -Lcbsd-${jname}"
return 0
}
# return 0 or 1 if $1 bus id exist in global $bhyve_pci_id_busy_list
# if is_bhyve_pci_id_busy 5; then
# echo "EXIST"
# fi
#
# if ! is_bhyve_pci_id_busy 5; then
# echo "NOT EXIST"
# fi
is_bhyve_pci_id_busy()
{
local _id="${1}"
local _i
local _buf_file="${jailsysdir}/${jname}/bhyve_pciid"
# use $_buf_file as an intermediate buffer for share
# and exchange $bhyve_pci_id_busy_list vars
[ -r ${_buf_file} ] && . ${_buf_file}
# free
[ -z "${bhyve_pci_id_busy_list}" ] && return 1
for _i in ${bhyve_pci_id_busy_list}; do
[ ${_i} -eq ${_id} ] && return 0 # already exist
done
# free
return 1
}
# add new $1 bus id in $bhyve_pci_id_busy_list array
add_bhyve_pci_id_busy()
{
local _id="${1}"
local _buf_file="${jailsysdir}/${jname}/bhyve_pciid"
if is_bhyve_pci_id_busy ${_id}; then
# already in list
return 0
fi
bhyve_pci_id_busy_list="${bhyve_pci_id_busy_list} ${_id}"
# use $_buf_file as an intermediate buffer for share
# and exchange $bhyve_pci_id_busy_list vars
${SYSRC_CMD} -qf ${_buf_file} bhyve_pci_id_busy_list="${bhyve_pci_id_busy_list}" > /dev/null 2>&1
return 0
}
# -a _pcislot_bus
# -b _pcislot_pcislot
# -c _pcislot_function
# -n - device name: fbuf,..
# -d - device descr/args, e.g <path_to_device>
store_bhyve_pci_slot()
{
local _pcislot_bus=0
local _pcislot_pcislot=0
local _pcislot_function=0
local _pcislot_name=
local _pcislot_desc=
local _mydb _i _res
while getopts "a:b:c:d:n:" opt; do
case "${opt}" in
a) _pcislot_bus="${OPTARG}" ;;
b) _pcislot_pcislot="${OPTARG}" ;;
c) _pcislot_function="${OPTARG}" ;;
d) _pcislot_desc="${OPTARG}" ;;
n) _pcislot_name="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
_mydb="${jailsysdir}/${jname}/local.sqlite"
[ -z "${_pcislot_name}" ] && err 1 "store_bhyve_pci_slot: name is empty"
for _i in _pcislot_bus _pcislot_pcislot _pcislot_function; do
eval _res="\${${_i}}"
if is_number ${_res}; then
err 1 "store_bhyve_pci_slot: ${_i} is not number: ${_res}"
fi
done
local _search_condition
local _tmp_bhyve_pci_index=
# store into temporary pcibus map to determine/avoid bus id collision
cbsdsqlrw ${_mydb} "INSERT INTO pcibus_run ( pcislot_name,pcislot_bus,pcislot_pcislot,pcislot_function,pcislot_desc,modified ) VALUES ( \"${_pcislot_name}\", \"${_pcislot_bus}\", \"${_pcislot_pcislot}\", \"${_pcislot_function}\", \"${_pcislot_desc}\", true )"
_search_condition="pcislot_name=\"${_pcislot_name}\""
[ -n "${_pcislot_desc}" ] && _search_condition="${_search_condition} AND pcislot_desc=\"${_pcislot_desc}\""
_tmp_bhyve_pci_index=$( cbsdsqlro ${_mydb} SELECT pcislot_bus FROM pcibus WHERE ${_search_condition} LIMIT 1 | ${AWK_CMD} '{printf $1}' )
if [ -n "${_tmp_bhyve_pci_index}" ]; then
# update modified flags
cbsdsqlrw ${_mydb} "UPDATE pcibus SET modified=true WHERE ${_search_condition}"
[ "${_tmp_bhyve_pci_index}" = "${_pcislot_bus}" ] && return 0 # record already exist (todo: check for bus/func equal
# pci slot not equal, update
cbsdsqlrw ${_mydb} "UPDATE pcibus SET pcislot_bus=\"${_pcislot_bus}\" WHERE ${_search_condition}"
else
# store pci bus map
cbsdsqlrw ${_mydb} "INSERT INTO pcibus ( pcislot_name,pcislot_bus,pcislot_pcislot,pcislot_function,pcislot_desc,modified ) VALUES ( \"${_pcislot_name}\", \"${_pcislot_bus}\", \"${_pcislot_pcislot}\", \"${_pcislot_function}\", \"${_pcislot_desc}\", true )"
fi
}
# autoincrement for $bhyve_pci_index variable which symbolizes pci id bus
# -e end bus range range (e.g: -e 31), default: 31
# -o (order): next or prev (default is: next)
# -s start bus range (e.g: -s 4), default: 3
# -n - device name: fbuf,..
# -d - device descr/args, e.g <path_to_device>
# when (-n name) and/or (-d descr) is specified,
# lookup for stored pciid information first to
# get old settings
# 0 - reserved for hostbridge
# 1 - reserved for ReFIND device
# 2 - reserved for boot device
# normal use:
# if ! next_pci_id; then
# # no free pci bus
# exit 0
# fi
# or:
# if ! next_pci_id -n fbuf; then
# # no free pci bus
# exit 0
# fi
#
next_pci_id()
{
local _start_bus_id=3 _end_bus_id=31 _order="next"
local _pcislot_name=
local _pcislot_desc=
local _res
while getopts "d:e:n:o:s:" opt; do
case "${opt}" in
e) _end_bus_id="${OPTARG}" ;;
o) _order="${OPTARG}" ;;
s) _start_bus_id="${OPTARG}" ;;
d) _pcislot_desc="${OPTARG}" ;;
n) _pcislot_name="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
# lookup database stored first
if [ -n "${_pcislot_name}" ]; then
local _mydb
local _search_condition
local _tmp_bhyve_pci_index=
_mydb="${jailsysdir}/${jname}/local.sqlite"
_search_condition="pcislot_name=\"${_pcislot_name}\""
[ -n "${_pcislot_desc}" ] && _search_condition="${_search_condition} AND pcislot_desc=\"${_pcislot_desc}\""
_tmp_bhyve_pci_index=$( cbsdsqlro ${_mydb} SELECT pcislot_bus FROM pcibus WHERE ${_search_condition} LIMIT 1 | ${AWK_CMD} '{printf $1}' )
if [ -n "${_tmp_bhyve_pci_index}" ]; then
# update modified
cbsdsqlrw ${_mydb} "UPDATE pcibus SET modified=true WHERE ${_search_condition}"
bhyve_pci_index=${_tmp_bhyve_pci_index}
return 0
fi
fi
# end of lookup database stored first
[ ${_start_bus_id} -gt ${_end_bus_id} ] && ${ECHO} "${N1_COLOR}next_pci_id: invalid range${N0_COLOR}" && return 1
[ -z "${bhyve_pci_index}" ] && bhyve_pci_index="0"
bhyve_pci_index=0
for bhyve_pci_index in $( ${SEQ_CMD} ${_start_bus_id} ${_end_bus_id} ); do
_res=$( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT idx FROM pcibus WHERE pcislot_bus=${bhyve_pci_index} LIMIT 1 2>/dev/null )
[ -n "${_res}" ] && continue # probably already used by another devices
if ! is_bhyve_pci_id_busy ${bhyve_pci_index}; then
# found
return 0
fi
done
${ECHO} "${N1_COLOR}No free available bhyve_pci_index in ${_start_bus_id} ${_end_bus_id} range${N0_COLOR}"
return 1
}
# autoincement for nmdm_index
next_nmdm_id()
{
local _i
nmdm_index=0
for _i in $( ${SEQ_CMD} 0 500 ); do
${LS_CMD} /dev/nmdm${_i}A* > /dev/null 2>&1
if [ $? -ne 0 ]; then
nmdm_index=${_i}
return 0
fi
done
[ ${_i} -eq 500 ] && return 1
return 0
}
get_pcislot_args()
{
local _i
local _pcislot_bus
local _pcislot_pcislot
local _pcislot_function
local _bhyve_pci_index
local _pcislot_args
_pcislot_pcislot=0 # optional, 0 to 255. if not specified, the bus value defaults to 0
_pcislot_function=0 # optional, 0 to 7. if not specified, the function value defaults to 0
while getopts "a:b:c:i:" opt; do
case "${opt}" in
a) _pcislot_bus="${OPTARG}" ;;
b) _pcislot_pcislot="${OPTARG}" ;;
c) _pcislot_function="${OPTARG}" ;;
i) _bhyve_pci_index="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
[ -z "${_bhyve_pci_index}" ] && err 1 "get_pcislot_args: empty _bhyve_pci_index"
add_bhyve_pci_id_busy ${_bhyve_pci_index}
_pcislot_bus="${_bhyve_pci_index}" # 0 to 31
# always set pcislot
_pcislot_args="${_pcislot_bus}"
# skip defaults for bus and func
for _i in ${_pcislot_pcislot} ${_pcislot_function}; do
[ ${_i} -eq 0 ] && continue
_pcislot_args="${_pcislot_args}:${_i}"
done
printf "${_pcislot_args}"
}
compile_uefi_boot_args()
{
local _pcislot_args=
# inherit cd_boot_firmware if hdd_boot_firmware not set
[ "${hdd_boot_firmware}" = "0" ] && hdd_boot_firmware="${cd_boot_firmware}"
#also same string in compile_cd_args cd_args
case "${vm_boot}" in
hdd)
# refind alwas at 1 reserved pci id
case "${hdd_boot_firmware}" in
refind)
bhyve_pci_index=1
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
uefi_boot_args="-s ${_pcislot_args},ahci-cd,/usr/local/cbsd/upgrade/patch/efirefd.fd,ro"
store_bhyve_pci_slot -n ahci-cd -d /usr/local/cbsd/upgrade/patch/efirefd.fd -a ${bhyve_pci_index}
;;
esac
;;
cd)
case "${cd_boot_firmware}" in
refind)
# refind always at 1 reserved pci id
if ! next_pci_id -n ahci-cd -d /usr/local/cbsd/upgrade/patch/efirefd.fd; then
# no free pci bus
exit 0
fi
bhyve_pci_index=1
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
uefi_boot_args="-s ${_pcislot_args},ahci-cd,/usr/local/cbsd/upgrade/patch/efirefd.fd,ro"
store_bhyve_pci_slot -n ahci-cd -d /usr/local/cbsd/upgrade/patch/efirefd.fd -a ${bhyve_pci_index}
;;
bhyve)
# populate cd_args2
# refind alwas at 1 reserved pci id
if ! next_pci_id -n ahci-cd -d /usr/local/cbsd/upgrade/patch/efirefd.fd; then
# no free pci bus
exit 0
fi
bhyve_pci_index=1
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
cd_args2="-s ${_pcislot_args},ahci-cd,/usr/local/cbsd/upgrade/patch/efirefd.fd,ro"
store_bhyve_pci_slot -n ahci-cd -d /usr/local/cbsd/upgrade/patch/efirefd.fd -a ${bhyve_pci_index}
;;
esac
;;
net)
# refind always at 1 reserved pci id
#if ! next_pci_id -n ahci-cd -d /usr/local/cbsd/upgrade/patch/efirefn.fd; then
# # no free pci bus
# exit 0
#fi
bhyve_pci_index=1
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
uefi_boot_args="-s ${_pcislot_args},ahci-cd,/usr/local/cbsd/upgrade/patch/efirefn.fd,ro"
store_bhyve_pci_slot -n ahci-cd -d /usr/local/cbsd/upgrade/patch/efirefn.fd -a ${bhyve_pci_index}
;;
esac
}
# export arguments for bhyve about dsk vms in $dsk_args variable
# $jname must be set's
# sample:
# jname="debian"
# if compile_dsk_args; then
# echo $dsk_args
# else
# echo "No disks"
# fi
compile_dsk_args()
{
local dsk_id=0
local sqldelimer=" "
local prefix
local full_dsk_path
local _shared_devs
local _pcislot_args= T F
# temporary workaround for Alex' (CBSD Telegram) issue with no disk (old SQL schema) - don't select dsk_conf
# eval $( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT dsk_controller,dsk_path,dsk_slot,dsk_conf FROM bhyvedsk WHERE jname=\"${jname}\" AND dsk_type=\"vhd\" |while read dsk_controller dsk_path dsk_slot dsk_conf; do
# select only direct-bus attached
eval $( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT dsk_controller,dsk_path,dsk_slot,bootable FROM bhyvedsk WHERE dsk_type=\"vhd\" AND controller_id='0' | while read dsk_controller dsk_path dsk_slot bootable; do
dsk_sectorsize=
dsk_conf=
rate_limit_conf=
case "${dsk_controller}" in
virtio-blk|ahci-hd)
# direct connect
;;
*)
# via controller
continue
;;
esac
dsk_conf=$( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT dsk_conf FROM bhyvedsk WHERE dsk_path=\"${dsk_path}\" LIMIT 1 2>/dev/null )
# back compatible for < CBSD 12.0.12, unset sectorsize=512
[ "${dsk_conf}" = "sectorsize=512" -o "${dsk_conf}" = "0" ] && dsk_conf=
dsk_sectorsize=$( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT dsk_sectorsize FROM bhyvedsk WHERE dsk_path=\"${dsk_path}\" LIMIT 1 2>/dev/null )
if [ -n "${dsk_sectorsize}" ]; then
if [ -n "${dsk_conf}" ]; then
dsk_conf="${dsk_conf},sectorsize=${dsk_sectorsize}"
else
dsk_conf="sectorsize=${dsk_sectorsize}"
fi
fi
# if [ "${bhyve_have_dsk_ratelimit}" = "1" ]; then
# dsk_iops_limit=$( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT dsk_iops_limit FROM bhyvedsk WHERE dsk_path=\"${dsk_path}\" LIMIT 1 2>/dev/null )
# dsk_mbps_limit=$( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT dsk_mbps_limit FROM bhyvedsk WHERE dsk_path=\"${dsk_path}\" LIMIT 1 2>/dev/null )
# [ -z "${dsk_iops_limit}" ] && dsk_iops_limit=0
# [ -z "${dsk_mbps_limit}" ] && dsk_mbps_limit=0
# else
dsk_iops_limit=0
dsk_mbps_limit=0
# fi
if [ "${dsk_iops_limit}" != "0" ]; then
if [ -z "${rate_limit_conf}" ]; then
rate_limit_conf="iops_limit=${dsk_iops_limit}"
else
rate_limit_conf="${rate_limit_conf},iops_limit=${dsk_iops_limit}"
fi
fi
if [ "${dsk_mbps_limit}" != "0" ]; then
if [ -z "${rate_limit_conf}" ]; then
rate_limit_conf="mbps_limit=${dsk_mbps_limit}"
else
rate_limit_conf="${rate_limit_conf},mbps_limit=${dsk_mbps_limit}"
fi
fi
# test for full path. If path not started from '/' - append $data dir
full_dsk_path=
prefix=$( substr --pos=0 --len=1 --str="${dsk_path}" )
if [ "${prefix}" != "/" ]; then
full_dsk_path="${data}/${dsk_path}"
else
full_dsk_path="${dsk_path}"
fi
if ! next_pci_id -n ${dsk_controller} -d ${full_dsk_path}; then
# no free pci bus
exit 0
fi
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
store_bhyve_pci_slot -n ${dsk_controller} -d ${full_dsk_path} -a ${bhyve_pci_index}
if [ "${bootable}" = "1" ]; then
# use reserved bus id 2 for bootable device, not for store in pcibus
# just overwrite pcislot_args
bhyve_pci_index=2
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
echo "export dsk_bootable=\"${dsk_path}\" ;"
fi
if [ -n "${rate_limit_conf}" ]; then
if [ -z "${dsk_conf}" ]; then
dsk_conf="${rate_limit_conf}"
else
dsk_conf="${dsk_conf},${rate_limit_conf}"
fi
fi
if [ -n "${dsk_conf}" ]; then
echo "export dsk${dsk_id}=\"-s ${_pcislot_args},${dsk_controller},${full_dsk_path},${dsk_conf}\" ;"
else
echo "export dsk${dsk_id}=\"-s ${_pcislot_args},${dsk_controller},${full_dsk_path}\" ;"
fi
# export dskX_path
echo "export dsk${dsk_id}_path=\"${full_dsk_path}\";"
dsk_id=$(( dsk_id + 1 ))
done ) || err 1 "${N1_COLOR}Error while create disk map${N0_COLOR}"
dsk_args=
mydsk=
for i in $( ${SEQ_CMD} 0 31 ); do
T=
eval T="\$dsk$i"
[ -z "${T}" ] && break
F=
eval F="\$dsk${i}_path"
if [ -n "${F}" ]; then
if [ -z "${mydsk}" ]; then
mydsk="${F}"
else
mydsk="${mydsk} ${F}"
fi
fi
## make custom argument for bhyve and add count to bhive_pci_index cause increment in while loop we lose
#if ! next_pci_id; then
# # no free pci bus
#fi
#add_bhyve_pci_id_busy ${bhyve_pci_index}
if [ -z "${dsk_args}" ]; then
dsk_args="${T}"
else
dsk_args="${dsk_args} ${T}"
fi
done
# shared devices
_shared_devs=$( cbsdsqlro storage_media "SELECT path FROM media WHERE type=\"shared\" AND jname=\"${jname}\"" )
for i in ${_shared_devs}; do
if ! next_pci_id -n virtio-blk -d ${i}; then
# no free pci bus
exit 0
fi
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
dsk_args="${dsk_args} -s ${_pcislot_args},virtio-blk,${i}"
store_bhyve_pci_slot -n virtio-blk -d ${i} -a ${bhyve_pci_index}
done
if [ -r ${data}/cbsd.img ]; then
if ! next_pci_id -n virtio-blk -d cbsd.img; then
# no free pci bus
exit 0
fi
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
dsk_args="${dsk_args} -s ${_pcislot_args},virtio-blk,${data}/cbsd.img"
store_bhyve_pci_slot -n virtio-blk -d cbsd.img -a ${bhyve_pci_index}
fi
[ -z "${dsk_args}" ] && return 1
return 0
}
# export arguments for bhyve cd dsk vms in $cd_args variable
# $jname must be set's
# sample:
# jname="debian"
# if compile_cd_args; then
# echo $cd_args
# else
# echo "No disks"
# fi
compile_cd_args()
{
local cd_id=0
local sqldelimer=" "
local _pcislot_args=
local _ext
local _device T F
eval $( cbsdsqlro storage_media SELECT idx,path FROM media WHERE jname=\"${jname}\" AND type=\"iso\" | while read idx path; do
# check for .fs$ extension: use ahci-hd for this (e.g OpenBSD image)
_ext=${path##*.}
if [ "${_ext}" = "fs" ]; then
_device="ahci-hd"
else
_device="ahci-cd"
fi
if ! next_pci_id -n ${_device} -d ${path}-${idx}; then
# no free pci bus
exit 0
fi
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
store_bhyve_pci_slot -n ${_device} -d ${path}-${idx} -a ${bhyve_pci_index}
echo "export cd${cd_id}=\"-s ${_pcislot_args},${_device},${path},ro\" ;"
# export cdX_path
echo "export cd${cd_id}_path=\"${path}\";"
cd_id=$(( cd_id + 1 ))
done ) || err 1 "${N1_COLOR}Error while create cd map${N0_COLOR}"
cd_args=
for i in $( ${SEQ_CMD} 0 31 ); do
eval T="\$cd$i"
[ -z "${T}" ] && break
F=
eval F="\$cd${i}_path"
if [ -n "${F}" ]; then
if [ -z "${mydsk}" ]; then
mycd="${F}"
else
mycd="${mydsk} ${F}"
fi
fi
cd_args="${cd_args} ${T}"
done
# Cloud-init support. Looking for seed.iso in sysdir
if [ -r ${jailsysdir}/${jname}/seed.iso ]; then
if ! next_pci_id -n ahci-cd -d seed.iso; then
# no free pci bus
exit 0
fi
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
# cd_args="${cd_args} -s ${_pcislot_args},ahci-cd,${jailsysdir}/${jname}/seed.iso,nocache,direct,ro" # no cache/direct for direct editing in VM runtime
cd_args="${cd_args} -s ${_pcislot_args},ahci-cd,${jailsysdir}/${jname}/seed.iso,ro" # no cache/direct for direct editing in VM runtime
store_bhyve_pci_slot -n ahci-cd -d seed.iso -a ${bhyve_pci_index}
fi
[ -z "${cd_args}" ] && return 1
# export for boot/install/eject mode as cd_args
#cd_args2=
#"${cd_args}"
return 0
}
# export arguments for bhyve about dsk vms attached to controller_id in $dsk_controller_args variable
# $jname must be set's
# sample:
# jname="debian"
# if compile_dsk_controller_args; then
# echo $dsk_controller_args
# else
# echo "No disks"
# fi
compile_dsk_controller_args()
{
local prefix
local full_dsk_path
local _pcislot_args=
dsk_controller_args=
local _dsk_path=
local _i
_dsk_controller_name=$( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT name FROM bhyve_dskcontroller WHERE name!=\"0\" )
_dsk_controller_type=$( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT type FROM bhyve_dskcontroller WHERE name=\"${_dsk_controller_name}\" )
[ -z "${_dsk_controller_name}" ] && return 0
# todo: controller name as desc ? dsk inherit bus id ?
if ! next_pci_id -n ${_dsk_controller_type}; then
# no free pci bus
exit 0
fi
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
store_bhyve_pci_slot -n ${_dsk_controller_name} -a ${bhyve_pci_index}
dsk_controller_args="-s ${_pcislot_args},${_dsk_controller_type}"
_dsk_path=$( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT dsk_path FROM bhyvedsk WHERE dsk_controller=\"${_dsk_controller_name}\" | ${XARGS_CMD} )
for dsk_path in ${_dsk_path}; do
# test for full path. If path not started from '/' - append $data dir
full_dsk_path=
prefix=$( substr --pos=0 --len=1 --str="${dsk_path}" )
if [ "${prefix}" != "/" ]; then
full_dsk_path="${data}/${dsk_path}"
else
full_dsk_path="${dsk_path}"
fi
dsk_controller_args="${dsk_controller_args},hd:${full_dsk_path}"
done
[ -z "${dsk_controller_args}" ] && return 1
return 0
}
# -s <n>,nvme,devpath,maxq=#,qsz=#,ioslots=#,sectsz=#,ser=A-Z
compile_nvme_args()
{
local prefix
local full_dsk_path
local sqldelimer=" "
local _pcislot_args=
nvme_args=
cur_nvme_count=$( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite 'SELECT COUNT(id) FROM bhyve_nvme' | ${AWK_CMD} '{printf $1}' )
[ "${cur_nvme_count}" = "0" ] && return 1
${ECHO} "${N1_COLOR}NVMe controller: ${N2_COLOR}${cur_nvme_count}${N0_COLOR}"
eval $( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite "SELECT id,devpath,ram,maxq,qsz,ioslots,sectsz,ser FROM bhyve_nvme LIMIT 1" | while read id devpath ram maxq qsz ioslots sectsz ser; do
[ -z "${id}" ] && return 0
echo "export nvme_id=\"${id}\""
echo "export nvme_devpath=\"${devpath}\""
echo "export nvme_ram=\"${ram}\""
echo "export nvme_maxq=\"${maxq}\""
echo "export nvme_qsz=\"${qsz}\""
echo "export nvme_ioslots=\"${ioslots}\""
echo "export nvme_sectsz=\"${sectsz}\""
echo "export nvme_ser=\"${ser}\""
nvme_last_id=$(( nvme_last_id + 1 ))
done ) || err 1 "${N1_COLOR}Error while create nvme map${N0_COLOR}"
${ECHO} "${N1_COLOR}NVMe controller path: ${N2_COLOR}${nvme_devpath}${N0_COLOR}"
[ -z "${nvme_devpath}" ] && return 1
# todo: nvme name, path?
if ! next_pci_id -n nvme; then
# no free pci bus
exit 0
fi
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
nvme_args="-s ${_pcislot_args},nvme"
store_bhyve_pci_slot -n nvme -a ${bhyve_pci_index}
if [ ! -r ${nvme_devpath} ]; then
${ECHO} "${LDED}Warning: compile_nvme_args: path not available, skipp: ${N2_COLOR}${nvme_devpath}${N0_COLOR}"
return 1
fi
# todo
# validation for ram>0 and so on..
nvme_args="${nvme_args},${nvme_devpath},ram=${nvme_ram}"
[ -z "${nvme_args}" ] && return 1
return 0
}
# print 5c:f9:dd:76:d5:c4 from 5cf9dd76d5c4
normalize_nic_hwaddr()
{
local _hwaddr
[ -z "${1}" ] && return 0
_hwaddr=$( echo "${1}" | ${TR_CMD} -d ":" | ${SED_CMD} -e 's/\([0-9A-Fa-f]\{2\}\)/\1:/g' -e 's/\(.*\):$/\1/' )
printf ${_hwaddr}
}
# export arguments for bhyve about nic vms in $nic_args variable
# $jname must be set's
# sample:
# jname="debian"
# if compile_nic_args; then
# echo $nic_args
# else
# echo "No nic"
# fi
compile_nic_args()
{
local _id=0 i _res taplist=
local sqldelimer=" "
local mybridge
local net_emul=
local _parent_mtu=0
local _ret=
local _is_bridge
local autoconn_iface=$( ${SYSRC_CMD} -n cbsd_autoconnect_bridge 2>&1 )
local _pcislot_args= _pref
local IFS OIFS
. ${distdir}/vnet.subr # for get_vm_uplink_interface
local errmsg=
# bstart.conf: clear VM hwaddr in global ARP table
[ -z "${clean_arp_table_by_vm}" ] && clean_arp_table_by_vm="1"
eval $( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT id,nic_order,nic_driver,nic_slot,nic_type,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit FROM bhyvenic WHERE jname=\"${jname}\" | while read nic_id nic_order nic_driver nic_slot nic_type nic_parent nic_hwaddr nic_address nic_mtu nic_persistent nic_ratelimit; do
case "${nic_driver}" in
e1000)
net_emul="e1000"
;;
*)
net_emul="virtio-net"
;;
esac
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: nic driver: ${nic_driver}, nic_parent: ${nic_parent}
# when nic_persistent=1 just put it to args
if [ ${nic_persistent} -eq 1 ]; then
_pref3=$( substr --pos=0 --len=3 --str=${nic_parent} )
[ "${_pref3}" != "tap" ] && err 1 "errmsg=\"only tap interface can persistent, not: ${nic_parent}\""
${IFCONFIG_CMD} ${nic_parent} > /dev/null 2>&1
[ $? -ne 0 ] && err 1 "errmsg=\"interface ${nic_parent} marked as persistent but not exist\""
nic_hwaddr="0"
mytap="${nic_parent}"
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: ${mytap} marked as persistent
else # << if [ ${nic_persistent} -eq 0 ]
case ${nic_parent} in
cbsdvale*)
get_vm_uplink_interface -p ${nic_parent} -u
;;
vale*)
get_vm_uplink_interface -p ${nic_parent} -u
;;
*)
# VPC support
case "${nic_parent}" in
vpc-*)
VPC_ROOT_DIR="${dbdir}/vpc"
_arg_len=$( strlen ${nic_parent} )
vpc_name=$( substr --pos=5 --len=${_arg_len} --str="${nic_parent}" )
_dbpath="${VPC_ROOT_DIR}/${vpc_name}.sqlite"
[ ! -r ${_dbpath} ] && err 1 "${N1_COLOR}VPC not exist: ${N2_COLOR}${_dbpath}${N0_COLOR}"
mybridge=
if ! mybridge=$( get_my_device vpc vpc-${vpc_name} ); then
err 1 "${N1_COLOR}${CBSD_APP} failed to get VPC bridge: ${mybridge}${N0_COLOR}"
fi
${IFCONFIG_CMD} ${mybridge} > /dev/null 2>&1
_ret=$?
if [ ${_ret} -ne 0 ]; then
err 1 "${N1_COLOR}no such VPC bridge interface: ${N2_COLOR}${mybridge}${N0_COLOR}"
fi
;;
*)
if [ "${nic_parent}" != "0" -o "${nic_parent}" = "auto" ]; then
_is_bridge=
[ "${nic_parent}" != "auto" ] && _is_bridge=$( ${IFCONFIG_CMD} ${nic_parent} | ${AWK_CMD} '/groups:/{print $2}' | ${GREP_CMD} -o bridge )
if [ "${_is_bridge}" != "bridge" ]; then
# this is not bridge, detect uplink iface
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: ${nic_parent} is not bridge
get_vm_uplink_interface -p ${nic_parent}
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: uplink interface selected: ${interface}
if [ "${interface}" != "disable" ]; then
# always up parent device
[ "${interface}" != "auto" ] && ${IFCONFIG_CMD} ${interface} up
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: try to get bridge for interface: ${interface}
if ! mybridge=$( get_my_device bridge ${interface} ); then
err 1 "errmsg=\"Error:get_my_device_bridge_for_${interface}: ${mybridge}\""
fi
else
# don't create/attach to bridge
mybridge="disable"
fi
else
${IFCONFIG_CMD} ${nic_parent} > /dev/null 2>&1
_ret=$?
[ ${_ret} -ne 0 ] && err 1 "errmsg=\"Error:no_such_bridge: ${nic_parent}. Please create it\""
mybridge="${nic_parent}"
fi
else
# don't create/attach to bridge
mybridge="disable"
fi
;;
esac
mytap=$( get_my_tap ${mybridge} )
_ret=$?
[ ${_ret} -eq 1 ] && err 1 "errmsg=\"Error:get_my_tap_for_${mybridge}: ${mybridge}\""
# MTU management, should be before addm
[ -z "${nic_mtu}" ] && nic_mtu=0 # AUTO
_parent_mtu=0
if [ ${nic_mtu} -eq 0 -a "${mybridge}" != "disable" ]; then
# Get parent MTU size
_parent_mtu=$( ${toolsdir}/nic_info --nic=${mybridge} --mtu --quiet 2>/dev/null )
_ret=$?
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: mtu set to auto, parent ${mybridge} MTU: ${_parent_mtu}
if [ ${_ret} -eq 0 ]; then
[ -n "${_parent_mtu}" ] && _ret=$( ${IFCONFIG_CMD} ${mytap} mtu ${_parent_mtu} )
fi
else
# Get parent MTU size
# compare MTU with parent
#_parent_mtu=$( ${toolsdir}/nic_info --nic=${mybridge} --mtu --quiet 2>/dev/null )
#_ret=$?
#if [ ${_ret} -eq 0 ]; then
# [ ${nic_mtu} -gt ${_parent_mtu} ]
#fi
[ "${nic_mtu}" = "0" ] && nic_mtu="1500" # case when mybridge = "disable"
_ret=$( ${IFCONFIG_CMD} ${mytap} mtu ${nic_mtu} )
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: MTU fixed: ${nic_mtu}
fi
${IFCONFIG_CMD} ${mytap} description "${jname}-nic${_id}" group vm-port up
if [ "${mybridge}" != "disable" ]; then
if [ "${autoconn_iface}" = "NO" ]; then
${IFCONFIG_CMD} ${mybridge} addm ${mytap} >/dev/null 2>&1
# export in mytap_X_bridge= as bridge members
echo "export mytap_${mytap}_bridge=\"${mybridge}\";"
else
${IFCONFIG_CMD} ${mybridge} addm ${interface} addm ${mytap} >/dev/null 2>&1
# export in mytap_X_bridge= as bridge members
echo "export mytap_${mytap}_bridge=\"${mybridge}\";"
fi
fi
if [ -n "${nic_address}" ]; then
OIFS="${OIFS}"
if [ "${nic_address}" != "0" -o "${nic_address}" != "disable" ]; then
IFS=","
for ip in ${nic_address}; do
if [ "${ip}" = "DHCP" ]; then
ip=$( dhcpd )
[ $? -eq 2 ] && err 1 "${N1_COLOR}No free IP address for DHCP in nodeippool${N0_COLOR}"
fi
IFS="${OIFS}"
ipwmask ${ip}
iptype ${IWM}
local _inet=$?
case "${_inet}" in
1)
local proto="inet"
;;
2)
local proto="inet6"
;;
*)
continue
;;
esac
# ipv type + mask
${IFCONFIG_CMD} ${mytap} ${proto} ${ip} alias
IFS=","
done
IFS="${OIFS}"
fi
fi
;;
esac
fi # << if [ ${nic_persistent} -eq 0 ]
# MAC MGMT
if [ "${nic_hwaddr}" = "0" ]; then
nic_hwaddr=$( mac_gen 00:a0:98 )
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: MAC address randomized and updated for nic id ${nic_id}: ${nic_hwaddr}
cbsdsqlrw ${jailsysdir}/${jname}/local.sqlite "UPDATE bhyvenic SET nic_hwaddr=\"${nic_hwaddr}\" WHERE id=\"${nic_id}\""
fi
if [ "${clean_arp_table_by_vm}" = "1" ]; then
${ARP_CMD} -an | ${AWK_CMD} -v pattern=" ${nic_hwaddr} " '$0 ~ pattern { print $2 }' | ${SORT_CMD} | ${TR_CMD} -d '()' | while read _ip; do
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: clean_arp_table_by_vm set to 1: prune ${nic_hwaddr} for ${_ip} in ARP table
${ARP_CMD} -n -d ${_ip} > /dev/null 2>&1 || true
done
fi
# todo: name as descr? or mac as desc?
if ! next_pci_id -n ${net_emul} -d ${nic_id}; then
# no free pci bus
exit 0
fi
_pcislot_args=$( get_pcislot_args -i ${bhyve_pci_index} )
myargs="-s ${_pcislot_args},${net_emul},${mytap}"
# virtio-net support for MTU after r359704
if [ ${freebsdhostversion} -gt 1300095 ]; then
# manage MTU on bhyve tap/virtio side
[ ${nic_mtu} -eq 0 -a ${_parent_mtu} -ne 0 ] && nic_mtu="${_parent_mtu}" # adjust nic_mtu by parent value
case "${nic_mtu}" in
0)
;;
*)