-
Notifications
You must be signed in to change notification settings - Fork 11
/
propagate.sh
973 lines (821 loc) · 28.6 KB
/
propagate.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
#!/bin/sh
#-
# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
#
# Copyright 2024, 2025 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.0.6
# propagate.sh - Packaged Base installer to boot environments and VM-IMAGES
##############
# MOTIVATION #
##############
# The "p" and "g" in propagate are for "pkg", obviously.
#
# The 2018 propagate.sh installed upstream FreeBSD distribution sets or
# custom-built binaries to boot environments, and this incarnation does
# the same with upstream or OccamBSD-built FreeBSD base packages.
#########
# USAGE #
#########
# propagate.sh must be run with root privileges and has three modes of operation
# to install a given release to:
#
# * A pre-existing directory or mounted dataset for use with jail(8)
# * A new boot environment that it will create based on the name provided
# * A new PkgBase-based VM-IMAGE
#
# The package selection is currently determined by the base_pkg_exclusions
# variable below. Example sets are provided and the community needs to
# come up with a syntax/strategy for base package selection. Some options are:
#
# * Meta Packages
# * Long lists
# * grep -vE exclusions
# * pkg query/rquery -e evaluation
#
# Challenges: You probably want to exclude packages in case a new one
# appears or the base set is re-arranged. Dependencies may surprise you.
#
#
# To install 14.2-RELEASE to the default location of /tmp/propagate/root,
# copying in packages from the host (must also be 14.2-RELEASE), and clean the
# pkg cache on the destination:
#
# sh propagate.sh -r 14.2-RELEASE -c -C
#
# Cleaning up in advance is left to the user and remember to unmount the /dev
# directory of the destination if you want to delete the target root:
#
# umount /tmp/propagate/root/dev
#
#
# To install 14.2-RELEASE to a new boot environment "test" on the zpool "zroot":
#
# sh propagate.sh -r 14.2-RELEASE -c -C -t zroot/ROOT/test
#
# -m will keep it mounted for further configuration
# -d will install ALL base packages (you probably do not want that)
#
#
# To create a 14.2-RELEASE PkgBase VM-IMAGE:
#
# sh propagate.sh -r 14.2-RELEASE -d -c -C -v
#
# To install 15.0-CURRENT to a new boot environment "test" on the zpool "zroot":
#
# sh propagate.sh -r 15.0-CURRENT -C -t t14/ROOT/pb15
#####################
# NOTES AND CAVEATS #
#####################
# This syntax aims to be consistent with occambsd.sh and imagine.sh
#
# That the directory/dataset distinction is made by the leading slash
#
# Cleanup is left to the reader
#
# Q: How to choose a PkgBase kernel such as nodebug?
# A: A loader variable
#########
# USAGE #
#########
f_usage() {
echo ; echo "USAGE:"
echo "-r <release> (i.e. 14.2-RELEASE | 15.0-CURRENT - Required)"
echo "-a <architecture> [ amd64 | arm64 ] (Default: Host)"
echo "-t <target root directory> (Boot environment or Jail path"
echo " i.e. zroot/ROOT/pkgbase15, zroot/jails/pkgbase15 datasets or"
echo " /jails/myjail directory"
echo " Default: /tmp/propagate/root unless VM image is selected)"
echo "-m (Keep boot environment mounted for further configuration)"
echo "-d (Default FreeBSD installation package set with sources)"
echo "-p \"<additional packages>\" (Quoted space-separated list)"
echo "-s (Perform best-effort sideload of the current configuration)"
echo "-c (Copy cached FreeBSD- packages from the host - must match!)"
echo "-C (Clean package cache after installation)"
echo "-v (Generate VM image and boot scripts)"
echo "-O <output directory/work> (Default: /tmp/propagate)"
echo
exit 0
}
# Attic
# echo "-j (Use Jail package set)"
# echo "-q (quarterly package branch - default latest)"
# echo "-3 (Install lib32 packages)"
# echo "-z (Prepare target for ZFS boot)"
###################################
# INTERNAL VARIABLES AND DEFAULTS #
###################################
release_input=""
abi_major=""
abi_minor=""
copy_glob=""
hw_platform=$( uname -m ) # i.e. amd64|arm64
cpu_arch=$( uname -p ) # i.e. amd64|aarch64
target_input=""
target_prefix=""
target_type="" # directory or dataset
keep_mounted=0
#############################
# USER-OVERRIDABLE DEFAULTS #
#############################
mount_point="/tmp/propagate/root"
work_dir="/tmp/propagate"
default_packages=0
# A full-featured system, including src
#base_pkg_exclusions="dbg|dev|lib32|tests"
# A full-featured system, without src
base_pkg_exclusions="dbg|dev|lib32|tests|src|src-sys"
# Why does freebsd-ftpd slip in? ftp|ftpd dependency?
# FreeBSD-zoneinfo: 14.2 [FreeBSD-base]
# freebsd-ftpd: 20240719 [FreeBSD-latest]
# A lightweight jail system
# Why does this set install zfs no matter how much it is excluded? Dependency?
#base_pkg_exclusions="zfs|ufs-lib32|nfs|ipf|ipfw|telnet|sendmail|rcmds|dhclient|pf|kernel|kernel-generic|kernel-generic-mmccam|kernel-minimal|dbg|dev|lib32|man|tests|src|src-sys|acpi|apm|autofs|bhyve|bluetooth|bootloader|bsdinstall|bsnmp|ccdconfig|clang|cxgbe-tools|dtrace|efi-tools|elftoolchain|examples|fwget|games|geom|ggate|hast|hostapd|hyperv-tools|iscsi|lld|lldb|lp|mlx-tools|nvme-tools|ppp|rescue|rdma|smbutils|src|src-sys|tests|ufs|wpa|ftp|ftpd"
additional_packages=""
sideload=0
copy_cache=0
clean_cache=0
mkvm_image=0
# Check after boot/zfs with a full installation
# Drawing from /usr/src/release/tools/vmimage.subr
skel_dirs="boot/efi/EFI/BOOT
dev
etc/pkg
var/cache/pkg
var/db/pkg
usr/local/etc/pkg/repos
usr/share/keys/pkg/trusted
usr/share/keys/pkg/revoked
mnt
media
root
home
tmp
net
proc
usr/ports
usr/src
usr/obj
var/audit
var/crash
var/log
var/mail
var/tmp
boot/zfs
etc/zfs
boot/firmware
boot/modules
boot/images
boot/fonts
boot/uboot
etc/jail.conf.d
etc/profile.d
etc/rc.conf.d
etc/sysctl.kld.d
etc/zfs/compatibility.d
var/db/etcupdate
var/db/etcupdate/current
var/db/etcupdate/current/boot
var/db/etcupdate/current/etc
var/db/etcupdate/current/etc/autofs
var/db/etcupdate/current/etc/bluetooth
var/db/etcupdate/current/etc/cron.d
var/db/etcupdate/current/etc/defaults
var/db/etcupdate/current/etc/devd
var/db/etcupdate/current/etc/dma
var/db/etcupdate/current/etc/gss
var/db/etcupdate/current/etc/kyua
var/db/etcupdate/current/etc/mail
var/db/etcupdate/current/etc/mtree
var/db/etcupdate/current/etc/newsyslog.conf.d
var/db/etcupdate/current/etc/pam.d
var/db/etcupdate/current/etc/periodic
var/db/etcupdate/current/etc/periodic/daily
var/db/etcupdate/current/etc/periodic/monthly
var/db/etcupdate/current/etc/periodic/security
var/db/etcupdate/current/etc/periodic/weekly
var/db/etcupdate/current/etc/pkg
var/db/etcupdate/current/etc/ppp
var/db/etcupdate/current/etc/rc.d
var/db/etcupdate/current/etc/security
var/db/etcupdate/current/etc/ssh
var/db/etcupdate/current/etc/ssl
var/db/etcupdate/current/etc/syslog.d
var/db/etcupdate/current/root
var/db/etcupdate/current/usr
var/db/etcupdate/current/usr/share
var/db/etcupdate/current/usr/share/nls
var/db/etcupdate/current/var
var/db/etcupdate/current/var/crash
usr/libdata"
# Note permissions
#Note empty files /etc/zfs/exports /etc/zfs/exports.lock
#####################################
# USER INPUT AND VARIABLE OVERRIDES #
#####################################
while getopts r:a:t:mdp:scCvzO opts ; do
case $opts in
r)
[ "$OPTARG" ] || f_usage
release_input="$OPTARG"
echo "$release_input" | grep -q "\." || f_usage
echo "$release_input" | grep -q "-" || f_usage
release_version=$( echo "$release_input" | cut -d "-" -f 1 )
release_build=$( echo "$release_input" | cut -d "-" -f 2 )
abi_major="$( echo "$release_version" | cut -d "." -f 1 )"
# cut -d "." -f 2 only works with a .N
abi_minor="$( echo "$release_version" | cut -d "." -f 2 )"
if [ "$release_build" = "CURRENT" ] ; then
abi_string="base_latest"
copy_glob="${abi_major}.snap"
else
abi_string="base_release_${abi_minor}"
copy_glob="$release_version"
fi
# Perform more validation
;;
a)
case "$OPTARG" in
amd64)
hw_platform="amd64"
;;
arm64)
hw_platform="aarch64"
;;
*)
echo "Invalid architecture"
f_usage
;;
esac
;;
t)
target_input="$OPTARG"
target_prefix=$( printf %.1s "$target_input" )
if [ "$target_prefix" = "/" ] ; then
if [ "$target_input" = "/" ] ; then
echo "Target is / - Exiting"
exit 1
fi
echo Creating root directory
[ -d "$target_input" ] || mkdir -p "$target_input"
[ -d "$target_input" ] || \
{ echo "mkdir $target_input failed" ; exit 1 ; }
target_type="directory"
mount_point="$target_input"
else
# DEBUG SHELLCHECK SUGGESTS QUOTING AFTER THE DOLLAR SIGN
zpool get name "$( echo "$target_input" | cut -d "/" -f 1 )" \
> /dev/null 2>&1 || \
{ echo "Target $target_input likely invalid" ; exit 1 ; }
zfs get name "$target_input" > /dev/null 2>&1 && \
{ echo "Target exists - Exiting" ; exit 1 ; }
target_type="dataset"
echo Creating root dataset
zfs get name "$target_input" > /dev/null 2>&1 || \
zfs create -o canmount=noauto -o mountpoint=/ \
"$target_input"
zfs get name "$target_input" > /dev/null 2>&1 || \
{ echo "root dataset failed to create" ; exit 1 ; }
# PICK YOUR DATASET PROPERTY PARSING METHOD OF CHOICE FROM BSDINSTALL OR VMIAGE
# From /usr/libexec/bsdinstall/zfsboot
# echo Sourcing bsdconfig/bsdintall functions/variables
# # Obtain default ZFSBOOT_DATASETS
# . /usr/libexec/bsdinstall/zfsboot || \
# { echo "zfsboot failed to source" ; exit 1 ; }
# . /usr/share/bsdconfig/common.subr || \
# { echo "common.subr failed to source" ; exit 1 ; }
# FOLLOWING /lab/github/occambsd/vmimage.subr
# zfs create -o canmount=noauto -o mountpoint=/ \
# "$target_input"
# -o fs=zroot/home\;mountpoint=/home \
zfs create -o canmount=noauto \
-o mountpoint=/home \
"$target_input/home" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/tmp\;mountpoint=/tmp\;exec=on\;setuid=off \
zfs create -o canmount=noauto \
-o mountpoint=/tmp -o exec=on -o setuid=off \
"$target_input/tmp" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/usr\;mountpoint=/usr\;canmount=off \
# OVERRIDING canmount=off with noauto for nesting
zfs create \
-o mountpoint=/usr -o canmount=noauto \
"$target_input/usr" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/usr/ports\;setuid=off \
zfs create -o canmount=noauto \
-o setuid=off \
"$target_input/usr/ports" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/usr/src \
zfs create -o canmount=noauto \
"$target_input/usr/src" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/usr/obj \
zfs create -o canmount=noauto \
"$target_input/usr/obj" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/var\;mountpoint=/var\;canmount=off \
# OVERRIDING canmount=off with noauto for nesting
zfs create \
-o mountpoint=/var -o canmount=noauto \
"$target_input/var" || \
# -o fs=zroot/var/audit\;setuid=off\;exec=off \
zfs create -o canmount=noauto \
-o setuid=off -o exec=off \
"$target_input/var/audit" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/var/crash\;setuid=off\;exec=off \
zfs create -o canmount=noauto \
-o setuid=off -o exec=off \
"$target_input/var/crash" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/var/log\;setuid=off\;exec=off \
zfs create -o canmount=noauto \
-o setuid=off -o exec=off \
"$target_input/var/log" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/var/mail\;atime=on \
zfs create -o canmount=noauto \
-o atime=on \
"$target_input/var/mail" || \
{ echo "dataset failed to create" ; exit 1 ; }
# -o fs=zroot/var/tmp\;setuid=off
zfs create -o canmount=noauto \
-o setuid=off \
"$target_input/var/tmp" || \
{ echo "dataset failed to create" ; exit 1 ; }
# BECTL HANDLES OUR NESTING!
echo "Mounting root dataset"
bectl mount "$( basename "$target_input" )" \
"${mount_point:?}" || \
{ echo "target BE mount failed" ; exit 1 ; }
fi # End dataset handling
;;
m)
keep_mounted=1
;;
d)
default_packages=1
# Not using this approach yet as it may trip up the regex
# base_pkg_exclusions=""
;;
p)
[ "$OPTARG" ] || f_usage
additional_packages="$OPTARG"
;;
s)
sideload=1
;;
c)
copy_cache=1
;;
C)
clean_cache=1
;;
v)
mkvm_image=1
;;
O)
[ "$OPTARG" ] || f_usage
work_dir="$OPTARG"
# Verify that it auto-creates
# [ -d "${work_dir:?}" ] || \
# { echo "${work_dir:?} not found" ; exit 1 ; }
;;
*)
f_usage
;;
esac
done
[ "$release_input" ] || f_usage
case "$hw_platform" in
amd64)
cpu_arch="amd64"
;;
arm64)
cpu_arch="aarch64"
;;
*)
echo "Invalid architecture"
exit 1
;;
esac
ABI="FreeBSD:${abi_major}:${cpu_arch}"
###############
# DIRECTORIES #
###############
# Consider root_mount_point or even target_root_mount_point for clarity
# Likely a boot environment or jail, but could be a nested one for VM creation
mount_point="/tmp/propagate/root"
# At a minimum the parent of the default mount point
# Location of boot scripts, top root of deeply-nested VM tree
work_dir="/tmp/propagate"
# root directory and dataset should be agnostic
# A dataset is assumed to be a boot environemnt BUT could be a jail
# A VM image is NOT a dataset...
#########
# TESTS #
#########
if [ "$target_input" = "dataset" ] && [ "$mkvm_image" = "1" ] ; then
echo "A VM image assumes a work directory but not target directory"
exit 1
fi
if [ "$target_type" = "dataset" ] && [ "$mkvm_image" = "1" ] ; then
echo "A VM image assumes a transient directory"
exit 1
fi
if [ "$mkvm_image" = "1" ] ; then
# Need a work directory and prefixed parent directories
# Either use the default or overridden work directory
# work_dir="/tmp/propagate"
# Only used by VM-IMAGES so set here
fake_obj_dir="$work_dir"
fake_src_dir="$work_dir/src"
mount_point="$fake_obj_dir$fake_src_dir/amd64.amd64/release/vm"
echo Making directories
mkdir -p "$fake_src_dir/release/scripts" || { echo "failed" ; exit 1 ; }
mkdir -p "$fake_src_dir/release/tools" || { echo "failed" ; exit 1 ; }
mkdir -p "${mount_point:?}/dev"
echo Fetching release script and tool
fetch https://cgit.freebsd.org/src/plain/release/scripts/mk-vmimage.sh \
-o "$fake_src_dir/release/scripts/mk-vmimage.sh" || \
{ echo "mk-vmimage.sh fetch failed" ; exit 1 ; }
fetch https://cgit.freebsd.org/src/plain/release/tools/vmimage.subr \
-o "$fake_src_dir/release/tools/vmimage.subr" || \
{ echo "vmimage.subr fetch failed" ; exit 1 ; }
fi # End extra VM scaffolding
echo Creating skeleton directories
for directory in $skel_dirs ; do
mkdir -vp "${mount_point:?}/${directory:?}" || \
{ echo "mkdir $directory failed" ; exit 1 ; }
done
#pkg-static: Cannot open dev/null
echo Mounting devfs for pkg-static
mount -t devfs -o ruleset=4 devfs "${mount_point:?}/dev" || \
{ echo "mount devfs failed" ; exit 1 ; }
mkdir -p "${mount_point:?}/usr/share/keys/pkg/trusted"
echo Generating pkg.freebsd.org.2013102301 key
cat <<- HERE > "${mount_point:?}/usr/share/keys/pkg/trusted/pkg.freebsd.org.2013102301"
function: "sha256"
fingerprint: "b0170035af3acc5f3f3ae1859dc717101b4e6c1d0a794ad554928ca0cbb2f438"
HERE
[ -f "${mount_point:?}/usr/share/keys/pkg/trusted/pkg.freebsd.org.2013102301" ] || { echo "pkg.freebsd.org.2013102301 failed" ; exit 1 ; }
#du -h ${mount_point:?}
########################################
# FreeBSD.conf REPO CONFIGURATION FILE #
########################################
echo ; echo Generating "${mount_point:?}/etc/pkg/FreeBSD-base.conf"
mkdir -p "${mount_point:?}/etc/pkg" || \
{ echo "mkdir ${mount_point:?}/etc/pkg failed" ; exit ; }
# WILL WE NEED TO OVERWRITE $ABI when cross installing?
# UCL!
# <<- will strip tab indenting
# 'HERE' to not expand, allowing $ABI
cat <<- HERE > "${mount_point:?}/etc/pkg/FreeBSD-base.conf"
FreeBSD-base: {
priority: 10
enabled: yes
url: "pkg+https://pkg.FreeBSD.org/\${ABI}/${abi_string}"
mirror_type: "srv"
signature_type: "fingerprints"
fingerprints: "/usr/share/keys/pkg"
}
HERE
[ -f "${mount_point:?}/etc/pkg/FreeBSD-base.conf" ] || \
{ echo FreeBSD-base.conf failed ; exit 1 ; }
echo ; echo Generating "${mount_point:?}/etc/pkg/FreeBSD-latest.conf"
# Priority overrides the default of quarterly
# 'HERE' = NO SHELL VARIABLE EXPANSION
#cat <<- 'HERE' > "${mount_point:?}/etc/pkg/FreeBSD-latest.conf"
cat <<- HERE > "${mount_point:?}/etc/pkg/FreeBSD-latest.conf"
FreeBSD-latest: {
priority: 10
enabled: yes
url: "pkg+https://pkg.FreeBSD.org/\${ABI}/latest",
mirror_type: "srv",
signature_type: "fingerprints",
fingerprints: "/usr/share/keys/pkg",
enabled: yes
}
HERE
[ -f "${mount_point:?}/etc/pkg/FreeBSD-latest.conf" ] || \
{ echo "FreeBSD-latest.conf failed" ; exit 1 ; }
mkdir -p "${mount_point:?}/usr/local/etc/pkg/repos" || \
{ echo "mkdir ${mount_point:?}/usr/local/etc/pkg/repos failed" ; exit 1 ; }
echo ; echo "Generating ${mount_point:?}/usr/local/etc/pkg/repos/FreeBSD-quarterly.conf"
cat <<- HERE > "${mount_point:?}/usr/local/etc/pkg/repos/FreeBSD-quarterly.conf"
FreeBSD: { enabled: no }
HERE
[ -f "${mount_point:?}/usr/local/etc/pkg/repos/FreeBSD-quarterly.conf" ] || \
{ echo "FreeBSD-quarterly.conf failed" ; exit 1 ; }
echo "Installing pkg"
pkg \
--option ABI="${ABI:?}" \
--option IGNORE_OSVERSION="yes" \
--rootdir "${mount_point:?}" \
--repo-conf-dir "${mount_point:?}/etc/pkg" \
install -y -- pkg || \
{ echo "pkg install failed" ; exit 1 ; }
#####################
# INSTALL PREFLIGHT #
#####################
################
# BASE INSTALL #
################
if [ "$copy_cache" = "1" ] ; then
echo ; echo "Copying /var/cache/pkg/FreeBSD- packages from the host"
#cp: /var/cache/pkg/FreeBSD-*14.2.pkg: No such file or directory
# /var/cache/pkg/FreeBSD-vi-14.2.pkg
# This will fail if there is not a match
# cp -p "/var/cache/pkg/FreeBSD-*${copy_glob}.pkg" \
find /var/cache/pkg -type f | grep "$copy_glob" | xargs -I % cp % \
"${mount_point:?}/var/cache/pkg/"
fi
#echo ; echo SMOKE TEST: Counting requested packages
#pkg \
# --option ABI="${ABI:?}" \
# --option IGNORE_OSVERSION="yes" \
# --rootdir "${mount_point:?}" \
# --repo-conf-dir "${mount_point:?}/etc/pkg" \
# rquery --repository="FreeBSD-base" '%n' \
# | grep -vE "($base_pkg_exclusions)" \
# | wc -l
echo ; echo Installing base packages
if [ "$default_packages" = "1" ] || [ -n "$base_pkg_exclusion" ] ; then
# No special requests, install every available FreeBSD-* package
# Strong quoting required for egrep and variables
pkg \
--option ABI="${ABI:?}" \
--option IGNORE_OSVERSION="yes" \
--rootdir "${mount_point:?}" \
--repo-conf-dir "${mount_point:?}/etc/pkg" \
rquery --repository="FreeBSD-base" '%n' \
| grep -vE "($base_pkg_exclusions)" \
| xargs -o pkg \
--option ABI="${ABI:?}" \
--option IGNORE_OSVERSION="yes" \
--rootdir "${mount_point:?}" \
--repo-conf-dir "${mount_point:?}/etc/pkg" \
install \
--
else
pkg \
--option ABI="${ABI:?}" \
--option IGNORE_OSVERSION="yes" \
--rootdir "${mount_point:?}" \
--repo-conf-dir "${mount_point:?}/etc/pkg" \
rquery --repository="FreeBSD-base" '%n' \
| grep -vE "($base_pkg_exclusions)" \
| xargs -o pkg \
--option ABI="${ABI:?}" \
--option IGNORE_OSVERSION="yes" \
--rootdir "${mount_point:?}" \
--repo-conf-dir "${mount_point:?}/etc/pkg" \
install \
--
fi
#echo DEBUG checking the size of the result
# du -h -d 1 "${mount_point:?}"
#######################
# ADDITIONAL PACKAGES #
#######################
# Removing quotation marks here - they are double quotes everywhere but became
# single on the ride
if [ "$additional_packages" ] ; then
echo Installing additional packages
pkg \
--option ABI="${ABI:?}" \
--option IGNORE_OSVERSION="yes" \
--rootdir "${mount_point:?}" \
--repo-conf-dir "${mount_point:?}/etc/pkg" \
install -y "$additional_packages" || \
{ echo "Additional packages failed" ; exit 1 ; }
fi
#################
# CONFIGURATION #
#################
if [ "$sideload" = "1" ] ; then
echo "Copying configuration files - missing ones will fail for now"
[ -f /boot/loader.conf ] && cp /boot/loader.conf \
"${mount_point:?}/boot/"
[ -f /etc/fstab ] && cp /etc/fstab "${mount_point:?}/etc/"
#touch "${mount_point:?}/etc/fstab"
[ -f /etc/rc.conf ] && cp /etc/rc.conf "${mount_point:?}/etc/"
[ -f /etc/sysctl.conf ] && cp /etc/sysctl.conf "${mount_point:?}/etc/"
[ -f /etc/group ] && cp /etc/group "${mount_point:?}/etc/"
[ -f /etc/pwd.db ] && cp /etc/pwd.db "${mount_point:?}/etc/"
[ -f /etc/spwd.db ] && cp /etc/spwd.db "${mount_point:?}/etc/"
[ -f /etc/master.passwd ] && cp /etc/master.passwd \
"${mount_point:?}/etc/"
[ -f /etc/passwd ] && cp /etc/passwd "${mount_point:?}/etc/"
[ -f /etc/wpa_supplicant.conf ] && \
cp /etc/wpa_supplicant.conf "${mount_point:?}/etc/"
# Was failing on host keys
cp -rp /etc/ssh/* "${mount_point:?}/etc/ssh/"
cp -rp /root/.ssh "${mount_point:?}/root/"
echo "Sideloading packages"
pkg prime-list | xargs -o pkg \
--option ABI="${ABI:?}" \
--option IGNORE_OSVERSION="yes" \
--rootdir "${mount_point:?}" \
--repo-conf-dir "${mount_point:?}/etc/pkg" \
install -y -- || \
{ echo "Package installation failed" ; exit 1 ; }
elif [ "$target_type" = "dataset" ] || [ "$mkvm_image" = "1" ] ; then
# Use sysrc when possible
cat << HERE > ${mount_point:?}/boot/loader.conf
kern.geom.label.disk_ident.enable="0"
kern.geom.label.gptid.enable="0"
cryptodev_load="YES"
zfs_load="YES"
HERE
echo ; echo The loader.conf reads:
cat ${mount_point:?}/boot/loader.conf
echo
cat << HERE > ${mount_point:?}/etc/rc.conf
hostname="propagate"
zfs_enable="YES"
HERE
echo ; echo The rc.conf reads:
cat ${mount_point:?}/etc/rc.conf
echo
fi
###############
# CLEAN CACHE #
###############
#echo DEBUG checking the size of the pkg cache
# du -h -d 1 "${mount_point:?}/var/cache/pkg"
if [ "$clean_cache" = "1" ] ; then
echo "Cleaning ${mount_point:?}/var/cache/pkg/"
find -s -f "${mount_point:?}/var/cache/pkg/" -- -mindepth 1 -delete
fi
#echo DEBUG checking the size of the pkg cache
# du -h -d 1 "${mount_point:?}/var/cache/pkg"
#################
# UPDATE SCRIPT #
#################
cat << HERE > "${mount_point:?}/root/update-pkgbase.sh"
#!/bin/sh
pkg upgrade
if [ -f /etc/ssh/sshd_config.save ] ; then
mv /etc/ssh/sshd_config /etc/ssh/sshd_config.default
cp /etc/ssh/sshd_config.pkgsave /etc/ssh/sshd_config
# Check if running?
service sshd restart
fi
if [ -f /etc/group.pkgsave ] ; then
mv /etc/group /etc/group.default
cp /etc/group.pkgsave /etc/group
fi
if [ -f /etc/master.passwd.save ] ; then
mv /etc/master.passwd /etc/master.passwd.default
cp /etc/master.passwd.pkgsave /etc/master.passwd
pwd_mkdb -p /etc/master.passwd
fi
if [ -f /etc/sysctl.conf.pkgsave ] ; then
mv /etc/sysctl.conf /etc/sysctl.conf.default
cp /etc/sysctl.conf.pkgsave /etc/sysctl.conf
fi
if [ -f /etc/shells.pkgsave ] ; then
mv /etc/shells /etc/shells.default
cp /etc/shells.pkgsave /etc/shells
fi
HERE
[ -f "${mount_point:?}/root/update-pkgbase.sh" ] || \
{ echo "update-packages.sh failed" ; exit 1 ; }
############
# VM-IMAGE #
############
# This with generate a script to generate a script to create a VM image
# A two-step process has the benefit of allowing configuration of the
# propagated system prior to creating the VM image.
# But acrobatics are required.
# /usr/src/release/scripts/mk-vmimage.sh exists to automate VM image creation.
# Unfortunately, it assumes installworld|installkernel|make distribution etc.
# /usr/src/release/tools/vmimage.subr is a library for VM image creation.
# Unfortunately, it has a hard requirement of being used by a script in /usr/src
if [ "$mkvm_image" = 1 ] ; then
# Deleting if re-running
[ -f "${fake_src_dir}/release/scripts/propagate-mkvm-image.sh" ] && \
rm "${fake_src_dir}/release/scripts/propagate-mkvm-image.sh"
[ -e "${mount_point:?}/vm/fd" ] && umount "${mount_point:?}/vm/dev"
#################################################
# COPY FROM DESTINATION TO A FAKE SRC DIRECTORY #
#################################################
# Satisfying dependencies in the order in which they failed
mkdir -p "$fake_src_dir/stand/efi/loader_lua" || \
{ echo "Failed to make loader_lua directory" ; exit 1 ; }
cp "$mount_point/boot/loader_lua.efi" \
"$fake_src_dir/stand/efi/loader_lua/" || \
{ echo "loader_lua.efi failed to copy" ; exit 1 ; }
mkdir -p "$fake_src_dir/stand/i386/pmbr" || \
{ echo "pmbr directory failed to make" ; exit 1 ; }
cp "$mount_point/boot/pmbr" \
"$fake_src_dir/stand/i386/pmbr/" || \
{ echo "pmbr failed to copy" ; exit 1 ; }
mkdir -p "$fake_src_dir/stand/i386/gptzfsboot" || \
{ echo "gptzfsboot directory failed to make" ; exit 1 ; }
cp "$mount_point/boot/gptzfsboot" \
"$fake_src_dir/stand/i386/gptzfsboot/" || \
{ echo "gptzfsboot failed to copy" ; exit 1 ; }
mkdir -p "$fake_src_dir/tools/boot" || \
{ echo "$fake_src_dir/tools/boot failed to make" ; exit 1 ; }
# ACTUAL HOST SOURCE DIRECTORY
cp /usr/src/tools/boot/install-boot.sh \
"$fake_src_dir/tools/boot/" || \
{ echo "install-boot.sh failed to copy" ; exit 1 ; }
[ -f "$fake_src_dir/release/scripts/propagate-mkvm-image.sh" ] && \
rm "$fake_src_dir/release/scripts/propagate-mkvm-image.sh"
cat << HERE > "$fake_src_dir/release/scripts/propagate-mkvm-image.sh"
# Trying this in an attempt to call from the original script
cd "$fake_src_dir/release/scripts/"
[ -e "${mount_point:?}/dev/fd" ] && umount "${mount_point:?}/dev"
# REQUIRED
WORLDDIR=$fake_src_dir
export MAKEOBJDIRPREFIX=$fake_obj_dir
VMSIZE=8g
#export VMFSLIST=zfs
VMFS=zfs
TARGET=amd64
TARGET_ARCH=amd64
VMFORMAT=raw
# THE IMAGE OR PARTITION?
#export VMIMAGE=$fake_obj_dir/../vm.zfs.img
VMBASE=raw.zfs.img
VMIMAGE=vm.zfs.img
# WHOOPS: SETTING VMROOT MAY BLOW UP makefs (core dump)
DESTDIR=$mount_point
#DESTDIR=$fake_obj_dir
SWAPSIZE=1g
. ../tools/vmimage.subr
vm_create_disk
HERE
[ -f "$fake_src_dir/release/scripts/propagate-mkvm-image.sh" ] || \
{ echo "$fake_src_dir/release/scripts/propagate-mkvm-image.sh failed" ; exit 1 ; }
echo ; echo "Generating the VM-IMAGE"
sh $fake_src_dir/release/scripts/propagate-mkvm-image.sh || \
{ echo "sh $fake_src_dir/release/scripts/propagate-mkvm-image.sh failed" ; exit 1 ; }
echo "Generating simple boot script"
cat << HERE > "$fake_src_dir/release/scripts/boot-vm.sh"
#!/bin/sh
[ \$( id -u ) = 0 ] || { echo "Must be root" ; exit 1 ; }
[ -e /dev/vmm/propagate ] && { bhyvectl --destroy --vm=propagate ; sleep 1 ; }
[ -f /usr/local/share/uefi-firmware/BHYVE_UEFI.fd ] || \\
{ echo \"BHYVE_UEFI.fd missing\" ; exit 1 ; }
kldstat -q -m vmm || kldload vmm
sleep 1
bhyve -m 1G -A -H -l com1,stdio -s 31,lpc -s 0,hostbridge \\
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd \\
-s 2,virtio-blk,$fake_src_dir/release/scripts/vm.zfs.img \\
propagate
sleep 2
bhyvectl --destroy --vm=propagate
HERE
echo ; echo "To boot the VM image, run:"
echo ; echo "sh $fake_src_dir/release/scripts/boot-vm.sh"
echo
fi # End if VM-IMAGE
##################
# MOUNT HANDLING #
##################
# DEBUG DECIDE ON CORRECT BEHAVIOR
if [ "$keep_mounted" = 0 ] ; then
if [ "$target_type" = "dataset" ] ; then
echo ; echo "Unmounting ${mount_point:?}"
umount "${mount_point:?}/dev" || \
{ echo "${mount_point:?}/dev umount failed" ; exit 1 ; }
# umount ${mount_point:?} || \
# { echo "${mount_point:?} failed to unmount" ; exit 1 ; }
bectl umount "$( basename "$target_input" )" || \
{ echo "target BE umount failed" ; exit 1 ; }
else
echo ; echo "To unmount the target root, run:"
echo "umount ${mount_point:?}/dev"
echo "umount ${mount_point:?}"
echo
fi
fi
# No closing exit for use wrapped by other scripts