xref: /openbmc/qemu/tests/qemu-iotests/264 (revision 86044b24)
1#!/usr/bin/env python
2#
3# Test nbd reconnect
4#
5# Copyright (c) 2019 Virtuozzo International GmbH.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21import time
22
23import iotests
24from iotests import qemu_img_create, qemu_io_silent_check, file_path, \
25        qemu_nbd_popen, log
26
27disk_a, disk_b, nbd_sock = file_path('disk_a', 'disk_b', 'nbd-sock')
28nbd_uri = 'nbd+unix:///?socket=' + nbd_sock
29size = 5 * 1024 * 1024
30wait_limit = 3
31wait_step = 0.2
32
33qemu_img_create('-f', iotests.imgfmt, disk_a, str(size))
34qemu_img_create('-f', iotests.imgfmt, disk_b, str(size))
35srv = qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b)
36
37# Wait for NBD server availability
38t = 0
39ok = False
40while t < wait_limit:
41    ok = qemu_io_silent_check('-f', 'raw', '-c', 'read 0 512', nbd_uri)
42    if ok:
43        break
44    time.sleep(wait_step)
45    t += wait_step
46
47assert ok
48
49vm = iotests.VM().add_drive(disk_a)
50vm.launch()
51vm.hmp_qemu_io('drive0', 'write 0 {}'.format(size))
52
53vm.qmp_log('blockdev-add', filters=[iotests.filter_qmp_testfiles],
54           **{'node_name': 'backup0',
55              'driver': 'raw',
56              'file': {'driver': 'nbd',
57                       'server': {'type': 'unix', 'path': nbd_sock},
58                       'reconnect-delay': 10}})
59vm.qmp_log('blockdev-backup', device='drive0', sync='full', target='backup0',
60           speed=(1 * 1024 * 1024))
61
62# Wait for some progress
63t = 0
64while t < wait_limit:
65    jobs = vm.qmp('query-block-jobs')['return']
66    if jobs and jobs[0]['offset'] > 0:
67        break
68    time.sleep(wait_step)
69    t += wait_step
70
71if jobs and jobs[0]['offset'] > 0:
72    log('Backup job is started')
73
74log('Kill NBD server')
75srv.kill()
76srv.wait()
77
78jobs = vm.qmp('query-block-jobs')['return']
79if jobs and jobs[0]['offset'] < jobs[0]['len']:
80    log('Backup job is still in progress')
81
82vm.qmp_log('block-job-set-speed', device='drive0', speed=0)
83
84# Emulate server down time for 1 second
85time.sleep(1)
86
87log('Start NBD server')
88srv = qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b)
89
90e = vm.event_wait('BLOCK_JOB_COMPLETED')
91log('Backup completed: {}'.format(e['data']['offset']))
92
93vm.qmp_log('blockdev-del', node_name='backup0')
94srv.kill()
95vm.shutdown()
96