master
text 172 lines 4.49 KB
Raw
1 #!/usr/bin/env bash
2 # group: rw quick
3 #
4 # Test image locking for POSIX locks
5 #
6 # Copyright 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
23 owner=fam@euphon.net
24
25 seq="$(basename $0)"
26 echo "QA output created by $seq"
27
28 tmp=/tmp/$$
29 status=1 # failure is the default!
30
31 _cleanup()
32 {
33 _cleanup_test_img
34 _rm_test_img "$TEST_IMG.overlay"
35 rm -f "$SOCK_DIR/nbd.socket"
36 }
37 trap "_cleanup; exit \$status" 0 1 2 3 15
38
39 # get standard environment, filters and checks
40 . ./common.rc
41 . ./common.filter
42 . ./common.qemu
43
44 _supported_fmt qcow2
45 _supported_proto file
46 _supported_os Linux
47
48 size=32M
49
50 _make_test_img $size
51
52 echo "Starting QEMU"
53 _launch_qemu -drive file=$TEST_IMG,if=none,id=drive0,file.locking=on \
54 -device virtio-blk,drive=drive0
55
56 echo
57 echo "Starting a second QEMU using the same image should fail"
58 echo 'quit' | $QEMU -nographic -monitor stdio \
59 -drive file=$TEST_IMG,if=none,id=drive0,file.locking=on \
60 -device virtio-blk,drive=drive0 2>&1 | _filter_testdir 2>&1 |
61 _filter_qemu |
62 sed -e '/falling back to POSIX file/d' \
63 -e '/locks can be lost unexpectedly/d'
64
65 _cleanup_qemu
66
67 echo
68 echo '=== Testing reopen ==='
69 echo
70
71 # This tests that reopening does not unshare any permissions it should
72 # not unshare
73 # (There was a bug where reopening shared exactly the opposite of the
74 # permissions it was supposed to share)
75
76 _launch_qemu
77
78 _send_qemu_cmd $QEMU_HANDLE \
79 "{'execute': 'qmp_capabilities'}" \
80 'return'
81
82 # Open the image without any format layer (we are not going to access
83 # it, so that is fine)
84 # This should keep all permissions shared.
85 success_or_failure=y _send_qemu_cmd $QEMU_HANDLE \
86 "{'execute': 'blockdev-add',
87 'arguments': {
88 'node-name': 'node0',
89 'driver': 'file',
90 'filename': '$TEST_IMG',
91 'locking': 'on'
92 } }" \
93 'return' \
94 'error'
95
96 # This snapshot will perform a reopen to drop R/W to RO.
97 # It should still keep all permissions shared.
98 success_or_failure=y _send_qemu_cmd $QEMU_HANDLE \
99 "{'execute': 'blockdev-snapshot-sync',
100 'arguments': {
101 'node-name': 'node0',
102 'snapshot-file': '$TEST_IMG.overlay',
103 'snapshot-node-name': 'node1'
104 } }" \
105 'return' \
106 'error'
107
108 # Now open the same file again
109 # This does not require any permissions (and does not unshare any), so
110 # this will not conflict with node0.
111 success_or_failure=y _send_qemu_cmd $QEMU_HANDLE \
112 "{'execute': 'blockdev-add',
113 'arguments': {
114 'node-name': 'node1',
115 'driver': 'file',
116 'filename': '$TEST_IMG',
117 'locking': 'on'
118 } }" \
119 'return' \
120 'error'
121
122 # Start an NBD server to which we can attach node1
123 success_or_failure=y _send_qemu_cmd $QEMU_HANDLE \
124 "{'execute': 'nbd-server-start',
125 'arguments': {
126 'addr': {
127 'type': 'unix',
128 'data': {
129 'path': '$SOCK_DIR/nbd.socket'
130 } } } }" \
131 'return' \
132 'error'
133
134 # Now we attach the image to the NBD server. This server does require
135 # some permissions (at least WRITE and READ_CONSISTENT), so if
136 # reopening node0 unshared any (which it should not have), this will
137 # fail (but it should not).
138 success_or_failure=y _send_qemu_cmd $QEMU_HANDLE \
139 "{'execute': 'nbd-server-add',
140 'arguments': {
141 'device': 'node1'
142 } }" \
143 'return' \
144 'error'
145
146 _cleanup_qemu
147
148 echo
149 echo '=== Testing failure to loosen restrictions ==='
150 echo
151
152 _launch_qemu -drive file=$TEST_IMG,if=none,file.locking=on
153
154 _send_qemu_cmd $QEMU_HANDLE \
155 "{'execute': 'qmp_capabilities'}" \
156 'return'
157
158 _cleanup_test_img
159
160 # When quitting qemu, it will try to drop its locks on the test image.
161 # Because that file no longer exists, it will be unable to do so.
162 # However, that is not fatal, so it should just move on.
163 _send_qemu_cmd $QEMU_HANDLE \
164 "{'execute': 'quit'}" \
165 'return'
166
167 wait=1 _cleanup_qemu
168
169 # success, all done
170 echo "*** done"
171 rm -f $seq.full
172 status=0