111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2a1532a22SEric Blake# 3a1532a22SEric Blake# Test reading dirty bitmap over NBD 4a1532a22SEric Blake# 5*506902c6SEric Blake# Copyright (C) 2018-2019 Red Hat, Inc. 6a1532a22SEric Blake# 7a1532a22SEric Blake# This program is free software; you can redistribute it and/or modify 8a1532a22SEric Blake# it under the terms of the GNU General Public License as published by 9a1532a22SEric Blake# the Free Software Foundation; either version 2 of the License, or 10a1532a22SEric Blake# (at your option) any later version. 11a1532a22SEric Blake# 12a1532a22SEric Blake# This program is distributed in the hope that it will be useful, 13a1532a22SEric Blake# but WITHOUT ANY WARRANTY; without even the implied warranty of 14a1532a22SEric Blake# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15a1532a22SEric Blake# GNU General Public License for more details. 16a1532a22SEric Blake# 17a1532a22SEric Blake# You should have received a copy of the GNU General Public License 18a1532a22SEric Blake# along with this program. If not, see <http://www.gnu.org/licenses/>. 19a1532a22SEric Blake# 20a1532a22SEric Blake 21a1532a22SEric Blakeseq="$(basename $0)" 22a1532a22SEric Blakeecho "QA output created by $seq" 23a1532a22SEric Blake 24a1532a22SEric Blakestatus=1 # failure is the default! 25a1532a22SEric Blake 26a1532a22SEric Blake_cleanup() 27a1532a22SEric Blake{ 28636192c4SEric Blake nbd_server_stop 29a1532a22SEric Blake _cleanup_test_img 30a1532a22SEric Blake _cleanup_qemu 31a1532a22SEric Blake rm -f "$TEST_DIR/nbd" 32a1532a22SEric Blake} 33a1532a22SEric Blaketrap "_cleanup; exit \$status" 0 1 2 3 15 34a1532a22SEric Blake 35a1532a22SEric Blake# get standard environment, filters and checks 36a1532a22SEric Blake. ./common.rc 37a1532a22SEric Blake. ./common.filter 38a1532a22SEric Blake. ./common.qemu 39636192c4SEric Blake. ./common.nbd 40a1532a22SEric Blake 41a1532a22SEric Blake_supported_fmt qcow2 42a1532a22SEric Blake_supported_proto file # uses NBD as well 43a1532a22SEric Blake_supported_os Linux 44092b9c40SMax Reitz# Persistent dirty bitmaps require compat=1.1 45092b9c40SMax Reitz_unsupported_imgopts 'compat=0.10' 46a1532a22SEric Blake 478cedcffdSEric Blakedo_run_qemu() 48a1532a22SEric Blake{ 49a1532a22SEric Blake echo Testing: "$@" 50a1532a22SEric Blake $QEMU -nographic -qmp stdio -serial none "$@" 51a1532a22SEric Blake echo 52a1532a22SEric Blake} 53a1532a22SEric Blake 548cedcffdSEric Blakerun_qemu() 55a1532a22SEric Blake{ 56a1532a22SEric Blake do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_qmp \ 57a1532a22SEric Blake | _filter_qemu | _filter_imgfmt \ 58a1532a22SEric Blake | _filter_actual_image_size 59a1532a22SEric Blake} 60a1532a22SEric Blake 61a1532a22SEric Blakeecho 62a237dea3SEric Blakeecho "=== Create partially sparse image, then add dirty bitmaps ===" 63a1532a22SEric Blakeecho 64a1532a22SEric Blake 65a237dea3SEric Blake# Two bitmaps, to contrast granularity issues 66702aa50dSEric Blake# Also note that b will be disabled, while b2 is left enabled, to 67702aa50dSEric Blake# check for read-only interactions 68a237dea3SEric Blake_make_test_img -o cluster_size=4k 4M 69a1532a22SEric Blake$QEMU_IO -c 'w -P 0x11 1M 2M' "$TEST_IMG" | _filter_qemu_io 70a1532a22SEric Blakerun_qemu <<EOF 71a1532a22SEric Blake{ "execute": "qmp_capabilities" } 72a1532a22SEric Blake{ "execute": "blockdev-add", 73a1532a22SEric Blake "arguments": { 74a1532a22SEric Blake "driver": "$IMGFMT", 75a1532a22SEric Blake "node-name": "n", 76a1532a22SEric Blake "file": { 77a1532a22SEric Blake "driver": "file", 78a1532a22SEric Blake "filename": "$TEST_IMG" 79a1532a22SEric Blake } 80a1532a22SEric Blake } 81a1532a22SEric Blake} 82a1532a22SEric Blake{ "execute": "block-dirty-bitmap-add", 83a1532a22SEric Blake "arguments": { 84a1532a22SEric Blake "node": "n", 85a1532a22SEric Blake "name": "b", 86a237dea3SEric Blake "persistent": true, 87a237dea3SEric Blake "granularity": 65536 88a237dea3SEric Blake } 89a237dea3SEric Blake} 90a237dea3SEric Blake{ "execute": "block-dirty-bitmap-add", 91a237dea3SEric Blake "arguments": { 92a237dea3SEric Blake "node": "n", 93a237dea3SEric Blake "name": "b2", 94a237dea3SEric Blake "persistent": true, 95a237dea3SEric Blake "granularity": 512 96a1532a22SEric Blake } 97a1532a22SEric Blake} 98a1532a22SEric Blake{ "execute": "quit" } 99a1532a22SEric BlakeEOF 100a1532a22SEric Blake 101a1532a22SEric Blakeecho 102a1532a22SEric Blakeecho "=== Write part of the file under active bitmap ===" 103a1532a22SEric Blakeecho 104a1532a22SEric Blake 105a237dea3SEric Blake$QEMU_IO -c 'w -P 0x22 512 512' -c 'w -P 0x33 2M 2M' "$TEST_IMG" \ 106a237dea3SEric Blake | _filter_qemu_io 107a1532a22SEric Blake 108a1532a22SEric Blakeecho 109a237dea3SEric Blakeecho "=== End dirty bitmaps, and start serving image over NBD ===" 110a1532a22SEric Blakeecho 111a1532a22SEric Blake 112*506902c6SEric Blake_launch_qemu -object iothread,id=io0 2> >(_filter_nbd) 113a1532a22SEric Blake 1142d2fd674SEric Blake# Intentionally provoke some errors as well, to check error handling 115a1532a22SEric Blakesilent= 116a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"qmp_capabilities"}' "return" 117a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"blockdev-add", 118a1532a22SEric Blake "arguments":{"driver":"qcow2", "node-name":"n", 119a1532a22SEric Blake "file":{"driver":"file", "filename":"'"$TEST_IMG"'"}}}' "return" 120*506902c6SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"x-blockdev-set-iothread", 121*506902c6SEric Blake "arguments":{"node-name":"n", "iothread":"io0"}}' "return" 1220e2b7f09SJohn Snow_send_qemu_cmd $QEMU_HANDLE '{"execute":"block-dirty-bitmap-disable", 123a1532a22SEric Blake "arguments":{"node":"n", "name":"b"}}' "return" 1242d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1252d2fd674SEric Blake "arguments":{"device":"n"}}' "error" # Attempt add without server 126a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-start", 127a1532a22SEric Blake "arguments":{"addr":{"type":"unix", 128a1532a22SEric Blake "data":{"path":"'"$TEST_DIR/nbd"'"}}}}' "return" 1292d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-start", 1302d2fd674SEric Blake "arguments":{"addr":{"type":"unix", 1312d2fd674SEric Blake "data":{"path":"'"$TEST_DIR/nbd"1'"}}}}' "error" # Attempt second server 132ddd09448SEric Blake$QEMU_NBD_PROG -L -k "$TEST_DIR/nbd" 133a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1345fcbeb06SEric Blake "arguments":{"device":"n", "bitmap":"b"}}' "return" 1352d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1362d2fd674SEric Blake "arguments":{"device":"nosuch"}}' "error" # Attempt to export missing node 1372d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1382d2fd674SEric Blake "arguments":{"device":"n"}}' "error" # Attempt to export same name twice 139a237dea3SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1405fcbeb06SEric Blake "arguments":{"device":"n", "name":"n2", 1415fcbeb06SEric Blake "bitmap":"b2"}}' "error" # enabled vs. read-only 142702aa50dSEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1435fcbeb06SEric Blake "arguments":{"device":"n", "name":"n2", 1445fcbeb06SEric Blake "bitmap":"b3"}}' "error" # Missing bitmap 1455fcbeb06SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1465fcbeb06SEric Blake "arguments":{"device":"n", "name":"n2", "writable":true, 1475fcbeb06SEric Blake "bitmap":"b2"}}' "return" 148ddd09448SEric Blake$QEMU_NBD_PROG -L -k "$TEST_DIR/nbd" 149a1532a22SEric Blake 150a1532a22SEric Blakeecho 151a237dea3SEric Blakeecho "=== Contrast normal status to large granularity dirty-bitmap ===" 152a1532a22SEric Blakeecho 153a1532a22SEric Blake 154a1532a22SEric BlakeQEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT 155a1532a22SEric BlakeIMG="driver=nbd,export=n,server.type=unix,server.path=$TEST_DIR/nbd" 156a237dea3SEric Blake$QEMU_IO -r -c 'r -P 0x22 512 512' -c 'r -P 0 512k 512k' -c 'r -P 0x11 1m 1m' \ 157a237dea3SEric Blake -c 'r -P 0x33 2m 2m' --image-opts "$IMG" | _filter_qemu_io 158a1532a22SEric Blake$QEMU_IMG map --output=json --image-opts \ 159a1532a22SEric Blake "$IMG" | _filter_qemu_img_map 160a1532a22SEric Blake$QEMU_IMG map --output=json --image-opts \ 161a1532a22SEric Blake "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b" | _filter_qemu_img_map 162a1532a22SEric Blake 163a1532a22SEric Blakeecho 164a237dea3SEric Blakeecho "=== Contrast to small granularity dirty-bitmap ===" 165a237dea3SEric Blakeecho 166a237dea3SEric Blake 167a237dea3SEric BlakeIMG="driver=nbd,export=n2,server.type=unix,server.path=$TEST_DIR/nbd" 168a237dea3SEric Blake$QEMU_IMG map --output=json --image-opts \ 169a237dea3SEric Blake "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b2" | _filter_qemu_img_map 170a237dea3SEric Blake 171a237dea3SEric Blakeecho 172636192c4SEric Blakeecho "=== End qemu NBD server ===" 173a1532a22SEric Blakeecho 174a1532a22SEric Blake 175a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-remove", 176a1532a22SEric Blake "arguments":{"name":"n"}}' "return" 177a237dea3SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-remove", 178a237dea3SEric Blake "arguments":{"name":"n2"}}' "return" 1792d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-remove", 1802d2fd674SEric Blake "arguments":{"name":"n2"}}' "error" # Attempt duplicate clean 181a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-stop"}' "return" 1827801c3a7SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-stop"}' "error" # Again 183a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"quit"}' "return" 184054be360SEric Blakewait=yes _cleanup_qemu 185a1532a22SEric Blake 186636192c4SEric Blakeecho 187636192c4SEric Blakeecho "=== Use qemu-nbd as server ===" 188636192c4SEric Blakeecho 189636192c4SEric Blake 190636192c4SEric Blakenbd_server_start_unix_socket -r -f $IMGFMT -B b "$TEST_IMG" 191636192c4SEric BlakeIMG="driver=nbd,server.type=unix,server.path=$nbd_unix_socket" 192636192c4SEric Blake$QEMU_IMG map --output=json --image-opts \ 193636192c4SEric Blake "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b" | _filter_qemu_img_map 194636192c4SEric Blake 195636192c4SEric Blakenbd_server_start_unix_socket -f $IMGFMT -B b2 "$TEST_IMG" 196636192c4SEric BlakeIMG="driver=nbd,server.type=unix,server.path=$nbd_unix_socket" 197636192c4SEric Blake$QEMU_IMG map --output=json --image-opts \ 198636192c4SEric Blake "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b2" | _filter_qemu_img_map 199636192c4SEric Blake 200a1532a22SEric Blake# success, all done 201a1532a22SEric Blakeecho '*** done' 202a1532a22SEric Blakerm -f $seq.full 203a1532a22SEric Blakestatus=0 204