| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Test for configuring cache modes of arbitrary nodes (requires O_DIRECT) |
| 4 | # |
| 5 | # Copyright (C) 2015 Red Hat, Inc. |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | # |
| 20 | |
| 21 | # creator |
| 22 | owner=kwolf@redhat.com |
| 23 | |
| 24 | seq=`basename $0` |
| 25 | echo "QA output created by $seq" |
| 26 | |
| 27 | status=1 # failure is the default! |
| 28 | |
| 29 | _cleanup() |
| 30 | { |
| 31 | _cleanup_test_img |
| 32 | _rm_test_img "$TEST_IMG.snap" |
| 33 | } |
| 34 | trap "_cleanup; exit \$status" 0 1 2 3 15 |
| 35 | |
| 36 | # get standard environment, filters and checks |
| 37 | . ./common.rc |
| 38 | . ./common.filter |
| 39 | |
| 40 | _supported_fmt qcow2 |
| 41 | _supported_proto file |
| 42 | |
| 43 | # We test all cache modes anyway, but O_DIRECT needs to be supported |
| 44 | _default_cache_mode none |
| 45 | _supported_cache_modes none directsync |
| 46 | _require_hmp |
| 47 | |
| 48 | do_run_qemu() |
| 49 | { |
| 50 | echo Testing: "$@" |
| 51 | ( |
| 52 | if ! test -t 0; then |
| 53 | while read cmd; do |
| 54 | echo $cmd |
| 55 | done |
| 56 | fi |
| 57 | echo quit |
| 58 | ) | $QEMU -nographic -monitor stdio -nodefaults "$@" |
| 59 | echo |
| 60 | } |
| 61 | |
| 62 | run_qemu() |
| 63 | { |
| 64 | do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_qemu | _filter_hmp |
| 65 | } |
| 66 | |
| 67 | size=128M |
| 68 | |
| 69 | TEST_IMG="$TEST_IMG.base" _make_test_img $size |
| 70 | TEST_IMG="$TEST_IMG.snap" _make_test_img $size |
| 71 | _make_test_img -b "$TEST_IMG.base" $size -F $IMGFMT |
| 72 | |
| 73 | echo |
| 74 | echo === Simple test for all cache modes === |
| 75 | echo |
| 76 | |
| 77 | run_qemu -drive file="$TEST_IMG",cache=none |
| 78 | run_qemu -drive file="$TEST_IMG",cache=directsync |
| 79 | run_qemu -drive file="$TEST_IMG",cache=writeback |
| 80 | run_qemu -drive file="$TEST_IMG",cache=writethrough |
| 81 | run_qemu -drive file="$TEST_IMG",cache=unsafe |
| 82 | run_qemu -drive file="$TEST_IMG",cache=invalid_value |
| 83 | |
| 84 | echo |
| 85 | echo === Check inheritance of cache modes === |
| 86 | echo |
| 87 | |
| 88 | files="if=none,file=$TEST_IMG,backing.file.filename=$TEST_IMG.base" |
| 89 | ids="node-name=image,backing.node-name=backing,backing.file.node-name=backing-file,file.node-name=file" |
| 90 | |
| 91 | check_cache_all() |
| 92 | { |
| 93 | # cache.direct is supposed to be inherited by both bs->file and |
| 94 | # bs->backing |
| 95 | |
| 96 | printf "cache.direct=on on none0\n" |
| 97 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",cache.direct=on | grep -e "Cache" -e "[Cc]annot|[Cc]ould not|[Cc]an't" |
| 98 | printf "\ncache.direct=on on file\n" |
| 99 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",file.cache.direct=on | grep -e "Cache" -e "[Cc]annot|[Cc]ould not|[Cc]an't" |
| 100 | printf "\ncache.direct=on on backing\n" |
| 101 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.cache.direct=on | grep -e "Cache" -e "[Cc]annot|[Cc]ould not|[Cc]an't" |
| 102 | printf "\ncache.direct=on on backing-file\n" |
| 103 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.file.cache.direct=on | grep -e "Cache" -e "[Cc]annot|[Cc]ould not|[Cc]an't" |
| 104 | |
| 105 | # cache.writeback is supposed to be inherited by bs->backing; bs->file |
| 106 | # always gets cache.writeback=on |
| 107 | |
| 108 | printf "\n\ncache.writeback=off on none0\n" |
| 109 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",cache.writeback=off | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 110 | printf "\ncache.writeback=off on file\n" |
| 111 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",file.cache.writeback=off | grep -e "doesn't" -e "does not" |
| 112 | printf "\ncache.writeback=off on backing\n" |
| 113 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.cache.writeback=off | grep -e "doesn't" -e "does not" |
| 114 | printf "\ncache.writeback=off on backing-file\n" |
| 115 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.file.cache.writeback=off | grep -e "doesn't" -e "does not" |
| 116 | |
| 117 | # cache.no-flush is supposed to be inherited by both bs->file and bs->backing |
| 118 | |
| 119 | printf "\n\ncache.no-flush=on on none0\n" |
| 120 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 121 | printf "\ncache.no-flush=on on file\n" |
| 122 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",file.cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 123 | printf "\ncache.no-flush=on on backing\n" |
| 124 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 125 | printf "\ncache.no-flush=on on backing-file\n" |
| 126 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.file.cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 127 | } |
| 128 | |
| 129 | echo |
| 130 | echo "--- Configure cache modes on the command line ---" |
| 131 | echo |
| 132 | |
| 133 | # First check the inherited cache mode after opening the image. |
| 134 | |
| 135 | hmp_cmds="info block none0 |
| 136 | info block image |
| 137 | info block file |
| 138 | info block backing |
| 139 | info block backing-file" |
| 140 | |
| 141 | check_cache_all |
| 142 | |
| 143 | echo |
| 144 | echo "--- Cache modes after reopen (live snapshot) ---" |
| 145 | echo |
| 146 | |
| 147 | # Then trigger a reopen and check that the cache modes are still the same. |
| 148 | |
| 149 | hmp_cmds="snapshot_blkdev -n none0 $TEST_IMG.snap $IMGFMT |
| 150 | info block |
| 151 | info block image |
| 152 | info block file |
| 153 | info block backing |
| 154 | info block backing-file" |
| 155 | |
| 156 | check_cache_all |
| 157 | |
| 158 | echo |
| 159 | echo "--- Change cache modes with reopen (qemu-io command, flags) ---" |
| 160 | echo |
| 161 | |
| 162 | # This one actually changes the cache mode with the reopen. For this test, the |
| 163 | # new cache mode is specified in the flags, not as an option. |
| 164 | |
| 165 | hmp_cmds='qemu-io none0 "reopen -c none" |
| 166 | info block none0 |
| 167 | info block image |
| 168 | info block file |
| 169 | info block backing |
| 170 | info block backing-file' |
| 171 | |
| 172 | check_cache_all |
| 173 | |
| 174 | echo |
| 175 | echo "--- Change cache modes with reopen (qemu-io command, options) ---" |
| 176 | echo |
| 177 | |
| 178 | # This one actually changes the cache mode with the reopen. For this test, the |
| 179 | # new cache mode is specified as an option, not in the flags. |
| 180 | |
| 181 | hmp_cmds='qemu-io none0 "reopen -o cache.direct=on" |
| 182 | info block none0 |
| 183 | info block image |
| 184 | info block file |
| 185 | info block backing |
| 186 | info block backing-file' |
| 187 | |
| 188 | check_cache_all |
| 189 | |
| 190 | echo |
| 191 | echo "--- Change cache modes after snapshot ---" |
| 192 | echo |
| 193 | |
| 194 | # This checks that the original image doesn't inherit from the snapshot |
| 195 | |
| 196 | hmp_cmds="snapshot_blkdev -n none0 $TEST_IMG.snap $IMGFMT |
| 197 | qemu-io none0 \"reopen -c none\" |
| 198 | info block none0 |
| 199 | info block image |
| 200 | info block file |
| 201 | info block backing |
| 202 | info block backing-file" |
| 203 | |
| 204 | check_cache_all |
| 205 | |
| 206 | echo |
| 207 | echo "--- Change cache mode in parent, child has explicit option in JSON ---" |
| 208 | echo |
| 209 | |
| 210 | # This checks that children with options explicitly set by the json: |
| 211 | # pseudo-protocol don't inherit these options from their parents. |
| 212 | # |
| 213 | # Yes, blkdebug::json:... is criminal, but I can't see another way to have a |
| 214 | # BDS initialised with the json: pseudo-protocol, but still have it inherit |
| 215 | # options from its parent node. |
| 216 | |
| 217 | hmp_cmds="qemu-io none0 \"reopen -o cache.direct=on,cache.no-flush=on\" |
| 218 | info block none0 |
| 219 | info block image |
| 220 | info block blkdebug |
| 221 | info block file" |
| 222 | |
| 223 | echo "$hmp_cmds" | run_qemu -drive if=none,file="blkdebug::json:{\"filename\":\"$TEST_IMG\",,\"cache\":{\"direct\":false}}",node-name=image,file.node-name=blkdebug,file.image.node-name=file | grep "Cache" |
| 224 | |
| 225 | echo |
| 226 | echo "=== Check that referenced BDSes don't inherit ===" |
| 227 | echo |
| 228 | |
| 229 | drv_bkfile="if=none,driver=file,filename=$TEST_IMG.base,node-name=backing-file" |
| 230 | drv_bk="if=none,file=json:{'driver':'$IMGFMT',,'file':'backing-file',,'node-name':'backing'}" |
| 231 | drv_file="if=none,driver=file,filename=$TEST_IMG,node-name=file" |
| 232 | drv_img="if=none,id=blk,file=json:{'driver':'$IMGFMT',,'file':'file',,'backing':'backing',,'node-name':'image'}" |
| 233 | |
| 234 | check_cache_all_separate() |
| 235 | { |
| 236 | # Check cache.direct |
| 237 | |
| 238 | printf "cache.direct=on on blk\n" |
| 239 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img",cache.direct=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 240 | printf "\ncache.direct=on on file\n" |
| 241 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file",cache.direct=on -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 242 | printf "\ncache.direct=on on backing\n" |
| 243 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk",cache.direct=on -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 244 | printf "\ncache.direct=on on backing-file\n" |
| 245 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile",cache.direct=on -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 246 | |
| 247 | # Check cache.writeback |
| 248 | |
| 249 | printf "\n\ncache.writeback=off on blk\n" |
| 250 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img",cache.writeback=off | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 251 | printf "\ncache.writeback=off on file\n" |
| 252 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file",cache.writeback=off -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 253 | printf "\ncache.writeback=off on backing\n" |
| 254 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk",cache.writeback=off -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 255 | printf "\ncache.writeback=off on backing-file\n" |
| 256 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile",cache.writeback=off -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 257 | |
| 258 | # Check cache.no-flush |
| 259 | |
| 260 | printf "\n\ncache.no-flush=on on blk\n" |
| 261 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img",cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 262 | printf "\ncache.no-flush=on on file\n" |
| 263 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file",cache.no-flush=on -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 264 | printf "\ncache.no-flush=on on backing\n" |
| 265 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk",cache.no-flush=on -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 266 | printf "\ncache.no-flush=on on backing-file\n" |
| 267 | echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile",cache.no-flush=on -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't" |
| 268 | } |
| 269 | |
| 270 | echo |
| 271 | echo "--- Configure cache modes on the command line ---" |
| 272 | echo |
| 273 | |
| 274 | # First check the inherited cache mode after opening the image. |
| 275 | |
| 276 | hmp_cmds="info block image |
| 277 | info block file |
| 278 | info block backing |
| 279 | info block backing-file" |
| 280 | |
| 281 | check_cache_all_separate |
| 282 | |
| 283 | echo |
| 284 | echo "--- Cache modes after reopen (live snapshot) ---" |
| 285 | echo |
| 286 | |
| 287 | # Then trigger a reopen and check that the cache modes are still the same. |
| 288 | |
| 289 | hmp_cmds="snapshot_blkdev -n blk $TEST_IMG.snap $IMGFMT |
| 290 | info block blk |
| 291 | info block image |
| 292 | info block file |
| 293 | info block backing |
| 294 | info block backing-file" |
| 295 | |
| 296 | check_cache_all_separate |
| 297 | |
| 298 | echo |
| 299 | echo "--- Change cache modes with reopen (qemu-io command, flags) ---" |
| 300 | echo |
| 301 | |
| 302 | # This one actually changes the cache mode with the reopen. For this test, the |
| 303 | # new cache mode is specified as flags, not as option. |
| 304 | |
| 305 | hmp_cmds='qemu-io blk "reopen -c none" |
| 306 | info block image |
| 307 | info block file |
| 308 | info block backing |
| 309 | info block backing-file' |
| 310 | |
| 311 | check_cache_all_separate |
| 312 | |
| 313 | |
| 314 | echo |
| 315 | echo "=== Reopening children instead of the root ===" |
| 316 | echo |
| 317 | |
| 318 | files="if=none,file=$TEST_IMG,backing.file.filename=$TEST_IMG.base" |
| 319 | ids="node-name=image,backing.node-name=backing,backing.file.node-name=backing-file,file.node-name=file" |
| 320 | |
| 321 | echo |
| 322 | echo "--- Basic reopen ---" |
| 323 | echo |
| 324 | |
| 325 | hmp_cmds='qemu-io none0 "reopen -o backing.cache.direct=on" |
| 326 | info block none0 |
| 327 | info block image |
| 328 | info block file |
| 329 | info block backing |
| 330 | info block backing-file' |
| 331 | |
| 332 | check_cache_all |
| 333 | |
| 334 | echo |
| 335 | echo "--- Change cache mode after reopening child ---" |
| 336 | echo |
| 337 | |
| 338 | # This checks that children with options explicitly set with reopen don't |
| 339 | # inherit these options from their parents any more |
| 340 | |
| 341 | # TODO Implement node-name support for 'qemu-io' HMP command for -c |
| 342 | # Can use only -o to access child node options for now |
| 343 | |
| 344 | hmp_cmds="qemu-io none0 \"reopen -o file.cache.direct=off,file.cache.no-flush=off\" |
| 345 | qemu-io none0 \"reopen -o backing.file.cache.direct=off,backing.file.cache.no-flush=on\" |
| 346 | qemu-io none0 \"reopen -c none\" |
| 347 | info block image |
| 348 | info block file |
| 349 | info block backing |
| 350 | info block backing-file" |
| 351 | |
| 352 | echo "$hmp_cmds" | run_qemu -drive "$files","$ids" | grep "Cache" |
| 353 | |
| 354 | echo |
| 355 | echo "--- Alignment after changing O_DIRECT ---" |
| 356 | echo |
| 357 | |
| 358 | # Directly test the protocol level: Can unaligned requests succeed even if |
| 359 | # O_DIRECT was only enabled through a reopen and vice versa? |
| 360 | |
| 361 | # Ensure image size is a multiple of the sector size (required for O_DIRECT) |
| 362 | $QEMU_IMG create -f file "$TEST_IMG" 1M | _filter_img_create |
| 363 | |
| 364 | # And write some data (not strictly necessary, but it feels better to actually |
| 365 | # have something to be read) |
| 366 | $QEMU_IO -f file -c 'write 0 4096' "$TEST_IMG" | _filter_qemu_io |
| 367 | |
| 368 | $QEMU_IO --cache=writeback -f file $TEST_IMG <<EOF | _filter_qemu_io |
| 369 | read 42 42 |
| 370 | reopen -o cache.direct=on |
| 371 | read 42 42 |
| 372 | reopen -o cache.direct=off |
| 373 | read 42 42 |
| 374 | EOF |
| 375 | $QEMU_IO --cache=none -f file $TEST_IMG <<EOF | _filter_qemu_io |
| 376 | read 42 42 |
| 377 | reopen -o cache.direct=off |
| 378 | read 42 42 |
| 379 | reopen -o cache.direct=on |
| 380 | read 42 42 |
| 381 | EOF |
| 382 | |
| 383 | # success, all done |
| 384 | echo "*** done" |
| 385 | rm -f $seq.full |
| 386 | status=0 |