xref: /openbmc/qemu/tests/qemu-iotests/085 (revision 68894b5fed671d49587209697f2224b4e857fd1a)
111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw
34089f7c6SJeff Cody#
44089f7c6SJeff Cody# Live snapshot tests
54089f7c6SJeff Cody#
64089f7c6SJeff Cody# This tests live snapshots of images on a running QEMU instance, using
74089f7c6SJeff Cody# QMP commands.  Both single disk snapshots, and transactional group
84089f7c6SJeff Cody# snapshots are performed.
94089f7c6SJeff Cody#
104089f7c6SJeff Cody# Copyright (C) 2014 Red Hat, Inc.
1189e3a2d8SAlberto Garcia# Copyright (C) 2015 Igalia, S.L.
124089f7c6SJeff Cody#
134089f7c6SJeff Cody# This program is free software; you can redistribute it and/or modify
144089f7c6SJeff Cody# it under the terms of the GNU General Public License as published by
154089f7c6SJeff Cody# the Free Software Foundation; either version 2 of the License, or
164089f7c6SJeff Cody# (at your option) any later version.
174089f7c6SJeff Cody#
184089f7c6SJeff Cody# This program is distributed in the hope that it will be useful,
194089f7c6SJeff Cody# but WITHOUT ANY WARRANTY; without even the implied warranty of
204089f7c6SJeff Cody# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
214089f7c6SJeff Cody# GNU General Public License for more details.
224089f7c6SJeff Cody#
234089f7c6SJeff Cody# You should have received a copy of the GNU General Public License
244089f7c6SJeff Cody# along with this program.  If not, see <http://www.gnu.org/licenses/>.
254089f7c6SJeff Cody#
264089f7c6SJeff Cody
274089f7c6SJeff Cody# creator
28*42a5009dSJohn Snowowner=codyprime@gmail.com
294089f7c6SJeff Cody
304089f7c6SJeff Codyseq=`basename $0`
314089f7c6SJeff Codyecho "QA output created by $seq"
324089f7c6SJeff Cody
334089f7c6SJeff Codystatus=1	# failure is the default!
344089f7c6SJeff Cody
354089f7c6SJeff Codysnapshot_virt0="snapshot-v0.qcow2"
364089f7c6SJeff Codysnapshot_virt1="snapshot-v1.qcow2"
374089f7c6SJeff Cody
3889e3a2d8SAlberto GarciaSNAPSHOTS=10
394089f7c6SJeff Cody
404089f7c6SJeff Cody_cleanup()
414089f7c6SJeff Cody{
42e86e8697SJeff Cody    _cleanup_qemu
43801ddbdaSMax Reitz    _cleanup_test_img
4489e3a2d8SAlberto Garcia    for i in $(seq 1 ${SNAPSHOTS})
454089f7c6SJeff Cody    do
46f91ecbd7SMax Reitz        _rm_test_img "${TEST_DIR}/${i}-${snapshot_virt0}"
47f91ecbd7SMax Reitz        _rm_test_img "${TEST_DIR}/${i}-${snapshot_virt1}"
484089f7c6SJeff Cody    done
49f91ecbd7SMax Reitz    for img in "${TEST_IMG}".{1,2,base}
50f91ecbd7SMax Reitz    do
51f91ecbd7SMax Reitz        _rm_test_img "$img"
52f91ecbd7SMax Reitz    done
534089f7c6SJeff Cody
544089f7c6SJeff Cody}
554089f7c6SJeff Codytrap "_cleanup; exit \$status" 0 1 2 3 15
564089f7c6SJeff Cody
574089f7c6SJeff Cody# get standard environment, filters and checks
584089f7c6SJeff Cody. ./common.rc
594089f7c6SJeff Cody. ./common.filter
60e86e8697SJeff Cody. ./common.qemu
614089f7c6SJeff Cody
624089f7c6SJeff Cody_supported_fmt qcow2
634089f7c6SJeff Cody_supported_proto file
644089f7c6SJeff Cody
654089f7c6SJeff Cody
664089f7c6SJeff Cody# ${1}: unique identifier for the snapshot filename
678cedcffdSEric Blakecreate_single_snapshot()
684089f7c6SJeff Cody{
694089f7c6SJeff Cody    cmd="{ 'execute': 'blockdev-snapshot-sync',
704089f7c6SJeff Cody                      'arguments': { 'device': 'virtio0',
71f2d7f16fSAlberto Garcia                                     'snapshot-file':'${TEST_DIR}/${1}-${snapshot_virt0}',
724089f7c6SJeff Cody                                     'format': 'qcow2' } }"
73e86e8697SJeff Cody    _send_qemu_cmd $h "${cmd}" "return"
744089f7c6SJeff Cody}
754089f7c6SJeff Cody
764089f7c6SJeff Cody# ${1}: unique identifier for the snapshot filename
778cedcffdSEric Blakecreate_group_snapshot()
784089f7c6SJeff Cody{
794089f7c6SJeff Cody    cmd="{ 'execute': 'transaction', 'arguments':
804089f7c6SJeff Cody           {'actions': [
814089f7c6SJeff Cody               { 'type': 'blockdev-snapshot-sync', 'data' :
824089f7c6SJeff Cody                   { 'device': 'virtio0',
83f2d7f16fSAlberto Garcia                      'snapshot-file': '${TEST_DIR}/${1}-${snapshot_virt0}' } },
844089f7c6SJeff Cody               { 'type': 'blockdev-snapshot-sync', 'data' :
854089f7c6SJeff Cody                   { 'device': 'virtio1',
86f2d7f16fSAlberto Garcia                       'snapshot-file': '${TEST_DIR}/${1}-${snapshot_virt1}' } } ]
874089f7c6SJeff Cody             } }"
884089f7c6SJeff Cody
89e86e8697SJeff Cody    _send_qemu_cmd $h "${cmd}" "return"
904089f7c6SJeff Cody}
914089f7c6SJeff Cody
9289e3a2d8SAlberto Garcia# ${1}: unique identifier for the snapshot filename
93ecffa634SFam Zheng# ${2}: extra_params to the blockdev-add command
94ecffa634SFam Zheng# ${3}: filename
958cedcffdSEric Blakedo_blockdev_add()
96ecffa634SFam Zheng{
97ecffa634SFam Zheng    cmd="{ 'execute': 'blockdev-add', 'arguments':
98ecffa634SFam Zheng           { 'driver': 'qcow2', 'node-name': 'snap_${1}', ${2}
99ecffa634SFam Zheng             'file':
100ecffa634SFam Zheng             { 'driver': 'file', 'filename': '${3}',
101ecffa634SFam Zheng               'node-name': 'file_${1}' } } }"
102ecffa634SFam Zheng    _send_qemu_cmd $h "${cmd}" "return"
103ecffa634SFam Zheng}
104ecffa634SFam Zheng
105ecffa634SFam Zheng# ${1}: unique identifier for the snapshot filename
106bfb8aa6dSKevin Wolfcreate_snapshot_image()
10789e3a2d8SAlberto Garcia{
10889e3a2d8SAlberto Garcia    base_image="${TEST_DIR}/$((${1}-1))-${snapshot_virt0}"
10989e3a2d8SAlberto Garcia    snapshot_file="${TEST_DIR}/${1}-${snapshot_virt0}"
110b66ff2c2SEric Blake    TEST_IMG=$snapshot_file _make_test_img -u -b "${base_image}" -F $IMGFMT "$size"
111bfb8aa6dSKevin Wolf}
112bfb8aa6dSKevin Wolf
113bfb8aa6dSKevin Wolf# ${1}: unique identifier for the snapshot filename
114bfb8aa6dSKevin Wolfadd_snapshot_image()
115bfb8aa6dSKevin Wolf{
116bfb8aa6dSKevin Wolf    snapshot_file="${TEST_DIR}/${1}-${snapshot_virt0}"
117bfb8aa6dSKevin Wolf    create_snapshot_image "$1"
118c42e8742SMarkus Armbruster    do_blockdev_add "$1" "'backing': null, " "${snapshot_file}"
11989e3a2d8SAlberto Garcia}
12089e3a2d8SAlberto Garcia
12189e3a2d8SAlberto Garcia# ${1}: unique identifier for the snapshot filename
12289e3a2d8SAlberto Garcia# ${2}: expected response, defaults to 'return'
1238cedcffdSEric Blakeblockdev_snapshot()
12489e3a2d8SAlberto Garcia{
12589e3a2d8SAlberto Garcia    cmd="{ 'execute': 'blockdev-snapshot',
12689e3a2d8SAlberto Garcia                      'arguments': { 'node': 'virtio0',
127f2d7f16fSAlberto Garcia                                     'overlay':'snap_${1}' } }"
12889e3a2d8SAlberto Garcia    _send_qemu_cmd $h "${cmd}" "${2:-return}"
12989e3a2d8SAlberto Garcia}
13089e3a2d8SAlberto Garcia
1314089f7c6SJeff Codysize=128M
1324089f7c6SJeff Cody
133d88bef19SMax ReitzTEST_IMG="$TEST_IMG.1" _make_test_img $size
134d88bef19SMax ReitzTEST_IMG="$TEST_IMG.2" _make_test_img $size
1354089f7c6SJeff Cody
1364089f7c6SJeff Codyecho
1374089f7c6SJeff Codyecho === Running QEMU ===
1384089f7c6SJeff Codyecho
1394089f7c6SJeff Cody
140e86e8697SJeff Codyqemu_comm_method="qmp"
14189e3a2d8SAlberto Garcia_launch_qemu -drive file="${TEST_IMG}.1",if=virtio -drive file="${TEST_IMG}.2",if=virtio
142e86e8697SJeff Codyh=$QEMU_HANDLE
1434089f7c6SJeff Cody
1444089f7c6SJeff Codyecho
1454089f7c6SJeff Codyecho === Sending capabilities ===
1464089f7c6SJeff Codyecho
1474089f7c6SJeff Cody
148e86e8697SJeff Cody_send_qemu_cmd $h "{ 'execute': 'qmp_capabilities' }" "return"
1494089f7c6SJeff Cody
15089e3a2d8SAlberto Garcia# Tests for the blockdev-snapshot-sync command
15189e3a2d8SAlberto Garcia
1524089f7c6SJeff Codyecho
1534089f7c6SJeff Codyecho === Create a single snapshot on virtio0 ===
1544089f7c6SJeff Codyecho
1554089f7c6SJeff Cody
1564089f7c6SJeff Codycreate_single_snapshot 1
1574089f7c6SJeff Cody
1584089f7c6SJeff Cody
1594089f7c6SJeff Codyecho
1604089f7c6SJeff Codyecho === Invalid command - missing device and nodename ===
1614089f7c6SJeff Codyecho
1624089f7c6SJeff Cody
163e86e8697SJeff Cody_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot-sync',
164f2d7f16fSAlberto Garcia                         'arguments': { 'snapshot-file':'${TEST_DIR}/1-${snapshot_virt0}',
1654089f7c6SJeff Cody                                     'format': 'qcow2' } }" "error"
1664089f7c6SJeff Cody
1674089f7c6SJeff Codyecho
1684089f7c6SJeff Codyecho === Invalid command - missing snapshot-file ===
1694089f7c6SJeff Codyecho
1704089f7c6SJeff Cody
171e86e8697SJeff Cody_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot-sync',
1724089f7c6SJeff Cody                         'arguments': { 'device': 'virtio0',
1734089f7c6SJeff Cody                                     'format': 'qcow2' } }" "error"
1744089f7c6SJeff Codyecho
1754089f7c6SJeff Codyecho
1764089f7c6SJeff Codyecho === Create several transactional group snapshots ===
1774089f7c6SJeff Codyecho
1784089f7c6SJeff Cody
17989e3a2d8SAlberto Garciafor i in $(seq 2 ${SNAPSHOTS})
1804089f7c6SJeff Codydo
1814089f7c6SJeff Cody    create_group_snapshot ${i}
1824089f7c6SJeff Codydone
1834089f7c6SJeff Cody
18489e3a2d8SAlberto Garcia# Tests for the blockdev-snapshot command
18589e3a2d8SAlberto Garcia
18689e3a2d8SAlberto Garciaecho
18789e3a2d8SAlberto Garciaecho === Create a couple of snapshots using blockdev-snapshot ===
18889e3a2d8SAlberto Garciaecho
18989e3a2d8SAlberto Garcia
19089e3a2d8SAlberto GarciaSNAPSHOTS=$((${SNAPSHOTS}+1))
19189e3a2d8SAlberto Garciaadd_snapshot_image ${SNAPSHOTS}
19289e3a2d8SAlberto Garciablockdev_snapshot ${SNAPSHOTS}
19389e3a2d8SAlberto Garcia
19489e3a2d8SAlberto GarciaSNAPSHOTS=$((${SNAPSHOTS}+1))
19589e3a2d8SAlberto Garciaadd_snapshot_image ${SNAPSHOTS}
19689e3a2d8SAlberto Garciablockdev_snapshot ${SNAPSHOTS}
19789e3a2d8SAlberto Garcia
19889e3a2d8SAlberto Garciaecho
1993fa123d0SAlberto Garciaecho === Invalid command - cannot create a snapshot using a file BDS ===
2003fa123d0SAlberto Garciaecho
2013fa123d0SAlberto Garcia
2023fa123d0SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
2033fa123d0SAlberto Garcia                     'arguments': { 'node':'virtio0',
2043fa123d0SAlberto Garcia                                    'overlay':'file_${SNAPSHOTS}' }
2053fa123d0SAlberto Garcia                   }" "error"
2063fa123d0SAlberto Garcia
2073fa123d0SAlberto Garciaecho
20889e3a2d8SAlberto Garciaecho === Invalid command - snapshot node used as active layer ===
20989e3a2d8SAlberto Garciaecho
21089e3a2d8SAlberto Garcia
21189e3a2d8SAlberto Garciablockdev_snapshot ${SNAPSHOTS} error
21289e3a2d8SAlberto Garcia
21389e3a2d8SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
21489e3a2d8SAlberto Garcia                     'arguments': { 'node':'virtio0',
21589e3a2d8SAlberto Garcia                                    'overlay':'virtio0' }
21689e3a2d8SAlberto Garcia                   }" "error"
21789e3a2d8SAlberto Garcia
21889e3a2d8SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
21989e3a2d8SAlberto Garcia                     'arguments': { 'node':'virtio0',
22089e3a2d8SAlberto Garcia                                    'overlay':'virtio1' }
22189e3a2d8SAlberto Garcia                   }" "error"
22289e3a2d8SAlberto Garcia
22389e3a2d8SAlberto Garciaecho
22489e3a2d8SAlberto Garciaecho === Invalid command - snapshot node used as backing hd ===
22589e3a2d8SAlberto Garciaecho
22689e3a2d8SAlberto Garcia
22789e3a2d8SAlberto Garciablockdev_snapshot $((${SNAPSHOTS}-1)) error
22889e3a2d8SAlberto Garcia
22989e3a2d8SAlberto Garciaecho
23089e3a2d8SAlberto Garciaecho === Invalid command - snapshot node has a backing image ===
23189e3a2d8SAlberto Garciaecho
23289e3a2d8SAlberto Garcia
23389e3a2d8SAlberto GarciaSNAPSHOTS=$((${SNAPSHOTS}+1))
234ecffa634SFam Zheng
235ecffa634SFam ZhengTEST_IMG="$TEST_IMG.base" _make_test_img "$size"
236b66ff2c2SEric Blake_make_test_img -b "${TEST_IMG}.base" -F $IMGFMT "$size"
237ecffa634SFam Zhengdo_blockdev_add ${SNAPSHOTS} "" "${TEST_IMG}"
23889e3a2d8SAlberto Garciablockdev_snapshot ${SNAPSHOTS} error
23989e3a2d8SAlberto Garcia
24089e3a2d8SAlberto Garciaecho
241bfb8aa6dSKevin Wolfecho === Invalid command - creating loops ===
242bfb8aa6dSKevin Wolfecho
243bfb8aa6dSKevin Wolf
244bfb8aa6dSKevin WolfSNAPSHOTS=$((${SNAPSHOTS}+1))
245bfb8aa6dSKevin Wolfadd_snapshot_image ${SNAPSHOTS}
246bfb8aa6dSKevin Wolf
247bfb8aa6dSKevin Wolf_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
248bfb8aa6dSKevin Wolf                     'arguments': { 'node':'snap_${SNAPSHOTS}',
249bfb8aa6dSKevin Wolf                                    'overlay':'snap_${SNAPSHOTS}' }
250bfb8aa6dSKevin Wolf                   }" "error"
251bfb8aa6dSKevin Wolf
252bfb8aa6dSKevin WolfSNAPSHOTS=$((${SNAPSHOTS}+1))
253bfb8aa6dSKevin Wolfcreate_snapshot_image ${SNAPSHOTS}
254bfb8aa6dSKevin Wolfdo_blockdev_add ${SNAPSHOTS} "'backing': 'snap_$((${SNAPSHOTS}-1))', " \
255bfb8aa6dSKevin Wolf    "${TEST_DIR}/${SNAPSHOTS}-${snapshot_virt0}"
256bfb8aa6dSKevin Wolf
257bfb8aa6dSKevin Wolf_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
258bfb8aa6dSKevin Wolf                     'arguments': { 'node':'snap_${SNAPSHOTS}',
259bfb8aa6dSKevin Wolf                                    'overlay':'snap_$((${SNAPSHOTS}-1))' }
260bfb8aa6dSKevin Wolf                   }" "error"
261bfb8aa6dSKevin Wolf
262bfb8aa6dSKevin Wolfecho
26389e3a2d8SAlberto Garciaecho === Invalid command - The node does not exist ===
26489e3a2d8SAlberto Garciaecho
26589e3a2d8SAlberto Garcia
26689e3a2d8SAlberto Garciablockdev_snapshot $((${SNAPSHOTS}+1)) error
26789e3a2d8SAlberto Garcia
26889e3a2d8SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot',
26989e3a2d8SAlberto Garcia                     'arguments': { 'node':'nodevice',
270f2d7f16fSAlberto Garcia                                    'overlay':'snap_${SNAPSHOTS}' }
27189e3a2d8SAlberto Garcia                   }" "error"
27289e3a2d8SAlberto Garcia
2734089f7c6SJeff Cody# success, all done
2744089f7c6SJeff Codyecho "*** done"
2754089f7c6SJeff Codyrm -f $seq.full
2764089f7c6SJeff Codystatus=0
277