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