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 234a960eceSKevin 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): 30250c04f5SMax Reitz result = vm.qmp_log('blockdev-create', job_id='job0', options=options, 31250c04f5SMax Reitz filters=[iotests.filter_qmp_testfiles]) 321c4e7b64SKevin Wolf 331c4e7b64SKevin Wolf if 'return' in result: 341c4e7b64SKevin Wolf assert result['return'] == {} 351c4e7b64SKevin Wolf vm.run_job('job0') 361c4e7b64SKevin Wolf iotests.log("") 371c4e7b64SKevin Wolf 381c4e7b64SKevin Wolfwith iotests.FilePath('t.vmdk') as disk_path, \ 391c4e7b64SKevin Wolf iotests.FilePath('t.vmdk.1') as extent1_path, \ 401c4e7b64SKevin Wolf iotests.FilePath('t.vmdk.2') as extent2_path, \ 411c4e7b64SKevin Wolf iotests.FilePath('t.vmdk.3') as extent3_path, \ 421c4e7b64SKevin Wolf iotests.VM() as vm: 431c4e7b64SKevin Wolf 441c4e7b64SKevin Wolf # 451c4e7b64SKevin Wolf # Successful image creation (defaults) 461c4e7b64SKevin Wolf # 471c4e7b64SKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 481c4e7b64SKevin Wolf iotests.log("") 491c4e7b64SKevin Wolf 501c4e7b64SKevin Wolf size = 5 * 1024 * 1024 * 1024 511c4e7b64SKevin Wolf 521c4e7b64SKevin Wolf vm.launch() 531c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': 'file', 541c4e7b64SKevin Wolf 'filename': disk_path, 551c4e7b64SKevin Wolf 'size': 0 }) 561c4e7b64SKevin Wolf 571c4e7b64SKevin Wolf vm.qmp_log('blockdev-add', driver='file', filename=disk_path, 58250c04f5SMax Reitz node_name='imgfile', filters=[iotests.filter_qmp_testfiles]) 591c4e7b64SKevin Wolf 601c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 611c4e7b64SKevin Wolf 'file': 'imgfile', 621c4e7b64SKevin Wolf 'size': size }) 631c4e7b64SKevin Wolf vm.shutdown() 641c4e7b64SKevin Wolf 651c4e7b64SKevin Wolf iotests.img_info_log(disk_path) 661c4e7b64SKevin Wolf 671c4e7b64SKevin Wolf # 681c4e7b64SKevin Wolf # Successful image creation (inline blockdev-add, explicit defaults) 691c4e7b64SKevin Wolf # 701c4e7b64SKevin Wolf iotests.log("=== Successful image creation (inline blockdev-add, explicit defaults) ===") 711c4e7b64SKevin Wolf iotests.log("") 721c4e7b64SKevin Wolf 731c4e7b64SKevin Wolf # Choose a different size to show that we got a new image 741c4e7b64SKevin Wolf size = 64 * 1024 * 1024 751c4e7b64SKevin Wolf 761c4e7b64SKevin Wolf vm.launch() 771c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': 'file', 781c4e7b64SKevin Wolf 'filename': disk_path, 791c4e7b64SKevin Wolf 'size': 0 }) 801c4e7b64SKevin Wolf 811c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 821c4e7b64SKevin Wolf 'file': { 831c4e7b64SKevin Wolf 'driver': 'file', 841c4e7b64SKevin Wolf 'filename': disk_path, 851c4e7b64SKevin Wolf }, 861c4e7b64SKevin Wolf 'size': size, 871c4e7b64SKevin Wolf 'extents': [], 881c4e7b64SKevin Wolf 'subformat': 'monolithicSparse', 891c4e7b64SKevin Wolf 'adapter-type': 'ide', 901c4e7b64SKevin Wolf 'hwversion': '4', 911c4e7b64SKevin Wolf 'zeroed-grain': False }) 921c4e7b64SKevin Wolf vm.shutdown() 931c4e7b64SKevin Wolf 941c4e7b64SKevin Wolf iotests.img_info_log(disk_path) 951c4e7b64SKevin Wolf 961c4e7b64SKevin Wolf # 971c4e7b64SKevin Wolf # Successful image creation (non-default options) 981c4e7b64SKevin Wolf # 991c4e7b64SKevin Wolf iotests.log("=== Successful image creation (with non-default options) ===") 1001c4e7b64SKevin Wolf iotests.log("") 1011c4e7b64SKevin Wolf 1021c4e7b64SKevin Wolf # Choose a different size to show that we got a new image 1031c4e7b64SKevin Wolf size = 32 * 1024 * 1024 1041c4e7b64SKevin Wolf 1051c4e7b64SKevin Wolf vm.launch() 1061c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': 'file', 1071c4e7b64SKevin Wolf 'filename': disk_path, 1081c4e7b64SKevin Wolf 'size': 0 }) 1091c4e7b64SKevin Wolf 1101c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 1111c4e7b64SKevin Wolf 'file': { 1121c4e7b64SKevin Wolf 'driver': 'file', 1131c4e7b64SKevin Wolf 'filename': disk_path, 1141c4e7b64SKevin Wolf }, 1151c4e7b64SKevin Wolf 'size': size, 1161c4e7b64SKevin Wolf 'extents': [], 1171c4e7b64SKevin Wolf 'subformat': 'monolithicSparse', 1181c4e7b64SKevin Wolf 'adapter-type': 'buslogic', 1191c4e7b64SKevin Wolf 'zeroed-grain': True }) 1201c4e7b64SKevin Wolf vm.shutdown() 1211c4e7b64SKevin Wolf 1221c4e7b64SKevin Wolf iotests.img_info_log(disk_path) 1231c4e7b64SKevin Wolf 1241c4e7b64SKevin Wolf # 1251c4e7b64SKevin Wolf # Invalid BlockdevRef 1261c4e7b64SKevin Wolf # 1271c4e7b64SKevin Wolf iotests.log("=== Invalid BlockdevRef ===") 1281c4e7b64SKevin Wolf iotests.log("") 1291c4e7b64SKevin Wolf 1301c4e7b64SKevin Wolf vm.launch() 1311c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 1321c4e7b64SKevin Wolf 'file': "this doesn't exist", 1331c4e7b64SKevin Wolf 'size': size }) 1341c4e7b64SKevin Wolf vm.shutdown() 1351c4e7b64SKevin Wolf 1361c4e7b64SKevin Wolf # 1371c4e7b64SKevin Wolf # Adapter types 1381c4e7b64SKevin Wolf # 1391c4e7b64SKevin Wolf 1401c4e7b64SKevin Wolf iotests.log("=== Adapter types ===") 1411c4e7b64SKevin Wolf iotests.log("") 1421c4e7b64SKevin Wolf 1431c4e7b64SKevin Wolf vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path)) 1441c4e7b64SKevin Wolf 1451c4e7b64SKevin Wolf # Valid 1461c4e7b64SKevin Wolf iotests.log("== Valid adapter types ==") 1471c4e7b64SKevin Wolf iotests.log("") 1481c4e7b64SKevin Wolf 1491c4e7b64SKevin Wolf vm.launch() 1501c4e7b64SKevin Wolf for adapter_type in [ 'ide', 'buslogic', 'lsilogic', 'legacyESX' ]: 1511c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 1521c4e7b64SKevin Wolf 'file': 'node0', 1531c4e7b64SKevin Wolf 'size': size, 1541c4e7b64SKevin Wolf 'adapter-type': adapter_type }) 1551c4e7b64SKevin Wolf vm.shutdown() 1561c4e7b64SKevin Wolf 1571c4e7b64SKevin Wolf # Invalid 1581c4e7b64SKevin Wolf iotests.log("== Invalid adapter types ==") 1591c4e7b64SKevin Wolf iotests.log("") 1601c4e7b64SKevin Wolf 1611c4e7b64SKevin Wolf vm.launch() 1621c4e7b64SKevin Wolf for adapter_type in [ 'foo', 'IDE', 'legacyesx', 1 ]: 1631c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 1641c4e7b64SKevin Wolf 'file': 'node0', 1651c4e7b64SKevin Wolf 'size': size, 1661c4e7b64SKevin Wolf 'adapter-type': adapter_type }) 1671c4e7b64SKevin Wolf vm.shutdown() 1681c4e7b64SKevin Wolf 1691c4e7b64SKevin Wolf # 1701c4e7b64SKevin Wolf # Other subformats 1711c4e7b64SKevin Wolf # 1721c4e7b64SKevin Wolf iotests.log("=== Other subformats ===") 1731c4e7b64SKevin Wolf iotests.log("") 1741c4e7b64SKevin Wolf 1751c4e7b64SKevin Wolf for path in [ extent1_path, extent2_path, extent3_path ]: 1761c4e7b64SKevin Wolf msg = iotests.qemu_img_pipe('create', '-f', imgfmt, path, '0') 1771c4e7b64SKevin Wolf iotests.log(msg, [iotests.filter_testfiles]) 1781c4e7b64SKevin Wolf 1791c4e7b64SKevin Wolf vm.add_blockdev('driver=file,filename=%s,node-name=ext1' % (extent1_path)) 1801c4e7b64SKevin Wolf vm.add_blockdev('driver=file,filename=%s,node-name=ext2' % (extent2_path)) 1811c4e7b64SKevin Wolf vm.add_blockdev('driver=file,filename=%s,node-name=ext3' % (extent3_path)) 1821c4e7b64SKevin Wolf 1831c4e7b64SKevin Wolf # Missing extent 1841c4e7b64SKevin Wolf iotests.log("== Missing extent ==") 1851c4e7b64SKevin Wolf iotests.log("") 1861c4e7b64SKevin Wolf 1871c4e7b64SKevin Wolf vm.launch() 1881c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 1891c4e7b64SKevin Wolf 'file': 'node0', 1901c4e7b64SKevin Wolf 'size': size, 1911c4e7b64SKevin Wolf 'subformat': 'monolithicFlat' }) 1921c4e7b64SKevin Wolf vm.shutdown() 1931c4e7b64SKevin Wolf 1941c4e7b64SKevin Wolf # Correct extent 1951c4e7b64SKevin Wolf iotests.log("== Correct extent ==") 1961c4e7b64SKevin Wolf iotests.log("") 1971c4e7b64SKevin Wolf 1981c4e7b64SKevin Wolf vm.launch() 1991c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 2001c4e7b64SKevin Wolf 'file': 'node0', 2011c4e7b64SKevin Wolf 'size': size, 2021c4e7b64SKevin Wolf 'subformat': 'monolithicFlat', 2031c4e7b64SKevin Wolf 'extents': ['ext1'] }) 2041c4e7b64SKevin Wolf vm.shutdown() 2051c4e7b64SKevin Wolf 2061c4e7b64SKevin Wolf # Extra extent 2071c4e7b64SKevin Wolf iotests.log("== Extra extent ==") 2081c4e7b64SKevin Wolf iotests.log("") 2091c4e7b64SKevin Wolf 2101c4e7b64SKevin Wolf vm.launch() 2111c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 2121c4e7b64SKevin Wolf 'file': 'node0', 2131c4e7b64SKevin Wolf 'size': 512, 2141c4e7b64SKevin Wolf 'subformat': 'monolithicFlat', 2151c4e7b64SKevin Wolf 'extents': ['ext1', 'ext2', 'ext3'] }) 2161c4e7b64SKevin Wolf vm.shutdown() 2171c4e7b64SKevin Wolf 2181c4e7b64SKevin Wolf # Split formats 2191c4e7b64SKevin Wolf iotests.log("== Split formats ==") 2201c4e7b64SKevin Wolf iotests.log("") 2211c4e7b64SKevin Wolf 2221c4e7b64SKevin Wolf for size in [ 512, 1073741824, 2147483648, 5368709120 ]: 2231c4e7b64SKevin Wolf for subfmt in [ 'twoGbMaxExtentFlat', 'twoGbMaxExtentSparse' ]: 2241c4e7b64SKevin Wolf iotests.log("= %s %d =" % (subfmt, size)) 2251c4e7b64SKevin Wolf iotests.log("") 2261c4e7b64SKevin Wolf 227*10ba68d1SMax Reitz num_extents = int(math.ceil(size / 2.0**31)) 2284a960eceSKevin Wolf extents = [ "ext%d" % (i) for i in range(1, num_extents + 1) ] 2294a960eceSKevin Wolf 2301c4e7b64SKevin Wolf vm.launch() 2311c4e7b64SKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 2321c4e7b64SKevin Wolf 'file': 'node0', 2331c4e7b64SKevin Wolf 'size': size, 2341c4e7b64SKevin Wolf 'subformat': subfmt, 2354a960eceSKevin Wolf 'extents': extents }) 2361c4e7b64SKevin Wolf vm.shutdown() 2371c4e7b64SKevin Wolf 2381c4e7b64SKevin Wolf iotests.img_info_log(disk_path) 239