xref: /openbmc/qemu/tests/qemu-iotests/058 (revision f764718d)
1#!/bin/bash
2#
3# Test export internal snapshot by qemu-nbd, convert it by qemu-img.
4#
5# Copyright (C) 2013 IBM, Inc.
6#
7# Based on 029.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22
23# creator
24owner=xiawenc@linux.vnet.ibm.com
25
26seq=`basename $0`
27echo "QA output created by $seq"
28
29here=`pwd`
30status=1	# failure is the default!
31
32nbd_unix_socket=$TEST_DIR/test_qemu_nbd_socket
33nbd_snapshot_img="nbd:unix:$nbd_unix_socket"
34rm -f "${TEST_DIR}/qemu-nbd.pid"
35
36_cleanup_nbd()
37{
38    local NBD_SNAPSHOT_PID
39    if [ -f "${TEST_DIR}/qemu-nbd.pid" ]; then
40        read NBD_SNAPSHOT_PID < "${TEST_DIR}/qemu-nbd.pid"
41        rm -f "${TEST_DIR}/qemu-nbd.pid"
42        if [ -n "$NBD_SNAPSHOT_PID" ]; then
43            kill "$NBD_SNAPSHOT_PID"
44        fi
45    fi
46    rm -f "$nbd_unix_socket"
47}
48
49_wait_for_nbd()
50{
51    for ((i = 0; i < 300; i++))
52    do
53        if [ -r "$nbd_unix_socket" ]; then
54            return
55        fi
56        sleep 0.1
57    done
58    echo "Failed in check of unix socket created by qemu-nbd"
59    exit 1
60}
61
62converted_image=$TEST_IMG.converted
63
64_export_nbd_snapshot()
65{
66    _cleanup_nbd
67    $QEMU_NBD -v -t -k "$nbd_unix_socket" "$TEST_IMG" -l $1 &
68    _wait_for_nbd
69}
70
71_export_nbd_snapshot1()
72{
73    _cleanup_nbd
74    $QEMU_NBD -v -t -k "$nbd_unix_socket" "$TEST_IMG" -l snapshot.name=$1 &
75    _wait_for_nbd
76}
77
78_cleanup()
79{
80    _cleanup_nbd
81    _cleanup_test_img
82    rm -f "$converted_image"
83}
84trap "_cleanup; exit \$status" 0 1 2 3 15
85
86# get standard environment, filters and checks
87. ./common.rc
88. ./common.filter
89. ./common.pattern
90
91_supported_fmt qcow2
92_supported_proto file
93_supported_os Linux
94_require_command QEMU_NBD
95# Internal snapshots are (currently) impossible with refcount_bits=1
96_unsupported_imgopts 'refcount_bits=1[^0-9]'
97
98# Use -f raw instead of -f $IMGFMT for the NBD connection
99QEMU_IO_NBD="$QEMU_IO -f raw --cache=$CACHEMODE"
100
101echo
102echo "== preparing image =="
103_make_test_img 64M
104$QEMU_IO -c 'write -P 0xa 0x1000 0x1000' "$TEST_IMG" | _filter_qemu_io
105$QEMU_IO -c 'write -P 0xb 0x2000 0x1000' "$TEST_IMG" | _filter_qemu_io
106$QEMU_IMG snapshot -c sn1 "$TEST_IMG"
107$QEMU_IO -c 'write -P 0xc 0x1000 0x1000' "$TEST_IMG" | _filter_qemu_io
108$QEMU_IO -c 'write -P 0xd 0x2000 0x1000' "$TEST_IMG" | _filter_qemu_io
109_check_test_img
110
111echo
112echo "== verifying the image file with patterns =="
113$QEMU_IO -c 'read -P 0xc 0x1000 0x1000' "$TEST_IMG" | _filter_qemu_io
114$QEMU_IO -c 'read -P 0xd 0x2000 0x1000' "$TEST_IMG" | _filter_qemu_io
115
116_export_nbd_snapshot sn1
117
118echo
119echo "== verifying the exported snapshot with patterns, method 1 =="
120$QEMU_IO_NBD -r -c 'read -P 0xa 0x1000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io
121$QEMU_IO_NBD -r -c 'read -P 0xb 0x2000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io
122
123_export_nbd_snapshot1 sn1
124
125echo
126echo "== verifying the exported snapshot with patterns, method 2 =="
127$QEMU_IO_NBD -r -c 'read -P 0xa 0x1000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io
128$QEMU_IO_NBD -r -c 'read -P 0xb 0x2000 0x1000' "$nbd_snapshot_img" | _filter_qemu_io
129
130$QEMU_IMG convert "$TEST_IMG" -l sn1 -O qcow2 "$converted_image"
131
132echo
133echo "== verifying the converted snapshot with patterns, method 1 =="
134$QEMU_IO -c 'read -P 0xa 0x1000 0x1000' "$converted_image" | _filter_qemu_io
135$QEMU_IO -c 'read -P 0xb 0x2000 0x1000' "$converted_image" | _filter_qemu_io
136
137$QEMU_IMG convert "$TEST_IMG" -l snapshot.name=sn1 -O qcow2 "$converted_image"
138
139echo
140echo "== verifying the converted snapshot with patterns, method 2 =="
141$QEMU_IO -c 'read -P 0xa 0x1000 0x1000' "$converted_image" | _filter_qemu_io
142$QEMU_IO -c 'read -P 0xb 0x2000 0x1000' "$converted_image" | _filter_qemu_io
143
144# success, all done
145echo "*** done"
146rm -f $seq.full
147status=0
148