xref: /openbmc/qemu/tests/qemu-iotests/264 (revision 979a8902)
1#!/usr/bin/env python3
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
27iotests.verify_image_format(supported_fmts=['qcow2'])
28
29disk_a, disk_b, nbd_sock = file_path('disk_a', 'disk_b', 'nbd-sock')
30nbd_uri = 'nbd+unix:///?socket=' + nbd_sock
31size = 5 * 1024 * 1024
32wait_limit = 3
33wait_step = 0.2
34
35qemu_img_create('-f', iotests.imgfmt, disk_a, str(size))
36qemu_img_create('-f', iotests.imgfmt, disk_b, str(size))
37srv = qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b)
38
39# Wait for NBD server availability
40t = 0
41ok = False
42while t < wait_limit:
43    ok = qemu_io_silent_check('-f', 'raw', '-c', 'read 0 512', nbd_uri)
44    if ok:
45        break
46    time.sleep(wait_step)
47    t += wait_step
48
49assert ok
50
51vm = iotests.VM().add_drive(disk_a)
52vm.launch()
53vm.hmp_qemu_io('drive0', 'write 0 {}'.format(size))
54
55vm.qmp_log('blockdev-add', filters=[iotests.filter_qmp_testfiles],
56           **{'node_name': 'backup0',
57              'driver': 'raw',
58              'file': {'driver': 'nbd',
59                       'server': {'type': 'unix', 'path': nbd_sock},
60                       'reconnect-delay': 10}})
61vm.qmp_log('blockdev-backup', device='drive0', sync='full', target='backup0',
62           speed=(1 * 1024 * 1024))
63
64# Wait for some progress
65t = 0
66while t < wait_limit:
67    jobs = vm.qmp('query-block-jobs')['return']
68    if jobs and jobs[0]['offset'] > 0:
69        break
70    time.sleep(wait_step)
71    t += wait_step
72
73if jobs and jobs[0]['offset'] > 0:
74    log('Backup job is started')
75
76log('Kill NBD server')
77srv.kill()
78srv.wait()
79
80jobs = vm.qmp('query-block-jobs')['return']
81if jobs and jobs[0]['offset'] < jobs[0]['len']:
82    log('Backup job is still in progress')
83
84vm.qmp_log('block-job-set-speed', device='drive0', speed=0)
85
86# Emulate server down time for 1 second
87time.sleep(1)
88
89log('Start NBD server')
90srv = qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b)
91
92e = vm.event_wait('BLOCK_JOB_COMPLETED')
93log('Backup completed: {}'.format(e['data']['offset']))
94
95vm.qmp_log('blockdev-del', node_name='backup0')
96srv.kill()
97vm.shutdown()
98