2011-12-06 18:31:17 +00:00
|
|
|
#!/bin/bash -ex
|
|
|
|
|
|
|
|
CEPH_SECRET_FILE=${CEPH_SECRET_FILE:-}
|
|
|
|
CEPH_ID=${CEPH_ID:-admin}
|
|
|
|
SECRET_ARGS=''
|
2011-12-06 22:19:19 +00:00
|
|
|
if [ ! -z $CEPH_SECRET_FILE ]; then
|
2011-12-06 18:31:17 +00:00
|
|
|
SECRET_ARGS="--secret $CEPH_SECRET_FILE"
|
|
|
|
fi
|
|
|
|
|
|
|
|
TMP_FILES="/tmp/img1 /tmp/img1.small /tmp/img1.snap1 /tmp/img1.export /tmp/img1.trunc"
|
|
|
|
|
|
|
|
function get_device_dir {
|
|
|
|
local POOL=$1
|
|
|
|
local IMAGE=$2
|
|
|
|
local SNAP=$3
|
2012-10-04 23:56:53 +00:00
|
|
|
rbd showmapped | tail -n +2 | egrep "\s+$POOL\s+$IMAGE\s+$SNAP\s+" | awk '{print $1;}'
|
2011-12-06 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function clean_up {
|
2013-05-30 23:10:46 +00:00
|
|
|
udevadm settle
|
|
|
|
[ -e /dev/rbd/rbd/testimg1@snap1 ] &&
|
|
|
|
rbd unmap /dev/rbd/rbd/testimg1@snap1 || true
|
|
|
|
if [ -e /dev/rbd/rbd/testimg1 ]; then
|
|
|
|
rbd unmap /dev/rbd/rbd/testimg1 || true
|
|
|
|
rbd snap purge testimg1 || true
|
|
|
|
fi
|
|
|
|
udevadm settle
|
2011-12-06 18:31:17 +00:00
|
|
|
sudo chown root /sys/bus/rbd/add /sys/bus/rbd/remove
|
2013-05-30 23:10:46 +00:00
|
|
|
rbd ls | grep testimg1 > /dev/null && rbd rm testimg1 || true
|
2011-12-06 22:19:19 +00:00
|
|
|
sudo rm -f $TMP_FILES
|
2011-12-06 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
2013-07-08 16:58:16 +00:00
|
|
|
[ -d /sys/bus/rbd ] || sudo modprobe rbd
|
|
|
|
|
2011-12-06 18:31:17 +00:00
|
|
|
clean_up
|
|
|
|
|
|
|
|
trap clean_up INT TERM EXIT
|
|
|
|
|
|
|
|
# allow ubuntu user to map/unmap rbd devices
|
|
|
|
sudo chown ubuntu /sys/bus/rbd/add /sys/bus/rbd/remove
|
|
|
|
|
|
|
|
# create an image
|
|
|
|
dd if=/bin/sh of=/tmp/img1 bs=1k count=1 seek=10
|
|
|
|
dd if=/bin/dd of=/tmp/img1 bs=1k count=10 seek=100
|
|
|
|
dd if=/bin/rm of=/tmp/img1 bs=1k count=100 seek=1000
|
|
|
|
dd if=/bin/ls of=/tmp/img1 bs=1k seek=10000
|
|
|
|
dd if=/bin/ln of=/tmp/img1 bs=1k seek=100000
|
|
|
|
dd if=/dev/zero of=/tmp/img1 count=0 seek=150000
|
|
|
|
|
|
|
|
# import
|
|
|
|
rbd import /tmp/img1 testimg1
|
|
|
|
rbd map testimg1 --user $CEPH_ID $SECRET_ARGS
|
2013-05-30 23:10:46 +00:00
|
|
|
# wait for udev to catch up
|
|
|
|
udevadm settle
|
|
|
|
|
2011-12-06 18:31:17 +00:00
|
|
|
DEV_ID1=$(get_device_dir rbd testimg1 -)
|
|
|
|
echo "dev_id1 = $DEV_ID1"
|
|
|
|
cat /sys/bus/rbd/devices/$DEV_ID1/size
|
|
|
|
cat /sys/bus/rbd/devices/$DEV_ID1/size | grep 76800000
|
|
|
|
|
|
|
|
sudo dd if=/dev/rbd/rbd/testimg1 of=/tmp/img1.export
|
|
|
|
cmp /tmp/img1 /tmp/img1.export
|
|
|
|
|
|
|
|
# snapshot
|
|
|
|
rbd snap create testimg1 --snap=snap1
|
|
|
|
rbd map --snap=snap1 testimg1 --user $CEPH_ID $SECRET_ARGS
|
2013-05-30 23:10:46 +00:00
|
|
|
# wait for udev to catch up
|
|
|
|
udevadm settle
|
|
|
|
|
2011-12-06 18:31:17 +00:00
|
|
|
DEV_ID2=$(get_device_dir rbd testimg1 snap1)
|
|
|
|
cat /sys/bus/rbd/devices/$DEV_ID2/size | grep 76800000
|
|
|
|
|
|
|
|
sudo dd if=/dev/rbd/rbd/testimg1@snap1 of=/tmp/img1.snap1
|
|
|
|
cmp /tmp/img1 /tmp/img1.snap1
|
|
|
|
|
|
|
|
# resize
|
2013-04-19 21:08:51 +00:00
|
|
|
rbd resize testimg1 --size=40 --allow-shrink
|
2011-12-06 18:31:17 +00:00
|
|
|
cat /sys/bus/rbd/devices/$DEV_ID1/size | grep 41943040
|
|
|
|
cat /sys/bus/rbd/devices/$DEV_ID2/size | grep 76800000
|
|
|
|
|
|
|
|
sudo dd if=/dev/rbd/rbd/testimg1 of=/tmp/img1.small
|
|
|
|
cp /tmp/img1 /tmp/img1.trunc
|
|
|
|
truncate -s 41943040 /tmp/img1.trunc
|
|
|
|
cmp /tmp/img1.trunc /tmp/img1.small
|
|
|
|
|
|
|
|
# rollback and check data again
|
|
|
|
rbd snap rollback --snap=snap1 testimg1
|
|
|
|
cat /sys/bus/rbd/devices/$DEV_ID1/size | grep 76800000
|
2013-05-30 23:10:46 +00:00
|
|
|
cat /sys/bus/rbd/devices/$DEV_ID2/size | grep 76800000
|
2011-12-06 22:19:19 +00:00
|
|
|
sudo rm -f /tmp/img1.snap1 /tmp/img1.export
|
2011-12-06 18:31:17 +00:00
|
|
|
|
|
|
|
sudo dd if=/dev/rbd/rbd/testimg1@snap1 of=/tmp/img1.snap1
|
|
|
|
cmp /tmp/img1 /tmp/img1.snap1
|
|
|
|
sudo dd if=/dev/rbd/rbd/testimg1 of=/tmp/img1.export
|
|
|
|
cmp /tmp/img1 /tmp/img1.export
|
|
|
|
|
|
|
|
# remove snapshot and detect error from mapped snapshot
|
|
|
|
rbd snap rm --snap=snap1 testimg1
|
|
|
|
sudo dd if=/dev/rbd/rbd/testimg1@snap1 of=/tmp/img1.snap1 2>&1 | grep 'Input/output error'
|
|
|
|
|
|
|
|
echo OK
|