What exactly does Linux kernel's `make defconfig` do?

Michael Burr picture Michael Burr · Jan 27, 2017 · Viewed 48k times · Source

I can use the following command to create a Linux kernel .config file based on a specified architecture default for a custom ARM-based board:

ARCH=arm make defconfig KBUILD_DEFCONFIG=var_som_mx6_android_defconfig

I thought that this command more or less copies ./arch/arm/configs/var_som_mx6_android_defconfig to ./.config. However the resulting .config file isn't exactly a copy:

$ 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

I don't understand where the extra lines are coming from, and I have always found the internal workings of the kernel configuration, makefiles, and build scripts to be difficult to understand. Can anyone explain where those lines in the .config might be coming from?

Answer

Sam Protsenko picture Sam Protsenko · Jan 27, 2017

Motivation

The .config file is not simply copied from your defconfig file. The motivation for storing defconfig in such a format is next: in defconfig we can specify only options with non-default values (i.e. options we changed for our board). This way we can keep it small and clear. Every new kernel version brings a bunch of new options, and this way we don't need to update our defconfig file each time the kernel releases. Also, it should be mentioned that kernel build system keeps very specific order of options in defconfig file, so it's better to avoid modifying it by hand. Instead you should use make savedefconfig rule.

Simplified explanation

When .config file is being generated, kernel build system goes through all Kconfig files (from all subdirs), checking all options in those Kconfig files:

  • if option is mentioned in defconfig, build system puts that option into .config with value chosen in defconfig
  • if option isn't mentioned in defconfig, build system puts that option into .config using its default value, specified in corresponding Kconfig

Check scripts/kconfig/Makefile and scripts/kconfig/conf.c files to see how it's actually done.

More precise and detailed explanation

From "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.

Useful commands

You can use simpler syntax for make defconfig, like:

$ make ARCH=arm your_board_defconfig

See the full list of available defconfigs with:

$ make ARCH=arm help | grep defconfig

If you need to do reverse action (i.e. create a neat small defconfig from extensive .config), you can use savedefconfig rule:

$ make ARCH=arm savedefconfig

Also, as 0andriy mentioned, you can use diffconfig script to see changes from one .config to another one:

$ scripts/diffconfig .config_old .config_new