xref: /openbmc/qemu/tests/qemu-iotests/237 (revision a68694cd)
1#!/usr/bin/env python3
2#
3# Test vmdk 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 math
24import iotests
25from iotests import imgfmt
26
27iotests.script_initialize(supported_fmts=['vmdk'])
28
29with iotests.FilePath('t.vmdk') as disk_path, \
30     iotests.FilePath('t.vmdk.1') as extent1_path, \
31     iotests.FilePath('t.vmdk.2') as extent2_path, \
32     iotests.FilePath('t.vmdk.3') as extent3_path, \
33     iotests.VM() as vm:
34
35    #
36    # Successful image creation (defaults)
37    #
38    iotests.log("=== Successful image creation (defaults) ===")
39    iotests.log("")
40
41    size = 5 * 1024 * 1024 * 1024
42
43    vm.launch()
44    vm.blockdev_create({ 'driver': 'file',
45                         'filename': disk_path,
46                         'size': 0 })
47
48    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
49               node_name='imgfile', filters=[iotests.filter_qmp_testfiles])
50
51    vm.blockdev_create({ 'driver': imgfmt,
52                         'file': 'imgfile',
53                         'size': size })
54    vm.shutdown()
55
56    iotests.img_info_log(disk_path)
57
58    #
59    # Successful image creation (inline blockdev-add, explicit defaults)
60    #
61    iotests.log("=== Successful image creation (inline blockdev-add, explicit defaults) ===")
62    iotests.log("")
63
64    # Choose a different size to show that we got a new image
65    size = 64 * 1024 * 1024
66
67    vm.launch()
68    vm.blockdev_create({ 'driver': 'file',
69                         'filename': disk_path,
70                         'size': 0 })
71
72    vm.blockdev_create({ 'driver': imgfmt,
73                         'file': {
74                             'driver': 'file',
75                             'filename': disk_path,
76                         },
77                         'size': size,
78                         'extents': [],
79                         'subformat': 'monolithicSparse',
80                         'adapter-type': 'ide',
81                         'hwversion': '4',
82                         'zeroed-grain': False })
83    vm.shutdown()
84
85    iotests.img_info_log(disk_path)
86
87    #
88    # Successful image creation (non-default options)
89    #
90    iotests.log("=== Successful image creation (with non-default options) ===")
91    iotests.log("")
92
93    # Choose a different size to show that we got a new image
94    size = 32 * 1024 * 1024
95
96    vm.launch()
97    vm.blockdev_create({ 'driver': 'file',
98                         'filename': disk_path,
99                         'size': 0 })
100
101    vm.blockdev_create({ 'driver': imgfmt,
102                         'file': {
103                             'driver': 'file',
104                             'filename': disk_path,
105                         },
106                         'size': size,
107                         'extents': [],
108                         'subformat': 'monolithicSparse',
109                         'adapter-type': 'buslogic',
110                         'zeroed-grain': True })
111    vm.shutdown()
112
113    iotests.img_info_log(disk_path)
114
115    #
116    # Invalid BlockdevRef
117    #
118    iotests.log("=== Invalid BlockdevRef ===")
119    iotests.log("")
120
121    vm.launch()
122    vm.blockdev_create({ 'driver': imgfmt,
123                         'file': "this doesn't exist",
124                         'size': size })
125    vm.shutdown()
126
127    #
128    # Adapter types
129    #
130
131    iotests.log("=== Adapter types ===")
132    iotests.log("")
133
134    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
135
136    # Valid
137    iotests.log("== Valid adapter types ==")
138    iotests.log("")
139
140    vm.launch()
141    for adapter_type in [ 'ide', 'buslogic', 'lsilogic', 'legacyESX' ]:
142        vm.blockdev_create({ 'driver': imgfmt,
143                             'file': 'node0',
144                             'size': size,
145                             'adapter-type': adapter_type })
146    vm.shutdown()
147
148    # Invalid
149    iotests.log("== Invalid adapter types ==")
150    iotests.log("")
151
152    vm.launch()
153    for adapter_type in [ 'foo', 'IDE', 'legacyesx', 1 ]:
154        vm.blockdev_create({ 'driver': imgfmt,
155                             'file': 'node0',
156                             'size': size,
157                             'adapter-type': adapter_type })
158    vm.shutdown()
159
160    #
161    # Other subformats
162    #
163    iotests.log("=== Other subformats ===")
164    iotests.log("")
165
166    for path in [ extent1_path, extent2_path, extent3_path ]:
167        msg = iotests.qemu_img_pipe('create', '-f', imgfmt, path, '0')
168        iotests.log(msg, [iotests.filter_testfiles])
169
170    vm.add_blockdev('driver=file,filename=%s,node-name=ext1' % (extent1_path))
171    vm.add_blockdev('driver=file,filename=%s,node-name=ext2' % (extent2_path))
172    vm.add_blockdev('driver=file,filename=%s,node-name=ext3' % (extent3_path))
173
174    # Missing extent
175    iotests.log("== Missing extent ==")
176    iotests.log("")
177
178    vm.launch()
179    vm.blockdev_create({ 'driver': imgfmt,
180                         'file': 'node0',
181                         'size': size,
182                         'subformat': 'monolithicFlat' })
183    vm.shutdown()
184
185    # Correct extent
186    iotests.log("== Correct extent ==")
187    iotests.log("")
188
189    vm.launch()
190    vm.blockdev_create({ 'driver': imgfmt,
191                         'file': 'node0',
192                         'size': size,
193                         'subformat': 'monolithicFlat',
194                         'extents': ['ext1'] })
195    vm.shutdown()
196
197    # Extra extent
198    iotests.log("== Extra extent ==")
199    iotests.log("")
200
201    vm.launch()
202    vm.blockdev_create({ 'driver': imgfmt,
203                         'file': 'node0',
204                         'size': 512,
205                         'subformat': 'monolithicFlat',
206                         'extents': ['ext1', 'ext2', 'ext3'] })
207    vm.shutdown()
208
209    # Split formats
210    iotests.log("== Split formats ==")
211    iotests.log("")
212
213    for size in [ 512, 1073741824, 2147483648, 5368709120 ]:
214        for subfmt in [ 'twoGbMaxExtentFlat', 'twoGbMaxExtentSparse' ]:
215            iotests.log("= %s %d =" % (subfmt, size))
216            iotests.log("")
217
218            num_extents = int(math.ceil(size / 2.0**31))
219            extents = [ "ext%d" % (i) for i in range(1, num_extents + 1) ]
220
221            vm.launch()
222            vm.blockdev_create({ 'driver': imgfmt,
223                                 'file': 'node0',
224                                 'size': size,
225                                 'subformat': subfmt,
226                                 'extents': extents })
227            vm.shutdown()
228
229            iotests.img_info_log(disk_path)
230