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