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