目录
首次尝试编译Linux内核,记录过程,提供Linux Kernel(linux-4.19.178)下载https://download.csdn.net/download/qpeity/15637656。
一、安装依赖项
安装依赖项ncurses-devel、lfutils-libelf-devel、bc。
yum install ncurses-devel elfutils-libelf-devel bc
二、下载、解压缩、制作.config文件
去官网https://www.kernel.org/找合适的Linux Kernel版本,下载后解压缩。从 /boot 目录将现有版本的内核编译config配置文件拷过来到放到新的内核源码解压目录内并重命名为.config的隐藏文件。运行 make menuconfig,开启文本界面的编译选项菜单窗口,可以对内核加载的模块编译选项进行调整。具体选项根据实际需要调整。
wget https://mirrors.tuna.tsinghua.edu.cn/kernel/v4.x/linux-4.19.178.tar.xz
tar xvJf linux-4.19.178.tar.xz
cd linux-4.19.178/
cp /boot/config-3.10.0-1127.el7.x86_64 ./.config
make menuconfig
三、编译内核及打包
echo $(nproc)
或者
cat /proc/cpuinfo | grep 'model name'|wc -l 查看CPU硬件条件,最大限度利用硬件。然后make -j编译。
[root@toy SOURCES]# echo $(nproc)
2
[root@toy SOURCES]# cat /proc/cpuinfo | grep 'model name'|wc -l
2
make rpm-pkg 同时构建源和二进制RPM软件包
或者
make binrpm-pkg ##仅构建二进制RPM软件包
[root@toy linux-4.19.178]$ make binrpm-pkg
……
……
……
+ cp System.map /root/rpmbuild/BUILDROOT/kernel-4.19.178-2.x86_64/boot/System.map-4.19.178
+ cp .config /root/rpmbuild/BUILDROOT/kernel-4.19.178-2.x86_64/boot/config-4.19.178
+ bzip2 -9 --keep vmlinux
+ mv vmlinux.bz2 /root/rpmbuild/BUILDROOT/kernel-4.19.178-2.x86_64/boot/vmlinux-4.19.178.bz2
+ /usr/lib/rpm/brp-compress
Processing files: kernel-4.19.178-2.x86_64
Provides: kernel = 4.19.178-2 kernel(x86-64) = 4.19.178-2 kernel-4.19.178
Requires(interp): /bin/sh /bin/sh /bin/sh
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires(post): /bin/sh
Requires(preun): /bin/sh
Requires(postun): /bin/sh
Processing files: kernel-headers-4.19.178-2.x86_64
Provides: kernel-headers = 4.19.178 kernel-headers = 4.19.178-2 kernel-headers(x86-64) = 4.19.178-2
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Obsoletes: kernel-headers
Wrote: /root/rpmbuild/RPMS/x86_64/kernel-4.19.178-2.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/kernel-headers-4.19.178-2.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.qFpGsq
+ umask 022
+ cd .
+ rm -rf /root/rpmbuild/BUILDROOT/kernel-4.19.178-2.x86_64
+ exit 0
四、升级内核
rpm -ivh安装新内核,这样的好处是旧内核得以保留,有退回的余地。然后,用grub2-set-default工具设置默认的启动内核,再用grub2-editenv list查看一下,确认无误了再reboot。启动后,默认的内核就是升级后的内核了。
[root@toy x86_64]# rpm -ivh kernel-4.19.178-2.x86_64.rpm
[root@toy ~]# grub2-editenv list
saved_entry=CentOS Linux (3.10.0-1127.el7.x86_64) 7 (Core)
[root@toy ~]# cat /boot/grub2/grub.cfg | grep menuentry
if [ x"${feature_menuentry_id}" = xy ]; then
menuentry_id_option="--id"
menuentry_id_option=""
export menuentry_id_option
menuentry 'CentOS Linux (4.19.178) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1127.el7.x86_64-advanced-090a39b5-e2e5-4aff-8305-b92138c900de' {
menuentry 'CentOS Linux (3.10.0-1127.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1127.el7.x86_64-advanced-090a39b5-e2e5-4aff-8305-b92138c900de' {
menuentry 'CentOS Linux (0-rescue-853cf882e9e4451c87d05c2a97af6f78) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-853cf882e9e4451c87d05c2a97af6f78-advanced-090a39b5-e2e5-4aff-8305-b92138c900de' {
[root@toy ~]# grub2-set-default 'CentOS Linux (4.19.178) 7 (Core)'
[root@toy ~]# grub2-editenv list
saved_entry=CentOS Linux (4.19.178) 7 (Core)
[root@toy ~]# reboot
Connection closing...Socket close.
[root@toy ~]# uname -msr
Linux 4.19.178 x86_64