111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2a2c7e082SMax Reitz# 3a2c7e082SMax Reitz# Test preallocated resize of raw images 4a2c7e082SMax Reitz# 5a2c7e082SMax Reitz# Copyright (C) 2017 Red Hat, Inc. 6a2c7e082SMax Reitz# 7a2c7e082SMax Reitz# This program is free software; you can redistribute it and/or modify 8a2c7e082SMax Reitz# it under the terms of the GNU General Public License as published by 9a2c7e082SMax Reitz# the Free Software Foundation; either version 2 of the License, or 10a2c7e082SMax Reitz# (at your option) any later version. 11a2c7e082SMax Reitz# 12a2c7e082SMax Reitz# This program is distributed in the hope that it will be useful, 13a2c7e082SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 14a2c7e082SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15a2c7e082SMax Reitz# GNU General Public License for more details. 16a2c7e082SMax Reitz# 17a2c7e082SMax Reitz# You should have received a copy of the GNU General Public License 18a2c7e082SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 19a2c7e082SMax Reitz# 20a2c7e082SMax Reitz 21a2c7e082SMax Reitz# creator 22a2c7e082SMax Reitzowner=mreitz@redhat.com 23a2c7e082SMax Reitz 24a2c7e082SMax Reitzseq=$(basename $0) 25a2c7e082SMax Reitzecho "QA output created by $seq" 26a2c7e082SMax Reitz 27a2c7e082SMax Reitzstatus=1 # failure is the default! 28a2c7e082SMax Reitz 29a2c7e082SMax Reitz_cleanup() 30a2c7e082SMax Reitz{ 31a2c7e082SMax Reitz _cleanup_test_img 32a2c7e082SMax Reitz} 33a2c7e082SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 34a2c7e082SMax Reitz 35a2c7e082SMax Reitz# get standard environment and filters 36a2c7e082SMax Reitz. ./common.rc 37a2c7e082SMax Reitz. ./common.filter 38a2c7e082SMax Reitz 39a2c7e082SMax Reitz_supported_fmt raw 40*57284d2aSMax Reitz_supported_proto file fuse 41a2c7e082SMax Reitz_supported_os Linux 42a2c7e082SMax Reitz 43a2c7e082SMax Reitz# in kB 44a2c7e082SMax ReitzCREATION_SIZE=128 45a2c7e082SMax ReitzGROWTH_SIZE=256 46a2c7e082SMax Reitz 47a2c7e082SMax Reitzecho '=== Testing image growth ===' 48a2c7e082SMax Reitz 49a2c7e082SMax Reitzfor create_mode in off falloc full; do 50a2c7e082SMax Reitz for growth_mode in off falloc full; do 51a2c7e082SMax Reitz echo 52a2c7e082SMax Reitz echo "--- create_mode=$create_mode growth_mode=$growth_mode ---" 53a2c7e082SMax Reitz 54ffa244c8SKevin Wolf # Our calculation below assumes kilobytes as unit for the actual size. 55ffa244c8SKevin Wolf # Disable the extent size hint because it would give us a result in 56ffa244c8SKevin Wolf # megabytes. 57ffa244c8SKevin Wolf _make_test_img -o "preallocation=$create_mode,extent_size_hint=0" ${CREATION_SIZE}K 58a2c7e082SMax Reitz $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K 59a2c7e082SMax Reitz 60a2c7e082SMax Reitz expected_size=0 61a2c7e082SMax Reitz if [ $create_mode != off ]; then 62a2c7e082SMax Reitz expected_size=$CREATION_SIZE 63a2c7e082SMax Reitz fi 64a2c7e082SMax Reitz if [ $growth_mode != off ]; then 65a2c7e082SMax Reitz expected_size=$((expected_size + $GROWTH_SIZE)) 66a2c7e082SMax Reitz fi 67a2c7e082SMax Reitz 68a2c7e082SMax Reitz actual_size=$($QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep 'disk size') 69a2c7e082SMax Reitz actual_size=$(echo "$actual_size" | sed -e 's/^[^0-9]*\([0-9]\+\).*$/\1/') 70a2c7e082SMax Reitz 71a2c7e082SMax Reitz # The actual size may exceed the expected size, depending on the file 72a2c7e082SMax Reitz # system. Therefore we just test that the actual size is at least what 73a2c7e082SMax Reitz # we expect. 74a2c7e082SMax Reitz if [ $actual_size -lt $expected_size ]; then 75a2c7e082SMax Reitz echo "ERROR: Image should have at least ${expected_size}K, but has ${actual_size}K" 76a2c7e082SMax Reitz fi 77a2c7e082SMax Reitz done 78a2c7e082SMax Reitzdone 79a2c7e082SMax Reitz 80a2c7e082SMax Reitzecho 81a2c7e082SMax Reitzecho '=== Testing image shrinking ===' 82a2c7e082SMax Reitz 83a2c7e082SMax Reitz# None of this should work except for "off", because other modes cannot be used 84a2c7e082SMax Reitz# for shrinking 85a2c7e082SMax Reitzfor growth_mode in falloc full off; do 86a2c7e082SMax Reitz echo 87a2c7e082SMax Reitz echo "--- growth_mode=$growth_mode ---" 884ffca890SPavel Butsykin $QEMU_IMG resize -f "$IMGFMT" --shrink --preallocation=$growth_mode "$TEST_IMG" -${GROWTH_SIZE}K 89a2c7e082SMax Reitzdone 90a2c7e082SMax Reitz 91733d1dceSMax Reitzecho 92733d1dceSMax Reitzecho '=== Testing image growth on 2G empty image ===' 93733d1dceSMax Reitz 94733d1dceSMax Reitzfor growth_mode in falloc full; do 95733d1dceSMax Reitz echo 96733d1dceSMax Reitz echo "--- growth_mode=$growth_mode ---" 97733d1dceSMax Reitz 98733d1dceSMax Reitz # Maybe we want to do an lseek() to the end of the file before the 99733d1dceSMax Reitz # preallocation; if the file has a length of 2 GB, that would 100733d1dceSMax Reitz # return an integer that overflows to negative when put into a 101733d1dceSMax Reitz # plain int. We should use the correct type for the result, and 102733d1dceSMax Reitz # this tests we do. 103733d1dceSMax Reitz 104ffa244c8SKevin Wolf _make_test_img -o "extent_size_hint=0" 2G 105733d1dceSMax Reitz $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K 106733d1dceSMax Reitz 107733d1dceSMax Reitz actual_size=$($QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep 'disk size') 108733d1dceSMax Reitz actual_size=$(echo "$actual_size" | sed -e 's/^[^0-9]*\([0-9]\+\).*$/\1/') 109733d1dceSMax Reitz 110733d1dceSMax Reitz if [ $actual_size -lt $GROWTH_SIZE ]; then 111733d1dceSMax Reitz echo "ERROR: Image should have at least ${GROWTH_SIZE}K, but has ${actual_size}K" 112733d1dceSMax Reitz fi 113733d1dceSMax Reitzdone 114733d1dceSMax Reitz 115a2c7e082SMax Reitz# success, all done 116a2c7e082SMax Reitzecho '*** done' 117a2c7e082SMax Reitzrm -f $seq.full 118a2c7e082SMax Reitzstatus=0 119