-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit-rbdmap
executable file
·155 lines (138 loc) · 3.07 KB
/
init-rbdmap
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
#
# rbdmap Ceph RBD Mapping
#
# chkconfig: 2345 20 80
# description: Ceph RBD Mapping
### BEGIN INIT INFO
# Provides: rbdmap
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs
# Should-Start: ceph
# Should-Stop: ceph
# X-Start-Before: $x-display-manager
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Ceph RBD Mapping
# Description: Ceph RBD Mapping
### END INIT INFO
DESC="RBD Mapping:"
RBDMAPFILE="/etc/ceph/rbdmap"
. /lib/lsb/init-functions
do_map() {
if [ ! -f "$RBDMAPFILE" ]; then
log_warning_msg "$DESC : No $RBDMAPFILE found."
exit 0
fi
# Read /etc/rbdtab to create non-existant mapping
RET=0
while read DEV PARAMS; do
case "$DEV" in
""|\#*)
continue
;;
*/*)
;;
*)
DEV=rbd/$DEV
;;
esac
log_action_begin_msg "${DESC} '${DEV}'"
newrbd=""
MAP_RV=""
RET_OP=0
OIFS=$IFS
IFS=','
for PARAM in ${PARAMS[@]}; do
CMDPARAMS="$CMDPARAMS --$(echo $PARAM | tr '=' ' ')"
done
IFS=$OIFS
if [ ! -b /dev/rbd/$DEV ]; then
MAP_RV=$(rbd map $DEV $CMDPARAMS 2>&1)
if [ $? -eq 0 ]; then
newrbd="yes"
else
RET=$((${RET}+$?))
RET_OP=1
fi
fi
log_action_end_msg ${RET_OP} "${MAP_RV}"
if [ "$newrbd" ]; then
## Mount new rbd
MNT_RV=""
mount --fake /dev/rbd/$DEV >>/dev/null 2>&1 \
&& MNT_RV=$(mount -vn /dev/rbd/$DEV 2>&1)
[ -n "${MNT_RV}" ] && log_action_msg "mount: ${MNT_RV}"
## post-mapping
if [ -x "/etc/ceph/rbd.d/${DEV}" ]; then
log_action_msg "RBD Running post-map hook '/etc/ceph/rbd.d/${DEV}'"
/etc/ceph/rbd.d/${DEV} map "/dev/rbd/${DEV}"
fi
fi
done < $RBDMAPFILE
exit ${RET}
}
do_unmap() {
RET=0
## Unmount and unmap all rbd devices
if ls /dev/rbd[0-9]* >/dev/null 2>&1; then
for DEV in /dev/rbd[0-9]*; do
## pre-unmapping
for L in $(find /dev/rbd -type l); do
LL="${L##/dev/rbd/}"
if [ "$(readlink -f $L)" = "${DEV}" ] \
&& [ -x "/etc/ceph/rbd.d/${LL}" ]; then
log_action_msg "RBD pre-unmap: '${DEV}' hook '/etc/ceph/rbd.d/${LL}'"
/etc/ceph/rbd.d/${LL} unmap "$L"
break
fi
done
log_action_begin_msg "RBD un-mapping: '${DEV}'"
UMNT_RV=""
UMAP_RV=""
RET_OP=0
MNT=$(findmnt --mtab --source ${DEV} --noheadings | awk '{print $1'})
if [ -n "${MNT}" ]; then
log_action_cont_msg "un-mounting '${MNT}'"
UMNT_RV=$(umount "${MNT}" 2>&1)
fi
if mountpoint -q "${MNT}"; then
## Un-mounting failed.
RET_OP=1
RET=$((${RET}+1))
else
## Un-mapping.
UMAP_RV=$(rbd unmap $DEV 2>&1)
if [ $? -ne 0 ]; then
RET=$((${RET}+$?))
RET_OP=1
fi
fi
log_action_end_msg ${RET_OP} "${UMAP_RV}"
[ -n "${UMNT_RV}" ] && log_action_msg "${UMNT_RV}"
done
fi
exit ${RET}
}
case "$1" in
start)
do_map
;;
stop)
do_unmap
;;
restart|force-reload)
$0 stop
$0 start
;;
reload)
do_map
;;
status)
rbd showmapped
;;
*)
log_success_msg "Usage: rbdmap {start|stop|restart|force-reload|reload|status}"
exit 1
;;
esac