forked from michaeldexter/vmrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkvm.sh.functions
executable file
·1070 lines (905 loc) · 24.1 KB
/
mkvm.sh.functions
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 <[email protected]>
#
# 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: make VM script function library
# Version: v.0.8.8
#
# Functions used by the mkvm.sh Virtual Machine provisioning script
#
################################################################## USAGE
f_timestamp() # A simple timestamp function
{
echo $( date "+%Y%m%d-%H%M%S" )
}
f_filetype() # A smarter file(1) that briefer than brief mode
{
case $( file -bi $1 ) in
"application/x-bzip2; charset=binary") echo .bz2
;;
"application/x-compress; charset=binary") echo .Z
;;
"application/x-gzip; charset=binary") echo .gz
;;
"application/x-xz; charset=binary") echo .xz
;;
"application/x-zip; charset=binary") echo .zip
;;
*) echo "Uncompressed" # This might want to be a return value
# FYI images are application/octet-stream; charset=binary
esac
}
# md(4) functions from vm and may be useful after mid-provision exits
f_getmdname() # vm_name # Retrieve md(4) IDs associated with $1 ($vm_name)
{
# mdconfig -lv | awk -v vm_name="$1" \
# '$0 ~ "^[^/]*/" vm_name "\.img$" { print $1 }'
local local_img_name=$1 # 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() # $1 ($vm_name) Destroy multiple md(4) IDs of $vm_name
{
echo Destroying all memory devices associated with $1
for dev in $( f_getmdname $1 ); do
echo Destroying mdconfig device $dev
mdconfig -du "$dev"
done
}
f_wipe_dev() ## $1 dev ## - Wipe everything possible from a device
{
# NOTE /dev/label/
# CONSIDER SWAP OFF AND JOURNAL OFF, pool membership?
# PC-BSD
# for i in `swapctl -l | grep "$DISK | awk '{print $1}'`
# do
# swapoff ${i} >/dev/null 2>/dev/null
# done
# gjournal stop -f ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
# gjournal clear ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
# Destroy pools
# Destroy nop's
# Delete partitions 1 2 3 4... before destroy
# gpart delete -i n $1
#swapoff ${1}p1 # FreeNAS
geli detach $1
gpart destroy -F $1
graid delete $1
zpool labelclear -f $1
gpart create -s gpt $1
gpart destroy -F $1
dd if=/dev/zero of=$1 bs=1m count=1 # 1048576 bytes
#dd if=/dev/zero of=$1 count=3000 # 1536000 bytes
#dd if=/dev/zero of=/dev/md0 bs=512 count=34 # 17408 bytes
#dd if=/dev/zero of=$f_device bs=512 count=1
dd if=/dev/zero of=${1} bs=1m oseek=`diskinfo ${1} | awk '{print int($3 / (1024*1024)) - 4;}'`
}
# PARTITIONING
# Optional data partitions would require sizes for all
# Verify if a device has aligned gpart partitions
f_check_gpt_alignment() ## $1 dev ##
{
exitcode="0" # Mathematical answer that doubles as a return value
for alignment in $( gpart backup $1 | tail -n +2 | cut -d " " -f 3 ); do
result=$( echo "$alignment % 8" | bc )
exitcode=$(( $exitcode+$result )) # Add any non-zero results to total
done
return $exitcode
}
# Create a traditional fdisk/MBR layout
f_fdisk_mbr_layout_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_fdisk_mbr_layout() ## $1 dev ##
{
fdisk -BI $1
}
f_fdisk_mbr_layout_debug() ## $1 dev ##
{
echo
echo Running ls on the $1 asterisk
ls ${1}*
# /dev/md0s1
# test: file diska.img: x86 boot sector; partition 1: ID=0xa5, active, starthead 1, startsector 63, 2088387 sectors, code offset 0x31
# END RESULT: /dev/md0s1
# 512/4096 is not an issue at this stage!
}
# Create a gpart/MBR layout
f_gpart_mbr_layout_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_mbr_layout() ## $1 dev ##
{
gpart create -s mbr $1
gpart add -t freebsd $1
gpart create -s bsd ${1}s1 # DO NOT DELETE AND RECREATE WILL FAIL
}
# Create a gpart/GPT layout
f_gpart_gpt_layout_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_gpt_layout() ## $1 dev ##
{
echo Creating Layout
gpart create -s gpt $1
}
f_gpart_gpt_layout_debug() ## $1 dev ##
{
echo
echo Running ls on $1 asterisk
# file <disk image>
ls ${1}*
echo
echo Running gpart show $1
gpart show $1
}
# Install boot code to an fdisk/MBR layout
f_fdisk_mbr_ufs_boot_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_fdisk_mbr_ufs_boot() ## $1 dev ##
{
bsdlabel -wB ${1}s1
# creates /dev/md0s1a surprisingly. why?
}
f_fdisk_mbr_ufs_boot_debug() ## $1 dev ##
{
echo Running ls $1
ls $1
# Should this have created /dev/mdNs1a ?
}
# Install the boot0 boot manager to an fdisk/MBR layout
# Will this only give the menu if there are multiple slices/drives?
f_fdisk_mbr_ufs_bootmgr_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_fdisk_mbr_ufs_bootmgr() ## $1 dev ## Same as fdisk -B -b /boot/boot0
{
bsdlabel -w -B -b /boot/boot0 ${1}s1
# creates /dev/md0s1a surprisingly why?
}
f_fdisk_mbr_ufs_bootmgr_debug() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
# http://daemon-notes.com/articles/system/install-zfs/gpart
# Install boot code to a gpart/MBR/UFS layout
f_gpart_mbr_ufs_boot_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_mbr_ufs_boot() ## $1 dev ## no freebsd-boot!
{
# CHECK THESE
gpart bootcode -b /boot/mbr $1
gpart set -a active -i 1 $1
gpart bootcode -b /boot/boot ${1}s1
}
f_gpart_mbr_ufs_boot_debug() ## $1 dev ##
{
echo Running ls $1 asterisk
ls ${1}*
gpart show $1
}
# Install the boot0 boot manager to a gpart/MBR/UFS layout
f_gpart_mbr_ufs_bootmgr_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_mbr_ufs_bootmgr() ## $1 dev ##
{
gpart bootcode -b /boot/boot0 $1
gpart set -a active -i 1 $1
gpart bootcode -b /boot/boot ${1}s1 # mgr HERE TOO? (see w block)
}
f_gpart_mbr_ufs_bootmgr_debug() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
# Install boot code to a gpart/GPT/UFS layout
# Install the boot0 boot manager to a gpart/MBR/UFS layout
f_gpart_gpt_ufs_boot_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_gpt_ufs_boot() ## $1 dev ## active by default? boot0 mgr?
{
# Note alignment. Change?
gpart add -s 512k -a 4k -t freebsd-boot $1
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 $1
}
f_gpart_gpt_ufs_boot_debug() ## $1 dev ##
{
echo Running gpart show $1
gpart show $1
}
# Install the boot0 boot manager to a gpart/GPT/UFS layout
f_gpart_gpt_ufs_bootmgr_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_gpt_ufs_bootmgr() ## $1 dev ## active by default? boot0 mgr?
{
# Note alignment
# IMPLEMENT
gpart add -s 512k -a 4k -t freebsd-boot $1
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 $1
}
f_gpart_gpt_ufs_bootmgr_debug() ## $1 dev ##
{
echo Simple debug output
echo
}
# Install boot code to a gpart/MBR/ZFS layout
f_gpart_mbr_zfs_boot_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_mbr_zfs_boot() ## $1 dev ##
{
echo Adding Boot Code
gpart bootcode -b /boot/mbr $1
#CHECK THIS ONE:
gpart set -a active -i 1 $1
gpart bootcode -b /boot/boot ${1}s1
dd if=/boot/zfsboot of=${1}s1 count=1
###### !!! ######
# Probably too early! Needed?
# dd if=/boot/zfsboot of=${1}s1a skip=1 seek=1024
}
f_gpart_mbr_zfs_boot_debug() ## $1 dev ##
{
echo Running ls and gpart show on $1
#file 2gb.img
ls ${1}*
gpart show $1
gpart show ${1}s1
gpart show ${1}s1a
}
# Install the boot0 boot manager to a gpart/MBR/ZFS layout
f_gpart_mbr_zfs_bootmgr_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_mbr_zfs_bootmgr() ## $1 dev ##
{
gpart bootcode -b /boot/boot0 $1
#CHECK THIS ONE:
gpart set -a active -i 1 $1
gpart bootcode -b /boot/boot ${1}s1
dd if=/boot/zfsboot of=${1}s1 count=1
# Probably too early!
dd if=/boot/zfsboot of=${1}s1a skip=1 seek=1024
}
f_gpart_mbr_zfs_bootmgr_debug() ## $1 dev ##
{
echo Simple debug output
echo
}
# Install boot code to a gpart/GPT/ZFS layout - active by default? boot0 mgr?
f_gpart_gpt_zfs_boot_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_gpt_zfs_boot() ## $1 dev ##
{
# Note alignment
# CHECK
gpart add -s 512k -a 4k -t freebsd-boot $1
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 $1
}
f_gpart_gpt_zfs_boot_debug() ## $1 dev ##
{
echo Running ls and gpart show on $1
#file 2gb.img
ls ${1}*
gpart show $1
gpart show ${1}p1
#gpart show ${1}p2
}
# Install the boot0 boot manager to a gpart/GPT/ZFS layout
f_gpart_gpt_zfs_bootmgr_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_gpt_zfs_bootmgr() ## $1 dev ## active by default? boot0 mgr?
{
# Note alignment
# CHECK THIS
gpart add -s 512k -a 4k -t freebsd-boot $1
gpart bootcode -b /boot/pmbr -p /boot/gpt_zfsboot -i 1 $1
}
# DO I WANT TO LABEL? UFS by label, ZFS by label...
# NOTE NEWFS -L http://www.wonkity.com/~wblock/docs/html/disksetup.html
# then disktab!!!
# FSCK! This isn't even with datasets in mind!
# md device disk labels? iscsi?
# See boot0 with bhyveload?
# SWAP!!! slice? dataset?
f_gpart_gpt_zfs_bootmgr_debug() ## $1 dev ##
{
echo Running ls on $1 asterisk
ls ${1}*
}
# Create a new fdisk/MBR/UFS bootable filesystem (better name)
f_fdisk_mbr_ufs_newfs_bootable_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_fdisk_mbr_ufs_newfs_bootable() ## $1 dev ##
{
newfs -U ${1}s1a
}
f_fdisk_mbr_ufs_newfs_bootable_debug() ## $1 dev ##
{
echo Running ls and fdisk on $1
ls ${1}*
fdisk $1
}
# Create a new fdisk/MBR/UFS data filesystem (better name)
f_fdisk_mbr_ufs_newfs_data_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_fdisk_mbr_ufs_newfs_data() ## $1 dev ##
{
newfs -U ${1}s1 # IF A DATA DISK WITH NO BOOT!
}
# Create a new gpart/MBR/UFS partition and filesystem
f_gpart_mbr_ufs_newfs_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_mbr_ufs_newfs() ## $1 dev ##
{
# Note alignment
gpart add -t freebsd-ufs -a 1m ${1}s1
newfs -U ${1}s1a
# was giving: gpart: autofill: No space left on device
}
f_gpart_mbr_ufs_newfs_debug() ## $1 dev ##
{
echo Running ls and gpart show on $1
ls ${1}*
gpart show $1
}
# Create a new gpart/GPT/UFS partition and filesystem
f_gpart_gpt_ufs_newfs_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_gpt_ufs_newfs() ## $1 dev ##
{
# Note alignment
gpart add -t freebsd-ufs -a 1m $1
newfs -U ${1}p2
}
f_gpart_gpt_ufs_newfs_debug() ## $1 dev ##
{
echo Running ls and gpart show on $1
ls ${1}*
gpart show $1
}
# Create a new gpart/MBR/ZFS partition
f_gpart_mbr_zfs_part_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_mbr_zfs_part() ## $1 dev ##
{
# Note alignment
gpart add -t freebsd-zfs -a 1m ${1}s1
}
f_gpart_mbr_zfs_part_debug() ## $1 dev ##
{
echo Running ls and gpart show on $1
ls ${1}*
gpart show $1
}
# Create a new gpart/GPT/ZFS partition
f_gpart_gpt_zfs_part_preflight() ## $1 dev ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
}
f_gpart_gpt_zfs_part() ## $1 dev ##
{
# Note alignment
gpart add -t freebsd-zfs -a 1m $1
}
f_gpart_gpt_zfs_part_debug() ## $1 dev ##
{
echo Running ls and gpart show on $1
ls ${1}*
gpart show $1
}
# add boot0 manager to zfs?
# Create a new gpart/MBR/zpool ???
# WE REALLY WANT TO MAKE SURE $DEST IS CLEAR OR ZPOOL FAILS
# Create a new gpart/GPT/zpool
f_gpart_mbr_zpool_preflight() ## $1 dev $2 pool name $3 mount point ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
[ -e $3 ] || \
{ echo Mount point $3 does not exist. Exiting ; exit 1 ; }
ls $3
echo You entered $2 and $3
}
f_gpart_mbr_zpool() ## $1 dev $2 pool name $3 mount point ##
{
#kldload zfs
# Note alignment
gnop create -S 4096 ${1}s1a
echo debug: check for nop *s1a.nop ; ls ${1}s1a*
zpool create $vm_dev_flags -o cachefile=none -R $3 -m / $2 ${1}s1a.nop
# Allan: -R $3 (no mount point, no cache file, see man page)
# -O canmount=off ? -m none -o altroot
zpool export $2
gnop destroy -f ${1}s1a.nop
# Note that it is exported and not ready for population!
}
f_gpart_mbr_zpool_debug() ## $1 dev $2 pool name $3 mount point ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
[ -e $3 ] || \
{ echo Mount point $3 does not exist. Exiting ; exit 1 ; }
ls $3
ls ${1}*
gpart show $1
zpool import
# zpool list | grep $2
# zfs list | grep $2
# echo debug: zpool list | grep $2 ; zpool list | grep $2
# echo debug: zfs list | grep $2 ; zfs list | grep $2
# needs to be imported first:
# echo debug: checking sector size ; zdb -C $pool | grep ashift
}
# Create a new gpart/GPT/zpool
f_gpart_gpt_zpool_preflight() ## $1 dev $2 pool name $3 mount point ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
[ -e $3 ] || \
{ echo Mount point $3 does not exist. Exiting ; exit 1 ; }
[ -e $1 ] || echo Mount point $3 does not exist.
ls $3
echo You entered $2 and $3
echo CreatingPool
}
f_gpart_gpt_zpool() ## $1 dev $2 pool name $3 mount point ##
{
#kldload zfs
# Note alignment
gnop create -S 4096 ${1}p2
echo inline debug: check for nop *p2.nop ; ls ${1}p2*
zpool create $vm_dev_flags -o cachefile=none -R $3 -m / $2 ${1}p2.nop
# -O canmount=off ? -m none -o altroot
zpool export $2
gnop destroy -f ${1}p2.nop
# Note that it is exported and not ready for population!
}
f_gpart_gpt_zpool_debug() ## $1 dev $2 pool name $3 mount point ##
{
echo Verifying that $1 exists
[ -e $1 ] || \
{ echo Device $1 does not exist. Exiting ; exit 1 ; }
[ -e $3 ] || \
{ echo Mount point $3 does not exist. Exiting ; exit 1 ; }
[ -e $1 ] || echo Mount point $3 does not exist.
ls $3
ls ${1}*
gpart show $1
zpool import
}
f_installset_preflight() ## $1 set name and $2 destination ##
{
if [ ! -f $1 ]; then
echo Distribution set $1 does not exist. Exiting
exit 1
fi
if [ ! -d $2 ]; then
echo Mount destination directory $2 does not exist. Exiting
exit 1
fi
# CHECK IF EMPTY. UFS will show .snap and zfs will show nothing
# if [ $( ls $2 * ) = "ls: No match." ]; then
# echo Destination directory is not empty. Exiting.
# exit
# fi
}
f_installset() ## $1 set name and $2 destination ##
{
echo Extracting distribution set $1 to $2
# cat $1 | tar xpf - -C $2
tar xpf $1 -C $2
}
f_installset_debug() ## $1 set name and $2 destination ##
{
if [ "$1" = "kernel.txz" ]; then
ls ${2}/boot/
else
ls $2
fi
}
# Configure loader.conf, takes in $1 mount point
f_config_loader_conf_preflight() ## $1 mount point ##
{
echo Backing up loader.conf if it exists
if [ -f ${1}/boot/loader.conf ]; then
local timestamp=$( f_timestamp )
cp ${1}/boot/loader.conf ${1}/boot/loader.conf.$timestamp
fi
}
f_config_loader_conf() ## $1 mount point ##
{
cat >> ${1}/boot/loader.conf <<-EOF
autoboot_delay="3"
zfs_load="YES"
# Seems fine without: vfs.root.mountfrom="zfs:$pool/ROOT/default"
EOF
}
# $1 mount point
f_config_loader_conf_debug_preflight() ## $1 mount point ##
{
echo loader_conf
}
f_config_loader_conf_debug() ## $1 mount point ##
{
cat $1/boot/loader.conf
}
# Configure a VirtIO MBR fstab, takes in $1 mount point
f_config_mbr_fstab_preflight() ## $1 mount point ##
{
echo Backing up fstab if it exists
if [ -f ${1}/etc/fstab ]; then
local timestamp=$( f_timestamp )
cp ${1}/etc/fstab ${1}/etc/fstab.$timestamp
fi
}
f_config_mbr_fstab() ## $1 mount point ##
{
cat > ${1}/etc/fstab <<-EOF
# Device Mountpoint FStype Options Dump Pass#
/dev/ada0s1a / ufs rw 1 1
EOF
}
f_config_mbr_fstab_debug() ## $1 mount point ##
{
cat $1/etc/fstab
}
# Configure a VirtIO GPT fstab, takes in $1 mount point
f_config_gpt_fstab_preflight() ## $1 mount point ##
{
echo Backing up fstab if it exists
if [ -f ${1}/etc/fstab ]; then
local timestamp=$( f_timestamp )
cp ${1}/etc/fstab ${1}/etc/fstab.$timestamp
fi
}
f_config_gpt_fstab() ## $1 mount point ##
{
cat > ${1}/etc/fstab <<-EOF
# Device Mountpoint FStype Options Dump Pass#
/dev/ada0p2 / ufs rw 1 1
EOF
}
f_config_gpt_fstab_debug() ## $1 mount point ##
{
cat $1/etc/fstab
}
f_config_zfs_fstab_preflight() ## $1 mount point ##
{
echo Backing up fstab if it exists
if [ -f ${1}/etc/fstab ]; then
local timestamp=$( f_timestamp )
cp ${1}/etc/fstab ${1}/etc/fstab.$timestamp
fi
}
f_config_zfs_fstab() ## $1 mount point ##
{
cat > ${1}/etc/fstab <<-EOF
# Device Mountpoint FStype Options Dump Pass#
EOF
}
f_config_zfs_fstab_debug() ## $1 mount point ##
{
cat $1/etc/fstab
}
# Configure bhyve-friendly /etc/ttys, takes in $1 vm_name (NOT vm_mountpoint)
f_config_ttys_preflight() ## $1 mount point ##
{
echo
echo Verifying that VM is mounted on its mount point
( mount | grep -qw $1/mnt ) || \
{ echo $1 is not mounted. Exiting ; exit 1 ; }
echo
echo Verifying that $1/etc/ttys exists
[ -f $1/etc/ttys ] && \
{ echo $1/etc/ttys does not exist. Exiting ; exit 1 ; }
}
f_config_ttys() ## $1 mount point ##
{
echo
echo Determining if a NanoBSD or generic layout
if [ -f $1/etc/diskless ]; then
echo
echo Assuming a NanoBSD layout
echo
echo Backing up $1/conf/base/etc/ttys
local timestamp=$( f_timestamp )
cp $1/conf/base/etc/ttys $1/conf/base/etc/ttys.orig.$timestamp
echo
echo Verifying if FreeNAS or generic NanoBSD
if [ ! $( grep -qw freenas $1/conf/base/etc/ttys ) ]; then
echo
echo setting ttyu0 to vt100 for FreeNAS
cat >> $1/conf/base/etc/ttys <<-EOF
ttyu0 "/usr/libexec/getty freenas" vt100 on secure
EOF
else
echo Exiting! No sure why it chokes on the cat
exit 1
# BUG: With the cat, it reports "expecting fi" at the end of the function
echo
echo Appending the bhyve tty
#cat >> $1/conf/base/etc/ttys <<-EOF
#ttyu0 "/usr/libexec/getty 3wire.9600" vt100 on secure
#EOF
fi # Done with NanoBSD
else # Not NanoBSD
echo
echo Backing up $1/etc/ttys
# it passes if the nested if is removed
local timestamp=$( f_timestamp )
cp $1/etc/ttys $1/etc/ttys.orig.$timestamp
echo
echo Appending the bhyve tty
cat >> $1/etc/ttys <<-EOF
#console "/usr/libexec/getty std.9600" vt100 on secure
ttyu0 "/usr/libexec/getty 3wire.9600" vt100 on secure
EOF
fi
}
# tty Notes:
# We assume a serial-ready pfSense download!
# Tried for FreeNAS: sed -i '' -e "s/cons25/vt100/" $1/etc/ttys
# ttyv0 "/usr/libexec/getty freenas" cons25 on secure
# console "/usr/libexec/getty freenas" vt100 on secure
# ttyu0 "/usr/libexec/getty freenas" vt100 on secure
f_config_ttys_debug() ## $1 mount point ##
{
if [ -f $1/conf/base/etc/ttys ]; then
echo
echo tailing $1/conf/base/etc/ttys
tail $1/conf/base/etc/ttys
else
echo
echo $1/etc/ttys
tail $1/etc/ttys
fi
}
# Configure timezone
f_config_tz_preflight() ## $1 mount point, $2 time zone ##
{
echo Creating /var/db/zoneinfo and running tzsetup
}
f_config_tz() ## $1 mount point, $2 time zone ##
{
echo $2 >> $1/var/db/zoneinfo
tzsetup -r -C ${1}/
}
f_config_tz_debug() ## $1 mount point, $2 time zone ##
{
echo cat $1/var/db/timezone
}
# Configure a bhyve-friendly rc.conf
f_config_rc_conf_preflight() ## $1 mount point ##
{
echo Backing up rc.conf if it exists
if [ -f ${1}/etc/rc.conf ]; then
local timestamp=$( f_timestamp )
cp ${1}/etc/rc.conf ${1}/etc/rc.conf.$timestamp
fi
}
f_config_rc_conf() ## $1 mount point $2 hostname $3 IP $4 GW ##
{
if [ ! "$3" = "" ]; then # if an IP is set
cat > ${1}/etc/rc.conf <<-EOF
hostname="$2"
ifconfig_vtnet0="$3 netmask 255.255.255.0"
defaultrouter="$4"
sshd_enable="YES"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"
EOF
else
cat > ${1}/etc/rc.conf <<-EOF
hostname="$2"
ifconfig_vtnet0="DHCP"
sshd_enable="YES"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"
EOF
fi
}
f_config_rc_conf_debug() ## $1 mount point ##
{
cat ${1}/etc/rc.conf
}
# Configure resolv.conf
f_config_resolv_conf_preflight() ## $1 mount point ##
{
echo Backing up rc.conf if it exists
if [ -f ${1}/etc/resolv.conf ]; then
local timestamp=$( f_timestamp )
cp ${1}/etc/resolv.conf ${1}/etc/resolv.conf.$timestamp
fi
}
f_config_resolv_conf() ## $1 mount point $2 searchdomain $3 nameserver ##
{
cat > ${1}/etc/resolv.conf <<-EOF
search $2
nameserver $3
EOF
}
f_config_resolv_conf_debug() ## $1 mount point ##
{
cat ${1}/etc/resolv.conf
}
# Configure sshd root login
f_config_sshd_root_preflight() ## $1 mount point ##
{
echo sshd_root_enable
}
f_config_sshd_root() ## $1 mount point ##
{
echo PermitRootLogin yes >> ${1}/etc/ssh/sshd_config
}
f_config_sshd_root_debug() ## $1 mount point ##
{
grep Root $1/etc/ssh/sshd_config
}
f_set_password_preflight() ## ##
{
echo set_password_pre
}
f_set_password() ## $1 password $2 mount point ##
{