111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw quick 3a1532a22SEric Blake# 4a1532a22SEric Blake# Test reading dirty bitmap over NBD 5a1532a22SEric Blake# 6d8154b09SEric Blake# Copyright (C) 2018-2020 Red Hat, Inc. 7a1532a22SEric Blake# 8a1532a22SEric Blake# This program is free software; you can redistribute it and/or modify 9a1532a22SEric Blake# it under the terms of the GNU General Public License as published by 10a1532a22SEric Blake# the Free Software Foundation; either version 2 of the License, or 11a1532a22SEric Blake# (at your option) any later version. 12a1532a22SEric Blake# 13a1532a22SEric Blake# This program is distributed in the hope that it will be useful, 14a1532a22SEric Blake# but WITHOUT ANY WARRANTY; without even the implied warranty of 15a1532a22SEric Blake# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16a1532a22SEric Blake# GNU General Public License for more details. 17a1532a22SEric Blake# 18a1532a22SEric Blake# You should have received a copy of the GNU General Public License 19a1532a22SEric Blake# along with this program. If not, see <http://www.gnu.org/licenses/>. 20a1532a22SEric Blake# 21a1532a22SEric Blake 22a1532a22SEric Blakeseq="$(basename $0)" 23a1532a22SEric Blakeecho "QA output created by $seq" 24a1532a22SEric Blake 25a1532a22SEric Blakestatus=1 # failure is the default! 26a1532a22SEric Blake 27a1532a22SEric Blake_cleanup() 28a1532a22SEric Blake{ 29636192c4SEric Blake nbd_server_stop 30a1532a22SEric Blake _cleanup_test_img 31a1532a22SEric Blake _cleanup_qemu 32135a4663SMax Reitz rm -f "$SOCK_DIR/nbd" 33a1532a22SEric Blake} 34a1532a22SEric Blaketrap "_cleanup; exit \$status" 0 1 2 3 15 35a1532a22SEric Blake 36a1532a22SEric Blake# get standard environment, filters and checks 37a1532a22SEric Blake. ./common.rc 38a1532a22SEric Blake. ./common.filter 39a1532a22SEric Blake. ./common.qemu 40636192c4SEric Blake. ./common.nbd 41a1532a22SEric Blake 42a1532a22SEric Blake_supported_fmt qcow2 43a1532a22SEric Blake_supported_proto file # uses NBD as well 44a1532a22SEric Blake_supported_os Linux 45092b9c40SMax Reitz# Persistent dirty bitmaps require compat=1.1 46092b9c40SMax Reitz_unsupported_imgopts 'compat=0.10' 47a1532a22SEric Blake 488cedcffdSEric Blakedo_run_qemu() 49a1532a22SEric Blake{ 50a1532a22SEric Blake echo Testing: "$@" 51a1532a22SEric Blake $QEMU -nographic -qmp stdio -serial none "$@" 52a1532a22SEric Blake echo 53a1532a22SEric Blake} 54a1532a22SEric Blake 558cedcffdSEric Blakerun_qemu() 56a1532a22SEric Blake{ 57a1532a22SEric Blake do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_qmp \ 58a1532a22SEric Blake | _filter_qemu | _filter_imgfmt \ 59a1532a22SEric Blake | _filter_actual_image_size 60a1532a22SEric Blake} 61a1532a22SEric Blake 62a1532a22SEric Blakeecho 63a237dea3SEric Blakeecho "=== Create partially sparse image, then add dirty bitmaps ===" 64a1532a22SEric Blakeecho 65a1532a22SEric Blake 66a237dea3SEric Blake# Two bitmaps, to contrast granularity issues 67702aa50dSEric Blake# Also note that b will be disabled, while b2 is left enabled, to 68702aa50dSEric Blake# check for read-only interactions 69a237dea3SEric Blake_make_test_img -o cluster_size=4k 4M 70a1532a22SEric Blake$QEMU_IO -c 'w -P 0x11 1M 2M' "$TEST_IMG" | _filter_qemu_io 71a1532a22SEric Blakerun_qemu <<EOF 72a1532a22SEric Blake{ "execute": "qmp_capabilities" } 73a1532a22SEric Blake{ "execute": "blockdev-add", 74a1532a22SEric Blake "arguments": { 75a1532a22SEric Blake "driver": "$IMGFMT", 76a1532a22SEric Blake "node-name": "n", 77a1532a22SEric Blake "file": { 78a1532a22SEric Blake "driver": "file", 79a1532a22SEric Blake "filename": "$TEST_IMG" 80a1532a22SEric Blake } 81a1532a22SEric Blake } 82a1532a22SEric Blake} 83a1532a22SEric Blake{ "execute": "block-dirty-bitmap-add", 84a1532a22SEric Blake "arguments": { 85a1532a22SEric Blake "node": "n", 86a1532a22SEric Blake "name": "b", 87a237dea3SEric Blake "persistent": true, 88a237dea3SEric Blake "granularity": 65536 89a237dea3SEric Blake } 90a237dea3SEric Blake} 91a237dea3SEric Blake{ "execute": "block-dirty-bitmap-add", 92a237dea3SEric Blake "arguments": { 93a237dea3SEric Blake "node": "n", 94a237dea3SEric Blake "name": "b2", 95a237dea3SEric Blake "persistent": true, 96a237dea3SEric Blake "granularity": 512 97a1532a22SEric Blake } 98a1532a22SEric Blake} 99a1532a22SEric Blake{ "execute": "quit" } 100a1532a22SEric BlakeEOF 101a1532a22SEric Blake 102a1532a22SEric Blakeecho 103a1532a22SEric Blakeecho "=== Write part of the file under active bitmap ===" 104a1532a22SEric Blakeecho 105a1532a22SEric Blake 106a237dea3SEric Blake$QEMU_IO -c 'w -P 0x22 512 512' -c 'w -P 0x33 2M 2M' "$TEST_IMG" \ 107a237dea3SEric Blake | _filter_qemu_io 108a1532a22SEric Blake 109a1532a22SEric Blakeecho 110a237dea3SEric Blakeecho "=== End dirty bitmaps, and start serving image over NBD ===" 111a1532a22SEric Blakeecho 112a1532a22SEric Blake 113506902c6SEric Blake_launch_qemu -object iothread,id=io0 2> >(_filter_nbd) 114a1532a22SEric Blake 1152d2fd674SEric Blake# Intentionally provoke some errors as well, to check error handling 116a1532a22SEric Blakesilent= 117a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"qmp_capabilities"}' "return" 118a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"blockdev-add", 119a1532a22SEric Blake "arguments":{"driver":"qcow2", "node-name":"n", 120a1532a22SEric Blake "file":{"driver":"file", "filename":"'"$TEST_IMG"'"}}}' "return" 1210e2b7f09SJohn Snow_send_qemu_cmd $QEMU_HANDLE '{"execute":"block-dirty-bitmap-disable", 122a1532a22SEric Blake "arguments":{"node":"n", "name":"b"}}' "return" 123*c08c220bSVladimir Sementsov-Ogievskiy_send_qemu_cmd $QEMU_HANDLE '{"execute":"blockdev-add", 124*c08c220bSVladimir Sementsov-Ogievskiy "arguments":{"driver":"null-co", "node-name":"null", 125*c08c220bSVladimir Sementsov-Ogievskiy "size": 4194304}}' "return" 126*c08c220bSVladimir Sementsov-Ogievskiy_send_qemu_cmd $QEMU_HANDLE '{"execute":"block-dirty-bitmap-add", 127*c08c220bSVladimir Sementsov-Ogievskiy "arguments":{"node":"null", "name":"b3"}}' "return" 128296416ffSEric Blake 129296416ffSEric Blakefor attempt in normal iothread; do 130296416ffSEric Blake 131296416ffSEric Blakeecho 132296416ffSEric Blakeecho "=== Set up NBD with $attempt access ===" 133296416ffSEric Blakeecho 134296416ffSEric Blakeif [ $attempt = iothread ]; then 135296416ffSEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"x-blockdev-set-iothread", 136296416ffSEric Blake "arguments":{"node-name":"n", "iothread":"io0"}}' "return" 137296416ffSEric Blakefi 138296416ffSEric Blake 1392d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1402d2fd674SEric Blake "arguments":{"device":"n"}}' "error" # Attempt add without server 141a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-start", 142a1532a22SEric Blake "arguments":{"addr":{"type":"unix", 143135a4663SMax Reitz "data":{"path":"'"$SOCK_DIR/nbd"'"}}}}' "return" 1442d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-start", 1452d2fd674SEric Blake "arguments":{"addr":{"type":"unix", 146135a4663SMax Reitz "data":{"path":"'"$SOCK_DIR/nbd"1'"}}}}' "error" # Attempt second server 147135a4663SMax Reitz$QEMU_NBD_PROG -L -k "$SOCK_DIR/nbd" 148a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1495fcbeb06SEric Blake "arguments":{"device":"n", "bitmap":"b"}}' "return" 1502d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1512d2fd674SEric Blake "arguments":{"device":"nosuch"}}' "error" # Attempt to export missing node 1522d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1532d2fd674SEric Blake "arguments":{"device":"n"}}' "error" # Attempt to export same name twice 154a237dea3SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1555fcbeb06SEric Blake "arguments":{"device":"n", "name":"n2", 1565fcbeb06SEric Blake "bitmap":"b2"}}' "error" # enabled vs. read-only 157702aa50dSEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1585fcbeb06SEric Blake "arguments":{"device":"n", "name":"n2", 1595fcbeb06SEric Blake "bitmap":"b3"}}' "error" # Missing bitmap 1605fcbeb06SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", 1615fcbeb06SEric Blake "arguments":{"device":"n", "name":"n2", "writable":true, 162deb6ccb0SEric Blake "description":"some text", "bitmap":"b2"}}' "return" 163*c08c220bSVladimir Sementsov-Ogievskiy_send_qemu_cmd $QEMU_HANDLE '{"execute":"block-export-add", 164*c08c220bSVladimir Sementsov-Ogievskiy "arguments":{"type": "nbd", "node-name":"n", "id":"n3", "name": "n3", 165*c08c220bSVladimir Sementsov-Ogievskiy "bitmaps":[{"node":"null","name":"b3"}]}}' "return" 166135a4663SMax Reitz$QEMU_NBD_PROG -L -k "$SOCK_DIR/nbd" 167a1532a22SEric Blake 168a1532a22SEric Blakeecho 169a237dea3SEric Blakeecho "=== Contrast normal status to large granularity dirty-bitmap ===" 170a1532a22SEric Blakeecho 171a1532a22SEric Blake 172a1532a22SEric BlakeQEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT 173135a4663SMax ReitzIMG="driver=nbd,export=n,server.type=unix,server.path=$SOCK_DIR/nbd" 174a237dea3SEric Blake$QEMU_IO -r -c 'r -P 0x22 512 512' -c 'r -P 0 512k 512k' -c 'r -P 0x11 1m 1m' \ 175a237dea3SEric Blake -c 'r -P 0x33 2m 2m' --image-opts "$IMG" | _filter_qemu_io 176a1532a22SEric Blake$QEMU_IMG map --output=json --image-opts \ 177a1532a22SEric Blake "$IMG" | _filter_qemu_img_map 178a1532a22SEric Blake$QEMU_IMG map --output=json --image-opts \ 179a1532a22SEric Blake "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b" | _filter_qemu_img_map 180a1532a22SEric Blake 181a1532a22SEric Blakeecho 182a237dea3SEric Blakeecho "=== Contrast to small granularity dirty-bitmap ===" 183a237dea3SEric Blakeecho 184a237dea3SEric Blake 185135a4663SMax ReitzIMG="driver=nbd,export=n2,server.type=unix,server.path=$SOCK_DIR/nbd" 186a237dea3SEric Blake$QEMU_IMG map --output=json --image-opts \ 187a237dea3SEric Blake "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b2" | _filter_qemu_img_map 188a237dea3SEric Blake 189a237dea3SEric Blakeecho 190*c08c220bSVladimir Sementsov-Ogievskiyecho "=== Check bitmap taken from another node ===" 191*c08c220bSVladimir Sementsov-Ogievskiyecho 192*c08c220bSVladimir Sementsov-Ogievskiy 193*c08c220bSVladimir Sementsov-OgievskiyIMG="driver=nbd,export=n3,server.type=unix,server.path=$SOCK_DIR/nbd" 194*c08c220bSVladimir Sementsov-Ogievskiy$QEMU_IMG map --output=json --image-opts \ 195*c08c220bSVladimir Sementsov-Ogievskiy "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b3" | _filter_qemu_img_map 196*c08c220bSVladimir Sementsov-Ogievskiy 197*c08c220bSVladimir Sementsov-Ogievskiyecho 198636192c4SEric Blakeecho "=== End qemu NBD server ===" 199a1532a22SEric Blakeecho 200a1532a22SEric Blake 201a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-remove", 202a1532a22SEric Blake "arguments":{"name":"n"}}' "return" 203a237dea3SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-remove", 204a237dea3SEric Blake "arguments":{"name":"n2"}}' "return" 2052d2fd674SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-remove", 2062d2fd674SEric Blake "arguments":{"name":"n2"}}' "error" # Attempt duplicate clean 207a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-stop"}' "return" 2087801c3a7SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-stop"}' "error" # Again 209296416ffSEric Blake 210296416ffSEric Blakedone 211296416ffSEric Blake 212a1532a22SEric Blake_send_qemu_cmd $QEMU_HANDLE '{"execute":"quit"}' "return" 213054be360SEric Blakewait=yes _cleanup_qemu 214a1532a22SEric Blake 215636192c4SEric Blakeecho 216636192c4SEric Blakeecho "=== Use qemu-nbd as server ===" 217636192c4SEric Blakeecho 218636192c4SEric Blake 219636192c4SEric Blakenbd_server_start_unix_socket -r -f $IMGFMT -B b "$TEST_IMG" 220636192c4SEric BlakeIMG="driver=nbd,server.type=unix,server.path=$nbd_unix_socket" 221636192c4SEric Blake$QEMU_IMG map --output=json --image-opts \ 222636192c4SEric Blake "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b" | _filter_qemu_img_map 223636192c4SEric Blake 224636192c4SEric Blakenbd_server_start_unix_socket -f $IMGFMT -B b2 "$TEST_IMG" 225636192c4SEric BlakeIMG="driver=nbd,server.type=unix,server.path=$nbd_unix_socket" 226d8154b09SEric Blake$QEMU_IMG map --output=json --image-opts --max-length=12345 \ 227d8154b09SEric Blake "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b2" | _filter_qemu_img_map 228d8154b09SEric Blake$QEMU_IMG map --output=json --image-opts --start-offset=12345 \ 229636192c4SEric Blake "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b2" | _filter_qemu_img_map 230636192c4SEric Blake 231a1532a22SEric Blake# success, all done 232a1532a22SEric Blakeecho '*** done' 233a1532a22SEric Blakerm -f $seq.full 234a1532a22SEric Blakestatus=0 235