111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw quick 3de9efdb3SFam Zheng# 4de9efdb3SFam Zheng# Test image locking for POSIX locks 5de9efdb3SFam Zheng# 6de9efdb3SFam Zheng# Copyright 2017 Red Hat, Inc. 7de9efdb3SFam Zheng# 8de9efdb3SFam Zheng# This program is free software; you can redistribute it and/or modify 9de9efdb3SFam Zheng# it under the terms of the GNU General Public License as published by 10de9efdb3SFam Zheng# the Free Software Foundation; either version 2 of the License, or 11de9efdb3SFam Zheng# (at your option) any later version. 12de9efdb3SFam Zheng# 13de9efdb3SFam Zheng# This program is distributed in the hope that it will be useful, 14de9efdb3SFam Zheng# but WITHOUT ANY WARRANTY; without even the implied warranty of 15de9efdb3SFam Zheng# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16de9efdb3SFam Zheng# GNU General Public License for more details. 17de9efdb3SFam Zheng# 18de9efdb3SFam Zheng# You should have received a copy of the GNU General Public License 19de9efdb3SFam Zheng# along with this program. If not, see <http://www.gnu.org/licenses/>. 20de9efdb3SFam Zheng# 21de9efdb3SFam Zheng 22de9efdb3SFam Zheng# creator 23*42a5009dSJohn Snowowner=fam@euphon.net 24de9efdb3SFam Zheng 25de9efdb3SFam Zhengseq="$(basename $0)" 26de9efdb3SFam Zhengecho "QA output created by $seq" 27de9efdb3SFam Zheng 28de9efdb3SFam Zhengtmp=/tmp/$$ 29de9efdb3SFam Zhengstatus=1 # failure is the default! 30de9efdb3SFam Zheng 31de9efdb3SFam Zheng_cleanup() 32de9efdb3SFam Zheng{ 33de9efdb3SFam Zheng _cleanup_test_img 34f91ecbd7SMax Reitz _rm_test_img "$TEST_IMG.overlay" 35748f831bSMax Reitz rm -f "$SOCK_DIR/nbd.socket" 36de9efdb3SFam Zheng} 37de9efdb3SFam Zhengtrap "_cleanup; exit \$status" 0 1 2 3 15 38de9efdb3SFam Zheng 39de9efdb3SFam Zheng# get standard environment, filters and checks 40de9efdb3SFam Zheng. ./common.rc 41de9efdb3SFam Zheng. ./common.filter 42de9efdb3SFam Zheng. ./common.qemu 43de9efdb3SFam Zheng 44de9efdb3SFam Zheng_supported_fmt qcow2 45de9efdb3SFam Zheng_supported_proto file 46de9efdb3SFam Zheng 47de9efdb3SFam Zhengsize=32M 48de9efdb3SFam Zheng 49de9efdb3SFam Zheng_make_test_img $size 50de9efdb3SFam Zheng 51de9efdb3SFam Zhengecho "Starting QEMU" 52de9efdb3SFam Zheng_launch_qemu -drive file=$TEST_IMG,if=none,id=drive0,file.locking=on \ 5322329f0dSLaurent Vivier -device virtio-blk,drive=drive0 54de9efdb3SFam Zheng 55de9efdb3SFam Zhengecho 56de9efdb3SFam Zhengecho "Starting a second QEMU using the same image should fail" 57c60f6fcfSKevin Wolfecho 'quit' | $QEMU -nographic -monitor stdio \ 58de9efdb3SFam Zheng -drive file=$TEST_IMG,if=none,id=drive0,file.locking=on \ 5922329f0dSLaurent Vivier -device virtio-blk,drive=drive0 2>&1 | _filter_testdir 2>&1 | 60de9efdb3SFam Zheng _filter_qemu | 61de9efdb3SFam Zheng sed -e '/falling back to POSIX file/d' \ 62de9efdb3SFam Zheng -e '/locks can be lost unexpectedly/d' 63de9efdb3SFam Zheng 64de9efdb3SFam Zheng_cleanup_qemu 65de9efdb3SFam Zheng 666d0a4a0fSMax Reitzecho 676d0a4a0fSMax Reitzecho '=== Testing reopen ===' 686d0a4a0fSMax Reitzecho 696d0a4a0fSMax Reitz 706d0a4a0fSMax Reitz# This tests that reopening does not unshare any permissions it should 716d0a4a0fSMax Reitz# not unshare 726d0a4a0fSMax Reitz# (There was a bug where reopening shared exactly the opposite of the 736d0a4a0fSMax Reitz# permissions it was supposed to share) 746d0a4a0fSMax Reitz 756d0a4a0fSMax Reitz_launch_qemu 766d0a4a0fSMax Reitz 776d0a4a0fSMax Reitz_send_qemu_cmd $QEMU_HANDLE \ 786d0a4a0fSMax Reitz "{'execute': 'qmp_capabilities'}" \ 796d0a4a0fSMax Reitz 'return' 806d0a4a0fSMax Reitz 816d0a4a0fSMax Reitz# Open the image without any format layer (we are not going to access 826d0a4a0fSMax Reitz# it, so that is fine) 836d0a4a0fSMax Reitz# This should keep all permissions shared. 846d0a4a0fSMax Reitzsuccess_or_failure=y _send_qemu_cmd $QEMU_HANDLE \ 856d0a4a0fSMax Reitz "{'execute': 'blockdev-add', 866d0a4a0fSMax Reitz 'arguments': { 876d0a4a0fSMax Reitz 'node-name': 'node0', 886d0a4a0fSMax Reitz 'driver': 'file', 896d0a4a0fSMax Reitz 'filename': '$TEST_IMG', 906d0a4a0fSMax Reitz 'locking': 'on' 916d0a4a0fSMax Reitz } }" \ 926d0a4a0fSMax Reitz 'return' \ 936d0a4a0fSMax Reitz 'error' 946d0a4a0fSMax Reitz 956d0a4a0fSMax Reitz# This snapshot will perform a reopen to drop R/W to RO. 966d0a4a0fSMax Reitz# It should still keep all permissions shared. 976d0a4a0fSMax Reitzsuccess_or_failure=y _send_qemu_cmd $QEMU_HANDLE \ 986d0a4a0fSMax Reitz "{'execute': 'blockdev-snapshot-sync', 996d0a4a0fSMax Reitz 'arguments': { 1006d0a4a0fSMax Reitz 'node-name': 'node0', 1016d0a4a0fSMax Reitz 'snapshot-file': '$TEST_IMG.overlay', 1026d0a4a0fSMax Reitz 'snapshot-node-name': 'node1' 1036d0a4a0fSMax Reitz } }" \ 1046d0a4a0fSMax Reitz 'return' \ 1056d0a4a0fSMax Reitz 'error' 1066d0a4a0fSMax Reitz 1076d0a4a0fSMax Reitz# Now open the same file again 1086d0a4a0fSMax Reitz# This does not require any permissions (and does not unshare any), so 1096d0a4a0fSMax Reitz# this will not conflict with node0. 1106d0a4a0fSMax Reitzsuccess_or_failure=y _send_qemu_cmd $QEMU_HANDLE \ 1116d0a4a0fSMax Reitz "{'execute': 'blockdev-add', 1126d0a4a0fSMax Reitz 'arguments': { 1136d0a4a0fSMax Reitz 'node-name': 'node1', 1146d0a4a0fSMax Reitz 'driver': 'file', 1156d0a4a0fSMax Reitz 'filename': '$TEST_IMG', 1166d0a4a0fSMax Reitz 'locking': 'on' 1176d0a4a0fSMax Reitz } }" \ 1186d0a4a0fSMax Reitz 'return' \ 1196d0a4a0fSMax Reitz 'error' 1206d0a4a0fSMax Reitz 1218fabb8beSMax Reitz# Start an NBD server to which we can attach node1 1228fabb8beSMax Reitzsuccess_or_failure=y _send_qemu_cmd $QEMU_HANDLE \ 1238fabb8beSMax Reitz "{'execute': 'nbd-server-start', 1248fabb8beSMax Reitz 'arguments': { 1258fabb8beSMax Reitz 'addr': { 1268fabb8beSMax Reitz 'type': 'unix', 1278fabb8beSMax Reitz 'data': { 128748f831bSMax Reitz 'path': '$SOCK_DIR/nbd.socket' 1298fabb8beSMax Reitz } } } }" \ 1308fabb8beSMax Reitz 'return' \ 1318fabb8beSMax Reitz 'error' 1328fabb8beSMax Reitz 1338fabb8beSMax Reitz# Now we attach the image to the NBD server. This server does require 1348fabb8beSMax Reitz# some permissions (at least WRITE and READ_CONSISTENT), so if 1356d0a4a0fSMax Reitz# reopening node0 unshared any (which it should not have), this will 1366d0a4a0fSMax Reitz# fail (but it should not). 1376d0a4a0fSMax Reitzsuccess_or_failure=y _send_qemu_cmd $QEMU_HANDLE \ 1388fabb8beSMax Reitz "{'execute': 'nbd-server-add', 1396d0a4a0fSMax Reitz 'arguments': { 1408fabb8beSMax Reitz 'device': 'node1' 1416d0a4a0fSMax Reitz } }" \ 1426d0a4a0fSMax Reitz 'return' \ 1436d0a4a0fSMax Reitz 'error' 1446d0a4a0fSMax Reitz 1456d0a4a0fSMax Reitz_cleanup_qemu 1466d0a4a0fSMax Reitz 147dc68c9d3SMax Reitzecho 148dc68c9d3SMax Reitzecho '=== Testing failure to loosen restrictions ===' 149dc68c9d3SMax Reitzecho 150dc68c9d3SMax Reitz 151dc68c9d3SMax Reitz_launch_qemu -drive file=$TEST_IMG,if=none,file.locking=on 152dc68c9d3SMax Reitz 153dc68c9d3SMax Reitz_send_qemu_cmd $QEMU_HANDLE \ 154dc68c9d3SMax Reitz "{'execute': 'qmp_capabilities'}" \ 155dc68c9d3SMax Reitz 'return' 156dc68c9d3SMax Reitz 157dc68c9d3SMax Reitz_cleanup_test_img 158dc68c9d3SMax Reitz 159dc68c9d3SMax Reitz# When quitting qemu, it will try to drop its locks on the test image. 160dc68c9d3SMax Reitz# Because that file no longer exists, it will be unable to do so. 161dc68c9d3SMax Reitz# However, that is not fatal, so it should just move on. 162dc68c9d3SMax Reitz_send_qemu_cmd $QEMU_HANDLE \ 163dc68c9d3SMax Reitz "{'execute': 'quit'}" \ 164dc68c9d3SMax Reitz 'return' 165dc68c9d3SMax Reitz 166dc68c9d3SMax Reitzwait=1 _cleanup_qemu 167dc68c9d3SMax Reitz 168de9efdb3SFam Zheng# success, all done 169de9efdb3SFam Zhengecho "*** done" 170de9efdb3SFam Zhengrm -f $seq.full 171de9efdb3SFam Zhengstatus=0 172