Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qemu/ar7.git] / tests / qemu-iotests / 215
blob958c14f5a075260f33da6f813abfd31d57e1e639
1 #!/usr/bin/env bash
3 # Test case for copy-on-read into qcow2, using the COR filter driver
5 # Copyright (C) 2018 Red Hat, Inc.
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.
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.
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/>.
21 seq="$(basename $0)"
22 echo "QA output created by $seq"
24 status=1 # failure is the default!
26 # get standard environment, filters and checks
27 . ./common.rc
28 . ./common.filter
30 TEST_WRAP="$TEST_DIR/t.wrap.qcow2"
31 BLKDBG_CONF="$TEST_DIR/blkdebug.conf"
33 # Sanity check: our use of blkdebug fails if $TEST_DIR contains spaces
34 # or other problems
35 case "$TEST_DIR" in
36 *[^-_a-zA-Z0-9/]*)
37 _notrun "Suspicious TEST_DIR='$TEST_DIR', cowardly refusing to run" ;;
38 esac
40 _cleanup()
42 _cleanup_test_img
43 rm -f "$TEST_WRAP"
44 rm -f "$BLKDBG_CONF"
46 trap "_cleanup; exit \$status" 0 1 2 3 15
48 # Test is supported for any backing file; but we force qcow2 for our wrapper.
49 _supported_fmt generic
50 _supported_proto generic
51 # LUKS support may be possible, but it complicates things.
52 _unsupported_fmt luks
54 echo
55 echo '=== Copy-on-read ==='
56 echo
58 # Prep the images
59 # VPC rounds image sizes to a specific geometry, force a specific size.
60 if [ "$IMGFMT" = "vpc" ]; then
61 IMGOPTS=$(_optstr_add "$IMGOPTS" "force_size")
63 _make_test_img 4G
64 $QEMU_IO -c "write -P 55 3G 1k" "$TEST_IMG" | _filter_qemu_io
65 IMGPROTO=file IMGFMT=qcow2 IMGOPTS= TEST_IMG_FILE="$TEST_WRAP" \
66 _make_test_img -F "$IMGFMT" -b "$TEST_IMG" | _filter_img_create
67 $QEMU_IO -f qcow2 -c "write -z -u 1M 64k" "$TEST_WRAP" | _filter_qemu_io
69 # Ensure that a read of two clusters, but where one is already allocated,
70 # does not re-write the allocated cluster
71 cat > "$BLKDBG_CONF" <<EOF
72 [inject-error]
73 event = "cor_write"
74 sector = "2048"
75 EOF
76 $QEMU_IO -c "open \
77 -o driver=copy-on-read,file.driver=blkdebug,file.config=$BLKDBG_CONF,file.image.driver=qcow2 $TEST_WRAP" \
78 -c "read -P 0 1M 128k" | _filter_qemu_io
80 # Read the areas we want copied. A zero-length read should still be a
81 # no-op. The next read is under 2G, but aligned so that rounding to
82 # clusters copies more than 2G of zeroes. The final read will pick up
83 # the non-zero data in the same cluster. Since a 2G read may exhaust
84 # memory on some machines (particularly 32-bit), we skip the test if
85 # that fails due to memory pressure.
86 $QEMU_IO \
87 -c "open -o driver=copy-on-read,file.driver=qcow2 $TEST_WRAP" \
88 -c "read 0 0" \
89 | _filter_qemu_io
90 output=$($QEMU_IO \
91 -c "open -o driver=copy-on-read,file.driver=qcow2 $TEST_WRAP" \
92 -c "read -P 0 1k $((2*1024*1024*1024 - 512))" \
93 2>&1 | _filter_qemu_io)
94 case $output in
95 *allocate*)
96 _notrun "Insufficent memory to run test" ;;
97 *) printf '%s\n' "$output" ;;
98 esac
99 $QEMU_IO \
100 -c "open -o driver=copy-on-read,file.driver=qcow2 $TEST_WRAP" \
101 -c "read -P 0 $((3*1024*1024*1024 + 1024)) 1k" \
102 | _filter_qemu_io
104 # Copy-on-read is incompatible with read-only
105 $QEMU_IO \
106 -c "open -r -o driver=copy-on-read,file.driver=qcow2 $TEST_WRAP" \
107 2>&1 | _filter_testdir
109 # Break the backing chain, and show that images are identical, and that
110 # we properly copied over explicit zeros.
111 $QEMU_IMG rebase -u -b "" -f qcow2 "$TEST_WRAP"
112 $QEMU_IO -f qcow2 -c map "$TEST_WRAP"
113 _check_test_img
114 $QEMU_IMG compare -f $IMGFMT -F qcow2 "$TEST_IMG" "$TEST_WRAP"
116 # success, all done
117 echo '*** done'
118 status=0