Linux initrd 和 initramfs 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10603104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:20:30  来源:igfitidea点击:

The difference between initrd and initramfs?

linuxfilesystemskernelboot

提问by Amumu

As far as I know, initrdacts as a block device, thus requiring a filesystem driver (such as ext2). The kernel must have at least one built-in module for detecting filesystem of initrd. In this article, Introducing initramfs, a new model for initial RAM disks, it is written that:

据我所知,initrd充当块设备,因此需要文件系统驱动程序(例如ext2)。内核必须至少有一个内置模块来检测initrd. 在这篇文章中,介绍了initramfs,一种用于初始RAM磁盘的新模型,它写道:

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.

但是由于缓存,虚拟磁盘实际上浪费了更多内存。Linux 旨在缓存从块设备读取或写入块设备的所有文件和目录条目,因此 Linux 将数据复制到 ramdisk 和从 ramdisk 复制到“页面缓存”(用于文件数据)和“dentry 缓存”(用于目录条目) . 假装是块设备的 ramdisk 的缺点是它被视为块设备。

What's page cacheand dentry cache? In the paragraph, does it mean the data got duplicated because ramdiskis treated as a block device, thus all the data is cached?

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

In constrast, ramfs:

相比之下,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.

几年前,Linus Torvalds 有一个巧妙的想法:如果 Linux 的缓存可以像文件系统一样挂载会怎样?只是将文件保存在缓存中,并且在它们被删除或系统重新启动之前永远不要删除它们?Linus 围绕缓存编写了一个名为“ramfs”的小包装器,其他内核开发人员创建了一个名为“tmpfs”的改进版本(它可以将数据写入交换空间,并限制给定挂载点的大小,以便在消耗之前填满所有可用内存)。Initramfs 是 tmpfs 的一个实例。

这些基于 ram 的文件系统会自动增长或缩小以适应它们包含的数据大小。将文件添加到 ramfs(或扩展现有文件)会自动分配更多内存,删除或截断文件会释放该内存。块设备和缓存之间没有重复,因为没有块设备。缓存中的副本是数据的唯一副本。最重要的是,这不是新代码,而是现有 Linux 缓存代码的新应用程序,这意味着它几乎没有增加大小,非常简单,并且基于经过充分测试的基础架构。

In sum, ramfsis just file opened and loaded into memory, isn't it?

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

Both initrdand ramfsare zipped at compile time, but the difference is, initrdis a block device unpacked to be mounted by the kernel at booting, while ramfsis unpacked via cpio into memory. Am I correct? Or is ramfsa very minimal file system?

二者initrdramfs在编译时拉链,但不同的是,initrd是块设备解压到通过在启动内核被安装,而ramfs通过的cpio是解压到存储器中。我对么?或者是ramfs一个非常小的文件系统?

Finally, up until this day, the initrdimage is still presented in the latest kernel. However, is that initrdactually the ramfsused today and the name is just for historical purpose?

最后,直到今天,该initrd图像仍然呈现在最新的内核中。然而,这initrd真的是ramfs今天使用的吗?这个名字只是为了历史目的吗?

采纳答案by Jan Hudec

Dentry (and inode) cache

dentry(和 inode)缓存

Filesystem subsystem in Linux has three layers. The VFS (virtual filesystem), which implements the system calls interface and handles crossing mountpoints and default permission and limits checks. Below it are the drivers for individual filesystems and those in turn interface to drivers for block devices (disks, memory cards, etc.; network interfaces are exception).

Linux 中的文件系统子系统分为三层。VFS(虚拟文件系统),它实现系统调用接口并处理交叉挂载点和默认权限和限制检查。在它下面是单个文件系统的驱动程序,而这些驱动程序又与块设备(磁盘、存储卡等;网络接口除外)的驱动程序接口。

The interface between VFS and filesystem are several classes (it's plain C, so structures containing pointers to functions and such, but it's object-oriented interface conceptually). The main three classes are inode, which describes any object (file or directory) in a filesystem, dentry, which describes entry in a directory and file, which describes file open by a process. When mounted, the filesystem driver creates inodeand dentryfor it's root and the other ones are created on demand when process wants to access a file and eventually expired. That's a dentry and inode cache.

VFS 和文件系统之间的接口是几个类(它是普通的 C,所以结构包含指向函数等的指针,但它在概念上是面向对象的接口)。主要的三个类是inode,描述文件系统中的任何对象(文件或目录)dentry,描述目录中的条目file,描述进程打开的文件。挂载后,文件系统驱动程序创建inodedentry为其根目录,其他驱动程序在进程想要访问文件并最终过期时按需创建。那是一个 dentry 和 inode 缓存。

Yes, it does mean that for every open file and any directory down to root there has to be inodeand dentrystructures allocated in kernel memory representing it.

是的,这确实意味着每一个打开的文件和任何目录下,以根,必须有inodedentry结构分配的内核内存代表它。

Page cache

页面缓存

In Linux, each memory page that contains userland data is represented by unified pagestructure. This might mark the page as either anonymous (might be swapped to swap space if available) or associate it with inodeon some filesystem (might be written back to and re-read from the filesystem) and it can be part of any number of memory maps, i.e. visible in address space of some process. The sum of all pages currently loaded in memory is the page cache.

在 Linux 中,每个包含用户态数据的内存页都用统一的page结构表示。这可能会将页面标记为匿名(如果可用,可能会被交换到交换空间)或将其与inode某个文件系统相关联(可能会被写回文件系统并从文件系统中重新读取),并且它可以是任意数量的内存映射的一部分,即在某些进程的地址空间中可见。当前加载到内存中的所有页面的总和就是页面缓存。

The pages are used to implement mmap interface and while regular read and write system calls can be implemented by the filesystem by other means, majority of interfaces uses generic function that also uses pages. There are generic functions, that when file read is requested allocate pages and call the filesystem to fill them in, one by one. For block-device-based filesystem, it just calculates appropriate addresses and delegates this filling to the block device driver.

页面用于实现 mmap 接口,虽然文件系统可以通过其他方式实现常规的读写系统调用,但大多数接口使用也使用页面的通用函数。有一些通用函数,当请求文件读取时,分配页面并调用文件系统来一一填充它们。对于基于块设备的文件系统,它只计算适当的地址并将此填充委托给块设备驱动程序。

ramdev (ramdisk)

ramdev(虚拟磁盘)

Ramdev is regular block device. This allows layering any filesystem on top of it, but it is restricted by the block device interface. And that has just methods to fill in a page allocated by the caller and write it back. That's exactly what is needed for real block devices like disks, memory cards, USB mass storage and such, but for ramdisk it means, that the data exist in memory twice, once in the memory of the ramdev and once in the memory allocated by the caller.

Ramdev 是常规块设备。这允许在其上分层任何文件系统,但它受到块设备接口的限制。这只有填充调用者分配的页面并将其写回的方法。这正是真正的块设备(如磁盘、存储卡、USB 大容量存储器等)所需要的,但对于 ramdisk 而言,这意味着数据在内存中存在两次,一次在 ramdev 的内存中,一次在由 ramdev 分配的内存中。呼叫者。

This is the old way of implementing initrd. From times when initrd was rare and exotic occurence.

这是旧的实施方式initrd。从那时起 initrd 是罕见且奇特的事件。

tmpfs

tmpfs

Tmpfs is different. It's a dummy filesystem. The methods it provides to VFS are the absolute bare minimum to make it work (as such it's excellent documentation of what the inode, dentry and file methods should do). Files only exist if there is corresponding inode and dentry in the inode cache, created when the file is created and never expired unless the file is deleted. The pages are associated to files when data is written and otherwise behave as anonymous ones (data may be stored to swap, pagestructures remain in use as long as the file exists).

Tmpfs 是不同的。这是一个虚拟文件系统。它提供给 VFS 的方法是使其工作的绝对最低限度(因此它是 inode、dentry 和文件方法应该做什么的极好文档)。文件只有在 inode 缓存中有对应的 inode 和 dentry 时才存在,在文件创建时创建,除非文件被删除,否则永不过期。这些页面在写入数据时与文件相关联,否则表现为匿名页面(数据可以存储交换,page只要文件存在,结构就会一直使用)。

This means there are no extra copies of the data in memory and the whole thing is a lot simpler and due to that slightly faster too. It simply uses the data structures, that serve as cache for any other filesystem, as it's primary storage.

这意味着内存中没有额外的数据副本,整个过程要简单得多,因此速度也稍快一些。它只是使用数据结构,作为任何其他文件系统的缓存,因为它是主存储。

This is the new way of implementing initrd(initramfs, but the image is still called just initrd).

这是实现initrd( initramfs,但图像仍称为 just initrd)的新方法。

It is also the way of implementing "posix shared memory" (which simply means tmpfs is mounted on /dev/shmand applications are free to create files there and mmap them; simple and efficient) and recently even /tmpand /run(or /var/run) often have tmpfs mounted especially on notebooks to keep disks from having to spin up or avoid some wear in case of SSDs.

这也是实现“posix共享内存”的方式(这只是意味着安装了tmpfs /dev/shm,应用程序可以自由地在那里创建文件并映射它们;简单而高效),最近甚至/tmp/run(或/var/run)经常在笔记本电脑上安装tmpfs防止磁盘必须旋转或避免在 SSD 的情况下出现磨损。

回答by rodrigo

I think you are right in all.

我认为你是对的。

The difference is easy to see if you follow the steps needed when booting:

如果您在启动时遵循所需的步骤,则很容易看出差异:

initrd

initrd

  • A ramdevblock device is created. It is a ram-based block device, that is a simulated hard disk that uses memory instead of physical disks.
  • The initrdfile is read and unzipped into the device, as if you did zcat initrd | dd of=/dev/ram0or something similar.
  • The initrdcontains an image of a filesystem, so now you can mount the filesystem as usual: mount /dev/ram0 /root. Naturally, filesystems need a driver, so if you use ext2, the ext2 driver has to be compiled in-kernel.
  • Done!
  • ramdev块设备被创建。它是一种基于内存的块设备,即使用内存代替物理磁盘的模拟硬盘。
  • initrd文件被读取并解压缩到设备中,就像您这样做zcat initrd | dd of=/dev/ram0或类似的东西一样。
  • initrd包含文件系统的图像,所以现在你可以挂载文件系统和往常一样:mount /dev/ram0 /root。自然,文件系统需要一个驱动程序,因此如果您使用 ext2,则必须在内核中编译 ext2 驱动程序。
  • 完毕!

initramfs

initramfs

  • A tmpfsis mounted: mount -t tmpfs nodev /root. The tmpfs doesn't need a driver, it is always on-kernel. No device needed, no additional drivers.
  • The initramfsis uncompressed directly into this new filesystem: zcat initramfs | cpio -i, or similar.
  • Done!
  • Atmpfs已安装:mount -t tmpfs nodev /root。tmpfs 不需要驱动程序,它始终在内核上。不需要设备,不需要额外的驱动程序。
  • initramfs直接解压缩到这个新文件系统中:zcat initramfs | cpio -i或类似的。
  • 完毕!

And yes, it is still called initrdin many places although it is a initramfs, particularly in boot loaders, as for them it is just a BLOB. The difference is made by the OS when it boots.

是的,initrd虽然它是一个initramfs. 差异是由操作系统在启动时产生的。

回答by humanityANDpeace

To add another noteworthy difference between initrdand initramfsnot mentioned in the excellent answer above.

在上面的优秀答案中添加initrdinitramfs未提及的另一个值得注意的区别。

  • With initrdthe kernel by default hands over to userspace pid 1at /sbin/init
  • Newer initramfs however changes things up and executes pid 1at /init
  • 随着initrd内核默认移交到用户空间pid 1/sbin/init
  • 然而,较新的 initramfs 改变了一切并pid 1/init

as it could become a pitfall (see https://unix.stackexchange.com/a/147688/24394)

因为它可能成为一个陷阱(参见https://unix.stackexchange.com/a/147688/24394