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