【内核学习】001编译和运行
compile the kernel
clone和compile在容器中(虚拟机)操作
1 | docker run --privileged -d -it --name angel -v `pwd`:/root/code -w /root/code ghcr.io/cybrid-systems/dev |
clone code
1 | git clone https://github.com/torvalds/linux.git |
compile
1 | # 1. 生成默认配置 |
binrpm-pkg: Builds binary RPMs (kernel image, modules, headers). Use rpm-pkg for both binary and source RPMs.LOCALVERSION=-debug: Appends a custom suffix (e.g., "-debug") to the kernel version for identification in GRUB.
1 | dpkg -i kernel-*.deb |
在 Ubuntu 系统上,你可以通过 GRUB 引导菜单
在开机时手动选择要启动的内核(包括你刚编译安装的新内核,如带
-debug
后缀的那个)。这是最简单、最安全的临时/测试方式,不需要修改配置文件。
步骤:开机时选择内核
重启电脑
执行sudo reboot或正常重启系统。进入 GRUB 菜单
- 在开机过程中(看到紫色/黑色屏幕或厂商 logo 后),立即按住
Shift 键(大多数 BIOS/UEFI 系统适用)。
- 如果是纯 UEFI 系统且 Shift 不起作用,试试 按住 Esc
键 或 快速连按 Shift。
- 目的是强制显示 GRUB 菜单(默认情况下,如果只有一个内核且超时为 0,它会直接启动而不显示菜单)。
- 在开机过程中(看到紫色/黑色屏幕或厂商 logo 后),立即按住
Shift 键(大多数 BIOS/UEFI 系统适用)。
在 GRUB 菜单中选择内核
- 你会看到类似这样的主菜单(紫色背景):
- Ubuntu
- Advanced options for Ubuntu
- (可能还有 Windows 或其他系统,如果是双系统)
- Ubuntu
- 用 方向键 ↓ 选中 Advanced options for Ubuntu(Ubuntu 高级选项),按 Enter 进入子菜单。
- 你会看到类似这样的主菜单(紫色背景):
在高级选项中挑选内核
- 子菜单会列出所有已安装的内核版本,例如:
- Ubuntu, with Linux
6.x.x-debug(你新编译的那个,通常带自定义后缀)
- Ubuntu, with Linux 6.x.x-generic(旧的或默认的)
- Ubuntu, with Linux 6.x.x-debug (recovery mode)
- Ubuntu, with Linux
6.x.x-debug(你新编译的那个,通常带自定义后缀)
- 用 方向键 选中你想要的内核(比如新编译的那个),然后按 Enter 启动。
- 子菜单会列出所有已安装的内核版本,例如:
启动成功后验证
进入系统后,打开终端运行:
sudo grub-reboot "Advanced options for Ubuntu>Ubuntu, with Linux 6.x.x-debug" sudo reboot1
2
3
4
5
6
7uname -r
```
输出应该显示你选择的内核版本(例如 `6.x.x-debug`)。
### 额外小技巧
- **临时只下一次启动某个内核**(不改默认):
在进入系统后运行(替换成你想要的内核名称,从 `/boot/grub/grub.cfg` 中 grep 找):sudo nano /etc/default/grub1
2
3
4只影响下一次重启,下下次又恢复默认。
- **如果 GRUB 菜单一直不显示**(超时为 0):
编辑 `/etc/default/grub`:sudo update-grub1
把 `GRUB_TIMEOUT=0` 改成 `GRUB_TIMEOUT=10`(单位:秒),保存后运行:
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以后开机就会显示菜单 10 秒。
- **想永久默认新内核**(不再手动选):
参考之前回复:修改 `/etc/default/grub` 中的 `GRUB_DEFAULT=` 为新内核的完整菜单路径(或编号 0,如果它排第一),然后 `sudo update-grub`。
这样操作后,你就可以自由测试不同内核了。如果新内核启动失败(比如黑屏、panic),重启再选回旧内核即可,非常安全。需要截图或具体报错也可以继续问!
## 使用 BusyBox 创建最小 rootfs 的步骤如下:
0. gcc 15 fix
```bash
Option 1: Disable the tc Applet (Recommended if You Don't Need It)
The tc command is for traffic control and may not be essential for your use case. Disabling it avoids the problem without patching code.
Run make menuconfig (or make config for text-based) in your BusyBox source directory.
Navigate to Networking Utilities > Disable tc.
Save and exit.
Re-run make to build BusyBox.
Solution: Patch the Detection Script
Edit scripts/kconfig/lxdialog/check-lxdialog.sh to update the test program for compatibility with modern GCC.
Open the file:
nano scripts/kconfig/lxdialog/check-lxdialog.sh (or use your preferred editor).
Locate the section around lines 49-55 (may vary slightly) that echoes the test code:textecho "#include CURSES_LOC
main() {}
EOF
Change it to:textecho "#include CURSES_LOC
int main(void) { return 0; }
EOF
This adds an explicit int return type and a return statement to satisfy stricter standards.
Using void for parameters avoids potential implicit declaration issues.
Save and exit.
Optionally, clean up:
make clean
Retry:
make menuconfig
This should now detect ncurses correctly and launch the menuconfig interface.下载并编译 BusyBox
1
2
3
4
5
6
7
8
9
10
11
12wget https://busybox.net/downloads/busybox-1.36.1.tar.bz2
tar xf busybox-1.36.1.tar.bz2
cd busybox-1.36.1
make defconfig
make menuconfig # 可选:静态编译 Settings → Build static binary (no shared libs)
# 2. 用 sed 两行命令完成上述两项修改
sed -i 's/^# CONFIG_STATIC is not set/CONFIG_STATIC=y/' .config
sed -i 's|CONFIG_PREFIX=.*|CONFIG_PREFIX="./rootfs"|' .config
# 3. 让 Kbuild 自动补齐依赖(重要!)
make oldconfig >/dev/null </dev/null
make -j$(nproc)
make install
这里回到mac宿主机。
- 创建必要的目录结构
1 | cd rootfs |
- 创建基础设备节点
1 | cd rootfs/dev |
- 创建初始化脚本
1 | cd rootfs |
- 创建简单的fstab
1 | cat > etc/fstab << 'EOF' |
- 创建rootfs镜像
1 | cd rootfs |
- 使用QEMU启动
1
2
3
4
5
6
7
8
9
10qemu-system-aarch64 \
-machine virt \
-cpu cortex-a72 \
-smp 4 \
-m 2G \
-kernel linux/arch/arm64/boot/Image \
-initrd busybox-1.36.1/rootfs.cpio.gz \
-append "console=ttyAMA0 rdinit=/init" \
-nographic \
-no-reboot