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