xref: /openbmc/qemu/tests/qemu-iotests/085 (revision 0ce46ab5)
1#!/usr/bin/env bash
2#
3# Live snapshot tests
4#
5# This tests live snapshots of images on a running QEMU instance, using
6# QMP commands.  Both single disk snapshots, and transactional group
7# snapshots are performed.
8#
9# Copyright (C) 2014 Red Hat, Inc.
10# Copyright (C) 2015 Igalia, S.L.
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24#
25
26# creator
27owner=jcody@redhat.com
28
29seq=`basename $0`
30echo "QA output created by $seq"
31
32status=1	# failure is the default!
33
34snapshot_virt0="snapshot-v0.qcow2"
35snapshot_virt1="snapshot-v1.qcow2"
36
37SNAPSHOTS=10
38
39_cleanup()
40{
41    _cleanup_qemu
42    for i in $(seq 1 ${SNAPSHOTS})
43    do
44        _rm_test_img "${TEST_DIR}/${i}-${snapshot_virt0}"
45        _rm_test_img "${TEST_DIR}/${i}-${snapshot_virt1}"
46    done
47    for img in "${TEST_IMG}".{1,2,base}
48    do
49        _rm_test_img "$img"
50    done
51
52}
53trap "_cleanup; exit \$status" 0 1 2 3 15
54
55# get standard environment, filters and checks
56. ./common.rc
57. ./common.filter
58. ./common.qemu
59
60_supported_fmt qcow2
61_supported_proto file
62
63
64# ${1}: unique identifier for the snapshot filename
65create_single_snapshot()
66{
67    cmd="{ 'execute': 'blockdev-snapshot-sync',
68                      'arguments': { 'device': 'virtio0',
69                                     'snapshot-file':'${TEST_DIR}/${1}-${snapshot_virt0}',
70                                     'format': 'qcow2' } }"
71    _send_qemu_cmd $h "${cmd}" "return"
72}
73
74# ${1}: unique identifier for the snapshot filename
75create_group_snapshot()
76{
77    cmd="{ 'execute': 'transaction', 'arguments':
78           {'actions': [
79               { 'type': 'blockdev-snapshot-sync', 'data' :
80                   { 'device': 'virtio0',
81                      'snapshot-file': '${TEST_DIR}/${1}-${snapshot_virt0}' } },
82               { 'type': 'blockdev-snapshot-sync', 'data' :
83                   { 'device': 'virtio1',
84                       'snapshot-file': '${TEST_DIR}/${1}-${snapshot_virt1}' } } ]
85             } }"
86
87    _send_qemu_cmd $h "${cmd}" "return"
88}
89
90# ${1}: unique identifier for the snapshot filename
91# ${2}: extra_params to the blockdev-add command
92# ${3}: filename
93do_blockdev_add()
94{
95    cmd="{ 'execute': 'blockdev-add', 'arguments':
96           { 'driver': 'qcow2', 'node-name': 'snap_${1}', ${2}
97             'file':
98             { 'driver': 'file', 'filename': '${3}',
99               'node-name': 'file_${1}' } } }"
100    _send_qemu_cmd $h "${cmd}" "return"
101}
102
103# ${1}: unique identifier for the snapshot filename
104add_snapshot_image()
105{
106    base_image="${TEST_DIR}/$((${1}-1))-${snapshot_virt0}"
107    snapshot_file="${TEST_DIR}/${1}-${snapshot_virt0}"
108    TEST_IMG=$snapshot_file _make_test_img -u -b "${base_image}" "$size"
109    do_blockdev_add "$1" "'backing': null, " "${snapshot_file}"
110}
111
112# ${1}: unique identifier for the snapshot filename
113# ${2}: expected response, defaults to 'return'
114blockdev_snapshot()
115{
116    cmd="{ 'execute': 'blockdev-snapshot',
117                      'arguments': { 'node': 'virtio0',
118                                     'overlay':'snap_${1}' } }"
119    _send_qemu_cmd $h "${cmd}" "${2:-return}"
120}
121
122size=128M
123
124TEST_IMG="$TEST_IMG.1" _make_test_img $size
125TEST_IMG="$TEST_IMG.2" _make_test_img $size
126
127echo
128echo === Running QEMU ===
129echo
130
131qemu_comm_method="qmp"
132_launch_qemu -drive file="${TEST_IMG}.1",if=virtio -drive file="${TEST_IMG}.2",if=virtio
133h=$QEMU_HANDLE
134
135echo
136echo === Sending capabilities ===
137echo
138
139_send_qemu_cmd $h "{ 'execute': 'qmp_capabilities' }" "return"
140
141# Tests for the blockdev-snapshot-sync command
142
143echo
144echo === Create a single snapshot on virtio0 ===
145echo
146
147create_single_snapshot 1
148
149
150echo
151echo === Invalid command - missing device and nodename ===
152echo
153
154_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot-sync',
155                         'arguments': { 'snapshot-file':'${TEST_DIR}/1-${snapshot_virt0}',
156                                     'format': 'qcow2' } }" "error"
157
158echo
159echo === Invalid command - missing snapshot-file ===
160echo
161
162_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot-sync',
163                         'arguments': { 'device': 'virtio0',
164                                     'format': 'qcow2' } }" "error"
165echo
166echo
167echo === Create several transactional group snapshots ===
168echo
169
170for i in $(seq 2 ${SNAPSHOTS})
171do
172    create_group_snapshot ${i}
173done
174
175# Tests for the blockdev-snapshot command
176
177echo
178echo === Create a couple of snapshots using blockdev-snapshot ===
179echo
180
181SNAPSHOTS=$((${SNAPSHOTS}+1))
182add_snapshot_image ${SNAPSHOTS}
183blockdev_snapshot ${SNAPSHOTS}
184
185SNAPSHOTS=$((${SNAPSHOTS}+1))
186add_snapshot_image ${SNAPSHOTS}
187blockdev_snapshot ${SNAPSHOTS}
188
189echo
190echo === Invalid command - cannot create a snapshot using a file BDS ===
191echo
192
193_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
194                     'arguments': { 'node':'virtio0',
195                                    'overlay':'file_${SNAPSHOTS}' }
196                   }" "error"
197
198echo
199echo === Invalid command - snapshot node used as active layer ===
200echo
201
202blockdev_snapshot ${SNAPSHOTS} error
203
204_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
205                     'arguments': { 'node':'virtio0',
206                                    'overlay':'virtio0' }
207                   }" "error"
208
209_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
210                     'arguments': { 'node':'virtio0',
211                                    'overlay':'virtio1' }
212                   }" "error"
213
214echo
215echo === Invalid command - snapshot node used as backing hd ===
216echo
217
218blockdev_snapshot $((${SNAPSHOTS}-1)) error
219
220echo
221echo === Invalid command - snapshot node has a backing image ===
222echo
223
224SNAPSHOTS=$((${SNAPSHOTS}+1))
225
226TEST_IMG="$TEST_IMG.base" _make_test_img "$size"
227_make_test_img -b "${TEST_IMG}.base" "$size"
228do_blockdev_add ${SNAPSHOTS} "" "${TEST_IMG}"
229blockdev_snapshot ${SNAPSHOTS} error
230
231echo
232echo === Invalid command - The node does not exist ===
233echo
234
235blockdev_snapshot $((${SNAPSHOTS}+1)) error
236
237_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
238                     'arguments': { 'node':'nodevice',
239                                    'overlay':'snap_${SNAPSHOTS}' }
240                   }" "error"
241
242# success, all done
243echo "*** done"
244rm -f $seq.full
245status=0
246