Imported Upstream version 2.02.79
[platform/upstream/device-mapper.git] / scripts / clvmd_init_red_hat.in
1 #!/bin/bash
2 #
3 # clvmd - Clustered LVM Daemon init script
4 #
5 # chkconfig: - 24 76
6 # description: Cluster daemon for userland logical volume management tools.
7 #
8 # For Red-Hat-based distributions such as Fedora, RHEL, CentOS.
9 #
10 ### BEGIN INIT INFO
11 # Provides:             clvmd
12 # Required-Start:       $local_fs@CLVMD_CMANAGERS@
13 # Required-Stop:        $local_fs@CLVMD_CMANAGERS@
14 # Short-Description:    This service is Clusterd LVM Daemon.
15 # Description:          Cluster daemon for userland logical volume management tools.
16 ### END INIT INFO
17
18 . /etc/rc.d/init.d/functions
19
20 DAEMON=clvmd
21
22 exec_prefix=@exec_prefix@
23 sbindir=@sbindir@
24
25 lvm_vgchange=${sbindir}/vgchange
26 lvm_vgdisplay=${sbindir}/vgdisplay
27 lvm_vgscan=${sbindir}/vgscan
28 lvm_lvdisplay=${sbindir}/lvdisplay
29
30 CLVMDOPTS="-T30"
31
32 [ -f /etc/sysconfig/cluster ] && . /etc/sysconfig/cluster
33 [ -f /etc/sysconfig/$DAEMON ] && . /etc/sysconfig/$DAEMON
34
35 [ -n "$CLVMD_CLUSTER_IFACE" ] && CLVMDOPTS="$CLVMDOPTS -I $CLVMD_CLUSTER_IFACE" 
36
37 # allow up to $CLVMD_STOP_TIMEOUT seconds to clvmd to complete exit operations
38 # default to 10 seconds
39
40 [ -z $CLMVD_STOP_TIMEOUT ] && CLVMD_STOP_TIMEOUT=10
41
42 LOCK_FILE="/var/lock/subsys/$DAEMON"
43
44 # NOTE: replace this with vgs, once display filter per attr is implemented.
45 clustered_vgs() {
46         ${lvm_vgdisplay} 2>/dev/null | \
47                 awk 'BEGIN {RS="VG Name"} {if (/Clustered/) print $1;}'
48 }
49
50 clustered_active_lvs() {
51         for i in $(clustered_vgs); do
52                 ${lvm_lvdisplay} $i 2>/dev/null | \
53                 awk 'BEGIN {RS="LV Name"} {if (/[^N^O^T] available/) print $1;}'
54         done
55 }
56
57 rh_status() {
58         status $DAEMON
59 }
60
61 rh_status_q() {
62         rh_status >/dev/null 2>&1
63 }
64
65 start()
66 {
67         if ! rh_status_q; then
68                 echo -n "Starting $DAEMON: "
69                 $DAEMON $CLVMDOPTS || return $?
70                 echo
71         fi
72
73         # Refresh local cache.
74         #
75         # It's possible that new PVs were added to this, or other VGs
76         # while this node was down. So we run vgscan here to avoid
77         # any potential "Missing UUID" messages with subsequent
78         # LVM commands.
79
80         # The following step would be better and more informative to the user:
81         # 'action "Refreshing VG(s) local cache:" ${lvm_vgscan}'
82         # but it could show warnings such as:
83         # 'clvmd not running on node x-y-z  Unable to obtain global lock.'
84         # and the action would be shown as FAILED when in reality it didn't.
85         # Ideally vgscan should have a startup mode that would not print
86         # unnecessary warnings.
87
88         ${lvm_vgscan} > /dev/null 2>&1
89
90         action "Activating VG(s):" ${lvm_vgchange} -ayl $LVM_VGS || return $?
91
92         touch $LOCK_FILE
93
94         return 0
95 }
96
97 wait_for_finish()
98 {
99         count=0
100         while [ "$count" -le "$CLVMD_STOP_TIMEOUT" ] && \
101                  rh_status_q ]; do
102                 sleep 1
103                 count=$((count+1))
104         done
105
106         ! rh_status_q
107 }
108
109 stop()
110 {
111         rh_status_q || return 0
112
113         [ -z "$LVM_VGS" ] && LVM_VGS="$(clustered_vgs)"
114         if [ -n "$LVM_VGS" ]; then
115                 action "Deactivating clustered VG(s):" ${lvm_vgchange} -anl $LVM_VGS || return $?
116         fi
117
118         action "Signaling $DAEMON to exit" kill -TERM $(pidofproc $DAEMON) || return $?
119
120         # wait half second before we start the waiting loop or we will show
121         # the loop more time than really necessary
122         usleep 500000
123
124         # clvmd could take some time to stop
125         rh_status_q && action "Waiting for $DAEMON to exit:" wait_for_finish
126
127         if rh_status_q; then
128                 echo -n "$DAEMON failed to exit"
129                 failure
130                 echo
131                 return 1
132         else
133                 echo -n "$DAEMON terminated"
134                 success
135                 echo
136         fi
137
138         rm -f $LOCK_FILE
139
140         return 0
141 }
142
143 reload() {
144         rh_status_q || exit 7
145         action "Reloading $DAEMON configuration: " $DAEMON -R || return $?
146 }
147
148 restart() {
149         # if stop fails, restart will return the error and not attempt
150         # another start. Even if start is protected by rh_status_q,
151         # that would avoid spawning another daemon, it would try to
152         # reactivate the VGs.
153
154         # Try to get clvmd to restart itself. This will preserve 
155         # exclusive LV locks
156         action "Restarting $DAEMON: " $DAEMON -S
157
158         # If that fails then do a normal stop & restart
159         if  [ $? != 0 ]; then
160             stop && start
161             return $?
162         else
163             touch $LOCK_FILE
164             return 0
165         fi
166 }
167
168 [ "$EUID" != "0" ] && {
169         echo "clvmd init script can only be executed as root user"
170         exit 4
171 }
172
173 # See how we were called.
174 case "$1" in
175   start)
176         start
177         rtrn=$?
178         ;;
179
180   stop)
181         stop
182         rtrn=$?
183         ;;
184
185   restart|force-reload)
186         restart
187         rtrn=$?
188         ;;
189
190   condrestart|try-restart)
191         rh_status_q || exit 0
192         restart
193         rtrn=$? 
194         ;;
195
196   reload)
197         reload
198         rtrn=$?
199         ;;
200
201   status)
202         rh_status
203         rtrn=$?
204         if [ $rtrn = 0 ]; then
205                 cvgs="$(clustered_vgs)"
206                 echo Clustered Volume Groups: ${cvgs:-"(none)"}
207                 clvs="$(clustered_active_lvs)"
208                 echo Active clustered Logical Volumes: ${clvs:-"(none)"}
209         fi
210         ;;
211
212   *)
213         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
214         rtrn=2
215         ;;
216 esac
217
218 exit $rtrn