目录
一、简介
本文主要用实例详细讲述了如何用交叉编译工具编译内核的操作。总体概括内核编译主要有下面几个过程:
1、本地环境搭建过程:包括gcc工具链和本地编译依赖库配置;
2、下载内核源码;
3、配置.config 文件;
4、交叉编译内核;
5、打包编译好内核文件。
二、环境搭建
2.1 交叉编译工具环境的设置
[root@localhost /] export PATH=/opt/mips-loongson-gcc7.3-linux-gnu/2019.06-29/bin/:$PATH
[root@localhost /] export LD_LIBRARY_PATH=/opt/mips-loongson-gcc7.3-linux-gnu/2019.06-29/lib:/opt/mips-loongson-gcc7.3-linux-gnu/2019.06-29/mips-linux-gnu/lib/:$LD_LIBRARY_PATH
注意:
1、如果在终端中执行这两条命令,交叉编译环境则只对该终端生效;
2、想要自动运行该命令可以降这两条命令加入到~/.bashrc中,然后执行source ~/.bashrc生效,则每次打开终端会自动执行这两条命令;
2.2 配置本地编译环境
下载本地编译依赖包
[root@localhost /] apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
这里列举了常用的一些依赖包,也可以直接先不安装相应的依赖包,根据报错再进行安装相应包,具体报错如下:
```c
>> Unable to find the Ncurses libraries.
>> You must install ncurses-devel in order
>> to use 'make menuconfig'
报错中明确表示缺少ncurses-devel包,安装继续编译即可。
三、内核编译实例
3.1 下载对应版本内核源码包
[root@localhost /]# wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.10.40.tar.xz
3.2 解压源码包到对应路径下,注意具体路径,后面写Makefile要用到
[root@localhost /]# tar -xvf linux-3.10.40.tar.xz /
3.3 在内核根目录清理文件
这一步的主要的作用就是防止下载的内核有编译过内核的残留文件,影响本次内核编译
[root@localhost linux-3.10.40]# make clean //只清理编译所有产生的文件
[root@localhost linux-3.10.40]# make mrproper //清理编译所有产生的文件与config配置文件
[root@localhost linux-3.10.40]# make distclean //清理编译所有产生的文件与config配置文件,并且编辑过的补丁文件
3.4 配置 .config 文件和生成defconfig
1、config就是设置内核编译参数,如需要编译的模块、平台架构相关的设置、内存管理等。
主目录如下:
[*] 64-bit kernel
General setup --->
[ ] Provide system-wide ring of trusted keys
[ ] Provide system-wide ring of blacklisted keys
[*] Enable loadable module support --->
[*] Enable the block layer --->
Processor type and features --->
Power management and ACPI options --->
Bus options (PCI etc.) --->
Executable file formats / Emulations --->
< > Volume Management Device Driver
-*- Networking support --->
Device Drivers --->
Firmware Drivers --->
File systems --->
Kernel hacking --->
Security options --->
-*- Cryptographic API --->
[*] Virtualization --->
Library routines --->
[ ] Enables more stringent kabi checks in the macros
2、配置基础config文件
//1、直接复制需要编译架构下的config文件到内核根目录下
[root@localhost linux-3.10.40]# cp /arm_kernel/linux-3.10.40/arch/mips64/configs/defconfig .config
//2、可以通过make ARCH=x86 i386_defconfig得到.config文件,如arch/x86/configs目录,这里有一个i386_defconfig,此时就会根据带的参数用arch/x86/configs/i386_defconfig在内核根目录下生成.config
[root@localhost linux-3.10.40]# make ARCH=x86 i386_defconfig
//3、保存内核通用配置defconfig,我们通过make ARCH=x86 i386_defconfig生成的.config跟我们初始的i386_defconfig是增加了一些当前环境的Kconfig配置的,要重新生成defconfig文件可以通过下面的命令
[root@localhost linux-3.10.40]# make ARCH=x86 savedefconfig //在内核根目录下生成defconfig文件
3、修改.config文件
[root@localhost linux-3.10.40] make config //基于文本模式的交互配置;
[root@localhost linux-3.10.40] make menuconfig //基于文本模式的菜单模式(推荐使用),内核三态:<*>将其编译进内核中, <M> 将其编译成模块,<>不编译该模块;
[root@localhost linux-3.10.40] make oldconfig //使用已有的.config,但会询问新增的配置项,如果用户输入y,则把该特性编入内核,如果是m,则把该特性编译成模块,如果是n,则不编译该特性;
//保存默认修改后的config文件
make savedefconfig && mv defconfig arch/arm/configs/xxx_defconfig
3.5 不同方式交叉编译内核
1、直接编译内核
[root@localhost linux-3.10.40] make ARCH=arm CROSS_COMPILE=arm-linux-
//ARCH代表目标平台的架构,CROSS_COMPILE代表交叉编译工具链
2、将配置写进Makefile中再编译内核
ARCH ?= arm
CROSS_COMPILE ?= arm-linux-
[root@localhost linux-3.10.40] make
3、安装内核模块
[root@localhost linux-3.10.40] make ARCH=mips CROSS_COMPILE=mips64el-linux- INSTALL_MOD_PATH=/test_install modules //安装已编译好的内核模块到指定目录
[root@localhost linux-3.10.40] make modules_install //直接安装到当前系统的/lib/modules路径下
4、打包内核文件
[root@localhost linux-3.10.40] cp vmlinuz System.map /test_install/boot/ //复制内核和映射文件
[root@localhost linux-3.10.40] cd /test_install/
[root@localhost test_install]tar -cvf kernel_3.10.40_test.tar * //打包已编译完成的相关内核文件
5、将编译的内核文件制作成deb二进制包
[root@localhost linux-3.10.40] dpkg-buildpackage -us -uc -b -d
6、其他编译命令
1、后台多线程编译内核
[root@localhost linux-3.10.40] nohup make -j4&
//nohup为后台执行编译过程,make -jn(n为线程数量,如make -j4就是使用4个线程编译)
[root@localhost linux-3.10.40] tail -f nohup.out
//查看后台编译内核进度
2、只编译指定目录下模块
[root@localhost linux-3.10.40] make ARCH=mips CROSS_COMPILE=mips64el-linux- modules M=`pwd`/src //只编译/linux-3.10.40/src下的文件
3、指定内核源码路径进行编译内核
[root@localhost /]# make -C /linux-3.10.40 ARCH=mips CROSS_COMPILE=mips64el-linux-
//这种方式编译的内核可以防止编译的内核再目标机器上出现版本不匹配安装不上的情况
其他相关链接:
gcc交叉编译工具搭建学习链接