ext2 superblock offset

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
HackerCow
Posts: 9
Joined: Mon Dec 19, 2016 5:15 am
Libera.chat IRC: esi

ext2 superblock offset

Post 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!
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: ext2 superblock offset

Post by iansjack »

The super block is always located at offset 1024 from the start of the partition (not the start of the disk).
HackerCow
Posts: 9
Joined: Mon Dec 19, 2016 5:15 am
Libera.chat IRC: esi

Re: ext2 superblock offset

Post 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)?
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: ext2 superblock offset

Post 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.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: ext2 superblock offset

Post 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.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: ext2 superblock offset

Post 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.
Post Reply