Whamcloud - gitweb
LU-930 utils: fix 'lfs find' error message
[fs/lustre-release.git] / lustre / scripts / lustre_routes_config
1 #!/bin/bash
2 #
3 # lustre_route_config
4 # This script configures lnet with the routes in the passed in file.
5 # The routes should be in the following format:
6 # <network>: { gateway: <gateway>@<exit network> [hop: <hop>] [priority: <prioirty>] }
7 #
8 # Examples:
9 # tcp1: { gateway: 10.1.1.2@tcp0, priority: 3 }
10 # tcp4: { gateway: 10.3.3.4@tcp }
11 # tcp6: { gateway: 10.3.3.6@tcp, hop: 2, priority: 5 }
12 # tcp7: { gateway: 10.3.3.[6-12]@tcp, priority: 20, hop: 8 }
13 #
14 # The purpose of this script is to circumvent the limitation on the number of
15 # routes which could be configured through the lustre.conf module parameters.
16 #
17 ###########################################################################
18
19
20 progname=$(basename $0)
21
22 # print usage
23 usage() {
24         cat <<- USAGE
25         Setup or cleanup LNET routes from specified config file"
26         usage: $progname [--setup|--cleanup|--dry-run|--verbose]
27         <config_file>
28                 --setup: configure routes listed in config_file
29                 --cleanup: unconfigure routes listed in config_file
30                 --dry-run: echo commands to be run, but do not execute them
31                 --verbose: echo commands before they are executed
32         USAGE
33 }
34
35 # Set default paramters
36 CMD=add_route
37 VERBOSE=false
38 EXEC=true
39
40 # sanity check
41 [ -z "$1" ] && usage && exit 1
42
43 # check parameters
44 while [ ! -f "$1" ]; do
45         case "$1" in
46         -c|--cleanup) CMD=del_route; shift ;;
47         -h|--help)    usage; exit 0 ;;
48         -n|--dry-run) EXEC=false; VERBOSE=true; shift ;;
49         -s|--setup)   CMD=add_route; shift ;;
50         -v|--verbose) VERBOSE=true; shift ;;
51         *)            usage; exit 1 ;;
52         esac
53 done
54
55 # Usage: do_lctl <params>
56 # execut the command and/or print if verbose is set
57 do_lctl() {
58         local RC=0
59
60         $VERBOSE && echo "lctl $@"
61         if $EXEC; then
62                 lctl "$@"
63                 RC=$?
64         fi
65
66         return $RC
67 }
68
69 # Usage: find_arg_value <array> <arg>
70 find_arg_value() {
71         local i=0
72         local value=""
73         local arg="$2"
74
75         declare -a array=("${!1}")
76         for ((i = 0; i < ${#array[@]}; i++)); do
77                 if [ "${array[$i]}" == "$arg" ]; then
78                         value="${array[$((i + 1))]}"
79                         break
80                 fi
81         done
82         echo -n $value
83 }
84
85 while read line; do
86         # skip lines that are entirely whitespace or
87         # when the first non-whitespace character is #
88         if [[ "$line" =~ ^[[:space:]]*(#|$) ]]; then
89                 continue
90         fi
91
92         # Parse line using ':' and ',' as delimiters and ignoring all
93         # white space, tabs and linefeed
94         IFS="$IFS:,"
95         params=($line)
96
97         # get the mandatory parameters: network and gateway
98         # If either is not present skip that line
99         network=${params[0]}
100         OBR=${params[1]}
101         GATE=${params[2]}
102         gateway=${params[3]}
103
104         if [ -z $network ] || [ -z $gateway ] ||
105            [ $GATE != "gateway" ]; then
106                 continue
107         fi
108
109         case "$CMD" in
110                 add_route)
111                         baselctl="--net $network add_route $gateway"
112
113                         # walk through the optional params until you hit
114                         # the closing brace.  Build an associative db:
115                         # option=value
116                         i=4
117                         while [ $i -lt ${#params[@]} ]; do
118                                 option=${params[$i]}
119                                 if [ "$option" == "}" ]; then
120                                         break
121                                 fi
122                                 outoptions[$i]=$option
123                                 ((i++))
124                                 value=${params[$i]}
125                                 outoptions[$i]=$value
126                                 ((i++))
127                         done
128
129                         # find the hop and priority
130                         # This can be expanded later on if we add extra
131                         # parameters
132                         # NOTE: the order between hop and priority is not
133                         # enforced.  It's also possible to add hop without
134                         # prio or prio without hop
135                         priority=$(find_arg_value outoptions[@] "priority")
136                         hop=$(find_arg_value outoptions[@] "hop")
137                         if [ -n "$priority" ] && [ -z "$hop" ]; then
138                                 baselctl+=" 1 $priority"
139                         else
140                                 baselctl+=" $hop $priority"
141                         fi
142                         ;;
143                 del_route)
144                         baselctl="del_route $gateway"
145         esac
146
147         do_lctl $baselctl
148 done < "$1"