-
Notifications
You must be signed in to change notification settings - Fork 0
/
fde-install
executable file
·1044 lines (956 loc) · 32.7 KB
/
fde-install
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 -u
# fde-packages
export PACKAGELIST="base \
linux \
linux-firmware \
linux-headers \
neovim \
git \
curl \
intel-ucode \
lvm2 \
grub \
networkmanager \
network-manager-applet \
dialog \
wpa_supplicant \
mtools \
dosfstools \
reflector \
base-devel \
linux-headers \
avahi \
xdg-user-dirs \
xdg-utils \
gvfs \
gvfs-smb \
nfs-utils \
inetutils \
dnsutils \
bluez \
bluez-utils \
cups \
hplip \
alsa-utils \
bash-completion \
openssh \
rsync \
acpi \
acpi_call \
virt-manager \
qemu \
qemu-arch-extra \
edk2-ovmf \
bridge-utils \
dnsmasq \
vde2 \
openbsd-netcat \
iptables-nft \
ipset \
sof-firmware \
nss-mdns \
acpid \
ntfs-3g"
# fde-blank
export DEV=""
export CRYPTNAME="arch-crypt"
export VGNAME="system"
export LVROOTNAME="root"
export LVROOTSIZE="20G"
export WANTSHOME=1
export LVHOMENAME="home"
export LVHOMESIZE="100"
export LVSWAPNAME="swap"
export LVSWAPSIZE="2G"
export EFISIZE="1G"
export TARGETMOUNTPOINT="/mnt/system"
export NEWHOSTNAME="arch"
export ROOTPASS=
export WANTSSDGRUB=
export WANTKEY=
export KEYDEV=""
export KEYFORMAT=""
export UKEYNAME="arch-boot"
export KEYNAME="arch-boot.key"
export KEYMOUNT="/tmp/stick"
export KEYDIR="/keys"
export KEYPATH=""
export ERASEALL=
export OVERWRITEINSTALL=
export SYSCHANGE=
export KEYCHANGE=
export MULTILIB=
export PASSPHRASE=
# Do not touch these variables
export GREPTEST=" \|\!\|@\|#\|%\|\^\|&\|*\|(\|)\|{\|}\|<\|>\|?\|;\|:\|'\||\|\"\|=\|+\|~\|\`"
export FDECONFIG="fde-config"
export BLANKCONFIG="fde-blank"
# Detect UEFI or MBR
if [ -d /sys/firmware/efi ]; then
export ISEFI=1
export EFIPARTITION=1
export CRYPTPARTITION=2
else
export ISEFI=0
export CRYPTPARTITION=1
fi
# Do not touch this parameter, unless you understand what you are doing.
# This is a paameter value of the --iter-time option for cyrptsetup command.
# If you specify 1000, that means 1000mSec. 0 means compile default.
export ITERTIME=0
# Colors for status/error messages
export RED="\e[0;31m"
export GREEN="\e[0;32m"
export END="\e[0m"
# fde-functions
function format_dev() {
if [[ "${ERASEALL}" -eq 1 ]] ; then
# UEFI
if [[ "${ISEFI}" -eq 1 ]] ; then
send_prog "Initializing ${DEV} with GPT"
sgdisk --zap-all "${DEV}"
if is_error ; then
send_prog "Retrying initialization"
sgdisk --zap-all "${DEV}"
if is_error ; then return 1 ; fi
fi
send_prog "Creating an EFI partition on ${DEV}"
sgdisk --new="${EFIPARTITION}":0:+"${EFISIZE}" --change-name="${EFIPARTITION}":"EFI System" --typecode="${EFIPARTITION}":ef00 "${DEV}"
if is_error ; then return 1 ; fi
send_prog "Creating boot filesystem"
if ! mkfs.vfat -n boot "${DEV}${EFIPARTITION}" ; then send_error "Making boot filesystem" ; return 1 ; fi
send_prog "Creating a Linux partition on ${DEV}"
sgdisk --new="${CRYPTPARTITION}":0:0 --change-name="${CRYPTPARTITION}":"Linux LUKS" --typecode="${CRYPTPARTITION}":8309 "${DEV}"
if is_error ; then return 1 ; fi
sgdisk --print "${DEV}"
else
send_prog "Erasing partition table of ${DEV}"
dd if=/dev/zero of="${DEV}" bs=512 count=1
if is_error ; then return 1 ; fi
send_prog "Creating a Linux partition on ${DEV} with MBR"
sfdisk "${DEV}" <<- HEREDOC
2M,,L
HEREDOC
if is_error ; then return 1 ; fi
fi
fi
}
function encrypt_dev() {
send_prog "Initializing ${DEV}${CRYPTPARTITION} as crypt partition"
if [[ "${ISEFI}" -eq 1 ]]; then
printf %s "${PASSPHRASE}" | cryptsetup luksFormat --iter-time "${ITERTIME}" --key-file - --batch-mode "${DEV}${CRYPTPARTITION}" >/dev/null 2>&1
else
printf %s "${PASSPHRASE}" | cryptsetup luksFormat --iter-time "${ITERTIME}" --type=luks1 --key-file - --batch-mode "${DEV}${CRYPTPARTITION}" >/dev/null 2>&1
fi
send_prog "Opening crypt partition ${DEV}${CRYPTPARTITION} as ${CRYPTNAME}"
printf %s "${PASSPHRASE}" | cryptsetup open -d - "${DEV}${CRYPTPARTITION}" "${CRYPTNAME}" >/dev/null 2>&1
send_prog "Creating physical volume on ${CRYPTNAME}"
if ! pvcreate "/dev/mapper/${CRYPTNAME}" >/dev/null 2>&1; then return 1 ; fi
send_prog "Creating volume group ${VGNAME}"
if ! vgcreate "${VGNAME}" "/dev/mapper/${CRYPTNAME}" >/dev/null 2>&1; then return 1 ; fi
if [[ "${WANTSHOME}" -eq 1 ]]; then
send_prog "Creating logical volume for root : ${LVROOTNAME}"
if ! lvcreate -n "${LVROOTNAME}" -L "${LVROOTSIZE}" "${VGNAME}" >/dev/null 2>&1; then return 1 ; fi
send_prog "Creating logical volume for swap : ${LVSWAPNAME}"
if ! lvcreate -n "${LVSWAPNAME}" -L "${LVSWAPSIZE}" "${VGNAME}" >/dev/null 2>&1; then return 1 ; fi
send_prog "Creating logical volume for home : ${LVHOMENAME}"
if ! lvcreate -n "${LVHOMENAME}" -l "${LVHOMESIZE}%FREE" "${VGNAME}" >/dev/null 2>&1; then return 1 ; fi
else
send_prog "Creating logical volume for swap : ${LVSWAPNAME}"
if ! lvcreate -n "${LVSWAPNAME}" -L "${LVSWAPSIZE}" "${VGNAME}" >/dev/null 2>&1; then return 1 ; fi
send_prog "Creating logical volume for root : ${LVROOTNAME}"
if ! lvcreate -n "${LVROOTNAME}" -l "${LVROOTSIZE}%FREE" "${VGNAME}" >/dev/null 2>&1; then return 1 ; fi
fi
}
function reformat_key() {
if [[ "${KEYFORMAT}" -eq 1 ]]; then
send_prog "Reformating ${KEYDEV}"
if ! mkfs.vfat -F 32 -n "BOOT_KEY" "${KEYDEV}" >/dev/null 2>&1; then
send_error "Reformatting ${KEYDEV} to ${KEYFSTYPE}" ; return 1
fi
elif [[ "${KEYFORMAT}" =~ ^[2-4]$ ]]; then
send_prog "Reformating ${KEYDEV} to ${KEYFSTYPE}"
if ! mkfs."${KEYFSTYPE}" -L "BOOT_KEY" "${KEYFLAG}" "${KEYDEV}" >/dev/null 2>&1; then
send_error "Reformating ${KEYDEV} to ${KEYFSTYPE}" ; return 1
fi
else
send_error "With key format variable : ${KEYFORMAT}"
fi
}
function luks_add_keyfile() {
if [[ ! -d "${KEYMOUNT}" ]] && [[ "${WANTKEY}" -eq 1 ]] ; then
send_prog "Making direcotry ${KEYMOUNT}"
mkdir -p "${KEYMOUNT}"
fi
send_prog "Mounting ${KEYDEV} at ${KEYMOUNT}"
if ! mount "${KEYDEV}" "${KEYMOUNT}" >/dev/null 2>&1; then send_error "Mounting ${KEYDEV} at ${KEYMOUNT}" ; return 1 ; fi
if [[ ! -d "${KEYMOUNT}/${KEYDIR}" ]]; then
send_prog "Creating directory ${KEYDIR} on ${KEYDEV}"
mkdir -p "${KEYMOUNT}/${KEYDIR}"
else
send_prog "${KEYDIR} already exists on ${KEYDEV} checking for keyfile"
fi
if [[ ! -e "${KEYMOUNT}"/"${KEYPATH}" ]]; then
send_prog "Creating keyfile ${KEYNAME} on ${KEYDEV}"
if ! dd bs=512 count=4 if=/dev/urandom of="${KEYMOUNT}/${KEYPATH}" iflag=fullblock ; then
send_error "Creating keyfile : ${KEYNAME}"
return 1
fi
else
send_prog "${KEYNAME} already exists, skipping"
fi
if ! chmod u=rx,go-rwx "${KEYMOUNT}/${KEYDIR}" ; then send_error "Chmoding ${KEYMOUNT}/${KEYDIR}" ; return 1 ; fi
if ! chmod u=r,go-rwx "${KEYMOUNT}""${KEYPATH}" ; then send_error "Chmoding ${KEYMOUNT}/${KEYPATH}" ; return 1 ; fi
send_prog "Adding keyfile, ${KEYNAME}, to ${DEV}${CRYPTPARTITION}"
printf %s "${PASSPHRASE}" | cryptsetup luksAddKey --iter-time "${ITERTIME}" -d - "${DEV}${CRYPTPARTITION}" "${KEYMOUNT}""${KEYPATH}"
send_prog "Unmounting ${KEYDEV}"
if ! umount -f "${KEYMOUNT}" ; then send_error "Unmounting ${KEYDEV} at ${KEYMOUNT}" ; return 1 ; fi
}
function make_filesystem() {
send_prog "Creating root filesystem"
if ! mkfs.ext4 -L root "/dev/${VGNAME}/${LVROOTNAME}" >/dev/null 2>&1; then send_error "Making root filesystem" ; return 1 ; fi
send_prog "Creating swap"
if ! mkswap -L swap "/dev/${VGNAME}/${LVSWAPNAME}" >/dev/null 2>&1; then send_error "Creating swap" ; return 1 ; fi
if [[ "${WANTSHOME}" -eq 1 ]]; then
send_prog "Creating home filesystem"
if ! mkfs.ext4 -L home "/dev/${VGNAME}/${LVHOMENAME}" >/dev/null 2>&1; then send_error "Making home filesystem" ; return 1 ; fi
fi
}
function mount_filesystem() {
send_prog "Mounting root file system"
if ! mount "/dev/${VGNAME}/${LVROOTNAME}" "${TARGETMOUNTPOINT}" ; then return 1 ; fi
if [[ "${WANTSHOME}" -eq 1 ]]; then
send_prog "Creating /home"
if ! mkdir -p "${TARGETMOUNTPOINT}/home" ; then return 1 ; fi
send_prog "Mounting /home"
if ! mount "/dev/${VGNAME}/${LVHOMENAME}" "${TARGETMOUNTPOINT}/home" ; then return 1 ; fi
fi
if [[ "${ISEFI}" -eq 1 ]]; then
send_prog "Creating /boot"
if ! mkdir -p "${TARGETMOUNTPOINT}/boot" ; then return 1 ; fi
send_prog "Mounting /boot"
if ! mount "${DEV}${EFIPARTITION}" "${TARGETMOUNTPOINT}/boot" ; then return 1 ; fi
fi
send_prog "Turning swap on"
if ! swapon "/dev/${VGNAME}/${LVSWAPNAME}" ; then return 1 ; fi
}
function install_base_system() {
send_prog "Enabling parallel downloads"
sed -i 's/^#ParallelDownloads/ParallelDownloads/' "/etc/pacman.conf"
send_prog "Sorting pacman mirrors via reflector"
reflector --latest 5 --country "United States" --sort rate --save /etc/pacman.d/mirrorlist --protocol https --download-timeout 15 >/dev/null 2>&1
send_prog "Installing system to ${DEV}${CRYPTPARTITION}"
if [[ "${ISEFI}" -eq 1 ]]; then
pacstrap "${TARGETMOUNTPOINT}" efibootmgr ${PACKAGELIST}
else
pacstrap "${TARGETMOUNTPOINT}" ${PACKAGELIST}
fi
send_prog "Generating fstab"
genfstab -U "${TARGETMOUNTPOINT}" >> "${TARGETMOUNTPOINT}/etc/fstab"
}
function enable_multilib() {
if [[ $MULTILIB == "1" ]]; then
send_prog "Enabling multilib repository"
cp "${TARGETMOUNTPOINT}/etc/pacman.conf" "${TARGETMOUNTPOINT}/etc/pacman.conf.backup"
sed -i -e 's/\#\[multilib\]/\[multilib\]/' -e '/^\[multilib\]/,+1 s/\#//' "${TARGETMOUNTPOINT}/etc/pacman.conf"
return 0
else
send_prog "Multilib will not be enabled"
return 0
fi
}
function chroot_prep() {
send_prog "Setting locale"
sed -i 's/#en_US.UTF-8/en_US.UTF-8/' "${TARGETMOUNTPOINT}/etc/locale.gen"
echo "LANG=en_US.UTF-8" > "${TARGETMOUNTPOINT}/etc/locale.conf"
send_prog "Setting system hostname to ${NEWHOSTNAME}"
echo "${NEWHOSTNAME}" > "${TARGETMOUNTPOINT}/etc/hostname"
if ! grep "127.0.0.1" "${TARGETMOUNTPOINT}/etc/hosts" ; then
send_prog "Configuring hosts file"
echo "127.0.0.1 localhost" >> "${TARGETMOUNTPOINT}/etc/hosts"
echo "::1 localhost" >> "${TARGETMOUNTPOINT}/etc/hosts"
echo "127.0.1.1 ${NEWHOSTNAME}.localdomain ${NEWHOSTNAME}" >> "${TARGETMOUNTPOINT}/etc/hosts"
fi
send_prog "Configuring pacman mirrorlist on ${DEV}"
echo -e "--sort rate\n--latest 5\n--country \"United States\",Canada\n--save /etc/pacman.d/mirrorlist\n--protocol https\n--download-timeout 15\n--connection-timeout 15" > "${TARGETMOUNTPOINT}/etc/xdg/reflector/reflector.conf"
}
function configure_grub_and_hooks() {
if [[ "$WANTKEY" -eq 1 ]]; then
export CRYPTKEYGRUB=" cryptkey=UUID=$(lsblk -dno UUID ${KEYDEV}):$(lsblk -dno FSTYPE ${KEYDEV}):${KEYPATH}"
sed -i -e "/^MODULES=()$/c MODULES=($(lsblk -dno FSTYPE ${KEYDEV}))" "${TARGETMOUNTPOINT}/etc/mkinitcpio.conf"
else
export CRYPTKEYGRUB=""
fi
send_prog "Adding ${DEV}${CRYPTPARTITION} uuid to /etc/default/grub"
sed -i -e "/GRUB_CMDLINE_LINUX=/c GRUB_CMDLINE_LINUX=\"cryptdevice=UUID=$(lsblk -dno UUID ${DEV}${CRYPTPARTITION}):${CRYPTNAME}${SSDGRUB} root=/dev/mapper/${VGNAME}-${LVROOTNAME}${CRYPTKEYGRUB}\"" "${TARGETMOUNTPOINT}/etc/default/grub"
send_prog "Adding required hooks and modules to /etc/mkinitcpio.conf"
sed -i -e '/^HOOKS=/c HOOKS=(base udev autodetect keyboard keymap modconf block encrypt lvm2 filesystems fsck)' "${TARGETMOUNTPOINT}/etc/mkinitcpio.conf"
if [[ "${ISEFI}" -eq 0 ]]; then
send_prog "Enabling grub cryptodisk setting"
sed -i '/^#GRUB_ENABLE_CRYPTODISK=y$/c GRUB_ENABLE_CRYPTODISK=y' "${TARGETMOUNTPOINT}/etc/default/grub"
fi
}
function chroot_config() {
send_prog "Chrooting into ${TARGETMOUNTPOINT}"
cat <<- HEREDOC | arch-chroot "${TARGETMOUNTPOINT}"
ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
systemctl enable NetworkManager
systemctl enable cups.service
systemctl enable avahi-daemon
systemctl enable reflector.service
systemctl enable reflector.timer
systemctl enable acpid
hwclock --systohc
locale-gen
echo "root:${ROOTPASS}" | chpasswd
HEREDOC
if ! configure_grub_and_hooks ; then
send_error "Failed to configure grub and mkinitcpo hooks"
return 1
fi
if [[ "${ISEFI}" -eq 1 ]]; then
send_prog "Installing GRUB"
cat <<- HEREDOC | arch-chroot "${TARGETMOUNTPOINT}"
mkinitcpio -p linux >/dev/null 2>&1
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id="ARCH_GRUB"
grub-mkconfig -o /boot/grub/grub.cfg
HEREDOC
else
send_prog "Installing GRUB"
cat <<- HEREDOC | arch-chroot ${TARGETMOUNTPOINT}
mkinitcpio -p linux >/dev/null 2>&1
grub-install --target=i386-pc --bootloader-id="ARCH_GRUB" "${DEV}"
grub-mkconfig -o /boot/grub/grub.cfg
HEREDOC
fi
}
function is_error() {
if [[ $? -eq 0 ]] ; then
return 1
else
term_message
return 0
fi;
}
function send_prog() {
echo -e "${GREEN}...${1}${END}"
}
function send_error() {
echo -e "${RED}***** ERROR : ${1} *****${END}"
}
function term_message() {
echo -e "${RED}***** Installation process terminated *****${END}"
}
function remove_key() {
if ! mount "${KEYDEV}" "${KEYMOUNT}" ; then
send_error "Failed to mount ${KEYDEV} at ${KEYMOUNT}"
fi
if ! rm -f -- "${KEYMOUNT}"/"${KEYPATH}" ; then
send_error "Failed to remove keyfile at ${KEYMOUNT}${KEYPATH}"
fi
if ! umount "${KEYMOUNT}" ; then return 1 ; fi
}
function close_and_unmount() {
echo -e "${RED}***** CLOSING AND UNMOUNTING FILE SYSTEM *****${END}"
umount "${TARGETMOUNTPOINT}/home"
umount "${TARGETMOUNTPOINT}/boot"
swapoff "/dev/mapper/${VGNAME}-${LVSWAPNAME}"
umount "${TARGETMOUNTPOINT}"
umount "${KEYMOUNT}"
vgchange -an
cryptsetup close "${CRYPTNAME}"
}
function usage() {
cat <<EOF
Usage : fde-install [options]
Options:
-c [file] Uses a config file instead of the user being queried
for script options. If no config file is provided
then ${FDECONFIG} will be used if present in the
working directory. Any var not set or set improperly
will result in the user being queried for said var.
--no-home A logical volume for /home will be not be created
during the installation process.
-h, --help Show this help message and exit.
EOF
}
function get_opts() {
while : ; do
if [[ $# -ge 1 ]]; then
case $1 in
-h | --help) usage ; return 1 ; break ;;
--no-home) export WANTSHOME=0
export ROOTSIZE=100
shift ;;
-c) shift
if [[ $# -ge 1 ]] && [[ "$1" != -* ]]; then
FDECONFIG="$1"
if [[ -e "${FDECONFIG}" ]]; then
source "${FDECONFIG}"
export CONFIGUSED=1
shift
else
echo -e "${RED}Config file does not exist : ${FDECONFIG}${END}"
return 1
fi
elif [[ $# -ge 1 ]] && [[ "$1" == -* ]]; then
if [[ -e "${FDECONFIG}" ]]; then
source "${FDECONFIG}"
export CONFIGUSED=1
continue
fi
else
if [[ -e "${FDECONFIG}" ]]; then
source "${FDECONFIG}"
export CONFIGUSED=1
break
else
echo -e "${RED}Config file does not exist : ${FDECONFIG}${END}"
return 1
break
fi
fi
;;
-*) echo -e "${RED}Unrecognized option: ${END}${1}" >&2
usage
return 1
break
;;
*)
break ;;
esac
else
break
fi
done
}
# fde-prompts
function system_prompts() {
# ERASEALL prompt
while [[ -z "${ERASEALL}" ]] || [[ ! "${ERASEALL}" =~ ^[0-1]$ ]]; do
clear
echo -e "${RED}\
This script will erase all the data on the install target${END}
0 : Do not proceed, I don't want to erase anything
1 : Yes, I want to erase the contents of my device"
read -rp "Proceed? " ERASEALL
export ERASEALL
done
if [[ "${ERASEALL}" -eq 0 ]]; then return 1 ; fi
# LUKS passphrase prompt
while [[ -z "${PASSPHRASE}" ]]; do
clear
echo -e "${GREEN}Type LUKS encryption passprhase${END}"
read -sr PASSPHRASE
export PASSPHRASE
echo -e "${GREEN}Type LUKS passprhase again${END}"
read -sr PASSPHRASE_C
export PASSPHRASE_C
while [[ "${PASSPHRASE}" != "${PASSPHRASE_C}" ]]; do
clear
echo -e "${RED}Passphrases did not match, try again${END}"
read -sr PASSPHRASE
export PASSPHRASE
echo -e "${GREEN}Type passprhase again${END}"
read -sr PASSPHRASE_C
export PASSPHRASE_C
done
done
# ROOTPASS prompt
while [[ -z "${ROOTPASS}" ]] ; do
clear
echo -e "${GREEN}Please specify your root password for your new system${END}"
read -sr ROOTPASS
export ROOTPASS
echo -e "${GREEN}Type password again${END}"
read -sr ROOTPASS_C
export ROOTPASS_C
while [[ "${ROOTPASS}" != "${ROOTPASS_C}" ]]; do
clear
echo -e "${RED}Passphrases did not match, try again${END}"
read -sr ROOTPASS
export ROOTPASS
echo -e "${GREEN}Type passprhase again${END}"
read -sr ROOTPASS_C
export ROOTPASS_C
done
done
# DEV prompt
while [[ -z "${DEV}" ]] || [[ ! -e "${DEV}" ]] || ! echo "${DEV}" | grep -q -v "[0-9]$" ; do
clear
lsblk
echo -e "${GREEN}\
Please choose a target for installation${END}
i.e. ${GREEN}/dev/sda${END} and not ${RED}/dev/sda1${END}"
read -rp "Use dev: " DEV
export DEV
done
# TARGETMOUNTPOINT prompt
while [[ -z "${TARGETMOUNTPOINT}" ]] || ! echo "${TARGETMOUNTPOINT}" | grep -q "^/" || grep -sq " ${TARGETMOUNTPOINT}" /proc/mounts || echo "${TARGETMOUNTPOINT}" | grep -q "${GREPTEST}"; do
clear
if [[ -n "${TARGETMOUNTPOINT}" ]] && grep -sq " ${TARGETMOUNTPOINT}" /proc/mounts ; then
echo -e "${RED}The given directory is occupied. Please select a free mount point.${END}"
fi
echo -e "${GREEN}\
Please specify a mount point for /dev/${VGNAME}/${LVROOTNAME}${END}
If the directory does not exist, it will be created"
read -rp "Target mount point: " TARGETMOUNTPOINT
export TARGETMOUNTPOINT
done
# CRYPTNAME prompt
while [[ -z "${CRYPTNAME}" ]] || echo "${CRYPTNAME}" | grep -q "${GREPTEST}\|/" ; do
clear
echo -e "${GREEN}\
Please choose a sane name for your encrypted drive i.e. arch-crypt${END}
Do not include spaces, or special characters"
read -rp "Crypt name: " CRYPTNAME
export CRYPTNAME
done
# NEWHOSTNAME prompt
while [[ -z "${NEWHOSTNAME}" ]] || echo "${NEWHOSTNAME}" | grep -q "${GREPTEST}\|/"; do
clear
echo -e "${GREEN}\
Please specify the hostname of the new system${END}
Do not include spaces, or special characters"
read -rp "New hostname: " NEWHOSTNAME
export NEWHOSTNAME
done
# VGNAME prompt
while [[ -z "${VGNAME}" ]] || echo "${VGNAME}" | grep -q "${GREPTEST}\|-\|/" -i ; do
clear
echo -e "${GREEN}\
Please choose a volume group name${END}
Do not include spaces, special characters, or a minus"
read -rp "Volume group name: " VGNAME
export VGNAME
done
# WANTSHOME prompt
while [[ -z "${WANTSHOME}" ]] || [[ ! "${WANTSHOME}" =~ ^[0-1]$ ]] ; do
clear
echo -e "${GREEN}\
Would you like to create a seprate logical volume for home?${END}
0 : Do not create a separate LV for home
1 : Create a separate LV for home"
read -rp "Create LV for home? " WANTSHOME
export WANTSHOME
done
# LVROOTNAME prompt
while [[ -z "${LVROOTNAME}" ]] || echo "${LVROOTNAME}" | grep -q "${GREPTEST}\|-\|/" -i ; do
clear
echo -e "${GREEN}\
Please choose a root name${END}
Do not include spaces, special characters, or a minus"
read -rp "Root name: " LVROOTNAME
export LVROOTNAME
done
if [[ "${WANTSHOME}" -eq 1 ]]; then
# LVROOTSIZE prompt
while [[ -z "${LVROOTSIZE}" ]] || ! echo "${LVROOTSIZE}" | grep -E '^[0-9]+[G|T]$' | grep -q -v "${GREPTEST}\|-\|/" ; do
clear
echo -e "${GREEN}\
Please specify the size of your root volume i.e. 10G${END}
Must end with a \"${RED}G${END}\", or \"${RED}T${END}\""
read -rp "Root size: " LVROOTSIZE
export LVROOTSIZE
done
else
# LVROOTSIZE prompt
while [[ -z "${LVROOTSIZE}" ]] || ! echo "${LVROOTSIZE}" | grep -E '^100$|^[1-9][0-9]$|^[1-9]$' ; do
clear
echo -e "${GREEN}\
Please specify the size of your root volume between 1 and 100${END}
enter 100 to use 100% of the remaining storage on ${DEV}${CRYPTPARTITION}
Do not include the \"${RED}%${END}\""
read -rp "Root size: " LVROOTSIZE
export LVROOTSIZE
done
fi
# LVSWAPNAME prompt
while [[ -z "${LVSWAPNAME}" ]] || echo "${LVSWAPNAME}" | grep -q "${GREPTEST}\|-\|/" -i ; do
clear
echo -e "${GREEN}\
Please choose a swap name${END}
Do not include spaces, special characters, or a minus"
read -rp "Swap name: " LVSWAPNAME
export LVSWAPNAME
done
# LVSWAPSIZE prompt
while [[ -z "${LVSWAPSIZE}" ]] || ! echo "${LVSWAPSIZE}" | grep -E '^[0-9]+[M|G]$' | grep -q -v "${GREPTEST}\|-\|/" ; do
clear
echo -e "${GREEN}\
Please specify the size of your swap volume i.e. 2G${END}
Do not include a \"${RED}+${END}\"
Must end with an \"${RED}M${END}\", or \"${RED}G${END}\""
read -rp "Swap size: " LVSWAPSIZE
export LVSWAPSIZE
done
if [[ "${WANTSHOME}" -eq 1 ]]; then
# LVHOMENAME prompt
while [[ -z "${LVHOMENAME}" ]] || echo "${LVHOMENAME}" | grep -q "${GREPTEST}\|-\|/" -i ; do
clear
echo -e "${GREEN}\
Please choose a home name${END}
Do not include spaces, special characters, or a minus"
read -rp "Home name: " LVHOMENAME
export LVHOMENAME
done
# LVHOMESIZE prompt
while [[ -z "${LVHOMESIZE}" ]] || ! echo "${LVHOMESIZE}" | grep -E '^100$|^[1-9][0-9]$|^[1-9]$' ; do
clear
echo -e "${GREEN}\
Please specify the size of your home volume between 1 and 100${END}
enter 100 to use 100% of the remaining storage on ${DEV}${CRYPTPARTITION}
Do not include the \"${RED}%${END}\""
read -rp "Home size: " LVHOMESIZE
export LVHOMESIZE
done
fi
# EFISIZE prompt
if [[ "${ISEFI}" -eq 1 ]]; then
while [[ -z "${EFISIZE}" ]] || ! echo "${EFISIZE}" | grep -E '^[0-9]+[M|G]$' | grep -q -v "${GREPTEST}\|/\|^0[M\|G]$" ; do
clear
echo -e "${GREEN}\
Please specify the size of your EFI partition${END}
Do not include a \"${RED}+${END}\"
Must end with an \"${RED}M${END}\", or \"${RED}G${END}\""
read -rp "EFI size: " EFISIZE
export EFISIZE
done
fi
# MULTILIB prompt
while [[ -z "${MULTILIB}" ]] || [[ ! "${MULTILIB}" =~ ^[0-1]$ ]] ; do
clear
echo -e "${GREEN}\
Would you like to enable the multilib repository?${END}
0 : Do not enable the multilib repo
1 : Enable the multilib repo"
read -rp "Enable multilib? " MULTILIB
export MULTILIB
done
# SSDGRUB prompt
while [[ -z "${WANTSSDGRUB}" ]] || [[ ! "${WANTSSDGRUB}" =~ ^[0-1]$ ]] && lsblk --discard "${DEV}" | grep -q "[1-9]" ; do
clear
echo -e "${RED}\
***** PLEASE MAKE SURE YOU UNDERSTAND THE *****
***** PROS AND CONS OF ENABLING DISCARDS *****${END}
${GREEN}${DEV} supports TRIM, do you want to enable discards?${END}
0 : do not enable discards
1 : enable discards"
read -rp "Enable discards? " WANTSSDGRUB
export WANTSSDGRUB
done
if [[ "${WANTSSDGRUB}" -eq 1 ]]; then
export SSDGRUB=":allow-discards"
else
export SSDGRUB=""
fi
}
function key_prompts() {
# WANTKEY prompt
while [[ ! "${WANTKEY}" =~ ^[0-1]$ ]]; do
clear
echo -e "${GREEN}\
Would you like to use a keyfile on an external drive to unlock LUKS at boot?${END}
0 : Do not create a keyfile
1 : I want a keyfile and I have an external drive to put it on"
read -rp "Add keyfile?: " WANTKEY
export WANTKEY
done
# Key prompts if user wants a keyfile
if [[ "${WANTKEY}" -eq 0 ]]; then export KEYCHANGE=0 ; return 1 ; fi
# KEYDEV prompt
while [[ ! -e "${KEYDEV}" ]] || [[ -z "${KEYDEV}" ]] || echo "${KEYDEV}" | grep -q "${DEV}" || ! echo "${KEYDEV}" | grep -q -E ".*[0-9]+$" || echo "${KEYDEV}" | grep -q "${GREPTEST}"; do
clear
lsblk
echo -e "${GREEN}\
Please choose a device to store your keyfile on${END}
i.e. /dev/sdb1
Choose an external drive and not ${DEV}"
read -rp "Select device: " KEYDEV
export KEYDEV
done
# KEYNAME prompt
while [[ -z "${UKEYNAME}" ]] || echo "${UKEYNAME}" | grep -q "${GREPTEST}\|\.key\|/" || [[ -z "${KEYNAME}" ]]; do
clear
echo -e "${GREEN}Please enter a reasonable name for your keyfile${END}\nDo not include the \"${RED}.key${END}\" extension in the name."
read -rp "Keyname: " UKEYNAME
export KEYNAME="${UKEYNAME}.key"
done
# KEYMOUNT prompt
while [[ -z "${KEYMOUNT}" ]] || grep -sq " ${KEYMOUNT}" /proc/mounts || ! echo "${KEYMOUNT}" | grep -q "^/" || [[ "$TARGETMOUNTPOINT" == "$KEYMOUNT" ]] || echo "${KEYMOUNT}" | grep -q "${GREPTEST}\|^${TARGETMOUNTPOINT}" ; do
clear
if [[ -n "${KEYMOUNT}" ]] && grep -sq " ${KEYMOUNT}" /proc/mounts ; then
echo -e "${RED}The given directory is occupied. Please select a free mount point.${END}"
fi
echo -e "${GREEN}Please specify the directory, starting at root${END} \"${RED}/${END}\" ${GREEN}to mount ${KEYDEV}.${END}
/dev/${VGNAME}/${LVROOTNAME} will be mounted at ${TARGETMOUNTPOINT}"
read -rp "Mount at: " KEYMOUNT
export KEYMOUNT
done
# KEYDIR prompt
while [[ -z "${KEYDIR}" ]] || ! echo "${KEYDIR}" | grep -q "^/" || echo "${KEYDIR}" | grep -q "${GREPTEST}"; do
clear
echo -e "${GREEN}Please specify the directory on ${KEYDEV} to store your keyfile.${END}
If the directory does not exist it will be created.
If ${KEYNAME} exists in this directory then it will
be used to encrypt ${DEV}${CRYPTPARTITION}"
read -rp "Keyfile directory: " KEYDIR
export KEYDIR
done
# KEYFORMAT prompt
while [[ -z "${KEYFORMAT}" ]] || [[ ! "${KEYFORMAT}" =~ ^[0-4]$ ]]; do
clear
echo -e "${GREEN}\
Detected ${KEYDEV} filesystem as : $(lsblk -dno FSTYPE ${KEYDEV})${END}
Would you like to reformat to another filesystem?
0 : Do not reformat ${KEYDEV}
***** THE FOLLOWING WILL ERASE DATA ON ${KEYDEV} *****
1 : vfat
2 : btrfs
3 : ext4
4 : xfs"
read -rp "Format ${KEYDEV} ? " KEYFORMAT
export KEYFORMAT
done
# Format Key based on selection
if [[ "$KEYFORMAT" =~ ^[1-4]$ ]] ; then
case "$KEYFORMAT" in
"1") export KEYFSTYPE="vfat" ; export KEYFLAG= ;;
"2") export KEYFSTYPE="btrfs" ; export KEYFLAG="-f" ;;
"3") export KEYFSTYPE="ext4" ; export KEYFLAG="-F" ;;
"4") export KEYFSTYPE="xfs" ; export KEYFLAG="-f" ;;
*) ;;
esac
fi
export KEYPATH="${KEYDIR}/${KEYNAME}"
}
function system_var_check() {
while : ; do
clear
echo -e "${GREEN}\
Please review your new system settings${END}
01 - Install device : ${DEV}
02 - Target mount : ${TARGETMOUNTPOINT}
03 - Crypt name : ${CRYPTNAME}
04 - System Hostname : ${NEWHOSTNAME}
05 - LV Group name : ${VGNAME}
06 - LV Root name : ${LVROOTNAME}
07 - LV Root size : ${LVROOTSIZE}
08 - LV Swap name : ${LVSWAPNAME}
09 - LV Swap size : ${LVSWAPSIZE}"
if [[ "${WANTSHOME}" -eq 1 ]]; then
echo -e "\
10 - LV Home name : ${LVHOMENAME}
11 - LV Home size (%) : ${LVHOMESIZE}"
elif [[ "${WANTSHOME}" -eq 0 ]]; then
echo -e "\
10 - Home wanted : No"
elif [[ -z "${WANTSHOME}" ]]; then
echo -e "\
10 - Home wanted : NULL"
fi
if [[ "${MULTILIB}" -eq 1 ]]; then
echo -e "\
12 - Multilib enabled : Yes"
else
echo -e "\
12 - Multilib enabled : No"
fi
if [[ "${WANTSSDGRUB}" -eq 1 ]]; then
echo -e "\
13 - Discards enabled : Yes"
else
echo -e "\
13 - Discards enabled : No"
fi
if [[ "${ISEFI}" -eq 1 ]]; then
echo -e "\
14 - EFI boot size : ${EFISIZE}"
fi
if [[ "${SYSCHANGE}" -ne 1 ]]; then
read -rp "Do you wish to change any options? [y/N] " ans
case "${ans}" in
"n"|"N"|"") unset ans ; export SYSCHANGE=0 ; break ;;
"y"|"Y") unset ans ; export SYSCHANGE=1 ; continue ;;
*) unset ans ; continue ;;
esac
fi
echo -e "${RED}\
Enter the option number to change${END}
i.e. 01 to change install device
Leave blank or enter 0 when done"
read -rp "Option number: " ans
case "${ans}" in
""|"0") unset ans ; break ;;
"01") export DEV= ;;
"02") export TARGETMOUNTPOINT= ;;
"03") export CRYPTNAME= ;;
"04") export NEWHOSTNAME= ;;
"05") export VGNAME= ;;
"06") export LVROOTNAME= ;;
"07") export LVROOTSIZE= ;;
"08") export LVSWAPNAME= ;;
"09") export LVSWAPSIZE= ;;
"10")
if [[ "${WANTSHOME}" -eq 0 ]]; then
export WANTSHOME=
elif [[ "${WANTSHOME}" -eq 1 ]]; then
export LVHOMENAME=
fi ;;
"11") export LVHOMESIZE= ;;
"12") export MULTILIB= ;;
"13") export WANTSSDGRUB= ;;
"14") export EFISIZE= ;;
*) clear
echo -e "${RED}Unrecognized option${END}\nDon't forget to include the zero for 01-09"
sleep 2 ;;
esac
done
}
function key_var_check() {
while : ; do
clear
echo -e "${GREEN}\
Please review your keyfile settings${END}
01 - Key device : ${KEYDEV}
02 - Key name : ${KEYNAME}
03 - Key mountpoint : ${KEYMOUNT}
04 - Key directory : ${KEYDIR}"
if [[ "${KEYFORMAT}" -ne 0 ]]; then
echo -e "\
05 - New filesystem : ${KEYFSTYPE}"
fi
if [[ "${KEYCHANGE}" -ne 1 ]]; then
read -rp "Do you wish to change any options? [y/N] " ans
case "${ans}" in
"n"|"N"|"") unset ans ; export KEYCHANGE=0 ; break ;;
"y"|"Y") unset ans ; export KEYCHANGE=1 ; continue ;;
*) unset ans ; continue ;;
esac
fi
echo -e "${RED}\
Enter the option number to change${END}
i.e. 01 to change key device
Leave blank or enter 0 when done"
read -rp "Option number: " ans
case "${ans}" in
""|"0") unset ans ; break ;;
"01") export KEYDEV= ;;
"02") export KEYNAME= ;;
"03") export KEYMOUNT= ;;
"04") export KEYDIR= ;;
"05") export KEYFSTYPE= ; export KEYFORMAT= ;;
*) clear
echo -e "${RED}Unrecognized option${END}\nDon't forget to include the zero for 01-05"
sleep 2 ;;
esac
done
}
function confirm_install() {
while : ; do
clear
echo -e "${RED}\
***** INSTALLTION CONFIRMATION *****${END}
${DEV} is about to be wiped clean and
a new Arch Linux system will be installed"
read -rp "Please enter YES to continue: " ans
case "${ans}" in
"YES") unset ans ; return 0 ; break ;;
*) unset ans ; return 1 ; break ;;
esac
done
}
# fde-install
function main() {
if ! get_opts "$@" ; then
return 1
fi
while [[ -z "${SYSCHANGE}" ]] || [[ "${SYSCHANGE}" -eq 1 ]]; do
export SYSCHANGE=
if ! system_prompts ; then
echo -e "${RED}\
***** ERROR : Configuring installation settings *****${END}
Please either review your config or report this issue at
https://github.com/johndovern/FDE-Arch"
return 1
fi
system_var_check
done
while [[ -z "${KEYCHANGE}" ]] || [[ "${KEYCHANGE}" -eq 1 ]]; do
export KEYCHANGE=
if ! key_prompts ; then
if [[ ! "${WANTKEY}" =~ ^[0-1]$ ]]; then
echo -e "${RED}\
***** ERROR : Configuring keyfile settings *****${END}
Please either review your config or report this issue at
https://github.com/johndovern/FDE-Arch"
return 1
fi
fi
if [[ "${WANTKEY}" -eq 1 ]]; then
key_var_check
fi
done
if ! confirm_install ; then
send_error "User terminated installation"
return 1
fi
if [[ ! -d "${TARGETMOUNTPOINT}" ]]; then send_prog "Making direcotry ${TARGETMOUNTPOINT}" ; mkdir -p "${TARGETMOUNTPOINT}" ; fi
if ! format_dev ; then
send_error "Unable to format ${DEV}"
close_and_unmount
return 1
fi
if ! encrypt_dev ; then
send_error "Failed to encrypt ${DEV}${CRYPTPARTITION}"
close_and_unmount
return 1
fi
if [[ "${WANTKEY}" -eq 1 ]]; then
if [[ "${KEYFORMAT}" -ne 0 ]]; then
if ! reformat_key ; then
send_error "Failed to reformat ${KEYDEV} to ${KEYFSTYPE}"
close_and_unmount
return 1
fi
fi
if ! luks_add_keyfile ; then
send_error "Failed to add a keyfile to ${DEV}${CRYPTPARTITION}"
close_and_unmount
return 1
fi
fi