源码编译构建安装内核kernel

Posted rtoax

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了源码编译构建安装内核kernel相关的知识,希望对你有一定的参考价值。

源码编译构建安装内核kernel

荣涛
2021年10月27日

文档修改日志

日期修改内容修改人备注
2021年10月27日创建荣涛
2021年10月28日添加可能的问题荣涛

1. 引言

1.1. 编译前准备

$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.6.9.tar.xz
$ unxz -v linux-5.6.9.tar.xz # 或者
$ xz -d -v linux-5.6.9.tar.xz 
$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.6.9.tar.sign
$ gpg --verify linux-5.6.9.tar.sign
#    上面命令可能的输出为:
#    gpg: assuming signed data in 'linux-5.6.9.tar'
#    gpg: Signature made Sun 12 Aug 2018 04:00:28 PM CDT
#    gpg:                using RSA key 79BE3E4300411886
#    gpg: Can't check signature: No public key
$ gpg --recv-keys 79BE3E4300411886
#    上面命令可能的输出为:
#    gpg: key 79BE3E4300411886: 7 duplicate signatures removed
#    gpg: key 79BE3E4300411886: 172 signatures not checked due to missing keys
#    gpg: /home/vivek/.gnupg/trustdb.gpg: trustdb created
#    gpg: key 79BE3E4300411886: public key "Linus Torvalds <torvalds@kernel.org>" imported
#    gpg: no ultimately trusted keys found
#    gpg: Total number processed: 1
#    gpg:               imported: 1
$ gpg --verify linux-5.6.9.tar.sign
#    上面命令可能的输出为:
#    gpg: assuming signed data in 'linux-5.6.9.tar'
#    gpg: Signature made Sun 12 Aug 2018 04:00:28 PM CDT
#    gpg:                using RSA key 79BE3E4300411886
#    gpg: Good signature from "Linus Torvalds <torvalds@kernel.org>" [unknown]
#    gpg:                 aka "Linus Torvalds <torvalds@linux-foundation.org>" [unknown]
#    gpg: WARNING: This key is not certified with a trusted signature!
#    gpg:          There is no indication that the signature belongs to the owner.
#    Primary key fingerprint: ABAF 11C6 5A29 70B1 30AB  E3C4 79BE 3E43 0041 1886
$ tar xvf linux-5.6.9.tar
$ cd linux-5.6.9
$ cp -v /boot/config-$(uname -r) .config
#    上步可能的输出
#    '/boot/config-4.15.0-30-generic' -> '.config'

1.2. 依赖安装

1.2.1. Debian/Ubuntu Linux

$ sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev

1.2.2. CentOS/RHEL/Oracle/Scientific Linux

$ sudo yum group install "Development Tools" # or
$ sudo yum groupinstall "Development Tools"
$ sudo yum install ncurses-devel bison flex elfutils-libelf-devel openssl-devel

1.2.3. Fedora Linux

$ sudo dnf group install "Development Tools"
$ sudo dnf install ncurses-devel bison flex elfutils-libelf-devel openssl-devel

1.3. 配置内核

几个选项:

  • $ make menuconfig
  • $ make xconfig
  • $ make gconfig
$ make menuconfig

1.4. 编译内核

## use 4 core/thread ##
$ make -j 4
## get thread or cpu core count using nproc command ##
$ make -j $(nproc)

1.5. 安装内核

# Install the Linux kernel modules
$ sudo make modules_install
# Install the Linux kernel
$ sudo make install
#    It will install three files into /boot directory as well 
#    as modification to your kernel grub configuration file:
#    initramfs-5.6.9.img
#    System.map-5.6.9
#    vmlinuz-5.6.9

1.6. 更新grub

1.6.1. CentOS/RHEL/Oracle/Scientific 和 Fedora Linux

$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
$ sudo grubby --set-default /boot/vmlinuz-5.6.9
# You can confirm the details with the following commands:
$ sudo grubby --info=ALL | more
$ sudo grubby --default-index
$ sudo grubby --default-kernel

1.6.2. Debian/Ubuntu Linux

$ sudo update-initramfs -c -k 5.6.9
$ sudo update-grub

1.7. 重启

$ sudo reboot
# 这里将进行系统重启
$ uname -mrs
# 可能的输出
# Linux 5.6.9 x86_64

2. 安装tools

2.1. perf

我的环境缺少了依赖:

$ sudo yum install slang-devel
$ sudo yum install binutils-devel zlib-static
$ sudo yum install libbabeltrace-devel
$ sudo yum install java-1.8.0-openjdk-devel
# 下面的 platform-python-devel 安装后并不起作用
$ sudo yum install platform-python-devel

编译perf:

$ make 

安装perf:

# 注意,这里需要指定目标目录,否则可能被安装到 /root/bin/perf
$ sudo make install DESTDIR=/usr

2.2. bpftools

直接安装即可。

2.3. kvm_stat

直接安装即可。

3. 可能遇到的问题

3.1. BTF: .tmp_vmlinux.btf: pahole (pahole) is not available

在编译内核过程中,可能出现问题现象:

BTF: .tmp_vmlinux.btf: pahole (pahole) is not available
Failed to generate BTF for vmlinux
Try to disable CONFIG_DEBUG_INFO_BTF
make: *** [Makefile:1106: vmlinux] Error 1

参考链接:BTF: .tmp_vmlinux.btf: pahole (pahole) is not available

解决办法:

尝试安装(根据不同系统类型):

$ sudo apt install dwarves
$ sudo dnf install dwarves
$ sudo zypper in dwarves

如果搜不到,就代开powertools的yum源:

vim /etc/yum.repo.d/CentOS-Linux-PowerTools.repo

enabled=1

4. 参考链接


Copyright (C) CESTC Com.

以上是关于源码编译构建安装内核kernel的主要内容,如果未能解决你的问题,请参考以下文章

构建调试Linux内核网络代码的环境MenuOS系统

编译安装kernel 5.1.10及删除内核

构建调试Linux内核网络代码的环境MenuOS系统

编译安装linux内核步骤

CentOS7手动编译安装内核4.11.7

构建调试Linux内核网络代码的环境MenuOS系统