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