1#!/usr/bin/env python3
2#
3# Test VDI 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=['vdi'])
27iotests.verify_protocol(supported=['file'])
28
29def blockdev_create(vm, options):
30    error = vm.blockdev_create(options)
31    if error and 'Could not allocate bmap' in error:
32        iotests.notrun('Insufficient memory')
33
34with iotests.FilePath('t.vdi') as disk_path, \
35     iotests.VM() as vm:
36
37    #
38    # Successful image creation (defaults)
39    #
40    iotests.log("=== Successful image creation (defaults) ===")
41    iotests.log("")
42
43    size = 128 * 1024 * 1024
44
45    vm.launch()
46    blockdev_create(vm, { 'driver': 'file',
47                          'filename': disk_path,
48                          'size': 0 })
49
50    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
51               node_name='imgfile', filters=[iotests.filter_qmp_testfiles])
52
53    blockdev_create(vm, { 'driver': imgfmt,
54                          'file': 'imgfile',
55                          'size': size })
56    vm.shutdown()
57
58    iotests.img_info_log(disk_path)
59    iotests.log(iotests.qemu_img_pipe('map', '--output=json', disk_path))
60
61    #
62    # Successful image creation (explicit defaults)
63    #
64    iotests.log("=== Successful image creation (explicit defaults) ===")
65    iotests.log("")
66
67    size = 64 * 1024 * 1024
68
69    vm.launch()
70    blockdev_create(vm, { 'driver': 'file',
71                          'filename': disk_path,
72                          'size': 0 })
73    blockdev_create(vm, { 'driver': imgfmt,
74                          'file': {
75                              'driver': 'file',
76                              'filename': disk_path,
77                          },
78                          'size': size,
79                          'preallocation': 'off' })
80    vm.shutdown()
81
82    iotests.img_info_log(disk_path)
83    iotests.log(iotests.qemu_img_pipe('map', '--output=json', disk_path))
84
85    #
86    # Successful image creation (with non-default options)
87    #
88    iotests.log("=== Successful image creation (with non-default options) ===")
89    iotests.log("")
90
91    size = 32 * 1024 * 1024
92
93    vm.launch()
94    blockdev_create(vm, { 'driver': 'file',
95                          'filename': disk_path,
96                          'size': 0 })
97    blockdev_create(vm, { 'driver': imgfmt,
98                          'file': {
99                              'driver': 'file',
100                              'filename': disk_path,
101                          },
102                          'size': size,
103                          'preallocation': 'metadata' })
104    vm.shutdown()
105
106    iotests.img_info_log(disk_path)
107    iotests.log(iotests.qemu_img_pipe('map', '--output=json', disk_path))
108
109    #
110    # Invalid BlockdevRef
111    #
112    iotests.log("=== Invalid BlockdevRef ===")
113    iotests.log("")
114
115    vm.launch()
116    blockdev_create(vm, { '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    blockdev_create(vm, { '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    blockdev_create(vm, { 'driver': imgfmt,
144                          'file': 'node0',
145                          'size': 562949819203584 })
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. 0x1fffff8000001 (one byte more than maximum image size for VDI)
161
162    iotests.log("=== Invalid sizes ===")
163    iotests.log("")
164
165    vm.launch()
166    for size in [ 18446744073709551104, 9223372036854775808, 562949819203585 ]:
167        blockdev_create(vm, { 'driver': imgfmt,
168                              'file': 'node0',
169                              'size': size })
170    vm.shutdown()
171