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.
The first command you can use is lsblk , which shows information about available block devices. This command can read information of a block device whether or not it is mounted. When run wlith -f option, it shows filesystem type of every mounted or unmounted block device.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With