编译Linux内核

环境准备

1
$ sudo apt install libelf-dev libssl-dev build-essential flex bison

下载内核源码

1
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/linux.git

or

1
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/linux-stable.git

在这里我们使用国内的清华tuna源加速下载内核源码

配置内核

在这里我们使用ubuntu 20.04自己带的配置文件

1
cp /usr/src/linux-headers-5.4.0-24-generic/.config arch/x86/configs/ubuntu_defconfig

编译内核

生成配置文件

1
make -j$(nproc) O=../build/linux-kernel ubuntu_defconfig

-j$(nproc) 启动多少个job来编译内核,nproc返回逻辑CPU的数量

O=../build/linux-kernel 在编译过程中会生成大量的.o文件,为了避免和源码混淆,指定输出目录

ubuntu_defconfig 是我们要指定的编译文件配置文件,位于arch/{ARCH}/configs/xxx_defconfig

如果一切都顺利的话,会在../build/linux-kernel目录下生成.config文件,这个将是这次编译内核所使用的配置文件

同时还会生成下面几个重要的配置文件

include/generated/autoconf.h 该文件会被所有内核源码包含,里面包含了大量的配置宏定义

1
2
3
4
5
6
7
8
9
10
11
12
/*
*
* Automatically generated file; DO NOT EDIT.
* Linux/x86 5.4.100-custom Kernel Configuration
*
*/
#define CONFIG_IP6_NF_MATCH_AH_MODULE 1
#define CONFIG_NLS_CODEPAGE_861_MODULE 1
#define CONFIG_MTD_SPI_NAND_MODULE 1
#define CONFIG_RING_BUFFER 1
#define CONFIG_HARDENED_USERCOPY_FALLBACK 1
...

编译linux内核

编译vmlinux, vmlinux是内核最终生成的内核文件

1
make -j$(nproc) O=../build/linux-kernel vmlinux

也可以直接编译 整个deb包

1
make -j$(nproc) O=../build/linux-kernel bindeb-pkg

编译bindeb-pkg需要编译很多内核模块,需要花费很多时间

编译内核模块

1
make -j$(nproc) O=../build/linux-kernel modules M=`pwd`/drivers/gpu/drm

modules 该target用与编译内核模块

M=drivers/gpu/drm 用于指定编译的内核模块

1
make -j$(nproc) O=../build/linux-kernel drivers/gpu/drm/amd/amdgpu/amdgpu.ko

也可以直接编译具体ko文件

重新生成配置文件

如果修改了.config文件,需要重新输入下面命令来生成对应的头文件

1
make -j$(nproc) O=../build/linux-kernel oldconfig

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
nconfig - Update current config utilising a ncurses menu based 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
defconfig - New config with default from ARCH supplied defconfig
savedefconfig - Save current config as ./defconfig (minimal config)
allnoconfig - New config where all options are answered with no
allyesconfig - New config where all options are accepted with yes
allmodconfig - New config selecting modules when possible
alldefconfig - New config with all symbols set to default
randconfig - New config with random answer to all options
listnewconfig - List new options
olddefconfig - Same as oldconfig but sets new symbols to their
default value without prompting
kvmconfig - Enable additional options for kvm guest kernel support
xenconfig - Enable additional options for xen dom0 and guest kernel support
tinyconfig - Configure the tiniest possible kernel
testconfig - Run Kconfig unit tests (requires python3 and pytest)

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: /)
dir/ - Build all files in dir and below
dir/file.[ois] - Build specified target only
dir/file.ll - Build the LLVM assembly file
(requires compiler support for LLVM assembly generation)
dir/file.lst - Build specified mixed source/assembly target only
(requires a recent binutils and recent build (System.map))
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
gtags - Generate GNU GLOBAL index
kernelrelease - Output the release version string (use with make -s)
kernelversion - Output the version stored in Makefile (use with make -s)
image_name - Output the image name (use with make -s)
headers_install - Install sanitised kernel headers to INSTALL_HDR_PATH
(default: ./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
headerdep - Detect inclusion cycles in headers
coccicheck - Check with Coccinelle

Tools:
nsdeps - Generate missing symbol namespace dependencies

Kernel selftest:
kselftest - Build and run kernel selftest (run as root)
Build, install, and boot kernel before
running kselftest on it
kselftest-clean - Remove all generated kselftest files
kselftest-merge - Merge all the config dependencies of kselftest to existing
.config.

Userspace tools targets:
use "make tools/help"
or "cd tools; make help"

Kernel packaging:
rpm-pkg - Build both source and binary RPM kernel packages
binrpm-pkg - Build only the binary kernel RPM package
deb-pkg - Build both source and binary deb kernel packages
bindeb-pkg - Build only the binary kernel deb package
snap-pkg - Build only the binary kernel snap package (will connect to external hosts)
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
tarxz-pkg - Build the kernel as a xz compressed tarball
perf-tar-src-pkg - Build perf-5.4.100.tar source tarball
perf-targz-src-pkg - Build perf-5.4.100.tar.gz source tarball
perf-tarbz2-src-pkg - Build perf-5.4.100.tar.bz2 source tarball
perf-tarxz-src-pkg - Build perf-5.4.100.tar.xz source tarball

Documentation targets:
Linux kernel internal documentation in different formats from ReST:
htmldocs - HTML
latexdocs - LaTeX
pdfdocs - PDF
epubdocs - EPUB
xmldocs - XML
linkcheckdocs - check for broken external links (will connect to external hosts)
refcheckdocs - check for references to non-existing files under Documentation
cleandocs - clean all generated files

make SPHINXDIRS="s1 s2" [target] Generate only docs of folder s1, s2
valid values for SPHINXDIRS are:

make SPHINX_CONF={conf-file} [target] use *additional* sphinx-build
configuration. This is e.g. useful to build with nit-picking config.

Default location for the generated documents is Documentation/output

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
ubuntu_defconfig - Build for ubuntu
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 re-compiled c source with $CHECK (sparse by default)
make C=2 [targets] Force check of all c source with $CHECK
make RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sections
make W=n [targets] Enable extra build checks, n=1,2,3 where
1: warnings which may be relevant and do not occur too often
2: warnings which occur quite often but may still be relevant
3: more obscure warnings, can most likely be ignored
Multiple levels can be combined with W=12 or W=123

作者

Wang Yang

发布于

2020-05-25

更新于

2020-05-25

许可协议