xref: /openbmc/qemu/tests/qemu-iotests/228 (revision 0b877d09)
17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3
20f62cd82SMax Reitz#
30f62cd82SMax Reitz# Test for when a backing file is considered overridden (thus, a
40f62cd82SMax Reitz# json:{} filename is generated for the overlay) and when it is not
50f62cd82SMax Reitz#
60f62cd82SMax Reitz# Copyright (C) 2018 Red Hat, Inc.
70f62cd82SMax Reitz#
80f62cd82SMax Reitz# This program is free software; you can redistribute it and/or modify
90f62cd82SMax Reitz# it under the terms of the GNU General Public License as published by
100f62cd82SMax Reitz# the Free Software Foundation; either version 2 of the License, or
110f62cd82SMax Reitz# (at your option) any later version.
120f62cd82SMax Reitz#
130f62cd82SMax Reitz# This program is distributed in the hope that it will be useful,
140f62cd82SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
150f62cd82SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
160f62cd82SMax Reitz# GNU General Public License for more details.
170f62cd82SMax Reitz#
180f62cd82SMax Reitz# You should have received a copy of the GNU General Public License
190f62cd82SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
200f62cd82SMax Reitz#
210f62cd82SMax Reitz# Creator/Owner: Max Reitz <mreitz@redhat.com>
220f62cd82SMax Reitz
230f62cd82SMax Reitzimport iotests
240f62cd82SMax Reitzfrom iotests import log, qemu_img, filter_testfiles, filter_imgfmt, \
250f62cd82SMax Reitz        filter_qmp_testfiles, filter_qmp_imgfmt
260f62cd82SMax Reitz
270f62cd82SMax Reitz# Need backing file and change-backing-file support
287d814059SJohn Snowiotests.script_initialize(
297d814059SJohn Snow    supported_fmts=['qcow2', 'qed'],
307d814059SJohn Snow    supported_platforms=['linux'],
317d814059SJohn Snow)
320f62cd82SMax Reitz
330f62cd82SMax Reitz
340f62cd82SMax Reitzdef log_node_info(node):
350f62cd82SMax Reitz    log('')
360f62cd82SMax Reitz
370f62cd82SMax Reitz    log('bs->filename: ' + node['image']['filename'],
380f62cd82SMax Reitz        filters=[filter_testfiles, filter_imgfmt])
39*0b877d09SMax Reitz    log('bs->backing_file: ' + node['image']['full-backing-filename'],
400f62cd82SMax Reitz        filters=[filter_testfiles, filter_imgfmt])
410f62cd82SMax Reitz
420f62cd82SMax Reitz    if 'backing-image' in node['image']:
430f62cd82SMax Reitz        log('bs->backing->bs->filename: ' +
440f62cd82SMax Reitz            node['image']['backing-image']['filename'],
450f62cd82SMax Reitz            filters=[filter_testfiles, filter_imgfmt])
460f62cd82SMax Reitz    else:
470f62cd82SMax Reitz        log('bs->backing: (none)')
480f62cd82SMax Reitz
490f62cd82SMax Reitz    log('')
500f62cd82SMax Reitz
510f62cd82SMax Reitz
520f62cd82SMax Reitzwith iotests.FilePath('base.img') as base_img_path, \
530f62cd82SMax Reitz     iotests.FilePath('top.img') as top_img_path, \
540f62cd82SMax Reitz     iotests.VM() as vm:
550f62cd82SMax Reitz
560f62cd82SMax Reitz    assert qemu_img('create', '-f', iotests.imgfmt, base_img_path, '64M') == 0
570f62cd82SMax Reitz    # Choose a funny way to describe the backing filename
580f62cd82SMax Reitz    assert qemu_img('create', '-f', iotests.imgfmt, '-b',
59b66ff2c2SEric Blake                    'file:' + base_img_path, '-F', iotests.imgfmt,
60b66ff2c2SEric Blake                    top_img_path) == 0
610f62cd82SMax Reitz
620f62cd82SMax Reitz    vm.launch()
630f62cd82SMax Reitz
640f62cd82SMax Reitz    log('--- Implicit backing file ---')
650f62cd82SMax Reitz    log('')
660f62cd82SMax Reitz
670f62cd82SMax Reitz    vm.qmp_log('blockdev-add',
680f62cd82SMax Reitz                node_name='node0',
690f62cd82SMax Reitz                driver=iotests.imgfmt,
700f62cd82SMax Reitz                file={
710f62cd82SMax Reitz                    'driver': 'file',
720f62cd82SMax Reitz                    'filename': top_img_path
730f62cd82SMax Reitz                },
740f62cd82SMax Reitz                filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
750f62cd82SMax Reitz
76*0b877d09SMax Reitz    # Filename should be plain, and the backing node filename should
77*0b877d09SMax Reitz    # not contain the "file:" prefix
780f62cd82SMax Reitz    log_node_info(vm.node_info('node0'))
790f62cd82SMax Reitz
800f62cd82SMax Reitz    vm.qmp_log('blockdev-del', node_name='node0')
810f62cd82SMax Reitz
820f62cd82SMax Reitz    log('')
830f62cd82SMax Reitz    log('--- change-backing-file ---')
840f62cd82SMax Reitz    log('')
850f62cd82SMax Reitz
860f62cd82SMax Reitz    vm.qmp_log('blockdev-add',
870f62cd82SMax Reitz               node_name='node0',
880f62cd82SMax Reitz               driver=iotests.imgfmt,
890f62cd82SMax Reitz               file={
900f62cd82SMax Reitz                   'driver': 'file',
910f62cd82SMax Reitz                   'filename': top_img_path
920f62cd82SMax Reitz               },
930f62cd82SMax Reitz               filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
940f62cd82SMax Reitz
950f62cd82SMax Reitz    # Changing the backing file to a qemu-reported filename should
960f62cd82SMax Reitz    # result in qemu accepting the corresponding BDS as the implicit
970f62cd82SMax Reitz    # backing BDS (and thus not generate a json:{} filename).
980f62cd82SMax Reitz    # So, first, query the backing filename.
990f62cd82SMax Reitz
1000f62cd82SMax Reitz    backing_filename = \
1010f62cd82SMax Reitz        vm.node_info('node0')['image']['backing-image']['filename']
1020f62cd82SMax Reitz
1030f62cd82SMax Reitz    # Next, change the backing file to something different
1040f62cd82SMax Reitz
1050f62cd82SMax Reitz    vm.qmp_log('change-backing-file',
1060f62cd82SMax Reitz               image_node_name='node0',
1070f62cd82SMax Reitz               device='node0',
1080f62cd82SMax Reitz               backing_file='null-co://',
1090f62cd82SMax Reitz               filters=[filter_qmp_testfiles])
1100f62cd82SMax Reitz
1110f62cd82SMax Reitz    # Now, verify that we get a json:{} filename
1120f62cd82SMax Reitz    # (Image header says "null-co://", actual backing file still is
1130f62cd82SMax Reitz    # base_img_path)
1140f62cd82SMax Reitz
1150f62cd82SMax Reitz    log_node_info(vm.node_info('node0'))
1160f62cd82SMax Reitz
1170f62cd82SMax Reitz    # Change it back
1180f62cd82SMax Reitz    # (To get header and backing file in sync)
1190f62cd82SMax Reitz
1200f62cd82SMax Reitz    vm.qmp_log('change-backing-file',
1210f62cd82SMax Reitz               image_node_name='node0',
1220f62cd82SMax Reitz               device='node0',
1230f62cd82SMax Reitz               backing_file=backing_filename,
1240f62cd82SMax Reitz               filters=[filter_qmp_testfiles])
1250f62cd82SMax Reitz
1260f62cd82SMax Reitz    # And verify that we get our original results
1270f62cd82SMax Reitz
1280f62cd82SMax Reitz    log_node_info(vm.node_info('node0'))
1290f62cd82SMax Reitz
1300f62cd82SMax Reitz    # Finally, try a "file:" prefix.  While this is actually what we
1310f62cd82SMax Reitz    # originally had in the image header, qemu will not reopen the
1320f62cd82SMax Reitz    # backing file here, so it cannot verify that this filename
1330f62cd82SMax Reitz    # "resolves" to the actual backing BDS's filename and will thus
1340f62cd82SMax Reitz    # consider both to be different.
1350f62cd82SMax Reitz    # (This may be fixed in the future.)
1360f62cd82SMax Reitz
1370f62cd82SMax Reitz    vm.qmp_log('change-backing-file',
1380f62cd82SMax Reitz               image_node_name='node0',
1390f62cd82SMax Reitz               device='node0',
1400f62cd82SMax Reitz               backing_file=('file:' + backing_filename),
1410f62cd82SMax Reitz               filters=[filter_qmp_testfiles])
1420f62cd82SMax Reitz
1430f62cd82SMax Reitz    # So now we should get a json:{} filename
1440f62cd82SMax Reitz
1450f62cd82SMax Reitz    log_node_info(vm.node_info('node0'))
1460f62cd82SMax Reitz
1470f62cd82SMax Reitz    # Remove and re-attach so we can see that (as in our first try),
1480f62cd82SMax Reitz    # opening the image anew helps qemu resolve the header backing
1490f62cd82SMax Reitz    # filename.
1500f62cd82SMax Reitz
1510f62cd82SMax Reitz    vm.qmp_log('blockdev-del', node_name='node0')
1520f62cd82SMax Reitz
1530f62cd82SMax Reitz    vm.qmp_log('blockdev-add',
1540f62cd82SMax Reitz               node_name='node0',
1550f62cd82SMax Reitz               driver=iotests.imgfmt,
1560f62cd82SMax Reitz               file={
1570f62cd82SMax Reitz                   'driver': 'file',
1580f62cd82SMax Reitz                   'filename': top_img_path
1590f62cd82SMax Reitz               },
1600f62cd82SMax Reitz               filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
1610f62cd82SMax Reitz
1620f62cd82SMax Reitz    log_node_info(vm.node_info('node0'))
1630f62cd82SMax Reitz
1640f62cd82SMax Reitz    vm.qmp_log('blockdev-del', node_name='node0')
1650f62cd82SMax Reitz
1660f62cd82SMax Reitz    log('')
1670f62cd82SMax Reitz    log('--- Override backing file ---')
1680f62cd82SMax Reitz    log('')
1690f62cd82SMax Reitz
1700f62cd82SMax Reitz    # For this test, we need the plain filename in the image header
1710f62cd82SMax Reitz    # (because qemu cannot "canonicalize"/"resolve" the backing
1720f62cd82SMax Reitz    # filename unless the backing file is opened implicitly with the
1730f62cd82SMax Reitz    # overlay)
1740f62cd82SMax Reitz    assert qemu_img('create', '-f', iotests.imgfmt, '-b', base_img_path,
175b66ff2c2SEric Blake                    '-F', iotests.imgfmt, top_img_path) == 0
1760f62cd82SMax Reitz
1770f62cd82SMax Reitz    # You can only reliably override backing options by using a node
1780f62cd82SMax Reitz    # reference (or by specifying file.filename, but, well...)
1790f62cd82SMax Reitz    vm.qmp_log('blockdev-add', node_name='null', driver='null-co')
1800f62cd82SMax Reitz
1810f62cd82SMax Reitz    vm.qmp_log('blockdev-add',
1820f62cd82SMax Reitz               node_name='node0',
1830f62cd82SMax Reitz               driver=iotests.imgfmt,
1840f62cd82SMax Reitz               file={
1850f62cd82SMax Reitz                   'driver': 'file',
1860f62cd82SMax Reitz                   'filename': top_img_path
1870f62cd82SMax Reitz               },
1880f62cd82SMax Reitz               backing='null',
1890f62cd82SMax Reitz               filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
1900f62cd82SMax Reitz
1910f62cd82SMax Reitz    # Should get a json:{} filename (and bs->backing_file is
1920f62cd82SMax Reitz    # null-co://, because that field actually has not much to do
1930f62cd82SMax Reitz    # with the header backing filename (except that it is changed by
1940f62cd82SMax Reitz    # change-backing-file))
1950f62cd82SMax Reitz
1960f62cd82SMax Reitz    log_node_info(vm.node_info('node0'))
1970f62cd82SMax Reitz
1980f62cd82SMax Reitz    # Detach the backing file by reopening the whole thing
1990f62cd82SMax Reitz
2000f62cd82SMax Reitz    vm.qmp_log('blockdev-del', node_name='node0')
2010f62cd82SMax Reitz    vm.qmp_log('blockdev-del', node_name='null')
2020f62cd82SMax Reitz
2030f62cd82SMax Reitz    vm.qmp_log('blockdev-add',
2040f62cd82SMax Reitz               node_name='node0',
2050f62cd82SMax Reitz               driver=iotests.imgfmt,
2060f62cd82SMax Reitz               file={
2070f62cd82SMax Reitz                   'driver': 'file',
2080f62cd82SMax Reitz                   'filename': top_img_path
2090f62cd82SMax Reitz               },
2100f62cd82SMax Reitz               backing=None,
2110f62cd82SMax Reitz               filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
2120f62cd82SMax Reitz
2130f62cd82SMax Reitz    # Should get a json:{} filename (because we overrode the backing
2140f62cd82SMax Reitz    # file to not be there)
2150f62cd82SMax Reitz
2160f62cd82SMax Reitz    log_node_info(vm.node_info('node0'))
2170f62cd82SMax Reitz
2180f62cd82SMax Reitz    # Open the original backing file
2190f62cd82SMax Reitz
2200f62cd82SMax Reitz    vm.qmp_log('blockdev-add',
2210f62cd82SMax Reitz               node_name='original-backing',
2220f62cd82SMax Reitz               driver=iotests.imgfmt,
2230f62cd82SMax Reitz               file={
2240f62cd82SMax Reitz                   'driver': 'file',
2250f62cd82SMax Reitz                   'filename': base_img_path
2260f62cd82SMax Reitz               },
2270f62cd82SMax Reitz               filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
2280f62cd82SMax Reitz
2290f62cd82SMax Reitz    # Attach the original backing file to its overlay
2300f62cd82SMax Reitz
2310f62cd82SMax Reitz    vm.qmp_log('blockdev-snapshot',
2320f62cd82SMax Reitz               node='original-backing',
2330f62cd82SMax Reitz               overlay='node0')
2340f62cd82SMax Reitz
2350f62cd82SMax Reitz    # This should give us the original plain result
2360f62cd82SMax Reitz
2370f62cd82SMax Reitz    log_node_info(vm.node_info('node0'))
2380f62cd82SMax Reitz
2390f62cd82SMax Reitz    vm.qmp_log('blockdev-del', node_name='node0')
2400f62cd82SMax Reitz    vm.qmp_log('blockdev-del', node_name='original-backing')
2410f62cd82SMax Reitz
2420f62cd82SMax Reitz    vm.shutdown()
243