xref: /openbmc/qemu/tests/qemu-iotests/153 (revision f7160f32)
1#!/usr/bin/env bash
2#
3# Test image locking
4#
5# Copyright 2016, 2017 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21# creator
22owner=famz@redhat.com
23
24seq="$(basename $0)"
25echo "QA output created by $seq"
26
27tmp=/tmp/$$
28status=1	# failure is the default!
29
30_cleanup()
31{
32    _cleanup_test_img
33    for img in "${TEST_IMG}".{base,overlay,convert,a,b,c,lnk}; do
34        _rm_test_img "$img"
35    done
36}
37trap "_cleanup; exit \$status" 0 1 2 3 15
38
39# get standard environment, filters and checks
40. ./common.rc
41. ./common.filter
42. ./common.qemu
43
44size=32M
45
46_check_ofd()
47{
48    _make_test_img $size >/dev/null
49    if $QEMU_IMG_PROG info --image-opts "driver=file,locking=on,filename=$TEST_IMG" 2>&1 |
50        grep -q 'falling back to POSIX file'; then
51        return 1
52    else
53        return 0
54    fi
55}
56
57_check_ofd || _notrun "OFD lock not available"
58
59_supported_fmt qcow2
60_supported_proto file
61
62_run_cmd()
63{
64    echo
65    (echo "$@"; "$@" 2>&1 1>/dev/null) | _filter_testdir
66}
67
68_do_run_qemu()
69{
70    (
71        if ! test -t 0; then
72            while read cmd; do
73                echo $cmd
74            done
75        fi
76        echo quit
77    ) | $QEMU -nographic -monitor stdio -serial none "$@" 1>/dev/null
78}
79
80_run_qemu_with_images()
81{
82    _do_run_qemu \
83        $(for i in $@; do echo "-drive if=none,file=$i"; done) 2>&1 \
84        | _filter_testdir | _filter_qemu
85}
86
87echo "== readonly=off,force-share=on should be rejected =="
88_run_qemu_with_images null-co://,readonly=off,force-share=on
89
90for opts1 in "" "read-only=on" "read-only=on,force-share=on"; do
91    echo
92    echo "== Creating base image =="
93    TEST_IMG="${TEST_IMG}.base" _make_test_img $size
94
95    echo
96    echo "== Creating test image =="
97    _make_test_img -b "${TEST_IMG}.base" -F $IMGFMT
98
99    echo
100    echo "== Launching QEMU, opts: '$opts1' =="
101    _launch_qemu -drive file="${TEST_IMG}",if=none,$opts1
102    h=$QEMU_HANDLE
103
104    for opts2 in "" "read-only=on" "read-only=on,force-share=on"; do
105        echo
106        echo "== Launching another QEMU, opts: '$opts2' =="
107        echo "quit" | \
108            $QEMU -nographic -monitor stdio \
109            -drive file="${TEST_IMG}",if=none,$opts2 2>&1 1>/dev/null | \
110            _filter_testdir | _filter_qemu
111    done
112
113    for L in "" "-U"; do
114
115        echo
116        echo "== Running utility commands $L =="
117        _run_cmd $QEMU_IO $L -c "read 0 512" "${TEST_IMG}"
118        _run_cmd $QEMU_IO $L -r -c "read 0 512" "${TEST_IMG}"
119        _run_cmd $QEMU_IO -c "open $L ${TEST_IMG}" -c "read 0 512"
120        _run_cmd $QEMU_IO -c "open -r $L ${TEST_IMG}" -c "read 0 512"
121        _run_cmd $QEMU_IMG info        $L "${TEST_IMG}"
122        _run_cmd $QEMU_IMG check       $L "${TEST_IMG}"
123        _run_cmd $QEMU_IMG compare     $L "${TEST_IMG}" "${TEST_IMG}"
124        _run_cmd $QEMU_IMG map         $L "${TEST_IMG}"
125        _run_cmd $QEMU_IMG amend -o "size=$size" $L "${TEST_IMG}"
126        _run_cmd $QEMU_IMG commit      $L "${TEST_IMG}"
127        _run_cmd $QEMU_IMG resize      $L "${TEST_IMG}" $size
128        _run_cmd $QEMU_IMG rebase      $L "${TEST_IMG}" -b "${TEST_IMG}.base" -F $IMGFMT
129        _run_cmd $QEMU_IMG snapshot -l $L "${TEST_IMG}"
130        _run_cmd $QEMU_IMG convert     $L "${TEST_IMG}" "${TEST_IMG}.convert"
131        _run_cmd $QEMU_IMG dd          $L if="${TEST_IMG}" of="${TEST_IMG}.convert" bs=512 count=1
132        _run_cmd $QEMU_IMG bench       $L -c 1 "${TEST_IMG}"
133        _run_cmd $QEMU_IMG bench       $L -w -c 1 "${TEST_IMG}"
134
135        # qemu-img create does not support -U
136        if [ -z "$L" ]; then
137            _run_cmd $QEMU_IMG create -f $IMGFMT "${TEST_IMG}" \
138                                      -b ${TEST_IMG}.base -F $IMGFMT
139            # Read the file format.  It used to be the case that
140            # file-posix simply truncated the file, but the qcow2
141            # driver then failed to format it because it was unable
142            # to acquire the necessary WRITE permission.  However, the
143            # truncation was already wrong, and the whole process
144            # resulted in the file being completely empty and thus its
145            # format would be detected to be raw.
146            # So we read it here to see that creation either completed
147            # successfully (thus the format is qcow2) or it aborted
148            # before the file was changed at all (thus the format stays
149            # qcow2).
150            _img_info -U | grep 'file format'
151        fi
152    done
153    _send_qemu_cmd $h "{ 'execute': 'quit' }" ''
154    echo
155    echo "Round done"
156    _cleanup_qemu
157done
158
159test_opts="read-only=off read-only=on read-only=on,force-share=on"
160for opt1 in $test_opts; do
161    for opt2 in $test_opts; do
162        echo
163        echo "== Two devices with the same image ($opt1 - $opt2) =="
164        _run_qemu_with_images "${TEST_IMG},$opt1" "${TEST_IMG},$opt2"
165    done
166done
167
168echo
169echo "== Creating ${TEST_IMG}.[abc] ==" | _filter_testdir
170$QEMU_IMG create -f qcow2 "${TEST_IMG}.a" -b "${TEST_IMG}" -F $IMGFMT | _filter_img_create
171$QEMU_IMG create -f qcow2 "${TEST_IMG}.b" -b "${TEST_IMG}" -F $IMGFMT | _filter_img_create
172$QEMU_IMG create -f qcow2 "${TEST_IMG}.c" -b "${TEST_IMG}.b" -F $IMGFMT \
173    | _filter_img_create
174
175echo
176echo "== Two devices sharing the same file in backing chain =="
177_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.b"
178_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.c"
179
180echo
181echo "== Backing image also as an active device =="
182_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}"
183
184echo
185echo "== Backing image also as an active device (ro) =="
186_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG},readonly=on"
187
188echo
189echo "== Symbolic link =="
190rm -f "${TEST_IMG}.lnk" &>/dev/null
191ln -s ${TEST_IMG} "${TEST_IMG}.lnk" || echo "Failed to create link"
192_run_qemu_with_images "${TEST_IMG}.lnk" "${TEST_IMG}"
193
194echo
195echo "== Active commit to intermediate layer should work when base in use =="
196_launch_qemu -drive format=$IMGFMT,file="${TEST_IMG}.a",id=drive0,if=none \
197             -device virtio-blk,drive=drive0
198
199_send_qemu_cmd $QEMU_HANDLE \
200    "{ 'execute': 'qmp_capabilities' }" \
201    'return'
202_run_cmd $QEMU_IMG commit -b "${TEST_IMG}.b" "${TEST_IMG}.c"
203
204_cleanup_qemu
205
206_launch_qemu
207
208_send_qemu_cmd $QEMU_HANDLE \
209    "{ 'execute': 'qmp_capabilities' }" \
210    'return'
211
212echo "Adding drive"
213_send_qemu_cmd $QEMU_HANDLE \
214    "{ 'execute': 'human-monitor-command',
215       'arguments': { 'command-line': 'drive_add 0 if=none,id=d0,file=${TEST_IMG}' } }" \
216    'return'
217
218_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
219
220echo "Creating overlay with qemu-img when the guest is running should be allowed"
221_run_cmd $QEMU_IMG create -f $IMGFMT -b "${TEST_IMG}" -F $IMGFMT "${TEST_IMG}.overlay"
222
223echo "== Closing an image should unlock it =="
224_send_qemu_cmd $QEMU_HANDLE \
225    "{ 'execute': 'human-monitor-command',
226       'arguments': { 'command-line': 'drive_del d0' } }" \
227    'return'
228
229_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
230
231echo "Adding two and closing one"
232for d in d0 d1; do
233    _send_qemu_cmd $QEMU_HANDLE \
234        "{ 'execute': 'human-monitor-command',
235           'arguments': { 'command-line': 'drive_add 0 if=none,id=$d,file=${TEST_IMG},readonly=on' } }" \
236        'return'
237done
238
239_run_cmd $QEMU_IMG info "${TEST_IMG}"
240
241_send_qemu_cmd $QEMU_HANDLE \
242    "{ 'execute': 'human-monitor-command',
243       'arguments': { 'command-line': 'drive_del d0' } }" \
244    'return'
245
246_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
247
248echo "Closing the other"
249_send_qemu_cmd $QEMU_HANDLE \
250    "{ 'execute': 'human-monitor-command',
251       'arguments': { 'command-line': 'drive_del d1' } }" \
252    'return'
253
254_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
255
256_cleanup_qemu
257
258echo
259echo "== Detecting -U and force-share conflicts =="
260
261echo
262echo 'No conflict:'
263$QEMU_IMG info -U --image-opts driver=null-co,force-share=on
264echo
265echo 'Conflict:'
266$QEMU_IMG info -U --image-opts driver=null-co,force-share=off
267
268echo
269echo 'No conflict:'
270$QEMU_IO -c 'open -r -U -o driver=null-co,force-share=on'
271echo
272echo 'Conflict:'
273$QEMU_IO -c 'open -r -U -o driver=null-co,force-share=off'
274
275# success, all done
276echo "*** done"
277rm -f $seq.full
278status=0
279