xref: /openbmc/qemu/tests/qemu-iotests/244 (revision e9e51b71)
1#!/usr/bin/env bash
2# group: rw auto quick
3#
4# Test qcow2 with external data files
5#
6# Copyright (C) 2019 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22# creator
23owner=kwolf@redhat.com
24
25seq=$(basename $0)
26echo "QA output created by $seq"
27
28status=1	# failure is the default!
29
30_cleanup()
31{
32    _cleanup_test_img
33    _rm_test_img "$TEST_IMG.data"
34    _rm_test_img "$TEST_IMG.src"
35}
36trap "_cleanup; exit \$status" 0 1 2 3 15
37
38# get standard environment, filters and checks
39. ./common.rc
40. ./common.filter
41
42_supported_fmt qcow2
43_supported_proto file
44_supported_os Linux
45# External data files do not work with compat=0.10, and because we use
46# our own external data file, we cannot let the user specify one
47_unsupported_imgopts 'compat=0.10' data_file
48
49echo
50echo "=== Create and open image with external data file ==="
51echo
52
53echo "With data file name in the image:"
54_make_test_img -o "data_file=$TEST_IMG.data" 64M
55_check_test_img
56
57$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
58$QEMU_IO -c "open -odata-file.filename=$TEST_IMG.data $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
59$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
60
61echo
62echo "Data file required, but without data file name in the image:"
63$QEMU_IMG amend -odata_file= $TEST_IMG
64
65$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
66$QEMU_IO -c "open -odata-file.filename=$TEST_IMG.data $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
67$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
68
69echo
70echo "Setting data-file for an image with internal data:"
71_make_test_img 64M
72
73$QEMU_IO -c "open -odata-file.filename=$TEST_IMG.data $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
74$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
75
76echo
77echo "=== Conflicting features ==="
78echo
79
80echo "Convert to compressed target with data file:"
81TEST_IMG="$TEST_IMG.src" _make_test_img 64M
82
83$QEMU_IO -c 'write -P 0x11 0 1M' \
84         -f $IMGFMT "$TEST_IMG.src" |
85         _filter_qemu_io
86
87$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -c -odata_file="$TEST_IMG.data" \
88    "$TEST_IMG.src" "$TEST_IMG"
89
90echo
91echo "Convert uncompressed, then write compressed data manually:"
92$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -odata_file="$TEST_IMG.data" \
93    "$TEST_IMG.src" "$TEST_IMG"
94$QEMU_IMG compare "$TEST_IMG.src" "$TEST_IMG"
95
96$QEMU_IO -c 'write -c -P 0x22 0 1M' \
97         -f $IMGFMT "$TEST_IMG" |
98         _filter_qemu_io
99_check_test_img
100
101echo
102echo "Take an internal snapshot:"
103
104$QEMU_IMG snapshot -c test "$TEST_IMG"
105_check_test_img
106
107echo
108echo "=== Standalone image with external data file (efficient) ==="
109echo
110
111_make_test_img -o "data_file=$TEST_IMG.data" 64M
112
113echo -n "qcow2 file size before I/O: "
114du -b $TEST_IMG | cut -f1
115
116# Create image with the following layout
117# 0-1 MB: Unallocated
118# 1-2 MB: Written (pattern 0x11)
119# 2-3 MB: Discarded
120# 3-4 MB: Zero write over discarded space
121# 4-5 MB: Zero write over written space
122# 5-6 MB: Zero write over unallocated space
123
124echo
125$QEMU_IO -c 'write -P 0x11 1M 4M' \
126         -c 'discard 2M 2M' \
127         -c 'write -z 3M 3M' \
128         -f $IMGFMT "$TEST_IMG" |
129         _filter_qemu_io
130_check_test_img
131
132echo
133$QEMU_IMG map --output=json "$TEST_IMG"
134
135echo
136$QEMU_IO -c 'read -P 0 0 1M' \
137         -c 'read -P 0x11 1M 1M' \
138         -c 'read -P 0 2M 4M' \
139         -f $IMGFMT "$TEST_IMG" |
140         _filter_qemu_io
141
142# Zero clusters are only marked as such in the qcow2 metadata, but contain
143# stale data in the external data file
144echo
145$QEMU_IO -c 'read -P 0 0 1M' \
146         -c 'read -P 0x11 1M 1M' \
147         -c 'read -P 0x11 4M 1M' \
148         -c 'read -P 0 5M 1M' \
149         -f raw "$TEST_IMG.data" |
150         _filter_qemu_io
151
152
153echo -n "qcow2 file size after I/O: "
154du -b $TEST_IMG | cut -f1
155
156echo
157echo "=== Standalone image with external data file (valid raw) ==="
158echo
159
160_make_test_img -o "data_file=$TEST_IMG.data,data_file_raw=on" 64M
161
162echo -n "qcow2 file size before I/O: "
163du -b $TEST_IMG | cut -f1
164
165echo
166$QEMU_IO -c 'write -P 0x11 1M 4M' \
167         -c 'discard 2M 2M' \
168         -c 'write -z 3M 3M' \
169         -f $IMGFMT "$TEST_IMG" |
170         _filter_qemu_io
171_check_test_img
172
173echo
174$QEMU_IMG map --output=json "$TEST_IMG"
175
176echo
177$QEMU_IO -c 'read -P 0 0 1M' \
178         -c 'read -P 0x11 1M 1M' \
179         -c 'read -P 0 2M 4M' \
180         -f $IMGFMT "$TEST_IMG" |
181         _filter_qemu_io
182
183# Discarded clusters are only marked as such in the qcow2 metadata, but
184# they can contain stale data in the external data file.  Instead, zero
185# clusters must be zeroed in the external data file too.
186echo
187$QEMU_IO -c 'read -P 0 0 1M' \
188         -c 'read -P 0x11 1M 1M' \
189         -c 'read -P 0 3M 3M' \
190         -f raw "$TEST_IMG".data |
191         _filter_qemu_io
192
193echo -n "qcow2 file size after I/O: "
194du -b $TEST_IMG | cut -f1
195
196echo
197echo "=== bdrv_co_block_status test for file and offset=0 ==="
198echo
199
200_make_test_img -o "data_file=$TEST_IMG.data" 64M
201
202$QEMU_IO -c 'write -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
203$QEMU_IO -c 'read -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
204$QEMU_IMG map --output=human "$TEST_IMG" | _filter_testdir
205$QEMU_IMG map --output=json "$TEST_IMG"
206
207echo
208echo "=== Copy offloading ==="
209echo
210
211# Make use of copy offloading if the test host can provide it
212_make_test_img -o "data_file=$TEST_IMG.data" 64M
213$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG"
214$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG"
215
216# blkdebug doesn't support copy offloading, so this tests the error path
217$QEMU_IMG amend -f $IMGFMT -o "data_file=blkdebug::$TEST_IMG.data" "$TEST_IMG"
218$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG"
219$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG"
220
221echo
222echo "=== Flushing should flush the data file ==="
223echo
224
225# We are going to flush a qcow2 file with a blkdebug node inserted
226# between the qcow2 node and its data file node.  The blkdebug node
227# will return an error for all flushes and so we if the data file is
228# flushed, we will see qemu-io return an error.
229
230# We need to write something or the flush will not do anything; we
231# also need -t writeback so the write is not done as a FUA write
232# (which would then fail thanks to the implicit flush)
233$QEMU_IO -c 'write 0 512' -c flush \
234    -t writeback \
235    "json:{
236         'driver': 'qcow2',
237         'file': {
238             'driver': 'file',
239             'filename': '$TEST_IMG'
240         },
241         'data-file': {
242             'driver': 'blkdebug',
243             'inject-error': [{
244                 'event': 'none',
245                 'iotype': 'flush'
246             }],
247             'image': {
248                 'driver': 'file',
249                 'filename': '$TEST_IMG.data'
250             }
251         }
252     }" \
253    | _filter_qemu_io
254
255result=${PIPESTATUS[0]}
256echo
257
258case $result in
259    0)
260        echo "ERROR: qemu-io succeeded, so the data file was not flushed"
261        ;;
262    1)
263        echo "Success: qemu-io failed, so the data file was flushed"
264        ;;
265    *)
266        echo "ERROR: qemu-io returned unknown exit code $result"
267        ;;
268esac
269
270# success, all done
271echo "*** done"
272rm -f $seq.full
273status=0
274