linux - initrd 和 initramfs 的区别?

据我所知,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。操作系统启动时会产生差异。

https://stackoverflow.com/questions/10603104/

相关文章:

python - 在单行中打印不带括号的列表

regex - sed - 注释与特定字符串匹配且尚未注释掉的行

python - 比较两个 DataFrame 并并排输出它们的差异

linux - 使用 Wget 发布请求?

linux - 在 Linux 中注册文件扩展名/mime 类型

python - 以 Unix 时间戳格式获取当前 GMT 时间的最简单方法是什么?

linux - 如何使用调试版的 libc

python - 如何找到列表交集?

python - 如何使用列的格式字符串显示 float 的pandas DataFrame?

c++ - .o 文件与 .a 文件