xref: /openbmc/qemu/tests/qemu-iotests/175 (revision 3a20013fbb26d2a1bd11ef148eefdb1508783787)
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
40*3a20013fSNir Soffer# Parameter 2: Minimal number of blocks in an image
41*3a20013fSNir Soffer# Parameter 3: Image size in bytes
42a3bd71b5SMax Reitz_filter_blocks()
43a3bd71b5SMax Reitz{
44a3bd71b5SMax Reitz    extra_blocks=$1
45*3a20013fSNir Soffer    min_blocks=$2
46*3a20013fSNir Soffer    img_size=$3
47a3bd71b5SMax Reitz
48*3a20013fSNir Soffer    sed -e "s/blocks=$min_blocks\\(\$\\|[^0-9]\\)/min allocation/" \
49*3a20013fSNir Soffer        -e "s/blocks=$((extra_blocks + img_size / 512))\\(\$\\|[^0-9]\\)/max allocation/"
50a3bd71b5SMax Reitz}
51a3bd71b5SMax Reitz
526f993f3fSNir Soffer# get standard environment, filters and checks
536f993f3fSNir Soffer. ./common.rc
546f993f3fSNir Soffer. ./common.filter
556f993f3fSNir Soffer
566f993f3fSNir Soffer_supported_fmt raw
576f993f3fSNir Soffer_supported_proto file
586f993f3fSNir Soffer_supported_os Linux
596f993f3fSNir Soffer
60a3bd71b5SMax Reitzsize=$((1 * 1024 * 1024))
61a3bd71b5SMax Reitz
62a3bd71b5SMax Reitztouch "$TEST_DIR/empty"
63a3bd71b5SMax Reitzextra_blocks=$(stat -c '%b' "$TEST_DIR/empty")
646f993f3fSNir Soffer
65*3a20013fSNir Soffer# We always write the first byte; check how many blocks this filesystem
66*3a20013fSNir Soffer# allocates to match empty image alloation.
67*3a20013fSNir Sofferprintf "\0" > "$TEST_DIR/empty"
68*3a20013fSNir Soffermin_blocks=$(stat -c '%b' "$TEST_DIR/empty")
69*3a20013fSNir Soffer
706f993f3fSNir Sofferecho
716f993f3fSNir Sofferecho "== creating image with default preallocation =="
726f993f3fSNir Soffer_make_test_img $size | _filter_imgfmt
73*3a20013fSNir Sofferstat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $min_blocks $size
746f993f3fSNir Soffer
756f993f3fSNir Sofferfor mode in off full falloc; do
766f993f3fSNir Soffer    echo
776f993f3fSNir Soffer    echo "== creating image with preallocation $mode =="
786f993f3fSNir Soffer    IMGOPTS=preallocation=$mode _make_test_img $size | _filter_imgfmt
79*3a20013fSNir Soffer    stat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $min_blocks $size
806f993f3fSNir Sofferdone
816f993f3fSNir Soffer
826f993f3fSNir Soffer# success, all done
836f993f3fSNir Sofferecho "*** done"
846f993f3fSNir Sofferrm -f $seq.full
856f993f3fSNir Sofferstatus=0
86