Philippe Mathieu-Daudé | 11a82d1 | 2019-03-07 15:58:38 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Vladimir Sementsov-Ogievskiy | 9dd003a | 2021-01-16 16:44:19 +0300 | [diff] [blame] | 2 | # group: rw quick |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 3 | # |
| 4 | # Test image locking |
| 5 | # |
| 6 | # Copyright 2016, 2017 Red Hat, Inc. |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | # |
| 21 | |
| 22 | # creator |
John Snow | 42a5009 | 2022-03-22 13:42:12 -0400 | [diff] [blame] | 23 | owner=fam@euphon.net |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 24 | |
| 25 | seq="$(basename $0)" |
| 26 | echo "QA output created by $seq" |
| 27 | |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 28 | tmp=/tmp/$$ |
| 29 | status=1 # failure is the default! |
| 30 | |
| 31 | _cleanup() |
| 32 | { |
| 33 | _cleanup_test_img |
Max Reitz | f91ecbd | 2019-11-07 17:37:01 +0100 | [diff] [blame] | 34 | for img in "${TEST_IMG}".{base,overlay,convert,a,b,c,lnk}; do |
| 35 | _rm_test_img "$img" |
| 36 | done |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 37 | } |
| 38 | trap "_cleanup; exit \$status" 0 1 2 3 15 |
| 39 | |
| 40 | # get standard environment, filters and checks |
| 41 | . ./common.rc |
| 42 | . ./common.filter |
| 43 | . ./common.qemu |
| 44 | |
| 45 | size=32M |
| 46 | |
| 47 | _check_ofd() |
| 48 | { |
| 49 | _make_test_img $size >/dev/null |
| 50 | if $QEMU_IMG_PROG info --image-opts "driver=file,locking=on,filename=$TEST_IMG" 2>&1 | |
| 51 | grep -q 'falling back to POSIX file'; then |
| 52 | return 1 |
| 53 | else |
| 54 | return 0 |
| 55 | fi |
| 56 | } |
| 57 | |
| 58 | _check_ofd || _notrun "OFD lock not available" |
| 59 | |
| 60 | _supported_fmt qcow2 |
| 61 | _supported_proto file |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 62 | |
| 63 | _run_cmd() |
| 64 | { |
| 65 | echo |
| 66 | (echo "$@"; "$@" 2>&1 1>/dev/null) | _filter_testdir |
| 67 | } |
| 68 | |
Eric Blake | 8cedcff | 2018-11-16 15:50:02 -0600 | [diff] [blame] | 69 | _do_run_qemu() |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 70 | { |
| 71 | ( |
| 72 | if ! test -t 0; then |
| 73 | while read cmd; do |
| 74 | echo $cmd |
| 75 | done |
| 76 | fi |
| 77 | echo quit |
| 78 | ) | $QEMU -nographic -monitor stdio -serial none "$@" 1>/dev/null |
| 79 | } |
| 80 | |
Eric Blake | 8cedcff | 2018-11-16 15:50:02 -0600 | [diff] [blame] | 81 | _run_qemu_with_images() |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 82 | { |
| 83 | _do_run_qemu \ |
| 84 | $(for i in $@; do echo "-drive if=none,file=$i"; done) 2>&1 \ |
| 85 | | _filter_testdir | _filter_qemu |
| 86 | } |
| 87 | |
| 88 | echo "== readonly=off,force-share=on should be rejected ==" |
| 89 | _run_qemu_with_images null-co://,readonly=off,force-share=on |
| 90 | |
| 91 | for opts1 in "" "read-only=on" "read-only=on,force-share=on"; do |
| 92 | echo |
| 93 | echo "== Creating base image ==" |
| 94 | TEST_IMG="${TEST_IMG}.base" _make_test_img $size |
| 95 | |
| 96 | echo |
| 97 | echo "== Creating test image ==" |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 98 | _make_test_img -b "${TEST_IMG}.base" -F $IMGFMT |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 99 | |
| 100 | echo |
| 101 | echo "== Launching QEMU, opts: '$opts1' ==" |
| 102 | _launch_qemu -drive file="${TEST_IMG}",if=none,$opts1 |
| 103 | h=$QEMU_HANDLE |
| 104 | |
| 105 | for opts2 in "" "read-only=on" "read-only=on,force-share=on"; do |
| 106 | echo |
| 107 | echo "== Launching another QEMU, opts: '$opts2' ==" |
| 108 | echo "quit" | \ |
| 109 | $QEMU -nographic -monitor stdio \ |
| 110 | -drive file="${TEST_IMG}",if=none,$opts2 2>&1 1>/dev/null | \ |
| 111 | _filter_testdir | _filter_qemu |
| 112 | done |
| 113 | |
| 114 | for L in "" "-U"; do |
| 115 | |
| 116 | echo |
| 117 | echo "== Running utility commands $L ==" |
| 118 | _run_cmd $QEMU_IO $L -c "read 0 512" "${TEST_IMG}" |
| 119 | _run_cmd $QEMU_IO $L -r -c "read 0 512" "${TEST_IMG}" |
| 120 | _run_cmd $QEMU_IO -c "open $L ${TEST_IMG}" -c "read 0 512" |
| 121 | _run_cmd $QEMU_IO -c "open -r $L ${TEST_IMG}" -c "read 0 512" |
| 122 | _run_cmd $QEMU_IMG info $L "${TEST_IMG}" |
| 123 | _run_cmd $QEMU_IMG check $L "${TEST_IMG}" |
| 124 | _run_cmd $QEMU_IMG compare $L "${TEST_IMG}" "${TEST_IMG}" |
| 125 | _run_cmd $QEMU_IMG map $L "${TEST_IMG}" |
Maxim Levitsky | 44e808c | 2020-05-04 16:19:59 +0300 | [diff] [blame] | 126 | _run_cmd $QEMU_IMG amend -o "size=$size" $L "${TEST_IMG}" |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 127 | _run_cmd $QEMU_IMG commit $L "${TEST_IMG}" |
| 128 | _run_cmd $QEMU_IMG resize $L "${TEST_IMG}" $size |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 129 | _run_cmd $QEMU_IMG rebase $L "${TEST_IMG}" -b "${TEST_IMG}.base" -F $IMGFMT |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 130 | _run_cmd $QEMU_IMG snapshot -l $L "${TEST_IMG}" |
| 131 | _run_cmd $QEMU_IMG convert $L "${TEST_IMG}" "${TEST_IMG}.convert" |
| 132 | _run_cmd $QEMU_IMG dd $L if="${TEST_IMG}" of="${TEST_IMG}.convert" bs=512 count=1 |
| 133 | _run_cmd $QEMU_IMG bench $L -c 1 "${TEST_IMG}" |
| 134 | _run_cmd $QEMU_IMG bench $L -w -c 1 "${TEST_IMG}" |
Max Reitz | f45b638 | 2018-05-09 23:53:36 +0200 | [diff] [blame] | 135 | |
| 136 | # qemu-img create does not support -U |
| 137 | if [ -z "$L" ]; then |
| 138 | _run_cmd $QEMU_IMG create -f $IMGFMT "${TEST_IMG}" \ |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 139 | -b ${TEST_IMG}.base -F $IMGFMT |
Max Reitz | f45b638 | 2018-05-09 23:53:36 +0200 | [diff] [blame] | 140 | # Read the file format. It used to be the case that |
| 141 | # file-posix simply truncated the file, but the qcow2 |
| 142 | # driver then failed to format it because it was unable |
| 143 | # to acquire the necessary WRITE permission. However, the |
| 144 | # truncation was already wrong, and the whole process |
| 145 | # resulted in the file being completely empty and thus its |
| 146 | # format would be detected to be raw. |
| 147 | # So we read it here to see that creation either completed |
| 148 | # successfully (thus the format is qcow2) or it aborted |
| 149 | # before the file was changed at all (thus the format stays |
| 150 | # qcow2). |
| 151 | _img_info -U | grep 'file format' |
| 152 | fi |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 153 | done |
Sergio Lopez | 9cd9795 | 2019-03-15 12:46:55 +0100 | [diff] [blame] | 154 | _send_qemu_cmd $h "{ 'execute': 'quit' }" '' |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 155 | echo |
| 156 | echo "Round done" |
| 157 | _cleanup_qemu |
| 158 | done |
| 159 | |
Fam Zheng | 0e1a582 | 2018-07-11 15:05:19 +0800 | [diff] [blame] | 160 | test_opts="read-only=off read-only=on read-only=on,force-share=on" |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 161 | for opt1 in $test_opts; do |
| 162 | for opt2 in $test_opts; do |
| 163 | echo |
| 164 | echo "== Two devices with the same image ($opt1 - $opt2) ==" |
| 165 | _run_qemu_with_images "${TEST_IMG},$opt1" "${TEST_IMG},$opt2" |
| 166 | done |
| 167 | done |
| 168 | |
Fam Zheng | 0e1a582 | 2018-07-11 15:05:19 +0800 | [diff] [blame] | 169 | echo |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 170 | echo "== Creating ${TEST_IMG}.[abc] ==" | _filter_testdir |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 171 | $QEMU_IMG create -f qcow2 "${TEST_IMG}.a" -b "${TEST_IMG}" -F $IMGFMT | _filter_img_create |
| 172 | $QEMU_IMG create -f qcow2 "${TEST_IMG}.b" -b "${TEST_IMG}" -F $IMGFMT | _filter_img_create |
| 173 | $QEMU_IMG create -f qcow2 "${TEST_IMG}.c" -b "${TEST_IMG}.b" -F $IMGFMT \ |
Max Reitz | 57ee95e | 2020-06-25 14:55:30 +0200 | [diff] [blame] | 174 | | _filter_img_create |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 175 | |
| 176 | echo |
| 177 | echo "== Two devices sharing the same file in backing chain ==" |
| 178 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.b" |
| 179 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.c" |
| 180 | |
| 181 | echo |
| 182 | echo "== Backing image also as an active device ==" |
| 183 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}" |
| 184 | |
| 185 | echo |
| 186 | echo "== Backing image also as an active device (ro) ==" |
| 187 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG},readonly=on" |
| 188 | |
| 189 | echo |
| 190 | echo "== Symbolic link ==" |
| 191 | rm -f "${TEST_IMG}.lnk" &>/dev/null |
| 192 | ln -s ${TEST_IMG} "${TEST_IMG}.lnk" || echo "Failed to create link" |
| 193 | _run_qemu_with_images "${TEST_IMG}.lnk" "${TEST_IMG}" |
| 194 | |
Fam Zheng | de96350 | 2018-03-13 22:20:03 +0800 | [diff] [blame] | 195 | echo |
| 196 | echo "== Active commit to intermediate layer should work when base in use ==" |
| 197 | _launch_qemu -drive format=$IMGFMT,file="${TEST_IMG}.a",id=drive0,if=none \ |
| 198 | -device virtio-blk,drive=drive0 |
| 199 | |
| 200 | _send_qemu_cmd $QEMU_HANDLE \ |
| 201 | "{ 'execute': 'qmp_capabilities' }" \ |
| 202 | 'return' |
| 203 | _run_cmd $QEMU_IMG commit -b "${TEST_IMG}.b" "${TEST_IMG}.c" |
| 204 | |
| 205 | _cleanup_qemu |
| 206 | |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 207 | _launch_qemu |
| 208 | |
| 209 | _send_qemu_cmd $QEMU_HANDLE \ |
| 210 | "{ 'execute': 'qmp_capabilities' }" \ |
| 211 | 'return' |
| 212 | |
| 213 | echo "Adding drive" |
| 214 | _send_qemu_cmd $QEMU_HANDLE \ |
| 215 | "{ 'execute': 'human-monitor-command', |
| 216 | 'arguments': { 'command-line': 'drive_add 0 if=none,id=d0,file=${TEST_IMG}' } }" \ |
Sergio Lopez | 9cd9795 | 2019-03-15 12:46:55 +0100 | [diff] [blame] | 217 | 'return' |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 218 | |
| 219 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' |
| 220 | |
Fam Zheng | fcca5dc | 2017-12-25 10:51:07 +0800 | [diff] [blame] | 221 | echo "Creating overlay with qemu-img when the guest is running should be allowed" |
Eric Blake | b66ff2c | 2020-07-06 15:39:52 -0500 | [diff] [blame] | 222 | _run_cmd $QEMU_IMG create -f $IMGFMT -b "${TEST_IMG}" -F $IMGFMT "${TEST_IMG}.overlay" |
Fam Zheng | fcca5dc | 2017-12-25 10:51:07 +0800 | [diff] [blame] | 223 | |
| 224 | echo "== Closing an image should unlock it ==" |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 225 | _send_qemu_cmd $QEMU_HANDLE \ |
| 226 | "{ 'execute': 'human-monitor-command', |
| 227 | 'arguments': { 'command-line': 'drive_del d0' } }" \ |
Sergio Lopez | 9cd9795 | 2019-03-15 12:46:55 +0100 | [diff] [blame] | 228 | 'return' |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 229 | |
| 230 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' |
| 231 | |
| 232 | echo "Adding two and closing one" |
| 233 | for d in d0 d1; do |
| 234 | _send_qemu_cmd $QEMU_HANDLE \ |
| 235 | "{ 'execute': 'human-monitor-command', |
| 236 | 'arguments': { 'command-line': 'drive_add 0 if=none,id=$d,file=${TEST_IMG},readonly=on' } }" \ |
Sergio Lopez | 9cd9795 | 2019-03-15 12:46:55 +0100 | [diff] [blame] | 237 | 'return' |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 238 | done |
| 239 | |
| 240 | _run_cmd $QEMU_IMG info "${TEST_IMG}" |
| 241 | |
| 242 | _send_qemu_cmd $QEMU_HANDLE \ |
| 243 | "{ 'execute': 'human-monitor-command', |
| 244 | 'arguments': { 'command-line': 'drive_del d0' } }" \ |
Sergio Lopez | 9cd9795 | 2019-03-15 12:46:55 +0100 | [diff] [blame] | 245 | 'return' |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 246 | |
| 247 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' |
| 248 | |
| 249 | echo "Closing the other" |
| 250 | _send_qemu_cmd $QEMU_HANDLE \ |
| 251 | "{ 'execute': 'human-monitor-command', |
| 252 | 'arguments': { 'command-line': 'drive_del d1' } }" \ |
Sergio Lopez | 9cd9795 | 2019-03-15 12:46:55 +0100 | [diff] [blame] | 253 | 'return' |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 254 | |
| 255 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' |
| 256 | |
| 257 | _cleanup_qemu |
| 258 | |
Max Reitz | 4e7d73c | 2018-05-02 22:20:51 +0200 | [diff] [blame] | 259 | echo |
| 260 | echo "== Detecting -U and force-share conflicts ==" |
| 261 | |
| 262 | echo |
| 263 | echo 'No conflict:' |
| 264 | $QEMU_IMG info -U --image-opts driver=null-co,force-share=on |
| 265 | echo |
| 266 | echo 'Conflict:' |
| 267 | $QEMU_IMG info -U --image-opts driver=null-co,force-share=off |
| 268 | |
| 269 | echo |
| 270 | echo 'No conflict:' |
| 271 | $QEMU_IO -c 'open -r -U -o driver=null-co,force-share=on' |
| 272 | echo |
| 273 | echo 'Conflict:' |
| 274 | $QEMU_IO -c 'open -r -U -o driver=null-co,force-share=off' |
| 275 | |
Fam Zheng | ba89807 | 2017-05-03 00:35:57 +0800 | [diff] [blame] | 276 | # success, all done |
| 277 | echo "*** done" |
| 278 | rm -f $seq.full |
| 279 | status=0 |