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