xref: /openbmc/qemu/tests/qemu-iotests/197 (revision c5ea91da443b458352c1b629b490ee6631775cb4)
111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw quick
346174339SEric Blake#
446174339SEric Blake# Test case for copy-on-read into qcow2
546174339SEric Blake#
646174339SEric Blake# Copyright (C) 2017 Red Hat, Inc.
746174339SEric Blake#
846174339SEric Blake# This program is free software; you can redistribute it and/or modify
946174339SEric Blake# it under the terms of the GNU General Public License as published by
1046174339SEric Blake# the Free Software Foundation; either version 2 of the License, or
1146174339SEric Blake# (at your option) any later version.
1246174339SEric Blake#
1346174339SEric Blake# This program is distributed in the hope that it will be useful,
1446174339SEric Blake# but WITHOUT ANY WARRANTY; without even the implied warranty of
1546174339SEric Blake# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1646174339SEric Blake# GNU General Public License for more details.
1746174339SEric Blake#
1846174339SEric Blake# You should have received a copy of the GNU General Public License
1946174339SEric Blake# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2046174339SEric Blake#
2146174339SEric Blake
2246174339SEric Blake# creator
2346174339SEric Blakeowner=eblake@redhat.com
2446174339SEric Blake
2546174339SEric Blakeseq="$(basename $0)"
2646174339SEric Blakeecho "QA output created by $seq"
2746174339SEric Blake
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
47f91ecbd7SMax Reitz    _rm_test_img "$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# LUKS support may be possible, but it complicates things.
5646174339SEric Blake_unsupported_fmt luks
57325dd915SMax Reitz_unsupported_imgopts "subformat=streamOptimized"
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
7010b61256SMax ReitzIMGPROTO=file IMGFMT=qcow2 TEST_IMG_FILE="$TEST_WRAP" \
7110b61256SMax Reitz    _make_test_img --no-opts -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*)
96*96420a30SMichael Tokarev        _notrun "Insufficient 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
112b0ddcbbbSKevin Wolfecho
113b0ddcbbbSKevin Wolfecho '=== Partial final cluster ==='
114b0ddcbbbSKevin Wolfecho
115b0ddcbbbSKevin Wolf
11618555362SMax Reitz# Force compat=1.1, because writing zeroes on a v2 image without a
11718555362SMax Reitz# backing file would just result in an unallocated cluster
118afac471bSMax Reitz# (Also, note that this is really a pure qcow2 test.)
119afac471bSMax ReitzIMGPROTO=file IMGFMT=qcow2 TEST_IMG_FILE="$TEST_WRAP" \
120afac471bSMax Reitz    _make_test_img --no-opts -o compat=1.1 1024
121afac471bSMax Reitz$QEMU_IO -f qcow2 -C -c 'read 0 1024' "$TEST_WRAP" | _filter_qemu_io
122afac471bSMax Reitz$QEMU_IO -f qcow2 -c map "$TEST_WRAP"
123b0ddcbbbSKevin Wolf_check_test_img
124b0ddcbbbSKevin Wolf
125e2f93826SAndrey Drobyshevecho
126e2f93826SAndrey Drobyshevecho '=== Copy-on-read with subclusters ==='
127e2f93826SAndrey Drobyshevecho
128e2f93826SAndrey Drobyshev
129e2f93826SAndrey Drobyshev# Create base and top images 64K (1 cluster) each.  Make subclusters enabled
130e2f93826SAndrey Drobyshev# for the top image
131e2f93826SAndrey Drobyshev_make_test_img 64K
132e2f93826SAndrey DrobyshevIMGPROTO=file IMGFMT=qcow2 TEST_IMG_FILE="$TEST_WRAP" \
133e2f93826SAndrey Drobyshev    _make_test_img --no-opts -o extended_l2=true -F "$IMGFMT" -b "$TEST_IMG" \
134e2f93826SAndrey Drobyshev    64K | _filter_img_create
135e2f93826SAndrey Drobyshev
136e2f93826SAndrey Drobyshev$QEMU_IO -c "write -P 0xaa 0 64k" "$TEST_IMG" | _filter_qemu_io
137e2f93826SAndrey Drobyshev
138e2f93826SAndrey Drobyshev# Allocate individual subclusters in the top image, and not the whole cluster
139b87151a8SAndrey Drobyshev$QEMU_IO -f qcow2 -c "write -P 0xbb 28K 2K" -c "write -P 0xcc 34K 2K" "$TEST_WRAP" \
140e2f93826SAndrey Drobyshev    | _filter_qemu_io
141e2f93826SAndrey Drobyshev
142e2f93826SAndrey Drobyshev# Only 2 subclusters should be allocated in the top image at this point
143b87151a8SAndrey Drobyshev$QEMU_IO -f qcow2 -c map "$TEST_WRAP"
144e2f93826SAndrey Drobyshev
145e2f93826SAndrey Drobyshev# Actual copy-on-read operation
146b87151a8SAndrey Drobyshev$QEMU_IO -f qcow2 -C -c "read -P 0xaa 30K 4K" "$TEST_WRAP" | _filter_qemu_io
147e2f93826SAndrey Drobyshev
148e2f93826SAndrey Drobyshev# And here we should have 4 subclusters allocated right in the middle of the
149e2f93826SAndrey Drobyshev# top image. Make sure the whole cluster remains unallocated
150b87151a8SAndrey Drobyshev$QEMU_IO -f qcow2 -c map "$TEST_WRAP"
151e2f93826SAndrey Drobyshev
152e2f93826SAndrey Drobyshev_check_test_img
153e2f93826SAndrey Drobyshev
15446174339SEric Blake# success, all done
15546174339SEric Blakeecho '*** done'
15646174339SEric Blakestatus=0
157