Linux filesystem detection
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开发者_如何学C 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.
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.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
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.
精彩评论