一.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;