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