Whamcloud - gitweb
e66ad373602bb8028d0e74c0159ee793fdb4ef21
[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 awkprog='BEGIN { rc = -1 }
22                        { if ( $1 == module_name ) { rc = $3; exit; } }
23                     END { print rc }'
24
25 # Usage: run_preexec_check [ start | restart | condrestart ]
26 # The single parameter will be passed to the PREEXEC_SCRIPT
27 run_preexec_check ()
28 {
29         if [ -n "$PREEXEC_CHECK" ] && ! $PREEXEC_CHECK ; then
30                 echo "Pre-exec check \"$PREEXEC_CHECK\" failed.  Aborting."
31                 exit 1
32         fi
33
34         if [ -n "$PREEXEC_SCRIPT" ] && ! "$PREEXEC_SCRIPT" "$1" ; then
35                 echo "Pre-exec script \"$PREEXEC_SCRIPT\" failed.  Aborting."
36                 exit 1
37         fi
38 }
39
40 # Usage: run_postexec_check [ start | restart | condrestart ]
41 # The single parameter will be passed to the POSTEXEC_SCRIPT
42 run_postexec_check ()
43 {
44         if [ -n "$POSTEXEC_CHECK" ] && ! $POSTEXEC_CHECK ; then
45                 echo "Post-exec check \"$POSTEXEC_CHECK\" failed.  Aborting."
46                 exit 1
47         fi
48
49         if [ -n "$POSTEXEC_SCRIPT" ] && ! "$POSTEXEC_SCRIPT" "$1" ; then
50                 echo "Post-exec script \"$POSTEXEC_SCRIPT\" failed.  Aborting."
51                 exit 1
52         fi
53 }
54
55 stop_lnet ()
56 {
57         local errmsg=$(/usr/sbin/lctl network down 2>&1)
58         if [ $? -gt 0 ]; then
59                 # The following error message means that lnet is already
60                 # unconfigured, and the modules are not loaded.
61                 echo $errmsg | grep "LNET unconfigure error 19" > /dev/null
62                 if [ $? -gt 0 ]; then
63                         return 0
64                 else
65                         echo "$errmsg"
66                         return 1
67                 fi
68         fi
69         return 0
70 }
71
72 status ()
73 {
74         old_nullglob="`shopt -p nullglob`"
75         shopt -u nullglob
76
77         STATE="stopped"
78         # LSB compliance - return 3 if service is not running
79         # Lustre-specific returns
80         # 150 - partial startup
81         # 151 - health_check unhealthy
82         # 152 - LBUG
83         RETVAL=3
84         egrep -q "lnet" /proc/modules && STATE="loaded"
85
86         # check for any routes - on a portals router this is the only thing
87         VAR=$(lctl get_param -n routes 2>&1)
88         if [ $? = 0 ] ; then
89                 STATE="running"
90                 RETVAL=0
91         fi
92
93         # check if this is a router
94         if [[ "$(lctl get_param -n routes)" =~ "Routing enabled" ]]; then
95                 STATE="running"
96                 RETVAL=0
97         fi
98
99         # check for error in health_check
100         local health_check=$(lctl get_param -n health_check)
101         if [[ "$health_check" =~ "NOT HEALTHY" ]]; then
102                 STATE="unhealthy"
103                 RETVAL=1
104         fi
105
106         if [[ "$health_check" =~ "LBUG" ]]; then
107                 STATE="LBUG"
108                 RETVAL=152
109         fi
110
111         echo $STATE
112         eval $old_nullglob
113 }
114
115 LUSTRE_ROUTES_CONFIG_FILE="/etc/sysconfig/lnet_routes.conf"
116 LUSTRE_LNET_CONFIG_FILE="/etc/sysconfig/lnet.conf"
117 LUSTRE_LNET_CONFIG_UTILITY="/usr/sbin/lnetctl"
118
119 # See how we were called.
120 case "$1" in
121   start)
122         run_preexec_check "start"
123         touch /var/lock/subsys/lnet
124         modprobe lnet || exit 1
125         # if lnet.conf file exists then use lnetctl lnet configure, since
126         # that doesn't load the networks and routes defined in the mod
127         # params.  The appropriate configuration will be picked up from
128         # the lnet.conf YAML file.
129         if [ -x $LUSTRE_LNET_CONFIG_UTILITY -a -f "$LUSTRE_LNET_CONFIG_FILE" ]; then
130                 $LUSTRE_LNET_CONFIG_UTILITY lnet configure || exit 1
131         else
132                 lctl network up || exit 1
133         fi
134         # if an lnet.conf file exists then pass that to the lnetctl
135         # utility for parsing.  This will configure the items defined
136         # in YAML format in the config file.
137         if [ -x $LUSTRE_LNET_CONFIG_UTILITY -a -f "$LUSTRE_LNET_CONFIG_FILE" ]; then
138                 $LUSTRE_LNET_CONFIG_UTILITY import < $LUSTRE_LNET_CONFIG_FILE
139         fi
140         # if a routes config file is given then use it to configure the
141         # routes if not then default to LUSTRE_ROUTES_CONFIG_FILE
142         if [ -f "$2" ]; then
143                 lustre_routes_config $2
144         elif [ -f "$LUSTRE_ROUTES_CONFIG_FILE" ]; then
145                 lustre_routes_config $LUSTRE_ROUTES_CONFIG_FILE
146         fi
147         run_postexec_check "start"
148         ;;
149   stop)
150         run_preexec_check "stop"
151         stop_lnet || exit 1
152         lustre_rmmod || exit 1
153         rm -f /var/lock/subsys/lnet
154         run_postexec_check "stop"
155         ;;
156   status)
157         status
158         ;;
159   restart)
160         $0 stop
161         $0 start
162         ;;
163   reload)
164         touch /var/lock/subsys/lnet
165         ;;
166   probe)
167         if [ ! -f /var/lock/subsys/lnet ] ; then
168           echo $"start"; exit 0
169         fi
170         ;;
171   condrestart)
172         [ -f /var/lock/subsys/lnet ] && {
173                 $0 stop
174                 $0 start
175         }
176         ;;
177   *)
178         echo $"Usage: lnet {start|stop|status|restart|reload|condrestart}"
179         exit 1
180 esac
181
182 exit 0