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