-
Notifications
You must be signed in to change notification settings - Fork 141
/
functions.sh
4233 lines (3706 loc) · 133 KB
/
functions.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
#!/bin/bash
#
# functions
#
# (c) 2007-2024, Hetzner Online GmbH
#
# nil settings parsed out of the config
PART_COUNT=""
PART_MOUNT=""
PART_FS=""
PART_SIZE=""
PARTS_SUM_SIZE=""
HASROOT=""
SWRAID=""
SWRAIDLEVEL=""
LVM=""
LVM_VG_CHECK=""
IMAGE_PATH=""
IMAGE_PATH_TYPE=""
IMAGE_FILE=""
IMAGE_FILE_TYPE=""
IMAGE_SIGN=""
IMAGE_PUBKEY=""
IMAGE_PUBKEY_IMPORTED=""
IAM=""
IMG_VERSION=0
BOOTLOADER=""
GOVERNOR=""
CRYPT=""
CRYPTPASSWORD=""
IPV4_ONLY=0
USE_KERNEL_MODE_SETTING=1
declare -i BTRFS_VOL_COUNT=0
declare -a BTRFS_VOL_NAME
declare -a BTRFS_VOL_SIZE
declare -i BTRFS_SV_COUNT=0
declare -a BTRFS_SV_VOL
declare -a BTRFS_SV_NAME
declare -a BTRFS_SV_MOUNT
# this var is probably not used anymore. keep it for safety
#SFDISKPARTS=""
COUNT_DRIVES=0
# this var is probably not used anymore. keep it for safety
#LAST_PART_START=""
# this var is probably not used anymore. keep it for safety
#LAST_PART_END=""
# this var is probably not used anymore. keep it for safety
#DISK_SIZE_SECTORS=""
SYSTEMROOTDEVICE=""
SYSTEMBOOTDEVICE=""
SYSTEMREALBOOTDEVICE=""
EXTRACTFROM=""
ROOTHASH=""
ERROREXIT="0"
FINALIMAGEPATH=""
SYSMFC="$(dmidecode -s system-manufacturer 2>/dev/null | tail -n1)"
SYSTYPE="$(dmidecode -s system-product-name 2>/dev/null | tail -n1)"
debug "# SYSTYPE: $SYSTYPE"
debug "# SYSMFC: $SYSMFC"
MBTYPE="$(dmidecode -s baseboard-product-name 2>/dev/null | tail -n1)"
SYSARCH="$(uname -m)"
if [ "$SYSARCH" == "aarch64" ]; then
SYSARCH="arm64"
fi
if [ -d "/sys/firmware/efi" ]; then
UEFI=1
fi
# functions
# show text in a different color
echo_red() {
echo -e "\033[01;31m$*\033[00m"
}
echo_green() {
echo -e "\033[01;32m$*\033[00m"
}
echo_bold() {
echo -e "\033[0;1m$*\033[00m"
}
# generate submenus to choose which image to install
# generate_menu "SUBMENU"
generate_menu() {
# security check - just execute the function WITH parameters
if [ -n "$1" ]; then
# empty the menu
MENULIST=""
PROXMOX=false
# find image-files and generate raw list
FINALIMAGEPATH="$IMAGESPATH"
if [ "$1" = "Other" ]; then
RAWLIST="$(other_images)"
elif [ "$1" = "Old images" ]; then
# skip CPANEL images and signatures files from list
RAWLIST=$(find "$OLDIMAGESPATH"/ -maxdepth 1 -type f -not -name "*.sig" -a -not -name "*cpanel*" -a -not -name 'README.txt' -printf '%f\n')
FINALIMAGEPATH="$OLDIMAGESPATH"
elif [[ "$1" == 'Arch Linux' ]]; then
RAWLIST="$IMAGESPATH/archlinux-latest-64-minimal.tar.gz"
elif [[ "$1" == 'CentOS Stream' ]]; then
RAWLIST=$(find "$IMAGESPATH"/ -maxdepth 1 -type f -iname '*centos*stream*' -not -regex '.*\.sig$' -printf '%f\n')
elif [[ "$1" == 'AlmaLinux' ]]; then
RAWLIST=$(find "$IMAGESPATH"/ -maxdepth 1 -type f -iname '*alma*' -not -regex '.*\.sig$' -printf '%f\n')
elif [[ "$1" == 'Rocky Linux' ]]; then
RAWLIST=$(find "$IMAGESPATH"/ -maxdepth 1 -type f -iname '*rocky*' -not -regex '.*\.sig$' -printf '%f\n')
else
# skip CPANEL images and signatures files from list
RAWLIST=$(find "$IMAGESPATH"/ \
-maxdepth 1 \
-type f \
-not -name "*cpanel*" \
-a -regextype sed -regex ".*/\(old_\)\?$1.*" \
-a -not -regex '.*\.sig$' \
-a -not -iname '*-beta.*' \
-a -not -iname '*centos*stream*' \
-printf '%f\n')
fi
# ensure rawlist is newline separated
RAWLIST="$(echo "$RAWLIST" | xargs | tr ' ' "\n")"
# filter images based on $SYSARCH
case "$SYSARCH" in
x86_64)
RAWLIST="$(echo "$RAWLIST" | grep -v -E "\-(32|i386)\-[a-zA-Z]")"
RAWLIST="$(echo "$RAWLIST" | grep -v -E "\-arm64\-[a-zA-Z]")"
;;
i386)
RAWLIST="$(echo "$RAWLIST" | grep -E "\-(32|i386)\-[a-zA-Z]")"
;;
arm64)
RAWLIST="$(echo "$RAWLIST" | grep "\-arm64\-[a-zA-Z]")"
;;
*)
echo "unknown arch $SYSARCH - exiting"
exit 1
;;
esac
case "${1,,}" in
*)
# order by image version desc
RAWLIST="$(sort -k 1,1 -k 2,2hr -t '-' <<< "$RAWLIST")"
;;
esac
# remove still beta images unless menu is Other
if [[ "$1" != 'Other' ]]; then
local image beta_image new_rawlist=()
while read image; do
for beta_image in "${IMAGE_STILL_BETA_OVERRIDES[@]}"; do
if [[ "$image" == "$beta_image" ]]; then
continue 2
fi
done
new_rawlist+=("$image")
done <<< "$RAWLIST"
RAWLIST="$(printf '%s\n' "${new_rawlist[@]}")"
fi
# generate formatted list for usage with "dialog"
for i in $RAWLIST; do
TEMPVAR="$i"
TEMPVAR=$(basename "$TEMPVAR" .tar.zst)
TEMPVAR=$(basename "$TEMPVAR" .txz)
TEMPVAR=$(basename "$TEMPVAR" .tar.xz)
TEMPVAR=$(basename "$TEMPVAR" .tgz)
TEMPVAR=$(basename "$TEMPVAR" .tar.gz)
TEMPVAR=$(basename "$TEMPVAR" .tbz)
TEMPVAR=$(basename "$TEMPVAR" .tar.bz)
TEMPVAR=$(basename "$TEMPVAR" .tar.bz2)
TEMPVAR=$(basename "$TEMPVAR" .tar)
MENULIST="$MENULIST$TEMPVAR . "
done
# add "back to mainmenu" entry
MENULIST="$MENULIST"'back . '
# show menu and get result
# shellcheck disable=SC2086
dialog --backtitle "$DIATITLE" --title "$1 images" --no-cancel --menu "choose image" 0 0 0 $MENULIST 2> "$FOLD/submenu.chosen"
IMAGENAME=$(cat "$FOLD/submenu.chosen")
# create proxmox post-install file if needed
case $IMAGENAME in
Proxmox-Virtualization-Environment*)
case "$IMAGENAME" in
Proxmox-Virtualization-Environment-on-Debian-Bookworm) export PROXMOX_VERSION="8" ;;
Proxmox-Virtualization-Environment-on-Debian-Bullseye) export PROXMOX_VERSION="7" ;;
esac
cp "$SCRIPTPATH/post-install/proxmox$PROXMOX_VERSION" /post-install
chmod 0755 /post-install
PROXMOX=true
IMAGENAME=$(eval echo \$PROXMOX${PROXMOX_VERSION}_BASE_IMAGE)
DEFAULTPARTS=""
if [ "$UEFI" -eq 1 ]; then
DEFAULTPARTS="$DEFAULTPARTS\nPART /boot/efi esp 256M"
fi
DEFAULTPARTS="$DEFAULTPARTS\nPART /boot ext3 1024M"
DEFAULTPARTS="$DEFAULTPARTS\nPART lvm vg0 all\n"
DEFAULTPARTS="$DEFAULTPARTS\nLV vg0 root / ext3 15G"
DEFAULTPARTS="$DEFAULTPARTS\nLV vg0 swap swap swap 6G"
;;
CoreOS*)
PROXMOX=false
;;
*)
: # no proxmox installation
;;
esac
whoami "$IMAGENAME"
fi
}
# create new config file from standardconfig and misc options
# create_config "IMAGENAME"
create_config() {
if [ "$1" ]; then
CNF="$FOLD/install.conf"
getdrives; EXITCODE=$?
if [ $COUNT_DRIVES -eq 0 ] ; then
graph_notice "There are no drives in your server!\nIf there is a raid controller in your server, please configure it!\n\nThe setup will quit now!"
return 1
fi
{
echo "## ======================================================"
echo "## $COMPANY - installimage - standard config"
echo "## ======================================================"
echo ""
} > "$CNF"
# first drive
{
echo ""
echo "## ===================="
echo "## HARD DISK DRIVE(S):"
echo "## ===================="
echo ""
} >> "$CNF"
[ $COUNT_DRIVES -gt 2 ] && echo "## PLEASE READ THE NOTES BELOW!" >> "$CNF"
echo "" >> "$CNF"
local found_optdrive=0
local optdrive_count=0
for ((i=1; i<=COUNT_DRIVES; i++)); do
DISK="$(eval echo \$DRIVE${i})"
OPTDISK="$(eval echo \$OPT_DRIVE${i})"
if [ -n "$OPTDISK" ] ; then
optdrive_count=$((optdrive_count+1))
found_optdrive=1
hdinfo "/dev/$OPTDISK" >> "$CNF"
echo "DRIVE$i /dev/$OPTDISK" >> "$CNF"
else
hdinfo "$DISK" >> "$CNF"
# comment drive out when not given via commandline
[ $found_optdrive -eq 1 ] && echo -n "# " >> "$CNF"
echo "DRIVE$i $DISK" >> "$CNF"
fi
done
# reset drive count to number of drives explicitly passed via command line
[ $optdrive_count -gt 0 ] && COUNT_DRIVES=$optdrive_count
echo "" >> "$CNF"
if [ $COUNT_DRIVES -gt 2 ] ; then
if [ $COUNT_DRIVES -lt 4 ] ; then
{
echo "## if you dont want raid over your three drives then comment out the following line and set SWRAIDLEVEL not to 5"
echo "## please make sure the DRIVE[nr] variable is strict ascending with the used harddisks, when you comment out one or more harddisks"
} >> "$CNF"
else
{
echo "## if you dont want raid over all of your drives then comment out the following line and set SWRAIDLEVEL not to 5 or 6 or 10"
echo "## please make sure the DRIVE[nr] variable is strict ascending with the used harddisks, when you comment out one or more harddisks"
} >> "$CNF"
fi
fi
echo "" >> "$CNF"
# software-raid
if [ $COUNT_DRIVES -gt 1 ]; then
{
echo ""
echo "## ==============="
echo "## SOFTWARE RAID:"
echo "## ==============="
echo ""
echo "## activate software RAID? < 0 | 1 >"
echo ""
} >> "$CNF"
case "$OPT_SWRAID" in
0) echo "SWRAID 0" >> "$CNF" ;;
1) echo "SWRAID 1" >> "$CNF" ;;
*) echo "SWRAID $DEFAULTSWRAID" >> "$CNF" ;;
esac
echo '' >> "$CNF"
# available raidlevels
local raid_levels="0 1 5 6 10"
# set default raidlevel
local default_level="$DEFAULTTWODRIVESWRAIDLEVEL"
if [ "$COUNT_DRIVES" -eq 3 ] ; then
default_level="$DEFAULTTHREEDRIVESWRAIDLEVEL"
elif [ "$COUNT_DRIVES" -gt 3 ] ; then
default_level="$DEFAULTFOURDRIVESWRAIDLEVEL"
fi
local set_level=""
local avail_level=""
# check for possible raidlevels
for level in $raid_levels ; do
# set raidlevel to given opt raidlevel
if [ -n "$OPT_SWRAIDLEVEL" ] ; then
[ "$OPT_SWRAIDLEVEL" -eq "$level" ] && set_level="$level"
fi
# no raidlevel 5 if less then 3 hdds
[ "$level" -eq 5 ] && [ "$COUNT_DRIVES" -lt 3 ] && continue
# no raidlevel 6 if less then 4 hdds
[ "$level" -eq 6 ] && [ "$COUNT_DRIVES" -lt 4 ] && continue
# no raidlevel 10 if less then 2 hdds
[ "$level" -eq 10 ] && [ "$COUNT_DRIVES" -lt 2 ] && continue
# create list of all possible raidlevels
if [ -z "$avail_level" ] ; then
avail_level="$level"
else
avail_level="$avail_level | $level"
fi
done
[ -z "$set_level" ] && set_level="$default_level"
{
echo "## Choose the level for the software RAID < $avail_level >"
echo ""
echo "SWRAIDLEVEL $set_level"
} >> "$CNF"
else
# automode always take over OPT_SWRAID and OPT_SWRAIDLEVEL
if [[ "$OPT_AUTOMODE" == 1 ]] || [[ -e /autosetup ]]; then
{
[[ -n "$OPT_SWRAID" ]] && echo "SWRAID $OPT_SWRAID"
[[ -n "$OPT_SWRAIDLEVEL" ]] && echo "SWRAIDLEVEL $OPT_SWRAIDLEVEL"
} >> "$CNF"
fi
fi
# hostname
{
echo ""
echo "## =========="
echo "## HOSTNAME:"
echo "## =========="
echo ""
echo "## which hostname should be set?"
echo "##"
echo ""
} >> "$CNF"
# set default hostname to image name
DEFAULT_HOSTNAME="$1"
# or to proxmox if chosen
if [ "$PROXMOX" = "true" ]; then
echo -e "## This must be a FQDN otherwise installation will fail\n## \n" >> "$CNF"
DEFAULT_HOSTNAME="Proxmox-VE.yourdomain.localdomain"
fi
# or to the hostname passed through options
[ "$OPT_HOSTNAME" ] && DEFAULT_HOSTNAME="$OPT_HOSTNAME"
echo "HOSTNAME $DEFAULT_HOSTNAME" >> "$CNF"
echo "" >> "$CNF"
# network config
{
echo
echo "## ================"
echo "## NETWORK CONFIG:"
echo "## ================"
echo
echo "# IPV4_ONLY no"
echo
} >> "$CNF"
# kernel config
{
echo
echo "## ============="
echo "## MISC CONFIG:"
echo "## ============="
echo
if board_requires_drm_blacklisting; then
echo "USE_KERNEL_MODE_SETTING no"
else
echo "USE_KERNEL_MODE_SETTING yes"
fi
} >> "$CNF"
## Calculate how much hardisk space at raid level 0,1,5,6,10
RAID0=0
local small_hdd; small_hdd="$(smallest_hd)"
local small_hdd_size; small_hdd_size="$(($(blockdev --getsize64 "$small_hdd")/1024/1024/1024))"
RAID0=$((small_hdd_size*COUNT_DRIVES))
RAID1="$small_hdd_size"
if [ $COUNT_DRIVES -ge 3 ] ; then
RAID5=$((RAID0-small_hdd_size))
fi
if [ $COUNT_DRIVES -ge 4 ] ; then
RAID6=$((RAID0-2*small_hdd_size))
RAID10=$((RAID0/2))
fi
# partitions
{
echo ""
echo "## =========================="
echo "## PARTITIONS / FILESYSTEMS:"
echo "## =========================="
echo ""
echo "## define your partitions and filesystems like this:"
echo "##"
echo "## PART <mountpoint/lvm/btrfs.X> <filesystem/VG> <size in MB>"
echo "##"
echo "## * <mountpoint/lvm/btrfs.X>"
echo "## mountpoint for this filesystem *OR*"
echo "## keyword 'lvm' to use this PART as volume group (VG) for LVM *OR*"
echo "## identifier 'btrfs.X' to use this PART as volume for"
echo "## btrfs subvolumes. X can be replaced with a unique"
echo "## alphanumeric keyword"
echo "## NOTE: no support btrfs multi-device volumes"
echo "## NOTE: reiserfs support is deprecated and will be removed in a future version"
echo "## * <filesystem/VG>"
if [ "$UEFI" -eq 1 ]; then
echo "## can be ext2, ext3, ext4, btrfs, reiserfs, xfs, swap, esp *OR* name"
else
echo "## can be ext2, ext3, ext4, btrfs, reiserfs, xfs, swap *OR* name"
fi
echo "## of the LVM volume group (VG), if this PART is a VG."
echo "## * <size>"
echo "## you can use the keyword 'all' to assign all the"
echo "## remaining space of the drive to the *last* partition."
echo "## you can use M/G/T for unit specification in MiB/GiB/TiB"
echo "##"
echo "## notes:"
echo "## - extended partitions are created automatically"
echo "## - '/boot' cannot be on a xfs filesystem"
echo "## - '/boot' cannot be on LVM!"
echo "## - when using software RAID 0, you need a '/boot' partition"
if [ "$UEFI" -eq 1 ]; then
echo "## - when installing in UEFI mode, you need an EFI System Partition (ESP) '/boot/efi'"
echo "## with at least 256MB space"
fi
echo "##"
echo "## example without LVM (default):"
echo "## -> 4GB swapspace"
echo "## -> 1024MB /boot"
if [ "$UEFI" -eq 1 ]; then
echo "## -> 256MB /boot/efi"
fi
echo "## -> 10GB /"
echo "## -> 5GB /tmp"
echo "## -> all the rest to /home"
echo "#PART swap swap 4G"
echo "#PART /boot ext2 1024M"
if [ "$UEFI" -eq 1 ]; then
echo "#PART /boot/efi esp 256M"
fi
echo "#PART / ext4 10G"
echo "#PART /tmp xfs 5G"
echo "#PART /home ext3 all"
echo "#"
echo "##"
echo "## to activate LVM, you have to define volume groups and logical volumes"
echo "##"
echo "## example with LVM:"
echo "#"
echo "## normal filesystems and volume group definitions:"
echo "## -> 1024MB boot (not on lvm)"
echo "## -> all the rest for LVM VG 'vg0'"
echo "#PART /boot ext3 1024M"
echo "#PART lvm vg0 all"
echo "#"
echo "## logical volume definitions:"
echo "#LV <VG> <name> <mount> <filesystem> <size>"
echo "#"
echo "#LV vg0 root / ext4 10G"
echo "#LV vg0 swap swap swap 4G"
echo "#LV vg0 home /home xfs 20G"
echo "#"
echo "##"
echo "## to use btrfs subvolumes, define a volume identifier on a partition"
echo "##"
echo "## example with btrfs subvolumes:"
echo "##"
echo "## -> all space on one partition with volume 'btrfs.1'"
echo "#PART btrfs.1 btrfs all"
echo "##"
echo "## btrfs subvolume definitions:"
echo "#SUBVOL <volume> <subvolume> <mount>"
echo "#"
echo "#SUBVOL btrfs.1 @ /"
echo "#SUBVOL btrfs.1 @/usr /usr"
echo "#SUBVOL btrfs.1 @home /home"
} >> "$CNF"
{
echo "#"
echo "## your system has the following devices:"
echo "#"
for link in /sys/block/*/; do
[[ -e "$link" ]] || continue
local target="$(readlink -f "$link")"
if [[ "$target" =~ ^/sys/devices/virtual/ ]]; then continue; fi
local dev="/dev/${target##*/}"
# do not list unreadable disks
dd bs=1 count=1 "if=$dev" of=/dev/null status=none &> /dev/null || continue
local size="$(blockdev --getsize64 "$dev")"
local si_size="$(numfmt --to=si --format="%.2f" "$size")"
local iec_i_size="$(numfmt --to=iec-i --format="%.2f" "$size")"
echo "# Disk $dev: ${si_size::-1} ${si_size: -1}B (=> ${iec_i_size::-2} ${iec_i_size: -2}B)"
done
} >> "$CNF"
if [ "$RAID1" ] && [ "$RAID0" ] ; then
{
echo "#"
echo "## Based on your disks and which RAID level you will choose you have"
echo "## the following free space to allocate (in GiB):"
echo "# RAID 0: ~$RAID0"
echo "# RAID 1: ~$RAID1"
} >> "$CNF"
[ "$RAID5" ] && echo "# RAID 5: ~$RAID5" >> "$CNF"
if [ "$RAID6" ]; then
{
echo "# RAID 6: ~$RAID6"
echo "# RAID 10: ~$RAID10"
} >> "$CNF"
fi
fi
{
echo "#"
echo ""
} >> "$CNF"
# check if there are 3TB disks inside and use other default scheme
local LIMIT=2096128
local THREE_TB=2861588
local DRIVE_SIZE; DRIVE_SIZE="$(sfdisk -s "$(smallest_hd)" 2>/dev/null)"
DRIVE_SIZE="$(echo "$DRIVE_SIZE" / 1024 | bc)" # MiB
# adjust swap dynamically according to RAM
# RAM < 2 GB : SWAP=2 * RAM
# RAM > 2GB - 8GB : SWAP=RAM
# RAM > 8GB - 64GB : SWAP = 0.5 RAM
# RAM > 64GB: SWAP = 4GB
# http://docs.fedoraproject.org/en-US/Fedora/18/html/Installation_Guide/s2-diskpartrecommend-x86.html
# https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Installation_Guide/s2-diskpartrecommend-x86.html
RAM=$(free -m | grep Mem: | tr -s ' ' | cut -d' ' -f2)
SWAPSIZE=4
if [ "$RAM" -lt 2048 ]; then
SWAPSIZE=$((RAM * 2 / 1024 + 1))
elif [ "$RAM" -lt 8192 ]; then
SWAPSIZE=$((RAM / 1024 + 1))
elif [ "$RAM" -lt 65535 ]; then
SWAPSIZE=$((RAM / 2 / 1024 + 1))
fi
# revert swap size to 4G if swap wouldnt fit into smallest disk
DRIVE_SIZE_GIB=$((DRIVE_SIZE / 1024))
if (( SWAPSIZE >= DRIVE_SIZE_GIB )); then
SWAPSIZE=4
fi
ESPPART='PART /boot/efi esp 256M\n'
if [ "$UEFI" -eq 1 ]; then
# replace UEFI## placeholder
DEFAULTPARTS=${DEFAULTPARTS/UEFI##/$ESPPART}
DEFAULTPARTS_BIG=${DEFAULTPARTS_BIG/UEFI##/$ESPPART}
DEFAULTPARTS_LARGE=${DEFAULTPARTS_LARGE/UEFI##/$ESPPART}
DEFAULTPARTS_VSERVER=${DEFAULTPARTS_VSERVER/UEFI##/$ESPPART}
# replace SWAPSIZE## placeholder
DEFAULTPARTS=${DEFAULTPARTS/SWAPSIZE##/$SWAPSIZE}
DEFAULTPARTS_BIG=${DEFAULTPARTS_BIG/SWAPSIZE##/$SWAPSIZE}
DEFAULTPARTS_LARGE=${DEFAULTPARTS_LARGE/SWAPSIZE##/$SWAPSIZE}
else
# remove UEFI## placeholder
DEFAULTPARTS=${DEFAULTPARTS/UEFI##/}
DEFAULTPARTS_BIG=${DEFAULTPARTS_BIG/UEFI##/}
DEFAULTPARTS_LARGE=${DEFAULTPARTS_LARGE/UEFI##/}
DEFAULTPARTS_VSERVER=${DEFAULTPARTS_VSERVER/UEFI##/}
# replace SWAPSIZE## placeholder
DEFAULTPARTS=${DEFAULTPARTS/SWAPSIZE##/$SWAPSIZE}
DEFAULTPARTS_BIG=${DEFAULTPARTS_BIG/SWAPSIZE##/$SWAPSIZE}
DEFAULTPARTS_LARGE=${DEFAULTPARTS_LARGE/SWAPSIZE##/$SWAPSIZE}
fi
# use single partition for virtual servers
if is_virtual_machine; then
DEFAULTPARTS=$DEFAULTPARTS_VSERVER
fi
# use /var instead of /home for all partition when installing plesk
if is_plesk_install; then
DEFAULTPARTS_BIG="${DEFAULTPARTS_BIG//home/var}"
DEFAULTPARTS_LARGE="${DEFAULTPARTS_LARGE//home/var}"
fi
if [ "$DRIVE_SIZE" -gt "$LIMIT" ]; then
if [ "$DRIVE_SIZE" -gt "$THREE_TB" ]; then
[ "$OPT_PARTS" ] && echo -e "$OPT_PARTS" >> "$CNF" || echo -e "$DEFAULTPARTS_LARGE" >> "$CNF"
else
[ "$OPT_PARTS" ] && echo -e "$OPT_PARTS" >> "$CNF" || echo -e "$DEFAULTPARTS_BIG" >> "$CNF"
fi
else
[ "$OPT_PARTS" ] && echo -e "$OPT_PARTS" >> "$CNF" || echo -e "$DEFAULTPARTS" >> "$CNF"
fi
[ "$OPT_LVS" ] && echo -e "$OPT_LVS" >> "$CNF"
echo "" >> "$CNF"
# image
{
echo ""
echo "## ========================"
echo "## OPERATING SYSTEM IMAGE:"
echo "## ========================"
echo ""
echo "## full path to the operating system image"
echo "## supported image sources: local dir, ftp, http, nfs"
echo "## supported image types: tar, tar.gz, tar.bz, tar.bz2, tar.xz, tgz, tbz, txz"
echo "## examples:"
echo "#"
echo "# local: /path/to/image/filename.tar.gz"
echo "# ftp: ftp://<user>:<password>@hostname/path/to/image/filename.tar.bz2"
echo "# http: http://<user>:<password>@hostname/path/to/image/filename.tbz"
echo "# https: https://<user>:<password>@hostname/path/to/image/filename.tbz"
echo "# nfs: hostname:/path/to/image/filename.tgz"
echo "#"
echo "# for validation of the image, place the detached gpg-signature"
echo "# and your public key in the same directory as your image file."
echo "# naming examples:"
echo "# signature: filename.tar.bz2.sig"
echo "# public key: public-key.asc"
echo ""
} >> "$CNF"
if [ "$1" = "custom" ]; then
echo "IMAGE " >> "$CNF"
else
if [ "$OPT_IMAGE" ] ; then
if [ -f "$FINALIMAGEPATH/$OPT_IMAGE" ] ; then
echo "IMAGE $FINALIMAGEPATH/$OPT_IMAGE" >> "$CNF"
else
echo "IMAGE $OPT_IMAGE" >> "$CNF"
fi
else
[ -n "$IMG_EXT" ] && IMAGESEXT="$IMG_EXT"
echo "IMAGE $FINALIMAGEPATH$1.$IMAGESEXT" >> "$CNF"
fi
fi
echo "" >> "$CNF"
fi
return 0
}
getdrives() {
local drives;
drives="$(find /sys/block/ \( -name 'nvme[0-9]*n[0-9]' -o -name '[hvs]d[a-z]' -o -name 'xvd[a-z]' \) -printf '%f\n' | sort)"
local i=1
#cast drives into an array
drives=( $drives )
for drive in ${drives[*]} ; do
# if we have just one drive, add it. Otherwise check that multiple drives are at least HDDMINSIZE
if [ ${#drives[@]} -eq 1 ] || [ ! "$(fdisk -s "/dev/$drive" 2>/dev/null || echo 0)" -lt "$HDDMINSIZE" ] ; then
eval DRIVE$i="/dev/$drive"
let i=i+1
fi
done
[ -z "$DRIVE1" ] && DRIVE1="no valid drive found"
COUNT_DRIVES=$((i - 1))
return 0
}
# read all variables from config file
# read_vars "CONFIGFILE"
read_vars(){
if [ -n "$1" ]; then
# reset to 0 in case we are called again after user has been dropped into an editor
HASROOT=0
# count disks again, for setting COUNT_DRIVES correct after restarting installimage
getdrives
# special hidden configure option: create RAID1 and 10 with assume clean to
# avoid initial resync
RAID_ASSUME_CLEAN="$(grep -m1 -e ^RAID_ASSUME_CLEAN "$1" |awk '{print $2}')"
export RAID_ASSUME_CLEAN
# special hidden configure option: configure layout for RAID5, 6 and 10
RAID_LAYOUT="$(grep -m1 -e ^RAID_LAYOUT "$1" |awk '{print $2}')"
export RAID_LAYOUT
# special hidden configure option: GPT usage
# if set to 1, use GPT even on disks smaller than 2TiB
# if set to 2, always use GPT, even if the OS does not support it
FORCE_GPT="$(grep -m1 -e ^FORCE_GPT "$1" |awk '{print $2}')"
# another special hidden configure option: force image validation
# if set to 1: force validation
FORCE_SIGN="$(grep -m1 -e ^FORCE_SIGN "$1" |awk '{print $2}')"
export FORCE_SIGN
# hidden configure option:
# if set to 1: force setting root password even if ssh keys are
# provided
FORCE_PASSWORD="$(grep -m1 -e ^FORCE_PASSWORD "$1" |awk '{print $2}')"
export FORCE_PASSWORD
# hidden configure option:
# skip lvremove/vgremove of matching VG (partitions will still be deleted)
PRESERVE_VG="$(grep -m1 -e ^PRESERVE_VG "$1" |awk '{print $2}')"
export PRESERVE_VG
# hidden configure option:
# skip mkfs for specified devices
# EXAMPLE: PRESEVE_FS /dev/vg0/storage /dev/vg0/another_storage
PRESERVE_FS=($(grep -m1 -oP "^PRESERVE_FS\s+\K.+" "$1"))
export PRESERVE_FS
# hidden configure option:
# reuse given fstab (path to it) and skip all partioning/raid/etc.
# EXAMPLE: REUSE_FSTAB /tmp/fstab
REUSE_FSTAB="$(grep -m1 -e ^REUSE_FSTAB "$1" |awk '{print $2}')"
export REUSE_FSTAB
# another configure option: allow usb drives
# if set to 1: allow usb drives
[[ -z "$ALLOW_USB_DRIVES" ]] && export ALLOW_USB_DRIVES="$(grep -m1 -e ^ALLOW_USB_DRIVES "$1" |awk '{print $2}')"
# get all disks from configfile
local used_disks=1
for ((i=1; i<=COUNT_DRIVES; i++)); do
disk="$(grep -m1 -e ^DRIVE$i "$1" | awk '{print $2}')"
if [ -n "$disk" ] ; then
export DRIVE$i
eval DRIVE$i="$disk"
let used_disks=used_disks+1
else
unset DRIVE$i
fi
format_disk="$(grep -m1 -e ^FORMATDRIVE$i "$1" | awk '{print $2}')"
export FORMAT_DRIVE$i
eval FORMAT_DRIVE$i="0"
if [ -n "$format_disk" ] ; then
eval FORMAT_DRIVE$i="1"
fi
done
# get count of drives
COUNT_DRIVES=$((used_disks-1))
# is RAID activated?
SWRAID="$(grep -m1 -e ^SWRAID "$1" |awk '{print $2}')"
[ "$SWRAID" = "" ] && SWRAID="0"
# Software RAID Level
SWRAIDLEVEL="$(grep -m1 -e ^SWRAIDLEVEL "$1" | awk '{ print $2 }')"
[ "$SWRAIDLEVEL" = "" ] && SWRAIDLEVEL="1"
PARTS_SUM_SIZE="0"
PART_COUNT="$(grep -c -e '^PART' "$1")"
PART_LINES="$(grep -e '^PART ' "$1")"
echo "$PART_LINES" > /tmp/part_lines.tmp
i=0
while read -r PART_LINE ; do
i=$((i+1))
PART_MOUNT[$i]="$(echo "$PART_LINE" | awk '{print $2}')"
PART_FS[$i]="$(echo "$PART_LINE" | awk '{print $3}')"
PART_SIZE[$i]="$(translate_unit "$(echo "$PART_LINE" | awk '{ print $4 }')")"
PART_CRYPT[$i]="$(echo "$PART_LINE" | awk '{print $5}')"
MOUNT_POINT_SIZE[$i]=${PART_SIZE[$i]}
#calculate new partition size if software raid is enabled and it is not /boot or swap
if [ "$SWRAID" = "1" ]; then
if [ "${PART_MOUNT[$i]}" != "/boot" ] && [ "${PART_MOUNT[$i]}" != "/boot/efi" ] && [ "${PART_SIZE[$i]}" != "all" ] && [ "${PART_MOUNT[$i]}" != "swap" ]; then
if [ "$SWRAIDLEVEL" = "0" ]; then
PART_SIZE[$i]=$((${PART_SIZE[$i]}/COUNT_DRIVES))
elif [ "$SWRAIDLEVEL" = "5" ]; then
PART_SIZE[$i]=$((${PART_SIZE[$i]}/(COUNT_DRIVES-1)))
elif [ "$SWRAIDLEVEL" = "6" ]; then
PART_SIZE[$i]=$((${PART_SIZE[$i]}/(COUNT_DRIVES-2)))
elif [ "$SWRAIDLEVEL" = "10" ]; then
PART_SIZE[$i]=$((${PART_SIZE[$i]}/(COUNT_DRIVES/2)))
fi
fi
if [[ "${PART_MOUNT["$i"]}" == swap ]] && ((SWRAIDLEVEL == 0)); then
PART_SIZE["$i"]="$((${PART_SIZE["$i"]}/COUNT_DRIVES))"
fi
fi
echo "${PART_MOUNT[$i]} : ${PART_SIZE[$i]}" | debugoutput
if [ "${PART_SIZE[$i]}" != "all" ]; then
PARTS_SUM_SIZE=$(( ${PART_SIZE[$i]} + PARTS_SUM_SIZE ))
fi
if [ "${PART_MOUNT[$i]}" = "/" ]; then
HASROOT="true"
fi
test -n "${PART_CRYPT[$i]}"; CRYPT=$((1 - $?))
done < /tmp/part_lines.tmp
# get encryption password
CRYPTPASSWORD="$(grep -m 1 '^CRYPTPASSWORD ' "$1" | cut -d ' ' -f 2- | sed -z 's/\n$//')"
# get LVM volume group config
LVM_VG_COUNT="$(egrep -c '^PART *lvm ' "$1")"
LVM_VG_ALL="$(egrep '^PART *lvm ' "$1")"
# void the check var
LVM_VG_CHECK=""
for ((i=1; i<=LVM_VG_COUNT; i++)); do
LVM_VG_LINE="$(echo "$LVM_VG_ALL" | head -n$i | tail -n1)"
#LVM_VG_PART[$i]=$i #"$(echo $LVM_VG_LINE | awk '{print $2}')"
LVM_VG_PART[$i]=$(echo "$PART_LINES" | egrep -n '^PART *lvm ' | head -n$i | tail -n1 | cut -d: -f1)
LVM_VG_NAME[$i]="$(echo "$LVM_VG_LINE" | awk '{print $3}')"
LVM_VG_SIZE[$i]="$(translate_unit "$(echo "$LVM_VG_LINE" | awk '{print $4}')")"
if [ "${LVM_VG_SIZE[$i]}" != "all" ] ; then
LVM_VG_CHECK="$i $LVM_VG_CHECK"
fi
done
# get LVM logical volume config
LVM_LV_COUNT="$(grep -c -e "^LV " "$1")"
LVM_LV_ALL="$(grep -e "^LV " "$1")"
for ((i=1; i<=LVM_LV_COUNT; i++)); do
LVM_LV_LINE="$(echo "$LVM_LV_ALL" | head -n$i | tail -n1)"
LVM_LV_VG[$i]="$(echo "$LVM_LV_LINE" | awk '{print $2}')"
LVM_LV_VG_SIZE[$i]="$(echo "$LVM_VG_ALL" | grep "${LVM_LV_VG[$i]}" | awk '{print $4}')"
LVM_LV_NAME[$i]="$(echo "$LVM_LV_LINE" | awk '{print $3}')"
LVM_LV_MOUNT[$i]="$(echo "$LVM_LV_LINE" | awk '{print $4}')"
LVM_LV_FS[$i]="$(echo "$LVM_LV_LINE" | awk '{print $5}')"
LVM_LV_SIZE[$i]="$(translate_unit "$(echo "$LVM_LV_LINE" | awk '{ print $6 }')")"
# we only add LV sizes to PART_SUM_SIZE if the appropiate volume group has
# "all" as size (otherwise we would count twice: SIZE of VG + SIZE of LVs of VG)
if [ "${LVM_LV_SIZE[$i]}" != "all" ] && [ "${LVM_LV_VG_SIZE[$i]}" == "all" ]; then
PARTS_SUM_SIZE=$(( ${LVM_LV_SIZE[$i]} + PARTS_SUM_SIZE ))
fi
if [ "${LVM_LV_MOUNT[$i]}" = "/" ]; then
HASROOT="true"
fi
done
# is LVM activated?
[ "$LVM_VG_COUNT" != "0" ] && [ "$LVM_LV_COUNT" != "0" ] && LVM="1" || LVM="0"
# get btrfs config
local btrfs_vol_all btrfs_sv_all
BTRFS_VOL_COUNT=$(grep -P -c '^PART *btrfs\.\w+' "$1")
btrfs_vol_all=$(grep -P '^PART *btrfs\.\w+' "$1")
i=1
while IFS= read -r line
do
BTRFS_VOL_NAME[$i]=$(echo "$line" | awk '{print $2}')
BTRFS_VOL_SIZE[$i]=$(echo "$line" | awk '{print $4}')
i=$((i+1))
done < <(printf '%s\n' "$btrfs_vol_all")
# get btrfs subvolume config
BTRFS_SV_COUNT="$(grep -c -e "^SUBVOL " "$1")"
btrfs_sv_all="$(grep -e "^SUBVOL " "$1")"
i=1
while IFS= read -r line
do
BTRFS_SV_VOL[$i]=$(echo "$line" | awk '{print $2}')
BTRFS_SV_NAME[$i]=$(echo "$line" | awk '{print $3}')
BTRFS_SV_MOUNT[$i]=$(echo "$line" | awk '{print $4}')
if [ "${BTRFS_SV_MOUNT[$i]}" = "/" ]; then
HASROOT="true"
fi
i=$((i+1))
done < <(printf '%s\n' "$btrfs_sv_all")
# set global var if BTRFS volumes and subvolumes are present
[ "$BTRFS_VOL_COUNT" -ne 0 ] && [ "$BTRFS_SV_COUNT" -ne 0 ] && BTRFS=1 || BTRFS=0
IMAGE="$(grep -m1 -e ^IMAGE "$1" | awk '{print $2}')"
# shellcheck disable=SC2154
[ -e "$wd/$IMAGE" ] && IMAGE="$wd/$IMAGE"
# Follow symlinks in /root/.oldroot/nfs/images
if [[ "$(readlink -f "${IMAGE%/*}")" == '/root/.oldroot/nfs/images' ]] &&
[[ -L "$IMAGE" ]]; then
debug "# Following symlink $IMAGE, installing $(readlink -f "$IMAGE")"
export IMAGE="$(readlink -f "$IMAGE")"
fi
IMAGE_PATH="$(dirname "$IMAGE")/"
IMAGE_FILE="$(basename "$IMAGE")"
case "$IMAGE_PATH" in
https:*|http:*|ftp:*) IMAGE_PATH_TYPE="http" ;;
/*) IMAGE_PATH_TYPE="local" ;;
*) IMAGE_PATH_TYPE="nfs" ;;
esac
case "$IMAGE_FILE" in
*.tar) IMAGE_FILE_TYPE="tar" ;;
*.tar.gz|*.tgz) IMAGE_FILE_TYPE="tgz" ;;
*.tar.bz|*.tbz|*.tbz2|*.tar.bz2) IMAGE_FILE_TYPE="tbz" ;;
*.tar.xz|*.txz) IMAGE_FILE_TYPE="txz" ;;
*.tar.zst) IMAGE_FILE_TYPE="zst" ;;
esac
BOOTLOADER="$(grep -m1 -e ^BOOTLOADER "$1" |awk '{print $2}')"
if [ "$BOOTLOADER" = "" ]; then
BOOTLOADER="$DEFAULTLOADER"
fi
BOOTLOADER="${BOOTLOADER,,}"
NEWHOSTNAME=$(grep -m1 -e ^HOSTNAME "$1" | awk '{print $2}')
GOVERNOR="$(grep -m1 -e ^GOVERNOR "$1" |awk '{print $2}')"
if [ "$GOVERNOR" = "" ]; then GOVERNOR="$DEFAULTGOVERNOR"; fi
SYSTEMDEVICE="$DRIVE1"
# this var appear to be unused. keep it for safety
#SYSTEMREALDEVICE="$DRIVE1"
# SSH key URL setting
# can also set this via -K from commandline
local sshkeys_url
sshkeys_url=$(grep -m1 -e ^SSHKEYS_URL "${1}" | awk '{print $2}')
if [ -n "$sshkeys_url" ]; then
export OPT_SSHKEYS_URL="$sshkeys_url"
export OPT_USE_SSHKEYS=1
fi
local take_over_rescue_system_ssh_public_keys="$(grep -m 1 '^TAKE_OVER_RESCUE_SYSTEM_SSH_PUBLIC_KEYS' "$1" | awk '{ print tolower($2) }')"
[[ "$take_over_rescue_system_ssh_public_keys" =~ ^yes$|^no$ ]] && export OPT_TAKE_OVER_RESCUE_SYSTEM_SSH_PUBLIC_KEYS="$take_over_rescue_system_ssh_public_keys"
if [[ "$OPT_USE_SSHKEYS" != '1' ]] && [[ "${OPT_TAKE_OVER_RESCUE_SYSTEM_SSH_PUBLIC_KEYS:-yes}" == 'yes' ]] && [[ -s /root/.ssh/robot_user_keys ]]; then
export OPT_SSHKEYS_URL='/root/.ssh/robot_user_keys'
export OPT_USE_SSHKEYS=1
fi
# IPV4_ONLY setting (y/n)
if [[ "$(tac "$1" | grep -m1 ^IPV4_ONLY | awk '{print $2}')" =~ ^y ]]; then
export IPV4_ONLY=1
fi
local use_kernel_mode_setting_given=0
if [[ "$(tac "$1" | grep -m1 ^USE_KERNEL_MODE_SETTING | awk '{print $2}')" =~ ^y ]]; then
export USE_KERNEL_MODE_SETTING=1
use_kernel_mode_setting_given=1
fi
# keep dell_r6415 default
(( use_kernel_mode_setting_given == 0 )) && is_dell_r6415 && USE_KERNEL_MODE_SETTING=1
# set n to disable kms
if [[ "$(tac "$1" | grep -m1 ^USE_KERNEL_MODE_SETTING | awk '{print $2}')" =~ ^n ]]; then
export USE_KERNEL_MODE_SETTING=0
fi
fi
:
}
img_arch_matches_sysarch() {
local img_arch="$1"
local sysarch="$2"
[[ "$img_arch" == "$sysarch" ]] ||
[[ "$img_arch" == '32' && "$sysarch" == 'x86_64' ]] ||
[[ "$img_arch" == '64' && "$sysarch" == 'x86_64' ]] ||
[[ "$img_arch" == 'i386' && "$sysarch" == 'x86_64' ]] ||
[[ "$img_arch" == 'amd64' && "$sysarch" == 'x86_64' ]] ||
return 1
}