xref: /qemu/tests/qemu-iotests/178 (revision 5086c997)
1#!/usr/bin/env bash
2# group: img
3#
4# qemu-img measure sub-command tests
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
23owner=stefanha@redhat.com
24
25seq=`basename $0`
26echo "QA output created by $seq"
27
28status=1    # failure is the default!
29
30_cleanup()
31{
32    _cleanup_test_img
33    _rm_test_img "$TEST_IMG.converted"
34}
35trap "_cleanup; exit \$status" 0 1 2 3 15
36
37# get standard environment, filters and checks
38. ./common.rc
39. ./common.filter
40. ./common.pattern
41
42_supported_fmt raw qcow2
43_supported_proto file
44_supported_os Linux
45_require_working_luks
46
47echo "== Input validation =="
48echo
49
50_make_test_img 1G
51
52$QEMU_IMG measure # missing arguments
53$QEMU_IMG measure --size 2G "$TEST_IMG" # only one allowed
54$QEMU_IMG measure "$TEST_IMG" a # only one filename allowed
55$QEMU_IMG measure --object secret,id=sec0,data=MTIzNDU2,format=base64 # size or filename needed
56$QEMU_IMG measure --image-opts # missing filename
57$QEMU_IMG measure -f qcow2 # missing filename
58$QEMU_IMG measure -l snap1 # missing filename
59$QEMU_IMG measure -o , # invalid option list
60$QEMU_IMG measure -l snapshot.foo=bar # invalid snapshot option
61$QEMU_IMG measure --output foo # invalid output format
62$QEMU_IMG measure --size -1 # invalid image size
63$QEMU_IMG measure -O foo "$TEST_IMG" # unknown image file format
64
65make_test_img_with_fmt() {
66    # Shadow global variables within this function
67    local IMGFMT="$1"
68    _make_test_img --no-opts "$2"
69}
70
71qemu_io_with_fmt() {
72    # Shadow global variables within this function
73    local QEMU_IO_OPTIONS=$(echo "$QEMU_IO_OPTIONS" | sed "s/-f $IMGFMT/-f $1/")
74    shift
75    $QEMU_IO "$@"
76}
77
78# The proof is in the pudding: converted image size cannot be larger than the
79# required size.
80#
81# Note: if a change to the image format code causes the file size to change,
82# then this test fails!  This is good because it's a reminder to check that the
83# required size is still at least as big as the actual converted file size.
84convert_and_show_size() {
85    local fmt="$1"
86    shift
87    $QEMU_IMG convert -f "$fmt" -O "$IMGFMT" "$TEST_IMG" "$@" "$TEST_IMG.converted"
88    stat -c "converted image file size in bytes: %s" "$TEST_IMG.converted"
89}
90
91for ofmt in human json; do
92    echo
93    echo "== Size calculation for a new file ($ofmt) =="
94    echo
95
96    # Try a few interesting sizes
97    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 0
98    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 2G
99    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 64G
100    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 256G
101    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 1T
102    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 2P
103    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 7E
104
105    # Always test the raw input files but also IMGFMT
106    for fmt in $(echo -e "raw\n$IMGFMT\n" | sort -u); do
107        echo
108        echo "== Empty $fmt input image ($ofmt) =="
109        echo
110        make_test_img_with_fmt "$fmt" 0
111        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
112        echo
113        convert_and_show_size "$fmt"
114
115        echo
116        echo "== $fmt input image with data ($ofmt) =="
117        echo
118        make_test_img_with_fmt "$fmt" 1G
119        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
120        qemu_io_with_fmt "$fmt" -c "write 512 512" "$TEST_IMG" | _filter_qemu_io
121        qemu_io_with_fmt "$fmt" -c "write 64K 64K" "$TEST_IMG" | _filter_qemu_io
122        if [ "$fmt" = "qcow2" ]; then
123            $QEMU_IMG snapshot -c snapshot1 "$TEST_IMG"
124        fi
125        qemu_io_with_fmt "$fmt" -c "write 128M 63K" "$TEST_IMG" | _filter_qemu_io
126        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
127        echo
128        convert_and_show_size "$fmt"
129
130        if [ "$fmt" = "qcow2" ]; then
131            echo
132            echo "== $fmt input image with internal snapshot ($ofmt) =="
133            echo
134            $QEMU_IMG measure --output=$ofmt -f "$fmt" -l snapshot1 \
135                              -O "$IMGFMT" "$TEST_IMG"
136            echo
137            convert_and_show_size "$fmt" -l snapshot1
138        fi
139
140        if [ "$IMGFMT" = "qcow2" ]; then
141            echo
142            echo "== $fmt input image and a backing file ($ofmt) =="
143            echo
144            # The backing file doesn't need to exist :)
145            $QEMU_IMG measure --output=$ofmt -o backing_file=x \
146                              -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
147
148            echo
149            echo "== $fmt input image and LUKS encryption =="
150            echo
151            $QEMU_IMG measure --output=$ofmt \
152                              --object secret,id=sec0,data=base \
153                              -o encrypt.format=luks,encrypt.key-secret=sec0,encrypt.iter-time=10 \
154                              -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
155        fi
156
157        echo
158        echo "== $fmt input image and preallocation ($ofmt) =="
159        echo
160        $QEMU_IMG measure --output=$ofmt -o preallocation=full \
161                          -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
162        echo
163        convert_and_show_size "$fmt" -o preallocation=full
164
165        echo
166        echo "== Fully-allocated $fmt input image ($ofmt) =="
167        echo
168        make_test_img_with_fmt "$fmt" 8M
169        qemu_io_with_fmt "$fmt" -c "write 0 8M" "$TEST_IMG" | _filter_qemu_io
170        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
171        echo
172        convert_and_show_size "$fmt"
173    done
174done
175
176# success, all done
177echo "*** done"
178rm -f $seq.full
179status=0
180