]> git.proxmox.com Git - ceph.git/blob - ceph/qa/workunits/rbd/rbd-nbd.sh
import ceph quincy 17.2.6
[ceph.git] / ceph / qa / workunits / rbd / rbd-nbd.sh
1 #!/usr/bin/env bash
2 set -ex
3
4 . $(dirname $0)/../../standalone/ceph-helpers.sh
5
6 POOL=rbd
7 ANOTHER_POOL=new_default_pool$$
8 NS=ns
9 IMAGE=testrbdnbd$$
10 SIZE=64
11 DATA=
12 DEV=
13
14 _sudo()
15 {
16 local cmd
17
18 if [ `id -u` -eq 0 ]
19 then
20 "$@"
21 return $?
22 fi
23
24 # Look for the command in the user path. If it fails run it as is,
25 # supposing it is in sudo path.
26 cmd=`which $1 2>/dev/null` || cmd=$1
27 shift
28 sudo -nE "${cmd}" "$@"
29 }
30
31 setup()
32 {
33 local ns x
34
35 if [ -e CMakeCache.txt ]; then
36 # running under cmake build dir
37
38 CEPH_SRC=$(readlink -f $(dirname $0)/../../../src)
39 CEPH_ROOT=${PWD}
40 CEPH_BIN=${CEPH_ROOT}/bin
41
42 export LD_LIBRARY_PATH=${CEPH_ROOT}/lib:${LD_LIBRARY_PATH}
43 export PYTHONPATH=${PYTHONPATH}:${CEPH_SRC}/pybind:${CEPH_ROOT}/lib/cython_modules/lib.3
44 PATH=${CEPH_BIN}:${PATH}
45 fi
46
47 _sudo echo test sudo
48
49 trap cleanup INT TERM EXIT
50 TEMPDIR=`mktemp -d`
51 DATA=${TEMPDIR}/data
52 dd if=/dev/urandom of=${DATA} bs=1M count=${SIZE}
53
54 rbd namespace create ${POOL}/${NS}
55
56 for ns in '' ${NS}; do
57 rbd --dest-pool ${POOL} --dest-namespace "${ns}" --no-progress import \
58 ${DATA} ${IMAGE}
59 done
60
61 # create another pool
62 ceph osd pool create ${ANOTHER_POOL} 8
63 rbd pool init ${ANOTHER_POOL}
64 }
65
66 function cleanup()
67 {
68 local ns s
69
70 set +e
71
72 mount | fgrep ${TEMPDIR}/mnt && _sudo umount -f ${TEMPDIR}/mnt
73
74 rm -Rf ${TEMPDIR}
75 if [ -n "${DEV}" ]
76 then
77 _sudo rbd device --device-type nbd unmap ${DEV}
78 fi
79
80 for ns in '' ${NS}; do
81 if rbd -p ${POOL} --namespace "${ns}" status ${IMAGE} 2>/dev/null; then
82 for s in 0.5 1 2 4 8 16 32; do
83 sleep $s
84 rbd -p ${POOL} --namespace "${ns}" status ${IMAGE} |
85 grep 'Watchers: none' && break
86 done
87 rbd -p ${POOL} --namespace "${ns}" snap purge ${IMAGE}
88 rbd -p ${POOL} --namespace "${ns}" remove ${IMAGE}
89 fi
90 done
91 rbd namespace remove ${POOL}/${NS}
92
93 # cleanup/reset default pool
94 rbd config global rm global rbd_default_pool
95 ceph osd pool delete ${ANOTHER_POOL} ${ANOTHER_POOL} --yes-i-really-really-mean-it
96 }
97
98 function expect_false()
99 {
100 if "$@"; then return 1; else return 0; fi
101 }
102
103 function get_pid()
104 {
105 local pool=$1
106 local ns=$2
107
108 PID=$(rbd device --device-type nbd --format xml list | $XMLSTARLET sel -t -v \
109 "//devices/device[pool='${pool}'][namespace='${ns}'][image='${IMAGE}'][device='${DEV}']/id")
110 test -n "${PID}" || return 1
111 ps -p ${PID} -C rbd-nbd
112 }
113
114 unmap_device()
115 {
116 local args=$1
117 local pid=$2
118
119 _sudo rbd device --device-type nbd unmap ${args}
120 rbd device --device-type nbd list | expect_false grep "^${pid}\\b" || return 1
121 ps -C rbd-nbd | expect_false grep "^ *${pid}\\b" || return 1
122
123 # workaround possible race between unmap and following map
124 sleep 0.5
125 }
126
127 #
128 # main
129 #
130
131 setup
132
133 # exit status test
134 expect_false rbd-nbd
135 expect_false rbd-nbd INVALIDCMD
136 if [ `id -u` -ne 0 ]
137 then
138 expect_false rbd device --device-type nbd map ${IMAGE}
139 fi
140 expect_false _sudo rbd device --device-type nbd map INVALIDIMAGE
141 expect_false _sudo rbd-nbd --device INVALIDDEV map ${IMAGE}
142
143 # list format test
144 expect_false rbd device --device-type nbd --format INVALID list
145 rbd device --device-type nbd --format json --pretty-format list
146 rbd device --device-type nbd --format xml list
147
148 # map test using the first unused device
149 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
150 get_pid ${POOL}
151 # map test specifying the device
152 expect_false _sudo rbd-nbd --device ${DEV} map ${POOL}/${IMAGE}
153 dev1=${DEV}
154 unmap_device ${DEV} ${PID}
155 DEV=
156 # XXX: race possible when the device is reused by other process
157 DEV=`_sudo rbd-nbd --device ${dev1} map ${POOL}/${IMAGE}`
158 [ "${DEV}" = "${dev1}" ]
159 rbd device --device-type nbd list | grep "${IMAGE}"
160 get_pid ${POOL}
161
162 # read test
163 [ "`dd if=${DATA} bs=1M | md5sum`" = "`_sudo dd if=${DEV} bs=1M | md5sum`" ]
164
165 # write test
166 dd if=/dev/urandom of=${DATA} bs=1M count=${SIZE}
167 _sudo dd if=${DATA} of=${DEV} bs=1M oflag=direct
168 [ "`dd if=${DATA} bs=1M | md5sum`" = "`rbd -p ${POOL} --no-progress export ${IMAGE} - | md5sum`" ]
169 unmap_device ${DEV} ${PID}
170
171 # notrim test
172 DEV=`_sudo rbd device --device-type nbd --options notrim map ${POOL}/${IMAGE}`
173 get_pid ${POOL}
174 provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} |
175 $XMLSTARLET sel -t -m "//stats/images/image/provisioned_size" -v .`
176 used=`rbd -p ${POOL} --format xml du ${IMAGE} |
177 $XMLSTARLET sel -t -m "//stats/images/image/used_size" -v .`
178 [ "${used}" -eq "${provisioned}" ]
179 # should fail discard as at time of mapping notrim was used
180 expect_false _sudo blkdiscard ${DEV}
181 sync
182 provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} |
183 $XMLSTARLET sel -t -m "//stats/images/image/provisioned_size" -v .`
184 used=`rbd -p ${POOL} --format xml du ${IMAGE} |
185 $XMLSTARLET sel -t -m "//stats/images/image/used_size" -v .`
186 [ "${used}" -eq "${provisioned}" ]
187 unmap_device ${DEV} ${PID}
188
189 # trim test
190 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
191 get_pid ${POOL}
192 provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} |
193 $XMLSTARLET sel -t -m "//stats/images/image/provisioned_size" -v .`
194 used=`rbd -p ${POOL} --format xml du ${IMAGE} |
195 $XMLSTARLET sel -t -m "//stats/images/image/used_size" -v .`
196 [ "${used}" -eq "${provisioned}" ]
197 # should honor discard as at time of mapping trim was considered by default
198 _sudo blkdiscard ${DEV}
199 sync
200 provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} |
201 $XMLSTARLET sel -t -m "//stats/images/image/provisioned_size" -v .`
202 used=`rbd -p ${POOL} --format xml du ${IMAGE} |
203 $XMLSTARLET sel -t -m "//stats/images/image/used_size" -v .`
204 [ "${used}" -lt "${provisioned}" ]
205
206 # resize test
207 devname=$(basename ${DEV})
208 blocks=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions)
209 test -n "${blocks}"
210 rbd resize ${POOL}/${IMAGE} --size $((SIZE * 2))M
211 rbd info ${POOL}/${IMAGE}
212 blocks2=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions)
213 test -n "${blocks2}"
214 test ${blocks2} -eq $((blocks * 2))
215 rbd resize ${POOL}/${IMAGE} --allow-shrink --size ${SIZE}M
216 blocks2=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions)
217 test -n "${blocks2}"
218 test ${blocks2} -eq ${blocks}
219
220 # read-only option test
221 unmap_device ${DEV} ${PID}
222 DEV=`_sudo rbd --device-type nbd map --read-only ${POOL}/${IMAGE}`
223 PID=$(rbd device --device-type nbd list | awk -v pool=${POOL} -v img=${IMAGE} -v dev=${DEV} \
224 '$2 == pool && $3 == img && $5 == dev {print $1}')
225 test -n "${PID}"
226 ps -p ${PID} -C rbd-nbd
227
228 _sudo dd if=${DEV} of=/dev/null bs=1M
229 expect_false _sudo dd if=${DATA} of=${DEV} bs=1M oflag=direct
230 unmap_device ${DEV} ${PID}
231
232 # exclusive option test
233 DEV=`_sudo rbd --device-type nbd map --exclusive ${POOL}/${IMAGE}`
234 get_pid ${POOL}
235
236 _sudo dd if=${DATA} of=${DEV} bs=1M oflag=direct
237 expect_false timeout 10 \
238 rbd bench ${IMAGE} --io-type write --io-size=1024 --io-total=1024
239 unmap_device ${DEV} ${PID}
240 DEV=
241 rbd bench ${IMAGE} --io-type write --io-size=1024 --io-total=1024
242
243 # unmap by image name test
244 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
245 get_pid ${POOL}
246 unmap_device ${IMAGE} ${PID}
247 DEV=
248
249 # map/unmap snap test
250 rbd snap create ${POOL}/${IMAGE}@snap
251 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}@snap`
252 get_pid ${POOL}
253 unmap_device "${IMAGE}@snap" ${PID}
254 DEV=
255
256 # map/unmap snap test with --snap-id
257 SNAPID=`rbd snap ls ${POOL}/${IMAGE} | awk '$2 == "snap" {print $1}'`
258 DEV=`_sudo rbd device --device-type nbd map --snap-id ${SNAPID} ${POOL}/${IMAGE}`
259 get_pid ${POOL}
260 unmap_device "--snap-id ${SNAPID} ${IMAGE}" ${PID}
261 DEV=
262
263 # map/unmap namespace test
264 rbd snap create ${POOL}/${NS}/${IMAGE}@snap
265 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${NS}/${IMAGE}@snap`
266 get_pid ${POOL} ${NS}
267 unmap_device "${POOL}/${NS}/${IMAGE}@snap" ${PID}
268 DEV=
269
270 # map/unmap namespace test with --snap-id
271 SNAPID=`rbd snap ls ${POOL}/${NS}/${IMAGE} | awk '$2 == "snap" {print $1}'`
272 DEV=`_sudo rbd device --device-type nbd map --snap-id ${SNAPID} ${POOL}/${NS}/${IMAGE}`
273 get_pid ${POOL} ${NS}
274 unmap_device "--snap-id ${SNAPID} ${POOL}/${NS}/${IMAGE}" ${PID}
275 DEV=
276
277 # map/unmap namespace using options test
278 DEV=`_sudo rbd device --device-type nbd map --pool ${POOL} --namespace ${NS} --image ${IMAGE}`
279 get_pid ${POOL} ${NS}
280 unmap_device "--pool ${POOL} --namespace ${NS} --image ${IMAGE}" ${PID}
281 DEV=`_sudo rbd device --device-type nbd map --pool ${POOL} --namespace ${NS} --image ${IMAGE} --snap snap`
282 get_pid ${POOL} ${NS}
283 unmap_device "--pool ${POOL} --namespace ${NS} --image ${IMAGE} --snap snap" ${PID}
284 DEV=
285
286 # unmap by image name test 2
287 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
288 get_pid ${POOL}
289 pid=$PID
290 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${NS}/${IMAGE}`
291 get_pid ${POOL} ${NS}
292 unmap_device ${POOL}/${NS}/${IMAGE} ${PID}
293 DEV=
294 unmap_device ${POOL}/${IMAGE} ${pid}
295
296 # map/unmap test with just image name and expect image to come from default pool
297 if [ "${POOL}" = "rbd" ];then
298 DEV=`_sudo rbd device --device-type nbd map ${IMAGE}`
299 get_pid ${POOL}
300 unmap_device ${IMAGE} ${PID}
301 DEV=
302 fi
303
304 # map/unmap test with just image name after changing default pool
305 rbd config global set global rbd_default_pool ${ANOTHER_POOL}
306 rbd create --size 10M ${IMAGE}
307 DEV=`_sudo rbd device --device-type nbd map ${IMAGE}`
308 get_pid ${ANOTHER_POOL}
309 unmap_device ${IMAGE} ${PID}
310 DEV=
311
312 # reset
313 rbd config global rm global rbd_default_pool
314
315 # auto unmap test
316 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
317 get_pid ${POOL}
318 _sudo kill ${PID}
319 for i in `seq 10`; do
320 rbd device --device-type nbd list | expect_false grep "^${PID} *${POOL} *${IMAGE}" && break
321 sleep 1
322 done
323 rbd device --device-type nbd list | expect_false grep "^${PID} *${POOL} *${IMAGE}"
324
325 # quiesce test
326 QUIESCE_HOOK=${TEMPDIR}/quiesce.sh
327 DEV=`_sudo rbd device --device-type nbd map --quiesce --quiesce-hook ${QUIESCE_HOOK} ${POOL}/${IMAGE}`
328 get_pid ${POOL}
329
330 # test it fails if the hook does not exists
331 test ! -e ${QUIESCE_HOOK}
332 expect_false rbd snap create ${POOL}/${IMAGE}@quiesce1
333 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
334
335 # test the hook is executed
336 touch ${QUIESCE_HOOK}
337 chmod +x ${QUIESCE_HOOK}
338 cat > ${QUIESCE_HOOK} <<EOF
339 #/bin/sh
340 echo "test the hook is executed" >&2
341 echo \$1 > ${TEMPDIR}/\$2
342 EOF
343 rbd snap create ${POOL}/${IMAGE}@quiesce1
344 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
345 test "$(cat ${TEMPDIR}/quiesce)" = ${DEV}
346 test "$(cat ${TEMPDIR}/unquiesce)" = ${DEV}
347
348 # test snap create fails if the hook fails
349 touch ${QUIESCE_HOOK}
350 chmod +x ${QUIESCE_HOOK}
351 cat > ${QUIESCE_HOOK} <<EOF
352 #/bin/sh
353 echo "test snap create fails if the hook fails" >&2
354 exit 22
355 EOF
356 expect_false rbd snap create ${POOL}/${IMAGE}@quiesce2
357 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
358
359 # test the hook is slow
360 cat > ${QUIESCE_HOOK} <<EOF
361 #/bin/sh
362 echo "test the hook is slow" >&2
363 sleep 7
364 EOF
365 rbd snap create ${POOL}/${IMAGE}@quiesce2
366 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
367
368 # test rbd-nbd_quiesce hook that comes with distribution
369 unmap_device ${DEV} ${PID}
370 LOG_FILE=${TEMPDIR}/rbd-nbd.log
371 if [ -n "${CEPH_SRC}" ]; then
372 QUIESCE_HOOK=${CEPH_SRC}/tools/rbd_nbd/rbd-nbd_quiesce
373 DEV=`_sudo rbd device --device-type nbd map --quiesce --quiesce-hook ${QUIESCE_HOOK} \
374 ${POOL}/${IMAGE} --log-file=${LOG_FILE}`
375 else
376 DEV=`_sudo rbd device --device-type nbd map --quiesce ${POOL}/${IMAGE} --log-file=${LOG_FILE}`
377 fi
378 get_pid ${POOL}
379 _sudo mkfs ${DEV}
380 mkdir ${TEMPDIR}/mnt
381 _sudo mount ${DEV} ${TEMPDIR}/mnt
382 rbd snap create ${POOL}/${IMAGE}@quiesce3
383 _sudo dd if=${DATA} of=${TEMPDIR}/mnt/test bs=1M count=1 oflag=direct
384 _sudo umount ${TEMPDIR}/mnt
385 unmap_device ${DEV} ${PID}
386 DEV=
387 cat ${LOG_FILE}
388 expect_false grep 'quiesce failed' ${LOG_FILE}
389
390 # test detach/attach
391 OUT=`_sudo rbd device --device-type nbd --options try-netlink,show-cookie map ${POOL}/${IMAGE}`
392 read DEV COOKIE <<< "${OUT}"
393 get_pid ${POOL}
394 _sudo mount ${DEV} ${TEMPDIR}/mnt
395 _sudo rbd device detach ${POOL}/${IMAGE} --device-type nbd
396 expect_false get_pid ${POOL}
397 expect_false _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd
398 if [ -n "${COOKIE}" ]; then
399 _sudo rbd device attach --device ${DEV} --cookie ${COOKIE} ${POOL}/${IMAGE} --device-type nbd
400 else
401 _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd --force
402 fi
403 get_pid ${POOL}
404 _sudo rbd device detach ${DEV} --device-type nbd
405 expect_false get_pid ${POOL}
406 if [ -n "${COOKIE}" ]; then
407 _sudo rbd device attach --device ${DEV} --cookie ${COOKIE} ${POOL}/${IMAGE} --device-type nbd
408 else
409 _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd --force
410 fi
411 get_pid ${POOL}
412 ls ${TEMPDIR}/mnt/
413 dd if=${TEMPDIR}/mnt/test of=/dev/null bs=1M count=1
414 _sudo dd if=${DATA} of=${TEMPDIR}/mnt/test1 bs=1M count=1 oflag=direct
415 _sudo umount ${TEMPDIR}/mnt
416 unmap_device ${DEV} ${PID}
417 # if kernel supports cookies
418 if [ -n "${COOKIE}" ]; then
419 OUT=`_sudo rbd device --device-type nbd --show-cookie --cookie "abc de" --options try-netlink map ${POOL}/${IMAGE}`
420 read DEV ANOTHER_COOKIE <<< "${OUT}"
421 get_pid ${POOL}
422 test "${ANOTHER_COOKIE}" = "abc de"
423 unmap_device ${DEV} ${PID}
424 fi
425 DEV=
426
427 # test detach/attach with --snap-id
428 SNAPID=`rbd snap ls ${POOL}/${IMAGE} | awk '$2 == "snap" {print $1}'`
429 OUT=`_sudo rbd device --device-type nbd --options try-netlink,show-cookie map --snap-id ${SNAPID} ${POOL}/${IMAGE}`
430 read DEV COOKIE <<< "${OUT}"
431 get_pid ${POOL}
432 _sudo rbd device detach ${POOL}/${IMAGE} --snap-id ${SNAPID} --device-type nbd
433 expect_false get_pid ${POOL}
434 expect_false _sudo rbd device attach --device ${DEV} --snap-id ${SNAPID} ${POOL}/${IMAGE} --device-type nbd
435 if [ -n "${COOKIE}" ]; then
436 _sudo rbd device attach --device ${DEV} --cookie ${COOKIE} --snap-id ${SNAPID} ${POOL}/${IMAGE} --device-type nbd
437 else
438 _sudo rbd device attach --device ${DEV} --snap-id ${SNAPID} ${POOL}/${IMAGE} --device-type nbd --force
439 fi
440 get_pid ${POOL}
441 _sudo rbd device detach ${DEV} --device-type nbd
442 expect_false get_pid ${POOL}
443 DEV=
444
445 # test discard granularity with journaling
446 rbd config image set ${POOL}/${IMAGE} rbd_discard_granularity_bytes 4096
447 rbd feature enable ${POOL}/${IMAGE} journaling
448 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
449 get_pid ${POOL}
450 # since a discard will now be pruned to only whole blocks (0..4095, 4096..8191)
451 # let us test all the cases around those alignments. 512 is the smallest
452 # possible block blkdiscard allows us to use. Thus the test checks
453 # 512 before, on the alignment, 512 after.
454 _sudo blkdiscard --offset 0 --length $((4096-512)) ${DEV}
455 _sudo blkdiscard --offset 0 --length 4096 ${DEV}
456 _sudo blkdiscard --offset 0 --length $((4096+512)) ${DEV}
457 _sudo blkdiscard --offset 512 --length $((8192-1024)) ${DEV}
458 _sudo blkdiscard --offset 512 --length $((8192-512)) ${DEV}
459 _sudo blkdiscard --offset 512 --length 8192 ${DEV}
460 # wait for commit log to be empty, 10 seconds should be well enough
461 tries=0
462 queue_length=`rbd journal inspect --pool ${POOL} --image ${IMAGE} | awk '/entries inspected/ {print $1}'`
463 while [ ${tries} -lt 10 ] && [ ${queue_length} -gt 0 ]; do
464 rbd journal inspect --pool ${POOL} --image ${IMAGE} --verbose
465 sleep 1
466 queue_length=`rbd journal inspect --pool ${POOL} --image ${IMAGE} | awk '/entries inspected/ {print $1}'`
467 tries=$((tries+1))
468 done
469 [ ${queue_length} -eq 0 ]
470 unmap_device ${DEV} ${PID}
471 DEV=
472 rbd feature disable ${POOL}/${IMAGE} journaling
473 rbd config image rm ${POOL}/${IMAGE} rbd_discard_granularity_bytes
474
475 # test that rbd_op_threads setting takes effect
476 EXPECTED=`ceph-conf --show-config-value librados_thread_count`
477 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
478 get_pid ${POOL}
479 ACTUAL=`ps -p ${PID} -T | grep -c io_context_pool`
480 [ ${ACTUAL} -eq ${EXPECTED} ]
481 unmap_device ${DEV} ${PID}
482 EXPECTED=$((EXPECTED * 3 + 1))
483 DEV=`_sudo rbd device --device-type nbd --rbd-op-threads ${EXPECTED} map ${POOL}/${IMAGE}`
484 get_pid ${POOL}
485 ACTUAL=`ps -p ${PID} -T | grep -c io_context_pool`
486 [ ${ACTUAL} -eq ${EXPECTED} ]
487 unmap_device ${DEV} ${PID}
488 DEV=
489
490 echo OK