3 # Test qcow2 and file image creation
5 # Copyright (C) 2018 Red Hat, Inc.
7 # Creator/Owner: Kevin Wolf <kwolf@redhat.com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 from iotests
import imgfmt
26 iotests
.verify_image_format(supported_fmts
=['qcow2'])
28 def blockdev_create(vm
, options
):
29 result
= vm
.qmp_log('blockdev-create',
30 filters
=[iotests
.filter_qmp_testfiles
],
31 job_id
='job0', options
=options
)
33 if 'return' in result
:
34 assert result
['return'] == {}
38 with iotests
.FilePath('t.qcow2') as disk_path
, \
39 iotests
.FilePath('t.qcow2.base') as backing_path
, \
42 vm
.add_object('secret,id=keysec0,data=foo')
45 # Successful image creation (defaults)
47 iotests
.log("=== Successful image creation (defaults) ===")
50 size
= 128 * 1024 * 1024
53 blockdev_create(vm
, { 'driver': 'file',
54 'filename': disk_path
,
57 vm
.qmp_log('blockdev-add',
58 filters
=[iotests
.filter_qmp_testfiles
],
59 driver
='file', filename
=disk_path
,
62 blockdev_create(vm
, { 'driver': imgfmt
,
67 iotests
.img_info_log(disk_path
)
70 # Successful image creation (inline blockdev-add, explicit defaults)
72 iotests
.log("=== Successful image creation (inline blockdev-add, explicit defaults) ===")
75 # Choose a different size to show that we got a new image
76 size
= 64 * 1024 * 1024
79 blockdev_create(vm
, { 'driver': 'file',
80 'filename': disk_path
,
82 'preallocation': 'off',
85 blockdev_create(vm
, { 'driver': imgfmt
,
88 'filename': disk_path
,
92 'cluster-size': 65536,
93 'preallocation': 'off',
94 'lazy-refcounts': False,
95 'refcount-bits': 16 })
98 iotests
.img_info_log(disk_path
)
101 # Successful image creation (v3 non-default options)
103 iotests
.log("=== Successful image creation (v3 non-default options) ===")
106 # Choose a different size to show that we got a new image
107 size
= 32 * 1024 * 1024
110 blockdev_create(vm
, { 'driver': 'file',
111 'filename': disk_path
,
113 'preallocation': 'falloc',
116 blockdev_create(vm
, { 'driver': imgfmt
,
119 'filename': disk_path
,
123 'cluster-size': 2097152,
124 'preallocation': 'metadata',
125 'lazy-refcounts': True,
126 'refcount-bits': 1 })
129 iotests
.img_info_log(disk_path
)
132 # Successful image creation (v2 non-default options)
134 iotests
.log("=== Successful image creation (v2 non-default options) ===")
138 blockdev_create(vm
, { 'driver': 'file',
139 'filename': disk_path
,
142 blockdev_create(vm
, { 'driver': imgfmt
,
145 'filename': disk_path
,
148 'backing-file': backing_path
,
149 'backing-fmt': 'qcow2',
151 'cluster-size': 512 })
154 iotests
.img_info_log(disk_path
)
157 # Successful image creation (encrypted)
159 iotests
.log("=== Successful image creation (encrypted) ===")
163 blockdev_create(vm
, { 'driver': imgfmt
,
166 'filename': disk_path
,
171 'key-secret': 'keysec0',
172 'cipher-alg': 'twofish-128',
173 'cipher-mode': 'ctr',
174 'ivgen-alg': 'plain64',
175 'ivgen-hash-alg': 'md5',
181 iotests
.img_info_log(disk_path
)
184 # Invalid BlockdevRef
186 iotests
.log("=== Invalid BlockdevRef ===")
190 blockdev_create(vm
, { 'driver': imgfmt
,
191 'file': "this doesn't exist",
198 iotests
.log("=== Invalid sizes ===")
200 # TODO Negative image sizes aren't handled correctly, but this is a problem
201 # with QAPI's implementation of the 'size' type and affects other commands
202 # as well. Once this is fixed, we may want to add a test case here.
204 # 1. Misaligned image size
206 # 3. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
207 # 4. 2^63 - 512 (generally valid, but qcow2 can't handle images this size)
209 vm
.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path
))
212 for size
in [ 1234, 18446744073709551104, 9223372036854775808,
213 9223372036854775296 ]:
214 blockdev_create(vm
, { 'driver': imgfmt
,
222 iotests
.log("=== Invalid version ===")
225 blockdev_create(vm
, { 'driver': imgfmt
,
229 blockdev_create(vm
, { 'driver': imgfmt
,
233 'lazy-refcounts': True })
234 blockdev_create(vm
, { 'driver': imgfmt
,
238 'refcount-bits': 8 })
242 # Invalid backing file options
244 iotests
.log("=== Invalid backing file options ===")
247 blockdev_create(vm
, { 'driver': imgfmt
,
250 'backing-file': '/dev/null',
251 'preallocation': 'full' })
252 blockdev_create(vm
, { 'driver': imgfmt
,
255 'backing-fmt': imgfmt
})
259 # Invalid cluster size
261 iotests
.log("=== Invalid cluster size ===")
264 for csize
in [ 1234, 128, 4194304, 0 ]:
265 blockdev_create(vm
, { 'driver': imgfmt
,
268 'cluster-size': csize
})
269 blockdev_create(vm
, { 'driver': imgfmt
,
271 'size': 281474976710656,
272 'cluster-size': 512 })
276 # Invalid refcount width
278 iotests
.log("=== Invalid refcount width ===")
281 for refcount_bits
in [ 128, 0, 7 ]:
282 blockdev_create(vm
, { 'driver': imgfmt
,
285 'refcount-bits': refcount_bits
})