CentOS 7 内核升级日记

前言:去年在某宝购买了一台赛扬J1900迷你主机,到手后第一时间装上了CentOS 7.4,安装后出现一个烦人的问题,关机或者重启会卡住不动,用企鹅云提供的办法也不能修复,天真的我以为是CentOS的兼容性问题……

昨天在一个叫逗比根据地的博客中看到,Linux系统通过开启BBR可以有效提高网络传输效率,遂寻思着给服务器开启BBR,但是文中说到开启BBR的前提是更换支持的内核。由此产生了此篇文章,此文章仅作为博主记录之用,如果您参考此文章升级内核导致出现诸如无法启动之类的问题请自行Google或百度解决,另外BBR仅4.9以及以上内核支持。
因为服务器没有类似VNC的链接方式,担心升级内核导致系统无法启动问题,所以就想着先拿小主机测试,原本只是想体验以下升级内核的过程,结果意外发现升级内核后关机或者重启不再出现卡住的问题。
大致流程:

  1. CentOS联网(也可以离线升级,但是这里只讨论在线升级)
  2. SSH或其它方式登录服务器
  3. 导入elrepo公钥(好像不是必要操作)
  4. 安装elrepo第三方源
  5. 升级内核
  6. 进行必要操作
  7. 清理文件(非必需)

先通过SSH管理软件或者ssh命令登录服务器,在终端中输入下面命令导入elrepo公钥

# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

导入公钥不会有任何回显结果,导入后输入下面命令安装elrepo第三方源

# yum install -y http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

Loaded plugins: fastestmirror
elrepo-release-7.0-3.el7.elrepo.noarch.rpm | 8.5 kB 00:00:00
Examining /var/tmp/yum-root-5CLquM/elrepo-release-7.0-3.el7.elrepo.noarch.rpm: elrepo-release-7.0-3.el7.elrepo.noarch
Marking /var/tmp/yum-root-5CLquM/elrepo-release-7.0-3.el7.elrepo.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package elrepo-release.noarch 0:7.0-3.el7.elrepo will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================
Package Arch Version Repository Size
====================================================================================================
Installing:
elrepo-release noarch 7.0-3.el7.elrepo /elrepo-release-7.0-3.el7.elrepo.noarch 5.2 k

Transaction Summary
====================================================================================================
Install 1 Package

Total size: 5.2 k
Installed size: 5.2 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : elrepo-release-7.0-3.el7.elrepo.noarch 1/1
Verifying : elrepo-release-7.0-3.el7.elrepo.noarch 1/1

Installed:
elrepo-release.noarch 0:7.0-3.el7.elrepo

Complete!

这里需要注意的地方:1.elrepo第三方源地址可能会有更新,更新后旧源不能使用,请自行登录http://www.elrepo.org/查询;2.也可以使用rpm进行安装;3.文末有一个香港的镜像源,但是该镜像的更新时间稍晚一些。

第三方源安装后就可以开始升级CentOS内核了,elrepo提供的内核有lt(Long Term Support)和ml(Mainline)两个版本,这里建议生产环境使用lt版,输入下面命令开始安装内核

# yum --enablerepo=elrepo-kernel -y install kernel-ml

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.vpshosting.com.hk
* elrepo: hkg.mirror.rackspace.com
* elrepo-kernel: hkg.mirror.rackspace.com
* epel: mirror.vinahost.vn
* extras: mirror.vpshosting.com.hk
* updates: mirror.vpshosting.com.hk
Resolving Dependencies
--> Running transaction check
---> Package kernel-ml.x86_64 0:4.14.10-1.el7.elrepo will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================
Package Arch Version Repository Size
====================================================================================================
Installing:
kernel-ml x86_64 4.14.10-1.el7.elrepo elrepo-kernel 43 M

Transaction Summary
====================================================================================================
Install 1 Package

Total download size: 43 M
Installed size: 194 M
Downloading packages:
kernel-ml-4.14.10-1.el7.elrepo.x86_64.rpm | 43 MB 00:00:14
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : kernel-ml-4.14.10-1.el7.elrepo.x86_64 1/1
Verifying : kernel-ml-4.14.10-1.el7.elrepo.x86_64 1/1

Installed:
kernel-ml.x86_64 0:4.14.10-1.el7.elrepo

Complete!

如果需要安装lt版请将其中的kernel-ml改为kernel-lt,国内服务器使用镜像网站的话下载速度会快很多。内核安装完毕后,需要设置启动项,否则直接重启服务器还是旧的内核。网上很多教程最后都是使用grub2-mkconfig -o /boot/grub2/grub.cfg生成启动项配置文件,这里不是反对使用此办法,而是这个办法并没有考虑到EFI启动方式(博主的小主机正是EFI启动方式),所以我们通过其它方式修改启动项,输入下面命令显示启动项

# grub2-mkconfig | grep '^menuentry' | awk '{match($0, /\047([^\047]+)\047/,arr);print arr[1]}'

Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.14.10-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-4.14.10-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.11.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.11.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-514.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-514.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-6712b88e2d104e0982edad833b6a6755
Found initrd image: /boot/initramfs-0-rescue-6712b88e2d104e0982edad833b6a6755.img
done
CentOS Linux (4.14.10-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux (3.10.0-693.11.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-693.5.2.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-514.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-6712b88e2d104e0982edad833b6a6755) 7 (Core)

提示:输出的结果里面可能包含无用信息

找到和你安装的内核版本相同的启动项,完整拷贝它或者记住它,因为等下要用。

输入下面命令设置为默认启动项(请注意替换红色字体为刚刚复制或记住的内容),请注意,博主并没有测试输错后重启会怎么用,感兴趣的小伙伴自行测试。

# grub2-set-default 'CentOS Linux (4.14.10-1.el7.elrepo.x86_64) 7 (Core)'

运行后并没有任何回显结果,但是可以使用下面命令查询默认启动项

# grub2-editenv list

saved_entry=CentOS Linux (4.14.10-1.el7.elrepo.x86_64) 7 (Core)

如果回显的结果与你设置的启动项相同,则可以reboot重启服务器了。

启动后可以使用下面命令查询内核版本

# uname -sr

Linux 4.14.10-1.el7.elrepo.x86_64

如果回显结果与你设置的启动项一致,表示启动成功。

接下来可以根据需求,清理旧版本内核(谨慎操作,清理步骤有误可能会导致服务器无法启动!!!)

使用下面命令查询已安装内核

# rpm -qa | grep kernel | awk '!/headers/'

kernel-3.10.0-514.el7.x86_64
kernel-devel-3.10.0-693.11.1.el7.x86_64
kernel-3.10.0-693.5.2.el7.x86_64
kernel-tools-libs-3.10.0-693.11.1.el7.x86_64
kernel-tools-3.10.0-693.11.1.el7.x86_64
kernel-ml-4.14.10-1.el7.elrepo.x86_64
kernel-devel-3.10.0-693.5.2.el7.x86_64
kernel-3.10.0-693.11.1.el7.x86_64

根据需求,拷贝或者记住需要卸载的内核版本,输入下面命令进行卸载

请注意!请注意!请注意!此命令仅供参考!由此导致的问题博主概不负责!

# yum -y autoremove kernel-3.10.0-514.el7.x86_64 kernel-devel-3.10.0-693.11.1.el7.x86_64 kernel-3.10.0-693.5.2.el7.x86_64 kernel-tools-libs-3.10.0-693.11.1.el7.x86_64 kernel-tools-3.10.0-693.11.1.el7.x86_64 kernel-devel-3.10.0-693.5.2.el7.x86_64 kernel-3.10.0-693.11.1.el7.x86_64

Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-514.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-693.5.2.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-693.11.1.el7 will be erased
---> Package kernel-devel.x86_64 0:3.10.0-693.5.2.el7 will be erased
--> Processing Dependency: kernel-devel-uname-r for package: systemtap-devel-3.1-4.el7_4.x86_64
---> Package kernel-devel.x86_64 0:3.10.0-693.11.1.el7 will be erased
---> Package kernel-tools.x86_64 0:3.10.0-693.11.1.el7 will be erased
---> Package kernel-tools-libs.x86_64 0:3.10.0-693.11.1.el7 will be erased
--> Running transaction check
---> Package systemtap-devel.x86_64 0:3.1-4.el7_4 will be erased
--> Processing Dependency: systemtap-devel = 3.1-4.el7_4 for package: systemtap-3.1-4.el7_4.x86_64
--> Running transaction check
---> Package systemtap.x86_64 0:3.1-4.el7_4 will be erased
--> Finished Dependency Resolution
--> Finding unneeded leftover dependencies
---> Marking systemtap-client to be removed - no longer needed by systemtap
---> Marking avahi-libs to be removed - no longer needed by systemtap-devel
---> Marking systemtap-runtime to be removed - no longer needed by systemtap-client
---> Marking mokutil to be removed - no longer needed by systemtap-client
---> Marking dyninst to be removed - no longer needed by systemtap-runtime
---> Marking efivar-libs to be removed - no longer needed by mokutil
---> Marking boost-thread to be removed - no longer needed by dyninst
---> Marking boost-system to be removed - no longer needed by dyninst
---> Marking boost-date-time to be removed - no longer needed by dyninst
---> Marking libdwarf to be removed - no longer needed by dyninst
Found and removing 10 unneeded dependencies
--> Running transaction check
---> Package avahi-libs.x86_64 0:0.6.31-17.el7 will be erased
---> Package boost-date-time.x86_64 0:1.53.0-27.el7 will be erased
---> Package boost-system.x86_64 0:1.53.0-27.el7 will be erased
---> Package boost-thread.x86_64 0:1.53.0-27.el7 will be erased
---> Package dyninst.x86_64 0:9.3.1-1.el7 will be erased
---> Package efivar-libs.x86_64 0:31-4.el7 will be erased
---> Package libdwarf.x86_64 0:20130207-4.el7 will be erased
---> Package mokutil.x86_64 0:12-1.el7.centos will be erased
---> Package systemtap-client.x86_64 0:3.1-4.el7_4 will be erased
---> Package systemtap-runtime.x86_64 0:3.1-4.el7_4 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================
Package Arch Version Repository Size
====================================================================================================
Removing:
kernel x86_64 3.10.0-514.el7 @anaconda 148 M
kernel x86_64 3.10.0-693.5.2.el7 @updates 59 M
kernel x86_64 3.10.0-693.11.1.el7 @updates 59 M
kernel-devel x86_64 3.10.0-693.5.2.el7 @updates 36 M
kernel-devel x86_64 3.10.0-693.11.1.el7 @updates 36 M
kernel-tools x86_64 3.10.0-693.11.1.el7 @updates 259 k
kernel-tools-libs x86_64 3.10.0-693.11.1.el7 @updates 18 k
Removing for dependencies:
avahi-libs x86_64 0.6.31-17.el7 @base 121 k
boost-date-time x86_64 1.53.0-27.el7 @base 136 k
boost-system x86_64 1.53.0-27.el7 @base 32 k
boost-thread x86_64 1.53.0-27.el7 @base 93 k
dyninst x86_64 9.3.1-1.el7 @base 12 M
efivar-libs x86_64 31-4.el7 @base 190 k
libdwarf x86_64 20130207-4.el7 @base 285 k
mokutil x86_64 12-1.el7.centos @base 82 k
systemtap x86_64 3.1-4.el7_4 @updates 197 k
systemtap-client x86_64 3.1-4.el7_4 @updates 9.6 M
systemtap-devel x86_64 3.1-4.el7_4 @updates 7.2 M
systemtap-runtime x86_64 3.1-4.el7_4 @updates 981 k

Transaction Summary
====================================================================================================
Remove 7 Packages (+12 Dependent packages)

Installed size: 369 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : systemtap-3.1-4.el7_4.x86_64 1/19
Erasing : kernel-devel.x86_64 2/19
Erasing : kernel.x86_64 3/19
Erasing : kernel.x86_64 4/19
Erasing : kernel.x86_64 5/19
Erasing : systemtap-client-3.1-4.el7_4.x86_64 6/19
Erasing : systemtap-devel-3.1-4.el7_4.x86_64 7/19
Erasing : mokutil-12-1.el7.centos.x86_64 8/19
Erasing : systemtap-runtime-3.1-4.el7_4.x86_64 9/19
Erasing : dyninst-9.3.1-1.el7.x86_64 10/19
Erasing : boost-thread-1.53.0-27.el7.x86_64 11/19
Erasing : kernel-tools-3.10.0-693.11.1.el7.x86_64 12/19
Erasing : kernel-devel.x86_64 13/19
Erasing : kernel-tools-libs-3.10.0-693.11.1.el7.x86_64 14/19
Erasing : boost-system-1.53.0-27.el7.x86_64 15/19
Erasing : boost-date-time-1.53.0-27.el7.x86_64 16/19
Erasing : libdwarf-20130207-4.el7.x86_64 17/19
Erasing : efivar-libs-31-4.el7.x86_64 18/19
Erasing : avahi-libs-0.6.31-17.el7.x86_64 19/19
Verifying : avahi-libs-0.6.31-17.el7.x86_64 1/19
Verifying : kernel-devel-3.10.0-693.5.2.el7.x86_64 2/19
Verifying : kernel-3.10.0-693.5.2.el7.x86_64 3/19
Verifying : systemtap-3.1-4.el7_4.x86_64 4/19
Verifying : boost-date-time-1.53.0-27.el7.x86_64 5/19
Verifying : dyninst-9.3.1-1.el7.x86_64 6/19
Verifying : kernel-tools-3.10.0-693.11.1.el7.x86_64 7/19
Verifying : boost-system-1.53.0-27.el7.x86_64 8/19
Verifying : kernel-3.10.0-693.11.1.el7.x86_64 9/19
Verifying : kernel-tools-libs-3.10.0-693.11.1.el7.x86_64 10/19
Verifying : libdwarf-20130207-4.el7.x86_64 11/19
Verifying : boost-thread-1.53.0-27.el7.x86_64 12/19
Verifying : systemtap-client-3.1-4.el7_4.x86_64 13/19
Verifying : systemtap-devel-3.1-4.el7_4.x86_64 14/19
Verifying : systemtap-runtime-3.1-4.el7_4.x86_64 15/19
Verifying : efivar-libs-31-4.el7.x86_64 16/19
Verifying : mokutil-12-1.el7.centos.x86_64 17/19
Verifying : kernel-3.10.0-514.el7.x86_64 18/19
Verifying : kernel-devel-3.10.0-693.11.1.el7.x86_64 19/19

Removed:
kernel.x86_64 0:3.10.0-514.el7 kernel.x86_64 0:3.10.0-693.5.2.el7
kernel.x86_64 0:3.10.0-693.11.1.el7 kernel-devel.x86_64 0:3.10.0-693.5.2.el7
kernel-devel.x86_64 0:3.10.0-693.11.1.el7 kernel-tools.x86_64 0:3.10.0-693.11.1.el7
kernel-tools-libs.x86_64 0:3.10.0-693.11.1.el7

Dependency Removed:
avahi-libs.x86_64 0:0.6.31-17.el7 boost-date-time.x86_64 0:1.53.0-27.el7
boost-system.x86_64 0:1.53.0-27.el7 boost-thread.x86_64 0:1.53.0-27.el7
dyninst.x86_64 0:9.3.1-1.el7 efivar-libs.x86_64 0:31-4.el7
libdwarf.x86_64 0:20130207-4.el7 mokutil.x86_64 0:12-1.el7.centos
systemtap.x86_64 0:3.1-4.el7_4 systemtap-client.x86_64 0:3.1-4.el7_4
systemtap-devel.x86_64 0:3.1-4.el7_4 systemtap-runtime.x86_64 0:3.1-4.el7_4

Complete!

到这里全部完成,关于开启BBR请参考此文章:https://teddysun.com/489.html

elrepo的香港镜像:http://hkg.mirror.rackspace.com/elrepo/kernel/el7/x86_64/RPMS/

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.