xref: /openbmc/qemu/tests/qemu-iotests/194 (revision de15df5e)
1#!/usr/bin/env python3
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.script_initialize(supported_fmts=['qcow2', 'qed', 'raw'],
25                          supported_platforms=['linux'])
26
27with iotests.FilePath('source.img') as source_img_path, \
28     iotests.FilePath('dest.img') as dest_img_path, \
29     iotests.FilePaths(['migration.sock', 'nbd.sock'], iotests.sock_dir) as \
30         [migration_sock_path, nbd_sock_path], \
31     iotests.VM('source') as source_vm, \
32     iotests.VM('dest') as dest_vm:
33
34    img_size = '1G'
35    iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, source_img_path, img_size)
36    iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, dest_img_path, img_size)
37
38    iotests.log('Launching VMs...')
39    (source_vm.add_drive(source_img_path)
40              .launch())
41    (dest_vm.add_drive(dest_img_path)
42            .add_incoming('unix:{0}'.format(migration_sock_path))
43            .launch())
44
45    source_vm.qmp_log('block-dirty-bitmap-add', node='drive0', name='bitmap0')
46
47    iotests.log('Launching NBD server on destination...')
48    iotests.log(dest_vm.qmp('nbd-server-start', addr={'type': 'unix', 'data': {'path': nbd_sock_path}}))
49    iotests.log(dest_vm.qmp('nbd-server-add', device='drive0', writable=True))
50
51    iotests.log('Starting `drive-mirror` on source...')
52    iotests.log(source_vm.qmp(
53                  'drive-mirror',
54                  device='drive0',
55                  target='nbd+unix:///drive0?socket={0}'.format(nbd_sock_path),
56                  sync='full',
57                  format='raw', # always raw, the server handles the format
58                  mode='existing',
59                  job_id='mirror-job0'))
60
61    iotests.log('Waiting for `drive-mirror` to complete...')
62    iotests.log(source_vm.event_wait('BLOCK_JOB_READY'),
63                filters=[iotests.filter_qmp_event])
64
65    iotests.log('Starting migration...')
66    capabilities = [{'capability': 'events', 'state': True},
67                    {'capability': 'dirty-bitmaps', 'state': True}]
68    source_vm.qmp('migrate-set-capabilities', capabilities=capabilities)
69    dest_vm.qmp('migrate-set-capabilities', capabilities=capabilities)
70    iotests.log(source_vm.qmp('migrate', uri='unix:{0}'.format(migration_sock_path)))
71
72    source_vm.qmp_log('migrate-start-postcopy')
73
74    while True:
75        event1 = source_vm.event_wait('MIGRATION')
76        iotests.log(event1, filters=[iotests.filter_qmp_event])
77        if event1['data']['status'] in ('completed', 'failed'):
78            iotests.log('Gracefully ending the `drive-mirror` job on source...')
79            iotests.log(source_vm.qmp('block-job-cancel', device='mirror-job0'))
80            break
81
82    while True:
83        event2 = source_vm.event_wait('BLOCK_JOB_COMPLETED')
84        iotests.log(event2, filters=[iotests.filter_qmp_event])
85        if event2['event'] == 'BLOCK_JOB_COMPLETED':
86            iotests.log('Stopping the NBD server on destination...')
87            iotests.log(dest_vm.qmp('nbd-server-stop'))
88            break
89
90    iotests.log('Wait for migration completion on target...')
91    migr_events = (('MIGRATION', {'data': {'status': 'completed'}}),
92                   ('MIGRATION', {'data': {'status': 'failed'}}))
93    event = dest_vm.events_wait(migr_events)
94    iotests.log(event, filters=[iotests.filter_qmp_event])
95
96    iotests.log('Check bitmaps on source:')
97    iotests.log(source_vm.qmp('query-block')['return'][0]['dirty-bitmaps'])
98
99    iotests.log('Check bitmaps on target:')
100    iotests.log(dest_vm.qmp('query-block')['return'][0]['dirty-bitmaps'])
101