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