4 . $
(dirname $0)/..
/..
/standalone
/ceph-helpers.sh
7 ANOTHER_POOL
=new_default_pool$$
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
28 sudo
-nE "${cmd}" "$@"
35 if [ -e CMakeCache.txt
]; then
36 # running under cmake build dir
38 CEPH_SRC
=$
(readlink
-f $
(dirname $0)/..
/..
/..
/src
)
40 CEPH_BIN
=${CEPH_ROOT}/bin
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}
49 trap cleanup INT TERM EXIT
52 dd if=/dev
/urandom of
=${DATA} bs
=1M count
=${SIZE}
54 rbd namespace create
${POOL}/${NS}
56 for ns
in '' ${NS}; do
57 rbd
--dest-pool ${POOL} --dest-namespace "${ns}" --no-progress import \
62 ceph osd pool create
${ANOTHER_POOL} 8
63 rbd pool init
${ANOTHER_POOL}
72 mount | fgrep
${TEMPDIR}/mnt
&& _sudo umount
-f ${TEMPDIR}/mnt
77 _sudo rbd device
--device-type nbd unmap
${DEV}
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
84 rbd
-p ${POOL} --namespace "${ns}" status ${IMAGE} |
85 grep 'Watchers: none' && break
87 rbd
-p ${POOL} --namespace "${ns}" snap purge ${IMAGE}
88 rbd
-p ${POOL} --namespace "${ns}" remove ${IMAGE}
91 rbd namespace remove
${POOL}/${NS}
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
98 function expect_false
()
100 if "$@"; then return 1; else return 0; fi
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
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
123 # workaround possible race between unmap and following map
135 expect_false rbd-nbd INVALIDCMD
138 expect_false rbd device
--device-type nbd map
${IMAGE}
140 expect_false _sudo rbd device
--device-type nbd map INVALIDIMAGE
141 expect_false _sudo rbd-nbd
--device INVALIDDEV map
${IMAGE}
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
148 # map test using the first unused device
149 DEV
=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
151 # map test specifying the device
152 expect_false _sudo rbd-nbd
--device ${DEV} map ${POOL}/${IMAGE}
154 unmap_device
${DEV} ${PID}
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}"
163 [ "`dd if=${DATA} bs=1M | md5sum`" = "`_sudo dd if=${DEV} bs=1M | md5sum`" ]
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}
172 DEV
=`_sudo rbd device --device-type nbd --options notrim map ${POOL}/${IMAGE}`
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}
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}
190 DEV
=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
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}
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}" ]
207 devname
=$
(basename ${DEV})
208 blocks
=$
(awk -v dev
=${devname} '$4 == dev {print $3}' /proc
/partitions
)
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
)
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
)
218 test ${blocks2} -eq ${blocks}
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}')
226 ps
-p ${PID} -C rbd-nbd
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}
232 # exclusive option test
233 DEV
=`_sudo rbd --device-type nbd map --exclusive ${POOL}/${IMAGE}`
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}
241 rbd bench
${IMAGE} --io-type write --io-size=1024 --io-total=1024
243 # unmap by image name test
244 DEV
=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
246 unmap_device
${IMAGE} ${PID}
249 # map/unmap snap test
250 rbd snap create
${POOL}/${IMAGE}@snap
251 DEV
=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}@snap`
253 unmap_device
"${IMAGE}@snap" ${PID}
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}`
260 unmap_device
"--snap-id ${SNAPID} ${IMAGE}" ${PID}
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}
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}
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}
286 # unmap by image name test 2
287 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
290 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${NS}/${IMAGE}`
291 get_pid ${POOL} ${NS}
292 unmap_device ${POOL}/${NS}/${IMAGE} ${PID}
294 unmap_device ${POOL}/${IMAGE} ${pid}
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}`
300 unmap_device ${IMAGE} ${PID}
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}
313 rbd config global rm global rbd_default_pool
316 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
319 for i in `seq 10`; do
320 rbd device --device-type nbd list | expect_false grep "^
${PID} *${POOL} *${IMAGE}" && break
323 rbd device --device-type nbd list | expect_false grep "^
${PID} *${POOL} *${IMAGE}"
326 QUIESCE_HOOK=${TEMPDIR}/quiesce.sh
327 DEV=`_sudo rbd device --device-type nbd map --quiesce --quiesce-hook ${QUIESCE_HOOK} ${POOL}/${IMAGE}`
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
335 # test the hook is executed
336 touch ${QUIESCE_HOOK}
337 chmod +x ${QUIESCE_HOOK}
338 cat > ${QUIESCE_HOOK} <<EOF
340 echo "test the hook is executed
" >&2
341 echo \$1 > ${TEMPDIR}/\$2
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}
348 # test snap create fails if the hook fails
349 touch ${QUIESCE_HOOK}
350 chmod +x ${QUIESCE_HOOK}
351 cat > ${QUIESCE_HOOK} <<EOF
353 echo "test snap create fails
if the hook fails
" >&2
356 expect_false rbd snap create ${POOL}/${IMAGE}@quiesce2
357 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
359 # test the hook is slow
360 cat > ${QUIESCE_HOOK} <<EOF
362 echo "test the hook is slow
" >&2
365 rbd snap create ${POOL}/${IMAGE}@quiesce2
366 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
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}`
376 DEV=`_sudo rbd device --device-type nbd map --quiesce ${POOL}/${IMAGE} --log-file=${LOG_FILE}`
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}
388 expect_false grep 'quiesce failed' ${LOG_FILE}
391 OUT=`_sudo rbd device --device-type nbd --options try-netlink,show-cookie map ${POOL}/${IMAGE}`
392 read DEV COOKIE <<< "${OUT}"
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
401 _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd --force
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
409 _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd --force
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}"
422 test "${ANOTHER_COOKIE}" = "abc de
"
423 unmap_device ${DEV} ${PID}
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}"
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
438 _sudo rbd device attach --device ${DEV} --snap-id ${SNAPID} ${POOL}/${IMAGE} --device-type nbd --force
441 _sudo rbd device detach ${DEV} --device-type nbd
442 expect_false get_pid ${POOL}
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}`
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
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
466 queue_length=`rbd journal inspect --pool ${POOL} --image ${IMAGE} | awk '/entries inspected/ {print $1}'`
469 [ ${queue_length} -eq 0 ]
470 unmap_device ${DEV} ${PID}
472 rbd feature disable ${POOL}/${IMAGE} journaling
473 rbd config image rm ${POOL}/${IMAGE} rbd_discard_granularity_bytes
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}`
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}`
485 ACTUAL=`ps -p ${PID} -T | grep -c io_context_pool`
486 [ ${ACTUAL} -eq ${EXPECTED} ]
487 unmap_device ${DEV} ${PID}