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 24ad53ea42SKevin Wolfiotests.verify_image_format(supported_fmts=['qcow2', 'qed', 'raw']) 2512314f2dSStefan Hajnocziiotests.verify_platform(['linux']) 2612314f2dSStefan Hajnoczi 27921a3217SStefan Hajnocziwith iotests.FilePath('source.img') as source_img_path, \ 28921a3217SStefan Hajnoczi iotests.FilePath('dest.img') as dest_img_path, \ 29*4b4d34f4SMax Reitz iotests.FilePaths(['migration.sock', 'nbd.sock'], iotests.sock_dir) as \ 30*4b4d34f4SMax 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 4512314f2dSStefan Hajnoczi iotests.log('Launching NBD server on destination...') 4612314f2dSStefan Hajnoczi iotests.log(dest_vm.qmp('nbd-server-start', addr={'type': 'unix', 'data': {'path': nbd_sock_path}})) 4712314f2dSStefan Hajnoczi iotests.log(dest_vm.qmp('nbd-server-add', device='drive0', writable=True)) 4812314f2dSStefan Hajnoczi 492c94e271SKashyap Chamarthy iotests.log('Starting `drive-mirror` on source...') 5012314f2dSStefan Hajnoczi iotests.log(source_vm.qmp( 5112314f2dSStefan Hajnoczi 'drive-mirror', 5212314f2dSStefan Hajnoczi device='drive0', 5312314f2dSStefan Hajnoczi target='nbd+unix:///drive0?socket={0}'.format(nbd_sock_path), 5412314f2dSStefan Hajnoczi sync='full', 5512314f2dSStefan Hajnoczi format='raw', # always raw, the server handles the format 562c94e271SKashyap Chamarthy mode='existing', 572c94e271SKashyap Chamarthy job_id='mirror-job0')) 5812314f2dSStefan Hajnoczi 592c94e271SKashyap Chamarthy iotests.log('Waiting for `drive-mirror` to complete...') 6012314f2dSStefan Hajnoczi iotests.log(source_vm.event_wait('BLOCK_JOB_READY'), 6112314f2dSStefan Hajnoczi filters=[iotests.filter_qmp_event]) 6212314f2dSStefan Hajnoczi 6312314f2dSStefan Hajnoczi iotests.log('Starting migration...') 6412314f2dSStefan Hajnoczi source_vm.qmp('migrate-set-capabilities', 6512314f2dSStefan Hajnoczi capabilities=[{'capability': 'events', 'state': True}]) 6612314f2dSStefan Hajnoczi dest_vm.qmp('migrate-set-capabilities', 6712314f2dSStefan Hajnoczi capabilities=[{'capability': 'events', 'state': True}]) 6812314f2dSStefan Hajnoczi iotests.log(source_vm.qmp('migrate', uri='unix:{0}'.format(migration_sock_path))) 6912314f2dSStefan Hajnoczi 7012314f2dSStefan Hajnoczi while True: 712c94e271SKashyap Chamarthy event1 = source_vm.event_wait('MIGRATION') 722c94e271SKashyap Chamarthy iotests.log(event1, filters=[iotests.filter_qmp_event]) 732c94e271SKashyap Chamarthy if event1['data']['status'] in ('completed', 'failed'): 742c94e271SKashyap Chamarthy iotests.log('Gracefully ending the `drive-mirror` job on source...') 752c94e271SKashyap Chamarthy iotests.log(source_vm.qmp('block-job-cancel', device='mirror-job0')) 762c94e271SKashyap Chamarthy break 772c94e271SKashyap Chamarthy 782c94e271SKashyap Chamarthy while True: 792c94e271SKashyap Chamarthy event2 = source_vm.event_wait('BLOCK_JOB_COMPLETED') 802c94e271SKashyap Chamarthy iotests.log(event2, filters=[iotests.filter_qmp_event]) 812c94e271SKashyap Chamarthy if event2['event'] == 'BLOCK_JOB_COMPLETED': 822c94e271SKashyap Chamarthy iotests.log('Stopping the NBD server on destination...') 832c94e271SKashyap Chamarthy iotests.log(dest_vm.qmp('nbd-server-stop')) 8412314f2dSStefan Hajnoczi break 85