xref: /qemu/tests/qemu-iotests/210 (revision e7b3af81)
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
29def blockdev_create(vm, options):
30    result = vm.qmp_log('blockdev-create', job_id='job0', options=options)
31
32    if 'return' in result:
33        assert result['return'] == {}
34        vm.run_job('job0')
35    iotests.log("")
36
37with iotests.FilePath('t.luks') as disk_path, \
38     iotests.VM() as vm:
39
40    vm.add_object('secret,id=keysec0,data=foo')
41
42    #
43    # Successful image creation (defaults)
44    #
45    iotests.log("=== Successful image creation (defaults) ===")
46    iotests.log("")
47
48    size = 128 * 1024 * 1024
49
50    vm.launch()
51    blockdev_create(vm, { 'driver': 'file',
52                          'filename': disk_path,
53                          'size': 0 })
54
55    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
56               node_name='imgfile')
57
58    blockdev_create(vm, { 'driver': imgfmt,
59                          'file': 'imgfile',
60                          'key-secret': 'keysec0',
61                          'size': size,
62                          'iter-time': 10 })
63    vm.shutdown()
64
65    # TODO Proper support for images to be used with imgopts and/or protocols
66    iotests.img_info_log(
67        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
68        filter_path=disk_path,
69        extra_args=['--object', 'secret,id=keysec0,data=foo'],
70        imgopts=True)
71
72    #
73    # Successful image creation (with non-default options)
74    #
75    iotests.log("=== Successful image creation (with non-default options) ===")
76    iotests.log("")
77
78    size = 64 * 1024 * 1024
79
80    vm.launch()
81    blockdev_create(vm, { 'driver': 'file',
82                          'filename': disk_path,
83                          'size': 0 })
84    blockdev_create(vm, { 'driver': imgfmt,
85                          'file': {
86                              'driver': 'file',
87                              'filename': disk_path,
88                          },
89                          'size': size,
90                          'key-secret': 'keysec0',
91                          'cipher-alg': 'twofish-128',
92                          'cipher-mode': 'ctr',
93                          'ivgen-alg': 'plain64',
94                          'ivgen-hash-alg': 'md5',
95                          'hash-alg': 'sha1',
96                          'iter-time': 10 })
97    vm.shutdown()
98
99    # TODO Proper support for images to be used with imgopts and/or protocols
100    iotests.img_info_log(
101        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
102        filter_path=disk_path,
103        extra_args=['--object', 'secret,id=keysec0,data=foo'],
104        imgopts=True)
105
106    #
107    # Invalid BlockdevRef
108    #
109    iotests.log("=== Invalid BlockdevRef ===")
110    iotests.log("")
111
112    size = 64 * 1024 * 1024
113
114    vm.launch()
115    blockdev_create(vm, { 'driver': imgfmt,
116                          'file': "this doesn't exist",
117                          'size': size })
118    vm.shutdown()
119
120    #
121    # Zero size
122    #
123    iotests.log("=== Zero size ===")
124    iotests.log("")
125
126    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
127    vm.launch()
128    blockdev_create(vm, { 'driver': imgfmt,
129                          'file': 'node0',
130                          'key-secret': 'keysec0',
131                          'size': 0,
132                          'iter-time': 10 })
133    vm.shutdown()
134
135    # TODO Proper support for images to be used with imgopts and/or protocols
136    iotests.img_info_log(
137        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
138        filter_path=disk_path,
139        extra_args=['--object', 'secret,id=keysec0,data=foo'],
140        imgopts=True)
141
142    #
143    # Invalid sizes
144    #
145
146    # TODO Negative image sizes aren't handled correctly, but this is a problem
147    # with QAPI's implementation of the 'size' type and affects other commands as
148    # well. Once this is fixed, we may want to add a test case here.
149
150    # 1. 2^64 - 512
151    # 2. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
152    # 3. 2^63 - 512 (generally valid, but with the crypto header the file will
153    #                exceed 63 bits)
154    iotests.log("=== Invalid sizes ===")
155    iotests.log("")
156
157    vm.launch()
158    for size in [ 18446744073709551104, 9223372036854775808, 9223372036854775296 ]:
159        blockdev_create(vm, { 'driver': imgfmt,
160                              'file': 'node0',
161                              'key-secret': 'keysec0',
162                              'size': size })
163    vm.shutdown()
164
165    #
166    # Resize image with invalid sizes
167    #
168    iotests.log("=== Resize image with invalid sizes ===")
169    iotests.log("")
170
171    vm.add_blockdev('driver=luks,file=node0,key-secret=keysec0,node-name=node1')
172    vm.launch()
173    vm.qmp_log('block_resize', node_name='node1', size=9223372036854775296)
174    vm.qmp_log('block_resize', node_name='node1', size=9223372036854775808)
175    vm.qmp_log('block_resize', node_name='node1', size=18446744073709551104)
176    vm.qmp_log('block_resize', node_name='node1', size=-9223372036854775808)
177    vm.shutdown()
178
179    # TODO Proper support for images to be used with imgopts and/or protocols
180    iotests.img_info_log(
181        'driver=luks,file.driver=file,file.filename=%s,key-secret=keysec0' % (disk_path),
182        filter_path=disk_path,
183        extra_args=['--object', 'secret,id=keysec0,data=foo'],
184        imgopts=True)
185