xref: /openbmc/qemu/tests/qemu-iotests/272 (revision 57284d2ada481fe557c9a2f00228eca2a83e1d60)
1b7cd2c11SMax Reitz#!/usr/bin/env bash
2b7cd2c11SMax Reitz#
3b7cd2c11SMax Reitz# Test compressed write to a qcow2 image at an offset above 4 GB
4b7cd2c11SMax Reitz#
5b7cd2c11SMax Reitz# Copyright (C) 2019 Red Hat, Inc.
6b7cd2c11SMax Reitz#
7b7cd2c11SMax Reitz# This program is free software; you can redistribute it and/or modify
8b7cd2c11SMax Reitz# it under the terms of the GNU General Public License as published by
9b7cd2c11SMax Reitz# the Free Software Foundation; either version 2 of the License, or
10b7cd2c11SMax Reitz# (at your option) any later version.
11b7cd2c11SMax Reitz#
12b7cd2c11SMax Reitz# This program is distributed in the hope that it will be useful,
13b7cd2c11SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
14b7cd2c11SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15b7cd2c11SMax Reitz# GNU General Public License for more details.
16b7cd2c11SMax Reitz#
17b7cd2c11SMax Reitz# You should have received a copy of the GNU General Public License
18b7cd2c11SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19b7cd2c11SMax Reitz#
20b7cd2c11SMax Reitz
21b7cd2c11SMax Reitzseq=$(basename "$0")
22b7cd2c11SMax Reitzecho "QA output created by $seq"
23b7cd2c11SMax Reitz
24b7cd2c11SMax Reitzstatus=1	# failure is the default!
25b7cd2c11SMax Reitz
26b7cd2c11SMax Reitz_cleanup()
27b7cd2c11SMax Reitz{
28b7cd2c11SMax Reitz    _cleanup_test_img
29b7cd2c11SMax Reitz}
30b7cd2c11SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15
31b7cd2c11SMax Reitz
32b7cd2c11SMax Reitz# get standard environment, filters and checks
33b7cd2c11SMax Reitz. ./common.rc
34b7cd2c11SMax Reitz. ./common.filter
35b7cd2c11SMax Reitz
36b7cd2c11SMax Reitz# This is a qcow2 regression test
37b7cd2c11SMax Reitz_supported_fmt qcow2
38*57284d2aSMax Reitz_supported_proto file fuse
39b7cd2c11SMax Reitz
40b7cd2c11SMax Reitz# External data files do not support compression;
41b7cd2c11SMax Reitz# We need an exact cluster size (2M) and refcount width (2) so we can
42b7cd2c11SMax Reitz# get this test quickly over with; and this in turn require
43b7cd2c11SMax Reitz# compat=1.1
44b7cd2c11SMax Reitz_unsupported_imgopts data_file cluster_size refcount_bits 'compat=0.10'
45b7cd2c11SMax Reitz
46b7cd2c11SMax Reitz# The idea is: Create an empty file, mark the first 4 GB as used, then
47b7cd2c11SMax Reitz# do a compressed write that thus must be put beyond 4 GB.
48b7cd2c11SMax Reitz# (This used to fail because the compressed sector mask was just a
49b7cd2c11SMax Reitz# 32 bit mask, so qemu-img check will count a cluster before 4 GB as
50b7cd2c11SMax Reitz# referenced twice.)
51b7cd2c11SMax Reitz
52b7cd2c11SMax Reitz# We would like to use refcount_bits=1 here, but then qemu-img check
53b7cd2c11SMax Reitz# will throw an error when trying to count a cluster as referenced
54b7cd2c11SMax Reitz# twice.
55b7cd2c11SMax Reitz_make_test_img -o cluster_size=2M,refcount_bits=2 64M
56b7cd2c11SMax Reitz
57b7cd2c11SMax Reitzreft_offs=$(peek_file_be "$TEST_IMG" 48 8)
58b7cd2c11SMax Reitzrefb_offs=$(peek_file_be "$TEST_IMG" $reft_offs 8)
59b7cd2c11SMax Reitz
60b7cd2c11SMax Reitz# We want to cover 4 GB, those are 2048 clusters, equivalent to
61b7cd2c11SMax Reitz# 4096 bit = 512 B.
62b7cd2c11SMax Reitztruncate -s 4G "$TEST_IMG"
63b7cd2c11SMax Reitzfor ((in_refb_offs = 0; in_refb_offs < 512; in_refb_offs += 8)); do
64b7cd2c11SMax Reitz    poke_file "$TEST_IMG" $((refb_offs + in_refb_offs)) \
65b7cd2c11SMax Reitz        '\x55\x55\x55\x55\x55\x55\x55\x55'
66b7cd2c11SMax Reitzdone
67b7cd2c11SMax Reitz
68b7cd2c11SMax Reitz$QEMU_IO -c 'write -c -P 42 0 2M' "$TEST_IMG" | _filter_qemu_io
69b7cd2c11SMax Reitz
70b7cd2c11SMax Reitzecho
71b7cd2c11SMax Reitzecho '--- Check ---'
72b7cd2c11SMax Reitz
73b7cd2c11SMax Reitz# This should only print the leaked clusters in the first 4 GB
74b7cd2c11SMax Reitz_check_test_img | grep -v '^Leaked cluster '
75b7cd2c11SMax Reitz
76b7cd2c11SMax Reitz# success, all done
77b7cd2c11SMax Reitzecho "*** done"
78b7cd2c11SMax Reitzrm -f $seq.full
79b7cd2c11SMax Reitzstatus=0
80