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?
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.
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.
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
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.
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.
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.