-
Notifications
You must be signed in to change notification settings - Fork 78
/
configure
executable file
·1652 lines (1457 loc) · 57.4 KB
/
configure
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
set -uo pipefail
################################################
# Initialize script environment
# Find the directory this script is stored in. (from: http://stackoverflow.com/questions/59895)
function get_source_dir() {
local source="${BASH_SOURCE[0]}"
while [[ -h $source ]]; do
local tmp
tmp="$(cd -P "$(dirname "${source}")" && pwd)"
source="$(readlink "${source}")"
[[ $source != /* ]] && source="${tmp}/${source}"
done
echo -n "$(realpath "$(dirname "${source}")")"
}
GENTOO_INSTALL_REPO_DIR="$(get_source_dir)"
export GENTOO_INSTALL_REPO_DIR
export GENTOO_INSTALL_REPO_SCRIPT_ACTIVE=true
# shellcheck source=./scripts/utils.sh
source "$GENTOO_INSTALL_REPO_DIR/scripts/utils.sh"
# Remember actual config file path
CONFIG_FILE="$(realpath "${1-"gentoo.conf"}" 2>/dev/null)"
RELA_CONFIG_FILE="$(realpath --relative-to="$(pwd)" "$CONFIG_FILE" 2>/dev/null)"
# Check if help is requested
while [[ $# -gt 0 ]]; do
case "$1" in
""|"help"|"--help"|"-help"|"-h")
echo "Usage: $0 [config]"
echo "Starts the gentoo configurator. If no file is given, it defaults to 'gentoo.conf'."
echo "If the file doesn't exist, the configurator loads default values, otherwise, the"
echo "the configuration is loaded into the configurator."
exit 0
;;
esac
shift
done
check_wanted_programs dialog ncurses=ncursesw6-config
# Determine whether EFI is available
HAS_EFI_SUPPORT=$([[ -d /sys/firmware/efi ]] && echo -n "true" || echo -n "false")
DEFAULT_BOOT_TYPE=$([[ $HAS_EFI_SUPPORT == true ]] && echo -n "efi" || echo -n "bios")
EFI_UNSUPPORTED_MESSAGE_SHORT="Missing EFI support on this system!"
EFI_UNSUPPORTED_MESSAGE="Apparently, your system does NOT support efi. Double-check before proceeding!"
# Wrap dialog in two functions to prevent it from cluttering stderr.
function dialog_wrapper() { dialog_out=$(command dialog --colors "$@" 3>&2 2>&1 1>&3 3>&-); }
function dialog() { dialog_wrapper "$@" 2>&1; }
################################################
# Configuration helper functions
function get_timezone() {
local file
if file="$(readlink /etc/localtime)"; then
# /etc/localtime is a symlink as expected
local timezone
timezone=${file#*zoneinfo/}
if [[ $timezone == "$file" ]]; then
# not pointing to expected location or not Region/City
echo "Europe/London"
else
echo "$timezone"
fi
else
# compare files by contents
find /usr/share/zoneinfo -type f -exec cmp -s {} /etc/localtime \; -print \
| sed -e 's,.*/zoneinfo/,,' \
| head -1
fi
}
function get_default_keymap() {
local keymap
keymap="$(grep KEYMAP /etc/vconsole.conf 2>/dev/null || echo "KEYMAP=us")"
keymap="${keymap#KEYMAP=}"
local map
for map in "${ALL_KEYMAPS[@]}"; do
if [[ $map == "$keymap" ]]; then
echo -n "${keymap}"
return
fi
done
# Fallback to us
echo -n "us"
}
function get_all_keymaps() {
ALL_KEYMAPS=()
local map
for map in $(find /usr/share/keymaps/ /usr/share/kbd/keymaps/ -type f -iname '*.map.gz' -printf "%f\n" 2>/dev/null | sort -u); do
ALL_KEYMAPS+=("${map%%.map.gz}")
done
}
function get_all_timezones() {
readarray -t ALL_TIMEZONES < <(find /usr/share/zoneinfo -type f -printf "%P\n" | sort -u)
}
function recalculate_locales() {
LOCALES=""
N_LOCALES=0
local selected_index_list="$SELECTED_LOCALES"
local next_selected
next_selected="${selected_index_list%% *}"
selected_index_list="${selected_index_list#* }"
local i=0
for item in "${SUPPORTED_LOCALES[@]}"; do
if [[ "$i" == "$next_selected" ]]; then
LOCALES="$LOCALES"$'\n'"$item"
next_selected="${selected_index_list%% *}"
selected_index_list="${selected_index_list#* }"
((++N_LOCALES))
fi
((++i))
done
LOCALES="${LOCALES:1}"
}
function define_disk_configuration_function() {
cat << EOF
function disk_configuration() {
$* ;
}
EOF
}
function define_swap() {
if [[ $PARTITIONING_USE_SWAP == "true" ]]; then
echo -n "${PARTITIONING_SWAP@Q}"
else
echo -n "false"
fi
}
function define_zfs_compression() {
if [[ $PARTITIONING_ZFS_USE_COMPRESSION != "false" ]]; then
echo -n "${PARTITIONING_ZFS_COMPRESSION@Q}"
else
echo -n "false"
fi
}
function define_disk_layout() {
local swapdev
case "$PARTITIONING_SCHEME" in
"classic_single_disk") define_disk_configuration_function "create_classic_single_disk_layout swap=$(define_swap) type=${PARTITIONING_BOOT_TYPE@Q} luks=${PARTITIONING_USE_LUKS@Q} root_fs=${PARTITIONING_ROOT_FS@Q}" "${PARTITIONING_DEVICE@Q}" ;;
"existing_partitions")
if [[ "$PARTITIONING_USE_SWAP" ]]; then
swapdev=${PARTITIONING_SWAP_DEVICE:-false}
else
swapdev=false
fi
define_disk_configuration_function "create_existing_partitions_layout boot=${PARTITIONING_BOOT_DEVICE@Q} swap=${swapdev@Q} type=${PARTITIONING_BOOT_TYPE@Q}" "${PARTITIONING_DEVICE@Q}" ;;
"zfs_centric") define_disk_configuration_function "create_zfs_centric_layout swap=$(define_swap) type=${PARTITIONING_BOOT_TYPE@Q} encrypt=${PARTITIONING_ZFS_USE_ENCRYPTION@Q} compress=$(define_zfs_compression) pool_type=${PARTITIONING_ZFS_POOL_TYPE@Q}" "${PARTITIONING_DEVICES[@]@Q}" ;;
"btrfs_centric") define_disk_configuration_function "create_btrfs_centric_layout swap=$(define_swap) type=${PARTITIONING_BOOT_TYPE@Q} raid_type=${PARTITIONING_BTRFS_RAID_TYPE@Q} luks=${PARTITIONING_USE_LUKS@Q}" "${PARTITIONING_DEVICES[@]@Q}" ;;
"raid0_luks") define_disk_configuration_function "create_raid0_luks_layout swap=$(define_swap) type=${PARTITIONING_BOOT_TYPE@Q} luks=${PARTITIONING_USE_LUKS@Q} root_fs=${PARTITIONING_ROOT_FS@Q}" "${PARTITIONING_DEVICES[@]@Q}" ;;
"raid1_luks") define_disk_configuration_function "create_raid1_luks_layout swap=$(define_swap) type=${PARTITIONING_BOOT_TYPE@Q} luks=${PARTITIONING_USE_LUKS@Q} root_fs=${PARTITIONING_ROOT_FS@Q}" "${PARTITIONING_DEVICES[@]@Q}" ;;
"custom")
# Show current function declaration, trim trailing whitespace
declare -f disk_configuration \
| sed -e 's/\s*$//'
;;
esac
}
ALL_GENTOO_ARCHS=("x86" "amd64" "arm" "arm64")
ALL_STAGE3_VARIANTS=(
"openrc" "openrc | Minimal OpenRC base (recommended)"
"desktop-openrc" "openrc-desktop | OpenRC, desktop profile, might have blockers"
"systemd" "systemd | Minimal systemd base (recommended)"
"systemd-mergedusr" "systemd-mergedusr | Minimal systemd base with merged filesystem layout"
"desktop-systemd" "systemd-desktop | systemd, desktop profile, might have blockers"
"nomultilib-openrc" "nomultilib-openrc | Minimal OpenRC base without 32bits support (Experimental)"
"nomultilib-systemd" "nomultilib-systemd | Minimal systemd base without 32bits support (Experimental)"
"nomultilib-systemd-mergedusr" "nomultilib-systemd-mergedusr | Minimal systemd base with merged filesystem layout and without 32bits support (Experimental)"
"x32-openrc" "x32-openrc | Minimal OpenRC base without 64bits support (Experimental)"
"x32-systemd" "x32-systemd | Minimal systemd base without 64bits support (Experimental)"
"x32-systemd-mergedusr" "x32-systemd-mergedusr | Minimal systemd base with merged filesystem layout and without 64bits support (Experimental)"
"llvm-openrc" "llvm-openrc | Minimal OpenRC base compiled with LLVM (Experimental)"
"llvm-systemd" "llvm-systemd | Minimal systemd base compiled with LLVM (Experimental)"
"llvm-systemd-mergedusr" "llvm-systemd-mergedusr | Minimal systemd base with merged filesystem layout compiled with LLVM (Experimental)"
"hardened-openrc" "hardened-openrc | Hardened OpenRC base (Experimental)"
"hardened-nomultilib-openrc" "hardened-nomultilib-openrc | Hardened OpenRC base without 32bits support (Experimental)"
"hardened-selinux-openrc" "hardened-selinux-openrc | Hardened OpenRC base with SELinux (Experimental)"
"hardened-nomultilib-selinux-openrc" "hardened-nomultilib-selinux-openrc | Hardened OpenRC base with SELinux and without 32bits support (Experimental)"
"musl" "musl-openrc | Minimal OpenRC base using musl (Experimental)"
"musl-llvm" "musl-llvm-openrc | Minimal OpenRC base using musl compiled with LLVM (Experimental)"
"musl-hardened" "musl-hardened-openrc | Hardened OpenRC base using musl (Experimental)"
)
ALL_PARTITIONING_SCHEMES=(
"classic_single_disk" "Classic single disk layout (boot/efi, swap?, root)"
"existing_partitions" "Skip partitioning, use existing pre-formatted partitions"
"zfs_centric" "ZFS centric (optional ZFS compression and encryption)"
"btrfs_centric" "Btrfs centric (optional raid0/1 via btrfs)"
"raid0_luks" "Raid0 (N>=2 disks) and luks for root"
"raid1_luks" "Raid1 (N>=2 disks) and luks for root"
"custom" "Custom (expert option; edit the config manually later)"
)
PARTITIONING_BOOT_TYPES=(
"efi" "efi$([[ $HAS_EFI_SUPPORT == true ]] && echo -n "" || echo -n " (!! $EFI_UNSUPPORTED_MESSAGE_SHORT !!)")"
"bios" "bios"
)
PARTITIONING_ROOT_FS_TYPES=("ext4" "btrfs")
PARTITIONING_BTRFS_RAID_TYPES=("raid0" "raid1")
PARTITIONING_ZFS_POOL_TYPES=("standard" "custom")
PARTITIONING_ZFS_COMPRESSIONS=("on" "gzip" "lz4" "lzjb" "zle" "zstd" "zstd-fast")
PORTAGE_SYNC_TYPES=("git" "rsync")
function create_single_disk_layout() {
create_classic_single_disk_layout "$@"
}
function parse_swap() {
if [[ $1 == "false" ]]; then
PARTITIONING_USE_SWAP=false
PARTITIONING_SWAP=8GiB
else
PARTITIONING_USE_SWAP=true
PARTITIONING_SWAP="$1"
fi
}
function parse_zfs_compression() {
if [[ $1 == "false" ]]; then
PARTITIONING_ZFS_USE_COMPRESSION=false
PARTITIONING_ZFS_COMPRESSION=zstd
else
PARTITIONING_ZFS_USE_COMPRESSION=true
PARTITIONING_ZFS_COMPRESSION="$1"
fi
}
function create_classic_single_disk_layout() {
PARTITIONING_SCHEME="classic_single_disk"
local known_arguments=('+swap' '?type' '?luks' '?root_fs')
local extra_arguments=()
declare -A arguments; parse_arguments "$@"
PARTITIONING_DEVICE="${extra_arguments[0]}"
parse_swap "${arguments[swap]}"
PARTITIONING_BOOT_TYPE="${arguments[type]}"
PARTITIONING_USE_LUKS="${arguments[luks]:-false}"
PARTITIONING_ROOT_FS="${arguments[root_fs]:-ext4}"
}
function create_existing_partitions_layout() {
PARTITIONING_SCHEME="existing_partitions"
local known_arguments=('+swap' '+boot' '?type')
local extra_arguments=()
declare -A arguments; parse_arguments "$@"
PARTITIONING_DEVICE="${extra_arguments[0]}"
if [[ "${arguments[swap]}" == "false" ]]; then
PARTITIONING_USE_SWAP=false
PARTITIONING_SWAP_DEVICE=""
else
PARTITIONING_USE_SWAP=true
PARTITIONING_SWAP_DEVICE="${arguments[swap]}"
fi
PARTITIONING_BOOT_TYPE="${arguments[type]}"
PARTITIONING_BOOT_DEVICE="${arguments[boot]}"
}
function create_raid0_luks_layout() {
PARTITIONING_SCHEME="raid0_luks"
local known_arguments=('+swap' '?type' '?luks' '?root_fs')
local extra_arguments=()
declare -A arguments; parse_arguments "$@"
PARTITIONING_DEVICES=("${extra_arguments[@]}")
parse_swap "${arguments[swap]}"
PARTITIONING_BOOT_TYPE="${arguments[type]}"
PARTITIONING_USE_LUKS="${arguments[luks]:-true}"
PARTITIONING_ROOT_FS="${arguments[root_fs]:-ext4}"
}
function create_raid1_luks_layout() {
PARTITIONING_SCHEME="raid1_luks"
local known_arguments=('+swap' '?type' '?luks' '?root_fs')
local extra_arguments=()
declare -A arguments; parse_arguments "$@"
PARTITIONING_DEVICES=("${extra_arguments[@]}")
parse_swap "${arguments[swap]}"
PARTITIONING_BOOT_TYPE="${arguments[type]}"
PARTITIONING_USE_LUKS="${arguments[luks]:-true}"
PARTITIONING_ROOT_FS="${arguments[root_fs]:-ext4}"
}
function create_zfs_centric_layout() {
PARTITIONING_SCHEME="zfs_centric"
local known_arguments=('+swap' '?type' '?pool_type' '?encrypt' '?compress')
local extra_arguments=()
declare -A arguments; parse_arguments "$@"
PARTITIONING_DEVICES=("${extra_arguments[@]}")
parse_swap "${arguments[swap]}"
PARTITIONING_BOOT_TYPE="${arguments[type]}"
PARTITIONING_ZFS_POOL_TYPE="${arguments[pool_type]:-standard}"
PARTITIONING_ZFS_USE_ENCRYPTION="${arguments[encrypt]:-false}"
parse_zfs_compression "${arguments[compress]:-false}"
}
function create_btrfs_raid_layout() {
create_btrfs_centric_layout "$@"
}
function create_btrfs_centric_layout() {
PARTITIONING_SCHEME="btrfs_centric"
# shellcheck disable=SC2034
local known_arguments=('+swap' '?type' '?raid_type' '?luks')
local extra_arguments=()
declare -A arguments; parse_arguments "$@"
PARTITIONING_DEVICES=("${extra_arguments[@]}")
parse_swap "${arguments[swap]}"
PARTITIONING_BOOT_TYPE="${arguments[type]}"
PARTITIONING_USE_LUKS="${arguments[luks]:-false}"
PARTITIONING_BTRFS_RAID_TYPE="${arguments[raid_type]:-raid0}"
}
################################################
# Configuration constants
get_all_keymaps
get_all_timezones
readarray -t SUPPORTED_LOCALES < "$GENTOO_INSTALL_REPO_DIR/contrib/i18n_supported"
readarray -t LOCALE_A < <(locale -a)
################################################
# Load/Default configuration
function load_selected_locales() {
local sel_locales=()
local IFS=$'\n'
declare -A selected_by_name
for i in $LOCALES; do
selected_by_name["$i"]=true
done
local i=0
for item in "${SUPPORTED_LOCALES[@]}"; do
[[ "${selected_by_name[$item]:-}" == true ]] \
&& sel_locales+=("$i")
((++i))
done
local IFS=" "
SELECTED_LOCALES="${sel_locales[*]}"
}
function process_config() {
disk_configuration
if [[ "$KEYMAP" == "$KEYMAP_INITRAMFS" ]]; then
KEYMAP_INITRAMFS_OTHER=false
else
KEYMAP_INITRAMFS_OTHER=true
fi
load_selected_locales
recalculate_locales
}
function load_config() {
# First load defaults, then replace by sourcing config.
load_default_config
# Fallback to custom partitioning scheme if it isn't set in the actual config
PARTITIONING_SCHEME="custom"
# Load settings
# shellcheck disable=SC1090
source "$1" || die "Could not load given configuration."
# After loading a config, no unsaved changes exist.
UNSAVED_CHANGES=false
}
function load_default_config() {
HOSTNAME="gentoo"
TIMEZONE="$(get_timezone)"
KEYMAP="$(get_default_keymap)"
KEYMAP_INITRAMFS="$KEYMAP"
LOCALES="C.UTF-8 UTF-8"
LOCALE="C.UTF-8"
SYSTEMD_NETWORKD=true
SYSTEMD_NETWORKD_INTERFACE_NAME="en*"
SYSTEMD_NETWORKD_DHCP=true
SYSTEMD_NETWORKD_ADDRESSES=("192.168.1.100/32" "fd00::1/64")
SYSTEMD_NETWORKD_GATEWAY="192.168.1.1"
SYSTEMD_INITRAMFS_SSHD=false
function disk_configuration() {
#create_zfs_centric_layout swap=8GiB type="$DEFAULT_BOOT_TYPE" encrypt=true compress=zstd pool_type=standard /dev/sdX
create_classic_single_disk_layout swap=8GiB type="$DEFAULT_BOOT_TYPE" luks=true root_fs=ext4 /dev/sdX
}
STAGE3_VARIANT="systemd"
PORTAGE_SYNC_TYPE="git"
PORTAGE_GIT_FULL_HISTORY=false
PORTAGE_GIT_MIRROR="https://anongit.gentoo.org/git/repo/sync/gentoo.git"
GENTOO_MIRROR="https://mirror.eu.oneandone.net/linux/distributions/gentoo/gentoo"
GENTOO_ARCH="amd64"
USE_PORTAGE_TESTING=true
SELECT_MIRRORS=false
SELECT_MIRRORS_LARGE_FILE=false
ADDITIONAL_PACKAGES=()
ENABLE_SSHD=true
ENABLE_BINPKG=false
ROOT_SSH_AUTHORIZED_KEYS=""
# All settings are unsaved.
UNSAVED_CHANGES=true
}
SAVE_AS_FILENAME="$RELA_CONFIG_FILE"
if [[ -e "$CONFIG_FILE" ]]; then
load_config "$CONFIG_FILE"
else
load_default_config
fi
process_config
################################################
# Menu helpers and constants
# $1: exit code
function clear_and_exit() {
dialog --clear
clear -x
exit "$1"
}
function ellipsis() {
local len="$1"
shift
local str="$*"
if [[ "${#str}" -gt "$len" ]]; then
echo "${str:0:$len}…"
else
echo "$str"
fi
}
function on_off_toggle() {
if [[ "${!1}" == true ]]; then
declare -g "$1"=false
else
declare -g "$1"=true
fi
}
function on_off_str() {
if [[ "$1" == true ]]; then
echo -n "$2"
else
echo -n "$3"
fi
}
function on_off_label() {
local prefix="${2:-}"
on_off_str "$1" "${prefix}[*]" "${prefix}[ ]"
}
function on_off_label_inverted() {
local var=$1
shift
on_off_label "$(is_on "$var" && echo false || echo true)" "$@"
}
function is_on() {
[[ "$1" == true ]]
}
function is_off() {
[[ "$1" != true ]]
}
# if $1 is in $2..
function one_of() {
local what="$1"
shift
for i in "$@"; do
[[ "$i" == "$what" ]] \
&& return 0
done
return 1
}
# $1: title
# $2: description
# $3: space separated index list of selected items (e.g. "0 1 5 6")
# $@: all items
function menu_splitlist() {
local title="$1"
local description="$2"
local selected_index_list="$3"
shift 3
# Build option array
local items=()
local item
local i=0
local next_selected="${selected_index_list%% *}"
local selected_index_list="${selected_index_list#* }"
for item in "$@"; do
if [[ "$i" == "$next_selected" ]]; then
items+=("$i" "$item" "on")
next_selected="${selected_index_list%% *}"
selected_index_list="${selected_index_list#* }"
else
items+=("$i" "$item" "off")
fi
((++i))
done
# Show selection dialog
dialog \
--title "$title" \
--buildlist "$description\nUse ^ to focus the list of unselected items and $ to focus the list of selected items. Use <Space> to select/deselect an item and select <OK> by pressing <Enter> when finished." \
"${BUILDLIST_SIZE[@]}" "${items[@]}"
local diag_exit=$?
if [[ $diag_exit == 0 ]]; then
# <OK>
return 0
elif [[ $diag_exit == 1 ]]; then
# <Cancel>
return 1
else
# <ESC><ESC>
return 1
fi
}
# $1: title
# $2: description
# $3: default item
# $@: [tag label]...
function menu_radiolist_labeled() {
local title="$1"
local description="$2"
local default_item="$3"
shift 3
# Build option array
local items=()
local tag
local label
while [[ "$#" -gt 0 ]]; do
tag="$1"
label="$2"
shift 2
if [[ $tag == "$default_item" ]]; then
items+=("$tag" "$label" "on")
else
items+=("$tag" "$label" "off")
fi
done
# Show selection dialog
dialog \
--no-tags \
--title "$title" \
--help-button \
--help-label "Select" \
--help-status \
--ok-label "OK" \
--default-item "$default_item" \
--default-button help \
--radiolist "$description\nUse <Select> to select the option under the cursor, or <OK> to use the option which is selected with an asterisk." \
"${RADIOLIST_SIZE[@]}" "${items[@]}"
local diag_exit=$?
if [[ $diag_exit == 0 ]]; then
# <OK>
return 0
elif [[ $diag_exit == 1 ]]; then
# <Cancel>
return 1
elif [[ $diag_exit == 2 ]]; then
# <Select>
local sel="${dialog_out#HELP }"
local sel_cur="${sel% *}"
#local sel_radio="${sel#* }"
dialog_out="$sel_cur"
return 0
else
# <ESC><ESC>
return 1
fi
}
# $1: title
# $2: description
# $3: default item
# $@: items
function menu_radiolist() {
local title="$1"
local description="$2"
local default_item="$3"
shift 3
# Build option array
local items=()
local item
for item in "$@"; do
if [[ $item == "$default_item" ]]; then
items+=("$item" "on")
else
items+=("$item" "off")
fi
done
# Show selection dialog
dialog \
--no-items \
--title "$title" \
--help-button \
--help-label "Select" \
--help-status \
--ok-label "OK" \
--default-item "$default_item" \
--default-button help \
--radiolist "$description\nUse <Select> to select the option under the cursor, or <OK> to use the option which is selected with an asterisk." \
"${RADIOLIST_SIZE[@]}" "${items[@]}"
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <OK>
return 0
elif [[ $diag_exit == 1 ]]; then
# <Cancel>
return 1
elif [[ $diag_exit == 2 ]]; then
# <Select>
local sel="${dialog_out#HELP }"
local sel_cur="${sel% *}"
#local sel_radio="${sel#* }"
dialog_out="$sel_cur"
return 0
else
# <ESC><ESC>
return 1
fi
}
# $1: title
# $2: description
# $3: current device (will be canonicalized if possible)
function menu_select_device() {
local title="$1"
local desc="$2"
local prev_selected
prev_selected=$(canonicalize_device "$3")
while true; do
local all_devices=()
for dev in /dev/disk/by-id/*; do
all_devices+=("$dev" "$(basename "$dev")")
done
all_devices+=("/dev/null" "<enter custom path>")
if menu_radiolist_labeled "$title" "$desc" "$prev_selected" "${all_devices[@]}"; then
if [[ "$dialog_out" == "/dev/null" ]]; then
while true; do
if dialog \
--title "$1" \
--inputbox "$([[ -e $prev_selected ]] || echo -n "\Z1The previously selected device $prev_selected does not exist!\Zn ")Enter the path of the desired device." \
"${INPUTBOX_SIZE[@]}" "$prev_selected"
then
# Repeat until selected device exists or cancelled
prev_selected="$dialog_out"
[[ ! -e "$dialog_out" ]] && continue
return 0
else
# <Cancel> -> return to previous menu
break
fi
done
# Return to radiolist
continue
else
return 0
fi
else
# <Cancel>
return 1
fi
done
}
function msgbox_help() {
dialog --msgbox "$1" "${HELP_POPUP_SIZE[@]}"
}
function menu_exit() {
if [[ $UNSAVED_CHANGES == "true" ]]; then
dialog \
--help-button --help-label "Back" \
--yes-label "Save" --no-label "Discard" \
--yesno "Do you want to save your configuration?\n(Press <ESC><ESC>, or choose <Back> to continue gentoo configuration)." \
"${CONFIRM_SIZE[@]}"
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <Save>
save "$CONFIG_FILE"
clear_and_exit 0
elif [[ $diag_exit == 1 ]]; then
# <Discard>
clear_and_exit 0
else
# Back to menu (<ESC><ESC>, <Back>)
true
fi
else
# Nothing was changed. Exit immediately.
clear_and_exit 0
fi
}
function menu_save_as() {
dialog \
--ok-label "Save" \
--inputbox "Enter a filename to which this configuration should be saved.\n(Press <ESC><ESC>, or choose <Cancel> to abort)." \
"${INPUTBOX_SIZE[@]}" "$SAVE_AS_FILENAME"
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <Save>
SAVE_AS_FILENAME="$dialog_out"
save "$SAVE_AS_FILENAME"
UNSAVED_CHANGES=false
else
# Back to menu (<ESC><ESC>, <Cancel>)
true
fi
}
function menu() {
local item
local item_tag
local tag_item_list=()
declare -A reverse_lookup
# Create menu list
for item in "${MENU_ITEMS[@]}"; do
# Only if item is visible
"${item}_show" || continue
item_tag="$("${item}_tag")"
tag_item_list+=("$item_tag" "$("${item}_label")")
reverse_lookup["$item_tag"]="$item"
done
dialog --colors \
--title "Gentoo configuration ($RELA_CONFIG_FILE)" \
--extra-button --extra-label "Exit" \
--help-button \
--default-item "$SELECTED_MENU_ITEM" \
--ok-label "Select" --cancel-label "Save" \
--menu "This is the gentoo configuration menu. Read and adjust all options below carefully. Save your desired configuration and run ./install afterwards. Use <Help> if you want further information on any option." \
"${MENU_SIZE[@]}" "${tag_item_list[@]}"
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <Select>
SELECTED_MENU_ITEM="$dialog_out"
[[ -z "$SELECTED_MENU_ITEM" ]] \
|| "${reverse_lookup[$SELECTED_MENU_ITEM]}_menu"
elif [[ $diag_exit == 1 ]]; then
# <Save>
SELECTED_MENU_ITEM="$dialog_out"
menu_save_as
elif [[ $diag_exit == 2 ]]; then
# <Help>
SELECTED_MENU_ITEM="${dialog_out#HELP }"
msgbox_help "$("${reverse_lookup[$SELECTED_MENU_ITEM]}_help")"
else
# Exit (<ESC><ESC>, <Exit>)
SELECTED_MENU_ITEM="${dialog_out:-$SELECTED_MENU_ITEM}"
menu_exit
true
fi
}
function init_menu_size() {
local cols
local rows
cols=$(tput cols) || cols=80
rows=$(tput lines) || rows=24
local max_h=$((rows - 3))
local max_w=$((cols - 6))
SELECTED_MENU_ITEM=""
MENU_SIZE=("$max_h" "$max_w" "$((max_h - 8))")
INPUTBOX_SIZE=("11" "$max_w")
EDITTEXT_SIZE=("$max_h" "$max_w")
RADIOLIST_SIZE=("$max_h" "$max_w" "$((max_h - 12))")
BUILDLIST_SIZE=("$max_h" "$max_w" "$((max_h - 12))")
HELP_POPUP_SIZE=("$((max_h - 12))" "$((max_w - 10))")
CONFIRM_SIZE=("8" "66")
}
#SELECTED_MENU_ITEM=""
#MENU_SIZE=("20" "76" "12")
#INPUTBOX_SIZE=("11" "76")
#EDITTEXT_SIZE=("16" "76")
#RADIOLIST_SIZE=("20" "76" "8")
#BUILDLIST_SIZE=("20" "76" "8")
#HELP_POPUP_SIZE=("8" "66")
#CONFIRM_SIZE=("8" "66")
init_menu_size
################################################
# Menu definition
MENU_ITEMS=(
"PARTITIONING_SCHEME"
"PARTITIONING_BOOT_TYPE"
"PARTITIONING_BOOT_DEVICE"
"PARTITIONING_USE_SWAP"
"PARTITIONING_SWAP"
"PARTITIONING_SWAP_DEVICE"
"PARTITIONING_ROOT_FS"
"PARTITIONING_USE_LUKS"
"PARTITIONING_ZFS_POOL_TYPE"
"PARTITIONING_ZFS_USE_ENCRYPTION"
"PARTITIONING_ZFS_USE_COMPRESSION"
"PARTITIONING_ZFS_COMPRESSION"
"PARTITIONING_BTRFS_RAID_TYPE"
"PARTITIONING_DEVICE"
"PARTITIONING_DEVICES"
"--------"
"HOSTNAME"
"TIMEZONE"
"KEYMAP"
"KEYMAP_INITRAMFS_OTHER"
"KEYMAP_INITRAMFS"
"LOCALES"
"LOCALE"
"SYSTEMD_NETWORKD"
"SYSTEMD_INITRAMFS_SSHD"
"SYSTEMD_NETWORKD_INTERFACE_NAME"
"SYSTEMD_NETWORKD_DHCP"
"SYSTEMD_NETWORKD_ADDRESSES"
"SYSTEMD_NETWORKD_GATEWAY"
"--------"
"STAGE3_VARIANT"
"PORTAGE_SYNC_TYPE"
"PORTAGE_GIT_FULL_HISTORY"
"PORTAGE_GIT_MIRROR"
"GENTOO_MIRROR"
"GENTOO_ARCH"
"USE_PORTAGE_TESTING"
"SELECT_MIRRORS"
"SELECT_MIRRORS_LARGE_FILE"
"--------"
"ENABLE_SSHD"
"ENABLE_BINPKG"
"ROOT_SSH_AUTHORIZED_KEYS"
"ADDITIONAL_PACKAGES"
)
function --------_tag() { echo "────────────────────────────"; }
function --------_label() { echo "────────────────────────────"; }
function --------_show() { return 0; }
function --------_help() { echo "Congratulations, you found a separator."; }
function --------_menu() { true; }
function PARTITIONING_SCHEME_tag() { echo "Partitioning scheme"; }
function PARTITIONING_SCHEME_label() { echo "($PARTITIONING_SCHEME)"; }
function PARTITIONING_SCHEME_show() { return 0; }
function PARTITIONING_SCHEME_help() { echo "Select the desired partitioning scheme."; }
function PARTITIONING_SCHEME_menu() {
if menu_radiolist_labeled \
"Select partitioning scheme" \
"Select which partitioning scheme you want to follow. Have a look at the help text for this option (available in the menu) for more details.\n\nAll options support EFI/BIOS, swap and some form of encryption (luks/zfs).\n" \
"$PARTITIONING_SCHEME" \
"${ALL_PARTITIONING_SCHEMES[@]}"
then
# Set disk scheme
case "$dialog_out" in
"classic_single_disk") create_classic_single_disk_layout swap=8GiB type="$DEFAULT_BOOT_TYPE" luks=false root_fs=ext4 /dev/sdX ;;
"existing_partitions") create_existing_partitions_layout boot=/dev/sdA swap=false type="$DEFAULT_BOOT_TYPE" /dev/sdX ;;
"zfs_centric") create_zfs_centric_layout swap=8GiB type="$DEFAULT_BOOT_TYPE" encrypt=true compress=zstd pool_type=standard /dev/sdX ;;
"btrfs_centric") create_btrfs_centric_layout swap=8GiB type="$DEFAULT_BOOT_TYPE" raid_type=raid0 luks=false /dev/sdX ;;
"raid0_luks") create_raid0_luks_layout swap=8GiB type="$DEFAULT_BOOT_TYPE" luks=true root_fs=ext4 /dev/sdX /dev/sdY ;;
"raid1_luks") create_raid1_luks_layout swap=8GiB type="$DEFAULT_BOOT_TYPE" luks=true root_fs=ext4 /dev/sdX /dev/sdY ;;
"custom") PARTITIONING_SCHEME="$dialog_out" ;;
esac
UNSAVED_CHANGES=true
else
# Return to menu
true
fi
}
function PARTITIONING_BOOT_TYPE_tag() { echo " ├ Boot type"; }
function PARTITIONING_BOOT_TYPE_label() { echo " ├ ($PARTITIONING_BOOT_TYPE)$([[ $PARTITIONING_BOOT_TYPE == efi && $HAS_EFI_SUPPORT == false ]] && echo -n " \Z1$EFI_UNSUPPORTED_MESSAGE_SHORT\Zn" || echo -n "")"; }
function PARTITIONING_BOOT_TYPE_show() { [[ $PARTITIONING_SCHEME != "custom" ]]; }
function PARTITIONING_BOOT_TYPE_help() { echo "Select whether to use EFI or BIOS boot."; }
function PARTITIONING_BOOT_TYPE_menu() {
if menu_radiolist_labeled \
"Select boot type" \
"Select whether you want to use EFI or BIOS boot.$(on_off_str "$HAS_EFI_SUPPORT" "" " \Z1$EFI_UNSUPPORTED_MESSAGE\Zn")" \
"$PARTITIONING_BOOT_TYPE" \
"${PARTITIONING_BOOT_TYPES[@]}"
then
PARTITIONING_BOOT_TYPE="$dialog_out"
UNSAVED_CHANGES=true
else
# Return to menu
true
fi
}
function PARTITIONING_BOOT_DEVICE_tag() { echo " ├ Boot Device"; }
function PARTITIONING_BOOT_DEVICE_label() {
local devshort=$PARTITIONING_BOOT_DEVICE
devshort=$(shorten_device "$devshort")
if [[ -e "$PARTITIONING_BOOT_DEVICE" ]]; then
echo " ├ ($devshort)"
else
echo " ├ (\Z1$devshort\Zn)"
fi
}
function PARTITIONING_BOOT_DEVICE_show() { [[ $PARTITIONING_SCHEME != "custom" ]] && one_of "$PARTITIONING_SCHEME" "existing_partitions"; }
function PARTITIONING_BOOT_DEVICE_help() { echo "The device to use for the boot partition. For EFI systems this is the efi partition. Must be formatted already."; }
function PARTITIONING_BOOT_DEVICE_menu() {
if menu_select_device \
"Select boot device" \
"Select the device containing the EFI filesystem or /boot partition respectively for systems using efi or bios boot." \
"$PARTITIONING_BOOT_DEVICE"
then
PARTITIONING_BOOT_DEVICE="$dialog_out"
UNSAVED_CHANGES=true
else
# Return to menu
true
fi
}
function PARTITIONING_USE_SWAP_tag() { echo " ├ Use swap"; }
function PARTITIONING_USE_SWAP_label() { on_off_label "$PARTITIONING_USE_SWAP" " ├ "; }
function PARTITIONING_USE_SWAP_show() { [[ $PARTITIONING_SCHEME != "custom" ]]; }
function PARTITIONING_USE_SWAP_help() { echo "Select whether or not to create a swap partition."; }
function PARTITIONING_USE_SWAP_menu() {
on_off_toggle "PARTITIONING_USE_SWAP"
UNSAVED_CHANGES=true
}
function PARTITIONING_SWAP_tag() { echo " │ └ Swap amount"; }
function PARTITIONING_SWAP_label() { echo " │ └ ($PARTITIONING_SWAP)"; }
function PARTITIONING_SWAP_show() { [[ $PARTITIONING_SCHEME != "custom" ]] && is_on "$PARTITIONING_USE_SWAP" && ! one_of "$PARTITIONING_SCHEME" "existing_partitions"; }
function PARTITIONING_SWAP_help() { echo "Select the amount of swap to use."; }
function PARTITIONING_SWAP_menu() {
dialog \
--title "Select swap amount" \
--inputbox "Enter the amount of swap for the new system. Use the correct suffix (e.g. 16GiB, 1000MB)." \
"${INPUTBOX_SIZE[@]}" "$PARTITIONING_SWAP" \