Linux 文件系统检测
我试图按照这本书来了解如何Linux 内核可以工作。
我真正无法理解的是,我无法理解 Linux 如何检测文件系统类型,Linux 支持无数的文件系统,每个文件系统都有其特殊性。
谁能给我指出内核中的一段代码,该代码应该区分 fat 和 ext4 吗?
MBR不包含此类信息,并且每种类型的超级块都不同。
当发出 mount /dev/whatever /media
时,不需要添加文件系统类型。
I am trying to follow this book to gain a bit of understanding of how the Linux kernel works.
What I can't really wrap my head around is that I can't understand how Linux detects a filesystem type, there are a gazillion filesystems supported in Linux each with its particularities.
Could anyone point me to a piece of code in the kernel that is supposed to distinguish between let's say fat and ext4?
The MBR does not contain this sort of information, and the superblock of each type is different.
When issuing a mount /dev/whatever /media
it's not necessary to add the filesystem type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您找不到它的原因是,在大多数情况下,它不在内核中 - 它位于用户空间
mount
实用程序中,该实用程序位于util-linux
包。如果你没有给它一个文件系统类型,或者给它一个“any”类型,mount
只会遍历内核知道的所有文件系统的列表,并尝试每个文件系统按顺序进行,直到其中一个成功安装(如果没有成功安装,则返回错误)。它如何找出内核知道哪些文件系统类型?它读取
/proc/filesystems
文件,该文件遍历fs/filesystems.c
中的file_systems
链接列表。加载文件系统驱动程序时,它会在同一文件中调用register_filesystem
将自身添加到该列表中。例如,在fs/ext2/super.c
的init_ext2_fs
中有对register_filesystem
的调用 -init_ext2_fs
是ext2 模块的 module-init 函数。当有人尝试使用错误的文件系统挂载设备时,某些文件系统会产生噪音并在内核调试日志中打印错误,这就是为什么,例如,当成功时,您可能会看到有关“无效的 XFS 文件系统”的错误如果
mount
碰巧先尝试 xfs,则挂载 ext4 文件系统。The reason you can't find it is because, for the most part, it's not in the kernel -- it's in the userspace
mount
utility, which is in theutil-linux
package. If you don't give it a filesystem type, or if you give it a type of "any",mount
merely goes through the list of all of the filesystems the kernel knows about, and tries each one in order until one of them mounts successfully (or returns an error if none of them do).How does it find out what filesystem types the kernel knows about? It reads the
/proc/filesystems
file, which walks thefile_systems
linked list infs/filesystems.c
. When a filesystem driver is loaded, it callsregister_filesystem
in that same file to add itself to that list. For example, there's a call toregister_filesystem
ininit_ext2_fs
infs/ext2/super.c
—init_ext2_fs
is the module-init function for the ext2 module.Some filesystems are noisy and print errors to the kernel debug log when someone tries to mount a device with the wrong filesystem, which is why, for instance, you might see errors about "invalid XFS filesystem" when successfully mounting an ext4 filesystem, if
mount
happened to try xfs first.blkid -o value -s TYPE /dev/path/to/device
blkid -o value -s TYPE /dev/path/to/device
从 mount 手册页:
另外,我的 ubuntu 盒子有这个 is mount 手册页(提到
volume_id
库)From mount man page:
Also, my ubuntu box has this is mount man page (mentions
volume_id
library)