111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2*9dd003a9SVladimir Sementsov-Ogievskiy# group: quick 36f993f3fSNir Soffer# 46f993f3fSNir Soffer# Test creating raw image preallocation mode 56f993f3fSNir Soffer# 66f993f3fSNir Soffer# Copyright (C) 2017 Nir Soffer <nirsof@gmail.com> 76f993f3fSNir Soffer# 86f993f3fSNir Soffer# This program is free software; you can redistribute it and/or modify 96f993f3fSNir Soffer# it under the terms of the GNU General Public License as published by 106f993f3fSNir Soffer# the Free Software Foundation; either version 2 of the License, or 116f993f3fSNir Soffer# (at your option) any later version. 126f993f3fSNir Soffer# 136f993f3fSNir Soffer# This program is distributed in the hope that it will be useful, 146f993f3fSNir Soffer# but WITHOUT ANY WARRANTY; without even the implied warranty of 156f993f3fSNir Soffer# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 166f993f3fSNir Soffer# GNU General Public License for more details. 176f993f3fSNir Soffer# 186f993f3fSNir Soffer# You should have received a copy of the GNU General Public License 196f993f3fSNir Soffer# along with this program. If not, see <http://www.gnu.org/licenses/>. 206f993f3fSNir Soffer# 216f993f3fSNir Soffer 226f993f3fSNir Soffer# creator 236f993f3fSNir Sofferowner=nirsof@gmail.com 246f993f3fSNir Soffer 256f993f3fSNir Sofferseq=`basename $0` 266f993f3fSNir Sofferecho "QA output created by $seq" 276f993f3fSNir Soffer 286f993f3fSNir Sofferstatus=1 # failure is the default! 296f993f3fSNir Soffer 306f993f3fSNir Soffer_cleanup() 316f993f3fSNir Soffer{ 326f993f3fSNir Soffer _cleanup_test_img 33a3bd71b5SMax Reitz rm -f "$TEST_DIR/empty" 346f993f3fSNir Soffer} 356f993f3fSNir Soffertrap "_cleanup; exit \$status" 0 1 2 3 15 366f993f3fSNir Soffer 37a3bd71b5SMax Reitz# Some file systems sometimes allocate extra blocks independently of 38a3bd71b5SMax Reitz# the file size. This function hides the resulting difference in the 39a3bd71b5SMax Reitz# stat -c '%b' output. 40a3bd71b5SMax Reitz# Parameter 1: Number of blocks an empty file occupies 413a20013fSNir Soffer# Parameter 2: Minimal number of blocks in an image 423a20013fSNir Soffer# Parameter 3: Image size in bytes 43a3bd71b5SMax Reitz_filter_blocks() 44a3bd71b5SMax Reitz{ 45a3bd71b5SMax Reitz extra_blocks=$1 463a20013fSNir Soffer min_blocks=$2 473a20013fSNir Soffer img_size=$3 48a3bd71b5SMax Reitz 493a20013fSNir Soffer sed -e "s/blocks=$min_blocks\\(\$\\|[^0-9]\\)/min allocation/" \ 503a20013fSNir Soffer -e "s/blocks=$((extra_blocks + img_size / 512))\\(\$\\|[^0-9]\\)/max allocation/" 51a3bd71b5SMax Reitz} 52a3bd71b5SMax Reitz 537e3dc2baSNir Soffer# Resize image using block_resize. 547e3dc2baSNir Soffer# Parameter 1: image path 557e3dc2baSNir Soffer# Parameter 2: new size 567e3dc2baSNir Soffer_block_resize() 577e3dc2baSNir Soffer{ 587e3dc2baSNir Soffer local path=$1 597e3dc2baSNir Soffer local size=$2 607e3dc2baSNir Soffer 617e3dc2baSNir Soffer $QEMU -qmp stdio -nographic -nodefaults \ 627e3dc2baSNir Soffer -blockdev file,node-name=file,filename=$path,cache.direct=on \ 637e3dc2baSNir Soffer <<EOF 647e3dc2baSNir Soffer{'execute': 'qmp_capabilities'} 657e3dc2baSNir Soffer{'execute': 'block_resize', 'arguments': {'node-name': 'file', 'size': $size}} 667e3dc2baSNir Soffer{'execute': 'quit'} 677e3dc2baSNir SofferEOF 687e3dc2baSNir Soffer} 697e3dc2baSNir Soffer 706f993f3fSNir Soffer# get standard environment, filters and checks 716f993f3fSNir Soffer. ./common.rc 726f993f3fSNir Soffer. ./common.filter 736f993f3fSNir Soffer 746f993f3fSNir Soffer_supported_fmt raw 7557284d2aSMax Reitz_supported_proto file fuse 766f993f3fSNir Soffer_supported_os Linux 776f993f3fSNir Soffer 787e3dc2baSNir Soffer_default_cache_mode none 797e3dc2baSNir Soffer_supported_cache_modes none directsync 807e3dc2baSNir Soffer 81a3bd71b5SMax Reitzsize=$((1 * 1024 * 1024)) 82a3bd71b5SMax Reitz 83a3bd71b5SMax Reitztouch "$TEST_DIR/empty" 84a3bd71b5SMax Reitzextra_blocks=$(stat -c '%b' "$TEST_DIR/empty") 856f993f3fSNir Soffer 863a20013fSNir Soffer# We always write the first byte; check how many blocks this filesystem 873a20013fSNir Soffer# allocates to match empty image alloation. 883a20013fSNir Sofferprintf "\0" > "$TEST_DIR/empty" 893a20013fSNir Soffermin_blocks=$(stat -c '%b' "$TEST_DIR/empty") 903a20013fSNir Soffer 916f993f3fSNir Sofferecho 926f993f3fSNir Sofferecho "== creating image with default preallocation ==" 93f96e59daSMax Reitz_make_test_img -o extent_size_hint=0 $size 943a20013fSNir Sofferstat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $min_blocks $size 956f993f3fSNir Soffer 966f993f3fSNir Sofferfor mode in off full falloc; do 976f993f3fSNir Soffer echo 986f993f3fSNir Soffer echo "== creating image with preallocation $mode ==" 99f96e59daSMax Reitz _make_test_img -o preallocation=$mode,extent_size_hint=0 $size 1003a20013fSNir Soffer stat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $min_blocks $size 1016f993f3fSNir Sofferdone 1026f993f3fSNir Soffer 1037e3dc2baSNir Sofferfor new_size in 4096 1048576; do 1047e3dc2baSNir Soffer echo 1057e3dc2baSNir Soffer echo "== resize empty image with block_resize ==" 106f96e59daSMax Reitz _make_test_img -o extent_size_hint=0 0 1077e3dc2baSNir Soffer _block_resize $TEST_IMG $new_size >/dev/null 1087e3dc2baSNir Soffer stat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $min_blocks $new_size 1097e3dc2baSNir Sofferdone 1107e3dc2baSNir Soffer 1116f993f3fSNir Soffer# success, all done 1126f993f3fSNir Sofferecho "*** done" 1136f993f3fSNir Sofferrm -f $seq.full 1146f993f3fSNir Sofferstatus=0 115