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