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.
This might be a stupid question, but: Every source of documentation for ext2 that I've found has specified that the ext2 superblock is always at offset 1024 (or 0x400, or LBA 2 with 512 byte sectors), though when I create a qemu image and create an empty ext2 file system on it the superblock is located at offset 2560 (or 0xA00, or LBA 5). fsck still correctly identifies the file system, so it's obvious that I'm misunderstanding something here.
Ah, I figured it'd have something to do with partitions. Is there any other reliable way to find the superblock (since I guess it won't always be at offset 2560 then)?
You read the partition table (located at a fixed point) to determine where each partition starts. Then you can read the block at offset 1024 from the start of each partition to determine whether they contain an ext2 filesystem.
# qemu-img create -f raw harddrive.img 512M
# kpartx -a harddrive.img
# mkfs.ext2 /dev/mapper/loop0p1
Discarding device blocks: done
[...]
I don't believe you. When run on a completely empty disk image (as created by qemu-img create), kpartx won't create a /dev/mapper/loop0p1, because there are no partitions on the image yet.
I'm guessing that the OP has forgotten to mention the partition creation step. But the numbers given are correct for the first (or only) partition on a hard disk of that size.