结论速览

  • 最小系统就一条链:U-Boot 到 Kernel 到 initramfs/rootfs 到 /init 到 BusyBox 或自定义应用。
  • U-Boot 不是用户空间程序,bootm 是 U-Boot 命令,不是 Linux 命令。
  • ls、sh、wget 这些命令不由内核提供,内核只提供进程调度、内存管理、驱动、文件系统、TCP/IP 和系统调用。
  • Debian、OpenWrt、Ubuntu 不是另一种内核,差别只在用户空间那一套。
  • 分析问题的第一步是判断它属于哪一层,分层正确,问题就不会显得混在一起。

1. 基本启动链

一个最小的 MT7621 Linux 系统可以简化成:

1
2
3
4
5
6
7
8
9
U-Boot
↓
Linux Kernel
↓
initramfs / rootfs
↓
/init 或 /sbin/init
↓
BusyBox / systemd / 自定义应用

每一层负责的事情不同。

层级 主要职责
U-Boot 加载内核、设备树、rootfs/initramfs,并跳转到内核入口
Linux Kernel 管理 CPU、内存、中断、驱动、网络、文件系统
initramfs/rootfs 提供用户空间中的目录、程序、库、配置
/init 第一个用户空间进程,通常为 PID 1
BusyBox 提供 shell 和常用 Linux 命令
应用程序 调用 Linux API 完成功能

2. U-Boot 和 Linux 的边界

U-Boot 是 Bootloader,不属于 Linux 用户空间。

它主要做:

1
2
3
4
5
6
7
8
9
10
11
12
13
上电
↓
基础硬件初始化
↓
初始化 RAM
↓
找到 Kernel
↓
找到 DTB
↓
可选:找到 initramfs
↓
跳转到 Kernel

bootm 是 U-Boot 命令,不是 Linux 命令。

当 U-Boot 把控制权交给 Kernel 后,后续初始化工作由 Linux 内核接管。

3. Kernel 和用户空间的边界

Linux Kernel 本身并不提供:

1
2
3
4
5
ls
cp
wget
sh
cat

这些都属于用户空间程序。

Kernel 负责:

1
2
3
4
5
6
进程调度
内存管理
设备驱动
文件系统
TCP/IP
系统调用

用户程序则通过 Kernel 提供的接口使用这些能力。

例如:

1
2
3
4
5
6
7
8
9
应用程序
↓
write()
↓
Linux syscall
↓
串口驱动
↓
UART 硬件

4. Linux 发行版是什么

Debian、OpenWrt、Ubuntu 并不是「另一种内核」。

更准确地说:

1
2
3
4
5
Linux Kernel
+
一套用户空间
=
完整 Linux 系统

例如:

1
2
Linux Kernel + BusyBox rootfs
= 极简嵌入式 Linux

而:

1
2
Linux Kernel + Debian rootfs
= Debian 系统

二者底层都可以使用 Linux Kernel。

5. 最核心的分层思想

以后分析 Linux 问题时,先判断问题属于哪一层:

1
2
3
4
5
6
Bootloader 问题?
Kernel 问题?
驱动问题?
rootfs 问题?
libc 问题?
应用程序问题?

只要分层正确,Linux 系统就不会显得「所有东西混在一起」。