graph TB
A["硬件上电"] --> B["BIOS / UEFI"]
B --> C["GRUB / Bootloader"]
C --> D["vmlinuz\n(压缩内核)"]
D --> E["解压缩 → vmlinux"]
E --> F["start_kernel()"]
F --> G["挂载 rootfs"]
G --> H["init 进程"]
H --> I["用户态\nShell / GUI"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#FFDAB9,stroke:#FFAB76,color:#333
style D fill:#FFB3C6,stroke:#F48FB1,color:#333
style E fill:#B5EAD7,stroke:#80CBC4,color:#333
style F fill:#B5EAD7,stroke:#80CBC4,color:#333
style G fill:#FFF9C4,stroke:#F9A825,color:#333
style H fill:#FFF9C4,stroke:#F9A825,color:#333
style I fill:#C7CEEA,stroke:#9FA8DA,color:#333
一、BIOS / UEFI 阶段
1.1 BIOS 启动(Legacy)
graph TB
A["按下电源"] --> B["POST\nPower-On Self-Test"]
B --> C["查找启动设备"]
C --> D["读取 MBR\n512 字节"]
D --> E["跳到 MBR 代码"]
E --> F["加载 bootloader"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#FFDAB9,stroke:#FFAB76,color:#333
style D fill:#FFB3C6,stroke:#F48FB1,color:#333
style E fill:#B5EAD7,stroke:#80CBC4,color:#333
style F fill:#FFF9C4,stroke:#F9A825,color:#333
MBR 限制:
最大 2 TB 磁盘
4 个主分区
16 字节 bootloader
1.2 UEFI 启动(现代)
graph TB
A["按下电源"] --> B["SEC\nSecurity"]
B --> C["PEI\nPre-EFI Initialization"]
C --> D["DXE\nDriver Execution"]
D --> E["BDS\nBoot Device Select"]
E --> F["TSL\nTransient System Load"]
F --> G["RT\nRun Time"]
G --> H["加载内核"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#FFDAB9,stroke:#FFAB76,color:#333
style D fill:#FFDAB9,stroke:#FFAB76,color:#333
style E fill:#FFB3C6,stroke:#F48FB1,color:#333
style F fill:#B5EAD7,stroke:#80CBC4,color:#333
style G fill:#B5EAD7,stroke:#80CBC4,color:#333
style H fill:#FFF9C4,stroke:#F9A825,color:#333
UEFI 优势:
支持大磁盘(GPT)
安全启动(Secure Boot)
模块化驱动
二、Bootloader(GRUB)
2.1 GRUB 启动流程
graph TB
A["MBR / EFI 分区"] --> B["GRUB Stage 1\n(MBR / EFI)"]
B --> C["GRUB Stage 1.5\n(fs 模块)"]
C --> D["GRUB Stage 2\n(/boot/grub)"]
D --> E["读 grub.cfg"]
E --> F["加载 vmlinuz + initramfs"]
F --> G["启动内核"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#FFDAB9,stroke:#FFAB76,color:#333
style D fill:#FFB3C6,stroke:#F48FB1,color:#333
style E fill:#B5EAD7,stroke:#80CBC4,color:#333
style F fill:#FFF9C4,stroke:#F9A825,color:#333
style G fill:#FFF9C4,stroke:#F9A825,color:#333
graph TB
A["start_kernel()"] --> B["setup_arch()\n架构初始化"]
B --> C["setup_command_line()\n解析 cmdline"]
C --> D["setup_per_cpu()\nper-CPU 数据"]
D --> E["build_all_zonelists()\n内存节点"]
E --> F["page_alloc_init()\n伙伴系统"]
F --> G["vfs_caches_init()\nVFS 缓存"]
G --> H["sched_init()\n调度器"]
H --> I["fork_init()\nPID 哈希"]
I --> J["kernel_init()\n启动 init"]
J --> J1["kernel_init_freeable()\n各种子系统"]
J1 --> K["try_to_run_init_process()\n执行 /init"]
K --> L["/sbin/init\nPID 1"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#FFDAB9,stroke:#FFAB76,color:#333
style D fill:#FFB3C6,stroke:#F48FB1,color:#333
style E fill:#B5EAD7,stroke:#80CBC4,color:#333
style F fill:#B5EAD7,stroke:#80CBC4,color:#333
style G fill:#FFF9C4,stroke:#F9A825,color:#333
style H fill:#FFF9C4,stroke:#F9A825,color:#333
style I fill:#FFF9C4,stroke:#F9A825,color:#333
style J fill:#FFF9C4,stroke:#F9A825,color:#333
style J1 fill:#FFF9C4,stroke:#F9A825,color:#333
style K fill:#FFF9C4,stroke:#F9A825,color:#333
style L fill:#C7CEEA,stroke:#9FA8DA,color:#333
3.4 第一个进程(init / PID 1)
graph TB
A["kernel_init()"] --> B{"init=/bin/sh?"}
B -->|"是"| C["执行 /bin/sh"]
B -->|"否"| D["执行 /sbin/init"]
D --> E["systemd\n或 init"]
E --> F["挂载其他 FS"]
F --> G["启动服务"]
G --> H["用户登录"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#FFF9C4,stroke:#F9A825,color:#333
style C fill:#B5EAD7,stroke:#80CBC4,color:#333
style D fill:#B5EAD7,stroke:#80CBC4,color:#333
style E fill:#B5EAD7,stroke:#80CBC4,color:#333
style F fill:#FFDAB9,stroke:#FFAB76,color:#333
style G fill:#FFB3C6,stroke:#F48FB1,color:#333
style H fill:#C7CEEA,stroke:#9FA8DA,color:#333
四、initramfs / initrd
4.1 为什么需要 initramfs?
graph TB
A["内核启动"] --> B{"rootfs 在哪?"}
B -->|"复杂设备\n如 LVM/RAID"| C["内核无法直接挂载"]
B -->|"简单设备"| D["内核直接挂载"]
C -.->|解决| E["initramfs\n早期根文件系统"]
E --> F["加载驱动\n挂载真 rootfs"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#FFF9C4,stroke:#F9A825,color:#333
style C fill:#FFB3C6,stroke:#F48FB1,color:#333
style D fill:#B5EAD7,stroke:#80CBC4,color:#333
style E fill:#FFDAB9,stroke:#FFAB76,color:#333
style F fill:#B5EAD7,stroke:#80CBC4,color:#333
graph TB
A["BIOS/UEFI"] --> B["GRUB"]
B --> C["real mode startup"]
C --> D["protected mode"]
D --> E["long mode (64-bit)"]
E --> F["start_kernel"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#FFDAB9,stroke:#FFAB76,color:#333
style D fill:#FFB3C6,stroke:#F48FB1,color:#333
style E fill:#B5EAD7,stroke:#80CBC4,color:#333
style F fill:#FFF9C4,stroke:#F9A825,color:#333
6.2 ARM 启动路径
graph TB
A["BootROM"] --> B["First-stage bootloader"]
B --> C["U-Boot / ATF"]
C --> D["设备树 + 内核"]
D --> E["start_kernel"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#FFDAB9,stroke:#FFAB76,color:#333
style D fill:#FFB3C6,stroke:#F48FB1,color:#333
style E fill:#FFF9C4,stroke:#F9A825,color:#333
graph TB
A["需要查找?"] --> B{"key 是什么?"}
B -->|"整数,密集"| C["数组"]
B -->|"整数,稀疏"| D["基数树"]
B -->|"任意 key"| E{"有序?"}
E -->|"是"| F["红黑树"]
E -->|"否"| G["哈希表"]
A -->|"只顺序遍历"| H["链表"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#FFF9C4,stroke:#F9A825,color:#333
style C fill:#E8D5F5,stroke:#CE93D8,color:#333
style D fill:#FFDAB9,stroke:#FFAB76,color:#333
style E fill:#FFF9C4,stroke:#F9A825,color:#333
style F fill:#FFB3C6,stroke:#F48FB1,color:#333
style G fill:#B5EAD7,stroke:#80CBC4,color:#333
style H fill:#FFF9C4,stroke:#F9A825,color:#333
graph TB
A["深入 Linux 内核架构"] --> B["进程管理\n调度/CFS/COW"]
A --> C["内存管理\nbuddy/slab/swap"]
A --> D["文件系统\nVFS/Ext4/page cache"]
A --> E["设备驱动\n字符/块/网络"]
A --> F["网络栈\nTCP/IP/netfilter"]
A --> G["同步 + 定时器\nRCU/jiffies"]
A --> H["中断 + 模块\nsoftirq/workqueue"]
A --> I["启动 + 调试\nBIOS→init"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#FFDAB9,stroke:#FFAB76,color:#333
style D fill:#FFB3C6,stroke:#F48FB1,color:#333
style E fill:#B5EAD7,stroke:#80CBC4,color:#333
style F fill:#FFF9C4,stroke:#F9A825,color:#333
style G fill:#C7CEEA,stroke:#9FA8DA,color:#333
style H fill:#E8D5F5,stroke:#CE93D8,color:#333
style I fill:#FFDAB9,stroke:#FFAB76,color:#333
十四、系列完结:Linux 内核学习路线
14.1 推荐路径
graph LR
A["C 语言基础"] --> B["UNIX 系统编程"]
B --> C["《Linux 内核设计与实现》"]
C --> D["《深入Linux内核架构》"]
D --> E["Linux 内核源码"]
E --> F["贡献内核"]
style A fill:#C7CEEA,stroke:#9FA8DA,color:#333
style B fill:#E8D5F5,stroke:#CE93D8,color:#333
style C fill:#FFDAB9,stroke:#FFAB76,color:#333
style D fill:#FFB3C6,stroke:#F48FB1,color:#333
style E fill:#B5EAD7,stroke:#80CBC4,color:#333
style F fill:#FFF9C4,stroke:#F9A825,color:#333