Whamcloud - gitweb
- Fixes obdecho
[fs/lustre-release.git] / lustre / tests / common.sh
1 #!/bin/sh
2 export PATH=$PATH:/sbin:/usr/sbin
3
4 [ -d /r ] && R=/r
5
6 PORTALS=$SRCDIR../../portals
7 LUSTRE=$SRCDIR../../lustre
8
9 PTLCTL=$PORTALS/linux/utils/ptlctl
10 DBGCTL=$PORTALS/linux/utils/debugctl
11 ACCEPTOR=$PORTALS/linux/utils/acceptor
12
13 OBDCTL=$LUSTRE/utils/obdctl
14
15 LOOPNUM=0; export LOOPNUM
16 if [ -b /dev/loop0 ]; then
17         LOOP=/dev/loop
18 elif [ -b /dev/loop/0 ]; then
19         LOOP=/dev/loop/
20 else
21         echo "Cannot find /dev/loop0 or /dev/loop/0" 1>&2 && exit -1
22 fi
23
24 do_insmod() {
25         MODULE=$1
26         BASE=`echo $MODULE | sed -e "s^.*/^^" -e "s/\.o$//"`
27
28         [ "$MODULE" ] || fail "usage: $0 <module>"
29         [ -f $MODULE ] || echo "$0: module '$MODULE' not found" 1>&2
30         lsmod | grep -q "\<$BASE\>" && return 0
31         insmod $MODULE
32 }
33
34 # Return the next unused loop device on stdout and in the $LOOPDEV
35 # environment variable.
36 next_loop_dev() {
37         NEXT=
38         while [ -b ${LOOP}${LOOPNUM} ]; do
39                 LOOPDEV=${LOOP}${LOOPNUM}
40                 losetup ${LOOPDEV} > /dev/null 2>&1 || NEXT=${LOOPDEV}
41                 LOOPNUM=`expr ${LOOPNUM} + 1`
42                 [ "$NEXT" ] && echo ${NEXT} && break
43         done
44 }
45
46 # Create a new filesystem.  If we are using a loopback device, we check
47 # for existing "template" filesystems instead of creating a new one,
48 # because it is _much_ faster to gunzip the empty filesystem instead of
49 # creating a new one from scratch.  Conversely, if we are creating a
50 # filesystem on a device we use mkfs, because that only writes sparsely
51 # to the device.  The empty filesystems are also highly compressed (1000:1)
52 # so they don't take too much space.
53 #
54 new_fs_usage() {
55         echo "new_fs <fstype> {device | file} [size]" 1>&2
56         exit -1
57 }
58 new_fs () {
59         EFILE="$1_$3.gz"
60         MKFS="mkfs.$1"
61         MKFSOPT="-b 4096"
62
63         [ "$1" = "ext3" ] && MKFS="mkfs.ext2 -j"
64         if [ "$1" = "extN" ]; then
65                 MKFS="mkfs.ext2 -j"
66                 EFILE="ext3_$3.gz"
67         fi
68
69         if [ -b "$2" ]; then
70                 [ $# -lt 2 -o $# -gt 3 ] && new_fs_usage
71
72                 PM="/proc/mounts"
73                 [ -r "$PM" ] || PM="/etc/mtab"
74
75                 grep "$2 " $PM 1>&2 && echo "$0: $2 is in $PM!" 1>&2 && exit -1
76
77                 $MKFS $MKFSOPT $2 $3 || exit -1
78                 LOOPDEV=$2      # Not really a loop device
79         else
80                 [ $# -ne 3 ] && new_fs_usage
81
82                 if [ -r "$EFILE" ]; then
83                         echo "using prepared filesystem $EFILE for $2"
84                         zcat "$EFILE" > $2 || exit -1
85                         sync
86                 else
87                         echo "creating new sparse filesystem on $2"
88                         dd if=/dev/zero of=$2 bs=1k seek=$3 count=1 1>&2 || exit -1
89                         $MKFS $MKFSOPT -F $2 1>&2 || exit -1
90                 fi
91                 LOOPDEV=`next_loop_dev`
92                 losetup ${LOOPDEV} $2 1>&2 || exit -1
93         fi
94
95         # Enable hash-indexed directories for extN filesystems
96         [ "$1" = "extN" ] && echo "feature FEATURE_C5" | debugfs -w $2
97 }
98
99 # Set up to use an existing filesystem.  We take the same parameters as
100 # new_fs, even though we only use the <fstype> and <file> parameters, to
101 # make it easy to convert between new_fs and old_fs in testing scripts.
102 old_fs () {
103         [ -e $2 ] || exit -1
104
105         if [ -b "$2" ]; then
106                 LOOPDEV=$2      # Not really a loop device
107         else
108                 LOOPDEV=`next_loop_dev`
109                 losetup ${LOOPDEV} $2 1>&2 || exit -1
110         fi
111 }
112
113 list_mods() {
114         $DBGCTL modules > $R/tmp/ogdb
115         echo "The GDB module script is in $R/tmp/ogdb"
116         [ "$DEBUG_WAIT" = "yes" ] && echo -n "Press ENTER to continue" && read
117 }
118
119 # We need at least one setup file to be given.  It can be passed on
120 # the command-line, or it can be found in the home directory, or it
121 # can even be sourced into the current shell environment.
122 setup_opts() {
123         DEF=$HOME/.lustretestrc
124         [ -r $DEF ] && . $DEF && SETUP=y
125
126         for CFG in "$@" ; do
127                 case $CFG  in
128                 *.cfg) [ -r "$CFG" ] && . $CFG && SETUP=y ;;
129                 *) echo "unknown option '$CFG'" 1>&2
130                 esac
131         done
132
133         if [ "$SETUP" != "y" ]; then
134                 echo "error: no config file on command-line and no $DEF" 1>&2
135                 exit -1
136         fi
137         
138         [ -z "$MDS_RSH" ] && MDS_RSH="eval"
139         [ -z "$OST_RSH" ] && OST_RSH="eval"
140         [ -z "$OSC_RSH" ] && OSC_RSH="eval"
141 }
142
143 setup_portals() {
144         if grep -q portals /proc/modules; then
145                 echo "$0: portals already appears to be set up, skipping"
146                 return 0
147         fi
148
149         if [ -z "$NETWORK" -o -z "$LOCALHOST" -o -z "$SERVER" ]; then
150                 echo "$0: NETWORK or LOCALHOST or SERVER is not set" 1>&2
151                 exit -1
152         fi
153
154         [ -z "$OSTNODE" ] && OSTNODE=$SERVER
155
156         if [ -z "$DLM" ]; then
157                 if [ "$LOCALHOST" == "$SERVER" ]; then
158                         DLM=localhost
159                 else
160                         DLM=$SERVER
161                 fi
162         fi
163
164         [ -c /dev/portals ] || mknod /dev/portals c 10 240
165
166         do_insmod $PORTALS/linux/oslib/portals.o || exit -1
167
168         case $NETWORK in
169         elan)   [ "$PORT" ] && fail "$0: NETWORK is elan but PORT is set"
170                 do_insmod $PORTALS/linux/qswnal/kqswnal.o || exit -1
171                 ;;
172         tcp)    [ "$PORT" ] || fail "$0: NETWORK is tcp but PORT is not set"
173                 do_insmod $PORTALS/linux/socknal/ksocknal.o || exit -1
174                 $ACCEPTOR $PORT
175                 ;;
176         *)      fail "$0: unknown NETWORK '$NETWORK'" ;;
177         esac
178
179         $PTLCTL <<- EOF
180         setup $NETWORK
181         mynid $LOCALHOST
182         connect $SERVER $PORT
183         add_uuid self
184         add_uuid mds
185         connect $OSTNODE $PORT
186         add_uuid ost
187         connect $DLM $PORT
188         add_uuid ldlm
189         quit
190         EOF
191 }
192
193 setup_lustre() {
194         [ -c /dev/obd ] || mknod /dev/obd c 10 241
195
196         do_insmod $LUSTRE/obdclass/obdclass.o || exit -1
197         do_insmod $LUSTRE/ptlrpc/ptlrpc.o || exit -1
198         do_insmod $LUSTRE/ldlm/ldlm.o || exit -1
199         do_insmod $LUSTRE/extN/extN.o || \
200                 echo "info: can't load extN.o module, not fatal if using ext3"
201         do_insmod $LUSTRE/mds/mds.o || exit -1
202         #do_insmod $LUSTRE/mds/mds_ext2.o || exit -1
203         #do_insmod $LUSTRE/mds/mds_ext3.o || exit -1
204         do_insmod $LUSTRE/mds/mds_extN.o || \
205                 echo "info: can't load mds_extN.o module, needs extN.o"
206         do_insmod $LUSTRE/obdecho/obdecho.o || exit -1
207         do_insmod $LUSTRE/obdext2/obdext2.o || exit -1
208         do_insmod $LUSTRE/obdfilter/obdfilter.o || exit -1
209         do_insmod $LUSTRE/ost/ost.o || exit -1
210         do_insmod $LUSTRE/osc/osc.o || exit -1
211         do_insmod $LUSTRE/mdc/mdc.o || exit -1
212         do_insmod $LUSTRE/llite/llite.o || exit -1
213
214         list_mods
215
216         if $OBDCTL name2dev RPCDEV > /dev/null 2>&1; then
217                 echo "$0: RPCDEV is already configured, skipping"
218                 return 0
219         fi
220
221         $OBDCTL <<- EOF || return $?
222         newdev
223         attach ptlrpc RPCDEV
224         setup
225         quit
226         EOF
227
228         [ -d /mnt/lustre ] || mkdir /mnt/lustre
229 }
230
231 setup_ldlm() {
232         [ "$SETUP_LDLM" = "y" ] || return 0
233
234         [ -c /dev/portals ] || mknod /dev/portals c 10 240
235
236         $OBDCTL <<- EOF || return $?
237         newdev
238         attach ldlm LDLMDEV
239         setup
240         quit
241         EOF
242
243 }
244
245 find_devno() {
246         if [ -z "$1" ]; then
247                 echo "usage: $0 <devname>" 1>&2
248                 return -1
249         fi
250
251         $OBDCTL name2dev $1
252 }
253
254 setup_mds() {
255         [ "$SETUP_MDS" = "y" ] || return 0
256
257         if [ -z "$MDSFS" -o -z "$MDSDEV" ]; then
258                 echo "error: setup_mds: MDSFS or MDSDEV unset" 1>&2
259                 return -1
260         fi
261
262         [ "$1" ] && DO_FS=$1
263         if [ "$DO_FS" != "new_fs" -a "$DO_FS" != "old_fs" ]; then
264                 echo "usage: setup_mds {new_fs|old_fs}" 1>&2
265                 return -1
266         fi
267
268         if $OBDCTL name2dev MDSDEV > /dev/null 2>&1; then
269                 echo "$0: MDSDEV is already configured"
270                 return 0
271         fi
272
273         $DO_FS ${MDSFS} ${MDSDEV} ${MDSSIZE}
274         MDS=${LOOPDEV}
275
276         $OBDCTL <<- EOF || return $?
277         newdev
278         attach mds MDSDEV
279         setup ${MDS} ${MDSFS}
280         quit
281         EOF
282 }
283
284 setup_ost() {
285         [ "$SETUP_OST" = "y" ] || return 0
286
287         if [ -z "$OSTTYPE" ]; then
288                 echo "error: setup_ost: OSTTYPE unset" 1>&2
289                 return -1
290         fi
291
292         case $OSTTYPE in
293         obdecho)        OBD=
294                         OBDARG=
295                         NEED_FS=n
296                 ;;
297         obdext2)        OBDARG=
298                         NEED_FS=y
299                 ;;
300         obdfilter)      OBDARG=$OSTFS
301                         NEED_FS=y
302                 ;;
303         *)      echo "error: setup_ost: unknown OSTTYPE '$OSTTYPE'" 1>&2
304                 return -1
305                 ;;
306         esac
307
308         if $OBDCTL name2dev OBDDEV > /dev/null 2>&1; then
309                 echo "$0: OBDDEV is already configured"
310                 return 0
311         fi
312
313         if [ "$NEED_FS" = "y" ]; then
314                 [ "$1" ] && DO_FS=$1
315                 if [ -z "$OSTFS" -o -z "$OSTDEV" ]; then
316                         echo "error: setup_ost: OSTFS or OSTDEV unset" 1>&2
317                         return -1
318                 fi
319
320                 if [ "$DO_FS" != "new_fs" -a "$DO_FS" != "old_fs" ]; then
321                         echo "usage: setup_ost {new_fs|old_fs}" 1>&2
322                         return -1
323                 fi
324
325                 $DO_FS ${OSTFS} ${OSTDEV} ${OSTSIZE}
326                 OBD=${LOOPDEV}
327         fi
328
329         $OBDCTL <<- EOF || return $?
330         newdev
331         attach ${OSTTYPE} OBDDEV
332         setup ${OBD} ${OBDARG}
333         quit
334         EOF
335         $OBDCTL <<- EOF || return $?
336         newdev
337         attach ost OSTDEV
338         setup \$OBDDEV
339         quit
340         EOF
341 }
342
343 setup_server() {
344         setup_mds $1 && setup_ost $1
345 }
346
347 setup_osc() {
348         [ "$SETUP_OSC" != "y" ] && return 0
349
350         if $OBDCTL name2dev OSCDEV > /dev/null 2>&1; then
351                 echo "$0: OSCDEV is already configured"
352                 return 0
353         fi
354
355         $OBDCTL <<- EOF || return $?
356         newdev
357         attach osc OSCDEV
358         setup -1
359         quit
360         EOF
361 }
362
363 setup_mount() {
364         [ "$SETUP_MOUNT" != "y" ] && return 0
365
366         [ "$OSCMT" ] || fail "error: $0: OSCMT unset"
367
368         if mount | grep -q $OSCMT; then
369                 echo "$0: $OSCMT is already mounted"
370                 return 0
371         fi
372
373         [ ! -d $OSCMT ] && mkdir $OSCMT
374         echo "$0: mounting \$OSCDEV on $OSCMT"
375         mount -t lustre_lite -o device=`find_devno OSCDEV` none $OSCMT
376 }
377
378 setup_client() {
379         setup_osc && setup_mount
380 }
381
382 DEBUG_ON="echo 0xffffffff > /proc/sys/portals/debug"
383 DEBUG_OFF="echo 0 > /proc/sys/portals/debug"
384
385 debug_server_off() {
386         [ "$MDS_RSH" ] && echo "Turn OFF debug on MDS" && $MDS_RSH "$DEBUG_OFF"
387         [ "$OST_RSH" ] && echo "Turn OFF debug on OST" && $OST_RSH "$DEBUG_OFF"
388 }
389
390 debug_server_on() {
391         [ "$MDS_RSH" ] && echo "Turn ON debug on MDS" && $MDS_RSH "$DEBUG_ON"
392         [ "$OST_RSH" ] && echo "Turn ON debug on OST" && $OST_RSH "$DEBUG_ON"
393 }
394
395 debug_client_off() {
396         echo "Turning OFF debug on client" && $OSC_RSH "$DEBUG_OFF"
397 }
398
399 debug_client_on() {
400         echo "Turning ON debug on client" && $OSC_RSH "$DEBUG_ON"
401 }
402
403 cleanup_portals() {
404         [ -z "$NETWORK" ] && NETWORK=tcp
405         $PTLCTL <<- EOF
406         setup $NETWORK
407         disconnect
408         del_uuid self
409         del_uuid mds
410         del_uuid ost
411         del_uuid ldlm
412         quit
413         EOF
414
415         rmmod kqswnal
416         rmmod ksocknal
417         rmmod portals
418 }
419
420 cleanup_lustre() {
421         killall acceptor
422
423         losetup -d ${LOOP}0
424         losetup -d ${LOOP}1
425         losetup -d ${LOOP}2
426
427         rmmod llite
428         rmmod mdc
429
430         rmmod mds_extN
431         rmmod mds_ext3
432         rmmod mds_ext2
433         rmmod mds
434         rmmod ost
435         rmmod osc
436         rmmod obdecho
437         rmmod obdfilter
438         rmmod obdext2
439         rmmod extN
440
441         rmmod ldlm
442         rmmod ptlrpc
443         rmmod obdclass
444 }
445
446 cleanup_ldlm() {
447         [ "$SETUP" -a -z "$SETUP_LDLM" ] && return 0
448
449         LDLMDEVNO=`find_devno LDLMDEV`
450         if [ "$LDLMDEVNO" ]; then
451                 $OBDCTL <<- EOF
452                 device $LDLMDEVNO
453                 cleanup
454                 detach
455                 quit
456                 EOF
457         fi
458 }
459
460 cleanup_mds() {
461         [ "$SETUP" -a -z "$SETUP_MDS" ] && return 0
462
463         MDSDEVNO=`find_devno MDSDEV`
464         if [ "$MDSDEVNO" ]; then
465                 $OBDCTL <<- EOF
466                 device $MDSDEVNO
467                 cleanup
468                 detach
469                 quit
470                 EOF
471         fi
472 }
473
474 cleanup_ost() {
475         [ "$SETUP" -a -z "$SETUP_OST" ] && return 0
476
477         OSTDEVNO=`find_devno OSTDEV`
478         if [ "$OSTDEVNO" ]; then
479                 $OBDCTL <<- EOF
480                 device $OSTDEVNO
481                 cleanup
482                 detach
483                 quit
484                 EOF
485         fi
486
487         OBDDEVNO=`find_devno OBDDEV`
488         if [ "$OBDDEVNO" ]; then
489                 $OBDCTL <<- EOF
490                 device $OBDDEVNO
491                 cleanup
492                 detach
493                 quit
494                 EOF
495         fi
496 }
497
498 cleanup_server() {
499         cleanup_ost && cleanup_mds
500 }
501
502 cleanup_mount() {
503         [ "$SETUP" -a -z "$SETUP_MOUNT" ] && return 0
504
505         [ "$OSCMT" ] || OSCMT=/mnt/lustre
506         if [ "`mount | grep $OSCMT`" ]; then
507                 umount $OSCMT || fail "unable to unmount $OSCMT"
508         fi
509 }
510
511 cleanup_osc() {
512         [ "$SETUP" -a -z "$SETUP_OSC" ] && return 0
513
514         OSCDEVNO=`find_devno OSCDEV`
515         if [ "$OSCDEVNO" ]; then
516                 $OBDCTL <<- EOF
517                 device $OSCDEVNO
518                 cleanup
519                 detach
520                 quit
521                 EOF
522         fi
523 }
524
525 cleanup_rpc() {
526         RPCDEVNO=`find_devno RPCDEV`
527         if [ "$RPCDEVNO" ]; then
528                 $OBDCTL <<- EOF
529                 device $RPCDEVNO
530                 cleanup
531                 detach
532                 quit
533                 EOF
534         fi
535 }
536
537 cleanup_client() {
538         cleanup_mount && cleanup_osc && cleanup_rpc
539 }
540
541 fail() { 
542     echo "ERROR: $1" 1>&2
543     [ $2 ] && RC=$2 || RC=1
544     exit $RC
545 }