-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbuild_tiny_linux.sh
More file actions
executable file
·1687 lines (1436 loc) · 52.6 KB
/
build_tiny_linux.sh
File metadata and controls
executable file
·1687 lines (1436 loc) · 52.6 KB
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/bash
# SPDX-License-Identifier: BSD-3-Clause
# SPDX-FileCopyrightText: Copyright (c) 2009-2023, NVIDIA CORPORATION. All rights reserved.
# See LICENSE file for details.
set -e
MIRROR="http://gentoo.osuosl.org"
PORTAGEPKG="portage-latest.tar.bz2"
DISTFILESPKG="distfiles.tar.bz2"
PORTAGE="/var/db/repos/gentoo"
BUILDROOT="buildroot"
BUILDSCRIPTS="/buildscripts"
NEWROOT="/newroot"
INSTALL="/install"
SQUASHFS="/tiny/squash.bin"
MAKECONF="/etc/portage/make.conf"
NICE="ionice -c 3 nice -n 19"
PYTHON_VER=3.12
# Inherit TEGRAABI from parent process
TEGRAABI="${TEGRAABI:-aarch64-unknown-linux-gnu}"
die()
{
echo "$@"
exit 1
}
if [[ $# -eq 0 || $1 = "-h" || $1 = "--help" ]]; then
echo "Usage:"
echo "sudo `basename $0` [OPTIONS] <PROFILE>"
echo
echo "Options:"
echo " -j N Launch N jobs simultaneously"
echo " -v VER Use specified version number"
echo " -r Rebuild entire target filesystem"
echo " -i Run interactive shell within the build environment"
echo " -k Force rebuilding the kernel"
echo " -q Force recompressing squashfs"
echo " -m Launch kernel menuconfig before compiling the kernel"
exit
fi
# Use COW if available
REAL_CP=$(which cp)
cp()
{
"$REAL_CP" --reflink=auto "$@"
}
# Auto-detect number of CPUs
JOBS="${JOBS:-$(grep -c ^processor /proc/cpuinfo)}"
# Auto-detect architecture
case "$(uname -m)" in
x86_64) STAGE3ARCH="amd64" ;;
aarch64) STAGE3ARCH="arm64" ;;
*) die "Unrecognized arch - $(uname -m)"
esac
# Parse options
while [[ $# -gt 1 ]]; do
OPT=$1
shift
case "$OPT" in
-j) JOBS="$1" ; shift ;;
-v) VERSION="$1" ; shift ;;
-r) REBUILDNEWROOT="1" ;;
-i) INTERACTIVE="1" ;;
-k) REBUILDKERNEL="1" ;;
-q) REBUILDSQUASHFS="1" ;;
-m) KERNELMENUCONFIG="1" ; REBUILDKERNEL="1" ;;
--arch=*) ARCH="${OPT#--arch=}"
case "$ARCH" in
amd64|x86_64) [[ $STAGE3ARCH = amd64 ]] || die "Cannot cross-compile x86_64 from ARM" ;;
armv8|arm64|aarch64) STAGE3ARCH="arm64" ;;
*) die "Unrecognized arch - $ARCH" ;;
esac
;;
--grub) USEGRUB=1 ;;
--rc-kernel) RCKERNEL=1 ;;
--custom-kernel)
CUSTOMKERNEL="$1"
REBUILDKERNEL="1"
[[ -d $CUSTOMKERNEL ]] || die "Kernel directory $CUSTOMKERNEL does not exist"
[[ -f $CUSTOMKERNEL/Makefile ]] || die "File $CUSTOMKERNEL/Makefile does not exist"
shift
;;
*) die "Unrecognized option - $OPT" ;;
esac
done
# Set profile
if [[ $1 = "-i" ]]; then
if [[ -f $BUILDROOT/var/lib/misc/extra ]]; then
PROFILE=`cat "$BUILDROOT/var/lib/misc/extra"`
else
die "No profile selected"
fi
INTERACTIVE="1"
else
[[ $1 && ${1#-} = $1 ]] || die "No profile selected"
PROFILE="$1"
fi
shift
# Set Tegra build type for Tegra profile
TEGRATYPE="`dirname "$0"`/profiles/$PROFILE/tegra"
[[ -z $TEGRABUILD && -f $TEGRATYPE ]] && TEGRABUILD=`cat "$TEGRATYPE"`
# Find default stage3 package
STAGE3PKG="${STAGE3PKG:-$(find ./ -maxdepth 1 -name stage3-$STAGE3ARCH-*.tar.xz | head -n 1)}"
# Override package name with profile name
FINALPACKAGE="$PROFILE.zip"
# Set default version
VERSION="${VERSION:-$(date "+%y.%m.%d")}"
# Export user arguments
[[ $JOBS ]] && export JOBS
[[ $PROFILE ]] && export PROFILE
[[ $VERSION ]] && export VERSION
[[ $REBUILDNEWROOT ]] && export REBUILDNEWROOT
[[ $INTERACTIVE ]] && export INTERACTIVE
[[ $REBUILDKERNEL ]] && export REBUILDKERNEL
[[ $REBUILDSQUASHFS ]] && export REBUILDSQUASHFS
[[ $KERNELMENUCONFIG ]] && export KERNELMENUCONFIG
[[ $USEGRUB ]] && export USEGRUB
[[ $RCKERNEL ]] && export RCKERNEL
[[ $TEGRABUILD ]] && export TEGRABUILD
[[ $TEGRAABI ]] && export TEGRAABI
boldecho()
{
echo -e "\033[1m$@\033[0m"
}
download()
{
local URL
local FILENAME
local TYPE
URL="$1"
FILENAME=`basename $1`
boldecho "Downloading $FILENAME"
curl -f -O "$URL" || die "Unable to download $FILENAME"
TYPE=`file "$FILENAME"`
if echo "$TYPE" | grep -q HTML; then
rm "$FILENAME"
echo "Unable to download $FILENAME"
exit 1
fi
}
find_stage3()
{
local STAGE3
local URL
local PARSEFILELIST
local FILELIST
local DIRLIST
local DIR
local LASTERROR
STAGE3="$1"
URL="$2"
PARSEFILELIST="s/<[^>]*>/ /g ; s/^ *// ; s/ .*//"
echo "Trying $URL" >&2
FILELIST=$(curl -f "$URL" | sed "$PARSEFILELIST")
if echo "$FILELIST" | grep -q "$GREPSTAGE"; then
FILELIST=`echo "$FILELIST" | grep "$GREPSTAGE"`
echo "${URL}$FILELIST"
return 0
fi
DIRLIST=`echo "$FILELIST" | grep "/$"`
while read DIR; do
[[ -n $DIR ]] && find_stage3 "$STAGE3" "${URL}$DIR" && return 0
done <<- EOF
$DIRLIST
EOF
return 1
}
download_packages()
{
# Don't do anything if we are inside the host tree already
[[ -d ./$BUILDSCRIPTS ]] && return 0
# Don't do anything if the host tree already exists
[[ -d $BUILDROOT ]] && return 0
# Download package database if needed
[[ -f $PORTAGEPKG ]] || download "$MIRROR/snapshots/$PORTAGEPKG"
# Download stage 3 image if needed
if [[ ! -f $STAGE3PKG ]]; then
local GREPSTAGE
local STAGE3PATH
local FLAVOR
FLAVOR="openrc"
GREPSTAGE="^stage3-$STAGE3ARCH-$FLAVOR-[0-9TZ].*\.tar\.xz$"
boldecho "Downloading stage3 file list from the server"
STAGE3PATH="$MIRROR/releases/$STAGE3ARCH/autobuilds/current-stage3-$STAGE3ARCH-$FLAVOR/"
STAGE3PKG=`find_stage3 "$GREPSTAGE" "$STAGE3PATH"`
download "$STAGE3PKG"
STAGE3PKG=`basename "$STAGE3PKG"`
fi
}
tar_bz2()
{
local PROGRAM
if [[ ${2##*.} = xz ]]; then
PROGRAM=xz
elif which lbzip2 >/dev/null 2>&1; then
PROGRAM=lbzip2
else
PROGRAM=bzip2
fi
$NICE tar --use-compress-program "$PROGRAM" "$@"
}
unpack_packages()
{
# Don't do anything if we are inside the host tree already
[[ -d ./$BUILDSCRIPTS ]] && return 0
# Don't do anything if the host tree already exists
[[ -d $BUILDROOT ]] && return 0
# Unpack the root
boldecho "Unpacking stage3 package"
mkdir "$BUILDROOT"
tar_bz2 -xpf "$STAGE3PKG" --xattrs-include='*.*' --numeric-owner -C "$BUILDROOT"
# Unpack portage tree
boldecho "Unpacking portage tree"
tar_bz2 -xpf "$PORTAGEPKG" -C "$BUILDROOT/var/db/repos"
mv "$BUILDROOT/var/db/repos/portage" "${BUILDROOT}$PORTAGE"
# Prepare portage configuration
mkdir -p "$BUILDROOT/etc/portage/repos.conf"
cp "$BUILDROOT/usr/share/portage/config/repos.conf" "$BUILDROOT/etc/portage/repos.conf/gentoo.conf"
# Unpack distfiles if available
if [[ -f $DISTFILESPKG ]]; then
boldecho "Unpacking distfiles"
tar_bz2 -xpf "$DISTFILESPKG" -C "$BUILDROOT"
fi
}
copy_scripts()
{
local SCRIPTSDIR="$(cd -P $(dirname "$0") && pwd)"
# Don't do anything if we are inside the host tree already
[[ -d ./$BUILDSCRIPTS ]] && return 0
# Don't do anything if we are running the copy of build scripts already
local DESTDIR="$BUILDROOT/$BUILDSCRIPTS"
[[ -d $DESTDIR ]] && DESTDIR="$(cd -P "$DESTDIR" && pwd)"
if [[ $DESTDIR != $SCRIPTSDIR ]]; then
# Delete stale build scripts
rm -rf "$DESTDIR"
boldecho "Copying scripts to build environment"
# Check if submodules have been initialized
if [[ ! -f $SCRIPTSDIR/mods/Makefile ]]; then
echo "Error: mods/Makefile not found!"
echo
echo "Tip: Use the following commands to initialize submodules:"
echo "% git submodule init"
echo "% git submodule update"
exit 1
fi
# Check access to the scripts
[[ -f $SCRIPTSDIR/scripts/etc/inittab ]] || die "TinyLinux scripts are not available"
[[ -d $SCRIPTSDIR/profiles/$PROFILE ]] || die "Selected profile $PROFILE is not available"
# Copy TinyLinux scripts
mkdir -p "$DESTDIR"
find "$SCRIPTSDIR"/ -maxdepth 1 -type f -exec cp '{}' "$DESTDIR" \;
cp -r "$SCRIPTSDIR"/profiles "$DESTDIR"
cp -r "$SCRIPTSDIR"/mods "$DESTDIR"
cp -r "$SCRIPTSDIR"/scripts "$DESTDIR"
cp -r "$SCRIPTSDIR"/extra "$DESTDIR"
[[ -z $TEGRABUILD ]] || cp -r "$SCRIPTSDIR"/tegra "$DESTDIR"
fi
# Copy custom kernel
if [[ -n $CUSTOMKERNEL ]]; then
boldecho "Copying custom kernel sources"
mkdir -p "$BUILDROOT/usr/src/linux-custom"
rsync -av --progress "$CUSTOMKERNEL"/ "$BUILDROOT/usr/src/linux-custom"
rm -f "$BUILDROOT/usr/src/linux"
ln -s linux-custom "$BUILDROOT/usr/src/linux"
fi
# Update TinyLinux version printed on boot
sed -i "s/^VERSION=.*/VERSION=\"$VERSION\"/" "$DESTDIR/linuxrc"
}
exit_chroot()
{
sleep 1
sync
umount -l "$BUILDROOT"/{sys,dev/shm,dev/pts,dev,proc} || boldecho "Unmount failed!"
}
run_in_chroot()
{
local QEMUEXE
# Don't do anything if we are inside the host tree already
[[ -d ./$BUILDSCRIPTS ]] && return 0
if [[ $(uname -m) = "x86_64" && $STAGE3ARCH = "arm64" ]]; then
boldecho "Preparing qemu"
QEMUEXE="qemu-aarch64-static"
local QEMUPATH="$(which "$QEMUEXE" 2>/dev/null || true)"
if [[ -z $QEMUPATH ]]; then
[[ -f /etc/debian_version ]] && apt-get install qemu-user-static
QEMUPATH="$(which "$QEMUEXE" 2>/dev/null || true)"
fi
[[ -n $QEMUPATH ]] || die "$QEMUEXE not found!"
if [[ ! -f "$BUILDROOT/$QEMUEXE" ]]; then
local QEMUDIR="$(dirname "$QEMUPATH")"
mkdir -p "$BUILDROOT/$QEMUDIR"
cp "$QEMUPATH" "$BUILDROOT/$QEMUEXE"
fi
QEMUEXE="./$QEMUEXE /bin/bash"
fi
boldecho "Entering build environment"
local SRCDIR="$(cd -P "$(dirname "$0")" && pwd)"
local DESTDIR="$(cd -P "$BUILDROOT/$BUILDSCRIPTS" && pwd)"
[[ $SRCDIR = $DESTDIR ]] || cp "$0" "$DESTDIR"/
cp /etc/resolv.conf "$BUILDROOT/etc"/
mount -t proc none "$BUILDROOT/proc"
trap exit_chroot EXIT
mount --bind /dev "$BUILDROOT/dev"
mount --bind /dev/pts "$BUILDROOT/dev/pts"
mount -t sysfs none "$BUILDROOT/sys"
mkdir -p "$BUILDROOT/dev/shm"
mount -t tmpfs -o mode=1777,nodev none "$BUILDROOT/dev/shm"
$NICE chroot "$BUILDROOT" $QEMUEXE "$BUILDSCRIPTS/`basename $0`" "$PROFILE"
if [ -s "$BUILDROOT/$DISTFILESPKG" ]; then
mv "$BUILDROOT/$DISTFILESPKG" ./
touch -r ./"$DISTFILESPKG" "$BUILDROOT/$DISTFILESPKG"
fi
exit
}
check_env()
{
if [[ -f /var/lib/misc/extra ]]; then
local TARGET
TARGET=`cat /var/lib/misc/extra`
[[ $PROFILE = $TARGET || $REBUILDNEWROOT = 1 ]] || die "Invalid profile, target system was built with $TARGET profile"
fi
eselect news read
}
prepare_portage()
{
sed -i -e "/^MAKEOPTS/d ; /^PORTAGE_NICENESS/d ; /^USE/d ; /^GRUB_PLATFORMS/d" "$MAKECONF"
(
[[ $JOBS ]] && echo "MAKEOPTS=\"-j$JOBS -l $JOBS\""
echo 'PORTAGE_NICENESS="15"'
echo "USE=\"-* ipv6 multicall ncurses readline syslog threads unicode python_targets_python${PYTHON_VER/./_} python_single_target_python${PYTHON_VER/./_}\""
echo 'GRUB_PLATFORMS="efi-64"'
) >> "$MAKECONF"
# Needed in qemu
if [[ $(uname -m) = aarch64 ]] && ! grep -q "pid-sandbox" "$MAKECONF"; then
echo 'FEATURES="-pid-sandbox"' >> "$MAKECONF"
fi
# Enable binary packages
if ! grep -q "getbinpkg" "$MAKECONF"; then
echo 'FEATURES="$FEATURES getbinpkg"' >> "$MAKECONF"
fi
local KEYWORDS="/etc/portage/package.accept_keywords/tinylinux"
mkdir -p /etc/portage/package.accept_keywords
mkdir -p /etc/portage/package.use
mkdir -p /etc/portage/package.mask
mkdir -p /etc/portage/package.unmask
if [[ ! -f $KEYWORDS ]]; then
local ACCEPT_PKGS
ACCEPT_PKGS=(
"sys-kernel/gentoo-sources"
"sys-kernel/git-sources"
"net-misc/ipsvd"
"sys-apps/hwids"
"=sys-devel/patch-2.7.1-r3"
"=sys-boot/gnu-efi-3.0u"
"=sys-auth/libnss-nis-3.1"
)
local PKG
for PKG in ${ACCEPT_PKGS[*]}; do
echo "${PKG} **" >> "$KEYWORDS"
done
fi
local USE_FLAGS=(
"app-alternatives/awk busybox"
"app-alternatives/bc gnu"
"app-alternatives/bzip2 lbzip2"
"app-alternatives/cpio gnu"
"app-alternatives/gzip pigz"
"app-alternatives/tar gnu"
"app-arch/tar acl"
"dev-cpp/eigen openmp"
"dev-lang/python xml ssl"
"dev-libs/libtomcrypt libtommath"
"dev-libs/libverto libev"
"dev-libs/libxml2 python"
"dev-libs/libxslt python"
"dev-libs/openssl asm bindist tls-heartbeat zlib"
"dev-vcs/git curl"
"media-libs/leptonica jpeg zlib"
"media-libs/libv4l jpeg"
"net-fs/autofs libtirpc"
"net-misc/dropbear pam"
"net-misc/curl ssl openssl curl_ssl_openssl zstd"
"sys-apps/hwids net pci usb"
"sys-auth/pambase pam_krb5 nullok"
"sys-boot/syslinux bios uefi"
"sys-fs/quota rpc"
"sys-fs/squashfs-tools lzma"
"sys-libs/glibc crypt rpc"
"sys-libs/pam nis"
)
touch /etc/portage/package.use/tinylinux
local IDX
for IDX in $(seq 0 $((${#USE_FLAGS[@]} - 1))); do
grep -q "${USE_FLAGS[$IDX]}" /etc/portage/package.use/tinylinux || echo "${USE_FLAGS[$IDX]}" >> /etc/portage/package.use/tinylinux
done
# Mask systemd-tmpfiles pulled by virtual/tmpfiles, pulled e.g. by screen
echo "sys-apps/systemd-tmpfiles" >> /etc/portage/package.mask/tinylinux
# Enable the latest iasl tool
echo "sys-power/iasl ~*" >> $KEYWORDS
# Enable some packages on 64-bit ARM (temporary, until enabled in Gentoo)
if [[ $STAGE3ARCH = arm64 ]]; then
local ACCEPT_PKGS
ACCEPT_PKGS=(
dev-libs/libffi-3.2.1
dev-util/valgrind-3.19.0
net-dns/libidn2-2.0.5
net-fs/autofs-5.1.8-r1
net-libs/libtirpc-1.0.2-r1
net-nds/portmap-6.0
net-nds/rpcbind-0.2.4-r1
net-nds/yp-tools-4.2.3-r1
net-wireless/bluez-5.50-r2
net-wireless/rfkill-0.5-r3
sys-apps/kexec-tools-2.0.22
sys-block/fio-3.27-r4
sys-libs/glibc-2.36
)
local PKG
for PKG in ${ACCEPT_PKGS[*]}; do
echo "=${PKG} **" >> /etc/portage/package.accept_keywords/tegra
done
fi
# Setup split glibc symbols for valgrind and remote debugging
if [[ $TEGRABUILD ]]; then
mkdir -p "/etc/portage/package.env"
echo "sys-libs/glibc debug" > "/etc/portage/package.env/glibc"
echo "dev-util/valgrind debug" > "/etc/portage/package.env/valgrind"
mkdir -p "/etc/portage/env"
echo 'CFLAGS="${CFLAGS} -ggdb"' > "/etc/portage/env/debug"
echo 'CXXFLAGS="${CXXFLAGS} -ggdb"' >> "/etc/portage/env/debug"
echo 'FEATURES="$FEATURES splitdebug compressdebug"' >> "/etc/portage/env/debug"
fi
# Patch uninitialized variable in syslinux
local EBUILD=$PORTAGE/sys-boot/syslinux/syslinux-6.04_pre1-r5.ebuild
if [[ -f $EBUILD ]] && ! grep -q "bios-free-mem" "$EBUILD"; then
boldecho "Patching $EBUILD"
mkdir -p $PORTAGE/sys-boot/syslinux/files
cp "$BUILDSCRIPTS/extra/syslinux-bios-free-mem.patch" $PORTAGE/sys-boot/syslinux/files/
sed -i "s/PATCHES=(/PATCHES=( \"\${FILESDIR}\"\/\${PN}-bios-free-mem.patch/" "$EBUILD"
ebuild "$EBUILD" digest
fi
# Patch compilation failure in autofs
local EBUILD=$PORTAGE/net-fs/autofs/autofs-5.1.6.ebuild
if [[ -f $EBUILD ]] && ! grep -q "gcc=strip" "$EBUILD"; then
boldecho "Patching $EBUILD"
sed -i '/Makefile.rules/ a\\tsed -i -e "/^STRIP.*strip-debug/s/strip/\\$(CC:gcc=strip)/" Makefile.rules' "$EBUILD"
ebuild "$EBUILD" digest
fi
# Install ypbind ebuild
local SRC=ypbind-2.7.2.ebuild
local EBUILD=$PORTAGE/net-nds/ypbind/$SRC
if [[ ! -f $EBUILD ]]; then
boldecho "Adding $EBUILD"
mkdir -p $PORTAGE/net-nds/ypbind
mkdir -p /var/cache/distfiles
cp "$BUILDSCRIPTS/extra/$SRC" "$EBUILD"
ebuild "$EBUILD" digest
fi
# Fix splitdebug feature in glibc
local EBUILD=$PORTAGE/sys-libs/glibc/glibc-2.29-r2.ebuild
if [[ $TEGRABUILD ]] && [[ -f $EBUILD ]] && ! grep -q src_strip "$EBUILD"; then
boldecho "Patching $EBUILD"
cd "$PORTAGE"
patch -p0 < "$BUILDSCRIPTS/extra/glibc-splitdebug.patch"
cd -
ebuild "$EBUILD" digest
fi
# Patch busybox to avoid failing to copy symlinks
if grep -q "die.*copying" "$PORTAGE"/sys-apps/busybox/busybox*.ebuild; then
local EBUILD
for EBUILD in $(grep -l "die.*copying" "$PORTAGE"/sys-apps/busybox/busybox*.ebuild); do
boldecho "Patching $EBUILD"
sed -i '/die.*copying/s/die/echo/' "$EBUILD"
ebuild "$EBUILD" digest
done
fi
# Patch ipsvd compilation failure with newer gcc
local EBUILD="$PORTAGE/net-misc/ipsvd/ipsvd-1.0.0-r3.ebuild"
if ! grep -q incompatible-pointer-types "$EBUILD"; then
boldecho "Patching $EBUILD"
sed -i '/CFLAGS.*conf-cc/s/CFLAGS}/& -Wno-incompatible-pointer-types/' "$EBUILD"
ebuild "$EBUILD" digest
fi
# Prepare GPG keys for binary packages
getuto
}
run_interactive()
{
if [[ $INTERACTIVE = 1 ]]; then
bash
exit
fi
}
emerge_basic_packages()
{
boldecho "Compiling basic host packages"
local SYSLINUX_PKG=""
[[ $(uname -m) = x86_64 ]] && SYSLINUX_PKG="dev-lang/nasm syslinux"
local HOST_PKGS=(
binutils-libs
dev-libs/glib
dosfstools
dropbear
genkernel
grub
lbzip2
less
libtirpc
pkgconfig
btrfs-progs
rpcbind
rpcsvc-proto
squashfs-tools
sys-devel/bc
$SYSLINUX_PKG
zip
)
if ! emerge --quiet --noreplace ${HOST_PKGS[@]}; then
boldecho "Failed to emerge some packages"
boldecho "Please complete installation manually"
bash
fi
USE="static-libs" emerge --quiet --noreplace sys-libs/libxcrypt
# Skip the kernel on Tegra or if it is already installed
if [[ -z $TEGRABUILD ]] && [ ! -e /usr/src/linux ]; then
local KERNELPKG=gentoo-sources
[[ $RCKERNEL = 1 ]] && KERNELPKG=git-sources
if ! USE=symlink emerge --quiet $KERNELPKG; then
boldecho "Failed to emerge some packages"
boldecho "Please complete installation manually"
bash
fi
# WAR for failure when loading drm module
# The kernel wants to load 8KB for the drm module from the reserved
# per-CPU chunk while the size of that chunk also defaults to 8KB,
# but for some reason the block is not page-aligned. Bump default
# size of reserved chunk to to 16KB.
sed -i '/^#define PERCPU_MODULE_RESERVE\>.*\<8\>/s/8/16/' /usr/src/linux/include/linux/percpu.h
fi
}
compile_kernel()
{
local MAKEOPTS
local GKOPTS
local CCPREFIX
# Do not compile kernel for Tegra
if [[ $TEGRABUILD ]]; then
touch /usr/src/linux
return 0
fi
# Skip compilation if kernel has already been built
[ ! -f /boot/vmlinuz-* ] || [[ $REBUILDKERNEL = 1 ]] || return 0
[ -e /usr/src/linux ] && [[ $(readlink /usr/src/linux) = linux-custom ]] && boldecho "Using custom kernel sources"
# Delete old kernel
rm -rf /boot/vmlinuz-*
rm -rf /boot/initramfs-*
rm -f "$INSTALL/tiny/kernel"
rm -f "$INSTALL/tiny/initrd"
# Force regeneration of squashfs
rm -f "$INSTALL/$SQUASHFS"
# Remove disklabel (blkid) from genkernel configuration
sed -i "/^DISKLABEL/s/yes/no/" /etc/genkernel.conf
boldecho "Preparing kernel"
[[ $JOBS ]] && MAKEOPTS="--makeopts=-j$JOBS -l$JOBS"
rm -rf /lib/{modules,firmware}
cp "$BUILDSCRIPTS/kernel-config" /usr/src/linux/.config
if [[ $KERNELMENUCONFIG = 1 ]]; then
boldecho "Configuring kernel"
make -C /usr/src/linux menuconfig
fi
boldecho "Compiling kernel"
genkernel --oldconfig --linuxrc="$BUILDSCRIPTS/linuxrc" --no-mountboot "$MAKEOPTS" kernel
boldecho "Creating initial ramdisk"
local BBCFG="/tmp/init-busy-config"
cp /usr/share/genkernel/defaults/busy-config "$BBCFG"
local BUSYBOX_OPTS=(
"CONFIG_MODPROBE_SMALL=n"
"CONFIG_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE=n"
"CONFIG_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED=n"
"CONFIG_INSMOD=y"
"CONFIG_RMMOD=y"
"CONFIG_LSMOD=y"
"CONFIG_MODPROBE=y"
"CONFIG_FEATURE_MODUTILS_ALIAS=y"
"CONFIG_FEATURE_MODUTILS_SYMBOLS=y"
)
local OPT
for OPT in "${BUSYBOX_OPTS[@]}"; do
sed -i -e "/\<${OPT%=*}\>/s/.*/$OPT/" "$BBCFG"
done
genkernel --oldconfig --linuxrc="$BUILDSCRIPTS/linuxrc" --no-mountboot --no-zfs "$MAKEOPTS" --all-ramdisk-modules --busybox-config="$BBCFG" ramdisk
trim_initrd_modules
}
trim_initrd_modules()
{
boldecho "Trimming initrd"
local NUM_INITRD="$(ls /boot | grep initramfs | wc -l)"
[[ $NUM_INITRD -ge 1 ]] || die "Missing /boot/initramfs"
[[ $NUM_INITRD -eq 1 ]] || die "Multiple /boot/initramfs files"
# Unpack initrd
local INITRD="$(ls /boot/initramfs*)"
rm -rf /tmp/initrd
mkdir /tmp/initrd
cd /tmp/initrd
unxz < "$INITRD" | cpio -i
# Delete unwanted modules
local MODULE_DIRS=(
kernel/drivers/media
kernel/drivers/net/wireless
kernel/drivers/usb/serial
kernel/net/wireless
kernel/sound
)
local DIR
for DIR in "${MODULE_DIRS[@]}"; do
rm -rf lib/modules/*gentoo*/"$DIR"
done
# Compress initrd
find . -print | cpio --create --format=newc | xz -v -e --check=none -z -f -9 -T 0 > "$INITRD"
cd -
}
need_host_package()
{
local HOST_PACKAGES=(
sys-auth/libnss-nis
)
while [[ $# -gt 0 ]]; do
local PKG
for PKG in "${HOST_PACKAGES[@]}"; do
[[ $1 = $PKG ]] && return 0
done
shift
done
return 1
}
target_emerge()
{
ROOT="$NEWROOT" emerge "$@"
local NEWS="var/lib/gentoo/news/news-gentoo.unread"
if test -s "$NEWROOT/$NEWS"; then
echo "Erasing news from $NEWROOT/$NEWS"
echo -n > "$NEWROOT/$NEWS"
fi
}
install_package()
{
USE="$2" target_emerge --quiet --usepkg --buildpkg $3 "$1"
}
list_package_files()
{
grep -v "^dir" "$NEWROOT"/var/db/pkg/$1-*/CONTENTS | \
cut -f 2 -d ' ' | cut -c 1 --complement
}
install_syslinux()
{
[[ $TEGRABUILD ]] && return 0
[[ $(uname -m) = x86_64 ]] || return 0
local DESTDIR
local SAVEROOT
DESTDIR="/tmp/syslinux"
SAVEROOT="$NEWROOT"
NEWROOT="$DESTDIR" target_emerge --quiet --nodeps --usepkg --buildpkg syslinux mtools
NEWROOT="$SAVEROOT"
cp -p "$DESTDIR/sbin/extlinux" "$NEWROOT/usr/bin/extlinux"
local FILE
for FILE in syslinux mcopy mattrib; do
cp -p "$DESTDIR/usr/bin/$FILE" "$NEWROOT/usr/bin/$FILE"
done
rm -rf "$DESTDIR"
mkdir -p "$NEWROOT/usr/share/syslinux"
cp /usr/share/syslinux/mbr.bin "$NEWROOT/usr/share/syslinux"/
cp /usr/share/syslinux/gptmbr.bin "$NEWROOT/usr/share/syslinux"/
cp -r /usr/share/syslinux/efi64 "$NEWROOT/usr/share/syslinux"/
}
remove_gentoo_services()
{
local DIR
while [[ $# -gt 0 ]]; do
for DIR in init.d conf.d; do
rm -f "$NEWROOT/etc/$DIR/$1"
done
shift
done
}
record_dir_symlinks()
{
local SYMLINK
for SYMLINK in /bin /sbin /lib /lib64 /usr/sbin; do
record_symlink sys-libs/glibc "$SYMLINK"
done
}
build_newroot()
{
# Remove old build
if [[ $REBUILDNEWROOT = 1 ]]; then
rm -rf "$NEWROOT"
rm -rf "$INSTALL"
rm -f /var/lib/misc/extra
fi
# Skip if new root already exists
[[ -d $NEWROOT ]] && return 0
# Handle Gentoo news items
eselect news read
boldecho "Building TinyLinux root filesystem"
mkdir -p "$NEWROOT"
mkdir -p "$NEWROOT/var/lib/gentoo/news"
# Prepare merged dirs
# Note: this list is duplicated in record_dir_symlinks
mkdir -p "$NEWROOT/usr/bin"
mkdir -p "$NEWROOT/usr/lib"
mkdir -p "$NEWROOT/usr/lib64"
ln -sf usr/bin "$NEWROOT/bin"
ln -sf usr/bin "$NEWROOT/sbin"
ln -sf usr/lib "$NEWROOT/lib"
ln -sf usr/lib64 "$NEWROOT/lib64"
ln -sf bin "$NEWROOT/usr/sbin"
# Restore busybox config file
local BUSYBOXCFG="$BUILDSCRIPTS/busybox-config"
local HOSTBUSYBOXCFGDIR=/etc/portage/savedconfig/sys-apps
local TARGETBUSYBOXCFGDIR="$NEWROOT/etc/portage/savedconfig/sys-apps"
local BUSYBOX_VER
ls $PORTAGE/sys-apps/busybox/*ebuild | sed "s:.*/:: ; s:\.ebuild::" | while read BUSYBOX_VER; do
mkdir -p "$HOSTBUSYBOXCFGDIR"
cp "$BUSYBOXCFG" "$HOSTBUSYBOXCFGDIR/$BUSYBOX_VER"
mkdir -p "$TARGETBUSYBOXCFGDIR"
cp "$BUSYBOXCFG" "$TARGETBUSYBOXCFGDIR/$BUSYBOX_VER"
done
# Setup directories for valgrind and for debug symbols
if [[ $TEGRABUILD ]]; then
rm -rf /tiny/debug /tiny/valgrind
mkdir -p /tiny/debug/mnt /tiny/valgrind
mkdir -p "$NEWROOT/usr/libexec"
mkdir -p "$NEWROOT/usr/share"
ln -s /tiny/debug "$NEWROOT/usr/lib64/debug"
ln -s /tiny/debug "$NEWROOT/usr/lib/debug"
ln -s /tiny/valgrind "$NEWROOT/usr/libexec/valgrind"
fi
# Copy keys used for binary packages
mkdir -p "$NEWROOT/usr/share/openpgp-keys"
cp "/usr/share/openpgp-keys/gentoo-release.asc" "$NEWROOT/usr/share/openpgp-keys"/
# Install basic system packages
install_package sys-libs/glibc "" --nodeps # Latest glibc pulls deps!!!
record_dir_symlinks # Record directory symlinks with glibc
install_package sys-auth/libnss-nis
rm -rf "$NEWROOT"/lib*/gentoo # Remove Gentoo scripts
# Remove 32-bit glibc in 64-bit builds
rm -rf "$NEWROOT"/lib
rm -rf "$NEWROOT"/usr/lib
mkdir -p "$NEWROOT"/usr/lib
ln -s usr/lib "$NEWROOT/lib"
[[ $TEGRABUILD ]] && ln -s /tiny/debug "$NEWROOT/usr/lib/debug"
install_package ncurses
ln -s libncurses.so.6 "$NEWROOT"/lib64/libncurses.so.5
ln -s libncursesw.so.6 "$NEWROOT"/lib64/libncursesw.so.5
install_package pciutils
rm -f "$NEWROOT/usr/share/misc"/*.gz # Remove compressed version of hwids
install_package busybox "make-symlinks mdev nfs pam savedconfig"
rm -rf "${NEWROOT}-busybox"
mkdir -p "${NEWROOT}-busybox"/{bin,sbin,usr/bin,usr/sbin} # workaround for busybox symlinks clashing with merged bin/sbin/lib
NEWROOT="${NEWROOT}-busybox" install_package busybox "make-symlinks mdev nfs savedconfig" --nodeps
rm -f "$NEWROOT"/etc/portage/savedconfig/sys-apps/._cfg* # Avoid excess of portage messages
create_busybox_symlinks
rm "$NEWROOT/usr/bin/env"
ln -s busybox "$NEWROOT/usr/bin/env"
install_package dropbear "multicall"
ignore_busybox_symlinks /usr/bin/bc
install_package sys-devel/bc
install_package net-wireless/wireless-tools
install_package dev-libs/openssl
COLLISION_IGNORE="/bin /usr/bin" install_package app-alternatives/tar
# Install more basic packages
install_package nano
install_package bash "net"
test -e "$NEWROOT/bin/bash" || ln -s $(ls "$NEWROOT"/bin/bash-* | head -n 1 | xargs basename) "$NEWROOT/bin/bash"
# Install NFS utils
install_package net-nds/rpcbind
if [[ $TEGRABUILD ]]; then
COLLISION_IGNORE="/bin /sbin /usr/bin /usr/sbin" install_package sys-apps/util-linux "" --nodeps
fi
emerge --quiet --noreplace dev-libs/libevent # nfs-utils dependency
install_package nfs-utils "nfsv3" --nodeps
remove_gentoo_services nfs nfsmount rpcbind rpc.statd
rm "$NEWROOT/usr/bin/fsidd" # Remove due to lack of libevent and libsqlite3
# Additional non-Tegra-specific packages
if [[ -z $TEGRABUILD ]]; then
install_package libusb-compat
install_package numactl
install_package efibootmgr
install_package ntfs3g "fuse mount-ntfs ntfsprogs xattr" --nodeps # nodeps to avoid util-linux
install_package linux-firmware
remove_gentoo_services netmount
fi
# Finish installing dropbear
mkdir "$NEWROOT/etc/dropbear"
dropbearkey -t rsa -f "$NEWROOT/etc/dropbear/dropbear_rsa_host_key"
dropbearkey -t ecdsa -f "$NEWROOT/etc/dropbear/dropbear_ecdsa_host_key"
dropbearkey -t ed25519 -f "$NEWROOT/etc/dropbear/dropbear_ed25519_host_key"
( cd "$NEWROOT/usr/bin" && ln -s dbclient ssh )
( cd "$NEWROOT/usr/bin" && ln -s dbscp scp )
# Copy libgcc and libstdc++ needed by some tools
cp /usr/lib/gcc/*/*/{libgcc_s.so.1,libstdc++.so.6} "$NEWROOT/lib64"/
# Remove linuxrc script from busybox
rm -rf "$NEWROOT/linuxrc"
# Update ns switch
sed -i "s/compat/db files nis/" "$NEWROOT/etc/nsswitch.conf"
sed -i "/^[ #]*initgroups *:/s/^[ #]*//" "$NEWROOT/etc/nsswitch.conf"
# Remove unneeded scripts
remove_gentoo_services autofs dropbear fuse mdev mit-krb5kadmind mit-krb5kdc mit-krb5kpropd nfsclient nscd pciparm ypbind
rm -f "$NEWROOT/etc"/{init.d,conf.d}/busybox-*
rm -rf "$NEWROOT/etc/systemd"
# Build extra tools
local TOOL
for TOOL in setdomainname modalias; do
echo ">>> Compiling $TOOL"
gcc -O3 -Wall -Wextra -Wl,-s -o "$NEWROOT/usr/bin/$TOOL" "$BUILDSCRIPTS/extra/$TOOL.c"
done
# Copy TinyLinux scripts
local FILE
( cd "$BUILDSCRIPTS/scripts" && find ./ ! -type d && find ./ -type f -name ".*" ) | while read FILE; do
local SRC
local DEST
SRC="$BUILDSCRIPTS/scripts/$FILE"
DEST=$(sed 's:/lib/:/lib64/:' <<< "$NEWROOT/$FILE")
mkdir -p $(dirname "$DEST")
cp -P "$SRC" "$DEST"
if [[ ! -h $DEST ]]; then
if [[ ${FILE:2:11} = etc/init.d/ || $FILE =~ etc/acpi/actions || $FILE =~ etc/udhcpc.scripts ]]; then
chmod 755 "$DEST"
elif [[ ${FILE:2:4} = etc/ || $FILE =~ usr/share ]]; then
chmod 644 "$DEST"
else
chmod 755 "$DEST"
fi
fi
done
# Remove additional terminals on ARM
if [[ $TEGRABUILD ]] || [[ $(uname -m) = aarch64 ]]; then
sed -i "/^tty.*getty/d" "$NEWROOT"/etc/inittab
fi
# Install syslinux so TinyLinux can reinstall itself
install_syslinux
# Create /etc/passwd and /etc/group
echo "root:x:0:0:root:/root:/usr/bin/bash" > "$NEWROOT/etc/passwd"
echo "root::0:root" > "$NEWROOT/etc/group"
echo "dhcp:x:101:101:dhcp:/:/usr/bin/false" >> "$NEWROOT/etc/passwd"
echo "dhcp::101:" >> "$NEWROOT/etc/group"
echo "tftp:x:102:102:tftp:/:/usr/bin/false" >> "$NEWROOT/etc/passwd"
echo "tftp::102:" >> "$NEWROOT/etc/group"
echo "tty::5:" >> "$NEWROOT/etc/group"
echo "disk::6:root" >> "$NEWROOT/etc/group"
echo "kmem::9:" >> "$NEWROOT/etc/group"
echo "users::100:" >> "$NEWROOT/etc/group"
echo "audio::107:" >> "$NEWROOT/etc/group"
# Create /etc/shells so that root can log in remotely using bash
local ASHELL