1*ced14843SMax Reitz#!/bin/bash 2*ced14843SMax Reitz# 3*ced14843SMax Reitz# Test preallocated growth of qcow2 images 4*ced14843SMax Reitz# 5*ced14843SMax Reitz# Copyright (C) 2017 Red Hat, Inc. 6*ced14843SMax Reitz# 7*ced14843SMax Reitz# This program is free software; you can redistribute it and/or modify 8*ced14843SMax Reitz# it under the terms of the GNU General Public License as published by 9*ced14843SMax Reitz# the Free Software Foundation; either version 2 of the License, or 10*ced14843SMax Reitz# (at your option) any later version. 11*ced14843SMax Reitz# 12*ced14843SMax Reitz# This program is distributed in the hope that it will be useful, 13*ced14843SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 14*ced14843SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*ced14843SMax Reitz# GNU General Public License for more details. 16*ced14843SMax Reitz# 17*ced14843SMax Reitz# You should have received a copy of the GNU General Public License 18*ced14843SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 19*ced14843SMax Reitz# 20*ced14843SMax Reitz 21*ced14843SMax Reitz# creator 22*ced14843SMax Reitzowner=mreitz@redhat.com 23*ced14843SMax Reitz 24*ced14843SMax Reitzseq=$(basename $0) 25*ced14843SMax Reitzecho "QA output created by $seq" 26*ced14843SMax Reitz 27*ced14843SMax Reitzhere=$PWD 28*ced14843SMax Reitzstatus=1 # failure is the default! 29*ced14843SMax Reitz 30*ced14843SMax Reitz_cleanup() 31*ced14843SMax Reitz{ 32*ced14843SMax Reitz _cleanup_test_img 33*ced14843SMax Reitz} 34*ced14843SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 35*ced14843SMax Reitz 36*ced14843SMax Reitzget_image_size_on_host() 37*ced14843SMax Reitz{ 38*ced14843SMax Reitz $QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep "disk size" \ 39*ced14843SMax Reitz | sed -e 's/^[^0-9]*\([0-9]\+\).*$/\1/' 40*ced14843SMax Reitz} 41*ced14843SMax Reitz 42*ced14843SMax Reitz# get standard environment and filters 43*ced14843SMax Reitz. ./common.rc 44*ced14843SMax Reitz. ./common.filter 45*ced14843SMax Reitz 46*ced14843SMax Reitz_supported_fmt qcow2 47*ced14843SMax Reitz_supported_proto file 48*ced14843SMax Reitz_supported_os Linux 49*ced14843SMax Reitz 50*ced14843SMax Reitzif [ -z "$TEST_IMG_FILE" ]; then 51*ced14843SMax Reitz TEST_IMG_FILE=$TEST_IMG 52*ced14843SMax Reitzfi 53*ced14843SMax Reitz 54*ced14843SMax Reitz# Generally, we create some image with or without existing preallocation and 55*ced14843SMax Reitz# then resize it. Then we write some data into the image and verify that its 56*ced14843SMax Reitz# size does not change if we have used preallocation. 57*ced14843SMax Reitz 58*ced14843SMax Reitz# With a cluster size of 512 B, one L2 table covers 64 * 512 B = 32 kB. 59*ced14843SMax Reitz# One cluster of the L1 table covers 64 * 32 kB = 2 MB. 60*ced14843SMax Reitz# There are multiple cases we want to test: 61*ced14843SMax Reitz# (1) Grow an image without having to allocate a new L2 table. 62*ced14843SMax Reitz# (2) Grow an image, having to allocate a new L2 table. 63*ced14843SMax Reitz# (3) Grow an image, having to grow the L1 table. 64*ced14843SMax Reitz# Therefore, we create an image that is 48 kB below 2 MB. Then: 65*ced14843SMax Reitz# (1) We resize it to 2 MB - 32 kB. (+ 16 kB) 66*ced14843SMax Reitz# (2) We resize it to 2 MB. (+ 48 kB) 67*ced14843SMax Reitz# (3) We resize it to 2 MB + 32 kB. (+ 80 kB) 68*ced14843SMax Reitz 69*ced14843SMax Reitz# in B 70*ced14843SMax ReitzCREATION_SIZE=$((2 * 1024 * 1024 - 48 * 1024)) 71*ced14843SMax Reitz 72*ced14843SMax Reitz# in kB 73*ced14843SMax Reitzfor GROWTH_SIZE in 16 48 80; do 74*ced14843SMax Reitz for create_mode in off metadata falloc full; do 75*ced14843SMax Reitz for growth_mode in off metadata falloc full; do 76*ced14843SMax Reitz echo "--- growth_size=$GROWTH_SIZE create_mode=$create_mode growth_mode=$growth_mode ---" 77*ced14843SMax Reitz 78*ced14843SMax Reitz IMGOPTS="preallocation=$create_mode,cluster_size=512" _make_test_img ${CREATION_SIZE} 79*ced14843SMax Reitz $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K 80*ced14843SMax Reitz 81*ced14843SMax Reitz host_size_0=$(get_image_size_on_host) 82*ced14843SMax Reitz file_length_0=$(stat -c '%s' "$TEST_IMG_FILE") 83*ced14843SMax Reitz 84*ced14843SMax Reitz $QEMU_IO -c "write 0 $CREATION_SIZE" "$TEST_IMG" | _filter_qemu_io 85*ced14843SMax Reitz 86*ced14843SMax Reitz host_size_1=$(get_image_size_on_host) 87*ced14843SMax Reitz file_length_1=$(stat -c '%s' "$TEST_IMG_FILE") 88*ced14843SMax Reitz 89*ced14843SMax Reitz $QEMU_IO -c "write $CREATION_SIZE ${GROWTH_SIZE}K" "$TEST_IMG" | _filter_qemu_io 90*ced14843SMax Reitz 91*ced14843SMax Reitz host_size_2=$(get_image_size_on_host) 92*ced14843SMax Reitz file_length_2=$(stat -c '%s' "$TEST_IMG_FILE") 93*ced14843SMax Reitz 94*ced14843SMax Reitz # Test creation preallocation: Compare #0 against #1 95*ced14843SMax Reitz if [ $create_mode != off ]; then 96*ced14843SMax Reitz # The image length should not have grown 97*ced14843SMax Reitz if [ $file_length_1 -gt $file_length_0 ]; then 98*ced14843SMax Reitz echo "ERROR (create): Image length has grown from $file_length_0 to $file_length_1" 99*ced14843SMax Reitz fi 100*ced14843SMax Reitz if [ $create_mode != metadata ]; then 101*ced14843SMax Reitz # The host size should not have grown either 102*ced14843SMax Reitz if [ $host_size_1 -gt $host_size_0 ]; then 103*ced14843SMax Reitz echo "ERROR (create): Host size has grown from $host_size_0 to $host_size_1" 104*ced14843SMax Reitz fi 105*ced14843SMax Reitz fi 106*ced14843SMax Reitz fi 107*ced14843SMax Reitz 108*ced14843SMax Reitz # Test resize preallocation: Compare #2 against #1 109*ced14843SMax Reitz if [ $growth_mode != off ]; then 110*ced14843SMax Reitz # The image length should not have grown 111*ced14843SMax Reitz if [ $file_length_2 -gt $file_length_1 ]; then 112*ced14843SMax Reitz echo "ERROR (grow): Image length has grown from $file_length_1 to $file_length_2" 113*ced14843SMax Reitz fi 114*ced14843SMax Reitz if [ $create_mode != metadata ]; then 115*ced14843SMax Reitz # The host size should not have grown either 116*ced14843SMax Reitz if [ $host_size_2 -gt $host_size_1 ]; then 117*ced14843SMax Reitz echo "ERROR (grow): Host size has grown from $host_size_1 to $host_size_2" 118*ced14843SMax Reitz fi 119*ced14843SMax Reitz fi 120*ced14843SMax Reitz fi 121*ced14843SMax Reitz 122*ced14843SMax Reitz echo 123*ced14843SMax Reitz done 124*ced14843SMax Reitz done 125*ced14843SMax Reitzdone 126*ced14843SMax Reitz 127*ced14843SMax Reitz# success, all done 128*ced14843SMax Reitzecho '*** done' 129*ced14843SMax Reitzrm -f $seq.full 130*ced14843SMax Reitzstatus=0 131