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