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 os 23import atexit 24import iotests 25 26iotests.verify_platform(['linux']) 27 28img_size = '1G' 29source_img_path = os.path.join(iotests.test_dir, 'source.img') 30dest_img_path = os.path.join(iotests.test_dir, 'dest.img') 31iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, source_img_path, img_size) 32iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, dest_img_path, img_size) 33 34iotests.log('Launching VMs...') 35migration_sock_path = os.path.join(iotests.test_dir, 'migration.sock') 36nbd_sock_path = os.path.join(iotests.test_dir, 'nbd.sock') 37source_vm = iotests.VM('source').add_drive(source_img_path) 38dest_vm = (iotests.VM('dest').add_drive(dest_img_path) 39 .add_incoming('unix:{0}'.format(migration_sock_path))) 40source_vm.launch() 41atexit.register(source_vm.shutdown) 42dest_vm.launch() 43atexit.register(dest_vm.shutdown) 44 45iotests.log('Launching NBD server on destination...') 46iotests.log(dest_vm.qmp('nbd-server-start', addr={'type': 'unix', 'data': {'path': nbd_sock_path}})) 47iotests.log(dest_vm.qmp('nbd-server-add', device='drive0', writable=True)) 48 49iotests.log('Starting `drive-mirror` on source...') 50iotests.log(source_vm.qmp( 51 'drive-mirror', 52 device='drive0', 53 target='nbd+unix:///drive0?socket={0}'.format(nbd_sock_path), 54 sync='full', 55 format='raw', # always raw, the server handles the format 56 mode='existing', 57 job_id='mirror-job0')) 58 59iotests.log('Waiting for `drive-mirror` to complete...') 60iotests.log(source_vm.event_wait('BLOCK_JOB_READY'), 61 filters=[iotests.filter_qmp_event]) 62 63iotests.log('Starting migration...') 64source_vm.qmp('migrate-set-capabilities', 65 capabilities=[{'capability': 'events', 'state': True}]) 66dest_vm.qmp('migrate-set-capabilities', 67 capabilities=[{'capability': 'events', 'state': True}]) 68iotests.log(source_vm.qmp('migrate', uri='unix:{0}'.format(migration_sock_path))) 69 70while True: 71 event1 = source_vm.event_wait('MIGRATION') 72 iotests.log(event1, filters=[iotests.filter_qmp_event]) 73 if event1['data']['status'] in ('completed', 'failed'): 74 iotests.log('Gracefully ending the `drive-mirror` job on source...') 75 iotests.log(source_vm.qmp('block-job-cancel', device='mirror-job0')) 76 break 77 78while True: 79 event2 = source_vm.event_wait('BLOCK_JOB_COMPLETED') 80 iotests.log(event2, filters=[iotests.filter_qmp_event]) 81 if event2['event'] == 'BLOCK_JOB_COMPLETED': 82 iotests.log('Stopping the NBD server on destination...') 83 iotests.log(dest_vm.qmp('nbd-server-stop')) 84 break 85