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 -f $TEST_IMG.data
33    rm -f $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
45echo
46echo "=== Create and open image with external data file ==="
47echo
48
49echo "With data file name in the image:"
50IMGOPTS="data_file=$TEST_IMG.data" _make_test_img 64M
51_check_test_img
52
53$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
54$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
55$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
56
57echo
58echo "Data file required, but without data file name in the image:"
59$QEMU_IMG amend -odata_file= $TEST_IMG
60
61$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
62$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
63$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
64
65echo
66echo "Setting data-file for an image with internal data:"
67_make_test_img 64M
68
69$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
70$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
71
72echo
73echo "=== Conflicting features ==="
74echo
75
76echo "Convert to compressed target with data file:"
77TEST_IMG="$TEST_IMG.src" _make_test_img 64M
78
79$QEMU_IO -c 'write -P 0x11 0 1M' \
80         -f $IMGFMT "$TEST_IMG.src" |
81         _filter_qemu_io
82
83$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -c -odata_file="$TEST_IMG.data" \
84    "$TEST_IMG.src" "$TEST_IMG"
85
86echo
87echo "Convert uncompressed, then write compressed data manually:"
88$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -odata_file="$TEST_IMG.data" \
89    "$TEST_IMG.src" "$TEST_IMG"
90$QEMU_IMG compare "$TEST_IMG.src" "$TEST_IMG"
91
92$QEMU_IO -c 'write -c -P 0x22 0 1M' \
93         -f $IMGFMT "$TEST_IMG" |
94         _filter_qemu_io
95_check_test_img
96
97echo
98echo "Take an internal snapshot:"
99
100$QEMU_IMG snapshot -c test "$TEST_IMG"
101_check_test_img
102
103echo
104echo "=== Standalone image with external data file (efficient) ==="
105echo
106
107IMGOPTS="data_file=$TEST_IMG.data" _make_test_img 64M
108
109echo -n "qcow2 file size before I/O: "
110du -b $TEST_IMG | cut -f1
111
112# Create image with the following layout
113# 0-1 MB: Unallocated
114# 1-2 MB: Written (pattern 0x11)
115# 2-3 MB: Discarded
116# 3-4 MB: Zero write over discarded space
117# 4-5 MB: Zero write over written space
118# 5-6 MB: Zero write over unallocated space
119
120echo
121$QEMU_IO -c 'write -P 0x11 1M 4M' \
122         -c 'discard 2M 2M' \
123         -c 'write -z 3M 3M' \
124         -f $IMGFMT "$TEST_IMG" |
125         _filter_qemu_io
126_check_test_img
127
128echo
129$QEMU_IMG map --output=json "$TEST_IMG"
130
131echo
132$QEMU_IO -c 'read -P 0 0 1M' \
133         -c 'read -P 0x11 1M 1M' \
134         -c 'read -P 0 2M 4M' \
135         -f $IMGFMT "$TEST_IMG" |
136         _filter_qemu_io
137
138# Zero clusters are only marked as such in the qcow2 metadata, but contain
139# stale data in the external data file
140echo
141$QEMU_IO -c 'read -P 0 0 1M' \
142         -c 'read -P 0x11 1M 1M' \
143         -c 'read -P 0 2M 2M' \
144         -c 'read -P 0x11 4M 1M' \
145         -c 'read -P 0 5M 1M' \
146         -f raw "$TEST_IMG.data" |
147         _filter_qemu_io
148
149
150echo -n "qcow2 file size after I/O: "
151du -b $TEST_IMG | cut -f1
152
153echo
154echo "=== Standalone image with external data file (valid raw) ==="
155echo
156
157IMGOPTS="data_file=$TEST_IMG.data,data_file_raw=on" _make_test_img 64M
158
159echo -n "qcow2 file size before I/O: "
160du -b $TEST_IMG | cut -f1
161
162echo
163$QEMU_IO -c 'write -P 0x11 1M 4M' \
164         -c 'discard 2M 2M' \
165         -c 'write -z 3M 3M' \
166         -f $IMGFMT "$TEST_IMG" |
167         _filter_qemu_io
168_check_test_img
169
170echo
171$QEMU_IMG map --output=json "$TEST_IMG"
172
173echo
174$QEMU_IO -c 'read -P 0 0 1M' \
175         -c 'read -P 0x11 1M 1M' \
176         -c 'read -P 0 2M 4M' \
177         -f $IMGFMT "$TEST_IMG" |
178         _filter_qemu_io
179
180echo
181$QEMU_IMG compare "$TEST_IMG" "$TEST_IMG.data"
182
183echo -n "qcow2 file size after I/O: "
184du -b $TEST_IMG | cut -f1
185
186echo
187echo "=== bdrv_co_block_status test for file and offset=0 ==="
188echo
189
190IMGOPTS="data_file=$TEST_IMG.data" _make_test_img 64M
191
192$QEMU_IO -c 'write -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
193$QEMU_IO -c 'read -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
194$QEMU_IMG map --output=human "$TEST_IMG" | _filter_testdir
195$QEMU_IMG map --output=json "$TEST_IMG"
196
197echo
198echo "=== Copy offloading ==="
199echo
200
201# Make use of copy offloading if the test host can provide it
202_make_test_img -o "data_file=$TEST_IMG.data" 64M
203$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG"
204$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG"
205
206# blkdebug doesn't support copy offloading, so this tests the error path
207$QEMU_IMG amend -f $IMGFMT -o "data_file=blkdebug::$TEST_IMG.data" "$TEST_IMG"
208$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG"
209$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG"
210
211# success, all done
212echo "*** done"
213rm -f $seq.full
214status=0
215