开始学习
< 返回

Linux 内核编译

内核配置

在编译 Linux 内核之前,首先需要配置内核。Linux 提供了多种配置方式,包括 make config、make menuconfig、make xconfig 和 make gconfig 四种方式。推荐使用 make menuconfig,因为它不依赖于 Qt 或 GTK+,而且界面也比较直观。

下图是 ARM64 Linux 5.4 内核的 menuconfig 配置界面。

内核配置包含的条目相当多,为了方便配置,在 arch/*/configs 目录下提供了众多电路板的默认配置。

ls arch/*/configs/*_defconfig

例如,对于 arch/arm/configs/xxx_defconfig 默认配置,执行如下命令即可使用该模板来配置内核。

make ARCH=arm xxx_defconfig

接着可以通过 menuconfig 进一步定制内核。

make menuconfig

配置完成后,会在源代码根目录下生成一个 .config 配置文件,记录哪些部分需要编译进内核,哪些部分编译为内核模块。

内核编译

当配置完成后,就可以执行如下命令编译内核及模块。

make ARCH=arm zImage
make ARCH=arm modules

提示:上述命令中的 ARCH=arm 用于指定硬件平台架构,如果已经作为环境变量导出到当前终端,则可以省略。

当编译完成后,在源代码根目录下会得到未压缩的内核映像 vmlinux 和内核符号表文件 System.map,在 arch/arm/boot/ 目录下会得到压缩的内核映像 zImage,在内核各对应目录内得到选中的内核模块。

另外,对于 3.x 以上版本的内核,还需要编译设备树(device tree),命令如下:

make ARCH=arm dtbs

将在 arch/arm/boot/dts/ 目录下得到相应的 dtb 文件。

Was this article helpful?
0 out of 5 stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
Please Share Your Feedback
How Can We Improve This Article?
文章目录