Whamcloud - gitweb
Clean up the right portals network type if it is set (default tcp).
[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         mount -t lustre_lite -o device=`find_devno OSCDEV` none $OSCMT
375 }
376
377 setup_client() {
378         setup_osc && setup_mount
379 }
380
381 DEBUG_ON="echo 0xffffffff > /proc/sys/portals/debug"
382 DEBUG_OFF="echo 0 > /proc/sys/portals/debug"
383
384 debug_server_off() {
385         [ "$MDS_RSH" ] && echo "Turn OFF debug on MDS" && $MDS_RSH "$DEBUG_OFF"
386         [ "$OST_RSH" ] && echo "Turn OFF debug on OST" && $OST_RSH "$DEBUG_OFF"
387 }
388
389 debug_server_on() {
390         [ "$MDS_RSH" ] && echo "Turn ON debug on MDS" && $MDS_RSH "$DEBUG_ON"
391         [ "$OST_RSH" ] && echo "Turn ON debug on OST" && $OST_RSH "$DEBUG_ON"
392 }
393
394 debug_client_off() {
395         echo "Turning OFF debug on client" && $OSC_RSH "$DEBUG_OFF"
396 }
397
398 debug_client_on() {
399         echo "Turning ON debug on client" && $OSC_RSH "$DEBUG_ON"
400 }
401
402 cleanup_portals() {
403         [ -z "$NETWORK" ] && NETWORK=tcp
404         $PTLCTL <<- EOF
405         setup $NETWORK
406         disconnect
407         del_uuid self
408         del_uuid mds
409         del_uuid ost
410         del_uuid ldlm
411         quit
412         EOF
413
414         rmmod kqswnal
415         rmmod ksocknal
416         rmmod portals
417 }
418
419 cleanup_lustre() {
420         killall acceptor
421
422         losetup -d ${LOOP}0
423         losetup -d ${LOOP}1
424         losetup -d ${LOOP}2
425
426         rmmod llite
427         rmmod mdc
428
429         rmmod mds_extN
430         rmmod mds_ext3
431         rmmod mds_ext2
432         rmmod mds
433         rmmod ost
434         rmmod osc
435         rmmod obdecho
436         rmmod obdfilter
437         rmmod obdext2
438         rmmod extN
439
440         rmmod ldlm
441         rmmod ptlrpc
442         rmmod obdclass
443 }
444
445 cleanup_ldlm() {
446         [ "$SETUP" -a -z "$SETUP_LDLM" ] && return 0
447
448         LDLMDEVNO=`find_devno LDLMDEV`
449         if [ "$LDLMDEVNO" ]; then
450                 $OBDCTL <<- EOF
451                 device $LDLMDEVNO
452                 cleanup
453                 detach
454                 quit
455                 EOF
456         fi
457 }
458
459 cleanup_mds() {
460         [ "$SETUP" -a -z "$SETUP_MDS" ] && return 0
461
462         MDSDEVNO=`find_devno MDSDEV`
463         if [ "$MDSDEVNO" ]; then
464                 $OBDCTL <<- EOF
465                 device $MDSDEVNO
466                 cleanup
467                 detach
468                 quit
469                 EOF
470         fi
471 }
472
473 cleanup_ost() {
474         [ "$SETUP" -a -z "$SETUP_OST" ] && return 0
475
476         OSTDEVNO=`find_devno OSTDEV`
477         if [ "$OSTDEVNO" ]; then
478                 $OBDCTL <<- EOF
479                 device $OSTDEVNO
480                 cleanup
481                 detach
482                 quit
483                 EOF
484         fi
485
486         OBDDEVNO=`find_devno OBDDEV`
487         if [ "$OBDDEVNO" ]; then
488                 $OBDCTL <<- EOF
489                 device $OBDDEVNO
490                 cleanup
491                 detach
492                 quit
493                 EOF
494         fi
495 }
496
497 cleanup_server() {
498         cleanup_ost && cleanup_mds
499 }
500
501 cleanup_mount() {
502         [ "$SETUP" -a -z "$SETUP_MOUNT" ] && return 0
503
504         [ "$OSCMT" ] || OSCMT=/mnt/lustre
505         if [ "`mount | grep $OSCMT`" ]; then
506                 umount $OSCMT || fail "unable to unmount $OSCMT"
507         fi
508 }
509
510 cleanup_osc() {
511         [ "$SETUP" -a -z "$SETUP_OSC" ] && return 0
512
513         OSCDEVNO=`find_devno OSCDEV`
514         if [ "$OSCDEVNO" ]; then
515                 $OBDCTL <<- EOF
516                 device $OSCDEVNO
517                 cleanup
518                 detach
519                 quit
520                 EOF
521         fi
522 }
523
524 cleanup_rpc() {
525         RPCDEVNO=`find_devno RPCDEV`
526         if [ "$RPCDEVNO" ]; then
527                 $OBDCTL <<- EOF
528                 device $RPCDEVNO
529                 cleanup
530                 detach
531                 quit
532                 EOF
533         fi
534 }
535
536 cleanup_client() {
537         cleanup_mount && cleanup_osc && cleanup_rpc
538 }
539
540 fail() { 
541     echo "ERROR: $1" 1>&2
542     [ $2 ] && RC=$2 || RC=1
543     exit $RC
544 }