Partition detection?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Partition detection?

Post by Candy »

How do you detect what filesystem a partition is formatted in? What things do you take into account?

Why I'm asking:
I've got partition table reading in place, which now can tell me if something is type 0x06 (FAT16) or 0x0C (FAT32). For these two it's slightly oversufficient, as 0x04 and 0x0B are treated the exact same way as 0x06 and 0x0C - it's a FAT file system. If it says 0x07 however, that's NTFS, HPFS, HFS or HFS+. If it says 0x83, it could be just about anything. And then there's the option that somebody messed up their system config and has a 0x0C partition with an ext2 file system.

How do you determine what filesystem driver to load for what partition?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Partition detection?

Post by gerryg400 »

I guess there are little tricks to identify each fs type. For example, ext2/3/4 have a magic number in the superblock. I'm sure each fs has some sort of identifying signature.
If a trainstation is where trains stop, what is a workstation ?
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Partition detection?

Post by Owen »

Start up the FS driver and have it verify that you've detected the partition correctly. For 0x7, start an "OS/2 IFS partition" driver, which distinguishes based upon bytes 3-11 of the partition which type it is (By the way - 0x07 is a great place to cram new partition types into the MBR!)

This still leaves some problems - 0x86 could be either Linux swap or an old Solaris slice table. Which is problematic, because IIRC Linux Swap has no headers! (The other reason this is problematic is that many livecds automatically swapon any 0x86 partitions...)

Final conclusion: MBR is an absolute mess!
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Partition detection?

Post by Candy »

gerryg400 wrote:I guess there are little tricks to identify each fs type. For example, ext2/3/4 have a magic number in the superblock. I'm sure each fs has some sort of identifying signature.
So that's in general, assume the data is correct and move that FS to the head of the probe list. Otherwise, probe them all.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Partition detection?

Post by Solar »

Owen wrote:Final conclusion: MBR is an absolute mess!
"For historical reasons..."

Whenever I read that sentence I feel like going somewhere with a chainsaw under my arm... :evil:

On the other hand: Why has your OS to auto-detect all those file systems? Do you actually support booting off all of them? Personally, I would require my OS to be located on a selection of file systems (like, 0x83, ext2 or ext3 - you could expand the list later on), boot from there, and have the user tell me which partition is in which format. He knows, doesn't he?
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Partition detection?

Post by Candy »

Solar wrote:Whenever I read that sentence I feel like going somewhere with a chainsaw under my arm... :evil:
Same here. Especially if there's nobody to explain why it became this mess. The MBR wasn't such a bad idea but lacked central organisation - and had two pigheaded OSes which misused it. Microsoft OSes which took all to mean some variant of FAT, and then took 7 to mean anything but FAT. Linux which took 0x83 to mean "something we have to look at".

That just doesn't work.
On the other hand: Why has your OS to auto-detect all those file systems? Do you actually support booting off all of them? Personally, I would require my OS to be located on a selection of file systems (like, 0x83, ext2 or ext3 - you could expand the list later on), boot from there, and have the user tell me which partition is in which format. He knows, doesn't he?
Well... I'd like my users to not have to enter filesystems manually, not in the last case because configuration kept separately from the volume is going to desync if something changes without your OS knowing it. Add a new disk to your system and Linux may miss half the disks.

What should go where other than the root is predefined - your root contains your OS & programs. Part of the abstraction, I'll call it.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Partition detection?

Post by egos »

FS association with device (kernel works with partitions as virtual devices) occures inside "mount" function. VFS subsystem transfers control to FS driver that had register specified FS. The driver checks that device contains the FS structure. Only if it's true the function will have successful result. If to specify NULL instead FS name kernel will search FS by calling every FS handler and analysing its result.
PT fields are not used for FS type detection.
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Partition detection?

Post by Owen »

My personal strategy would be to use the MBR ID as a first guess - after all, its probably right! For GPT I would use that strategy too (In fact doubly so: GPT doesn't have the collision issues)
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Partition detection?

Post by thepowersgang »

Just GPT has the problem that the IDs don't usually denote a filesystem type, they denote what it is used for (in most cases)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Post Reply