Repository navigation
Expand file tree
/
Copy pathstart_install.sh
More file actions
1446 lines (1253 loc) · 46.9 KB
/
Copy pathstart_install.sh
File metadata and controls
1446 lines (1253 loc) · 46.9 KB
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
#
# start_install.sh - part of the DebianInstaller project
# Copyright (C) 2026, Scott Wyman, development@scottwyman.me
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# TODO: Add logic for selecting disk
# TODO: Check that disk and partitions exist before running
# TODO: Add section for checking that all needed files are accessible
PRETTY_OUTPUT_LIBRARY=./DebianInstaller/pretty_output_library.sh
COMPLETION_FILE=./start_install_completion.txt
INSTALL_CONSTANTS_FILE=./DebianInstaller/install_constants
FIRMWARE_PACKAGES=()
export DEBIAN_FRONTEND=noninteractive
if ! source $INSTALL_CONSTANTS_FILE &>/dev/null
then
printf "\n\n\e[31m%s %s\e[0m\n\n" \
"[!] Couldn't source the install_constants file. Make sure" \
"to run \`bash ./DebianInstaller/start_install.sh\`"
exit 1
fi
if ! source $PRETTY_OUTPUT_LIBRARY &>/dev/null
then
printf "\n\n\e[31m%s %s\e[0m\n\n" \
"[!] Couldn't source the pretty output library. Make sure" \
"to run \`bash ./DebianInstaller/start_install.sh\`"
exit 1
fi
STDOUT_LOG_PATH="/tmp/start_install_stdout.log"
STDERR_LOG_PATH="/tmp/start_install_stderr.log"
if [[ "$(whoami)" != "root" ]]
then
printf "\n\e[31m%s\e[0m\n" "[!] Must run script as root"
exit 1
fi
check_required_install_constants()
{
if [[ -z "$DISK" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$DISK constant not set, this is fatal...stopping"
return 1
fi
if [[ -z "$EFI_PARTITION" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$EFI_PARTITION constant not set, this is fatal...stopping"
return 1
fi
if [[ -z "$BOOT_PARTITION" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$BOOT_PARTITION constant not set, this is fatal...stopping"
return 1
fi
if [[ -z "$ROOT_PARTITION" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$ROOT_PARTITION constant not set, this is fatal...stopping"
return 1
fi
if [[ -z "$HOME_PARTITION" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$HOME_PARTITION constant not set, this is fatal...stopping"
return 1
fi
if [[ -n "$RAID_LEVEL" ]]
then
if ! [[ "$RAID_LEVEL" =~ ^(0|1|4|5|6|10)$ ]]
then
printf "\n\n\e[31m%s %s\e[0m \e[36m%s\e[0m\n" "[ERROR]" \
"Raid level '$RAID_LEVEL' not supported." \
"Support levels are 0,1,4,5,6,10"
return 1
fi
if [[ -z "$RAID_ARRAY_DEVICE" ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"\$RAID_PARTITIONS constant must be populated if \$RAID_LEVEL is set."
return 1
fi
if [[ -z "${RAID_PARTITIONS[*]}" ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"\$RAID_PARTITIONS constant must be populated if \$RAID_LEVEL is set."
return 1
fi
for raid_partition in ${RAID_PARTITIONS[@]}
do
if ! [[ -b "$raid_partition" ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"'$raid_partition' in the install constant '\$RAID_PARTITIONS' is not a valid partition"
return 1
fi
done
HOME_PARTITION="$RAID_ARRAY_DEVICE"
elif [[ ${#RAID_PARTITIONS[@]} > 1 ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"\$RAID_LEVEL constant must be populated if \$RAID_PARTITIONS is set."
return 1
fi
if [[ "$OVERWRITE_HOME_PARTITION" != 'y' && "$OVERWRITE_HOME_PARTITION" != 'n' ]]
then
printf "\n\e[31m%s %s\e[0m\n" \
"[!] \$OVERWRITE_HOME_PARTITION constant must be 'y' or 'n'," \
"this is fatal...stopping"
return 1
fi
if [[ -z "$HOSTNAME" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$HOSTNAME constant not set, this is fatal...stopping"
return 1
fi
if [[ -z "$ADMIN_USERNAME" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$ADMIN_USERNAME constant not set, this is fatal...stopping"
return 1
fi
if [[ -z "$ADMIN_PASSWORD" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$ADMIN_PASSWORD constant not set, this is fatal...stopping"
return 1
fi
if [[ -z "$USER_USERNAME" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$USER_USERNAME constant not set, this is fatal...stopping"
return 1
fi
if [[ -z "$TIMEZONE" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] \$TIMEZONE constant not set, this is fatal...stopping"
return 1
fi
if ! [[ -f "/usr/share/zoneinfo/${TIMEZONE}" ]]
then
printf "\n\e[31m%s %s\e[0m\n" \
"[!] \$TIMEZONE constant must be a valid timezone from" \
"/usr/share/zoneinfo, this is fatal...stopping"
return 1
fi
if [[ "$ENABLE_WIFI" != 'y' && "$ENABLE_WIFI" != 'n' ]]
then
printf "\n\e[31m%s %s\e[0m\n" \
"[!] \$ENABLE_WIFI constant must be 'y' or 'n'." \
return 1
fi
OVERWRITE_ROOT_PARTITION='y'
if [[ "$SKIP_INSTALLING_PACKAGES" != 'y' && "$SKIP_INSTALLING_PACKAGES" != 'n' ]]
then
printf "\n\e[31m%s %s\e[0m\n" \
"[!] \$SKIP_INSTALLING_PACKAGES constant must be 'y' or 'n'." \
return 1
elif [[ "$SKIP_INSTALLING_PACKAGES" == 'y' ]]
then
OVERWRITE_ROOT_PARTITION='n'
fi
if [[ -n "$LUKS_KEYFILE_PARTITION" && -n "$LUKS_PASSWORD" ]]
then
if [[ "$USE_KEYFILE_AT_BOOT" != 'y' && "$USE_KEYFILE_AT_BOOT" != 'n' ]]
then
printf "\n\e[31m%s %s\e[0m\n" \
"[!] \$USE_KEYFILE_AT_BOOT constant must be 'y' or 'n'."
return 1
fi
fi
return 0
}
check_required_install_constants || exit $?
if [[ -n "$LUKS_PASSWORD" || -n "$LUKS_KEYFILE_PARTITION" ]]
then
ENCRYPT_SYSTEM='y'
else
ENCRYPT_SYSTEM='n'
fi
check_for_cache_server()
{
if [[ -n "$APT_CACHE_SERVER" ]]
then
curl --max-time 5 "$APT_CACHE_SERVER" 1>/dev/null 2>$STDERR_LOG_PATH &
task_output $! "$STDERR_LOG_PATH" \
"Check connection to apt cache server at '$APT_CACHE_SERVER'"
if [[ $? -ne 0 ]]
then
printf "\n\e[36m%s %s %s %s\e[0m\n\n" \
"[TIP] Change the APT_CACHE_SERVER line in" \
"'./DebianInstaller/install_constants' to your new apt cache" \
"server url, or remove the line entirely if you aren't using" \
"an apt cache server"
return 1
fi
fi
return 0
}
check_for_cache_server || exit $?
stop_raid_arrays()
{
if ! [[ -f /proc/mdstat ]]
then
return 1
fi
if grep "md[0-9]\+ : active" /proc/mdstat &>/dev/null
then
mdadm --stop --scan >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Stop active RAID array(s)"
if [[ $? -ne 0 ]]
then
printf "\n\e[36m%s %s %s %s\e[0m\n\n" "[TIP]" \
"If the live system is still mounted, try running" \
"DebianInstaller/safely_close_system.sh before" \
"running start_install.sh again"
return 1
fi
fi
return 0
}
create_raid_array()
{
if ! grep "^create_raid_array$" $COMPLETION_FILE &>/dev/null
then
if [[ "$OVERWRITE_HOME_PARTITION" != 'y' ]] # Double check before overwriting anything
then
printf "\n\e[31m%s %s\e[0m\n" \
"[!] \$OVERWRITE_HOME_PARTITION not set to 'y' yet 'setup_raid_array'," \
"function is being called... This shouldn't happen. Stopping"
return 1
fi
wipefs --all "${RAID_PARTITIONS[@]}" >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"Wipe old partition data from RAID partitions: ${RAID_PARTITIONS[*]}"
[[ $? -ne 0 ]] && return 1
sudo mdadm --create $RAID_ARRAY_DEVICE \
--level=$RAID_LEVEL \
--raid-devices=${#RAID_PARTITIONS[@]} \
--metadata=1.2 \
--bitmap=internal \
--run \
"${RAID_PARTITIONS[@]}" >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"Create RAID $RAID_LEVEL array on partitions ${RAID_PARTITIONS[*]}"
[[ $? -ne 0 ]] && return 1
echo "create_raid_array" >> $COMPLETION_FILE
fi
return 0
}
configure_raid_array()
{
if ! [[ -d "/mnt/etc/mdadm" ]]
then
mkdir -p /mnt/etc/mdadm >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"Create /mnt/etc/mdadm for mdadm.conf"
[[ $? -ne 0 ]] && return 1
fi
sudo mdadm --detail --scan | sudo tee /mnt/etc/mdadm/mdadm.conf \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"Add RAID array details to /mnt/etc/mdadm/mdadm.conf (Automatically starts raid array at boot)"
[[ $? -ne 0 ]] && return 1
return 0
}
assemble_existing_raid_array()
{
mdadm --assemble $RAID_ARRAY_DEVICE "${RAID_PARTITIONS[@]}" \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"Assemble existing RAID array '$RAID_ARRAY_DEVICE' with partitions: ${RAID_PARTITIONS[*]}"
[[ $? -ne 0 ]] && return 1
return 0
}
create_luks_keyfile_on_usb()
{
if ! [[ -b /dev/disk/by-label/keyfile_usb ]]
then
echo 'y' | mkfs.fat -F 32 -n "keyfile_usb" $LUKS_KEYFILE_PARTITION \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Format keyfile_usb partition with FAT32"
[[ $? -ne 0 ]] && return 1
fi
if ! [[ -d /media/keyfile_usb ]]
then
mount --mkdir /dev/disk/by-label/keyfile_usb /media/keyfile_usb \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Mount keyfile_usb partition"
[[ $? -ne 0 ]] && return 1
fi
if ! [[ -f /media/keyfile_usb/luks_keyfile ]]
then
dd if=/dev/urandom of=/media/keyfile_usb/luks_keyfile bs=1024 count=2 \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Create luks_keyfile on keyfile_usb"
[[ $? -ne 0 ]] && return 1
chmod 400 /media/keyfile_usb/luks_keyfile \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Set permissions on luks_keyfile (400)"
[[ $? -ne 0 ]] && return 1
fi
return 0
}
luks_format_root_with_keyfile()
{
if ! grep "^luksFormatRootWithKeyfile$" $COMPLETION_FILE &>/dev/null
then
cryptsetup luksFormat --key-file /media/keyfile_usb/luks_keyfile \
--batch-mode $ROOT_PARTITION \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "luksFormat root ($ROOT_PARTITION) with USB keyfile"
[[ $? -ne 0 ]] && return 1
echo "luksFormatRootWithKeyfile" >> $COMPLETION_FILE
fi
return 0
}
luks_format_home_with_keyfile()
{
if ! grep "^luksFormatHomeWithKeyfile$" $COMPLETION_FILE &>/dev/null
then
cryptsetup luksFormat --key-file /media/keyfile_usb/luks_keyfile \
--batch-mode $HOME_PARTITION \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "luksFormat home ($HOME_PARTITION) with USB keyfile"
[[ $? -ne 0 ]] && return 1
echo "luksFormatHomeWithKeyfile" >> $COMPLETION_FILE
fi
return 0
}
luks_format_root_with_passphrase() {
if [[ -z "$LUKS_PASSWORD" ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"Luks password not set in 'install_constants' file"
return 1
fi
if ! grep "^luksFormatRootWithPassphrase$" $COMPLETION_FILE &>/dev/null
then
echo -n "$LUKS_PASSWORD" | cryptsetup luksFormat -s 512 -h sha512 \
--key-file=- $ROOT_PARTITION >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "luksFormat root ($ROOT_PARTITION) with passphrase"
[[ $? -ne 0 ]] && return 1
echo "luksFormatRootWithPassphrase" >> $COMPLETION_FILE
fi
return 0
}
luks_format_home_with_passphrase() {
if [[ -z "$LUKS_PASSWORD" ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"Luks password not set in 'install_constants' file"
return 1
fi
if ! grep "^luksFormatHomeWithPassphrase$" $COMPLETION_FILE &>/dev/null
then
echo -n "$LUKS_PASSWORD" | cryptsetup luksFormat -s 512 -h sha512 \
--key-file=- $HOME_PARTITION >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "luksFormat home ($HOME_PARTITION) with passphrase"
[[ $? -ne 0 ]] && return 1
echo "luksFormatHomeWithPassphrase" >> $COMPLETION_FILE
fi
return 0
}
luks_add_passphrase_to_root()
{
if [[ -z "$LUKS_PASSWORD" ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"Luks password not set in 'install_constants' file"
return 1
fi
if ! grep "^luksAddPassphraseToRoot$" $COMPLETION_FILE &>/dev/null
then
echo -n "$LUKS_PASSWORD" | cryptsetup luksAddKey \
--key-file /media/keyfile_usb/luks_keyfile \
$ROOT_PARTITION >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"Add LUKS passphrase to root ($ROOT_PARTITION)"
[[ $? -ne 0 ]] && return 1
echo "luksAddPassphraseToRoot" >> $COMPLETION_FILE
fi
return 0
}
luks_add_passphrase_to_home()
{
if [[ -z "$LUKS_PASSWORD" ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"Luks password not set in 'install_constants' file"
return 1
fi
if ! grep "^luksAddPassphraseToHome$" $COMPLETION_FILE &>/dev/null
then
echo -n "$LUKS_PASSWORD" | cryptsetup luksAddKey \
--key-file /media/keyfile_usb/luks_keyfile \
$HOME_PARTITION >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"Add LUKS passphrase to home ($HOME_PARTITION)"
[[ $? -ne 0 ]] && return 1
echo "luksAddPassphraseToHome" >> $COMPLETION_FILE
fi
return 0
}
luks_add_keyfile_to_root()
{
if [[ -z "$LUKS_PASSWORD" ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"Luks password not set in 'install_constants' file"
return 1
fi
if ! grep "^luksAddKeyFileToRoot$" $COMPLETION_FILE &>/dev/null
then
echo -n "$LUKS_PASSWORD" | cryptsetup luksAddKey --key-file=- \
$ROOT_PARTITION /media/keyfile_usb/luks_keyfile \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"Add LUKS keyfile to root ($ROOT_PARTITION)"
[[ $? -ne 0 ]] && return 1
echo "luksAddKeyFileToRoot" >> $COMPLETION_FILE
fi
return 0
}
luks_add_keyfile_to_home()
{
if [[ -z "$LUKS_PASSWORD" ]]
then
printf "\n\e[31m%s %s\e[0m\n" "[ERROR]" \
"Luks password not set in 'install_constants' file"
return 1
fi
if ! grep "^luksAddKeyFileToHome$" $COMPLETION_FILE &>/dev/null
then
echo -n "$LUKS_PASSWORD" | cryptsetup luksAddKey --key-file=- \
$HOME_PARTITION /media/keyfile_usb/luks_keyfile \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"Add LUKS keyfile to home ($HOME_PARTITION)"
[[ $? -ne 0 ]] && return 1
echo "luksAddKeyFileToHome" >> $COMPLETION_FILE
fi
return 0
}
luks_open_root()
{
if ! grep "^luksOpenRoot$" $COMPLETION_FILE &>/dev/null
then
cryptsetup close crypt_root &>/dev/null
if [[ -n "$LUKS_PASSWORD" && -z "$LUKS_KEYFILE_PARTITION" ]]
then
echo -n "$LUKS_PASSWORD" | cryptsetup open --key-file=- $ROOT_PARTITION \
crypt_root >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted root partition with passphrase"
if [[ $? -ne 0 ]]
then
if [[ "$OVERWRITE_ROOT_PARTITION" == 'n' ]]
then
printf "\n\n\e[36m%s %s %s\e[0m\n" "[TIP]" \
"Probably used the wrong partition passphrase, try" \
"changing it in install_constants"
fi
return 1
fi
elif [[ -n "$LUKS_KEYFILE_PARTITION" && -z "$LUKS_PASSWORD" ]]
then
cryptsetup open --key-file /media/keyfile_usb/luks_keyfile \
$ROOT_PARTITION crypt_root >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted root partition with USB keyfile"
if [[ $? -ne 0 ]]
then
if [[ "$OVERWRITE_ROOT_PARTITION" == 'n' ]]
then
printf "\n\n\e[36m%s %s %s\e[0m\n" "[TIP]" \
"Probably used the wrong partition passphrase, try" \
"changing it in install_constants"
fi
return 1
fi
elif [[ -n "$LUKS_PASSWORD" && -n "$LUKS_KEYFILE_PARTITION" ]]
then
cryptsetup open --key-file /media/keyfile_usb/luks_keyfile \
$ROOT_PARTITION crypt_root >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted root partition with USB keyfile"
local luks_open_return_code=$?
if [[ $luks_open_return_code -eq 0 && "$OVERWRITE_ROOT_PARTITION" == 'n' ]]
then
cryptsetup close crypt_root &>/dev/null
echo -n "$LUKS_PASSWORD" | cryptsetup open --key-file=- $ROOT_PARTITION \
crypt_root >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted root partition with passphrase"
if [[ $? -ne 0 ]]
then
printf "\n\e[36m%s %s\e[0m\n\n" "[TIP]" \
"It's okay if this fails, as long as subsequent steps don't"
luks_add_passphrase_to_root
luks_open_root
fi
elif [[ $luks_open_return_code -ne 0 && "$OVERWRITE_ROOT_PARTITION" == 'n' ]]
then
printf "\n\e[36m%s %s\e[0m\n\n" "[TIP]" \
"It's okay if this fails, as long as subsequent steps don't"
echo -n "$LUKS_PASSWORD" | cryptsetup open --key-file=- $ROOT_PARTITION \
crypt_root >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted root partition with passphrase"
if [[ $? -eq 0 ]]
then
luks_add_keyfile_to_root
else
printf "\n\n\e[36m%s %s %s %s\e[0m\n" "[TIP]" \
"Neither the passphrase nor the keyfile could unlock" \
"the root partition. Did you mean to set" \
"SKIP_INSTALLING_PACKAGES='y' in install_constants?"
return 1
fi
elif [[ $luks_open_return_code -ne 0 && "$OVERWRITE_ROOT_PARTITION" == 'y' ]]
then
printf "\n\n\e[36m%s %s %s\e[0m\n" "[TIP]" \
"The root partition is being overwritten, so this luksOpen" \
"step should never fail. Must be a logic error."
return 1
fi
fi
echo "luksOpenRoot" >> $COMPLETION_FILE
fi
return 0
}
luks_open_home()
{
if ! grep "^luksOpenHome$" $COMPLETION_FILE &>/dev/null
then
cryptsetup close crypt_home &>/dev/null
if [[ -n "$LUKS_PASSWORD" && -z "$LUKS_KEYFILE_PARTITION" ]]
then
echo -n "$LUKS_PASSWORD" | cryptsetup open --key-file=- $HOME_PARTITION \
crypt_home >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted home partition with passphrase"
if [[ $? -ne 0 ]]
then
if [[ "$OVERWRITE_HOME_PARTITION" == 'n' ]]
then
printf "\n\n\e[36m%s %s %s %s\e[0m\n" "[TIP]" \
"Probably used the wrong partition passphrase, or set" \
"OVERWRITE_HOME_PARTITION='n' when you meant 'y', try" \
"changing it in install_constants"
fi
return 1
fi
elif [[ -n "$LUKS_KEYFILE_PARTITION" && -z "$LUKS_PASSWORD" ]]
then
cryptsetup open --key-file /media/keyfile_usb/luks_keyfile \
$HOME_PARTITION crypt_home >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted home partition with USB keyfile"
if [[ $? -ne 0 ]]
then
if [[ "$OVERWRITE_HOME_PARTITION" == 'n' ]]
then
printf "\n\n\e[36m%s %s %s\e[0m\n" "[TIP]" \
"Probably used the wrong partition passphrase, try" \
"changing it in install_constants"
fi
return 1
fi
elif [[ -n "$LUKS_PASSWORD" && -n "$LUKS_KEYFILE_PARTITION" ]]
then
cryptsetup open --key-file /media/keyfile_usb/luks_keyfile \
$HOME_PARTITION crypt_home >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted home partition with USB keyfile"
local luks_open_return_code=$?
if [[ $luks_open_return_code -eq 0 && "$OVERWRITE_HOME_PARTITION" == 'n' ]]
then
cryptsetup close crypt_home &>/dev/null
echo -n "$LUKS_PASSWORD" | cryptsetup open --key-file=- $HOME_PARTITION \
crypt_home >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted home partition with passphrase"
if [[ $? -ne 0 ]]
then
printf "\n\e[36m%s %s\e[0m\n\n" "[TIP]" \
"It's okay if this fails, as long as subsequent steps don't"
luks_add_passphrase_to_home
luks_open_home
fi
elif [[ $luks_open_return_code -ne 0 && "$OVERWRITE_HOME_PARTITION" == 'n' ]]
then
printf "\n\e[36m%s %s\e[0m\n\n" "[TIP]" \
"It's okay if this fails, as long as subsequent steps don't"
echo -n "$LUKS_PASSWORD" | cryptsetup open --key-file=- $HOME_PARTITION \
crypt_home >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" \
"open luks encrypted home partition with passphrase"
if [[ $? -eq 0 ]]
then
luks_add_keyfile_to_home
else
printf "\n\n\e[36m%s %s %s %s\e[0m\n" "[TIP]" \
"Neither the passphrase nor the keyfile could unlock" \
"the home partition. Did you mean to set" \
"OVERWRITE_HOME_PARTITION='y' in install_constants?"
return 1
fi
elif [[ $luks_open_return_code -ne 0 && "$OVERWRITE_HOME_PARTITION" == 'y' ]]
then
printf "\n\n\e[36m%s %s %s\e[0m\n" "[TIP]" \
"The home partition is being overwritten, so this luksOpen" \
"step should never fail. Must be a logic error."
return 1
fi
fi
echo "luksOpenHome" >> $COMPLETION_FILE
fi
return 0
}
format_partitions()
{
local home_partition="$1"
local root_partition="$2"
if [[ "$OVERWRITE_ROOT_PARTITION" = 'y' ]]
then
if ! grep "^mkfs_efi$" $COMPLETION_FILE &>/dev/null
then
echo 'y' | mkfs.fat -F 32 $EFI_PARTITION \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Format EFI partition ($EFI_PARTITION) with FAT32"
[[ $? -ne 0 ]] && return 1
echo "mkfs_efi" >> $COMPLETION_FILE
fi
if ! grep "^mkfs_boot$" $COMPLETION_FILE &>/dev/null
then
echo 'y' | mkfs.ext4 $BOOT_PARTITION \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Format boot partition ($BOOT_PARTITION) with EXT4"
[[ $? -ne 0 ]] && return 1
echo "mkfs_boot" >> $COMPLETION_FILE
fi
if ! grep "^mkfs_root$" $COMPLETION_FILE &>/dev/null
then
echo 'y' | mkfs.ext4 "$root_partition" \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Format root partition ($ROOT_PARTITION) with EXT4"
[[ $? -ne 0 ]] && return 1
echo "mkfs_root" >> $COMPLETION_FILE
fi
fi
if [[ $OVERWRITE_HOME_PARTITION == 'y' ]]
then
if ! grep "^mkfs_home$" $COMPLETION_FILE &>/dev/null
then
echo 'y' | mkfs.ext4 "$home_partition" \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Format home partition ($HOME_PARTITION) with EXT4"
[[ $? -ne 0 ]] && return 1
echo "mkfs_home" >> $COMPLETION_FILE
fi
fi
return 0
}
mount_partitions()
{
local home_partition="$1"
local root_partition="$2"
if ! grep "^mount_root$" $COMPLETION_FILE &>/dev/null
then
mount "$root_partition" /mnt >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Mount the root partition ($ROOT_PARTITION)"
[[ $? -ne 0 ]] && return 1
echo "mount_root" >> $COMPLETION_FILE
fi
if ! grep "^mount_home$" $COMPLETION_FILE &>/dev/null
then
mount --mkdir "$home_partition" /mnt/home \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Mount the home partition ($HOME_PARTITION)"
if [[ $? -ne 0 ]]
then
printf "\n\n\e[36m%s %s %s\e[0m\n" "[TIP]" \
"Is it possible OVERWRITE_HOME_PARTITION is set to 'n' in" \
"install_constants, but the existing home partition is empty?"
return 1
fi
echo "mount_home" >> $COMPLETION_FILE
fi
if ! grep "^mount_boot$" $COMPLETION_FILE &>/dev/null
then
mount --mkdir $BOOT_PARTITION /mnt/boot \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Mount the boot partition ($BOOT_PARTITION)"
[[ $? -ne 0 ]] && return 1
echo "mount_boot" >> $COMPLETION_FILE
fi
if ! grep "^mount_efi$" $COMPLETION_FILE &>/dev/null
then
mount --mkdir $EFI_PARTITION /mnt/boot/efi \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Mount the efi partition ($EFI_PARTITION)"
[[ $? -ne 0 ]] && return 1
echo "mount_efi" >> $COMPLETION_FILE
fi
return 0
}
populate_crypttab()
{
LUKS_ROOT_PARTITION_UUID="$(blkid -s UUID -o value $ROOT_PARTITION)"
LUKS_HOME_PARTITION_UUID="$(blkid -s UUID -o value $HOME_PARTITION)"
if [[ -z "$LUKS_ROOT_PARTITION_UUID" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] Couldn't find encrypted root partition in blkid output"
return 1
fi
if [[ -z "$LUKS_HOME_PARTITION_UUID" ]]
then
printf "\n\e[31m%s\e[0m\n" \
"[!] Couldn't find encrypted home partition in blkid output"
return 1
fi
if ! [[ -d /mnt/etc/ ]]
then
mkdir -p /mnt/etc 1>/dev/null 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Create /mnt/etc for crypttab"
[[ $? -ne 0 ]] && return 1
fi
if [[ -n "$LUKS_PASSWORD" ]]
then
if [[ "$USE_KEYFILE_AT_BOOT" == 'n' || -z "$LUKS_KEYFILE_PARTITION" ]]
then
echo "crypt_root UUID=$LUKS_ROOT_PARTITION_UUID none luks,discard,keyscript=decrypt_keyctl,initramfs" \
> /mnt/etc/crypttab 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Add LUKS entry for root to /mnt/etc/crypttab"
[[ $? -ne 0 ]] && return 1
echo "crypt_home UUID=$LUKS_HOME_PARTITION_UUID none luks,discard,keyscript=decrypt_keyctl,initramfs" \
>> /mnt/etc/crypttab 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Add LUKS entry for home to /mnt/etc/crypttab"
[[ $? -ne 0 ]] && return 1
fi
fi
if [[ -n "$LUKS_KEYFILE_PARTITION" ]]
then
if [[ "$USE_KEYFILE_AT_BOOT" == 'y' || -z "$LUKS_PASSWORD" ]]
then
echo "crypt_root UUID=$LUKS_ROOT_PARTITION_UUID /dev/disk/by-label/keyfile_usb:/luks_keyfile:60 luks,discard,keyscript=passdev,tries=2,initramfs" \
> /mnt/etc/crypttab 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Add LUKS entry for root to /mnt/etc/crypttab"
[[ $? -ne 0 ]] && return 1
echo "crypt_home UUID=$LUKS_HOME_PARTITION_UUID /dev/disk/by-label/keyfile_usb:/luks_keyfile:60 luks,discard,keyscript=passdev,tries=2,initramfs" \
>> /mnt/etc/crypttab 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Add LUKS entry for home to /mnt/etc/crypttab"
[[ $? -ne 0 ]] && return 1
fi
fi
return 0
}
configure_swap()
{
if [[ -n "$SWAP_SIZE_IN_GB" ]]
then
if ! [[ "$SWAP_SIZE_IN_GB" =~ ^[1-9][0-9]*$ ]]
then
printf "\n\e[31m%s\e[0m\n" "[!] Invalid swap size: '$SWAP_SIZE_IN_GB'"
return 1
fi
if [[ "$SWAP_SIZE_IN_GB" -gt 32 ]]
then
printf "\n\e[31m%s\e[0m\n" "[!] Max swap size is 32GB"
return 1
fi
if ! grep "^mkswap$" $COMPLETION_FILE &>/dev/null
then
if [[ -f "/mnt/swapfile" ]]
then
swapoff /mnt/swapfile &>/dev/null
rm /mnt/swapfile &>/dev/null
fi
mkswap -U clear --size ${SWAP_SIZE_IN_GB}G --file /mnt/swapfile \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Create ${SWAP_SIZE_IN_GB}GB swapfile"
[[ $? -ne 0 ]] && return 1
echo "mkswap" >> $COMPLETION_FILE
fi
if ! grep "^swapon$" $COMPLETION_FILE &>/dev/null
then
swapon /mnt/swapfile >>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Enable the swapfile"
[[ $? -ne 0 ]] && return 1
echo "swapon" >> $COMPLETION_FILE
fi
fi
return 0
}
set_timezone()
{
local user_timezone="$1"
if [[ -z "$user_timezone" ]]
then
printf "\n\e[31m%s\e[0m %s\n" "[Error]" \
"No user timezone passed to function, this shouldn't happen. Stopping."
return 1
fi
if ! cmp -s "/usr/share/zoneinfo/${user_timezone}" /etc/localtime &>/dev/null
then
ln -sf "/usr/share/zoneinfo/${user_timezone}" /etc/localtime \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Set live system timezone: '$user_timezone'"
[[ $? -ne 0 ]] && return 1
fi
return 0
}
set_apt_cache_server()
{
if [[ -n "$APT_CACHE_SERVER" && -n "$APT_CACHE_FILE" ]]
then
if ! grep "Acquire::http::Proxy \"$APT_CACHE_SERVER\";"\
$APT_CACHE_FILE &>/dev/null
then
echo "Acquire::http::Proxy \"$APT_CACHE_SERVER\";" \
> $APT_CACHE_FILE &
task_output $! "$STDERR_LOG_PATH" \
"Use apt proxy server '$APT_CACHE_SERVER'"
[[ $? -ne 0 ]] && return 1
fi
if ! apt-config dump | grep "Proxy" &>/dev/null
then
printf "\n\n\e[31m%s %s\e[0m\n\n" \
"[!] The apt proxy isn't set up correctly. This shouldn't" \
"happen...stopping"
return 1
fi
fi
return 0
}
copy_apt_sources_to_live_system()
{
if ! cmp -s ./DebianInstaller/configuration_files/sources.list \
/etc/apt/sources.list &>/dev/null
then
cp ./DebianInstaller/configuration_files/sources.list /etc/apt/sources.list \
>>"$STDOUT_LOG_PATH" 2>>"$STDERR_LOG_PATH" &
task_output $! "$STDERR_LOG_PATH" "Copy sources.list to the current live system"
[[ $? -ne 0 ]] && return 1
fi
return 0
}
apt_update()
{
if ! grep "^apt_update$" $COMPLETION_FILE &>/dev/null