Skip to main content
 首页 » 编程设计

linux-kernel之Linux 内核的 `make defconfig` 究竟是做什么的

2024年12月31日24bluestorm

我可以使用以下命令创建 Linux 内核 .config文件基于自定义基于 ARM 的板的指定体系结构默认值:

ARCH=arm make defconfig KBUILD_DEFCONFIG=var_som_mx6_android_defconfig 

我以为这个命令或多或少会复制 ./arch/arm/configs/var_som_mx6_android_defconfig./.config .然而由此产生的 .config文件不完全是副本:
$ diff --unified arch/arm/configs/var_som_mx6_android_defconfig  .config 
--- arch/arm/configs/var_som_mx6_android_defconfig  2017-01-20 12:10:51.891515984 -0800 
+++ .config 2017-01-26 15:31:29.000000000 -0800 
@@ -407,6 +407,7 @@ 
 CONFIG_ARM_ERRATA_751472=y 
 CONFIG_ARM_ERRATA_794072=y 
 CONFIG_ARM_ERRATA_761320=y 
+CONFIG_ARM_ERRATA_845369=y 
 # CONFIG_ARM_ERRATA_753970 is not set 
 CONFIG_ARM_ERRATA_754322=y 
 # CONFIG_ARM_ERRATA_754327 is not set 
@@ -2683,7 +2684,6 @@ 
 CONFIG_AUTOFS4_FS=y 
 CONFIG_FUSE_FS=y 
 # CONFIG_CUSE is not set 
-CONFIG_AUFS_FS=y 
 
 # 
 # Caches 
@@ -2759,6 +2759,21 @@ 
 # CONFIG_PSTORE is not set 
 # CONFIG_SYSV_FS is not set 
 # CONFIG_UFS_FS is not set 
+CONFIG_AUFS_FS=y 
+CONFIG_AUFS_BRANCH_MAX_127=y 
+# CONFIG_AUFS_BRANCH_MAX_511 is not set 
+# CONFIG_AUFS_BRANCH_MAX_1023 is not set 
+# CONFIG_AUFS_BRANCH_MAX_32767 is not set 
+CONFIG_AUFS_SBILIST=y 
+# CONFIG_AUFS_HNOTIFY is not set 
+# CONFIG_AUFS_RDU is not set 
+# CONFIG_AUFS_PROC_MAP is not set 
+# CONFIG_AUFS_SP_IATTR is not set 
+# CONFIG_AUFS_SHWH is not set 
+# CONFIG_AUFS_BR_RAMFS is not set 
+# CONFIG_AUFS_BR_FUSE is not set 
+CONFIG_AUFS_BDEV_LOOP=y 
+# CONFIG_AUFS_DEBUG is not set 
 CONFIG_NETWORK_FILESYSTEMS=y 
 CONFIG_NFS_FS=y 
 CONFIG_NFS_V3=y 

我不明白多余的行是从哪里来的,而且我总是发现内核配置、makefile 和构建脚本的内部工作很难理解。谁能解释这些行在 .config 中的位置可能来自?

请您参考如下方法:

动机.config文件不是简单地从您的 defconfig 复制的文件。存储的动机 defconfig下一个格式是这样的:在 defconfig我们只能指定具有非默认值的选项(即我们为董事会更改的选项)。这样我们就可以保持它小而清晰。每一个新的内核版本都会带来一堆新的选项,这样我们就不需要更新我们的 defconfig每次内核发布时文件。另外,应该提到的是,内核构建系统在 defconfig 中保留了非常具体的选项顺序。文件,因此最好避免手动修改它。相反,您应该使用 make savedefconfig规则。
简化说明
.config正在生成文件,内核构建系统会遍历所有 Kconfig文件(来自所有子目录),检查这些 Kconfig 中的所有选项文件:

  • 如果选项在 defconfig 中提到, 构建系统将该选项放入 .configdefconfig 中选择的值
  • 如果选项未在 defconfig 中提及, 构建系统将该选项放入 .config使用其默认值,在相应的 Kconfig 中指定

  • 查询 scripts/kconfig/Makefilescripts/kconfig/conf.c文件以查看它实际上是如何完成的。
    更准确详细的解释
    来自 "Kbuild: the Linux Kernel Build System" by Javier Martinez :

    Defining Configuration Symbols: Kconfig Files

    Configuration symbols are defined in files known as Kconfig files. Each Kconfig file can describe an arbitrary number of symbols and can also include (source) other Kconfig files. Compilation targets that construct configuration menus of kernel compile options, such as make menuconfig, read these files to build the tree-like structure. Every directory in the kernel has one Kconfig that includes the Kconfig files of its subdirectories. On top of the kernel source code directory, there is a Kconfig file that is the root of the options tree. The menuconfig (scripts/kconfig/mconf), gconfig (scripts/kconfig/gconf) and other compile targets invoke programs that start at this root Kconfig and recursively read the Kconfig files located in each subdirectory to build their menus. Which subdirectory to visit also is defined in each Kconfig file and also depends on the config symbol values chosen by the user.

    Storing Symbol Values: .config File

    All config symbol values are saved in a special file called .config. Every time you want to change a kernel compile configuration, you execute a make target, such as menuconfig or xconfig. These read the Kconfig files to create the menus and update the config symbols' values using the values defined in the .config file. Additionally, these tools update the .config file with the new options you chose and also can generate one if it didn't exist before.

    Because the .config file is plain text, you also can change it without needing any specialized tool. It is very convenient for saving and restoring previous kernel compilation configurations as well.


    有用的命令
    您可以对 make defconfig 使用更简单的语法。 , 喜欢:
    $ make ARCH=arm your_board_defconfig 
    
    查看可用 defconfigs 的完整列表:
    $ make ARCH=arm help | grep defconfig 
    
    如果您需要执行反向操作(即从广泛的 defconfig 创建一个整洁的小 .config ),您可以使用 savedefconfig规则:
    $ make ARCH=arm savedefconfig 
    
    另外,如 0andriy 提到,您可以使用 diffconfig脚本以查看来自一个的更改 .config到另一个:
    $ scripts/diffconfig .config_old .config_new