xref: /openbmc/qemu/tests/qemu-iotests/272 (revision 2a886794)
1#!/usr/bin/env bash
2#
3# Test compressed write to a qcow2 image at an offset above 4 GB
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
21seq=$(basename "$0")
22echo "QA output created by $seq"
23
24status=1	# failure is the default!
25
26_cleanup()
27{
28    _cleanup_test_img
29}
30trap "_cleanup; exit \$status" 0 1 2 3 15
31
32# get standard environment, filters and checks
33. ./common.rc
34. ./common.filter
35
36# This is a qcow2 regression test
37_supported_fmt qcow2
38_supported_proto file
39
40# External data files do not support compression;
41# We need an exact cluster size (2M) and refcount width (2) so we can
42# get this test quickly over with; and this in turn require
43# compat=1.1
44_unsupported_imgopts data_file cluster_size refcount_bits 'compat=0.10'
45
46# The idea is: Create an empty file, mark the first 4 GB as used, then
47# do a compressed write that thus must be put beyond 4 GB.
48# (This used to fail because the compressed sector mask was just a
49# 32 bit mask, so qemu-img check will count a cluster before 4 GB as
50# referenced twice.)
51
52# We would like to use refcount_bits=1 here, but then qemu-img check
53# will throw an error when trying to count a cluster as referenced
54# twice.
55_make_test_img -o cluster_size=2M,refcount_bits=2 64M
56
57reft_offs=$(peek_file_be "$TEST_IMG" 48 8)
58refb_offs=$(peek_file_be "$TEST_IMG" $reft_offs 8)
59
60# We want to cover 4 GB, those are 2048 clusters, equivalent to
61# 4096 bit = 512 B.
62truncate -s 4G "$TEST_IMG"
63for ((in_refb_offs = 0; in_refb_offs < 512; in_refb_offs += 8)); do
64    poke_file "$TEST_IMG" $((refb_offs + in_refb_offs)) \
65        '\x55\x55\x55\x55\x55\x55\x55\x55'
66done
67
68$QEMU_IO -c 'write -c -P 42 0 2M' "$TEST_IMG" | _filter_qemu_io
69
70echo
71echo '--- Check ---'
72
73# This should only print the leaked clusters in the first 4 GB
74_check_test_img | grep -v '^Leaked cluster '
75
76# success, all done
77echo "*** done"
78rm -f $seq.full
79status=0
80