Whamcloud - gitweb
3beeecf3361da177eae7ac4bdedb068484182107
[fs/lustre-release.git] / lustre-iokit / obdfilter-survey / libobd
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 # This function executes the command sent through parameters to host
34 # parameters
35 # 1. hostname
36 # 2. command to be executed on host
37 custom_remote_shell () {
38     host=$1
39     shift
40     cmds="$*"
41     here=`pwd`
42     # Hop on to the remote node, chdir to 'here' and run the given
43     # commands. One of the following will probably work.
44     ssh $host "cd $here; $cmds"
45     #rsh $host "cd $here; $cmds"
46     # we have to remove the leading `uname -n`: from pdsh output lines
47     #pdsh -w $host "cd $here; $cmds" | sed 's/^[^:]*://'
48 }
49
50 # how to run commands on other nodes
51 # You need to make this work on your cluster if you have specified
52 # non-local obd instances above
53 remote_shell () {
54     host=$1
55     shift
56     cmds="$*"
57     if [ "$host" = "localhost" -o "$host" = `uname -n` ]; then
58         eval "$cmds"
59     else
60         custom_remote_shell $host "$cmds"
61     fi
62 }
63
64 # checks whether obdecho module is loded on given host.
65 # parameter: 1. hostname
66 obdecho_loaded() {
67     local host=$1
68     remote_shell $host $lsmod | grep obdecho > /dev/null 2>&1
69 }
70
71 # load obdecho.ko or obdecho.o module on host kernel.
72 load_obdecho () {
73     local index=$1
74     local host=${unique_hosts[$index]}
75     do_unload_echo[$index]=0
76     if obdecho_loaded $host; then
77         return 0
78     fi
79     if [ -z "$lustre_root" ]; then
80         remote_shell $host $modprobe obdecho
81     elif [ -f ${lustre_root}/obdecho/obdecho.ko ]; then
82         remote_shell $host $insmod ${lustre_root}/obdecho/obdecho.ko
83     else
84         remote_shell $host $insmod ${lustre_root}/obdecho/obdecho.o
85     fi
86     if obdecho_loaded $host; then
87         do_unload_echo[$index]=1
88     else
89         echo Could not install obdecho on $host
90         return 1
91     fi
92     return 0
93 }
94
95 load_obdechos () {
96     for ((i = 0; i < ${#unique_hosts[@]}; i++)); do
97                 load_obdecho $i || cleanup 1
98     done
99 }
100
101 # unload obdecho module from host kernel.
102 unload_obdecho () {
103     local index=$1
104     local host=${unique_hosts[$index]}
105     if ((${do_unload_echo[$index]})); then
106         remote_shell $host $rmmod obdecho
107         do_unload_echo[$index]=0
108     fi
109 }
110
111 # returns the device number which is displayed in "lctl device_list"
112
113 # parameter: 1. hostname
114 #            2. type of device ex: echo_client
115 #            3. name of device ex: ECHO_matrix.linsyssoft.com
116 get_devno () {
117     local host=$1
118     local type=$2
119     local name=$3
120     remote_shell $host $lctl device_list | \
121         awk "{if (\$2 == \"UP\" && \$3 == \"$type\" && \$4 == \"$name\") {\
122                   print \$1; exit}}"
123 }
124
125 get_devnos () {
126     local i=0
127     local host
128     for ((i = 0; i < $count; i++)); do
129         ost=${ost_names[$i]}
130         host=${host_list[$i]}
131         dev=$(get_devno $host obdfilter $ost)
132         dev_list[$i]=$dev
133         if [ -z "$dev" ]; then
134             echo Cant find device for $ost on $host
135             return 1
136         fi
137     done
138     return 0
139 }
140
141 # do cleanup and exit.
142 cleanup () {
143     for ((i = 0; i < ndevs; i++)); do
144         host=${host_names[$i]}
145             if [ -n ${do_teardown_ec[$i]} ]; then
146                 teardown_ec_devno $host ${client_names[$i]}
147             fi
148     done
149     pidcount=0
150     for host in ${unique_hosts[@]}; do
151         remote_shell $host "killall vmstat -q" &
152         pid=$!
153         kill -term ${vmstatpids[$pidcount]} 2>/dev/null
154         kill -kill ${vmstatpids[$pidcount]} 2>/dev/null
155         wait $pid
156         pidcount=$((pidcount+1))
157         if ((${do_unload_obdecho[$host]})); then
158             unload_obdecho $host
159         fi
160     done
161     if [ $case == "network" ]; then
162         lctl <<EOF
163         device osc_testfs
164         cleanup
165         detach
166 EOF
167         remote_shell "root@$server_nid" "lctl << EOF
168             device OSS
169             cleanup
170             detach
171             device ost_testfs
172             cleanup
173             detach
174 EOF"
175     fi
176     if [ $1 -ne 0 ]; then 
177         echo "program exited with error "
178     else
179         echo "done!"
180     fi
181     exit $1
182 }
183 trap cleanup SIGHUP SIGINT SIGTERM
184
185 # gets echoclient device number and attch it to the client UUID
186
187 # parameter: 1. hostname
188 #            2. client name, ex:- ns8:ECHO_ns8
189 #            3. name of ost instances, ex:- lustre-OST0001 
190 get_ec_devno () {
191     local host=$1
192     local client_name="$2"
193     local ost_name="$3"
194     if [ -z "$client_name" ]; then
195         if [ -z "$ost_name" ]; then
196             echo "client and ost name both null" 1>&2
197             return
198         fi
199         client_name=${ost_name}_echo_client
200     fi
201     ec=`get_devno $host echo_client $client_name`
202     if [ -n "$ec" ]; then
203         echo $ec $client_name $client_name
204         return
205     fi
206     if [ -z "$ost_name" ]; then
207         echo "no echo client and ost_name not set, client: $client_name, host: $host" 1>&2
208         return
209     fi
210     ost=`get_devno $host obdfilter $ost_name`
211     if [ -z "$ost" ]; then
212         echo "OST $ost_name not setup" 1>&2
213         return
214     fi
215     remote_shell $host "$lctl <<EOF
216         attach echo_client $client_name ${client_name}_UUID
217         setup $ost_name
218 EOF"
219     ec=`get_devno $host echo_client $client_name`
220     if [ -z "$ec" ]; then
221         echo "Can't setup echo client" 1>&2
222         return
223     fi
224     echo $ec $client_name 1
225 }
226
227 # Create echo-clients using osc_names and osc_uuid
228 # It creates echoclients for all osc listed using #lctl device_list command
229 ec_using_osc () {
230     local osc_name=$1
231     local osc_uuid=$2
232     $lctl <<EOF
233         attach echo_client ECHO_$osc_name $osc_uuid
234         cfg_device ECHO_$osc_name
235         setup $osc_name
236 EOF
237
238 }
239
240 # create echo client using server nid.
241 ec_using_srv_nid () {
242     local server_nid=$1
243     local ocsname=$2
244     local oscuuid=$3
245     $lctl <<EOF
246         add_uuid testfs_UUID $server_nid@tcp
247         attach osc $ocsname $oscuuid
248         cfg_device $ocsname
249         setup ost_testfs_UUID testfs_UUID
250 EOF
251     $lctl <<EOF
252         attach echo_client ECHO_$ocsname $oscuuid
253         setup $ocsname 
254 EOF
255 }
256
257 # setup obdecho and ost on server
258 setup_srv_obd () {
259     local server_nid=$1
260     local test_ostfsnm=$2
261     echo 
262     remote_shell "root@$server_nid" "$lctl << EOF
263         attach obdecho $test_ostfsnm ${test_ostfsnm}_UUID
264         cfg_device $test_ostfsnm
265         setup
266         attach ost OSS OSS_UUID
267         cfg_device OSS
268         setup
269 EOF"  
270 }
271
272 # cleanup and detach the echo-clients that we have created during the test.
273 # parameter: 1. hostname
274 #            2. client name, ex:- ns8:ECHO_ns8
275 teardown_ec_devno () {
276     local host=$1
277     local client_name=$2
278     remote_shell $host "$lctl <<EOF
279         cfg $client_name
280         cleanup
281         detach
282 EOF"
283 }
284
285 unique () {
286     echo "$@" | xargs -n1 echo | sort -u
287 }
288
289 split_hostname () {
290     local name=$1
291     case $name in
292     *:*) host=`echo $name | sed 's/:.*$//'`
293          name=`echo $name | sed 's/[^:]*://'`
294          ;;
295     *)   host=localhost
296          ;;
297     esac
298     echo "$host $name"
299 }
300
301 check_cleanup () {
302     type_obj="$1"
303     osc_names_str=$(lctl dl | grep $type_obj)
304     count=0;
305     for name in $osc_names_str; do
306         count=$((count+1))
307     done
308     
309     if [ $count != 0 ]; then
310         echo "$type_obj could not be cleanup";
311         exit 0;
312     fi 
313
314 }
315
316 check_setup () {
317     type_obj="$1"
318     osc_names_str=$(lctl dl | grep $type_obj)
319     count=0;
320     for name in $osc_names_str; do
321         count=$((count+1))
322     done
323
324     if [ $count == 0 ]; then
325         echo "$type_obj could not be setup";
326         exit 0;
327     fi
328
329 }
330
331 # added from bugzill req.
332 get_targets () {
333     if [ -z "$ost_names" ]; then
334         OSTS=$($lctl device_list | awk "{if (\$2 == \"UP\" && \
335             \$3 == \"obdfilter\") {print \$4} }")
336     fi
337     if [ -z "$OSTS" ]; then
338         echo "Can't find any OSTs to test.  Please set ost_names=..."
339         exit 1
340     fi
341     count=0
342     for name in $OSTS; do
343         ost_names[$count]=$name
344         count=$((count+1))
345     done
346 }
347
348 get_hosts () {
349     # split out hostnames from ost names
350     for ((i = 0; i < count; i++)); do
351         str=(`split_hostname ${OSTS[$i]}`)
352         host_list[$i]=${str[0]}
353         ost_names[$i]=${str[1]}
354     done
355 }