xref: /openbmc/qemu/tests/qemu-iotests/210 (revision 7d8140595f1e131935ba1c98a55af7d066660707)
17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3
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
26*7d814059SJohn Snowiotests.script_initialize(
27*7d814059SJohn Snow    supported_fmts=['luks'],
28*7d814059SJohn Snow    supported_protocols=['file'],
29*7d814059SJohn Snow)
30d06195e6SKevin Wolf
315ba141dcSKevin Wolfwith iotests.FilePath('t.luks') as disk_path, \
325ba141dcSKevin Wolf     iotests.VM() as vm:
33d06195e6SKevin Wolf
345ba141dcSKevin Wolf    vm.add_object('secret,id=keysec0,data=foo')
35d06195e6SKevin Wolf
365ba141dcSKevin Wolf    #
375ba141dcSKevin Wolf    # Successful image creation (defaults)
385ba141dcSKevin Wolf    #
395ba141dcSKevin Wolf    iotests.log("=== Successful image creation (defaults) ===")
405ba141dcSKevin Wolf    iotests.log("")
41d06195e6SKevin Wolf
425ba141dcSKevin Wolf    size = 128 * 1024 * 1024
43d06195e6SKevin Wolf
445ba141dcSKevin Wolf    vm.launch()
45e55c2413SKevin Wolf    vm.blockdev_create({ 'driver': 'file',
465ba141dcSKevin Wolf                         'filename': disk_path,
475ba141dcSKevin Wolf                         'size': 0 })
48d06195e6SKevin Wolf
495ba141dcSKevin Wolf    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
50250c04f5SMax Reitz               node_name='imgfile', filters=[iotests.filter_qmp_testfiles])
51d06195e6SKevin Wolf
52e55c2413SKevin Wolf    vm.blockdev_create({ 'driver': imgfmt,
535ba141dcSKevin Wolf                         'file': 'imgfile',
545ba141dcSKevin Wolf                         'key-secret': 'keysec0',
555ba141dcSKevin Wolf                         'size': size,
565ba141dcSKevin Wolf                         'iter-time': 10 })
575ba141dcSKevin Wolf    vm.shutdown()
58d06195e6SKevin Wolf
595ba141dcSKevin Wolf    # TODO Proper support for images to be used with imgopts and/or protocols
605ba141dcSKevin Wolf    iotests.img_info_log(
615ba141dcSKevin Wolf        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
625ba141dcSKevin Wolf        filter_path=disk_path,
635ba141dcSKevin Wolf        extra_args=['--object', 'secret,id=keysec0,data=foo'],
645ba141dcSKevin Wolf        imgopts=True)
65d06195e6SKevin Wolf
665ba141dcSKevin Wolf    #
675ba141dcSKevin Wolf    # Successful image creation (with non-default options)
685ba141dcSKevin Wolf    #
695ba141dcSKevin Wolf    iotests.log("=== Successful image creation (with non-default options) ===")
705ba141dcSKevin Wolf    iotests.log("")
71d06195e6SKevin Wolf
725ba141dcSKevin Wolf    size = 64 * 1024 * 1024
735ba141dcSKevin Wolf
745ba141dcSKevin Wolf    vm.launch()
75e55c2413SKevin Wolf    vm.blockdev_create({ 'driver': 'file',
765ba141dcSKevin Wolf                         'filename': disk_path,
775ba141dcSKevin Wolf                         'size': 0 })
78e55c2413SKevin Wolf    vm.blockdev_create({ 'driver': imgfmt,
795ba141dcSKevin Wolf                         'file': {
805ba141dcSKevin Wolf                             'driver': 'file',
815ba141dcSKevin Wolf                             'filename': disk_path,
82d06195e6SKevin Wolf                         },
835ba141dcSKevin Wolf                         'size': size,
845ba141dcSKevin Wolf                         'key-secret': 'keysec0',
855ba141dcSKevin Wolf                         'cipher-alg': 'twofish-128',
865ba141dcSKevin Wolf                         'cipher-mode': 'ctr',
875ba141dcSKevin Wolf                         'ivgen-alg': 'plain64',
885ba141dcSKevin Wolf                         'ivgen-hash-alg': 'md5',
895ba141dcSKevin Wolf                         'hash-alg': 'sha1',
905ba141dcSKevin Wolf                         'iter-time': 10 })
915ba141dcSKevin Wolf    vm.shutdown()
92d06195e6SKevin Wolf
935ba141dcSKevin Wolf    # TODO Proper support for images to be used with imgopts and/or protocols
945ba141dcSKevin Wolf    iotests.img_info_log(
955ba141dcSKevin Wolf        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
965ba141dcSKevin Wolf        filter_path=disk_path,
975ba141dcSKevin Wolf        extra_args=['--object', 'secret,id=keysec0,data=foo'],
985ba141dcSKevin Wolf        imgopts=True)
99d06195e6SKevin Wolf
1005ba141dcSKevin Wolf    #
1015ba141dcSKevin Wolf    # Invalid BlockdevRef
1025ba141dcSKevin Wolf    #
1035ba141dcSKevin Wolf    iotests.log("=== Invalid BlockdevRef ===")
1045ba141dcSKevin Wolf    iotests.log("")
105d06195e6SKevin Wolf
1065ba141dcSKevin Wolf    size = 64 * 1024 * 1024
107d06195e6SKevin Wolf
1085ba141dcSKevin Wolf    vm.launch()
109e55c2413SKevin Wolf    vm.blockdev_create({ 'driver': imgfmt,
1105ba141dcSKevin Wolf                         'file': "this doesn't exist",
1115ba141dcSKevin Wolf                         'size': size })
1125ba141dcSKevin Wolf    vm.shutdown()
113d06195e6SKevin Wolf
1145ba141dcSKevin Wolf    #
1155ba141dcSKevin Wolf    # Zero size
1165ba141dcSKevin Wolf    #
1175ba141dcSKevin Wolf    iotests.log("=== Zero size ===")
1185ba141dcSKevin Wolf    iotests.log("")
119d06195e6SKevin Wolf
1205ba141dcSKevin Wolf    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
1215ba141dcSKevin Wolf    vm.launch()
122e55c2413SKevin Wolf    vm.blockdev_create({ 'driver': imgfmt,
1235ba141dcSKevin Wolf                         'file': 'node0',
1245ba141dcSKevin Wolf                         'key-secret': 'keysec0',
1255ba141dcSKevin Wolf                         'size': 0,
1265ba141dcSKevin Wolf                         'iter-time': 10 })
1275ba141dcSKevin Wolf    vm.shutdown()
128d06195e6SKevin Wolf
1295ba141dcSKevin Wolf    # TODO Proper support for images to be used with imgopts and/or protocols
1305ba141dcSKevin Wolf    iotests.img_info_log(
1315ba141dcSKevin Wolf        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
1325ba141dcSKevin Wolf        filter_path=disk_path,
1335ba141dcSKevin Wolf        extra_args=['--object', 'secret,id=keysec0,data=foo'],
1345ba141dcSKevin Wolf        imgopts=True)
135d06195e6SKevin Wolf
1365ba141dcSKevin Wolf    #
1375ba141dcSKevin Wolf    # Invalid sizes
1385ba141dcSKevin Wolf    #
139d06195e6SKevin Wolf
140d06195e6SKevin Wolf    # TODO Negative image sizes aren't handled correctly, but this is a problem
141d06195e6SKevin Wolf    # with QAPI's implementation of the 'size' type and affects other commands as
142d06195e6SKevin Wolf    # well. Once this is fixed, we may want to add a test case here.
143d06195e6SKevin Wolf
144d06195e6SKevin Wolf    # 1. 2^64 - 512
145d06195e6SKevin Wolf    # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
146d06195e6SKevin Wolf    # 3. 2^63 - 512 (generally valid, but with the crypto header the file will
147d06195e6SKevin Wolf    #                exceed 63 bits)
1485ba141dcSKevin Wolf    iotests.log("=== Invalid sizes ===")
1495ba141dcSKevin Wolf    iotests.log("")
150d06195e6SKevin Wolf
1515ba141dcSKevin Wolf    vm.launch()
1525ba141dcSKevin Wolf    for size in [ 18446744073709551104, 9223372036854775808, 9223372036854775296 ]:
153e55c2413SKevin Wolf        vm.blockdev_create({ 'driver': imgfmt,
1545ba141dcSKevin Wolf                             'file': 'node0',
1555ba141dcSKevin Wolf                             'key-secret': 'keysec0',
1565ba141dcSKevin Wolf                             'size': size })
1575ba141dcSKevin Wolf    vm.shutdown()
158d06195e6SKevin Wolf
1595ba141dcSKevin Wolf    #
1605ba141dcSKevin Wolf    # Resize image with invalid sizes
1615ba141dcSKevin Wolf    #
1625ba141dcSKevin Wolf    iotests.log("=== Resize image with invalid sizes ===")
1635ba141dcSKevin Wolf    iotests.log("")
16450880f25SKevin Wolf
1655ba141dcSKevin Wolf    vm.add_blockdev('driver=luks,file=node0,key-secret=keysec0,node-name=node1')
1665ba141dcSKevin Wolf    vm.launch()
1675ba141dcSKevin Wolf    vm.qmp_log('block_resize', node_name='node1', size=9223372036854775296)
1685ba141dcSKevin Wolf    vm.qmp_log('block_resize', node_name='node1', size=9223372036854775808)
1695ba141dcSKevin Wolf    vm.qmp_log('block_resize', node_name='node1', size=18446744073709551104)
1705ba141dcSKevin Wolf    vm.qmp_log('block_resize', node_name='node1', size=-9223372036854775808)
1715ba141dcSKevin Wolf    vm.shutdown()
17250880f25SKevin Wolf
1735ba141dcSKevin Wolf    # TODO Proper support for images to be used with imgopts and/or protocols
1745ba141dcSKevin Wolf    iotests.img_info_log(
1755ba141dcSKevin Wolf        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
1765ba141dcSKevin Wolf        filter_path=disk_path,
1775ba141dcSKevin Wolf        extra_args=['--object', 'secret,id=keysec0,data=foo'],
1785ba141dcSKevin Wolf        imgopts=True)
179