4 # Test for when a backing file is considered overridden (thus, a
5 # json:{} filename is generated for the overlay) and when it is not
7 # Copyright (C) 2018 Red Hat, Inc.
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.
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.
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/>.
22 # Creator/Owner: Hanna Reitz <hreitz@redhat.com>
25 from iotests import log, qemu_img, filter_testfiles, filter_imgfmt, \
26 filter_qmp_testfiles, filter_qmp_imgfmt
28 # Need backing file and change-backing-file support
29 iotests.script_initialize(
30 supported_fmts=['qcow2', 'qed'],
31 supported_platforms=['linux'],
35 def log_node_info(node):
38 log('bs->filename: ' + node['image']['filename'],
39 filters=[filter_testfiles, filter_imgfmt])
40 log('bs->backing_file: ' + node['image']['full-backing-filename'],
41 filters=[filter_testfiles, filter_imgfmt])
43 if 'backing-image' in node['image']:
44 log('bs->backing->bs->filename: ' +
45 node['image']['backing-image']['filename'],
46 filters=[filter_testfiles, filter_imgfmt])
48 log('bs->backing: (none)')
53 with iotests.FilePath('base.img') as base_img_path, \
54 iotests.FilePath('top.img') as top_img_path, \
57 qemu_img('create', '-f', iotests.imgfmt, base_img_path, '64M')
58 # Choose a funny way to describe the backing filename
59 qemu_img('create', '-f', iotests.imgfmt, '-b',
60 'file:' + base_img_path, '-F', iotests.imgfmt,
65 log('--- Implicit backing file ---')
68 vm.qmp_log('blockdev-add',
70 driver=iotests.imgfmt,
73 'filename': top_img_path
75 filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
77 # Filename should be plain, and the backing node filename should
78 # not contain the "file:" prefix
79 log_node_info(vm.node_info('node0'))
81 vm.qmp_log('blockdev-del', node_name='node0')
84 log('--- change-backing-file ---')
87 vm.qmp_log('blockdev-add',
89 driver=iotests.imgfmt,
92 'filename': top_img_path
94 filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
96 # Changing the backing file to a qemu-reported filename should
97 # result in qemu accepting the corresponding BDS as the implicit
98 # backing BDS (and thus not generate a json:{} filename).
99 # So, first, query the backing filename.
102 vm.node_info('node0')['image']['backing-image']['filename']
104 # Next, change the backing file to something different
106 vm.qmp_log('change-backing-file',
107 image_node_name='node0',
109 backing_file='null-co://',
110 filters=[filter_qmp_testfiles])
112 # Now, verify that we get a json:{} filename
113 # (Image header says "null-co://", actual backing file still is
116 log_node_info(vm.node_info('node0'))
119 # (To get header and backing file in sync)
121 vm.qmp_log('change-backing-file',
122 image_node_name='node0',
124 backing_file=backing_filename,
125 filters=[filter_qmp_testfiles])
127 # And verify that we get our original results
129 log_node_info(vm.node_info('node0'))
131 # Finally, try a "file:" prefix. While this is actually what we
132 # originally had in the image header, qemu will not reopen the
133 # backing file here, so it cannot verify that this filename
134 # "resolves" to the actual backing BDS's filename and will thus
135 # consider both to be different.
136 # (This may be fixed in the future.)
138 vm.qmp_log('change-backing-file',
139 image_node_name='node0',
141 backing_file=('file:' + backing_filename),
142 filters=[filter_qmp_testfiles])
144 # So now we should get a json:{} filename
146 log_node_info(vm.node_info('node0'))
148 # Remove and re-attach so we can see that (as in our first try),
149 # opening the image anew helps qemu resolve the header backing
152 vm.qmp_log('blockdev-del', node_name='node0')
154 vm.qmp_log('blockdev-add',
156 driver=iotests.imgfmt,
159 'filename': top_img_path
161 filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
163 log_node_info(vm.node_info('node0'))
165 vm.qmp_log('blockdev-del', node_name='node0')
168 log('--- Override backing file ---')
171 # For this test, we need the plain filename in the image header
172 # (because qemu cannot "canonicalize"/"resolve" the backing
173 # filename unless the backing file is opened implicitly with the
175 qemu_img('create', '-f', iotests.imgfmt, '-b', base_img_path,
176 '-F', iotests.imgfmt, top_img_path)
178 # You can only reliably override backing options by using a node
179 # reference (or by specifying file.filename, but, well...)
180 vm.qmp_log('blockdev-add', node_name='null', driver='null-co')
182 vm.qmp_log('blockdev-add',
184 driver=iotests.imgfmt,
187 'filename': top_img_path
190 filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
192 # Should get a json:{} filename (and bs->backing_file is
193 # null-co://, because that field actually has not much to do
194 # with the header backing filename (except that it is changed by
195 # change-backing-file))
197 log_node_info(vm.node_info('node0'))
199 # Detach the backing file by reopening the whole thing
201 vm.qmp_log('blockdev-del', node_name='node0')
202 vm.qmp_log('blockdev-del', node_name='null')
204 vm.qmp_log('blockdev-add',
206 driver=iotests.imgfmt,
209 'filename': top_img_path
212 filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
214 # Should get a json:{} filename (because we overrode the backing
215 # file to not be there)
217 log_node_info(vm.node_info('node0'))
219 # Open the original backing file
221 vm.qmp_log('blockdev-add',
222 node_name='original-backing',
223 driver=iotests.imgfmt,
226 'filename': base_img_path
228 filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
230 # Attach the original backing file to its overlay
232 vm.qmp_log('blockdev-snapshot',
233 node='original-backing',
236 # This should give us the original plain result
238 log_node_info(vm.node_info('node0'))
240 vm.qmp_log('blockdev-del', node_name='node0')
241 vm.qmp_log('blockdev-del', node_name='original-backing')