xref: /openbmc/qemu/tests/qemu-iotests/211 (revision d2dfe0b5)
1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test VDI and file image creation
5#
6# Copyright (C) 2018 Red Hat, Inc.
7#
8# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22#
23
24import iotests
25from iotests import imgfmt
26
27iotests.script_initialize(
28    supported_fmts=['vdi'],
29    supported_protocols=['file'],
30)
31
32def blockdev_create(vm, options):
33    error = vm.blockdev_create(options)
34    if error and 'Could not allocate bmap' in error:
35        iotests.notrun('Insufficient memory')
36
37with iotests.FilePath('t.vdi') as disk_path, \
38     iotests.VM() as vm:
39
40    #
41    # Successful image creation (defaults)
42    #
43    iotests.log("=== Successful image creation (defaults) ===")
44    iotests.log("")
45
46    size = 128 * 1024 * 1024
47
48    vm.launch()
49    blockdev_create(vm, { 'driver': 'file',
50                          'filename': disk_path,
51                          'size': 0 })
52
53    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
54               node_name='imgfile', filters=[iotests.filter_qmp_testfiles])
55
56    blockdev_create(vm, { 'driver': imgfmt,
57                          'file': 'imgfile',
58                          'size': size })
59    vm.shutdown()
60
61    iotests.img_info_log(disk_path)
62    iotests.log(iotests.qemu_img_map(disk_path))
63
64    #
65    # Successful image creation (explicit defaults)
66    #
67    iotests.log("=== Successful image creation (explicit defaults) ===")
68    iotests.log("")
69
70    size = 64 * 1024 * 1024
71
72    vm.launch()
73    blockdev_create(vm, { 'driver': 'file',
74                          'filename': disk_path,
75                          'size': 0 })
76    blockdev_create(vm, { 'driver': imgfmt,
77                          'file': {
78                              'driver': 'file',
79                              'filename': disk_path,
80                          },
81                          'size': size,
82                          'preallocation': 'off' })
83    vm.shutdown()
84
85    iotests.img_info_log(disk_path)
86    iotests.log(iotests.qemu_img_map(disk_path))
87
88    #
89    # Successful image creation (with non-default options)
90    #
91    iotests.log("=== Successful image creation (with non-default options) ===")
92    iotests.log("")
93
94    size = 32 * 1024 * 1024
95
96    vm.launch()
97    blockdev_create(vm, { 'driver': 'file',
98                          'filename': disk_path,
99                          'size': 0 })
100    blockdev_create(vm, { 'driver': imgfmt,
101                          'file': {
102                              'driver': 'file',
103                              'filename': disk_path,
104                          },
105                          'size': size,
106                          'preallocation': 'metadata' })
107    vm.shutdown()
108
109    iotests.img_info_log(disk_path)
110    iotests.log(iotests.qemu_img_map(disk_path))
111
112    #
113    # Invalid BlockdevRef
114    #
115    iotests.log("=== Invalid BlockdevRef ===")
116    iotests.log("")
117
118    vm.launch()
119    blockdev_create(vm, { 'driver': imgfmt,
120                          'file': "this doesn't exist",
121                          'size': size })
122    vm.shutdown()
123
124    #
125    # Zero size
126    #
127    iotests.log("=== Zero size ===")
128    iotests.log("")
129
130    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
131    vm.launch()
132    blockdev_create(vm, { 'driver': imgfmt,
133                          'file': 'node0',
134                          'size': 0 })
135    vm.shutdown()
136
137    iotests.img_info_log(disk_path)
138
139    #
140    # Maximum size
141    #
142    iotests.log("=== Maximum size ===")
143    iotests.log("")
144
145    vm.launch()
146    blockdev_create(vm, { 'driver': imgfmt,
147                          'file': 'node0',
148                          'size': 562949819203584 })
149    vm.shutdown()
150
151    iotests.img_info_log(disk_path)
152
153    #
154    # Invalid sizes
155    #
156
157    # TODO Negative image sizes aren't handled correctly, but this is a problem
158    # with QAPI's implementation of the 'size' type and affects other commands
159    # as well. Once this is fixed, we may want to add a test case here.
160
161    # 1. 2^64 - 512
162    # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
163    # 3. 0x1fffff8000001 (one byte more than maximum image size for VDI)
164
165    iotests.log("=== Invalid sizes ===")
166    iotests.log("")
167
168    vm.launch()
169    for size in [ 18446744073709551104, 9223372036854775808, 562949819203585 ]:
170        blockdev_create(vm, { 'driver': imgfmt,
171                              'file': 'node0',
172                              'size': size })
173    vm.shutdown()
174