qemu NBD client behaviour when device size is not a multiple of 512
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 9 lines · Richard W.M. Jones
Hi Eric. Is this a bug?
$ nbdkit -fv random size=1023(You can choose any size which is not a multiple of 512.)
$ qemu-img convert nbd:localhost:10809 /var/tmp/test qemu-img: error while reading sector 0: Invalid argument
Or more directly:
$ qemu-io -r -f raw --trace=nbd_\* nbd://localhost:10809 ... [email protected]:nbd_receive_negotiate_size_flags Size is 1023, export flags 0x3 qemu-io> r -v 0 1023 [email protected]:nbd_send_request Sending request to server: { .from = 0, .len = 1024, .handle = 94749973992480, .flags = 0x0, .type = 0 (read) } [email protected]:nbd_receive_simple_reply Got simple reply: { .error = 22 (EINVAL), handle = 94749973992480 } read failed: Invalid argument
Yep. Looks likes the client code is trying to enforce 512-byte alignment, and qemu rounds ALL disk sizes up to the next sector boundary (even when the actual disk is unaligned). I'll have to patch things to not round past image end on the final partial sector.
Note that with qemu as server:
$ truncate --size=1023 file $ qemu-nbd -f raw -x '' file
the client now sees:
[email protected]:nbd_opt_go_info_block_size Block sizes are 0x200, 0x1000, 0x2000000
...
[email protected]:nbd_receive_negotiate_size_flags Size is 1024, export flags 0x1ed
where qemu as server is 0-padding the source file in all read requests; and that probably explains why I haven't noticed the problem with unaligned images in the client before (since qemu as NBD server doesn't advertise unaligned images). What's more, a write request to the tail of the partial sector (when not using a read-only connection) succeeds, and resizes the file in the process (at least, on protocols that support live resizing); but I wouldn't count on it behaving sanely for all protocols.
What SHOULD happen is that if qemu as server advertises a block size, it should truncate the advertised image size DOWN (not round up). I'm also not sure why qemu as server is advertising 512-byte blocks when backed by a file - it should be able to advertise 1-byte blocks. Likewise, qemu as client should pay attention to the size advertised by the server: if it is not sector-aligned and the server did not advertise block size, then assume that byte alignment works; if the server DID advertise block size (greater than 1) then qemu should round the image size down (as obeying the advertised block size implies that the partial sector at the end is inaccessible without violating NBD protocol - although the NBD protocol also states that servers SHOULD advertise a size that is a multiple of the block size).
At any rate, you've given me plenty to work with for fixing things, although it probably won't make it into 3.0-rc3 and I don't know if we'll have an -rc4.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 11 lines · Eric Blake
Quoted 3 lines · Richard W.M. Jones
Hi Eric. Is this a bug?
Note that with qemu as server:
$ truncate --size=1023 file $ qemu-nbd -f raw -x '' file
Quoted 7 lines
the client now sees:
[email protected]:nbd_opt_go_info_block_size Block sizes are
0x200,0x1000,0x2000000
...
[email protected]:nbd_receive_negotiate_size_flags Size is 1024, export flags0x1edwhere qemu as server is 0-padding the source file in all read requests; and that probably explains why I haven't noticed the problem with unaligned images in the client before (since qemu as NBD server doesn't advertise unaligned images). What's more, a write request to the tail of the partial sector (when not using a read-only connection) succeeds, and resizes the file in the process (at least, on protocols that support live resizing); but I wouldn't count on it behaving sanely for all protocols.
What's more, with qemu as server, then 'qemu-img map' as client dies because the server is sending incorrect data according to protocol.
Quoted 1 line
At any rate, you've given me plenty to work with for fixing things, although it probably won't make it into 3.0-rc3 and I don't know if we'll have an -rc4.
Not a regression introduce in 3.0 (it exists in 2.12), so not serious enough of a show-stopper to cause an -rc4 release.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org