xref: /openbmc/qemu/tests/qemu-iotests/125 (revision 285f595d74738f9f99c5b5b05005956e0d7c1b77)
111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash
2ced14843SMax Reitz#
3ced14843SMax Reitz# Test preallocated growth of qcow2 images
4ced14843SMax Reitz#
5ced14843SMax Reitz# Copyright (C) 2017 Red Hat, Inc.
6ced14843SMax Reitz#
7ced14843SMax Reitz# This program is free software; you can redistribute it and/or modify
8ced14843SMax Reitz# it under the terms of the GNU General Public License as published by
9ced14843SMax Reitz# the Free Software Foundation; either version 2 of the License, or
10ced14843SMax Reitz# (at your option) any later version.
11ced14843SMax Reitz#
12ced14843SMax Reitz# This program is distributed in the hope that it will be useful,
13ced14843SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
14ced14843SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15ced14843SMax Reitz# GNU General Public License for more details.
16ced14843SMax Reitz#
17ced14843SMax Reitz# You should have received a copy of the GNU General Public License
18ced14843SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19ced14843SMax Reitz#
20ced14843SMax Reitz
21ced14843SMax Reitz# creator
22ced14843SMax Reitzowner=mreitz@redhat.com
23ced14843SMax Reitz
24ced14843SMax Reitzseq=$(basename $0)
25ced14843SMax Reitzecho "QA output created by $seq"
26ced14843SMax Reitz
27ced14843SMax Reitzstatus=1	# failure is the default!
28ced14843SMax Reitz
29ced14843SMax Reitz_cleanup()
30ced14843SMax Reitz{
31ced14843SMax Reitz	_cleanup_test_img
32ced14843SMax Reitz}
33ced14843SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15
34ced14843SMax Reitz
35ced14843SMax Reitzget_image_size_on_host()
36ced14843SMax Reitz{
37ced14843SMax Reitz    $QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep "disk size" \
38ced14843SMax Reitz        | sed -e 's/^[^0-9]*\([0-9]\+\).*$/\1/'
39ced14843SMax Reitz}
40ced14843SMax Reitz
41ced14843SMax Reitz# get standard environment and filters
42ced14843SMax Reitz. ./common.rc
43ced14843SMax Reitz. ./common.filter
44ced14843SMax Reitz
45ced14843SMax Reitz_supported_fmt qcow2
46ced14843SMax Reitz_supported_proto file
47ced14843SMax Reitz
48ced14843SMax Reitzif [ -z "$TEST_IMG_FILE" ]; then
49ced14843SMax Reitz    TEST_IMG_FILE=$TEST_IMG
50ced14843SMax Reitzfi
51ced14843SMax Reitz
52*285f595dSMax Reitz# Test whether we are running on a broken XFS version.  There is this
53*285f595dSMax Reitz# bug:
54*285f595dSMax Reitz
55*285f595dSMax Reitz# $ rm -f foo
56*285f595dSMax Reitz# $ touch foo
57*285f595dSMax Reitz# $ block_size=4096 # Your FS's block size
58*285f595dSMax Reitz# $ fallocate -o $((block_size / 2)) -l $block_size foo
59*285f595dSMax Reitz# $ LANG=C xfs_bmap foo | grep hole
60*285f595dSMax Reitz#         1: [8..15]: hole
61*285f595dSMax Reitz#
62*285f595dSMax Reitz# The problem is that the XFS driver rounds down the offset and
63*285f595dSMax Reitz# rounds up the length to the block size, but independently.  As
64*285f595dSMax Reitz# such, it only allocates the first block in the example above,
65*285f595dSMax Reitz# even though it should allocate the first two blocks (because our
66*285f595dSMax Reitz# request is to fallocate something that touches both the first
67*285f595dSMax Reitz# two blocks).
68*285f595dSMax Reitz#
69*285f595dSMax Reitz# This means that when you then write to the beginning of the
70*285f595dSMax Reitz# second block, the disk usage of the first two blocks grows.
71*285f595dSMax Reitz#
72*285f595dSMax Reitz# That is precisely what fallocate() promises, though: That when you
73*285f595dSMax Reitz# write to an area that you have fallocated, no new blocks will have
74*285f595dSMax Reitz# to be allocated.
75*285f595dSMax Reitz
76*285f595dSMax Reitztouch "$TEST_IMG_FILE"
77*285f595dSMax Reitz# Assuming there is no FS with a block size greater than 64k
78*285f595dSMax Reitzfallocate -o 65535 -l 2 "$TEST_IMG_FILE"
79*285f595dSMax Reitzlen0=$(get_image_size_on_host)
80*285f595dSMax Reitz
81*285f595dSMax Reitz# Write to something that in theory we have just fallocated
82*285f595dSMax Reitz# (Thus, the on-disk size should not increase)
83*285f595dSMax Reitzpoke_file "$TEST_IMG_FILE" 65536 42
84*285f595dSMax Reitzlen1=$(get_image_size_on_host)
85*285f595dSMax Reitz
86*285f595dSMax Reitzif [ $len1 -gt $len0 ]; then
87*285f595dSMax Reitz    _notrun "the test filesystem's fallocate() is broken"
88*285f595dSMax Reitzfi
89*285f595dSMax Reitz
90*285f595dSMax Reitzrm -f "$TEST_IMG_FILE"
91*285f595dSMax Reitz
92ced14843SMax Reitz# Generally, we create some image with or without existing preallocation and
93ced14843SMax Reitz# then resize it. Then we write some data into the image and verify that its
94ced14843SMax Reitz# size does not change if we have used preallocation.
95ced14843SMax Reitz
96ced14843SMax Reitz# With a cluster size of 512 B, one L2 table covers 64 * 512 B = 32 kB.
97ced14843SMax Reitz# One cluster of the L1 table covers 64 * 32 kB = 2 MB.
98ced14843SMax Reitz# There are multiple cases we want to test:
99ced14843SMax Reitz# (1) Grow an image without having to allocate a new L2 table.
100ced14843SMax Reitz# (2) Grow an image, having to allocate a new L2 table.
101ced14843SMax Reitz# (3) Grow an image, having to grow the L1 table.
102ced14843SMax Reitz# Therefore, we create an image that is 48 kB below 2 MB. Then:
103ced14843SMax Reitz# (1) We resize it to 2 MB - 32 kB. (+ 16 kB)
104ced14843SMax Reitz# (2) We resize it to 2 MB.         (+ 48 kB)
105ced14843SMax Reitz# (3) We resize it to 2 MB + 32 kB. (+ 80 kB)
106ced14843SMax Reitz
107ced14843SMax Reitz# in B
108ced14843SMax ReitzCREATION_SIZE=$((2 * 1024 * 1024 - 48 * 1024))
109ced14843SMax Reitz
1104c112a39SMax Reitz# 512 is the actual test -- but it's good to test 64k as well, just to be sure.
1114c112a39SMax Reitzfor cluster_size in 512 64k; do
112ced14843SMax Reitz# in kB
113ced14843SMax Reitzfor GROWTH_SIZE in 16 48 80; do
114ced14843SMax Reitz    for create_mode in off metadata falloc full; do
115ced14843SMax Reitz        for growth_mode in off metadata falloc full; do
1164c112a39SMax Reitz            echo "--- cluster_size=$cluster_size growth_size=$GROWTH_SIZE create_mode=$create_mode growth_mode=$growth_mode ---"
117ced14843SMax Reitz
1184c112a39SMax Reitz            IMGOPTS="preallocation=$create_mode,cluster_size=$cluster_size" _make_test_img ${CREATION_SIZE}
119ced14843SMax Reitz            $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K
120ced14843SMax Reitz
121ced14843SMax Reitz            host_size_0=$(get_image_size_on_host)
122ced14843SMax Reitz            file_length_0=$(stat -c '%s' "$TEST_IMG_FILE")
123ced14843SMax Reitz
124ced14843SMax Reitz            $QEMU_IO -c "write 0 $CREATION_SIZE" "$TEST_IMG" | _filter_qemu_io
125ced14843SMax Reitz
126ced14843SMax Reitz            host_size_1=$(get_image_size_on_host)
127ced14843SMax Reitz            file_length_1=$(stat -c '%s' "$TEST_IMG_FILE")
128ced14843SMax Reitz
129ced14843SMax Reitz            $QEMU_IO -c "write $CREATION_SIZE ${GROWTH_SIZE}K" "$TEST_IMG" | _filter_qemu_io
130ced14843SMax Reitz
131ced14843SMax Reitz            host_size_2=$(get_image_size_on_host)
132ced14843SMax Reitz            file_length_2=$(stat -c '%s' "$TEST_IMG_FILE")
133ced14843SMax Reitz
134ced14843SMax Reitz            # Test creation preallocation: Compare #0 against #1
135ced14843SMax Reitz            if [ $create_mode != off ]; then
136ced14843SMax Reitz                # The image length should not have grown
137ced14843SMax Reitz                if [ $file_length_1 -gt $file_length_0 ]; then
138ced14843SMax Reitz                    echo "ERROR (create): Image length has grown from $file_length_0 to $file_length_1"
139ced14843SMax Reitz                fi
140ced14843SMax Reitz                if [ $create_mode != metadata ]; then
141ced14843SMax Reitz                    # The host size should not have grown either
142ced14843SMax Reitz                    if [ $host_size_1 -gt $host_size_0 ]; then
143ced14843SMax Reitz                        echo "ERROR (create): Host size has grown from $host_size_0 to $host_size_1"
144ced14843SMax Reitz                    fi
145ced14843SMax Reitz                fi
146ced14843SMax Reitz            fi
147ced14843SMax Reitz
148ced14843SMax Reitz            # Test resize preallocation: Compare #2 against #1
149ced14843SMax Reitz            if [ $growth_mode != off ]; then
150ced14843SMax Reitz                # The image length should not have grown
151ced14843SMax Reitz                if [ $file_length_2 -gt $file_length_1 ]; then
152ced14843SMax Reitz                    echo "ERROR (grow): Image length has grown from $file_length_1 to $file_length_2"
153ced14843SMax Reitz                fi
154e6e8db03SMax Reitz                if [ $growth_mode != metadata ]; then
155ced14843SMax Reitz                    # The host size should not have grown either
156ced14843SMax Reitz                    if [ $host_size_2 -gt $host_size_1 ]; then
157ced14843SMax Reitz                        echo "ERROR (grow): Host size has grown from $host_size_1 to $host_size_2"
158ced14843SMax Reitz                    fi
159ced14843SMax Reitz                fi
160ced14843SMax Reitz            fi
161ced14843SMax Reitz
162ced14843SMax Reitz            echo
163ced14843SMax Reitz        done
164ced14843SMax Reitz    done
165ced14843SMax Reitzdone
1664c112a39SMax Reitzdone
167ced14843SMax Reitz
168ced14843SMax Reitz# success, all done
169ced14843SMax Reitzecho '*** done'
170ced14843SMax Reitzrm -f $seq.full
171ced14843SMax Reitzstatus=0
172