1*3b94c343SEric Blake#!/bin/bash 2*3b94c343SEric Blake# 3*3b94c343SEric Blake# max limits on compression in huge qcow2 files 4*3b94c343SEric Blake# 5*3b94c343SEric Blake# Copyright (C) 2018 Red Hat, Inc. 6*3b94c343SEric Blake# 7*3b94c343SEric Blake# This program is free software; you can redistribute it and/or modify 8*3b94c343SEric Blake# it under the terms of the GNU General Public License as published by 9*3b94c343SEric Blake# the Free Software Foundation; either version 2 of the License, or 10*3b94c343SEric Blake# (at your option) any later version. 11*3b94c343SEric Blake# 12*3b94c343SEric Blake# This program is distributed in the hope that it will be useful, 13*3b94c343SEric Blake# but WITHOUT ANY WARRANTY; without even the implied warranty of 14*3b94c343SEric Blake# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*3b94c343SEric Blake# GNU General Public License for more details. 16*3b94c343SEric Blake# 17*3b94c343SEric Blake# You should have received a copy of the GNU General Public License 18*3b94c343SEric Blake# along with this program. If not, see <http://www.gnu.org/licenses/>. 19*3b94c343SEric Blake# 20*3b94c343SEric Blake 21*3b94c343SEric Blakeseq=$(basename $0) 22*3b94c343SEric Blakeecho "QA output created by $seq" 23*3b94c343SEric Blake 24*3b94c343SEric Blakestatus=1 # failure is the default! 25*3b94c343SEric Blake 26*3b94c343SEric Blake_cleanup() 27*3b94c343SEric Blake{ 28*3b94c343SEric Blake _cleanup_test_img 29*3b94c343SEric Blake} 30*3b94c343SEric Blaketrap "_cleanup; exit \$status" 0 1 2 3 15 31*3b94c343SEric Blake 32*3b94c343SEric Blake# get standard environment, filters and checks 33*3b94c343SEric Blake. ./common.rc 34*3b94c343SEric Blake. ./common.filter 35*3b94c343SEric Blake. ./common.pattern 36*3b94c343SEric Blake 37*3b94c343SEric Blake_supported_fmt qcow2 38*3b94c343SEric Blake_supported_proto file 39*3b94c343SEric Blake_supported_os Linux 40*3b94c343SEric Blake 41*3b94c343SEric Blakeecho "== Creating huge file ==" 42*3b94c343SEric Blake 43*3b94c343SEric Blake# Sanity check: We require a file system that permits the creation 44*3b94c343SEric Blake# of a HUGE (but very sparse) file. tmpfs works, ext4 does not. 45*3b94c343SEric Blakeif ! truncate --size=513T "$TEST_IMG"; then 46*3b94c343SEric Blake _notrun "file system on $TEST_DIR does not support large enough files" 47*3b94c343SEric Blakefi 48*3b94c343SEric Blakerm "$TEST_IMG" 49*3b94c343SEric BlakeIMGOPTS='cluster_size=2M,refcount_bits=1' _make_test_img 513T 50*3b94c343SEric Blake 51*3b94c343SEric Blakeecho "== Populating refcounts ==" 52*3b94c343SEric Blake# We want an image with 256M refcounts * 2M clusters = 512T referenced. 53*3b94c343SEric Blake# Each 2M cluster holds 16M refcounts; the refcount table initially uses 54*3b94c343SEric Blake# 1 refblock, so we need to add 15 more. The refcount table lives at 2M, 55*3b94c343SEric Blake# first refblock at 4M, L2 at 6M, so our remaining additions start at 8M. 56*3b94c343SEric Blake# Then, for each refblock, mark it as fully populated. 57*3b94c343SEric Blaketo_hex() { 58*3b94c343SEric Blake printf %016x\\n $1 | sed 's/\(..\)/\\x\1/g' 59*3b94c343SEric Blake} 60*3b94c343SEric Blaketruncate --size=38m "$TEST_IMG" 61*3b94c343SEric Blakeentry=$((0x200000)) 62*3b94c343SEric Blake$QEMU_IO_PROG -f raw -c "w -P 0xff 4m 2m" "$TEST_IMG" | _filter_qemu_io 63*3b94c343SEric Blakefor i in {1..15}; do 64*3b94c343SEric Blake offs=$((0x600000 + i*0x200000)) 65*3b94c343SEric Blake poke_file "$TEST_IMG" $((i*8 + entry)) $(to_hex $offs) 66*3b94c343SEric Blake $QEMU_IO_PROG -f raw -c "w -P 0xff $offs 2m" "$TEST_IMG" | _filter_qemu_io 67*3b94c343SEric Blakedone 68*3b94c343SEric Blake 69*3b94c343SEric Blakeecho "== Checking file before ==" 70*3b94c343SEric Blake# FIXME: 'qemu-img check' doesn't diagnose refcounts beyond the end of 71*3b94c343SEric Blake# the file as leaked clusters 72*3b94c343SEric Blake_check_test_img 2>&1 | sed '/^Leaked cluster/d' 73*3b94c343SEric Blakestat -c 'image size %s' "$TEST_IMG" 74*3b94c343SEric Blake 75*3b94c343SEric Blakeecho "== Trying to write compressed cluster ==" 76*3b94c343SEric Blake# Given our file size, the next available cluster at 512T lies beyond the 77*3b94c343SEric Blake# maximum offset that a compressed 2M cluster can reside in 78*3b94c343SEric Blake$QEMU_IO_PROG -c 'w -c 0 2m' "$TEST_IMG" | _filter_qemu_io 79*3b94c343SEric Blake# The attempt failed, but ended up allocating a new refblock 80*3b94c343SEric Blakestat -c 'image size %s' "$TEST_IMG" 81*3b94c343SEric Blake 82*3b94c343SEric Blakeecho "== Writing normal cluster ==" 83*3b94c343SEric Blake# The failed write should not corrupt the image, so a normal write succeeds 84*3b94c343SEric Blake$QEMU_IO_PROG -c 'w 0 2m' "$TEST_IMG" | _filter_qemu_io 85*3b94c343SEric Blake 86*3b94c343SEric Blakeecho "== Checking file after ==" 87*3b94c343SEric Blake# qemu-img now sees the millions of leaked clusters, thanks to the allocations 88*3b94c343SEric Blake# at 512T. Undo many of our faked references to speed up the check. 89*3b94c343SEric Blake$QEMU_IO_PROG -f raw -c "w -z 5m 1m" -c "w -z 8m 30m" "$TEST_IMG" | 90*3b94c343SEric Blake _filter_qemu_io 91*3b94c343SEric Blake_check_test_img 2>&1 | sed '/^Leaked cluster/d' 92*3b94c343SEric Blake 93*3b94c343SEric Blake# success, all done 94*3b94c343SEric Blakeecho "*** done" 95*3b94c343SEric Blakerm -f $seq.full 96*3b94c343SEric Blakestatus=0 97