4

The final purpose is to build the image of a partition sector by sector. I want the sector size to be 4096. As a first step I am trying to create an empty image of 32MiB with 4096bytes sectors rather than 512bytes. For this I am trying:

dd if=/dev/zero of=empty4k.img bs=4096 count=8192

Then I do

fdisk -l empty4k.img

and shows 512 byte sectors. I believe it is because if you do "

fdisk -l /dev/zero

it also says 512 byte sectors.

Can anyone help me?

2
  • 5
    If I understand correctly, a drive (mass storage device) has physical and logical sectors. Files have no sectors, and I guess you mean that the image is a file.
    – sudodus
    Commented Feb 13, 2020 at 16:08
  • 4
    AFAIK dd doesn't really care about sectors, and there is no sector information in an IMG file. Sectors are a hardware thing these days. The sector size would only change how you compute a file offset from a sector index...
    – xenoid
    Commented Feb 13, 2020 at 16:13

3 Answers 3

14

It's not possible to do it the way you describe. Sector size is a block device property which files don't inherently have. A file is just a sequence of a certain number of bytes, how those are stored is an implementation detail...

So if you want a specific sector size, you need a block device. And Linux offers loop devices just for this purpose, so use losetup to create a file-backed virtual block device with a certain sector size.

Test file:

# dd if=/dev/zero of=empty4k.img bs=4096 count=8192

Regular loop device:

# losetup --find --show empty4k.img 
/dev/loop0
# fdisk -l /dev/loop0
Disk /dev/loop0: 32 MiB, 33554432 bytes, 65536 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

4096-byte sectors loop device:

# losetup --find --show --sector-size=4096 empty4k.img 
/dev/loop1
# fdisk -l /dev/loop1
Disk /dev/loop1: 32 MiB, 33554432 bytes, 8192 sectors
Units: sectors of 1 * 4096 = 4096 bytes
Sector size (logical/physical): 4096 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

In both cases, the file is completely identical, sector size property is provided by the block loop device layer.

2
  • Rather than dd on newer distros, I would use fallocate -l size /path/to/image.img to pre-allocate space to an image file (where the -l option specifies the length of the allocation, in bytes where the suffixes of k, m, g, t, p, e may be specified to denote KiB, MiB, GiB, etc.). You'll need mkfs to format the filesystem inside the image, but I find that it's still faster and cleaner this way. This is still a good answer, though, so I'm giving it an upvote. :) Commented May 24, 2021 at 14:46
  • @EvilSupahFly I just used the command given in the question, as there was no real reason to change it. fallocate is fine too, or truncate. when you mkfs, it will trim/free the unused space (at least for ext234 and others, unless you disable it), so don't expect it to be pre-allocated anymore afterwards. Commented May 24, 2021 at 14:53
11

The bs given to dd just tells how large the buffer should be during creating the file. In the end, the file consists of nothing but zero-bytes, there is no information about alignment.

You have to use the specific parameter to fdisk, which is -b, as per the man-page of fdisk(8):

  -b, --sector-size sectorsize
          Specify  the  sector  size  of  the  disk.   Valid values are 512,    1024, 2048, and 4096.  (Recent kernels know the sector size.  Use this option only on old kernels or to override the kernel's
          ideas.)  Since util-linux-2.17, fdisk differentiates between logical and physical sector size.  This option changes both sector sizes to sectorsize.
0

The blocksize to dd just asks it to read/write in chunks of that size. It used to be relevant to write in disk-sector size chunks for performance, given today's disk and much smarter operating system handling of I/O, it makes little (if any) difference.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .