xref: /qemu/tests/qemu-iotests/213 (revision 5b76dd13)
1#!/usr/bin/env python
2#
3# Test vhdx and file image creation
4#
5# Copyright (C) 2018 Red Hat, Inc.
6#
7# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
8#
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.
13#
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.
18#
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/>.
21#
22
23import iotests
24from iotests import imgfmt
25
26iotests.verify_image_format(supported_fmts=['vhdx'])
27iotests.verify_protocol(supported=['file'])
28
29def blockdev_create(vm, options):
30    result = vm.qmp_log('blockdev-create', job_id='job0', options=options)
31
32    if 'return' in result:
33        assert result['return'] == {}
34        vm.run_job('job0')
35    iotests.log("")
36
37with iotests.FilePath('t.vhdx') as disk_path, \
38     iotests.VM() as vm:
39
40    #
41    # Successful image creation (defaults)
42    #
43    iotests.log("=== Successful image creation (defaults) ===")
44    iotests.log("")
45
46    size = 128 * 1024 * 1024
47
48    vm.launch()
49    blockdev_create(vm, { 'driver': 'file',
50                          'filename': disk_path,
51                          'size': 0 })
52
53    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
54               node_name='imgfile')
55
56    blockdev_create(vm, { 'driver': imgfmt,
57                          'file': 'imgfile',
58                          'size': size })
59    vm.shutdown()
60
61    iotests.img_info_log(disk_path)
62
63    #
64    # Successful image creation (explicit defaults)
65    #
66    iotests.log("=== Successful image creation (explicit defaults) ===")
67    iotests.log("")
68
69    # Choose a different size to show that we got a new image
70    size = 64 * 1024 * 1024
71
72    vm.launch()
73    blockdev_create(vm, { 'driver': 'file',
74                          'filename': disk_path,
75                          'size': 0 })
76    blockdev_create(vm, { 'driver': imgfmt,
77                          'file': {
78                              'driver': 'file',
79                              'filename': disk_path,
80                          },
81                          'size': size,
82                          'log-size': 1048576,
83                          'block-size': 8388608,
84                          'subformat': 'dynamic',
85                          'block-state-zero': True })
86    vm.shutdown()
87
88    iotests.img_info_log(disk_path)
89
90    #
91    # Successful image creation (with non-default options)
92    #
93    iotests.log("=== Successful image creation (with non-default options) ===")
94    iotests.log("")
95
96    # Choose a different size to show that we got a new image
97    size = 32 * 1024 * 1024
98
99    vm.launch()
100    blockdev_create(vm, { 'driver': 'file',
101                          'filename': disk_path,
102                          'size': 0 })
103    blockdev_create(vm, { 'driver': imgfmt,
104                          'file': {
105                              'driver': 'file',
106                              'filename': disk_path,
107                          },
108                          'size': size,
109                          'log-size': 8388608,
110                          'block-size': 268435456,
111                          'subformat': 'fixed',
112                          'block-state-zero': False })
113    vm.shutdown()
114
115    iotests.img_info_log(disk_path)
116
117    #
118    # Invalid BlockdevRef
119    #
120    iotests.log("=== Invalid BlockdevRef ===")
121    iotests.log("")
122
123    vm.launch()
124    blockdev_create(vm, { 'driver': imgfmt,
125                          'file': "this doesn't exist",
126                          'size': size })
127    vm.shutdown()
128
129    #
130    # Zero size
131    #
132    iotests.log("=== Zero size ===")
133    iotests.log("")
134
135    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
136    vm.launch()
137    blockdev_create(vm, { 'driver': imgfmt,
138                          'file': 'node0',
139                          'size': 0 })
140    vm.shutdown()
141
142    iotests.img_info_log(disk_path)
143
144    #
145    # Maximum size
146    #
147    iotests.log("=== Maximum size ===")
148    iotests.log("")
149
150    vm.launch()
151    blockdev_create(vm, { 'driver': imgfmt,
152                          'file': 'node0',
153                          'size': 70368744177664 })
154    vm.shutdown()
155
156    iotests.img_info_log(disk_path)
157
158    #
159    # Invalid sizes
160    #
161
162    # TODO Negative image sizes aren't handled correctly, but this is a problem
163    # with QAPI's implementation of the 'size' type and affects other commands
164    # as well. Once this is fixed, we may want to add a test case here.
165
166    # 1. 2^64 - 512
167    # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
168    # 3. 2^63 - 512 (generally valid, but with the image header the file will
169    #                exceed 63 bits)
170    # 4. 2^46 + 1 (one byte more than maximum image size)
171
172    iotests.log("=== Invalid sizes ===")
173    iotests.log("")
174
175    vm.launch()
176    for size in [ 18446744073709551104, 9223372036854775808,
177                  9223372036854775296, 70368744177665 ]:
178        blockdev_create(vm, { 'driver': imgfmt,
179                              'file': 'node0',
180                              'size': size })
181    vm.shutdown()
182
183    #
184    # Invalid block size
185    #
186    iotests.log("=== Invalid block size ===")
187    iotests.log("")
188
189    vm.launch()
190    for bsize in [ 1234567, 128, 3145728, 536870912, 0 ]:
191        blockdev_create(vm, { 'driver': imgfmt,
192                              'file': 'node0',
193                              'size': 67108864,
194                              'block-size': bsize })
195    vm.shutdown()
196
197    #
198    # Invalid log size
199    #
200    iotests.log("=== Invalid log size ===")
201    iotests.log("")
202
203    vm.launch()
204    for lsize in [ 1234567, 128, 4294967296, 0 ]:
205        blockdev_create(vm, { 'driver': imgfmt,
206                              'file': 'node0',
207                              'size': 67108864,
208                              'log-size': lsize })
209    vm.shutdown()
210