内核编译¶
什么是Linux¶
Linux是Unix操作系统的克隆,它的目标是POSIX和单一UNIX规范兼容。
它具有现代成熟的Unix所具有的所有特性,包括真正的多任务处理、虚拟内存、共享库、按需加载、共享写时复制可执行文件、适当的内存管理以及包括IPv4和IPv6在内的多堆栈网络。
Linux可以在哪些机器上运行¶
最初是为基于x86的32位pc(386或更高版本)开发的,今天Linux可以运行在(至少)Compaq Alpha AXP、Sun SPARC和UltraSPARC、摩托罗拉68000、PowerPC、PowerPC64、ARM、日立SuperH、Cell、IBM S/390、MIPS、惠普PA-RISC、英特尔IA-64、DEC VAX、AMD x86-64 Xtensa和ARC架构上。
只要CPU支持分页内存管理单元(PMMU)和GCC,Linux很容易一直到大多数通用的32位或64位体系架构上。
另外,Linux也可以被一直到一些没有PMMU的CPU结构上,只是功能会有些限制。
最后,Linux 内核还可以运行在它自己的环境中,就当作一个用户空间的应用程序,这称为User Mode Linux(简称:UML)。
通过源文件安装内核¶
解压内核源码¶
xz -cd linux-6.x.tar.xz | tar xvf -
注意:不要使用
/usr/src/linux下的内核源码,这个地方的源代码通常不完整,只是供库的头文件使用。
打补丁¶
xz -cd ../patch-6.x.xz | patch -p1
最好使用内核提供的打补丁方法:
# 补丁在内核根目录,也可手动指定
linux/scripts/patch-kernel linux
注意:补丁不是增量的,假如目前内核版本是
6.x,要想升级到6.x.3,只需打一个补丁就可以了,不需要先打补丁到6.x.1这样依次打补丁。假如目前源码是6.x.2,要想升级到6.x.3,需要先反打补丁patch -R变为6.x然后在打补丁到6.x.3。
清理整个源码目录¶
make mrproper
安装内核编译需要的依赖¶
配置内核¶
"make config" Plain text interface.
"make menuconfig" Text based color menus, radiolists & dialogs.
"make nconfig" Enhanced text based color menus.
"make xconfig" Qt based configuration tool.
"make gconfig" GTK+ based configuration tool.
"make oldconfig" Default all questions based on the contents of
your existing ./.config file and asking about
new config symbols.
"make olddefconfig"
Like above, but sets new symbols to their default
values without prompting.
"make defconfig" Create a ./.config file by using the default
symbol values from either arch/$ARCH/defconfig
or arch/$ARCH/configs/${PLATFORM}_defconfig,
depending on the architecture.
"make ${PLATFORM}_defconfig"
Create a ./.config file by using the default
symbol values from
arch/$ARCH/configs/${PLATFORM}_defconfig.
Use "make help" to get a list of all available
platforms of your architecture.
"make allyesconfig"
Create a ./.config file by setting symbol
values to 'y' as much as possible.
"make allmodconfig"
Create a ./.config file by setting symbol
values to 'm' as much as possible.
"make allnoconfig" Create a ./.config file by setting symbol
values to 'n' as much as possible.
"make randconfig" Create a ./.config file by setting symbol
values to random values.
"make localmodconfig" Create a config based on current config and
loaded modules (lsmod). Disables any module
option that is not needed for the loaded modules.
To create a localmodconfig for another machine,
store the lsmod of that machine into a file
and pass it in as a LSMOD parameter.
Also, you can preserve modules in certain folders
or kconfig files by specifying their paths in
parameter LMC_KEEP.
target$ lsmod > /tmp/mylsmod
target$ scp /tmp/mylsmod host:/tmp
host$ make LSMOD=/tmp/mylsmod \
LMC_KEEP="drivers/usb:drivers/gpu:fs" \
localmodconfig
The above also works when cross compiling.
"make localyesconfig" Similar to localmodconfig, except it will convert
all module options to built in (=y) options. You can
also preserve modules by LMC_KEEP.
"make kvm_guest.config" Enable additional options for kvm guest kernel
support.
"make xen.config" Enable additional options for xen dom0 guest kernel
support.
"make tinyconfig" Configure the tiniest possible kernel.
编译¶
# kernel source code: /usr/src/linux-6.x
# build directory: /home/name/build/kernel
cd /usr/src/linux-6.x
# 配置内核
make O=/home/name/build/kernel menuconfig
# 编译内核和内核模块
make O=/home/name/build/kernel
# 安装内核模块
sudo make O=/home/name/build/kernel modules_install install
# 最后复制内核文件到 /boot/ 下,修改grub或者lilo,使新内核生效
# O= 参数指定输出文件的位置
# V=1 输出更多编译信息
# V=2 输出重新构建每个目标的原因
# V=0 默认值
注意:如果使用了
O=选项,那么所有执行的make中都要使用这一选项。