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