背景
这两天读《UNIX传奇:历史与回忆》这本书,重新唤起了我对 Linux 开发的向往。
事不迟疑,赶紧动手干点儿什么。再说,万一这次真坚持下去了呢?
不知道为什么,“构建并替换内核”这件事在曾经的我眼中好似“活人换心脏”,让我打了退堂鼓,但如今看来不过是一连串命令,应该没有任何难度。
机器参数
我这次用的是一台大概 2011 年左右买的一台宏碁(Acer)笔记本,早就没人用了,后来我装了 Ubuntu 和 CentOS 双系统(参见:安装 Ubuntu 和 CentOS 双系统),但装完没两天也不用了。
大致看一下 CPU 和 内存:
~/Downloads$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 42
Model name: Intel(R) Core(TM) i3-2348M CPU @ 2.30GHz
Stepping: 7
~/Downloads$ free -m
total used free shared buff/cache available
Mem: 3773 1602 529 187 1640 1715
Swap: 0 0 0
参考链接
以下步骤参考这篇文章:How to Build Linux Kernel From Scratch {Step-By-Step Guide}
操作流程
开始之前,首先查看一下当前的内核版本:
~/Downloads$ uname -mrs
Linux 5.3.0-46-generic x86_64
嗯,用的是 5.3.0-46 。
步骤1:下载 Linux 内核源码
截至写稿时,Linux 内核官网 上的最新版本是 5.13.1 ,比参考文章上的又高了几个小版本。 使用 wget 下载源码:
$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.13.1.tar.xz
--2021-07-14 21:30:10-- https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.13.1.tar.xz
Resolving cdn.kernel.org (cdn.kernel.org)... 2a04:4e42:1a::432, 151.101.229.176
Connecting to cdn.kernel.org (cdn.kernel.org)|2a04:4e42:1a::432|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 119286316 (114M) [application/x-xz]
Saving to: ‘linux-5.13.1.tar.xz’
linux-5.13.1.tar.xz 100%[===================>] 113.76M 90.4KB/s in 18m 11s
2021-07-14 21:48:22 (107 KB/s) - ‘linux-5.13.1.tar.xz’ saved [119286316/119286316]
步骤 2:解压源码
这一步也没啥说的。
$ tar xvf linux-5.13.1.tar.xz
linux-5.13.1/virt/kvm/Kconfig
linux-5.13.1/virt/kvm/async_pf.c
linux-5.13.1/virt/kvm/async_pf.h
linux-5.13.1/virt/kvm/coalesced_mmio.c
linux-5.13.1/virt/kvm/coalesced_mmio.h
linux-5.13.1/virt/kvm/dirty_ring.c
linux-5.13.1/virt/kvm/eventfd.c
linux-5.13.1/virt/kvm/irqchip.c
linux-5.13.1/virt/kvm/kvm_main.c
linux-5.13.1/virt/kvm/mmu_lock.h
linux-5.13.1/virt/kvm/vfio.c
linux-5.13.1/virt/kvm/vfio.h
linux-5.13.1/virt/lib/
linux-5.13.1/virt/lib/Kconfig
linux-5.13.1/virt/lib/Makefile
linux-5.13.1/virt/lib/irqbypass.c
步骤 3:下载所需软件包
回头再研究这些软件包,先按参考文章来吧:
~/Downloads$ sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
[sudo] password for doudou:
Reading package lists... Done
Building dependency tree
Reading state information... Done
3 upgraded, 8 newly installed, 0 to remove and 518 not upgraded.
Need to get 8,035 kB of archives.
After this operation, 4,734 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.aliyun.com/ubuntu bionic/main amd64 flex amd64 2.6.4-6 [316 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 libssl-dev amd64 1.1.1-1ubuntu2.1~18.04.9 [1,566 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 libssl1.1 amd64 1.1.1-1ubuntu2.1~18.04.9 [1,301 kB]
update-alternatives: using /usr/bin/bison.yacc to provide /usr/bin/yacc (yacc) in auto mode
Setting up git (1:2.17.1-1ubuntu0.8) ...
Setting up libssl-dev:amd64 (1.1.1-1ubuntu2.1~18.04.9) ...
Setting up libfl-dev:amd64 (2.6.4-6) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
参考文章中简单描述了一下这些软件包:链接
步骤 4:内核配置
这一步也没啥好说的,把系统当前版本内核的配置文件,拿过来直接用。
~/Downloads$ cd linux-5.13.1/
~/Downloads/linux-5.13.1$ cp -v /boot/config-$(uname -r) .config
'/boot/config-5.3.0-46-generic' -> '.config'
步骤 5:开始构建!
这一步就很有意思了,输入 make 之后弹出来一堆选项,都标着 (NEW) 的字样,估计是之前版本内核配置里没有包括的新参数吧。
一开始我还想看看这些配置都是啥意思,选一选,后来发现要选的参数实在太多了,怎么也得有几千个吧,我就一路都按回车了,按了也有半天!
$ make
Makefile:656: include/config/auto.conf: No such file or directory
Makefile:701: include/config/auto.conf.cmd: No such file or directory
SYNC include/config/auto.conf.cmd
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/conf.o
.config:10171:warning: symbol value '1' invalid for KASAN_STACK
*
* Restart config...
*
*
* General setup
*
Compile also drivers which will not load (COMPILE_TEST) [N/y/?] n
Local version - append to kernel release (LOCALVERSION) []
Automatically append version information to the version string (LOCALVERSION_AUTO) [N/y/?] n
Build ID Salt (BUILD_SALT) []
Kernel compression mode
> 1. Gzip (KERNEL_GZIP)
2. Bzip2 (KERNEL_BZIP2)
3. LZMA (KERNEL_LZMA)
4. XZ (KERNEL_XZ)
5. LZO (KERNEL_LZO)
6. LZ4 (KERNEL_LZ4)
7. ZSTD (KERNEL_ZSTD) (NEW)
choice[1-7?]:
SYSHDR arch/x86/include/generated/uapi/asm/unistd_32.h
SYSHDR arch/x86/include/generated/uapi/asm/unistd_64.h
SYSHDR arch/x86/include/generated/uapi/asm/unistd_x32.h
SYSTBL arch/x86/include/generated/asm/syscalls_32.h
SYSHDR arch/x86/include/generated/asm/unistd_32_ia32.h
SYSHDR arch/x86/include/generated/asm/unistd_64_x32.h
SYSTBL arch/x86/include/generated/asm/syscalls_64.h
HYPERCALLS arch/x86/include/generated/asm/xen-hypercalls.h
HOSTCC arch/x86/tools/relocs_32.o
HOSTCC arch/x86/tools/relocs_64.o
HOSTCC arch/x86/tools/relocs_common.o
HOSTLD arch/x86/tools/relocs
HOSTCC scripts/selinux/genheaders/genheaders
HOSTCC scripts/selinux/mdp/mdp
HOSTCC scripts/bin2c
截至写稿时 make 还在跑,估计要跑一晚上了,明天再来看。
第二天早上发现,终于跑完啦:
CC [M] sound/usb/snd-usbmidi-lib.mod.o
LD [M] sound/usb/snd-usbmidi-lib.ko
CC [M] sound/usb/usx2y/snd-usb-us122l.mod.o
LD [M] sound/usb/usx2y/snd-usb-us122l.ko
CC [M] sound/usb/usx2y/snd-usb-usx2y.mod.o
LD [M] sound/usb/usx2y/snd-usb-usx2y.ko
CC [M] sound/x86/snd-hdmi-lpe-audio.mod.o
LD [M] sound/x86/snd-hdmi-lpe-audio.ko
CC [M] sound/xen/snd_xen_front.mod.o
LD [M] sound/xen/snd_xen_front.ko
CC [M] virt/lib/irqbypass.mod.o
LD [M] virt/lib/irqbypass.ko
GEN scripts/gdb/linux/constants.py
未完待续
|