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