1*11a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 24089f7c6SJeff Cody# 34089f7c6SJeff Cody# Live snapshot tests 44089f7c6SJeff Cody# 54089f7c6SJeff Cody# This tests live snapshots of images on a running QEMU instance, using 64089f7c6SJeff Cody# QMP commands. Both single disk snapshots, and transactional group 74089f7c6SJeff Cody# snapshots are performed. 84089f7c6SJeff Cody# 94089f7c6SJeff Cody# Copyright (C) 2014 Red Hat, Inc. 1089e3a2d8SAlberto Garcia# Copyright (C) 2015 Igalia, S.L. 114089f7c6SJeff Cody# 124089f7c6SJeff Cody# This program is free software; you can redistribute it and/or modify 134089f7c6SJeff Cody# it under the terms of the GNU General Public License as published by 144089f7c6SJeff Cody# the Free Software Foundation; either version 2 of the License, or 154089f7c6SJeff Cody# (at your option) any later version. 164089f7c6SJeff Cody# 174089f7c6SJeff Cody# This program is distributed in the hope that it will be useful, 184089f7c6SJeff Cody# but WITHOUT ANY WARRANTY; without even the implied warranty of 194089f7c6SJeff Cody# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 204089f7c6SJeff Cody# GNU General Public License for more details. 214089f7c6SJeff Cody# 224089f7c6SJeff Cody# You should have received a copy of the GNU General Public License 234089f7c6SJeff Cody# along with this program. If not, see <http://www.gnu.org/licenses/>. 244089f7c6SJeff Cody# 254089f7c6SJeff Cody 264089f7c6SJeff Cody# creator 274089f7c6SJeff Codyowner=jcody@redhat.com 284089f7c6SJeff Cody 294089f7c6SJeff Codyseq=`basename $0` 304089f7c6SJeff Codyecho "QA output created by $seq" 314089f7c6SJeff Cody 324089f7c6SJeff Codystatus=1 # failure is the default! 334089f7c6SJeff Cody 344089f7c6SJeff Codysnapshot_virt0="snapshot-v0.qcow2" 354089f7c6SJeff Codysnapshot_virt1="snapshot-v1.qcow2" 364089f7c6SJeff Cody 3789e3a2d8SAlberto GarciaSNAPSHOTS=10 384089f7c6SJeff Cody 394089f7c6SJeff Cody_cleanup() 404089f7c6SJeff Cody{ 41e86e8697SJeff Cody _cleanup_qemu 4289e3a2d8SAlberto Garcia for i in $(seq 1 ${SNAPSHOTS}) 434089f7c6SJeff Cody do 444089f7c6SJeff Cody rm -f "${TEST_DIR}/${i}-${snapshot_virt0}" 454089f7c6SJeff Cody rm -f "${TEST_DIR}/${i}-${snapshot_virt1}" 464089f7c6SJeff Cody done 47ecffa634SFam Zheng rm -f "${TEST_IMG}" "${TEST_IMG}.1" "${TEST_IMG}.2" "${TEST_IMG}.base" 484089f7c6SJeff Cody 494089f7c6SJeff Cody} 504089f7c6SJeff Codytrap "_cleanup; exit \$status" 0 1 2 3 15 514089f7c6SJeff Cody 524089f7c6SJeff Cody# get standard environment, filters and checks 534089f7c6SJeff Cody. ./common.rc 544089f7c6SJeff Cody. ./common.filter 55e86e8697SJeff Cody. ./common.qemu 564089f7c6SJeff Cody 574089f7c6SJeff Cody_supported_fmt qcow2 584089f7c6SJeff Cody_supported_proto file 594089f7c6SJeff Cody_supported_os Linux 604089f7c6SJeff Cody 614089f7c6SJeff Cody 624089f7c6SJeff Cody# ${1}: unique identifier for the snapshot filename 638cedcffdSEric Blakecreate_single_snapshot() 644089f7c6SJeff Cody{ 654089f7c6SJeff Cody cmd="{ 'execute': 'blockdev-snapshot-sync', 664089f7c6SJeff Cody 'arguments': { 'device': 'virtio0', 67f2d7f16fSAlberto Garcia 'snapshot-file':'${TEST_DIR}/${1}-${snapshot_virt0}', 684089f7c6SJeff Cody 'format': 'qcow2' } }" 69e86e8697SJeff Cody _send_qemu_cmd $h "${cmd}" "return" 704089f7c6SJeff Cody} 714089f7c6SJeff Cody 724089f7c6SJeff Cody# ${1}: unique identifier for the snapshot filename 738cedcffdSEric Blakecreate_group_snapshot() 744089f7c6SJeff Cody{ 754089f7c6SJeff Cody cmd="{ 'execute': 'transaction', 'arguments': 764089f7c6SJeff Cody {'actions': [ 774089f7c6SJeff Cody { 'type': 'blockdev-snapshot-sync', 'data' : 784089f7c6SJeff Cody { 'device': 'virtio0', 79f2d7f16fSAlberto Garcia 'snapshot-file': '${TEST_DIR}/${1}-${snapshot_virt0}' } }, 804089f7c6SJeff Cody { 'type': 'blockdev-snapshot-sync', 'data' : 814089f7c6SJeff Cody { 'device': 'virtio1', 82f2d7f16fSAlberto Garcia 'snapshot-file': '${TEST_DIR}/${1}-${snapshot_virt1}' } } ] 834089f7c6SJeff Cody } }" 844089f7c6SJeff Cody 85e86e8697SJeff Cody _send_qemu_cmd $h "${cmd}" "return" 864089f7c6SJeff Cody} 874089f7c6SJeff Cody 8889e3a2d8SAlberto Garcia# ${1}: unique identifier for the snapshot filename 89ecffa634SFam Zheng# ${2}: extra_params to the blockdev-add command 90ecffa634SFam Zheng# ${3}: filename 918cedcffdSEric Blakedo_blockdev_add() 92ecffa634SFam Zheng{ 93ecffa634SFam Zheng cmd="{ 'execute': 'blockdev-add', 'arguments': 94ecffa634SFam Zheng { 'driver': 'qcow2', 'node-name': 'snap_${1}', ${2} 95ecffa634SFam Zheng 'file': 96ecffa634SFam Zheng { 'driver': 'file', 'filename': '${3}', 97ecffa634SFam Zheng 'node-name': 'file_${1}' } } }" 98ecffa634SFam Zheng _send_qemu_cmd $h "${cmd}" "return" 99ecffa634SFam Zheng} 100ecffa634SFam Zheng 101ecffa634SFam Zheng# ${1}: unique identifier for the snapshot filename 1028cedcffdSEric Blakeadd_snapshot_image() 10389e3a2d8SAlberto Garcia{ 10489e3a2d8SAlberto Garcia base_image="${TEST_DIR}/$((${1}-1))-${snapshot_virt0}" 10589e3a2d8SAlberto Garcia snapshot_file="${TEST_DIR}/${1}-${snapshot_virt0}" 1066e6e55f5SJohn Snow _make_test_img -u -b "${base_image}" "$size" 10789e3a2d8SAlberto Garcia mv "${TEST_IMG}" "${snapshot_file}" 108c42e8742SMarkus Armbruster do_blockdev_add "$1" "'backing': null, " "${snapshot_file}" 10989e3a2d8SAlberto Garcia} 11089e3a2d8SAlberto Garcia 11189e3a2d8SAlberto Garcia# ${1}: unique identifier for the snapshot filename 11289e3a2d8SAlberto Garcia# ${2}: expected response, defaults to 'return' 1138cedcffdSEric Blakeblockdev_snapshot() 11489e3a2d8SAlberto Garcia{ 11589e3a2d8SAlberto Garcia cmd="{ 'execute': 'blockdev-snapshot', 11689e3a2d8SAlberto Garcia 'arguments': { 'node': 'virtio0', 117f2d7f16fSAlberto Garcia 'overlay':'snap_${1}' } }" 11889e3a2d8SAlberto Garcia _send_qemu_cmd $h "${cmd}" "${2:-return}" 11989e3a2d8SAlberto Garcia} 12089e3a2d8SAlberto Garcia 1214089f7c6SJeff Codysize=128M 1224089f7c6SJeff Cody 1234089f7c6SJeff Cody_make_test_img $size 12489e3a2d8SAlberto Garciamv "${TEST_IMG}" "${TEST_IMG}.1" 1254089f7c6SJeff Cody_make_test_img $size 12689e3a2d8SAlberto Garciamv "${TEST_IMG}" "${TEST_IMG}.2" 1274089f7c6SJeff Cody 1284089f7c6SJeff Codyecho 1294089f7c6SJeff Codyecho === Running QEMU === 1304089f7c6SJeff Codyecho 1314089f7c6SJeff Cody 132e86e8697SJeff Codyqemu_comm_method="qmp" 13389e3a2d8SAlberto Garcia_launch_qemu -drive file="${TEST_IMG}.1",if=virtio -drive file="${TEST_IMG}.2",if=virtio 134e86e8697SJeff Codyh=$QEMU_HANDLE 1354089f7c6SJeff Cody 1364089f7c6SJeff Codyecho 1374089f7c6SJeff Codyecho === Sending capabilities === 1384089f7c6SJeff Codyecho 1394089f7c6SJeff Cody 140e86e8697SJeff Cody_send_qemu_cmd $h "{ 'execute': 'qmp_capabilities' }" "return" 1414089f7c6SJeff Cody 14289e3a2d8SAlberto Garcia# Tests for the blockdev-snapshot-sync command 14389e3a2d8SAlberto Garcia 1444089f7c6SJeff Codyecho 1454089f7c6SJeff Codyecho === Create a single snapshot on virtio0 === 1464089f7c6SJeff Codyecho 1474089f7c6SJeff Cody 1484089f7c6SJeff Codycreate_single_snapshot 1 1494089f7c6SJeff Cody 1504089f7c6SJeff Cody 1514089f7c6SJeff Codyecho 1524089f7c6SJeff Codyecho === Invalid command - missing device and nodename === 1534089f7c6SJeff Codyecho 1544089f7c6SJeff Cody 155e86e8697SJeff Cody_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot-sync', 156f2d7f16fSAlberto Garcia 'arguments': { 'snapshot-file':'${TEST_DIR}/1-${snapshot_virt0}', 1574089f7c6SJeff Cody 'format': 'qcow2' } }" "error" 1584089f7c6SJeff Cody 1594089f7c6SJeff Codyecho 1604089f7c6SJeff Codyecho === Invalid command - missing snapshot-file === 1614089f7c6SJeff Codyecho 1624089f7c6SJeff Cody 163e86e8697SJeff Cody_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot-sync', 1644089f7c6SJeff Cody 'arguments': { 'device': 'virtio0', 1654089f7c6SJeff Cody 'format': 'qcow2' } }" "error" 1664089f7c6SJeff Codyecho 1674089f7c6SJeff Codyecho 1684089f7c6SJeff Codyecho === Create several transactional group snapshots === 1694089f7c6SJeff Codyecho 1704089f7c6SJeff Cody 17189e3a2d8SAlberto Garciafor i in $(seq 2 ${SNAPSHOTS}) 1724089f7c6SJeff Codydo 1734089f7c6SJeff Cody create_group_snapshot ${i} 1744089f7c6SJeff Codydone 1754089f7c6SJeff Cody 17689e3a2d8SAlberto Garcia# Tests for the blockdev-snapshot command 17789e3a2d8SAlberto Garcia 17889e3a2d8SAlberto Garciaecho 17989e3a2d8SAlberto Garciaecho === Create a couple of snapshots using blockdev-snapshot === 18089e3a2d8SAlberto Garciaecho 18189e3a2d8SAlberto Garcia 18289e3a2d8SAlberto GarciaSNAPSHOTS=$((${SNAPSHOTS}+1)) 18389e3a2d8SAlberto Garciaadd_snapshot_image ${SNAPSHOTS} 18489e3a2d8SAlberto Garciablockdev_snapshot ${SNAPSHOTS} 18589e3a2d8SAlberto Garcia 18689e3a2d8SAlberto GarciaSNAPSHOTS=$((${SNAPSHOTS}+1)) 18789e3a2d8SAlberto Garciaadd_snapshot_image ${SNAPSHOTS} 18889e3a2d8SAlberto Garciablockdev_snapshot ${SNAPSHOTS} 18989e3a2d8SAlberto Garcia 19089e3a2d8SAlberto Garciaecho 1913fa123d0SAlberto Garciaecho === Invalid command - cannot create a snapshot using a file BDS === 1923fa123d0SAlberto Garciaecho 1933fa123d0SAlberto Garcia 1943fa123d0SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot', 1953fa123d0SAlberto Garcia 'arguments': { 'node':'virtio0', 1963fa123d0SAlberto Garcia 'overlay':'file_${SNAPSHOTS}' } 1973fa123d0SAlberto Garcia }" "error" 1983fa123d0SAlberto Garcia 1993fa123d0SAlberto Garciaecho 20089e3a2d8SAlberto Garciaecho === Invalid command - snapshot node used as active layer === 20189e3a2d8SAlberto Garciaecho 20289e3a2d8SAlberto Garcia 20389e3a2d8SAlberto Garciablockdev_snapshot ${SNAPSHOTS} error 20489e3a2d8SAlberto Garcia 20589e3a2d8SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot', 20689e3a2d8SAlberto Garcia 'arguments': { 'node':'virtio0', 20789e3a2d8SAlberto Garcia 'overlay':'virtio0' } 20889e3a2d8SAlberto Garcia }" "error" 20989e3a2d8SAlberto Garcia 21089e3a2d8SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot', 21189e3a2d8SAlberto Garcia 'arguments': { 'node':'virtio0', 21289e3a2d8SAlberto Garcia 'overlay':'virtio1' } 21389e3a2d8SAlberto Garcia }" "error" 21489e3a2d8SAlberto Garcia 21589e3a2d8SAlberto Garciaecho 21689e3a2d8SAlberto Garciaecho === Invalid command - snapshot node used as backing hd === 21789e3a2d8SAlberto Garciaecho 21889e3a2d8SAlberto Garcia 21989e3a2d8SAlberto Garciablockdev_snapshot $((${SNAPSHOTS}-1)) error 22089e3a2d8SAlberto Garcia 22189e3a2d8SAlberto Garciaecho 22289e3a2d8SAlberto Garciaecho === Invalid command - snapshot node has a backing image === 22389e3a2d8SAlberto Garciaecho 22489e3a2d8SAlberto Garcia 22589e3a2d8SAlberto GarciaSNAPSHOTS=$((${SNAPSHOTS}+1)) 226ecffa634SFam Zheng 227ecffa634SFam ZhengTEST_IMG="$TEST_IMG.base" _make_test_img "$size" 228ecffa634SFam Zheng_make_test_img -b "${TEST_IMG}.base" "$size" 229ecffa634SFam Zhengdo_blockdev_add ${SNAPSHOTS} "" "${TEST_IMG}" 23089e3a2d8SAlberto Garciablockdev_snapshot ${SNAPSHOTS} error 23189e3a2d8SAlberto Garcia 23289e3a2d8SAlberto Garciaecho 23389e3a2d8SAlberto Garciaecho === Invalid command - The node does not exist === 23489e3a2d8SAlberto Garciaecho 23589e3a2d8SAlberto Garcia 23689e3a2d8SAlberto Garciablockdev_snapshot $((${SNAPSHOTS}+1)) error 23789e3a2d8SAlberto Garcia 23889e3a2d8SAlberto Garcia_send_qemu_cmd $h "{ 'execute': 'blockdev-snapshot', 23989e3a2d8SAlberto Garcia 'arguments': { 'node':'nodevice', 240f2d7f16fSAlberto Garcia 'overlay':'snap_${SNAPSHOTS}' } 24189e3a2d8SAlberto Garcia }" "error" 24289e3a2d8SAlberto Garcia 2434089f7c6SJeff Cody# success, all done 2444089f7c6SJeff Codyecho "*** done" 2454089f7c6SJeff Codyrm -f $seq.full 2464089f7c6SJeff Codystatus=0 247