xref: /openbmc/qemu/tests/qemu-iotests/058 (revision 9c8ab1ae)
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 Xiatmp=/tmp/$$
319c468a01SWenchao Xiastatus=1	# failure is the default!
329c468a01SWenchao Xia
339c468a01SWenchao Xianbd_unix_socket=$TEST_DIR/test_qemu_nbd_socket
349c468a01SWenchao Xianbd_snapshot_img="nbd:unix:$nbd_unix_socket"
359c468a01SWenchao Xia
369c468a01SWenchao Xia_cleanup_nbd()
379c468a01SWenchao Xia{
389c468a01SWenchao Xia    if [ -n "$NBD_SNAPSHOT_PID" ]; then
399c468a01SWenchao Xia        kill "$NBD_SNAPSHOT_PID"
409c468a01SWenchao Xia    fi
419c468a01SWenchao Xia    rm -f "$nbd_unix_socket"
429c468a01SWenchao Xia}
439c468a01SWenchao Xia
449c468a01SWenchao Xia_wait_for_nbd()
459c468a01SWenchao Xia{
469c468a01SWenchao Xia    for ((i = 0; i < 300; i++))
479c468a01SWenchao Xia    do
489c468a01SWenchao Xia        if [ -r "$nbd_unix_socket" ]; then
499c468a01SWenchao Xia            return
509c468a01SWenchao Xia        fi
519c468a01SWenchao Xia        sleep 0.1
529c468a01SWenchao Xia    done
539c468a01SWenchao Xia    echo "Failed in check of unix socket created by qemu-nbd"
549c468a01SWenchao Xia    exit 1
559c468a01SWenchao Xia}
569c468a01SWenchao Xia
57f33d2873SWenchao Xiaconverted_image=$TEST_IMG.converted
58f33d2873SWenchao Xia
599c468a01SWenchao Xia_export_nbd_snapshot()
609c468a01SWenchao Xia{
619c468a01SWenchao Xia    _cleanup_nbd
629c468a01SWenchao Xia    $QEMU_NBD -v -t -k "$nbd_unix_socket" "$TEST_IMG" -l $1 &
639c468a01SWenchao Xia    NBD_SNAPSHOT_PID=$!
649c468a01SWenchao Xia    _wait_for_nbd
659c468a01SWenchao Xia}
669c468a01SWenchao Xia
679c468a01SWenchao Xia_export_nbd_snapshot1()
689c468a01SWenchao Xia{
699c468a01SWenchao Xia    _cleanup_nbd
709c468a01SWenchao Xia    $QEMU_NBD -v -t -k "$nbd_unix_socket" "$TEST_IMG" -l snapshot.name=$1 &
719c468a01SWenchao Xia    NBD_SNAPSHOT_PID=$!
729c468a01SWenchao Xia    _wait_for_nbd
739c468a01SWenchao Xia}
749c468a01SWenchao Xia
759c468a01SWenchao Xia_cleanup()
769c468a01SWenchao Xia{
779c468a01SWenchao Xia    _cleanup_nbd
789c468a01SWenchao Xia    _cleanup_test_img
79f33d2873SWenchao Xia    rm -f "$converted_image"
809c468a01SWenchao Xia}
819c468a01SWenchao Xiatrap "_cleanup; exit \$status" 0 1 2 3 15
829c468a01SWenchao Xia
839c468a01SWenchao Xia# get standard environment, filters and checks
849c468a01SWenchao Xia. ./common.rc
859c468a01SWenchao Xia. ./common.filter
869c468a01SWenchao Xia. ./common.pattern
879c468a01SWenchao Xia
889c468a01SWenchao Xia_supported_fmt qcow2
899c468a01SWenchao Xia_supported_proto file
90*9c8ab1aeSFam Zheng_supported_os Linux
919c468a01SWenchao Xia_require_command QEMU_NBD
929c468a01SWenchao Xia
938f9e835fSKevin Wolf# Use -f raw instead of -f $IMGFMT for the NBD connection
948f9e835fSKevin WolfQEMU_IO_NBD="$QEMU_IO -f raw --cache=$CACHEMODE"
958f9e835fSKevin Wolf
969c468a01SWenchao Xiaecho
979c468a01SWenchao Xiaecho "== preparing image =="
989c468a01SWenchao Xia_make_test_img 64M
999c468a01SWenchao Xia$QEMU_IO -c 'write -P 0xa 0x1000 0x1000' "$TEST_IMG" | _filter_qemu_io
1009c468a01SWenchao Xia$QEMU_IO -c 'write -P 0xb 0x2000 0x1000' "$TEST_IMG" | _filter_qemu_io
1019c468a01SWenchao Xia$QEMU_IMG snapshot -c sn1 "$TEST_IMG"
1029c468a01SWenchao Xia$QEMU_IO -c 'write -P 0xc 0x1000 0x1000' "$TEST_IMG" | _filter_qemu_io
1039c468a01SWenchao Xia$QEMU_IO -c 'write -P 0xd 0x2000 0x1000' "$TEST_IMG" | _filter_qemu_io
1049c468a01SWenchao Xia_check_test_img
1059c468a01SWenchao Xia
1069c468a01SWenchao Xiaecho
1079c468a01SWenchao Xiaecho "== verifying the image file with patterns =="
1089c468a01SWenchao Xia$QEMU_IO -c 'read -P 0xc 0x1000 0x1000' "$TEST_IMG" | _filter_qemu_io
1099c468a01SWenchao Xia$QEMU_IO -c 'read -P 0xd 0x2000 0x1000' "$TEST_IMG" | _filter_qemu_io
1109c468a01SWenchao Xia
1119c468a01SWenchao Xia_export_nbd_snapshot sn1
1129c468a01SWenchao Xia
1139c468a01SWenchao Xiaecho
1149c468a01SWenchao Xiaecho "== verifying the exported snapshot with patterns, method 1 =="
1158f9e835fSKevin Wolf$QEMU_IO_NBD -c 'read -P 0xa 0x1000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io
1168f9e835fSKevin Wolf$QEMU_IO_NBD -c 'read -P 0xb 0x2000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io
1179c468a01SWenchao Xia
1189c468a01SWenchao Xia_export_nbd_snapshot1 sn1
1199c468a01SWenchao Xia
1209c468a01SWenchao Xiaecho
1219c468a01SWenchao Xiaecho "== verifying the exported snapshot with patterns, method 2 =="
1228f9e835fSKevin Wolf$QEMU_IO_NBD -c 'read -P 0xa 0x1000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io
1238f9e835fSKevin Wolf$QEMU_IO_NBD -c 'read -P 0xb 0x2000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io
1249c468a01SWenchao Xia
125f33d2873SWenchao Xia$QEMU_IMG convert "$TEST_IMG" -l sn1 -O qcow2 "$converted_image"
126f33d2873SWenchao Xia
127f33d2873SWenchao Xiaecho
128f33d2873SWenchao Xiaecho "== verifying the converted snapshot with patterns, method 1 =="
129f33d2873SWenchao Xia$QEMU_IO -c 'read -P 0xa 0x1000 0x1000' "$converted_image" | _filter_qemu_io
130f33d2873SWenchao Xia$QEMU_IO -c 'read -P 0xb 0x2000 0x1000' "$converted_image" | _filter_qemu_io
131f33d2873SWenchao Xia
132f33d2873SWenchao Xia$QEMU_IMG convert "$TEST_IMG" -l snapshot.name=sn1 -O qcow2 "$converted_image"
133f33d2873SWenchao Xia
134f33d2873SWenchao Xiaecho
135f33d2873SWenchao Xiaecho "== verifying the converted snapshot with patterns, method 2 =="
136f33d2873SWenchao Xia$QEMU_IO -c 'read -P 0xa 0x1000 0x1000' "$converted_image" | _filter_qemu_io
137f33d2873SWenchao Xia$QEMU_IO -c 'read -P 0xb 0x2000 0x1000' "$converted_image" | _filter_qemu_io
138f33d2873SWenchao Xia
1399c468a01SWenchao Xia# success, all done
1409c468a01SWenchao Xiaecho "*** done"
1419c468a01SWenchao Xiarm -f $seq.full
1429c468a01SWenchao Xiastatus=0
143