Page 1 of 1

ext2 superblock offset

Posted: Mon Dec 19, 2016 5:29 am
by HackerCow
Hey everyone!

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.

Here is my process for creating the ext2 volume:

Code: Select all

# qemu-img create -f raw harddrive.img 512M
# kpartx -a harddrive.img
# mkfs.ext2 /dev/mapper/loop0p1
Discarding device blocks: done                            
Creating filesystem with 524284 1k blocks and 131072 inodes
Filesystem UUID: 73f5f38b-d312-4af2-afda-ac34f356a6da
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done 
Any help on this would be greatly appreciated!

Re: ext2 superblock offset

Posted: Mon Dec 19, 2016 5:44 am
by iansjack
The super block is always located at offset 1024 from the start of the partition (not the start of the disk).

Re: ext2 superblock offset

Posted: Mon Dec 19, 2016 5:50 am
by HackerCow
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)?

Re: ext2 superblock offset

Posted: Mon Dec 19, 2016 5:55 am
by iansjack
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.

Re: ext2 superblock offset

Posted: Mon Dec 19, 2016 6:16 am
by Kevin
HackerCow wrote:Here is my process for creating the ext2 volume:

Code: Select all

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

Re: ext2 superblock offset

Posted: Mon Dec 19, 2016 11:34 am
by iansjack
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.