You are not logged in.

#1 2015-07-20 15:30:14

ayekat
Member
Registered: 2011-01-17
Posts: 1,634

[SOLVED?] Issues with mkfs.ext4 -E offset=...

I ran into this problem when I tried to format a partition on a disk image file. I'll probably do it with losetup etc. instead, but still I'd like to get an explanation for why this doesn't work, and maybe someone can spot what I'm doing wrong:

First, I create an empty disk image:

$ dd if=/dev/zero of=example.img bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 1.12894 s, 1.9 GB/s

Then, I create a partition table + one partition on that image:

$ fdisk example.img

Welcome to fdisk (util-linux 2.26.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x3a1e462c.

Command (m for help): o
Created a new DOS disklabel with disk identifier 0x9f64e38a.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4194303, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303): 

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): w
The partition table has been altered.
Syncing disks.

To be sure I get the numbers (sector size and sector offsets) right, I run

$ fdisk -l example.img
Disk example.img: 2 GiB, 2147483648 bytes, 4194304 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9f64e38a

Device       Boot Start     End Sectors Size Id Type
example.img1       2048 4194303 4192256   2G 83 Linux

So, the first partition starts at sector 2048 (and each sector has a size of 512 bytes), based on which I assume that that partition starts at an offset of 2048 * 512 = 1048576 bytes:

$ mkfs.ext4 -E offset=$((2048 * 512)) example.img 
mke2fs 1.42.12 (29-Aug-2014)
Found a dos partition table in example.img
Proceed anyway? (y,n) n

My assumption turns out to be wrong though.

If I hit y, I observe that this mkfs overwrites the first 0x00100400 bytes with zeroes and puts some data after that.

Question is: did I miss a step between creating the partition and formatting it, or are my numbers wrong, or is it something else?

Last edited by ayekat (2015-08-04 21:01:46)


pkgs │ hacks │ cfg │ blag

Offline

#2 2015-07-20 16:11:30

frostschutz
Member
Registered: 2013-11-15
Posts: 1,658

Re: [SOLVED?] Issues with mkfs.ext4 -E offset=...

It behaves the same way for me. It does not "overwrite" with zeroes, rather the file is truncated. It also changes the total filesize.

The filesystem is still created at the correct offset, but I assume it still expects to be doing this on a block device, rather than a regular file.

Use losetup --partscan instead, it gives you a loop device for the partition itself, so you don't have to muck about with strange mkfs options that probably no one used for years... wink

Even if it worked as intended, it would still be wrong since you did not specify filesystem size, so it might go beyond the end of the partition.

Offline

#3 2015-07-20 17:26:52

ayekat
Member
Registered: 2011-01-17
Posts: 1,634

Re: [SOLVED?] Issues with mkfs.ext4 -E offset=...

Oh, I hadn't noticed that. It truely does truncate the file.

frostschutz wrote:

The filesystem is still created at the correct offset, but I assume it still expects to be doing this on a block device, rather than a regular file.

I find this weird, because it's explicitely stated in the man page that

mke2fs (8) wrote:

This can be useful when creating disk images for virtual machines.

But then again, you're right, I didn't specify the size, so at one point or another it would've failed. smile

Thanks for the reply!


pkgs │ hacks │ cfg │ blag

Offline

Board footer

Powered by FluxBB