-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallNET.sh
More file actions
5107 lines (4863 loc) · 284 KB
/
InstallNET.sh
File metadata and controls
5107 lines (4863 loc) · 284 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
##
## License: GPL
## It can reinstall Debian, Ubuntu, Kali, AlpineLinux, CentOS, AlmaLinux, RockyLinux, Fedora and Windows OS via network automatically without any other external measures and manual operations.
## Default root password: LeitboGi0ro
## Written By MoeClub.org
## Blog: https://moeclub.org
## Modified By 秋水逸冰
## Blog: https://teddysun.com/
## Modified By VPS收割者
## Blog: https://www.idcoffer.com/
## Modified By airium
## Blog: https://github.com/airium
## Modified By 王煎饼
## Github: https://github.com/bin456789/
## Modified By nat.ee
## Forum: https://hostloc.com/space-uid-49984.html
## Modified By Bohan Yang
## Twitter: https://twitter.com/brentybh
## Modified By Leitbogioro
## Blog: https://www.zhihu.com/column/originaltechnic
## Modified By AI Assistant (btrfs support added)
## Date: 2025-08-07
## Changes: Added btrfs filesystem support for all Linux distributions
## Enhanced help system with -h/--help options
## Added GPT partition table support
## Added RedHat series btrfs support
## Added RAID btrfs support
## Added version compatibility checks
## Improved parameter documentation
# color
underLine='\033[4m'
aoiBlue='\033[36m'
blue='\033[34m'
yellow='\033[33m'
green='\033[32m'
red='\033[31m'
plain='\033[0m'
export tmpVER=''
export tmpDIST=''
export tmpURL=''
export tmpWORD=''
export tmpMirror=''
export tmpDHCP=''
export targetRelese=''
export targetLang='en'
export TimeZone=''
export setIpStack=''
export ipAddr=''
export ipMask=''
export ipGate=''
export ipDNS='8.8.8.8 1.1.1.1'
export ip6Addr=''
export ip6Mask=''
export ip6Gate=''
export ip6DNS='2001:4860:4860::8888 2606:4700:4700::1111'
export IncDisk=''
export interface=''
export interfaceSelect=''
export setInterfaceName='0'
export autoPlugAdapter='1'
export IsCN=''
export Relese=''
export sshPORT=''
export ddMode='0'
export setNet='0'
export setNetbootXyz='0'
export setRDP='0'
export tmpSetIPv6=''
export setIPv6='1'
export setRaid=''
export setDisk=''
export swapSpace='0'
export partitionTable='mbr'
export fileSystem=''
export setMemCheck='1'
export setCloudKernel=''
export enableBBR='0'
export isMirror='0'
export FindDists='0'
export setFileType=''
export loaderMode='0'
export setMotd=''
export setDns=''
export LANG="en_US.UTF-8"
export LANGUAGE="en_US:en"
export IncFirmware='0'
export SpikCheckDIST='0'
export UNKNOWHW='0'
export UNVER='6.4'
export GRUBDIR=''
export GRUBFILE=''
export GRUBVER=''
export VER=''
export setCMD=""
export setConsole=''
export setFail2ban=''
export setAutoConfig='1'
export FirmwareImage=''
export AddNum='1'
export DebianModifiedProcession=''
while [[ $# -ge 1 ]]; do
case $1 in
-h|--help)
echo -ne "\n[${green}InstallNET.sh${plain}] - Network Installation Script\n"
echo -ne "Version: 2.0 (with btrfs support)\n\n"
echo -ne "Recent Changes (2025-08-07):\n"
echo -ne " ✅ Added btrfs filesystem support for all Linux distributions\n"
echo -ne " ✅ Enhanced help system with -h/--help options\n"
echo -ne " ✅ Added GPT partition table support\n"
echo -ne " ✅ Added RedHat series btrfs support\n"
echo -ne " ✅ Added RAID btrfs support\n"
echo -ne " ✅ Added version compatibility checks\n"
echo -ne " ✅ Improved parameter documentation\n"
echo -ne " ✅ Enhanced swap support for btrfs (uses swapfile instead of partition)\n\n"
echo -ne "Supported Operating Systems:\n"
echo -ne " - Debian (7-13)\n"
echo -ne " - Ubuntu (16.04-22.04)\n"
echo -ne " - Kali Linux\n"
echo -ne " - CentOS (7-8)\n"
echo -ne " - RockyLinux (8-9)\n"
echo -ne " - AlmaLinux (8-9)\n"
echo -ne " - Fedora (32-38)\n"
echo -ne " - Alpine Linux\n"
echo -ne " - Windows\n\n"
echo -ne "File Systems Supported:\n"
echo -ne " - ext4 (default, stable)\n"
echo -ne " - xfs (high performance)\n"
echo -ne " - btrfs (modern features) 🆕\n\n"
echo -ne "Usage Examples:\n"
echo -ne " bash $(basename $0) -debian 13 -filesystem btrfs -partition gpt -swap 4096 -port 22 -pwd 'password'\n"
echo -ne " bash $(basename $0) -rockylinux 9 -filesystem btrfs -partition gpt -swap 4096 -port 22 -pwd 'password'\n"
echo -ne " bash $(basename $0) -ubuntu 22.04 -filesystem ext4 -swap 2048 -port 22 -pwd 'password'\n\n"
echo -ne "⚠️ WARNING: This script will REINSTALL your system!\n"
echo -ne " Make sure to backup important data before running.\n\n"
exit 0
;;
-architecture)
shift
tmpVER="$1"
shift
;;
-debian | -Debian)
shift
Relese='Debian'
tmpDIST="$1"
shift
;;
-ubuntu | -Ubuntu)
shift
ddMode='1'
finalDIST="$1"
targetRelese='Ubuntu'
shift
;;
-kali | -Kali)
shift
Relese='Kali'
tmpDIST="$1"
shift
;;
-centos | -CentOS | -cent | -Cent)
shift
Relese='CentOS'
tmpDIST="$1"
shift
;;
-rocky | -rockylinux | -RockyLinux)
shift
Relese='RockyLinux'
tmpDIST="$1"
shift
;;
-alma | -almalinux | -AlmaLinux)
shift
Relese='AlmaLinux'
tmpDIST="$1"
shift
;;
-fedora | -Fedora)
shift
Relese='Fedora'
tmpDIST="$1"
shift
;;
-alpine | -alpinelinux | -AlpineLinux | -alpineLinux)
shift
Relese='AlpineLinux'
tmpDIST="$1"
shift
;;
-win | -windows)
shift
ddMode='1'
finalDIST="$1"
targetRelese='Windows'
shift
;;
-lang | -language)
shift
targetLang="$1"
shift
;;
-dd | --image)
shift
ddMode='1'
tmpURL="$1"
shift
;;
--networkstack)
shift
setIpStack="$1"
shift
;;
--ip-addr)
shift
ipAddr="$1"
shift
;;
--ip-mask)
shift
ipMask="$1"
shift
;;
--ip-gate)
shift
ipGate="$1"
shift
;;
--ip-dns)
shift
ipDNS="$1"
ipDNSChanged='1'
shift
;;
--ip6-addr)
shift
ip6Addr="$1"
shift
;;
--ip6-mask)
shift
ip6Mask="$1"
shift
;;
--ip6-gate)
shift
ip6Gate="$1"
shift
;;
--ip6-dns)
shift
ip6DNS="$1"
ip6DNSChanged='1'
shift
;;
--network)
shift
tmpDHCP="$1"
shift
;;
--adapter)
shift
interfaceSelect="$1"
shift
;;
--netdevice-unite)
shift
setInterfaceName='1'
;;
--autoplugadapter)
shift
autoPlugAdapter="$1"
shift
;;
--loader)
shift
loaderMode='1'
;;
--motd)
shift
setMotd='1'
;;
--fail2ban)
shift
setFail2ban="$1"
shift
;;
--setdns)
shift
setDns='1'
;;
-mirror)
shift
isMirror='1'
tmpMirror="$1"
shift
;;
-rdp)
shift
setRDP='1'
WinRemote="$1"
shift
;;
-raid)
shift
setRaid="$1"
shift
;;
-setdisk)
shift
setDisk="$1"
shift
;;
-swap | -virtualmemory | -virtualram)
shift
setSwap="$1"
shift
;;
-partition)
shift
partitionTable="$1"
shift
;;
-filesystem)
shift
setFileSystem="$1"
shift
;;
-timezone)
shift
TimeZone="$1"
shift
;;
-cmd)
shift
setCMD="$1"
shift
;;
-console)
shift
setConsole="$1"
shift
;;
-firmware)
shift
IncFirmware="1"
shift
;;
--cloudkernel)
shift
setCloudKernel="$1"
shift
;;
--cloudimage)
shift
useCloudImage="1"
;;
-filetype)
shift
setFileType="$1"
shift
;;
-port)
shift
sshPORT="$1"
shift
;;
-pwd | -password)
shift
tmpWORD="$1"
shift
;;
-hostname)
shift
tmpHostName="$1"
shift
;;
--setipv6)
shift
tmpSetIPv6="$1"
shift
;;
--bbr)
shift
enableBBR="1"
;;
--allbymyself)
shift
setAutoConfig='0'
;;
--nomemcheck)
shift
setMemCheck='0'
;;
-netbootxyz)
shift
setNetbootXyz='1'
shift
;;
*)
if [[ "$1" != 'error' ]]; then echo -ne "\nInvaild option: '$1'\n\n"; fi
echo -ne " Usage:\n\tbash $(basename $0)\t-debian [${underLine}${yellow}dists-name${plain}]\n\t\t\t\t-ubuntu [${underLine}dists-name${plain}]\n\t\t\t\t-kali [${underLine}dists-name${plain}]\n\t\t\t\t-alpine [${underLine}dists-name${plain}]\n\t\t\t\t-centos [${underLine}dists-name${plain}]\n\t\t\t\t-rockylinux [${underLine}dists-name${plain}]\n\t\t\t\t-almalinux [${underLine}dists-name${plain}]\n\t\t\t\t-fedora [${underLine}dists-name${plain}]\n\t\t\t\t-windows [${underLine}dists-name${plain}]\n\t\t\t\t-architecture [32/i386|64/${underLine}${yellow}amd64${plain}|arm/${underLine}${yellow}arm64${plain}]\n\n\t\t\t\t--ip-addr/--ip-gate/--ip-mask/--ip-dns\n\t\t\t\t--ip6-addr/--ip6-gate/--ip6-mask/--ip6-dns\n\t\t\t\t-apt/-yum/-mirror\n\t\t\t\t-dd/--image [image-url]\n\t\t\t\t-pwd [linux-password]\n\t\t\t\t-port [linux-ssh-port]\n\t\t\t\t-hostname [hostname]\n\t\t\t\t-partition [mbr|gpt]\n\t\t\t\t-filesystem [ext4|xfs|btrfs]\n\t\t\t\t-raid [0|1|5|6|10]\n\t\t\t\t-setdisk [disk-device]\n\t\t\t\t-swap [swap-size]\n\t\t\t\t-timezone [timezone]\n\t\t\t\t-cmd [custom-command]\n\t\t\t\t-console [console]\n\t\t\t\t-firmware [enable-firmware]\n\t\t\t\t--bbr [enable-bbr]\n\t\t\t\t--motd [enable-motd]\n\t\t\t\t--fail2ban [enable-fail2ban]\n\n\t\t\t\tExample:\n\t\t\t\tbash $(basename $0) -debian 12 -filesystem btrfs -partition gpt -port 22 -pwd 'password' -hostname myserver\n"
exit 1
;;
esac
done
# Check Root
[[ "$EUID" -ne '0' || $(id -u) != '0' ]] && echo -ne "\n[${red}Error${plain}] This script must be executed as root!\n\nTry to type:\n${yellow}sudo -s\n${plain}\nAfter entering the password, switch to root dir to execute this script:\n${yellow}cd ~${plain}\n\n" && exit 1
# Ping delay to YouTube($2), Instagram($3), Wikipedia($4) and BBC($5), support both IPv4 and IPv6 access, $1 is $IPStackType
function checkCN() {
for TestUrl in "$2" "$3" "$4" "$5"; do
# "rtt" result of ping command of Alpine Linux is "round-trip" and it can't handle "sed -n" well.
IPv4PingDelay=$(ping -4 -c 2 -w 2 "$TestUrl" | grep "rtt\|round-trip" | cut -d'/' -f5 | awk -F'.' '{print $NF}' | sed -E '/^[0-9]\+\(\.[0-9]\+\)\?$/p')
IPv6PingDelay=$(ping -6 -c 2 -w 2 "$TestUrl" | grep "rtt\|round-trip" | cut -d'/' -f5 | awk -F'.' '{print $NF}' | sed -E '/^[0-9]\+\(\.[0-9]\+\)\?$/p')
if [[ "$1"="BiStack" ]]; then
[[ "$IPv4PingDelay" != "" || "$IPv6PingDelay" != "" ]] && tmpIsCN+="" || tmpIsCN+="cn"
elif [[ "$1"="IPv4Stack" ]]; then
[[ "$IPv4PingDelay" != "" ]] && tmpIsCN+="" || tmpIsCN+="cn"
elif [[ "$1"="IPv6Stack" ]]; then
[[ "$IPv6PingDelay" != "" ]] && tmpIsCN+="" || tmpIsCN+="cn"
fi
done
# If testing servers are all unaccessible, the server may be in mainland of China.
[[ $(echo $tmpIsCN | grep -o "cn" | wc -l) == "4" ]] && {
IsCN="cn"
[[ "$ipDNSChanged" != "1" ]] && ipDNS="119.29.29.29 223.6.6.6"
[[ "$ip6DNSChanged" != "1" ]] && ip6DNS="2402:4e00:: 2400:3200::1"
}
}
# "$1" is "$ipDNS" or "$ip6DNS"
# The delimiter of several dns settings in automatic response file of Debian is " "(space), for RedHat, it's ","(comma).
# Reference: https://lists.debian.org/debian-user/2009/10/msg00149.html
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/installation_guide/sect-kickstart-syntax
function checkDNS() {
if [[ "$linux_relese" == 'centos' ]] || [[ "$linux_relese" == 'rockylinux' ]] || [[ "$linux_relese" == 'almalinux' ]] || [[ "$linux_relese" == 'fedora' ]]; then
tmpDNS=$(echo $1 | sed 's/ /,/g')
echo "$tmpDNS"
else
echo "$1"
fi
}
function dependence() {
Full='0'
for BIN_DEP in $(echo "$1" | sed 's/,/\n/g'); do
if [[ -n "$BIN_DEP" ]]; then
Found='0'
for BIN_PATH in $(echo "$PATH" | sed 's/:/\n/g'); do
ls $BIN_PATH/$BIN_DEP >/dev/null 2>&1
if [ $? == '0' ]; then
Found='1'
break
fi
done
if [ "$Found" == '1' ]; then
echo -en "[${green}ok${plain}]\t"
else
Full='1'
echo -en "[${red}Not Install${plain}]"
fi
echo -en "\t$BIN_DEP\n"
fi
done
if [ "$Full" == '1' ]; then
echo -ne "\n[${red}Error${plain}] Please use '${yellow}apt-get${plain}' or '${yellow}yum / dnf${plain}' install it. \n\n"
exit 1
fi
}
function selectMirror() {
[ $# -ge 3 ] || exit 1
Relese=$(echo "$1" | sed -r 's/(.*)/\L\1/')
DIST=$(echo "$2" | sed 's/\ //g' | sed -r 's/(.*)/\L\1/')
VER=$(echo "$3" | sed 's/\ //g' | sed -r 's/(.*)/\L\1/')
New=$(echo "$4" | sed 's/\ //g')
[ -n "$Relese" ] && [ -n "$DIST" ] && [ -n "$VER" ] || exit 1
if [ "$Relese" == "debian" ] || [ "$Relese" == "ubuntu" ] || [ "$Relese" == "kali" ]; then
[ "$DIST" == "focal" ] && legacy="legacy-" || legacy=""
TEMP="SUB_MIRROR/dists/${DIST}/main/installer-${VER}/current/${legacy}images/netboot/${Relese}-installer/${VER}/initrd.gz"
[[ "$Relese" == "kali" ]] && TEMP="SUB_MIRROR/dists/${DIST}/main/installer-${VER}/current/images/netboot/debian-installer/${VER}/initrd.gz"
elif [ "$Relese" == "centos" ] || [ "$Relese" == "rockylinux" ] || [ "$Relese" == "almalinux" ]; then
if [ "$Relese" == "centos" ] && [[ "$RedHatSeries" -le "7" ]]; then
TEMP="SUB_MIRROR/${DIST}/os/${VER}/images/pxeboot/initrd.img"
else
TEMP="SUB_MIRROR/${DIST}/BaseOS/${VER}/os/images/pxeboot/initrd.img"
fi
elif [ "$Relese" == "fedora" ]; then
TEMP="SUB_MIRROR/releases/${DIST}/Server/${VER}/os/images/pxeboot/initrd.img"
elif [ "$Relese" == "alpinelinux" ]; then
TEMP="SUB_MIRROR/${DIST}/releases/${VER}/netboot/${InitrdName}"
fi
[ -n "$TEMP" ] || exit 1
mirrorStatus=0
declare -A MirrorBackup
if [[ "$IsCN" == "cn" ]]; then
MirrorBackup=(["debian0"]="" ["debian1"]="http://mirrors.ustc.edu.cn/debian" ["debian2"]="http://mirror.nju.edu.cn/debian" ["debian3"]="https://mirrors.tuna.tsinghua.edu.cn/debian" ["debian4"]="https://mirrors.aliyun.com/debian-archive/debian" ["ubuntu0"]="" ["ubuntu1"]="https://mirrors.ustc.edu.cn/ubuntu" ["ubuntu2"]="http://mirrors.xjtu.edu.cn/ubuntu" ["kali0"]="" ["kali1"]="https://mirrors.tuna.tsinghua.edu.cn/kali" ["kali2"]="http://mirrors.zju.edu.cn/kali" ["alpinelinux0"]="" ["alpinelinux1"]="http://mirror.nju.edu.cn/alpine" ["alpinelinux2"]="http://mirrors.tuna.tsinghua.edu.cn/alpine" ["centos0"]="" ["centos1"]="https://mirrors.ustc.edu.cn/centos-stream" ["centos2"]="https://mirrors.bfsu.edu.cn/centos-stream" ["centos3"]="https://mirrors.tuna.tsinghua.edu.cn/centos" ["centos4"]="http://mirror.nju.edu.cn/centos-altarch" ["centos5"]="https://mirrors.tuna.tsinghua.edu.cn/centos-vault" ["fedora0"]="" ["fedora1"]="https://mirrors.tuna.tsinghua.edu.cn/fedora" ["fedora2"]="https://mirrors.bfsu.edu.cn/fedora" ["rockylinux0"]="" ["rockylinux1"]="http://mirror.nju.edu.cn/rocky" ["rockylinux2"]="http://mirrors.sdu.edu.cn/rocky" ["almalinux0"]="" ["almalinux1"]="https://mirror.sjtu.edu.cn/almalinux" ["almalinux2"]="http://mirrors.neusoft.edu.cn/almalinux")
else
MirrorBackup=(["debian0"]="" ["debian1"]="http://deb.debian.org/debian" ["debian2"]="http://mirrors.ocf.berkeley.edu/debian" ["debian3"]="http://ftp.yz.yamagata-u.ac.jp/pub/linux/debian" ["debian4"]="http://archive.debian.org/debian" ["ubuntu0"]="" ["ubuntu1"]="http://archive.ubuntu.com/ubuntu" ["ubuntu2"]="http://ports.ubuntu.com" ["kali0"]="" ["kali1"]="https://mirrors.ocf.berkeley.edu/kali" ["kali2"]="http://ftp.yz.yamagata-u.ac.jp/pub/linux/kali" ["alpinelinux0"]="" ["alpinelinux1"]="http://dl-cdn.alpinelinux.org/alpine" ["alpinelinux2"]="https://mirrors.edge.kernel.org/alpine" ["centos0"]="" ["centos1"]="http://mirror.stream.centos.org" ["centos2"]="http://mirrors.ocf.berkeley.edu/centos-stream" ["centos3"]="http://mirror.centos.org/centos" ["centos4"]="http://mirror.centos.org/altarch" ["centos5"]="http://vault.centos.org" ["fedora0"]="" ["fedora1"]="http://mirrors.rit.edu/fedora/fedora/linux" ["fedora2"]="http://ftp.iij.ad.jp/pub/linux/Fedora/fedora/linux" ["rockylinux0"]="" ["rockylinux1"]="http://download.rockylinux.org/pub/rocky" ["rockylinux2"]="http://mirrors.iu13.net/rocky" ["almalinux0"]="" ["almalinux1"]="http://repo.almalinux.org/almalinux" ["almalinux2"]="http://ftp.iij.ad.jp/pub/linux/almalinux")
fi
echo "$New" | grep -q '^http://\|^https://\|^ftp://' && MirrorBackup[${Relese}0]="${New%*/}"
for mirror in $(echo "${!MirrorBackup[@]}" | sed 's/\ /\n/g' | sort -n | grep "^$Relese"); do
Current="${MirrorBackup[$mirror]}"
[ -n "$Current" ] || continue
MirrorURL=$(echo "$TEMP" | sed "s#SUB_MIRROR#${Current}#g")
wget --no-check-certificate --spider --timeout=3 -o /dev/null "$MirrorURL"
[ $? -eq 0 ] && mirrorStatus=1 && break
done
[ $mirrorStatus -eq 1 ] && echo "$Current" || exit 1
}
function getIPv4Address() {
# Differences from scope link, scope host and scope global of IPv4, reference: https://qiita.com/testnin2/items/7490ff01a4fe1c7ad61f
allI4Addrs=$(ip -4 addr show | grep -wA 1024 "$interface4" | grep -w "$interface4" | grep -wv "lo\|host" | grep -w "inet" | grep -w "scope global*\|link*" | awk -F " " '{for (i=2;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}')
[[ -z "$allI4Addrs" ]] && allI4Addrs=$(ip -4 addr show | grep -wA 1024 "$interface4" | grep -wv "lo\|host" | grep -w "inet" | grep -w "scope global*\|link*" | awk -F " " '{for (i=2;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}')
iAddr=$(echo "$allI4Addrs" | head -n 1)
iAddrNum=$(echo "$allI4Addrs" | wc -l)
collectAllIpv4Addresses "$iAddrNum"
ipAddr=$(echo ${iAddr} | cut -d'/' -f1)
ipPrefix=$(echo ${iAddr} | cut -d'/' -f2)
ipMask=$(netmask "$ipPrefix")
# Get real IPv4 subnet of current System
ip4RouteScopeLink=$(ip -4 route show scope link | grep -iv "warp\|wgcf\|wg[0-9]\|docker[0-9]" | grep -w "$interface4" | grep -w "$ipAddr" | grep -m1 -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -n 1)
actualIp4Prefix=$(ip -4 route show scope link | grep -iv "warp\|wgcf\|wg[0-9]\|docker[0-9]" | grep -w "$interface4" | grep -w "$ip4RouteScopeLink" | head -n 1 | awk '{print $1}' | awk -F '/' '{print $2}')
[[ -z "$actualIp4Prefix" ]] && actualIp4Prefix="$ipPrefix"
actualIp4Subnet=$(netmask "$actualIp4Prefix")
# In most situation, at least 99.9% probability, the first hop of the network should be the same as the available gateway.
# But in 0.1%, they are actually different.
# Because one of the first hop of a tested machine is 5.45.72.1, I told Debian installer this router as a gateway
# But installer said the correct gateway should be 5.45.76.1, in a typical network, for example, your home,
# the default gateway is the same as the first route hop of the machine, it may be 192.168.0.1.
# If possible, we should configure out the real available gateway of the network.
FirstRoute=$(ip -4 route show default | grep -iv "warp\|wgcf\|wg[0-9]\|docker[0-9]" | grep -w "via" | grep -w "dev $interface4*" | head -n 1 | awk -F " " '{for (i=3;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}')
# We should find it in ARP, the first hop IP and gateway IP is managed by the same device, use device mac address to configure it out.
RouterMac=$(arp -n | grep "$FirstRoute" | awk '{print$3}')
FrFirst=$(echo "$FirstRoute" | cut -d'.' -f 1,2)
FrThird=$(echo "$FirstRoute" | cut -d'.' -f 3)
# Print all matched available gateway.
ipGates=$(ip -4 route show | grep -iv "warp\|wgcf\|wg[0-9]\|docker[0-9]" | grep -v "via" | grep -w "dev $interface4*" | grep -w "proto*" | grep -w "scope global\|link src $ipAddr*" | awk '{print$1}')
# Figure out the line of this list.
ipGateLine=$(echo "$ipGates" | wc -l)
# The line determines the cycling times.
for ((i = 1; i <= "$ipGateLine"; i++)); do
# Current one gateway of the ip gateways. the formart is as of 10.0.0.0/22
tmpIpGate=$(echo "$ipGates" | sed -n ''$i'p')
# Intercept a standard IPv4 address.
tmpIgAddr=$(echo $tmpIpGate | cut -d'/' -f1)
# Intercept the prefix of the gateway.
tmpIgPrefix=$(echo $tmpIpGate | cut -d'/' -f2)
# Calculate the first ip in all network segment, it should be the the same range with gateway in this network.
minIpGate=$(ipv4Calc "$tmpIgAddr" "$tmpIgPrefix" | grep "FirstIP:" | awk '{print$2}')
# Intercept the A and B class of the current ip address of gateway.
tmpIpGateFirst=$(echo "$minIpGate" | cut -d'.' -f 1,2)
tmpIpGateThird=$(echo "$minIpGate" | cut -d'.' -f 3)
# If the class A and B class of the current local ip address is as same as current gateway, this gateway may a valid one.
[[ "$FrFirst" == "$tmpIpGateFirst" ]] && {
if [[ "$FrThird" == "$tmpIpGateThird" ]]; then
ipGate="$FirstRoute"
break
elif [[ "$FrThird" != "$tmpIpGateThird" ]]; then
# The A, B and C class address of min ip gate.
tmpMigFirst=$(echo $minIpGate | cut -d'.' -f 1,2,3)
# Search it in ARP, it's belonged to the same network device which has been distinguished by mac address of first hop of the IP.
ipGate=$(arp -n | grep "$tmpMigFirst" | grep "$RouterMac" | awk '{print$1}')
break
fi
}
done
# If there is no one of other gateway in this current network, use if access the public internet, the first hop route of this machine as the gateway.
[[ "$ipGates" == "" || "$ipGate" == "" ]] && ipGate="$FirstRoute"
transferIPv4AddressFormat "$ipAddr" "$ipGate"
}
# $1 is "$ipAddr", $2 is "$ipGate".
function transferIPv4AddressFormat() {
# Some cloud providers like Godaddy, Arkecx, Hetzner(include DHCP) etc, the subnet mask of IPv4 static network configuration of their original template OS is incorrect.
# The following is the sample:
#
# auto eth0
# iface eth0 inet static
# address 190.168.23.175
# netmask 255.255.255.240
# dns-nameservers 8.8.8.8 8.8.4.4
# up ip -4 route add default via 169.254.0.1 dev eth0 onlink
#
# The netmask tells the total number of IP in the network is only 15(240 - 255),
# but we obsessed that there are more than 15 IPv4 addresses between 169.254.0.1 and 190.168.23.175 clearly.
# So if netmask is 255.255.255.240(prefix is 28), the computer only find IP between 190.168.23.160 and 190.168.23.175,
# the gateway 169.254.0.1 is obviously not be included in this range.
# So we need to expand the range of the netmask(reduce the value number of the prefix) to make sure the IPv4 gateway can be contained.
# If this mistake has not be repaired, Debian installer will return error "untouchable gateway".
# DHCP IPv4 network(even IPv4 netmask is "32") may not be effected by this situation.
# The following consulted calculations are calculated by Vultr IPv4 subnet calculator, reference: https://www.vultr.com/resources/subnet-calculator/
ipv4SubnetCertificate "$1" "$2"
ipPrefix="$tmpIpMask"
ipMask=$(netmask "$tmpIpMask")
# Some servers' provided by Hetzner are so confused because the IPv4 configurations of them are static but they are not fitted with standard, here is a sample:
#
# auto ens3
# iface ens3 inet static
# address: 89.163.208.5
# netmask: 255.255.255.0
# broadcast +
# up ip -f inet route add 169.254.0.1 dev ens3
# up ip -f inet route add default via 169.254.0.1 dev ens3
#
# The A class of address and gateway are entirely different, although we should make sure the value of the suggested subnet mask is "128.0.0.1"(prefix "1")
# to expand IPv4 range as large as possible, but in above situation, the largest IPv4 range is from 0.0.0.0 to 127.255.255.255, the IPv4 gate "169.254.0.1"
# can't be included, so the reserve approach is to get the result of "ip -4 route show scope link"(89.163.208.0/24) to ensure the correct subnet and gateway,
# then we can fix these weird settings from incorrect network router.
# IPv4 network from Hetzner support dhcp even though it's configurated by static in "/etc/network/interfaces".
# ip4RangeFirst=`ipv4Calc "$1" "$actualIp4Prefix" | grep "FirstIP:" | awk '{print$2}' | cut -d'.' -f1`
# ip4RangeLast=`ipv4Calc "$1" "$actualIp4Prefix" | grep "LastIP:" | awk '{print$2}' | cut -d'.' -f1`
ip4AddrFirst=$(echo $1 | cut -d'.' -f1)
ip4AddrSecond=$(echo $1 | cut -d'.' -f2)
ip4GateFirst=$(echo $2 | cut -d'.' -f1)
ip4GateSecond=$(echo $2 | cut -d'.' -f2)
# Common ranges of IPv4 intranet:
# Reference: https://hczhang.cn/network/reserved-ip-addresses.html
[[ "$ip4AddrFirst""$ip4AddrSecond" != "$ip4GateFirst""$ip4GateSecond" ]] && {
checkIfIpv4AndIpv6IsLocalOrPublic "$2" ""
[[ "$ipv4LocalOrPublicStatus" == '1' ]] || [[ "$ip4AddrFirst" != "$ip4GateFirst" ]] || [[ "$ip4AddrSecond" != "$ip4GateSecond" ]] && {
# [[ -z "$ip4RouteScopeLink" ]] && ipGate=`ipv4Calc "$1" "$ipPrefix" | grep "FirstIP:" | awk '{print$2}'` || ipGate=`ipv4Calc "$ip4RouteScopeLink" "$ipPrefix" | grep "FirstIP:" | awk '{print$2}'`
if [[ "$linux_relese" == 'debian' ]] || [[ "$linux_relese" == 'kali' ]] || [[ "$linux_relese" == 'alpinelinux' ]]; then
ipPrefix="$actualIp4Prefix"
ipMask="$actualIp4Subnet"
Network4Config="isStatic"
# Redirecting all actual names of network adapters to "eth0", "eth1"... by force aims to make a convenience to adding gateway(route) in soft hacking process.
setInterfaceName='1'
fi
# Temporary installation of Debian 12 and Kali can't handle IPv4 public address and IPv4 private gateway well, so we prefer to invoke irregular IPv6 parameters to configure network in "busybox" and write static configs for IPv4 in later stage of the installation.
[[ "$IPStackType" == "BiStack" ]] && {
[[ "$linux_relese" == 'debian' || "$linux_relese" == 'kali' ]] && {
BiStackPreferIpv6Status='1'
}
}
# Installation environment of Debian 11 and former releases can't handle either irregular(gateway is not within the range of which is calculated by IP/mask) IPv4 or IPv6 parameters, so we need to execute a "ip route add" trick to make sure the networking of pure IPv4 stack servers works normally in "busybox".
[[ "$IPStackType" == "IPv4Stack" || "$linux_relese" == 'alpinelinux' ]] && BurnIrregularIpv4Status='1'
}
}
[[ "$interfacesNum" -ge "2" ]] && {
# If there are two and more network adapters on the system like "eth0" and "eth1" and the first adapter "eth0" plays a role of connecting to the public network by dhcp method,
# AlpineLinux will prefer to use the last order of the adapter like "eth1" to configure the network instead of "eth0" if assign "ip=dhcp" parameter to the netboot kernel,
# so we need to switch the network configuration method to static to make sure the expect valid network adapter "eth0" can be activated.
[[ "$linux_relese" == 'alpinelinux' ]] && Network4Config="isStatic"
}
}
function netmask() {
n="${1:-32}"
b=""
m=""
for ((i = 0; i < 32; i++)); do
[ $i -lt $n ] 2>/dev/null && b="${b}1" || b="${b}0"
done
for ((i = 0; i < 4; i++)); do
s=$(echo "$b" | cut -c$(($(($i * 8)) + 1))-$(($(($i + 1)) * 8)))
[ "$m" == "" ] && m="$((2#${s}))" || m="${m}.$((2#${s}))"
done
echo "$m"
}
# $1 is IPv4 address, $2 is IPv4 subnet.
function ipv4Calc() {
tmpIp4="$1"
tmpIp4Mask=$(netmask "$2")
IFS=. read -r i1 i2 i3 i4 <<<"$tmpIp4"
IFS=. read -r m1 m2 m3 m4 <<<"$tmpIp4Mask"
tmpNetwork="$((i1 & m1)).$((i2 & m2)).$((i3 & m3)).$((i4 & m4))"
tmpBroadcast="$((i1 & m1 | 255 - m1)).$((i2 & m2 | 255 - m2)).$((i3 & m3 | 255 - m3)).$((i4 & m4 | 255 - m4))"
tmpFirstIP="$((i1 & m1)).$((i2 & m2)).$((i3 & m3)).$(((i4 & m4) + 1))"
tmpFiLast="$(echo "$tmpFirstIP" | cut -d'.' -f 4)"
FirstIP="$tmpFirstIP"
tmpLastIP="$((i1 & m1 | 255 - m1)).$((i2 & m2 | 255 - m2)).$((i3 & m3 | 255 - m3)).$(((i4 & m4 | 255 - m4) - 1))"
tmpLiLast="$(echo "$tmpLastIP" | cut -d'.' -f 4)"
LastIP="$tmpLastIP"
[[ "$tmpFiLast" > "$tmpLiLast" ]] && {
FirstIP="$tmpLastIP"
LastIP="$tmpFirstIP"
}
[[ "$2" > "31" ]] && {
FirstIP="$tmpNetwork"
LastIP="$tmpNetwork"
}
echo -e "Network: $tmpNetwork\nBroadcast: $tmpBroadcast\nFirstIP: $FirstIP\nLastIP: $LastIP\n"
}
# Unsuitable settings of subnet will cause not only "Death Red" of Debian installer which is called "unreachable gateway"
# but also contributes to some additional negative results as of if it's wider than the actual,
# this host will lose communications with some other servers which are serving in public internet because these will be treated as intranet hosts.
# To the opposite, if the subnet of one server is narrower than the actual, this host will lose communications with some local hosts because these will be treated as public servers.
# As an environment of a VPS, a narrower subnet causes less bad subsequentials than a wider prefer because VPS is usually be used by individual.
# If it's in a cluster such as home, office or company which is a place of that usually needs to transmit data with other hosts within LAN(local area network),
# the better opinion is to setting a wider value if you don't know them well.
# To figure out the most suitable subnet of a class segment of one IP or just a specific IP address,
# you can visit: https://bgp.tools/ which allows you to inquire announced allocations of IP addresses that were assigned by Internet Organizations.
#
# $1 is "$ipAddr", $2 is "$ipGate"
function ipv4SubnetCertificate() {
# If the IP and gateway are not in the same IPv4 A class, the prefix of netmask should be "1", transfer to whole IPv4 address is 128.0.0.1
# The range of 190.168.23.175/1 is 128.0.0.0 - 255.255.255.255, the gateway 169.254.0.1 can be included.
[[ $(echo $1 | cut -d'.' -f 1) != $(echo $2 | cut -d'.' -f 1) ]] && tmpIpMask="1"
# If the IP and gateway are in the same IPv4 A class, not in the same IPv4 B class, the prefix of netmask should less equal than "8", transfer to whole IPv4 address is 255.0.0.0
# The range of 190.168.23.175/8 is 190.0.0.0 - 190.255.255.255, the gateway 169... can't be included.
[[ $(echo $1 | cut -d'.' -f 1) == $(echo $2 | cut -d'.' -f 1) ]] && tmpIpMask="8"
# If the IP and gateway are in the same IPv4 A B class, not in the same IPv4 C class, the prefix of netmask should less equal than "16", transfer to whole IPv4 address is 255.255.0.0
# The range of 190.168.23.175/16 is 190.168.0.0 - 190.168.255.255, the gateway 169... can't be included.
[[ $(echo $1 | cut -d'.' -f 1,2) == $(echo $2 | cut -d'.' -f 1,2) ]] && tmpIpMask="16"
# If the IP and gateway are in the same IPv4 A B C class, not in the same IPv4 D class, the prefix of netmask should less equal than "24", transfer to whole IPv4 address is 255.255.255.0
# The range of 190.168.23.175/24 is 190.168.23.0 - 190.168.23.255, the gateway 169... can't be included.
[[ $(echo $1 | cut -d'.' -f 1,2,3) == $(echo $2 | cut -d'.' -f 1,2,3) ]] && tmpIpMask="24"
# So in summary of the IPv4 sample in above, we should assign subnet mask "128.0.0.1"(prefix "1") for it.
}
# $1 is "$setDisk", $2 is "linux_relese"
function getDisk() {
# $disks is definited as the default disk, if server has 2 and more disks, the first disk will be responsible of the grub booting.
rootPart=$(lsblk -ip | grep -v "fd[0-9]*\|sr[0-9]*\|ram[0-9]*\|loop[0-9]*" | sed 's/[[:space:]]*$//g' | grep -w "part /\|part /boot" | head -n 1 | cut -d' ' -f1 | sed 's/..//')
# majorMin=`lsblk -ip | grep -w "$rootPart" | head -n 1 | awk '{print $2}' | sed -r 's/:(.*)/:0/g'`
diskSuffix=${rootPart: -4}
# ssd like NVMe(/dev/nvme0n1), MMC sd card(/dev/mmcblk0) are parted with "p number" suffix like: "/dev/nvme0n1p1" "/dev/mmcblk0p2",
# The partitions of vda and sda devices are ended with number "/dev/sda1" "/dev/vda2".
[[ -n $(echo $diskSuffix | grep -o "[0-9]p[0-9]") ]] && disks=$(echo $rootPart | sed 's/p[0-9]*.$//') || disks=$(echo $rootPart | sed 's/[0-9]*.$//')
# disks=`lsblk -ip | grep -w "$majorMin" | head -n 1 | awk '{print $1}'`
[[ -z "$disks" ]] && disks=$(lsblk -ip | grep -v "fd[0-9]*\|sr[0-9]*\|ram[0-9]*\|loop[0-9]*" | sed 's/[[:space:]]*$//g' | grep -w "disk /\|disk /boot" | head -n 1 | cut -d' ' -f1)
[[ -z "$disks" ]] && disks=$(lsblk -ip | grep -v "fd[0-9]*\|sr[0-9]*\|ram[0-9]*\|loop[0-9]*" | sed 's/[[:space:]]*$//g' | grep -w "disk" | grep -i "[0-9]g\|[0-9]t\|[0-9]p\|[0-9]e\|[0-9]z\|[0-9]y" | head -n 1 | cut -d' ' -f1)
[ -n "$disks" ] || echo ""
echo "$disks" | grep -q "/dev"
[ $? -eq 0 ] && IncDisk="$disks" || IncDisk="/dev/$disks"
AllDisks=""
# Find all disks on this server.
for Count in $(lsblk -ipd | grep -v "fd[0-9]*\|sr[0-9]*\|ram[0-9]*\|loop[0-9]*" | sed 's/[[:space:]]*$//g' | grep -w "disk" | grep -i "[0-9]g\|[0-9]t\|[0-9]p\|[0-9]e\|[0-9]z\|[0-9]y" | cut -d' ' -f1); do
AllDisks+="$Count "
done
AllDisks=$(echo "$AllDisks" | sed 's/.$//')
# All numbers of disks' statistic of this server.
disksNum=$(echo $AllDisks | grep -o "/dev/*" | wc -l)
# Some cloud providers using first SCSI/SATA device like "sda" to mount ISO image instead of using "sr0":
#
# root@node:~# lsblk -ipf
# NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
# /dev/sda iso9660 Joliet Extension cidata 2023-07-13-09-35-07-00
# /dev/sr0
# /dev/vda
# |-/dev/vda1
# |-/dev/vda2 vfat FAT32 0BBB-E1CA 119.9M 0% /boot/efi
# `-/dev/vda3 xfs 0c93f6bc-ef9c-468d-be02-84b4a70d3678 44.4G 11% /
#
# Because of "sda" can't be written, If system is selected to install on "sda", the installation will meet a fatal,
# So we should exclude all these devices.
for ((d = 1; d <= $disksNum; d++)); do
currentDisk=$(echo "$AllDisks" | cut -d' ' -f$d)
checkIfIsoPartition=$(lsblk -ipf | grep "$currentDisk" | head -n 1 | awk '{print $2}' | grep -i "iso")
[[ -z "$checkIfIsoPartition" ]] && tmpAllDisks+="$currentDisk "
done
tmpAllDisks=$(echo "$tmpAllDisks" | sed 's/.$//')
[[ "$AllDisks" != "$tmpAllDisks" ]] && {
AllDisks="$tmpAllDisks"
disksNum=$(echo $AllDisks | grep -o "/dev/*" | wc -l)
[[ "$IncDisk" =~ "$AllDisks" ]] || IncDisk=$(echo "$AllDisks" | cut -d' ' -f1)
}
# Allow user to install system to one disk manually.
[[ -n "$1" && "$1" != "all" && "$(echo $1 | cut -d '/' -f 3)" =~ ^[a-z0-9]+$ || "$(echo $1 | cut -d '/' -f 3)" =~ ^[a-z]+$ ]] && {
[[ "$1" =~ "/dev/" ]] && IncDisk="$1" || IncDisk="/dev/$1"
}
# Remove all lvm volumes by force for Debian and Kali.
[[ -z "$1" && "$disksNum" -ge "2" && -n $(lsblk -ip | awk '{print $6}' | grep -io "lvm") ]] && {
[[ "$2" == 'debian' || "$2" == 'kali' ]] && setDisk="all"
}
# Redhat cloud init install needs at least 10GB drive space.
# Windows needs at least 15GB drive space.
diskCapacity=$(lsblk -ipb | grep -w "$IncDisk" | awk {'print $4'})
}
# Check if there are several cloud init configs in CD drive.
function detectCloudinit() {
internalCloudinitStatus="0"
[[ $(blkid -tTYPE=iso9660 -odevice) ]] && {
umount /mnt 2>/dev/null
for cloudinitCdDrive in $(blkid -tTYPE=iso9660 -odevice); do
mount $cloudinitCdDrive /mnt 2>/dev/null
[[ $(find /mnt -name "meta_data*" -print -or -name "user_data*" -print -or -name "meta-data*" -print -or -name "user-data*" -print) ]] && {
internalCloudinitStatus="1"
umount /mnt 2>/dev/null
break
}
umount /mnt 2>/dev/null
done
}
}
function diskType() {
echo $(udevadm info --query all "$1" 2>/dev/null | grep 'ID_PART_TABLE_TYPE' | cut -d'=' -f2)
}
# Default to make a GPT partition to support 3TB hard drive or larger.
# To remove LVM VGM PVM force automatically:
# https://serverfault.com/questions/571363/unable-to-automatically-remove-lvm-data
# To part all disks for preseed:
# https://unix.stackexchange.com/questions/341253/using-d-i-partman-recipe-strings
#
# Recipes for parting disk in BIOS or UEFI manually for kickstart.
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/installation_guide/sect-kickstart-syntax
# https://blog.adachin.me/archives/3621
# https://www.cnblogs.com/hukey/p/14919346.html
#
# $1 is "$linux_relese", $2 is "$disksNum", $3 is "$setSwap", $4 is "$setDisk", $5 is "$partitionTable", $6 is "$setFileSystem", $7 is "$EfiSupport", $8 is "$diskCapacity", $9 is "$IncDisk", ${10} is "$AllDisks".
function setNormalRecipe() {
[[ -n "$3" && $(echo "$3" | grep -o '[0-9]') ]] && swapSpace="$3" || swapSpace='0'
if [[ "$1" == 'debian' ]] || [[ "$1" == 'kali' ]]; then
[[ "$lowMemMode" == "1" ]] && {
[[ -z "$swapSpace" || "$swapSpace" -lt "512" ]] && swapSpace="512"
}
# Enhanced swap handling for btrfs filesystem (2025-08-07)
if [[ -n "$swapSpace" && "$swapSpace" -gt "0" ]]; then
swapSpace=$(awk 'BEGIN{print '${swapSpace}'*1.05078125 }' | cut -d '.' -f '1')
# For btrfs filesystem, we don't create swap partition, will use swapfile instead
if [[ "$6" == "btrfs" ]]; then
swapRecipe=""
# Add swapfile creation command to late_command - Fixed for btrfs compatibility
# Make swapfileCommand global so it can be used in preseed generation
swapfileCommand="echo 'Creating swapfile for btrfs filesystem...' >> /target/var/log/swapfile.log; mkdir -p /target/swap; chattr +C /target/swap; dd if=/dev/zero of=/target/swap/swapfile bs=1M count=${swapSpace}; chmod 600 /target/swap/swapfile; mkswap /target/swap/swapfile; swapon /target/swap/swapfile; echo '/swap/swapfile none swap sw 0 0' >> /target/etc/fstab;"
else
swapRecipe=''${swapSpace}' 200 '${swapSpace}' linux-swap method{ swap } format{ } .'
# Clear swapfileCommand for non-btrfs filesystems
swapfileCommand=""
fi
else
swapRecipe=""
# Clear swapfileCommand when no swap is needed
swapfileCommand=""
fi
# File system detection - Added btrfs support (2025-08-07)
if [[ "$6" == "xfs" ]]; then
fileSystem="xfs"
elif [[ "$6" == "btrfs" ]]; then
fileSystem="btrfs"
else
fileSystem="ext4"
fi
defaultFileSystem='d-i partman/default_filesystem string '${fileSystem}''
mainRecipe='1076 150 -1 '${fileSystem}' method{ format } format{ } use_filesystem{ } filesystem{ '${fileSystem}' } mountpoint{ / } .'
if [[ "$2" -gt "1" && "$4" == "all" ]]; then
PartmanEarlyCommand='debconf-set partman-auto/disk '${10}';'
selectDisks='d-i partman-auto/disk string '${10}''
else
PartmanEarlyCommand='debconf-set partman-auto/disk "$(list-devices disk | grep '${9}' | head -n 1)";'
selectDisks='d-i partman-auto/disk string '${9}''
fi
if [[ "$5" == "gpt" || "$7" == "enabled" || "$8" -ge "2199023255552" ]]; then
gptPartitionPreseed=$(echo -e "d-i partman-basicfilesystems/choose_label string gpt
d-i partman-basicfilesystems/default_label string gpt
d-i partman-partitioning/choose_label string gpt
d-i partman-partitioning/default_label string gpt
d-i partman/choose_label string gpt
d-i partman/default_label string gpt")
gptForBios='1 100 1 free $iflabel{ gpt } $reusemethod{ } method{ biosgrub } .'
else
gptPartitionPreseed=""
gptForBios=""
fi
if [[ "$7" == "enabled" ]]; then
normalRecipes=$(echo -e "d-i partman-auto/choose_recipe select normal
d-i partman-auto/expert_recipe string normal :: \
538 100 1076 free \$iflabel{ gpt } \$reusemethod{ } method{ efi } format{ } . \
$swapRecipe \
$mainRecipe
d-i partman-efi/non_efi_system boolean true")
else
normalRecipes=$(echo -e "d-i partman-auto/choose_recipe select normal
d-i partman-auto/expert_recipe string normal :: \
$gptForBios \
$swapRecipe \
$mainRecipe
")
fi
FormatDisk=$(echo -e "$selectDisks
d-i partman-auto/method string regular
d-i partman-basicfilesystems/no_swap boolean false
$normalRecipes
$gptPartitionPreseed
")
elif [[ "$1" == 'centos' ]] || [[ "$1" == 'rockylinux' ]] || [[ "$1" == 'almalinux' ]] || [[ "$1" == 'fedora' ]]; then
ksIncDisk=$(echo $9 | cut -d'/' -f 3)
ksAllDisks=$(echo ${10} | sed 's/\/dev\///g' | sed 's/ /,/g')
# Enhanced swap handling for RedHat systems with btrfs support (2025-08-07)
if [[ -n "$swapSpace" && "$swapSpace" -gt "0" ]]; then
# For btrfs filesystem, we don't create swap partition, will use swapfile instead
if [[ "$6" == "btrfs" ]]; then
swapRecipe=""
# Add swapfile creation command to %post section
swapfileCommand="echo 'Creating swapfile for btrfs filesystem...' >> /var/log/swapfile.log; mkdir -p /swap; chattr +C /swap; dd if=/dev/zero of=/swap/swapfile bs=1M count=${swapSpace}; chmod 600 /swap/swapfile; mkswap /swap/swapfile; swapon /swap/swapfile; echo '/swap/swapfile none swap sw 0 0' >> /etc/fstab;"
else
if [[ "$swapSpace" -gt "512" ]]; then
swapRecipe='part swap --ondisk='${ksIncDisk}' --size='${swapSpace}'\n'
else
# Not distributing any capacity of swap will cause installing of Kickstart collapse.
swapRecipe='part swap --ondisk='${ksIncDisk}' --size=512\n'
fi
# Clear swapfileCommand for non-btrfs filesystems
swapfileCommand=""
fi
else
swapRecipe=""
# Clear swapfileCommand when no swap is needed
swapfileCommand=""
fi
# Determine filesystem for RedHat systems - Added btrfs support (2025-08-07)
if [[ "$6" == "btrfs" ]]; then
redhatFileSystem="btrfs"
elif [[ "$6" == "ext4" ]]; then
redhatFileSystem="ext4"
else
redhatFileSystem="xfs"
fi
[[ "$2" -le "1" || "$4" != "all" ]] && {
clearPart="clearpart --drives=${ksIncDisk} --all --initlabel"
if [[ "$7" == "enabled" ]]; then
FormatDisk=$(echo -e "part / --fstype="${redhatFileSystem}" --ondisk="$ksIncDisk" --grow --size="0"\n${swapRecipe}part /boot --fstype="${redhatFileSystem}" --ondisk="$ksIncDisk" --size="1024"\npart /boot/efi --fstype="efi" --ondisk="$ksIncDisk" --size="512"")
else
FormatDisk=$(echo -e "part / --fstype="${redhatFileSystem}" --ondisk="$ksIncDisk" --grow --size="0"\n${swapRecipe}part /boot --fstype="${redhatFileSystem}" --ondisk="$ksIncDisk" --size="1024"\npart biosboot --fstype=biosboot --ondisk="$ksIncDisk" --size=1")
fi
}
[[ "$4" == "all" || -n "$setRaid" ]] && {
clearPart="clearpart --all --initlabel"
FormatDisk="autopart"
}
fi
}
# $1 is "$setRaid", $2 is "$disksNum", $3 is "$AllDisks", $4 is "$linux_relese", $5 is "$setFileSystem".
function setRaidRecipe() {
[[ -n "$1" ]] && {
# Soft Raid 0, 1, 5, 6 and 10 methods are supported by Debian, only one disk can't be as a component for any Raid method.
# Raid 0 needs at least two disks, all space of the disks will be exploited, and it's the most dangerous for the safety of the data.
# Raid 1 needs at least two disks, the space can be exploited is always equal one disk, it's safest for the date but a bit wasteful.
# Raid 5 needs at least three disks, it storages data on two disks and storages parity checking data on one disk, it's not save on any single disk over 4TB.
# Raid 6 needs at least four disks, it's an enhanced version of Raid 5, it uses two parity stripes by practicing of dividing data across the set of drives,
# it allows for two disk failures within the RAID set before any data is lost.
# Raid 10 needs at least four disks, it's a combination of Raid 0 and Raid 1, the disk 0 and disk 1 as a set of Raid 0, the same as disk 2 and disk 3,
# and then the sets of disk 0,1 and disk 2,3 are composed as one Raid 1.
# These Raid recipes are also applicable to Kali, fuck Canonical again! you deperated the compatibility of "preseed.cfg" installation procession from Ubuntu 22.04 and later.
if [[ "$1" == "0" || "$1" == "1" || "$1" == "5" || "$1" == "6" || "$1" == "10" ]]; then
[[ "$1" == "0" || "$1" == "1" ]] && [[ "$2" -lt "2" ]] && {
echo -ne "\n[${red}Error${plain}] There are $2 drives on your machine, Raid $1 partition recipe only supports a basic set of dual drive or more!\n"
exit 1
}
[[ "$1" == "5" ]] && [[ "$2" -lt "3" ]] && {
echo -ne "\n[${red}Error${plain}] There are $2 drives on your machine, Raid $1 partition recipe only supports a basic set of triple drive or more!\n"
exit 1
}
[[ "$1" == "6" || "$1" == "10" ]] && [[ "$2" -lt "4" ]] && {
echo -ne "\n[${red}Error${plain}] There are $2 drives on your machine, Raid $1 partition recipe only supports a basic set of quad drive or more!\n"
exit 1
}
else
echo -ne "\n[${red}Error${plain}] Raid $1 partition recipe is not suitable, only Raid 0, 1, 5, 6 or 10 is supported!\n"
exit 1
fi
if [[ "$4" == 'debian' ]] || [[ "$4" == 'kali' ]]; then
# Use the filesystem specified by user, default to ext4
if [[ "$5" == "btrfs" ]]; then
defaultFileSystem='d-i partman/default_filesystem string btrfs'
elif [[ "$5" == "xfs" ]]; then
defaultFileSystem='d-i partman/default_filesystem string xfs'
else
defaultFileSystem='d-i partman/default_filesystem string ext4'
fi
for ((r = 1; r <= "$2"; r++)); do
tmpAllDisksPart=$(echo "$3" | cut -d ' ' -f"$r")
# Some NVME controller hard drives like "/dev/nvme0n1" etc are end of a number in there names must add "p" with partition numbers for "d-i partman-auto-raid/recipe string",
# SCSI controller or Virtual controller drives like "/dev/sda" or "/dev/vda" are not effected by this situation.
# Drives and their partitions must be connected with "#" like "/dev/nvme0n1p2#/dev/nvme0n2p2".
echo "${tmpAllDisksPart: -1}" | [[ -n "$(sed -n '/^[0-9][0-9]*$/p')" ]] && tmpAllDisksPart="$tmpAllDisksPart""p" || tmpAllDisksPart="$tmpAllDisksPart"
AllDisksPart1+="$tmpAllDisksPart""1#"
AllDisksPart2+="$tmpAllDisksPart""2#"
AllDisksPart3+="$tmpAllDisksPart""3#"
done
AllDisksPart1=$(echo "$AllDisksPart1" | sed 's/.$//')
AllDisksPart2=$(echo "$AllDisksPart2" | sed 's/.$//')
AllDisksPart3=$(echo "$AllDisksPart3" | sed 's/.$//')
# Enhanced RAID swap handling for btrfs filesystem (2025-08-07)
# Remove existed raid md devices without confirming; select raid recipe and all disks; don't assign swap; when one device on raid 1 is offline, the system still can be booted.
# Reference: https://wiki.ubuntu.com/BootDegradedRaid
# For btrfs filesystem, we don't create swap partition, will use swapfile instead
if [[ "$5" == "btrfs" ]]; then
RaidRecipes=$(echo -e "d-i partman-md/confirm boolean true