xref: /openbmc/qemu/tests/qemu-iotests/197 (revision 461743390d3a1ceafa4503811adbc87c7d372741)
1*46174339SEric Blake#!/bin/bash
2*46174339SEric Blake#
3*46174339SEric Blake# Test case for copy-on-read into qcow2
4*46174339SEric Blake#
5*46174339SEric Blake# Copyright (C) 2017 Red Hat, Inc.
6*46174339SEric Blake#
7*46174339SEric Blake# This program is free software; you can redistribute it and/or modify
8*46174339SEric Blake# it under the terms of the GNU General Public License as published by
9*46174339SEric Blake# the Free Software Foundation; either version 2 of the License, or
10*46174339SEric Blake# (at your option) any later version.
11*46174339SEric Blake#
12*46174339SEric Blake# This program is distributed in the hope that it will be useful,
13*46174339SEric Blake# but WITHOUT ANY WARRANTY; without even the implied warranty of
14*46174339SEric Blake# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*46174339SEric Blake# GNU General Public License for more details.
16*46174339SEric Blake#
17*46174339SEric Blake# You should have received a copy of the GNU General Public License
18*46174339SEric Blake# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19*46174339SEric Blake#
20*46174339SEric Blake
21*46174339SEric Blake# creator
22*46174339SEric Blakeowner=eblake@redhat.com
23*46174339SEric Blake
24*46174339SEric Blakeseq="$(basename $0)"
25*46174339SEric Blakeecho "QA output created by $seq"
26*46174339SEric Blake
27*46174339SEric Blakehere="$PWD"
28*46174339SEric Blakestatus=1 # failure is the default!
29*46174339SEric Blake
30*46174339SEric Blake# get standard environment, filters and checks
31*46174339SEric Blake. ./common.rc
32*46174339SEric Blake. ./common.filter
33*46174339SEric Blake
34*46174339SEric BlakeTEST_WRAP="$TEST_DIR/t.wrap.qcow2"
35*46174339SEric BlakeBLKDBG_CONF="$TEST_DIR/blkdebug.conf"
36*46174339SEric Blake
37*46174339SEric Blake# Sanity check: our use of blkdebug fails if $TEST_DIR contains spaces
38*46174339SEric Blake# or other problems
39*46174339SEric Blakecase "$TEST_DIR" in
40*46174339SEric Blake    *[^-_a-zA-Z0-9/]*)
41*46174339SEric Blake        _notrun "Suspicious TEST_DIR='$TEST_DIR', cowardly refusing to run" ;;
42*46174339SEric Blakeesac
43*46174339SEric Blake
44*46174339SEric Blake_cleanup()
45*46174339SEric Blake{
46*46174339SEric Blake    _cleanup_test_img
47*46174339SEric Blake    rm -f "$BLKDBG_CONF"
48*46174339SEric Blake}
49*46174339SEric Blaketrap "_cleanup; exit \$status" 0 1 2 3 15
50*46174339SEric Blake
51*46174339SEric Blake# Test is supported for any backing file; but we force qcow2 for our wrapper.
52*46174339SEric Blake_supported_fmt generic
53*46174339SEric Blake_supported_proto generic
54*46174339SEric Blake_supported_os Linux
55*46174339SEric Blake# LUKS support may be possible, but it complicates things.
56*46174339SEric Blake_unsupported_fmt luks
57*46174339SEric Blake
58*46174339SEric Blakeecho
59*46174339SEric Blakeecho '=== Copy-on-read ==='
60*46174339SEric Blakeecho
61*46174339SEric Blake
62*46174339SEric Blake# Prep the images
63*46174339SEric Blake_make_test_img 4G
64*46174339SEric Blake$QEMU_IO -c "write -P 55 3G 1k" "$TEST_IMG" | _filter_qemu_io
65*46174339SEric BlakeIMGPROTO=file IMGFMT=qcow2 IMGOPTS= TEST_IMG_FILE="$TEST_WRAP" \
66*46174339SEric Blake    _make_test_img -F "$IMGFMT" -b "$TEST_IMG" | _filter_img_create
67*46174339SEric Blake$QEMU_IO -f qcow2 -c "write -z -u 1M 64k" "$TEST_WRAP" | _filter_qemu_io
68*46174339SEric Blake
69*46174339SEric Blake# Ensure that a read of two clusters, but where one is already allocated,
70*46174339SEric Blake# does not re-write the allocated cluster
71*46174339SEric Blakecat > "$BLKDBG_CONF" <<EOF
72*46174339SEric Blake[inject-error]
73*46174339SEric Blakeevent = "cor_write"
74*46174339SEric Blakesector = "2048"
75*46174339SEric BlakeEOF
76*46174339SEric Blake$QEMU_IO -c "open -C \
77*46174339SEric Blake -o driver=blkdebug,config=$BLKDBG_CONF,image.driver=qcow2 $TEST_WRAP" \
78*46174339SEric Blake -c "read -P 0 1M 128k" | _filter_qemu_io
79*46174339SEric Blake
80*46174339SEric Blake# Read the areas we want copied. A zero-length read should still be a
81*46174339SEric Blake# no-op.  The next read is under 2G, but aligned so that rounding to
82*46174339SEric Blake# clusters copies more than 2G of zeroes. The final read will pick up
83*46174339SEric Blake# the non-zero data in the same cluster.  Since a 2G read may exhaust
84*46174339SEric Blake# memory on some machines (particularly 32-bit), we skip the test if
85*46174339SEric Blake# that fails due to memory pressure.
86*46174339SEric Blake$QEMU_IO -f qcow2 -C -c "read 0 0" "$TEST_WRAP" | _filter_qemu_io
87*46174339SEric Blakeoutput=$($QEMU_IO -f qcow2 -C -c "read -P 0 1k $((2*1024*1024*1024 - 512))" \
88*46174339SEric Blake        "$TEST_WRAP" 2>&1 | _filter_qemu_io)
89*46174339SEric Blakecase $output in
90*46174339SEric Blake    *allocate*)
91*46174339SEric Blake        _notrun "Insufficent memory to run test" ;;
92*46174339SEric Blake    *) printf '%s\n' "$output" ;;
93*46174339SEric Blakeesac
94*46174339SEric Blake$QEMU_IO -f qcow2 -C -c "read -P 0 $((3*1024*1024*1024 + 1024)) 1k" \
95*46174339SEric Blake    "$TEST_WRAP" | _filter_qemu_io
96*46174339SEric Blake
97*46174339SEric Blake# Copy-on-read is incompatible with read-only
98*46174339SEric Blake$QEMU_IO -f qcow2 -C -r "$TEST_WRAP" 2>&1 | _filter_testdir
99*46174339SEric Blake
100*46174339SEric Blake# Break the backing chain, and show that images are identical, and that
101*46174339SEric Blake# we properly copied over explicit zeros.
102*46174339SEric Blake$QEMU_IMG rebase -u -b "" -f qcow2 "$TEST_WRAP"
103*46174339SEric Blake$QEMU_IO -f qcow2 -c map "$TEST_WRAP"
104*46174339SEric Blake_check_test_img
105*46174339SEric Blake$QEMU_IMG compare -f $IMGFMT -F qcow2 "$TEST_IMG" "$TEST_WRAP"
106*46174339SEric Blake
107*46174339SEric Blake# success, all done
108*46174339SEric Blakeecho '*** done'
109*46174339SEric Blakestatus=0
110