111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw 3ced14843SMax Reitz# 4ced14843SMax Reitz# Test preallocated growth of qcow2 images 5ced14843SMax Reitz# 6ced14843SMax Reitz# Copyright (C) 2017 Red Hat, Inc. 7ced14843SMax Reitz# 8ced14843SMax Reitz# This program is free software; you can redistribute it and/or modify 9ced14843SMax Reitz# it under the terms of the GNU General Public License as published by 10ced14843SMax Reitz# the Free Software Foundation; either version 2 of the License, or 11ced14843SMax Reitz# (at your option) any later version. 12ced14843SMax Reitz# 13ced14843SMax Reitz# This program is distributed in the hope that it will be useful, 14ced14843SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 15ced14843SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16ced14843SMax Reitz# GNU General Public License for more details. 17ced14843SMax Reitz# 18ced14843SMax Reitz# You should have received a copy of the GNU General Public License 19ced14843SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 20ced14843SMax Reitz# 21ced14843SMax Reitz 22ced14843SMax Reitz# creator 23*42a5009dSJohn Snowowner=hreitz@redhat.com 24ced14843SMax Reitz 25ced14843SMax Reitzseq=$(basename $0) 26ced14843SMax Reitzecho "QA output created by $seq" 27ced14843SMax Reitz 28ced14843SMax Reitzstatus=1 # failure is the default! 29ced14843SMax Reitz 30ced14843SMax Reitz_cleanup() 31ced14843SMax Reitz{ 32ced14843SMax Reitz _cleanup_test_img 33ced14843SMax Reitz} 34ced14843SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 35ced14843SMax Reitz 36ced14843SMax Reitzget_image_size_on_host() 37ced14843SMax Reitz{ 38f2d86adeSMax Reitz echo $(($(stat -c '%b * %B' "$TEST_IMG_FILE"))) 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 478e958260SAlberto Garcia# Growing a file with a backing file (without preallocation=full or 488e958260SAlberto Garcia# =falloc) requires zeroing the newly added area, which is impossible 498e958260SAlberto Garcia# to do quickly for v2 images, and hence is unsupported. 508e958260SAlberto Garcia_unsupported_imgopts 'compat=0.10' 51ced14843SMax Reitz 52ced14843SMax Reitzif [ -z "$TEST_IMG_FILE" ]; then 53ced14843SMax Reitz TEST_IMG_FILE=$TEST_IMG 54ced14843SMax Reitzfi 55ced14843SMax Reitz 56285f595dSMax Reitz# Test whether we are running on a broken XFS version. There is this 57285f595dSMax Reitz# bug: 58285f595dSMax Reitz 59285f595dSMax Reitz# $ rm -f foo 60285f595dSMax Reitz# $ touch foo 61285f595dSMax Reitz# $ block_size=4096 # Your FS's block size 62285f595dSMax Reitz# $ fallocate -o $((block_size / 2)) -l $block_size foo 63285f595dSMax Reitz# $ LANG=C xfs_bmap foo | grep hole 64285f595dSMax Reitz# 1: [8..15]: hole 65285f595dSMax Reitz# 66285f595dSMax Reitz# The problem is that the XFS driver rounds down the offset and 67285f595dSMax Reitz# rounds up the length to the block size, but independently. As 68285f595dSMax Reitz# such, it only allocates the first block in the example above, 69285f595dSMax Reitz# even though it should allocate the first two blocks (because our 70285f595dSMax Reitz# request is to fallocate something that touches both the first 71285f595dSMax Reitz# two blocks). 72285f595dSMax Reitz# 73285f595dSMax Reitz# This means that when you then write to the beginning of the 74285f595dSMax Reitz# second block, the disk usage of the first two blocks grows. 75285f595dSMax Reitz# 76285f595dSMax Reitz# That is precisely what fallocate() promises, though: That when you 77285f595dSMax Reitz# write to an area that you have fallocated, no new blocks will have 78285f595dSMax Reitz# to be allocated. 79285f595dSMax Reitz 80285f595dSMax Reitztouch "$TEST_IMG_FILE" 81285f595dSMax Reitz# Assuming there is no FS with a block size greater than 64k 82285f595dSMax Reitzfallocate -o 65535 -l 2 "$TEST_IMG_FILE" 83285f595dSMax Reitzlen0=$(get_image_size_on_host) 84285f595dSMax Reitz 85285f595dSMax Reitz# Write to something that in theory we have just fallocated 86285f595dSMax Reitz# (Thus, the on-disk size should not increase) 87285f595dSMax Reitzpoke_file "$TEST_IMG_FILE" 65536 42 88285f595dSMax Reitzlen1=$(get_image_size_on_host) 89285f595dSMax Reitz 90285f595dSMax Reitzif [ $len1 -gt $len0 ]; then 91285f595dSMax Reitz _notrun "the test filesystem's fallocate() is broken" 92285f595dSMax Reitzfi 93285f595dSMax Reitz 94285f595dSMax Reitzrm -f "$TEST_IMG_FILE" 95285f595dSMax Reitz 96ced14843SMax Reitz# Generally, we create some image with or without existing preallocation and 97ced14843SMax Reitz# then resize it. Then we write some data into the image and verify that its 98ced14843SMax Reitz# size does not change if we have used preallocation. 99ced14843SMax Reitz 100ced14843SMax Reitz# With a cluster size of 512 B, one L2 table covers 64 * 512 B = 32 kB. 101ced14843SMax Reitz# One cluster of the L1 table covers 64 * 32 kB = 2 MB. 102ced14843SMax Reitz# There are multiple cases we want to test: 103ced14843SMax Reitz# (1) Grow an image without having to allocate a new L2 table. 104ced14843SMax Reitz# (2) Grow an image, having to allocate a new L2 table. 105ced14843SMax Reitz# (3) Grow an image, having to grow the L1 table. 106ced14843SMax Reitz# Therefore, we create an image that is 48 kB below 2 MB. Then: 107ced14843SMax Reitz# (1) We resize it to 2 MB - 32 kB. (+ 16 kB) 108ced14843SMax Reitz# (2) We resize it to 2 MB. (+ 48 kB) 109ced14843SMax Reitz# (3) We resize it to 2 MB + 32 kB. (+ 80 kB) 110ced14843SMax Reitz 111ced14843SMax Reitz# in B 112ced14843SMax ReitzCREATION_SIZE=$((2 * 1024 * 1024 - 48 * 1024)) 113ced14843SMax Reitz 1144c112a39SMax Reitz# 512 is the actual test -- but it's good to test 64k as well, just to be sure. 1154c112a39SMax Reitzfor cluster_size in 512 64k; do 116ced14843SMax Reitz# in kB 117ced14843SMax Reitzfor GROWTH_SIZE in 16 48 80; do 118ced14843SMax Reitz for create_mode in off metadata falloc full; do 119ced14843SMax Reitz for growth_mode in off metadata falloc full; do 1204c112a39SMax Reitz echo "--- cluster_size=$cluster_size growth_size=$GROWTH_SIZE create_mode=$create_mode growth_mode=$growth_mode ---" 121ced14843SMax Reitz 122407fb56aSMax Reitz _make_test_img -o "preallocation=$create_mode,cluster_size=$cluster_size" ${CREATION_SIZE} 123ced14843SMax Reitz $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K 124ced14843SMax Reitz 125ced14843SMax Reitz host_size_0=$(get_image_size_on_host) 126ced14843SMax Reitz file_length_0=$(stat -c '%s' "$TEST_IMG_FILE") 127ced14843SMax Reitz 128ced14843SMax Reitz $QEMU_IO -c "write 0 $CREATION_SIZE" "$TEST_IMG" | _filter_qemu_io 129ced14843SMax Reitz 130ced14843SMax Reitz host_size_1=$(get_image_size_on_host) 131ced14843SMax Reitz file_length_1=$(stat -c '%s' "$TEST_IMG_FILE") 132ced14843SMax Reitz 133ced14843SMax Reitz $QEMU_IO -c "write $CREATION_SIZE ${GROWTH_SIZE}K" "$TEST_IMG" | _filter_qemu_io 134ced14843SMax Reitz 135ced14843SMax Reitz host_size_2=$(get_image_size_on_host) 136ced14843SMax Reitz file_length_2=$(stat -c '%s' "$TEST_IMG_FILE") 137ced14843SMax Reitz 138ced14843SMax Reitz # Test creation preallocation: Compare #0 against #1 139ced14843SMax Reitz if [ $create_mode != off ]; then 140ced14843SMax Reitz # The image length should not have grown 141ced14843SMax Reitz if [ $file_length_1 -gt $file_length_0 ]; then 142ced14843SMax Reitz echo "ERROR (create): Image length has grown from $file_length_0 to $file_length_1" 143ced14843SMax Reitz fi 144ced14843SMax Reitz if [ $create_mode != metadata ]; then 145ced14843SMax Reitz # The host size should not have grown either 146ced14843SMax Reitz if [ $host_size_1 -gt $host_size_0 ]; then 147ced14843SMax Reitz echo "ERROR (create): Host size has grown from $host_size_0 to $host_size_1" 148ced14843SMax Reitz fi 149ced14843SMax Reitz fi 150ced14843SMax Reitz fi 151ced14843SMax Reitz 152ced14843SMax Reitz # Test resize preallocation: Compare #2 against #1 153ced14843SMax Reitz if [ $growth_mode != off ]; then 154ced14843SMax Reitz # The image length should not have grown 155ced14843SMax Reitz if [ $file_length_2 -gt $file_length_1 ]; then 156ced14843SMax Reitz echo "ERROR (grow): Image length has grown from $file_length_1 to $file_length_2" 157ced14843SMax Reitz fi 158e6e8db03SMax Reitz if [ $growth_mode != metadata ]; then 159ced14843SMax Reitz # The host size should not have grown either 160ced14843SMax Reitz if [ $host_size_2 -gt $host_size_1 ]; then 161ced14843SMax Reitz echo "ERROR (grow): Host size has grown from $host_size_1 to $host_size_2" 162ced14843SMax Reitz fi 163ced14843SMax Reitz fi 164ced14843SMax Reitz fi 165ced14843SMax Reitz 166ced14843SMax Reitz echo 167ced14843SMax Reitz done 168ced14843SMax Reitz done 169ced14843SMax Reitzdone 1704c112a39SMax Reitzdone 171ced14843SMax Reitz 172a5675f39SAlberto Garcia# Test image resizing using preallocation and unaligned offsets 173a5675f39SAlberto Garcia$QEMU_IMG create -f raw "$TEST_IMG.base" 128k | _filter_img_create 174a5675f39SAlberto Garcia$QEMU_IO -c 'write -q -P 1 0 128k' -f raw "$TEST_IMG.base" 175a5675f39SAlberto Garciafor orig_size in 31k 33k; do 1768e958260SAlberto Garcia for dst_size in 96k 128k; do 1778e958260SAlberto Garcia for prealloc in metadata full; do 1788e958260SAlberto Garcia echo "--- Resizing image from $orig_size to $dst_size (preallocation=$prealloc) ---" 179a5675f39SAlberto Garcia _make_test_img -F raw -b "$TEST_IMG.base" -o cluster_size=64k "$orig_size" 1808e958260SAlberto Garcia $QEMU_IMG resize -f "$IMGFMT" --preallocation="$prealloc" "$TEST_IMG" "$dst_size" 181a5675f39SAlberto Garcia # The first part of the image should contain data from the backing file 182a5675f39SAlberto Garcia $QEMU_IO -c "read -q -P 1 0 ${orig_size}" "$TEST_IMG" 183a5675f39SAlberto Garcia # The resized part of the image should contain zeroes 184a5675f39SAlberto Garcia $QEMU_IO -c "read -q -P 0 ${orig_size} 63k" "$TEST_IMG" 185a5675f39SAlberto Garcia # If the image does not have an external data file we can also verify its 186a5675f39SAlberto Garcia # actual size. The resized image should have 7 clusters: 187a5675f39SAlberto Garcia # header, L1 table, L2 table, refcount table, refcount block, 2 data clusters 188a5675f39SAlberto Garcia if ! _get_data_file "$TEST_IMG" > /dev/null; then 189a5675f39SAlberto Garcia expected_file_length=$((65536 * 7)) 190a5675f39SAlberto Garcia file_length=$(stat -c '%s' "$TEST_IMG_FILE") 191a5675f39SAlberto Garcia if [ "$file_length" != "$expected_file_length" ]; then 192a5675f39SAlberto Garcia echo "ERROR: file length $file_length (expected $expected_file_length)" 193a5675f39SAlberto Garcia fi 194a5675f39SAlberto Garcia fi 195a5675f39SAlberto Garcia echo 196a5675f39SAlberto Garcia done 1978e958260SAlberto Garcia done 1988e958260SAlberto Garciadone 199a5675f39SAlberto Garcia 200ced14843SMax Reitz# success, all done 201ced14843SMax Reitzecho '*** done' 202ced14843SMax Reitzrm -f $seq.full 203ced14843SMax Reitzstatus=0 204