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