Philippe Mathieu-Daudé | 11a82d1 | 2019-03-07 15:58:38 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Vladimir Sementsov-Ogievskiy | 9dd003a | 2021-01-16 16:44:19 +0300 | [diff] [blame] | 2 | # group: rw quick |
Max Reitz | a2c7e08 | 2017-06-13 22:21:06 +0200 | [diff] [blame] | 3 | # |
| 4 | # Test preallocated resize of raw images |
| 5 | # |
| 6 | # Copyright (C) 2017 Red Hat, Inc. |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | # |
| 21 | |
| 22 | # creator |
John Snow | 42a5009 | 2022-03-22 13:42:12 -0400 | [diff] [blame] | 23 | owner=hreitz@redhat.com |
Max Reitz | a2c7e08 | 2017-06-13 22:21:06 +0200 | [diff] [blame] | 24 | |
| 25 | seq=$(basename $0) |
| 26 | echo "QA output created by $seq" |
| 27 | |
Max Reitz | a2c7e08 | 2017-06-13 22:21:06 +0200 | [diff] [blame] | 28 | status=1 # failure is the default! |
| 29 | |
| 30 | _cleanup() |
| 31 | { |
| 32 | _cleanup_test_img |
| 33 | } |
| 34 | trap "_cleanup; exit \$status" 0 1 2 3 15 |
| 35 | |
| 36 | # get standard environment and filters |
| 37 | . ./common.rc |
| 38 | . ./common.filter |
| 39 | |
| 40 | _supported_fmt raw |
Max Reitz | 57284d2 | 2020-10-27 20:05:59 +0100 | [diff] [blame] | 41 | _supported_proto file fuse |
Max Reitz | a2c7e08 | 2017-06-13 22:21:06 +0200 | [diff] [blame] | 42 | _supported_os Linux |
| 43 | |
| 44 | # in kB |
| 45 | CREATION_SIZE=128 |
| 46 | GROWTH_SIZE=256 |
| 47 | |
| 48 | echo '=== Testing image growth ===' |
| 49 | |
| 50 | for create_mode in off falloc full; do |
| 51 | for growth_mode in off falloc full; do |
| 52 | echo |
| 53 | echo "--- create_mode=$create_mode growth_mode=$growth_mode ---" |
| 54 | |
Kevin Wolf | ffa244c | 2020-07-07 16:23:29 +0200 | [diff] [blame] | 55 | # Our calculation below assumes kilobytes as unit for the actual size. |
| 56 | # Disable the extent size hint because it would give us a result in |
| 57 | # megabytes. |
| 58 | _make_test_img -o "preallocation=$create_mode,extent_size_hint=0" ${CREATION_SIZE}K |
Max Reitz | a2c7e08 | 2017-06-13 22:21:06 +0200 | [diff] [blame] | 59 | $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K |
| 60 | |
| 61 | expected_size=0 |
| 62 | if [ $create_mode != off ]; then |
| 63 | expected_size=$CREATION_SIZE |
| 64 | fi |
| 65 | if [ $growth_mode != off ]; then |
| 66 | expected_size=$((expected_size + $GROWTH_SIZE)) |
| 67 | fi |
| 68 | |
Hanna Reitz | 74163ad | 2022-06-20 18:27:02 +0200 | [diff] [blame] | 69 | actual_size=$($QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep 'disk size' | head -n 1) |
Max Reitz | a2c7e08 | 2017-06-13 22:21:06 +0200 | [diff] [blame] | 70 | actual_size=$(echo "$actual_size" | sed -e 's/^[^0-9]*\([0-9]\+\).*$/\1/') |
| 71 | |
| 72 | # The actual size may exceed the expected size, depending on the file |
| 73 | # system. Therefore we just test that the actual size is at least what |
| 74 | # we expect. |
| 75 | if [ $actual_size -lt $expected_size ]; then |
| 76 | echo "ERROR: Image should have at least ${expected_size}K, but has ${actual_size}K" |
| 77 | fi |
| 78 | done |
| 79 | done |
| 80 | |
| 81 | echo |
| 82 | echo '=== Testing image shrinking ===' |
| 83 | |
| 84 | # None of this should work except for "off", because other modes cannot be used |
| 85 | # for shrinking |
| 86 | for growth_mode in falloc full off; do |
| 87 | echo |
| 88 | echo "--- growth_mode=$growth_mode ---" |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 89 | $QEMU_IMG resize -f "$IMGFMT" --shrink --preallocation=$growth_mode "$TEST_IMG" -${GROWTH_SIZE}K |
Max Reitz | a2c7e08 | 2017-06-13 22:21:06 +0200 | [diff] [blame] | 90 | done |
| 91 | |
Max Reitz | 733d1dc | 2018-02-28 14:13:15 +0100 | [diff] [blame] | 92 | echo |
| 93 | echo '=== Testing image growth on 2G empty image ===' |
| 94 | |
| 95 | for growth_mode in falloc full; do |
| 96 | echo |
| 97 | echo "--- growth_mode=$growth_mode ---" |
| 98 | |
| 99 | # Maybe we want to do an lseek() to the end of the file before the |
| 100 | # preallocation; if the file has a length of 2 GB, that would |
| 101 | # return an integer that overflows to negative when put into a |
| 102 | # plain int. We should use the correct type for the result, and |
| 103 | # this tests we do. |
| 104 | |
Kevin Wolf | ffa244c | 2020-07-07 16:23:29 +0200 | [diff] [blame] | 105 | _make_test_img -o "extent_size_hint=0" 2G |
Max Reitz | 733d1dc | 2018-02-28 14:13:15 +0100 | [diff] [blame] | 106 | $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" +${GROWTH_SIZE}K |
| 107 | |
Hanna Reitz | 74163ad | 2022-06-20 18:27:02 +0200 | [diff] [blame] | 108 | actual_size=$($QEMU_IMG info -f "$IMGFMT" "$TEST_IMG" | grep 'disk size' | head -n 1) |
Max Reitz | 733d1dc | 2018-02-28 14:13:15 +0100 | [diff] [blame] | 109 | actual_size=$(echo "$actual_size" | sed -e 's/^[^0-9]*\([0-9]\+\).*$/\1/') |
| 110 | |
| 111 | if [ $actual_size -lt $GROWTH_SIZE ]; then |
| 112 | echo "ERROR: Image should have at least ${GROWTH_SIZE}K, but has ${actual_size}K" |
| 113 | fi |
| 114 | done |
| 115 | |
Max Reitz | a2c7e08 | 2017-06-13 22:21:06 +0200 | [diff] [blame] | 116 | # success, all done |
| 117 | echo '*** done' |
| 118 | rm -f $seq.full |
| 119 | status=0 |