1#!/usr/bin/env bash
2#
3# Test old-style block migration (migrate -b)
4#
5# Copyright (C) 2017 Red Hat, Inc.
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
21# creator
22owner=kwolf@redhat.com
23
24seq=`basename $0`
25echo "QA output created by $seq"
26
27status=1 # failure is the default!
28
29MIG_SOCKET="${SOCK_DIR}/migrate"
30
31_cleanup()
32{
33    rm -f "${MIG_SOCKET}"
34    rm -f "${TEST_IMG}.dest"
35    _cleanup_test_img
36    _cleanup_qemu
37}
38trap "_cleanup; exit \$status" 0 1 2 3 15
39
40# get standard environment, filters and checks
41. ./common.rc
42. ./common.filter
43. ./common.qemu
44
45_supported_fmt qcow2 raw qed quorum
46_supported_proto file
47
48size=64M
49_make_test_img $size
50TEST_IMG="${TEST_IMG}.dest" _make_test_img $size
51
52echo
53echo === Starting VMs ===
54echo
55
56qemu_comm_method="qmp"
57
58_launch_qemu \
59    -drive file="${TEST_IMG}",cache=$CACHEMODE,driver=$IMGFMT,id=disk
60src=$QEMU_HANDLE
61_send_qemu_cmd $src "{ 'execute': 'qmp_capabilities' }" 'return'
62
63_launch_qemu \
64    -drive file="${TEST_IMG}.dest",cache=$CACHEMODE,driver=$IMGFMT,id=disk \
65    -incoming "unix:${MIG_SOCKET}"
66dest=$QEMU_HANDLE
67_send_qemu_cmd $dest "{ 'execute': 'qmp_capabilities' }" 'return'
68
69echo
70echo === Write something on the source ===
71echo
72
73_send_qemu_cmd $src \
74    "{ 'execute': 'human-monitor-command',
75       'arguments': { 'command-line':
76                      'qemu-io disk \"write -P 0x55 0 64k\"' } }" \
77    'return'
78_send_qemu_cmd $src \
79    "{ 'execute': 'human-monitor-command',
80       'arguments': { 'command-line':
81                      'qemu-io disk \"read -P 0x55 0 64k\"' } }" \
82    'return'
83
84echo
85echo === Do block migration to destination ===
86echo
87
88reply="$(_send_qemu_cmd $src \
89    "{ 'execute': 'migrate',
90       'arguments': { 'uri': 'unix:${MIG_SOCKET}', 'blk': true } }" \
91    'return\|error')"
92echo "$reply"
93if echo "$reply" | grep "compiled without old-style" > /dev/null; then
94    _notrun "migrate -b support not compiled in"
95fi
96
97timeout_comm=$QEMU_COMM_TIMEOUT
98if [ "${VALGRIND_QEMU}" == "y" ]; then
99    QEMU_COMM_TIMEOUT=4
100else
101    QEMU_COMM_TIMEOUT=0.1
102fi
103qemu_cmd_repeat=50 silent=yes \
104    _send_qemu_cmd $src "{ 'execute': 'query-migrate' }" '"status": "completed"'
105QEMU_COMM_TIMEOUT=$timeout_comm
106_send_qemu_cmd $src "{ 'execute': 'query-status' }" "return"
107
108echo
109echo === Do some I/O on the destination ===
110echo
111
112# It is important that we use the BlockBackend of the guest device here instead
113# of the node name, which would create a new BlockBackend and not test whether
114# the guest has the necessary permissions to access the image now
115silent=yes _send_qemu_cmd $dest "" "100 %"
116_send_qemu_cmd $dest "{ 'execute': 'query-status' }" "return"
117_send_qemu_cmd $dest \
118    "{ 'execute': 'human-monitor-command',
119       'arguments': { 'command-line':
120                      'qemu-io disk \"read -P 0x55 0 64k\"' } }" \
121    'return'
122_send_qemu_cmd $dest \
123    "{ 'execute': 'human-monitor-command',
124       'arguments': { 'command-line':
125                      'qemu-io disk \"write -P 0x66 1M 64k\"' } }" \
126    'return'
127
128echo
129echo === Shut down and check image ===
130echo
131
132_send_qemu_cmd $src '{"execute":"quit"}' 'return'
133_send_qemu_cmd $dest '{"execute":"quit"}' 'return'
134wait=1 _cleanup_qemu
135
136_check_test_img
137TEST_IMG="${TEST_IMG}.dest" _check_test_img
138
139$QEMU_IO -c 'write -P 0x66 1M 64k' "$TEST_IMG" | _filter_qemu_io
140$QEMU_IMG compare "$TEST_IMG.dest" "$TEST_IMG"
141
142# success, all done
143echo "*** done"
144rm -f $seq.full
145status=0
146