111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2a62cbac4SMax Reitz# 3a62cbac4SMax Reitz# Test case for copy-on-read into qcow2, using the COR filter driver 4a62cbac4SMax Reitz# 5a62cbac4SMax Reitz# Copyright (C) 2018 Red Hat, Inc. 6a62cbac4SMax Reitz# 7a62cbac4SMax Reitz# This program is free software; you can redistribute it and/or modify 8a62cbac4SMax Reitz# it under the terms of the GNU General Public License as published by 9a62cbac4SMax Reitz# the Free Software Foundation; either version 2 of the License, or 10a62cbac4SMax Reitz# (at your option) any later version. 11a62cbac4SMax Reitz# 12a62cbac4SMax Reitz# This program is distributed in the hope that it will be useful, 13a62cbac4SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 14a62cbac4SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15a62cbac4SMax Reitz# GNU General Public License for more details. 16a62cbac4SMax Reitz# 17a62cbac4SMax Reitz# You should have received a copy of the GNU General Public License 18a62cbac4SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 19a62cbac4SMax Reitz# 20a62cbac4SMax Reitz 21a62cbac4SMax Reitzseq="$(basename $0)" 22a62cbac4SMax Reitzecho "QA output created by $seq" 23a62cbac4SMax Reitz 24a62cbac4SMax Reitzstatus=1 # failure is the default! 25a62cbac4SMax Reitz 26a62cbac4SMax Reitz# get standard environment, filters and checks 27a62cbac4SMax Reitz. ./common.rc 28a62cbac4SMax Reitz. ./common.filter 29a62cbac4SMax Reitz 30a62cbac4SMax ReitzTEST_WRAP="$TEST_DIR/t.wrap.qcow2" 31a62cbac4SMax ReitzBLKDBG_CONF="$TEST_DIR/blkdebug.conf" 32a62cbac4SMax Reitz 33a62cbac4SMax Reitz# Sanity check: our use of blkdebug fails if $TEST_DIR contains spaces 34a62cbac4SMax Reitz# or other problems 35a62cbac4SMax Reitzcase "$TEST_DIR" in 36a62cbac4SMax Reitz *[^-_a-zA-Z0-9/]*) 37a62cbac4SMax Reitz _notrun "Suspicious TEST_DIR='$TEST_DIR', cowardly refusing to run" ;; 38a62cbac4SMax Reitzesac 39a62cbac4SMax Reitz 40a62cbac4SMax Reitz_cleanup() 41a62cbac4SMax Reitz{ 42a62cbac4SMax Reitz _cleanup_test_img 43a62cbac4SMax Reitz rm -f "$TEST_WRAP" 44a62cbac4SMax Reitz rm -f "$BLKDBG_CONF" 45a62cbac4SMax Reitz} 46a62cbac4SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 47a62cbac4SMax Reitz 48a62cbac4SMax Reitz# Test is supported for any backing file; but we force qcow2 for our wrapper. 49a62cbac4SMax Reitz_supported_fmt generic 50a62cbac4SMax Reitz_supported_proto generic 51a62cbac4SMax Reitz# LUKS support may be possible, but it complicates things. 52a62cbac4SMax Reitz_unsupported_fmt luks 53325dd915SMax Reitz_unsupported_imgopts "subformat=streamOptimized" 54a62cbac4SMax Reitz 55a62cbac4SMax Reitzecho 56a62cbac4SMax Reitzecho '=== Copy-on-read ===' 57a62cbac4SMax Reitzecho 58a62cbac4SMax Reitz 59a62cbac4SMax Reitz# Prep the images 60a62cbac4SMax Reitz# VPC rounds image sizes to a specific geometry, force a specific size. 61a62cbac4SMax Reitzif [ "$IMGFMT" = "vpc" ]; then 62a62cbac4SMax Reitz IMGOPTS=$(_optstr_add "$IMGOPTS" "force_size") 63a62cbac4SMax Reitzfi 64a62cbac4SMax Reitz_make_test_img 4G 65a62cbac4SMax Reitz$QEMU_IO -c "write -P 55 3G 1k" "$TEST_IMG" | _filter_qemu_io 66*10b61256SMax ReitzIMGPROTO=file IMGFMT=qcow2 TEST_IMG_FILE="$TEST_WRAP" \ 67*10b61256SMax Reitz _make_test_img --no-opts -F "$IMGFMT" -b "$TEST_IMG" | _filter_img_create 68a62cbac4SMax Reitz$QEMU_IO -f qcow2 -c "write -z -u 1M 64k" "$TEST_WRAP" | _filter_qemu_io 69a62cbac4SMax Reitz 70a62cbac4SMax Reitz# Ensure that a read of two clusters, but where one is already allocated, 71a62cbac4SMax Reitz# does not re-write the allocated cluster 72a62cbac4SMax Reitzcat > "$BLKDBG_CONF" <<EOF 73a62cbac4SMax Reitz[inject-error] 74a62cbac4SMax Reitzevent = "cor_write" 75a62cbac4SMax Reitzsector = "2048" 76a62cbac4SMax ReitzEOF 77a62cbac4SMax Reitz$QEMU_IO -c "open \ 78a62cbac4SMax Reitz -o driver=copy-on-read,file.driver=blkdebug,file.config=$BLKDBG_CONF,file.image.driver=qcow2 $TEST_WRAP" \ 79a62cbac4SMax Reitz -c "read -P 0 1M 128k" | _filter_qemu_io 80a62cbac4SMax Reitz 81a62cbac4SMax Reitz# Read the areas we want copied. A zero-length read should still be a 82a62cbac4SMax Reitz# no-op. The next read is under 2G, but aligned so that rounding to 83a62cbac4SMax Reitz# clusters copies more than 2G of zeroes. The final read will pick up 84a62cbac4SMax Reitz# the non-zero data in the same cluster. Since a 2G read may exhaust 85a62cbac4SMax Reitz# memory on some machines (particularly 32-bit), we skip the test if 86a62cbac4SMax Reitz# that fails due to memory pressure. 87a62cbac4SMax Reitz$QEMU_IO \ 88a62cbac4SMax Reitz -c "open -o driver=copy-on-read,file.driver=qcow2 $TEST_WRAP" \ 89a62cbac4SMax Reitz -c "read 0 0" \ 90a62cbac4SMax Reitz | _filter_qemu_io 91a62cbac4SMax Reitzoutput=$($QEMU_IO \ 92a62cbac4SMax Reitz -c "open -o driver=copy-on-read,file.driver=qcow2 $TEST_WRAP" \ 93a62cbac4SMax Reitz -c "read -P 0 1k $((2*1024*1024*1024 - 512))" \ 94a62cbac4SMax Reitz 2>&1 | _filter_qemu_io) 95a62cbac4SMax Reitzcase $output in 96a62cbac4SMax Reitz *allocate*) 97a62cbac4SMax Reitz _notrun "Insufficent memory to run test" ;; 98a62cbac4SMax Reitz *) printf '%s\n' "$output" ;; 99a62cbac4SMax Reitzesac 100a62cbac4SMax Reitz$QEMU_IO \ 101a62cbac4SMax Reitz -c "open -o driver=copy-on-read,file.driver=qcow2 $TEST_WRAP" \ 102a62cbac4SMax Reitz -c "read -P 0 $((3*1024*1024*1024 + 1024)) 1k" \ 103a62cbac4SMax Reitz | _filter_qemu_io 104a62cbac4SMax Reitz 105a62cbac4SMax Reitz# Copy-on-read is incompatible with read-only 106a62cbac4SMax Reitz$QEMU_IO \ 107a62cbac4SMax Reitz -c "open -r -o driver=copy-on-read,file.driver=qcow2 $TEST_WRAP" \ 108a62cbac4SMax Reitz 2>&1 | _filter_testdir 109a62cbac4SMax Reitz 110a62cbac4SMax Reitz# Break the backing chain, and show that images are identical, and that 111a62cbac4SMax Reitz# we properly copied over explicit zeros. 112a62cbac4SMax Reitz$QEMU_IMG rebase -u -b "" -f qcow2 "$TEST_WRAP" 113a62cbac4SMax Reitz$QEMU_IO -f qcow2 -c map "$TEST_WRAP" 114a62cbac4SMax Reitz_check_test_img 115a62cbac4SMax Reitz$QEMU_IMG compare -f $IMGFMT -F qcow2 "$TEST_IMG" "$TEST_WRAP" 116a62cbac4SMax Reitz 117a62cbac4SMax Reitz# success, all done 118a62cbac4SMax Reitzecho '*** done' 119a62cbac4SMax Reitzstatus=0 120