1#!/usr/bin/env bash
2#
3# Test qemu-nbd -A
4#
5# Copyright (C) 2018-2020 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    nbd_server_stop
30}
31trap "_cleanup; exit \$status" 0 1 2 3 15
32
33# get standard environment, filters and checks
34. ./common.rc
35. ./common.filter
36. ./common.nbd
37
38_supported_fmt qcow2
39_supported_proto file
40_supported_os Linux
41_require_command QEMU_NBD
42
43echo
44echo "=== Initial image setup ==="
45echo
46
47TEST_IMG="$TEST_IMG.base" _make_test_img 4M
48$QEMU_IO -c 'w 0 2M' -f $IMGFMT "$TEST_IMG.base" | _filter_qemu_io
49_make_test_img -b "$TEST_IMG.base" -F $IMGFMT 4M
50$QEMU_IO -c 'w 1M 2M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
51
52echo
53echo "=== Check allocation over NBD ==="
54echo
55
56$QEMU_IMG map --output=json -f qcow2 "$TEST_IMG"
57IMG="driver=nbd,server.type=unix,server.path=$nbd_unix_socket"
58nbd_server_start_unix_socket -r -f qcow2 -A "$TEST_IMG"
59# Normal -f raw NBD block status loses access to allocation information
60$QEMU_IMG map --output=json --image-opts \
61    "$IMG" | _filter_qemu_img_map
62# But when we use -A, coupled with x-dirty-bitmap in the client for feeding
63# 2-bit block status from an alternative NBD metadata context (note that
64# the client code for x-dirty-bitmap intentionally collapses all depths
65# beyond 2 into a single value), we can determine:
66#    unallocated (depth 0) => "zero":false, "data":true
67#    local (depth 1)       => "zero":false, "data":false
68#    backing (depth 2+)    => "zero":true,  "data":true
69$QEMU_IMG map --output=json --image-opts \
70    "$IMG,x-dirty-bitmap=qemu:allocation-depth" | _filter_qemu_img_map
71# More accurate results can be obtained by other NBD clients such as
72# libnbd, but this test works without such external dependencies.
73
74# success, all done
75echo '*** done'
76rm -f $seq.full
77status=0
78