forked from michaeldexter/vmrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm
executable file
·1769 lines (1623 loc) · 47.9 KB
/
vm
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
#!/bin/sh
#-
################################################################ LICENSE
#
# Copyright (c) 2012-2014 Michael Dexter <editor@callfortesting.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
############################################################ INFORMATION
#
# $Title: virtual machine management script $
# $Version: v.0.6$
#
# rc(8) command script for managing virtual machines.
#
############################################################# RCORDER(8)
# See rcorder(8) for additional information
# PROVIDE: bhyve
# REQUIRE: LOGIN hostname
# KEYWORD: shutdown
################################################################## RC(8)
# See rc(8) for additional information.
. /etc/rc.subr
name="vm"
rcvar="vm_enable"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
restart_cmd="${name}_restart"
extra_commands="
attach
boot
debug
delete
destroy
format
grub
iso
jail
load
mount
qemu
status
umount
" # END-QUOTE
attach_cmd="${name}_attach"
boot_cmd="${name}_boot"
debug_cmd="${name}_debug"
delete_cmd="${name}_delete"
destroy_cmd="${name}_destroy"
format_cmd="${name}_format"
grub_cmd="${name}_grub"
iso_cmd="${name}_iso"
jail_cmd="${name}_jail"
load_cmd="${name}_load"
mount_cmd="${name}_mount"
qemu_cmd="${name}_qemu"
status_cmd="${name}_status"
umount_cmd="${name}_umount"
################################################################ GLOBALS
ISO_BOOT="isobootno"
###################################################### UTILITY FUNCTIONS
# info $format [$arguments ...]
#
# Print text to the console using printf(1) syntax (can be used within a sub-
# shell). The printf(1) format does not require a trailing newline sequence
# (`\n').
#
info()
{
[ $# -gt 0 ] && printf "$@" >&3 && echo >&3
}
# die $format [$arguments ...]
#
# Exit with error status, optionally printing a message to standard error using
# printf(1) syntax before terminating. The printf(1) format does not require a
# trailing newline sequence (`\n').
#
die()
{
exec 3>&2
info "$@"
exit 1
}
# f_timestamp
#
# A simple invocation of 'date' for use with logging. Modify as you please.
#
f_timestamp()
{
echo $( date "+%Y-%m-%d %H:%M:%S:" ) # Perhaps insert a newline
}
# f_eptcheck
#
# Check CPU for EPT feature flag. Causes premature termination with error
# message if the CPU does not appear to support EPT.
#
f_eptcheck()
{
local funcname=f_eptcheck
info "Entering $funcname()"
grep -qw POPCNT /var/run/dmesg.boot || die \
"$funcname: Your CPU does not appear to support EPT! Exiting."
}
# f_vmmcheck
#
# Make sure the vmm kernel module is loaded. If not, attempt to load it. Causes
# premature termination with error message if the module cannot be loaded.
#
f_vmmcheck()
{
local funcname=f_vmmcheck
info "Entering $funcname()"
if ! kldstat | grep -qwF vmm.ko; then
info "$funcname: vmm.ko kernel module not loaded. Loading..."
kldload vmm || die
else
info "$funcname: vmm.ko is loaded."
fi
}
# f_tmuxcheck
#
# Check to see if tmux is installed. Causes premature termination with error
# message if tmux is not installed.
#
f_tmuxcheck()
{
local funcname=f_tmuxcheck
info "Entering $funcname()"
case "$vm_console" in
tmux*)
[ -x /usr/local/bin/tmux ] || die "$funcname: %s Exiting." \
"tmux is not installed! (ports/sysutils/tmux)"
esac
}
# f_grubcheck
#
# Check to see if the grub-bhyve utility is installed. Causes premature
# termination with error message if grub-bhyve is not installed.
#
f_grubcheck()
{
local funcname=f_grubcheck
info "Entering $funcname()"
[ -x /usr/local/sbin/grub-bhyve ] || die "$funcname: %s Exiting." \
"grub-bhyve is not installed! (ports/sysutils/grub2-bhyve)"
}
# f_netstart
#
# Create bridgeX device (loading required kernel modules) if it doesn't exist.
#
f_netstart()
{
local funcname=f_netstart
# Just in case
. /usr/local/etc/vm.conf
if ! kldstat | grep -qwF bridgestp.ko; then
info "$funcname: bridgestp.ko kernel module not loaded. Loading..."
kldload bridgestp || die
else
info "$funcname: bridgestp.ko is loaded."
fi
if ! kldstat | grep -qwF if_bridge.ko; then
info "$funcname: if_bridge.ko kernel module not loaded. Loading..."
kldload if_bridge || die
else
info "$funcname: if_bridge.ko is loaded."
fi
if ! kldstat | grep -qwF if_tap.ko; then
info "$funcname: if_tap.ko kernel module not loaded. Loading..."
kldload if_tap || die
sysctl net.link.tap.up_on_open=1
else
info "$funcname: if_tap.ko is loaded."
fi
if ! ifconfig -l | grep -qwF bridge${host_bridge}; then
info "Creating the bridge${host_bridge} network interface."
ifconfig bridge${host_bridge} create || die
ifconfig bridge${host_bridge} up || die
else
info "$funcname: bridge$host_bridge exists."
fi
if ! ifconfig bridge${host_bridge} | grep ${host_nic}; then
info "Associating $host_nic with bridge${host_bridge}."
echo "Running: ifconfig bridge${host_bridge} addm ${host_nic} up"
ifconfig bridge${host_bridge} addm ${host_nic} up || die
else
info "$funcname: $host_nic is assoc. with host${host_bridge}."
fi
}
# f_readconfig_exit
#
# Make sure a given VM configuration file exists and read it. Causes
# premature termination with error message if the module cannot be loaded.
#
f_readconfig_exit()
{
local funcname=f_readconfig_exit
info "Entering $funcname()"
vm_name="$vm_names"
info "Checking for $host_vmdir/$vm_name/${vm_name}.conf"
if [ ! -f "$host_vmdir/$vm_name/${vm_name}.conf" ]; then
info "$funcname: $host_vmdir/$vm_name/${vm_name}.conf does not exist. Exiting."
exit
else
. "$host_vmdir/$vm_name/${vm_name}.conf"
fi
}
# f_readconfig_return
#
# Make sure a given VM configuration file exists and read it. Returns
# if the module cannot be loaded as not to break looping functions.
#
f_readconfig_return()
{
local funcname=f_readconfig_return
info "Entering $funcname()"
vm_name="$vm_names"
info "Checking for $host_vmdir/$vm_name/${vm_name}.conf"
if [ ! -f "$host_vmdir/$vm_name/${vm_name}.conf" ]; then
info "$funcname: $host_vmdir/$vm_name/${vm_name}.conf does not exist. Skipping..."
return
else
. "$host_vmdir/$vm_name/${vm_name}.conf"
fi
}
# f_getmdname
#
# Obtain the name of the md(4) device associated with $vm_name (argument to
# the script).
#
# Used by: f_mount (to see if already attached), and mddestroy (search & d)
#
f_getmdname()
{
# local funcname=f_getmdname
# info "Entering $funcname()"
# mdconfig -lv | awk -v vm_name="$vm_name" \
# '$0 ~ "^[^/]*/" vm_name "\.img$" { print $1 }'
local local_img_name="$vm_name" # Takes vm_name, lacks `.img' suffix
mdconfig -lv | awk -v local_vmname="$local_img_name" '
{
md = $1
sub("^[^/]*", "")
if ($0 ~ "/" local_vmname "\.img$")
print md
}'
}
# f_mddestroy
#
# Destroy md(4) devices associated with $vm_name (argument to script).
#
f_mddestroy()
{
local funcname=f_mddestroy
info "Entering $funcname()"
info "$funcname: Destroying all memory devices associated with %s" \
"$vm_name"
for dev in $( f_getmdname ); do
info "$funcname: Destroying mdconfig device: %s" "$dev"
mdconfig -du "$dev"
done
}
########################################################## SUB FUNCTIONS
# f_load
#
# Load the vm_device associated with $vm_name argument.
#
f_load()
{
local funcname=f_load
info "Entering $funcname()"
f_eptcheck
f_vmmcheck
f_netstart
if [ -e "/dev/vmm/$vm_name" ]; then
info "$funcname: %s is loaded. Skipping..." "$vm_name"
return
fi
if [ "$( mount | grep -o "${host_vmdir}/${vm_name}/mnt" )" = "${host_vmdir}/${vm_name}/mnt" ]; then
info "$funcname: $vm_name is currently mounted on ${host_vmdir}/${vm_name}/mnt/ Skipping..."
return
fi
# f_readconfig_return
. "$host_vmdir/$vm_name/${vm_name}.conf"
if [ "$vm_os_type" = "freebsd" ]; then
case "$vm_dev_type" in
img)
if [ ! -e "${host_vmdir}/${vm_name}/${vm_name}.img" ]; then
info "$funcname: ${host_vmdir}/${vm_name}/${vm_name}.img does not exist. Skipping..."
return
else
info "$funcname: Attaching raw image %s for fsck" "${host_vmdir}/${vm_name}/${vm_name}.img"
vm_device=$( mdconfig -af "${host_vmdir}/${vm_name}/${vm_name}.img" )
info "$funcname: DEBUG: disk image is %s; vm_device is %s" \
"${host_vmdir}/${vm_name}/${vm_name}.img" "$vm_device"
fi ;;
malloc)
if [ ! -e "/dev/$vm_device" ]; then
info "$funcname: malloc device $vm_device does not exist. Skipping..."
echo "DEBUG: Running ls /dev/$vm_device"
ls /dev/$vm_device
else
vm_device="/dev/$vm_device"
echo $vm_device
fi ;;
zvol)
if [ ! -e "/dev/zvol/${host_zpool}/$vm_name" ]; then
info "/dev/zvol/${host_zpool}/$vm_name does not exist. Skipping..."
return
else
vm_device="/dev/zvol/$host_zpool/$vm_name"
fi ;;
*)
# fix: should this check by prefixed by /dev/ ?
if [ ! -e "$vm_device" ]; then
info "$funcname: $vm_device does not exist. Skipping..."
return # otherwise we stay with the user-defined vm_device
fi
esac
case "$vm_dev_layout" in
# fix: Why is [Mm] etc failing?
# [Mm][Bb][Rr])
mbr)
vol_part="${vm_device}s1a" ;;
# Gg][Pp][Tt])
gpt)
vol_part="${vm_device}p2" ;;
*)
info "Invalid VM device layout"
return
esac
info "$funcname: fsck_ufs -y %s" "$vol_part"
fsck_ufs -y "$vol_part"
if [ "$vm_dev_type" = "img" ]; then
f_mddestroy
vm_device="${host_vmdir}/${vm_name}/${vm_name}.img"
fi
fi
# fix: note: This assumes FreeBSD=bhyveload other=grub. Could have FreeBSD/grub
if [ "$vm_os_type" = "freebsd" ]; then
# Note that $vm_device is prefixed with "/dev/"
bhyveload_cmd="/usr/sbin/bhyveload -m $vm_ram -d $vm_device $vm_name"
info "$funcname: Running the bhyveload command:\n%s" \
"$bhyveload_cmd"
local timestamp=$( f_timestamp )
echo "$timestamp $bhyveload_cmd" >> ${host_vmdir}/${vm_name}/${vm_name}.log
eval "$bhyveload_cmd"
else
f_grubcheck
info "$funcname: Creating ${host_vmdir}/${vm_name}/device.map"
vm_device="${host_vmdir}/${vm_name}/${vm_name}.img"
echo "(hd0) $vm_device" > "${host_vmdir}/${vm_name}/device.map"
echo "(cd0) ${host_vmdir}/${vm_name}/${vm_name}.iso" >> "${host_vmdir}/${vm_name}/device.map"
cat "${host_vmdir}/${vm_name}/device.map"
info "$funcname: Running the grub command:\n%s" \
"$grub_boot_cmd"
local timestamp=$( f_timestamp )
echo "$timestamp $grub_boot_cmd" >> ${host_vmdir}/${vm_name}/${vm_name}.log
eval "$grub_boot_cmd"
fi
[ -e "/dev/vmm/$vm_name" ] ||
info "$funcname: $vm_name failed to load! Exiting."
info "$funcname: $vm_name appears to have loaded."
}
# f_boot
#
# Boot function. Takes in vm_name
#
# Boot $vm_name using bhyve(8) in the desired fashion defined by $vm_console.
#
# NOTE: $ISO_BOOT is set to "isobootyes" by f_iso().
#
f_boot()
{
local funcname=f_boot
info "Entering $funcname()"
vm_id="${vm_name#${vm_name%[0-9]*}}"
if [ ! -e "/dev/vmm/$vm_name" ]; then
info "$funcname: $vm_name is not loaded. Skipping..."
return
fi
# Setting up VM PCI devices:
# 0 hostbridge
# 2 boot block device
# 3:0-7 virtual network devices
# 4 optional ISO block device
# 5 console
# 6:0-7 additional storage devices
# Note: PCI devices can be single digit 0, or 0:0 with a limit of 8 total
# Simply add another device if 8 is not enough: 7:[0-7] etc.
case "$vm_dev_type" in
img)
if [ ! -e "${host_vmdir}/${vm_name}/${vm_name}.img" ]; then
info "$funcname: ${host_vmdir}/${vm_name}/${vm_name}.img does not exist. Skipping..."
return
else
vm_device="${host_vmdir}/${vm_name}/${vm_name}.img"
fi
;;
malloc)
if [ ! -e "/dev/$vm_device" ]; then
info "/dev/$vm_device does not exist. Skipping..."
# fix: return? continue?
return
else
vm_device="/dev/$vm_device"
fi
;;
zvol)
if [ ! -e "/dev/zvol/${host_zpool}/$vm_name" ]; then
info "/dev/zvol/${host_zpool}/$vm_name does not exist. Skipping..."
return
else
vm_device="/dev/zvol/$host_zpool/$vm_name"
fi
;;
*)
# fix: is this condition different from "malloc"? Why not set /dev/$vm_device ?
if [ ! -e "/dev/$vm_device" ]; then
info"$funcname: /dev/$vm_device does not exist. Skipping..."
return
fi
;;
esac
local console_flags=""
case "$vm_console" in
nmdm) console_flags="-s 5,lpc -l com1,/dev/nmdm${vm_id}A" ;;
*) console_flags="-s 5,lpc -l com1,stdio"
esac
local iso_flags=
[ "$ISO_BOOT" = "isobootyes" ] &&
iso_flags="-s 4,ahci-cd,${host_vmdir}/${vm_name}/${vm_name}.iso"
local device_flags=""
if [ -n "$vm_devices" ]; then
echo "Additional devices requested: $vm_devices"
# device_flags=""
dev_count=0
for device in $vm_devices; do
if [ ! -e ${device} ]; then
echo "$device not found. Skipping..."
else
device_flags="$device_flags -s 6:${dev_count},${virtio_type},${device}"
dev_count=$((dev_count+1))
fi
done
fi
# MULTIPLE NIC ACROBATICS:
# vm3 has a default NIC of tap8030 NIC on PCI 3:0, tap8031 is NIC 1 on PCI 3:1
# WHY 8 you might ask? ifconfig can't take "tap020" so we use "tap8020".
# 8 represents infinity... The middle digits represent the VM ID in the tap dev
info "$funcname: Starting VM Networking. \"File exists\" warnings are okay."
# Recall that the bridge was created with f_netstart
tap_prefix=8
tap_base_id=$vm_id
tap_id="$(printf "%02i" "$tap_base_id")" # vm2 "2" -> 02 for tap8020
tap_unit=0 #Additional vtnet PCI device and tap device suffix
### WE REALLY SHOULD CHECK FOR THESE BEFORE ADDING THEM ###
# and not say "File exists" warnings are okay...
# Initialize the first VM NIC
echo "Running: ifconfig tap${tap_prefix}${tap_id}${tap_unit} create"
ifconfig tap${tap_prefix}${tap_id}${tap_unit} create
nic_flags="-s 3:${tap_unit},virtio-net,tap${tap_prefix}${tap_id}${tap_unit}"
echo "Running: ifconfig bridge${host_bridge} addm tap${tap_prefix}${tap_id}${tap_unit} up"
ifconfig bridge${host_bridge} addm tap${tap_prefix}${tap_id}${tap_unit} up
# Check for additional requested VM NICs and create
if [ ${vm_addtl_nics:-0} -gt 0 ]; then # if gt "" or "0" additional nics
echo "$vm_addtl_nics additional virtual NICs requested"
while [ $tap_unit -lt $vm_addtl_nics ]; do # start at unit 0
tap_unit=$((tap_unit+1)) # increment it
nic_flags="$nic_flags -s 3:${tap_unit},virtio-net,tap${tap_prefix}${tap_id}${tap_unit}"
# Informational info or echo commands are not working here because of the loop?
echo "Running: ifconfig tap${tap_prefix}${tap_id}${tap_unit} create"
ifconfig tap${tap_prefix}${tap_id}${tap_unit} create
echo "Running: ifconfig bridge${host_bridge} addm tap${tap_prefix}${tap_id}${tap_unit} up"
ifconfig bridge${host_bridge} addm tap${tap_prefix}${tap_id}${tap_unit} up
done
fi
# Experimental: Run a preflight script to set up devices, bridges etc.
echo "Checking for preflight script ${host_vmdir}/${vm_name}/${vm_name}.
preflight.sh"
if [ -f "${host_vmdir}/${vm_name}/${vm_name}.preflight.sh" ]; then
echo "Running ${vm_name}.preflight.sh script"
sh ${host_vmdir}/${vm_name}/${vm_name}.preflight.sh
echo "Done running ${vm_name}.preflight.sh script"
echo
fi
# Hypothetical usage of the preflight script:
# 1. Create malloc devices for use as the boot or additional devices
# 2. Create an additional bridge and reassign the tapXXXX devices to it
# fix: note: NO indenting for attractive bhyve command output
# fix: Any way to remove double spaces?
# THE ACTUAL BHYVE COMMAND #
bhyve_cmd="/usr/sbin/bhyve \
-c "$vm_cpus" \
-m "$vm_ram" -A -H \
"$bhyve_flags" \
-s "0,${vm_hostbridge}hostbridge" \
-s "2,$virtio_type,$vm_device" \
"$nic_flags" \
"$iso_flags" \
"$console_flags" \
"$device_flags" \
"$vm_name"
"
# Was -s "3,virtio-net,tap$vm_id" \ before multiple NIC syntax
# fix: Hint: ps -ww is your friend when debugging tmux command execution
echo
info "$funcname: Running the bhyveload command:\n%s" \
"$bhyveload_cmd"
local timestamp=$( f_timestamp )
echo "$timestamp $bhyve_cmd" >> ${host_vmdir}/${vm_name}/${vm_name}.log
case "$vm_console" in
stdio)
# fix: clean up is run upon bhyve exit:
eval "$bhyve_cmd"
f_stop
;;
nmdm)
if ! kldstat | grep -qwF nmdm.ko; then
info "$funcname: %s is not loaded. Loading..." \
"nmdm.ko kernel module"
kldload nmdm.ko || die
else
info "$funcname: nmdm.ko is loaded."
fi
info "$funcname: Booting %s on console /dev/nmdm%uA" \
"$vm_name" "$vm_id"
# eval "$bhyve_cmd" &
( trap "f_stop" EXIT; eval "$bhyve_cmd" ) &
# echo "$bhyve_cmd" > "${host_vmdir}/${vm_name}/output.log"
# eval "$bhyve_cmd" > /dev/null 2>&1 # Or direct to a logfile
;;
tmux)
info "$funcname: Remember to destroy %s after shutdown\n" \
"$vm_name"
/usr/local/bin/tmux new -s "$vm_name" "eval $bhyve_cmd"
;;
tmux-detached)
### It is tricky to destroy the VM after shutdown when attached to tmux ###
# /usr/local/bin/tmux new -d -s "$vm_name" "eval $bhyve_cmd"
# This variation will no destroy the VM after VM reboot
/usr/local/bin/tmux new -d -s "$vm_name" "eval $bhyve_cmd ; bhyvectl --destroy --vm=$vm_name "
#tmux new "sh -c '( trap \"touch /tmp/FOO\" EXIT; vi )'"
# This variation attempts to destroy the VM after reboot (needs attention)
# /usr/local/bin/tmux new -d -s $vm_name "( trap \"f_destroy\" EXIT; eval \"$bhyve_cmd\" )"
# fix: preferred syntax but fails: tmux loses the f_destroy function:
# /usr/local/bin/tmux new -d -s "$vm_name" "sh -c ( trap f_destroy EXIT; eval $bhyve_cmd )"
# /usr/local/bin/tmux new -d -s "$vm_name" "( trap f_destroy EXIT; eval "$bhyve_cmd" )"
info "$funcname: Remember to stop %s after shutdown\n" \
"$vm_name"
info "\nAttach to %s with: tmux attach-session -t %s" \
"$vm_name" "$vm_name"
info "HINT: Ctrl-b d to detach from it"
;;
*)
info "Console not defined. Skipping..."
return
esac
}
# f_stop
#
# Stop bhyve instance associated with $vm_name (argument to script).
#
f_stop()
{
local funcname=f_stop
info "Entering $funcname()"
info "$funcname: sending ACPI shutdown to %s" "$vm_name"
/bin/pkill -f $vm_name
info "$funcname: Destroying $vm_name"
/usr/sbin/bhyvectl --destroy --vm="$vm_name" > /dev/null 2>&1
info "%s destroyed with bhyvectl --destroy" "$vm_name"
# fix: Note that this now supports multiple tap devices and we should
# remove tap members from the bridge first
# info "$funcname: Destroying %s" "tap$vm_id"
# ifconfig "tap$vm_id" destroy
# f_readconfig_return
. "$host_vmdir/$vm_name/${vm_name}.conf"
case "$vm_console" in
stdio)
return
;;
nmdm)
info "Terminating any cu processes associated with $vm_name (hit enter)"
ps axopid,ucomm,command | awk -v ucomm=cu -v find=nmdm${vm_id}B '
$2 == ucomm {
pid = $1
sub("^[" FS "]*[^" FS "]+", "")
sub("^[" FS "]*[^" FS "]+", "")
sub("^[" FS "]*[^" FS "]+", "")
if ( $0 ~ find ) print pid
}' | xargs kill
# fix: the above does not result in a prompt. Requires <enter>
;;
tmux*) # fix: was tmux|tmux-detached. Wildcard works?
tmux list-sessions | grep -qwF "$vm_name" || die \
"$funcname: %s Exiting." \
"tmux session $vm_name is not running!"
info "$funcname: Destroying the associated tmux session"
tmux kill-session -t "$vm_name"
;;
*)
return
esac
}
# f_destroy
#
# Attempt to destroy the bhyve instance associated with $vm_name (argument to
# script).
#
f_destroy()
{
local funcname=f_destroy
info "Entering $funcname()"
if [ ! -e "/dev/vmm/$vm_name" ]; then
info "$funcname: $vm_name is not loaded. Skipping..."
# no return as to clean up detached consoles
else
info "$funcname: sending pkill -9 -f to $vm_name"
/bin/pkill -9 -f $vm_name
# fix: perform a wait?
info "$funcname: Destroying $vm_name"
/usr/sbin/bhyvectl --destroy --vm="$vm_name" > /dev/null 2>&1
info "%s destroyed with bhyvectl --destroy" "$vm_name"
# fix: remove tap members from the bridge, then destroy. See above
# info "$funcname: Destroying %s" "tap$vm_id"
# fix: disabled to stop lag after destroy
# ifconfig "tap$vm_id" destroy
fi
info "$funcname: Cleaning up detached consoles..."
# fix: Check for the config file first
# f_readconfig_return
. "$host_vmdir/$vm_name/${vm_name}.conf"
case "$vm_console" in
stdio)
# fix: do I want 'return'?
return
;;
nmdm)
info "Terminating any cu processes associated with $vm_name (hit enter)"
ps axopid,ucomm,command | awk -v ucomm=cu -v find=nmdm${vm_id}B '
$2 == ucomm {
pid = $1
sub("^[" FS "]*[^" FS "]+", "")
sub("^[" FS "]*[^" FS "]+", "")
sub("^[" FS "]*[^" FS "]+", "")
if ( $0 ~ find ) print pid
}' | xargs kill
# return
# info "Removing /dev/nmdm${vm_id}A and /dev/nmdm${vm_id}B"
# rm "/dev/nmdm${vm_id}A"
# rm "/dev/nmdm${vm_id}B"
;;
tmux|tmux-detached)
tmux list-sessions | grep -qwF "$vm_name" || die \
"$funcname: %s Exiting." \
"tmux session $vm_name is not running!"
info "$funcname: Destroying the associated tmux session"
tmux kill-session -t "$vm_name"
;;
*)
return
esac
}
# f_attach
#
# Attach to $vm_name in the desired fashion defined by $vm_console.
#
f_attach()
{
local funcname=f_attach
info "Entering $funcname()"
# fix: update to "return style?
vm_name="$vm_names"
vm_id="${vm_name#${vm_name%[0-9]*}}"
[ -e "/dev/vmm/$vm_name" ] ||
die "$funcname: %s is not loaded! Exiting." "$vm_name"
# f_readconfig_exit
. "$host_vmdir/$vm_name/${vm_name}.conf"
case "$vm_console" in
stdio)
die "%s\n%s" \
"Something must have gone wrong if you cannot see the" \
"VM in the console you launched it on. Exiting."
;;
nmdm)
[ -e "/dev/nmdm${vm_id}B" ] || die "$funcname: nmdm device not found"
info "Remember you can detach with \" ~ CTRL-d \""
/usr/bin/cu -l "/dev/nmdm${vm_id}B" -s 9600
;;
tmux|tmux-detached)
# fix: Check here if the session in fact exists?
info "Remember you can detach with \" CTRL-b d \""
tmux attach-session -t "$vm_name"
;;
*)
die "vm_console not defined. Exiting."
esac
}
# f_iso
#
# Create a storage device, fetch an installation ISO and boot to that ISO.
#
f_iso()
{
local funcname=f_iso
info "Entering $funcname()"
vm_name=$vm_names
# vm_id="${vm_name#${vm_name%[0-9]*}}"
# f_readconfig_exit
. "$host_vmdir/$vm_name/${vm_name}.conf"
case "$vm_dev_type" in
img)
if [ -e "${host_vmdir}/${vm_name}/${vm_name}.img" ]; then
info "$funcname: ${host_vmdir}/${vm_name}/${vm_name}.img found."
vm_device="${host_vmdir}/${vm_name}/${vm_name}.img"
else
info "$funcname: ${host_vmdir}/${vm_name}/${vm_name}.img does not exist. Formatting..."
f_format
vm_device="${host_vmdir}/${vm_name}/${vm_name}.img"
fi ;;
zvol)
if [ -e "/dev/zvol/${host_zpool}/$vm_name" ]; then
info "/dev/zvol/${host_zpool}/$vm_name found."
vm_device="/dev/zvol/$host_zpool/$vm_name"
else
info "$funcname: /dev/zvol/$host_zpool/$vm_name does not exist. Formatting..."
f_format
vm_device="/dev/zvol/$host_zpool/$vm_name"
fi ;;
*)
if [ ! -e "$vm_device" ]; then
info "$vm_device does not exist. Skipping..."
return # otherwise we stay with the user-defined vm_device
fi
esac
# [ -e "${host_vmdir}/${vm_name}/${vm_name}.img" ] && die "\n%s\n%s Exiting." \
# "$funcname: ${host_vmdir}/${vm_name}/${vm_name}.img already exists!" \
# "$funcname: \"delete\" it first."
# f_readconfig_exit
info "$funcname: Checking for ${host_distdir}/$vm_os_ver/"
if [ ! -d "${host_distdir}/$vm_os_ver/" ]; then
info "$funcname: \"%s\" does not exist. Creating it..." \
"${host_distdir}/$vm_os_ver/"
mkdir -p "${host_distdir}/$vm_os_ver/" || die
fi
info "$funcname: Checking for ${host_vmdir}/${vm_name}/${vm_name}.iso"
if [ ! -f "${host_vmdir}/${vm_name}/${vm_name}.iso" ]; then
if [ ! -f "${host_distdir}/$vm_os_ver/$iso_img" ]; then
info "$funcname: Fetching %s" "$iso_img"
fetch "$iso_site/$iso_img" -o "${host_distdir}/$vm_os_ver/" || die
info "$funcname: Copying %s to %s" "$iso_img" "${host_vmdir}/${vm_name}/${vm_name}.iso"
fi
[ -f "${host_distdir}/$vm_os_ver/$iso_img" ] ||
die "$funcname: %s did not fetch! Exiting." "$iso_img"
info "Copying %s to %s" "$iso_img" "${host_vmdir}/${vm_name}/${vm_name}.iso"
ln -sf "${host_distdir}/$vm_os_ver/$iso_img" "${host_vmdir}/${vm_name}/${vm_name}.iso"
[ -f "${host_vmdir}/${vm_name}/${vm_name}.iso" ] ||
die "$funcname: %s did not copy! Exiting." "${host_vmdir}/${vm_name}/${vm_name}.iso"
fi
ISO_BOOT="isobootyes" # Setting to inform f_boot() to use the ISO
# fix: This maybe should be normal f_load with and ISO_BOOT check
# [ "$ISO_BOOT" = "isobootyes" ] && ... in f_load
# key point: $vm_device is set to the ISO image
if [ -e "/dev/vmm/$vm_name" ]; then
info "$funcname: $vm_name is already loaded"
else
if [ "$vm_os_type" = "freebsd" ]; then
info "$funcname: WARNING! This will not set the serial tty!"
bhyveload_cmd="/usr/sbin/bhyveload -m $vm_ram -d \
${host_vmdir}/${vm_name}/${vm_name}.iso $vm_name"
info "$funcname: Running the bhyveload command:\n%s" \
"$bhyveload_cmd"
local timestamp=$( f_timestamp )
echo "$timestamp $bhyveload_cmd" >> ${host_vmdir}/${vm_name}/${vm_name}.log
eval "$bhyveload_cmd"
else
info "$funcname: Creating ${host_vmdir}/${vm_name}/device.map"
echo "(hd0) $vm_device" > "${host_vmdir}/${vm_name}/device.map"
echo "(cd0) ${host_vmdir}/${vm_name}/${vm_name}.iso" >> "${host_vmdir}/${vm_name}/device.map"
cat "${host_vmdir}/${vm_name}/device.map"
info "$funcname: Running the ISO grub command:\n%s" \
"$grub_iso_cmd"
local timestamp=$( f_timestamp )
echo "$timestamp $grub_iso_cmd" >> ${host_vmdir}/${vm_name}/${vm_name}.log
eval "$grub_iso_cmd"
fi
fi
info "funcname: Calling f_boot"
f_boot
}
# f_grub
#
# Configure device map and execute grub-bhyve command.
#
f_grub()
{
local funcname=f_grub
info "Entering $funcname()"
vm_name=$vm_names
# f_readconfig_exit
. "$host_vmdir/$vm_name/${vm_name}.conf"
info "$funcname: Creating ${host_vmdir}/${vm_name}/device.map"
echo "(hd0) ${host_vmdir}/${vm_name}/${vm_name}.img" > "${host_vmdir}/${vm_name}/device.map"
echo "(cd0) ${host_vmdir}/${vm_name}/${vm_name}.iso" >> "${host_vmdir}/${vm_name}/device.map"
cat "${host_vmdir}/${vm_name}/device.map"
grub_cmd="/usr/local/sbin/grub-bhyve -m "${host_vmdir}/${vm_name}/device.map" -M "$vm_ram" "$vm_name""
info "$funcname: Running the bhyveload command:\n%s" \
"$grub_cmd"
local timestamp=$( f_timestamp )
echo "$timestamp $grub_cmd" >> ${host_vmdir}/${vm_name}/${vm_name}.log
eval "$grub_cmd"
}
# f_mount
#
# Mount the vm_device associated with $vm_name. Optionally performs
# fsck_ufs(8) (FreeBSD only). Causes premature termination with error message
# if already mounted or if missing any required devices or image files.
#
f_mount()
{
local funcname=f_mount
local devs
vm_name=$vm_names
# f_readconfig_exit
. "$host_vmdir/$vm_name/${vm_name}.conf"
[ "$vm_os_type" = "freebsd" ] ||
die "$funcname: Only supported for FreeBSD VMs"
f_umount
info "Entering $funcname()"
info "$funcname: Checking if %s is loaded" "$vm_name"
[ -e "/dev/vmm/$vm_name" ] &&
die "$funcname: %s is loaded! Exiting." "$vm_name"
case "$vm_dev_type" in
img|malloc)
info "$funcname: Checking if raw disk %s is mounted?" "${host_vmdir}/${vm_name}/${vm_name}.img"
[ -f "${host_vmdir}/${vm_name}/${vm_name}.img" ] ||
die "$funcname: %s does not exist! Exiting" "${host_vmdir}/${vm_name}/${vm_name}.img"
info "$funcname: checking for attached device..."
devs=$( f_getmdname )
[ "$devs" ] && die "$funcname: %s\n$funcname: %s" \
"$vm_name: already attached to $devs" "Exiting."
info "$funcname: Checking for %s" "${host_vmdir}/${vm_name}/${vm_name}.img"
[ -e "${host_vmdir}/${vm_name}/${vm_name}.img" ] ||
die "$funcname: %s does not exist! Exiting." "${host_vmdir}/${vm_name}/${vm_name}.img"
info "$funcname: Attaching to %s with mdconfig(8)" "${host_vmdir}/${vm_name}/${vm_name}.img"
vm_device="/dev/$( mdconfig -af "${host_vmdir}/${vm_name}/${vm_name}.img" )" || die
info "$funcname: Checking for device /dev/%s" "$vm_device"
;;
zvol)
if [ ! -e "/dev/zvol/${host_zpool}/$vm_name" ]; then
info "/dev/zvol/${host_zpool}/$vm_name already exists! \"destroy\" it first. Exiting."
die
else
vm_device="/dev/zvol/$host_zpool/$vm_name"
fi
;;
*)
if [ ! -e "$vm_device" ]; then
die "$funcname: vm_device %s does not exist! Exiting." \
"$vm_device"
fi
;;
esac
case "$vm_dev_layout" in
# fix: Why is Mm... failing?
# [Mm][Bb][Rr])
mbr)
vol_part="${vm_device}s1a" ;;
# Gg][Pp][Tt])
gpt)
vol_part="${vm_device}p2" ;;
*)
die "Invalid VM device layout"
esac
info "$funcname: Running fsck_ufs -y %s" "$vol_part"
fsck_ufs -y "$vol_part"
if [ ! -d "${host_vmdir}/${vm_name}/mnt" ]; then
info "$funcname: Creating %s directory" "${host_vmdir}/${vm_name}/mnt"
mkdir -p "${host_vmdir}/${vm_name}/mnt" || die
fi
# fix: CHECK FOR UFS OR ZFS HERE, MOUNT POOL AS APPRPRIATE
info "$funcname: Running mount %s %s" "$vol_part" "${host_vmdir}/${vm_name}/mnt/"
mount "$vol_part" "${host_vmdir}/${vm_name}/mnt/"
}
# f_umount
#
# Unmount the vm_device associated with $vm_name. Causes premature termination
# with error message if not mounted.
#
f_umount()