为什么不只是普通文件而不是 initramfs 或 initrd ?
Linux 内核使用一种 RAM 磁盘在早期启动阶段访问模块。出于好奇,我想了解我有一个问题: 在此阶段,内核必须可以访问包含 initramfs/initrd 的文件。这意味着内核必须支持适当的文件系统。那么为什么 initramfs 的内容不能与普通文件位于同一文件系统中呢?
The linux kernel uses a kind of ram disk to access modules at an early boot stage. Out of curiosity I want to understand I have a question:
The file containing the initramfs/initrd must be accessible for the kernel at this stage. This means the kernel must have support for the appropriate filesystem. So why couldn't the content of the initramfs just be in the very same filesystem as plain files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎是您困惑的核心:读取 initramfs/initrd 的不是内核,而是引导加载程序!内核不需要知道如何访问 initramfs/initrd。这就是练习的全部要点!
引导加载程序将initramfs/initrd加载到内存中,并且有一个明确定义的通信协议,该协议允许引导加载程序告诉内核它加载initramfs/initrd的内存地址。
对于 initramfs,initramfs 映像也可以附加到内核映像。
This seems to be the heart of your confusion: it's not the kernel that reads the initramfs/initrd, it's the bootloader! The kernel does not need to know how to access the initramfs/initrd. That's the whole point of the exercise!
The bootloader loads the initramfs/initrd into memory, and there is a well-defined communication protocol which allows the bootloader to tell the kernel at what memory address it loaded the initramfs/initrd.
In the case of initramfs, the initramfs image(s) may also be appended to the kernel image.
可以的。启动不需要 initrd 映像,但大多数情况下都会使用它。然而,它的作用是允许您对文件系统进行不同的设置。例如,当使用非 ext2 文件系统(NFS?XFS?)、使用 RAID 或在不寻常的设备(USB 驱动器?带 unionfs 的 CDROM?)上托管根文件系统时,通常需要 initrd。
当然,可以通过将适当的文件系统或 RAID 驱动程序编译到内核中来避免这种情况,但这会使您的内核更大。
It can be. An initrd image is not needed to boot, but most use it. What this does, however is allows you to have different setups for your filesystems. For example, an initrd is usually required when using a non-ext2 filesystem (NFS? XFS?), when using RAID, or when hosting the root filesystem on an unusual device (USB drive? CDROM with unionfs?).
This can be avoided, of course, by compiling the proper filesystem or RAID drivers into your kernel, but this will make your kernel larger.
首先,引导加载程序(可能是 LILO 或 Grub)使用 BIOS 调用从引导驱动器读取内核映像。然后它将控制权传递给刚刚放置在系统 RAM 中的内核。
内核不使用 BIOS 调用来访问磁盘。当内核启动时,它会扫描系统中的设备,并为系统中的每个设备加载设备驱动程序模块,挂载根文件系统并启动第一个用户进程。
但是等等:内核是如何加载磁盘子系统的设备驱动程序模块的?这里有一个先有鸡还是先有蛋的问题,您需要访问磁盘才能获取允许您访问磁盘的设备驱动程序。
解决方案是将所有这些驱动程序放在初始 ramdisk (initrd) 中;该 ramdisk 映像由引导加载程序读取并与内核一起放置在 RAM 中,这为内核提供了访问根文件系统所需的驱动程序集。
如果没有这个,您将需要拥有访问内核内置的根文件系统所需的所有驱动程序。
First a bootloader (probably LILO or Grub) uses BIOS calls to read the kernel image from your boot drive. Then it passes control to the kernel it just placed in system RAM.
The kernel does not use BIOS calls to access disks. As the kernel is booting up, it scans the system for devices, and loads device driver modules for each device in the system, mounts the root filesystem and launches the first user process.
But wait: how did the kernel load the device driver module for the disk subsystem get loaded? There's a chicken-and-egg problem here, where you need to access the disk to get the device driver that allows you to access the disk.
The solution is to put all those drivers in the initial ramdisk (initrd); that ramdisk image is read by the bootloader and placed in RAM alongside the kernel, and that gives the kernel the set of drivers needed to get to the root filesystem.
Without this, you would need to have all of the drivers needed to access the root filesystem built in to the kernel.