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