146174339SEric Blake#!/bin/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 Blakehere="$PWD" 2846174339SEric Blakestatus=1 # failure is the default! 2946174339SEric Blake 3046174339SEric Blake# get standard environment, filters and checks 3146174339SEric Blake. ./common.rc 3246174339SEric Blake. ./common.filter 3346174339SEric Blake 3446174339SEric BlakeTEST_WRAP="$TEST_DIR/t.wrap.qcow2" 3546174339SEric BlakeBLKDBG_CONF="$TEST_DIR/blkdebug.conf" 3646174339SEric Blake 3746174339SEric Blake# Sanity check: our use of blkdebug fails if $TEST_DIR contains spaces 3846174339SEric Blake# or other problems 3946174339SEric Blakecase "$TEST_DIR" in 4046174339SEric Blake *[^-_a-zA-Z0-9/]*) 4146174339SEric Blake _notrun "Suspicious TEST_DIR='$TEST_DIR', cowardly refusing to run" ;; 4246174339SEric Blakeesac 4346174339SEric Blake 4446174339SEric Blake_cleanup() 4546174339SEric Blake{ 4646174339SEric Blake _cleanup_test_img 475fdc0b73SMax Reitz rm -f "$TEST_WRAP" 4846174339SEric Blake rm -f "$BLKDBG_CONF" 4946174339SEric Blake} 5046174339SEric Blaketrap "_cleanup; exit \$status" 0 1 2 3 15 5146174339SEric Blake 5246174339SEric Blake# Test is supported for any backing file; but we force qcow2 for our wrapper. 5346174339SEric Blake_supported_fmt generic 5446174339SEric Blake_supported_proto generic 5546174339SEric Blake_supported_os Linux 5646174339SEric Blake# LUKS support may be possible, but it complicates things. 5746174339SEric Blake_unsupported_fmt luks 5846174339SEric Blake 5946174339SEric Blakeecho 6046174339SEric Blakeecho '=== Copy-on-read ===' 6146174339SEric Blakeecho 6246174339SEric Blake 6346174339SEric Blake# Prep the images 64bff55548SJohn Snow# VPC rounds image sizes to a specific geometry, force a specific size. 65bff55548SJohn Snowif [ "$IMGFMT" = "vpc" ]; then 66bff55548SJohn Snow IMGOPTS=$(_optstr_add "$IMGOPTS" "force_size") 67bff55548SJohn Snowfi 6846174339SEric Blake_make_test_img 4G 6946174339SEric Blake$QEMU_IO -c "write -P 55 3G 1k" "$TEST_IMG" | _filter_qemu_io 7046174339SEric BlakeIMGPROTO=file IMGFMT=qcow2 IMGOPTS= TEST_IMG_FILE="$TEST_WRAP" \ 7146174339SEric Blake _make_test_img -F "$IMGFMT" -b "$TEST_IMG" | _filter_img_create 7246174339SEric Blake$QEMU_IO -f qcow2 -c "write -z -u 1M 64k" "$TEST_WRAP" | _filter_qemu_io 7346174339SEric Blake 7446174339SEric Blake# Ensure that a read of two clusters, but where one is already allocated, 7546174339SEric Blake# does not re-write the allocated cluster 7646174339SEric Blakecat > "$BLKDBG_CONF" <<EOF 7746174339SEric Blake[inject-error] 7846174339SEric Blakeevent = "cor_write" 7946174339SEric Blakesector = "2048" 8046174339SEric BlakeEOF 8146174339SEric Blake$QEMU_IO -c "open -C \ 8246174339SEric Blake -o driver=blkdebug,config=$BLKDBG_CONF,image.driver=qcow2 $TEST_WRAP" \ 8346174339SEric Blake -c "read -P 0 1M 128k" | _filter_qemu_io 8446174339SEric Blake 8546174339SEric Blake# Read the areas we want copied. A zero-length read should still be a 8646174339SEric Blake# no-op. The next read is under 2G, but aligned so that rounding to 8746174339SEric Blake# clusters copies more than 2G of zeroes. The final read will pick up 8846174339SEric Blake# the non-zero data in the same cluster. Since a 2G read may exhaust 8946174339SEric Blake# memory on some machines (particularly 32-bit), we skip the test if 9046174339SEric Blake# that fails due to memory pressure. 9146174339SEric Blake$QEMU_IO -f qcow2 -C -c "read 0 0" "$TEST_WRAP" | _filter_qemu_io 9246174339SEric Blakeoutput=$($QEMU_IO -f qcow2 -C -c "read -P 0 1k $((2*1024*1024*1024 - 512))" \ 9346174339SEric Blake "$TEST_WRAP" 2>&1 | _filter_qemu_io) 9446174339SEric Blakecase $output in 9546174339SEric Blake *allocate*) 9646174339SEric Blake _notrun "Insufficent memory to run test" ;; 9746174339SEric Blake *) printf '%s\n' "$output" ;; 9846174339SEric Blakeesac 9946174339SEric Blake$QEMU_IO -f qcow2 -C -c "read -P 0 $((3*1024*1024*1024 + 1024)) 1k" \ 10046174339SEric Blake "$TEST_WRAP" | _filter_qemu_io 10146174339SEric Blake 10246174339SEric Blake# Copy-on-read is incompatible with read-only 10346174339SEric Blake$QEMU_IO -f qcow2 -C -r "$TEST_WRAP" 2>&1 | _filter_testdir 10446174339SEric Blake 10546174339SEric Blake# Break the backing chain, and show that images are identical, and that 10646174339SEric Blake# we properly copied over explicit zeros. 10746174339SEric Blake$QEMU_IMG rebase -u -b "" -f qcow2 "$TEST_WRAP" 10846174339SEric Blake$QEMU_IO -f qcow2 -c map "$TEST_WRAP" 10946174339SEric Blake_check_test_img 11046174339SEric Blake$QEMU_IMG compare -f $IMGFMT -F qcow2 "$TEST_IMG" "$TEST_WRAP" 11146174339SEric Blake 112*b0ddcbbbSKevin Wolfecho 113*b0ddcbbbSKevin Wolfecho '=== Partial final cluster ===' 114*b0ddcbbbSKevin Wolfecho 115*b0ddcbbbSKevin Wolf 116*b0ddcbbbSKevin Wolf_make_test_img 1024 117*b0ddcbbbSKevin Wolf$QEMU_IO -f $IMGFMT -C -c 'read 0 1024' "$TEST_IMG" | _filter_qemu_io 118*b0ddcbbbSKevin Wolf$QEMU_IO -f $IMGFMT -c map "$TEST_IMG" 119*b0ddcbbbSKevin Wolf_check_test_img 120*b0ddcbbbSKevin Wolf 12146174339SEric Blake# success, all done 12246174339SEric Blakeecho '*** done' 12346174339SEric Blakestatus=0 124