Linux内核配置
0 前言
核心构建过程具体说明可见《Linux内核编译》这篇文章中。本文着重阐述了其中的内核配置部分。
1 下载源码
获取linux-2.6.34.7.tar.bz2并进行后续解压步骤:
tar -axvf linux-2.6.34.7.tar.bz2
2 默认配置
进入内核源码顶级目录执行:
make defconfig
输出如下:
HOSTCC scripts/basic/fixdep
HOSTCC scripts/basic/docproc
HOSTCC scripts/basic/hash
HOSTCC scripts/kconfig/conf.o
scripts/kconfig/conf.c: In function ‘conf_sym’:
scripts/kconfig/conf.c:159:6: warning: variable ‘type’ set but not used [-Wunused-but-set-variable]
int type;
^
scripts/kconfig/conf.c: In function ‘conf_choice’:
scripts/kconfig/conf.c:231:6: warning: variable ‘type’ set but not used [-Wunused-but-set-variable]
int type;
^
scripts/kconfig/conf.c:307:9: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]
fgets(line, 128, stdin);
^
scripts/kconfig/conf.c: In function ‘conf_askvalue’:
scripts/kconfig/conf.c:105:8: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]
fgets(line, 128, stdin);
^
HOSTCC scripts/kconfig/kxgettext.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/lex.zconf.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
*** Default configuration is based on 'i386_defconfig'
#
# configuration written to .config
#
默认情况下内核配置针对x86架构运行,并按照 arch/x86/configs/i386_defconfig 标准生成相应的配置文件(注:通过 diff 命令对比发现两者并非完全一致!)
2.1 默认架构
此默认配置主要适用于 x86 架构。那么如何启动针对 **ARM 架构 ** 的默认配置呢?答案在于指定顶级 Makefile 中 ARCH 变量的值,并且指定了交叉编译工具链前缀的位置。
ARCH ?= arm
CROSS_COMPILE ?= arm-linux-
这时候再执行make defconfig 时,针对的就是ARM架构 了:
HOSTCC scripts/basic/fixdep
HOSTCC scripts/basic/docproc
HOSTCC scripts/basic/hash
HOSTCC scripts/kconfig/conf.o
scripts/kconfig/conf.c: In function ‘conf_sym’:
scripts/kconfig/conf.c:159:6: warning: variable ‘type’ set but not used [-Wunused-but-set-variable]
int type;
^
scripts/kconfig/conf.c: In function ‘conf_choice’:
scripts/kconfig/conf.c:231:6: warning: variable ‘type’ set but not used [-Wunused-but-set-variable]
int type;
^
scripts/kconfig/conf.c:307:9: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]
fgets(line, 128, stdin);
^
scripts/kconfig/conf.c: In function ‘conf_askvalue’:
scripts/kconfig/conf.c:105:8: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]
fgets(line, 128, stdin);
^
HOSTCC scripts/kconfig/kxgettext.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/lex.zconf.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
*** Default configuration is based on 'versatile_defconfig'
#
# configuration written to .config
#
通过以上详细信息可以看出,在当前情况下按照'arch/arm/configs/versatile_defconfig'执行默认配置以生成.config文件(注:两者虽然有所关联但并非完全相同)。
2.2 .config
从上文可知, 配置信息主要存储在内核源码顶层目录下的\textbf{.config}文件中, 并且\textbf{.config}中的\textbf{ARM架构}最开始部分的内容如下所示。
#
# Automatically generated make config: don't edit
# Linux kernel version: 2.6.34.7
# Tue May 10 11:30:25 2016
#
CONFIG_ARM=y
CONFIG_SYS_SUPPORTS_APM_EMULATION=y
CONFIG_GENERIC_TIME=y
CONFIG_GENERIC_CLOCKEVENTS=y
CONFIG_HAVE_PROC_CPU=y
CONFIG_GENERIC_HARDIRQS=y
CONFIG_STACKTRACE_SUPPORT=y
CONFIG_HAVE_LATENCYTOP_SUPPORT=y
CONFIG_LOCKDEP_SUPPORT=y
CONFIG_TRACE_IRQFLAGS_SUPPORT=y
CONFIG_HARDIRQS_SW_RESEND=y
CONFIG_GENERIC_IRQ_PROBE=y
CONFIG_RWSEM_GENERIC_SPINLOCK=y
CONFIG_GENERIC_HWEIGHT=y
CONFIG_GENERIC_CALIBRATE_DELAY=y
CONFIG_NEED_DMA_MAP_STATE=y
CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ=y
CONFIG_VECTORS_BASE=0xffff0000
CONFIG_DEFCONFIG_LIST="/lib/modules/$UNAME_RELEASE/.config"
CONFIG_CONSTRUCTORS=y
从上述信息中可以看出第2行的描述表明*.config文件的内容是由make config自动生成(同样地,i386_defconfig也是一样)。因此,请避免手动修改
第6行指定了使用ARM架构 ,此外,其余所有的配置项都以CONFIG_ 为前缀。
2.3 其它配置
参考文献[1]提供了一些关于内核源码的关键提示,在其相关文档中提到了位于arch目录中的几个重要子目录

图2.1
其中 arm 目录包括 boot common configs 和 include 等等子目录 并且还包括内核已支持的所有 ARM 芯片对应的子目录

图2.2
上图configs 目录包含了各个ARM芯片的默认配置文件 (包括3.1小节的versatile_defconfig ):

图2.3
参考文献[1]中提到,如果需要将这些平台的配置文件复制到顶级目录,并且重命名为.config
cp arch/arm/configs/mini2440_defconfig .config
或者
make mini2440_defconfig
3 修改配置
3.1 交换式
进入内核源码顶级目录执行:
make config
输出如下:
liyihai@ubuntu:~/Mini2440/Kernels/linux-2.6.34.7$ make config
HOSTCC scripts/basic/fixdep
HOSTCC scripts/basic/docproc
HOSTCC scripts/basic/hash
HOSTCC scripts/kconfig/conf.o
scripts/kconfig/conf.c: In function ‘conf_sym’:
scripts/kconfig/conf.c:159:6: warning: variable ‘type’ set but not used [-Wunused-but-set-variable]
int type;
^
scripts/kconfig/conf.c: In function ‘conf_choice’:
scripts/kconfig/conf.c:231:6: warning: variable ‘type’ set but not used [-Wunused-but-set-variable]
int type;
^
scripts/kconfig/conf.c:307:9: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]
fgets(line, 128, stdin);
^
scripts/kconfig/conf.c: In function ‘conf_askvalue’:
scripts/kconfig/conf.c:105:8: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]
fgets(line, 128, stdin);
^
HOSTCC scripts/kconfig/kxgettext.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/lex.zconf.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
scripts/kconfig/conf arch/x86/Kconfig
#
# using defaults found in /boot/config-3.16.0-30-generic
#
/boot/config-3.16.0-30-generic:1264:warning: symbol value 'm' invalid for NET_DSA
/boot/config-3.16.0-30-generic:1680:warning: symbol value 'm' invalid for MTD_CMDLINE_PARTS
/boot/config-3.16.0-30-generic:2418:warning: symbol value 'm' invalid for NET_DSA_MV88E6XXX
/boot/config-3.16.0-30-generic:2419:warning: symbol value 'm' invalid for NET_DSA_MV88E6060
/boot/config-3.16.0-30-generic:2421:warning: symbol value 'm' invalid for NET_DSA_MV88E6131
/boot/config-3.16.0-30-generic:2422:warning: symbol value 'm' invalid for NET_DSA_MV88E6123_61_65
/boot/config-3.16.0-30-generic:2861:warning: symbol value 'm' invalid for IWL4965
/boot/config-3.16.0-30-generic:3783:warning: symbol value 'm' invalid for GPIO_UCB1400
/boot/config-3.16.0-30-generic:4259:warning: symbol value 'm' invalid for REGULATOR_88PM8607
/boot/config-3.16.0-30-generic:4318:warning: symbol value 'm' invalid for REGULATOR_TWL4030
/boot/config-3.16.0-30-generic:5396:warning: symbol value 'm' invalid for SND_HDA_CODEC_REALTEK
/boot/config-3.16.0-30-generic:5397:warning: symbol value 'm' invalid for SND_HDA_CODEC_ANALOG
/boot/config-3.16.0-30-generic:5398:warning: symbol value 'm' invalid for SND_HDA_CODEC_SIGMATEL
/boot/config-3.16.0-30-generic:5399:warning: symbol value 'm' invalid for SND_HDA_CODEC_VIA
/boot/config-3.16.0-30-generic:5402:warning: symbol value 'm' invalid for SND_HDA_CODEC_CIRRUS
/boot/config-3.16.0-30-generic:5403:warning: symbol value 'm' invalid for SND_HDA_CODEC_CONEXANT
/boot/config-3.16.0-30-generic:5404:warning: symbol value 'm' invalid for SND_HDA_CODEC_CA0110
/boot/config-3.16.0-30-generic:5407:warning: symbol value 'm' invalid for SND_HDA_CODEC_CMEDIA
/boot/config-3.16.0-30-generic:5408:warning: symbol value 'm' invalid for SND_HDA_CODEC_SI3054
/boot/config-3.16.0-30-generic:5409:warning: symbol value 'm' invalid for SND_HDA_GENERIC
/boot/config-3.16.0-30-generic:7288:warning: symbol value 'm' invalid for NFS_V3
/boot/config-3.16.0-30-generic:7290:warning: symbol value 'm' invalid for NFS_V4
* * Linux Kernel Configuration
* * * General setup
Prompt for development and/or incomplete code/drivers (EXPERIMENTAL) [N/y/?] (NEW)
从上文的最后一行可以看出, 是否需要用户提供 Prompt for development and/or incomplete code/drivers (EXPERIMENTAL)?由此可知这是一个 交互式的 配置方案, 并且对于内核的操作较为复杂.
3.2 菜单式
make menuconfig

图3.1
3.3 手动式(推荐)
在 arch/<platform_name>/configs 文件夹内获取一份与当前需求最为契合的配置文件,并随后手动进行调整。
4 清空配置
若要删除上述的配置值,执行:
make mrproper

图4.1
注:make distcean 不能替代上述命令!
5 帮助
可通过“make help”查看相关编译/配置命令和参数:
Cleaning targets:
clean - Remove most generated files but keep the config and
enough build support to build external modules
mrproper - Remove all generated files + config + various backup files
distclean - mrproper + remove editor backup and patch files
Configuration targets:
config - Update current config utilising a line-oriented program
menuconfig - Update current config utilising a menu based program
xconfig - Update current config utilising a QT based front-end
gconfig - Update current config utilising a GTK based front-end
oldconfig - Update current config utilising a provided .config as base
localmodconfig - Update current config disabling modules not loaded
localyesconfig - Update current config converting local mods to core
silentoldconfig - Same as oldconfig, but quietly, additionally update deps
randconfig - New config with random answer to all options
defconfig - New config with default answer to all options
allmodconfig - New config selecting modules when possible
allyesconfig - New config where all options are accepted with yes
allnoconfig - New config where all options are answered with no
Other generic targets:
all - Build all targets marked with [*]
* vmlinux - Build the bare kernel
* modules - Build all modules
modules_install - Install all modules to INSTALL_MOD_PATH (default: /)
firmware_install- Install all firmware to INSTALL_FW_PATH
(default: $(INSTALL_MOD_PATH)/lib/firmware)
dir/ - Build all files in dir and below
dir/file.[ois] - Build specified target only
dir/file.ko - Build module including final link
modules_prepare - Set up for building external modules
tags/TAGS - Generate tags file for editors
cscope - Generate cscope index
kernelrelease - Output the release version string
kernelversion - Output the version stored in Makefile
headers_install - Install sanitised kernel headers to INSTALL_HDR_PATH
(default: /home/liyihai/usb_disk9/Work/MyCodes/Kernels/linux-2.6.34.7/usr)
Static analysers
checkstack - Generate a list of stack hogs
namespacecheck - Name space analysis on compiled kernel
versioncheck - Sanity check on version.h usage
includecheck - Check for duplicate included header files
export_report - List the usages of all exported symbols
headers_check - Sanity check on exported headers
headerdep - Detect inclusion cycles in headers
Kernel packaging:
rpm-pkg - Build both source and binary RPM kernel packages
binrpm-pkg - Build only the binary kernel package
deb-pkg - Build the kernel as an deb package
tar-pkg - Build the kernel as an uncompressed tarball
targz-pkg - Build the kernel as a gzip compressed tarball
tarbz2-pkg - Build the kernel as a bzip2 compressed tarball
Documentation targets:
Linux kernel internal documentation in different formats:
htmldocs - HTML
pdfdocs - PDF
psdocs - Postscript
xmldocs - XML DocBook
mandocs - man pages
installmandocs - install man pages generated by mandocs
cleandocs - clean all generated DocBook files
Architecture specific targets (x86):
* bzImage - Compressed kernel image (arch/x86/boot/bzImage)
install - Install kernel using
(your) ~/bin/installkernel or
(distribution) /sbin/installkernel or
install to $(INSTALL_PATH) and run lilo
fdimage - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)
fdimage144 - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)
fdimage288 - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)
isoimage - Create a boot CD-ROM image (arch/x86/boot/image.iso)
bzdisk/fdimage*/isoimage also accept:
FDARGS="..." arguments for the booted kernel
FDINITRD=file initrd for the booted kernel
i386_defconfig - Build for i386
x86_64_defconfig - Build for x86_64
make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build
make V=2 [targets] 2 => give reason for rebuild of target
make O=dir [targets] Locate all output files in "dir", including .config
make C=1 [targets] Check all c source with $CHECK (sparse by default)
make C=2 [targets] Force check of all c source with $CHECK
Execute "make" or "make all" to build all targets marked with [*]
For further info see the ./README file
参考资料
cross-compiling Linux kernel on ARM架构
