1*abbab72cSKevin 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# 7*abbab72cSKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 8*abbab72cSKevin 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 23*abbab72cSKevin Wolfimport iotests 24*abbab72cSKevin Wolffrom iotests import imgfmt 25b7de0777SKevin Wolf 26*abbab72cSKevin Wolfiotests.verify_image_format(supported_fmts=['vdi']) 27*abbab72cSKevin Wolfiotests.verify_protocol(supported=['file']) 28b7de0777SKevin Wolf 29*abbab72cSKevin Wolfdef blockdev_create(vm, options): 30*abbab72cSKevin Wolf result = vm.qmp_log('x-blockdev-create', job_id='job0', options=options) 31b7de0777SKevin Wolf 32*abbab72cSKevin Wolf if 'return' in result: 33*abbab72cSKevin Wolf assert result['return'] == {} 34*abbab72cSKevin Wolf vm.run_job('job0') 35*abbab72cSKevin Wolf iotests.log("") 36b7de0777SKevin Wolf 37*abbab72cSKevin Wolfwith iotests.FilePath('t.vdi') as disk_path, \ 38*abbab72cSKevin Wolf iotests.VM() as vm: 39b7de0777SKevin Wolf 40*abbab72cSKevin Wolf # 41*abbab72cSKevin Wolf # Successful image creation (defaults) 42*abbab72cSKevin Wolf # 43*abbab72cSKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 44*abbab72cSKevin Wolf iotests.log("") 45b7de0777SKevin Wolf 46*abbab72cSKevin Wolf size = 128 * 1024 * 1024 47b7de0777SKevin Wolf 48*abbab72cSKevin Wolf vm.launch() 49*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': 'file', 50*abbab72cSKevin Wolf 'filename': disk_path, 51*abbab72cSKevin Wolf 'size': 0 }) 52b7de0777SKevin Wolf 53*abbab72cSKevin Wolf vm.qmp_log('blockdev-add', driver='file', filename=disk_path, 54*abbab72cSKevin Wolf node_name='imgfile') 55b7de0777SKevin Wolf 56*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 57*abbab72cSKevin Wolf 'file': 'imgfile', 58*abbab72cSKevin Wolf 'size': size }) 59*abbab72cSKevin Wolf vm.shutdown() 60b7de0777SKevin Wolf 61*abbab72cSKevin Wolf iotests.img_info_log(disk_path) 62*abbab72cSKevin Wolf iotests.log(iotests.qemu_img_pipe('map', '--output=json', disk_path)) 63b7de0777SKevin Wolf 64*abbab72cSKevin Wolf # 65*abbab72cSKevin Wolf # Successful image creation (explicit defaults) 66*abbab72cSKevin Wolf # 67*abbab72cSKevin Wolf iotests.log("=== Successful image creation (explicit defaults) ===") 68*abbab72cSKevin Wolf iotests.log("") 69b7de0777SKevin Wolf 70*abbab72cSKevin Wolf size = 64 * 1024 * 1024 71b7de0777SKevin Wolf 72*abbab72cSKevin Wolf vm.launch() 73*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': 'file', 74*abbab72cSKevin Wolf 'filename': disk_path, 75*abbab72cSKevin Wolf 'size': 0 }) 76*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 77*abbab72cSKevin Wolf 'file': { 78*abbab72cSKevin Wolf 'driver': 'file', 79*abbab72cSKevin Wolf 'filename': disk_path, 80b7de0777SKevin Wolf }, 81*abbab72cSKevin Wolf 'size': size, 82*abbab72cSKevin Wolf 'preallocation': 'off' }) 83*abbab72cSKevin Wolf vm.shutdown() 84b7de0777SKevin Wolf 85*abbab72cSKevin Wolf iotests.img_info_log(disk_path) 86*abbab72cSKevin Wolf iotests.log(iotests.qemu_img_pipe('map', '--output=json', disk_path)) 87b7de0777SKevin Wolf 88*abbab72cSKevin Wolf # 89*abbab72cSKevin Wolf # Successful image creation (with non-default options) 90*abbab72cSKevin Wolf # 91*abbab72cSKevin Wolf iotests.log("=== Successful image creation (with non-default options) ===") 92*abbab72cSKevin Wolf iotests.log("") 93b7de0777SKevin Wolf 94*abbab72cSKevin Wolf size = 32 * 1024 * 1024 95b7de0777SKevin Wolf 96*abbab72cSKevin Wolf vm.launch() 97*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': 'file', 98*abbab72cSKevin Wolf 'filename': disk_path, 99*abbab72cSKevin Wolf 'size': 0 }) 100*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 101*abbab72cSKevin Wolf 'file': { 102*abbab72cSKevin Wolf 'driver': 'file', 103*abbab72cSKevin Wolf 'filename': disk_path, 104b7de0777SKevin Wolf }, 105*abbab72cSKevin Wolf 'size': size, 106*abbab72cSKevin Wolf 'preallocation': 'metadata' }) 107*abbab72cSKevin Wolf vm.shutdown() 108b7de0777SKevin Wolf 109*abbab72cSKevin Wolf iotests.img_info_log(disk_path) 110*abbab72cSKevin Wolf iotests.log(iotests.qemu_img_pipe('map', '--output=json', disk_path)) 111b7de0777SKevin Wolf 112*abbab72cSKevin Wolf # 113*abbab72cSKevin Wolf # Invalid BlockdevRef 114*abbab72cSKevin Wolf # 115*abbab72cSKevin Wolf iotests.log("=== Invalid BlockdevRef ===") 116*abbab72cSKevin Wolf iotests.log("") 117b7de0777SKevin Wolf 118*abbab72cSKevin Wolf vm.launch() 119*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 120*abbab72cSKevin Wolf 'file': "this doesn't exist", 121*abbab72cSKevin Wolf 'size': size }) 122*abbab72cSKevin Wolf vm.shutdown() 123b7de0777SKevin Wolf 124*abbab72cSKevin Wolf # 125*abbab72cSKevin Wolf # Zero size 126*abbab72cSKevin Wolf # 127*abbab72cSKevin Wolf iotests.log("=== Zero size ===") 128*abbab72cSKevin Wolf iotests.log("") 129b7de0777SKevin Wolf 130*abbab72cSKevin Wolf vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path)) 131*abbab72cSKevin Wolf vm.launch() 132*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 133*abbab72cSKevin Wolf 'file': 'node0', 134*abbab72cSKevin Wolf 'size': 0 }) 135*abbab72cSKevin Wolf vm.shutdown() 136b7de0777SKevin Wolf 137*abbab72cSKevin Wolf iotests.img_info_log(disk_path) 138b7de0777SKevin Wolf 139*abbab72cSKevin Wolf # 140*abbab72cSKevin Wolf # Maximum size 141*abbab72cSKevin Wolf # 142*abbab72cSKevin Wolf iotests.log("=== Maximum size ===") 143*abbab72cSKevin Wolf iotests.log("") 144b7de0777SKevin Wolf 145*abbab72cSKevin Wolf vm.launch() 146*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 147*abbab72cSKevin Wolf 'file': 'node0', 148*abbab72cSKevin Wolf 'size': 562949819203584 }) 149*abbab72cSKevin Wolf vm.shutdown() 150b7de0777SKevin Wolf 151*abbab72cSKevin Wolf iotests.img_info_log(disk_path) 152b7de0777SKevin Wolf 153*abbab72cSKevin Wolf # 154*abbab72cSKevin Wolf # Invalid sizes 155*abbab72cSKevin Wolf # 156b7de0777SKevin Wolf 157b7de0777SKevin Wolf # TODO Negative image sizes aren't handled correctly, but this is a problem 158*abbab72cSKevin Wolf # with QAPI's implementation of the 'size' type and affects other commands 159*abbab72cSKevin Wolf # as well. Once this is fixed, we may want to add a test case here. 160b7de0777SKevin Wolf 161b7de0777SKevin Wolf # 1. 2^64 - 512 162b7de0777SKevin Wolf # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this) 163b7de0777SKevin Wolf # 3. 0x1fffff8000001 (one byte more than maximum image size for VDI) 164b7de0777SKevin Wolf 165*abbab72cSKevin Wolf iotests.log("=== Invalid sizes ===") 166*abbab72cSKevin Wolf iotests.log("") 167b7de0777SKevin Wolf 168*abbab72cSKevin Wolf vm.launch() 169*abbab72cSKevin Wolf for size in [ 18446744073709551104, 9223372036854775808, 562949819203585 ]: 170*abbab72cSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 171*abbab72cSKevin Wolf 'file': 'node0', 172*abbab72cSKevin Wolf 'size': size }) 173*abbab72cSKevin Wolf vm.shutdown() 174