linuxfilesystemsinodeext2superblock

ext2 "image" files vs real ext2 devices


I'm tasked with writing a reader program for windows that is able read an ext2 partition.

For my testing I'm using a drive I formatted to ext2 and a file I created using mkfs (a file that does mount and work well under linux)

For some reason when I read the superblock from the drive (the real one) I get all the right meta-data (i.e. block size, inode count etc..) but doing the exact same thing to the file returns bad results (which make no sense).

is there a difference between the 2?

I open the drive using \.\X: and I make the file using mkfs.


Solution

  • There shouldn't be any difference between ext2 on a partition and stored within a file (and indeed there isn't; I just checked); however, IIRC, the offset of the primary superblock is 2048 instead of 1024, if ext2 is installed on a bare disk (e.g. /dev/sda instead of /dev/sda1). This is to accomodate the MBR and other junk. (I can't find it in the docs from skimming just now, but this sticks out in my mind as something I ran into.) However, it's somewhat unusual to install to a bare drive, so I doubt this is your problem.

    I wrote some ext2 utilities a few years ago, and after starting writing it by hand, I switched to using Ted Ts'o (the ext2 filesystem creator)'s e2fsprogs, which come with headers/libraries/etc. for doing all this in a more flexible and reliable fashion.

    You may also want to check at offset 0x438 into the file/partition for the magic number 0xEF53, and consider it not an ext2/3 filesystem if that's not there, before pulling in the entire superblock, just as a sanity check.

    Here's some docs that will probably be helpful: http://www.nongnu.org/ext2-doc/ext2.html