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
${dev}
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 namespace test
257 rbd snap create
${POOL}/${NS}/${IMAGE}@snap
258 DEV
=`_sudo rbd device --device-type nbd map ${POOL}/${NS}/${IMAGE}@snap`
259 get_pid
${POOL} ${NS}
260 unmap_device
"${POOL}/${NS}/${IMAGE}@snap" ${PID}
263 # unmap by image name test 2
264 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
267 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${NS}/${IMAGE}`
268 get_pid ${POOL} ${NS}
269 unmap_device ${POOL}/${NS}/${IMAGE} ${PID}
271 unmap_device ${POOL}/${IMAGE} ${pid}
273 # map/unmap test with just image name and expect image to come from default pool
274 if [ "${POOL}" = "rbd
" ];then
275 DEV=`_sudo rbd device --device-type nbd map ${IMAGE}`
277 unmap_device ${IMAGE} ${PID}
281 # map/unmap test with just image name after changing default pool
282 rbd config global set global rbd_default_pool ${ANOTHER_POOL}
283 rbd create --size 10M ${IMAGE}
284 DEV=`_sudo rbd device --device-type nbd map ${IMAGE}`
285 get_pid ${ANOTHER_POOL}
286 unmap_device ${IMAGE} ${PID}
290 rbd config global rm global rbd_default_pool
293 DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
296 for i in `seq 10`; do
297 rbd device --device-type nbd list | expect_false grep "^
${PID} *${POOL} *${IMAGE}" && break
300 rbd device --device-type nbd list | expect_false grep "^
${PID} *${POOL} *${IMAGE}"
303 QUIESCE_HOOK=${TEMPDIR}/quiesce.sh
304 DEV=`_sudo rbd device --device-type nbd map --quiesce --quiesce-hook ${QUIESCE_HOOK} ${POOL}/${IMAGE}`
307 # test it fails if the hook does not exists
308 test ! -e ${QUIESCE_HOOK}
309 expect_false rbd snap create ${POOL}/${IMAGE}@quiesce1
310 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
312 # test the hook is executed
313 touch ${QUIESCE_HOOK}
314 chmod +x ${QUIESCE_HOOK}
315 cat > ${QUIESCE_HOOK} <<EOF
317 echo "test the hook is executed
" >&2
318 echo \$1 > ${TEMPDIR}/\$2
320 rbd snap create ${POOL}/${IMAGE}@quiesce1
321 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
322 test "$
(cat ${TEMPDIR}/quiesce
)" = ${DEV}
323 test "$
(cat ${TEMPDIR}/unquiesce
)" = ${DEV}
325 # test snap create fails if the hook fails
326 touch ${QUIESCE_HOOK}
327 chmod +x ${QUIESCE_HOOK}
328 cat > ${QUIESCE_HOOK} <<EOF
330 echo "test snap create fails
if the hook fails
" >&2
333 expect_false rbd snap create ${POOL}/${IMAGE}@quiesce2
334 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
336 # test the hook is slow
337 cat > ${QUIESCE_HOOK} <<EOF
339 echo "test the hook is slow
" >&2
342 rbd snap create ${POOL}/${IMAGE}@quiesce2
343 _sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
345 # test rbd-nbd_quiesce hook that comes with distribution
346 unmap_device ${DEV} ${PID}
347 LOG_FILE=${TEMPDIR}/rbd-nbd.log
348 if [ -n "${CEPH_SRC}" ]; then
349 QUIESCE_HOOK=${CEPH_SRC}/tools/rbd_nbd/rbd-nbd_quiesce
350 DEV=`_sudo rbd device --device-type nbd map --quiesce --quiesce-hook ${QUIESCE_HOOK} \
351 ${POOL}/${IMAGE} --log-file=${LOG_FILE}`
353 DEV=`_sudo rbd device --device-type nbd map --quiesce ${POOL}/${IMAGE} --log-file=${LOG_FILE}`
358 _sudo mount ${DEV} ${TEMPDIR}/mnt
359 rbd snap create ${POOL}/${IMAGE}@quiesce3
360 _sudo dd if=${DATA} of=${TEMPDIR}/mnt/test bs=1M count=1 oflag=direct
361 _sudo umount ${TEMPDIR}/mnt
362 unmap_device ${DEV} ${PID}
365 expect_false grep 'quiesce failed' ${LOG_FILE}
368 OUT=`_sudo rbd device --device-type nbd --options try-netlink,show-cookie map ${POOL}/${IMAGE}`
369 read DEV COOKIE <<< "${OUT}"
371 _sudo mount ${DEV} ${TEMPDIR}/mnt
372 _sudo rbd device detach ${POOL}/${IMAGE} --device-type nbd
373 expect_false get_pid ${POOL}
374 expect_false _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd
375 if [ -n "${COOKIE}" ]; then
376 _sudo rbd device attach --device ${DEV} --cookie ${COOKIE} ${POOL}/${IMAGE} --device-type nbd
378 _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd --force
381 _sudo rbd device detach ${DEV} --device-type nbd
382 expect_false get_pid ${POOL}
383 if [ -n "${COOKIE}" ]; then
384 _sudo rbd device attach --device ${DEV} --cookie ${COOKIE} ${POOL}/${IMAGE} --device-type nbd
386 _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd --force
390 dd if=${TEMPDIR}/mnt/test of=/dev/null bs=1M count=1
391 _sudo dd if=${DATA} of=${TEMPDIR}/mnt/test1 bs=1M count=1 oflag=direct
392 _sudo umount ${TEMPDIR}/mnt
393 unmap_device ${DEV} ${PID}
394 # if kernel supports cookies
395 if [ -n "${COOKIE}" ]; then
396 OUT=`_sudo rbd device --device-type nbd --show-cookie --cookie "abc de
" --options try-netlink map ${POOL}/${IMAGE}`
397 read DEV ANOTHER_COOKIE <<< "${OUT}"
399 test "${ANOTHER_COOKIE}" = "abc de
"
400 unmap_device ${DEV} ${PID}