]> git.proxmox.com Git - ceph.git/blame - ceph/qa/workunits/rbd/rbd-nbd.sh
import quincy beta 17.1.0
[ceph.git] / ceph / qa / workunits / rbd / rbd-nbd.sh
CommitLineData
11fdf7f2
TL
1#!/usr/bin/env bash
2set -ex
7c673cae 3
c07f9fc5 4. $(dirname $0)/../../standalone/ceph-helpers.sh
7c673cae
FG
5
6POOL=rbd
522d829b 7ANOTHER_POOL=new_default_pool$$
11fdf7f2 8NS=ns
7c673cae
FG
9IMAGE=testrbdnbd$$
10SIZE=64
11DATA=
12DEV=
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
31setup()
32{
11fdf7f2
TL
33 local ns x
34
7c673cae
FG
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}
9f95a23c 43 export PYTHONPATH=${PYTHONPATH}:${CEPH_SRC}/pybind:${CEPH_ROOT}/lib/cython_modules/lib.3
7c673cae
FG
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}
11fdf7f2
TL
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
522d829b
TL
60
61 # create another pool
62 ceph osd pool create ${ANOTHER_POOL} 8
63 rbd pool init ${ANOTHER_POOL}
7c673cae
FG
64}
65
66function cleanup()
67{
11fdf7f2
TL
68 local ns s
69
7c673cae 70 set +e
f67539c2
TL
71
72 mount | fgrep ${TEMPDIR}/mnt && _sudo umount -f ${TEMPDIR}/mnt
73
11fdf7f2 74 rm -Rf ${TEMPDIR}
7c673cae
FG
75 if [ -n "${DEV}" ]
76 then
522d829b 77 _sudo rbd device --device-type nbd unmap ${DEV}
7c673cae 78 fi
11fdf7f2
TL
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}
522d829b
TL
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
7c673cae
FG
96}
97
98function expect_false()
99{
100 if "$@"; then return 1; else return 0; fi
101}
102
11fdf7f2
TL
103function get_pid()
104{
522d829b
TL
105 local pool=$1
106 local ns=$2
11fdf7f2 107
522d829b
TL
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")
f67539c2 110 test -n "${PID}" || return 1
f91f0fd5 111 ps -p ${PID} -C rbd-nbd
11fdf7f2
TL
112}
113
81eedcae
TL
114unmap_device()
115{
f91f0fd5
TL
116 local dev=$1
117 local pid=$2
f67539c2 118
522d829b
TL
119 _sudo rbd device --device-type nbd unmap ${dev}
120 rbd device --device-type nbd list | expect_false grep "^${pid}\\b" || return 1
f67539c2 121 ps -C rbd-nbd | expect_false grep "^ *${pid}\\b" || return 1
81eedcae 122
f67539c2
TL
123 # workaround possible race between unmap and following map
124 sleep 0.5
81eedcae
TL
125}
126
7c673cae
FG
127#
128# main
129#
130
131setup
132
133# exit status test
134expect_false rbd-nbd
135expect_false rbd-nbd INVALIDCMD
136if [ `id -u` -ne 0 ]
137then
522d829b 138 expect_false rbd device --device-type nbd map ${IMAGE}
7c673cae 139fi
522d829b 140expect_false _sudo rbd device --device-type nbd map INVALIDIMAGE
7c673cae
FG
141expect_false _sudo rbd-nbd --device INVALIDDEV map ${IMAGE}
142
11fdf7f2 143# list format test
522d829b
TL
144expect_false rbd device --device-type nbd --format INVALID list
145rbd device --device-type nbd --format json --pretty-format list
146rbd device --device-type nbd --format xml list
11fdf7f2 147
7c673cae 148# map test using the first unused device
522d829b
TL
149DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
150get_pid ${POOL}
7c673cae
FG
151# map test specifying the device
152expect_false _sudo rbd-nbd --device ${DEV} map ${POOL}/${IMAGE}
153dev1=${DEV}
f91f0fd5 154unmap_device ${DEV} ${PID}
7c673cae
FG
155DEV=
156# XXX: race possible when the device is reused by other process
157DEV=`_sudo rbd-nbd --device ${dev1} map ${POOL}/${IMAGE}`
158[ "${DEV}" = "${dev1}" ]
522d829b
TL
159rbd device --device-type nbd list | grep "${IMAGE}"
160get_pid ${POOL}
7c673cae
FG
161
162# read test
163[ "`dd if=${DATA} bs=1M | md5sum`" = "`_sudo dd if=${DEV} bs=1M | md5sum`" ]
164
165# write test
166dd 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`" ]
20effc67
TL
169unmap_device ${DEV} ${PID}
170
171# notrim test
172DEV=`_sudo rbd device --device-type nbd --options notrim map ${POOL}/${IMAGE}`
173get_pid ${POOL}
174provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} |
175 $XMLSTARLET sel -t -m "//stats/images/image/provisioned_size" -v .`
176used=`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
180expect_false _sudo blkdiscard ${DEV}
181sync
182provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} |
183 $XMLSTARLET sel -t -m "//stats/images/image/provisioned_size" -v .`
184used=`rbd -p ${POOL} --format xml du ${IMAGE} |
185 $XMLSTARLET sel -t -m "//stats/images/image/used_size" -v .`
186[ "${used}" -eq "${provisioned}" ]
187unmap_device ${DEV} ${PID}
7c673cae
FG
188
189# trim test
20effc67
TL
190DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
191get_pid ${POOL}
7c673cae
FG
192provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} |
193 $XMLSTARLET sel -t -m "//stats/images/image/provisioned_size" -v .`
194used=`rbd -p ${POOL} --format xml du ${IMAGE} |
195 $XMLSTARLET sel -t -m "//stats/images/image/used_size" -v .`
196[ "${used}" -eq "${provisioned}" ]
20effc67
TL
197# should honor discard as at time of mapping trim was considered by default
198_sudo blkdiscard ${DEV}
7c673cae
FG
199sync
200provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} |
201 $XMLSTARLET sel -t -m "//stats/images/image/provisioned_size" -v .`
202used=`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
207devname=$(basename ${DEV})
208blocks=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions)
209test -n "${blocks}"
210rbd resize ${POOL}/${IMAGE} --size $((SIZE * 2))M
211rbd info ${POOL}/${IMAGE}
212blocks2=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions)
213test -n "${blocks2}"
214test ${blocks2} -eq $((blocks * 2))
215rbd resize ${POOL}/${IMAGE} --allow-shrink --size ${SIZE}M
216blocks2=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions)
217test -n "${blocks2}"
218test ${blocks2} -eq ${blocks}
219
220# read-only option test
f91f0fd5 221unmap_device ${DEV} ${PID}
522d829b
TL
222DEV=`_sudo rbd --device-type nbd map --read-only ${POOL}/${IMAGE}`
223PID=$(rbd device --device-type nbd list | awk -v pool=${POOL} -v img=${IMAGE} -v dev=${DEV} \
31f18b77
FG
224 '$2 == pool && $3 == img && $5 == dev {print $1}')
225test -n "${PID}"
f91f0fd5 226ps -p ${PID} -C rbd-nbd
31f18b77 227
7c673cae
FG
228_sudo dd if=${DEV} of=/dev/null bs=1M
229expect_false _sudo dd if=${DATA} of=${DEV} bs=1M oflag=direct
f91f0fd5 230unmap_device ${DEV} ${PID}
7c673cae
FG
231
232# exclusive option test
522d829b
TL
233DEV=`_sudo rbd --device-type nbd map --exclusive ${POOL}/${IMAGE}`
234get_pid ${POOL}
31f18b77 235
7c673cae
FG
236_sudo dd if=${DATA} of=${DEV} bs=1M oflag=direct
237expect_false timeout 10 \
238 rbd bench ${IMAGE} --io-type write --io-size=1024 --io-total=1024
f91f0fd5 239unmap_device ${DEV} ${PID}
11fdf7f2
TL
240DEV=
241rbd bench ${IMAGE} --io-type write --io-size=1024 --io-total=1024
242
243# unmap by image name test
522d829b
TL
244DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
245get_pid ${POOL}
f91f0fd5 246unmap_device ${IMAGE} ${PID}
11fdf7f2 247DEV=
11fdf7f2
TL
248
249# map/unmap snap test
250rbd snap create ${POOL}/${IMAGE}@snap
522d829b
TL
251DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}@snap`
252get_pid ${POOL}
f91f0fd5 253unmap_device "${IMAGE}@snap" ${PID}
11fdf7f2 254DEV=
11fdf7f2
TL
255
256# map/unmap namespace test
257rbd snap create ${POOL}/${NS}/${IMAGE}@snap
522d829b
TL
258DEV=`_sudo rbd device --device-type nbd map ${POOL}/${NS}/${IMAGE}@snap`
259get_pid ${POOL} ${NS}
f91f0fd5 260unmap_device "${POOL}/${NS}/${IMAGE}@snap" ${PID}
11fdf7f2 261DEV=
f91f0fd5
TL
262
263# unmap by image name test 2
522d829b
TL
264DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
265get_pid ${POOL}
f91f0fd5 266pid=$PID
522d829b
TL
267DEV=`_sudo rbd device --device-type nbd map ${POOL}/${NS}/${IMAGE}`
268get_pid ${POOL} ${NS}
f91f0fd5
TL
269unmap_device ${POOL}/${NS}/${IMAGE} ${PID}
270DEV=
271unmap_device ${POOL}/${IMAGE} ${pid}
31f18b77 272
522d829b
TL
273# map/unmap test with just image name and expect image to come from default pool
274if [ "${POOL}" = "rbd" ];then
275 DEV=`_sudo rbd device --device-type nbd map ${IMAGE}`
276 get_pid ${POOL}
277 unmap_device ${IMAGE} ${PID}
278 DEV=
279fi
280
281# map/unmap test with just image name after changing default pool
282rbd config global set global rbd_default_pool ${ANOTHER_POOL}
283rbd create --size 10M ${IMAGE}
284DEV=`_sudo rbd device --device-type nbd map ${IMAGE}`
285get_pid ${ANOTHER_POOL}
286unmap_device ${IMAGE} ${PID}
287DEV=
288
289# reset
290rbd config global rm global rbd_default_pool
291
31f18b77 292# auto unmap test
522d829b
TL
293DEV=`_sudo rbd device --device-type nbd map ${POOL}/${IMAGE}`
294get_pid ${POOL}
31f18b77
FG
295_sudo kill ${PID}
296for i in `seq 10`; do
522d829b 297 rbd device --device-type nbd list | expect_false grep "^${PID} *${POOL} *${IMAGE}" && break
31f18b77
FG
298 sleep 1
299done
522d829b 300rbd device --device-type nbd list | expect_false grep "^${PID} *${POOL} *${IMAGE}"
31f18b77 301
f67539c2
TL
302# quiesce test
303QUIESCE_HOOK=${TEMPDIR}/quiesce.sh
522d829b
TL
304DEV=`_sudo rbd device --device-type nbd map --quiesce --quiesce-hook ${QUIESCE_HOOK} ${POOL}/${IMAGE}`
305get_pid ${POOL}
f67539c2
TL
306
307# test it fails if the hook does not exists
308test ! -e ${QUIESCE_HOOK}
309expect_false rbd snap create ${POOL}/${IMAGE}@quiesce1
310_sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
311
312# test the hook is executed
313touch ${QUIESCE_HOOK}
314chmod +x ${QUIESCE_HOOK}
315cat > ${QUIESCE_HOOK} <<EOF
316#/bin/sh
317echo "test the hook is executed" >&2
318echo \$1 > ${TEMPDIR}/\$2
319EOF
320rbd snap create ${POOL}/${IMAGE}@quiesce1
321_sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
322test "$(cat ${TEMPDIR}/quiesce)" = ${DEV}
323test "$(cat ${TEMPDIR}/unquiesce)" = ${DEV}
324
325# test snap create fails if the hook fails
326touch ${QUIESCE_HOOK}
327chmod +x ${QUIESCE_HOOK}
328cat > ${QUIESCE_HOOK} <<EOF
329#/bin/sh
330echo "test snap create fails if the hook fails" >&2
331exit 22
332EOF
333expect_false rbd snap create ${POOL}/${IMAGE}@quiesce2
334_sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
335
336# test the hook is slow
337cat > ${QUIESCE_HOOK} <<EOF
338#/bin/sh
339echo "test the hook is slow" >&2
340sleep 7
341EOF
342rbd snap create ${POOL}/${IMAGE}@quiesce2
343_sudo dd if=${DATA} of=${DEV} bs=1M count=1 oflag=direct
344
345# test rbd-nbd_quiesce hook that comes with distribution
346unmap_device ${DEV} ${PID}
347LOG_FILE=${TEMPDIR}/rbd-nbd.log
348if [ -n "${CEPH_SRC}" ]; then
349 QUIESCE_HOOK=${CEPH_SRC}/tools/rbd_nbd/rbd-nbd_quiesce
522d829b 350 DEV=`_sudo rbd device --device-type nbd map --quiesce --quiesce-hook ${QUIESCE_HOOK} \
f67539c2
TL
351 ${POOL}/${IMAGE} --log-file=${LOG_FILE}`
352else
522d829b 353 DEV=`_sudo rbd device --device-type nbd map --quiesce ${POOL}/${IMAGE} --log-file=${LOG_FILE}`
f67539c2 354fi
522d829b 355get_pid ${POOL}
f67539c2
TL
356_sudo mkfs ${DEV}
357mkdir ${TEMPDIR}/mnt
358_sudo mount ${DEV} ${TEMPDIR}/mnt
359rbd 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
362unmap_device ${DEV} ${PID}
363DEV=
364cat ${LOG_FILE}
365expect_false grep 'quiesce failed' ${LOG_FILE}
366
367# test detach/attach
20effc67
TL
368OUT=`_sudo rbd device --device-type nbd --options try-netlink,show-cookie map ${POOL}/${IMAGE}`
369read DEV COOKIE <<< "${OUT}"
522d829b 370get_pid ${POOL}
f67539c2 371_sudo mount ${DEV} ${TEMPDIR}/mnt
20effc67 372_sudo rbd device detach ${POOL}/${IMAGE} --device-type nbd
522d829b 373expect_false get_pid ${POOL}
20effc67
TL
374expect_false _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd
375if [ -n "${COOKIE}" ]; then
376 _sudo rbd device attach --device ${DEV} --cookie ${COOKIE} ${POOL}/${IMAGE} --device-type nbd
377else
378 _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd --force
379fi
522d829b 380get_pid ${POOL}
20effc67 381_sudo rbd device detach ${DEV} --device-type nbd
522d829b 382expect_false get_pid ${POOL}
20effc67
TL
383if [ -n "${COOKIE}" ]; then
384 _sudo rbd device attach --device ${DEV} --cookie ${COOKIE} ${POOL}/${IMAGE} --device-type nbd
385else
386 _sudo rbd device attach --device ${DEV} ${POOL}/${IMAGE} --device-type nbd --force
387fi
522d829b 388get_pid ${POOL}
f67539c2
TL
389ls ${TEMPDIR}/mnt/
390dd 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
393unmap_device ${DEV} ${PID}
20effc67
TL
394# if kernel supports cookies
395if [ -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}"
398 get_pid ${POOL}
399 test "${ANOTHER_COOKIE}" = "abc de"
400 unmap_device ${DEV} ${PID}
401fi
f67539c2 402
7c673cae 403echo OK