17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw migration quick 312314f2dSStefan Hajnoczi# 412314f2dSStefan Hajnoczi# Copyright (C) 2017 Red Hat, Inc. 512314f2dSStefan Hajnoczi# 612314f2dSStefan Hajnoczi# This program is free software; you can redistribute it and/or modify 712314f2dSStefan Hajnoczi# it under the terms of the GNU General Public License as published by 812314f2dSStefan Hajnoczi# the Free Software Foundation; either version 2 of the License, or 912314f2dSStefan Hajnoczi# (at your option) any later version. 1012314f2dSStefan Hajnoczi# 1112314f2dSStefan Hajnoczi# This program is distributed in the hope that it will be useful, 1212314f2dSStefan Hajnoczi# but WITHOUT ANY WARRANTY; without even the implied warranty of 1312314f2dSStefan Hajnoczi# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1412314f2dSStefan Hajnoczi# GNU General Public License for more details. 1512314f2dSStefan Hajnoczi# 1612314f2dSStefan Hajnoczi# You should have received a copy of the GNU General Public License 1712314f2dSStefan Hajnoczi# along with this program. If not, see <http://www.gnu.org/licenses/>. 1812314f2dSStefan Hajnoczi# 1912314f2dSStefan Hajnoczi# Creator/Owner: Stefan Hajnoczi <stefanha@redhat.com> 2012314f2dSStefan Hajnoczi# 2112314f2dSStefan Hajnoczi# Non-shared storage migration test using NBD server and drive-mirror 2212314f2dSStefan Hajnoczi 2312314f2dSStefan Hajnocziimport iotests 2412314f2dSStefan Hajnoczi 257d814059SJohn Snowiotests.script_initialize(supported_fmts=['qcow2', 'qed', 'raw'], 267d814059SJohn Snow supported_platforms=['linux']) 2712314f2dSStefan Hajnoczi 28921a3217SStefan Hajnocziwith iotests.FilePath('source.img') as source_img_path, \ 29921a3217SStefan Hajnoczi iotests.FilePath('dest.img') as dest_img_path, \ 303192fad7SNir Soffer iotests.FilePath('migration.sock', 'nbd.sock', base_dir=iotests.sock_dir) \ 31a242b19eSNir Soffer as (migration_sock_path, nbd_sock_path), \ 32921a3217SStefan Hajnoczi iotests.VM('source') as source_vm, \ 33921a3217SStefan Hajnoczi iotests.VM('dest') as dest_vm: 34921a3217SStefan Hajnoczi 3512314f2dSStefan Hajnoczi img_size = '1G' 363d53818fSJohn Snow iotests.qemu_img_create('-f', iotests.imgfmt, source_img_path, img_size) 37eb896278SEric Blake iotests.qemu_io('-f', iotests.imgfmt, '-c', 'write 512M 1M', source_img_path) 383d53818fSJohn Snow iotests.qemu_img_create('-f', iotests.imgfmt, dest_img_path, img_size) 3912314f2dSStefan Hajnoczi 4012314f2dSStefan Hajnoczi iotests.log('Launching VMs...') 41921a3217SStefan Hajnoczi (source_vm.add_drive(source_img_path) 42921a3217SStefan Hajnoczi .launch()) 43921a3217SStefan Hajnoczi (dest_vm.add_drive(dest_img_path) 44921a3217SStefan Hajnoczi .add_incoming('unix:{0}'.format(migration_sock_path)) 45921a3217SStefan Hajnoczi .launch()) 4612314f2dSStefan Hajnoczi 47ae00aa23SVladimir Sementsov-Ogievskiy source_vm.qmp_log('block-dirty-bitmap-add', node='drive0', name='bitmap0') 48ae00aa23SVladimir Sementsov-Ogievskiy 4912314f2dSStefan Hajnoczi iotests.log('Launching NBD server on destination...') 5012314f2dSStefan Hajnoczi iotests.log(dest_vm.qmp('nbd-server-start', addr={'type': 'unix', 'data': {'path': nbd_sock_path}})) 5112314f2dSStefan Hajnoczi iotests.log(dest_vm.qmp('nbd-server-add', device='drive0', writable=True)) 5212314f2dSStefan Hajnoczi 532c94e271SKashyap Chamarthy iotests.log('Starting `drive-mirror` on source...') 5412314f2dSStefan Hajnoczi iotests.log(source_vm.qmp( 5512314f2dSStefan Hajnoczi 'drive-mirror', 5612314f2dSStefan Hajnoczi device='drive0', 5712314f2dSStefan Hajnoczi target='nbd+unix:///drive0?socket={0}'.format(nbd_sock_path), 5812314f2dSStefan Hajnoczi sync='full', 5912314f2dSStefan Hajnoczi format='raw', # always raw, the server handles the format 602c94e271SKashyap Chamarthy mode='existing', 612c94e271SKashyap Chamarthy job_id='mirror-job0')) 6212314f2dSStefan Hajnoczi 632c94e271SKashyap Chamarthy iotests.log('Waiting for `drive-mirror` to complete...') 6412314f2dSStefan Hajnoczi iotests.log(source_vm.event_wait('BLOCK_JOB_READY'), 65*181a6366SEric Blake filters=[iotests.filter_qmp_event, 66*181a6366SEric Blake iotests.filter_block_job]) 6712314f2dSStefan Hajnoczi 6812314f2dSStefan Hajnoczi iotests.log('Starting migration...') 69ae00aa23SVladimir Sementsov-Ogievskiy capabilities = [{'capability': 'events', 'state': True}, 70ae00aa23SVladimir Sementsov-Ogievskiy {'capability': 'dirty-bitmaps', 'state': True}] 71ae00aa23SVladimir Sementsov-Ogievskiy source_vm.qmp('migrate-set-capabilities', capabilities=capabilities) 72ae00aa23SVladimir Sementsov-Ogievskiy dest_vm.qmp('migrate-set-capabilities', capabilities=capabilities) 7312314f2dSStefan Hajnoczi iotests.log(source_vm.qmp('migrate', uri='unix:{0}'.format(migration_sock_path))) 7412314f2dSStefan Hajnoczi 75ae00aa23SVladimir Sementsov-Ogievskiy source_vm.qmp_log('migrate-start-postcopy') 76ae00aa23SVladimir Sementsov-Ogievskiy 7712314f2dSStefan Hajnoczi while True: 782c94e271SKashyap Chamarthy event1 = source_vm.event_wait('MIGRATION') 79dcc28ab6SVladimir Sementsov-Ogievskiy if event1['data']['status'] == 'postcopy-active': 80dcc28ab6SVladimir Sementsov-Ogievskiy # This event is racy, it depends do we really do postcopy or bitmap 81dcc28ab6SVladimir Sementsov-Ogievskiy # was migrated during downtime (and no data to migrate in postcopy 82dcc28ab6SVladimir Sementsov-Ogievskiy # phase). So, don't log it. 83dcc28ab6SVladimir Sementsov-Ogievskiy continue 842c94e271SKashyap Chamarthy iotests.log(event1, filters=[iotests.filter_qmp_event]) 852c94e271SKashyap Chamarthy if event1['data']['status'] in ('completed', 'failed'): 862c94e271SKashyap Chamarthy iotests.log('Gracefully ending the `drive-mirror` job on source...') 872c94e271SKashyap Chamarthy iotests.log(source_vm.qmp('block-job-cancel', device='mirror-job0')) 882c94e271SKashyap Chamarthy break 892c94e271SKashyap Chamarthy 902c94e271SKashyap Chamarthy while True: 912c94e271SKashyap Chamarthy event2 = source_vm.event_wait('BLOCK_JOB_COMPLETED') 92*181a6366SEric Blake iotests.log(event2, filters=[iotests.filter_qmp_event, 93*181a6366SEric Blake iotests.filter_block_job]) 942c94e271SKashyap Chamarthy if event2['event'] == 'BLOCK_JOB_COMPLETED': 952c94e271SKashyap Chamarthy iotests.log('Stopping the NBD server on destination...') 962c94e271SKashyap Chamarthy iotests.log(dest_vm.qmp('nbd-server-stop')) 9712314f2dSStefan Hajnoczi break 98ae00aa23SVladimir Sementsov-Ogievskiy 9993d48780SVladimir Sementsov-Ogievskiy iotests.log('Wait for migration completion on target...') 10093d48780SVladimir Sementsov-Ogievskiy migr_events = (('MIGRATION', {'data': {'status': 'completed'}}), 10193d48780SVladimir Sementsov-Ogievskiy ('MIGRATION', {'data': {'status': 'failed'}})) 10293d48780SVladimir Sementsov-Ogievskiy event = dest_vm.events_wait(migr_events) 10393d48780SVladimir Sementsov-Ogievskiy iotests.log(event, filters=[iotests.filter_qmp_event]) 10493d48780SVladimir Sementsov-Ogievskiy 10593d48780SVladimir Sementsov-Ogievskiy iotests.log('Check bitmaps on source:') 106e67d8e29SDaniel P. Berrangé iotests.log(source_vm.qmp('query-block')['return'][0]['inserted']['dirty-bitmaps']) 10793d48780SVladimir Sementsov-Ogievskiy 10893d48780SVladimir Sementsov-Ogievskiy iotests.log('Check bitmaps on target:') 109e67d8e29SDaniel P. Berrangé iotests.log(dest_vm.qmp('query-block')['return'][0]['inserted']['dirty-bitmaps']) 110