Whamcloud - gitweb
LU-1303 lod: transfer default striping from parent/fs
[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/lustre
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         fsfilt_ldiskfs          \
41         obdclass                \
42         lnet                    \
43         lvfs                    \
44         libcfs                  \
45         ldiskfs                 \
46 )
47
48 declare -r awkprog='BEGIN { rc = -1 }
49                        { if ( $1 == module_name ) { rc = $3; exit; } }
50                     END { print rc }'
51
52 # Usage: run_preexec_check [ start | restart | condrestart ]
53 # The single parameter will be passed to the PREEXEC_SCRIPT
54 run_preexec_check ()
55 {
56         if [ -n "$PREEXEC_CHECK" ] && ! $PREEXEC_CHECK ; then
57                 echo "Pre-exec check \"$PREEXEC_CHECK\" failed.  Aborting."
58                 exit 1
59         fi
60
61         if [ -n "$PREEXEC_SCRIPT" ] && ! "$PREEXEC_SCRIPT" "$1" ; then
62                 echo "Pre-exec script \"$PREEXEC_SCRIPT\" failed.  Aborting."
63                 exit 1
64         fi
65 }
66
67 # Usage: run_postexec_check [ start | restart | condrestart ]
68 # The single parameter will be passed to the POSTEXEC_SCRIPT
69 run_postexec_check ()
70 {
71         if [ -n "$POSTEXEC_CHECK" ] && ! $POSTEXEC_CHECK ; then
72                 echo "Post-exec check \"$POSTEXEC_CHECK\" failed.  Aborting."
73                 exit 1
74         fi
75
76         if [ -n "$POSTEXEC_SCRIPT" ] && ! "$POSTEXEC_SCRIPT" "$1" ; then
77                 echo "Post-exec script \"$POSTEXEC_SCRIPT\" failed.  Aborting."
78                 exit 1
79         fi
80 }
81
82 remove_modules ()
83 {
84         local modules="${@}"
85         local ref_cnt
86
87         for mod in $modules; do
88                 ref_cnt=`/sbin/lsmod | awk "$awkprog" "module_name=$mod"`
89                 if [ $ref_cnt -lt 0 ]; then
90                         # module not loaded, skip it
91                         continue
92                 fi
93                 if [ $ref_cnt -gt 0 ]; then
94                         # module in use.  maybe it just needs a few seconds
95                         # after removal of previous modules.
96                         sleep 5
97                         ref_cnt=`/sbin/lsmod | awk "$awkprog" module_name=$mod`
98                 fi
99                 if [ $ref_cnt -eq 0 ]; then
100                         # unload the module
101                         echo "Removing module $mod"
102                         /sbin/rmmod $mod
103                         if [ $? -ne 0 ]; then
104                                 echo "ERROR: Failed to remove module $mod."
105                                 return 1
106                         fi
107                 else
108                         # boo!  module still in use.
109                         echo "ERROR: Module $mod has non-zero reference count."
110                         return 1
111                 fi
112         done
113
114         return 0
115 }
116
117 stop_lnet ()
118 {
119         local errmsg=`/usr/sbin/lctl network unconfigure 2>&1`
120         if [ $? -gt 0 ]; then
121                 # The following error message means that lnet is already
122                 # unconfigured, and the modules are not loaded.
123                 echo $errmsg | grep "LNET unconfigure error 19" > /dev/null
124                 if [ $? -gt 0 ]; then
125                         return 0
126                 else
127                         echo "$errmsg"
128                         return 1
129                 fi
130         fi
131         return 0
132 }
133
134 status ()
135 {
136         old_nullglob="`shopt -p nullglob`"
137         shopt -u nullglob
138
139         STATE="stopped"
140         # LSB compliance - return 3 if service is not running
141         # Lustre-specific returns
142         # 150 - partial startup
143         # 151 - health_check unhealthy
144         # 152 - LBUG
145         RETVAL=3
146         egrep -q "lnet" /proc/modules && STATE="loaded"
147
148         # check for any routes - on a portals router this is the only thing
149         [ "`cat /proc/sys/lnet/routes 2> /dev/null`" ] && STATE="running" && RETVAL=0
150
151         # check if this is a router
152         if [ -d /proc/sys/lnet ]; then
153                 ROUTER="`cat /proc/sys/lnet/routes | head -1 | grep -i -c \"Routing enabled\"`"
154                 if [[ ! -z ${ROUTER} && ${ROUTER} -ge 1 ]]; then
155                         STATE="running"
156                         RETVAL=0
157                 fi
158         fi
159
160         # check for error in health_check
161         HEALTH="/proc/fs/lustre/health_check"
162         [ -f "$HEALTH" ] && grep -q "NOT HEALTHY" $HEALTH && STATE="unhealthy" && RETVAL=1
163
164         # check for LBUG
165         [ -f  "$HEALTH" ] && grep -q "LBUG" $HEALTH && STATE="LBUG" && RETVAL=152
166
167         echo $STATE
168         eval $old_nullglob
169 }
170
171 # See how we were called.
172 case "$1" in
173   start)
174         run_preexec_check "start"
175         touch /var/lock/subsys/lnet
176         modprobe lnet || exit 1
177         lctl network up || exit 1
178         run_postexec_check "start"
179         ;;
180   stop)
181         run_preexec_check "stop"
182         remove_modules ${TOP_MODULES[*]} || exit 1
183         stop_lnet || exit 1
184         remove_modules ${BOTTOM_MODULES[*]} || exit 1
185         rm -f /var/lock/subsys/lnet
186         run_postexec_check "stop"
187         ;;
188   status)
189         status
190         ;;
191   restart)
192         $0 stop
193         $0 start
194         ;;
195   reload)
196         touch /var/lock/subsys/lnet
197         ;;
198   probe)
199         if [ ! -f /var/lock/subsys/lnet ] ; then
200           echo $"start"; exit 0
201         fi
202         ;;
203   condrestart)
204         [ -f /var/lock/subsys/lnet ] && {
205                 $0 stop
206                 $0 start
207         }
208         ;;
209   *)
210         echo $"Usage: lustre {start|stop|status|restart|reload|condrestart}"
211         exit 1
212 esac
213
214 exit 0