Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre-iokit / obdfilter-survey / iokit-libecho
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3
4 #
5 # Copyright  2008 Sun Microsystems, Inc. All rights reserved
6 # Use is subject to license terms.
7 #
8 # Copyright (c) 2012, 2017, Intel Corporation.
9 #
10
11 #
12 # This file is part of Lustre, http://www.lustre.org/
13 #
14 # Author: Jitendra Pawar <jitendra@clusterfs.com>
15 #
16
17 # binaries
18 lsmod="/sbin/lsmod"
19 modprobe="/sbin/modprobe"
20 insmod="/sbin/insmod"
21 rmmod="/sbin/rmmod"
22
23 declare -a ost_names
24 declare -a client_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 NETTYPE=${NETTYPE:-tcp}
35
36 dsh () {
37     local node="$1"
38     local user="$2"
39     shift 2
40     local command="$@"
41
42     command="export PATH=/sbin:/usr/sbin:\$PATH; $command"
43
44     case $DSH in
45         ssh)
46                 if [ -n "$user" ]; then
47                         user="$user@"
48                 fi
49                 $DSH $user$node "$command"
50                 ;;
51         rsh)
52                 if [ -n "$user" ]; then
53                         user="-l $user"
54                 fi
55                 $DSH $user $node "$command"
56                 ;;
57     esac
58 }
59
60 # how to run commands on other nodes
61 # You need to make this work on your cluster if you have specified
62 # non-local obd instances above
63 remote_shell () {
64     host=$1
65     shift
66     cmds="$@"
67     if [ "$host" = "localhost" -o "$host" = `uname -n` ]; then
68                 eval "$cmds"
69     else
70                 # split $host into $host and $user
71                 local user=""
72                 if [[ $host == *@* ]]; then
73                         user=${host%@*}
74                         host=${host#*@}
75                 fi
76                 dsh $host "$user" "$cmds"
77         fi
78 }
79
80 # checks whether obdecho module is loded on given host.
81 # parameter: 1. hostname
82 obdecho_loaded() {
83     local host=$1
84     remote_shell $host $lsmod | grep obdecho > /dev/null 2>&1
85 }
86
87 # load obdecho.ko or obdecho.o module on host kernel.
88 load_obdecho () {
89     local index=$1
90     local host=${unique_hosts[$index]}
91
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
138     remote_shell $host $LCTL device_list |
139                 awk "{if (\$2 == \"UP\" && \$3 == \"$type\" && \$4 == \"$name\") {\
140                   print \$1; exit}}"
141 }
142
143 get_devnos () {
144     local i=0
145     local host
146     for ((i = 0; i < $count; i++)); do
147         ost=${ost_names[$i]}
148         host=${host_list[$i]}
149         dev=$(get_devno $host obdfilter $ost)
150         dev_list[$i]=$dev
151         if [ -z "$dev" ]; then
152                         echo "Cannot find device for $ost on $host"
153             return 1
154         fi
155     done
156     return 0
157 }
158
159 # do cleanup for network case.
160 cleanup_network () {
161         local clean_srv_OSS=$1
162
163         $LCTL <<-EOF
164                 --device echotmp cleanup
165                 --device echotmp detach
166         EOF
167         remote_shell "root@$server_nid" \
168         "$LCTL <<-EOF
169                 --device echo_srv cleanup
170                 --device echo_srv detach
171         EOF"
172         if [ $clean_srv_OSS ]; then
173                 remote_shell "root@$server_nid" \
174                 "$LCTL <<-EOF
175                         --device OSS cleanup
176                         --device OSS detach
177                 EOF"
178         fi
179 }
180
181 # do cleanup and exit.
182 cleanup () {
183         trap 0
184
185         local exit_status=$1
186         local host
187
188         case=${case:-"disk"}
189         shift
190         for ((i = 0; i < $ndevs; i++)); do
191                 host=${host_names[$i]}
192                 if [[ -n "${do_teardown_ec[$i]}" ]]; then
193                         teardown_ec_devno $host ${client_names[$i]}
194                 fi
195         done
196         pidcount=0
197         for ((i = 0; i < ${#unique_hosts[@]}; i++)); do
198                 host=${unique_hosts[$i]}
199                 remote_shell $host "killall -q vmstat >/dev/null 2>&1" &
200                 pid=$!
201                 kill -term ${vmstatpids[$pidcount]} 2>/dev/null
202                 kill -kill ${vmstatpids[$pidcount]} 2>/dev/null
203                 wait $pid
204                 pidcount=$((pidcount + 1))
205                 if ((${do_unload_echo[$i]})); then
206                         unload_obdecho $i
207                 fi
208         done
209         if [ $case == "network" ]; then
210                 cleanup_network $1
211         fi
212         if [ $exit_status ]; then
213                 if [ $exit_status -ne 0 ]; then
214                 echo "program exited with error "
215                 else
216                 echo "done!"
217                 fi
218         else
219                 echo "Terminated"
220         fi
221         exit $exit_status
222 }
223 trap 'cleanup 0 $clean_srv_OSS' EXIT SIGHUP SIGINT SIGTERM
224
225 # gets echoclient device number and attach it to the client UUID
226 # Results are  returned by an echo followed by an exit
227 # This must run in a subshell.
228 #
229 # parameter: 1. hostname
230 #            2. client name, ex:- ns8:ECHO_ns8
231 #            3. name of ost instances, ex:- lustre-OST0001
232 get_ec_devno () {
233         exec 8>&1 1>&2
234         local host=$1
235         local client_name="$2"
236         local ost_name="$3"
237         local dev_type="${4:-obdfilter}"
238         local stack_type="${5:-}"
239
240         if [ -z "$client_name" ]; then
241                 if [ -z "$ost_name" ]; then
242                         echo "client and ost name both null"
243                         exit 1
244                 fi
245                 client_name=${ost_name}_ecc
246         fi
247         ec=$(get_devno $host echo_client $client_name)
248         if [ -n "$ec" ]; then
249                 echo $ec $client_name $client_name >&8
250                 exit 0
251         fi
252         if [ -z "$ost_name" ]; then
253                 echo "no echo client and ost_name not set, client:" \
254                         "$client_name, host: $host"
255                 exit 1
256         fi
257         ost=$(get_devno $host $dev_type $ost_name)
258         if [ -z "$ost" ]; then
259                 echo "OST $ost_name not setup"
260                 exit 1
261         fi
262         client_name=${ost_name}_ecc
263         # Only 'attach' if '{client_name}_uuid' not already present
264         if ! [[ $($LCTL dl) =~ $client_name ]]; then
265                 remote_shell $host \
266                         "$LCTL <<-EOF
267                                 attach echo_client $client_name \
268                                         ${client_name}_UUID
269                                 setup $ost_name $stack_type
270                         EOF"
271         fi
272         ec=$(get_devno $host echo_client $client_name)
273         if [ -z "$ec" ]; then
274                 echo "Can't setup echo-client"
275                 exit 1
276         fi
277         echo $ec $client_name 1 >&8
278         exit 0
279 }
280
281 # create echo client using server nid.
282 ec_using_srv_nid () {
283         local server_nid=$1
284         local oscname=$2
285         local oscuuid=$3
286
287         $LCTL add_uuid echo_UUID $server_nid@$NETTYPE >/dev/null 2>&1
288         $LCTL <<-EOF
289                 attach osc $oscname $oscuuid
290                 --device $oscname setup echo_srv_UUID echo_UUID
291         EOF
292         $LCTL <<-EOF
293                 attach echo_client ${oscname}_ecc $oscuuid
294                 --device ${oscname}_ecc setup $oscname
295         EOF
296 }
297
298 # setup obdecho on server
299 setup_srv_obd () {
300         local server_nid=$1
301         local test_ostfsnm=$2
302
303         remote_shell "root@$server_nid" \
304         "$LCTL <<-EOF
305                 attach obdecho $test_ostfsnm ${test_ostfsnm}_UUID
306                 --device $test_ostfsnm setup
307         EOF"
308 }
309
310 # setup OSS on server
311 setup_OSS () {
312         local server_nid=$1
313
314         remote_shell "root@$server_nid" \
315         "$LCTL <<-EOF
316                 attach ost OSS OSS_UUID
317                 --device OSS setup
318         EOF"
319 }
320
321 # cleanup and detach the echo-clients that we have created during the test.
322 # parameter: 1. hostname
323 #            2. client name, ex:- ns8:ECHO_ns8
324 teardown_ec_devno () {
325         local host=$1
326         local client_name=$2
327
328         remote_shell $host \
329         "$LCTL <<-EOF
330                 --device $client_name cleanup
331                 --device $client_name detach
332         EOF"
333 }
334
335 unique () {
336         echo "$@" | xargs -n1 echo | sort -u
337 }
338
339 split_hostname () {
340         local name=$1
341
342         case $name in
343         *:*) host=$(echo $name | sed 's/:.*$//')
344                 name=$(echo $name | sed 's/[^:]*://')
345                 ;;
346         *)   host=localhost
347                 ;;
348         esac
349         echo "$host $name"
350 }
351
352 check_cleanup () {
353         local type_obj="$1"
354         local osc_names_str=$($LCTL dl | grep $type_obj)
355         local count=0;
356
357         for name in $osc_names_str; do
358                 count=$((count + 1))
359         done
360
361         if [ $count != 0 ]; then
362                 echo "$type_obj could not be cleanup";
363                 exit 0;
364         fi
365
366 }
367
368 check_setup () {
369         local type_obj="$1"
370         local osc_names_str=$($LCTL dl | grep $type_obj)
371         local count=0;
372
373         for name in $osc_names_str; do
374                 count=$((count + 1))
375                         done
376
377         if [ $count == 0 ]; then
378                 echo "$type_obj could not be setup";
379                 exit 0;
380         fi
381
382 }
383
384 # added from bugzill req.
385 get_targets () {
386         if [ -z "$ost_names" ]; then
387                 targets=$($LCTL device_list | awk "{if (\$2 == \"UP\" && \
388                           \$3 == \"obdfilter\") {print \$4} }")
389         fi
390         if [ -z "$targets" ]; then
391                 echo "Can't find any OSTs to test.  Please set targets=..."
392                 exit 1
393         fi
394
395         local count=0
396         for name in $targets; do
397                 ost_names[$count]=$name
398                 str=($(split_hostname $name))
399                 host_names[$count]=${str[0]}
400                 count=$((count + 1))
401         done
402 }
403
404 get_hosts () {
405         # split out hostnames from ost names
406         for ((i = 0; i < count; i++)); do
407                 local str=($(split_hostname ${targets[$i]}))
408                 host_list[$i]=${str[0]}
409                 ost_names[$i]=${str[1]}
410         done
411 }