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