-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
install_kernel.sh
3773 lines (2844 loc) · 145 KB
/
install_kernel.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
#
# Install linux kernel for TCP BBR and BBR Plus
#
# Copyright (C) 2021-2023 JinWYP
#
# 4.4 LTS 4.9 LTS 4.14 LTS 4.19 LTS
# 5.4 LTS 5.10 LTS
# 4.x版本内核最新的longterm版本是4.19.113,安装的话只能找个4.19的rpm包来安装了
# 从 Linux 4.9 版本开始,TCP BBR 就已经成为了 Linux 系统内核的一部分。因此,开启 BBR 的首要前提就是当前系统内核版本大于等于 4.9
# Linux 内核 5.6 正式发布了,内置了 wireguard module
# Linux 5.6 引入 FQ-PIE 数据包调度程序以帮助应对 Bufferbloat
# 5.5内核支持cake队列
# 自来光大佬: xamod内核5.8默认队列算法已经改为 fq_pie 之前是cake
# centos8 安装完成默认内核 kernel-core-4.18.0-240.15.1.el8_3.x86_64, kernel-modules-4.18.0-240.15.1.el8_3.x86_64
# ubuntu16 安装完成默认内核 linux-generic 4.4.0.210, linux-headers-4.4.0-210
# ubuntu18 安装完成默认内核 linux-generic 4.15.0.140, linux-headers-4.15.0-140
# ubuntu20 安装完成默认内核 linux-image-5.4.0-70-generic , linux-headers-5.4.0-70
# debian10 安装完成默认内核 4.19.0-16-amd64
# debian11 安装完成默认内核 linux-image-5.10.0-8-amd64
# UJX6N 编译的bbr plus 内核 5.10.27-bbrplus 5.9.16 5.4.86
# UJX6N 编译的bbr plus 内核 4.19.164 4.14.213 4.9.264-1.bbrplus
# https://github.com/cx9208/bbrplus/issues/27
# BBR 速度评测
# https://www.shopee6.com/web/web-tutorial/bbr-vs-plus-vs-bbr2.html
# https://hostloc.com/thread-644985-1-1.html
# https://dropbox.tech/infrastructure/evaluating-bbrv2-on-the-dropbox-edge-network
export LC_ALL=C
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
sudoCmd=""
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
sudoCmd="sudo"
fi
# fonts color
red(){
echo -e "\033[31m\033[01m$1\033[0m"
}
green(){
echo -e "\033[32m\033[01m$1\033[0m"
}
yellow(){
echo -e "\033[33m\033[01m$1\033[0m"
}
blue(){
echo -e "\033[34m\033[01m$1\033[0m"
}
bold(){
echo -e "\033[1m\033[01m$1\033[0m"
}
Green_font_prefix="\033[32m"
Red_font_prefix="\033[31m"
Green_background_prefix="\033[42;37m"
Red_background_prefix="\033[41;37m"
Font_color_suffix="\033[0m"
osCPU=""
osArchitecture="arm"
osInfo=""
osRelease=""
osReleaseVersion=""
osReleaseVersionNo=""
osReleaseVersionNoShort=""
osReleaseVersionCodeName="CodeName"
osSystemPackage=""
osSystemMdPath=""
osSystemShell="bash"
function checkArchitecture(){
# https://stackoverflow.com/questions/48678152/how-to-detect-386-amd64-arm-or-arm64-os-architecture-via-shell-bash
case $(uname -m) in
i386) osArchitecture="386" ;;
i686) osArchitecture="386" ;;
x86_64) osArchitecture="amd64" ;;
arm) dpkg --print-architecture | grep -q "arm64" && osArchitecture="arm64" || osArchitecture="arm" ;;
aarch64) dpkg --print-architecture | grep -q "arm64" && osArchitecture="arm64" || osArchitecture="arm" ;;
* ) osArchitecture="arm" ;;
esac
}
function checkCPU(){
osCPUText=$(cat /proc/cpuinfo | grep vendor_id | uniq)
if [[ $osCPUText =~ "GenuineIntel" ]]; then
osCPU="intel"
elif [[ $osCPUText =~ "AMD" ]]; then
osCPU="amd"
else
echo
fi
# green " Status 状态显示--当前CPU是: $osCPU"
}
# 检测系统版本号
getLinuxOSVersion(){
if [[ -s /etc/redhat-release ]]; then
osReleaseVersion=$(grep -oE '[0-9.]+' /etc/redhat-release)
else
osReleaseVersion=$(grep -oE '[0-9.]+' /etc/issue)
fi
# https://unix.stackexchange.com/questions/6345/how-can-i-get-distribution-name-and-version-number-in-a-simple-shell-script
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
source /etc/os-release
osInfo=$NAME
osReleaseVersionNo=$VERSION_ID
if [ -n "$VERSION_CODENAME" ]; then
osReleaseVersionCodeName=$VERSION_CODENAME
fi
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
osInfo=$(lsb_release -si)
osReleaseVersionNo=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
osInfo=$DISTRIB_ID
osReleaseVersionNo=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
osInfo=Debian
osReleaseVersion=$(cat /etc/debian_version)
osReleaseVersionNo=$(sed 's/\..*//' /etc/debian_version)
elif [ -f /etc/redhat-release ]; then
osReleaseVersion=$(grep -oE '[0-9.]+' /etc/redhat-release)
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
osInfo=$(uname -s)
osReleaseVersionNo=$(uname -r)
fi
osReleaseVersionNoShort=$(echo $osReleaseVersionNo | sed 's/\..*//')
}
# 检测系统发行版代号
function getLinuxOSRelease(){
if [[ -f /etc/redhat-release ]]; then
osRelease="centos"
osSystemPackage="yum"
osSystemMdPath="/usr/lib/systemd/system/"
osReleaseVersionCodeName=""
elif cat /etc/issue | grep -Eqi "debian|raspbian"; then
osRelease="debian"
osSystemPackage="apt-get"
osSystemMdPath="/lib/systemd/system/"
osReleaseVersionCodeName="buster"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
osRelease="ubuntu"
osSystemPackage="apt-get"
osSystemMdPath="/lib/systemd/system/"
osReleaseVersionCodeName="bionic"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
osRelease="centos"
osSystemPackage="yum"
osSystemMdPath="/usr/lib/systemd/system/"
osReleaseVersionCodeName=""
elif cat /proc/version | grep -Eqi "debian|raspbian"; then
osRelease="debian"
osSystemPackage="apt-get"
osSystemMdPath="/lib/systemd/system/"
osReleaseVersionCodeName="buster"
elif cat /proc/version | grep -Eqi "ubuntu"; then
osRelease="ubuntu"
osSystemPackage="apt-get"
osSystemMdPath="/lib/systemd/system/"
osReleaseVersionCodeName="bionic"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
osRelease="centos"
osSystemPackage="yum"
osSystemMdPath="/usr/lib/systemd/system/"
osReleaseVersionCodeName=""
fi
getLinuxOSVersion
checkArchitecture
checkCPU
virt_check
[[ -z $(echo $SHELL|grep zsh) ]] && osSystemShell="bash" || osSystemShell="zsh"
echo "OS info: ${osInfo}, ${osRelease}, ${osReleaseVersion}, ${osReleaseVersionNo}, ${osReleaseVersionCodeName}, ${osSystemShell}, ${osSystemPackage}, ${osSystemMdPath}"
}
virt_check(){
# if hash ifconfig 2>/dev/null; then
# eth=$(ifconfig)
# fi
virtualx=$(dmesg) 2>/dev/null
if [ "$(command -v dmidecode)" ]; then
sys_manu=$(dmidecode -s system-manufacturer) 2>/dev/null
sys_product=$(dmidecode -s system-product-name) 2>/dev/null
sys_ver=$(dmidecode -s system-version) 2>/dev/null
else
sys_manu=""
sys_product=""
sys_ver=""
fi
if grep docker /proc/1/cgroup -qa; then
virtual="Docker"
elif grep lxc /proc/1/cgroup -qa; then
virtual="Lxc"
elif grep -qa container=lxc /proc/1/environ; then
virtual="Lxc"
elif [[ -f /proc/user_beancounters ]]; then
virtual="OpenVZ"
elif [[ "$virtualx" == *kvm-clock* ]]; then
virtual="KVM"
elif [[ "$cname" == *KVM* ]]; then
virtual="KVM"
elif [[ "$cname" == *QEMU* ]]; then
virtual="KVM"
elif [[ "$virtualx" == *"VMware Virtual Platform"* ]]; then
virtual="VMware"
elif [[ "$virtualx" == *"Parallels Software International"* ]]; then
virtual="Parallels"
elif [[ "$virtualx" == *VirtualBox* ]]; then
virtual="VirtualBox"
elif [[ -e /proc/xen ]]; then
virtual="Xen"
elif [[ "$sys_manu" == *"Microsoft Corporation"* ]]; then
if [[ "$sys_product" == *"Virtual Machine"* ]]; then
if [[ "$sys_ver" == *"7.0"* || "$sys_ver" == *"Hyper-V" ]]; then
virtual="Hyper-V"
else
virtual="Microsoft Virtual Machine"
fi
fi
else
virtual="Dedicated母鸡"
fi
}
function installSoftDownload(){
if [[ "${osRelease}" == "debian" || "${osRelease}" == "ubuntu" ]]; then
PACKAGE_LIST=( "wget" "curl" "git" "unzip" "apt-transport-https" "cpu-checker" "bc" "cron" )
# 检查所有软件包是否已安装
for package in "${PACKAGE_LIST[@]}"; do
if ! dpkg -l | grep -qw "$package"; then
# green "$package is not installed. ${osSystemPackage} Installing..."
${osSystemPackage} install -y "$package"
fi
done
if ! dpkg -l | grep -qw curl; then
${osSystemPackage} -y install wget curl git
if [[ "${osRelease}" == "debian" ]]; then
echo "deb http://deb.debian.org/debian buster-backports main contrib non-free" > /etc/apt/sources.list.d/buster-backports.list
echo "deb-src http://deb.debian.org/debian buster-backports main contrib non-free" >> /etc/apt/sources.list.d/buster-backports.list
${sudoCmd} apt update -y
fi
fi
if ! dpkg -l | grep -qw ca-certificates; then
${osSystemPackage} -y install ca-certificates dmidecode
update-ca-certificates
fi
elif [[ "${osRelease}" == "centos" ]]; then
PACKAGE_LIST_Centos=( "wget" "curl" "git" "unzip" "bc" )
# 检查所有软件包是否已安装
for package in "${PACKAGE_LIST_Centos[@]}"; do
if ! rpm -qa | grep -qw "$package"; then
# green "$package is not installed. ${osSystemPackage} Installing..."
${osSystemPackage} install -y "$package"
fi
done
# 处理ca证书
if ! rpm -qa | grep -qw ca-certificates; then
${osSystemPackage} -y install ca-certificates dmidecode
update-ca-trust force-enable
fi
fi
}
# 更新本脚本
function upgradeScript(){
wget -Nq --no-check-certificate -O ./install_kernel.sh "https://raw.githubusercontent.com/jinwyp/one_click_script/master/install_kernel.sh"
green " Script upgrade successful. 本脚本升级成功! "
chmod +x ./install_kernel.sh
sleep 2s
exec "./install_kernel.sh"
}
function rebootSystem(){
if [ -z $1 ]; then
red "请检查上面的信息 是否有新内核版本, 老内核版本 ${osKernelVersionBackup} 是否已经卸载!"
echo
red "请注意检查 是否把新内核也误删卸载了, 无新内核 ${linuxKernelToInstallVersionFull} 不要重启, 可重新安装内核后再重启! "
fi
echo
read -p "是否立即重启? 请输入[Y/n]?" isRebootInput
isRebootInput=${isRebootInput:-Y}
if [[ $isRebootInput == [Yy] ]]; then
${sudoCmd} reboot
else
exit
fi
}
function promptContinueOpeartion(){
read -p "是否继续操作? 直接回车默认继续操作, 请输入[Y/n]:" isContinueInput
isContinueInput=${isContinueInput:-Y}
if [[ $isContinueInput == [Yy] ]]; then
echo ""
else
exit 1
fi
}
# https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
versionCompare () {
if [[ $1 == $2 ]]; then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
versionCompareWithOp () {
versionCompare $1 $2
case $? in
0) op='=';;
1) op='>';;
2) op='<';;
esac
if [[ $op != $3 ]]; then
# echo "Version Number Compare Fail: Expected '$3', Actual '$op', Arg1 '$1', Arg2 '$2'"
return 1
else
# echo "Version Number Compare Pass: '$1 $op $2'"
return 0
fi
}
osKernelVersionFull=$(uname -r)
osKernelVersionBackup=$(uname -r | awk -F "-" '{print $1}')
osKernelVersionShort=$(uname -r | cut -d- -f1 | awk -F "." '{print $1"."$2}')
osKernelBBRStatus=""
systemBBRRunningStatus="no"
systemBBRRunningStatusText=""
function listAvailableLinuxKernel(){
echo
green " =================================================="
green " 状态显示--当前可以被安装的 Linux 内核: "
if [[ "${osRelease}" == "centos" ]]; then
${sudoCmd} yum --disablerepo="*" --enablerepo="elrepo-kernel" list available | grep kernel
else
if [ -z $1 ]; then
${sudoCmd} apt-cache search linux-image
else
${sudoCmd} apt-cache search linux-image | grep $1
fi
fi
green " =================================================="
echo
}
function listInstalledLinuxKernel(){
echo
green " =================================================="
green " 状态显示--当前已安装的 Linux 内核: "
echo
if [[ "${osRelease}" == "debian" || "${osRelease}" == "ubuntu" ]]; then
dpkg --get-selections | grep linux-
# dpkg -l | grep linux-
# dpkg-query -l | grep linux-
# apt list --installed | grep linux-
echo
red " 如安装内核遇到kernel linux-image linux-headers 版本不一致问题, 请手动卸载已安装的kernel"
red " 卸载内核命令1 apt remove -y --purge linux-xxx名称"
red " 卸载内核命令2 apt autoremove -y --purge linux-xxx名称"
elif [[ "${osRelease}" == "centos" ]]; then
${sudoCmd} rpm -qa | grep kernel
echo
red " 如安装内核遇到kernel kernel-headers kernel-devel版本不一致问题, 请手动卸载已安装的kernel"
red " 卸载内核命令 rpm --nodeps -e kernel-xxx名称"
fi
green " =================================================="
echo
}
function showLinuxKernelInfoNoDisplay(){
isKernelSupportBBRVersion="4.9"
if versionCompareWithOp "${isKernelSupportBBRVersion}" "${osKernelVersionShort}" ">"; then
echo
else
osKernelBBRStatus="BBR"
fi
if [[ ${osKernelVersionFull} == *"bbrplus"* ]]; then
osKernelBBRStatus="BBR Plus"
elif [[ ${osKernelVersionFull} == *"xanmod"* ]]; then
osKernelBBRStatus="BBR 和 BBR2"
fi
net_congestion_control=`cat /proc/sys/net/ipv4/tcp_congestion_control | awk '{print $1}'`
net_qdisc=`cat /proc/sys/net/core/default_qdisc | awk '{print $1}'`
net_ecn=`cat /proc/sys/net/ipv4/tcp_ecn | awk '{print $1}'`
if [[ ${osKernelVersionBackup} == *"4.14.129"* ]]; then
# isBBREnabled=$(grep "net.ipv4.tcp_congestion_control" /etc/sysctl.conf | awk -F "=" '{print $2}')
# isBBREnabled=$(sysctl net.ipv4.tcp_available_congestion_control | awk -F "=" '{print $2}')
isBBRTcpEnabled=$(lsmod | grep "bbr" | awk '{print $1}')
isBBRPlusTcpEnabled=$(lsmod | grep "bbrplus" | awk '{print $1}')
isBBR2TcpEnabled=$(lsmod | grep "bbr2" | awk '{print $1}')
else
isBBRTcpEnabled=$(sysctl net.ipv4.tcp_congestion_control | grep "bbr" | awk -F "=" '{print $2}' | awk '{$1=$1;print}')
isBBRPlusTcpEnabled=$(sysctl net.ipv4.tcp_congestion_control | grep "bbrplus" | awk -F "=" '{print $2}' | awk '{$1=$1;print}')
isBBR2TcpEnabled=$(sysctl net.ipv4.tcp_congestion_control | grep "bbr2" | awk -F "=" '{print $2}' | awk '{$1=$1;print}')
fi
if [[ ${net_ecn} == "1" ]]; then
systemECNStatusText="已开启"
elif [[ ${net_ecn} == "0" ]]; then
systemECNStatusText="已关闭"
elif [[ ${net_ecn} == "2" ]]; then
systemECNStatusText="只对入站请求开启(默认值)"
else
systemECNStatusText=""
fi
if [[ ${net_congestion_control} == "bbr" ]]; then
if [[ ${isBBRTcpEnabled} == *"bbr"* ]]; then
systemBBRRunningStatus="bbr"
systemBBRRunningStatusText="BBR 已启动成功"
else
systemBBRRunningStatusText="BBR 启动失败"
fi
elif [[ ${net_congestion_control} == "bbrplus" ]]; then
if [[ ${isBBRPlusTcpEnabled} == *"bbrplus"* ]]; then
systemBBRRunningStatus="bbrplus"
systemBBRRunningStatusText="BBR Plus 已启动成功"
else
systemBBRRunningStatusText="BBR Plus 启动失败"
fi
elif [[ ${net_congestion_control} == "bbr2" ]]; then
if [[ ${isBBR2TcpEnabled} == *"bbr2"* ]]; then
systemBBRRunningStatus="bbr2"
systemBBRRunningStatusText="BBR2 已启动成功"
else
systemBBRRunningStatusText="BBR2 启动失败"
fi
else
systemBBRRunningStatusText="未启动加速模块"
fi
}
function showLinuxKernelInfo(){
# https://stackoverflow.com/questions/8654051/how-to-compare-two-floating-point-numbers-in-bash
# https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash
isKernelSupportBBRVersion="4.9"
green " =================================================="
green " 状态显示--当前Linux 内核版本: ${osKernelVersionShort} , $(uname -r) "
if versionCompareWithOp "${isKernelSupportBBRVersion}" "${osKernelVersionShort}" ">"; then
green " 当前系统内核低于4.9, 不支持开启 BBR "
else
green " 当前系统内核高于4.9, 支持开启 BBR, ${systemBBRRunningStatusText}"
fi
if [[ ${osKernelVersionFull} == *"xanmod"* ]]; then
green " 当前系统内核已支持开启 BBR2, ${systemBBRRunningStatusText}"
else
green " 当前系统内核不支持开启 BBR2"
fi
if [[ ${osKernelVersionFull} == *"bbrplus"* ]]; then
green " 当前系统内核已支持开启 BBR Plus, ${systemBBRRunningStatusText}"
else
green " 当前系统内核不支持开启 BBR Plus"
fi
# sysctl net.ipv4.tcp_available_congestion_control 返回值 net.ipv4.tcp_available_congestion_control = bbr cubic reno 或 reno cubic bbr
# sysctl net.ipv4.tcp_congestion_control 返回值 net.ipv4.tcp_congestion_control = bbr
# sysctl net.core.default_qdisc 返回值 net.core.default_qdisc = fq
# lsmod | grep bbr 返回值 tcp_bbr 20480 3 或 tcp_bbr 20480 1 注意:并不是所有的 VPS 都会有此返回值,若没有也属正常。
# isFlagBbr=$(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}')
# if [[ (${isFlagBbr} == *"bbr"*) && (${isFlagBbr} != *"bbrplus"*) && (${isFlagBbr} != *"bbr2"*) ]]; then
# green " 状态显示--是否开启BBR: 已开启 "
# else
# green " 状态显示--是否开启BBR: 未开启 "
# fi
# if [[ ${isFlagBbr} == *"bbrplus"* ]]; then
# green " 状态显示--是否开启BBR Plus: 已开启 "
# else
# green " 状态显示--是否开启BBR Plus: 未开启 "
# fi
# if [[ ${isFlagBbr} == *"bbr2"* ]]; then
# green " 状态显示--是否开启BBR2: 已开启 "
# else
# green " 状态显示--是否开启BBR2: 未开启 "
# fi
green " =================================================="
echo
}
function enableBBRSysctlConfig(){
# https://hostloc.com/thread-644985-1-1.html
# 优质线路用5.5+cake和原版bbr带宽跑的更足,不过cake的话就算高峰也不会像原版bbr那样跑不动,相比plus能慢些,但是区别不大,
# bbr plus的话美西或者一些延迟高的,用起来更好,锐速针对丢包高的有奇效
# 带宽大,并且延迟低不丢包的话5.5+cake在我这比较好,延迟高用plus更好,丢包多锐速最好. 一般130ms以下用cake不错,以上的话用plus更好些
# https://github.com/xanmod/linux/issues/26
# 说白了 bbrplus 就是改了点东西,然后那部分修改在 5.1 内核里合并进去了, 5.1 及以上的内核里自带的 bbr 已经包含了所谓的 bbrplus 的修改。
# PS:bbr 是一直在修改的,比如说 5.0 内核的 bbr,4.15 内核的 bbr 和 4.9 内核的 bbr 其实都是不一样的
# https://sysctl-explorer.net/net/ipv4/tcp_ecn/
removeBbrSysctlConfig
currentBBRText="bbr"
currentQueueText="fq"
currentECNValue="2"
currentECNText=""
if [ $1 = "bbrplus" ]; then
currentBBRText="bbrplus"
else
echo
echo " 请选择开启 (1) BBR 还是 (2) BBR2 网络加速 "
red " 选择 1 BBR 需要内核在 4.9 以上"
red " 选择 2 BBR2 需要内核为 XanMod "
read -p "请选择? 直接回车默认选1 BBR, 请输入[1/2]:" BBRTcpInput
BBRTcpInput=${BBRTcpInput:-1}
if [[ $BBRTcpInput == [2] ]]; then
if [[ ${osKernelVersionFull} == *"xanmod"* ]]; then
currentBBRText="bbr2"
echo
echo " 请选择是否开启 ECN, (1) 关闭 (2) 开启 (3) 仅对入站请求开启 "
red " 注意: 开启 ECN 可能会造成网络设备无法访问"
read -p "请选择? 直接回车默认选1 关闭ECN, 请输入[1/2]:" ECNTcpInput
ECNTcpInput=${ECNTcpInput:-1}
if [[ $ECNTcpInput == [2] ]]; then
currentECNValue="1"
currentECNText="+ ECN"
elif [[ $ECNTcpInput == [3] ]]; then
currentECNValue="2"
else
currentECNValue="0"
fi
else
echo
red " 当前系统内核没有安装 XanMod 内核, 无法开启BBR2, 改为开启BBR"
echo
currentBBRText="bbr"
fi
else
currentBBRText="bbr"
fi
fi
echo
echo " 请选择队列算法 (1) FQ, (2) FQ-Codel, (3) FQ-PIE, (4) CAKE "
red " 选择 2 FQ-Codel 队列算法 需要内核在 4.13 以上"
red " 选择 3 FQ-PIE 队列算法 需要内核在 5.6 以上"
red " 选择 4 CAKE 队列算法 需要内核在 5.5 以上"
read -p "请选择队列算法? 直接回车默认选1 FQ, 请输入[1/2/3/4]:" BBRQueueInput
BBRQueueInput=${BBRQueueInput:-1}
if [[ $BBRQueueInput == [2] ]]; then
currentQueueText="fq_codel"
elif [[ $BBRQueueInput == [3] ]]; then
currentQueueText="fq_pie"
elif [[ $BBRQueueInput == [4] ]]; then
currentQueueText="cake"
else
currentQueueText="fq"
fi
echo "net.core.default_qdisc=${currentQueueText}" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=${currentBBRText}" >> /etc/sysctl.conf
echo "net.ipv4.tcp_ecn=${currentECNValue}" >> /etc/sysctl.conf
isSysctlText=$(sysctl -p 2>&1 | grep "No such file")
echo
if [[ -z "$isSysctlText" ]]; then
green " 已成功开启 ${currentBBRText} + ${currentQueueText} ${currentECNText} "
else
green " 已成功开启 ${currentBBRText} ${currentECNText}"
red " 但当前内核版本过低, 开启队列算法 ${currentQueueText} 失败! "
red "请重新运行脚本, 选择'2 开启 BBR 加速'后, 务必再选择 (1)FQ 队列算法 !"
fi
echo
read -p "是否优化系统网络配置? 直接回车默认优化, 请输入[Y/n]:" isOptimizingSystemInput
isOptimizingSystemInput=${isOptimizingSystemInput:-Y}
if [[ $isOptimizingSystemInput == [Yy] ]]; then
addOptimizingSystemConfig "cancel"
else
echo
echo "sysctl -p"
echo
sysctl -p
echo
fi
}
# 卸载 bbr+锐速 配置
function removeBbrSysctlConfig(){
sed -i '/net.core.default_qdisc/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_congestion_control/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_ecn/d' /etc/sysctl.conf
if [[ -e /appex/bin/lotServer.sh ]]; then
bash <(wget --no-check-certificate -qO- https://git.io/lotServerInstall.sh) uninstall
fi
}
function removeOptimizingSystemConfig(){
removeBbrSysctlConfig
sed -i '/fs.file-max/d' /etc/sysctl.conf
sed -i '/fs.inotify.max_user_instances/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_syncookies/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_fin_timeout/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_tw_reuse/d' /etc/sysctl.conf
sed -i '/net.ipv4.ip_local_port_range/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_max_syn_backlog/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_max_tw_buckets/d' /etc/sysctl.conf
sed -i '/net.ipv4.route.gc_timeout/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_syn_retries/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_synack_retries/d' /etc/sysctl.conf
sed -i '/net.core.somaxconn/d' /etc/sysctl.conf
sed -i '/net.core.netdev_max_backlog/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_timestamps/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_max_orphans/d' /etc/sysctl.conf
# sed -i '/net.ipv4.ip_forward/d' /etc/sysctl.conf
sed -i '/1000000/d' /etc/security/limits.conf
sed -i '/1000000/d' /etc/profile
echo
green " 已删除当前系统的网络优化配置 "
echo
}
function addOptimizingSystemConfig(){
# https://ustack.io/2019-11-21-Linux%E5%87%A0%E4%B8%AA%E9%87%8D%E8%A6%81%E7%9A%84%E5%86%85%E6%A0%B8%E9%85%8D%E7%BD%AE.html
# https://www.cnblogs.com/xkus/p/7463135.html
# 优化系统配置
if grep -q "1000000" "/etc/profile"; then
echo
green " 系统网络配置 已经优化过, 不需要再次优化 "
echo
sysctl -p
echo
exit
fi
if [ -z $1 ]; then
removeOptimizingSystemConfig
fi
echo
green " 开始准备 优化系统网络配置 "
cat >> /etc/sysctl.conf <<-EOF
fs.file-max = 1000000
fs.inotify.max_user_instances = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 32768
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_max_orphans = 32768
# forward ipv4
#net.ipv4.ip_forward = 1
EOF
cat >> /etc/security/limits.conf <<-EOF
* soft nofile 1000000
* hard nofile 1000000
EOF
echo "ulimit -SHn 1000000" >> /etc/profile
source /etc/profile
echo
sysctl -p
echo
green " 已完成 系统网络配置的优化 "
echo
rebootSystem "noinfo"
}
function startIpv4(){
cat >> /etc/sysctl.conf <<-EOF
net.ipv4.tcp_retries2 = 8
net.ipv4.tcp_slow_start_after_idle = 0
# forward ipv4
net.ipv6.conf.all.disable_ipv6 = 0
net.ipv6.conf.default.disable_ipv6 = 0
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
EOF
}
function Enable_IPv6_Support() {
if [[ $(sysctl -a | grep 'disable_ipv6.*=.*1') || $(cat /etc/sysctl.{conf,d/*} | grep 'disable_ipv6.*=.*1') ]]; then
sed -i '/disable_ipv6/d' /etc/sysctl.{conf,d/*}
echo 'net.ipv6.conf.all.disable_ipv6 = 0' >/etc/sysctl.d/ipv6.conf
sysctl -w net.ipv6.conf.all.disable_ipv6=0
fi
}
isInstallFromRepo="no"
userHomePath="${HOME}/download_linux_kernel"
linuxKernelByUser="elrepo"
linuxKernelToBBRType=""
linuxKernelToInstallVersion="5.15"
linuxKernelToInstallVersionFull=""
elrepo_kernel_name="kernel-ml"
elrepo_kernel_version="5.4.110"
altarch_kernel_name="kernel"
altarch_kernel_version="5.4.105"
function downloadFile(){
tempUrl=$1
tempFilename=$(echo "${tempUrl##*/}")
echo "${userHomePath}/${linuxKernelToInstallVersionFull}/${tempFilename}"
if [ -f "${userHomePath}/${linuxKernelToInstallVersionFull}/${tempFilename}" ]; then
green "文件已存在, 不需要下载, 文件原下载地址: $1 "
else
green "文件下载中... Path: $1 "
wget -N --no-check-certificate -P ${userHomePath}/${linuxKernelToInstallVersionFull} $1
if [ $? -eq 0 ]; then
green "文件下载成功. ${userHomePath}/${linuxKernelToInstallVersionFull}/${tempFilename}"
else
red "文件下载失败, 请检查网络或者手动下载文件到目录 ${userHomePath}/${linuxKernelToInstallVersionFull} "
promptContinueOpeartion
fi
fi
echo
}
function installKernel(){
preferIPV4
if [ "${linuxKernelToBBRType}" = "bbrplus" ]; then
getVersionBBRPlus
fi
if [[ "${osRelease}" == "debian" || "${osRelease}" == "ubuntu" ]]; then
installDebianUbuntuKernel
elif [[ "${osRelease}" == "centos" ]]; then
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
if [ "${linuxKernelToBBRType}" = "xanmod" ]; then
red " xanmod 内核不支持 Centos 系统安装"
exit 255
fi
if [ "${isInstallFromRepo}" = "yes" ]; then
getLatestCentosKernelVersion
installCentosKernelFromRepo
else
if [ "${linuxKernelToBBRType}" = "bbrplus" ]; then
echo
else
getLatestCentosKernelVersion "manual"
fi
installCentosKernelManual
fi
fi
}
function getVersionBBRPlus(){
if [ "${linuxKernelToInstallVersion}" = "6.7" ]; then
bbrplusKernelVersion=$(getGithubLatestReleaseVersionBBRPlus "UJX6N/bbrplus-6.x_stable")
elif [ "${linuxKernelToInstallVersion}" = "6.6" ]; then
bbrplusKernelVersion=$(getGithubLatestReleaseVersionBBRPlus "UJX6N/bbrplus-6.6")
elif [ "${linuxKernelToInstallVersion}" = "6.1" ]; then
bbrplusKernelVersion=$(getGithubLatestReleaseVersionBBRPlus "UJX6N/bbrplus-6.1")