forked from nomadbsd/NomadBSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·1580 lines (1414 loc) · 40.3 KB
/
build
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
#
# Copyright (c) 2021, The NomadBSD Project
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
#
# COMMANDS
#
# all Set up base system, build and install kernel if BUILD_KERNEL is
# set to YES, intall packages, ports, software from Git repos,
# create uzip image, and finally build the NomadBSD image.
#
# init Create base system, add the nomad user, and copy the
# configuration files to the base system.
#
# initbase Create nomad user, and copy all configuration files from
# config/ and nomad/ to base system.
#
# cleanup Unmount filesystems, remove mount points, and detach md devices.
#
# uzip Create uzip image.
#
# handbook Build and install the NomadBSD handbook.
#
# image Build the NomadBSD image.
#
# modules Build and install kernel modules from the "modules" directory
#
# ports Build and install all ports defined in build.cfg
#
# pkgs Install all packages from pkg.list
#
# git Install software from Git repos defined in build.cfg
#
# pkgcfg Set up configuration files and users required by installed
# software packages.
#
# reset Build and install nomadbsd-reset
#
# resume Continue where the build failed.
#
# setupgui (Re)build and (re)install nomadbsd-setup-gui
#
# installgui (Re)build and (re)install nomadbsd-install-gui
#
# nbsdupdate Install nomadbsd-update
#
# unionfs (Re)build and (re)install unionfs-fuse
#
# usrcfg Install all files from nomad/ in ${SYSDIR]/home/nomad
#
# usrclean Remove all files from ${SYSDIR}/home/nomad
#
# clean Remove base system dir, distfiles, patch dir, and uzip images.
#
# dmconfig (Re)build and (re)install nomadbsd-dmconfig-gui
#
. ./common
case ${RELEASE##*-} in
RELEASE|BETA|RC*)
URL="${BASEURL}/releases";;
*)
URL="${BASEURL}/snapshots";;
esac
# Where bsdinstall stores fetched files.
DISTDIR="${WORKDIR}/dists"
PATCHDIR="${WORKDIR}/patchset"
XORG_CONF_D="${SYSDIR}/usr/local/etc/X11/xorg.conf.d"
FONTSDIR="${SYSDIR}/usr/local/share/fonts"
FONTPATHS_FILE="${XORG_CONF_D}/files.conf"
UZIP_IMAGE="${WORKDIR}/uzip_image"
UZIP_MNT="${WORKDIR}/uzip_mnt"
DESTDIR="${WORKDIR}/mnt"
TMP_IMAGE_NAME="${WORKDIR}/tmp_image.$$"
TMP_BASE_DIR="${WORKDIR}/tmp_mnt.$$"
BUILD_DEPENDENCIES=$(cat << BUILD_DEPENCIES_LIST_EOF
x11-fonts/mkfontscale
textproc/markdown
devel/git
BUILD_DEPENCIES_LIST_EOF
)
BUILD_STAGES=$(cat << BUILD_STAGES_END
build_modules
install_packages
install_from_git_repos
build_setupgui
build_installgui
build_addusergui
build_nomadbsd_update
build_dmconfig
build_chusr
build_lbi_gui
install_nomadbsd_reset
build_unionfs
install_ports
build_nvidia_drivers
post_pkg_config
build_uzip_image
build_image
cleanup
BUILD_STAGES_END
)
init_vars()
{
local _ARCH=${ARCH}
case "${ARCH}" in
i386)
export UNAME_m=i386
export UNAME_p=i386
;;
amd64)
;;
mac)
_ARCH=amd64
;;
*)
printerr "Invalid architecture '${ARCH}'. Must be one of i386, amd64, or mac"
exit 1
;;
esac
[ -z "${DESTDIR}" -o "${DESTDIR}" = "/" ] && \
bail "Invalid DESTDIR '${DESTDIR}' defined"
DISTSITE=${URL}/${_ARCH}/${RELEASE}
KERNCONF_DIR="${SYSDIR}/usr/src/sys/${_ARCH}/conf"
KERNCONF_NOMADBSD="${KERNCONF_DIR}/NOMADBSD"
KERNCONF_GENERIC="${KERNCONF_DIR}/GENERIC"
FILESYSTEM=$(echo ${FILESYSTEM} | tr '[a-z]' '[A-Z]')
if [ \( ${FILESYSTEM} != "UFS" \) -a \( ${FILESYSTEM} != "ZFS" \) ]; then
printerr "FILESYSTEM '${FILESYSTEM}' is invalid. Must be UFS or ZFS."
exit 1
fi
}
_umount()
{
local n=3
while [ $n -gt 0 ]; do
umount "$1" && return
n=$(($n - 1))
sleep 1
done
umount -f "$1"
}
mount_chroot_devfs()
{
if ! is_mounted "${SYSDIR}/dev"; then
chroot "${SYSDIR}" sh -c 'mount -t devfs devfs /dev' || \
bail "Failed to mount devfs"
fi
}
umount_chroot_devfs()
{
_umount "${SYSDIR}/dev"
}
mount_ports_tree()
{
[ ! -d "${SYSDIR}/usr/ports" ] && mkdir "${SYSDIR}/usr/ports"
if ! is_mounted "${SYSDIR}/usr/ports"; then
mount -t nullfs "${PORTSTREE}" "${SYSDIR}/usr/ports" || \
bail "Failed to mount ports tree"
fi
}
umount_ports_tree()
{
_umount "${SYSDIR}/usr/ports"
}
is_mounted()
{
local d mntpt
mntpt=$(realpath -q "$1") || return 1
for d in $(mount -p | awk '{print $2}'); do
[ $d = "${mntpt}" ] && return 0
done
return 1
}
install_user_config()
{
if [ -d "${SYSDIR}/home" ]; then
mv "${SYSDIR}/home" "${SYSDIR}/usr/home" || \
bail "Failed to move home dir"
fi
(cd nomad && tar cf - .) | (cd "${SYSDIR}/usr/home/nomad" && tar xf -)
chroot "${SYSDIR}" sh -c 'chown -R nomad:nomad /usr/home/nomad'
}
write_config_files()
{
local _SRCDIR=$1 _DESTDIR=$(realpath "$2")
[ -z "${_DESTDIR}" -o "${_DESTDIR}" = "/" ] && \
bail "Invalid DESTDIR '${_DESTDIR}' defined"
write_sysctl_conf "${_DESTDIR}/etc/sysctl.conf"
write_loader_conf "${_DESTDIR}/boot/loader.conf"
write_fstab "${_DESTDIR}/etc/fstab"
write_rc_conf "${_DESTDIR}/etc/rc.conf"
write_rc_subr "${_DESTDIR}/etc/rc.subr"
write_rc_shutdown "${_SRCDIR}/etc/rc.shutdown" "${_DESTDIR}/etc/rc.shutdown"
}
write_fstab()
{
local fstab=$1 rootdev
rm -f "${fstab}"
if [ ${FILESYSTEM} = "UFS" ]; then
if [ "${ARCH}" = "amd64" -o "${ARCH}" = "i386" ]; then
rootdev="/dev/label/${ROOTLABEL}"
else
rootdev="/dev/gpt/${ROOTLABEL}"
fi
echo "${rootdev} / ufs rw,noatime 1 1" > ${fstab} || bail
fi
cat config/etc/fstab >> "${fstab}" || bail
}
write_sysctl_conf()
{
local sysctl_conf=$1
cp config/etc/sysctl.conf "${sysctl_conf}" || bail
${FILESYSTEM}_SYSCTL_SETTINGS >> "${sysctl_conf}" || bail
}
write_loader_conf()
{
local loader_conf=$1
cp config/boot/loader.conf "${loader_conf}" || bail
${FILESYSTEM}_LOADER_SETTINGS >> "${loader_conf}" || bail
}
write_rc_conf()
{
local rc_conf=$1
cp config/etc/rc.conf "${rc_conf}" || bail
if [ ${FILESYSTEM} = "ZFS" ]; then
sysrc -f ${rc_conf} zfs_enable=YES || bail
fi
}
write_rc_subr()
{
local rc_subr=$1
# Include /etc/cecho in /etc/rc.subr for `echo´ with colors
awk '{
if (included)
print $0;
else if (/^$/) {
print ". /etc/cecho"; included = 1
} else
print $0
}' < "${SYSDIR}"/etc/rc.subr > "${rc_subr}" || bail
}
write_rc_shutdown()
{
local src_rc_shutdown=$1
local dst_rc_shutdown=$2
awk '{
if (/^# Insert other /) {
print $0;
print "/etc/rc.d/mount_uzip stop"
} else
print $0
}' "${src_rc_shutdown}" > "${dst_rc_shutdown}" || bail
}
init_base()
{
configure_shlib_cache
configure_root_user
add_nomad_user
install_config_files
install_handbook
install_user_config
set_pkg_repo
set_boot_menu_pos
configure_device_hints
configure_nsswitch
}
install_config_files()
{
(cd config && tar cf - .) | \
(cd "${SYSDIR}" && tar -xf - --uname root --gname wheel)
chroot "${SYSDIR}" sh -c 'cap_mkdb /etc/login.conf'
}
add_nomad_user()
{
if ! grep -q ^nomad "${SYSDIR}/etc/passwd"; then
chroot "${SYSDIR}" sh -c 'pw useradd nomad -m \
-G wheel,operator,video,games -s /bin/sh; \
pw usermod nomad -c Nomad'
fi
}
set_boot_menu_pos()
{
re="s/^menu_position =.*$/menu_position"
re="${re} = {x = ${MENU_POSITION_X}, y = ${MENU_POSITION_Y}}/"
sed -E -i '' "${re}" "${SYSDIR}"/boot/lua/drawer.lua
}
configure_nsswitch()
{
# Add mdns to "hosts:" line in /etc/nsswitch.conf
if ! grep -q 'hosts:.*mdns*' "${SYSDIR}"/etc/nsswitch.conf; then
sed -E -i '' 's/^(hosts:) (.*)/\1 \2 mdns/' \
"${SYSDIR}"/etc/nsswitch.conf
fi
}
configure_device_hints()
{
# Disable uart.1 to prevent hangs on CherryTrail systems
sed -E -i '' 's/^(hint\.uart\.1.*)/#\1/g' "${SYSDIR}"/boot/device.hints
}
configure_shlib_cache()
{
mount_chroot_devfs
chroot "${SYSDIR}" sh -c '/etc/rc.d/ldconfig start'
umount_chroot_devfs
}
configure_root_user()
{
chroot "${SYSDIR}" pw usermod root -s /bin/sh
}
set_pkg_repo()
{
if (echo ${RELEASE} | grep -q 'CURRENT'); then
sed -E -i '' 's/quarterly/latest/g' "${SYSDIR}/etc/pkg/FreeBSD.conf"
fi
}
define_font_paths()
{
[ ! -d "${XORG_CONF_D}" ] && mkdir -p "${XORG_CONF_D}"
for i in "${FONTSDIR}"/*; do
[ ! -d "$i" ] && continue
mkfontscale "$i/"
mkfontdir "$i/"
done
(echo "Section \"Files\""
IFS=
for i in "${FONTSDIR}"/*; do \
[ ! -d "$i" ] && continue
n=$(head -1 "$i/fonts.scale")
r="s#${SYSDIR}(/.*)\$#\\1#"
i=$(echo $i | sed -E $r)
if [ $n -gt 0 ]; then \
echo " FontPath \"$i\""
else
if [ ! -z ${ns} ]; then
ns=$(printf "${ns}\n$i")
else
ns="$i"
fi
fi
done
echo ${ns} | while read i; do \
echo " FontPath \"$i\""
done
echo "EndSection") > "${FONTPATHS_FILE}"
}
create_base()
{
local version srcurl
if [ ! -d "${SYSDIR}" ]; then
mkdir -p "${SYSDIR}" || bail
fi
for d in base.txz kernel.txz src.txz; do
fetch ${DISTSITE}/$d -o /tmp/$d || bail
tar --unlink -xvpJf /tmp/$d -C "${SYSDIR}" || bail
rm -f /tmp/$d
done
cp /etc/resolv.conf "${SYSDIR}"/etc
}
update_base()
{
PAGER=cat freebsd-update --currently-running ${RELEASE} \
-f "${SYSDIR}/etc/freebsd-update.conf" -b "${SYSDIR}" \
-d "${SYSDIR}/var/db/freebsd-update" fetch && \
PAGER=cat freebsd-update --currently-running ${RELEASE} \
-f "${SYSDIR}/etc/freebsd-update.conf" -b "${SYSDIR}" \
-d "${SYSDIR}/var/db/freebsd-update" install
}
build_nvidia_drivers()
{
local d v pkgname
mount_ports_tree
mount_chroot_devfs
for v in ${NVIDIA_DRIVERS}; do
# Skip nvidia-driver>=440 on i386
if [ ${ARCH} = "i386" ]; then
[ "$v" = "latest" ] && continue
[ $v -ge 440 ] && continue
fi
if [ $v = "latest" ]; then
d="nvidia-driver"
else
d="nvidia-driver-$v"
fi
pkgname=$(cd "${PORTSTREE}/x11/$d" && make -VPKGNAME)
[ -f "${PKGDIR}/${pkgname}.pkg" ] && continue
chroot "${SYSDIR}" sh -c "cd /usr/ports/x11/$d && \
make BATCH=1 USE_PACKAGE_DEPENDS=1 ${NVIDIA_BUILD_OPTS} \
clean package" || bail
mkdir -p "${SYSDIR}/usr/local/nvidia/$v" 2>/dev/null
cat "${SYSDIR}/usr/ports/x11/$d/work/pkg/${pkgname}.pkg" | \
(cd "${SYSDIR}/usr/local/nvidia/${v}" && tar xf -)
chroot "${SYSDIR}" sh -c "cd /usr/ports/x11/$d && make distclean"
done
# Remove build dependencies
pkg -c "${SYSDIR}" autoremove -y
umount_ports_tree
umount_chroot_devfs
mkdir -p "${SYSDIR}"/nvidia/usr/local/lib/xorg/modules/drivers
mkdir -p "${SYSDIR}"/nvidia/usr/local/lib/xorg/modules/extensions
rm -f "${SYSDIR}/usr/local/lib/xorg/modules/drivers/nvidia_drv.so"
ln -sf "/nvidia/usr/local/lib/xorg/modules/drivers/nvidia_drv.so" \
"${SYSDIR}/usr/local/lib/xorg/modules/drivers/"
for l in libglxserver_nvidia.so libglxserver_nvidia.so.1; do
rm -f "${SYSDIR}/usr/local/lib/xorg/modules/extensions/$l"
ln -sf "/nvidia/usr/local/lib/xorg/modules/extensions/$l" \
"${SYSDIR}/usr/local/lib/xorg/modules/extensions/"
done
}
pkg_filter() {
local exlst=PKG_EXCLUDE_${ARCH}
(while read x; do echo $x; done; ${exlst} 2>/dev/null) | sort -n | uniq -u
}
pkglist()
{
cat ${PKGLIST} | pkg_filter
}
install_packages()
{
mount_chroot_devfs
export ASSUME_ALWAYS_YES=yes
pkglist | xargs -J% pkg -c "${SYSDIR}" install -y % || bail
# Lock all kernel module packages to prevent kernel version mismatch
# problems when updating packages on CURRENT.
if (echo ${RELEASE} | grep -q 'CURRENT'); then
pkg -c "${SYSDIR}" lock -y -g '*-kmod-*'
fi
umount_chroot_devfs
}
install_from_git_repos()
{
[ ! -d "${SYSDIR}/git" ] && mkdir "${SYSDIR}/git"
mount_chroot_devfs
for r in ${GIT_REPOS}; do
rd=$(echo $r | sed -E 's#.*/(.*)\.git$#\1#')
repo_dirs="${repo_dirs} ${rd}"
if [ ! -d "${SYSDIR}/git/${rd}" ]; then
chroot "${SYSDIR}" sh -c "cd /git && git clone ${r}"
fi
done
for r in ${repo_dirs}; do
rname=$(echo $r | tr '-' '_')
eval build_cmd=\${${rname}_BUILD_CMD}
[ -z "${build_cmd}" ] && continue
chroot "${SYSDIR}" sh -c "cd /git/${r}; ${build_cmd}" || \
bail "Build command '${build_cmd}' failed."
done
umount_chroot_devfs
}
#
# Configurations to be done after all packages and ports
# have been installed.
#
post_pkg_config()
{
add_nomad_to_groups cups webcamd
define_font_paths
delete_unneeded_files
disable_suspend
configure_dsbmd
configure_sakura
configure_networkmgr
configure_cupsd
}
delete_unneeded_files()
{
# Remove tesserac-data which uses ~1G.
rm -rf "${SYSDIR}"/usr/local/share/tessdata/*
# Remove desktop files for LXDE and Xfce preferred applications
rm -f "${SYSDIR}"/usr/local/share/applications/exo-*.desktop
rm -f "${SYSDIR}"/usr/local/share/applications/libfm-pref-apps.desktop
# Remove useless Xfce desktop file
rm -f "${SYSDIR}"/usr/local/share/applications/xfce4-about.desktop
# Free 177M by removing the boost header files.
rm -rf "{SYSDIR}"/usr/local/include/boost
delete_unneeded_llvm_files
delete_docs
}
#
# Preserve all llvm libraries required by other packages, and delete
# all other files from the llvm package.
#
# The mesa ports depend on llvm13 which, unfortunately, requires > 1 GB.
# According to ldd only /usr/local/llvm13/lib/libLLVM-13.so are needed
# by mesa-dri and mesa-gallium-xa. So we delete everything else from
# the llvm package.
#
delete_unneeded_llvm_files()
{
local lib
local llvm_lib_paths
local tarfile="/tmp/saved.libs.$$.tar"
local llvm_libs=$(pkg -c ${SYSDIR} info -q -a --required-shlibs | \
grep libLLVM | sort | uniq)
for lib in ${llvm_libs}; do
local pkg=$(pkg -c ${SYSDIR} shlib -q --provides ${lib})
local paths=$(pkg -c ${SYSDIR} info -q --list-files ${pkg} | grep ${lib})
tar -C ${SYSDIR} -rf ${tarfile} ".${paths}"
pkg -c ${SYSDIR} info -q --list-files ${pkg} | \
xargs -I % rm -f "${SYSDIR}/%"
tar -C ${SYSDIR} -xf ${tarfile}
rm -f ${tarfile}
done
}
add_nomad_to_groups()
{
while [ $# -gt 0 ]; do
chroot "${SYSDIR}" sh -c "pw groupmod $1 -m nomad" || \
bail "Could not add user nomad to group $1"
shift
done
}
delete_docs()
{
# Delete most of the doc files under /usr/local/share/doc
# This frees approx. 280M
find "${SYSDIR}"/usr/local/share/doc -type d -depth 1 \
\! \( -name "fish" -o -name "neomutt" -o -name "cups" \) \
-exec rm -rf '{}' \;
# Free ~ 68M
rm -rf "${SYSDIR}"/usr/local/share/gtk-doc
}
disable_suspend()
{
# Disable suspend, hibernate, and hybrid sleep.
for s in suspend hibernate hybridsleep; do
printf '#!/bin/sh\nexit 1\n' > \
"${SYSDIR}"/usr/local/lib/ConsoleKit/scripts/ck-system-$s
done
}
configure_dsbmd()
{
cp "${SYSDIR}/usr/local/etc/dsbmd.conf.sample" \
"${SYSDIR}/usr/local/etc/dsbmd.conf"
}
configure_sakura()
{
# Use the Papirus terminal icon as default icon for Sakura
cp "${SYSDIR}"/usr/local/share/icons/Papirus/48x48/apps/utilities-terminal.svg \
"${SYSDIR}"/usr/local/share/pixmaps/terminal.svg
}
configure_networkmgr()
{
# Remove Networkmgr's netcardmgr script which messes with our auto-
# configured network interfaces in /etc/rc.conf.
rm -f "${SYSDIR}"/usr/local/bin/netcardmgr
# Prevent execution of networkmgr's setup-nic script.
sed -i '' -E 's/^[::space::]*(.*action.*setup-nic.*)/#\1/g' \
"${SYSDIR}"/usr/local/etc/devd/networkmgr.conf
# Prevent execution of networkmgr's auto-switch script.
sed -i '' -E 's/^[::space::]*(.*action.*auto-switch.*)/#\1/g' \
"${SYSDIR}"/usr/local/etc/devd/networkmgr.conf
}
configure_cupsd()
{
# Comment out IdleExitTimeout
sed -E 's/^IdleExitTimeout .*/#&/' \
"${SYSDIR}"/usr/local/etc/cups/cupsd.conf.sample > \
"${SYSDIR}"/usr/local/etc/cups/cupsd.conf
}
#
# Check if the port options of an installed package matches
# the given options. Options are defined as follows:
# "OPT1=<on|off> OPT2=<on|off> ... OPTn=<on|off>"
# Return != 0 if the value of one of the given options do not
# match the returned (pkg query) options.
#
check_port_opts()
{
local port=$1
local opts=$2
local curopts=$(pkg -c ${SYSDIR} query -i "%Ok=%Ov" ${port})
for o in ${opts}; do
if ! (echo ${curopts} | grep -q $o); then
return 1
fi
done
return 0
}
#
# Creates a port's _SET and _UNSET make variables from a list
# of OPT=<on|off> pairs.
#
# Example: port_opts cat/fooport "FOO=on BAR=off BAZ=on"
# becomes cat_fooport_SET="FOO BAZ" cat_fooport_UNSET="BAR"
#
port_opts()
{
local popts=$2
local pname=$(echo $1 | tr '/' '_')
echo ${popts} | tr ' ' '\n' | awk -v pname=${pname} '{
split($0, a, /=/);
if (a[2] ~ /on/) {
set_vars = sprintf("%s%s%s", set_vars,
set_vars ? " " : "", a[1]);
} else if (a[2] ~ /off/) {
unset_vars = sprintf("%s%s%s", unset_vars,
unset_vars ? " " : "", a[1]);
}
}
END {
if (set_vars)
printf("%s_SET=\"%s\" ", pname, set_vars);
if (unset_vars)
printf("%s_UNSET=\"%s\"", pname, unset_vars);
}'
}
install_ports()
{
mount_ports_tree
mount_chroot_devfs
for p in ${PORTSLIST}; do
printmsg "Checking whether $p is already installed"
pname=$(echo $p | cut -d/ -f2 | tr '.-' '__')
eval popts=\${${pname}_OPTS}
if pkg --chroot "${SYSDIR}" info --exists $p; then
if check_port_opts $p "${popts}"; then
# Port options not changed. Check whether the ports
# version is newer than the installed version.
p1=$(pkg --chroot "${SYSDIR}" info $p | head -1)
p2=$(cd ${SYSDIR}/usr/ports/$p && make -VPKGNAME);
result=$(pkg version -t $p2 $p1)
[ $result = '<' -o $result = '=' ] && continue
printmsg "Building newer version from ports ..."
else
printmsg "Options of installed package do not match."
fi
fi
printmsg "Building $p ..." >&2
local opts=$(port_opts $p "${popts}")
chroot "${SYSDIR}" sh -c "cd /usr/ports/$p && \
make USE_PACKAGE_DEPENDS=1 BATCH=1 ${opts} \
clean deinstall install" || bail
done
# Lock all kernel module packages to prevent kernel version mismatch
# problems when updating packages on CURRENT.
if (echo ${RELEASE} | grep -q 'CURRENT'); then
pkg -c "${SYSDIR}" lock -y -g '*-kmod-*'
fi
pkg -c "${SYSDIR}" autoremove -y
umount_ports_tree
umount_chroot_devfs
}
build_kernel()
{
local makeopts="KERNCONF=NOMADBSD ${BUILDKERNEL_OPTS}"
makeopts="${makeopts} WITHOUT_MODULES=\"${WITHOUT_KMODS}\""
local regx="/DEBUG=-g/d; /WITH_CTF=1/d; /^ident.*GENERIC/d;"
sed -e "${regx}" < ${KERNCONF_GENERIC} > ${KERNCONF_NOMADBSD}
KERNELOPTS >> ${KERNCONF_NOMADBSD}
for i in $(pwd)/patch/usr/src/*.diff; do
(cd "${SYSDIR}"/usr/src && patch -N < $i)
done
mount_chroot_devfs
chroot "${SYSDIR}" sh -c "cd /usr/src && make ${makeopts} kernel" || bail
umount_chroot_devfs
}
install_src_dist()
{
BSDINSTALL_DISTDIR="${DISTDIR}" DISTRIBUTIONS=src.txz \
BSDINSTALL_DISTSITE=${DISTSITE} bsdinstall distfetch
BSDINSTALL_DISTDIR="${DISTDIR}" BSDINSTALL_CHROOT="${SYSDIR}" \
DISTRIBUTIONS=src.txz bsdinstall distextract
}
calculate_est_uzip_image_size() {
local rootfs=$(mount -p | awk '$2 == "/" {print $3}')
if [ "${rootfs}" = "zfs" ]; then
du_extra_args="-A"
fi
nfiles=$(find "$1" | wc -l)
wasted=$(($nfiles * (${FRAGSIZE} / 2) / (1024 * 1024)))
size=$(du -mc ${du_extra_args} -B ${FRAGSIZE} "$1" | tail -1 | cut -f 1)
size=$((${size} + ${wasted}))
echo ${size}
}
get_uzip_size()
{
local rootfs=$(mount -p | awk '$2 == "/" {print $3}')
if [ "${rootfs}" = "zfs" ]; then
du_extra_args="-A"
fi
du -m ${du_extra_args} -B ${FRAGSIZE} ${UZIP_IMAGE}.uzip | \
tail -1 | cut -f 1
}
create_zpool()
{
local DEV=$1
if [ ! -d "${DESTDIR}" ]; then
mkdir -p "${DESTDIR}" || bail
fi
zpool create -f -o altroot=${DESTDIR} -o ashift=${ASHIFT} \
${ZFS_POOL} $DEV || bail
zpool set autoexpand=off ${ZFS_POOL}
zfs set atime=off compress=on compression=lz4 ${ZFS_POOL}
zfs create -o mountpoint=none ${ZFS_POOL}/ROOT || bail
zfs create -o mountpoint=/ -o canmount=noauto \
${ZFS_POOL}/ROOT/${ZFS_BE} || bail
mount -t zfs ${ZFS_POOL}/ROOT/${ZFS_BE} ${DESTDIR} || bail
zpool set bootfs=${ZFS_POOL}/ROOT/${ZFS_BE} ${ZFS_POOL} || bail
}
create_zfs_datasets()
{
zfs create -o canmount=off -o mountpoint=/usr ${ZFS_POOL}/usr || bail
zfs create -o exec=off -o setuid=off ${ZFS_POOL}/usr/src || bail
zfs create ${ZFS_POOL}/usr/obj || bail
zfs create -o mountpoint=/usr/ports ${ZFS_POOL}/usr/ports || bail
zfs create -o exec=off -o setuid=off ${ZFS_POOL}/usr/ports/distfiles || bail
zfs create -o exec=off -o setuid=off ${ZFS_POOL}/usr/ports/packages || bail
zfs create -o canmount=off -o mountpoint=/var ${ZFS_POOL}/var || bail
zfs create -o exec=off -o setuid=off ${ZFS_POOL}/var/audit || bail
zfs create -o exec=off -o setuid=off ${ZFS_POOL}/var/crash || bail
zfs create -o atime=on -o exec=off -o setuid=off ${ZFS_POOL}/var/mail || bail
}
create_zfs_and_copy_files()
{
local dev=$1
create_zpool ${dev}
create_zfs_datasets
copy_system_files_to_dest_dir ${SYSDIR} ${DESTDIR}
zpool export ${ZFS_POOL} || zpool export -f ${ZFS_POOL}
rmdir ${DESTDIR}
}
create_ufs_and_copy_files()
{
local dev=$1
create_ufs ${dev}
if [ ! -d "${DESTDIR}" ]; then
mkdir -p "${DESTDIR}" || bail
fi
mount -t ufs ${dev} ${DESTDIR} || bail
copy_system_files_to_dest_dir ${SYSDIR} ${DESTDIR}
_umount "${DESTDIR}"
rmdir "${DESTDIR}"
}
create_ufs()
{
local blksize dev
dev=$1
blksize=$((${FRAGSIZE} * 8))
newfs -t -E -U -O 1 -j -o time -b ${blksize} -f ${FRAGSIZE} \
-m ${UFS_MINFREE} ${dev}|| bail
}
write_mbr_zfs_bootcode()
{
local DEV=$1 PART=$2
gpart bootcode -b "${SYSDIR}/boot/mbr" ${DEV} || bail
dd if=/dev/zero of=${PART} count=2 || bail
dd if="${SYSDIR}/boot/zfsboot" of=${PART} count=1 || bail
dd if="${SYSDIR}/boot/zfsboot" of=${PART} iseek=1 oseek=1024 || bail
}
#
# Calls `gpart add` with the given arguments, and returns the name
# of the new partition device.
#
add_partition()
{
local output=$(gpart add $*)
set -- ${output}
[ "$2" != "added" ] && bail
echo $1
}
#
# Returns the index of a given partition name.
#
# Examples: ada0s1 -> 1, ada0s1e -> 5, ada0p3 -> 3
#
index_from_partition()
{
local part=$1
if echo ${part} | egrep -q 's[1-4][a-h]$'; then
echo ${part} | sed -E 's/.*([a-h])$/\1/' | tr 'abcdefgh' '12345678'
elif echo ${part} | egrep -q '[sp][0-9]+$'; then
echo ${part} | sed -E 's/.*[sp]([0-9]+)$/\1/'
fi
}
add_amd64_efi_partition()
{
local dev=$1
gpart add -a 1m -t efi -b 0 -s ${AMD64_EFI_SIZE}m ${dev} || bail
}
add_mac_efi_partition()
{
local dev=$1
gpart add -t efi -a 1m -l gpefiboot -s ${MAC_EFI_SIZE}m ${dev} || bail
}
copy_efi_loader_to_partition()
{
local part mnt
part=$(basename $1)
mnt=$(mktemp -d /tmp/efi.XXXXX)
newfs_msdos -F 32 -c 1 /dev/${part} || bail
mount -t msdosfs /dev/${part} ${mnt} || bail
mkdir -p ${mnt}/EFI/BOOT || bail
cp ${EFI_LOADER} ${mnt}/EFI/BOOT/BOOTX64.EFI || bail
_umount ${mnt}
rmdir ${mnt}
}
create_efi_partition()
{
local part dev=$(basename $1)
if [ ${ARCH} = "amd64" ]; then
part=$(add_amd64_efi_partition ${dev})
elif [ ${ARCH} = "mac" ]; then
part=$(add_mac_efi_partition ${dev})
else
bail "Cannot create EFI partition for ${ARCH}"
fi
copy_efi_loader_to_partition ${part}
}
#
# Creates a md(4) device backed by the given file, and returns
# the name of the new md(4) device.
#
create_mddev()
{
local image_file=$1 image_size_mib=$2
touch ${image_file} || bail
mddev=$(mdconfig -a -t vnode -f ${image_file} -s ${image_size_mib}m) || bail
dd if=/dev/zero of=/dev/${mddev} bs=1M 2>/dev/null
__active_mddevs="${__active_mddevs} ${mddev}"
echo ${mddev}
}
destroy_mddev()
{
local mddev=$1
__active_mddevs=$(echo ${__active_mddevs} | tr ' ' '\n' | \
awk -v dev=${mddev} '$0 != dev {print $0}')
mdconfig -d -u ${mddev}
}
destroy_active_mddevs()
{
local dev
for dev in ${__active_mddevs}; do
mdconfig -d -u ${dev}
done
__active_mddevs=""
}
build_uzip_image()
{
local mkuzip_extra_flags=""
local blksize=$((${FRAGSIZE} * 8))
local mversion=$(freebsd-version -u | sed -E 's/^([0-9]+).*/\1/')
local uzipsz mddev
if [ -f ${UZIP_IMAGE}.uzip ]; then
bail "${UZIP_IMAGE}.uzip already exists"
fi
rm -f "${UZIP_IMAGE}.img" > /dev/null 2>&1
uzipsz=$(calculate_est_uzip_image_size "${SYSDIR}/usr/local")
mddev=$(create_mddev ${UZIP_IMAGE}.img ${uzipsz})
newfs -O 1 -o space -m 0 -b ${blksize} -f ${FRAGSIZE} \
/dev/${mddev} || bail
[ ! -d "${UZIP_MNT}" ] && mkdir "${UZIP_MNT}"
mount /dev/${mddev} "${UZIP_MNT}" || bail
(cd "${SYSDIR}/usr/local" && \
find . -not \( -path "./etc" -or -path "./etc/*" \) | \
cpio -pdmu "${UZIP_MNT}")
(cd "${UZIP_MNT}" && ln -s /usr.local.etc etc)
_umount "${UZIP_MNT}"
destroy_mddev ${mddev}
rmdir "${UZIP_MNT}"
[ ${mversion} -ge 13 ] && mkuzip_extra_flags="-A zstd -C 19"
mkuzip -Z -j ${MKUZIP_JOBS} -d -s ${UZIP_CLUSTERSZ} ${mkuzip_extra_flags} \
-o ${UZIP_IMAGE}.uzip ${UZIP_IMAGE}.img || bail "mkuzip failed"
rm -f "${UZIP_IMAGE}.img"
}
copy_system_files_to_dest_dir()
{
local _SRCDIR=$1 _DESTDIR=$2
[ -z "${_DESTDIR}" -o "${_DESTDIR}" = "/" ] && \
bail "Invalid DESTDIR '${_DESTDIR}' defined"
(cd "${_SRCDIR}" && tar -cf - \
--exclude '^boot/kernel.old' \
--exclude '^git*' \
--exclude '^pkgs/*' \
--exclude '^tmp/*' \
--exclude '^usr/obj*' \
--exclude '^usr/src*' \