15ba141dcSKevin 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# 75ba141dcSKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 85ba141dcSKevin 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 235ba141dcSKevin Wolfimport iotests 245ba141dcSKevin Wolffrom iotests import imgfmt 25d06195e6SKevin Wolf 265ba141dcSKevin Wolfiotests.verify_image_format(supported_fmts=['luks']) 275ba141dcSKevin Wolfiotests.verify_protocol(supported=['file']) 28d06195e6SKevin Wolf 295ba141dcSKevin Wolfdef blockdev_create(vm, options): 30*250c04f5SMax Reitz result = vm.qmp_log('blockdev-create', job_id='job0', options=options, 31*250c04f5SMax Reitz filters=[iotests.filter_qmp_testfiles]) 32d06195e6SKevin Wolf 335ba141dcSKevin Wolf if 'return' in result: 345ba141dcSKevin Wolf assert result['return'] == {} 355ba141dcSKevin Wolf vm.run_job('job0') 365ba141dcSKevin Wolf iotests.log("") 37d06195e6SKevin Wolf 385ba141dcSKevin Wolfwith iotests.FilePath('t.luks') as disk_path, \ 395ba141dcSKevin Wolf iotests.VM() as vm: 40d06195e6SKevin Wolf 415ba141dcSKevin Wolf vm.add_object('secret,id=keysec0,data=foo') 42d06195e6SKevin Wolf 435ba141dcSKevin Wolf # 445ba141dcSKevin Wolf # Successful image creation (defaults) 455ba141dcSKevin Wolf # 465ba141dcSKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 475ba141dcSKevin Wolf iotests.log("") 48d06195e6SKevin Wolf 495ba141dcSKevin Wolf size = 128 * 1024 * 1024 50d06195e6SKevin Wolf 515ba141dcSKevin Wolf vm.launch() 525ba141dcSKevin Wolf blockdev_create(vm, { 'driver': 'file', 535ba141dcSKevin Wolf 'filename': disk_path, 545ba141dcSKevin Wolf 'size': 0 }) 55d06195e6SKevin Wolf 565ba141dcSKevin Wolf vm.qmp_log('blockdev-add', driver='file', filename=disk_path, 57*250c04f5SMax Reitz node_name='imgfile', filters=[iotests.filter_qmp_testfiles]) 58d06195e6SKevin Wolf 595ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 605ba141dcSKevin Wolf 'file': 'imgfile', 615ba141dcSKevin Wolf 'key-secret': 'keysec0', 625ba141dcSKevin Wolf 'size': size, 635ba141dcSKevin Wolf 'iter-time': 10 }) 645ba141dcSKevin Wolf vm.shutdown() 65d06195e6SKevin Wolf 665ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 675ba141dcSKevin Wolf iotests.img_info_log( 685ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 695ba141dcSKevin Wolf filter_path=disk_path, 705ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 715ba141dcSKevin Wolf imgopts=True) 72d06195e6SKevin Wolf 735ba141dcSKevin Wolf # 745ba141dcSKevin Wolf # Successful image creation (with non-default options) 755ba141dcSKevin Wolf # 765ba141dcSKevin Wolf iotests.log("=== Successful image creation (with non-default options) ===") 775ba141dcSKevin Wolf iotests.log("") 78d06195e6SKevin Wolf 795ba141dcSKevin Wolf size = 64 * 1024 * 1024 805ba141dcSKevin Wolf 815ba141dcSKevin Wolf vm.launch() 825ba141dcSKevin Wolf blockdev_create(vm, { 'driver': 'file', 835ba141dcSKevin Wolf 'filename': disk_path, 845ba141dcSKevin Wolf 'size': 0 }) 855ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 865ba141dcSKevin Wolf 'file': { 875ba141dcSKevin Wolf 'driver': 'file', 885ba141dcSKevin Wolf 'filename': disk_path, 89d06195e6SKevin Wolf }, 905ba141dcSKevin Wolf 'size': size, 915ba141dcSKevin Wolf 'key-secret': 'keysec0', 925ba141dcSKevin Wolf 'cipher-alg': 'twofish-128', 935ba141dcSKevin Wolf 'cipher-mode': 'ctr', 945ba141dcSKevin Wolf 'ivgen-alg': 'plain64', 955ba141dcSKevin Wolf 'ivgen-hash-alg': 'md5', 965ba141dcSKevin Wolf 'hash-alg': 'sha1', 975ba141dcSKevin Wolf 'iter-time': 10 }) 985ba141dcSKevin Wolf vm.shutdown() 99d06195e6SKevin Wolf 1005ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 1015ba141dcSKevin Wolf iotests.img_info_log( 1025ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 1035ba141dcSKevin Wolf filter_path=disk_path, 1045ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 1055ba141dcSKevin Wolf imgopts=True) 106d06195e6SKevin Wolf 1075ba141dcSKevin Wolf # 1085ba141dcSKevin Wolf # Invalid BlockdevRef 1095ba141dcSKevin Wolf # 1105ba141dcSKevin Wolf iotests.log("=== Invalid BlockdevRef ===") 1115ba141dcSKevin Wolf iotests.log("") 112d06195e6SKevin Wolf 1135ba141dcSKevin Wolf size = 64 * 1024 * 1024 114d06195e6SKevin Wolf 1155ba141dcSKevin Wolf vm.launch() 1165ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 1175ba141dcSKevin Wolf 'file': "this doesn't exist", 1185ba141dcSKevin Wolf 'size': size }) 1195ba141dcSKevin Wolf vm.shutdown() 120d06195e6SKevin Wolf 1215ba141dcSKevin Wolf # 1225ba141dcSKevin Wolf # Zero size 1235ba141dcSKevin Wolf # 1245ba141dcSKevin Wolf iotests.log("=== Zero size ===") 1255ba141dcSKevin Wolf iotests.log("") 126d06195e6SKevin Wolf 1275ba141dcSKevin Wolf vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path)) 1285ba141dcSKevin Wolf vm.launch() 1295ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 1305ba141dcSKevin Wolf 'file': 'node0', 1315ba141dcSKevin Wolf 'key-secret': 'keysec0', 1325ba141dcSKevin Wolf 'size': 0, 1335ba141dcSKevin Wolf 'iter-time': 10 }) 1345ba141dcSKevin Wolf vm.shutdown() 135d06195e6SKevin Wolf 1365ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 1375ba141dcSKevin Wolf iotests.img_info_log( 1385ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 1395ba141dcSKevin Wolf filter_path=disk_path, 1405ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 1415ba141dcSKevin Wolf imgopts=True) 142d06195e6SKevin Wolf 1435ba141dcSKevin Wolf # 1445ba141dcSKevin Wolf # Invalid sizes 1455ba141dcSKevin Wolf # 146d06195e6SKevin Wolf 147d06195e6SKevin Wolf # TODO Negative image sizes aren't handled correctly, but this is a problem 148d06195e6SKevin Wolf # with QAPI's implementation of the 'size' type and affects other commands as 149d06195e6SKevin Wolf # well. Once this is fixed, we may want to add a test case here. 150d06195e6SKevin Wolf 151d06195e6SKevin Wolf # 1. 2^64 - 512 152d06195e6SKevin Wolf # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this) 153d06195e6SKevin Wolf # 3. 2^63 - 512 (generally valid, but with the crypto header the file will 154d06195e6SKevin Wolf # exceed 63 bits) 1555ba141dcSKevin Wolf iotests.log("=== Invalid sizes ===") 1565ba141dcSKevin Wolf iotests.log("") 157d06195e6SKevin Wolf 1585ba141dcSKevin Wolf vm.launch() 1595ba141dcSKevin Wolf for size in [ 18446744073709551104, 9223372036854775808, 9223372036854775296 ]: 1605ba141dcSKevin Wolf blockdev_create(vm, { 'driver': imgfmt, 1615ba141dcSKevin Wolf 'file': 'node0', 1625ba141dcSKevin Wolf 'key-secret': 'keysec0', 1635ba141dcSKevin Wolf 'size': size }) 1645ba141dcSKevin Wolf vm.shutdown() 165d06195e6SKevin Wolf 1665ba141dcSKevin Wolf # 1675ba141dcSKevin Wolf # Resize image with invalid sizes 1685ba141dcSKevin Wolf # 1695ba141dcSKevin Wolf iotests.log("=== Resize image with invalid sizes ===") 1705ba141dcSKevin Wolf iotests.log("") 17150880f25SKevin Wolf 1725ba141dcSKevin Wolf vm.add_blockdev('driver=luks,file=node0,key-secret=keysec0,node-name=node1') 1735ba141dcSKevin Wolf vm.launch() 1745ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=9223372036854775296) 1755ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=9223372036854775808) 1765ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=18446744073709551104) 1775ba141dcSKevin Wolf vm.qmp_log('block_resize', node_name='node1', size=-9223372036854775808) 1785ba141dcSKevin Wolf vm.shutdown() 17950880f25SKevin Wolf 1805ba141dcSKevin Wolf # TODO Proper support for images to be used with imgopts and/or protocols 1815ba141dcSKevin Wolf iotests.img_info_log( 1825ba141dcSKevin Wolf 'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path), 1835ba141dcSKevin Wolf filter_path=disk_path, 1845ba141dcSKevin Wolf extra_args=['--object', 'secret,id=keysec0,data=foo'], 1855ba141dcSKevin Wolf imgopts=True) 186