-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmkvm.sh
executable file
·1025 lines (859 loc) · 33.9 KB
/
mkvm.sh
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-2016 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
# Version: v.0.9.7
#
# Script to provision FreeBSD Virtual Machines for use with vmrc
#
################################################################## USAGE
#
# Usage:
#
# Interactive: sh mkvm.sh
#
# Non-interactive: sh mkvm.sh <template name> <vm name> (sans id)
#
########################################################################
### PREFLIGHT CHECKS ###
# suid=$( id -u ) ; [ "$suid" = "0" ] || \
[ `id -u` -ne 0 ] && \
{ echo Must be excuted with root privileges. Exiting ; exit 1 ; }
[ -z ${2##*[!0-9]} ] || \
{ echo The requested VM name $2 cannot end in a number. Exiting ; exit 1 ; }
[ -f /usr/local/etc/vmrc.conf ] || \
{ echo /usr/local/etc/vmrc.conf does not exist. Exiting ; exit 1 ; }
[ -r /usr/local/etc/vmrc.conf ] || \
{ echo /usr/local/etc/vmrc.conf is not readable. Exiting ; exit 1 ; }
/bin/sh -n /usr/local/etc/vmrc.conf >/dev/null 2>&1 || \
{ echo /usr/local/etc/vmrc.conf failed to parse. Exiting ; exit 1 ; }
echo ; echo Reading /usr/local/etc/vmrc.conf
. /usr/local/etc/vmrc.conf >/dev/null 2>&1 || \
{ echo vmrc.conf failed to source. Exiting ; exit 1 ; }
# Verify that the paths are set
[ $host_templates ] || \
{ echo host_templates is not set in vmrc.conf. Exiting ; exit 1 ; }
[ $host_distdir ] || \
{ echo host_distdir is not set in vmrc.conf. Exiting ; exit 1 ; }
[ $host_vmdir ] || \
{ echo host_vmdir is not set in vmrc.conf. Exiting ; exit 1 ; }
# Verify that the paths begin and end with "/"
[ "${host_templates%${host_templates#?}}" = "/" ] ||
{ echo host_templates must begin with \"/\". Exiting ; exit 1 ; }
[ "${host_templates#${host_templates%?}}" = "/" ] ||
{ echo host_templates must end with \"/\". Exiting ; exit 1 ; }
[ "${host_distdir%${host_distdir#?}}" = "/" ] ||
{ echo host_distdir must begin with \"/\". Exiting ; exit 1 ; }
[ "${host_distdir#${host_distdir%?}}" = "/" ] ||
{ echo host_distdir must end with \"/\". Exiting ; exit 1 ; }
[ "${host_vmdir%${host_vmdir#?}}" = "/" ] ||
{ echo host_vmdir must begin with \"/\". Exiting ; exit 1 ; }
[ "${host_vmdir#${host_vmdir%?}}" = "/" ] ||
{ echo host_vmdir must end with \"/\". Exiting ; exit 1 ; }
echo echo Checking for required functions library
[ -f ./mkvm.sh.functions ] || \
{ echo ./mkvm.sh.functions does not exist. Exiting ; exit 1 ; }
echo Checking if required functions library is readable
[ -r ./vmrc.conf ] || \
{ echo ./mkvm.sh.functions is not readable. Exiting ; exit 1 ; }
echo Running sh n on required functions library
/bin/sh -n ./mkvm.sh.functions >/dev/null 2>&1 || \
{ echo ./mkvm.sh.functions failed to parse. Exiting ; exit 1 ; }
echo Reading ./mkvm.sh.functions
. ./mkvm.sh.functions >/dev/null 2>&1 || \
{ echo ./mkvm.sh.functions failed to source. Exiting ; exit 1 ; }
### FUNCTIONS ###
# NB! Is this triple digit safe?
f_getnextid() ## $1 host_vmdir from /usr/local/etc/vmrc.conf
{
local nextid=0 # Initialize to 0
local conflict=""
if [ $( ls $1 | wc -l ) = 0 ]; then # None exist. A better test?
# Note -d check in vm_start
echo $nextid # Use the initialized zero
exit 0
else
while :; do # Loop until satisfied - risky?
for vm_found in ${1}/* ; do # Full path name
vm_foundname="${vm_found##*/}" # Strip path
vm_foundid="${vm_foundname##*[!0-9]}"
if [ "$vm_foundid" = "$nextid" ]; then
conflict="yes"
fi
done # Pass completed
if [ "$conflict" = "yes" ]; then
nextid=$(($nextid+1))
conflict="" # Reset "conflict" !
else
echo $nextid
exit 0
fi
done
fi
} # End f_getnextid
f_filetype() # A smarter file(1) that is 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
esac
# FYI images are application/octet-stream; charset=binary
} # End f_filetype
f_dehumanize() # $1 # Human readable storage i.e. 1GB in, bytes out
{
disk_size_number=${1%[!0-9]}
disk_size_unit=${1##*[0-9]}
# NB! Do we want more clever ways of saying "kilobyte"? KiB?
case $disk_size_unit in
b|B)
disk_size_bytes=$disk_size_number
;;
k|K|KB)
disk_size_bytes=$(( $disk_size_number * 1024 ))
;;
m|M|MB)
disk_size_bytes=$(( $disk_size_number * 1024 * 1024 ))
;;
g|G|GB)
disk_size_bytes=$(( $disk_size_number * 1024 * 1024 * 1024 ))
;;
t|T|TB)
disk_size_bytes=$(( $disk_size_number * 1024 * 1024 * 1024 * 1024 ))
;;
esac
echo $disk_size_bytes
} # End f_dehumanize
del_vm() # Note that this operates on the hard-coded $vm_name
{
echo ; echo Deleting $vm_name
[ $( zfs get -pH name $host_zpool$host_vmdir$vm_name > /dev/null 2>&1 ) ] && \
{ echo Destroying VM with zfs destroy -f -r $host_zpool$host_vmdir$vm_name ; \
zfs destroy -f -r $host_zpool$host_vmdir$vm_name ; }
echo Running rm -rf $host_vmdir/$vm_name
umount -f $host_vmdir/$vm_name/mnt > /dev/null 2>&1
rm -rf $host_vmdir/$vm_name
}
### STEP ONE : Building VM directory based on the specified template ###
# Check that $host_vmdir is set
[ -z $host_vmdir ] && \
{ echo host_vmdir is not set from vmrc.conf. Exiting ; exit 1 ; }
[ -w $host_vmdir ] || \
{ echo host_vmdir is not writable. Exiting ; exit 1 ; }
echo ; echo Generating a new VM ID number
vm_id=$( f_getnextid $host_vmdir )
echo The resulting VM ID is $vm_id
if [ "$#" -gt 0 ]; then # Non-interactive mode
echo -------------------------------------------------------------
echo -------------- Running in non-interactive mode --------------
echo -------------------------------------------------------------
echo The requested template is\: $1
echo The requested VM name is\: $2
echo
# Verify if two arguments are passed in
[ "$#" = "2" ] || \
{ echo Either VM template or name were not specified. Exiting ; exit 1 ; }
[ -z ${2##*[!0-9]} ] || \
{ echo The VM name $2 cannot end in a number. Exiting ; exit 1 ; }
template=$1
vm_name=$2$vm_id
else # Enter interactive mode
echo ; echo Listing VMs in $host_vmdir
echo ; ls $host_vmdir
echo ; echo Listing templates in $host_templates
echo ; ls $host_templates ; echo
echo ; echo Enter a template to use: ; echo
read template
[ $template ] || \
{ echo No template entered. Exiting ; exit 1 ; }
[ -r "$host_templates/$template" ] || \
{ echo Template canot be read. Exiting ; exit 1 ; }
vm_name=vm$vm_id
echo VM will be $vm_name by default.
echo
echo Enter a custom name without ID or leave blank to keep $vm_name
echo
read vm_new_name
if [ ! "$vm_new_name" = "" ]; then
vm_name=$vm_new_name$vm_id
fi
fi # End non-interactive/interactive test
[ -f $host_vmdir/$vm_name ] && \
{ echo $host_vmdir/$vm_name already exists. Exiting ; exit 1 ; }
[ -r $host_templates/$template ] || \
{ echo $host_templates/$template not found. Exiting ; exit 1 ; }
echo
echo The resulting VM will be named $vm_name and use $host_templates/$template
# Verify if a host zpool is specified and verify if it exists, if it does,
# use datasets rather than directories in host_vmdir
echo ; echo Verifying if a zpool and datasets are requested
if [ ! "$host_zpool" ]; then
echo ; echo Creating $host_vmdir/$vm_name directory
mkdir -p $host_vmdir/$vm_name
else
echo ; echo Verifying if zpool $host_zpool exists
zpool get -pH name $host_zpool > /dev/null 2>&1
[ $? -gt 0 ] && \
{ echo Requested zpool $host_zpool does not exist. Exiting ; exit 1 ; }
if [ $( grep FreeNAS /etc/version > /dev/null 2>&1 ) ]; then
echo Appears to be a FreeNAS host
host_vmdir_mp=${host_vmdir#/mnt} # mp = mount point
# Explain this mapping - Remove "/mnt" from the pool name?
else
host_vmdir_mp=$host_vmdir
fi
echo ; echo Creating $host_vmdir$vm_name dataset
# See if the dataset does not exist
if [ ! $( zfs get -pH name $host_zpool$host_vmdir$vm_name > /dev/null 2>&1 ) ]; then
zfs create -p -o mountpoint=$host_vmdir_mp$vm_name $host_zpool$host_vmdir$vm_name
[ -d $host_vmdir_mp$vm_name ] || \
{ echo The $host_vmdir_mp$vm_name dataset failed to create. \
Trying a directory instead ; \
mkdir -p $host_vmdir_mp$vm_name ; }
[ -d $host_vmdir_mp$vm_name ] || \
{ echo $host_vmdir$vm_name_mp failed to create. Exiting ; exit 1 ; }
fi # End dataset test
fi # End pool test
[ -w $host_vmdir/$vm_name ] || \
{ echo $host_vmdir$vm_name failed to create. Exiting ; exit 1 ; }
# NB! Nothing to delete at this point, right?
# NB! Should this use vm_name_mp? Test on FreeNAS!
# ~/mnt is only used for temporary mounts. No need for a dataset
echo ; echo Creating $host_vmdir$vm_name/mnt directory
mkdir -p $host_vmdir$vm_name/mnt
[ -w $host_vmdir$vm_name/mnt ] || \
{ echo $host_vmdir$vm_name/mnt was not created. Exiting ; del_vm ; exit 1 ; }
echo ; echo Running cp $host_templates/$template $host_vmdir$vm_name/vm.conf
cp $host_templates/$template $host_vmdir$vm_name/vm.conf
[ -r $host_vmdir$vm_name/vm.conf ] || \
{ echo $host_vmdir$vm_name/vm.conf failed to copy. Exiting ; del_vm ; exit 1 ; }
# If in interactive mode, offer a chance to edit the file in place
if [ $# = 0 ]; then # Again verify if in interactive mode
echo ; echo Do you want to edit the configuration file in vi?
echo ; echo "(for example to set an existing VM hardware boot device)"
echo ; echo Y or N ; echo
read edit_in_vi
case $edit_in_vi in
y|Y|yes|Yes|YES)
vi $host_vmdir$vm_name/vm.conf
;;
*)
continue
esac
fi
echo ; echo Reading the $host_vmdir$vm_name/vm.conf config file
sh -n $host_vmdir$vm_name/vm.conf >/dev/null 2>&1 || \
{ echo $vm_name config file failed parse. Deleting VM ; del_vm ; exit 1 ; }
. $host_vmdir$vm_name/vm.conf || \
{ echo $vm_name config file failed to source. Deleting VM ; del_vm ; exit 1 ; }
# Aborting early on these because we won't get far without them
echo
echo Validating key variables in the $host_vmdir$vm_name/vm.conf config file
[ -z $install_site ] && \
{ echo install_site is not set. Deleting VM ; del_vm ; exit 1 ; }
# One COULD have a file:// setting and the payloads copied in?
#[ -z $site_path ] && \
#{ echo site path is not set. Deleting VM ; del_vm ; exit 1 ; }
[ -z $site_payload ] && \
{ echo site_payload is not set. Deleting VM ; del_vm ; exit 1 ; }
case $install_method in
raw|iso|img|distset) # objdir
continue
;;
*)
echo install_method is not set. Deleting VM
del_vm
exit 1
esac
case $disk0_type in
dev|img|zvol) # iscsi|nfs
continue
;;
*)
echo disk0_type is not set. Deleting VM
del_vm
exit 1
esac
# VM directory structure and customized configuration file are now in place
### STEP TWO: Fetch raw image, ISO, IMG or distribution sets ###
# This is install_method agnostic
# Avoiding the 8.4 dist set layout
# pv? dpv?
# Maybe fetch size check against the ondisk one to make sure it is correct?
# Note FreeBSD timestamp files, reproducable builds...
echo
echo Fetching install media if not present in $host_distdir
for distset in $site_payload; do
if [ -f $host_distdir/$site_path$distset ]; then # File is present
echo
echo $host_distdir$site_path$distset already exists locally
fetchexit="0"
else
mkdir -p $host_distdir$site_path # Harmless if present
echo
echo $host_distdir$site_path$distset is missing. Fetching
# Consider fetch -m mirror mode which verifies payload size BUT would not
# allow for offline use. gjb says there may be a timestamp check
fetch --no-verify-peer -m $install_site$site_path$distset -o \
$host_distdir/$site_path/
fetchexit=$?
[ "$fetchexit" -gt 0 ] && \
{ echo Distribution set did not fetch. Deleting VM ; \
del_vm ; exit 1 ; }
fi
done
# Make sure fetchexit is initialized
[ "$fetchexit" -gt 0 ] && \
{ echo Distribution set did not fetch. Deleting VM ; del_vm ; exit 1 ; }
echo
echo Expanding or copying the payload as necessary
# Note that gunzip will ignore endings that it does not recognize
# libarchive should do this but not the tar front end
# Consider fetch size check over a given size to distinguish
# text file answers, errors, redirects...
# Does fetch handle redirect output? ( earlier download above )
echo Checking if $host_distdir/$site_path/$site_payload is compressed
filetype=$( f_filetype "$host_distdir/$site_path/$site_payload" )
echo Download appears to be type $filetype according to f_filetype
payload_compressed="" # Override if set in an old-style template
case $filetype in
.bz2) echo Handling .bz2
payload_compressed="YES"
# Why does this test sometimes work with ! and sometimes not?
if [ ! -f $host_distdir/$site_path/$site_payload.unc ]; then
echo Expanding $host_distdir/$site_path/$site_payload \
to $host_distdir/$site_path/$site_payload.unc
gunzip -c -k $host_distdir/$site_path/$site_payload > \
$host_distdir/$site_path/$site_payload.unc || \
{ echo Image extraction failed. Deleting VM ; del_vm ; exit 1 ; }
fi
;;
.Z) echo Handling .Z
payload_compressed="YES"
# Why does this test sometimes work with ! and sometimes not?
if [ ! -f $host_distdir/$site_path/$site_payload.unc ]; then
echo Expanding $host_distdir/$site_path/$site_payload \
to $host_distdir/$site_path/$site_payload.unc
gunzip -c -k $host_distdir/$site_path/$site_payload > \
$host_distdir/$site_path/$site_payload.unc || \
{ echo Extraction failed. Deleting VM ; del_vm ; exit 1 ; }
fi
;;
.gz) echo Handling .gz
payload_compressed="YES"
# Why does this test sometimes work with ! and sometimes not?
if [ ! -f $host_distdir/$site_path/$site_payload.unc ]; then
echo Expanding $host_distdir/$site_path/$site_payload \
to $host_distdir/$site_path/$site_payload.unc
gunzip -c -k $host_distdir/$site_path/$site_payload > \
$host_distdir/$site_path/$site_payload.unc || \
{ echo Extraction failed. Deleting VM ; del_vm ; exit 1 ; }
fi
;;
.xz) echo Handling .xz
payload_compressed="YES"
# Why does this test sometimes work with ! and sometimes not?
if [ ! -f $host_distdir/$site_path/$site_payload.unc ]; then
echo Expanding $host_distdir/$site_path/$site_payload \
to $host_distdir/$site_path/$site_payload.unc
unxz -c -k $host_distdir/$site_path/$site_payload > \
$host_distdir/$site_path/$site_payload.unc || \
{ echo Extraction failed. Deleting VM ; del_vm ; exit 1 ; }
fi
;;
.zip) echo Handling .zip
payload_compressed="YES"
# Why does this test sometimes work with ! and sometimes not?
if [ ! -f $host_distdir/$site_path/$site_payload.unc ]; then
echo Expanding $host_distdir/$site_path/$site_payload \
to $host_distdir/$site_path/$site_payload.unc
gunzip -c -k $host_distdir/$site_path/$site_payload > \
$host_distdir/$site_path/$site_payload.unc || \
{ echo Extraction failed. Deleting VM ; del_vm ; exit 1 ; }
fi
;;
Uncompressed)
echo $host_distdir/$site_path/$site_payload not compressed
payload_compressed="NO"
# A wildcard is somewhat pointless as we are relying on our own f_filetype
esac
# NB! We're looking for .unc not .iso and what not
# Done with downloading and decompressing the ISO or image
# Do we end with .iso or .unc?
# Consider text/html; charset=us-ascii for redirects and errors
# We could check return values but will check for the desired result
ending="" # Clear the variable
if [ $payload_compressed = "YES" ]; then
ending=".unc"
fi
### STEP THREE : VM Storage Preparation ###
echo ; echo Preparing VM storage
case $install_method in
raw)
case $disk0_type in
img)
if [ "$disk0_size" = "" ]; then
echo Copying VM raw image $host_distdir/$site_path/${site_payload}$ending to \
$host_vmdir$vm_name/disk0.img
cp -p $host_distdir/$site_path/${site_payload}$ending \
$host_vmdir$vm_name/disk0.img || \
{ echo Image failed to copy or link. Deleting VM ; del_vm ; exit 1 ; }
else # disk0_size is set
echo Determining the size of $host_distdir/$site_path/${site_payload}$ending
payload_size=$( stat -f%z $host_distdir/$site_path/${site_payload}$ending ) || \
{ echo Could not determine payload size. Deleting VM ; del_vm ; exit 1 ; }
disk_size=$( f_dehumanize $disk0_size ) || \
{ echo disk0_size invalid. Deleting VM ; del_vm ; exit 1 ; }
if [ $disk_size -gt $payload_size ]; then
disk0_size=$disk_size
else
disk0_size=$payload_size
fi
fi # End disk0_blocksize
echo Running truncate -s $disk0_size $host_vmdir$vm_name/disk0.img
truncate -s $disk0_size $host_vmdir$vm_name/disk0.img
[ -w $host_vmdir$vm_name/disk0.img ] || \
{ echo $host_vmdir$vm_name/disk0.img \
failed to truncate. Deleting VM ; del_vm ; exit 1 ; }
# conv=notrunc = preserve existing blocks on larger img
echo Running dd if=$host_distdir/$site_path/${site_payload}$ending \
of=$host_vmdir$vm_name/disk0.img conv=notrunc bs=1m
# NB! Determine blocksize from config and add to dd? Pick one if not specified?
dd if=$host_distdir/$site_path/${site_payload}$ending \
of=$host_vmdir$vm_name/disk0.img conv=notrunc bs=1m || \
{ echo dd failed. Deleting VM ; del_vm ; exit 1 ; }
# NB! Determine if a gpart resize is needed?
# gpart resize -i 3 ada0 ; growfs /
# https://www.freebsd.org/doc/handbook/disks-growing.html
;; # End install_method: raw: disk0_type img
dev)
echo dev installation not yet implemented. Deleting VM
# NB! Determine if a gpart resize is needed
# gpart resize -i 3 ada0 ; growfs /
# https://www.freebsd.org/doc/handbook/disks-growing.html
del_vm ; exit 1
;; # End install_method: raw: disk0_type dev
zvol)
# Determine the requested disk size from the configuration file
payload_size=$( stat -f%z $host_distdir/$site_path/${site_payload}$ending ) || \
{ echo Could not determine payload size. Deleting VM ; del_vm ; exit 1 ; }
echo Verifying if a custom disk0_size is set
disk_size=""
if [ "$disk0_size" ]; then
echo Custom disk0_size of $disk0_size is set
disk_size=$( f_dehumanize $disk0_size ) || \
{ echo disk0_size invalid. Deleting VM ; del_vm ; exit 1 ; }
if [ "$disk_size" -gt "$payload_size" ]; then
disk0_size=$disk_size
fi
else
disk0_size=$payload_size
fi
echo Verifying if a custom disk0_blocksize is set
disk_blocksize=""
if [ "$disk0_blocksize" ]; then
echo Custom disk0_blocksize of $disk0_blocksize is set
disk_blocksize=$( f_dehumanize $disk0_blocksize ) || \
{ echo disk0blocksize invalid. Deleting VM ; del_vm ; exit 1 ; }
echo disk0_blocksize of $disk0_blocksize or $disk_blocksize bytes is set
else
echo Using the default ZFS blocksize of 8192 bytes
disk_blocksize=8192 # ZFS default of 128k
# NB! Double check this value
fi
# Sanitize adjusted disk0_size based on disk_blocksize
disk0_size=$(( $disk0_size / $disk_blocksize ))
disk0_size=$(( $disk0_size + 1 ))
disk0_size=$(( $disk0_size * $disk_blocksize ))
echo Creating zvol $host_zpool$host_vmdir$vm_name/disk0 with \
zfs create -s -o volblocksize=$disk_blocksize -V $disk0_size \
$host_zpool$host_vmdir$vm_name/disk0
[ -e /dev/zvol/$host_zpool$host_vmdir$vm_name/disk0 ] || \
{ zfs create -s -o volblocksize=$disk_blocksize \
-V $disk0_size \
$host_zpool$host_vmdir$vm_name/disk0 ; }
[ -e /dev/zvol/$host_zpool$host_vmdir$vm_name/disk0 ] || \
{ echo zvol failed to create. Deleting VM ; del_vm ; exit 1 ; }
echo Running dd if=$host_distdir/$site_path/${site_payload}$ending \
of=/dev/zvol/$host_zpool$host_vmdir$vm_name/disk0 conv=notrunc bs=1m
# NB! Determine blocksize from config and add to dd? Pick one if not specified?
dd if=$host_distdir/$site_path/${site_payload}$ending \
of=/dev/zvol/$host_zpool$host_vmdir$vm_name/disk0 conv=notrunc bs=1m || \
{ echo dd failed. Deleting VM ; del_vm ; exit 1 ; }
;; # End install_method: raw: disk0_type zvol
esac # End install_method: raw: disk0_type
echo ; echo "You can boot your VM with:"
echo ; echo "service vm onestart $vm_name"
echo "service vm oneattach $vm_name # Serial Console"
echo "service vm onevnc $vm_name # VNC Console, if appropriate"
echo
exit 0
;; # End install_method: raw
iso|img)
case $disk0_type in
img)
# Check if it exists, not sure when that might happen but would allow the
# operator to create a storage device during the vi customization stage
if [ ! -f $host_vmdir/$vm_name/disk0.img ]; then
echo
echo Truncating $host_vmdir/$vm_name/disk0.img
# BUG to consider: fixed in HEAD: a 'dd' option for use with 'tar'
[ -z $disk0_size ] && \
{ echo disk0_size is not set. Deleting VM ; del_vm ; exit 1 ; }
echo Running truncate -s $disk0_size $host_vmdir/$vm_name/disk0.img
truncate -s $disk0_size $host_vmdir/$vm_name/disk0.img
# NB! I thought truncate could set a blocksize... use 'dd'?
[ -f $host_vmdir/$vm_name/disk0.img ] || \
{ echo Disk image failed to create. Deleting VM ; del_vm ; exit 1 ; }
fi
;; # End install_method: iso|img: disk0_type img
dev)
continue
;; # End install_method: iso|img: disk0_type dev
zvol)
# Check if disk0_blocksize is set
disk_blocksize=""
if [ "$disk0_blocksize" ]; then
disk_blocksize=$( f_dehumanize $disk0_blocksize ) || \
{ echo Verify disk0_blocksize. Deleting VM ; del_vm ; exit 1 ; }
disk_blocksize="-o volblocksize=$disk_blocksize"
fi
echo Creating zvol $host_zpool$host_vmdir$vm_name/disk0 with \
zfs create -s $disk_blocksize -V $disk0_size \
$host_zpool$host_vmdir$vm_name/disk0
[ -e /dev/zvol/$host_zpool$host_vmdir$vm_name/disk0 ] || \
{ zfs create -s $disk_blocksize -V $disk0_size \
$host_zpool$host_vmdir$vm_name/disk0 ; }
[ -e /dev/zvol/$host_zpool$host_vmdir$vm_name/disk0 ] || \
{ echo zvol failed to create. Deleting VM ; del_vm ; exit 1 ; }
;; # End install_method: iso|img: disk0_type zvol
esac # End install_method: iso|img: disk0_type
echo Linking $host_distdir/$site_path/${site_payload}$ending \
to $host_vmdir/$vm_name/install.$install_method
# Alternative: cp -p because some containers may complain
ln -sf $host_distdir/$site_path/${site_payload}$ending \
$host_vmdir/$vm_name/install.$install_method || \
{ echo Image failed to copy or link. Deleting VM ; del_vm ; exit 1 ; }
echo ; echo "You can boot your VM and begin installation with:"
echo ; echo "service vm oneinstall $vm_name"
echo "service vm oneattach $vm_name # Serial Console"
echo "service vm onevnc $vm_name # VNC Console, if appropriate"
echo
echo Some installers will require a "stop" after installation
echo
exit 0
;; # End install_method: iso|img
distset|objdir)
case $disk0_fs in
ufs|zfs) continue
;;
*) echo disk0_fs not set. Deleting VM ; del_vm ; exit 1
esac
case $vm_dev_util in
fdisk|gpart) continue
;;
*) echo vm_dev_util not set. Deleting VM ; del_vm ; exit 1
esac
case $vm_dev_layout in
mbr|gpt) continue
;;
*) echo vm_dev_layout not set. Deleting VM ; del_vm ; exit 1
esac
case $disk0_type in
img)
# Check if it exists, not sure when that might happen but would allow the
# operator to create a storage device during the vi customization stage
if [ ! -f $host_vmdir/$vm_name/disk0.img ]; then
echo
echo Running truncate -s $disk0_size $host_vmdir/$vm_name/disk0.img
# BUG to consider: fixed in HEAD: a 'dd' option for use with 'tar'
[ -z $disk0_size ] && \
{ echo disk0_size is not set. Deleting VM ; del_vm ; exit 1 ; }
truncate -s $disk0_size $host_vmdir/$vm_name/disk0.img
# NB! I thought truncate could set a blocksize... use 'dd'?
[ -f $host_vmdir/$vm_name/disk0.img ] || \
{ echo Disk image failed to create. Deleting VM ; del_vm ; exit 1 ; }
fi
vm_device=$( mdconfig -af $host_vmdir$vm_name/disk0.img ) ||
{ echo "disk0.img failed to attach. Exiting" ; exit 1 ; }
;; # End install_method: distset|objdir: disk0_type img
dev)
continue
;; # End install_method: distset|objdir: disk0_type dev
zvol)
echo Verifying if a custom disk0_blocksize is set
disk_blocksize=""
if [ "$disk0_blocksize" ]; then
disk_blocksize=$( f_dehumanize $disk0_blocksize ) || \
{ echo Verify disk0_blocksize. Deleting VM ; del_vm ; exit 1 ; }
disk_blocksize="-o volblocksize=$disk_blocksize"
fi
echo Creating zvol $host_zpool$host_vmdir$vm_name/disk0
[ -e /dev/zvol/$host_zpool$host_vmdir$vm_name/disk0 ] || \
{ zfs create -s $disk_blocksize -V $disk0_size \
$host_zpool$host_vmdir$vm_name/disk0 ; }
[ -e /dev/zvol/$host_zpool$host_vmdir$vm_name/disk0 ] || \
{ echo zvol failed to create. Deleting VM ; del_vm ; exit 1 ; }
vm_device=zvol/$host_zpool$host_vmdir$vm_name/disk0
;; # End install_method: distset|objdir: disk0_type zvol
esac # End install_method: distset|objdir: disk0_type
echo ; echo Prefixing $vm_device as /dev/$vm_device
vm_device=/dev/$vm_device
echo ; echo The resulting VM device is $vm_device
echo ; echo Verifying that $vm_device exists
if [ -e $vm_device ]; then
echo Using $vm_device
else
echo VM device $vm_device failed to initialize. Exiting
exit 1
fi
echo ; echo Initializing the vm_mountpoint variable as
echo $host_vmdir/$vm_name/mnt/
vm_mountpoint=$host_vmdir/$vm_name/mnt/
# Note that vm_dev_util and vm_dev_layout were checked above
echo ; echo Running f_${vm_dev_util}_${vm_dev_layout}_layout
f_${vm_dev_util}_${vm_dev_layout}_layout_preflight $vm_device
f_${vm_dev_util}_${vm_dev_layout}_layout $vm_device
f_${vm_dev_util}_${vm_dev_layout}_layout_debug $vm_device
echo ; echo Running f_check_gpt_alignment $vm_device
f_check_gpt_alignment $vm_device && echo Partitions are aligned
# BUG: Make conditional, report if not alighed
# pointless here if a zvol. Move to function, need to test either way
#echo ; echo Running file on $host_vmdir/$vm_name/${vm_name}.img
#file $host_vmdir/$vm_name/${vm_name}.img
echo ; echo Running f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_boot
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_boot_preflight $vm_device
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_boot $vm_device
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_boot_debug $vm_device
# again, pointless here
#echo ; echo Running file on $host_vmdir/$vm_name/${vm_name}.img
#file $host_vmdir/$vm_name/${vm_name}.img
# Push test to function?
#f_"$vm_dev_util"_"$vm_dev_layout"_"$disk0_fs"_bootmgr_preflight $vm_device
#f_"$vm_dev_util"_"$vm_dev_layout"_"$disk0_fs"_bootmgr $vm_device
#f_"$vm_dev_util"_"$vm_dev_layout"_"$disk0_fs"_bootmgr_debug $vm_device
#file ${host_vmdir}/${vm_name}/${vm_name}.img
echo ; echo Formatting ZFS or UFS storage as appropriate
if [ "$disk0_fs" = "zfs" ]; then
# Name the VM's pool in a non-conflicting way
# One pool name for all VM's would be a dissaster when mounted on host
vm_pool=${vm_name}pool
echo ; echo Running f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_part
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_part_preflight $vm_device
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_part $vm_device
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_part_debug $vm_device
echo ; echo Destroying the pool just in case
zpool destroy $vm_pool >/dev/null 2>&1
case $vm_dev_layout in
mbr)
echo Destroying the gnop just in case
gnop destroy -f ${vm_device}s1a.nop >/dev/null 2>&1
;;
gpt)
echo Destroying the gnop just in case
gnop destroy -f ${vm_device}p2.nop >/dev/null 2>&1
;;
*)
echo Invalid VM device layout
return
esac
# case layout=mbr - doesn't seem necessary
#echo manually-adding-boot-code
#dd if=/boot/zfsboot of=${vm_device}s1a skip=1 seek=1024
echo ; echo Running f_${vm_dev_util}_${vm_dev_layout}_zpool
f_${vm_dev_util}_${vm_dev_layout}_zpool_preflight $vm_device $vm_pool $vm_mountpoint
f_${vm_dev_util}_${vm_dev_layout}_zpool $vm_device $vm_pool $vm_mountpoint
f_${vm_dev_util}_${vm_dev_layout}_zpool_debug $vm_device $vm_pool $vm_mountpoint
echo ; echo Running zpool import
zpool import
echo ; echo Running zpool import -o cachefile=none -R $vm_mountpoint $vm_pool
zpool import -o cachefile=none -R $vm_mountpoint $vm_pool
echo ; echo Running zpool list pipe grep $vm_pool
zpool list | grep $vm_pool
zfs list | grep $vm_pool
echo Running mount pipe grep $vm_pool
mount | grep $vm_pool
else # UFS
# BUG: Missing MBR variant. Does this even belong here?
# echo
# echo Running f_"$vm_dev_util"_"$vm_dev_layout"_"$disk0_fs"_newfs
# f_"$vm_dev_util"_"$vm_dev_layout"_"$disk0_fs"_newfs_preflight $vm_device
# f_"$vm_dev_util"_"$vm_dev_layout"_"$disk0_fs"_newfs $vm_device
# f_"$vm_dev_util"_"$vm_dev_layout"_"$disk0_fs"_newfs_debug $vm_device
case $vm_dev_layout in
mbr)
echo
echo Running f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_newfs_bootable
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_newfs_bootable_preflight $vm_device
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_newfs_bootable $vm_device
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_newfs_bootable_debug $vm_device
;;
gpt)
echo
echo Running f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_newfs
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_newfs_preflight $vm_device
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_newfs $vm_device
f_${vm_dev_util}_${vm_dev_layout}_${disk0_fs}_newfs_debug $vm_device
;;
*)
echo Invalid VM device layout
return
esac
echo ; echo Running ls $vm_device asterisk pipe head
ls ${vm_device}* | head
echo ; echo Mounting UFS-based storage if requested
# fsck is pointless as a test on a new device
# Note s1a/p2 - push or pull to/from config
case $vm_dev_layout in
mbr)
# echo runningfsck
# fsck_ufs -y ${vm_device}s1a
echo Mounting ${vm_device}s1a on $vm_mountpoint
mount ${vm_device}s1a $vm_mountpoint
;;
gpt)
# echo runningfsck
# fsck_ufs -y ${vm_device}p2
echo Mounting ${vm_device}p2 on $vm_mountpoint
mount ${vm_device}p2 $vm_mountpoint
;;
*)
echo Invalid VM device layout
return
esac
echo Running ls $vm_mountpoint
ls $vm_mountpoint
# BUG: Could check for .snap on UFS filesystems
fi # end ZFS|UFS check
# Safety belt but we could proabably use more
echo ; echo Running mountpoint checks to avoid extracting to root
if [ "$vm_mountpoint" = "" ]; then
echo We do not want to install to root. Deleting VM
del_vm
exit 1
elif [ "$vm_mountpoint" = "/" ]; then
echo We do not want to install to root. Deleting VM
del_vm
exit 1
fi
echo ; echo Determining if install method is distribution set or src/obj
if [ "$install_method" = "distset" ]; then
echo ; echo Running distribution set extraction loop
for distset in $site_payload; do
f_installset_preflight $host_distdir/$site_path/$distset \
$vm_mountpoint
f_installset $host_distdir/$site_path/$distset $vm_mountpoint
f_installset_debug $host_distdir/$site_path/$distset $vm_mountpoint
done
elif [ "$install_method" = "objdir" ]; then
echo ; echo Verifying that sources and a built world and kernel are present
if [ ! -f $vm_objdir/Makefile ]; then
echo Sources are not present in ${vm_objdir}. Deleting VM
del_vm
exit 1
# NB! Rethink this: only use a custom src and obj directories
elif [ ! -d /usr/obj/usr ]; then
echo Built world not present in /usr/obj/. Deleting VM
del_vm
exit 1
elif [ ! -d /usr/obj/usr/src/sys ]; then
echo Built kernel not present in /usr/obj/. Deleting VM
del_vm
exit 1
fi
echo ; echo Changing to the source directory for an obj installation
cd $vm_objdir
echo ; echo Running make installworld to $vm_mountpoint
make installworld DESTDIR=$vm_mountpoint
echo ; echo Running make installkernel to $vm_mountpoint
make installkernel DESTDIR=$vm_mountpoint
echo ; echo Running make distribution to $vm_mountpoint
make distribution DESTDIR=$vm_mountpoint
else
echo ; echo Install method not set. Deleting VM
del_vm
exit 1
fi
echo ; echo Running ls $vm_mountpoint
ls $vm_mountpoint
echo ; echo Configuring loader
f_config_loader_conf_preflight $vm_mountpoint
f_config_loader_conf $vm_mountpoint
f_config_loader_conf_debug $vm_mountpoint
echo ; echo Configuing fstab
if [ "$disk0_fs" = "zfs" ]; then
f_config_zfs_fstab_preflight $vm_mountpoint
f_config_zfs_fstab $vm_mountpoint
f_config_zfs_fstab_debug $vm_mountpoint
else
f_config_"$vm_dev_layout"_fstab_preflight $vm_mountpoint
f_config_"$vm_dev_layout"_fstab $vm_mountpoint
f_config_"$vm_dev_layout"_fstab_debug $vm_mountpoint
fi
echo ; echo Configuring time zone
f_config_tz_preflight $vm_mountpoint $vm_timezone
f_config_tz $vm_mountpoint $vm_timezone
f_config_tz_debug $vm_mountpoint $vm_timezone
echo ; echo Configuring rc.conf
f_config_rc_conf_preflight $vm_mountpoint $vm_hostname $vm_ipv4 $vm_gw
f_config_rc_conf $vm_mountpoint $vm_hostname $vm_ipv4 $vm_gw
f_config_rc_conf_debug $vm_mountpoint $vm_hostname $vm_ipv4 $vm_gw
echo ; echo Configuring resolv.conf
if [ ! "$vm_ipv4" = "" ]; then
f_config_resolv_conf_preflight $vm_mountpoint $vm_searchdomain $vm_dns
f_config_resolv_conf $vm_mountpoint $vm_searchdomain $vm_dns
f_config_resolv_conf_debug $vm_mountpoint $vm_searchdomain $vm_dns
fi
echo ; echo Enabling ssh root access
f_config_sshd_root_preflight $vm_mountpoint
f_config_sshd_root $vm_mountpoint
f_config_sshd_root_debug $vm_mountpoint
echo ; echo Setting root password
f_set_password_preflight $vm_password $vm_mountpoint
f_set_password $vm_password $vm_mountpoint
f_set_password_debug $vm_password $vm_mountpoint