Reputation: 396
1.When I use mkfs
command in linux, I am wondering when I set different --type
, what will happen to the disk.
2.When I use fdisk -l
, it will show me the file system type of each partition. So I guess there must be some information stored in the disk to be provided for identifying. Am I right? If I am, where does the information locate? In what format?
Upvotes: 0
Views: 399
Reputation: 616
The partition information in the MBR and EBR is not used by Linux to identify the file system stored in the partition. The reason for this is because the MBR only has room for a single 8-bit value for the file system type, and it is notoriously unreliable. There is no single registry which assigns partition type ID's, and if there were, given that the MBR has been used by a large number of operating systems through the years (from MS-DOS, to Windows, to Xenix, to many BSD's, to SCO Unix, to Linux), 256 different partition types Just Isn't Enough.
So what happens is that file systems have "magic numbers" which are stored in various different locations, almost always in the first 32k or so of the partition (and most commonly in the first 4-8k of the partition). When the Linux kernel mounts the root file system, it simply tries a number of different file system types until one successfully mounts. In the /etc/fstab file, the file system type that should be used for a particular block device is listed, so the kernel doesn't have to guess, or blindly try different file system types.
If you have no idea what sort of file system might be in a particular partition table, there are programs like blkid that will use hueristics, generally with a table driven set of magic numbers, to determine the file system type. One such table can be found here:
https://git.kernel.org/cgit/fs/ext2/e2fsprogs.git/tree/lib/blkid/probe.c?h=v1.43.1#n1411
The fact that the magic numbers can be found in multiple places does sound scary because you might get different answers depending on the order that you try probing for the magic numbers. But fortunately, most mkfs tools are smart enough to zero out other portions of the disk where magic numbers from file systems that might have previously been used on that partition, and in practice it works well enough if you zero the first 32k and the last 32k in the partition.
Upvotes: 1