背景:近期遇到一个项目,需要重编Linux Kernel,并安装。Linux OS 是RHEL 8.7。 由于这是企业版系统, Kernel source 很难弄到。最后发现可以用CentOS 替代。
本文参考CentOS WiKi:**I Need the Kernel Source **
下面是对这两个OS 的介绍:
Red Hat Enterprise Linux (RHEL) 是一款商业 Linux 发行版,它不像一些开源发行版那样直接提供 kernel 源代码的公开访问。RHEL 的 kernel 源代码是通过订阅服务获取的。 如果你有有效的 RHEL 订阅并登录到 Red Hat 的客户门户,你可以在以下位置下载 RHEL 8.7 的 kernel 源代码:
- 登录到 Red Hat 客户门户:访问 Red Hat 官方网站并登录你的账号:access.redhat.com/
- 导航到 kernel 资源:登录后,点击顶部导航菜单中的 "Downloads",然后选择 "Product Software"。
- 选择 RHEL 版本:在 "Product Software" 页面中,选择 RHEL 8,并进入 RHEL 8 的下载页面。
- 下载 kernel 源代码:在 RHEL 8 下载页面中,你应该能够找到 "Kernel" 或 "Kernel Source" 部分。在该部分中,你可以下载 RHEL 8.7 的 kernel 源代码.
如果你没有 RHEL 订阅,但仍然希望获取 Linux 内核源代码,你可以考虑下载 CentOS 或其他开源发行版的内核源代码,因为 CentOS 是从 RHEL 源代码构建的,它们的内核源代码应该是相同的。
创建build 文件夹
[user@host]$ mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
[user@host]$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros
安装source package 以及需要的库文件,需要root 权限
[root@host]# yum install asciidoc audit-libs-devel bash bc binutils binutils-devel bison diffutils elfutils
[root@host]# yum install elfutils-devel elfutils-libelf-devel findutils flex gawk gcc gettext gzip hmaccalc hostname java-devel
[root@host]# yum install m4 make module-init-tools ncurses-devel net-tools newt-devel numactl-devel openssl
[root@host]# yum install patch pciutils-devel perl perl-ExtUtils-Embed pesign python-devel python-docutils redhat-rpm-config
[root@host]# yum install rpm-build sh-utils tar xmlto xz zlib-devel
下载并安装需要的src RPM
Find the kernel source rpm package in:
http://vault.centos.org/7.N.YYMM/os/Source/SPackages/
http://vault.centos.org/7.N.YYMM/updates/Source/SPackages/
(Replace the "N.YYMM" with the relevant sub-version, year and month numbers.)
As an ordinary user, not root, install the source package by executing:
[user@host]$ rpm -i http://vault.centos.org/7.9.2009/updates/Source/SPackages/kernel-3.10.0-1160.90.1.el7.src.rpm 2>&1 | grep -v 'exist'
Now that the source package and tools are installed, unpack and prepare the source files 当完成上述操作后,kernel source 以及所需要的工具都已经安装了,现在只需要解压缩并准备kernel source 即可。
[user@host]$ cd ~/rpmbuild/SPECS
[user@host SPECS]$ rpmbuild -bp --target=$(uname -m) kernel.spec
The value of $(uname -m) sets the target to the architecture of your current kernel. This is generally accepted, as most people will need either i686 or x86_64 as the target. The kernel source tree will now be found under the ~/rpmbuild/BUILD/kernel*/linux*/ directory.
最后我们只需要在 ~/rpmbuild/BUILD/kernel*/linux*/ 就可以找到所需要的Kernel source code。接下来只需要修改config进行重编安装。
如何编译RHEL/CentOS kernel
有两种方式:
- 1.本机编译kernel,本机直接安装该kernel,推荐 编译RHEL 8.7 kernel 并重新安装 (make)
- 2.本机编译Kernel并打包成rpm 包,可以替换其他机器的kernel。 编译RHEL 8.7 kernel 并重新安装 (RPM build)
`