1#!/usr/bin/env bash
2# group: rw auto quick
3#
4# Test qemu-nbd -A
5#
6# Copyright (C) 2018-2021 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22seq="$(basename $0)"
23echo "QA output created by $seq"
24
25status=1 # failure is the default!
26
27_cleanup()
28{
29    _cleanup_test_img
30    nbd_server_stop
31}
32trap "_cleanup; exit \$status" 0 1 2 3 15
33
34# get standard environment, filters and checks
35cd ..
36. ./common.rc
37. ./common.filter
38. ./common.nbd
39
40_supported_fmt qcow2
41_supported_proto file
42_supported_os Linux
43_require_command QEMU_NBD
44
45echo
46echo "=== Initial image setup ==="
47echo
48
49TEST_IMG="$TEST_IMG.base" _make_test_img 4M
50$QEMU_IO -c 'w 0 2M' -f $IMGFMT "$TEST_IMG.base" | _filter_qemu_io
51_make_test_img -b "$TEST_IMG.base" -F $IMGFMT 4M
52$QEMU_IO -c 'w 1M 2M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
53
54echo
55echo "=== Check allocation over NBD ==="
56echo
57
58$QEMU_IMG map --output=json -f qcow2 "$TEST_IMG"
59IMG="driver=nbd,server.type=unix,server.path=$nbd_unix_socket"
60nbd_server_start_unix_socket -r -f qcow2 -A "$TEST_IMG"
61# Inspect what the server is exposing
62$QEMU_NBD --list -k $nbd_unix_socket
63# Normal -f raw NBD block status loses access to allocation information
64$QEMU_IMG map --output=json --image-opts \
65    "$IMG" | _filter_qemu_img_map
66# But when we use -A, coupled with x-dirty-bitmap in the client for feeding
67# 2-bit block status from an alternative NBD metadata context (note that
68# the client code for x-dirty-bitmap intentionally collapses all depths
69# beyond 2 into a single value), we can determine:
70#    unallocated (depth 0) => "zero":false, "data":true
71#    local (depth 1)       => "zero":false, "data":false
72#    backing (depth 2+)    => "zero":true,  "data":true
73$QEMU_IMG map --output=json --image-opts \
74    "$IMG,x-dirty-bitmap=qemu:allocation-depth" | _filter_qemu_img_map
75# More accurate results can be obtained by other NBD clients such as
76# libnbd, but this test works without such external dependencies.
77
78# success, all done
79echo '*** done'
80rm -f $seq.full
81status=0
82