/
installer.sh
executable file
·1468 lines (1235 loc) · 35.4 KB
/
installer.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
# Gentoo stage4 installation script for LinuxCNC
# Written by Alec Ari
set -eou pipefail
printf "\\033[0;33m
\\tGentoo stage4 installation script for LinuxCNC.
\\tWritten by Alec Ari
\\033[0m"
if [[ "${EUID}" -ne 0 ]] ; then
printf "\\n\\tError: Must have root privileges.\\n"
exit 1
fi
ROOT_MOUNT="/mnt/gentoo-cnc"
HOME_USER="lcnc"
HOME_MOUNT_SUBDIR="${ROOT_MOUNT}/home/${HOME_USER}"
STAGE4_TAG="v0.2-alpha"
STAGE4_NAME="lcnc-x86_64-v2-stage4"
STAGE4_SRCURI="https://github.com/NTULINUX/gentoo_backup/releases/download/${STAGE4_TAG}/${STAGE4_NAME}.tar.xz"
STAGE4_CHECKSUM="https://github.com/NTULINUX/gentoo_backup/releases/download/${STAGE4_TAG}/${STAGE4_NAME}.sha1sum"
verbose_prompt()
{
printf "\\n\\tEnable verbose installation?\\n
\\tThis may help diagnose problems with installation but may produce
\\texcessive terminal scrollback.\\n
\\tValid options (Enter for default):
\\t\\tYES/yes
\\t\\tNO/no (default)\\n\\n"
read -r "VERBOSE_ARG" ; printf "\\n"
if [[ "${VERBOSE_ARG}" == "N" || \
"${VERBOSE_ARG}" == "NO" || \
"${VERBOSE_ARG}" == "n" || \
"${VERBOSE_ARG}" == "no" || \
"${VERBOSE_ARG}" == "DEFAULT" || \
"${VERBOSE_ARG}" == "default" || \
-z "${VERBOSE_ARG}" ]]
then
VERBOSE_INSTALL="FALSE"
printf "\\tVerbose install disabled.\\n"
elif [[ "${VERBOSE_ARG}" == "Y" || \
"${VERBOSE_ARG}" == "YES" || \
"${VERBOSE_ARG}" == "y" || \
"${VERBOSE_ARG}" == "yes" ]]
then
VERBOSE_INSTALL="TRUE"
printf "\\tVerbose install enabled.\\n"
else
printf "\\n\\tError: Invalid selection: %s\\n" \
"${VERBOSE_ARG}"
exit 1
fi
}
verbose_prompt
if_log()
{
if [[ "${VERBOSE_INSTALL}" == "FALSE" ]] ; then
"${@}" >> /dev/null 2>&1
elif [[ "${VERBOSE_INSTALL}" == "TRUE" ]] ; then
"${@}"
fi
}
verify_psabi()
{
printf "\\n\\tChecking CPU requirements...\\n"
X86_64_V2="
mmx
mmxext
popcnt
sse
sse2
sse3
ssse3
sse4_1
sse4_2
"
mapfile -s 1 -t FLAGS < <(printf "%s" "${X86_64_V2}" | sed 's/\t//g')
for (( i=0 ; i < "${#FLAGS[@]}" ; i++ )) ; do
if_log printf "\\tChecking for: %s\\n" "${FLAGS[$i]}"
lscpu | grep "${FLAGS[$i]}" >> /dev/null 2>&1 || \
{
printf "\\tError: Missing: %s\\n" "${FLAGS[$i]}" ;
exit 1 ;
}
done
printf "\\n\\tDone. Your processor is x86-64-v2 or newer.\\n"
printf "\\tYou may safely use the Gentoo image for LinuxCNC.\\n"
}
linux_ver()
{
printf "\\tChecking Linux kernel version...\\n"
LINUX_MAJOR_VER=$(uname -r | cut -d '.' -f1)
LINUX_MINOR_VER=$(uname -r | cut -d '.' -f2)
if [[ "${LINUX_MAJOR_VER}" -lt 5 || \
"${LINUX_MAJOR_VER}" -eq 5 && \
"${LINUX_MINOR_VER}" -lt 15 ]]
then
printf "\\n\\tError: Linux kernel version must be at least 5.15.0\\n"
exit 1
elif [[ "${LINUX_MAJOR_VER}" -gt 6 ||
"${LINUX_MAJOR_VER}" -eq 6 && \
"${LINUX_MINOR_VER}" -gt 5 ]]
then
printf "\\n\\tError: Linux kernel version must not be newer than 6.5.\\n"
exit 1
else
printf "\\tLinux kernel version: %s\\n" "$(uname -r)"
fi
}
# If unable to locate config, skip checking for filesystem support. If options
# are disabled, do not error out until all options have been gathered.
linux_config_check()
{
if [[ -r "/proc/config.gz" ]] ; then
KCONFIG="/proc/config.gz"
elif [[ -r "/boot/config-$(uname -r)" ]] ; then
KCONFIG="/boot/config-$(uname -r)"
else
printf "\\tKernel configuration not found.\\n"
return 0 ;
fi
printf "\\n\\tCurrent kernel configuration found.
\\tEnsuring Linux kernel has proper filesystem support...\\n"
BTRFS_OPTIONS="
BTRFS_FS
BTRFS_FS_POSIX_ACL
"
EXT4_OPTIONS="
EXT4_FS
EXT4_FS_POSIX_ACL
"
F2FS_OPTIONS="
F2FS_FS
F2FS_FS_XATTR
F2FS_FS_POSIX_ACL
"
XFS_OPTIONS="
XFS_FS
XFS_POSIX_ACL
"
mapfile -s 1 -t BTRFS_STRINGS < <(printf "%s" "${BTRFS_OPTIONS}" | \
sed -e 's/\t//g' -e '$d')
mapfile -s 1 -t EXT4_STRINGS < <(printf "%s" "${EXT4_OPTIONS}" | \
sed -e 's/\t//g' -e '$d')
mapfile -s 1 -t F2FS_STRINGS < <(printf "%s" "${F2FS_OPTIONS}" | \
sed -e 's/\t//g' -e '$d')
mapfile -s 1 -t XFS_STRINGS < <(printf "%s" "${XFS_OPTIONS}" | \
sed -e 's/\t//g' -e '$d')
# Throw all errors first before exiting
set +e
for (( i=0 ; i < "${#BTRFS_STRINGS[@]}" ; i++ )) ; do
if ! zgrep "CONFIG_${BTRFS_STRINGS[$i]}=m" \
"${KCONFIG}" >> /dev/null 2>&1 && \
! zgrep "CONFIG_${BTRFS_STRINGS[$i]}=y" \
"${KCONFIG}" >> /dev/null 2>&1
then
printf "\\n\\tError: %s is not set.\\n" \
"CONFIG_${BTRFS_STRINGS[$i]}"
BTRFS_ERROR_THROWN=1
else
BTRFS_ERROR_THROWN=0
fi
done
for (( i=0 ; i < "${#EXT4_STRINGS[@]}" ; i++ )) ; do
if ! zgrep "CONFIG_${EXT4_STRINGS[$i]}=m" \
"${KCONFIG}" >> /dev/null 2>&1 && \
! zgrep "CONFIG_${EXT4_STRINGS[$i]}=y" \
"${KCONFIG}" >> /dev/null 2>&1
then
printf "\\n\\tError: %s is not set.\\n" \
"CONFIG_${EXT4_STRINGS[$i]}"
EXT4_ERROR_THROWN=1
else
EXT4_ERROR_THROWN=0
fi
done
for (( i=0 ; i < "${#F2FS_STRINGS[@]}" ; i++ )) ; do
if ! zgrep "CONFIG_${F2FS_STRINGS[$i]}=m" \
"${KCONFIG}" >> /dev/null 2>&1 && \
! zgrep "CONFIG_${F2FS_STRINGS[$i]}=y" \
"${KCONFIG}" >> /dev/null 2>&1
then
printf "\\n\\tError: %s is not set.\\n" \
"CONFIG_${F2FS_STRINGS[$i]}"
F2FS_ERROR_THROWN=1
else
F2FS_ERROR_THROWN=0
fi
done
for (( i=0 ; i < "${#XFS_STRINGS[@]}" ; i++ )) ; do
if ! zgrep "CONFIG_${XFS_STRINGS[$i]}=m" \
"${KCONFIG}" >> /dev/null 2>&1 && \
! zgrep "CONFIG_${XFS_STRINGS[$i]}=y" \
"${KCONFIG}" >> /dev/null 2>&1
then
printf "\\n\\tError: %s is not set.\\n" \
"CONFIG_${XFS_STRINGS[$i]}"
XFS_ERROR_THROWN=1
else
XFS_ERROR_THROWN=0
fi
done
set -e
if [[ "${BTRFS_ERROR_THROWN}" -eq 1 || \
"${EXT4_ERROR_THROWN}" -eq 1 || \
"${F2FS_ERROR_THROWN}" -eq 1 || \
"${XFS_ERROR_THROWN}" -eq 1 ]]
then
printf "\\n\\tErrors detected. Exiting...\\n\\n"
exit 1
else
printf "\\tAll filesystems enabled.\\n\\n"
fi
}
# Put version checks into their own section for ease of maintenance
btrfs_ver()
{
BTRFS_MAJOR_VER=$(mkfs.btrfs -V | grep -o "[0-9]" | sed -n '1p')
BTRFS_MINOR_VER=$(mkfs.btrfs -V | grep -o "[0-9]" | sed -n '2p')
BTRFS_PATCH_VER=$(mkfs.btrfs -V | grep -o "[0-9]" | sed -n '3p')
if [[ "${BTRFS_MAJOR_VER}" -lt 5 || \
"${BTRFS_MAJOR_VER}" -eq 5 && \
"${BTRFS_MINOR_VER}" -lt 15 || \
"${BTRFS_MINOR_VER}" -eq 15 && \
"${BTRFS_PATCH_VER}" -lt 1 ]]
then
printf "\\n\\tError: btfrs-progs must be 5.15.1 or newer.\\n"
exit 1
else
printf "\\tbtrfs-progs version: %s\\n" \
"${BTRFS_MAJOR_VER}.${BTRFS_MINOR_VER}.${BTRFS_PATCH_VER}"
fi
}
f2fs_ver()
{
F2FS_MAJOR_VER=$(mkfs.f2fs -V | cut -d ' ' -f2 | cut -d '.' -f1)
F2FS_MINOR_VER=$(mkfs.f2fs -V | cut -d ' ' -f2 | cut -d '.' -f2)
if [[ "${F2FS_MAJOR_VER}" -lt 1 || \
"${F2FS_MAJOR_VER}" -eq 1 && \
"${F2FS_MINOR_VER}" -lt 15 ]]
then
printf "\\n\\tError: f2fs-tools must be 1.15.0 or newer.\\n"
exit 1
else
printf "\\tf2fs-progs version: %s\\n" \
"${F2FS_MAJOR_VER}.${F2FS_MINOR_VER}"
fi
}
check_deps()
{
printf "\\n\\tChecking dependencies...\\n\\n"
# This is necessary to ensure we don't create a BTRFS or F2FS filesystem
# with an ancient kernel, but also that the kernel is not newer than that
# used to mount the F2FS filesystem (the PREEMPT_RT kernel)
# https://bugzilla.opensuse.org/show_bug.cgi?id=1109665#c0
linux_ver
# Make sure the running kernel supports BTRFS, EXT4, F2FS and XFS
linux_config_check
type mkfs.ext4 >> /dev/null 2>&1 || \
{
printf "\\n\\tError: e2fsprogs not installed.\\n" ;
exit 1 ;
}
type mkfs.xfs >> /dev/null 2>&1 || \
{
printf "\\n\\tError: xfsprogs not installed.\\n" ;
exit 1 ;
}
type mkfs.btrfs >> /dev/null 2>&1 || \
{
printf "\\n\\tError: btrfs-progs not installed.\\n" ;
exit 1 ;
} ; printf "\\tChecking version of btrfs-progs...\\n" ; btrfs_ver
type mkfs.f2fs >> /dev/null 2>&1 || \
{
printf "\\n\\tError: f2fs-tools not installed.\\n" ;
exit 1 ;
} ; printf "\\tChecking version of f2fs-tools...\\n" ; f2fs_ver
type mkfs.fat >> /dev/null 2>&1 || \
{
printf "\\n\\tError: dosfstools not installed.\\n" ;
exit 1 ;
}
type sfdisk >> /dev/null 2>&1 || \
{
printf "\\n\\tError: sfdisk (util-linux) not installed.\\n" ;
exit 1 ;
}
type blkdiscard >> /dev/null 2>&1 || \
{
printf "\\n\\tError: blkdiscard (util-linux) not installed.\\n" ;
exit 1 ;
}
type wipefs >> /dev/null 2>&1 || \
{
printf "\\n\\tError: wipefs (util-linux) not installed.\\n" ;
exit 1 ;
}
type partprobe >> /dev/null 2>&1 || \
{
printf "\\n\\tError: partprobe (GNU parted) not installed.\\n" ;
exit 1 ;
}
type tar >> /dev/null 2>&1 || \
{
printf "\\n\\tError: tar not installed.\\n" ;
exit 1 ;
}
type wget >> /dev/null 2>&1 || \
{
printf "\\n\\tError: wget not installed.\\n" ;
exit 1 ;
}
printf "\\n\\tDone.\\n"
}
legacy_or_uefi()
{
printf "\\n\\tPlease choose legacy or UEFI for installation.\\n
\\tValid options:
\\t\\tLEGACY/Legacy/legacy
\\t\\tUEFI/uefi
\\033[0;33m
\\tIMPORTANT:
\\tFor NVMe installation media, UEFI _MUST_ be selected!
\\033[0m\\n"
read -r "INSTALL_TYPE_ARG"
if [[ "${INSTALL_TYPE_ARG}" == "LEGACY" || \
"${INSTALL_TYPE_ARG}" == "Legacy" || \
"${INSTALL_TYPE_ARG}" == "legacy" ]]
then
INSTALL_TYPE="LEGACY"
printf "\\n\\tInstallation type: Legacy BIOS
\\033[0;33m
\\tIMPORTANT:
\\tPlease be sure CSM is enabled (if applicable) in BIOS before continuing!
\\033[0m\\n" ; sleep 3
elif [[ "${INSTALL_TYPE_ARG}" == "UEFI" || \
"${INSTALL_TYPE_ARG}" == "uefi" ]]
then
INSTALL_TYPE="UEFI"
printf "\\n\\tInstallation type: UEFI
\\033[0;33m
\\tIMPORTANT:
\\tPlease be sure CSM is disabled in BIOS before continuing!
\\033[0m\\n" ; sleep 3
printf "\\tEnsuring system has booted with UEFI Runtime Services...\\n"
# This method will not work on ancient kernels or those built
# with EFI_VARS instead of the replacement, EFIVAR_FS
# Double-check with mounted filesystems as well
if [[ $(find /sys/firmware/efi/efivars -type f | wc -l) -gt 1 ]] && \
grep -e "efivarfs" "/proc/mounts" >> /dev/null 2>&1 && \
mount | grep "efivarfs" >> /dev/null 2>&1
then
printf "\\tUEFI Runtime Services are supported.\\n"
else
printf "\\n\\tError: UEFI Runtime Services not supported.
\\tIf you are using a PREEMPT_RT kernel, you may need to pass: \`efi=runtime\`
\\ton the kernel command line.\\n"
exit 1
fi
elif [[ -z "${INSTALL_TYPE_ARG}" ]] ; then
printf "\\n\\tError: Must make a selection.\\n"
exit 1
else
printf "\\n\\tError: Invalid selection: %s\\n" \
"${INSTALL_TYPE_ARG}"
exit 1
fi
}
check_drive_prompt()
{
printf "\\n\\tPlease choose your device for the installation
\\ti.e. /dev/sda or /dev/nvme0n1 etc.
\\033[0;33m
\\tIMPORTANT:
\\tDo not specify a partition, an entire drive is required.
\\033[0m
\\tTo bring up a list of possible devices, type: list
\\tIf the list is too long to fully see, try: shortlist
\\033[0;31m
\\tWARNING:
\\tALL DATA ON THE SPECIFIED DEVICE WILL BE REMOVED!
\\tTHIS ACTION CANNOT BE UNDONE!
\\033[0m\\n"
read -r "ENTIRE_DRIVE" ; printf "\\n"
}
removable_prompt()
{
printf "\\n\\tIs this a removable device such as a flash drive?\\n
\\tThis is used to determine how GRUB will be installed (--removable)
\\tIf this is a device which will be used only on this system, say no. (default)
\\tSee: grub-install(8)\\n
\\tValid options (Enter for default):
\\t\\tYES/yes
\\t\\tNO/no (default)\\n\\n"
read -r "REMOVABLE_ARG" ; printf "\\n"
}
check_drive()
{
printf "\\n\\tPreparing drives...\\n"
check_drive_prompt
while [[ "${ENTIRE_DRIVE}" == "list" || \
"${ENTIRE_DRIVE}" == "LIST" || \
"${ENTIRE_DRIVE}" == "shortlist" || \
"${ENTIRE_DRIVE}" == "SHORTLIST" ]]
do
case "${ENTIRE_DRIVE}" in
list|LIST)
fdisk -l
check_drive_prompt
;;
shortlist|SHORTLIST)
fdisk -l | grep "Disk" -A 0
check_drive_prompt
;;
esac
done
printf "\\tVerifying entry...\\n"
if [[ "${ENTIRE_DRIVE}" == "/dev/nvme"* ]] ; then
if [[ "${INSTALL_TYPE}" != "UEFI" ]] ; then
printf "\\n\\tError: UEFI must be selected for NVMe.\\n"
exit 1
fi
if [[ "${ENTIRE_DRIVE}" == *"p"* ]] ; then
printf "\\n\\tError: NVMe Partition has been specified.\\n"
exit 1
fi
# Used to determine if we need `p` used in partitions or not
P="p"
elif [[ "${ENTIRE_DRIVE}" == *[0-9]* ]] ; then
printf "\\n\\tError: Partition has been specified.\\n"
exit 1
else
P=""
fi
if [[ "${ENTIRE_DRIVE}" == "/dev/"* && \
-b "${ENTIRE_DRIVE}" ]]
then
printf "\\tBlock device: %s is valid.\\n" "${ENTIRE_DRIVE}"
else
printf "\\n\\tError: Invalid block device: %s\\n" "${ENTIRE_DRIVE}"
exit 1
fi
if ! grep -e "${ENTIRE_DRIVE}" "/proc/mounts" >> /dev/null 2>&1 && \
! mount | grep "${ENTIRE_DRIVE}" >> /dev/null 2>&1
then
printf "\\tNo mount points found on: %s\\n" "${ENTIRE_DRIVE}"
else
printf "\\n\\tBlock device: %s is or has partitions mounted.
\\tVerifying device is not part of the root filesystem...\\n" \
"${ENTIRE_DRIVE}"
ROOT_BLOCK=$(findmnt -n -o "SOURCE" -c "/")
if [[ "${ROOT_BLOCK}" == *"${ENTIRE_DRIVE}"* ]] ; then
printf "\\n\\tError: Block device: %s may not be used for installation.\\n" \
"${ENTIRE_DRIVE}"
exit 1
else
printf "\\tAttempting to unmount partitions on: %s\\n" "${ENTIRE_DRIVE}"
mapfile -t PARTITIONS < <(lsblk -n -o MOUNTPOINTS "${ENTIRE_DRIVE}" | sed '/^$/d')
for i in "${PARTITIONS[@]}" ; do
umount "${i}" || \
{
printf "\\n\\tError: Failed to unmount: %s\\n" "${i}" ;
exit 1 ;
}
done
fi
fi
printf "\\n\\tDone.\\n"
# GRUB's `--removable` flag is only applicable to UEFI platforms
if [[ "${INSTALL_TYPE}" == "UEFI" ]] ; then
removable_prompt
if [[ "${REMOVABLE_ARG}" == "Y" || \
"${REMOVABLE_ARG}" == "YES" || \
"${REMOVABLE_ARG}" == "y" || \
"${REMOVABLE_ARG}" == "yes" ]]
then
REMOVABLE="TRUE"
printf "\\t%s: Removable media.
\\tWill pass \`--removable\` to UEFI GRUB installation.\\n" "${ENTIRE_DRIVE}"
elif [[ "${REMOVABLE_ARG}" == "N" || \
"${REMOVABLE_ARG}" == "NO" || \
"${REMOVABLE_ARG}" == "n" || \
"${REMOVABLE_ARG}" == "no" || \
"${REMOVABLE_ARG}" == "DEFAULT" || \
"${REMOVABLE_ARG}" == "default" || \
-z "${REMOVABLE_ARG}" ]]
then
REMOVABLE="FALSE"
printf "\\t%s: Persistent drive, non-removable.
\\tUsing standard UEFI GRUB installation.\\n" "${ENTIRE_DRIVE}"
else
printf "\\n\\tError: Invalid selection: %s\\n" \
"${REMOVABLE_ARG}"
exit 1
fi
else
REMOVABLE="FALSE"
printf "\\n\\tRemovable media not applicable.\\n"
fi
}
choose_filesystem()
{
printf "\\n\\tPlease select your choice of filesystem.
\\tFor NVMe and SSDs, F2FS may give best performance.\\n
\\tValid options (Enter for default):
\\t\\tBTRFS/btrfs
\\t\\tEXT4/ext4 (default for non-NVMe drives)
\\t\\tF2FS/f2fs (default for NVMe drives)
\\t\\tXFS/xfs\\n\\n"
read -r "FSTYPE_ARG"
if [[ "${FSTYPE_ARG}" == "BTRFS" || \
"${FSTYPE_ARG}" == "btrfs" ]]
then
FSTYPE="BTRFS"
elif [[ "${FSTYPE_ARG}" == "EXT4" || \
"${FSTYPE_ARG}" == "ext4" || \
"${FSTYPE_ARG}" == "DEFAULT" && \
"${ENTIRE_DRIVE}" != "/dev/nvme"* || \
"${FSTYPE_ARG}" == "default" && \
"${ENTIRE_DRIVE}" != "/dev/nvme"* || \
"${ENTIRE_DRIVE}" != "/dev/nvme"* && \
-z "${FSTYPE_ARG}" ]]
then
FSTYPE="EXT4"
elif [[ "${FSTYPE_ARG}" == "F2FS" || \
"${FSTYPE_ARG}" == "f2fs" || \
"${FSTYPE_ARG}" == "DEFAULT" && \
"${ENTIRE_DRIVE}" == "/dev/nvme"* || \
"${FSTYPE_ARG}" == "default" && \
"${ENTIRE_DRIVE}" == "/dev/nvme"* || \
"${ENTIRE_DRIVE}" == "/dev/nvme"* && \
-z "${FSTYPE_ARG}" ]]
then
FSTYPE="F2FS"
elif [[ "${FSTYPE_ARG}" == "XFS" || \
"${FSTYPE_ARG}" == "xfs" ]]
then
FSTYPE="XFS"
else
printf "\\n\\tError: Invalid selection: %s\\n" "${FSTYPE_ARG}"
exit 1
fi
printf "\\n\\tSelected filesystem: %s\\n" "${FSTYPE}"
}
partition_sizes()
{
# Use sane defaults for both legacy BIOS and UEFI
if [[ "${INSTALL_TYPE}" == "LEGACY" ]] ; then
BIOS_PART_SIZE="2M"
elif [[ "${INSTALL_TYPE}" == "UEFI" ]] ; then
EFI_PART_SIZE="100M"
fi
# Sane default for boot partition
BOOT_PART_SIZE="500M"
printf "\\n\\tPlease specify the size for the home partition ( /home )
\\tin gigabytes (GB.)\\n
\\tValue must be between 2 and 16000 (2 = 2GB, 16000 = 16TB)
\\tValue must be an exact integer.
\\tDo not specify a unit (i.e. m/M/MB g/G/GB t/T/TB)\\n\\n"
read -r "HOME_PART_SIZE"
if [[ "${HOME_PART_SIZE}" == [!0-9] || \
"${HOME_PART_SIZE}" == *[!0-9]* ]]
then
printf "\\n\\tError: Value must be an integer.\\n"
exit 1
elif [[ "${HOME_PART_SIZE}" -lt 2 || \
"${HOME_PART_SIZE}" -gt 16000 ]]
then
printf "\\n\\tError: Value: %s out of range.\\n" \
"${HOME_PART_SIZE}"
exit 1
fi
printf "\\n\\tPlease specify the size for the root partition ( / )
\\tin gigabytes (GB.)\\n
\\tValue must be between 10 and 16000 (10 = 10GB, 16000 = 16TB)
\\tValue must be an exact integer.
\\tDo not specify a unit (i.e. m/M/MB g/G/GB t/T/TB)\\n\\n"
read -r "ROOT_PART_SIZE"
if [[ "${ROOT_PART_SIZE}" == [!0-9] || \
"${ROOT_PART_SIZE}" == *[!0-9]* ]]
then
printf "\\n\\tError: Value must be an integer.\\n"
exit 1
elif [[ "${ROOT_PART_SIZE}" -lt 10 || \
"${ROOT_PART_SIZE}" -gt 16000 ]]
then
printf "\\n\\tError: Value: %s out of range.\\n" \
"${ROOT_PART_SIZE}"
exit 1
fi
}
wipe_drive()
{
printf "\\n\\tTarget installation media to erase: %s\\n
\\tAllowing 10 seconds to ensure correct device has been selected.
\\tPress Control+C to cancel.\\n
\\t10.\\n" "${ENTIRE_DRIVE}" ; sleep 1
printf "\\t9.\\n" ; sleep 1
printf "\\t8.\\n" ; sleep 1
printf "\\t7.\\n" ; sleep 1
printf "\\t6.\\n" ; sleep 1
printf "\\t5.\\n" ; sleep 1
printf "\\t4.\\n" ; sleep 1
printf "\\t3.\\n" ; sleep 1
printf "\\t2.\\n" ; sleep 1
printf "\\t1.\\n" ; sleep 1
printf "\\n\\tRemoving data on: %s\\n" "${ENTIRE_DRIVE}"
if_log wipefs -a -f "${ENTIRE_DRIVE}" ; printf "\\n"
if [[ "${ENTIRE_DRIVE}" == "/dev/nvme"* ]] ; then
if_log blkdiscard "${ENTIRE_DRIVE}"
else
if_log dd if=/dev/zero of="${ENTIRE_DRIVE}" bs=8M count=16 \
oflag=sync status=progress
fi
sleep 5 && sync
printf "\\tDone.\\n"
}
partition_drive()
{
if [[ "${INSTALL_TYPE}" == "LEGACY" ]] ; then
BIOS_GUID="21686148-6449-6E6F-744E-656564454649"
elif [[ "${INSTALL_TYPE}" == "UEFI" ]] ; then
EFI_GUID="C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
fi
BOOT_GUID="BC13C2FF-59E6-4262-A352-B275FD6F7172"
HOME_GUID="933AC7E1-2EB4-4F13-B844-0E14E2AEF915"
ROOT_GUID="4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709"
printf "\\n\\tPartitioning device: %s using the following layout:\\n\\n" \
"${ENTIRE_DRIVE}"
if [[ "${INSTALL_TYPE}" == "LEGACY" ]] ; then
printf "\\tBIOS boot partition: %s\\n" "${BIOS_PART_SIZE}"
elif [[ "${INSTALL_TYPE}" == "UEFI" ]] ; then
printf "\\tEFI system partition: %s\\n" "${EFI_PART_SIZE}"
fi
printf "\\tLinux extended boot partition ( /boot ): %s
\\tLinux home partition ( /home ): %sG ( %s )
\\tLinux root (x86-64) partition ( / ): %sG ( %s )\\n" \
"${BOOT_PART_SIZE}" "${HOME_PART_SIZE}" "${FSTYPE}" \
"${ROOT_PART_SIZE}" "${FSTYPE}"
sfdisk_die()
{
printf "\\n\\tError: Failed to partition: %s\\n" \
"${ENTIRE_DRIVE}" ;
exit 1 ;
}
if [[ "${INSTALL_TYPE}" == "LEGACY" ]] ; then
if_log sfdisk -w always -W always "${ENTIRE_DRIVE}" <<-EOF || \
sfdisk_die
label :gpt
,"${BIOS_PART_SIZE}","${BIOS_GUID}"
,"${BOOT_PART_SIZE}","${BOOT_GUID}"
,"${HOME_PART_SIZE}G","${HOME_GUID}"
,"${ROOT_PART_SIZE}G","${ROOT_GUID}"
EOF
elif [[ "${INSTALL_TYPE}" == "UEFI" ]] ; then
if_log sfdisk -w always -W always "${ENTIRE_DRIVE}" <<-EOF || \
sfdisk_die
label :gpt
,"${EFI_PART_SIZE}","${EFI_GUID}"
,"${BOOT_PART_SIZE}","${BOOT_GUID}"
,"${HOME_PART_SIZE}G","${HOME_GUID}"
,"${ROOT_PART_SIZE}G","${ROOT_GUID}"
EOF
fi
if [[ "${INSTALL_TYPE}" == "UEFI" ]] ; then
EFI_PART="${ENTIRE_DRIVE}${P}1"
fi
BOOT_PART="${ENTIRE_DRIVE}${P}2"
HOME_PART="${ENTIRE_DRIVE}${P}3"
ROOT_PART="${ENTIRE_DRIVE}${P}4"
# Required for properly parsing PARTUUIDs
sleep 5 ; sync
# This sometimes fails on Debian with SystemRescue plugged in
set +e
partprobe
set -e
sleep 5
# Make sure all PARTUUIDs are valid before proceeding
if [[ "${INSTALL_TYPE}" == "UEFI" ]] ; then
EFI_PARTUUID=$(lsblk -n -o PARTUUID "${EFI_PART}")
if [[ $(grep -o "-" <<< "${EFI_PARTUUID}" | wc -l) -ne 4 ]] ; then
printf "\\n\\tError: Invalid PARTUUID on: %s\\n" \
"${EFI_PART}"
exit 1
fi
fi
BOOT_PARTUUID=$(lsblk -n -o PARTUUID "${BOOT_PART}")
if [[ $(grep -o "-" <<< "${BOOT_PARTUUID}" | wc -l) -ne 4 ]] ; then
printf "\\n\\tError: Invalid PARTUUID on: %s\\n" \
"${BOOT_PART}"
exit 1
fi
HOME_PARTUUID=$(lsblk -n -o PARTUUID "${HOME_PART}")
if [[ $(grep -o "-" <<< "${HOME_PARTUUID}" | wc -l) -ne 4 ]] ; then
printf "\\n\\tError: Invalid PARTUUID on: %s\\n" \
"${HOME_PART}"
exit 1
fi
ROOT_PARTUUID=$(lsblk -n -o PARTUUID "${ROOT_PART}")
if [[ $(grep -o "-" <<< "${ROOT_PARTUUID}" | wc -l) -ne 4 ]] ; then
printf "\\n\\tError: Invalid PARTUUID on: %s\\n" \
"${ROOT_PART}"
exit 1
fi
printf "\\n\\tDone.\\n"
}
format_partitions()
{
printf "\\n\\tFormatting partitions...\\n\\n"
# Only if UEFI is enabled do we format the first partition
if [[ "${INSTALL_TYPE}" == "UEFI" ]] ; then
if_log mkfs.fat -F 32 "${EFI_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: FAT32\\n" \
"${BOOT_PART}" "${FSTYPE}" ;
exit 1 ;
}
fi
if [[ "${FSTYPE}" == "BTRFS" ]] ; then
if_log mkfs.btrfs "${BOOT_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${BOOT_PART}" "${FSTYPE}" ;
exit 1 ;
}
if_log mkfs.btrfs "${HOME_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${HOME_PART}" "${FSTYPE}" ;
exit 1 ;
}
if_log mkfs.btrfs "${ROOT_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${ROOT_PART}" "${FSTYPE}" ;
exit 1 ;
}
elif [[ "${FSTYPE}" == "EXT4" ]] ; then
if_log mkfs.ext4 "${BOOT_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${BOOT_PART}" "${FSTYPE}" ;
exit 1 ;
}
if_log mkfs.ext4 "${HOME_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${HOME_PART}" "${FSTYPE}" ;
exit 1 ;
}
if_log mkfs.ext4 "${ROOT_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${ROOT_PART}" "${FSTYPE}" ;
exit 1 ;
}
elif [[ "${FSTYPE}" == "F2FS" ]] ; then
F2FS_DEFAULTS="extra_attr,inode_checksum,sb_checksum"
printf "\\tF2FS selected. Using safe defaults...\\n"
# F2FS xattr currently not supported in GRUB, exclude for /boot
# (no compression)
if_log mkfs.f2fs "${BOOT_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${BOOT_PART}" "${FSTYPE}" ;
exit 1 ;
}
if_log mkfs.f2fs -O "${F2FS_DEFAULTS}" "${HOME_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${HOME_PART}" "${FSTYPE}" ;
exit 1 ;
}
if_log mkfs.f2fs -O "${F2FS_DEFAULTS}" "${ROOT_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${ROOT_PART}" "${FSTYPE}" ;
exit 1 ;
}
elif [[ "${FSTYPE}" == "XFS" ]] ; then
if_log mkfs.xfs "${BOOT_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${BOOT_PART}" "${FSTYPE}" ;
exit 1 ;
}
if_log mkfs.xfs "${HOME_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${HOME_PART}" "${FSTYPE}" ;
exit 1 ;
}
if_log mkfs.xfs "${ROOT_PART}" || \
{
printf "\\n\\tError: Failed to format: %s as: %s\\n" \
"${ROOT_PART}" "${FSTYPE}" ;
exit 1 ;
}
fi
printf "\\tDone.\\n"
}
mount_init_filesystems()
{
printf "\\n\\tPreparing for installation...\\n"
printf "\\tEnsuring directory: %s does not exist...\\n" "${ROOT_MOUNT}"
if [[ ! -d "${ROOT_MOUNT}" ]] ; then
printf "\\tGood.\\n"
else
printf "\\n\\tError: Found: %s\\n" "${ROOT_MOUNT}"
exit 1
fi
printf "\\tEnsuring directory: %s is unmounted...\\n" "${ROOT_MOUNT}"
if ! grep -e "${ROOT_MOUNT}" "/proc/mounts" >> /dev/null 2>&1 && \
! mount | grep "${ROOT_MOUNT}" >> /dev/null 2>&1
then
printf "\\tNo mount points found on: %s\\n" "${ROOT_MOUNT}"
else
printf "\\n\\tError: %s is mounted.\\n" "${ROOT_MOUNT}"
exit 1
fi
# Some distros are OCD about mounting everything ASAP because ADHD
printf "\\tEnsuring target media is still unmounted...\\n"
if ! grep -e "${ENTIRE_DRIVE}" "/proc/mounts" >> /dev/null 2>&1 && \
! mount | grep "${ENTIRE_DRIVE}" >> /dev/null 2>&1
then
printf "\\tNo mount points found on: %s\\n" "${ENTIRE_DRIVE}"
else
printf "\\n\\tError: %s is or has partitions mounted.\\n" \
"${ENTIRE_DRIVE}"
exit 1
fi
mkdir -p "${ROOT_MOUNT}" || \
{
printf "\\n\\tError: Failed to create: %s\\n" "${ROOT_MOUNT}" ;
exit 1 ;
}
printf "\\tMounting root filesystem...\\n"
mount "${ROOT_PART}" "${ROOT_MOUNT}" || \
{
printf "\\n\\tError: Failed to mount: %s to: %s\\n" \
"${ROOT_PART}" "${ROOT_MOUNT}" ;
exit 1 ;
}
printf "\\tMounting boot filesystem...\\n"
mkdir -p "${ROOT_MOUNT}/boot" || \
{
printf "\\n\\tError: Failed to create: %s\\n" \
"${ROOT_MOUNT}/boot" ;
exit 1 ;
}
mount "${BOOT_PART}" "${ROOT_MOUNT}/boot" || \
{
printf "\\n\\tError: Failed to mount: %s to: %s\\n" \
"${BOOT_PART}" "${ROOT_MOUNT}/boot" ;
exit 1 ;