关于linux内核对rootfs的挂载

gjq_1988 2014-11-24 04:57:08
最近在读VFS方面的源代码。
我发现,在系统启动过程的最后阶段,linux内核在调用sys_mount挂载实际根文件系统之前,先调用了init_mount_tree()这个函数挂载了一个虚拟的根文件系统,这是一个基于内存的根文件系统,类似于ramfs文件系统,在挂载完这个“虚拟的”文件系统之后,建立了VFS根目录,也就是“/” 目录。然后才调用sys_mount()挂载真正的根文件系统(比如SD卡上的ext4),最后将它的挂载点移动到“/”目录,覆盖掉原先挂载的虚拟根文件系统,请问linux这样做的原因是什么?为什么不直接挂载真实的根文件系统呢?
...全文
2413 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_18641307 2015-10-29
  • 打赏
  • 举报
回复
好高端。。。。。。。。
zeloas 2015-09-26
  • 打赏
  • 举报
回复
为了防止正式的系统出现问题,无法挂载,系统就无法启动,就无法进行修复 也不是一定要重新挂在根目录的,类似的系统入android就没有这么做,具体的行为可以通过定制init或者ramdisk做到的
zhangfei714 2015-09-25
  • 打赏
  • 举报
回复
如果直接加载根文件系统,那根文件系统是什么,/dev/xxx?那么这个u-boot启动参数代表是什么呢?先加载虚拟文件系统是为了挂载真实文件系统在切换到真实文件系统, 不知道我理解的对不对。
lxh0129 2015-08-03
  • 打赏
  • 举报
回复
有架构和灵活性方面的考虑,rootfs是目前最好的选择。至于每个开发者要这样那样实现,其实大家请便,技术上没问题,但我想大多数开发者都不会喜欢写死SD等设备的驱动。
binbin520_92 2015-04-16
  • 打赏
  • 举报
回复
目前大多数的根文件系统加载过程都是通过initrd的配合完成的(initrd具有的诸多优点网上可以找到),而这个过程中如果有一个“最原始的文件系统”配合将会使得initrd设计简单很多。
nswcfd 2015-04-16
  • 打赏
  • 举报
回复
有一篇关于bootloader的演化历史的paper, 推荐读一下 《booting linux: the history and the future》,某文库就有。
nswcfd 2015-04-16
  • 打赏
  • 举报
回复
#14楼的意思是说,要从ext4分区启动必须得有ext4 fs的驱动,如果不通过initrd/ramfs做中介的话,那就得把ext4的驱动link in到vmliux。 如果做一个只有自己用的kernel,可以采用这种方法。 但是不是所有人的硬盘分区都是ext4,有人是ext3,有人是ext2,所以作为一个通用内核,最好的选择还是以内存fs作为根,再加载fs driver,最后跳到hard disk上的文件系统。
lieye_leaves 2015-03-21
  • 打赏
  • 举报
回复
rootfs为启动阶段提供一个空的根目录,具体的文件系统就挂载在空的根目录上,它就是方便内核容易的改变实际的根文件系统
栗小游 2015-02-12
  • 打赏
  • 举报
回复
弱弱的参与一下讨论: 貌似楼上的讨论偏向两个方向, 1. 是否必须用initrd 2. 为什么系统先挂载了一个“假”的“/”,然后再挂载真的。 对于1.是否必须用initrd,答案我认为是不必须的。为什么必须用ramdisk呢?完全可以从其它fs里读rootfs。 对于2.我觉得是start_kernel的不同阶段。在那个阶段挂载一个“/”才能继续后面的操作。 虽然这个不是必须的,这是linux设计而已。这样设计是为了在挂载真正的fs之前先拥有“/”,然后建立"/dev"之类的东西把。 到了start_kernel的最后阶段,再根据配置来挂载initrd或者LZ说的“真实的FS”。也就是1里牵扯的东西。 我觉得10楼的同学说的也是有道理的,基于initrd这种设计会带来很多的便利之处,可以采用更小巧方便的fs来“封装”rootfs也是其中之一。 不是太熟,又错误请见谅。
gjq_1988 2014-12-03
  • 打赏
  • 举报
回复
引用 14 楼 micropentium6 的回复:
[quote=引用 13 楼 gjq_1988 的回复:] [quote=引用 12 楼 yangPSO 的回复:] 如果你使用ext4作为“最原始的文件系统”,那你是不是还先得格式化一个内存盘的ext4文件系统,太麻烦了,而且 ext4是相当复杂的文件系统,是针对磁盘的。 而rootfs则简单的多,它专门针对内存的。 rootfs+initrd是很好的组合。
“那你是不是还先得格式化一个内存盘的ext4文件系统”——————————那我可以直接在磁盘上读ext4文件系统阿,这样不就不用格式化一个内存盘的ext4文件系统了?[/quote] how could you read the data on ext4 file system if the module is not available in vmlinux, which is a statically linked executable? [/quote] 能不能用中文。。英文太专业的表述我怕有歧义。。
  • 打赏
  • 举报
回复
引用 13 楼 gjq_1988 的回复:
[quote=引用 12 楼 yangPSO 的回复:] 如果你使用ext4作为“最原始的文件系统”,那你是不是还先得格式化一个内存盘的ext4文件系统,太麻烦了,而且 ext4是相当复杂的文件系统,是针对磁盘的。 而rootfs则简单的多,它专门针对内存的。 rootfs+initrd是很好的组合。
“那你是不是还先得格式化一个内存盘的ext4文件系统”——————————那我可以直接在磁盘上读ext4文件系统阿,这样不就不用格式化一个内存盘的ext4文件系统了?[/quote] how could you read the data on ext4 file system if the module is not available in vmlinux, which is a statically linked executable?
gjq_1988 2014-12-02
  • 打赏
  • 举报
回复
引用 12 楼 yangPSO 的回复:
如果你使用ext4作为“最原始的文件系统”,那你是不是还先得格式化一个内存盘的ext4文件系统,太麻烦了,而且 ext4是相当复杂的文件系统,是针对磁盘的。 而rootfs则简单的多,它专门针对内存的。 rootfs+initrd是很好的组合。
“那你是不是还先得格式化一个内存盘的ext4文件系统”——————————那我可以直接在磁盘上读ext4文件系统阿,这样不就不用格式化一个内存盘的ext4文件系统了?
yangPSO 2014-12-02
  • 打赏
  • 举报
回复
如果你使用ext4作为“最原始的文件系统”,那你是不是还先得格式化一个内存盘的ext4文件系统,太麻烦了,而且 ext4是相当复杂的文件系统,是针对磁盘的。 而rootfs则简单的多,它专门针对内存的。 rootfs+initrd是很好的组合。
yangPSO 2014-12-02
  • 打赏
  • 举报
回复
我想这是内核开发者的设计策略。 目前大多数的根文件系统加载过程都是通过initrd的配合完成的(initrd具有的诸多优点网上可以找到),而这个过程中如果有一个“最原始的文件系统”配合将会使得initrd设计简单很多。 选择谁做“最原始的文件系统”呢,当前是roofs,因为它简单且专门针对内存的,而且rootfs部分的代码用于实现 共享内存等IPC,加载了这部分代码也不会“浪费”。 当然可以指定一个真实的根文件系统作为roofs,比如ext4,但是它比较复杂,且意味着内核必须将ext4编译进去。 也许LZ会说,我不用initrd且ext4编译进内核。但是先挂载roofs也可以很好的完成这种情况,且几乎没有增加多少的资源消耗, 也不需要针对你这种情况在内核代码中做更多的特殊处理。 这是个人的理解,请指正。
gjq_1988 2014-12-02
  • 打赏
  • 举报
回复
引用 10 楼 yangPSO 的回复:
我想这是内核开发者的设计策略。 目前大多数的根文件系统加载过程都是通过initrd的配合完成的(initrd具有的诸多优点网上可以找到),而这个过程中如果有一个“最原始的文件系统”配合将会使得initrd设计简单很多。 选择谁做“最原始的文件系统”呢,当前是roofs,因为它简单且专门针对内存的,而且rootfs部分的代码用于实现 共享内存等IPC,加载了这部分代码也不会“浪费”。 当然可以指定一个真实的根文件系统作为roofs,比如ext4,但是它比较复杂,且意味着内核必须将ext4编译进去。 也许LZ会说,我不用initrd且ext4编译进内核。但是先挂载roofs也可以很好的完成这种情况,且几乎没有增加多少的资源消耗, 也不需要针对你这种情况在内核代码中做更多的特殊处理。 这是个人的理解,请指正。
hi,仔细看了你的分析,感觉两个方案差不多。。请问使用rootfs比使用真实的根文件系统有什么明显的优势或者好处吗?
  • 打赏
  • 举报
回复
引用 6 楼 gjq_1988 的回复:
[quote=引用 5 楼 yangPSO 的回复:] 请参考: Ramfs-rootfs-initramfs.txt ========================================================== All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is extracted into rootfs when the kernel boots up. After extracting, the kernel checks to see if rootfs contains a file "init", and if so it executes it as PID 1. If found, this init process is responsible for bringing the system the rest of the way up, including locating and mounting the real root device (if any). If rootfs does not contain an init program after the embedded cpio archive is extracted into it, the kernel will fall through to the older code to locate and mount a root partition, then exec some variant of /sbin/init out of that.
这没有解释我的问题呢。。这只是说明linux的机制和rootfs的用法。我问的是why?为什么不直接挂载real root fs?[/quote] the initial RAM disk is necessary for booting linux system. the RAM disk image is created by mkinitrd. Why is it necessary? because we try to avoid a classic problem: "which comes first the chicken or the egg". Think about it: how do you boot the root file system if it resides on a device that requires a module in the root file system to initialize? The answer is: by using the initrd with the required module in it that is available to the kernel before the root file system is mounted. Kernel will then be started after RAM disk image is loaded. Sorry, it's ugly, but it is what it is...
  • 打赏
  • 举报
回复
引用 8 楼 gjq_1988 的回复:
[quote=引用 7 楼 micropentium6 的回复:] [quote=引用 6 楼 gjq_1988 的回复:] [quote=引用 5 楼 yangPSO 的回复:] 请参考: Ramfs-rootfs-initramfs.txt ========================================================== All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is extracted into rootfs when the kernel boots up. After extracting, the kernel checks to see if rootfs contains a file "init", and if so it executes it as PID 1. If found, this init process is responsible for bringing the system the rest of the way up, including locating and mounting the real root device (if any). If rootfs does not contain an init program after the embedded cpio archive is extracted into it, the kernel will fall through to the older code to locate and mount a root partition, then exec some variant of /sbin/init out of that.
这没有解释我的问题呢。。这只是说明linux的机制和rootfs的用法。我问的是why?为什么不直接挂载real root fs?[/quote] the initial RAM disk is necessary for booting linux system. the RAM disk image is created by mkinitrd. Why is it necessary? because we try to avoid a classic problem: "which comes first the chicken or the egg". Think about it: how do you boot the root file system if it resides on a device that requires a module in the root file system to initialize? The answer is: by using the initrd with the required module in it that is available to the kernel before the root file system is mounted. Kernel will then be started after RAM disk image is loaded. Sorry, it's ugly, but it is what it is...[/quote] 我知道你的意思,我们的确需要一个初始的文件系统来提供初始的安装点或者其他必要的设备模块,但这个文件系统一定要用rootfs虚拟文件系统吗?为什么不直接用一个真实的根文件系统来提供?问题在这里! 这么说吧,既然rootfs虚拟文件系统能做“第一只鸡”,为什么我的ext4不可以用来做这“第一只鸡”?你也许会说也许别人不用ext4做根文件系统,但总归是在某个根设备上实现了某个根文件系统吧,为什么不用这个真实的根文件系统作为“第一只鸡”? [/quote] first of all, ext4 is not a good example, init ram disk was invented long before ext4. I am not sure why you mention "rootfs" or "ramfs" here since they have nothing to do with this at all... In fact, init ram disk, in some linux distro, is in ext2 format. take a look on the concept of rootfs and ramfs please...
gjq_1988 2014-12-01
  • 打赏
  • 举报
回复
引用 5 楼 yangPSO 的回复:
请参考: Ramfs-rootfs-initramfs.txt ========================================================== All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is extracted into rootfs when the kernel boots up. After extracting, the kernel checks to see if rootfs contains a file "init", and if so it executes it as PID 1. If found, this init process is responsible for bringing the system the rest of the way up, including locating and mounting the real root device (if any). If rootfs does not contain an init program after the embedded cpio archive is extracted into it, the kernel will fall through to the older code to locate and mount a root partition, then exec some variant of /sbin/init out of that.
这没有解释我的问题呢。。这只是说明linux的机制和rootfs的用法。我问的是why?为什么不直接挂载real root fs?
gjq_1988 2014-12-01
  • 打赏
  • 举报
回复
引用 7 楼 micropentium6 的回复:
[quote=引用 6 楼 gjq_1988 的回复:] [quote=引用 5 楼 yangPSO 的回复:] 请参考: Ramfs-rootfs-initramfs.txt ========================================================== All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is extracted into rootfs when the kernel boots up. After extracting, the kernel checks to see if rootfs contains a file "init", and if so it executes it as PID 1. If found, this init process is responsible for bringing the system the rest of the way up, including locating and mounting the real root device (if any). If rootfs does not contain an init program after the embedded cpio archive is extracted into it, the kernel will fall through to the older code to locate and mount a root partition, then exec some variant of /sbin/init out of that.
这没有解释我的问题呢。。这只是说明linux的机制和rootfs的用法。我问的是why?为什么不直接挂载real root fs?[/quote] the initial RAM disk is necessary for booting linux system. the RAM disk image is created by mkinitrd. Why is it necessary? because we try to avoid a classic problem: "which comes first the chicken or the egg". Think about it: how do you boot the root file system if it resides on a device that requires a module in the root file system to initialize? The answer is: by using the initrd with the required module in it that is available to the kernel before the root file system is mounted. Kernel will then be started after RAM disk image is loaded. Sorry, it's ugly, but it is what it is...[/quote] 我知道你的意思,我们的确需要一个初始的文件系统来提供初始的安装点或者其他必要的设备模块,但这个文件系统一定要用rootfs虚拟文件系统吗?为什么不直接用一个真实的根文件系统来提供?问题在这里! 这么说吧,既然rootfs虚拟文件系统能做“第一只鸡”,为什么我的ext4不可以用来做这“第一只鸡”?你也许会说也许别人不用ext4做根文件系统,但总归是在某个根设备上实现了某个根文件系统吧,为什么不用这个真实的根文件系统作为“第一只鸡”?
yangPSO 2014-11-30
  • 打赏
  • 举报
回复
请参考: Ramfs-rootfs-initramfs.txt ========================================================== All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is extracted into rootfs when the kernel boots up. After extracting, the kernel checks to see if rootfs contains a file "init", and if so it executes it as PID 1. If found, this init process is responsible for bringing the system the rest of the way up, including locating and mounting the real root device (if any). If rootfs does not contain an init program after the embedded cpio archive is extracted into it, the kernel will fall through to the older code to locate and mount a root partition, then exec some variant of /sbin/init out of that.
加载更多回复(4)

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧