使用 dd 创建空 img,使其扇区大小为 4096 字节而不是 512

使用 dd 创建空 img,使其扇区大小为 4096 字节而不是 512

最终目的是逐个扇区构建分区镜像。我希望扇区大小为 4096。第一步,我尝试创建一个 32MiB 的空映像,其中包含 4096 字节扇区而不是 512 字节。为此我正在尝试:

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

那我就做

fdisk -l empty4k.img

并显示 512 字节扇区。我相信这是因为如果你这样做“

fdisk -l /dev/zero

它还说 512 字节扇区。

谁能帮我?

答案1

按照你描述的方式是不可能做到的。扇区大小是文件本身不具有的块设备属性。文件只是一定数量字节的序列,如何存储这些字节是实现细节......

因此,如果您想要特定的扇区大小,则需要块设备。 Linux 正是为此目的提供了循环设备,因此可以使用它losetup来创建具有特定扇区大小的文件支持的虚拟块设备。

测试文件:

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

常规循环装置:

# 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字节扇区循环设备:

# 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

在这两种情况下,文件完全相同,扇区大小属性由块循环设备层提供。

答案2

bs定的 todd只是告诉创建文件期间缓冲区应该有多大。最后,该文件仅由零字节组成,没有有关对齐的信息。

您必须使用特定参数fdisk,即-b,根据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.

答案3

块大小dd只是要求它以该大小的块读/写。它曾经与写入磁盘扇区大小的块以提高性能相关,考虑到当今的磁盘和更智能的操作系统对 I/O 的处理,它几乎没有(如果有的话)区别。

相关内容