xref: /openbmc/qemu/tests/qemu-iotests/125 (revision a37eaa53)
1#!/bin/bash
2#
3# Test preallocated growth of qcow2 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
27here=$PWD
28status=1	# failure is the default!
29
30_cleanup()
31{
32	_cleanup_test_img
33}
34trap "_cleanup; exit \$status" 0 1 2 3 15
35
36get_image_size_on_host()
37{
38    $QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep "disk size" \
39        | sed -e 's/^[^0-9]*\([0-9]\+\).*$/\1/'
40}
41
42# get standard environment and filters
43. ./common.rc
44. ./common.filter
45
46_supported_fmt qcow2
47_supported_proto file
48_supported_os Linux
49
50if [ -z "$TEST_IMG_FILE" ]; then
51    TEST_IMG_FILE=$TEST_IMG
52fi
53
54# Generally, we create some image with or without existing preallocation and
55# then resize it. Then we write some data into the image and verify that its
56# size does not change if we have used preallocation.
57
58# With a cluster size of 512 B, one L2 table covers 64 * 512 B = 32 kB.
59# One cluster of the L1 table covers 64 * 32 kB = 2 MB.
60# There are multiple cases we want to test:
61# (1) Grow an image without having to allocate a new L2 table.
62# (2) Grow an image, having to allocate a new L2 table.
63# (3) Grow an image, having to grow the L1 table.
64# Therefore, we create an image that is 48 kB below 2 MB. Then:
65# (1) We resize it to 2 MB - 32 kB. (+ 16 kB)
66# (2) We resize it to 2 MB.         (+ 48 kB)
67# (3) We resize it to 2 MB + 32 kB. (+ 80 kB)
68
69# in B
70CREATION_SIZE=$((2 * 1024 * 1024 - 48 * 1024))
71
72# 512 is the actual test -- but it's good to test 64k as well, just to be sure.
73for cluster_size in 512 64k; do
74# in kB
75for GROWTH_SIZE in 16 48 80; do
76    for create_mode in off metadata falloc full; do
77        for growth_mode in off metadata falloc full; do
78            echo "--- cluster_size=$cluster_size growth_size=$GROWTH_SIZE create_mode=$create_mode growth_mode=$growth_mode ---"
79
80            IMGOPTS="preallocation=$create_mode,cluster_size=$cluster_size" _make_test_img ${CREATION_SIZE}
81            $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K
82
83            host_size_0=$(get_image_size_on_host)
84            file_length_0=$(stat -c '%s' "$TEST_IMG_FILE")
85
86            $QEMU_IO -c "write 0 $CREATION_SIZE" "$TEST_IMG" | _filter_qemu_io
87
88            host_size_1=$(get_image_size_on_host)
89            file_length_1=$(stat -c '%s' "$TEST_IMG_FILE")
90
91            $QEMU_IO -c "write $CREATION_SIZE ${GROWTH_SIZE}K" "$TEST_IMG" | _filter_qemu_io
92
93            host_size_2=$(get_image_size_on_host)
94            file_length_2=$(stat -c '%s' "$TEST_IMG_FILE")
95
96            # Test creation preallocation: Compare #0 against #1
97            if [ $create_mode != off ]; then
98                # The image length should not have grown
99                if [ $file_length_1 -gt $file_length_0 ]; then
100                    echo "ERROR (create): Image length has grown from $file_length_0 to $file_length_1"
101                fi
102                if [ $create_mode != metadata ]; then
103                    # The host size should not have grown either
104                    if [ $host_size_1 -gt $host_size_0 ]; then
105                        echo "ERROR (create): Host size has grown from $host_size_0 to $host_size_1"
106                    fi
107                fi
108            fi
109
110            # Test resize preallocation: Compare #2 against #1
111            if [ $growth_mode != off ]; then
112                # The image length should not have grown
113                if [ $file_length_2 -gt $file_length_1 ]; then
114                    echo "ERROR (grow): Image length has grown from $file_length_1 to $file_length_2"
115                fi
116                if [ $create_mode != metadata ]; then
117                    # The host size should not have grown either
118                    if [ $host_size_2 -gt $host_size_1 ]; then
119                        echo "ERROR (grow): Host size has grown from $host_size_1 to $host_size_2"
120                    fi
121                fi
122            fi
123
124            echo
125        done
126    done
127done
128done
129
130# success, all done
131echo '*** done'
132rm -f $seq.full
133status=0
134