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