]> git.proxmox.com Git - ceph.git/blame - ceph/qa/workunits/rbd/import_export.sh
update sources to 12.2.7
[ceph.git] / ceph / qa / workunits / rbd / import_export.sh
CommitLineData
7c673cae
FG
1#!/bin/sh -ex
2
3# returns data pool for a given image
4get_image_data_pool () {
5 image=$1
6 data_pool=$(rbd info $image | grep "data_pool: " | awk -F':' '{ print $NF }')
7 if [ -z $data_pool ]; then
8 data_pool='rbd'
9 fi
10
11 echo $data_pool
12}
13
14# return list of object numbers populated in image
15objects () {
16 image=$1
17 prefix=$(rbd info $image | grep block_name_prefix | awk '{print $NF;}')
18
19 # strip off prefix and leading zeros from objects; sort, although
20 # it doesn't necessarily make sense as they're hex, at least it makes
21 # the list repeatable and comparable
22 objects=$(rados ls -p $(get_image_data_pool $image) | grep $prefix | \
23 sed -e 's/'$prefix'\.//' -e 's/^0*\([0-9a-f]\)/\1/' | sort -u)
24 echo $objects
25}
26
27# return false if either files don't compare or their ondisk
28# sizes don't compare
29
30compare_files_and_ondisk_sizes () {
31 cmp -l $1 $2 || return 1
32 origsize=$(stat $1 --format %b)
33 exportsize=$(stat $2 --format %b)
34 difference=$(($exportsize - $origsize))
35 difference=${difference#-} # absolute value
36 test $difference -ge 0 -a $difference -lt 4096
37}
38
39TMPDIR=/tmp/rbd_import_export_$$
40rm -rf $TMPDIR
41mkdir $TMPDIR
42trap "rm -rf $TMPDIR" INT TERM EXIT
43
44# cannot import a dir
45mkdir foo.$$
46rbd import foo.$$ foo.dir && exit 1 || true # should fail
47rmdir foo.$$
48
49# create a sparse file
50dd if=/bin/sh of=${TMPDIR}/img bs=1k count=1 seek=10
51dd if=/bin/dd of=${TMPDIR}/img bs=1k count=10 seek=100
52dd if=/bin/rm of=${TMPDIR}/img bs=1k count=100 seek=1000
53dd if=/bin/ls of=${TMPDIR}/img bs=1k seek=10000
54dd if=/bin/ln of=${TMPDIR}/img bs=1k seek=100000
55dd if=/bin/grep of=${TMPDIR}/img bs=1k seek=1000000
56
57rbd rm testimg || true
58
59rbd import $RBD_CREATE_ARGS ${TMPDIR}/img testimg
60rbd export testimg ${TMPDIR}/img2
61rbd export testimg - > ${TMPDIR}/img3
62rbd rm testimg
63cmp ${TMPDIR}/img ${TMPDIR}/img2
64cmp ${TMPDIR}/img ${TMPDIR}/img3
65rm ${TMPDIR}/img2 ${TMPDIR}/img3
66
67# try again, importing from stdin
68rbd import $RBD_CREATE_ARGS - testimg < ${TMPDIR}/img
69rbd export testimg ${TMPDIR}/img2
70rbd export testimg - > ${TMPDIR}/img3
71rbd rm testimg
72cmp ${TMPDIR}/img ${TMPDIR}/img2
73cmp ${TMPDIR}/img ${TMPDIR}/img3
74
75rm ${TMPDIR}/img ${TMPDIR}/img2 ${TMPDIR}/img3
76
224ce89b
WB
77if rbd help export | grep -q export-format; then
78 # try with --export-format for snapshots
79 dd if=/bin/dd of=${TMPDIR}/img bs=1k count=10 seek=100
80 rbd import $RBD_CREATE_ARGS ${TMPDIR}/img testimg
81 rbd snap create testimg@snap
28e407b8
AA
82 rbd image-meta set testimg key1 value1
83 IMAGEMETA_BEFORE=`rbd image-meta list testimg`
224ce89b
WB
84 rbd export --export-format 2 testimg ${TMPDIR}/img_v2
85 rbd import --export-format 2 ${TMPDIR}/img_v2 testimg_import
86 rbd info testimg_import
87 rbd info testimg_import@snap
28e407b8
AA
88 IMAGEMETA_AFTER=`rbd image-meta list testimg_import`
89 [ "$IMAGEMETA_BEFORE" = "$IMAGEMETA_AFTER" ]
224ce89b
WB
90
91 # compare the contents between testimg and testimg_import
92 rbd export testimg_import ${TMPDIR}/img_import
93 compare_files_and_ondisk_sizes ${TMPDIR}/img ${TMPDIR}/img_import
94
95 rbd export testimg@snap ${TMPDIR}/img_snap
96 rbd export testimg_import@snap ${TMPDIR}/img_snap_import
97 compare_files_and_ondisk_sizes ${TMPDIR}/img_snap ${TMPDIR}/img_snap_import
98
99 rm ${TMPDIR}/img_v2
100 rm ${TMPDIR}/img_import
101 rm ${TMPDIR}/img_snap
102 rm ${TMPDIR}/img_snap_import
103
104 rbd snap rm testimg_import@snap
105 rbd remove testimg_import
106 rbd snap rm testimg@snap
107 rbd rm testimg
108
109 # order
110 rbd import --order 20 ${TMPDIR}/img testimg
111 rbd export --export-format 2 testimg ${TMPDIR}/img_v2
112 rbd import --export-format 2 ${TMPDIR}/img_v2 testimg_import
113 rbd info testimg_import|grep order|awk '{print $2}'|grep 20
114
115 rm ${TMPDIR}/img_v2
116
117 rbd remove testimg_import
118 rbd remove testimg
119
120 # features
121 rbd import --image-feature layering ${TMPDIR}/img testimg
122 FEATURES_BEFORE=`rbd info testimg|grep features`
123 rbd export --export-format 2 testimg ${TMPDIR}/img_v2
124 rbd import --export-format 2 ${TMPDIR}/img_v2 testimg_import
125 FEATURES_AFTER=`rbd info testimg_import|grep features`
126 if [ "$FEATURES_BEFORE" != "$FEATURES_AFTER" ]; then
127 false
128 fi
7c673cae 129
224ce89b 130 rm ${TMPDIR}/img_v2
7c673cae 131
224ce89b
WB
132 rbd remove testimg_import
133 rbd remove testimg
7c673cae 134
224ce89b
WB
135 # stripe
136 rbd import --stripe-count 1000 --stripe-unit 4096 ${TMPDIR}/img testimg
137 rbd export --export-format 2 testimg ${TMPDIR}/img_v2
138 rbd import --export-format 2 ${TMPDIR}/img_v2 testimg_import
28e407b8 139 rbd info testimg_import|grep "stripe unit"|grep -Ei '(4 KiB|4K|4096)'
224ce89b 140 rbd info testimg_import|grep "stripe count"|awk '{print $3}'|grep 1000
7c673cae 141
224ce89b 142 rm ${TMPDIR}/img_v2
7c673cae 143
224ce89b
WB
144 rbd remove testimg_import
145 rbd remove testimg
146fi
7c673cae
FG
147
148tiered=0
149if ceph osd dump | grep ^pool | grep "'rbd'" | grep tier; then
150 tiered=1
151fi
152
153# create specifically sparse files
154# 1 1M block of sparse, 1 1M block of random
155dd if=/dev/urandom bs=1M seek=1 count=1 of=${TMPDIR}/sparse1
156
157# 1 1M block of random, 1 1M block of sparse
158dd if=/dev/urandom bs=1M count=1 of=${TMPDIR}/sparse2; truncate ${TMPDIR}/sparse2 -s 2M
159
160# 1M-block images; validate resulting blocks
161
162# 1M sparse, 1M data
163rbd rm sparse1 || true
164rbd import $RBD_CREATE_ARGS --order 20 ${TMPDIR}/sparse1
28e407b8 165rbd ls -l | grep sparse1 | grep -Ei '(2 MiB|2M|2048k)'
7c673cae
FG
166[ $tiered -eq 1 -o "$(objects sparse1)" = '1' ]
167
168# export, compare contents and on-disk size
169rbd export sparse1 ${TMPDIR}/sparse1.out
170compare_files_and_ondisk_sizes ${TMPDIR}/sparse1 ${TMPDIR}/sparse1.out
171rm ${TMPDIR}/sparse1.out
172rbd rm sparse1
173
174# 1M data, 1M sparse
175rbd rm sparse2 || true
176rbd import $RBD_CREATE_ARGS --order 20 ${TMPDIR}/sparse2
28e407b8 177rbd ls -l | grep sparse2 | grep -Ei '(2 MiB|2M|2048k)'
7c673cae
FG
178[ $tiered -eq 1 -o "$(objects sparse2)" = '0' ]
179rbd export sparse2 ${TMPDIR}/sparse2.out
180compare_files_and_ondisk_sizes ${TMPDIR}/sparse2 ${TMPDIR}/sparse2.out
181rm ${TMPDIR}/sparse2.out
182rbd rm sparse2
183
184# extend sparse1 to 10 1M blocks, sparse at the end
185truncate ${TMPDIR}/sparse1 -s 10M
186# import from stdin just for fun, verify still sparse
187rbd import $RBD_CREATE_ARGS --order 20 - sparse1 < ${TMPDIR}/sparse1
28e407b8 188rbd ls -l | grep sparse1 | grep -Ei '(10 MiB|10M|10240k)'
7c673cae
FG
189[ $tiered -eq 1 -o "$(objects sparse1)" = '1' ]
190rbd export sparse1 ${TMPDIR}/sparse1.out
191compare_files_and_ondisk_sizes ${TMPDIR}/sparse1 ${TMPDIR}/sparse1.out
192rm ${TMPDIR}/sparse1.out
193rbd rm sparse1
194
195# extend sparse2 to 4M total with two more nonsparse megs
196dd if=/dev/urandom bs=2M count=1 of=${TMPDIR}/sparse2 oflag=append conv=notrunc
197# again from stding
198rbd import $RBD_CREATE_ARGS --order 20 - sparse2 < ${TMPDIR}/sparse2
28e407b8 199rbd ls -l | grep sparse2 | grep -Ei '(4 MiB|4M|4096k)'
7c673cae
FG
200[ $tiered -eq 1 -o "$(objects sparse2)" = '0 2 3' ]
201rbd export sparse2 ${TMPDIR}/sparse2.out
202compare_files_and_ondisk_sizes ${TMPDIR}/sparse2 ${TMPDIR}/sparse2.out
203rm ${TMPDIR}/sparse2.out
204rbd rm sparse2
205
206# zeros import to a sparse image. Note: all zeros currently
207# doesn't work right now due to the way we handle 'empty' fiemaps;
208# the image ends up zero-filled.
209
210echo "partially-sparse file imports to partially-sparse image"
211rbd import $RBD_CREATE_ARGS --order 20 ${TMPDIR}/sparse1 sparse
212[ $tiered -eq 1 -o "$(objects sparse)" = '1' ]
213rbd rm sparse
214
215echo "zeros import through stdin to sparse image"
216# stdin
217dd if=/dev/zero bs=1M count=4 | rbd import $RBD_CREATE_ARGS - sparse
218[ $tiered -eq 1 -o "$(objects sparse)" = '' ]
219rbd rm sparse
220
221echo "zeros export to sparse file"
222# Must be tricky to make image "by hand" ; import won't create a zero image
223rbd create $RBD_CREATE_ARGS sparse --size 4
224prefix=$(rbd info sparse | grep block_name_prefix | awk '{print $NF;}')
225# drop in 0 object directly
226dd if=/dev/zero bs=4M count=1 | rados -p $(get_image_data_pool sparse) \
227 put ${prefix}.000000000000 -
228[ $tiered -eq 1 -o "$(objects sparse)" = '0' ]
229# 1 object full of zeros; export should still create 0-disk-usage file
230rm ${TMPDIR}/sparse || true
231rbd export sparse ${TMPDIR}/sparse
232[ $(stat ${TMPDIR}/sparse --format=%b) = '0' ]
233rbd rm sparse
234
235rm ${TMPDIR}/sparse ${TMPDIR}/sparse1 ${TMPDIR}/sparse2 ${TMPDIR}/sparse3 || true
236
237echo OK