1#!/bin/bash 2# 3# Test reading dirty bitmap over NBD 4# 5# Copyright (C) 2018 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 _cleanup_qemu 30 rm -f "$TEST_DIR/nbd" 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.qemu 38 39_supported_fmt qcow2 40_supported_proto file # uses NBD as well 41_supported_os Linux 42# Persistent dirty bitmaps require compat=1.1 43_unsupported_imgopts 'compat=0.10' 44 45do_run_qemu() 46{ 47 echo Testing: "$@" 48 $QEMU -nographic -qmp stdio -serial none "$@" 49 echo 50} 51 52run_qemu() 53{ 54 do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_qmp \ 55 | _filter_qemu | _filter_imgfmt \ 56 | _filter_actual_image_size 57} 58 59echo 60echo "=== Create partially sparse image, then add dirty bitmaps ===" 61echo 62 63# Two bitmaps, to contrast granularity issues 64_make_test_img -o cluster_size=4k 4M 65$QEMU_IO -c 'w -P 0x11 1M 2M' "$TEST_IMG" | _filter_qemu_io 66run_qemu <<EOF 67{ "execute": "qmp_capabilities" } 68{ "execute": "blockdev-add", 69 "arguments": { 70 "driver": "$IMGFMT", 71 "node-name": "n", 72 "file": { 73 "driver": "file", 74 "filename": "$TEST_IMG" 75 } 76 } 77} 78{ "execute": "block-dirty-bitmap-add", 79 "arguments": { 80 "node": "n", 81 "name": "b", 82 "persistent": true, 83 "granularity": 65536 84 } 85} 86{ "execute": "block-dirty-bitmap-add", 87 "arguments": { 88 "node": "n", 89 "name": "b2", 90 "persistent": true, 91 "granularity": 512 92 } 93} 94{ "execute": "quit" } 95EOF 96 97echo 98echo "=== Write part of the file under active bitmap ===" 99echo 100 101$QEMU_IO -c 'w -P 0x22 512 512' -c 'w -P 0x33 2M 2M' "$TEST_IMG" \ 102 | _filter_qemu_io 103 104echo 105echo "=== End dirty bitmaps, and start serving image over NBD ===" 106echo 107 108_launch_qemu 2> >(_filter_nbd) 109 110silent= 111_send_qemu_cmd $QEMU_HANDLE '{"execute":"qmp_capabilities"}' "return" 112_send_qemu_cmd $QEMU_HANDLE '{"execute":"blockdev-add", 113 "arguments":{"driver":"qcow2", "node-name":"n", 114 "file":{"driver":"file", "filename":"'"$TEST_IMG"'"}}}' "return" 115_send_qemu_cmd $QEMU_HANDLE '{"execute":"x-block-dirty-bitmap-disable", 116 "arguments":{"node":"n", "name":"b"}}' "return" 117_send_qemu_cmd $QEMU_HANDLE '{"execute":"x-block-dirty-bitmap-disable", 118 "arguments":{"node":"n", "name":"b2"}}' "return" 119_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-start", 120 "arguments":{"addr":{"type":"unix", 121 "data":{"path":"'"$TEST_DIR/nbd"'"}}}}' "return" 122_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 123 "arguments":{"device":"n"}}' "return" 124_send_qemu_cmd $QEMU_HANDLE '{"execute":"x-nbd-server-add-bitmap", 125 "arguments":{"name":"n", "bitmap":"b"}}' "return" 126_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 127 "arguments":{"device":"n", "name":"n2"}}' "return" 128_send_qemu_cmd $QEMU_HANDLE '{"execute":"x-nbd-server-add-bitmap", 129 "arguments":{"name":"n2", "bitmap":"b2"}}' "return" 130 131echo 132echo "=== Contrast normal status to large granularity dirty-bitmap ===" 133echo 134 135QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT 136IMG="driver=nbd,export=n,server.type=unix,server.path=$TEST_DIR/nbd" 137$QEMU_IO -r -c 'r -P 0x22 512 512' -c 'r -P 0 512k 512k' -c 'r -P 0x11 1m 1m' \ 138 -c 'r -P 0x33 2m 2m' --image-opts "$IMG" | _filter_qemu_io 139$QEMU_IMG map --output=json --image-opts \ 140 "$IMG" | _filter_qemu_img_map 141$QEMU_IMG map --output=json --image-opts \ 142 "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b" | _filter_qemu_img_map 143 144echo 145echo "=== Contrast to small granularity dirty-bitmap ===" 146echo 147 148IMG="driver=nbd,export=n2,server.type=unix,server.path=$TEST_DIR/nbd" 149$QEMU_IMG map --output=json --image-opts \ 150 "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b2" | _filter_qemu_img_map 151 152echo 153echo "=== End NBD server ===" 154echo 155 156_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-remove", 157 "arguments":{"name":"n"}}' "return" 158_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-remove", 159 "arguments":{"name":"n2"}}' "return" 160_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-stop"}' "return" 161_send_qemu_cmd $QEMU_HANDLE '{"execute":"quit"}' "return" 162 163# success, all done 164echo '*** done' 165rm -f $seq.full 166status=0 167