xref: /qemu/tests/qemu-iotests/211 (revision ec150c7e)
1#!/usr/bin/env python
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    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        error = vm.run_job('job0')
36        if error and 'Could not allocate bmap' in error:
37            iotests.notrun('Insufficient memory')
38    iotests.log("")
39
40with iotests.FilePath('t.vdi') as disk_path, \
41     iotests.VM() as vm:
42
43    #
44    # Successful image creation (defaults)
45    #
46    iotests.log("=== Successful image creation (defaults) ===")
47    iotests.log("")
48
49    size = 128 * 1024 * 1024
50
51    vm.launch()
52    blockdev_create(vm, { 'driver': 'file',
53                          'filename': disk_path,
54                          'size': 0 })
55
56    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
57               node_name='imgfile', filters=[iotests.filter_qmp_testfiles])
58
59    blockdev_create(vm, { 'driver': imgfmt,
60                          'file': 'imgfile',
61                          'size': size })
62    vm.shutdown()
63
64    iotests.img_info_log(disk_path)
65    iotests.log(iotests.qemu_img_pipe('map', '--output=json', disk_path))
66
67    #
68    # Successful image creation (explicit defaults)
69    #
70    iotests.log("=== Successful image creation (explicit defaults) ===")
71    iotests.log("")
72
73    size = 64 * 1024 * 1024
74
75    vm.launch()
76    blockdev_create(vm, { 'driver': 'file',
77                          'filename': disk_path,
78                          'size': 0 })
79    blockdev_create(vm, { 'driver': imgfmt,
80                          'file': {
81                              'driver': 'file',
82                              'filename': disk_path,
83                          },
84                          'size': size,
85                          'preallocation': 'off' })
86    vm.shutdown()
87
88    iotests.img_info_log(disk_path)
89    iotests.log(iotests.qemu_img_pipe('map', '--output=json', 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    size = 32 * 1024 * 1024
98
99    vm.launch()
100    blockdev_create(vm, { 'driver': 'file',
101                          'filename': disk_path,
102                          'size': 0 })
103    blockdev_create(vm, { 'driver': imgfmt,
104                          'file': {
105                              'driver': 'file',
106                              'filename': disk_path,
107                          },
108                          'size': size,
109                          'preallocation': 'metadata' })
110    vm.shutdown()
111
112    iotests.img_info_log(disk_path)
113    iotests.log(iotests.qemu_img_pipe('map', '--output=json', disk_path))
114
115    #
116    # Invalid BlockdevRef
117    #
118    iotests.log("=== Invalid BlockdevRef ===")
119    iotests.log("")
120
121    vm.launch()
122    blockdev_create(vm, { 'driver': imgfmt,
123                          'file': "this doesn't exist",
124                          'size': size })
125    vm.shutdown()
126
127    #
128    # Zero size
129    #
130    iotests.log("=== Zero size ===")
131    iotests.log("")
132
133    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
134    vm.launch()
135    blockdev_create(vm, { 'driver': imgfmt,
136                          'file': 'node0',
137                          'size': 0 })
138    vm.shutdown()
139
140    iotests.img_info_log(disk_path)
141
142    #
143    # Maximum size
144    #
145    iotests.log("=== Maximum size ===")
146    iotests.log("")
147
148    vm.launch()
149    blockdev_create(vm, { 'driver': imgfmt,
150                          'file': 'node0',
151                          'size': 562949819203584 })
152    vm.shutdown()
153
154    iotests.img_info_log(disk_path)
155
156    #
157    # Invalid sizes
158    #
159
160    # TODO Negative image sizes aren't handled correctly, but this is a problem
161    # with QAPI's implementation of the 'size' type and affects other commands
162    # as well. Once this is fixed, we may want to add a test case here.
163
164    # 1. 2^64 - 512
165    # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
166    # 3. 0x1fffff8000001 (one byte more than maximum image size for VDI)
167
168    iotests.log("=== Invalid sizes ===")
169    iotests.log("")
170
171    vm.launch()
172    for size in [ 18446744073709551104, 9223372036854775808, 562949819203585 ]:
173        blockdev_create(vm, { 'driver': imgfmt,
174                              'file': 'node0',
175                              'size': size })
176    vm.shutdown()
177