Linux 文件系统检测

发布于 2024-11-01 07:58:13 字数 344 浏览 6 评论 0原文

我试图按照这本书来了解如何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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕色琉璃 2024-11-08 07:58:13

您找不到它的原因是,在大多数情况下,它不在内核中 - 它位于用户空间 mount 实用程序中,该实用程序位于 util-linux 包。如果你没有给它一个文件系统类型,或者给它一个“any”类型,mount只会遍历内核知道的所有文件系统的列表,并尝试每个文件系统按顺序进行,直到其中一个成功安装(如果没有成功安装,则返回错误)。

它如何找出内核知道哪些文件系统类型?它读取 /proc/filesystems 文件,该文件遍历 fs/filesystems.c 中的 file_systems 链接列表。加载文件系统驱动程序时,它会在同一文件中调用 register_filesystem 将自身添加到该列表中。例如,在 fs/ext2/super.cinit_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 the util-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 the file_systems linked list in fs/filesystems.c. When a filesystem driver is loaded, it calls register_filesystem in that same file to add itself to that list. For example, there's a call to register_filesystem in init_ext2_fs in fs/ext2/super.cinit_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.

小…楫夜泊 2024-11-08 07:58:13

blkid -o value -s TYPE /dev/path/to/device

blkid -o value -s TYPE /dev/path/to/device

剩一世无双 2024-11-08 07:58:13

mount 手册页:

如果没有给出 -t 选项,或者指定了自动类型,mount 将尝试猜测所需的类型。如果 mount 是用 blkid 库编译的,则猜测是由该库完成的。否则,mount 通过探测超级块来猜测自己;如果没有出现任何看起来熟悉的内容,mount 将尝试读取文件 /etc/filesystems,或者,如果该文件不存在,则读取 /proc/filesystems。将尝试列出的所有文件系统类型,除了标记为“nodev”的文件系统类型(例如,devpts、proc、nfs 和 nfs4)。如果 /etc/filesystems 以仅一个 * 的行结尾,则 mount 之后将读取 /proc/filesystems。

另外,我的 ubuntu 盒子有这个 is mount 手册页(提到 volume_id 库)

如果没有给出 -t 选项,或者指定了自动类型,mount 将尝试猜测所需的类型。安装用途
用于猜测文件系统类型的 blkid 或volume_id 库;如果没有出现任何看起来的东西
熟悉的是,mount 将尝试读取文件 /etc/filesystems,或者,如果该文件不存在,则读取 /proc/filesystems。全部
将尝试列出的文件系统类型,除了标记为“nodev”的文件系统类型(例如,devpts、proc
和 nfs)。如果 /etc/filesystems 以仅一个 * 的行结尾,则 mount 之后将读取 /proc/filesystems。

From mount man page:

If no -t option is given, or if the auto type is specified, mount will try to guess the desired type. If mount was compiled with the blkid library, the guessing is done by this library. Otherwise, mount guesses itself by probing the superblock; if that does not turn up anything that looks familiar, mount will try to read the file /etc/filesystems, or, if that does not exist, /proc/filesystems. All of the filesystem types listed there will be tried, except for those that are labeled "nodev" (e.g., devpts, proc, nfs, and nfs4). If /etc/filesystems ends in a line with a single * only, mount will read /proc/filesystems afterwards.

Also, my ubuntu box has this is mount man page (mentions volume_id library)

If no -t option is given, or if the auto type is specified, mount will try to guess the desired type. Mount uses
the blkid or volume_id library for guessing the filesystem type; if that does not turn up anything that looks
familiar, mount will try to read the file /etc/filesystems, or, if that does not exist, /proc/filesystems. All
of the filesystem types listed there will be tried, except for those that are labeled "nodev" (e.g., devpts, proc
and nfs). If /etc/filesystems ends in a line with a single * only, mount will read /proc/filesystems afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文