summaryrefslogtreecommitdiffstats
path: root/qa/rbd/common.sh
blob: 232cf45adb46306d711fc2aac974ab825ddd0f04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash

die() {
	echo "$*"
	exit 1
}

cleanup() {
    rm -rf $TDIR
    TDIR=""
}

set_variables() {
    # defaults
    [ -z "$bindir" ] && bindir=$PWD       # location of init-ceph
    if [ -z "$conf" ]; then
        conf="$basedir/ceph.conf"
        [ -e $conf ] || conf="/etc/ceph/ceph.conf"
    fi
    [ -e $conf ] || die "conf file not found"

    CCONF="ceph-conf -c $conf"

    [ -z "$mnt" ] && mnt="/c"
    if [ -z "$monhost" ]; then
        $CCONF -t mon -i 0 'mon addr' > $TDIR/cconf_mon
        if [ $? -ne 0 ]; then
            $CCONF -t mon.a -i 0 'mon addr' > $TDIR/cconf_mon
            [ $? -ne 0 ] && die "can't figure out \$monhost"
        fi
        read monhost < $TDIR/cconf_mon
    fi

    [ -z "$imgsize" ] && imgsize=1024
    [ -z "$user" ] && user=admin
    [ -z "$keyring" ] && keyring="`$CCONF keyring`"
    [ -z "$secret" ] && secret="`ceph-authtool $keyring -n client.$user -p`"

    monip="`echo $monhost | sed 's/:/ /g' | awk '{print $1}'`"
    monport="`echo $monhost | sed 's/:/ /g' | awk '{print $2}'`"

    [ -z "$monip" ] && die "bad mon address"

    [ -z "$monport" ] && monport=6789

    set -e

    mydir=`hostname`_`echo $0 | sed 's/\//_/g'`

    img_name=test.`hostname`.$$
}

rbd_load() {
	modprobe rbd
}

rbd_create_image() {
	id=$1
	rbd create $img_name.$id --size=$imgsize
}

rbd_add() {
	id=$1
	echo "$monip:$monport name=$user,secret=$secret rbd $img_name.$id" \
	    > /sys/bus/rbd/add

	pushd /sys/bus/rbd/devices &> /dev/null
	[ $? -eq 0 ] || die "failed to cd"
	devid=""
	rm -f "$TDIR/rbd_devs"
	for f in *; do echo $f >> "$TDIR/rbd_devs"; done
	sort -nr "$TDIR/rbd_devs" > "$TDIR/rev_rbd_devs"
	while read f < "$TDIR/rev_rbd_devs"; do
	  read d_img_name < "$f/name"
	  if [ "x$d_img_name" == "x$img_name.$id" ]; then
	    devid=$f
	    break
	  fi
	done
	popd &> /dev/null

	[ "x$devid" == "x" ] && die "failed to find $img_name.$id"

	export rbd$id=$devid
	while [ ! -e /dev/rbd$devid ]; do sleep 1; done
}

rbd_test_init() {
	rbd_load
}

rbd_remove() {
	echo $1 > /sys/bus/rbd/remove
}

rbd_rm_image() {
	id=$1
	rbd rm $imgname.$id
}

TDIR=`mktemp -d`
trap cleanup INT TERM EXIT
set_variables