xref: /openbmc/qemu/tests/qemu-iotests/280 (revision 19f70347)
1#!/usr/bin/env python3
2#
3# Copyright (C) 2019 Red Hat, Inc.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17#
18# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
19#
20# Test migration to file for taking an external snapshot with VM state.
21
22import iotests
23import os
24
25iotests.verify_image_format(supported_fmts=['qcow2'])
26iotests.verify_protocol(supported=['file'])
27iotests.verify_platform(['linux'])
28
29with iotests.FilePath('base') as base_path , \
30     iotests.FilePath('top') as top_path, \
31     iotests.VM() as vm:
32
33    iotests.qemu_img_log('create', '-f', iotests.imgfmt, base_path, '64M')
34
35    iotests.log('=== Launch VM ===')
36    vm.add_object('iothread,id=iothread0')
37    vm.add_blockdev('file,filename=%s,node-name=base-file' % (base_path))
38    vm.add_blockdev('%s,file=base-file,node-name=base-fmt' % (iotests.imgfmt))
39    vm.add_device('virtio-blk,drive=base-fmt,iothread=iothread0,id=vda')
40    vm.launch()
41
42    vm.enable_migration_events('VM')
43
44    iotests.log('\n=== Migrate to file ===')
45    vm.qmp_log('migrate', uri='exec:cat > /dev/null')
46
47    with iotests.Timeout(3, 'Migration does not complete'):
48        vm.wait_migration('postmigrate')
49
50    iotests.log('\nVM is now stopped:')
51    iotests.log(vm.qmp('query-migrate')['return']['status'])
52    vm.qmp_log('query-status')
53
54    iotests.log('\n=== Create a snapshot of the disk image ===')
55    vm.blockdev_create({
56        'driver': 'file',
57        'filename': top_path,
58        'size': 0,
59    })
60    vm.qmp_log('blockdev-add', node_name='top-file',
61               driver='file', filename=top_path,
62               filters=[iotests.filter_qmp_testfiles])
63
64    vm.blockdev_create({
65        'driver': iotests.imgfmt,
66        'file': 'top-file',
67        'size': 1024 * 1024,
68    })
69    vm.qmp_log('blockdev-add', node_name='top-fmt',
70               driver=iotests.imgfmt, file='top-file')
71
72    vm.qmp_log('blockdev-snapshot', node='base-fmt', overlay='top-fmt')
73
74    iotests.log('\n=== Resume the VM and simulate a write request ===')
75    vm.qmp_log('cont')
76    iotests.log(vm.hmp_qemu_io('-d vda/virtio-backend', 'write 4k 4k'))
77
78    iotests.log('\n=== Commit it to the backing file ===')
79    result = vm.qmp_log('block-commit', job_id='job0', auto_dismiss=False,
80                        device='top-fmt', top_node='top-fmt',
81                        filters=[iotests.filter_qmp_testfiles])
82    if 'return' in result:
83        vm.run_job('job0')
84