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