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