17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3 212314f2dSStefan Hajnoczi# 312314f2dSStefan Hajnoczi# Copyright (C) 2017 Red Hat, Inc. 412314f2dSStefan Hajnoczi# 512314f2dSStefan Hajnoczi# This program is free software; you can redistribute it and/or modify 612314f2dSStefan Hajnoczi# it under the terms of the GNU General Public License as published by 712314f2dSStefan Hajnoczi# the Free Software Foundation; either version 2 of the License, or 812314f2dSStefan Hajnoczi# (at your option) any later version. 912314f2dSStefan Hajnoczi# 1012314f2dSStefan Hajnoczi# This program is distributed in the hope that it will be useful, 1112314f2dSStefan Hajnoczi# but WITHOUT ANY WARRANTY; without even the implied warranty of 1212314f2dSStefan Hajnoczi# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1312314f2dSStefan Hajnoczi# GNU General Public License for more details. 1412314f2dSStefan Hajnoczi# 1512314f2dSStefan Hajnoczi# You should have received a copy of the GNU General Public License 1612314f2dSStefan Hajnoczi# along with this program. If not, see <http://www.gnu.org/licenses/>. 1712314f2dSStefan Hajnoczi# 1812314f2dSStefan Hajnoczi# Creator/Owner: Stefan Hajnoczi <stefanha@redhat.com> 1912314f2dSStefan Hajnoczi# 2012314f2dSStefan Hajnoczi# Non-shared storage migration test using NBD server and drive-mirror 2112314f2dSStefan Hajnoczi 2212314f2dSStefan Hajnocziimport iotests 2312314f2dSStefan Hajnoczi 247d814059SJohn Snowiotests.script_initialize(supported_fmts=['qcow2', 'qed', 'raw'], 257d814059SJohn Snow supported_platforms=['linux']) 2612314f2dSStefan Hajnoczi 27921a3217SStefan Hajnocziwith iotests.FilePath('source.img') as source_img_path, \ 28921a3217SStefan Hajnoczi iotests.FilePath('dest.img') as dest_img_path, \ 294b4d34f4SMax Reitz iotests.FilePaths(['migration.sock', 'nbd.sock'], iotests.sock_dir) as \ 304b4d34f4SMax Reitz [migration_sock_path, nbd_sock_path], \ 31921a3217SStefan Hajnoczi iotests.VM('source') as source_vm, \ 32921a3217SStefan Hajnoczi iotests.VM('dest') as dest_vm: 33921a3217SStefan Hajnoczi 3412314f2dSStefan Hajnoczi img_size = '1G' 3512314f2dSStefan Hajnoczi iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, source_img_path, img_size) 3612314f2dSStefan Hajnoczi iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, dest_img_path, img_size) 3712314f2dSStefan Hajnoczi 3812314f2dSStefan Hajnoczi iotests.log('Launching VMs...') 39921a3217SStefan Hajnoczi (source_vm.add_drive(source_img_path) 40921a3217SStefan Hajnoczi .launch()) 41921a3217SStefan Hajnoczi (dest_vm.add_drive(dest_img_path) 42921a3217SStefan Hajnoczi .add_incoming('unix:{0}'.format(migration_sock_path)) 43921a3217SStefan Hajnoczi .launch()) 4412314f2dSStefan Hajnoczi 45ae00aa23SVladimir Sementsov-Ogievskiy source_vm.qmp_log('block-dirty-bitmap-add', node='drive0', name='bitmap0') 46ae00aa23SVladimir Sementsov-Ogievskiy 4712314f2dSStefan Hajnoczi iotests.log('Launching NBD server on destination...') 4812314f2dSStefan Hajnoczi iotests.log(dest_vm.qmp('nbd-server-start', addr={'type': 'unix', 'data': {'path': nbd_sock_path}})) 4912314f2dSStefan Hajnoczi iotests.log(dest_vm.qmp('nbd-server-add', device='drive0', writable=True)) 5012314f2dSStefan Hajnoczi 512c94e271SKashyap Chamarthy iotests.log('Starting `drive-mirror` on source...') 5212314f2dSStefan Hajnoczi iotests.log(source_vm.qmp( 5312314f2dSStefan Hajnoczi 'drive-mirror', 5412314f2dSStefan Hajnoczi device='drive0', 5512314f2dSStefan Hajnoczi target='nbd+unix:///drive0?socket={0}'.format(nbd_sock_path), 5612314f2dSStefan Hajnoczi sync='full', 5712314f2dSStefan Hajnoczi format='raw', # always raw, the server handles the format 582c94e271SKashyap Chamarthy mode='existing', 592c94e271SKashyap Chamarthy job_id='mirror-job0')) 6012314f2dSStefan Hajnoczi 612c94e271SKashyap Chamarthy iotests.log('Waiting for `drive-mirror` to complete...') 6212314f2dSStefan Hajnoczi iotests.log(source_vm.event_wait('BLOCK_JOB_READY'), 6312314f2dSStefan Hajnoczi filters=[iotests.filter_qmp_event]) 6412314f2dSStefan Hajnoczi 6512314f2dSStefan Hajnoczi iotests.log('Starting migration...') 66ae00aa23SVladimir Sementsov-Ogievskiy capabilities = [{'capability': 'events', 'state': True}, 67ae00aa23SVladimir Sementsov-Ogievskiy {'capability': 'dirty-bitmaps', 'state': True}] 68ae00aa23SVladimir Sementsov-Ogievskiy source_vm.qmp('migrate-set-capabilities', capabilities=capabilities) 69ae00aa23SVladimir Sementsov-Ogievskiy dest_vm.qmp('migrate-set-capabilities', capabilities=capabilities) 7012314f2dSStefan Hajnoczi iotests.log(source_vm.qmp('migrate', uri='unix:{0}'.format(migration_sock_path))) 7112314f2dSStefan Hajnoczi 72ae00aa23SVladimir Sementsov-Ogievskiy source_vm.qmp_log('migrate-start-postcopy') 73ae00aa23SVladimir Sementsov-Ogievskiy 7412314f2dSStefan Hajnoczi while True: 752c94e271SKashyap Chamarthy event1 = source_vm.event_wait('MIGRATION') 762c94e271SKashyap Chamarthy iotests.log(event1, filters=[iotests.filter_qmp_event]) 772c94e271SKashyap Chamarthy if event1['data']['status'] in ('completed', 'failed'): 782c94e271SKashyap Chamarthy iotests.log('Gracefully ending the `drive-mirror` job on source...') 792c94e271SKashyap Chamarthy iotests.log(source_vm.qmp('block-job-cancel', device='mirror-job0')) 802c94e271SKashyap Chamarthy break 812c94e271SKashyap Chamarthy 822c94e271SKashyap Chamarthy while True: 832c94e271SKashyap Chamarthy event2 = source_vm.event_wait('BLOCK_JOB_COMPLETED') 842c94e271SKashyap Chamarthy iotests.log(event2, filters=[iotests.filter_qmp_event]) 852c94e271SKashyap Chamarthy if event2['event'] == 'BLOCK_JOB_COMPLETED': 862c94e271SKashyap Chamarthy iotests.log('Stopping the NBD server on destination...') 872c94e271SKashyap Chamarthy iotests.log(dest_vm.qmp('nbd-server-stop')) 8812314f2dSStefan Hajnoczi break 89ae00aa23SVladimir Sementsov-Ogievskiy 90*93d48780SVladimir Sementsov-Ogievskiy iotests.log('Wait for migration completion on target...') 91*93d48780SVladimir Sementsov-Ogievskiy migr_events = (('MIGRATION', {'data': {'status': 'completed'}}), 92*93d48780SVladimir Sementsov-Ogievskiy ('MIGRATION', {'data': {'status': 'failed'}})) 93*93d48780SVladimir Sementsov-Ogievskiy event = dest_vm.events_wait(migr_events) 94*93d48780SVladimir Sementsov-Ogievskiy iotests.log(event, filters=[iotests.filter_qmp_event]) 95*93d48780SVladimir Sementsov-Ogievskiy 96*93d48780SVladimir Sementsov-Ogievskiy iotests.log('Check bitmaps on source:') 97ae00aa23SVladimir Sementsov-Ogievskiy iotests.log(source_vm.qmp('query-block')['return'][0]['dirty-bitmaps']) 98*93d48780SVladimir Sementsov-Ogievskiy 99*93d48780SVladimir Sementsov-Ogievskiy iotests.log('Check bitmaps on target:') 100*93d48780SVladimir Sementsov-Ogievskiy iotests.log(dest_vm.qmp('query-block')['return'][0]['dirty-bitmaps']) 101