为什么根文件系统被加载到ramdisk中?
我正在研究Linux的启动过程。我遇到过这句话“RAM 比软盘快几个数量级,因此 ramdisk 的系统操作速度很快”
内核无论如何都会将根文件系统加载到 RAM 中以执行它。所以我的问题是,如果内核将根文件系统加载到 RAM 中,为什么我们需要 ramdisk 来加载根文件系统?
I am studying the boot process in Linux. I came across this sentence "RAM is several orders of magnitude faster than a floppy disk, so system operation is fast from a ramdisk"
The kernel will anyway load the root filesystem in RAM for executing it. So my question why do we need a ramdisk for loading the root filesystem, if the kernel loads the root file system into RAM ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SUSE Linux 的文档很好地解释了原因Linux 使用 RAMDisk 启动:
这可以防止潜在的先有鸡还是先有蛋的情况,即在可以访问根文件系统所在的设备之前无法加载根文件系统,但在根文件系统加载之前也无法访问该设备 已加载:
当然,RAMDisk 并不是启动过程所必需的。例如,您可以编译一个内核,其中包含启动时加载的所有必需的硬件驱动程序和模块。但显然这对大多数人来说工作量太大,而 RAMDisk 被证明是一个更简单、更具可扩展性的解决方案。
The documentation for SUSE Linux provides a good explanation of why Linux is booted with a RAMDisk:
This prevents a potential chicken-or-egg situation where the root file system cannot be loaded until the device on which it is located can be accessed, but that device can't be accessed until the root file system has been loaded:
Of course, a RAMDisk is not strictly necessary for the boot process to take place. For example, you could compile a kernel that contained all necessary hardware drivers and modules to be loaded at startup. But apparently this is too much work for most people, and the RAMDisk proved to be a simpler, more scalable solution.
大多数Linux发行版在启动时使用ramfs(initramfs)的原因是因为它的内容可以包含在内核文件中,或者由引导加载程序提供。因此,它们在启动时立即可用,而无需内核从某个地方加载它们。
这允许内核运行用户空间程序,例如配置设备、加载模块、设置包含所有文件系统的漂亮 RAID 阵列,甚至要求用户输入其加密根文件系统的密码。
完成此配置后,仅从(现已配置且可用)根文件系统调用 exec()s /sbin/init 的第一个脚本。
我见过很多系统,其中磁盘控制器和 rootfs 的驱动程序本身是通过 initramfs 中的模块加载的,而不是包含在内核映像中。
您并不严格需要 initramfs 来启动 - 如果您的内核映像包含访问 rootfs 所需的所有驱动程序,并且您不需要任何特殊配置或用户输入(例如 RAID 阵列或加密文件系统)来启动挂载它后,通常可以直接从rootfs启动/sbin/init。
另请参阅:
http://www.kernel.org/doc/ Documentation/filesystems/ramfs-rootfs-initramfs.txt
http://www.kernel.org/doc/Documentation/initrd.txt" kernel.org/doc/Documentation/initrd.txt
作为旁注,某些系统(救援磁盘、嵌入式等)可能会使用 ramfs 作为根文件系统,而实际的根文件系统位于可能是已删除或不可写(CD、闪存 MTD 等)。
The reason that most Linux distributions use a ramfs (initramfs) when booting, is because its contents can be included in the kernel file, or provided by the bootloader. They are therefore available immediately at boot, without the kernel having to load them from somewhere.
That allows the kernel to run userspace programs that e.g. configure devices, load modules, setup that nifty RAID array that contains all filesystems or even ask the user for the password to his encrypted root filesystem.
When this configuration is done, the first script that is called just exec()s /sbin/init from the (now configured and available) root filesystem.
I have seen quite a few systems where the drivers themselvess for the disk controllers and the rootfs are loaded via modules in an initramfs, rather than being included in the kernel image.
You do not strictly need an initramfs to boot - if your kernel image contains all drivers necessary to access the rootfs and you don't need any special configuration or user input (like RAID arrays or encrypted filesystems) to mount it, it is often possible to directly start /sbin/init from the rootfs.
See also:
http://www.kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt
http://www.kernel.org/doc/Documentation/initrd.txt
As a side note, some systems (rescue disks, embedded and such) may use a ramfs as the root filesystem when the actual root filesystem is in a medium that may be removed or is not writable (CD, Flash MTDs etc).