更新 CentOS 7 的核心版本

為什麼要更新核心版本?

先前在維護 CentOS 7 的相關主機都是採用原本舊的 3.x 版本核心,在穩定的前提下,基本上都不會去變更核心的版本,但是在開發 OxOOL V4 的過程中,因為要測試大量的多人連線編輯工作效能,發現在新的 Rocky Linux 8 及 Ubuntu 20.0.4 的環境下,效能有不俗的躍進,故想測試看看 CentOS 7 在更新核心之後,在 OxOOL V4 的效能表現是否有改進,沒想到有不少的提升,故有本篇的記錄,所以接下來的說明,適用於以下的條件。

確認目前 CentOS 7 的核心版本

在進行本項工作前,建議先將 CentOS 7 的版本更新至最新的 CentOS Linux release 7.9.2009 (Core) 版本,可以使用以下的指令確認:

# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

然後確認一下目前系統的核心版本,指令如下:

# uname -a
Linux oxoolcommunity.ossii.com.tw 3.10.0-1160.66.1.el7.x86_64 #1 SMP Wed May 18 16:02:34 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

目前 CentOS 7 最新的核心版本為:3.10.0-1160.66.1.el7.x86_64,可以再使用 yum update 指令更新。

# yum update -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror01.idc.hinet.net
 * extras: mirror01.idc.hinet.net
 * updates: mirror01.idc.hinet.net
No packages marked for update

安裝最新的 Kernel,並移除舊的核心套件

請先匯入最新的核心來源,指令如下:

# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
正在擷取 http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
正在擷取 http://elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
正在準備…                       ################################# [100%]
Updating / installing...
   1:elrepo-release-7.0-4.el7.elrepo  ################################# [100%]

然後安裝最新的核心 5.x 版本。

# yum --enablerepo=elrepo-kernel install kernel-ml -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror01.idc.hinet.net
 * elrepo: ftp.yz.yamagata-u.ac.jp
 * elrepo-kernel: ftp.yz.yamagata-u.ac.jp
 * extras: mirror01.idc.hinet.net
 * updates: mirror01.idc.hinet.net
elrepo                                                   | 3.0 kB     00:00
elrepo-kernel                                            | 3.0 kB     00:00
(1/2): elrepo/primary_db                                   | 580 kB   00:00
(2/2): elrepo-kernel/primary_db                            | 2.1 MB   00:00
Resolving Dependencies
--> Running transaction check
---> Package kernel-ml.x86_64 0:5.18.6-1.el7.elrepo will be installed
--> Finished Dependency Resolution

Dependencies Resolved

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

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

Total download size: 56 M
Installed size: 257 M
Is this ok [y/d/N]: y
Downloading packages:
kernel-ml-5.18.6-1.el7.elrepo.x86_64.rpm                   |  56 MB   00:05
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : kernel-ml-5.18.6-1.el7.elrepo.x86_64                         1/1
  Verifying  : kernel-ml-5.18.6-1.el7.elrepo.x86_64                         1/1

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

Complete!

然後查詢一下目前有安裝在系統上的核心版本,指令如下:

# rpm -qa |grep kernel
kernel-tools-3.10.0-1160.66.1.el7.x86_64
kernel-3.10.0-1160.49.1.el7.x86_64
kernel-tools-libs-3.10.0-1160.66.1.el7.x86_64
kernel-3.10.0-1160.66.1.el7.x86_64
kernel-3.10.0-1160.53.1.el7.x86_64
kernel-3.10.0-1160.59.1.el7.x86_64
kernel-3.10.0-1160.62.1.el7.x86_64
kernel-ml-5.18.6-1.el7.elrepo.x86_64

接下來移掉舊版本的 kernel,以及其它核心的 tools、libs 套件,因為等一下要改安裝 kernel-5.18 的 libs、tools 等套件,會有衝突,故要先移除舊的,不過要小心處理,不要移錯,不然有可能等一下開不了機!以本例來說,我們要移掉以下套件,範例如下。

# yum remove kernel-tools-3.10.0-1160.66.1.el7.x86_64 kernel-tools-libs-3.10.0-1160.66.1.el7.x86_64
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package kernel-tools.x86_64 0:3.10.0-1160.66.1.el7 will be erased
---> Package kernel-tools-libs.x86_64 0:3.10.0-1160.66.1.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package               Arch       Version                    Repository    Size
================================================================================
Removing:
 kernel-tools          x86_64     3.10.0-1160.66.1.el7       @updates     337 k
 kernel-tools-libs     x86_64     3.10.0-1160.66.1.el7       @updates      18 k

Transaction Summary
================================================================================
Remove  2 Packages

Installed size: 355 k
Is this ok [y/N]:確認再按 y 鍵

移掉後,再查詢一下目前的 Kernel 版本。

# rpm -qa|grep kernel                                    kernel-3.10.0-1160.49.1.el7.x86_64
kernel-3.10.0-1160.66.1.el7.x86_64
kernel-3.10.0-1160.53.1.el7.x86_64
kernel-3.10.0-1160.59.1.el7.x86_64
kernel-3.10.0-1160.62.1.el7.x86_64
kernel-ml-5.18.6-1.el7.elrepo.x86_64

除了 kernel-ml-5.18.6-1.el7.elrepo.x86_64kernel-3.10.0-1160.66.1.el7.x86_64 外,其它舊版本都可以移掉了。

# yum remove kernel-3.10.0-1160.49.1.el7.x86_64 kernel-3.10.0-1160.53.1.el7.x86_64 kernel-3.10.0-1160.59.1.el7.x86_64 kernel-3.10.0-1160.62.1.el7.x86_64
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-1160.49.1.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-1160.53.1.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-1160.59.1.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-1160.62.1.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package       Arch          Version                      Repository       Size
================================================================================
Removing:
 kernel        x86_64        3.10.0-1160.49.1.el7         @updates         64 M
 kernel        x86_64        3.10.0-1160.53.1.el7         @updates         64 M
 kernel        x86_64        3.10.0-1160.59.1.el7         @updates         64 M
 kernel        x86_64        3.10.0-1160.62.1.el7         @updates         64 M

Transaction Summary
================================================================================
Remove  4 Packages

Installed size: 257 M
Is this ok [y/N]:一樣確認後再按 y 鍵

再安裝新核心的表頭、libs 及 tools 套件。

# yum --enablerepo=elrepo-kernel install kernel-ml-headers kernel-ml-tools kernel-ml-tools-libs -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror01.idc.hinet.net
 * elrepo: ftp.ne.jp
 * elrepo-kernel: ftp.ne.jp
 * extras: mirror01.idc.hinet.net
 * updates: mirror01.idc.hinet.net
Resolving Dependencies
--> Running transaction check
---> Package kernel-ml-headers.x86_64 0:5.18.6-1.el7.elrepo will be installed
---> Package kernel-ml-tools.x86_64 0:5.18.6-1.el7.elrepo will be installed
---> Package kernel-ml-tools-libs.x86_64 0:5.18.6-1.el7.elrepo will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch     Version                  Repository       Size
================================================================================
Installing:
 kernel-ml-headers      x86_64   5.18.6-1.el7.elrepo      elrepo-kernel   1.6 M
 kernel-ml-tools        x86_64   5.18.6-1.el7.elrepo      elrepo-kernel   267 k
 kernel-ml-tools-libs   x86_64   5.18.6-1.el7.elrepo      elrepo-kernel   152 k

Transaction Summary
================================================================================
Install  3 Packages

Total download size: 2.0 M
Installed size: 6.2 M
Downloading packages:
(1/3): kernel-ml-tools-5.18.6-1.el7.elrepo.x86_64.rpm      | 267 kB   00:00
(2/3): kernel-ml-tools-libs-5.18.6-1.el7.elrepo.x86_64.rpm | 152 kB   00:00
(3/3): kernel-ml-headers-5.18.6-1.el7.elrepo.x86_64.rpm    | 1.6 MB   00:00
--------------------------------------------------------------------------------
Total                                              2.6 MB/s | 2.0 MB  00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : kernel-ml-tools-libs-5.18.6-1.el7.elrepo.x86_64              1/3
  Installing : kernel-ml-tools-5.18.6-1.el7.elrepo.x86_64                   2/3
  Installing : kernel-ml-headers-5.18.6-1.el7.elrepo.x86_64                 3/3
  Verifying  : kernel-ml-headers-5.18.6-1.el7.elrepo.x86_64                 1/3
  Verifying  : kernel-ml-tools-5.18.6-1.el7.elrepo.x86_64                   2/3
  Verifying  : kernel-ml-tools-libs-5.18.6-1.el7.elrepo.x86_64              3/3

Installed:
  kernel-ml-headers.x86_64 0:5.18.6-1.el7.elrepo
  kernel-ml-tools.x86_64 0:5.18.6-1.el7.elrepo
  kernel-ml-tools-libs.x86_64 0:5.18.6-1.el7.elrepo

Complete!

最後再更新一下,確認是否還有其它更新套件。

# yum update -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror01.idc.hinet.net
 * elrepo: hkg.mirror.rackspace.com
 * extras: mirror01.idc.hinet.net
 * updates: mirror01.idc.hinet.net
Resolving Dependencies
--> Running transaction check
---> Package elrepo-release.noarch 0:7.0-4.el7.elrepo will be updated
---> Package elrepo-release.noarch 0:7.0-5.el7.elrepo will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package              Arch         Version                   Repository    Size
================================================================================
Updating:
 elrepo-release       noarch       7.0-5.el7.elrepo          elrepo       8.6 k

Transaction Summary
================================================================================
Upgrade  1 Package

Total download size: 8.6 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
elrepo-release-7.0-5.el7.elrepo.noarch.rpm                 | 8.6 kB   00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : elrepo-release-7.0-5.el7.elrepo.noarch                       1/2
  Cleanup    : elrepo-release-7.0-4.el7.elrepo.noarch                       2/2
  Verifying  : elrepo-release-7.0-5.el7.elrepo.noarch                       1/2
  Verifying  : elrepo-release-7.0-4.el7.elrepo.noarch                       2/2

Updated:
  elrepo-release.noarch 0:7.0-5.el7.elrepo

Complete!

設定 grub 2 確認開機順序,並重新開機

先使用以下指令確認新的核心有在開機選單中:

# awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (5.18.6-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux (3.10.0-1160.66.1.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-db7214c5f0fd4b74abbce0498c2ad9b8) 7 (Core)

沒有的話,請確認前面的更新指令是否正確執行,接下來設定預設使用第 0 個核心版本的順位開機。

# grub2-set-default 0

重新開機,會看到開機時的核心選單已經更新完成。

image-1656062433979.png

登入後再確認一次核心版本:

# uname -a
Linux oxoolcommunity.ossii.com.tw 5.18.6-1.el7.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jun 21 09:11:23 EDT 2022 x86_64 x86_64 x86_64 GNU/Linux

這樣就完成了核心升級的工作!