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 33f91ecbd7SMax Reitz for img in "${TEST_IMG}".{base,overlay,convert,a,b,c,lnk}; do 34f91ecbd7SMax Reitz _rm_test_img "$img" 35f91ecbd7SMax Reitz done 36ba898078SFam Zheng} 37ba898078SFam Zhengtrap "_cleanup; exit \$status" 0 1 2 3 15 38ba898078SFam Zheng 39ba898078SFam Zheng# get standard environment, filters and checks 40ba898078SFam Zheng. ./common.rc 41ba898078SFam Zheng. ./common.filter 42ba898078SFam Zheng. ./common.qemu 43ba898078SFam Zheng 44ba898078SFam Zhengsize=32M 45ba898078SFam Zheng 46ba898078SFam Zheng_check_ofd() 47ba898078SFam Zheng{ 48ba898078SFam Zheng _make_test_img $size >/dev/null 49ba898078SFam Zheng if $QEMU_IMG_PROG info --image-opts "driver=file,locking=on,filename=$TEST_IMG" 2>&1 | 50ba898078SFam Zheng grep -q 'falling back to POSIX file'; then 51ba898078SFam Zheng return 1 52ba898078SFam Zheng else 53ba898078SFam Zheng return 0 54ba898078SFam Zheng fi 55ba898078SFam Zheng} 56ba898078SFam Zheng 57ba898078SFam Zheng_check_ofd || _notrun "OFD lock not available" 58ba898078SFam Zheng 59ba898078SFam Zheng_supported_fmt qcow2 60ba898078SFam Zheng_supported_proto file 61ba898078SFam Zheng 62ba898078SFam Zheng_run_cmd() 63ba898078SFam Zheng{ 64ba898078SFam Zheng echo 65ba898078SFam Zheng (echo "$@"; "$@" 2>&1 1>/dev/null) | _filter_testdir 66ba898078SFam Zheng} 67ba898078SFam Zheng 688cedcffdSEric Blake_do_run_qemu() 69ba898078SFam Zheng{ 70ba898078SFam Zheng ( 71ba898078SFam Zheng if ! test -t 0; then 72ba898078SFam Zheng while read cmd; do 73ba898078SFam Zheng echo $cmd 74ba898078SFam Zheng done 75ba898078SFam Zheng fi 76ba898078SFam Zheng echo quit 77ba898078SFam Zheng ) | $QEMU -nographic -monitor stdio -serial none "$@" 1>/dev/null 78ba898078SFam Zheng} 79ba898078SFam Zheng 808cedcffdSEric Blake_run_qemu_with_images() 81ba898078SFam Zheng{ 82ba898078SFam Zheng _do_run_qemu \ 83ba898078SFam Zheng $(for i in $@; do echo "-drive if=none,file=$i"; done) 2>&1 \ 84ba898078SFam Zheng | _filter_testdir | _filter_qemu 85ba898078SFam Zheng} 86ba898078SFam Zheng 87ba898078SFam Zhengecho "== readonly=off,force-share=on should be rejected ==" 88ba898078SFam Zheng_run_qemu_with_images null-co://,readonly=off,force-share=on 89ba898078SFam Zheng 90ba898078SFam Zhengfor opts1 in "" "read-only=on" "read-only=on,force-share=on"; do 91ba898078SFam Zheng echo 92ba898078SFam Zheng echo "== Creating base image ==" 93ba898078SFam Zheng TEST_IMG="${TEST_IMG}.base" _make_test_img $size 94ba898078SFam Zheng 95ba898078SFam Zheng echo 96ba898078SFam Zheng echo "== Creating test image ==" 9752a97b5aSMax Reitz _make_test_img -b "${TEST_IMG}.base" 98ba898078SFam Zheng 99ba898078SFam Zheng echo 100ba898078SFam Zheng echo "== Launching QEMU, opts: '$opts1' ==" 101ba898078SFam Zheng _launch_qemu -drive file="${TEST_IMG}",if=none,$opts1 102ba898078SFam Zheng h=$QEMU_HANDLE 103ba898078SFam Zheng 104ba898078SFam Zheng for opts2 in "" "read-only=on" "read-only=on,force-share=on"; do 105ba898078SFam Zheng echo 106ba898078SFam Zheng echo "== Launching another QEMU, opts: '$opts2' ==" 107ba898078SFam Zheng echo "quit" | \ 108ba898078SFam Zheng $QEMU -nographic -monitor stdio \ 109ba898078SFam Zheng -drive file="${TEST_IMG}",if=none,$opts2 2>&1 1>/dev/null | \ 110ba898078SFam Zheng _filter_testdir | _filter_qemu 111ba898078SFam Zheng done 112ba898078SFam Zheng 113ba898078SFam Zheng for L in "" "-U"; do 114ba898078SFam Zheng 115ba898078SFam Zheng echo 116ba898078SFam Zheng echo "== Running utility commands $L ==" 117ba898078SFam Zheng _run_cmd $QEMU_IO $L -c "read 0 512" "${TEST_IMG}" 118ba898078SFam Zheng _run_cmd $QEMU_IO $L -r -c "read 0 512" "${TEST_IMG}" 119ba898078SFam Zheng _run_cmd $QEMU_IO -c "open $L ${TEST_IMG}" -c "read 0 512" 120ba898078SFam Zheng _run_cmd $QEMU_IO -c "open -r $L ${TEST_IMG}" -c "read 0 512" 121ba898078SFam Zheng _run_cmd $QEMU_IMG info $L "${TEST_IMG}" 122ba898078SFam Zheng _run_cmd $QEMU_IMG check $L "${TEST_IMG}" 123ba898078SFam Zheng _run_cmd $QEMU_IMG compare $L "${TEST_IMG}" "${TEST_IMG}" 124ba898078SFam Zheng _run_cmd $QEMU_IMG map $L "${TEST_IMG}" 125*44e808c1SMaxim Levitsky _run_cmd $QEMU_IMG amend -o "size=$size" $L "${TEST_IMG}" 126ba898078SFam Zheng _run_cmd $QEMU_IMG commit $L "${TEST_IMG}" 127ba898078SFam Zheng _run_cmd $QEMU_IMG resize $L "${TEST_IMG}" $size 128ba898078SFam Zheng _run_cmd $QEMU_IMG rebase $L "${TEST_IMG}" -b "${TEST_IMG}.base" 129ba898078SFam Zheng _run_cmd $QEMU_IMG snapshot -l $L "${TEST_IMG}" 130ba898078SFam Zheng _run_cmd $QEMU_IMG convert $L "${TEST_IMG}" "${TEST_IMG}.convert" 131ba898078SFam Zheng _run_cmd $QEMU_IMG dd $L if="${TEST_IMG}" of="${TEST_IMG}.convert" bs=512 count=1 132ba898078SFam Zheng _run_cmd $QEMU_IMG bench $L -c 1 "${TEST_IMG}" 133ba898078SFam Zheng _run_cmd $QEMU_IMG bench $L -w -c 1 "${TEST_IMG}" 134f45b638fSMax Reitz 135f45b638fSMax Reitz # qemu-img create does not support -U 136f45b638fSMax Reitz if [ -z "$L" ]; then 137f45b638fSMax Reitz _run_cmd $QEMU_IMG create -f $IMGFMT "${TEST_IMG}" \ 138f45b638fSMax Reitz -b ${TEST_IMG}.base 139f45b638fSMax Reitz # Read the file format. It used to be the case that 140f45b638fSMax Reitz # file-posix simply truncated the file, but the qcow2 141f45b638fSMax Reitz # driver then failed to format it because it was unable 142f45b638fSMax Reitz # to acquire the necessary WRITE permission. However, the 143f45b638fSMax Reitz # truncation was already wrong, and the whole process 144f45b638fSMax Reitz # resulted in the file being completely empty and thus its 145f45b638fSMax Reitz # format would be detected to be raw. 146f45b638fSMax Reitz # So we read it here to see that creation either completed 147f45b638fSMax Reitz # successfully (thus the format is qcow2) or it aborted 148f45b638fSMax Reitz # before the file was changed at all (thus the format stays 149f45b638fSMax Reitz # qcow2). 150f45b638fSMax Reitz _img_info -U | grep 'file format' 151f45b638fSMax Reitz fi 152ba898078SFam Zheng done 1539cd97956SSergio Lopez _send_qemu_cmd $h "{ 'execute': 'quit' }" '' 154ba898078SFam Zheng echo 155ba898078SFam Zheng echo "Round done" 156ba898078SFam Zheng _cleanup_qemu 157ba898078SFam Zhengdone 158ba898078SFam Zheng 1590e1a5827SFam Zhengtest_opts="read-only=off read-only=on read-only=on,force-share=on" 160ba898078SFam Zhengfor opt1 in $test_opts; do 161ba898078SFam Zheng for opt2 in $test_opts; do 162ba898078SFam Zheng echo 163ba898078SFam Zheng echo "== Two devices with the same image ($opt1 - $opt2) ==" 164ba898078SFam Zheng _run_qemu_with_images "${TEST_IMG},$opt1" "${TEST_IMG},$opt2" 165ba898078SFam Zheng done 166ba898078SFam Zhengdone 167ba898078SFam Zheng 1680e1a5827SFam Zhengecho 169ba898078SFam Zhengecho "== Creating ${TEST_IMG}.[abc] ==" | _filter_testdir 170ba898078SFam Zheng( 171ba898078SFam Zheng $QEMU_IMG create -f qcow2 "${TEST_IMG}.a" -b "${TEST_IMG}" 172ba898078SFam Zheng $QEMU_IMG create -f qcow2 "${TEST_IMG}.b" -b "${TEST_IMG}" 173ba898078SFam Zheng $QEMU_IMG create -f qcow2 "${TEST_IMG}.c" -b "${TEST_IMG}.b" 174ba898078SFam Zheng) | _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" 222fcca5dceSFam Zheng_run_cmd $QEMU_IMG create -f $IMGFMT -b "${TEST_IMG}" "${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