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