xref: /openbmc/qemu/tests/qemu-iotests/206 (revision d1fd31f8)
1#!/bin/bash
2#
3# Test qcow2 and file image creation
4#
5# Copyright (C) 2018 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=kwolf@redhat.com
23
24seq=`basename $0`
25echo "QA output created by $seq"
26
27here=`pwd`
28status=1	# failure is the default!
29
30# get standard environment, filters and checks
31. ./common.rc
32. ./common.filter
33
34_supported_fmt qcow2
35_supported_proto file
36_supported_os Linux
37
38function do_run_qemu()
39{
40    echo Testing: "$@"
41    $QEMU -nographic -qmp stdio -serial none "$@"
42    echo
43}
44
45function run_qemu()
46{
47    do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_qmp \
48                          | _filter_qemu | _filter_imgfmt \
49                          | _filter_actual_image_size
50}
51
52echo
53echo "=== Successful image creation (defaults) ==="
54echo
55
56size=$((128 * 1024 * 1024))
57
58run_qemu <<EOF
59{ "execute": "qmp_capabilities" }
60{ "execute": "x-blockdev-create",
61  "arguments": {
62      "driver": "file",
63      "filename": "$TEST_IMG",
64      "size": 0
65  }
66}
67{ "execute": "blockdev-add",
68  "arguments": {
69      "driver": "file",
70      "node-name": "imgfile",
71      "filename": "$TEST_IMG"
72  }
73}
74{ "execute": "x-blockdev-create",
75  "arguments": {
76      "driver": "$IMGFMT",
77      "file": "imgfile",
78      "size": $size
79  }
80}
81{ "execute": "quit" }
82EOF
83
84_img_info --format-specific
85
86echo
87echo "=== Successful image creation (inline blockdev-add, explicit defaults) ==="
88echo
89
90# Choose a different size to show that we got a new image
91size=$((64 * 1024 * 1024))
92
93run_qemu <<EOF
94{ "execute": "qmp_capabilities" }
95{ "execute": "x-blockdev-create",
96  "arguments": {
97      "driver": "file",
98      "filename": "$TEST_IMG",
99      "size": 0,
100      "preallocation": "off",
101      "nocow": false
102  }
103}
104{ "execute": "x-blockdev-create",
105  "arguments": {
106      "driver": "$IMGFMT",
107      "file": {
108          "driver": "file",
109          "filename": "$TEST_IMG"
110      },
111      "size": $size,
112      "version": "v3",
113      "cluster-size": 65536,
114      "preallocation": "off",
115      "lazy-refcounts": false,
116      "refcount-bits": 16
117  }
118}
119{ "execute": "quit" }
120EOF
121
122_img_info --format-specific
123
124echo
125echo "=== Successful image creation (v3 non-default options) ==="
126echo
127
128# Choose a different size to show that we got a new image
129size=$((32 * 1024 * 1024))
130
131run_qemu <<EOF
132{ "execute": "qmp_capabilities" }
133{ "execute": "x-blockdev-create",
134  "arguments": {
135      "driver": "file",
136      "filename": "$TEST_IMG",
137      "size": 0,
138      "preallocation": "falloc",
139      "nocow": true
140  }
141}
142{ "execute": "x-blockdev-create",
143  "arguments": {
144      "driver": "$IMGFMT",
145      "file": {
146          "driver": "file",
147          "filename": "$TEST_IMG"
148      },
149      "size": $size,
150      "version": "v3",
151      "cluster-size": 2097152,
152      "preallocation": "metadata",
153      "lazy-refcounts": true,
154      "refcount-bits": 1
155  }
156}
157{ "execute": "quit" }
158EOF
159
160_img_info --format-specific
161
162echo
163echo "=== Successful image creation (v2 non-default options) ==="
164echo
165
166mv $TEST_IMG $TEST_IMG.base
167
168run_qemu <<EOF
169{ "execute": "qmp_capabilities" }
170{ "execute": "x-blockdev-create",
171  "arguments": {
172      "driver": "file",
173      "filename": "$TEST_IMG",
174      "size": 0
175  }
176}
177{ "execute": "x-blockdev-create",
178  "arguments": {
179      "driver": "$IMGFMT",
180      "file": {
181          "driver": "file",
182          "filename": "$TEST_IMG"
183      },
184      "size": $size,
185      "backing-file": "$TEST_IMG.base",
186      "backing-fmt": "qcow2",
187      "version": "v2",
188      "cluster-size": 512
189  }
190}
191{ "execute": "quit" }
192EOF
193
194_img_info --format-specific
195
196echo
197echo "=== Successful image creation (encrypted) ==="
198echo
199
200run_qemu -object secret,id=keysec0,data="foo" <<EOF
201{ "execute": "qmp_capabilities" }
202{ "execute": "x-blockdev-create",
203  "arguments": {
204      "driver": "$IMGFMT",
205      "file": {
206          "driver": "file",
207          "filename": "$TEST_IMG"
208      },
209      "size": $size,
210      "encrypt": {
211          "format": "luks",
212          "key-secret": "keysec0",
213          "cipher-alg": "twofish-128",
214          "cipher-mode": "ctr",
215          "ivgen-alg": "plain64",
216          "ivgen-hash-alg": "md5",
217          "hash-alg": "sha1",
218          "iter-time": 10
219      }
220  }
221}
222{ "execute": "quit" }
223EOF
224
225_img_info --format-specific | _filter_img_info --format-specific
226
227echo
228echo "=== Invalid BlockdevRef ==="
229echo
230
231run_qemu <<EOF
232{ "execute": "qmp_capabilities" }
233{ "execute": "x-blockdev-create",
234  "arguments": {
235      "driver": "$IMGFMT",
236      "file": "this doesn't exist",
237      "size": $size
238  }
239}
240{ "execute": "quit" }
241EOF
242
243
244echo
245echo "=== Invalid sizes ==="
246echo
247
248# TODO Negative image sizes aren't handled correctly, but this is a problem
249# with QAPI's implementation of the 'size' type and affects other commands as
250# well. Once this is fixed, we may want to add a test case here.
251
252# 1. Misaligned image size
253# 2. 2^64 - 512
254# 3. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
255# 4. 2^63 - 512 (generally valid, but qcow2 can't handle images this size)
256
257run_qemu -blockdev driver=file,filename="$TEST_IMG",node-name=node0 <<EOF
258{ "execute": "qmp_capabilities" }
259{ "execute": "x-blockdev-create",
260  "arguments": {
261      "driver": "$IMGFMT",
262      "file": "node0",
263      "size": 1234
264  }
265}
266{ "execute": "x-blockdev-create",
267  "arguments": {
268      "driver": "$IMGFMT",
269      "file": "node0",
270      "size": 18446744073709551104
271  }
272}
273{ "execute": "x-blockdev-create",
274  "arguments": {
275      "driver": "$IMGFMT",
276      "file": "node0",
277      "size": 9223372036854775808
278  }
279}
280{ "execute": "x-blockdev-create",
281  "arguments": {
282      "driver": "$IMGFMT",
283      "file": "node0",
284      "size": 9223372036854775296
285  }
286}
287{ "execute": "quit" }
288EOF
289
290echo
291echo "=== Invalid version ==="
292echo
293
294run_qemu -blockdev driver=file,filename="$TEST_IMG",node-name=node0 <<EOF
295{ "execute": "qmp_capabilities" }
296{ "execute": "x-blockdev-create",
297  "arguments": {
298      "driver": "$IMGFMT",
299      "file": "node0",
300      "size": 67108864,
301      "version": "v1"
302  }
303}
304{ "execute": "x-blockdev-create",
305  "arguments": {
306      "driver": "$IMGFMT",
307      "file": "node0",
308      "size": 67108864,
309      "version": "v2",
310      "lazy-refcounts": true
311  }
312}
313{ "execute": "x-blockdev-create",
314  "arguments": {
315      "driver": "$IMGFMT",
316      "file": "node0",
317      "size": 67108864,
318      "version": "v2",
319      "refcount-bits": 8
320  }
321}
322{ "execute": "quit" }
323EOF
324
325echo
326echo "=== Invalid backing file options ==="
327echo
328
329run_qemu -blockdev driver=file,filename="$TEST_IMG",node-name=node0 <<EOF
330{ "execute": "qmp_capabilities" }
331{ "execute": "x-blockdev-create",
332  "arguments": {
333      "driver": "$IMGFMT",
334      "file": "node0",
335      "size": 67108864,
336      "backing-file": "/dev/null",
337      "preallocation": "full"
338  }
339}
340{ "execute": "x-blockdev-create",
341  "arguments": {
342      "driver": "$IMGFMT",
343      "file": "node0",
344      "size": 67108864,
345      "backing-fmt": "$IMGFMT"
346  }
347}
348{ "execute": "quit" }
349EOF
350
351echo
352echo "=== Invalid cluster size ==="
353echo
354
355run_qemu -blockdev driver=file,filename="$TEST_IMG",node-name=node0 <<EOF
356{ "execute": "qmp_capabilities" }
357{ "execute": "x-blockdev-create",
358  "arguments": {
359      "driver": "$IMGFMT",
360      "file": "node0",
361      "size": 67108864,
362      "cluster-size": 1234
363  }
364}
365{ "execute": "x-blockdev-create",
366  "arguments": {
367      "driver": "$IMGFMT",
368      "file": "node0",
369      "size": 67108864,
370      "cluster-size": 128
371  }
372}
373{ "execute": "x-blockdev-create",
374  "arguments": {
375      "driver": "$IMGFMT",
376      "file": "node0",
377      "size": 67108864,
378      "cluster-size": 4194304
379  }
380}
381{ "execute": "x-blockdev-create",
382  "arguments": {
383      "driver": "$IMGFMT",
384      "file": "node0",
385      "size": 67108864,
386      "cluster-size": 0
387  }
388}
389{ "execute": "x-blockdev-create",
390  "arguments": {
391      "driver": "$IMGFMT",
392      "file": "node0",
393      "size": 281474976710656,
394      "cluster-size": 512
395  }
396}
397{ "execute": "quit" }
398EOF
399
400echo
401echo "=== Invalid refcount width ==="
402echo
403
404run_qemu -blockdev driver=file,filename="$TEST_IMG",node-name=node0 <<EOF
405{ "execute": "qmp_capabilities" }
406{ "execute": "x-blockdev-create",
407  "arguments": {
408      "driver": "$IMGFMT",
409      "file": "node0",
410      "size": 67108864,
411      "refcount-bits": 128
412  }
413}
414{ "execute": "x-blockdev-create",
415  "arguments": {
416      "driver": "$IMGFMT",
417      "file": "node0",
418      "size": 67108864,
419      "refcount-bits": 0
420  }
421}
422{ "execute": "x-blockdev-create",
423  "arguments": {
424      "driver": "$IMGFMT",
425      "file": "node0",
426      "size": 67108864,
427      "refcount-bits": 7
428  }
429}
430{ "execute": "quit" }
431EOF
432
433# success, all done
434echo "*** done"
435rm -f $seq.full
436status=0
437