Whamcloud - gitweb
LU-4707 build: Don’t deploy the "lustre" init script on clients
[fs/lustre-release.git] / lustre / scripts / lnet
1 #!/bin/bash
2 #
3 # lnet  This shell script takes care of starting and stopping
4 #       the lnet (Lustre networking) services.
5 #
6 # chkconfig: - 59 76
7 # description:  Part of the lustre file system.
8 # probe: true
9 # config: /etc/sysconfig/lnet
10
11 # Source function library.
12 [ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions
13
14 # Source networking configuration and check that networking is up.
15 [ -f /etc/sysconfig/network ] && . /etc/sysconfig/network && \
16 [ "${NETWORKING}" = "no" ] && exit 0
17
18 # Check for and source configuration file otherwise set defaults
19 [ -f /etc/sysconfig/lnet ] && . /etc/sysconfig/lnet
20
21 declare -r TOP_MODULES=(        \
22         obdecho                 \
23         llite                   \
24         lustre                  \
25         osc                     \
26         lov                     \
27         mds                     \
28         mdc                     \
29         mgs                     \
30         mgc                     \
31         ost                     \
32         obdfilter               \
33         lquota                  \
34         ptlrpc                  \
35 )
36 declare -r BOTTOM_MODULES=(     \
37         ksocklnd                \
38         kqswlnd                 \
39         ko2iblnd                \
40         obdclass                \
41         lnet                    \
42         lvfs                    \
43         libcfs                  \
44         ldiskfs                 \
45 )
46
47 declare -r awkprog='BEGIN { rc = -1 }
48                        { if ( $1 == module_name ) { rc = $3; exit; } }
49                     END { print rc }'
50
51 # Usage: run_preexec_check [ start | restart | condrestart ]
52 # The single parameter will be passed to the PREEXEC_SCRIPT
53 run_preexec_check ()
54 {
55         if [ -n "$PREEXEC_CHECK" ] && ! $PREEXEC_CHECK ; then
56                 echo "Pre-exec check \"$PREEXEC_CHECK\" failed.  Aborting."
57                 exit 1
58         fi
59
60         if [ -n "$PREEXEC_SCRIPT" ] && ! "$PREEXEC_SCRIPT" "$1" ; then
61                 echo "Pre-exec script \"$PREEXEC_SCRIPT\" failed.  Aborting."
62                 exit 1
63         fi
64 }
65
66 # Usage: run_postexec_check [ start | restart | condrestart ]
67 # The single parameter will be passed to the POSTEXEC_SCRIPT
68 run_postexec_check ()
69 {
70         if [ -n "$POSTEXEC_CHECK" ] && ! $POSTEXEC_CHECK ; then
71                 echo "Post-exec check \"$POSTEXEC_CHECK\" failed.  Aborting."
72                 exit 1
73         fi
74
75         if [ -n "$POSTEXEC_SCRIPT" ] && ! "$POSTEXEC_SCRIPT" "$1" ; then
76                 echo "Post-exec script \"$POSTEXEC_SCRIPT\" failed.  Aborting."
77                 exit 1
78         fi
79 }
80
81 remove_modules ()
82 {
83         local modules="${@}"
84         local ref_cnt
85
86         for mod in $modules; do
87                 ref_cnt=`/sbin/lsmod | awk "$awkprog" "module_name=$mod"`
88                 if [ $ref_cnt -lt 0 ]; then
89                         # module not loaded, skip it
90                         continue
91                 fi
92                 if [ $ref_cnt -gt 0 ]; then
93                         # module in use.  maybe it just needs a few seconds
94                         # after removal of previous modules.
95                         sleep 5
96                         ref_cnt=`/sbin/lsmod | awk "$awkprog" module_name=$mod`
97                 fi
98                 if [ $ref_cnt -eq 0 ]; then
99                         # unload the module
100                         echo "Removing module $mod"
101                         /sbin/rmmod $mod
102                         if [ $? -ne 0 ]; then
103                                 echo "ERROR: Failed to remove module $mod."
104                                 return 1
105                         fi
106                 else
107                         # boo!  module still in use.
108                         echo "ERROR: Module $mod has non-zero reference count."
109                         return 1
110                 fi
111         done
112
113         return 0
114 }
115
116 stop_lnet ()
117 {
118         local errmsg=`/usr/sbin/lctl network unconfigure 2>&1`
119         if [ $? -gt 0 ]; then
120                 # The following error message means that lnet is already
121                 # unconfigured, and the modules are not loaded.
122                 echo $errmsg | grep "LNET unconfigure error 19" > /dev/null
123                 if [ $? -gt 0 ]; then
124                         return 0
125                 else
126                         echo "$errmsg"
127                         return 1
128                 fi
129         fi
130         return 0
131 }
132
133 status ()
134 {
135         old_nullglob="`shopt -p nullglob`"
136         shopt -u nullglob
137
138         STATE="stopped"
139         # LSB compliance - return 3 if service is not running
140         # Lustre-specific returns
141         # 150 - partial startup
142         # 151 - health_check unhealthy
143         # 152 - LBUG
144         RETVAL=3
145         egrep -q "lnet" /proc/modules && STATE="loaded"
146
147         # check for any routes - on a portals router this is the only thing
148         [ "`cat /proc/sys/lnet/routes 2> /dev/null`" ] && STATE="running" && RETVAL=0
149
150         # check if this is a router
151         if [ -d /proc/sys/lnet ]; then
152                 ROUTER="`cat /proc/sys/lnet/routes | head -1 | grep -i -c \"Routing enabled\"`"
153                 if [[ ! -z ${ROUTER} && ${ROUTER} -ge 1 ]]; then
154                         STATE="running"
155                         RETVAL=0
156                 fi
157         fi
158
159         # check for error in health_check
160         HEALTH="/proc/fs/lustre/health_check"
161         [ -f "$HEALTH" ] && grep -q "NOT HEALTHY" $HEALTH && STATE="unhealthy" && RETVAL=1
162
163         # check for LBUG
164         [ -f  "$HEALTH" ] && grep -q "LBUG" $HEALTH && STATE="LBUG" && RETVAL=152
165
166         echo $STATE
167         eval $old_nullglob
168 }
169
170 LUSTRE_ROUTES_CONFIG_FILE="/etc/sysconfig/lnet_routes.conf"
171
172 # See how we were called.
173 case "$1" in
174   start)
175         run_preexec_check "start"
176         touch /var/lock/subsys/lnet
177         modprobe lnet || exit 1
178         lctl network up || exit 1
179         # if a routes config file is given then use it to configure the
180         # routes if not then default to LUSTRE_ROUTES_CONFIG_FILE
181         if [ -f "$2" ]; then
182                 lustre_routes_config $2
183         elif [ -f "$LUSTRE_ROUTES_CONFIG_FILE" ]; then
184                 lustre_routes_config $LUSTRE_ROUTES_CONFIG_FILE
185         fi
186         run_postexec_check "start"
187         ;;
188   stop)
189         run_preexec_check "stop"
190         remove_modules ${TOP_MODULES[*]} || exit 1
191         stop_lnet || exit 1
192         remove_modules ${BOTTOM_MODULES[*]} || exit 1
193         rm -f /var/lock/subsys/lnet
194         run_postexec_check "stop"
195         ;;
196   status)
197         status
198         ;;
199   restart)
200         $0 stop
201         $0 start
202         ;;
203   reload)
204         touch /var/lock/subsys/lnet
205         ;;
206   probe)
207         if [ ! -f /var/lock/subsys/lnet ] ; then
208           echo $"start"; exit 0
209         fi
210         ;;
211   condrestart)
212         [ -f /var/lock/subsys/lnet ] && {
213                 $0 stop
214                 $0 start
215         }
216         ;;
217   *)
218         echo $"Usage: lustre {start|stop|status|restart|reload|condrestart}"
219         exit 1
220 esac
221
222 exit 0