首页 > 技术文章 > 自定义配置编译linux内核

luohaixian 2018-07-15 16:12 原文

1 编译linux内核原因
一般情况下,我们是不需要重新去编译linux内核的,但如果你发现你需要修改内核的某个部分或者说你需要的某个模块并没有编译进内核,那里你可以通过重新编译内核来满足你的需求,比如当我们需要用bcache时,但默认bcache是没有编译进内核的,我们可以通过修改编译配置文件,将bcache编译进内核,以下的编译操作均是在Centos7.3平台上进行的演示。

2 编译前准备工作
2.1 编译用户身份选择
官方是强调编译linux内核是强烈不建议以root身份来进行编译的,因为这样有可能在编译过程中改掉当前编译系统的重要配置而影响当前系统,而应该建议使用普通用户的身份来编译内核,这样该普通用户如果在编译过程中要修改系统重要的配置文件也会因为没有权限而报错。

2.2 构建编译所需环境
(1)rpm编译目录创建

[user@host]$ mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
[user@host]$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

 

(2)编译所需的一些依赖包安装

[root@host]# yum install rpm-build redhat-rpm-config asciidoc hmaccalc perl-ExtUtils-Embed pesign xmlto -y
[root@host]# yum install audit-libs-devel binutils-devel elfutils-devel elfutils-libelf-devel java-devel -y
[root@host]# yum install ncurses-devel newt-devel numactl-devel pciutils-devel python-devel zlib-devel -y
[root@host]# yum install make gcc bc openssl-devel -y
[root@host]#
yum groupinstall "Development Tools" -y

 

3 获取源码并进行编译

这里分两种,一种是获取src.rpm源文件来重新编译成rpm,一种是获取源码文件来编译成rpm。
3.1 获取src.rpm源码文件来编译

3.1.1 获取并安装源码
获取内核RPM源码包

[user@host]$ wget http://vault.centos.org/7.3.1611/updates/Source/SPackages/kernel-3.10.0-514.26.2.el7.src.rpm


安装源码包:

[user@host]$ rpm -i kernel-3.10.0-514.26.2.el7.src.rpm 2>&1 | grep -v exist

 

解压并释放出源文件

[user@host]$ cd ~/rpmbuild/SPECS
[user@host SPECS]$ rpmbuild -bp --target=$(uname -m) kernel.spec

 

3.1.2 修改编译配置文件
(1)拷贝源码文件到rpm编译目录:

[user@host] $ cp ~/rpmbuild/BUILD/kernel-3.10.0-514.26.2.el7/linux-3.10.0-514.26.2.el7.x86_64/configs/* ~/rpmbuild/SOURCES/

 

(2)改变工作目录:

[user@host]$ cd ~/rpmbuild/BUILD/kernel-3.10.0-514.26.2.el7/linux-3.10.0-514.26.2.el7.x86_64/

 

(3)拷贝内核的默认配置文件到当前工作目录:

[user@host]$ cp configs/kernel-3.10.0-`uname -m`.config .config

 

(4)执行make oldconfig命令生成默认内核配置

[user@host]$ make oldconfig

 

(5)执行make menuconfig命令自定义编译模块

[user@host]$ make menuconfig


此时会弹出窗口,如下图所示:

左侧的[]里面有三种可能的值:
[*]表示将编译进内核;
[M]表示以内核模块的形式编译进行内核,这种形式可以用modprobe xxx命令进行加载;
[]表示不对该模块进行编译
比如我自己的需求是需要将bcache模块以内核模块的形式编译进行内核中,则我的选择是:
Device Drivers -> Multiple devices driver support -> Block device as cache
找到Block device as cache这个选项,将其[]值改为[M],如下图所示:

选择完后进行save保存,其会生成到.config配置文件中,接着退出。

(6)编辑.config文件并拷贝到编译配置文件目录中
如果你的平台是x86_64平台则,在.config文件的第一行加上
# x86_64
是其它的则加上其它的,可以使用uname -m命令查看你的平台:

[user@host ~]$ uname -m
x86_64


拷贝.config文件到编译配置文件目录中:

[user@host]$ cp .config configs/kernel-3.10.0-`uname -m`.config
[user@host]$ cp configs/* ~/rpmbuild/SOURCES/

 

3.1.3 使用rpmbuild命令进行编译

[user@host]$ cd ~/rpmbuild/SPECS
[user@host SPECS]$ rpmbuild -bb --target=`uname -m` kernel.spec 2> build-err.log | tee build-out.log


还可以在编译项里加上一些编译选项:
--with baseonly
--without up
--without debug
--without debuginfo
--without fips
--without kabichk
比如,编译基础的则一般加上这些选项:
--with baseonly --without debug --without debuginfo --without kabichk
最后可以使用如下命令编译内核:

[user@host]$ rpmbuild -bb --target=`uname -m` kernel.spec --with baseonly --without debug --without debuginfo --without kabichk 2> build-err.log | tee build-out.log

 

编译完成后便可以在 ~/rpmbuild/RPMS/`uname -m`/ 目录下看到编译生成的内核rpm包了

 

3.2 获取源码文件来编译

3.2.1 获取源码文件

去linux kernel官网获取源码,官网地址:https://www.kernel.org

我这里是下载4.14.95版本的内核源码来编译:

[user@host ~]# wget https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.14.95.tar.xz

 解压源码文件:

[user@host ~]# tar xvf linux-4.14.95.tar.xz

解压完成后进入linux-4.14.95目录:

[user@host ~]# cd linux-4.14.95

 

3.2.2 配置.config文件

(可选)将当前系统的配置作为模板拷贝到当前目录命名为.config:

[user@host linux-4.14.95]# cp -v /boot/config-3.10.0-514.el7.x86_64 .config

如果提示权限不够,则可使用root用户来拷贝到那个目录:

[root@host ~]# cp -u user -v /boot/config-3.10.0-514.el7.x86_64 /home/lhx/.config

执行make menuconfig来选择模块,选择方式如3.1.2所述

 

3.2.3 编译rpm

[user@host linux-4.14.95]$ make rpm

注意点:这里有个疑问,我用4.19.12版本执行make rpm的时候会报如下错:

[user@host linux-4.19.12]$ make rpm
make: *** 没有规则可以创建目标“rpm”。 停止。

有些博客说执行make rpm-pkg,但这个编译完后生成的rpm非常大,有500多M,这个疑问还需解决。

 

4 内核编译注意点
编译时间一般比较长,可能1小时到几小时不等,看你的机器性能,但有一点是要注意的,就是存储空间要够,我之前用10G都不够,最好预留20G的空间给内核编译,否则编译过程中会报错,报错时用df命令看下空间是否占满了来排除下是否是这个错误。

 

成后便

推荐阅读