1ef9bba14SAlberto Garcia#!/usr/bin/env bash 2ef9bba14SAlberto Garcia# 3ef9bba14SAlberto Garcia# Test drive-mirror with quorum 4ef9bba14SAlberto Garcia# 5ef9bba14SAlberto Garcia# The goal of this test is to check how the quorum driver reports 6ef9bba14SAlberto Garcia# regions that are known to read as zeroes (BDRV_BLOCK_ZERO). The idea 7ef9bba14SAlberto Garcia# is that drive-mirror will try the efficient representation of zeroes 8ef9bba14SAlberto Garcia# in the destination image instead of writing actual zeroes. 9ef9bba14SAlberto Garcia# 10ef9bba14SAlberto Garcia# Copyright (C) 2020 Igalia, S.L. 11ef9bba14SAlberto Garcia# Author: Alberto Garcia <berto@igalia.com> 12ef9bba14SAlberto Garcia# 13ef9bba14SAlberto Garcia# This program is free software; you can redistribute it and/or modify 14ef9bba14SAlberto Garcia# it under the terms of the GNU General Public License as published by 15ef9bba14SAlberto Garcia# the Free Software Foundation; either version 2 of the License, or 16ef9bba14SAlberto Garcia# (at your option) any later version. 17ef9bba14SAlberto Garcia# 18ef9bba14SAlberto Garcia# This program is distributed in the hope that it will be useful, 19ef9bba14SAlberto Garcia# but WITHOUT ANY WARRANTY; without even the implied warranty of 20ef9bba14SAlberto Garcia# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21ef9bba14SAlberto Garcia# GNU General Public License for more details. 22ef9bba14SAlberto Garcia# 23ef9bba14SAlberto Garcia# You should have received a copy of the GNU General Public License 24ef9bba14SAlberto Garcia# along with this program. If not, see <http://www.gnu.org/licenses/>. 25ef9bba14SAlberto Garcia# 26ef9bba14SAlberto Garcia 27ef9bba14SAlberto Garcia# creator 28ef9bba14SAlberto Garciaowner=berto@igalia.com 29ef9bba14SAlberto Garcia 30ef9bba14SAlberto Garciaseq=`basename $0` 31ef9bba14SAlberto Garciaecho "QA output created by $seq" 32ef9bba14SAlberto Garcia 33ef9bba14SAlberto Garciastatus=1 # failure is the default! 34ef9bba14SAlberto Garcia 35ef9bba14SAlberto Garcia_cleanup() 36ef9bba14SAlberto Garcia{ 37ef9bba14SAlberto Garcia _rm_test_img "$TEST_IMG.0" 38ef9bba14SAlberto Garcia _rm_test_img "$TEST_IMG.1" 39ef9bba14SAlberto Garcia _rm_test_img "$TEST_IMG.2" 40ef9bba14SAlberto Garcia _rm_test_img "$TEST_IMG.3" 41ef9bba14SAlberto Garcia _cleanup_qemu 42ef9bba14SAlberto Garcia} 43ef9bba14SAlberto Garciatrap "_cleanup; exit \$status" 0 1 2 3 15 44ef9bba14SAlberto Garcia 45ef9bba14SAlberto Garcia# get standard environment, filters and checks 46ef9bba14SAlberto Garcia. ./common.rc 47ef9bba14SAlberto Garcia. ./common.filter 48ef9bba14SAlberto Garcia. ./common.qemu 49ef9bba14SAlberto Garcia 50ef9bba14SAlberto Garcia_supported_fmt qcow2 51ef9bba14SAlberto Garcia_supported_proto file 52ef9bba14SAlberto Garcia_supported_os Linux 53ef9bba14SAlberto Garcia_unsupported_imgopts cluster_size data_file 54ef9bba14SAlberto Garcia 55ef9bba14SAlberto Garciaecho 56ef9bba14SAlberto Garciaecho '### Create all images' # three source (quorum), one destination 57ef9bba14SAlberto Garciaecho 58ef9bba14SAlberto GarciaTEST_IMG="$TEST_IMG.0" _make_test_img -o cluster_size=64k 10M 59ef9bba14SAlberto GarciaTEST_IMG="$TEST_IMG.1" _make_test_img -o cluster_size=64k 10M 60ef9bba14SAlberto GarciaTEST_IMG="$TEST_IMG.2" _make_test_img -o cluster_size=64k 10M 61ef9bba14SAlberto GarciaTEST_IMG="$TEST_IMG.3" _make_test_img -o cluster_size=64k 10M 62ef9bba14SAlberto Garcia 63ef9bba14SAlberto Garciaquorum="driver=raw,file.driver=quorum,file.vote-threshold=2" 64ef9bba14SAlberto Garciaquorum="$quorum,file.children.0.file.filename=$TEST_IMG.0" 65ef9bba14SAlberto Garciaquorum="$quorum,file.children.1.file.filename=$TEST_IMG.1" 66ef9bba14SAlberto Garciaquorum="$quorum,file.children.2.file.filename=$TEST_IMG.2" 67ef9bba14SAlberto Garciaquorum="$quorum,file.children.0.driver=$IMGFMT" 68ef9bba14SAlberto Garciaquorum="$quorum,file.children.1.driver=$IMGFMT" 69ef9bba14SAlberto Garciaquorum="$quorum,file.children.2.driver=$IMGFMT" 70ef9bba14SAlberto Garcia 71ef9bba14SAlberto Garciaecho 72ef9bba14SAlberto Garciaecho '### Output of qemu-img map (empty quorum)' 73ef9bba14SAlberto Garciaecho 74ef9bba14SAlberto Garcia$QEMU_IMG map --image-opts $quorum | _filter_qemu_img_map 75ef9bba14SAlberto Garcia 76ef9bba14SAlberto Garcia# Now we write data to the quorum. All three images will read as 77ef9bba14SAlberto Garcia# zeroes in all cases, but with different ways to represent them 78ef9bba14SAlberto Garcia# (unallocated clusters, zero clusters, data clusters with zeroes) 79ef9bba14SAlberto Garcia# that will have an effect on how the data will be mirrored and the 80ef9bba14SAlberto Garcia# output of qemu-img map on the resulting image. 81ef9bba14SAlberto Garciaecho 82ef9bba14SAlberto Garciaecho '### Write data to the quorum' 83ef9bba14SAlberto Garciaecho 84ef9bba14SAlberto Garcia# Test 1: data regions surrounded by unallocated clusters. 85ef9bba14SAlberto Garcia# Three data regions, the largest one (0x30000) will be picked, end result: 86ef9bba14SAlberto Garcia# offset 0x10000, length 0x30000 -> data 87ef9bba14SAlberto Garcia$QEMU_IO -c "write -P 0 $((0x10000)) $((0x10000))" "$TEST_IMG.0" | _filter_qemu_io 88ef9bba14SAlberto Garcia$QEMU_IO -c "write -P 0 $((0x10000)) $((0x30000))" "$TEST_IMG.1" | _filter_qemu_io 89ef9bba14SAlberto Garcia$QEMU_IO -c "write -P 0 $((0x10000)) $((0x20000))" "$TEST_IMG.2" | _filter_qemu_io 90ef9bba14SAlberto Garcia 91ef9bba14SAlberto Garcia# Test 2: zero regions surrounded by data clusters. 92ef9bba14SAlberto Garcia# First we allocate the data clusters. 93ef9bba14SAlberto Garcia$QEMU_IO -c "open -o $quorum" -c "write -P 0 $((0x100000)) $((0x40000))" | _filter_qemu_io 94ef9bba14SAlberto Garcia 95ef9bba14SAlberto Garcia# Three zero regions, the smallest one (0x10000) will be picked, end result: 96ef9bba14SAlberto Garcia# offset 0x100000, length 0x10000 -> data 97ef9bba14SAlberto Garcia# offset 0x110000, length 0x10000 -> zeroes 98ef9bba14SAlberto Garcia# offset 0x120000, length 0x20000 -> data 99ef9bba14SAlberto Garcia$QEMU_IO -c "write -z $((0x110000)) $((0x10000))" "$TEST_IMG.0" | _filter_qemu_io 100ef9bba14SAlberto Garcia$QEMU_IO -c "write -z $((0x110000)) $((0x30000))" "$TEST_IMG.1" | _filter_qemu_io 101ef9bba14SAlberto Garcia$QEMU_IO -c "write -z $((0x110000)) $((0x20000))" "$TEST_IMG.2" | _filter_qemu_io 102ef9bba14SAlberto Garcia 103ef9bba14SAlberto Garcia# Test 3: zero clusters surrounded by unallocated clusters. 104ef9bba14SAlberto Garcia# Everything reads as zeroes, no effect on the end result. 105ef9bba14SAlberto Garcia$QEMU_IO -c "write -z $((0x150000)) $((0x10000))" "$TEST_IMG.0" | _filter_qemu_io 106ef9bba14SAlberto Garcia$QEMU_IO -c "write -z $((0x150000)) $((0x30000))" "$TEST_IMG.1" | _filter_qemu_io 107ef9bba14SAlberto Garcia$QEMU_IO -c "write -z $((0x150000)) $((0x20000))" "$TEST_IMG.2" | _filter_qemu_io 108ef9bba14SAlberto Garcia 109ef9bba14SAlberto Garcia# Test 4: mix of data and zero clusters. 110ef9bba14SAlberto Garcia# The zero region will be ignored in favor of the largest data region 111ef9bba14SAlberto Garcia# (0x20000), end result: 112ef9bba14SAlberto Garcia# offset 0x200000, length 0x20000 -> data 113ef9bba14SAlberto Garcia$QEMU_IO -c "write -P 0 $((0x200000)) $((0x10000))" "$TEST_IMG.0" | _filter_qemu_io 114ef9bba14SAlberto Garcia$QEMU_IO -c "write -z $((0x200000)) $((0x30000))" "$TEST_IMG.1" | _filter_qemu_io 115ef9bba14SAlberto Garcia$QEMU_IO -c "write -P 0 $((0x200000)) $((0x20000))" "$TEST_IMG.2" | _filter_qemu_io 116ef9bba14SAlberto Garcia 117*5cddb2e9SAlberto Garcia# Test 5: write data to a region and then zeroize it, doing it 118*5cddb2e9SAlberto Garcia# directly on the quorum device instead of the individual images. 119*5cddb2e9SAlberto Garcia# This has no effect on the end result but proves that the quorum driver 120*5cddb2e9SAlberto Garcia# supports 'write -z'. 121*5cddb2e9SAlberto Garcia$QEMU_IO -c "open -o $quorum" -c "write -P 1 $((0x250000)) $((0x10000))" | _filter_qemu_io 122*5cddb2e9SAlberto Garcia# Verify the data that we just wrote 123*5cddb2e9SAlberto Garcia$QEMU_IO -c "open -o $quorum" -c "read -P 1 $((0x250000)) $((0x10000))" | _filter_qemu_io 124*5cddb2e9SAlberto Garcia$QEMU_IO -c "open -o $quorum" -c "write -z $((0x250000)) $((0x10000))" | _filter_qemu_io 125*5cddb2e9SAlberto Garcia# Now it should read back as zeroes 126*5cddb2e9SAlberto Garcia$QEMU_IO -c "open -o $quorum" -c "read -P 0 $((0x250000)) $((0x10000))" | _filter_qemu_io 127*5cddb2e9SAlberto Garcia 128ef9bba14SAlberto Garciaecho 129ef9bba14SAlberto Garciaecho '### Launch the drive-mirror job' 130ef9bba14SAlberto Garciaecho 131ef9bba14SAlberto Garciaqemu_comm_method="qmp" _launch_qemu -drive if=virtio,"$quorum" 132ef9bba14SAlberto Garciah=$QEMU_HANDLE 133ef9bba14SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'qmp_capabilities' }" 'return' 134ef9bba14SAlberto Garcia 135ef9bba14SAlberto Garcia_send_qemu_cmd $h \ 136ef9bba14SAlberto Garcia "{'execute': 'drive-mirror', 137ef9bba14SAlberto Garcia 'arguments': {'device': 'virtio0', 138ef9bba14SAlberto Garcia 'format': '$IMGFMT', 139ef9bba14SAlberto Garcia 'target': '$TEST_IMG.3', 140ef9bba14SAlberto Garcia 'sync': 'full', 141ef9bba14SAlberto Garcia 'mode': 'existing' }}" \ 142ef9bba14SAlberto Garcia "BLOCK_JOB_READY.*virtio0" 143ef9bba14SAlberto Garcia 144ef9bba14SAlberto Garcia_send_qemu_cmd $h \ 145ef9bba14SAlberto Garcia "{ 'execute': 'block-job-complete', 146ef9bba14SAlberto Garcia 'arguments': { 'device': 'virtio0' } }" \ 147ef9bba14SAlberto Garcia 'BLOCK_JOB_COMPLETED' 148ef9bba14SAlberto Garcia 149ef9bba14SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'quit' }" '' 150ef9bba14SAlberto Garcia 151ef9bba14SAlberto Garciaecho 152ef9bba14SAlberto Garciaecho '### Output of qemu-img map (destination image)' 153ef9bba14SAlberto Garciaecho 154ef9bba14SAlberto Garcia$QEMU_IMG map "$TEST_IMG.3" | _filter_qemu_img_map 155ef9bba14SAlberto Garcia 156ef9bba14SAlberto Garcia# success, all done 157ef9bba14SAlberto Garciaecho "*** done" 158ef9bba14SAlberto Garciarm -f $seq.full 159ef9bba14SAlberto Garciastatus=0 160