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