xref: /openbmc/qemu/tests/qemu-iotests/220 (revision 0e324626)
111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash
2*9dd003a9SVladimir Sementsov-Ogievskiy# group: rw auto
33b94c343SEric Blake#
43b94c343SEric Blake# max limits on compression in huge qcow2 files
53b94c343SEric Blake#
63b94c343SEric Blake# Copyright (C) 2018 Red Hat, Inc.
73b94c343SEric Blake#
83b94c343SEric Blake# This program is free software; you can redistribute it and/or modify
93b94c343SEric Blake# it under the terms of the GNU General Public License as published by
103b94c343SEric Blake# the Free Software Foundation; either version 2 of the License, or
113b94c343SEric Blake# (at your option) any later version.
123b94c343SEric Blake#
133b94c343SEric Blake# This program is distributed in the hope that it will be useful,
143b94c343SEric Blake# but WITHOUT ANY WARRANTY; without even the implied warranty of
153b94c343SEric Blake# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
163b94c343SEric Blake# GNU General Public License for more details.
173b94c343SEric Blake#
183b94c343SEric Blake# You should have received a copy of the GNU General Public License
193b94c343SEric Blake# along with this program.  If not, see <http://www.gnu.org/licenses/>.
203b94c343SEric Blake#
213b94c343SEric Blake
223b94c343SEric Blakeseq=$(basename $0)
233b94c343SEric Blakeecho "QA output created by $seq"
243b94c343SEric Blake
253b94c343SEric Blakestatus=1    # failure is the default!
263b94c343SEric Blake
273b94c343SEric Blake_cleanup()
283b94c343SEric Blake{
293b94c343SEric Blake    _cleanup_test_img
303b94c343SEric Blake}
313b94c343SEric Blaketrap "_cleanup; exit \$status" 0 1 2 3 15
323b94c343SEric Blake
333b94c343SEric Blake# get standard environment, filters and checks
343b94c343SEric Blake. ./common.rc
353b94c343SEric Blake. ./common.filter
363b94c343SEric Blake. ./common.pattern
373b94c343SEric Blake
383b94c343SEric Blake_supported_fmt qcow2
3957284d2aSMax Reitz_supported_proto file fuse
403b94c343SEric Blake_supported_os Linux
413be2024aSMax Reitz# To use a different refcount width but 16 bits we need compat=1.1,
423be2024aSMax Reitz# and external data files do not support compressed clusters.
433be2024aSMax Reitz_unsupported_imgopts 'compat=0.10' data_file
443b94c343SEric Blake
453b94c343SEric Blakeecho "== Creating huge file =="
463b94c343SEric Blake
473b94c343SEric Blake# Sanity check: We require a file system that permits the creation
483b94c343SEric Blake# of a HUGE (but very sparse) file.  tmpfs works, ext4 does not.
4930729ae9SThomas Huth_require_large_file 513T
5030729ae9SThomas Huth
51407fb56aSMax Reitz_make_test_img -o 'cluster_size=2M,refcount_bits=1' 513T
523b94c343SEric Blake
533b94c343SEric Blakeecho "== Populating refcounts =="
543b94c343SEric Blake# We want an image with 256M refcounts * 2M clusters = 512T referenced.
553b94c343SEric Blake# Each 2M cluster holds 16M refcounts; the refcount table initially uses
563b94c343SEric Blake# 1 refblock, so we need to add 15 more.  The refcount table lives at 2M,
573b94c343SEric Blake# first refblock at 4M, L2 at 6M, so our remaining additions start at 8M.
583b94c343SEric Blake# Then, for each refblock, mark it as fully populated.
593b94c343SEric Blaketo_hex() {
603b94c343SEric Blake    printf %016x\\n $1 | sed 's/\(..\)/\\x\1/g'
613b94c343SEric Blake}
623b94c343SEric Blaketruncate --size=38m "$TEST_IMG"
633b94c343SEric Blakeentry=$((0x200000))
643b94c343SEric Blake$QEMU_IO_PROG -f raw -c "w -P 0xff 4m 2m" "$TEST_IMG" | _filter_qemu_io
653b94c343SEric Blakefor i in {1..15}; do
663b94c343SEric Blake    offs=$((0x600000 + i*0x200000))
673b94c343SEric Blake    poke_file "$TEST_IMG" $((i*8 + entry)) $(to_hex $offs)
683b94c343SEric Blake    $QEMU_IO_PROG -f raw -c "w -P 0xff $offs 2m" "$TEST_IMG" | _filter_qemu_io
693b94c343SEric Blakedone
703b94c343SEric Blake
713b94c343SEric Blakeecho "== Checking file before =="
723b94c343SEric Blake# FIXME: 'qemu-img check' doesn't diagnose refcounts beyond the end of
733b94c343SEric Blake# the file as leaked clusters
743b94c343SEric Blake_check_test_img 2>&1 | sed '/^Leaked cluster/d'
753b94c343SEric Blakestat -c 'image size %s' "$TEST_IMG"
763b94c343SEric Blake
773b94c343SEric Blakeecho "== Trying to write compressed cluster =="
783b94c343SEric Blake# Given our file size, the next available cluster at 512T lies beyond the
793b94c343SEric Blake# maximum offset that a compressed 2M cluster can reside in
803b94c343SEric Blake$QEMU_IO_PROG -c 'w -c 0 2m' "$TEST_IMG" | _filter_qemu_io
813b94c343SEric Blake# The attempt failed, but ended up allocating a new refblock
823b94c343SEric Blakestat -c 'image size %s' "$TEST_IMG"
833b94c343SEric Blake
843b94c343SEric Blakeecho "== Writing normal cluster =="
853b94c343SEric Blake# The failed write should not corrupt the image, so a normal write succeeds
863b94c343SEric Blake$QEMU_IO_PROG -c 'w 0 2m' "$TEST_IMG" | _filter_qemu_io
873b94c343SEric Blake
883b94c343SEric Blakeecho "== Checking file after =="
893b94c343SEric Blake# qemu-img now sees the millions of leaked clusters, thanks to the allocations
903b94c343SEric Blake# at 512T.  Undo many of our faked references to speed up the check.
913b94c343SEric Blake$QEMU_IO_PROG -f raw -c "w -z 5m 1m" -c "w -z 8m 30m" "$TEST_IMG" |
923b94c343SEric Blake    _filter_qemu_io
933b94c343SEric Blake_check_test_img 2>&1 | sed '/^Leaked cluster/d'
943b94c343SEric Blake
953b94c343SEric Blake# success, all done
963b94c343SEric Blakeecho "*** done"
973b94c343SEric Blakerm -f $seq.full
983b94c343SEric Blakestatus=0
99