-
Notifications
You must be signed in to change notification settings - Fork 2
/
gentoo-init.docker
executable file
·1690 lines (1534 loc) · 48.8 KB
/
gentoo-init.docker
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
#! /usr/bin/env bash
set -eu
set -o pipefail
trace="${TRACE:-}"
debug="${DEBUG:-}"
# With 'debug' enabled, set 'DEBUG_EXTRA_ARGS' to pass additional arguments
# to 'podman build'...
#DEBUG_EXTRA_ARGS=''
# If the python release(s) used in the upstream stage3 image differ from those
# we wish to build against or those specified in the host's
# /etc/portage/package.use file(s), then 'python_targets_override' must be set
# to the desired release below:
#declare -x python_targets_override='python3_13'
if [[ -n "${BASH_VERSINFO[0]:-}" ]] && (( BASH_VERSINFO[0] < 4 )); then
echo >&2 "FATAL: bash-4.0.0 or later is required to run this script - on" \
"macOS, try 'brew install bash'"
exit 1
fi
if [[ "$( uname -s )" == 'Darwin' ]]; then
readlink() {
if type -pf realpath >/dev/null 2>&1; then
realpath "${2}"
else
perl -MCwd -le 'print Cwd::abs_path shift' "${2}"
fi
} # readlink
export -f readlink
fi
cd "$( dirname "$( readlink -e "${0}" )" )" || exit 1
declare -i NOTIFY_MKDIR=0
# Set by common/vars.sh
#
#declare docker='docker'
#declare mail_from=''
#declare mail_to=''
#declare mail_mta=''
#declare env_name=''
#declare stage3_name=''
#declare init_name=''
#declare base_name=''
#declare build_name=''
#declare stage3_flags_file=''
#declare environment_file=''
#declare environment_filter=''
#declare base_dir=''
#declare use_cpu_arch=''
#declare gcc_target_opts=''
#declare rust_target_opts=''
#declare use_cpu_flags=''
#declare use_cpu_flags_raw=''
#declare use_essential=''
#declare use_essential_gcc=''
#declare python_default_target=''
#declare JOBS=''
#declare MAXLOAD=''
#declare bold=''
#declare red=''
#declare blue=''
#declare purple=''
#declare reset=''
# Set by common/run.sh _docker_setup()
#
#declare docker_arch=''
#declare arch=''
# pkg_pypy, pkg_pypy_use & pkg_pypy_post_remove are defined in common/vars.sh
#declare pkg_pypy=''
#declare pkg_pypy_use=''
#declare pkg_pypy_post_remove=''
# shellcheck disable=SC1091
[[ ! -s common/vars.sh ]] || . common/vars.sh
# shellcheck disable=SC2034 # Set from common/vars.sh
[[ -n "${__COMMON_VARS_INCLUDED:-}" ]] || {
echo >&2 "FATAL: Inclusion of common defaults from '${PWD}/common/vars.sh' failed"
exit 1
}
declare _output=''
if ! [[ -x "$( type -pf "${_command}" )" ]]; then
echo >&2 "FATAL: Cannot locate binary '${_command}'"
exit 1
elif ! _output="$( "${_command}" info 2>&1 )"; then
if [[ "${_command}" == 'podman' ]]; then
echo >&2 "FATAL: Unable to successfully execute '${_command}' - do" \
"you need to run '${_command} machine start' or re-run" \
"'$( basename "${0}" )' as 'root'?"
else
echo >&2 "FATAL: Unable to successfully execute '${_command}' - do" \
"you need to re-run '$( basename "${0}" )' as 'root'?"
fi
exit 1
elif (( $( id -u ) )) && grep -Fq -- 'rootless: false' <<<"${_output}"; then
echo >&2 "FATAL: Please re-run '$( basename "${0}")' as user 'root'"
exit 1
fi
unset _output
[[ -n "${base_dir:-}" ]]
[[ -d "${base_dir}" ]]
[[ -e "${base_dir}"/Dockerfile.env ]]
[[ -e "${base_dir}"/Dockerfile.init ]]
[[ -e "${base_dir}"/Dockerfile.build ]]
# shellcheck disable=SC2034
IMAGE="${base_name}:latest"
# shellcheck disable=SC1091
[[ -s common/run.sh ]] && . common/run.sh
# N.B. Below, 'docker' is a shell-function if no 'docker' binary exists...
#
if ! docker --version >/dev/null 2>&1; then
# shellcheck disable=SC2154
die "'${_command}' failed to execute: ${?}"
elif ! docker image build --help | grep -Fq -- '--platform'; then
die "'${_command} image build' lacks '--platform' support (try" \
"podman-3.x or later)"
fi
if
[[ -z "${env_name:-}" ]] ||
[[ -z "${stage3_name:-}" ]] ||
[[ -z "${init_name:-}" ]] ||
[[ -z "${base_name:-}" ]] ||
[[ -z "${build_name:-}" ]]
then
die "Image names not correctly set in '$( pwd )/common/vars.sh'"
fi
declare target_opt=''
declare rust_target_opt=''
declare -i build_helper=0
declare -i force=0
if [[ -n "${*:-}" ]]; then
if grep -Eq -- ' -(h|-help) ' <<<" ${*} "; then
output >&2 "Usage: $( basename "${0}" ) [--env] [--stage3] [--init]" \
"[[--force] --base] [[--force] --build] [[--force] --helper]"
exit 0
elif ! grep -Eq <<<" ${*} " -- \
'^(\s*--(env|stage3|init|base|build|helper|force|force-helper)\s*){1,7}$'
then
die "Unknown args '${*}'"
fi
if grep -Fq -- ' --force ' <<<" ${*} "; then
if ! grep -Eq -- ' --(base|build|helper) ' <<<" ${*} "; then
die "'--force' is only valid with '--base', '--build', or" \
"'--helper'"
fi
fi
fi
declare python_single_target="${python_targets_override:+"${python_targets_override%%" "*}"}"
declare python_targets="${python_targets_override:-}"
if [[ -z "${python_targets:-}" ]] && type -pf portageq >/dev/null 2>&1; then
python_targets="$( portageq envvar 'PYTHON_SINGLE_TARGET' )"
fi
if [[ -z "${python_targets:-}" ]]; then
python_targets="${python_default_target:-}"
fi
declare mask_file=''
if [[ -n "${arch:-}" ]] &&
[[ -f "${base_dir}/etc/portage/profile/use.mask.${arch}" ]]
then
mask_file="${base_dir}/etc/portage/profile/use.mask.${arch}"
fi
[[ -z "${mask_file:-}" ]] &&
mask_file="${base_dir}/etc/portage/profile/use.mask"
if [[ -n "${python_single_target:-}" ]]; then
[[ "${python_single_target}" =~ ^python[[:digit:]]_[[:digit:]]+$ ]] ||
die "python_single_target value '${python_single_target}'" \
"doesn't appear to be a valid python version" \
"(matching 'pythonX_Y', e.g. 'python3_12')"
if [[ -f "${mask_file}" ]]; then
if sed 's/#.*$//' "${mask_file}" |
grep -Fw -- "^python_single_target_${python_single_target}"
then
die "python_single_target value" \
"'python_single_target_${python_single_target}'" \
"is masked by '${mask_file}'"
fi
fi
fi
if [[ -n "${python_targets:-}" ]]; then
for python_target in ${python_targets}; do
[[ "${python_target}" =~ ^python[[:digit:]]_[[:digit:]]+$ ]] ||
die "python_targets value '${python_target}'" \
"doesn't appear to be a valid python version" \
"(matching 'pythonX_Y', e.g. 'python3_12')"
if [[ -f "${mask_file}" ]]; then
if sed 's/#.*$//' "${mask_file}" |
grep -Fw -- "^python_targets_${python_target}"
then
die "python_targets value 'python_targets_${python_target}' is" \
"masked by '${mask_file}'"
fi
fi
done
fi
unset mask_file
declare -a args=()
for arg in "${@:-}"; do
case "${arg:-}" in
*helper)
build_helper=1
args+=( "${arg}" )
;;
--force)
force=1
;;
*)
args+=( "${arg}" )
;;
esac
done
set -- "${args[@]:-}"
unset args arg
_docker_setup
[[ -d log ]] || mkdir -p log
pushd "${base_dir}" >/dev/null 2>&1 ||
die "chdir() to '${base_dir}' failed; ${?}"
checkids() {
local name="${1:-}"
local tag="${2:-"latest"}"
#inherit docker stage3_name env_id env_name gentoo_stage3_id bold reset red
local repo='' images='' stage3_env_id='' stage3_src_id=''
if [[ -z "${name:-}" ]]; then
error "Image name not set"
return 1
fi
if [[
-z "${_command:-}" ||
-z "${stage3_name:-}" ||
-z "${env_name:-}" ||
-z "${env_id:-}"
]]; then
error "Required global variable not set"
return 1
fi
local relation="inherited by"
if [[ "${name#*"/"}" == "${stage3_name#*"/"}" ]]; then
relation="about to be linked to"
fi
if ! [[ "${name}" =~ / ]]; then
repo='localhost/'
fi
images="$( docker image ls --noheading \
"${repo}${name}:${tag}" 2>/dev/null
)"
# Older releases of 'podman' exclude the repo when it is 'localhost/'...
# FIXME: Can't we just check whether 'images' is set?
if grep -Eq -- "^(${repo:-"${name%"/"*}"})?${name#*"/"}" <<<"${images}"
then
if stage3_env_id="$( # <- Syntax
stage3_repo=''
if ! [[ "${stage3_name}" =~ / ]]; then
stage3_repo='localhost/'
fi
docker image inspect \
--format='{{index .Config.Labels "environment-from"}}' \
"${stage3_repo:-}${stage3_name}:${tag}" 2>/dev/null
)" && [[ -n "${stage3_env_id:-}" ]]
then
stage3_env_id="$( echo "${stage3_env_id:-}" | cut -d':' -f 2- )"
# Does our current 'gentoo-env' image match the ID encoded in our
# local stage3?
# N.B.: 'env_id' inherited from global scope
if [[ "${env_id}" != "${stage3_env_id:-}" ]]; then
output >&2 "${bold:-}WARN${reset:-}: ${red:-}" \
"The current '${env_name#*"/"}' image (${env_id})" \
"differs from that used to build the" \
"'${stage3_name#*"/"}' image (${stage3_env_id:-})" \
"${relation} this stage${reset:-}"
fi
fi
if [[ -n "${gentoo_stage3_id:-}" ]]; then
# N.B.: Only valid for our 'init' and (local) 'stage3' images
if stage3_src_id="$( # <- Syntax
docker image inspect \
--format='{{index .Config.Labels "stage3-from"}}' \
"${repo}${name}:${tag}" 2>/dev/null
)" && [[ -n "${stage3_src_id:-}" ]]
then
stage3_src_id="$( # <- Syntax
echo "${stage3_src_id:-}" |
awk -F':' '{ print $NF }'
)"
# Does our upstream stage3 match the ID encoded in our current
# image?
if [[ "${gentoo_stage3_id}" != "${stage3_src_id:-}" ]]; then
output >&2 "${bold:-}WARN${reset:-}: ${red:-}" \
"The upstream 'stage3' image (${gentoo_stage3_id})" \
"differs from that used to build the '${name#*"/"}'" \
"image (${stage3_src_id:-}) ${relation} this" \
"stage${reset}"
fi
fi
fi
fi
# The above is a warning rather than an error, so...
return 0
} # checkids
makedir() {
local dir="${1:-}"
[[ -n "${dir}" ]] || return 1
if ! (( NOTIFY_MKDIR )) && (( EUID )); then
NOTIFY_MKDIR=1
info "In order to run on a non-gentoo system, we need to create" \
"several system"
info "directories."
info "You may be now prompted to enter your password in order to" \
"authenticate"
info "with superuser credentials and allow for this to proceed:"
fi
if [[ "$( uname -s )" == 'Darwin' ]] || (( EUID )); then
sudo mkdir -pm 0755 "${dir}" || die "mkdir() on '${dir}' failed: ${?}"
sudo chown ${EUID} "${dir}" || die "chown() on '${dir}' failed: ${?}"
else
mkdir -p "${dir}" || die "mkdir() on '${dir}' failed: ${?}"
fi
info " * ${dir}"
} # makedir
declare repo_path_srcshelton=''
# Check for environment sanity...
if ! type -pf portageq >/dev/null 2>&1; then
warn "Not running on Gentoo host system, not checking eclass overrides"
# shellcheck disable=SC2043
for dir in 'repos.conf' 'package.mask' 'savedconfig'; do
[[ -d "/etc/portage/${dir}" ]] || makedir "/etc/portage/${dir}"
done
unset dir
for cfg in DEFAULT.conf gentoo.conf srcshelton.conf; do
[[ -s /etc/portage/repos.conf/${cfg} ]] ||
cp -v etc/portage/repos.conf/${cfg} /etc/portage/repos.conf/
done
unset cfg
declare -i private_gentoo_clone=0
declare repo_path_gentoo='/var/db/repo/gentoo'
repo_path_srcshelton='/var/db/repo/srcshelton'
# FIXME: Hardcoded paths, use repos.conf
if
[[ ! -d "${repo_path_gentoo}" ]] &&
[[ ! -d /var/db/repos/gentoo ]]
then
# macOS 'df' lacks the below options
if [[ "$( uname -s )" != 'Darwin' ]]; then
for dir in /var/db/repo{,s} /var/db; do
if [[ -d "${dir}" ]]; then
# FIXME: Figure out best macOS implementation
if (( $( # <- Syntax
df --block-size=1G --output=avail "${dir}" |
tail -n 1
) < 2 )); then
die "Insufficient filesystem space to download" \
"Gentoo Portage repo: 2GB required"
fi
break
fi
done
unset dir
fi
type -pf git >/dev/null 2>&1 || die "required binary 'git' missing"
note "Creating local Gentoo Portage repo mirror at" \
"'${repo_path_gentoo}' ..."
# shellcheck disable=SC2043,SC2066
for dir in "$( dirname "${repo_path_gentoo}" )"; do
[[ -d "${dir}" ]] || makedir "${dir}"
done
unset dir
git clone https://github.com/gentoo/gentoo.git "${repo_path_gentoo}" ||
die "git 'clone' operation failed: ${?}"
if ! git config --global --get-all safe.directory |
grep -q "${repo_path_gentoo%"/"}"
then
git config --global --add safe.directory "${repo_path_gentoo}" ||
die "git safe directory addition of '${repo_path_gentoo}'" \
"failed: ${?}"
fi
private_gentoo_clone=1
elif [[ -d /var/db/repos/gentoo ]]; then
repo_path_gentoo='/var/db/repos/gentoo'
fi
if
[[ ! -d "${repo_path_srcshelton}" ]] &&
[[ ! -d /var/db/repos/srcshelton ]]
then
type -pf git >/dev/null 2>&1 || die "required binary 'git' missing"
note "Creating custom packages repo mirror at" \
"'${repo_path_srcshelton}' ..."
# shellcheck disable=SC2043,SC2066
for dir in "$( dirname "${repo_path_srcshelton}" )"; do
[[ -d "${dir}" ]] || makedir "${dir}"
done
unset dir
git clone \
https://github.com/srcshelton/gentoo-ebuilds.git \
"${repo_path_srcshelton}" ||
die "git 'clone' operation failed: ${?}"
if ! git config --global --get-all safe.directory |
grep -q "${repo_path_srcshelton%"/"}"
then
git config --global --add safe.directory \
"${repo_path_srcshelton}" ||
die "git safe directory addition of" \
"'${repo_path_srcshelton}' failed: ${?}"
fi
elif [[ -d /var/db/repos/srcshelton ]]; then
repo_path_srcshelton='/var/db/repos/srcshelton'
fi
declare ref=''
if
[[ -s "${repo_path_srcshelton}/.portage_commit" ]] &&
ref="$( # <- Syntax
grep -Fv '#' "${repo_path_srcshelton}/.portage_commit"
)" &&
[[ -n "${ref:-}" ]]
then
if ! git config --global --get-all safe.directory |
grep -q "${repo_path_gentoo%"/"}"
then
git config --global --add safe.directory "${repo_path_gentoo}" ||
die "git safe directory addition of '${repo_path_gentoo}'" \
"failed: ${?}"
fi
pushd "${repo_path_gentoo}" >/dev/null 2>&1
declare head=''
head="$( git rev-parse HEAD )"
if [[ "${ref}" == "${head:-}" ]]; then
note "Local Gentoo Portage repo mirror is in sync with custom" \
"packages repo at commit '${ref:0:7}'"
elif git rev-parse -q --verify "${ref}^{commit}" >/dev/null 2>&1; then
info "Local Gentoo Portage repo mirror is currently at commit" \
"'${head:0:7}' but can be reverted to custom packages repo" \
"target commit '${ref:0:7}'"
if (( private_gentoo_clone )); then
if git reset --hard "${ref}"; then
info "Local Gentoo Portage repo mirror is now in sync" \
"with custom packages repo at commit '${ref:0:7}'"
else
error "Unable to reset local Gentoo Portage repo mirror" \
"to commit '${ref:0:7}': ${?}"
fi
else
warn "Not changing local Gentoo Portage repo mirror contents" \
"for potentailly shared clone"
fi
else
error "Custom packages target commit '${ref:0:7}' does not exist" \
"in local Gentoo Portage repo mirror - mirror out of date?"
fi
unset head
# Return from ${repo_path_gentoo}
popd >/dev/null 2>&1
fi
unset ref
for dir in /var/cache/portage/dist \
/var/log/portage \
/var/cache/portage/pkg/${ARCH:-"${arch}"}/${PKGHOST:-"docker"}
do
[[ -d "${dir}" ]] || makedir "${dir}"
done
unset dir
unset private_gentoo_clone repo_path_gentoo
# Return from ${base_dir}
popd >/dev/null 2>&1
# Flags may have changed...
unset __COMMON_VARS_INCLUDED
# shellcheck disable=SC1091
[[ ! -s common/vars.sh ]] || . common/vars.sh
# shellcheck disable=SC2034 # Set from common/vars.sh
[[ -n "${__COMMON_VARS_INCLUDED:-}" ]] || {
echo >&2 "FATAL: Inclusion of common defaults from" \
"'${PWD}/common/vars.sh' failed"
exit 1
}
pushd "${base_dir}" >/dev/null 2>&1 ||
die "chdir() to '${base_dir}' failed; ${?}"
else # type -pf portageq >/dev/null 2>&1; then
declare -i rc=0
while read -r repo; do
# FIXME: Discover default rather than assuming 'gentoo'?
[[ "${repo}" == 'gentoo' ]] && continue
while read -r dir; do
if [[ -d "${dir}"/eclass ]]; then
grep -hR eclass /etc/portage/repos.conf |
sed 's/#.*//' |
grep '^eclass-overrides' |
cut -d '=' -f 2- |
grep -Fqw -- "${repo}" &&
continue
warn "ebuild repo '${repo}' contains class overrides, but" \
"portage isn't configured to allow their use"
warn "Add/update 'eclass-overrides = ${repo}' in" \
"/etc/portage/repos.conf"
rc=1
fi
done < <( portageq get_repo_path "${EROOT:-"/"}" "${repo}" )
unset dir
done < <( portageq get_repos "${EROOT:-"/"}" | xargs -n 1 )
unset repo
(( rc )) && sleep 5
unset rc
# FIXME: Discover default rather than assuming 'gentoo'?
declare repo_path_gentoo=''
declare ref=''
repo_path_gentoo="$( # <- Syntax
portageq get_repo_path "${EROOT:-"/"}" 'gentoo' 2>/dev/null
)"
repo_path_srcshelton="$( # <- Syntax
portageq get_repo_path "${EROOT:-"/"}" 'srcshelton' 2>/dev/null
)"
if
[[ -n "${repo_path_gentoo:-}" && -d "${repo_path_gentoo}" ]] &&
[[ -n "${repo_path_srcshelton:-}" && -d "${repo_path_srcshelton}" ]]
then
if
[[ -s "${repo_path_srcshelton}/.portage_commit" ]] &&
ref="$( # <- Syntax
grep -Fv '#' "${repo_path_srcshelton}/.portage_commit"
)" &&
[[ -n "${ref:-}" ]]
then
if ! git config --global --get-all safe.directory |
grep -q "${repo_path_gentoo%"/"}"
then
git config --global --add safe.directory "${repo_path_gentoo}" ||
die "git safe directory addition of '${repo_path_gentoo}'" \
"failed: ${?}"
fi
pushd "${repo_path_gentoo}" >/dev/null 2>&1
declare head=''
head="$( git rev-parse HEAD )"
if [[ "${ref}" == "${head:-}" ]]; then
note "Local Gentoo Portage repo mirror is in sync with" \
"custom packages repo at commit '${ref:0:7}'"
elif
git rev-parse -q --verify "${ref}^{commit}" >/dev/null 2>&1
then
warn "Local Gentoo Portage repo mirror is currently at" \
"commit '${head:0:7}' but can be reverted to custom" \
"packages repo target commit '${ref:0:7}'"
else
error "Custom packages target commit '${ref:0:7}' does not" \
"exist in local Gentoo Portage repo mirror - mirror out" \
"of date?"
fi
unset head
# Return from ${repo_path_gentoo}
popd >/dev/null 2>&1
else
warn "No upstream commit marker found in" \
"'${repo_path_srcshelton}/.portage_commit'"
fi
else
die "Could not determine essential repo paths for 'gentoo'" \
"(${repo_path_gentoo:-}) or 'srcshelton'" \
"(${repo_path_srcshelton:-})"
fi
unset ref repo_path_gentoo
fi
# 'cut' separately to emulate 'pipefail' when using /bin/sh
#
# (... which we aren't any longer - but pipefail does seem to cause some
# unexpected behaviour)
#
declare sum=''
sum="$( sha1sum entrypoint.sh )"
sum="$( echo "${sum}" | cut -d' ' -f 1 )"
gentoo_stage3_tag='latest'
# https://hub.docker.com/r/gentoo/stage3 tag-naming is a mess - not only does
# it keep changing, but there's now an inconsistency as to whether the init
# system is tagged onto the image or not (for example, the amd64 image used to
# be 'amd64-nomultilib', which is now no longer being updated. There are now
# new 'nomultilib' and 'amd64-nomultilib-openrc' images - while the 32bit image
# name has changed from 'x86' to 'i686-openrc' and the ARM image names are
# unchanged in the case of 'openrc' but now also have variants with a 'systemd'
# suffix)
# Update: As-of 18th March 2022, the 'arm64' image is no longer being updated,
# but the 'latest' image is now multi-arch, and there's an 'arm64-openrc' image
# also...
#
# shellcheck disable=SC2154
case "${docker_arch}" in
'amd64')
gentoo_stage3_tag="${docker_arch}-nomultilib-openrc" ;;
'arm64')
gentoo_stage3_tag="${docker_arch}-openrc" ;;
'arm/v7')
gentoo_stage3_tag='armv7a_hardfp-openrc' ;;
'arm/v6')
gentoo_stage3_tag='armv6j_hardfp-openrc' ;;
'i386')
gentoo_stage3_tag='i686-openrc' ;;
esac
target_opt="${gcc_target_opts:-}"
case "${docker_arch}" in
'arm'*)
# See https://community.arm.com/developer/tools-software/tools/b/tools-software-ides-blog/posts/compiler-flags-across-architectures-march-mtune-and-mcpu
[[ -z "${gcc_target_opts:-}" ]] &&
target_opt='-mcpu=native' ;;
esac
rust_target_opt="${rust_target_opts:-}"
env_id="$( docker image ls --noheading \
--format='{{.ID}}' \
"${env_name}:latest"
)" || die "Could not determine environment stage image name"
local_stage3_id="$( docker image ls --noheading \
--format='{{.ID}}' \
"${stage3_name}:latest"
)" || die "Could not determine local stage3 image name"
[[ -z "${trace:-}" ]] || set -o xtrace
# Create empty stage with shared environment variables ...
#
if [[ -z "${*:-}" ]] || grep -Fq -- ' --env ' <<<" ${*} "; then
output
# shellcheck disable=SC2154
output " ${blue}*${reset} Creating image '${env_name}:latest' ..."
output
# NAME: env
# LABELS:
# USES:
# USED_BY: stage3 build
declare -a build_args=()
# shellcheck disable=SC2207
build_args+=( # <- Syntax
--platform "linux/${docker_arch}"
--build-arg SET_TERM="${TERM:-}"
--build-arg SET_USE_ESSENTIAL="${use_essential:-}"
--build-arg SET_USE_ESSENTIAL_GCC="${use_essential_gcc:-}"
--build-arg JOBS="${JOBS:-}"
--build-arg MAXLOAD="${MAXLOAD:-}"
#--build-arg NICENESS="10"
--build-arg MAIL_FROM="${mail_from:-}"
--build-arg MAIL_TO="${mail_to:-}"
--build-arg MAIL_MTA="${mail_mta:-}"
--build-arg ARCH="${arch}"
--build-arg PROFILE_PATH="${profile:-}"
--build-arg CHOST="${chost:-}"
# Optionally set below:
#--build-arg CPU_FLAGS_ARM
#--build-arg CPU_FLAGS_PPC
#--build-arg CPU_FLAGS_X86
#--build-arg TARGET_OPT
#--build-arg CPPFLAGS
#--build-arg CFLAGS
#--build-arg CXXFLAGS
#--build-arg FFLAGS
#--build-arg FCFLAGS
#--build-arg LDFLAGS
#--build-arg FLFLAGS
#--build-arg RUSTFLAGS
--compress
--file 'Dockerfile.env'
--network none
--no-cache
$( # <- Syntax
docker image build --squash-all --help >/dev/null 2>&1 &&
echo '--squash-all'
)
--tag "${env_name}:latest"
--rm
)
# shellcheck disable=SC2154
[[ -z "${use_cpu_arch:-}" ]] || build_args+=( # <- Syntax
--build-arg "CPU_FLAGS_${use_cpu_arch^^}=${use_cpu_flags_raw}"
)
[[ -z "${target_opt:-}" ]] || build_args+=( # <- Syntax
--build-arg TARGET_OPT="${target_opt}"
)
[[ -z "${rust_target_opt:-}" ]] || build_args+=( # <- Syntax
--build-arg RUST_TARGET_OPT="${rust_target_opt}"
)
declare cppflags='' cflags='' cxxflags='' fflags='' fcflags='' ldflags='' \
flflags='' rustflags=''
if type -pf emerge >/dev/null 2>&1; then
cppflags="$( # <- Syntax
emerge --info 2>&1 |
grep -- '^CPPFLAGS=' |
awk -F'"' '{print $2}' || :
)"
cflags="$( # <- Syntax
emerge --info 2>&1 |
grep -- '^CFLAGS=' |
awk -F'"' '{print $2}' || :
)"
cxxflags="$( # <- Syntax
emerge --info 2>&1 |
grep -- '^CXXFLAGS=' |
awk -F'"' '{print $2}' || :
)"
fflags="$( # <- Syntax
emerge --info 2>&1 |
grep -- '^FFLAGS=' |
awk -F'"' '{print $2}' || :
)"
fcflags="$( # <- Syntax
emerge --info 2>&1 |
grep -- '^FCFLAGS=' |
awk -F'"' '{print $2}' || :
)"
ldflags="$( # <- Syntax
emerge --info 2>&1 |
grep -- '^LDFLAGS=' |
awk -F'"' '{print $2}' || :
)"
flflags="$( # <- Syntax
emerge --info 2>&1 |
grep -- '^FLFLAGS=' |
awk -F'"' '{print $2}' || :
)"
rustflags="$( # <- Syntax
emerge --info 2>&1 |
grep -- '^RUSTFLAGS=' |
awk -F'"' '{print $2}' || :
)"
else
eval "$(
# shellcheck disable=SC2034
[[ -n "${LDFLAGS:-}" ]] || case "${arch}" in
amd64|ppc64|x86)
LDFLAGS='-Wl,-O1 -Wl,--as-needed -Wl,-z,pack-relative-relocs'
;;
*)
LDFLAGS='-Wl,-O1 -Wl,--as-needed'
;;
esac
[[ -n "${ARCH:-}" ]] || ARCH="${arch}"
. /etc/portage/make.conf 2>&1
set |
grep -F -- 'FLAGS=' |
sed 's|^[A-Z]\+=|\L&| ; s|$|;|' |
grep -E -- '^c(xx)?flags='
)"
fi
[[ -n "${cflags:-}" ]] || cflags='-O2 -pipe'
[[ -n "${cxxflags:-}" ]] || cxxflags="${cflags}"
[[ -n "${fflags:-}" ]] || fflags="${cflags}"
[[ -n "${fcflags:-}" ]] || fcflags="${cflags}"
if [[ -z "${ldflags:-}" ]]; then
case "${arch}" in
amd64|ppc64|x86)
ldflags='-Wl,-O1 -Wl,--as-needed -Wl,-z,pack-relative-relocs'
;;
*)
ldflags='-Wl,-O1 -Wl,--as-needed'
;;
esac
fi
[[ -n "${flflags:-}" ]] || flflags="${ldflags}"
build_args+=( # <- Syntax
--build-arg CPPFLAGS="${cppflags:-}"
--build-arg CFLAGS="${cflags:-}"
--build-arg CXXFLAGS="${cxxflags:-}"
--build-arg FFLAGS="${fflags:-}"
--build-arg FCFLAGS="${fcflags:-}"
--build-arg LDFLAGS="${ldflags:-}"
--build-arg FLFLAGS="${flflags:-}"
--build-arg RUSTFLAGS="${rustflags:-}"
)
unset cppflags cflags cxxflags fflags fcflags ldflags flflags rustflags
# shellcheck disable=SC2206
[[ -z "${debug:-}" ]] || build_args+=( # <- Syntax
--loglevel 3
${DEBUG_EXTRA_ARGS:-}
)
docker image build "${build_args[@]}" . 2>&1 |
tee ../log/"docker.${env_name#*"/"}.log"
output " -> ${?}"
unset build_args
[[ "${*:-}" = '--env' ]] && exit 0
env_id="$( docker image ls --noheading \
--format='{{.ID}}' \
"${env_name}:latest" 2>/dev/null
)"
output
output " ${blue}*${reset} Stage 'env' complete, next is 'stage3' ..."
output
fi
gentoo_stage3_image="$( # <- Syntax highlight failure
grep -- '^ARG gentoo_stage3_image=' Dockerfile.stage3 |
cut -d'"' -f 2
)"
declare ref=''
declare -i rc=0 need_pull=1
trap '' INT
# We have to 'pull' manually, or else this stage fails ...
if [[ "${gentoo_stage3_tag}" = 'latest' ]]; then
info "Using upstream '${gentoo_stage3_image}' tag" \
"'${gentoo_stage3_tag}'"
case "$( docker image pull --help )" in
*'--platform'*)
docker image pull "${gentoo_stage3_image}:latest" \
--platform "linux/${docker_arch}" || rc=${?}
;;
*'--override-arch'*)
docker image pull "${gentoo_stage3_image}:latest" \
--override-arch "${docker_arch}" || rc=${?}
;;
*'--arch'*)
docker image pull "${gentoo_stage3_image}:latest" \
--arch "${docker_arch}" || rc=${?}
;;
*)
die "Could not determine method to retrieve" \
"non-native architecture with" \
"'${_command} image pull'"
;;
esac
need_pull=0
elif [[ -n "${repo_path_srcshelton:-}" ]] &&
[[ -d "${repo_path_srcshelton}" ]]
then
if [[ -s "${repo_path_srcshelton}/.image_digests" ]]; then
ref="$( # <- Syntax
grep -m 1 "^${docker_arch} " \
"${repo_path_srcshelton}/.image_digests" |
cut -d' ' -f 2-
)"
if ! [[ "${ref%":"*}" == 'sha256' ]]; then
warn "Unable to retrieve SHA256 digest for image" \
"'${gentoo_stage3_image}:${gentoo_stage3_tag}' with" \
"with architecture '${docker_arch}' from file" \
"'${repo_path_srcshelton}/.image_digests'"
need_pull=1
else
info "Using '${gentoo_stage3_image}' pin '${ref}'"
# podman-4.6.x has strange behaviour where untagging an image
# also seems to lose the associated SHA256?
#
# See https://github.com/containers/podman/issues/19594
#
# We can work around this by untagging *before* pulling...
#
if [[ -n "$( # <- Syntax
docker image list \
-q \
--filter "reference=${gentoo_stage3_image}:${gentoo_stage3_tag}"
)" ]]
then
if ! docker image untag \
"${gentoo_stage3_image}:${gentoo_stage3_tag}"
then
docker --debug image untag \
"${gentoo_stage3_image}:${gentoo_stage3_tag}" ||
die "${_command} image untag failed to remove" \
"'${gentoo_stage3_image}:${gentoo_stage3_tag}' tag"
fi
fi
info "Pulling image '${gentoo_stage3_image}@${ref}' ..."
if ! docker image pull "${gentoo_stage3_image}@${ref}"; then
docker --debug image pull "${gentoo_stage3_image}@${ref}" ||
rc=${?}
else
info "Tagging '${ref}' as" \
"'${gentoo_stage3_image}:${gentoo_stage3_tag}'"
if ! docker image tag "${gentoo_stage3_image}@${ref}" \
"${gentoo_stage3_image}:${gentoo_stage3_tag}"
then
docker --debug image tag \
"${gentoo_stage3_image}@${ref}" \
"${gentoo_stage3_image}:${gentoo_stage3_tag}" || :
warn "Pull of pinned image failed - reverting to tag" \
"'${gentoo_stage3_tag}'"
need_pull=1
else
rc=0
need_pull=0
fi
fi
fi
fi
fi
if (( need_pull )); then
warn "Pulling image '${gentoo_stage3_image}:${gentoo_stage3_tag}' ..."
if ! docker image pull "${gentoo_stage3_image}:${gentoo_stage3_tag}"; then
rc=${?}
docker --debug image pull "${gentoo_stage3_image}:${gentoo_stage3_tag}"
fi
fi
unset need_pull
trap - INT
if (( rc )); then
die "'${_command} image pull' failed to retrieve image" \
"'${gentoo_stage3_image}:${gentoo_stage3_tag}' for architecture" \
"'${docker_arch}'"
fi
unset rc
if [[ -n "${ref:-}" ]]; then
# 'podman image ls' won't show images by SHA256 reference...
#gentoo_stage3_id="$( docker image ls --noheading \
# --format='{{.ID}}' \
# "${gentoo_stage3_image}@${ref}" 2>/dev/null
#)" || :
gentoo_stage3_id="$( docker image ls --noheading --digests \
--format='{{.Digest}} {{.ID}}' 2>/dev/null |
grep -F -- "${ref}" |
awk '{ print $2 }'
)" || :
else
gentoo_stage3_id="$( docker image ls --noheading \
--format='{{.ID}}' \
"${gentoo_stage3_image}:${gentoo_stage3_tag}" 2>/dev/null
)" || :
fi
#unset ref
if [[ -z "${*:-}" ]] || grep -Fq -- ' --stage3 ' <<<" ${*} "; then
if [[ -z "${env_id:-}" ]]; then
die "Environment stage not found - please run" \
"'$( basename "${0}" ) --env'"
fi
output
output " ${blue}*${reset} Creating image '${stage3_name}:latest'" \
"${debug:+"from image ID '${gentoo_stage3_id}' "}..."
output