1
+ #! /bin/bash
2
+ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~ /bin
3
+ export PATH
4
+ # Green_font="\033[32m" && Yellow_font="\033[33m" && Red_font="\033[31m" && Font_suffix="\033[0m"
5
+ Info=" \033[32m [Info] \033[0m"
6
+ Error=" \033[31m [Error] \033[0m"
7
+ reboot=" \033[33m reboot \033[0m"
8
+ echo -e " \033[32m
9
+ #======================================
10
+ # Project: tcp_nanqinlang
11
+ # Version: 1.3.0
12
+ # Author: nanqinlang
13
+ # Blog: https://www.nanqinlang.com
14
+ # Github: https://github.com/nanqinlang
15
+ #====================================== \033[0m"
16
+
17
+ # check system
18
+ check_system (){
19
+ if [[ -f /etc/redhat-release ]]; then
20
+ release=" centos"
21
+ elif cat /etc/issue | grep -q -E -i " debian" ; then
22
+ release=" debian"
23
+ elif cat /etc/issue | grep -q -E -i " ubuntu" ; then
24
+ release=" ubuntu"
25
+ elif cat /etc/issue | grep -q -E -i " centos|red hat|redhat" ; then
26
+ release=" centos"
27
+ elif cat /proc/version | grep -q -E -i " debian" ; then
28
+ release=" debian"
29
+ elif cat /proc/version | grep -q -E -i " ubuntu" ; then
30
+ release=" ubuntu"
31
+ elif cat /proc/version | grep -q -E -i " centos|red hat|redhat" ; then
32
+ release=" centos"
33
+ fi
34
+ if [[ " ${release} " != " debian" ]]; then
35
+ if [[ " ${release} " != " ubuntu" ]]; then
36
+ echo -e " ${Error} not support!" && exit 1
37
+ else echo -e " ${Info} system is ubuntu"
38
+ fi
39
+ else echo -e " ${Info} system is debian"
40
+ fi
41
+ }
42
+
43
+ # check root
44
+ check_root (){
45
+ if [[ " $( id -u) " != " 0" ]]; then
46
+ echo -e " ${Error} must be root user" && exit 1
47
+ else echo -e " ${Info} user is root"
48
+ fi
49
+ }
50
+
51
+ # check ovz
52
+ check_ovz (){
53
+ virt=` virt-what`
54
+ if [[ " ${virt} " = " " ]]; then
55
+ apt-get install virt-what -y
56
+ virt=` virt-what`
57
+ fi
58
+ if [[ " ${virt} " = " openvz" ]]; then
59
+ echo -e " ${Error} OpenVZ is not support!" && exit 1
60
+ else echo -e " ${Info} virt is ${virt} "
61
+ fi
62
+ }
63
+
64
+ # determine workplace directory
65
+ directory (){
66
+ if [[ ! -d /root/tcp_nanqinlang ]]; then
67
+ mkdir /root/tcp_nanqinlang
68
+ fi
69
+ cd /root/tcp_nanqinlang
70
+ }
71
+
72
+ # get latest address
73
+ get_latest_version (){
74
+ latest_version=` wget -qO- " http://kernel.ubuntu.com/~kernel-ppa/mainline/" | awk -F' \"v' ' /v[0-9].[0-9]*.[0-9]/{print $2}' | grep -v ' \-rc' | cut -d/ -f1 | sort -V | tail -1`
75
+ echo -e " ${Info} latest version : ${latest_version} "
76
+ }
77
+ get_latest_url (){
78
+ headers_all_name=` wget -qO- http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} / | grep " linux-headers" | awk -F' \">' ' /all.deb/{print $2}' | cut -d' <' -f1 | head -1`
79
+ headers_all_url=" http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} /${headers_all_name} "
80
+ bit=` uname -m`
81
+ if [[ " ${bit} " = " x86_64" ]]; then
82
+ image_name=` wget -qO- http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} / | grep " linux-image" | grep " lowlatency" | awk -F' \">' ' /amd64.deb/{print $2}' | cut -d' <' -f1 | head -1`
83
+ image_url=" http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} /${image_name} "
84
+ headers_bit_name=` wget -qO- http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} / | grep " linux-headers" | grep " lowlatency" | awk -F' \">' ' /amd64.deb/{print $2}' | cut -d' <' -f1 | head -1`
85
+ headers_bit_url=" http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} /${headers_bit_name} "
86
+ elif [[ " ${bit} " = " i386" ]]; then
87
+ image_name=` wget -qO- http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} / | grep " linux-image" | grep " lowlatency" | awk -F' \">' ' /i386.deb/{print $2}' | cut -d' <' -f1 | head -1`
88
+ image_url=" http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} /${image_name} "
89
+ headers_bit_name=` wget -qO- http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} / | grep " linux-headers" | grep " lowlatency" | awk -F' \">' ' /i386.deb/{print $2}' | cut -d' <' -f1 | head -1`
90
+ headers_bit_url=" http://kernel.ubuntu.com/~kernel-ppa/mainline/v${latest_version} /${headers_bit_name} "
91
+ else
92
+ echo -e " ${Error} not support bit !" && exit 1
93
+ fi
94
+ }
95
+
96
+ # delete surplus image
97
+ delete_surplus_image (){
98
+ if [[ " ${surplus_total_image} " != " 0" ]]; then
99
+ for(( integer = 1 ; integer <= ${surplus_total_image} ; integer++ ))
100
+ do
101
+ surplus_sort_image=` dpkg -l| grep linux-image | awk ' {print $2}' | grep -v " ${latest_version} " | head -${integer} `
102
+ apt-get purge ${surplus_sort_image} -y
103
+ done
104
+ apt-get autoremove -y
105
+ if [[ " ${surplus_ver_image} " = " " ]]; then
106
+ echo -e " ${Info} uninstall all surplus images successfully, continuing"
107
+ else echo -e " ${Error} uninstall all surplus images failed, please check!" && exit 1
108
+ fi
109
+ else echo -e " ${Error} check image failed, please check!" && exit 1
110
+ fi
111
+ }
112
+ # delete surplus headers
113
+ delete_surplus_headers (){
114
+ if [[ " ${surplus_total_headers} " != " 0" ]]; then
115
+ for(( integer = 1 ; integer <= ${surplus_total_headers} ; integer++ ))
116
+ do
117
+ surplus_sort_headers=` dpkg -l| grep linux-headers | awk ' {print $2}' | grep -v " ${latest_version} " | head -${integer} `
118
+ apt-get purge ${surplus_sort_headers} -y
119
+ done
120
+ apt-get autoremove -y
121
+ if [[ " ${surplus_ver_headers} " = " " ]]; then
122
+ echo -e " ${Info} uninstall all surplus headers successfully, continuing"
123
+ else echo -e " ${Error} uninstall all surplus headers failed, please check!" && exit 1
124
+ fi
125
+ else echo -e " ${Error} check headers failed, please check!" && exit 1
126
+ fi
127
+ }
128
+
129
+ install_latest_image (){
130
+ if [[ -f " ${image_name} " ]]; then
131
+ echo -e " ${Info} image exists"
132
+ else echo -e " ${Info} downloading image" && wget ${image_url}
133
+ fi
134
+ if [[ -f " ${image_name} " ]]; then
135
+ echo -e " ${Info} installing image" && dpkg -i ${image_name}
136
+ else echo -e " ${Error} image download failed, please check!" && exit 1
137
+ fi
138
+ }
139
+ install_latest_headers (){
140
+ if [[ -f ${headers_all_name} ]]; then
141
+ echo -e " ${Info} headers_all exists"
142
+ else echo -e " ${Info} downloading headers_all" && wget ${headers_all_url}
143
+ fi
144
+ if [[ -f ${headers_all_name} ]]; then
145
+ echo -e " ${Info} installing headers_all" && dpkg -i ${headers_all_name}
146
+ else echo -e " ${Error} headers_all download failed, please check!" && exit 1
147
+ fi
148
+ if [[ -f ${headers_bit_name} ]]; then
149
+ echo -e " ${Info} headers_bit exists"
150
+ else echo -e " ${Info} downloading headers_bit" && wget ${headers_bit_url}
151
+ fi
152
+ if [[ -f ${headers_bit_name} ]]; then
153
+ echo -e " ${Info} installing headers_bit" && dpkg -i ${headers_bit_name}
154
+ else echo -e " ${Error} headers_bit download failed, please check!" && exit 1
155
+ fi
156
+ }
157
+
158
+ # check/install latest version and remove surplus kernel
159
+ check_kernel (){
160
+ get_latest_version && get_latest_url
161
+ # kernel version = latest version
162
+ surplus_ver_image=` dpkg -l | grep linux-image | awk ' {print $2}' | awk -F ' -' ' {print $3}' | grep " ${latest_version} " `
163
+ surplus_ver_headers=` dpkg -l | grep linux-headers | awk ' {print $2}' | awk -F ' -' ' {print $3}' | grep " ${latest_version} " `
164
+ # total digit of kernel without latest version
165
+ surplus_total_image=` dpkg -l| grep linux-image | awk ' {print $2}' | grep -v " ${latest_version} " | wc -l`
166
+ surplus_total_headers=` dpkg -l| grep linux-headers | awk ' {print $2}' | grep -v " ${latest_version} " | wc -l`
167
+ if [[ " ${surplus_ver_image} " = " ${latest_version} " ]]; then
168
+ echo -e " ${Info} image already have a latest version"
169
+ else echo -e " ${Info} installing latest image" && install_latest_image
170
+ fi
171
+ if [[ " ${surplus_total_image} " != " 0" ]]; then
172
+ echo -e " ${Info} removing surplus image" && delete_surplus_image
173
+ else echo -e " ${Info} no surplus image need to remove"
174
+ fi
175
+ if [[ " ${surplus_ver_headers} " = " ${latest_version} " ]]; then
176
+ echo -e " ${Info} headers already have a latest version"
177
+ else echo -e " ${Info} installing latest headers" && install_latest_headers
178
+ fi
179
+ if [[ " ${surplus_total_headers} " != " 0" ]]; then
180
+ echo -e " ${Info} removing surplus headers" && delete_surplus_headers
181
+ else echo -e " ${Info} no surplus headers need to remove"
182
+ fi
183
+ update-grub
184
+ }
185
+
186
+ # dpkg -l
187
+ dpkg_list (){
188
+ dpkg -l| grep linux-image | awk ' {print $2}'
189
+ dpkg -l| grep linux-headers | awk ' {print $2}'
190
+ echo -e " \033[32m please ensure above kernel list is the same as: \033[0m"
191
+ echo -e " \033[33m linux-image-${latest_version} -lowlatency \033[0m"
192
+ echo -e " \033[33m linux-headers-${latest_version} \033[0m"
193
+ echo -e " \033[33m linux-headers-${latest_version} -lowlatency \033[0m"
194
+ }
195
+
196
+ # (1)while kernel is 4.12.2
197
+ ver_4.12.2 (){
198
+ echo -e " ${Info} kernel version is 4.12.2"
199
+ if [[ ! -f tcp_nanqinlang.ko ]]; then
200
+ # directly download .ko pattern
201
+ echo -e " ${Info} downloading mod_for_4.12.2" && wget -O tcp_nanqinlang.ko " https://raw.githubusercontent.com/nanqinlang/tcp_nanqinlang/master/tcp_nanqinlang_for_4.12.2.ko"
202
+ fi
203
+ # check download or not and apply
204
+ if [[ ! -f tcp_nanqinlang.ko ]]; then
205
+ echo -e " ${Error} download mod_for_4.12.2 failed,please check!" && exit 1
206
+ else echo " ${Info} loading mod_for_4.12.2 now" && chmod +x ./tcp_nanqinlang.ko && insmod tcp_nanqinlang.ko && sysctl -p
207
+ fi
208
+ }
209
+
210
+ # (2)while kernel isn't 4.12.2
211
+ ver_current (){
212
+ if [[ ! -f tcp_nanqinlang.ko ]]; then
213
+ # need to compile .ko mod
214
+ echo -e " ${Info} will compile mod_for_current" && apt-get update && apt-get install build-essential -y && apt-get update && apt-get install git python make gcc-4.9 -y && apt-get update && wget https://raw.githubusercontent.com/nanqinlang/tcp_nanqinlang/master/tcp_nanqinlang.c && echo " obj-m:=tcp_nanqinlang.o" > Makefile && make -C /lib/modules/$( uname -r) /build M=` pwd` modules CC=/usr/bin/gcc-4.9
215
+ fi
216
+ # check compile or not and apply
217
+ if [[ ! -f tcp_nanqinlang.ko ]]; then
218
+ echo -e " ${Error} compiling mod_for_current failed, please check!" && exit 1
219
+ else echo -e " ${Info} mod_for_current is compiled, loading now" && chmod +x ./tcp_nanqinlang.ko && insmod tcp_nanqinlang.ko && sysctl -p
220
+ fi
221
+ }
222
+
223
+ # check status
224
+ check_status (){
225
+ status_sysctl=` sysctl net.ipv4.tcp_available_congestion_control | awk ' {print $3}' `
226
+ status_lsmod=` lsmod | grep nanqinlang`
227
+ if [[ " ${status_sysctl} " = " nanqinlang" ]]; then
228
+ echo -e " ${Info} tcp_nanqinlang is installed!"
229
+ if [[ " ${status_lsmod} " = " " ]]; then
230
+ echo -e " ${Info} tcp_nanqinlang is installed but not running, please ${reboot} then run \033[33m Start \033[0m" && exit 0
231
+ else echo -e " ${Info} tcp_nanqinlang is running!" && exit 0
232
+ fi
233
+ else echo -e " ${Error} tcp_nanqinlang not installed, please \033[33m install \033[0m then try start anain" && exit 1
234
+ fi
235
+ }
236
+
237
+ # install
238
+ install (){
239
+ # environment
240
+ check_system
241
+ check_root
242
+ apt-get update && apt-get install debian-keyring debian-archive-keyring -y && apt-key update && apt-get update
243
+ check_ovz
244
+ # replace and apply
245
+ directory
246
+ check_kernel
247
+ echo " net.ipv4.tcp_fastopen=3" >> /etc/sysctl.conf
248
+ echo " net.core.default_qdisc=fq_codel" >> /etc/sysctl.conf
249
+ echo " net.ipv4.tcp_congestion_control=nanqinlang" >> /etc/sysctl.conf
250
+ sysctl -p
251
+ # reboot part
252
+ dpkg_list
253
+ echo -e " then remember after ${reboot} , run \033[33m start \033[0m"
254
+ exit 0
255
+ }
256
+
257
+ # start
258
+ start (){
259
+ check_system
260
+ check_root
261
+ check_ovz
262
+ # determine kernel is 4.12.2 or not
263
+ ver=` dpkg -l | grep linux-image | awk ' {print $2}' | awk -F ' -' ' {print $3}' | grep " 4.12.2" `
264
+ if [[ " ${ver} " = " 4.12.2" ]]; then
265
+ ver_4.12.2
266
+ else
267
+ ver_current
268
+ fi
269
+ check_status
270
+ }
271
+
272
+ # status
273
+ status (){
274
+ check_system
275
+ check_root
276
+ check_ovz
277
+ check_status
278
+ }
279
+
280
+ # upgrade
281
+ upgrade (){
282
+ check_system
283
+ check_root
284
+ check_ovz
285
+ check_kernel
286
+ exit 0
287
+ }
288
+
289
+ # stop
290
+ stop (){
291
+ check_system
292
+ check_root
293
+ check_ovz
294
+ sed -i ' /net\.ipv4\.tcp_congestion_control=nanqinlang/d' /etc/sysctl.conf
295
+ sysctl -p
296
+ echo -e " ${Info} please remember ${reboot} to stop tcp_nanqinlang"
297
+ exit 0
298
+ }
299
+
300
+ # ${command}
301
+ command=$1
302
+ if [[ " ${command} " = " " ]]; then
303
+ echo -e " ${Info} command not found, usage: \033[32m { install | start | status | upgrade | stop } \033[0m" && exit 0
304
+ else
305
+ command=$1
306
+ fi
307
+ case " ${command} " in
308
+ install)
309
+ install 2>&1 | tee -i /root/tcp_nanqinlang_install.log
310
+ ;;
311
+ start)
312
+ start 2>&1 | tee -i /root/tcp_nanqinlang_start.log
313
+ ;;
314
+ status)
315
+ status 2>&1 | tee -i /root/tcp_nanqinlang_status.log
316
+ ;;
317
+ upgrade)
318
+ upgrade 2>&1 | tee -i /root/tcp_nanqinlang_upgrade.log
319
+ ;;
320
+ stop)
321
+ stop 2>&1 | tee -i /root/tcp_nanqinlang_stop.log
322
+ ;;
323
+ esac
0 commit comments