1#!/usr/bin/env python 2# 3# Copyright (C) 2017 Red Hat, Inc. 4# 5# This program is free software; you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation; either version 2 of the License, or 8# (at your option) any later version. 9# 10# This program is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with this program. If not, see <http://www.gnu.org/licenses/>. 17# 18# Creator/Owner: Stefan Hajnoczi <stefanha@redhat.com> 19# 20# Non-shared storage migration test using NBD server and drive-mirror 21 22import iotests 23 24iotests.verify_platform(['linux']) 25 26with iotests.FilePath('source.img') as source_img_path, \ 27 iotests.FilePath('dest.img') as dest_img_path, \ 28 iotests.FilePath('migration.sock') as migration_sock_path, \ 29 iotests.FilePath('nbd.sock') as nbd_sock_path, \ 30 iotests.VM('source') as source_vm, \ 31 iotests.VM('dest') as dest_vm: 32 33 img_size = '1G' 34 iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, source_img_path, img_size) 35 iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, dest_img_path, img_size) 36 37 iotests.log('Launching VMs...') 38 (source_vm.add_drive(source_img_path) 39 .launch()) 40 (dest_vm.add_drive(dest_img_path) 41 .add_incoming('unix:{0}'.format(migration_sock_path)) 42 .launch()) 43 44 iotests.log('Launching NBD server on destination...') 45 iotests.log(dest_vm.qmp('nbd-server-start', addr={'type': 'unix', 'data': {'path': nbd_sock_path}})) 46 iotests.log(dest_vm.qmp('nbd-server-add', device='drive0', writable=True)) 47 48 iotests.log('Starting `drive-mirror` on source...') 49 iotests.log(source_vm.qmp( 50 'drive-mirror', 51 device='drive0', 52 target='nbd+unix:///drive0?socket={0}'.format(nbd_sock_path), 53 sync='full', 54 format='raw', # always raw, the server handles the format 55 mode='existing', 56 job_id='mirror-job0')) 57 58 iotests.log('Waiting for `drive-mirror` to complete...') 59 iotests.log(source_vm.event_wait('BLOCK_JOB_READY'), 60 filters=[iotests.filter_qmp_event]) 61 62 iotests.log('Starting migration...') 63 source_vm.qmp('migrate-set-capabilities', 64 capabilities=[{'capability': 'events', 'state': True}]) 65 dest_vm.qmp('migrate-set-capabilities', 66 capabilities=[{'capability': 'events', 'state': True}]) 67 iotests.log(source_vm.qmp('migrate', uri='unix:{0}'.format(migration_sock_path))) 68 69 while True: 70 event1 = source_vm.event_wait('MIGRATION') 71 iotests.log(event1, filters=[iotests.filter_qmp_event]) 72 if event1['data']['status'] in ('completed', 'failed'): 73 iotests.log('Gracefully ending the `drive-mirror` job on source...') 74 iotests.log(source_vm.qmp('block-job-cancel', device='mirror-job0')) 75 break 76 77 while True: 78 event2 = source_vm.event_wait('BLOCK_JOB_COMPLETED') 79 iotests.log(event2, filters=[iotests.filter_qmp_event]) 80 if event2['event'] == 'BLOCK_JOB_COMPLETED': 81 iotests.log('Stopping the NBD server on destination...') 82 iotests.log(dest_vm.qmp('nbd-server-stop')) 83 break 84