如何编译Linux内核rpm包

嵌入式技术

1330人已加入

描述

一、下载源代码

1、登录github官网

进入github官网,搜索linux,使用git下载最新版本,或者其它版本的内核代码。

RPM

2. 命令行下载

使用git clone下载github上linux源代码,如下:

RPM

二、安装需要的软件包

安装一些编译内核的依赖。下面步骤 需要在root下进行

[root@localhost ~]# yum install -y ncurses-devel elfutils-libelf-devel openssl-devel bc

安装编译内核rpm包的依赖

[root@localhost ~]# yum install -y gcc rpm-build rpm-devel rpmlint make bash python3 coreutils diffutils patch rpmdevtools

三、配置内核代码

在编译内核之前,我们可以对内核源代码进行配置。配置的目的主要是确定哪些模块会编译到内核当中。

1、进入源代码目录

cd linux/

2、拷贝当前主机系统内核使用的config文件

从当前机器的启动目录拷贝config文件到内核源代码目录。目标是使编译内核的config采用与当前环境一致的配置。这个文件保存了在安装系统时内核所安装的模块配置信息,否则需要重新手动指定每一个模块的编译配置。

cp -v /boot/config-$(uname -r) .config

或者

make oldconfig

make oldconfig 命令的作用是检查已有的.config文件和Kconfig文件的规则是否一致,如果一致,就什么都不做,否则提示用户哪些源代码中有的选项在.config文件没有。一般情况下一直执行回车即可。

执行后,内核源码目录下会生成.config文件

[root@localhost linux]# ls -al | grep -w "config"
-rw-r--r--    1 root root 232033 May  6 13:39 .config

.config 中保存的就是内核编译的相关设置。它的内容大致是这样的:

# General setup
#
CONFIG_INIT_ENV_ARG_LIMIT=32
# CONFIG_COMPILE_TEST is not set
# CONFIG_WERROR is not set
CONFIG_LOCALVERSION=""
# CONFIG_LOCALVERSION_AUTO is not set
CONFIG_BUILD_SALT=""
CONFIG_HAVE_KERNEL_GZIP=y
CONFIG_HAVE_KERNEL_BZIP2=y
CONFIG_HAVE_KERNEL_LZMA=y
CONFIG_HAVE_KERNEL_XZ=y
CONFIG_HAVE_KERNEL_LZO=y
CONFIG_HAVE_KERNEL_LZ4=y
CONFIG_HAVE_KERNEL_ZSTD=y
CONFIG_KERNEL_GZIP=y

:以 # 开头的行就是注释行,这些 CONFIG_* 就是设置项,这些设置项与Linux内核的各功能相对应,它的值有三种分别表示编译时的三种操作:

  • CONFIG_*=y

表示该项目所对应的功能会被静态地添加到内核中

  • CONFIG_*=m

表示该项目对应的功能会被编译成模块,在内核需要时再加载入内核

  • CONFIG_* is not set

表示不编译该项目对应的功能

注意 :由于内核间功能的相互依赖十分复杂,因此不要尝试手工编辑 .config 文件,否则可能出现编译失败,或者编译后内核无法执行的情况!

3、启动配置界面(可选)

该命令会运行一些脚本,然后打开一个配置界面

RPM

4、打开配置界面

可以看出里面包含所有的内核组件,包括文件系统,网络,IO栈,虚拟化和设备驱动等等。

RPM

可以通过该图形界面对内核加载的模块编译选项进行调整,如修改编译后的内核名称、新添加之前系统缺少的模块等。

RPM

如果你不熟悉,可以不做任何修改。

5、确认config文件中功能是否配置成功

vi .config

打开.config文件,搜索相关变量名,即可查看。本文未进行相关修改,一切采用系统默认的config文件。

四、编译内核

1、编译内核rpm包

使用如下命令编译内核rpm包

make -j $(nproc --all) INSTALL_MOD_STRIP=1 rpm-pkg

编译完后会自动生成 rmp 安装包,编译时间比较长,建议使用配置较大的机器进行编译,磁盘空间要保持在20G以上。

INSTALL_MOD_STRIP 指定是否剔除模块的调试信息。通过传参或定义环境变量的方式定义为 1,使其生效。这个变量如果被指定,模块就会将一些额外的、运行时非必要的信息剥离出来以缩减模块的大小,当INSTALL_MOD_STRIP为1时,--strip-debug选项就会被使用,模块的调试信息将被删除,否则就执行默认的参数,模块编译时会添加一些辅助信息。

编译后会在如下路径生成rpm包:

......
Wrote: /root/rpmbuild/RPMS/x86_64/kernel-6.3.0+-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/kernel-headers-6.3.0+-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/kernel-devel-6.3.0+-1.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.k1BYNw
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd linux
+ rm -rf /root/rpmbuild/BUILDROOT/kernel-6.3.0+-1.x86_64
+ exit 0
Executing(--clean): /bin/sh -e /var/tmp/rpm-tmp.EjpV5x
+ umask 022
+ cd /root/rpmbuild/BUILD
+ rm -rf linux
+ exit 0

2、安装新编译好的内核rpm包

#yum install编译好的内核
[root@localhost ~]# yum install /root/rpmbuild/RPMS/x86_64/kernel-6.3.0+-1.x86_64.rpm
Last metadata expiration check: 4:08:53 ago on Fri 05 May 2023 01:13:32 PM CST.
Dependencies resolved.
===================================================================================================================================================================================================
 Package                                     Architecture                                Version                                          Repository                                          Size
===================================================================================================================================================================================================
Installing:
 kernel                                      x86_64                                      6.3.0+-1                                         @commandline                                        61 M
Removing:
 kernel                                      x86_64                                      6.2.0+-1                                         @@commandline                                      299 M

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

Total size: 61 M
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                           1/1
  Installing       : kernel-6.3.0+-1.x86_64                                                                                                                                                    1/2
  Running scriptlet: kernel-6.3.0+-1.x86_64                                                                                                                                                    1/2
  Running scriptlet: kernel-6.2.0+-1.x86_64                                                                                                                                                    2/2
  Erasing          : kernel-6.2.0+-1.x86_64                                                                                                                                                    2/2
  Running scriptlet: kernel-6.2.0+-1.x86_64                                                                                                                                                    2/2
  Verifying        : kernel-6.3.0+-1.x86_64                                                                                                                                                    1/2
  Verifying        : kernel-6.2.0+-1.x86_64                                                                                                                                                    2/2
Installed products updated.

Installed:
  kernel-6.3.0+-1.x86_64
Removed:
  kernel-6.2.0+-1.x86_64

Complete!
  kernel-5.19.0_rc4+-1.x86_64


Complete!

[root@localhost ~]# yum install /root/rpmbuild/RPMS/x86_64/kernel-devel-6.3.0+-1.x86_64.rpm
Last metadata expiration check: 4:10:05 ago on Fri 05 May 2023 01:13:32 PM CST.
Dependencies resolved.
===================================================================================================================================================================================================
 Package                                          Architecture                               Version                                        Repository                                        Size
===================================================================================================================================================================================================
Upgrading:
 kernel-devel                                     x86_64                                     6.3.0+-1                                       @commandline                                     182 M

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

Total size: 182 M
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                           1/1
  Upgrading        : kernel-devel-6.3.0+-1.x86_64                                                                                                                                              1/2
  Cleanup          : kernel-devel-6.2.0+-1.x86_64                                                                                                                                              2/2
  Running scriptlet: kernel-devel-6.2.0+-1.x86_64                                                                                                                                              2/2
  Verifying        : kernel-devel-6.3.0+-1.x86_64                                                                                                                                              1/2
  Verifying        : kernel-devel-6.2.0+-1.x86_64                                                                                                                                              2/2
Installed products updated.

Upgraded:
  kernel-devel-6.3.0+-1.x86_64

Complete!

五、设置默认启动的内核

首先,查看系统安装的全部内核:

grubby --info=ALL

执行结果如下:

[root@localhost ~]# grubby --info=ALL
index=0
kernel="/boot/vmlinuz-6.3.0_20230506"
args="ro crashkernel=512M rhgb quiet intermap=on iommu=pt console=ttyS0,115200 console=ttyS1,115200 intel_iommu=on pci=realloc default_hugepagesz=1G hugepagesz=1G hugepages=16 hugepagesz=2M hugepages=1375 isolcpus=1,3,5,7,9,11,13,17 nohz_full=1,3,5,7,9,11,13,17 rcu_nocbs=1,3,5,7,9,11,13,17 $tuned_params"
root="/dev/mapper/bel-root"
initrd="/boot/initramfs-6.3.0_20230506.img $tuned_initrd"
title="BigCloud Enterprise Linux (6.3.0_20230506) 8.2 (Core)"
id="613790481f5a48e0b1608a83a3c399f7-6.3.0_20230506"
index=1
kernel="/boot/vmlinuz-6.3.0-rc7+"
args="ro crashkernel=512M rhgb quiet intermap=on iommu=pt console=ttyS0,115200 console=ttyS1,115200 intel_iommu=on pci=realloc default_hugepagesz=1G hugepagesz=1G hugepages=16 hugepagesz=2M hugepages=1375 isolcpus=1,3,5,7,9,11,13,17 nohz_full=1,3,5,7,9,11,13,17 rcu_nocbs=1,3,5,7,9,11,13,17 $tuned_params"
root="/dev/mapper/bel-root"
initrd="/boot/initramfs-6.3.0-rc7+.img $tuned_initrd"
title="BigCloud Enterprise Linux (6.3.0-rc7+) 8.2 (Core)"
id="613790481f5a48e0b1608a83a3c399f7-6.3.0-rc7+"
index=2
kernel="/boot/vmlinuz-6.3.0+"
args="ro crashkernel=512M rhgb quiet intermap=on iommu=pt console=ttyS0,115200 console=ttyS1,115200 intel_iommu=on pci=realloc default_hugepagesz=1G hugepagesz=1G hugepages=16 hugepagesz=2M hugepages=1375 isolcpus=1,3,5,7,9,11,13,17 nohz_full=1,3,5,7,9,11,13,17 rcu_nocbs=1,3,5,7,9,11,13,17 $tuned_params"
root="/dev/mapper/bel-root"
initrd="/boot/initramfs-6.3.0+.img $tuned_initrd"
title="BigCloud Enterprise Linux (6.3.0+) 8.2 (Core)"
id="613790481f5a48e0b1608a83a3c399f7-6.3.0+"
index=3
kernel="/boot/vmlinuz-6.2.0-rc5+"
args="ro crashkernel=512M rhgb quiet intermap=on iommu=pt console=ttyS0,115200 console=ttyS1,115200 intel_iommu=on pci=realloc default_hugepagesz=1G hugepagesz=1G hugepages=16 hugepagesz=2M hugepages=1375 isolcpus=1,3,5,7,9,11,13,17 nohz_full=1,3,5,7,9,11,13,17 rcu_nocbs=1,3,5,7,9,11,13,17 $tuned_params"
root="/dev/mapper/bel-root"
initrd="/boot/initramfs-6.2.0-rc5+.img $tuned_initrd"
title="BigCloud Enterprise Linux (6.2.0-rc5+) 8.2 (Core)"
id="613790481f5a48e0b1608a83a3c399f7-6.2.0-rc5+"
index=4
kernel="/boot/vmlinuz-5.4.240-1.el8.elrepo.x86_64"
args="ro crashkernel=512M rhgb quiet intermap=on iommu=pt console=ttyS0,115200 console=ttyS1,115200 intel_iommu=on pci=realloc default_hugepagesz=1G hugepagesz=1G hugepages=16 hugepagesz=2M hugepages=1375 isolcpus=1,3,5,7,9,11,13,17 nohz_full=1,3,5,7,9,11,13,17 rcu_nocbs=1,3,5,7,9,11,13,17 $tuned_params"
root="/dev/mapper/bel-root"
initrd="/boot/initramfs-5.4.240-1.el8.elrepo.x86_64.img $tuned_initrd"
title="Enterprise Linux (5.4.240-1.el8.elrepo.x86_64) 8.7"
id="613790481f5a48e0b1608a83a3c399f7-5.4.240-1.el8.elrepo.x86_64"
index=5
kernel="/boot/vmlinuz-5.4.67-300.el8.bclinux.x86_64"
args="ro crashkernel=512M rhgb quiet intermap=on iommu=pt console=ttyS0,115200 console=ttyS1,115200 intel_iommu=on pci=realloc default_hugepagesz=1G hugepagesz=1G hugepages=16 hugepagesz=2M hugepages=1375 isolcpus=1,3,5,7,9,11,13,17 nohz_full=1,3,5,7,9,11,13,17 rcu_nocbs=1,3,5,7,9,11,13,17 $tuned_params"
root="/dev/mapper/bel-root"
initrd="/boot/initramfs-5.4.67-300.el8.bclinux.x86_64.img $tuned_initrd"
title="BigCloud Enterprise Linux (5.4.67-300.el8.bclinux.x86_64) 8.1 (Core)"
id="613790481f5a48e0b1608a83a3c399f7-5.4.67-300.el8.bclinux.x86_64"
index=6
kernel="/boot/vmlinuz-4.19.0-240.23.18.el8_2.bclinux.x86_64"
args="ro crashkernel=512M rhgb quiet intermap=on iommu=pt console=ttyS0,115200 console=ttyS1,115200 intel_iommu=on pci=realloc default_hugepagesz=1G hugepagesz=1G hugepages=16 hugepagesz=2M hugepages=1375 isolcpus=1,3,5,7,9,11,13,17 nohz_full=1,3,5,7,9,11,13,17 rcu_nocbs=1,3,5,7,9,11,13,17 $tuned_params"
root="/dev/mapper/bel-root"
initrd="/boot/initramfs-4.19.0-240.23.18.el8_2.bclinux.x86_64.img $tuned_initrd"
title="BigCloud Enterprise Linux (4.19.0-240.23.18.el8_2.bclinux.x86_64) 8.2 (Core)"
id="613790481f5a48e0b1608a83a3c399f7-4.19.0-240.23.18.el8_2.bclinux.x86_64"
index=7
kernel="/boot/vmlinuz-4.19.0-240.23.11.el8_2.bclinux.x86_64"
args="ro crashkernel=512M rhgb quiet intermap=on iommu=pt console=ttyS0,115200 console=ttyS1,115200 intel_iommu=on pci=realloc default_hugepagesz=1G hugepagesz=1G hugepages=16 hugepagesz=2M hugepages=1375 isolcpus=1,3,5,7,9,11,13,17 nohz_full=1,3,5,7,9,11,13,17 rcu_nocbs=1,3,5,7,9,11,13,17 $tuned_params"
root="/dev/mapper/bel-root"
initrd="/boot/initramfs-4.19.0-240.23.11.el8_2.bclinux.x86_64.img $tuned_initrd"
title="BigCloud Enterprise Linux (4.19.0-240.23.11.el8_2.bclinux.x86_64) 8.2 (Core)"
id="613790481f5a48e0b1608a83a3c399f7-4.19.0-240.23.11.el8_2.bclinux.x86_64"
index=8
kernel="/boot/vmlinuz-0-rescue-613790481f5a48e0b1608a83a3c399f7"
args="ro crashkernel=512M rhgb quiet intermap=on iommu=pt console=ttyS0,115200 console=ttyS1,115200 intel_iommu=on pci=realloc default_hugepagesz=1G hugepagesz=1G hugepages=16 hugepagesz=2M hugepages=1375 isolcpus=1,3,5,7,9,11,13,17 nohz_full=1,3,5,7,9,11,13,17 rcu_nocbs=1,3,5,7,9,11,13,17"
root="/dev/mapper/bel-root"
initrd="/boot/initramfs-0-rescue-613790481f5a48e0b1608a83a3c399f7.img"
title="BigCloud Enterprise Linux (0-rescue-613790481f5a48e0b1608a83a3c399f7) 8.1 (Core)"
id="613790481f5a48e0b1608a83a3c399f7-0-rescue"

设置新的默认启动内核,方法如下:

#方法一:使用路径来指定内核,可以使用--set-default=kernel-path
[root@localhost ~]# grubby --set-default=/boot/vmlinuz-6.3.0+
...
[root@localhost ~]# grubby --default-kernel
/boot/vmlinuz-6.3.0+

#方法二:使用index来指定内核,则使用--set-default-index=entry-index
[root@localhost ~]# grubby --set-default-index=2
...
[root@localhost ~]# grubby --default-kernel
/boot/vmlinuz-6.3.0+

重新启动后生效。

RPM

grubby是一个用于更新和显示有关各种体系结构特定的引导程序的配置文件信息的命令行工具。它主要设计用于安装新内核并需要查找有关当前引导环境的信息的脚本,同时也可以对启动内核的各项信息参数进行修改。在centos8中使用grubby工具来对系统的内核启动参数和启动顺序进行调整。使用yum或者dnf可以直接安装grubby工具。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分