Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / interop / interop-qemu-block-size.sh
blob81bda173805f7c25df5797ad683fbfeb1199554d
1 #!/usr/bin/env bash
2 # nbd client library in userspace
3 # Copyright Red Hat
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 # Test interop with qemu-nbd block sizes.
21 source ../tests/functions.sh
22 set -e
23 set -x
25 # versions of qemu-nbd older than 4.0 reported inaccurate block sizes
26 # Use 'qemu-nbd --list' as our witness, it was also added in 4.0
27 requires qemu-nbd --list --version
28 requires nbdsh --version
29 requires qemu-img --version
30 requires truncate --version
31 requires timeout --version
33 f="qemu-block-size.raw"
34 sock=$(mktemp -u /tmp/interop-qemu.XXXXXX)
35 rm -f $f $sock
36 cleanup_fn rm -f $f $sock
38 truncate --size=10M $f
39 export sock
40 fail=0
42 # run_test FMT REQUEST EXP_INFO EXP_GO
43 run_test() {
44 rm -f $sock
45 # No -t or -e, so qemu-nbd should exit once nbdsh disconnects
46 timeout 60s qemu-nbd -k $sock $1 $f &
47 pid=$!
48 # Wait for the socket to appear
49 for i in {1..30}; do
50 if test -e $sock; then
51 break
53 sleep 1
54 done
55 $VG nbdsh -c - <<EOF
56 import os
58 sock = os.environ["sock"]
60 h.set_opt_mode(True)
61 assert h.get_request_block_size()
62 h.set_request_block_size($2)
63 assert h.get_request_block_size() is $2
64 h.connect_unix(sock)
66 if not h.aio_is_negotiating():
67 h.shutdown()
68 exit(77) # Oldstyle negotiation lacks block size advertisement
70 try:
71 h.opt_info()
72 assert h.get_block_size(nbd.SIZE_MINIMUM) == $3
73 except nbd.Error:
74 assert $3 == 0
76 h.opt_go()
77 assert h.get_block_size(nbd.SIZE_MINIMUM) == $4
79 h.shutdown()
80 EOF
81 st=$?
82 if [ $st = 77 ]; then
83 echo "$0: skipping: qemu-nbd too old for this test"
84 exit 77
85 elif [ $st != 0 ]; then
86 fail=1
88 wait $pid || fail=1
91 # Without '-f raw', qemu-nbd forces sector granularity to prevent writing
92 # to sector 0 from changing the disk type. However, if the client does
93 # not request block sizes, it reports a size then errors out for NBD_OPT_INFO,
94 # while fudging size for NBD_OPT_GO.
95 run_test '' True 512 512
96 run_test '' False 0 1
98 # With '-f raw', qemu-nbd always exposes byte-level granularity for files.
99 run_test '-f raw' True 1 1
100 run_test '-f raw' False 1 1
102 exit $fail