initrd和initramfs及根文件系统切换

本文详细对比了initrd和initramfs两种Linux启动时使用的临时根文件系统的区别,包括内存管理、制作过程和驱动支持等方面,并介绍了它们分别使用pivot_root和switch_root进行根文件系统切换的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.initrd和initramfs区别

1. initramfs是对initrd的升级;

2. initrd将根文件系统包和Image分开,而initramfs将根文件系统与内核打包在一起;

3. initrd文件系统占用的内存得不到释放,而initramfs可以释放内存;

4. initrd是基于ramdisk实现,initramfs是基于ramfs实现;

5. ramdisk是把一块内存(ram)当做磁盘(disk)块设备去挂载,ramfs直接在ram上挂载文件系统;

6. initrd要比initramfs麻烦很多,比如内核中驱动的支持和制作压缩的步骤;

7. initrd和initramfs制作过程参考之前的两篇博客

initramfs制作步骤:
initramfs
initrd制作步骤:
initrd制作

二.switch_root和pivot_root

1. initrd切换根文件系统使用pivot_root,initramfs使用switch_root;

2. 以下是示例:

(1)initrd切换根文件系统

#!/bin/sh
echo "exec initrd init"
echo "mounting proc and sys"

mount -t proc proc /proc
mount -t sysfs sysfs /sys

echo "detect and export hardware info"
mdev -s


echo "Mount real rootfs to /mnt ..."

ubiattach /dev/ubi_ctrl -m 1
mount -t ubifs ubi0:rootfs /mnt

echo "Switch to real rootfs..."

mount --move /dev  /mnt/dev/
mount --move /proc /mnt/proc/
mount --move /sys  /mnt/sys/ 

cd /mnt
pivot_root . tmp/
exec chroot . /sbin/init <dev/console >dev/console 2>&1

 (2)initramfs切换根文件系统

#!/bin/sh
echo "exec initramfs init"
echo "mounting proc and sys"

mount -t proc proc /proc
mount -t sysfs sysfs /sys

echo "detect and export hardware info"
mdev -s

echo "Mount real rootfs to /mnt ..."
mount -t ext4 /dev/mmcblk1p2 /mnt

echo "Switch to real rootfs..."

exec switch_root /mnt /sbin/init

3. 切换脚本应在PID为1的进程中运行,可将脚本命名为第一个运行的程序linuxrc;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值