xref: /openbmc/qemu/tests/qemu-iotests/153 (revision 0e1a582750269d3dde0481ca034b08a5784e430c)
1ba898078SFam Zheng#!/bin/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 Zhenghere="$PWD"
28ba898078SFam Zhengtmp=/tmp/$$
29ba898078SFam Zhengstatus=1	# failure is the default!
30ba898078SFam Zheng
31ba898078SFam Zheng_cleanup()
32ba898078SFam Zheng{
33ba898078SFam Zheng    _cleanup_test_img
34ba898078SFam Zheng    rm -f "${TEST_IMG}.base"
35fcca5dceSFam Zheng    rm -f "${TEST_IMG}.overlay"
36ba898078SFam Zheng    rm -f "${TEST_IMG}.convert"
37ba898078SFam Zheng    rm -f "${TEST_IMG}.a"
38ba898078SFam Zheng    rm -f "${TEST_IMG}.b"
396a1e9096SKevin Wolf    rm -f "${TEST_IMG}.c"
40ba898078SFam Zheng    rm -f "${TEST_IMG}.lnk"
41ba898078SFam Zheng}
42ba898078SFam Zhengtrap "_cleanup; exit \$status" 0 1 2 3 15
43ba898078SFam Zheng
44ba898078SFam Zheng# get standard environment, filters and checks
45ba898078SFam Zheng. ./common.rc
46ba898078SFam Zheng. ./common.filter
47ba898078SFam Zheng. ./common.qemu
48ba898078SFam Zheng
49ba898078SFam Zhengsize=32M
50ba898078SFam Zheng
51ba898078SFam Zheng_check_ofd()
52ba898078SFam Zheng{
53ba898078SFam Zheng    _make_test_img $size >/dev/null
54ba898078SFam Zheng    if $QEMU_IMG_PROG info --image-opts "driver=file,locking=on,filename=$TEST_IMG" 2>&1 |
55ba898078SFam Zheng        grep -q 'falling back to POSIX file'; then
56ba898078SFam Zheng        return 1
57ba898078SFam Zheng    else
58ba898078SFam Zheng        return 0
59ba898078SFam Zheng    fi
60ba898078SFam Zheng}
61ba898078SFam Zheng
62ba898078SFam Zheng_check_ofd || _notrun "OFD lock not available"
63ba898078SFam Zheng
64ba898078SFam Zheng_supported_fmt qcow2
65ba898078SFam Zheng_supported_proto file
66ba898078SFam Zheng_supported_os Linux
67ba898078SFam Zheng
68ba898078SFam Zheng_run_cmd()
69ba898078SFam Zheng{
70ba898078SFam Zheng    echo
71ba898078SFam Zheng    (echo "$@"; "$@" 2>&1 1>/dev/null) | _filter_testdir
72ba898078SFam Zheng}
73ba898078SFam Zheng
74ba898078SFam Zhengfunction _do_run_qemu()
75ba898078SFam Zheng{
76ba898078SFam Zheng    (
77ba898078SFam Zheng        if ! test -t 0; then
78ba898078SFam Zheng            while read cmd; do
79ba898078SFam Zheng                echo $cmd
80ba898078SFam Zheng            done
81ba898078SFam Zheng        fi
82ba898078SFam Zheng        echo quit
83ba898078SFam Zheng    ) | $QEMU -nographic -monitor stdio -serial none "$@" 1>/dev/null
84ba898078SFam Zheng}
85ba898078SFam Zheng
86ba898078SFam Zhengfunction _run_qemu_with_images()
87ba898078SFam Zheng{
88ba898078SFam Zheng    _do_run_qemu \
89ba898078SFam Zheng        $(for i in $@; do echo "-drive if=none,file=$i"; done) 2>&1 \
90ba898078SFam Zheng        | _filter_testdir | _filter_qemu
91ba898078SFam Zheng}
92ba898078SFam Zheng
93ba898078SFam Zhengecho "== readonly=off,force-share=on should be rejected =="
94ba898078SFam Zheng_run_qemu_with_images null-co://,readonly=off,force-share=on
95ba898078SFam Zheng
96ba898078SFam Zhengfor opts1 in "" "read-only=on" "read-only=on,force-share=on"; do
97ba898078SFam Zheng    echo
98ba898078SFam Zheng    echo "== Creating base image =="
99ba898078SFam Zheng    TEST_IMG="${TEST_IMG}.base" _make_test_img $size
100ba898078SFam Zheng
101ba898078SFam Zheng    echo
102ba898078SFam Zheng    echo "== Creating test image =="
103ba898078SFam Zheng    $QEMU_IMG create -f $IMGFMT "${TEST_IMG}" -b ${TEST_IMG}.base | _filter_img_create
104ba898078SFam Zheng
105ba898078SFam Zheng    echo
106ba898078SFam Zheng    echo "== Launching QEMU, opts: '$opts1' =="
107ba898078SFam Zheng    _launch_qemu -drive file="${TEST_IMG}",if=none,$opts1
108ba898078SFam Zheng    h=$QEMU_HANDLE
109ba898078SFam Zheng
110ba898078SFam Zheng    for opts2 in "" "read-only=on" "read-only=on,force-share=on"; do
111ba898078SFam Zheng        echo
112ba898078SFam Zheng        echo "== Launching another QEMU, opts: '$opts2' =="
113ba898078SFam Zheng        echo "quit" | \
114ba898078SFam Zheng            $QEMU -nographic -monitor stdio \
115ba898078SFam Zheng            -drive file="${TEST_IMG}",if=none,$opts2 2>&1 1>/dev/null | \
116ba898078SFam Zheng            _filter_testdir | _filter_qemu
117ba898078SFam Zheng    done
118ba898078SFam Zheng
119ba898078SFam Zheng    for L in "" "-U"; do
120ba898078SFam Zheng
121ba898078SFam Zheng        echo
122ba898078SFam Zheng        echo "== Running utility commands $L =="
123ba898078SFam Zheng        _run_cmd $QEMU_IO $L -c "read 0 512" "${TEST_IMG}"
124ba898078SFam Zheng        _run_cmd $QEMU_IO $L -r -c "read 0 512" "${TEST_IMG}"
125ba898078SFam Zheng        _run_cmd $QEMU_IO -c "open $L ${TEST_IMG}" -c "read 0 512"
126ba898078SFam Zheng        _run_cmd $QEMU_IO -c "open -r $L ${TEST_IMG}" -c "read 0 512"
127ba898078SFam Zheng        _run_cmd $QEMU_IMG info        $L "${TEST_IMG}"
128ba898078SFam Zheng        _run_cmd $QEMU_IMG check       $L "${TEST_IMG}"
129ba898078SFam Zheng        _run_cmd $QEMU_IMG compare     $L "${TEST_IMG}" "${TEST_IMG}"
130ba898078SFam Zheng        _run_cmd $QEMU_IMG map         $L "${TEST_IMG}"
131ba898078SFam Zheng        _run_cmd $QEMU_IMG amend -o "" $L "${TEST_IMG}"
132ba898078SFam Zheng        _run_cmd $QEMU_IMG commit      $L "${TEST_IMG}"
133ba898078SFam Zheng        _run_cmd $QEMU_IMG resize      $L "${TEST_IMG}" $size
134ba898078SFam Zheng        _run_cmd $QEMU_IMG rebase      $L "${TEST_IMG}" -b "${TEST_IMG}.base"
135ba898078SFam Zheng        _run_cmd $QEMU_IMG snapshot -l $L "${TEST_IMG}"
136ba898078SFam Zheng        _run_cmd $QEMU_IMG convert     $L "${TEST_IMG}" "${TEST_IMG}.convert"
137ba898078SFam Zheng        _run_cmd $QEMU_IMG dd          $L if="${TEST_IMG}" of="${TEST_IMG}.convert" bs=512 count=1
138ba898078SFam Zheng        _run_cmd $QEMU_IMG bench       $L -c 1 "${TEST_IMG}"
139ba898078SFam Zheng        _run_cmd $QEMU_IMG bench       $L -w -c 1 "${TEST_IMG}"
140f45b638fSMax Reitz
141f45b638fSMax Reitz        # qemu-img create does not support -U
142f45b638fSMax Reitz        if [ -z "$L" ]; then
143f45b638fSMax Reitz            _run_cmd $QEMU_IMG create -f $IMGFMT "${TEST_IMG}" \
144f45b638fSMax Reitz                                      -b ${TEST_IMG}.base
145f45b638fSMax Reitz            # Read the file format.  It used to be the case that
146f45b638fSMax Reitz            # file-posix simply truncated the file, but the qcow2
147f45b638fSMax Reitz            # driver then failed to format it because it was unable
148f45b638fSMax Reitz            # to acquire the necessary WRITE permission.  However, the
149f45b638fSMax Reitz            # truncation was already wrong, and the whole process
150f45b638fSMax Reitz            # resulted in the file being completely empty and thus its
151f45b638fSMax Reitz            # format would be detected to be raw.
152f45b638fSMax Reitz            # So we read it here to see that creation either completed
153f45b638fSMax Reitz            # successfully (thus the format is qcow2) or it aborted
154f45b638fSMax Reitz            # before the file was changed at all (thus the format stays
155f45b638fSMax Reitz            # qcow2).
156f45b638fSMax Reitz            _img_info -U | grep 'file format'
157f45b638fSMax Reitz        fi
158ba898078SFam Zheng    done
159ba898078SFam Zheng    _send_qemu_cmd $h "{ 'execute': 'quit', }" ""
160ba898078SFam Zheng    echo
161ba898078SFam Zheng    echo "Round done"
162ba898078SFam Zheng    _cleanup_qemu
163ba898078SFam Zhengdone
164ba898078SFam Zheng
165*0e1a5827SFam Zhengtest_opts="read-only=off read-only=on read-only=on,force-share=on"
166ba898078SFam Zhengfor opt1 in $test_opts; do
167ba898078SFam Zheng    for opt2 in $test_opts; do
168ba898078SFam Zheng        echo
169ba898078SFam Zheng        echo "== Two devices with the same image ($opt1 - $opt2) =="
170ba898078SFam Zheng        _run_qemu_with_images "${TEST_IMG},$opt1" "${TEST_IMG},$opt2"
171ba898078SFam Zheng    done
172ba898078SFam Zhengdone
173ba898078SFam Zheng
174*0e1a5827SFam Zhengecho
175ba898078SFam Zhengecho "== Creating ${TEST_IMG}.[abc] ==" | _filter_testdir
176ba898078SFam Zheng(
177ba898078SFam Zheng    $QEMU_IMG create -f qcow2 "${TEST_IMG}.a" -b "${TEST_IMG}"
178ba898078SFam Zheng    $QEMU_IMG create -f qcow2 "${TEST_IMG}.b" -b "${TEST_IMG}"
179ba898078SFam Zheng    $QEMU_IMG create -f qcow2 "${TEST_IMG}.c" -b "${TEST_IMG}.b"
180ba898078SFam Zheng) | _filter_img_create
181ba898078SFam Zheng
182ba898078SFam Zhengecho
183ba898078SFam Zhengecho "== Two devices sharing the same file in backing chain =="
184ba898078SFam Zheng_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.b"
185ba898078SFam Zheng_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.c"
186ba898078SFam Zheng
187ba898078SFam Zhengecho
188ba898078SFam Zhengecho "== Backing image also as an active device =="
189ba898078SFam Zheng_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}"
190ba898078SFam Zheng
191ba898078SFam Zhengecho
192ba898078SFam Zhengecho "== Backing image also as an active device (ro) =="
193ba898078SFam Zheng_run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG},readonly=on"
194ba898078SFam Zheng
195ba898078SFam Zhengecho
196ba898078SFam Zhengecho "== Symbolic link =="
197ba898078SFam Zhengrm -f "${TEST_IMG}.lnk" &>/dev/null
198ba898078SFam Zhengln -s ${TEST_IMG} "${TEST_IMG}.lnk" || echo "Failed to create link"
199ba898078SFam Zheng_run_qemu_with_images "${TEST_IMG}.lnk" "${TEST_IMG}"
200ba898078SFam Zheng
201de963500SFam Zhengecho
202de963500SFam Zhengecho "== Active commit to intermediate layer should work when base in use =="
203de963500SFam Zheng_launch_qemu -drive format=$IMGFMT,file="${TEST_IMG}.a",id=drive0,if=none \
204de963500SFam Zheng             -device virtio-blk,drive=drive0
205de963500SFam Zheng
206de963500SFam Zheng_send_qemu_cmd $QEMU_HANDLE \
207de963500SFam Zheng    "{ 'execute': 'qmp_capabilities' }" \
208de963500SFam Zheng    'return'
209de963500SFam Zheng_run_cmd $QEMU_IMG commit -b "${TEST_IMG}.b" "${TEST_IMG}.c"
210de963500SFam Zheng
211de963500SFam Zheng_cleanup_qemu
212de963500SFam Zheng
213ba898078SFam Zheng_launch_qemu
214ba898078SFam Zheng
215ba898078SFam Zheng_send_qemu_cmd $QEMU_HANDLE \
216ba898078SFam Zheng    "{ 'execute': 'qmp_capabilities' }" \
217ba898078SFam Zheng    'return'
218ba898078SFam Zheng
219ba898078SFam Zhengecho "Adding drive"
220ba898078SFam Zheng_send_qemu_cmd $QEMU_HANDLE \
221ba898078SFam Zheng    "{ 'execute': 'human-monitor-command',
222ba898078SFam Zheng       'arguments': { 'command-line': 'drive_add 0 if=none,id=d0,file=${TEST_IMG}' } }" \
223ba898078SFam Zheng    ""
224ba898078SFam Zheng
225ba898078SFam Zheng_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
226ba898078SFam Zheng
227fcca5dceSFam Zhengecho "Creating overlay with qemu-img when the guest is running should be allowed"
228fcca5dceSFam Zheng_run_cmd $QEMU_IMG create -f $IMGFMT -b "${TEST_IMG}" "${TEST_IMG}.overlay"
229fcca5dceSFam Zheng
230fcca5dceSFam Zhengecho "== Closing an image should unlock it =="
231ba898078SFam Zheng_send_qemu_cmd $QEMU_HANDLE \
232ba898078SFam Zheng    "{ 'execute': 'human-monitor-command',
233ba898078SFam Zheng       'arguments': { 'command-line': 'drive_del d0' } }" \
234ba898078SFam Zheng    ""
235ba898078SFam Zheng
236ba898078SFam Zheng_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
237ba898078SFam Zheng
238ba898078SFam Zhengecho "Adding two and closing one"
239ba898078SFam Zhengfor d in d0 d1; do
240ba898078SFam Zheng    _send_qemu_cmd $QEMU_HANDLE \
241ba898078SFam Zheng        "{ 'execute': 'human-monitor-command',
242ba898078SFam Zheng           'arguments': { 'command-line': 'drive_add 0 if=none,id=$d,file=${TEST_IMG},readonly=on' } }" \
243ba898078SFam Zheng        ""
244ba898078SFam Zhengdone
245ba898078SFam Zheng
246ba898078SFam Zheng_run_cmd $QEMU_IMG info "${TEST_IMG}"
247ba898078SFam Zheng
248ba898078SFam Zheng_send_qemu_cmd $QEMU_HANDLE \
249ba898078SFam Zheng    "{ 'execute': 'human-monitor-command',
250ba898078SFam Zheng       'arguments': { 'command-line': 'drive_del d0' } }" \
251ba898078SFam Zheng    ""
252ba898078SFam Zheng
253ba898078SFam Zheng_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
254ba898078SFam Zheng
255ba898078SFam Zhengecho "Closing the other"
256ba898078SFam Zheng_send_qemu_cmd $QEMU_HANDLE \
257ba898078SFam Zheng    "{ 'execute': 'human-monitor-command',
258ba898078SFam Zheng       'arguments': { 'command-line': 'drive_del d1' } }" \
259ba898078SFam Zheng    ""
260ba898078SFam Zheng
261ba898078SFam Zheng_run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
262ba898078SFam Zheng
263ba898078SFam Zheng_cleanup_qemu
264ba898078SFam Zheng
2654e7d73c5SMax Reitzecho
2664e7d73c5SMax Reitzecho "== Detecting -U and force-share conflicts =="
2674e7d73c5SMax Reitz
2684e7d73c5SMax Reitzecho
2694e7d73c5SMax Reitzecho 'No conflict:'
2704e7d73c5SMax Reitz$QEMU_IMG info -U --image-opts driver=null-co,force-share=on
2714e7d73c5SMax Reitzecho
2724e7d73c5SMax Reitzecho 'Conflict:'
2734e7d73c5SMax Reitz$QEMU_IMG info -U --image-opts driver=null-co,force-share=off
2744e7d73c5SMax Reitz
2754e7d73c5SMax Reitzecho
2764e7d73c5SMax Reitzecho 'No conflict:'
2774e7d73c5SMax Reitz$QEMU_IO -c 'open -r -U -o driver=null-co,force-share=on'
2784e7d73c5SMax Reitzecho
2794e7d73c5SMax Reitzecho 'Conflict:'
2804e7d73c5SMax Reitz$QEMU_IO -c 'open -r -U -o driver=null-co,force-share=off'
2814e7d73c5SMax Reitz
282ba898078SFam Zheng# success, all done
283ba898078SFam Zhengecho "*** done"
284ba898078SFam Zhengrm -f $seq.full
285ba898078SFam Zhengstatus=0
286