112314f2dSStefan Hajnoczi#!/usr/bin/env python 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 2412314f2dSStefan Hajnocziiotests.verify_platform(['linux']) 2512314f2dSStefan Hajnoczi 26*921a3217SStefan Hajnocziwith iotests.FilePath('source.img') as source_img_path, \ 27*921a3217SStefan Hajnoczi iotests.FilePath('dest.img') as dest_img_path, \ 28*921a3217SStefan Hajnoczi iotests.FilePath('migration.sock') as migration_sock_path, \ 29*921a3217SStefan Hajnoczi iotests.FilePath('nbd.sock') as nbd_sock_path, \ 30*921a3217SStefan Hajnoczi iotests.VM('source') as source_vm, \ 31*921a3217SStefan Hajnoczi iotests.VM('dest') as dest_vm: 32*921a3217SStefan Hajnoczi 3312314f2dSStefan Hajnoczi img_size = '1G' 3412314f2dSStefan Hajnoczi iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, source_img_path, img_size) 3512314f2dSStefan Hajnoczi iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, dest_img_path, img_size) 3612314f2dSStefan Hajnoczi 3712314f2dSStefan Hajnoczi iotests.log('Launching VMs...') 38*921a3217SStefan Hajnoczi (source_vm.add_drive(source_img_path) 39*921a3217SStefan Hajnoczi .launch()) 40*921a3217SStefan Hajnoczi (dest_vm.add_drive(dest_img_path) 41*921a3217SStefan Hajnoczi .add_incoming('unix:{0}'.format(migration_sock_path)) 42*921a3217SStefan Hajnoczi .launch()) 4312314f2dSStefan Hajnoczi 4412314f2dSStefan Hajnoczi iotests.log('Launching NBD server on destination...') 4512314f2dSStefan Hajnoczi iotests.log(dest_vm.qmp('nbd-server-start', addr={'type': 'unix', 'data': {'path': nbd_sock_path}})) 4612314f2dSStefan Hajnoczi iotests.log(dest_vm.qmp('nbd-server-add', device='drive0', writable=True)) 4712314f2dSStefan Hajnoczi 482c94e271SKashyap Chamarthy iotests.log('Starting `drive-mirror` on source...') 4912314f2dSStefan Hajnoczi iotests.log(source_vm.qmp( 5012314f2dSStefan Hajnoczi 'drive-mirror', 5112314f2dSStefan Hajnoczi device='drive0', 5212314f2dSStefan Hajnoczi target='nbd+unix:///drive0?socket={0}'.format(nbd_sock_path), 5312314f2dSStefan Hajnoczi sync='full', 5412314f2dSStefan Hajnoczi format='raw', # always raw, the server handles the format 552c94e271SKashyap Chamarthy mode='existing', 562c94e271SKashyap Chamarthy job_id='mirror-job0')) 5712314f2dSStefan Hajnoczi 582c94e271SKashyap Chamarthy iotests.log('Waiting for `drive-mirror` to complete...') 5912314f2dSStefan Hajnoczi iotests.log(source_vm.event_wait('BLOCK_JOB_READY'), 6012314f2dSStefan Hajnoczi filters=[iotests.filter_qmp_event]) 6112314f2dSStefan Hajnoczi 6212314f2dSStefan Hajnoczi iotests.log('Starting migration...') 6312314f2dSStefan Hajnoczi source_vm.qmp('migrate-set-capabilities', 6412314f2dSStefan Hajnoczi capabilities=[{'capability': 'events', 'state': True}]) 6512314f2dSStefan Hajnoczi dest_vm.qmp('migrate-set-capabilities', 6612314f2dSStefan Hajnoczi capabilities=[{'capability': 'events', 'state': True}]) 6712314f2dSStefan Hajnoczi iotests.log(source_vm.qmp('migrate', uri='unix:{0}'.format(migration_sock_path))) 6812314f2dSStefan Hajnoczi 6912314f2dSStefan Hajnoczi while True: 702c94e271SKashyap Chamarthy event1 = source_vm.event_wait('MIGRATION') 712c94e271SKashyap Chamarthy iotests.log(event1, filters=[iotests.filter_qmp_event]) 722c94e271SKashyap Chamarthy if event1['data']['status'] in ('completed', 'failed'): 732c94e271SKashyap Chamarthy iotests.log('Gracefully ending the `drive-mirror` job on source...') 742c94e271SKashyap Chamarthy iotests.log(source_vm.qmp('block-job-cancel', device='mirror-job0')) 752c94e271SKashyap Chamarthy break 762c94e271SKashyap Chamarthy 772c94e271SKashyap Chamarthy while True: 782c94e271SKashyap Chamarthy event2 = source_vm.event_wait('BLOCK_JOB_COMPLETED') 792c94e271SKashyap Chamarthy iotests.log(event2, filters=[iotests.filter_qmp_event]) 802c94e271SKashyap Chamarthy if event2['event'] == 'BLOCK_JOB_COMPLETED': 812c94e271SKashyap Chamarthy iotests.log('Stopping the NBD server on destination...') 822c94e271SKashyap Chamarthy iotests.log(dest_vm.qmp('nbd-server-stop')) 8312314f2dSStefan Hajnoczi break 84