Whamcloud - gitweb
b:11171
[fs/lustre-release.git] / lustre-iokit / obdfilter-survey / libecho
1 #!/bin/bash
2 #*  Copyright (C) 2002 Cluster File Systems, Inc.
3 #*   Author: Jitendra Pawar <jitendra@clusterfs.com>
4 #*
5 #*   Lustre-iokit is free software; you can redistribute it and/or
6 #*   modify it under the terms of version 2 of the GNU General Public
7 #*   License as published by the Free Software Foundation.
8 #*
9 #*   Lustre-iokit is distributed in the hope that it will be useful,
10 #*   but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #*   GNU General Public License for more details.
13 #*
14 #*   You should have received a copy of the GNU General Public License
15 #*   along with Lustre; if not, write to the Free Software
16 #*   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 # binaries
19 lsmod="/sbin/lsmod"
20 modprobe="/sbin/modprobe"
21 insmod="/sbin/insmod"
22 rmmod="/sbin/rmmod"
23
24 declare -a ost_names
25 declare -a host_list
26 declare -a dev_list
27 declare -a unique_hosts
28 declare count
29 declare -a vmstatpids
30 declare -a do_unload_echo
31
32
33 DSH=${DSH:-"ssh"}
34
35 dsh () {
36     local node="$1"
37     local user="$2"
38     shift 2
39     local command="$@"
40
41     local here=$(pwd)
42
43     command="cd $here; export PATH=/sbin:/usr/sbin:\$PATH; $command"
44
45     case $DSH in
46         ssh)
47             if [ -n "$user" ]; then
48                 user="$user@"
49             fi
50             $DSH $user$node "$command"
51             ;;
52         rsh)
53             if [ -n "$user" ]; then
54                 user="-l $user"
55             fi
56             $DSH $user $node "$command"
57             ;;
58     esac
59 }
60
61 # how to run commands on other nodes
62 # You need to make this work on your cluster if you have specified
63 # non-local obd instances above
64 remote_shell () {
65     host=$1
66     shift
67     cmds="$@"
68     if [ "$host" = "localhost" -o "$host" = `uname -n` ]; then
69         eval "$cmds"
70     else
71         # split $host into $host and $user
72         local user=""
73         if [[ $host == *@* ]]; then
74             user=${host%@*}
75             host=${host#*@}
76         fi
77         dsh $host "$user" "$cmds"
78     fi
79 }
80
81 # checks whether obdecho module is loded on given host.
82 # parameter: 1. hostname
83 obdecho_loaded() {
84     local host=$1
85     remote_shell $host $lsmod | grep obdecho > /dev/null 2>&1
86 }
87
88 # load obdecho.ko or obdecho.o module on host kernel.
89 load_obdecho () {
90     local index=$1
91     local host=${unique_hosts[$index]}
92     do_unload_echo[$index]=0
93     if obdecho_loaded $host; then
94         return 0
95     fi
96     if [ -z "$lustre_root" ]; then
97         remote_shell $host $modprobe obdecho
98     elif [ -f ${lustre_root}/obdecho/obdecho.ko ]; then
99         remote_shell $host $insmod ${lustre_root}/obdecho/obdecho.ko
100     else
101         remote_shell $host $insmod ${lustre_root}/obdecho/obdecho.o
102     fi
103     if obdecho_loaded $host; then
104         do_unload_echo[$index]=1
105     else
106         echo Could not install obdecho on $host
107         return 1
108     fi
109     return 0
110 }
111
112 load_obdechos () {
113     for ((i = 0; i < ${#unique_hosts[@]}; i++)); do
114                 load_obdecho $i || cleanup 1
115     done
116 }
117
118 # unload obdecho module from host kernel.
119 unload_obdecho () {
120     local index=$1
121     local host=${unique_hosts[$index]}
122     if ((${do_unload_echo[$index]})); then
123         remote_shell $host $rmmod obdecho
124         do_unload_echo[$index]=0
125     fi
126 }
127
128 # returns the device number which is displayed in "lctl device_list"
129
130 # parameter: 1. hostname
131 #            2. type of device ex: echo_client
132 #            3. name of device ex: ECHO_matrix.linsyssoft.com
133 get_devno () {
134     local host=$1
135     local type=$2
136     local name=$3
137     remote_shell $host $lctl device_list | \
138         awk "{if (\$2 == \"UP\" && \$3 == \"$type\" && \$4 == \"$name\") {\
139                   print \$1; exit}}"
140 }
141
142 get_devnos () {
143     local i=0
144     local host
145     for ((i = 0; i < $count; i++)); do
146         ost=${ost_names[$i]}
147         host=${host_list[$i]}
148         dev=$(get_devno $host obdfilter $ost)
149         dev_list[$i]=$dev
150         if [ -z "$dev" ]; then
151             echo Cant find device for $ost on $host
152             return 1
153         fi
154     done
155     return 0
156 }
157
158 # do cleanup and exit.
159 cleanup () {
160     local clean_srv_OSS=$2
161     for ((i = 0; i < ndevs; i++)); do
162         host=${host_names[$i]}
163             if [ -n ${do_teardown_ec[$i]} ]; then
164                 teardown_ec_devno $host ${client_names[$i]}
165             fi
166     done
167     pidcount=0
168     for host in ${unique_hosts[@]}; do
169         remote_shell $host "killall -q vmstat >/dev/null 2>&1" &
170         pid=$!
171         kill -term ${vmstatpids[$pidcount]} 2>/dev/null
172         kill -kill ${vmstatpids[$pidcount]} 2>/dev/null
173         wait $pid
174         pidcount=$((pidcount+1))
175         if ((${do_unload_obdecho[$host]})); then
176             unload_obdecho $host
177         fi
178     done
179     if [ $case == "network" ]; then
180         lctl <<EOF
181         cfg_device osc_testfs
182         cleanup
183         detach
184 EOF
185         remote_shell "root@$server_nid" "lctl << EOF
186             cfg_device echo_srv
187             cleanup
188             detach
189 EOF"
190     fi
191     if [ $clean_srv_OSS ]; then
192         remote_shell "root@$server_nid" "lctl << EOF
193             cfg_device OSS
194             cleanup
195             detach
196 EOF"
197     fi
198     if [ $1 ]; then
199         if [ $1 -ne 0 ]; then
200             echo "program exited with error "
201         else
202             echo "done!"
203         fi
204     else
205         echo "Terminated"
206     fi
207     exit $1
208 }
209 trap cleanup SIGHUP SIGINT SIGTERM
210
211 # gets echoclient device number and attch it to the client UUID
212
213 # parameter: 1. hostname
214 #            2. client name, ex:- ns8:ECHO_ns8
215 #            3. name of ost instances, ex:- lustre-OST0001 
216 get_ec_devno () {
217     local host=$1
218     local client_name="$2"
219     local ost_name="$3"
220     if [ -z "$client_name" ]; then
221         if [ -z "$ost_name" ]; then
222             echo "client and ost name both null" 1>&2
223             return
224         fi
225         client_name=${ost_name}_echo_client
226     fi
227     ec=`get_devno $host echo_client $client_name`
228     if [ -n "$ec" ]; then
229         echo $ec $client_name $client_name
230         return
231     fi
232     if [ -z "$ost_name" ]; then
233         echo "no echo client and ost_name not set, client: $client_name, host: $host" 1>&2
234         return
235     fi
236     ost=`get_devno $host obdfilter $ost_name`
237     if [ -z "$ost" ]; then
238         echo "OST $ost_name not setup" 1>&2
239         return
240     fi
241     remote_shell $host "$lctl <<EOF
242         attach echo_client $client_name ${client_name}_UUID
243         setup $ost_name
244 EOF"
245     ec=`get_devno $host echo_client $client_name`
246     if [ -z "$ec" ]; then
247         echo "Can't setup echo client" 1>&2
248         return
249     fi
250     echo $ec $client_name 1
251 }
252
253 # Create echo-clients using osc_names and osc_uuid
254 # It creates echoclients for all osc listed using #lctl device_list command
255 ec_using_osc () {
256     local osc_name=$1
257     local osc_uuid=$2
258     $lctl <<EOF
259         attach echo_client ECHO_$osc_name $osc_uuid
260         cfg_device ECHO_$osc_name
261         setup $osc_name
262 EOF
263
264 }
265
266 # create echo client using server nid.
267 ec_using_srv_nid () {
268     local server_nid=$1
269     local ocsname=$2
270     local oscuuid=$3
271     $lctl add_uuid testfs_UUID $server_nid@tcp >/dev/null 2>&1
272     $lctl <<EOF
273         attach osc $ocsname $oscuuid
274         cfg_device $ocsname
275         setup echo_srv_UUID testfs_UUID
276 EOF
277     $lctl <<EOF
278         attach echo_client ECHO_$ocsname $oscuuid
279         setup $ocsname 
280 EOF
281 }
282
283 # setup obdecho on server
284 setup_srv_obd () {
285     local server_nid=$1
286     local test_ostfsnm=$2
287     remote_shell "root@$server_nid" "$lctl << EOF
288         attach obdecho $test_ostfsnm ${test_ostfsnm}_UUID
289         cfg_device $test_ostfsnm
290         setup
291 EOF"
292 }
293
294 # setup OSS on server
295 setup_OSS () {
296     local server_nid=$1
297     remote_shell "root@$server_nid" "$lctl << EOF
298         attach ost OSS OSS_UUID
299         cfg_device OSS
300         setup
301 EOF"
302 }
303
304 # cleanup and detach the echo-clients that we have created during the test.
305 # parameter: 1. hostname
306 #            2. client name, ex:- ns8:ECHO_ns8
307 teardown_ec_devno () {
308     local host=$1
309     local client_name=$2
310     remote_shell $host "$lctl <<EOF
311         cfg $client_name
312         cleanup
313         detach
314 EOF"
315 }
316
317 unique () {
318     echo "$@" | xargs -n1 echo | sort -u
319 }
320
321 split_hostname () {
322     local name=$1
323     case $name in
324     *:*) host=`echo $name | sed 's/:.*$//'`
325          name=`echo $name | sed 's/[^:]*://'`
326          ;;
327     *)   host=localhost
328          ;;
329     esac
330     echo "$host $name"
331 }
332
333 check_cleanup () {
334     type_obj="$1"
335     osc_names_str=$(lctl dl | grep $type_obj)
336     count=0;
337     for name in $osc_names_str; do
338         count=$((count+1))
339     done
340     
341     if [ $count != 0 ]; then
342         echo "$type_obj could not be cleanup";
343         exit 0;
344     fi 
345
346 }
347
348 check_setup () {
349     type_obj="$1"
350     osc_names_str=$(lctl dl | grep $type_obj)
351     count=0;
352     for name in $osc_names_str; do
353         count=$((count+1))
354     done
355
356     if [ $count == 0 ]; then
357         echo "$type_obj could not be setup";
358         exit 0;
359     fi
360
361 }
362
363 # added from bugzill req.
364 get_targets () {
365     if [ -z "$ost_names" ]; then
366         OSTS=$($lctl device_list | awk "{if (\$2 == \"UP\" && \
367             \$3 == \"obdfilter\") {print \$4} }")
368     fi
369     if [ -z "$OSTS" ]; then
370         echo "Can't find any OSTs to test.  Please set ost_names=..."
371         exit 1
372     fi
373     count=0
374     for name in $OSTS; do
375         ost_names[$count]=$name
376         count=$((count+1))
377     done
378 }
379
380 get_hosts () {
381     # split out hostnames from ost names
382     for ((i = 0; i < count; i++)); do
383         str=(`split_hostname ${OSTS[$i]}`)
384         host_list[$i]=${str[0]}
385         ost_names[$i]=${str[1]}
386     done
387 }