-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdistro2gentoo.sh
executable file
·2037 lines (1810 loc) · 56.8 KB
/
distro2gentoo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# Author: Ryan Tsien @cwittlut <i@bitbili.net>
# License: GPL-2
#
set -e
set -o pipefail
export LC_ALL=C
[[ $(bash --version | head -1) =~ version[[:space:]]([[:digit:]]+)\.([[:digit:]]+)\.[[:digit:]]+\([[:digit:]]+\) ]] || true
BASH_VER_MAJOR="${BASH_REMATCH[1]}"
BASH_VER_MINOR="${BASH_REMATCH[2]}"
if (( BASH_VER_MAJOR < 5 )) && (( BASH_VER_MINOR < 4 )); then
echo "Please update the bash version to at least 4.4"
exit 1
fi
BINHOST_ENABLED=1
EFI_ENABLED=0
NECESSARY_CMDS=()
MIRROR=""
STAGE3_TARBALL=""
D2G_GRUB_CMDLINE_LINUX=''
D2G_GRUB_CMDLINE_LINUX_DEFAULT=''
D2G_UNUNIFIED_GRUB_CMDLINE_LINUX=''
D2G_UNUNIFIED_GRUB_CMDLINE_LINUX_DEFAULT=''
EFIMNT=""
GRUB_INSTALLED=0
GRUB_CONFIGURED=0
CPUARCH="$(uname -m)"
CPUARCH="${CPUARCH/x86_64/amd64}"
CPUARCH="${CPUARCH/aarch64/arm64}"
NEWROOT="/root.d2g.${CPUARCH}"
TMP_DIR="$(mktemp -t "distro2gentoo.XXXXXXXX" -d)"
trap '
rm -rf "$TMP_DIR"
umount -R "$NEWROOT"/* &>/dev/null || true
' EXIT
[[ -z ${_BASHFUNC_DO} ]] || return 0
_BASHFUNC_DO=1
if [[ -z ${_BASHFUNC_DO_OFD} ]]; then
eval "exec {_BASHFUNC_DO_OFD}>&1"
fi
: "${_BASHFUNC_DO_DATE:=on}"
: "${_BASHFUNC_DO_DATE_FORMAT:=+[%Y-%m-%d %H:%M:%S] }"
_do() {
local prefix
if [[ ${1} == "-p" ]]; then
shift
prefix="${1}"
shift
fi
[[ -n ${1} ]] || return 0
local msg='\x1b[1m\x1b[32m'
if [[ ${_BASHFUNC_DO_DATE} == "on" ]]; then
msg+=$(date "${_BASHFUNC_DO_DATE_FORMAT}")
fi
msg+="${prefix}${prefix:+ }"
msg+='>>>\x1b[0m '
msg+="$*"
eval ">&${_BASHFUNC_DO_OFD} echo -e \${msg}"
"${@}"
}
[[ -z ${_BASHFUNC_LOG} ]] || return 0
_BASHFUNC_LOG=1
: "${_BASHFUNC_LOG_LEVEL:=info}"
: "${_BASHFUNC_LOG_OUT_FD:=1}"
: "${_BASHFUNC_LOG_ERR_FD:=2}"
: "${_BASHFUNC_LOG_DATE:=on}"
: "${_BASHFUNC_LOG_DATE_FORMAT:=+[%Y-%m-%d %H:%M:%S] }"
: "${_BASHFUNC_LOG_ECHO_FUNC:=echo}"
declare -A _BASHFUNC_LOG_LEVEL_INTERNAL=(
[debug]=3
[info]=2
[normal]=1
[warning]=0
[error]=-1
)
_log() {
: "${_BASHFUNC_LAST_EXIT:=$?}" # this should always at the first line of this function
[[ -n ${2} ]] || return 0
local lv=${_BASHFUNC_LOG_LEVEL_INTERNAL[${_BASHFUNC_LOG_LEVEL}]}
local out='' fatal='' endfunc='' msg="" is_normal=0
case ${1} in
d)
if [[ ${lv} -lt 3 ]]; then
return 0
fi
out=${_BASHFUNC_LOG_OUT_FD}
msg='\x1b[1m'
;;
i)
if [[ ${lv} -lt 2 ]]; then
return 0
fi
out=${_BASHFUNC_LOG_OUT_FD}
;;
n)
if [[ ${lv} -lt 1 ]]; then
return 0
fi
out=${_BASHFUNC_LOG_OUT_FD}
msg='\x1b[36m'
is_normal=1
;;
w)
if [[ ${lv} -lt 0 ]]; then
return 0
fi
out=${_BASHFUNC_LOG_ERR_FD}
msg='\x1b[1m\x1b[33m'
;;
e)
out=${_BASHFUNC_LOG_ERR_FD}
msg='\x1b[1m\x1b[31m'
;;
ee)
out=${_BASHFUNC_LOG_ERR_FD}
msg='\x1b[1m\x1b[31m'
fatal=${1}
if declare -F "${2}" &>/dev/null; then
endfunc=${2}
shift
fi
;;
*)
echo "internal function error: _log, unexpected argument '$1'" >&2
return 1
;;
esac
shift
if [[ ${_BASHFUNC_LOG_DATE} == "on" ]] && [[ $is_normal == 0 ]]; then
msg+=$(date "${_BASHFUNC_LOG_DATE_FORMAT}")
fi
msg+="$*"
msg+='\x1b[0m'
eval ">&${out} ${_BASHFUNC_LOG_ECHO_FUNC} -e \"\${msg}\""
if [[ ${fatal} == "ee" ]]; then
if [[ ${_BASHFUNC_LAST_EXIT} == 0 ]]; then
_BASHFUNC_LAST_EXIT=1
fi
${endfunc}
exit "${_BASHFUNC_LAST_EXIT}"
fi
}
printHelp() {
echo "
Usage: distro2gentoo [<options>]
options:
--disable-binhost Disable the binhost when installing Gentoo
-h, --help Show this help
"
exit "${1:-1}"
}
_gpg() {
if [[ ! -d "${TMP_DIR}/gnupg_home" ]]; then
_do mkdir -p "${TMP_DIR}/gnupg_home"
fi
export GNUPGHOME="${TMP_DIR}/gnupg_home"
_do gpg --homedir "${TMP_DIR}/gnupg_home" "$@"
}
gpgDecryptAndOutput() {
# $1: input file
# $2: output file
_log i "Decrypting file '$1' to '$2' ..."
_gpg -o "$2" --decrypt "$1"
}
gpgVerifyDetachedFile() {
# $1: input file
# $2: input signature file
_log i "Verifying file '$1' with signature file '$2' ..."
_gpg --verify "$2" "$1"
}
firstTip() {
echo
_log n " 1. This script won't format disks,"
_log n " 2. will remove all mounted data excepts /home, /root,"
_log n " kernels & modules"
echo
_log n " *** BACKUP YOUR DATA!!! ***"
echo
_log n " To avoid rebooting without privileges, make sure at least one interactive shell with root privileges is active."
echo
WAIT=5
echo -en "Starting in: \e[33m\e[1m"
while [[ ${WAIT} -gt 0 ]]; do
echo -en "${WAIT} "
WAIT=$((WAIT - 1))
sleep 1
done
echo -e "\e[0m"
}
parseParams() {
set +e
unset GETOPT_COMPATIBLE
getopt -T
if [[ ${?} != 4 ]]; then
_log ee "The Linux version of command 'getopt' is necessory to parse parameters."
fi
local _args
if ! _args=$(getopt -o 'h' -l 'disable-binhost,help' -n 'distro2gentoo' -- "$@"); then
printHelp 1
fi
set -e
# parse arguments
eval "set -- ${_args}"
while true; do
case "${1}" in
--disable-binhost)
BINHOST_ENABLED=0
shift
;;
-h|--help)
printHelp 0
;;
--)
shift
break
;;
*)
_log ee printHelp "unknow error"
;;
esac
done
}
_D2G_DOWNLOAD_CMD=("curl" "--retry" "3" "--connect-timeout" "60" "-Lfo")
_D2G_DOWNLOAD_CMD_RESUME=("curl" "-C" "-" "--retry" "3" "--connect-timeout" "60" "-Lfo")
_D2G_DOWNLOAD_CMD_QUIET=("curl" "--retry" "1" "--connect-timeout" "60" "-sfL")
if command -v curl >/dev/null; then
:
elif command -v wget >/dev/null; then
_D2G_DOWNLOAD_CMD=("wget" "-t" "3" "-T" "60" "-O")
_D2G_DOWNLOAD_CMD_RESUME=("wget" "-c" "-t" "3" "-T" "60" "-O")
_D2G_DOWNLOAD_CMD_QUIET=("wget" "-t" "1" "-T" "60" "-qO" "-")
else
NECESSARY_CMDS+=("curl")
fi
downloadCat() {
if [[ $1 == '-H' ]]; then
local header="$2"
shift 2
fi
_do "${_D2G_DOWNLOAD_CMD_QUIET[@]}" ${header:+--header} "${header}" "$1"
}
download() {
# $1: remote url
# $2: output file
local isForce=0 _done=0
if [[ ${FUNCNAME[1]} == "downloadForce" ]]; then
isForce=1
fi
if [[ $isForce == 0 ]] && [[ -f "$2" ]]; then
_log i "Resuming '$2' (url: '$1') ..."
if _do "${_D2G_DOWNLOAD_CMD_RESUME[@]}" "$2" "$1"; then
_done=1
fi
fi
if [[ $_done == 0 ]]; then
_log i "Downloading '$1' ..."
_do "${_D2G_DOWNLOAD_CMD[@]}" "$2" "$1"
fi
}
downloadForce() {
download "$@"
}
NECESSARY_CMDS+=("awk" "bc" "findmnt" "git" "gpg" "ip" "openssl" "sort" "tr" "wc" "xmllint" "xz")
declare -A -g _D2G_PKG_awk
_D2G_PKG_awk[apt]="gawk"
_D2G_PKG_awk[dnf]="gawk"
_D2G_PKG_awk[opkg]="gawk"
_D2G_PKG_awk[pacman]="gawk"
_D2G_PKG_awk[urpmi]="gawk"
_D2G_PKG_awk[yum]="gawk"
_D2G_PKG_awk[zypper]="gawk"
_D2G_PKG_awk[xbps-install]="gawk"
declare -A -g _D2G_PKG_bc
_D2G_PKG_bc[apt]="bc"
_D2G_PKG_bc[dnf]="bc"
_D2G_PKG_bc[opkg]="bc"
_D2G_PKG_bc[pacman]="bc"
_D2G_PKG_bc[urpmi]="bc"
_D2G_PKG_bc[yum]="bc"
_D2G_PKG_bc[zypper]="bc"
_D2G_PKG_bc[xbps-install]="bc"
declare -A -g _D2G_PKG_efibootmgr
_D2G_PKG_efibootmgr[apt]="efibootmgr"
_D2G_PKG_efibootmgr[dnf]="efibootmgr"
_D2G_PKG_efibootmgr[opkg]="efibootmgr"
_D2G_PKG_efibootmgr[pacman]="efibootmgr"
_D2G_PKG_efibootmgr[urpmi]="efibootmgr"
_D2G_PKG_efibootmgr[yum]="efibootmgr"
_D2G_PKG_efibootmgr[zypper]="efibootmgr"
_D2G_PKG_efibootmgr[xbps-install]="efibootmgr"
declare -A -g _D2G_PKG_findmnt
_D2G_PKG_findmnt[apt]="util-linux"
_D2G_PKG_findmnt[dnf]="util-linux"
_D2G_PKG_findmnt[opkg]="util-linux"
_D2G_PKG_findmnt[pacman]="util-linux"
_D2G_PKG_findmnt[urpmi]="util-linux"
_D2G_PKG_findmnt[yum]="util-linux"
_D2G_PKG_findmnt[zypper]="util-linux"
_D2G_PKG_findmnt[xbps-install]="util-linux"
declare -A -g _D2G_PKG_git
_D2G_PKG_git[apt]="git"
_D2G_PKG_git[dnf]="git"
_D2G_PKG_git[opkg]="git"
_D2G_PKG_git[pacman]="git"
_D2G_PKG_git[urpmi]="git"
_D2G_PKG_git[yum]="git"
_D2G_PKG_git[zypper]="git"
_D2G_PKG_git[xbps-install]="git"
declare -A -g _D2G_PKG_gpg
_D2G_PKG_gpg[apt]="gnupg"
_D2G_PKG_gpg[dnf]="gnupg2"
_D2G_PKG_gpg[opkg]="gnupg"
_D2G_PKG_gpg[pacman]="gnupg"
_D2G_PKG_gpg[urpmi]="gnupg2"
_D2G_PKG_gpg[yum]="gnupg2"
_D2G_PKG_gpg[zypper]="gpg2"
_D2G_PKG_gpg[xbps-install]="gnupg"
declare -A -g _D2G_PKG_ip
_D2G_PKG_ip[apt]="iproute2"
_D2G_PKG_ip[dnf]="iproute"
_D2G_PKG_ip[opkg]="ip"
_D2G_PKG_ip[pacman]="iproute2"
_D2G_PKG_ip[urpmi]="iproute2"
_D2G_PKG_ip[yum]="iproute"
_D2G_PKG_ip[zypper]="iproute2"
_D2G_PKG_ip[xbps-install]="iproute2"
declare -A -g _D2G_PKG_openssl
_D2G_PKG_openssl[apt]="openssl"
_D2G_PKG_openssl[dnf]="openssl"
_D2G_PKG_openssl[opkg]="openssl"
_D2G_PKG_openssl[pacman]="openssl"
_D2G_PKG_openssl[urpmi]="openssl"
_D2G_PKG_openssl[yum]="openssl"
_D2G_PKG_openssl[zypper]="openssl"
_D2G_PKG_openssl[xbps-install]="openssl"
declare -A -g _D2G_PKG_sort
_D2G_PKG_sort[apt]="coreutils"
_D2G_PKG_sort[dnf]="coreutils"
_D2G_PKG_sort[opkg]="coreutils"
_D2G_PKG_sort[pacman]="coreutils"
_D2G_PKG_sort[urpmi]="coreutils"
_D2G_PKG_sort[yum]="coreutils"
_D2G_PKG_sort[zypper]="coreutils"
_D2G_PKG_sort[xbps-install]="coreutils"
declare -A -g _D2G_PKG_tr
_D2G_PKG_tr[apt]="coreutils"
_D2G_PKG_tr[dnf]="coreutils"
_D2G_PKG_tr[opkg]="coreutils"
_D2G_PKG_tr[pacman]="coreutils"
_D2G_PKG_tr[urpmi]="coreutils"
_D2G_PKG_tr[yum]="coreutils"
_D2G_PKG_tr[zypper]="coreutils"
_D2G_PKG_tr[xbps-install]="coreutils"
declare -A -g _D2G_PKG_wc
_D2G_PKG_wc[apt]="coreutils"
_D2G_PKG_wc[dnf]="coreutils"
_D2G_PKG_wc[opkg]="coreutils"
_D2G_PKG_wc[pacman]="coreutils"
_D2G_PKG_wc[urpmi]="coreutils"
_D2G_PKG_wc[yum]="coreutils"
_D2G_PKG_wc[zypper]="coreutils"
_D2G_PKG_wc[xbps-install]="coreutils"
declare -A -g _D2G_PKG_xmllint
_D2G_PKG_xmllint[apt]="libxml2-utils"
_D2G_PKG_xmllint[dnf]="libxml2"
_D2G_PKG_xmllint[pacman]="libxml2"
_D2G_PKG_xmllint[zypper]="libxml2-tools"
_D2G_PKG_xmllint[urpmi]="lib64xml2"
_D2G_PKG_xmllint[opkg]="libxml2"
_D2G_PKG_xmllint[xbps-install]="libxml2"
declare -A -g _D2G_PKG_curl
_D2G_PKG_curl[apt]="curl"
_D2G_PKG_curl[dnf]="curl"
_D2G_PKG_curl[pacman]="curl"
_D2G_PKG_curl[zypper]="curl"
_D2G_PKG_curl[urpmi]="curl"
_D2G_PKG_curl[opkg]="curl"
_D2G_PKG_curl[xbps-install]="curl"
declare -A -g _D2G_PKG_xz
_D2G_PKG_xz[apt]="xz-utils"
_D2G_PKG_xz[dnf]="xz"
_D2G_PKG_xz[pacman]="xz"
_D2G_PKG_xz[zypper]="xz"
_D2G_PKG_xz[urpmi]="xz"
_D2G_PKG_xz[opkg]="xz"
_D2G_PKG_xz[xbps-install]="xz"
declare -A -g _D2G_PKG_cacerts
_D2G_PKG_cacerts[apt]="ca-certificates"
_D2G_PKG_cacerts[dnf]="ca-certificates"
_D2G_PKG_cacerts[pacman]="ca-certificates"
_D2G_PKG_cacerts[zypper]="ca-certificates"
_D2G_PKG_cacerts[urpmi]="rootcerts"
_D2G_PKG_cacerts[opkg]="ca-certificates"
_D2G_PKG_cacerts[xbps-install]="ca-certificates"
_D2G_PKG_DB_Updated=0
_install_pkg() {
local -i ret=0
local cmd_key="$1"
if command -v apt >/dev/null; then
if (( _D2G_PKG_DB_Updated == 0 )); then
apt-get update
_D2G_PKG_DB_Updated=1
fi
eval "apt -y install \${_D2G_PKG_${cmd_key}[apt]}" || ret=$?
elif command -v dnf >/dev/null; then
eval "dnf -y install \${_D2G_PKG_${cmd_key}[dnf]}" || ret=$?
elif command -v yum >/dev/null; then
eval "yum -y install \${_D2G_PKG_${cmd_key}[yum]}" || ret=$?
elif command -v pacman >/dev/null; then
if (( _D2G_PKG_DB_Updated == 0 )); then
pacman -Syy
_D2G_PKG_DB_Updated=1
fi
eval "pacman --noconfirm -S \${_D2G_PKG_${cmd_key}[pacman]}" || ret=$?
elif command -v zypper >/dev/null; then # SUSE
eval "zypper install -y \${_D2G_PKG_${cmd_key}[zypper]}" || ret=$?
elif command -v urpmi >/dev/null; then # Mageia
eval "urpmi --force \${_D2G_PKG_${cmd_key}[urpmi]}" || ret=$?
elif command -v opkg >/dev/null; then # OpenWRT
eval "opkg install \${_D2G_PKG_${cmd_key}[opkg]}" || ret=$?
elif command -v xbps-install >/dev/null; then # Void Linux
eval "xbps-install -y \${_D2G_PKG_${cmd_key}[xbps-install]}" || ret=$?
else
ret=1
fi
return $ret
}
_installDeps() {
_log i "Updating ca-certificates ..."
_install_pkg cacerts || true
_log i "Make sure commands '${NECESSARY_CMDS[*]}' are available."
for cmd_key in "${NECESSARY_CMDS[@]}"; do
if ! command -v "${cmd_key}" >/dev/null; then
if ! _install_pkg "${cmd_key}"; then
_fatal "Command '${cmd_key}' was not found and the installation failed!"
fi
fi
done
}
_installGPGKeys() {
# https://www.gentoo.org/downloads/signatures/
download "https://qa-reports.gentoo.org/output/service-keys.gpg" "${TMP_DIR}/service-keys.gpg"
_gpg --import "${TMP_DIR}/service-keys.gpg"
}
prerequisitesCheck() {
local res=000
# check root
[[ ${EUID} == 0 ]] || res=1${res:1}
# check cpu arch (only tested for amd64/arm64 now)
[[ ${CPUARCH} =~ ^amd64|arm64$ ]] || res=${res:0:1}1${res:2}
# check newroot dir
[[ -L ${NEWROOT} || -e ${NEWROOT} ]] && res=${res:0:2}1 || true
case ${res} in
1*)
_log e "Please run this shell as the root user!"
;;&
?1?)
_log e "This script only tested for amd64/arm64 arch now!"
;;&
*1)
_log e "New root path '${NEWROOT}' exists"
_log e "**umount** it's subdirs and remove it first."
_log e " # umount -R ${NEWROOT}/*"
_log e " # rm -r ${NEWROOT}"
;;
esac
if [[ ${res} != 000 ]]; then
_log ee "Abort it!"
fi
# check bios/efi
if [[ -e /sys/firmware/efi ]]; then
EFI_ENABLED=1
NECESSARY_CMDS+=( "efibootmgr" )
fi
# check iSCSI
# TODO
# check filesystem
# TODO
}
prerequisitesSolve() {
# installing necessary pkgs
_installDeps
# prepare GnuPG Keys
_installGPGKeys
}
_D2G_GIT_MIRROR="https://github.com/gentoo-mirror/gentoo.git"
_uris_from_xml() {
local xmllint_cmd_fmt="xmllint --xpath '/mirrors/mirrorgroup[@country=\"@@COUNTRYCODE@@\"]/mirror/uri'"
local old_xmllint=1 xmllint_cmd uris
if [[ $(xmllint --version 2>&1 | head -1 | cut -d' ' -f5 | cut -d'-' -f1) -ge 20909 ]]; then
xmllint_cmd_fmt="${xmllint_cmd_fmt%\'}/text()'"
unset old_xmllint
fi
xmllint_cmd="${xmllint_cmd_fmt/@@COUNTRYCODE@@/$1}"
uris=""
if uris=$(eval "$xmllint_cmd" "$2"); then
if [[ -n $old_xmllint ]]; then
uris="${uris//<\/uri>/$'\n'}"
uris="$(echo "$uris" | cut -d'>' -f2)"
fi
# remove rsync uris
uris="$(echo -n "$uris" | sed -E '/^rsync/d')"
fi
echo -n "$uris"
}
setupMirror() {
_log i "Getting mirror list..."
set +e
[[ $(downloadCat -H 'I-Agree-To-Use-Only-For-The-Distro2Gentoo-Script: True' 'https://ip7.d0a.io/self') =~ \"IsoCode\":\"([[:upper:]]{2})\" ]]
local country_code=${BASH_REMATCH[1]}
local mirrors
: "${country_code:=CN}"
local distfiles_xml="${TMP_DIR}/distfiles.xml"
download "https://api.gentoo.org/mirrors/distfiles.xml" "$distfiles_xml"
mirrors="$(_uris_from_xml "$country_code" "$distfiles_xml")"
if [[ $mirrors == "" ]]; then
country_code="CN" # fallback to CN
mirrors="$(_uris_from_xml "$country_code" "$distfiles_xml")"
fi
set -e
if [[ $country_code == "CN" ]]; then
_D2G_GIT_MIRROR="https://mirrors.bfsu.edu.cn/git/gentoo-portage.git"
fi
local uri selected
local -a mirrors_arr
mapfile -t mirrors_arr <<<"$mirrors"
_log i "Setting mirror url ..."
_log n "mirror list in ${country_code}:"
for (( i = 0; i < ${#mirrors_arr[@]}; ++i )); do
uri=${mirrors_arr[i]}
if [[ "${uri}" =~ ^https ]] && [[ -z ${MIRROR} ]]; then
MIRROR="${uri}"
_log n " -*-[${i}] ${uri}"
else
_log n " [${i}] ${uri}"
fi
done
mirrors_arr+=( "CUSTOM" )
_log n " [${i}] <Enter custom URL>"
while [[ ${#mirrors_arr[@]} -gt 0 ]]; do
read -r -p "Choose prefered mirror (enter the num, empty for default): " selected
[[ -n ${selected} ]] || break
if [[ ${selected} =~ ^[[:digit:]]+$ ]] && [[ -n ${mirrors_arr[${selected}]} ]]; then
MIRROR=${mirrors_arr[${selected}]}
break;
else
_log w "out of range!"
fi
done
if [[ ${MIRROR} == CUSTOM ]]; then
_set_custom_mirror() {
read -r -p "Enter your custom URL: " MIRROR
if [[ ! ${MIRROR} =~ ^(http[s]?|ftp):// ]]; then
_log e "Please use http[s] or ftp URL."
_set_custom_mirror
fi
}
_set_custom_mirror
fi
if [[ -z ${MIRROR} ]]; then
MIRROR="https://gentoo.osuosl.org/"
fi
_log i "Mirror has been set to '${MIRROR}'."
}
getStage3() {
_log i "Getting stage3 tarball ..."
local stage3_list_url="${MIRROR%/}/releases/${CPUARCH}/autobuilds/latest-stage3.txt"
local local_stage3_list="${TMP_DIR}/latest-stage3.txt"
local relative_path stage3_size selected_stage3 selected_stage3_tmp
local -a stage3s_names stage3s_paths stage3s_sizes
_log i "Getting stage3 tarball list ..."
download "$stage3_list_url" "${local_stage3_list}.with-gpg-signature"
gpgDecryptAndOutput "${local_stage3_list}.with-gpg-signature" "$local_stage3_list"
while read -r relative_path stage3_size _; do
if [[ $relative_path =~ ^[[:space:]]*#|^[[:space:]]*$ ]]; then
continue
fi
stage3s_names+=( "${relative_path##*/}" )
stage3s_paths+=( "$relative_path" )
stage3s_sizes+=( "$stage3_size" )
done <"$local_stage3_list"
_log n "stage3 list:"
for (( i=0; i < ${#stage3s_names[@]}; ++i )); do
local _stage=${stage3s_names[i]}
if [[ ! ${_stage} =~ ^stage3 ]]; then
# don't unset these until the problem of index 'i' solved
#unset 'stage3s_names[i]' 'stage3s_paths[i]' 'stage3s_sizes[i]'
continue
fi
# default to openrc
if [[ ${_stage} =~ ^stage3-amd64-openrc-2|stage3-arm64-openrc-2 ]]; then
selected_stage3=${i}
_log n " -*- [${i}] ${_stage}"
else
_log n " [${i}] ${_stage}"
fi
done
if (( ${#stage3s_names[@]} < 1 )); then
_log ee "No stage3 list, please check the mirror URL or your network."
fi
while :; do
read -r -p "Choose prefered stage3 tarball (enter the num, empty for the default): " selected_stage3_tmp
if [[ -z ${selected_stage3_tmp} ]]; then
if [[ $selected_stage3 == "" || -z $selected_stage3 ]]; then
_log e "No default stage3 selected, please select one!"
else
_log i "Use the default stage3."
break
fi
fi
if [[ ${selected_stage3_tmp} =~ ^[[:digit:]]+$ ]] && [[ -n ${stage3s_names[${selected_stage3_tmp}]} ]]; then
selected_stage3=${selected_stage3_tmp}
break;
else
_log e "Out of range, please select a valid one!"
fi
done
_log i "selected stage3: ${stage3s_names[${selected_stage3}]}"
# prepare signature
local stage3_asc="/${stage3s_names[${selected_stage3}]}.asc"
downloadForce "${stage3_list_url%/*}/${stage3s_paths[${selected_stage3}]}.asc" "$stage3_asc"
# prepare stage3 tarball
STAGE3_TARBALL="/${stage3s_names[${selected_stage3}]}"
local stage3_tarball_exists
if [[ -f $STAGE3_TARBALL ]]; then
if gpgVerifyDetachedFile "$STAGE3_TARBALL" "$stage3_asc"; then
stage3_tarball_exists=1
fi
fi
if [[ $stage3_tarball_exists != 1 ]]; then
download "${stage3_list_url%/*}/${stage3s_paths[${selected_stage3}]}" "$STAGE3_TARBALL"
# verify stage3 tarball and download again if doesn't match
local tries=1
while ! gpgVerifyDetachedFile "$STAGE3_TARBALL" "$stage3_asc"; do
downloadForce "${stage3_list_url%/*}/${stage3s_paths[${selected_stage3}]}" "$STAGE3_TARBALL"
tries=$((tries + 1))
if (( tries > 3 )); then
_log ee "The downloaded stage3 tarball '$STAGE3_TARBALL' doesn't match its asc file, abort!"
fi
done
fi
_log i "Stage3 tarball has been stored as '$STAGE3_TARBALL'."
}
unpackStage3ToNEWROOT() {
pushd "$NEWROOT" || _log ee "Pushd '$NEWROOT' failed!"
_log w "Untaring stage3 tarball ..."
_log w ">>> tar xpf \"$STAGE3_TARBALL\" --xattrs-include='*.*' --numeric-owner"
tar xpf "$STAGE3_TARBALL" --xattrs-include='*.*' --numeric-owner
popd || _log ee "Popd from '$NEWROOT' failed!"
}
declare -a _D2G_FS_SOURCES _D2G_FS_MOUNTPOINTS _D2G_FS_TYPES _D2G_FS_OPTS
_D2G_LVM_ENABLED=0
declare -a _D2G_LVM_LVS _D2G_LVM_LVS_MOUNTPOINTS
_D2G_LUKS_ENABLED=0
declare -a _D2G_LUKS_PARTS _D2G_LUKS_PARTS_MOUNTPOINTS _D2G_LUKS_PARTS_PARENTS
_D2G_BTRFS_ENABLED=0
_D2G_BTRFS_SUBVOL_ROOTFS=""
declare -a _D2G_BTRFS_MOUNTPOINTS _D2G_BTRFS_SUBVOLS _D2G_BTRFS_OPTS
_analyze_mnt() {
local _name _source _mp __mp _type _opts
while read -r _source _mp _type _opts _; do
_D2G_FS_SOURCES+=("${_source}")
_D2G_FS_MOUNTPOINTS+=("${_mp}")
_D2G_FS_TYPES+=("${_type}")
_D2G_FS_OPTS+=("${_opts}")
done <<<"$(findmnt --noheadings -loSOURCE,TARGET,FSTYPE,OPTIONS)"
local _sys_path_pattern='^(/|\[SWAP\])'
while read -r _name _type _mp; do
case "$_type" in
crypt)
while read -r __mp; do
if [[ ${__mp} =~ ${_sys_path_pattern} ]]; then
_D2G_LUKS_ENABLED=1
_D2G_LUKS_PARTS+=("${_name}")
_D2G_LUKS_PARTS_MOUNTPOINTS+=("${__mp}")
_D2G_LUKS_PARTS_PARENTS+=("$(lsblk -tpo NAME | grep -B1 "${_name}" | head -1 | cut -d'-' -f2)")
break
fi
done <<<"$(lsblk -lnoMOUNTPOINTS "${_name}")"
;;
lvm)
if [[ ${_mp} =~ ${_sys_path_pattern} ]]; then
_D2G_LVM_ENABLED=1
_D2G_LVM_LVS+=("${_name}")
_D2G_LVM_LVS_MOUNTPOINTS+=("${_mp}")
fi
;;
*)
:
;;
esac
done <<<"$(lsblk -lpnoNAME,TYPE,MOUNTPOINT)"
local -i i
local __subvol
for (( i = 0; i < ${#_D2G_FS_TYPES[@]}; ++i )); do
if [[ ${_D2G_FS_TYPES[i]} == btrfs ]]; then
if [[ ${_D2G_FS_MOUNTPOINTS[i]} =~ ^/ ]]; then
_D2G_BTRFS_ENABLED=1
_D2G_BTRFS_MOUNTPOINTS+=("${_D2G_FS_MOUNTPOINTS[i]}")
__subvol="$(<<<"${_D2G_FS_SOURCES[i]}" sed -nE 's/^[^[]+\[([^]]+)\]/\1/p')"
_D2G_BTRFS_SUBVOLS+=("${__subvol}")
_D2G_BTRFS_OPTS+=("${_D2G_FS_OPTS[i]}")
if [[ ${_D2G_FS_MOUNTPOINTS[i]} == / ]]; then
_D2G_BTRFS_SUBVOL_ROOTFS="${__subvol}"
fi
fi
fi
done
}
prepareBtrfsStuffs() {
# detect btrfs readonly subvol
while read -r _ _ _ _ _ _ _ _ __subvol_path; do
for (( i = 0; i < ${#_D2G_BTRFS_SUBVOLS[@]}; ++i )); do
__subvol_path=${__subvol_path#<FS_TREE>}
__subvol_path_remaining=${__subvol_path#"${_D2G_BTRFS_SUBVOLS[i]}"}
if [[ ${__subvol_path_remaining} != "${__subvol_path}" ]]; then
__subvol_path_readonlys+=("${_D2G_BTRFS_MOUNTPOINTS[i]}${__subvol_path_remaining}")
fi
done
done <<<"$(btrfs subvolume list -ar /)"
for __subvol_path_readonly in "${__subvol_path_readonlys[@]}"; do
_log w "'${__subvol_path_readonly}' is a readonly subvolume."
_D2G_EXCLUDED_READONLY_SUBVOL_PATH_OPTS+=" -and ! -regex '${__subvol_path_readonly}.*'"
done
# tune btrfs rootfs opts in /etc/fstab
if [[ -n ${_D2G_BTRFS_SUBVOL_ROOTFS} ]]; then
__btrfs_fstab_rootfs_subvol_sed_pattern="/subvol/!s/^([[:space:]]*[^#][^[:space:]]+[[:space:]]+\/[[:space:]]+btrfs[[:space:]]+[^[:space:]]+)[[:space:]]+([[:digit:]].+)$/\1,subvol=${_D2G_BTRFS_SUBVOL_ROOTFS//\//\\\/} \2/"
_log i ">>> sed -Ei '${__btrfs_fstab_rootfs_subvol_sed_pattern}' ${NEWROOT}/etc/fstab"
eval "sed -Ei '${__btrfs_fstab_rootfs_subvol_sed_pattern}' ${NEWROOT}/etc/fstab"
# keep the contents within the root subvol but with different path
# TODO, should be imporved
#for (( i = 0; i < ${#_D2G_BTRFS_SUBVOLS[@]}; ++i )); do
# if [[ ${_D2G_BTRFS_SUBVOL_ROOTFS} == "${_D2G_BTRFS_SUBVOLS[i]}" ]]; then
# continue
# fi
# __btrfs_subvol_rootfs_remaining=${_D2G_BTRFS_SUBVOL_ROOTFS#"${_D2G_BTRFS_SUBVOLS[i]}"}
# if [[ ${__btrfs_subvol_rootfs_remaining} != "${_D2G_BTRFS_SUBVOL_ROOTFS}" ]]; then
# _D2G_EXCLUDED_ROOTFS_SUBVOL_PATH_OPTS=" -and ! -regex '${_D2G_BTRFS_MOUNTPOINTS[i]}${__btrfs_subvol_rootfs_remaining}.*'"
# fi
#done
fi
}
prepareChroot() {
_log i "mounting necessaries ..."
_do mount -t proc /proc "${NEWROOT}/proc"
_do mount --rbind /sys "${NEWROOT}/sys"
_do mount --make-rslave "${NEWROOT}/sys"
_do mount --rbind /dev "${NEWROOT}/dev"
_do mount --make-rslave "${NEWROOT}/dev"
_do mount -t tmpfs -o nosuid,nodev,mode=0755 run "${NEWROOT}/run"
_log i "copying necessaries ..."
_do cp -aL /etc/fstab "${NEWROOT}/etc/"
_do cp -aL /etc/resolv.conf "${NEWROOT}/etc/" || true
_do cp -aL /etc/hosts "${NEWROOT}/etc/" || true
_do cp -aL /etc/hostname "${NEWROOT}/etc/" || \
echo "gentoo" > "${NEWROOT}/etc/hostname"
_do cp -aL /lib/modules "${NEWROOT}/lib/" || true
_analyze_mnt
mount /boot || true
local rootshadow _newpass _newday
rootshadow=$(grep -E '^root:' /etc/shadow)
if [[ $rootshadow =~ ^root:\*: ]]; then
_log i "setting root password to 'distro2gentoo' ..."
_newpass=$(openssl passwd -1 distro2gentoo)
_newday=$(echo "$(date +%s)/24/60/60" | bc)
else
_log i "backuping root password ..."
_newpass=$(echo "$rootshadow" | cut -d':' -f2)
_newday=$(echo "$rootshadow" | cut -d':' -f3)
fi
eval "sed -Ei '/root:\*:.*/s@root:\*:[[:digit:]]+:@root:${_newpass}:${_newday}:@' '${NEWROOT}/etc/shadow'"
if [[ ${CPUARCH} == amd64 ]]; then
echo $'\n'"GRUB_PLATFORMS=\"efi-64 pc\"" >> "${NEWROOT}/etc/portage/make.conf"
else
echo $'\n'"GRUB_PLATFORMS=\"efi-64\"" >> "${NEWROOT}/etc/portage/make.conf"
fi
echo $'\n'"GENTOO_MIRRORS=\"${MIRROR}\"" >> "${NEWROOT}/etc/portage/make.conf"
}
_chroot_exec() {
local arg cmds=""
for arg; do
if [[ $arg =~ [[:space:]] ]]; then
cmds+=" \"${arg//\"/\\\"}\""
else
cmds+=" ${arg}"
fi
done
cmds="${cmds# }"
_log w ">>> chroot '$NEWROOT' /bin/bash -lc '$cmds'"
eval "chroot '$NEWROOT' /bin/bash -lc '$cmds'"
}
declare -a _D2G_DRACUT_MODULES _D2G_ONETIME_PKGS _D2G_EXTRA_DEPS
_D2G_BINPKGS_PROFILE_VER="23.0"
_D2G_BINPKGS_CATEGORIES_AMD64_DEFAULT=1
_D2G_BINPKGS_CATEGORIES_AMD64=(
"x32"
"x86-64"
"x86-64-v3"
"x86-64_hardened"
"x86-64_llvm"
"x86-64_musl"
"x86-64_musl_hardened"
"x86-64_musl_llvm"
)
_D2G_BINPKGS_CATEGORIES_ARM64_DEFAULT=1
_D2G_BINPKGS_CATEGORIES_ARM64=(
"aarch64_be"
"arm64"
"arm64_llvm"
"arm64_musl"
"arm64_musl_hardened"
"arm64_musl_llvm"
)
_the_best_matched_binpkgs_category() {
local default_index matched_index='' i stage3_category="${STAGE3_TARBALL#*stage3-}" x86_64_v3_supported=0
local -a categories
stage3_category="${stage3_category//-/_}"
case "$CPUARCH" in
amd64)
stage3_category="${stage3_category/#amd64/x86-64}"
categories=("${_D2G_BINPKGS_CATEGORIES_AMD64[@]}")
default_index="$_D2G_BINPKGS_CATEGORIES_AMD64_DEFAULT"
if ld.so --help | grep 'x86-64-v3' | grep 'supported' >/dev/null; then
x86_64_v3_supported=1
fi
;;
arm64)
categories=("${_D2G_BINPKGS_CATEGORIES_ARM64[@]}")
default_index="$_D2G_BINPKGS_CATEGORIES_ARM64_DEFAULT"
;;
esac
for ((i=0; i<${#categories[@]}; i++)); do
if [[ $stage3_category == ${categories[i]}* ]]; then
matched_index="$i"
fi
done
if [[ $matched_index == '' ]]; then
matched_index="$default_index"
fi
# THIS IS A SPECIFIC CONDITION, PLEASE UPDATE IT CAREFULLY
if [[ $x86_64_v3_supported == 1 && $CPUARCH == "amd64" && $matched_index == 1 ]]; then
matched_index=2
fi
echo -n "${categories[$matched_index]}"
}
preparePkgsConfiguration() {
if [[ ! ${STAGE3_TARBALL} =~ systemd ]]; then
_D2G_EXTRA_DEPS+=("net-misc/netifrc")
fi
if [[ ${_D2G_LUKS_ENABLED} == 1 ]]; then
if [[ $STAGE3_TARBALL =~ systemd ]]; then
echo 'sys-apps/systemd cryptsetup' >>"${NEWROOT}/etc/portage/package.use/cryptsetup"
_D2G_ONETIME_PKGS+=("sys-apps/systemd")
fi
_D2G_DRACUT_MODULES+=("crypt")
echo 'sys-fs/cryptsetup -static-libs' >>"${NEWROOT}/etc/portage/package.use/cryptsetup"
_D2G_EXTRA_DEPS+=("sys-fs/cryptsetup")
fi
if [[ ${_D2G_LVM_ENABLED} == 1 ]]; then
cp -aL /etc/lvm "${NEWROOT}/etc/"
_D2G_DRACUT_MODULES+=("lvm")
_D2G_EXTRA_DEPS+=("sys-fs/lvm2")
fi
if [[ ${_D2G_BTRFS_ENABLED} == 1 ]]; then
_D2G_DRACUT_MODULES+=("btrfs")
_D2G_EXTRA_DEPS+=("sys-fs/btrfs-progs")
fi