19c468a01SWenchao Xia#!/bin/bash 29c468a01SWenchao Xia# 3f33d2873SWenchao Xia# Test export internal snapshot by qemu-nbd, convert it by qemu-img. 49c468a01SWenchao Xia# 59c468a01SWenchao Xia# Copyright (C) 2013 IBM, Inc. 69c468a01SWenchao Xia# 79c468a01SWenchao Xia# Based on 029. 89c468a01SWenchao Xia# 99c468a01SWenchao Xia# This program is free software; you can redistribute it and/or modify 109c468a01SWenchao Xia# it under the terms of the GNU General Public License as published by 119c468a01SWenchao Xia# the Free Software Foundation; either version 2 of the License, or 129c468a01SWenchao Xia# (at your option) any later version. 139c468a01SWenchao Xia# 149c468a01SWenchao Xia# This program is distributed in the hope that it will be useful, 159c468a01SWenchao Xia# but WITHOUT ANY WARRANTY; without even the implied warranty of 169c468a01SWenchao Xia# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 179c468a01SWenchao Xia# GNU General Public License for more details. 189c468a01SWenchao Xia# 199c468a01SWenchao Xia# You should have received a copy of the GNU General Public License 209c468a01SWenchao Xia# along with this program. If not, see <http://www.gnu.org/licenses/>. 219c468a01SWenchao Xia# 229c468a01SWenchao Xia 239c468a01SWenchao Xia# creator 249c468a01SWenchao Xiaowner=xiawenc@linux.vnet.ibm.com 259c468a01SWenchao Xia 269c468a01SWenchao Xiaseq=`basename $0` 279c468a01SWenchao Xiaecho "QA output created by $seq" 289c468a01SWenchao Xia 299c468a01SWenchao Xiahere=`pwd` 309c468a01SWenchao Xiastatus=1 # failure is the default! 319c468a01SWenchao Xia 329c468a01SWenchao Xianbd_unix_socket=$TEST_DIR/test_qemu_nbd_socket 339c468a01SWenchao Xianbd_snapshot_img="nbd:unix:$nbd_unix_socket" 34f6c8c2e0SJeff Codyrm -f "${TEST_DIR}/qemu-nbd.pid" 359c468a01SWenchao Xia 369c468a01SWenchao Xia_cleanup_nbd() 379c468a01SWenchao Xia{ 38f6c8c2e0SJeff Cody local NBD_SNAPSHOT_PID 39f6c8c2e0SJeff Cody if [ -f "${TEST_DIR}/qemu-nbd.pid" ]; then 40f6c8c2e0SJeff Cody read NBD_SNAPSHOT_PID < "${TEST_DIR}/qemu-nbd.pid" 41f6c8c2e0SJeff Cody rm -f "${TEST_DIR}/qemu-nbd.pid" 429c468a01SWenchao Xia if [ -n "$NBD_SNAPSHOT_PID" ]; then 439c468a01SWenchao Xia kill "$NBD_SNAPSHOT_PID" 449c468a01SWenchao Xia fi 45f6c8c2e0SJeff Cody fi 469c468a01SWenchao Xia rm -f "$nbd_unix_socket" 479c468a01SWenchao Xia} 489c468a01SWenchao Xia 499c468a01SWenchao Xia_wait_for_nbd() 509c468a01SWenchao Xia{ 519c468a01SWenchao Xia for ((i = 0; i < 300; i++)) 529c468a01SWenchao Xia do 539c468a01SWenchao Xia if [ -r "$nbd_unix_socket" ]; then 549c468a01SWenchao Xia return 559c468a01SWenchao Xia fi 569c468a01SWenchao Xia sleep 0.1 579c468a01SWenchao Xia done 589c468a01SWenchao Xia echo "Failed in check of unix socket created by qemu-nbd" 599c468a01SWenchao Xia exit 1 609c468a01SWenchao Xia} 619c468a01SWenchao Xia 62f33d2873SWenchao Xiaconverted_image=$TEST_IMG.converted 63f33d2873SWenchao Xia 649c468a01SWenchao Xia_export_nbd_snapshot() 659c468a01SWenchao Xia{ 669c468a01SWenchao Xia _cleanup_nbd 679c468a01SWenchao Xia $QEMU_NBD -v -t -k "$nbd_unix_socket" "$TEST_IMG" -l $1 & 689c468a01SWenchao Xia _wait_for_nbd 699c468a01SWenchao Xia} 709c468a01SWenchao Xia 719c468a01SWenchao Xia_export_nbd_snapshot1() 729c468a01SWenchao Xia{ 739c468a01SWenchao Xia _cleanup_nbd 749c468a01SWenchao Xia $QEMU_NBD -v -t -k "$nbd_unix_socket" "$TEST_IMG" -l snapshot.name=$1 & 759c468a01SWenchao Xia _wait_for_nbd 769c468a01SWenchao Xia} 779c468a01SWenchao Xia 789c468a01SWenchao Xia_cleanup() 799c468a01SWenchao Xia{ 809c468a01SWenchao Xia _cleanup_nbd 819c468a01SWenchao Xia _cleanup_test_img 82f33d2873SWenchao Xia rm -f "$converted_image" 839c468a01SWenchao Xia} 849c468a01SWenchao Xiatrap "_cleanup; exit \$status" 0 1 2 3 15 859c468a01SWenchao Xia 869c468a01SWenchao Xia# get standard environment, filters and checks 879c468a01SWenchao Xia. ./common.rc 889c468a01SWenchao Xia. ./common.filter 899c468a01SWenchao Xia. ./common.pattern 909c468a01SWenchao Xia 919c468a01SWenchao Xia_supported_fmt qcow2 929c468a01SWenchao Xia_supported_proto file 939c8ab1aeSFam Zheng_supported_os Linux 949c468a01SWenchao Xia_require_command QEMU_NBD 955262caa7SMax Reitz# Internal snapshots are (currently) impossible with refcount_bits=1 965262caa7SMax Reitz_unsupported_imgopts 'refcount_bits=1[^0-9]' 979c468a01SWenchao Xia 988f9e835fSKevin Wolf# Use -f raw instead of -f $IMGFMT for the NBD connection 998f9e835fSKevin WolfQEMU_IO_NBD="$QEMU_IO -f raw --cache=$CACHEMODE" 1008f9e835fSKevin Wolf 1019c468a01SWenchao Xiaecho 1029c468a01SWenchao Xiaecho "== preparing image ==" 1039c468a01SWenchao Xia_make_test_img 64M 1049c468a01SWenchao Xia$QEMU_IO -c 'write -P 0xa 0x1000 0x1000' "$TEST_IMG" | _filter_qemu_io 1059c468a01SWenchao Xia$QEMU_IO -c 'write -P 0xb 0x2000 0x1000' "$TEST_IMG" | _filter_qemu_io 1069c468a01SWenchao Xia$QEMU_IMG snapshot -c sn1 "$TEST_IMG" 1079c468a01SWenchao Xia$QEMU_IO -c 'write -P 0xc 0x1000 0x1000' "$TEST_IMG" | _filter_qemu_io 1089c468a01SWenchao Xia$QEMU_IO -c 'write -P 0xd 0x2000 0x1000' "$TEST_IMG" | _filter_qemu_io 1099c468a01SWenchao Xia_check_test_img 1109c468a01SWenchao Xia 1119c468a01SWenchao Xiaecho 1129c468a01SWenchao Xiaecho "== verifying the image file with patterns ==" 1139c468a01SWenchao Xia$QEMU_IO -c 'read -P 0xc 0x1000 0x1000' "$TEST_IMG" | _filter_qemu_io 1149c468a01SWenchao Xia$QEMU_IO -c 'read -P 0xd 0x2000 0x1000' "$TEST_IMG" | _filter_qemu_io 1159c468a01SWenchao Xia 1169c468a01SWenchao Xia_export_nbd_snapshot sn1 1179c468a01SWenchao Xia 1189c468a01SWenchao Xiaecho 1199c468a01SWenchao Xiaecho "== verifying the exported snapshot with patterns, method 1 ==" 120*1104d83cSEric Blake$QEMU_IO_NBD -r -c 'read -P 0xa 0x1000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io 121*1104d83cSEric Blake$QEMU_IO_NBD -r -c 'read -P 0xb 0x2000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io 1229c468a01SWenchao Xia 1239c468a01SWenchao Xia_export_nbd_snapshot1 sn1 1249c468a01SWenchao Xia 1259c468a01SWenchao Xiaecho 1269c468a01SWenchao Xiaecho "== verifying the exported snapshot with patterns, method 2 ==" 127*1104d83cSEric Blake$QEMU_IO_NBD -r -c 'read -P 0xa 0x1000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io 128*1104d83cSEric Blake$QEMU_IO_NBD -r -c 'read -P 0xb 0x2000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io 1299c468a01SWenchao Xia 130f33d2873SWenchao Xia$QEMU_IMG convert "$TEST_IMG" -l sn1 -O qcow2 "$converted_image" 131f33d2873SWenchao Xia 132f33d2873SWenchao Xiaecho 133f33d2873SWenchao Xiaecho "== verifying the converted snapshot with patterns, method 1 ==" 134f33d2873SWenchao Xia$QEMU_IO -c 'read -P 0xa 0x1000 0x1000' "$converted_image" | _filter_qemu_io 135f33d2873SWenchao Xia$QEMU_IO -c 'read -P 0xb 0x2000 0x1000' "$converted_image" | _filter_qemu_io 136f33d2873SWenchao Xia 137f33d2873SWenchao Xia$QEMU_IMG convert "$TEST_IMG" -l snapshot.name=sn1 -O qcow2 "$converted_image" 138f33d2873SWenchao Xia 139f33d2873SWenchao Xiaecho 140f33d2873SWenchao Xiaecho "== verifying the converted snapshot with patterns, method 2 ==" 141f33d2873SWenchao Xia$QEMU_IO -c 'read -P 0xa 0x1000 0x1000' "$converted_image" | _filter_qemu_io 142f33d2873SWenchao Xia$QEMU_IO -c 'read -P 0xb 0x2000 0x1000' "$converted_image" | _filter_qemu_io 143f33d2873SWenchao Xia 1449c468a01SWenchao Xia# success, all done 1459c468a01SWenchao Xiaecho "*** done" 1469c468a01SWenchao Xiarm -f $seq.full 1479c468a01SWenchao Xiastatus=0 148