111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 246174339SEric Blake# 346174339SEric Blake# Test case for copy-on-read into qcow2 446174339SEric Blake# 546174339SEric Blake# Copyright (C) 2017 Red Hat, Inc. 646174339SEric Blake# 746174339SEric Blake# This program is free software; you can redistribute it and/or modify 846174339SEric Blake# it under the terms of the GNU General Public License as published by 946174339SEric Blake# the Free Software Foundation; either version 2 of the License, or 1046174339SEric Blake# (at your option) any later version. 1146174339SEric Blake# 1246174339SEric Blake# This program is distributed in the hope that it will be useful, 1346174339SEric Blake# but WITHOUT ANY WARRANTY; without even the implied warranty of 1446174339SEric Blake# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1546174339SEric Blake# GNU General Public License for more details. 1646174339SEric Blake# 1746174339SEric Blake# You should have received a copy of the GNU General Public License 1846174339SEric Blake# along with this program. If not, see <http://www.gnu.org/licenses/>. 1946174339SEric Blake# 2046174339SEric Blake 2146174339SEric Blake# creator 2246174339SEric Blakeowner=eblake@redhat.com 2346174339SEric Blake 2446174339SEric Blakeseq="$(basename $0)" 2546174339SEric Blakeecho "QA output created by $seq" 2646174339SEric Blake 2746174339SEric Blakestatus=1 # failure is the default! 2846174339SEric Blake 2946174339SEric Blake# get standard environment, filters and checks 3046174339SEric Blake. ./common.rc 3146174339SEric Blake. ./common.filter 3246174339SEric Blake 3346174339SEric BlakeTEST_WRAP="$TEST_DIR/t.wrap.qcow2" 3446174339SEric BlakeBLKDBG_CONF="$TEST_DIR/blkdebug.conf" 3546174339SEric Blake 3646174339SEric Blake# Sanity check: our use of blkdebug fails if $TEST_DIR contains spaces 3746174339SEric Blake# or other problems 3846174339SEric Blakecase "$TEST_DIR" in 3946174339SEric Blake *[^-_a-zA-Z0-9/]*) 4046174339SEric Blake _notrun "Suspicious TEST_DIR='$TEST_DIR', cowardly refusing to run" ;; 4146174339SEric Blakeesac 4246174339SEric Blake 4346174339SEric Blake_cleanup() 4446174339SEric Blake{ 4546174339SEric Blake _cleanup_test_img 465fdc0b73SMax Reitz rm -f "$TEST_WRAP" 4746174339SEric Blake rm -f "$BLKDBG_CONF" 4846174339SEric Blake} 4946174339SEric Blaketrap "_cleanup; exit \$status" 0 1 2 3 15 5046174339SEric Blake 5146174339SEric Blake# Test is supported for any backing file; but we force qcow2 for our wrapper. 5246174339SEric Blake_supported_fmt generic 5346174339SEric Blake_supported_proto generic 5446174339SEric Blake# LUKS support may be possible, but it complicates things. 5546174339SEric Blake_unsupported_fmt luks 56*325dd915SMax Reitz_unsupported_imgopts "subformat=streamOptimized" 5746174339SEric Blake 5846174339SEric Blakeecho 5946174339SEric Blakeecho '=== Copy-on-read ===' 6046174339SEric Blakeecho 6146174339SEric Blake 6246174339SEric Blake# Prep the images 63bff55548SJohn Snow# VPC rounds image sizes to a specific geometry, force a specific size. 64bff55548SJohn Snowif [ "$IMGFMT" = "vpc" ]; then 65bff55548SJohn Snow IMGOPTS=$(_optstr_add "$IMGOPTS" "force_size") 66bff55548SJohn Snowfi 6746174339SEric Blake_make_test_img 4G 6846174339SEric Blake$QEMU_IO -c "write -P 55 3G 1k" "$TEST_IMG" | _filter_qemu_io 6946174339SEric BlakeIMGPROTO=file IMGFMT=qcow2 IMGOPTS= TEST_IMG_FILE="$TEST_WRAP" \ 7046174339SEric Blake _make_test_img -F "$IMGFMT" -b "$TEST_IMG" | _filter_img_create 7146174339SEric Blake$QEMU_IO -f qcow2 -c "write -z -u 1M 64k" "$TEST_WRAP" | _filter_qemu_io 7246174339SEric Blake 7346174339SEric Blake# Ensure that a read of two clusters, but where one is already allocated, 7446174339SEric Blake# does not re-write the allocated cluster 7546174339SEric Blakecat > "$BLKDBG_CONF" <<EOF 7646174339SEric Blake[inject-error] 7746174339SEric Blakeevent = "cor_write" 7846174339SEric Blakesector = "2048" 7946174339SEric BlakeEOF 8046174339SEric Blake$QEMU_IO -c "open -C \ 8146174339SEric Blake -o driver=blkdebug,config=$BLKDBG_CONF,image.driver=qcow2 $TEST_WRAP" \ 8246174339SEric Blake -c "read -P 0 1M 128k" | _filter_qemu_io 8346174339SEric Blake 8446174339SEric Blake# Read the areas we want copied. A zero-length read should still be a 8546174339SEric Blake# no-op. The next read is under 2G, but aligned so that rounding to 8646174339SEric Blake# clusters copies more than 2G of zeroes. The final read will pick up 8746174339SEric Blake# the non-zero data in the same cluster. Since a 2G read may exhaust 8846174339SEric Blake# memory on some machines (particularly 32-bit), we skip the test if 8946174339SEric Blake# that fails due to memory pressure. 9046174339SEric Blake$QEMU_IO -f qcow2 -C -c "read 0 0" "$TEST_WRAP" | _filter_qemu_io 9146174339SEric Blakeoutput=$($QEMU_IO -f qcow2 -C -c "read -P 0 1k $((2*1024*1024*1024 - 512))" \ 9246174339SEric Blake "$TEST_WRAP" 2>&1 | _filter_qemu_io) 9346174339SEric Blakecase $output in 9446174339SEric Blake *allocate*) 9546174339SEric Blake _notrun "Insufficent memory to run test" ;; 9646174339SEric Blake *) printf '%s\n' "$output" ;; 9746174339SEric Blakeesac 9846174339SEric Blake$QEMU_IO -f qcow2 -C -c "read -P 0 $((3*1024*1024*1024 + 1024)) 1k" \ 9946174339SEric Blake "$TEST_WRAP" | _filter_qemu_io 10046174339SEric Blake 10146174339SEric Blake# Copy-on-read is incompatible with read-only 10246174339SEric Blake$QEMU_IO -f qcow2 -C -r "$TEST_WRAP" 2>&1 | _filter_testdir 10346174339SEric Blake 10446174339SEric Blake# Break the backing chain, and show that images are identical, and that 10546174339SEric Blake# we properly copied over explicit zeros. 10646174339SEric Blake$QEMU_IMG rebase -u -b "" -f qcow2 "$TEST_WRAP" 10746174339SEric Blake$QEMU_IO -f qcow2 -c map "$TEST_WRAP" 10846174339SEric Blake_check_test_img 10946174339SEric Blake$QEMU_IMG compare -f $IMGFMT -F qcow2 "$TEST_IMG" "$TEST_WRAP" 11046174339SEric Blake 111b0ddcbbbSKevin Wolfecho 112b0ddcbbbSKevin Wolfecho '=== Partial final cluster ===' 113b0ddcbbbSKevin Wolfecho 114b0ddcbbbSKevin Wolf 115b0ddcbbbSKevin Wolf_make_test_img 1024 116b0ddcbbbSKevin Wolf$QEMU_IO -f $IMGFMT -C -c 'read 0 1024' "$TEST_IMG" | _filter_qemu_io 117b0ddcbbbSKevin Wolf$QEMU_IO -f $IMGFMT -c map "$TEST_IMG" 118b0ddcbbbSKevin Wolf_check_test_img 119b0ddcbbbSKevin Wolf 12046174339SEric Blake# success, all done 12146174339SEric Blakeecho '*** done' 12246174339SEric Blakestatus=0 123