forked from michaeldexter/occambsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagine.sh
1970 lines (1564 loc) · 57.1 KB
/
imagine.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
#-
# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
#
# Copyright 2022, 2023, 2024 Michael Dexter
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# Version v.0.5.4beta
# imagine.sh - a disk image imager for virtual and hardware machines
# MOTIVATION
# This is project motivated by the simple notion that "generic" raw boot images
# can be retrieved and booted on "generic" hardware and virtual machines.
# Unfortunately, operating systems are highly-inconsistent in providing
# "generic" raw boot images. The top issues are:
# * Lack of raw images (QEMU, VHDX, VDI, VMDK, etc. are obsoleted by OpenZFS)
# * Lack of "Latest" links (aliases to the most recent versions)
# * Lack of BIOS/Legacy and QEMU images
# * Inconsistent and/or ambiguous file naming with architecture
# SUPPORTED IMAGES SOURCES
# Downloaded FreeBSD amd64, arm64, i386, and riscv raw VM-IMAGEs
# Custom build FreeBSD raw VM-IMAGEs such as those build with OccamBSD
# OmniOS amd64 "cloud" images
# Debian amd64 and arm64 "cloud" images
# RouterOS amd64 and arm64 "cloud" images
# Windows autounattend.xml installations performed by bhyve(8)
# SUPPORTED IMAGE DESTINATIONS
# Unmodified copied or expanded images
# Resized copied or expanded images
# Mirrored unmodified or expanded root-on-ZFS images (FreeBSD)
# Physical block storage devices
# Mirrored physical root-on-ZFS block storage devices (FreeBSD only)
# ADDITIONAL OPTIONS
# Specify a work directory
# Resize images (needed for adding sources and other contents)
# Rename zpools (FreeBSD and OmniOS only)
# Keep the images or devices mounted for further configuration
# Include sources (FreeBSD only)
# Generate bhyve, QEMU, and Xen boot scripts
# Generate VMDK-compatible wrappers
# IN SHORT
# You can generate virtual and hardware machines in minutes in not seconds, and,
# combined with occambsd.sh, can build, release, configured, and boot custom
# operating systems in minutes, using mostly in-base tools.
# RELATIONSHIP TO OCCAMBSD
# occambsd.sh -p <profile> -v will build a UFS VM-IMAGE to
# /usr/obj/usr/src/amd64.amd64/release/vm.ufs.raw
# occambsd.sh -p <profile> -v -z will build a ZFS VM-IMAGE to
# /usr/obj/usr/src/amd64.amd64/release/vm.zfs.raw
# Accordingly, 'imagine -r raw' and 'imagine -r raw -z' will operate on these
# with flags such as -b for boot scripts, -g for growth, etc.
# CAVEATS
# This intimately follows the FreeBSD Release Engineering mirror layout.
# If the layout changes, this will probably break.
# FreeBSD 15.0-CURRENT VM-IMAGES are now 6GB in size.
# The generated bhyve boot scripts require the bhyve-firmware UEFI package.
# The canonical /media temporary mount point is hard-coded for now.
# The canonical /usr/src directory is hard-coded when using -r obj or a path.
# 'fetch -i' only checks date stamps, allow for false matches on interrupted downloads.
# Xen boot scripts do not support mirrored devices yet
# Running imagine.sh in the working directory will cause existing release versions to be misinterpreted as image paths to be copied.
# This will clean up previous images but not boot scripts.
# /etc/fstab is moved to fstab.original and replaced with an empty file on any
# runs that include advanced zpool handling
# EXAMPLES
# To fetch a 15.0-CURRENT raw boot image to ~/imagine-work/freebsd.raw
#
# sh imagine.sh -r 15.0-CURRENT
# To fetch a 15.0-CURRENT raw boot image and write to /dev/da1
#
# sh imagine.sh -r 15.0-CURRENT -t /dev/da1
# To copy a "make release" VM image from the canonical object directory:
# /usr/obj/usr/src/amd64.amd64/release/vm.ufs.raw to ~/imagine-work/freebsd.raw
#
# sh imagine.sh -r obj
# To copy a boot image from a custom path and name to a custom path and name:
#
# sh imagine.sh -r /tmp/myvm.img -t /tmp/myvmcopy.raw
# Add '-w /tmp/mydir' to override '~/imagine-work' with an existing directory
# Add '-z' to fetch the root-on-ZFS image
# Add '-b' to generate a simple bhyve, xen, and/or QEMU boot scripts depending on the architecture
# Add '-g 10' to grow boot image to 10GB
# The local and fetched source boot images will be always preserved for re-use
# and -o "offline" mode will skip the attempt to check for changes with fetch
# To generate a 10GB RISC-V system with root-on-ZFS and a QEMU boot script:
#
# sh imagine.sh -a riscv -r 14.0-RELEASE -z -g 10 -b
#
# Add '-V' to generate a VMDK that is VMware compatible, because you can
#########
# USAGE #
#########
f_usage() {
echo ; echo "USAGE:"
echo "-O <output directory> (Default: /root/imagine-work)"
echo "-a <architecture> [ amd64 | arm64 | i386 | riscv ] (Default: Host)"
echo "-r [ obj | /path/to/image | <version> | omnios | debian ]"
# HOW ARE obj and path any different? ONE TRIGGERS SRC - obj calculates the
# object directory path, for better or for worse
echo "obj i.e. /usr/obj/usr/src/<target>.<target_arch>/release/vm.ufs.raw"
echo "/path/to/image.raw for an existing image"
echo "<version> i.e. 14.0-RELEASE | 15.0-CURRENT | 15.0-ALPHAn|BETAn|RCn"
echo "-o (Offline mode to re-use fetched releases and src.txz)"
echo "-t <target> [ img | /dev/device | /path/myimg ] (Default: img)"
echo "-T <mirror target> [ img | /dev/device ]"
echo "-f (FORCE imaging to a device without asking)"
echo "-g <gigabytes> (grow image to gigabytes i.e. 10)"
echo "-s (Include src.txz or /usr/src as appropriate)"
echo "-m (Mount image and keep mounted for further configuration)"
echo "-V (Generate VMDK image wrapper)"
echo "-v (Generate VM boot scripts)"
echo "-z (Use a 14.0-RELEASE or newer root on ZFS image)"
echo "-Z <new zpool name>"
echo "-A (Set the ZFS ARC to only cache metadata)"
echo "-x <autounattend.xml file for Windows> (Requires -i and -g)"
echo "-i <Installation ISO file for Windows> (Requires -x and -g)"
echo
exit 0
}
###################################
# INTERNAL VARIABLES AND DEFAULTS #
###################################
work_dir=~/imagine-work # Default - fails if quoted
hw_platform=$( uname -m ) # i.e. arm64
cpu_arch=$( uname -p ) # i.e. aarch64
arch_string=""
release_input=""
offline_mode=0
force=0
release_type=""
release_name=""
release_image_url=""
release_image_file=""
#release_image_xz=""
release_branch=""
fs_type="ufs"
omnios_amd64_url="https://us-west.mirror.omnios.org/downloads/media/stable/omnios-r151050.cloud.raw.zst"
debian_amd64_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.raw"
debian_arm64_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-arm64.raw"
#debian_amd64_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.raw"
#debian_arm64_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-arm64.raw"
routeros_amd64_url="https://download.mikrotik.com/routeros/7.15.3/chr-7.15.3.img.zip"
routeros_arm64_url="https://download.mikrotik.com/routeros/7.15.3/chr-7.15.3-arm64.img.zip"
memtest86_url="https://www.memtest86.com/downloads/memtest86-usb.zip"
attachment_required=0
root_fs=""
root_part=""
root_dev=""
part_scheme=""
scheme=""
root_part=""
zpool_name=""
zpool_rename=0
zpool_newname=""
zfs_arc_metadata=0
#target_input="raw" # Default not helpful if -x
target_input="img" # Default not helpful if -x
target_dev=""
target_dev2=""
target_type=""
target_path=""
target_prefix=""
target_size=""
mirror_path=""
mirror_size=""
force=0
grow_required=0
grow_size=""
include_src=0
mount_required=0
keep_mounted=0
xml_file=""
iso_file=""
vmdk=0
boot_scripts=0
vm_device=""
vm_name="vm0" # Embedded Default
vm_ram="4096" # Embedded Default
vm_cores=1 # Embedded Default
framebuffer_required=0
bhyve_script=""
qemu_script=""
xen_cfg=""
xen_script=""
xen_destroy=""
md_id=42 # Default for easier cleanup if interrupted
#####################################
# USER INPUT AND VARIABLE OVERRIDES #
#####################################
while getopts O:a:r:zZ:At:T:ofg:smVvx:i: opts ; do
case $opts in
O)
work_dir="$OPTARG"
[ -d "$work_dir" ] || mkdir -p "$work_dir"
[ -d "$work_dir" ] || { echo "mkdir -p $work_dir failed" ; exit 1 ; }
;;
a)
case "$OPTARG" in
amd64|arm64|i386|riscv)
hw_platform="$OPTARG"
;;
*)
echo "Invalid architecture"
f_usage
;;
esac
;;
r)
# -r [ <version> | obj | /path/to/image | omnios | debian | routeros ]
# windows is requested by specifying an xml file
release_input="$OPTARG"
;;
o)
offline_mode=1
;;
z)
# Set for download, only modify if getting fancy
fs_type="zfs"
# Not attachment_required if only copying to an image file
;;
t)
[ "$( id -u )" = 0 ] || { echo "Must be root" ; exit 1 ; }
[ "$OPTARG" ] || f_usage
target_input="$OPTARG"
target_prefix=$( printf %.5s "$target_input" )
;;
f)
# Write to a device without prompting for confirmation
force=1
;;
V)
# Could simply add a wrapper to an existing image
vmdk=1
;;
v)
# root required to execute bhyve and Xen boot scripts
boot_scripts=1
;;
g)
grow_required=1
grow_size="$OPTARG"
# Implied numeric validation
[ "$grow_size" -gt 7 ] || \
{ echo "-g must be a number larger than 7" ; exit 1 ; }
attachment_required=1
;;
Z)
[ "$( id -u )" = 0 ] || { echo "Must be root" ; exit 1 ; }
zpool_newname="$OPTARG"
# Consider validation
# Implying this for use as shorthand
fs_type="zfs"
zpool_rename=1
attachment_required=1
;;
A)
zfs_arc_metadata=1
fs_type="zfs"
attachment_required=1
;;
T)
[ "$( id -u )" = 0 ] || { echo "Must be root" ; exit 1 ; }
[ "$OPTARG" ] || f_usage
mirror_path="$OPTARG"
[ "$mirror_path" = "img" ] || [ -c "$mirror_path" ] || \
{ echo "Likely invalid -T input" ; exit 1 ; }
# Mounting is required for fstab modifications
mount_required=1
attachment_required=1
;;
s)
[ "$( id -u )" = 0 ] || { echo "Must be root" ; exit 1 ; }
include_src=1
grow_required=1
mount_required=1
attachment_required=1
;;
m)
# root required to mount the image for extracting in src.txz
mount_required=1
keep_mounted=1
attachment_required=1
;;
x)
# root required for initial boot though it could be QEMU
xml_file="$OPTARG"
[ -r "$xml_file" ] || \
{ echo "$xml_file missing or unreadable" ; exit 1 ; }
boot_scripts=1
# DEBUG Refactor: This might be more than we need
release_name="windows"
release_input="windows" # Mutually exclusive with release_input
framebuffer_required=1
target_type="oneboot"
fs_type="ntfs"
;;
i)
iso_file="$OPTARG"
[ -r "$iso_file" ] || \
{ echo "$iso_file missing or unreadable" ; exit 1 ; }
target_type="oneboot"
;;
*)
f_usage
;;
esac
done
# Get the hardware device write warnings out of the way early
if [ "$target_prefix" = "/dev/" ] ; then
[ -c "$target_input" ] || { echo "$target_input not found" ; exit 1 ; }
if [ "$force" = 0 ] ; then
echo "WARNING! Writing to $target_input !"
diskinfo -v $target_input
echo -n "Continue? (y/n): " ; read confirmation
[ "$confirmation" = "y" ] || exit 0
fi
fi
if [ "$mirror_path" ] || [ "$mirror_path" = "img" ] ; then
[ -c "$mirror_path" ] || { echo "$mirror_path not found" ; exit 1 ; }
if [ "$force" = 0 ] ; then
echo "WARNING! Writing to $mirror_path !"
diskinfo -v $mirror_path
echo -n "Continue? (y/n): " ; read confirmation2
[ "$confirmation2" = "y" ] || exit 0
fi
fi
# Download mirror path expansion for FreeBSD
case "$hw_platform" in
amd64)
cpu_arch="amd64"
arch_string="amd64"
;;
arm64)
cpu_arch="aarch64"
arch_string="arm64-aarch64"
;;
i386)
cpu_arch="i386"
arch_string="i386"
;;
riscv)
cpu_arch="riscv64"
arch_string="riscv-riscv64"
;;
esac
# Needed for ZFS fstab sins
if [ "$fs_type" = "zfs" ] && [ "$grow_required" = 1 ] ; then
mount_required=1
fi
if [ "$fs_type" = "ufs" ] && [ -n "$mirror_path" ] ; then
echo Device mirroring only works with ZFS
fi
######################
# TESTS - FAIL EARLY #
######################
# This test gives a false positive with -t and -T
#if [ "$zpool_newname" ] || [ "$mirror_path" ] ; then
# # A mirror path could give a false positive but better safe than sorry
# zpool get name $zpool_newname > /dev/null 2>&1 && \
#{ echo zpool $zpool_newname in use and will conflict - use -Z ; exit 1 ; }
#fi
if [ "$target_prefix" = "/dev/" ] ; then
[ "$vmdk" = 0 ] || { echo "-v does not support devices" ; exit 1 ; }
fi
if [ "$xml_file" ] ; then
[ "$iso_file" ] || { echo "-x requires -i" ; exit 1 ; }
[ "$include_src" = 0 ] || { echo "-s not supported with -x" ; exit 1 ; }
# Disable this as a second boot does the installation
grow_required=0
[ "$target_input" ] || \
target_input="${work_dir}/windows-${hw_platform}-${fs_type}.raw"
fi
if [ "$iso_file" ] ; then
[ "$xml_file" ] || { echo "-i requires -x" ; exit 1 ; }
fi
[ -n "$release_input" ] || [ -n "$xml_file" ] || \
{ echo "-r <release> or -x/-i are required" ; f_usage ; exit 1 ; }
f_fetch_image () # $1 release_image_file $2 release_image_url
{
# fetch is not idempotent - check if exists before with fetch -i
if [ -r "$1" ] ; then
if [ "$offline_mode" = 0 ] ; then
[ -f "$1" ] && cp $1 ${1}.previous
fetch -a -i "$1" "$2" || \
{ echo "$2 fetch failed" ; exit 1 ; }
fi
else
fetch -a "$2" || \
{ echo "$2 fetch failed" ; exit 1 ; }
fi
}
f_extract () # $1 release_image_file.compression_ending
{
[ -f $1 ] || { echo "$1 not found" ; exit 1 ; }
# Build-in dpv or dd? Some have -v verbose
case ${1##*.} in
xz) xzcat -k -d $1 ;;
zst|zstd) unzstd -k -c $1 ;;
zip) unzip -p $1 ;;
gz) gunzip -k -c $1 ;;
raw|img|dd) cat $1 ;;
*)
echo "Unrecognized compression format"
exit 1
;;
esac
}
f_cleanse_device () # $1 device
{
echo Destroying all partitions on $1
gpart show -l $1 | tail -n+2 | grep . \
| awk '{print $1,$2,$3,$4}' | \
while read _start _stop _id _label ; do
_label=$( echo $_label | tr -d "[:digit:]" )
[ "$_label" = "null" -o "$_label" = "free" ] && break
echo Clearing zpool labels on $_label
zpool labelclear "$_label" >/dev/null 2>&1
done
echo Clearing zpool labels from $1
zpool labelclear "$1" >/dev/null 2>&1
echo Clearing partitions from $1
gpart recover "$1"
gpart destroy -F "$1"
gpart create -s gpt "$1"
gpart destroy -F "$1"
# Required to avoid corrupt zpool metadata (!)
dd if=/dev/zero of=$1 bs=1m count=1 # 1048576 bytes
dd if=/dev/zero of=$1 bs=1m oseek=`diskinfo $1 \
| awk '{print int($3 / (1024*1024)) - 4;}'`
}
#################################
# HEAVY LIFTING FLAG -r RELEASE #
#################################
# Identify or download a source image - hence the name imagine.sh
echo ; echo Status: Beginning -r release handling
if [ "$release_input" = "obj" ] ; then
release_type="raw"
# Better approach? Check all possibilities?
if [ -f "/usr/obj/usr/src/${hw_platform}.${cpu_arch}/release/vm.${fs_type}.raw" ] ; then
release_image_file="/usr/obj/usr/src/${hw_platform}.${cpu_arch}/release/vm.${fs_type}.raw"
elif [ -f "/usr/obj/usr/src/${hw_platform}.${cpu_arch}/release/vm.raw" ] ; then
# Previous release naming
release_image_file="/usr/obj/usr/src/${hw_platform}.${cpu_arch}/release/vm.raw"
else
echo "$release_image_file not found"
exit 1
fi
# A COPY HERE COULD MEAN WE ARE DONE but it needs a work dir to copy to
# and optional boot scripts
elif [ "$release_name" = "windows" ] ; then
echo "Preparing Windows ISO and boot script"
##########
# OMNIOS #
##########
elif [ "$release_input" = "omnios" ] ; then
# TEST AND FAIL EARLY
# Group these tests above?
[ "$include_src" = 1 ] && \
{ echo "-s src not available with OmniOS images" ; exit 1 ; }
[ "$zpool_rename" = 0 ] || \
{ echo "-Z renaming not yet supported" ; exit 1 ; }
[ -n "$mirror_path" ] && \
{ echo "-T mirroring not yet supported" ; exit 1 ; }
release_image_url="$omnios_amd64_url"
release_name="omnios"
release_image_file="$( basename $release_image_url )"
[ -d "${work_dir}/${release_name}" ] || \
mkdir -p "${work_dir}/${release_name}"
[ -d "${work_dir}/${release_name}" ] || \
{ echo "mkdir ${work_dir}/${release_name} failed" ; exit 1 ; }
# Needed for fetch to save the upstream file name
cd "${work_dir}/${release_name}"
f_fetch_image "$release_image_file" "$release_image_url"
##########
# DEBIAN #
##########
elif [ "$release_input" = "debian" ] ; then
release_type="raw"
case "$hw_platform" in
amd64) release_image_url="$debian_amd64_url"
;;
arm64) release_image_url="$debian_arm64_url"
;;
*) echo Invalid hardware architecture ; exit 1
;;
esac
release_name="debian"
release_image_file="$( basename $release_image_url )"
# TEST AND FAIL EARLY - can only test after parsing -r input
[ "$include_src" = 1 ] && \
{ echo "-s src not available with Debian images" ; exit 1 ; }
[ "$fs_type" = "zfs" ] && \
{ echo "-z ZFS not available with Debian images" ; exit 1 ; }
[ -n "$mirror_path" ] && \
{ echo "-T mirroring not supported" ; exit 1 ; }
# Redundant from FreeBSD/xz - refactor if possible
# Create the work directory if missing
[ -d "${work_dir}/${release_name}" ] || \
mkdir -p "${work_dir}/${release_name}"
[ -d "${work_dir}/${release_name}" ] || \
{ echo "mkdir ${work_dir}/${release_name} failed" ; exit 1 ; }
# Needed for fetch to save the upstream file name
cd "${work_dir}/${release_name}"
f_fetch_image "$release_image_file" "$release_image_url"
############
# ROUTEROS #
############
elif [ "$release_input" = "routeros" ] ; then
# For UEFI handling
attachment_required=1
release_type="zip"
case "$hw_platform" in
amd64) release_image_url="$routeros_amd64_url"
;;
arm64) release_image_url="$routeros_arm64_url"
;;
*) echo Invalid hardware architecture ; exit 1
;;
esac
release_name="routeros"
release_image_file="$( basename $release_image_url )"
# TEST AND FAIL EARLY - can only test after parsing -r input
[ "$include_src" = 1 ] && \
{ echo "-s src not available with RouterOS images" ; exit 1 ; }
[ "$fs_type" = "zfs" ] && \
{ echo "-z ZFS not available with RouterOS images" ; exit 1 ; }
[ -n "$mirror_path" ] && \
{ echo "-T mirroring not supported" ; exit 1 ; }
# Redundant from FreeBSD/xz - refactor if possible
[ -d "${work_dir}/${release_name}" ] || \
mkdir -p "${work_dir}/${release_name}"
[ -d "${work_dir}/${release_name}" ] || \
{ echo "mkdir ${work_dir}/${release_name} failed" ; exit 1 ; }
# Needed for fetch to save the upstream file name
cd "${work_dir}/${release_name}"
f_fetch_image "$release_image_file" "$release_image_url"
################
# CUSTOM IMAGE #
################
elif [ -f "$release_input" ] ; then # if a path to an image
# Arbitrary image may have arbitrary sources - manual copy needed
[ "$include_src" = 1 ] && \
{ echo "-s src not available with a custom image" ; exit 1 ; }
custom_image_file="$( basename $release_input )"
release_image_file="$release_input"
custom_image_file="$( basename $release_input )"
release_name="custom"
release_type="raw"
# Note that the vmrun.sh "file" test for boot blocks
#####################
# FREEBSD XZ IMAGES #
#####################
else
# Release version i.e. 15.0-CURRENT
release_type="xz"
echo "$release_input" | grep -q "-" || \
{ echo "Invalid release" ; exit 1 ; }
echo "$release_input" | grep -q "FreeBSD" && \
{ echo "Invalid release" ; exit 1 ; }
# Do we want to harmonize release_name="freebsd"?
release_name="$release_input"
release_version=$( echo "$release_input" | cut -d "-" -f 1 )
# Further validate the numeric version?
release_build=$( echo "$release_input" | cut -d "-" -f 2 )
case "$release_build" in
CURRENT|STABLE)
release_branch="snapshots"
;;
*)
release_branch="releases"
# This is a false assumption for ALPHA builds
;;
esac
# THIS CAN BE A MOVING TARGET
release_image_url="https://download.freebsd.org/${release_branch}/VM-IMAGES/${release_name}/${cpu_arch}/Latest/FreeBSD-${release_name}-${arch_string}-${fs_type}.raw.xz"
release_image_file="$( basename $release_image_url )"
[ -d "${work_dir}/${release_name}" ] || \
mkdir -p "${work_dir}/${release_name}"
[ -d "${work_dir}/${release_name}" ] || \
{ echo "mkdir ${work_dir}/${release_name} failed" ; exit 1 ; }
# Needed for fetch to save the upstream file name
cd "${work_dir}/${release_name}"
f_fetch_image "$release_image_file" "$release_image_url"
release_dist_url="https://download.freebsd.org/${release_branch}/$arch_string/$release_name"
src_url="https://download.freebsd.org/${release_branch}/${hw_platform}/${cpu_arch}/${release_name}/src.txz"
if [ "$include_src" = 1 ] ; then
if [ -r "src.txz" ] ; then
if [ "$offline_mode" = 0 ] ; then
fetch -a -i src.txz "$src_url" || \
{ echo "$src_url fetch failed" ; exit 1 ; }
fi
else
fetch -a "$src_url" || \
{ echo "$src_url fetch failed" ; exit 1 ; }
fi
fi
fi # End -r RELEASE HEAVY LIFTING
################################
# HEAVY LIFTING FLAG -t TARGET #
################################
# Default is img which can be user specified, a device under /dev/, or
# a path to a file
echo ; echo Status: Beginning -t target handling
if [ "$target_input" = "img" ] ; then
target_type="img"
[ -d "$work_dir" ] || mkdir -p "$work_dir"
[ -d "$work_dir" ] || { echo "mkdir -p $work_dir failed" ; exit 1 ; }
if [ -n "$mirror_path" ] ; then
[ "$mirror_path" = "img" ] || \
{ echo "-t and -T must both be images" ; exit 1 ; }
fi
if [ "$release_name" = "omnios" ] ; then
fs_type="zfs"
target_path="${work_dir}/omnios-${hw_platform}-${fs_type}.raw"
elif [ "$release_name" = "debian" ] ; then
fs_type="ext4"
target_path="${work_dir}/debian-${hw_platform}-${fs_type}.raw"
elif [ "$release_name" = "routeros" ] ; then
fs_type="ext4"
target_path="${work_dir}/routeros-${hw_platform}-${fs_type}.raw"
elif [ "$release_name" = "windows" ] ; then
fs_type="ntfs"
target_path="${work_dir}/windows-${hw_platform}-${fs_type}.raw"
elif [ "$release_name" = "custom" ] ; then
fs_type=""
target_path="${work_dir}/$custom_image_file"
else
# Challenge: FreeBSD does not have a notion of release_name
# TEST with "path" because that might get inserted
target_path="${work_dir}/FreeBSD-${hw_platform}-${release_input}-${fs_type}.raw"
fi
elif [ "$target_prefix" = "/dev/" ] ; then
grow_required=1
target_type="dev"
target_dev="$target_input"
# This is important for distinguishing extra ZFS handling
attachment_required=1
if [ -n "$mirror_path" ] ; then
[ "$mirror_path" = "img" ] && \
{ echo "-t and -T must both be devices" ; exit 1 ; }
target_dev2="$mirror_path"
target_size=$( diskinfo $target_dev | cut -f 3 )
mirror_size=$( diskinfo $target_dev2 | cut -f 3 )
if [ "$target_size" -gt "$mirror_size" ] ; then
echo ; echo Mirror device smaller than target
exit 1
fi
f_cleanse_device $target_dev2
fi
# After the size comparison in case the user backs out
f_cleanse_device $target_dev
elif [ "$target_type" = "oneboot" ] ; then
echo "Preparing for installation upon oneboot"
else # Input is a path to an image or invalid
# Validate parent directory
[ -d $( dirname "$target_input" ) ] || \
{ echo "-t directory path does not exist" ; exit 1 ; }
[ -w $( dirname "$target_input" ) ] || \
{ echo "-t directory path is not writable" ; exit 1 ; }
# Not true if Windows...
target_type="path"
target_path="$target_input"
fi # End -t TARGET HEAVY LIFTING
# grow_required=1 could have been set by target_type="dev"
# Test is regardless of where it was set
# Want grow for a device but do not need a size as it will use the whole dev
if [ "$grow_required" = 1 ] ; then
if [ ! "$target_type" = "dev" ] ; then
[ "$grow_size" ] || { echo "-g size required" ; exit 1 ; }
fi
fi
###########
# WINDOWS #
###########
echo ; echo Status: Checking iso_file for Windows
if [ "$xml_file" ] && [ "$iso_file" ] ; then
[ "$vmdk" = 1 ] && \
{ echo "Windows mode does not support VMDK" ; exit 1 ; }
# Ignore other unsupported flags at the risk of disappointment
# This will take care of quite a few of them
mount_required=0
include_src=0
attachment_required=0
which 7z > /dev/null 2>&1 || \
{ echo "7-zip package not installed" ; exit 1 ; }
which mkisofs > /dev/null 2>&1 || \
{ echo "cdrtools package not installed" ; exit 1 ; }
which xmllint > /dev/null 2>&1 || \
{ echo "libxml2 package not installed" ; exit 1 ; }
[ -d $work_dir/windows/iso ] || mkdir -p $work_dir/windows/iso
[ -d $work_dir/windows/iso ] || \
{ echo Making $work_dir/windows/iso failed ; exit 1 ; }
[ -f $work_dir/windows/iso/autorun.inf ] && \
rm -rf $work_dir/windows/iso/*
# Copy before changing directory and to validate early
echo ; echo Copying in $work_dir/windows/iso/autounattend.xml
cp $xml_file $work_dir/windows/iso/autounattend.xml || \
{ echo $xml_file copy failed ; exit 1 ; }
echo ; echo Validating $work_dir/windows/iso/autounattend.xml
xmllint --noout $work_dir/windows/iso/autounattend.xml ||
{ echo $work_dir/windows/iso/autounattend.xml failed to validate ; exit 1 ; }
cd $work_dir/windows/iso
echo ; echo Extracting $iso_file to $work_dir/windows/iso with 7z
7z x $iso_file || { echo UDF extraction failed ; exit 1 ; }
echo ; ls $work_dir/windows/iso
# Borrowing this convention for now
# TELL THE USER IT BEHAVES DIFFERENTLY - could copy things to the ISO
# and place them in with syntax in autounattend.xml
if [ "$keep_mounted" = "1" ] ; then
echo ; echo Pausing to allow manual configuration in another console
echo such as copying files to $work_dir/windows/iso
echo Press any key when ready to re-master the ISO
read waiting
fi
[ -f $work_dir/windows/iso/setup.exe ] || \
{ echo $work_dir/windows/iso/Setup.exe missing ; exit 1 ; }
echo ; echo Remastering ISO
mkisofs \
-quiet \
-b boot/etfsboot.com -no-emul-boot -c BOOT.CAT \
-iso-level 4 -J -l -D \
-N -joliet-long \
-relaxed-filenames \
-V "Custom" -udf \
-boot-info-table -eltorito-alt-boot -eltorito-platform 0xEF \
-eltorito-boot efi/microsoft/boot/efisys_noprompt.bin \
-no-emul-boot \
-o $work_dir/windows/windows.iso $work_dir/windows/iso || \
{ echo mkisofs failed ; exit 1 ; }
echo ; echo The resulting ISO image is $work_dir/windows/install.iso
cd -
# GENERATE WINDOWS ONE-TIME BOOT SCRIPTS
# Consider a warning that it will be writing to a device
# boot-windows-iso.sh to boot once to the ISO for auto-installation
#########
# BHYVE #
#########
# Used here and below
fbuf_string="-s 29,fbuf,tcp=0.0.0.0:5900,w=1024,h=768 -s 30,xhci,tablet"
cat << HERE > $work_dir/bhyve-windows-iso.sh
#!/bin/sh
[ -e /dev/vmm/$vm_name ] && { bhyvectl --destroy --vm=$vm_name ; sleep 1 ; }
[ -f /usr/local/share/uefi-firmware/BHYVE_UEFI.fd ] || \\
{ echo \"BHYVE_UEFI.fd missing\" ; exit 1 ; }
kldstat -q -m vmm || kldload vmm
HERE
if [ "$target_type" = "img" ] ; then
# Needed now and for later bhyve boot scripts use
vm_device="$target_path"
# Note the >> to not overwrite
cat << HERE >> $work_dir/bhyve-windows-iso.sh
echo ; echo Removing previous $vm_device if present
[ -f $vm_device ] && rm $vm_device
echo ; echo truncating ${grow_size}GB $vm_device
truncate -s ${grow_size}G $vm_device
HERE
else
vm_device="$target_dev"
fi
# Note the >> to not overwrite
cat << HERE >> $work_dir/bhyve-windows-iso.sh
bhyve -c 2 -m $vm_ram -H -A -D \\
-l com1,stdio \\
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd \\
-s 0,hostbridge \\
-s 1,ahci-cd,$work_dir/windows/windows.iso \\
-s 2,nvme,$vm_device \\
$fbuf_string \\
-s 31,lpc \\
$vm_name
sleep 2
bhyvectl --destroy --vm=$vm_name
HERE
echo Note: bhyve-windows-iso.sh
########
# QEMU #
########
cat << HERE > $work_dir/qemu-windows-iso.sh
#!/bin/sh
[ -f /usr/local/bin/qemu-system-x86_64 ] || \\
{ echo qemu-system-x86_64 not found ; exit ; }
[ -f /usr/local/share/edk2-qemu/QEMU_UEFI-x86_64.fd ] || \\
{ echo edk2-qemu-x64 not found ; exit ; }
HERE
if [ "$target_type" = "img" ] ; then
# Needed now and for later bhyve boot scripts use
vm_device="$target_path"