xref: /openbmc/qemu/tests/qemu-iotests/237 (revision 4a960ece17c94989d4082f5d3d8c32b34eded57c)
11c4e7b64SKevin Wolf#!/usr/bin/env python
21c4e7b64SKevin Wolf#
31c4e7b64SKevin Wolf# Test vmdk and file image creation
41c4e7b64SKevin Wolf#
51c4e7b64SKevin Wolf# Copyright (C) 2018 Red Hat, Inc.
61c4e7b64SKevin Wolf#
71c4e7b64SKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
81c4e7b64SKevin Wolf#
91c4e7b64SKevin Wolf# This program is free software; you can redistribute it and/or modify
101c4e7b64SKevin Wolf# it under the terms of the GNU General Public License as published by
111c4e7b64SKevin Wolf# the Free Software Foundation; either version 2 of the License, or
121c4e7b64SKevin Wolf# (at your option) any later version.
131c4e7b64SKevin Wolf#
141c4e7b64SKevin Wolf# This program is distributed in the hope that it will be useful,
151c4e7b64SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of
161c4e7b64SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
171c4e7b64SKevin Wolf# GNU General Public License for more details.
181c4e7b64SKevin Wolf#
191c4e7b64SKevin Wolf# You should have received a copy of the GNU General Public License
201c4e7b64SKevin Wolf# along with this program.  If not, see <http://www.gnu.org/licenses/>.
211c4e7b64SKevin Wolf#
221c4e7b64SKevin Wolf
23*4a960eceSKevin Wolfimport math
241c4e7b64SKevin Wolfimport iotests
251c4e7b64SKevin Wolffrom iotests import imgfmt
261c4e7b64SKevin Wolf
271c4e7b64SKevin Wolfiotests.verify_image_format(supported_fmts=['vmdk'])
281c4e7b64SKevin Wolf
291c4e7b64SKevin Wolfdef blockdev_create(vm, options):
301c4e7b64SKevin Wolf    result = vm.qmp_log('blockdev-create', job_id='job0', options=options)
311c4e7b64SKevin Wolf
321c4e7b64SKevin Wolf    if 'return' in result:
331c4e7b64SKevin Wolf        assert result['return'] == {}
341c4e7b64SKevin Wolf        vm.run_job('job0')
351c4e7b64SKevin Wolf    iotests.log("")
361c4e7b64SKevin Wolf
371c4e7b64SKevin Wolfwith iotests.FilePath('t.vmdk') as disk_path, \
381c4e7b64SKevin Wolf     iotests.FilePath('t.vmdk.1') as extent1_path, \
391c4e7b64SKevin Wolf     iotests.FilePath('t.vmdk.2') as extent2_path, \
401c4e7b64SKevin Wolf     iotests.FilePath('t.vmdk.3') as extent3_path, \
411c4e7b64SKevin Wolf     iotests.VM() as vm:
421c4e7b64SKevin Wolf
431c4e7b64SKevin Wolf    #
441c4e7b64SKevin Wolf    # Successful image creation (defaults)
451c4e7b64SKevin Wolf    #
461c4e7b64SKevin Wolf    iotests.log("=== Successful image creation (defaults) ===")
471c4e7b64SKevin Wolf    iotests.log("")
481c4e7b64SKevin Wolf
491c4e7b64SKevin Wolf    size = 5 * 1024 * 1024 * 1024
501c4e7b64SKevin Wolf
511c4e7b64SKevin Wolf    vm.launch()
521c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': 'file',
531c4e7b64SKevin Wolf                          'filename': disk_path,
541c4e7b64SKevin Wolf                          'size': 0 })
551c4e7b64SKevin Wolf
561c4e7b64SKevin Wolf    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
571c4e7b64SKevin Wolf               node_name='imgfile')
581c4e7b64SKevin Wolf
591c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': imgfmt,
601c4e7b64SKevin Wolf                          'file': 'imgfile',
611c4e7b64SKevin Wolf                          'size': size })
621c4e7b64SKevin Wolf    vm.shutdown()
631c4e7b64SKevin Wolf
641c4e7b64SKevin Wolf    iotests.img_info_log(disk_path)
651c4e7b64SKevin Wolf
661c4e7b64SKevin Wolf    #
671c4e7b64SKevin Wolf    # Successful image creation (inline blockdev-add, explicit defaults)
681c4e7b64SKevin Wolf    #
691c4e7b64SKevin Wolf    iotests.log("=== Successful image creation (inline blockdev-add, explicit defaults) ===")
701c4e7b64SKevin Wolf    iotests.log("")
711c4e7b64SKevin Wolf
721c4e7b64SKevin Wolf    # Choose a different size to show that we got a new image
731c4e7b64SKevin Wolf    size = 64 * 1024 * 1024
741c4e7b64SKevin Wolf
751c4e7b64SKevin Wolf    vm.launch()
761c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': 'file',
771c4e7b64SKevin Wolf                          'filename': disk_path,
781c4e7b64SKevin Wolf                          'size': 0 })
791c4e7b64SKevin Wolf
801c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': imgfmt,
811c4e7b64SKevin Wolf                          'file': {
821c4e7b64SKevin Wolf                              'driver': 'file',
831c4e7b64SKevin Wolf                              'filename': disk_path,
841c4e7b64SKevin Wolf                          },
851c4e7b64SKevin Wolf                          'size': size,
861c4e7b64SKevin Wolf                          'extents': [],
871c4e7b64SKevin Wolf                          'subformat': 'monolithicSparse',
881c4e7b64SKevin Wolf                          'adapter-type': 'ide',
891c4e7b64SKevin Wolf                          'hwversion': '4',
901c4e7b64SKevin Wolf                          'zeroed-grain': False })
911c4e7b64SKevin Wolf    vm.shutdown()
921c4e7b64SKevin Wolf
931c4e7b64SKevin Wolf    iotests.img_info_log(disk_path)
941c4e7b64SKevin Wolf
951c4e7b64SKevin Wolf    #
961c4e7b64SKevin Wolf    # Successful image creation (non-default options)
971c4e7b64SKevin Wolf    #
981c4e7b64SKevin Wolf    iotests.log("=== Successful image creation (with non-default options) ===")
991c4e7b64SKevin Wolf    iotests.log("")
1001c4e7b64SKevin Wolf
1011c4e7b64SKevin Wolf    # Choose a different size to show that we got a new image
1021c4e7b64SKevin Wolf    size = 32 * 1024 * 1024
1031c4e7b64SKevin Wolf
1041c4e7b64SKevin Wolf    vm.launch()
1051c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': 'file',
1061c4e7b64SKevin Wolf                          'filename': disk_path,
1071c4e7b64SKevin Wolf                          'size': 0 })
1081c4e7b64SKevin Wolf
1091c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': imgfmt,
1101c4e7b64SKevin Wolf                          'file': {
1111c4e7b64SKevin Wolf                              'driver': 'file',
1121c4e7b64SKevin Wolf                              'filename': disk_path,
1131c4e7b64SKevin Wolf                          },
1141c4e7b64SKevin Wolf                          'size': size,
1151c4e7b64SKevin Wolf                          'extents': [],
1161c4e7b64SKevin Wolf                          'subformat': 'monolithicSparse',
1171c4e7b64SKevin Wolf                          'adapter-type': 'buslogic',
1181c4e7b64SKevin Wolf                          'zeroed-grain': True })
1191c4e7b64SKevin Wolf    vm.shutdown()
1201c4e7b64SKevin Wolf
1211c4e7b64SKevin Wolf    iotests.img_info_log(disk_path)
1221c4e7b64SKevin Wolf
1231c4e7b64SKevin Wolf    #
1241c4e7b64SKevin Wolf    # Invalid BlockdevRef
1251c4e7b64SKevin Wolf    #
1261c4e7b64SKevin Wolf    iotests.log("=== Invalid BlockdevRef ===")
1271c4e7b64SKevin Wolf    iotests.log("")
1281c4e7b64SKevin Wolf
1291c4e7b64SKevin Wolf    vm.launch()
1301c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': imgfmt,
1311c4e7b64SKevin Wolf                          'file': "this doesn't exist",
1321c4e7b64SKevin Wolf                          'size': size })
1331c4e7b64SKevin Wolf    vm.shutdown()
1341c4e7b64SKevin Wolf
1351c4e7b64SKevin Wolf    #
1361c4e7b64SKevin Wolf    # Adapter types
1371c4e7b64SKevin Wolf    #
1381c4e7b64SKevin Wolf
1391c4e7b64SKevin Wolf    iotests.log("=== Adapter types ===")
1401c4e7b64SKevin Wolf    iotests.log("")
1411c4e7b64SKevin Wolf
1421c4e7b64SKevin Wolf    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
1431c4e7b64SKevin Wolf
1441c4e7b64SKevin Wolf    # Valid
1451c4e7b64SKevin Wolf    iotests.log("== Valid adapter types ==")
1461c4e7b64SKevin Wolf    iotests.log("")
1471c4e7b64SKevin Wolf
1481c4e7b64SKevin Wolf    vm.launch()
1491c4e7b64SKevin Wolf    for adapter_type in [ 'ide', 'buslogic', 'lsilogic', 'legacyESX' ]:
1501c4e7b64SKevin Wolf        blockdev_create(vm, { 'driver': imgfmt,
1511c4e7b64SKevin Wolf                              'file': 'node0',
1521c4e7b64SKevin Wolf                              'size': size,
1531c4e7b64SKevin Wolf                              'adapter-type': adapter_type })
1541c4e7b64SKevin Wolf    vm.shutdown()
1551c4e7b64SKevin Wolf
1561c4e7b64SKevin Wolf    # Invalid
1571c4e7b64SKevin Wolf    iotests.log("== Invalid adapter types ==")
1581c4e7b64SKevin Wolf    iotests.log("")
1591c4e7b64SKevin Wolf
1601c4e7b64SKevin Wolf    vm.launch()
1611c4e7b64SKevin Wolf    for adapter_type in [ 'foo', 'IDE', 'legacyesx', 1 ]:
1621c4e7b64SKevin Wolf        blockdev_create(vm, { 'driver': imgfmt,
1631c4e7b64SKevin Wolf                              'file': 'node0',
1641c4e7b64SKevin Wolf                              'size': size,
1651c4e7b64SKevin Wolf                              'adapter-type': adapter_type })
1661c4e7b64SKevin Wolf    vm.shutdown()
1671c4e7b64SKevin Wolf
1681c4e7b64SKevin Wolf    #
1691c4e7b64SKevin Wolf    # Other subformats
1701c4e7b64SKevin Wolf    #
1711c4e7b64SKevin Wolf    iotests.log("=== Other subformats ===")
1721c4e7b64SKevin Wolf    iotests.log("")
1731c4e7b64SKevin Wolf
1741c4e7b64SKevin Wolf    for path in [ extent1_path, extent2_path, extent3_path ]:
1751c4e7b64SKevin Wolf        msg = iotests.qemu_img_pipe('create', '-f', imgfmt, path, '0')
1761c4e7b64SKevin Wolf        iotests.log(msg, [iotests.filter_testfiles])
1771c4e7b64SKevin Wolf
1781c4e7b64SKevin Wolf    vm.add_blockdev('driver=file,filename=%s,node-name=ext1' % (extent1_path))
1791c4e7b64SKevin Wolf    vm.add_blockdev('driver=file,filename=%s,node-name=ext2' % (extent2_path))
1801c4e7b64SKevin Wolf    vm.add_blockdev('driver=file,filename=%s,node-name=ext3' % (extent3_path))
1811c4e7b64SKevin Wolf
1821c4e7b64SKevin Wolf    # Missing extent
1831c4e7b64SKevin Wolf    iotests.log("== Missing extent ==")
1841c4e7b64SKevin Wolf    iotests.log("")
1851c4e7b64SKevin Wolf
1861c4e7b64SKevin Wolf    vm.launch()
1871c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': imgfmt,
1881c4e7b64SKevin Wolf                          'file': 'node0',
1891c4e7b64SKevin Wolf                          'size': size,
1901c4e7b64SKevin Wolf                          'subformat': 'monolithicFlat' })
1911c4e7b64SKevin Wolf    vm.shutdown()
1921c4e7b64SKevin Wolf
1931c4e7b64SKevin Wolf    # Correct extent
1941c4e7b64SKevin Wolf    iotests.log("== Correct extent ==")
1951c4e7b64SKevin Wolf    iotests.log("")
1961c4e7b64SKevin Wolf
1971c4e7b64SKevin Wolf    vm.launch()
1981c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': imgfmt,
1991c4e7b64SKevin Wolf                          'file': 'node0',
2001c4e7b64SKevin Wolf                          'size': size,
2011c4e7b64SKevin Wolf                          'subformat': 'monolithicFlat',
2021c4e7b64SKevin Wolf                          'extents': ['ext1'] })
2031c4e7b64SKevin Wolf    vm.shutdown()
2041c4e7b64SKevin Wolf
2051c4e7b64SKevin Wolf    # Extra extent
2061c4e7b64SKevin Wolf    iotests.log("== Extra extent ==")
2071c4e7b64SKevin Wolf    iotests.log("")
2081c4e7b64SKevin Wolf
2091c4e7b64SKevin Wolf    vm.launch()
2101c4e7b64SKevin Wolf    blockdev_create(vm, { 'driver': imgfmt,
2111c4e7b64SKevin Wolf                          'file': 'node0',
2121c4e7b64SKevin Wolf                          'size': 512,
2131c4e7b64SKevin Wolf                          'subformat': 'monolithicFlat',
2141c4e7b64SKevin Wolf                          'extents': ['ext1', 'ext2', 'ext3'] })
2151c4e7b64SKevin Wolf    vm.shutdown()
2161c4e7b64SKevin Wolf
2171c4e7b64SKevin Wolf    # Split formats
2181c4e7b64SKevin Wolf    iotests.log("== Split formats ==")
2191c4e7b64SKevin Wolf    iotests.log("")
2201c4e7b64SKevin Wolf
2211c4e7b64SKevin Wolf    for size in [ 512, 1073741824, 2147483648, 5368709120 ]:
2221c4e7b64SKevin Wolf        for subfmt in [ 'twoGbMaxExtentFlat', 'twoGbMaxExtentSparse' ]:
2231c4e7b64SKevin Wolf            iotests.log("= %s %d =" % (subfmt, size))
2241c4e7b64SKevin Wolf            iotests.log("")
2251c4e7b64SKevin Wolf
226*4a960eceSKevin Wolf            num_extents = math.ceil(size / 2.0**31)
227*4a960eceSKevin Wolf            extents = [ "ext%d" % (i) for i in range(1, num_extents + 1) ]
228*4a960eceSKevin Wolf
2291c4e7b64SKevin Wolf            vm.launch()
2301c4e7b64SKevin Wolf            blockdev_create(vm, { 'driver': imgfmt,
2311c4e7b64SKevin Wolf                                  'file': 'node0',
2321c4e7b64SKevin Wolf                                  'size': size,
2331c4e7b64SKevin Wolf                                  'subformat': subfmt,
234*4a960eceSKevin Wolf                                  'extents': extents })
2351c4e7b64SKevin Wolf            vm.shutdown()
2361c4e7b64SKevin Wolf
2371c4e7b64SKevin Wolf            iotests.img_info_log(disk_path)
238