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