自定义配置编译linux内核

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 
[root@host]# yum install audit-libs-devel binutils-devel elfutils-devel elfutils-libelf-devel java-devel
[root@host]# yum install ncurses-devel newt-devel numactl-devel pciutils-devel python-devel zlib-devel
[root@host]# yum groupinstall "Development Tools"

3 获取源码并进行编译
3.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.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 oldconfig


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

左侧的[]里面有三种可能的值:
[*]表示将编译进内核;
[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.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
最后可以使用如下命令编译内核:

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

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

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

猜你喜欢

转载自www.cnblogs.com/luohaixian/p/9313863.html