3 # Test qcow2 and file image creation
5 # Copyright (C) 2018 Red Hat, Inc.
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', job_id='job0', options=options)
31 if 'return' in result:
32 assert result['return'] == {}
36 with iotests.FilePath('t.qcow2') as disk_path, \
37 iotests.FilePath('t.qcow2.base') as backing_path, \
40 vm.add_object('secret,id=keysec0,data=foo')
43 # Successful image creation (defaults)
45 iotests.log("=== Successful image creation (defaults) ===")
48 size = 128 * 1024 * 1024
51 blockdev_create(vm, { 'driver': 'file',
52 'filename': disk_path,
55 vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
58 blockdev_create(vm, { 'driver': imgfmt,
63 iotests.img_info_log(disk_path)
66 # Successful image creation (inline blockdev-add, explicit defaults)
68 iotests.log("=== Successful image creation (inline blockdev-add, explicit defaults) ===")
71 # Choose a different size to show that we got a new image
72 size = 64 * 1024 * 1024
75 blockdev_create(vm, { 'driver': 'file',
76 'filename': disk_path,
78 'preallocation': 'off',
81 blockdev_create(vm, { 'driver': imgfmt,
84 'filename': disk_path,
88 'cluster-size': 65536,
89 'preallocation': 'off',
90 'lazy-refcounts': False,
91 'refcount-bits': 16 })
94 iotests.img_info_log(disk_path)
97 # Successful image creation (v3 non-default options)
99 iotests.log("=== Successful image creation (v3 non-default options) ===")
102 # Choose a different size to show that we got a new image
103 size = 32 * 1024 * 1024
106 blockdev_create(vm, { 'driver': 'file',
107 'filename': disk_path,
109 'preallocation': 'falloc',
112 blockdev_create(vm, { 'driver': imgfmt,
115 'filename': disk_path,
119 'cluster-size': 2097152,
120 'preallocation': 'metadata',
121 'lazy-refcounts': True,
122 'refcount-bits': 1 })
125 iotests.img_info_log(disk_path)
128 # Successful image creation (v2 non-default options)
130 iotests.log("=== Successful image creation (v2 non-default options) ===")
134 blockdev_create(vm, { 'driver': 'file',
135 'filename': disk_path,
138 blockdev_create(vm, { 'driver': imgfmt,
141 'filename': disk_path,
144 'backing-file': backing_path,
145 'backing-fmt': 'qcow2',
147 'cluster-size': 512 })
150 iotests.img_info_log(disk_path)
153 # Successful image creation (encrypted)
155 iotests.log("=== Successful image creation (encrypted) ===")
159 blockdev_create(vm, { 'driver': imgfmt,
162 'filename': disk_path,
167 'key-secret': 'keysec0',
168 'cipher-alg': 'twofish-128',
169 'cipher-mode': 'ctr',
170 'ivgen-alg': 'plain64',
171 'ivgen-hash-alg': 'md5',
177 iotests.img_info_log(disk_path)
180 # Invalid BlockdevRef
182 iotests.log("=== Invalid BlockdevRef ===")
186 blockdev_create(vm, { 'driver': imgfmt,
187 'file': "this doesn't exist",
194 iotests.log("=== Invalid sizes ===")
196 # TODO Negative image sizes aren't handled correctly, but this is a problem
197 # with QAPI's implementation of the 'size' type and affects other commands
198 # as well. Once this is fixed, we may want to add a test case here.
200 # 1. Misaligned image size
202 # 3. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
203 # 4. 2^63 - 512 (generally valid, but qcow2 can't handle images this size)
205 vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
208 for size in [ 1234, 18446744073709551104, 9223372036854775808,
209 9223372036854775296 ]:
210 blockdev_create(vm, { 'driver': imgfmt,
218 iotests.log("=== Invalid version ===")
221 blockdev_create(vm, { 'driver': imgfmt,
225 blockdev_create(vm, { 'driver': imgfmt,
229 'lazy-refcounts': True })
230 blockdev_create(vm, { 'driver': imgfmt,
234 'refcount-bits': 8 })
238 # Invalid backing file options
240 iotests.log("=== Invalid backing file options ===")
243 blockdev_create(vm, { 'driver': imgfmt,
246 'backing-file': '/dev/null',
247 'preallocation': 'full' })
248 blockdev_create(vm, { 'driver': imgfmt,
251 'backing-fmt': imgfmt })
255 # Invalid cluster size
257 iotests.log("=== Invalid cluster size ===")
260 for csize in [ 1234, 128, 4194304, 0 ]:
261 blockdev_create(vm, { 'driver': imgfmt,
264 'cluster-size': csize })
265 blockdev_create(vm, { 'driver': imgfmt,
267 'size': 281474976710656,
268 'cluster-size': 512 })
272 # Invalid refcount width
274 iotests.log("=== Invalid refcount width ===")
277 for refcount_bits in [ 128, 0, 7 ]:
278 blockdev_create(vm, { 'driver': imgfmt,
281 'refcount-bits': refcount_bits })