xref: /openbmc/qemu/tests/qemu-iotests/214 (revision c63ca4ff)
1#!/usr/bin/env bash
2#
3# Test qcow2 image compression
4#
5# Copyright (C) 2018 Igalia, S.L.
6# Author: Alberto Garcia <berto@igalia.com>
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
22seq=$(basename "$0")
23echo "QA output created by $seq"
24
25status=1	# failure is the default!
26
27_cleanup()
28{
29    _cleanup_test_img
30}
31trap "_cleanup; exit \$status" 0 1 2 3 15
32
33# get standard environment, filters and checks
34. ./common.rc
35. ./common.filter
36
37_supported_fmt qcow2
38_supported_proto file fuse
39
40# Repairing the corrupted image requires qemu-img check to store a
41# refcount up to 3, which requires at least two refcount bits.
42# External data files do not support compressed clusters.
43_unsupported_imgopts 'refcount_bits=1[^0-9]' data_file
44
45
46echo
47echo "=== Corrupted size field in compressed cluster descriptor ==="
48echo
49# Create an empty image and fill half of it with compressed data.
50# The L2 entries of the two compressed clusters are located at
51# 0x800000 and 0x800008, their original values are 0x4008000000a00000
52# and 0x4008000000a00802 (5 sectors for compressed data each).
53_make_test_img 8M -o cluster_size=2M
54$QEMU_IO -c "write -c -P 0x11 0 2M" -c "write -c -P 0x11 2M 2M" "$TEST_IMG" \
55         2>&1 | _filter_qemu_io | _filter_testdir
56
57# Reduce size of compressed data to 4 sectors: this corrupts the image.
58poke_file "$TEST_IMG" $((0x800000)) "\x40\x06"
59$QEMU_IO -c "read  -P 0x11 0 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir
60
61# 'qemu-img check' however doesn't see anything wrong because it
62# doesn't try to decompress the data and the refcounts are consistent.
63# TODO: update qemu-img so this can be detected.
64_check_test_img
65
66# Increase size of compressed data to the maximum (8192 sectors).
67# This makes QEMU read more data (8192 sectors instead of 5, host
68# addresses [0xa00000, 0xdfffff]), but the decompression algorithm
69# stops once we have enough to restore the uncompressed cluster, so
70# the rest of the data is ignored.
71poke_file "$TEST_IMG" $((0x800000)) "\x7f\xfe"
72# Do it also for the second compressed cluster (L2 entry at 0x800008).
73# In this case the compressed data would span 3 host clusters
74# (host addresses: [0xa00802, 0xe00801])
75poke_file "$TEST_IMG" $((0x800008)) "\x7f\xfe"
76
77# Here the image is too small so we're asking QEMU to read beyond the
78# end of the image.
79$QEMU_IO -c "read  -P 0x11  0 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir
80# But if we grow the image we won't be reading beyond its end anymore.
81$QEMU_IO -c "write -P 0x22 4M 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir
82$QEMU_IO -c "read  -P 0x11  0 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir
83
84# The refcount data is however wrong because due to the increased size
85# of the compressed data it now reaches the following host clusters.
86# This can be repaired by qemu-img check by increasing the refcount of
87# those clusters.
88# TODO: update qemu-img to correct the compressed cluster size instead.
89_check_test_img -r all
90$QEMU_IO -c "read  -P 0x11  0 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir
91$QEMU_IO -c "read  -P 0x22 4M 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir
92
93echo
94echo "=== Write compressed data of multiple clusters ==="
95echo
96cluster_size=0x10000
97_make_test_img 2M -o cluster_size=$cluster_size
98
99echo "Write uncompressed data:"
100let data_size="8 * $cluster_size"
101$QEMU_IO -c "write -P 0xaa 0 $data_size" "$TEST_IMG" \
102         2>&1 | _filter_qemu_io | _filter_testdir
103sizeA=$($QEMU_IMG info --output=json "$TEST_IMG" |
104        sed -n '/"actual-size":/ s/[^0-9]//gp')
105
106_make_test_img 2M -o cluster_size=$cluster_size
107echo "Write compressed data:"
108let data_size="3 * $cluster_size + $cluster_size / 2"
109# Set compress on. That will align the written data
110# by the cluster size and will write them compressed.
111QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT \
112$QEMU_IO -c "write -P 0xbb 0 $data_size" --image-opts \
113         "driver=compress,file.driver=$IMGFMT,file.file.driver=file,file.file.filename=$TEST_IMG" \
114         2>&1 | _filter_qemu_io | _filter_testdir
115
116let offset="4 * $cluster_size + $cluster_size / 4"
117QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT \
118$QEMU_IO -c "write -P 0xcc $offset $data_size" "json:{\
119    'driver': 'compress',
120    'file': {'driver': '$IMGFMT',
121             'file': {'driver': 'file',
122                      'filename': '$TEST_IMG'}}}" | \
123                          _filter_qemu_io | _filter_testdir
124
125sizeB=$($QEMU_IMG info --output=json "$TEST_IMG" |
126        sed -n '/"actual-size":/ s/[^0-9]//gp')
127
128if [ $sizeA -lt $sizeB ]
129then
130    echo "Compression ERROR ($sizeA < $sizeB)"
131fi
132
133$QEMU_IMG check --output=json "$TEST_IMG" |
134          sed -n 's/,$//; /"compressed-clusters":/ s/^ *//p'
135
136# success, all done
137echo '*** done'
138rm -f $seq.full
139status=0
140