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