简体   繁体   English

确定分区的文件系统

[英]Determine what filesystem a partition is of

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? 换句话说,FAT16 / 32,NTFS,ext2 / 3等如何相互区分?

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. 如果您在Windows上使用Win32 API,则可以调用GetVolumeInformation( http://msdn.microsoft.com/en-us/library/aa364993.aspx )来确定给定已安装卷上存在的文件系统类型。

For example, if you're trying to detect the file system present on D:, then you can call: 例如,如果您尝试检测D:上存在的文件系统,则可以调用:

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. 但是,如果正在运行的操作系统“识别”和“可安装”文件系统,则此方法才有效。

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. MBR分区表还存储表示分区类型的8位值。

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. PT包含此驱动器上的分区列表。 Each entry in that list contains (among other things) a numeric System ID field that specifies the partitions file system. 该列表中的每个条目都包含(以及其他内容)数字系统ID字段,该字段指定分区文件系统。

Floppy discs and most USB sticks do not have a PT. 软盘和大多数USB记忆棒没有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. 第一个分区扇区(称为引导扇区 )通常包含与PT中的系统ID完全不同的系统ID。 Also, the location of the ID within the sector can differ between file systems. 此外,文件系统中ID的位置可能不同。

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: 例如,当'mount'工具没有被告知要安装的分区的文件系统类型时,它会执行它的'man'页面所描述的内容:

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. 如果你给它一个磁盘块设备(比如/ dev / sda)或分区块设备(比如/ dev / sda1),那么blkid库和'disktype'工具将使用启发式和有根据的猜测来确定它上面的内容设备。 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. 非常有用的工具,特别是在没有磁盘但只有虚拟分区的xen环境中,因此您不能只查询主引导记录。

When setting up a new Linux-based system like Ubuntu, similar tools are used to detect filesystems. 在设置像Ubuntu这样的基于Linux的新系统时,会使用类似的工具来检测文件系统。

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. 其次,每个分区都有不同的标题和结构,因此通过一些分析可以非常精确地确定。

Assuming you have an MBR then the details about the 4 primary partitions are found at 0x01BE. 假设您有MBR,那么有关4个主分区的详细信息位于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. 的0×06的ID是FAT16 ,0x0B中是FAT32,0x07的是NTFS ,为0x82是Linux分区。

Beyond that file-systems have structures at the specific locations within the partition that can be detected. 除此之外,文件系统在分区内的特定位置具有可以检测到的结构。

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. 在Linux上挂载文件系统时,你可以传递-t ext3 / ext3等 - 如果你查看/ etc / fstab(或等效的),每个驱动器可能都列出了它的fs类型。

Then for automatically doing it, there is the superblock/equivalent (think windows types call it something else) ... 然后为了自动执行它,有超级块/等价物(想想windows类型称之为别的东西)......

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. 每个文件系统都是不同的,它们的类型类似于ext2,ext3等。此外,每个文件系统的大小如5 GB,10 GB和状态(如挂载状态)。 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 http://www.cyberciti.biz/tips/understanding-unixlinux-filesystem-superblock.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM