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