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