Ch02: Building the 5.x Linux Kernel from Source - Part 1

Step 1 – obtaining a Linux kernel source tree wget --https-only -O ~/Downloads/linux-5.4.1.tar.xz https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.1.tar.xz Step 2 – extracting the kernel source tree cd ~/Downloads ; ls -lh linux-5.4.1.tar.xz tar xf ~/Downloads/linux-5.4.1.tar.xz mkdir -p ~/kernels tar xf ~/Downloads/linux-5.4.1.tar.xz --directory=${HOME}/kernels/ 設定環境變數是一個 good practice,可加入 ~/.bashrc 中: export LLKD_KSRC=${HOME}/kernels/linux-5.4 現在已經不像以前一定會把 source code 放到 usr/src 了 A brief tour of the kernel source tree user@ubuntu:~/kernels/linux-5.4$ ls COPYING Kconfig README crypto init mm security virt CREDITS LICENSES arch drivers ipc net sound Documentation MAINTAINERS block fs kernel samples tools Kbuild Makefile certs include lib scripts usr 使用 du -m . 可以看到檔案大小為 1011M 幾乎是 1 GB 了 user@ubuntu:~/kernels/linux-5.4$ head Makefile # SPDX-License-Identifier: GPL-2.0 VERSION = 5 PATCHLEVEL = 4 SUBLEVEL = 1 EXTRAVERSION = NAME = Kleptomaniac Octopus # *DOCUMENTATION* # To see a list of typical targets execute "make help" # More info can be located in ./README 這裡可以知道目前的 source code 版本為 5.4.1 最上層的檔案與資料夾 README: 告知 documents 的所在位置 COPYING: licence 的所在位置 MAINTAINERS Major subsystem directories Makefile: 這是 top-level 的 Makefile 使用 kbuild 與 kernel modules 使用這個 Makefile (至少在初始化階段) kernel: Core kernel subsystem process/thread life cycle CPU scheduling locking cgroups, timers interrupts signaling modules tracing mm: memory management fs: virtual file system block: block I/O, file system 的更底層 net: network ipc: inter-processes communication sound: Advanced Linux Sound Architecture (ALSA) virt: virtualization code like KVM Infrastructure/misc arch: architecture 相關,像是 x86-64, risc-v, arm 等等 crypto: 加密相關 include: arch-independent kernel headers, 但也有例外,像是 arch/<cpu>/include/... init: 初始化的過程 init/main.c:start_kernel(), start_kernel() lib: 注意 kernel 並不支援 shared library scripts: 一些有用的 scripts security: Linux Security Module (LSM) tools: mostly userspace applications user@ubuntu:~/kernels/linux-5.4$ cd ${LLKD_KSRC} ; ls arch/ Kconfig arm csky ia64 mips openrisc riscv sparc x86 alpha arm64 h8300 m68k nds32 parisc s390 um xtensa arc c6x hexagon microblaze nios2 powerpc sh unicore32 kernel 所支援的 ISA ...

December 3, 2025

Ch03: Building the 5.x Linux Kernel from Source - Part 2

Step 4 – building the kernel image and modules 從使用者的角度而言,這非常的簡單,只需要 make 就行了 make help 可以看到 help,這裡可以先觀察 target all $ make help [...] Other generic targets: all - Build all targets marked with [*] * vmlinux - Build the bare kernel * modules - Build all modules [...] Architecture specific targets (x86): * bzImage - Compressed kernel image (arch/x86/boot/bzImage) [...] 從這裡可以得知,make all 的時候會有 vmlinux, modules, bzImage 這三個 target 被觸發 vmlinux: 未壓縮的 kernel image modules: 在 config 中被標記為 M 的會變成 kernel modules (.ko files) bzImage: 壓縮過後的 kernel image (x86) 實際在開機時,會用壓縮過後的 bzImage,vmlinux 是作為 debug 用,(不在這本書的範圍) ...

December 6, 2025

Ch04: Writing Your First Kernel Module - LKMs Part 1

Understanding kernel architecture – part 1 Exploring LKMs The LKM framework Kernel modules within the kernel source tree kernel modules 存放於 /lib/modules/$(uname -r)/ 以我的例子來說,正在運行 95 個 modules,可以發現跟書上的範例 5359 差很多,我的推測是我使用的是 server 版,書上使用的是 desktop 版。 其中一個大量使用 modules 這種方式的是 device driver,例如可以看 kernel/drivers/net/ethernet 原先 Ubuntu 18.04 的 netword drivers: 我自己編譯的 5.4.1-llkd01 的 device drivers: 明顯少了許多,這是因為當初在 build 時,是使用 lsmod 抓取正在運行中的 module,並不像 ubuntu 需要考慮各種可能的情境 其中的個有名的 driver 是 Intel 1GbE Network Interface Card (NIC) lsmod | grep e1000 ...

December 8, 2025