linux - initrd 和 initramfs 的区别?

标签 linux filesystems kernel boot

据我所知,initrd 充当 block 设备,因此需要文件系统驱动程序(例如 ext2)。内核必须至少有一个内置模块用于检测 initrd 的文件系统。在这篇文章中,Introducing initramfs, a new model for initial RAM disks ,是这样写的:

But ramdisks actually waste even more memory due to caching. Linux is designed to cache all files and directory entries read from or written to block devices, so Linux copies data to and from the ramdisk into the "page cache" (for file data), and the "dentry cache" (for directory entries). The downside of the ramdisk pretending to be a block device is it gets treated like a block device.

什么是page cachedentry cache?在该段落中,是否意味着数据被复制,因为 ramdisk 被视为 block 设备,因此所有数据都被缓存了?

相比之下,ramfs:

A few years ago, Linus Torvalds had a neat idea: what if Linux's cache could be mounted like a filesystem? Just keep the files in cache and never get rid of them until they're deleted or the system reboots? Linus wrote a tiny wrapper around the cache called "ramfs", and other kernel developers created an improved version called "tmpfs" (which can write the data to swap space, and limit the size of a given mount point so it fills up before consuming all available memory). Initramfs is an instance of tmpfs.

These ram based filesystems automatically grow or shrink to fit the size of the data they contain. Adding files to a ramfs (or extending existing files) automatically allocates more memory, and deleting or truncating files frees that memory. There's no duplication between block device and cache, because there's no block device. The copy in the cache is the only copy of the data. Best of all, this isn't new code but a new application for the existing Linux caching code, which means it adds almost no size, is very simple, and is based on extremely well tested infrastructure.

总而言之,ramfs 只是文件打开并加载到内存中,不是吗?

initrdramfs 都是在编译时压缩的,但不同的是,initrd 是一个 block 设备,解压后由内核在启动时,而 ramfs 通过 cpio 解压到内存中。我对么?还是 ramfs 是一个非常小的文件系统?

最后,直到今天,initrd 镜像仍然显示在最新的内核中。然而,那个 initrd 真的是今天使用的 ramfs 并且这个名字只是为了历史目的吗?

最佳答案

我认为你是对的。

如果您按照启动时所需的步骤进行操作,很容易看出区别:

initrd

  • 创建了一个 ramdev block 设备。它是基于 ram 的 block 设备,即使用内存而不是物理磁盘的模拟硬盘。
  • initrd 文件被读取并解压缩到设备中,就像您执行 zcat initrd | dd of=/dev/ram0 或类似的东西。
  • initrd 包含文件系统的镜像,因此现在您可以照常挂载文件系统:mount/dev/ram0/root。自然,文件系统需要驱动程序,因此如果您使用 ext2,则必须在内核中编译 ext2 驱动程序。
  • 完成!

initramfs

  • 挂载了一个tmpfs:mount -t tmpfs nodev/root。 tmpfs 不需要驱动程序,它总是在内核上。无需设备,无需额外的驱动程序。
  • initramfs 直接解压缩到这个新文件系统中:zcat initramfs | cpio -i,或类似的。
  • 完成!

是的,尽管它是一个 initramfs,但在很多地方它仍然被称为 initrd,尤其是在引导加载程序中,因为它只是一个 BLOB。操作系统启动时会产生差异。

关于linux - initrd 和 initramfs 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10603104/

相关文章:

linux - perl有没有办法运行多个linux命令并在它们之间保存环境变量

c++ - 如何使用版本名称将二进制文件与库链接

linux - 如何初始化文件系统?

linux - 如何在 Linux 中停止/禁用 IPI(进程间中断)?

linux - 实时 Linux 扩展已经过时了吗?

linux - bitbake 期间出现“ fatal error : linux/compiler-gcc5. h:没有此类文件或目录”

linux - 如何在使用 sed 时将 "/"保留在字符串中

linux - 如何卸载内核代码中的文件系统

javascript - 尝试在 Android 中使用 ng-cordova 移动文件时没有任何反应

c - 基本内存地址混淆