initrd和initramfs的区别? [英] The difference between initrd and initramfs?

查看:240
本文介绍了initrd和initramfs的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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


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



在对比中, ramfs

lockquote
几年前, Linus Torvalds有一个很好的想法:如果Linux的缓存
可以像文件系统一样安装?只要将文件保存在缓存中,
永远不会摆脱它们,直到它们被删除或系统重新启动?
Linus在名为ramfs的缓存中写了一个很小的包装,其他
内核开发者创建了一个名为tmpfs的改进版本(
可以将数据写入交换空间,一个给定的挂载大小
点,所以它在消耗所有可用内存之前填满)。 Initramfs
是tmpfs的一个实例。



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

总之, ramfs 只是文件打开和加载进入记忆,不是吗?


$ b

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



最后,直到今天, initrd 映像仍然在最新的内核中提供。但是, initrd 实际上是现在使用的 ramfs ,这个名字只是为了历史的目的吗?

解决方案

Dentry(和inode)缓存



Linux中的文件系统子系统有三层。 VFS(虚拟文件系统),实现系统调用接口并处理交叉挂载点和默认权限和限制检查。下面是单个文件系统的驱动程序,以及接口驱动程序的块设备(磁盘,内存卡等;网络接口是例外)。

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



是的,这意味着对于每个打开的文件和任何到根目录的目录,必须有 inode dentry 在内核内存中分配的结构代表它。



页面缓存



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

这些页面用于实现mmap接口,而常规的读写系统调用可以由文件系统通过其他手段,大多数接口使用通用功能,也使用页面。有一些泛型函数,当请求文件读取时,分配页面并调用文件系统来逐个填充它们。对于基于块设备的文件系统,它只是计算适当的地址并将这个填充委托给块设备驱动程序。


$ b

ramdev(ramdisk)



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

这是执行 initrd 的旧方法。从initrd是罕见的和异乎寻常的发生。

tmpfs



Tmpfs是不同的。这是一个虚拟文件系统。它提供给VFS的方法是绝对的最低限度使它工作(因为它是什么inode,dentry和文件方法应该做的很好的文档)。文件只有在inode缓存中有相应的inode和dentry时才存在,当文件被创建时创建,并且除非文件被删除,否则永不过期。当数据被写入时页面与文件相关联,否则就像匿名文件一样(数据可能被存储到交换中,只要文件存在,页面结构仍然在使用中)。这意味着在内存中没有额外的数据副本,而且整个过程要简单得多,而且速度也稍快一些。它只是使用数据结构,作为任何其他文件系统的缓存,因为它是主存储。



这是实现 initrd的新方法 initramfs ,但是图片仍然被称为 initrd )。



这也是实现posix共享内存(这意味着将tmpfs加载到 / dev / shm 和应用程序可以在那里创建文件并进行mmap;简单而高效),最近甚至可以使用 / tmp / run (或 / var / run )经常会在笔记本电脑上安装tmpfs,以防止磁盘旋转或避免SSD出现磨损。

As far as I know, initrd acts 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:

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.

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

In constrast, 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.

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

Both initrd and ramfs are zipped at compile time, but the difference is, initrd is a block device unpacked to be mounted by the kernel at booting, while ramfs is unpacked via cpio into memory. Am I correct? Or is ramfs a very minimal file system?

Finally, up until this day, the initrd image is still presented in the latest kernel. However, is that initrd actually the ramfs used today and the name is just for historical purpose?

解决方案

Dentry (and inode) cache

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).

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 inode and dentry for 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.

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

Page cache

In Linux, each memory page that contains userland data is represented by unified page structure. This might mark the page as either anonymous (might be swapped to swap space if available) or associate it with inode on 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.

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.

ramdev (ramdisk)

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.

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

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, page structures remain in use as long as the file exists).

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).

It is also the way of implementing "posix shared memory" (which simply means tmpfs is mounted on /dev/shm and applications are free to create files there and mmap them; simple and efficient) and recently even /tmp and /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.

这篇关于initrd和initramfs的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆