How does operating system know what filesystem a partition is using? In other words, how are FAT16/32, NTFS, ext2/3 etc. distinguished from each other?
7 Answers
If you're using Win32 APIs on Windows, then you can call GetVolumeInformation (http://msdn.microsoft.com/en-us/library/aa364993.aspx) to determine the type of file system present on a given mounted volume.
For example, if you're trying to detect the file system present on D:, then you can call:
WCHAR FSType[512];
if (GetVolumeInformationW(L"D:\\", NULL, 0, NULL, NULL, NULL, FSType, ARRAYSIZE(FSType))) {
wprintf(L"FS type = %s\n", FSType);
}
This will only work, however, if the file system is "recognized" and "mountable" by the running operating system.

- 3,360
- 23
- 28
The OS tools that deal with setting up the filesystems or mounting them will use various heuristics to try to detect the filesystem, like looking for features that they have. For example, when the 'mount' tool is not told the filesystem type of a partition it is meant to mount, it does what the 'man' page for it describes:
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 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.
The blkid library and the 'disktype' tool will, if you give it a disk block device (like /dev/sda) or a partition block device (like /dev/sda1), use heuristics and educated guesses to determine what lives on that device. Very useful tool, especially in a xen environment where there is no disk but only virtual partitions, thus you can't just query the master boot record.
When setting up a new Linux-based system like Ubuntu, similar tools are used to detect filesystems.

- 6,586
- 4
- 32
- 34
-
Read the question again please. It is "How does operating system know" – billyswong Nov 27 '12 at 03:23
About every filesystem has some header information which is called "superblock." Superblocks contain magic numbers or other info about the type of filesystem.
MBR partition table also stores a 8 bit value representing the partition type.

- 414,610
- 91
- 852
- 789
There are several ways, depending on the hardware type.
Hard discs have a Master Boot Record followed by a Partition Table. The PT contains a list of the partitions on this drive. Each entry in that list contains (among other things) a numeric System ID field that specifies the partitions file system.
Floppy discs and most USB sticks do not have a PT. Here you have to look into the partition itself. The first partition sector (known as Boot Sector) usually contains a System ID in a completely different format from the System ID in the PT. Also, the location of the ID within the sector can differ between file systems.

- 19,903
- 7
- 54
- 87
-
1actually, it does not know. There's some ways to guess. but no OS uses that extensively. Mostly of the time, it's told. usually the boot manager mounts the partition with the info it also was told to use. then boots the kernel. which will mount partitions with FS it's told to use (in linux /etc/fstab) – gcb Jun 23 '12 at 21:22
-
@gcb: If the OS is told which FS to use, where does the information in `/etc/fstab` come from? It was written there by some program which read the information from the structures I mentioned. – Treb Jun 25 '12 at 08:01
-
1Er... actually while the above may be true (a program writing the contents of /etc/fstab), /etc/fstab was actually designed to be created by hand by a user. In other words, the 'program' will follow certain heuristics to determine what filesystem each partition is, but it will not always be successful, and it will be up to the user to correct the data in /etc/fstab in that case if he can. Talking of heuristics, see my answer. – Asfand Qazi Oct 10 '12 at 07:08
-
I remember writing my `/etc/fstab` by hand in the old days of Linux 2.2, but modern Linux distros tend to recognize partitions and write the `fstab` for you. – Fred Foo Nov 27 '12 at 22:27
First of all, the partition table has a byte in it that specifies the partition type. Secondly, every partition has different headers and structures, so with a bit of analysis it can be determined pretty much precisely.

- 104,512
- 87
- 279
- 422
Assuming you have an MBR then the details about the 4 primary partitions are found at 0x01BE. One of the sixteen bytes describing a partiton is a type identifier.
An id of 0x06 is fat16, 0x0B is FAT32, 0x07 is NTFS, 0x82 is a Linux partition.
Beyond that file-systems have structures at the specific locations within the partition that can be detected.

- 37,543
- 7
- 45
- 61
On linux when you mount a filesystem, you can pass -t ext3/ext3 etc - if you look in /etc/fstab (or equivalent) each drive probably has its fs type listed.
Then for automatically doing it, there is the superblock/equivalent (think windows types call it something else) ...
See this:
Superblock
Each file system is different and they have type like ext2, ext3 etc. Further each file system has size like 5 GB, 10 GB and status such as mount status. In short each file system has a superblock, which contains information about file system such as:
* File system type * Size * Status * Information about other metadata structures
Taken from:
http://www.cyberciti.biz/tips/understanding-unixlinux-filesystem-superblock.html

- 11,370
- 2
- 40
- 39