initrd 详解

徐小东

2023-01-03

是什么

initial ramdisk,它是一种将临时的根文件系统载入内存的方案,主要用于 Linux 的启动过程。也叫 early user space (早期用户空间),在 Linux kernel 2.5.46 引入。从出现至今存在以下两种方案:

initrd 在 Linux 引导过程中的作用

initrd 在 Linux 引导过程中的作用

存在原因

从根本上说:

其他次要的因素:

具体实现

制作工具

手动创建

mkdir --parents /usr/src/initramfs/{bin,dev,etc,lib,lib64,mnt/root,proc,root,sbin,sys}
cp --archive /dev/{null,console,tty,sda1} /usr/src/initramfs/dev/ # 设备节点
#!/bin/busybox sh

# Mount the /proc and /sys filesystems.
mount -t proc none /proc
mount -t sysfs none /sys

# Do your stuff here.
echo "This script just mounts and boots the rootfs, nothing else!"

# Mount the root filesystem.
mount -o ro /dev/sda1 /mnt/root

# Clean up.
umount /proc
umount /sys

# Boot the real thing.
exec switch_root /mnt/root /sbin/init
find . -print0 | cpio --null --create --verbose --format=newc | gzip --best > /boot/custom-initramfs.cpio.gz

配置 GRUB:

initrd custom-initramfs.cpio.gz # /boot/grub/grub.cfg

参考