Inside /boot/efi & initramfs: Standard ESP File Layout, systemd-boot BLS, initrd vs. initramfs & CPIO

In From Bare NVMe to Login Prompt, we walked through how a blank NVMe SSD is partitioned with GPT, formatted into vfat and ext4, attached to the directory tree via mount, and booted into systemd. Now let’s zoom into the standard Linux UEFI boot partition and early-boot memory filesystem to answer three classic questions: What exact files sit inside /boot/efi on a standard systemd-boot system? What do initrd and initramfs actually do—and why are their names used interchangeably? And what is the CPIO format inside an initramfs image?

在上一篇 From Bare NVMe to Login Prompt 中,我们梳理了空白 NVMe 从切 GPT partition、format、mount 到 UEFI 开机进入 systemd 的整体脉络。这一篇我们专门拆解标准 Linux 的 UEFI 启动分区与早期内存文件系统,把三个最常被问到底层细节讲透:标准 systemd-boot 机器的 /boot/efi 目录下到底放了哪几个文件?经常混着叫的 initrd 和 initramfs 到底是干什么用的、有什么区别?里面的 CPIO 归档又是什么结构?


1. Complete File Anatomy of /boot/efi Under Standard systemd-boot (/boot/efi 标准目录与文件全拆解)

Under the UEFI Specification, motherboard firmware reads bootloaders from an EFI System Partition (ESP)—a small GPT partition tagged with Type GUID C12A7328-F81F-11D2-BA4B-00A0C93EC93B and formatted as FAT32 (vfat). Inside a running Linux OS, this partition is typically mounted at /boot/efi; to power-on UEFI firmware, however, this partition’s root directory is simply /.

按照 UEFI 标准规范,主板固件从 EFI System Partition(ESP) 读取引导程序——这是一个打有专用类型 GUID(C12A7328-F81F-11D2-BA4B-00A0C93EC93B)并 format 为 FAT32(vfat)的小分区。进入 Linux 系统后,我们通常把它 mount 在 /boot/efi;而在刚上电的 UEFI 固件眼里,这个分区的根目录本身就是 /。

On a standard Linux installation using systemd-boot and the upstream Boot Loader Specification (BLS Type #1), the entire /boot/efi partition needs only 6 files. Click any file in the interactive tree below to see who reads it, what goes inside, and why it is needed:

在使用标准 systemd-boot 与上游 Boot Loader Specification(BLS Type #1) 规范的 Linux 系统上,整个 /boot/efi 分区核心只有 6 个文件。点击下方交互式目录树中的任意文件,即可查看它的读取方、真实内容与具体职责:

Standard /boot/efi File Explorer · Click any file (点击文件查看详情) FAT32 ESP = “/” in UEFI
/boot/efi/ (FAT32 ESP Root)
├── EFI/BOOT/
    └── BOOTAA64.EFI[1]
├── EFI/systemd/
    └── systemd-bootaa64.efi[2]
├── loader/
    ├── loader.conf[3]
    └── entries/
        └── linux-6.12.conf[4]
├── vmlinuz-6.12.0[5]
└── initramfs-6.12.0.img[6]

2. initrd vs. initramfs: What Are They For & How Do They Differ? (initrd 与 initramfs 的用途与区别)

2.1 Why Does Linux Need an Early Memory Rootfs at All?

If we kept every storage driver (NVMe, SATA, SCSI, VirtIO, RAID, LVM, LUKS encryption, ext4, XFS, Btrfs, network filesystems) statically compiled into vmlinuz, the kernel binary would become bloated and rigid—and still couldn’t prompt for a LUKS passphrase or assemble complex multi-disk arrays that require user-space tools. Yet those modular .ko drivers and user-space tools live inside /lib/modules and /usr/sbin on the very root disk we haven’t mounted yet!

如果把世上所有存储控制器驱动(NVMe、SATA、VirtIO、软 RAID、LVM)、文件系统(ext4、XFS、Btrfs)和磁盘解密逻辑全静态编死进 vmlinuz,内核会变得极其臃肿,而且依然没法完成输入 LUKS 密码、执行 fsck 修复或按 UUID 异步组装复杂存储栈等用户态工作。然而,这些模块化 .ko 驱动和工具恰恰躺在还没挂载上的根分区 /lib/modules 里——这就是经典的启动期“鸡生蛋”死锁。

Linux solves this chicken-and-egg bootstrap problem by loading a tiny, self-contained temporary root filesystem into RAM alongside the kernel. Step through the 5 stages below to see how this temporary memory environment prepares the real disk and cleanly erases itself:

Linux 的标准解法,就是由 bootloader 在加载内核的同时,顺手把一个包含必要驱动和挂载脚本的微型临时根文件系统读进内存。点击下方 5 个步骤,查看它从启动、挂盘到自我销毁让位给真正磁盘 / 的完整生命周期:

initramfs → switch_root Lifecycle · Click any stage (点击阶段查看) Early Userspace (RAM) → Real Disk “/”

2.2 Why Are Both Names (“initrd” and “initramfs”) Still Used?

Beginners often wonder why documentation talks about building an initramfs while bootloader configs (systemd-boot, GRUB) still write initrd /initramfs-6.12.0.img. They represent two generations of the same concept—where the old name survived as the configuration interface while the new mechanism won under the hood:

很多同学会困惑:为什么教程里都在讲生成 initramfs,但打开 systemd-boot 或 GRUB 配置文件写的指令却依然叫 initrd /initramfs-6.12.0.img?其实它们是同一需求的两代实现——老一代的名字作为配置接口保留了下来,而底层的运行机制早已全面换成了新一代:

TermFull Name & EraUnderlying Mechanism / 底层实现差异Status Today / 现状
initrdInitial RAM Disk
(Linux 2.4 legacy)
Simulated a fixed-size virtual block device (/dev/ram0) in RAM that had to be formatted with a real disk filesystem (ext2) and mounted—duplicating data in both the fake block device and the page cache.
在内存中硬模拟一块固定大小的虚拟块设备 /dev/ram0,里面还要格式化成 ext2 再挂载,造成双份缓存浪费且大小死板。
Mechanism retired, but the keyword initrd remains the universal bootloader config directive & UEFI handoff protocol name.
底层块设备机制已淘汰,但 initrd 作为配置指令名保留至今。
initramfsInitial RAM Filesystem
(Linux 2.6+ standard)
No fake block device or ext2 overhead: the kernel simply extracts a compressed CPIO archive directly into a dynamic ramfs/tmpfs tree in the page cache and executes /init.
无需模拟硬盘或格式化文件系统:内核直接把压缩的 CPIO 归档解压进动态伸缩的内存目录树(tmpfs),直接运行里面的 /init。
100% of modern Linux distros (Ubuntu/Debian initramfs-tools, Fedora/RHEL dracut, Arch mkinitcpio) build and boot initramfs.
现代所有主流 Linux 发行版的标准机制。

3. What Is CPIO? Inside the Archive Format of initramfs (CPIO 归档格式解析)

Even when a distro names the file initrd.img or initramfs-6.12.0.img, it is not an ISO or disk partition image—it is a compressed CPIO archive (specifically the SVR4 newc format, whose entries always start with the 6-byte ASCII magic string 070701).

不管发行版把启动镜像起名叫 initrd.img 还是 initramfs-6.12.0.img,它本质上都不是磁盘分区镜像,而是一个经过 zstd 或 gzip 压缩的 CPIO(Copy In and Copy Out)归档包——具体采用的是 SVR4 newc 格式(每个条目都以 6 字节 ASCII 魔数 070701 开头)。

# Layout of an uncompressed SVR4 "newc" CPIO stream (4-byte aligned):
# 未压缩 SVR4 "newc" CPIO 字节流的极简顺序结构(按 4 字节对齐):

+------------------------------------------------------------------+
| Entry 1: 110-byte ASCII hex header ("070701", mode, uid, size..) |
|          + NUL-padded filename (e.g. "init\0\0")                 |
|          + raw file contents (padded with NULs to 4-byte align)  |
+------------------------------------------------------------------+
| Entry 2: 110-byte ASCII hex header + "bin/sh\0..." + file bytes  |
+------------------------------------------------------------------+
| ... more files, directories, symlinks, device nodes ...          |
+------------------------------------------------------------------+
| End Marker: 110-byte header + filename "TRAILER!!!"              |
+------------------------------------------------------------------+

Why did the Linux kernel choose newc CPIO instead of tar?

为什么 Linux 内核钦定了 newc CPIO 而不是大家更熟悉的 tar?

  • Tiny in-kernel parser: Every header field (inode, permissions, UID/GID, file size, device major/minor, filename length) is fixed-width ASCII hex, allowing init/initramfs.c inside the kernel to stream-extract directories, symlinks, and character/block device nodes in just a couple hundred lines of C.
    内核解析器极度精简:所有元数据(权限、大小、主次设备号、文件名长度)都是定长 ASCII 十六进制字符,内核里的 init/initramfs.c 只需几百行 C 代码就能流式还原目录、软链接和设备节点。
  • Native archive concatenation (stacking): Because each CPIO stream simply ends with TRAILER!!!, the kernel can read multiple concatenated CPIO archives back-to-back in a single buffer! Modern distros use this every day to prepend an uncompressed cpu-microcode.cpio right in front of a zstd-compressed initramfs.cpio.zst—the kernel updates CPU microcode at the earliest possible moment and immediately continues unpacking the main initramfs into the same RAM rootfs.
    原生支持多包拼接叠加:由于每个 CPIO 流只是以 TRAILER!!! 收尾,Linux 内核天生支持把多个(甚至不同压缩格式的)CPIO 归档直接首尾拼接或依次传入!现代发行版普遍利用这一标准特性,把未压缩的 cpu-microcode.cpio 拼在 zstd 压缩的主 initramfs 前面,让内核一上来先刷入 CPU 微码补丁,紧接着无缝解压主 initramfs。

4. Practical Commands to Inspect /boot/efi & initramfs (常用查看与拆包命令速查)

You can inspect every piece of a live machine’s UEFI boot tree and initramfs CPIO archive using standard Linux utilities:

在任何标准 Linux 机器上,可以用下面几条命令直接查看 UEFI 启动配置与拆解 initramfs 内部结构:

# 1. Inspect active UEFI bootloader, ESP PARTUUID, and all BLS entries
# 1. 查看当前生效的 UEFI 引导器、ESP 分区信息与全部 BLS 启动项
bootctl status

# 2. Verify that vmlinuz has CONFIG_EFI_STUB=y (valid UEFI PE32+ executable)
# 2. 验证内核镜像是否开启了 CONFIG_EFI_STUB(可直接作为 EFI 程序启动)
file /boot/efi/vmlinuz-*

# 3. List files inside a compressed initramfs image (Debian/Ubuntu or Fedora/RHEL)
# 3. 不解压直接列出压缩 initramfs 归档内的全部文件清单
lsinitramfs /boot/initrd.img-$(uname -r)   # Debian / Ubuntu
lsinitrd /boot/initramfs-$(uname -r).img   # Fedora / RHEL / Rocky

# 4. Manually unpack an initramfs CPIO archive into /tmp/inspect-initramfs
# 4. 手工把一个 zstd 压缩的 initramfs CPIO 包解开到临时目录查看里面的 /init 脚本
mkdir -p /tmp/inspect-initramfs && cd /tmp/inspect-initramfs
zstdcat /boot/efi/initramfs-6.12.0.img | cpio -idmv

# 5. Repackage a directory into a standard SVR4 "newc" CPIO initramfs
# 5. 将目录重新打包成符合内核标准的 SVR4 "newc" (070701) CPIO 镜像
find . -print0 | cpio --null -o -H newc --owner 0:0 | zstd -19 > /tmp/new-initramfs.img

Leave a Reply

Your email address will not be published. Required fields are marked *