1#!/usr/bin/env bash
2#
3# Test qemu-nbd vs. unaligned images
4#
5# Copyright (C) 2018-2019 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21seq="$(basename $0)"
22echo "QA output created by $seq"
23
24status=1 # failure is the default!
25
26_cleanup()
27{
28    _cleanup_test_img
29    rm -f "$TEST_DIR/server.log"
30    nbd_server_stop
31}
32trap "_cleanup; exit \$status" 0 1 2 3 15
33
34# get standard environment, filters and checks
35. ./common.rc
36. ./common.filter
37. ./common.nbd
38
39_supported_fmt raw
40_supported_proto nbd
41_supported_os Linux
42_require_command QEMU_NBD
43
44# can't use _make_test_img, because qemu-img rounds image size up,
45# and because we want to use Unix socket rather than TCP port. Likewise,
46# we have to redirect TEST_IMG to our server.
47# This tests that we can deal with the hole at the end of an unaligned
48# raw file (either because the server doesn't advertise alignment too
49# large, or because the client ignores the server's noncompliance - even
50# though we can't actually wire iotests into checking trace messages).
51printf %01000d 0 > "$TEST_IMG_FILE"
52TEST_IMG="nbd:unix:$nbd_unix_socket"
53
54echo
55echo "=== Exporting unaligned raw image, natural alignment ==="
56echo
57
58nbd_server_start_unix_socket -f $IMGFMT "$TEST_IMG_FILE"
59
60$QEMU_NBD_PROG --list -k $nbd_unix_socket | grep '\(size\|min\)'
61$QEMU_IMG map -f raw --output=json "$TEST_IMG" | _filter_qemu_img_map
62$QEMU_IO -f raw -c map "$TEST_IMG"
63nbd_server_stop
64
65echo
66echo "=== Exporting unaligned raw image, forced server sector alignment ==="
67echo
68
69# Intentionally omit '-f' to force image probing, which in turn forces
70# sector alignment, here at the server.
71nbd_server_start_unix_socket "$TEST_IMG_FILE" 2> "$TEST_DIR/server.log"
72
73$QEMU_NBD_PROG --list -k $nbd_unix_socket | grep '\(size\|min\)'
74$QEMU_IMG map -f raw --output=json "$TEST_IMG" | _filter_qemu_img_map
75$QEMU_IO -f raw -c map "$TEST_IMG"
76nbd_server_stop
77cat "$TEST_DIR/server.log" | _filter_testdir
78
79echo
80echo "=== Exporting unaligned raw image, forced client sector alignment ==="
81echo
82
83# Now force sector alignment at the client.
84nbd_server_start_unix_socket -f $IMGFMT "$TEST_IMG_FILE"
85
86$QEMU_NBD_PROG --list -k $nbd_unix_socket | grep '\(size\|min\)'
87$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map
88$QEMU_IO -c map "$TEST_IMG"
89nbd_server_stop
90
91# Not tested yet: we also want to ensure that qemu as NBD client does
92# not access beyond the end of a server's advertised unaligned size:
93#  nbdkit -U - memory size=513 --run 'qemu-io -f raw -c "r 512 512" $nbd'
94# However, since qemu as server always rounds up to a sector alignment,
95# we would have to use nbdkit to provoke the current client failures.
96
97# success, all done
98echo '*** done'
99rm -f $seq.full
100status=0
101