~upstart-devel/upstart/upstart-jobs

« back to all changes in this revision

Viewing changes to utopic/etc/init.d/rbdmap

  • Committer: Dimitri John Ledkov
  • Date: 2014-11-19 12:58:41 UTC
  • Revision ID: dimitri.j.ledkov@intel.com-20141119125841-98dr37roy8dvcv3b
auto update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
#
3
 
# rbdmap Ceph RBD Mapping
4
 
#
5
 
# chkconfig: 2345 20 80
6
 
# description: Ceph RBD Mapping
7
 
 
8
 
### BEGIN INIT INFO
9
 
# Provides:          rbdmap
10
 
# Required-Start:    $network $remote_fs
11
 
# Required-Stop:     $network $remote_fs
12
 
# Should-Start:      ceph
13
 
# Should-Stop:       ceph
14
 
# X-Start-Before:    $x-display-manager
15
 
# Default-Start:     2 3 4 5
16
 
# Default-Stop:      0 1 6
17
 
# Short-Description: Ceph RBD Mapping
18
 
# Description:       Ceph RBD Mapping
19
 
### END INIT INFO
20
 
 
21
 
DESC="RBD Mapping:"
22
 
RBDMAPFILE="/etc/ceph/rbdmap"
23
 
 
24
 
. /lib/lsb/init-functions
25
 
 
26
 
do_map() {
27
 
        if [ ! -f "$RBDMAPFILE" ]; then
28
 
                log_warning_msg "$DESC : No $RBDMAPFILE found."
29
 
                exit 0
30
 
        fi
31
 
 
32
 
        # Read /etc/rbdtab to create non-existant mapping
33
 
        RET=0
34
 
        while read DEV PARAMS; do
35
 
                case "$DEV" in
36
 
                  ""|\#*)
37
 
                        continue
38
 
                        ;;
39
 
                  */*)
40
 
                        ;;
41
 
                  *)
42
 
                        DEV=rbd/$DEV
43
 
                        ;;
44
 
                esac
45
 
                log_action_begin_msg "${DESC} '${DEV}'"
46
 
                newrbd=""
47
 
                MAP_RV=""
48
 
                RET_OP=0
49
 
                OIFS=$IFS
50
 
                IFS=','
51
 
                for PARAM in ${PARAMS[@]}; do
52
 
                        CMDPARAMS="$CMDPARAMS --$(echo $PARAM | tr '=' ' ')"
53
 
                done
54
 
                IFS=$OIFS
55
 
                if [ ! -b /dev/rbd/$DEV ]; then
56
 
                        MAP_RV=$(rbd map $DEV $CMDPARAMS 2>&1)
57
 
                        if [ $? -eq 0 ]; then
58
 
                            newrbd="yes"
59
 
                        else
60
 
                            RET=$((${RET}+$?))
61
 
                            RET_OP=1
62
 
                        fi
63
 
                fi
64
 
                log_action_end_msg ${RET_OP} "${MAP_RV}"
65
 
 
66
 
                if [ "$newrbd" ]; then
67
 
                        ## Mount new rbd
68
 
                        MNT_RV=""
69
 
                        mount --fake /dev/rbd/$DEV >>/dev/null 2>&1 \
70
 
                        && MNT_RV=$(mount -v /dev/rbd/$DEV 2>&1)
71
 
                        [ -n "${MNT_RV}" ] && log_action_msg "mount: ${MNT_RV}"
72
 
 
73
 
                        ## post-mapping
74
 
                        if [ -x "/etc/ceph/rbd.d/${DEV}" ]; then
75
 
                            log_action_msg "RBD Running post-map hook '/etc/ceph/rbd.d/${DEV}'"
76
 
                            /etc/ceph/rbd.d/${DEV} map "/dev/rbd/${DEV}"
77
 
                        fi
78
 
                fi
79
 
        done < $RBDMAPFILE
80
 
        exit ${RET}
81
 
 
82
 
}
83
 
 
84
 
do_unmap() {
85
 
        RET=0
86
 
        ## Unmount and unmap all rbd devices
87
 
        if ls /dev/rbd[0-9]* >/dev/null 2>&1; then
88
 
                for DEV in /dev/rbd[0-9]*; do
89
 
                        ## pre-unmapping
90
 
                        for L in $(find /dev/rbd -type l); do
91
 
                            LL="${L##/dev/rbd/}"
92
 
                            if [ "$(readlink -f $L)" = "${DEV}" ] \
93
 
                            && [ -x "/etc/ceph/rbd.d/${LL}" ]; then
94
 
                                log_action_msg "RBD pre-unmap:  '${DEV}' hook '/etc/ceph/rbd.d/${LL}'"
95
 
                                /etc/ceph/rbd.d/${LL} unmap "$L"
96
 
                                break
97
 
                            fi
98
 
                        done
99
 
 
100
 
                        log_action_begin_msg "RBD un-mapping: '${DEV}'"
101
 
                        UMNT_RV=""
102
 
                        UMAP_RV=""
103
 
                        RET_OP=0
104
 
                        MNT=$(findmnt --mtab --source ${DEV} --noheadings | awk '{print $1'})
105
 
                        if [ -n "${MNT}" ]; then
106
 
                            log_action_cont_msg "un-mounting '${MNT}'"
107
 
                            local ULAZY=""
108
 
                            local RUNLEVEL=$(runlevel | awk '{print $2}')
109
 
                            if [ $? -eq 0 ]; then
110
 
                                if [ "${RUNLEVEL}" -eq 0 ] \
111
 
                                || [ "${RUNLEVEL}" -eq 6 ] ; then
112
 
                                    ULAZY="-l"
113
 
                                    if [ -x "$(which fuser)" ]; then
114
 
                                        fuser -M -m "${MNT}" --kill -TERM
115
 
                                    fi
116
 
                                fi
117
 
                            fi
118
 
                            UMNT_RV=$(umount ${ULAZY} "${MNT}" 2>&1)
119
 
                        fi
120
 
                        if mountpoint -q "${MNT}"; then
121
 
                            ## Un-mounting failed.
122
 
                            RET_OP=1
123
 
                            RET=$((${RET}+1))
124
 
                        else
125
 
                            ## Un-mapping.
126
 
                            UMAP_RV=$(rbd unmap $DEV 2>&1)
127
 
                            if [ $? -ne 0 ]; then
128
 
                                RET=$((${RET}+$?))
129
 
                                RET_OP=1
130
 
                            fi
131
 
                        fi
132
 
                        log_action_end_msg ${RET_OP} "${UMAP_RV}"
133
 
                        [ -n "${UMNT_RV}" ] && log_action_msg "${UMNT_RV}"
134
 
                done
135
 
        fi
136
 
        exit ${RET}
137
 
}
138
 
 
139
 
 
140
 
case "$1" in
141
 
  start)
142
 
        do_map
143
 
        ;;
144
 
 
145
 
  stop)
146
 
        do_unmap
147
 
        ;;
148
 
 
149
 
  restart|force-reload)
150
 
        $0 stop
151
 
        $0 start
152
 
        ;;
153
 
 
154
 
  reload)
155
 
        do_map
156
 
        ;;
157
 
 
158
 
  status)
159
 
        rbd showmapped
160
 
        ;;
161
 
 
162
 
  *)
163
 
        log_success_msg "Usage: rbdmap {start|stop|restart|force-reload|reload|status}"
164
 
        exit 1
165
 
        ;;
166
 
esac