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?

  • 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 Feb 13 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 Feb 13 at 16:13

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.
| improve this answer | |

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

| improve this answer | |

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.