xref: /openbmc/qemu/tests/qemu-iotests/220 (revision 0a553c58)
1#!/usr/bin/env bash
2#
3# max limits on compression in huge qcow2 files
4#
5# Copyright (C) 2018 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. ./common.pattern
36
37_supported_fmt qcow2
38_supported_proto file
39_supported_os Linux
40
41echo "== Creating huge file =="
42
43# Sanity check: We require a file system that permits the creation
44# of a HUGE (but very sparse) file.  tmpfs works, ext4 does not.
45if ! truncate --size=513T "$TEST_IMG"; then
46    _notrun "file system on $TEST_DIR does not support large enough files"
47fi
48rm "$TEST_IMG"
49IMGOPTS='cluster_size=2M,refcount_bits=1' _make_test_img 513T
50
51echo "== Populating refcounts =="
52# We want an image with 256M refcounts * 2M clusters = 512T referenced.
53# Each 2M cluster holds 16M refcounts; the refcount table initially uses
54# 1 refblock, so we need to add 15 more.  The refcount table lives at 2M,
55# first refblock at 4M, L2 at 6M, so our remaining additions start at 8M.
56# Then, for each refblock, mark it as fully populated.
57to_hex() {
58    printf %016x\\n $1 | sed 's/\(..\)/\\x\1/g'
59}
60truncate --size=38m "$TEST_IMG"
61entry=$((0x200000))
62$QEMU_IO_PROG -f raw -c "w -P 0xff 4m 2m" "$TEST_IMG" | _filter_qemu_io
63for i in {1..15}; do
64    offs=$((0x600000 + i*0x200000))
65    poke_file "$TEST_IMG" $((i*8 + entry)) $(to_hex $offs)
66    $QEMU_IO_PROG -f raw -c "w -P 0xff $offs 2m" "$TEST_IMG" | _filter_qemu_io
67done
68
69echo "== Checking file before =="
70# FIXME: 'qemu-img check' doesn't diagnose refcounts beyond the end of
71# the file as leaked clusters
72_check_test_img 2>&1 | sed '/^Leaked cluster/d'
73stat -c 'image size %s' "$TEST_IMG"
74
75echo "== Trying to write compressed cluster =="
76# Given our file size, the next available cluster at 512T lies beyond the
77# maximum offset that a compressed 2M cluster can reside in
78$QEMU_IO_PROG -c 'w -c 0 2m' "$TEST_IMG" | _filter_qemu_io
79# The attempt failed, but ended up allocating a new refblock
80stat -c 'image size %s' "$TEST_IMG"
81
82echo "== Writing normal cluster =="
83# The failed write should not corrupt the image, so a normal write succeeds
84$QEMU_IO_PROG -c 'w 0 2m' "$TEST_IMG" | _filter_qemu_io
85
86echo "== Checking file after =="
87# qemu-img now sees the millions of leaked clusters, thanks to the allocations
88# at 512T.  Undo many of our faked references to speed up the check.
89$QEMU_IO_PROG -f raw -c "w -z 5m 1m" -c "w -z 8m 30m" "$TEST_IMG" |
90    _filter_qemu_io
91_check_test_img 2>&1 | sed '/^Leaked cluster/d'
92
93# success, all done
94echo "*** done"
95rm -f $seq.full
96status=0
97