dd:在 iSCSI LUN 上写入 4k 块大小的无效参数

dd:在 iSCSI LUN 上写入 4k 块大小的无效参数

为什么不能oflag=direct在 4k 块大小的 iSCSI 磁盘上使用? 其他应用程序是否也会出现此问题?

root@testvm02:~# dd if=/root/speedtest of=/mnt/8k_512/speedtest oflag=direct
51200+0 records in
51200+0 records out
26214400 bytes (26 MB, 25 MiB) copied, 59,4312 s, 441 kB/s

root@testvm02:~# dd if=/root/speedtest of=/mnt/8k_4k/speedtest oflag=direct
dd: writing to '/mnt/8k_4k/speedtest': Invalid argument
1+0 records in
0+0 records out
0 bytes copied, 0,000790648 s, 0,0 kB/s

root@testvm02:~# dd if=/root/speedtest of=/mnt/4k_4k/speedtest oflag=direct
dd: writing to '/mnt/4k_4k/speedtest': Invalid argument
1+0 records in
0+0 records out
0 bytes copied, 0.000139662 s, 0.0 kB/s

我的环境@存储服务器:

/usr/sbin/zfs create -s -V 50GiB STORAGE01/4af1a9b7-0592-4707-a875-986d91fceac4 #<-- / & vda on testvm02
/usr/sbin/tgtadm --lld iscsi --op new --mode logicalunit --tid 19 --lun 1 -b /dev/zvol/STORAGE01/4af1a9b7-0592-4707-a875-986d91fceac4
#
/usr/sbin/zfs create -s -V 50GiB STORAGE01/209afd7c-bdd6-4125-a899-b98758fcc6c0 #<-- /mnt/8k-512 & vdb on testvm02
/usr/sbin/tgtadm --lld iscsi --op new --mode logicalunit --tid 19 --lun 2 -b /dev/zvol/STORAGE01/209afd7c-bdd6-4125-a899-b98758fcc6c0
#
/usr/sbin/zfs create -s -V 50GiB STORAGE01/6ae21fa3-df76-4843-ab65-0700af4f04f7 #<-- /mnt/8k-4k & vdc on testvm02
/usr/sbin/tgtadm --lld iscsi --op new --mode logicalunit --tid 19 --blocksize 4096 --lun 3 -b /dev/zvol/STORAGE01/6ae21fa3-df76-4843-ab65-0700af4f04f7
#
/usr/sbin/zfs create -s -o volblocksize=4k -V 50GiB STORAGE01/24d38989-b47b-4e3c-b5ea-5d9a30d611f6 #<-- /mnt/4k-4k & vdd on testvm02
/usr/sbin/tgtadm --lld iscsi --op new --mode logicalunit --tid 19 --blocksize 4096 --lun 4 -b /dev/zvol/STORAGE01/24d38989-b47b-4e3c-b5ea-5d9a30d611f6
#
# default zvol blocksize = 8k, default iSCSI LUN blocksize = 512

虚拟机:

virt-install --name 'testvm02.domain.de' --description 'desc' --os-type 'Linux' --os-variant 'debian9' --ram 2048 --vcpus 2 --cdrom '/var/lib/libvirt/boot/firmware-10.9.0-amd64-netinst.iso' --graphics vnc,password=foobar --network 'bridge:br540,model=virtio,virtualport_type=openvswitch' --disk 'vol=c0dcc42e-7805-4ab3-845f-363bdedada5b/unit:0:0:1,logical_block_size=512,physical_block_size=512' --disk 'vol=c0dcc42e-7805-4ab3-845f-363bdedada5b/unit:0:0:2,logical_block_size=512,physical_block_size=512' --disk 'vol=c0dcc42e-7805-4ab3-845f-363bdedada5b/unit:0:0:3,logical_block_size=4096,physical_block_size=4096' --disk 'vol=c0dcc42e-7805-4ab3-845f-363bdedada5b/unit:0:0:4,logical_block_size=4096,physical_block_size=4096'

所有使用 ext4 格式化的磁盘

root@testvm02:~# fdisk -l
Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 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: 0x50172cb2

Device     Boot  Start       End   Sectors  Size Id Type
/dev/vda1  *      2048    999423    997376  487M 83 Linux
/dev/vda2       999424 104855551 103856128 49,5G 83 Linux


Disk /dev/vdb: 50 GiB, 53687091200 bytes, 104857600 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: 0x5e831f4b

Device     Boot Start       End   Sectors Size Id Type
/dev/vdb1        2048 104855551 104853504  50G 83 Linux


Disk /dev/vdc: 50 GiB, 53687091200 bytes, 13107200 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
Disklabel type: dos
Disk identifier: 0x791f5094

Device     Boot Start      End  Sectors Size Id Type
/dev/vdc1         256 13106943 13106688  50G 83 Linux


Disk /dev/vdd: 50 GiB, 53687091200 bytes, 13107200 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
Disklabel type: dos
Disk identifier: 0x6de2b945

Device     Boot Start      End  Sectors Size Id Type
/dev/vdd1         256 13106943 13106688  50G 83 Linux

答案1

为什么不能oflag=direct在 4k 块大小的 iSCSI 磁盘上使用?

您可以使用直接 I/O,但绕过内核缓存会产生以下限制:open(2)联机帮助页:

O_DIRECT标志可能会对用户空间缓冲区的长度和地址以及 I/O 的文件偏移施加对齐限制。在 Linux 中,对齐限制因文件系统和内核版本而异,并且可能完全不存在。
(...)
在 Linux 2.4 下,传输大小、用户缓冲区对齐和文件偏移量都必须是文件系统逻辑块大小的倍数。从 Linux 2.6.0 开始,与底层存储的逻辑块大小(通常为 512 字节)对齐就足够了。逻辑块大小可以使用以下方式确定读写控制(2) BLKSSZGET操作或从 shell 使用以下命令:blockdev --getss

总而言之,如果您正在使用,oflag=direct则还必须设置bs(或仅obs在此处)为基础块大小的倍数。bs默认为 512;所以当你的块大小是4096时,你必须将其设置为4k、8k等。

dd if=/root/speedtest of=/mnt/8k_4k/speedtest oflag=direct bs=4k

相关内容