1*5ba141dcSKevin Wolf#!/usr/bin/env python 2d06195e6SKevin Wolf# 3d06195e6SKevin Wolf# Test luks and file image creation 4d06195e6SKevin Wolf# 5d06195e6SKevin Wolf# Copyright (C) 2018 Red Hat, Inc. 6d06195e6SKevin Wolf# 7*5ba141dcSKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 8*5ba141dcSKevin Wolf# 9d06195e6SKevin Wolf# This program is free software; you can redistribute it and/or modify 10d06195e6SKevin Wolf# it under the terms of the GNU General Public License as published by 11d06195e6SKevin Wolf# the Free Software Foundation; either version 2 of the License, or 12d06195e6SKevin Wolf# (at your option) any later version. 13d06195e6SKevin Wolf# 14d06195e6SKevin Wolf# This program is distributed in the hope that it will be useful, 15d06195e6SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 16d06195e6SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17d06195e6SKevin Wolf# GNU General Public License for more details. 18d06195e6SKevin Wolf# 19d06195e6SKevin Wolf# You should have received a copy of the GNU General Public License 20d06195e6SKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 21d06195e6SKevin Wolf# 22d06195e6SKevin Wolf 23*5ba141dcSKevin Wolfimport iotests 24*5ba141dcSKevin Wolffrom iotests import imgfmt 25d06195e6SKevin Wolf 26*5ba141dcSKevin Wolfiotests.verify_image_format(supported_fmts=['luks']) 27*5ba141dcSKevin Wolfiotests.verify_protocol(supported=['file']) 28d06195e6SKevin Wolf 29*5ba141dcSKevin Wolfdef blockdev_create(vm, options): 30*5ba141dcSKevin Wolf result = vm.qmp_log('x-blockdev-create', job_id='job0', options=options) 31d06195e6SKevin Wolf 32*5ba141dcSKevin Wolf if 'return' in result: 33*5ba141dcSKevin Wolf assert result['return'] == {} 34*5ba141dcSKevin Wolf vm.run_job('job0') 35*5ba141dcSKevin Wolf iotests.log("") 36d06195e6SKevin Wolf 37*5ba141dcSKevin Wolfwith iotests.FilePath('t.luks') as disk_path, \ 38*5ba141dcSKevin Wolf iotests.VM() as vm: 39d06195e6SKevin Wolf 40*5ba141dcSKevin Wolf vm.add_object('secret,id=keysec0,data=foo') 41d06195e6SKevin Wolf 42*5ba141dcSKevin Wolf # 43*5ba141dcSKevin Wolf # Successful image creation (defaults) 44*5ba141dcSKevin Wolf # 45*5ba141dcSKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 46*5ba141dcSKevin Wolf iotests.log("") 47d06195e6SKevin Wolf 48*5ba141dcSKevin Wolf size = 128 * 1024 * 1024 49d06195e6SKevin Wolf 50*5ba141dcSKevin Wolf vm.launch() 51*5ba141dcSKevin Wolf blockdev_create(vm, { 'driver': 'file', 52*5ba141dcSKevin Wolf 'filename': disk_path, 53*5ba141dcSKevin Wolf 'size': 0 }) 54d06195e6SKevin Wolf 55*5ba141dcSKevin Wolf vm.qmp_log('blockdev-add', driver='file', filename=disk_path, 56*5ba141dcSKevin Wolf node_name='imgfile') 57d06195e6SKevin Wolf 58*5ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 59*5ba141dcSKevin Wolf 'file': 'imgfile', 60*5ba141dcSKevin Wolf 'key-secret': 'keysec0', 61*5ba141dcSKevin Wolf 'size': size, 62*5ba141dcSKevin Wolf 'iter-time': 10 }) 63*5ba141dcSKevin Wolf vm.shutdown() 64d06195e6SKevin Wolf 65*5ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 66*5ba141dcSKevin Wolf iotests.img_info_log( 67*5ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 68*5ba141dcSKevin Wolf filter_path=disk_path, 69*5ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 70*5ba141dcSKevin Wolf imgopts=True) 71d06195e6SKevin Wolf 72*5ba141dcSKevin Wolf # 73*5ba141dcSKevin Wolf # Successful image creation (with non-default options) 74*5ba141dcSKevin Wolf # 75*5ba141dcSKevin Wolf iotests.log("=== Successful image creation (with non-default options) ===") 76*5ba141dcSKevin Wolf iotests.log("") 77d06195e6SKevin Wolf 78*5ba141dcSKevin Wolf size = 64 * 1024 * 1024 79*5ba141dcSKevin Wolf 80*5ba141dcSKevin Wolf vm.launch() 81*5ba141dcSKevin Wolf blockdev_create(vm, { 'driver': 'file', 82*5ba141dcSKevin Wolf 'filename': disk_path, 83*5ba141dcSKevin Wolf 'size': 0 }) 84*5ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 85*5ba141dcSKevin Wolf 'file': { 86*5ba141dcSKevin Wolf 'driver': 'file', 87*5ba141dcSKevin Wolf 'filename': disk_path, 88d06195e6SKevin Wolf }, 89*5ba141dcSKevin Wolf 'size': size, 90*5ba141dcSKevin Wolf 'key-secret': 'keysec0', 91*5ba141dcSKevin Wolf 'cipher-alg': 'twofish-128', 92*5ba141dcSKevin Wolf 'cipher-mode': 'ctr', 93*5ba141dcSKevin Wolf 'ivgen-alg': 'plain64', 94*5ba141dcSKevin Wolf 'ivgen-hash-alg': 'md5', 95*5ba141dcSKevin Wolf 'hash-alg': 'sha1', 96*5ba141dcSKevin Wolf 'iter-time': 10 }) 97*5ba141dcSKevin Wolf vm.shutdown() 98d06195e6SKevin Wolf 99*5ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 100*5ba141dcSKevin Wolf iotests.img_info_log( 101*5ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 102*5ba141dcSKevin Wolf filter_path=disk_path, 103*5ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 104*5ba141dcSKevin Wolf imgopts=True) 105d06195e6SKevin Wolf 106*5ba141dcSKevin Wolf # 107*5ba141dcSKevin Wolf # Invalid BlockdevRef 108*5ba141dcSKevin Wolf # 109*5ba141dcSKevin Wolf iotests.log("=== Invalid BlockdevRef ===") 110*5ba141dcSKevin Wolf iotests.log("") 111d06195e6SKevin Wolf 112*5ba141dcSKevin Wolf size = 64 * 1024 * 1024 113d06195e6SKevin Wolf 114*5ba141dcSKevin Wolf vm.launch() 115*5ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 116*5ba141dcSKevin Wolf 'file': "this doesn't exist", 117*5ba141dcSKevin Wolf 'size': size }) 118*5ba141dcSKevin Wolf vm.shutdown() 119d06195e6SKevin Wolf 120*5ba141dcSKevin Wolf # 121*5ba141dcSKevin Wolf # Zero size 122*5ba141dcSKevin Wolf # 123*5ba141dcSKevin Wolf iotests.log("=== Zero size ===") 124*5ba141dcSKevin Wolf iotests.log("") 125d06195e6SKevin Wolf 126*5ba141dcSKevin Wolf vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path)) 127*5ba141dcSKevin Wolf vm.launch() 128*5ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 129*5ba141dcSKevin Wolf 'file': 'node0', 130*5ba141dcSKevin Wolf 'key-secret': 'keysec0', 131*5ba141dcSKevin Wolf 'size': 0, 132*5ba141dcSKevin Wolf 'iter-time': 10 }) 133*5ba141dcSKevin Wolf vm.shutdown() 134d06195e6SKevin Wolf 135*5ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 136*5ba141dcSKevin Wolf iotests.img_info_log( 137*5ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 138*5ba141dcSKevin Wolf filter_path=disk_path, 139*5ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 140*5ba141dcSKevin Wolf imgopts=True) 141d06195e6SKevin Wolf 142*5ba141dcSKevin Wolf # 143*5ba141dcSKevin Wolf # Invalid sizes 144*5ba141dcSKevin Wolf # 145d06195e6SKevin Wolf 146d06195e6SKevin Wolf # TODO Negative image sizes aren't handled correctly, but this is a problem 147d06195e6SKevin Wolf # with QAPI's implementation of the 'size' type and affects other commands as 148d06195e6SKevin Wolf # well. Once this is fixed, we may want to add a test case here. 149d06195e6SKevin Wolf 150d06195e6SKevin Wolf # 1. 2^64 - 512 151d06195e6SKevin Wolf # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this) 152d06195e6SKevin Wolf # 3. 2^63 - 512 (generally valid, but with the crypto header the file will 153d06195e6SKevin Wolf # exceed 63 bits) 154*5ba141dcSKevin Wolf iotests.log("=== Invalid sizes ===") 155*5ba141dcSKevin Wolf iotests.log("") 156d06195e6SKevin Wolf 157*5ba141dcSKevin Wolf vm.launch() 158*5ba141dcSKevin Wolf for size in [ 18446744073709551104, 9223372036854775808, 9223372036854775296 ]: 159*5ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 160*5ba141dcSKevin Wolf 'file': 'node0', 161*5ba141dcSKevin Wolf 'key-secret': 'keysec0', 162*5ba141dcSKevin Wolf 'size': size }) 163*5ba141dcSKevin Wolf vm.shutdown() 164d06195e6SKevin Wolf 165*5ba141dcSKevin Wolf # 166*5ba141dcSKevin Wolf # Resize image with invalid sizes 167*5ba141dcSKevin Wolf # 168*5ba141dcSKevin Wolf iotests.log("=== Resize image with invalid sizes ===") 169*5ba141dcSKevin Wolf iotests.log("") 17050880f25SKevin Wolf 171*5ba141dcSKevin Wolf vm.add_blockdev('driver=luks,file=node0,key-secret=keysec0,node-name=node1') 172*5ba141dcSKevin Wolf vm.launch() 173*5ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=9223372036854775296) 174*5ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=9223372036854775808) 175*5ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=18446744073709551104) 176*5ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=-9223372036854775808) 177*5ba141dcSKevin Wolf vm.shutdown() 17850880f25SKevin Wolf 179*5ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 180*5ba141dcSKevin Wolf iotests.img_info_log( 181*5ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 182*5ba141dcSKevin Wolf filter_path=disk_path, 183*5ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 184*5ba141dcSKevin Wolf imgopts=True) 185