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