xref: /openbmc/qemu/tests/qemu-iotests/106 (revision 733d1dce)
1a2c7e082SMax Reitz#!/bin/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 Reitzhere=$PWD
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
41a2c7e082SMax Reitz_supported_proto file
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
55a2c7e082SMax Reitz        IMGOPTS="preallocation=$create_mode" _make_test_img ${CREATION_SIZE}K
56a2c7e082SMax Reitz        $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K
57a2c7e082SMax Reitz
58a2c7e082SMax Reitz        expected_size=0
59a2c7e082SMax Reitz        if [ $create_mode != off ]; then
60a2c7e082SMax Reitz            expected_size=$CREATION_SIZE
61a2c7e082SMax Reitz        fi
62a2c7e082SMax Reitz        if [ $growth_mode != off ]; then
63a2c7e082SMax Reitz            expected_size=$((expected_size + $GROWTH_SIZE))
64a2c7e082SMax Reitz        fi
65a2c7e082SMax Reitz
66a2c7e082SMax Reitz        actual_size=$($QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep 'disk size')
67a2c7e082SMax Reitz        actual_size=$(echo "$actual_size" | sed -e 's/^[^0-9]*\([0-9]\+\).*$/\1/')
68a2c7e082SMax Reitz
69a2c7e082SMax Reitz        # The actual size may exceed the expected size, depending on the file
70a2c7e082SMax Reitz        # system. Therefore we just test that the actual size is at least what
71a2c7e082SMax Reitz        # we expect.
72a2c7e082SMax Reitz        if [ $actual_size -lt $expected_size ]; then
73a2c7e082SMax Reitz            echo "ERROR: Image should have at least ${expected_size}K, but has ${actual_size}K"
74a2c7e082SMax Reitz        fi
75a2c7e082SMax Reitz    done
76a2c7e082SMax Reitzdone
77a2c7e082SMax Reitz
78a2c7e082SMax Reitzecho
79a2c7e082SMax Reitzecho '=== Testing image shrinking ==='
80a2c7e082SMax Reitz
81a2c7e082SMax Reitz# None of this should work except for "off", because other modes cannot be used
82a2c7e082SMax Reitz# for shrinking
83a2c7e082SMax Reitzfor growth_mode in falloc full off; do
84a2c7e082SMax Reitz    echo
85a2c7e082SMax Reitz    echo "--- growth_mode=$growth_mode ---"
864ffca890SPavel Butsykin    $QEMU_IMG resize -f "$IMGFMT" --shrink --preallocation=$growth_mode "$TEST_IMG" -${GROWTH_SIZE}K
87a2c7e082SMax Reitzdone
88a2c7e082SMax Reitz
89*733d1dceSMax Reitzecho
90*733d1dceSMax Reitzecho '=== Testing image growth on 2G empty image ==='
91*733d1dceSMax Reitz
92*733d1dceSMax Reitzfor growth_mode in falloc full; do
93*733d1dceSMax Reitz    echo
94*733d1dceSMax Reitz    echo "--- growth_mode=$growth_mode ---"
95*733d1dceSMax Reitz
96*733d1dceSMax Reitz    # Maybe we want to do an lseek() to the end of the file before the
97*733d1dceSMax Reitz    # preallocation; if the file has a length of 2 GB, that would
98*733d1dceSMax Reitz    # return an integer that overflows to negative when put into a
99*733d1dceSMax Reitz    # plain int.  We should use the correct type for the result, and
100*733d1dceSMax Reitz    # this tests we do.
101*733d1dceSMax Reitz
102*733d1dceSMax Reitz    _make_test_img 2G
103*733d1dceSMax Reitz    $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K
104*733d1dceSMax Reitz
105*733d1dceSMax Reitz    actual_size=$($QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep 'disk size')
106*733d1dceSMax Reitz    actual_size=$(echo "$actual_size" | sed -e 's/^[^0-9]*\([0-9]\+\).*$/\1/')
107*733d1dceSMax Reitz
108*733d1dceSMax Reitz    if [ $actual_size -lt $GROWTH_SIZE ]; then
109*733d1dceSMax Reitz        echo "ERROR: Image should have at least ${GROWTH_SIZE}K, but has ${actual_size}K"
110*733d1dceSMax Reitz    fi
111*733d1dceSMax Reitzdone
112*733d1dceSMax Reitz
113a2c7e082SMax Reitz# success, all done
114a2c7e082SMax Reitzecho '*** done'
115a2c7e082SMax Reitzrm -f $seq.full
116a2c7e082SMax Reitzstatus=0
117