Whamcloud - gitweb
Add mds filesystem helper modules to setup and cleanup.
[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../../obd
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 ] || fail "$0: module '$MODULE' not found"
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
96 # Set up to use an existing filesystem.  We take the same parameters as
97 # new_fs, even though we only use the <fstype> and <file> parameters, to
98 # make it easy to convert between new_fs and old_fs in testing scripts.
99 old_fs () {
100         [ -e $2 ] || exit -1
101
102         if [ -b "$2" ]; then
103                 LOOPDEV=$2      # Not really a loop device
104         else
105                 LOOPDEV=`next_loop_dev`
106                 losetup ${LOOPDEV} $2 1>&2 || exit -1
107         fi
108 }
109
110 list_mods() {
111         $DBGCTL modules > $R/tmp/ogdb
112         echo "The GDB module script is in $R/tmp/ogdb"
113         [ "$DEBUG_WAIT" = "yes" ] && echo -n "Press ENTER to continue" && read
114 }
115
116 # We need at least one setup file to be given.  It can be passed on
117 # the command-line, or it can be found in the home directory, or it
118 # can even be sourced into the current shell environment.
119 setup_opts() {
120         DEF=$HOME/.lustretestrc
121         [ -r $DEF ] && . $DEF && SETUP=y
122
123         for CFG in "$@" ; do
124                 case $CFG  in
125                 *.cfg) [ -r "$CFG" ] && . $CFG && SETUP=y ;;
126                 *) echo "unknown option '$CFG'" 1>&2
127                 esac
128         done
129
130         if [ "$SETUP" != "y" ]; then
131                 echo "error: no config file on command-line and no $DEF" 1>&2
132                 exit -1
133         fi
134         
135         [ -z "$MDS_RSH" ] && MDS_RSH="eval"
136         [ -z "$OST_RSH" ] && OST_RSH="eval"
137         [ -z "$OSC_RSH" ] && OSC_RSH="eval"
138 }
139
140 setup_portals() {
141         if grep -q portals /proc/modules; then
142                 echo "$0: portals already appears to be set up, skipping"
143                 return 0
144         fi
145
146         if [ -z "$NETWORK" -o -z "$LOCALHOST" -o -z "$SERVER" ]; then
147                 echo "$0: NETWORK or LOCALHOST or SERVER is not set" 1>&2
148                 exit -1
149         fi
150
151         [ -z "$OSTNODE" ] && OSTNODE=$SERVER
152
153         if [ -z "$DLM" ]; then
154                 if [ "$LOCALHOST" == "$SERVER" ]; then
155                         DLM=localhost
156                 else
157                         DLM=$SERVER
158                 fi
159         fi
160
161         [ -c /dev/portals ] || mknod /dev/portals c 10 240
162
163         do_insmod $PORTALS/linux/oslib/portals.o || exit -1
164
165         case $NETWORK in
166         elan)   [ "$PORT" ] && fail "$0: NETWORK is elan but PORT is set"
167                 do_insmod $PORTALS/linux/qswnal/kqswnal.o || exit -1
168                 ;;
169         tcp)    [ "$PORT" ] || fail "$0: NETWORK is tcp but PORT is not set"
170                 do_insmod $PORTALS/linux/socknal/ksocknal.o || exit -1
171                 $ACCEPTOR $PORT
172                 ;;
173         *)      fail "$0: unknown NETWORK '$NETWORK'" ;;
174         esac
175
176         $PTLCTL <<- EOF
177         setup $NETWORK
178         mynid $LOCALHOST
179         connect $SERVER $PORT
180         add_uuid self
181         add_uuid mds
182         connect $OSTNODE $PORT
183         add_uuid ost
184         connect $DLM $PORT
185         add_uuid ldlm
186         quit
187         EOF
188 }
189
190 setup_lustre() {
191         [ -c /dev/obd ] || mknod /dev/obd c 10 241
192
193         do_insmod $LUSTRE/class/obdclass.o || exit -1
194         do_insmod $LUSTRE/rpc/ptlrpc.o || exit -1
195         do_insmod $LUSTRE/ldlm/ldlm.o || exit -1
196         do_insmod $LUSTRE/extN/extN.o
197         do_insmod $LUSTRE/mds/mds.o || exit -1
198         do_insmod $LUSTRE/mds/mds_ext2.o || exit -1
199         do_insmod $LUSTRE/mds/mds_ext3.o || exit -1
200         do_insmod $LUSTRE/mds/mds_extN.o
201         do_insmod $LUSTRE/obdecho/obdecho.o || exit -1
202         do_insmod $LUSTRE/ext2obd/obdext2.o || exit -1
203         do_insmod $LUSTRE/filterobd/obdfilter.o || exit -1
204         do_insmod $LUSTRE/ost/ost.o || exit -1
205         do_insmod $LUSTRE/osc/osc.o || exit -1
206         do_insmod $LUSTRE/mdc/mdc.o || exit -1
207         do_insmod $LUSTRE/llight/llite.o || exit -1
208
209         list_mods
210
211         if $OBDCTL name2dev RPCDEV > /dev/null 2>&1; then
212                 echo "$0: RPCDEV is already configured, skipping"
213                 return 0
214         fi
215
216         $OBDCTL <<- EOF || return $rc
217         newdev
218         attach ptlrpc RPCDEV
219         setup
220         quit
221         EOF
222
223         [ -d /mnt/lustre ] || mkdir /mnt/lustre
224 }
225
226 setup_ldlm() {
227         [ "$SETUP_LDLM" = "y" ] || return 0
228
229         [ -c /dev/portals ] || mknod /dev/portals c 10 240
230
231         $OBDCTL <<- EOF || return $rc
232         newdev
233         attach ldlm LDLMDEV
234         setup
235         quit
236         EOF
237
238 }
239
240 find_devno() {
241         if [ -z "$1" ]; then
242                 echo "usage: $0 <devname>" 1>&2
243                 return -1
244         fi
245
246         $OBDCTL name2dev $1
247 }
248
249 setup_mds() {
250         [ "$SETUP_MDS" = "y" ] || return 0
251
252         if [ -z "$MDSFS" -o -z "$MDSDEV" ]; then
253                 echo "error: setup_mds: MDSFS or MDSDEV unset" 1>&2
254                 return -1
255         fi
256
257         [ "$1" ] && DO_FS=$1
258         if [ "$DO_FS" != "new_fs" -a "$DO_FS" != "old_fs" ]; then
259                 echo "usage: setup_mds {new_fs|old_fs}" 1>&2
260                 return -1
261         fi
262
263         if $OBDCTL name2dev MDSDEV > /dev/null 2>&1; then
264                 echo "$0: MDSDEV is already configured"
265                 return 0
266         fi
267
268         $DO_FS ${MDSFS} ${MDSDEV} ${MDSSIZE}
269         MDS=${LOOPDEV}
270
271         $OBDCTL <<- EOF || return $rc
272         newdev
273         attach mds MDSDEV
274         setup ${MDS} ${MDSFS}
275         quit
276         EOF
277 }
278
279 setup_ost() {
280         [ "$SETUP_OST" = "y" ] || return 0
281
282         if [ -z "$OSTTYPE" ]; then
283                 echo "error: setup_ost: OSTTYPE unset" 1>&2
284                 return -1
285         fi
286
287         case $OSTTYPE in
288         obdecho)        OBD=
289                         OBDARG=
290                         NEED_FS=n
291                 ;;
292         obdext2)        OBDARG=
293                         NEED_FS=y
294                 ;;
295         obdfilter)      OBDARG=$OSTFS
296                         NEED_FS=y
297                 ;;
298         *)      echo "error: setup_ost: unknown OSTTYPE '$OSTTYPE'" 1>&2
299                 return -1
300                 ;;
301         esac
302
303         if $OBDCTL name2dev OBDDEV > /dev/null 2>&1; then
304                 echo "$0: OBDDEV is already configured"
305                 return 0
306         fi
307
308         if [ "$NEED_FS" = "y" ]; then
309                 [ "$1" ] && DO_FS=$1
310                 if [ -z "$OSTFS" -o -z "$OSTDEV" ]; then
311                         echo "error: setup_ost: OSTFS or OSTDEV unset" 1>&2
312                         return -1
313                 fi
314
315                 if [ "$DO_FS" != "new_fs" -a "$DO_FS" != "old_fs" ]; then
316                         echo "usage: setup_ost {new_fs|old_fs}" 1>&2
317                         return -1
318                 fi
319
320                 $DO_FS ${OSTFS} ${OSTDEV} ${OSTSIZE}
321                 OBD=${LOOPDEV}
322         fi
323
324         $OBDCTL <<- EOF || return $rc
325         newdev
326         attach ${OSTTYPE} OBDDEV
327         setup ${OBD} ${OBDARG}
328         newdev
329         attach ost OSTDEV
330         setup \$OBDDEV
331         quit
332         EOF
333 }
334
335 setup_server() {
336         setup_mds $1 && setup_ost $1
337 }
338
339 setup_osc() {
340         [ "$SETUP_OSC" != "y" ] && return 0
341
342         if $OBDCTL name2dev OSCDEV > /dev/null 2>&1; then
343                 echo "$0: OSCDEV is already configured"
344                 return 0
345         fi
346
347         $OBDCTL <<- EOF || return $rc
348         newdev
349         attach osc OSCDEV
350         setup -1
351         quit
352         EOF
353 }
354
355 setup_mount() {
356         [ "$SETUP_MOUNT" != "y" ] && return 0
357
358         [ "$OSCMT" ] || fail "error: $0: OSCMT unset"
359
360         if mount | grep -q $OSCMT; then
361                 echo "$0: $OSCMT is already mounted"
362                 return 0
363         fi
364
365         [ ! -d $OSCMT ] && mkdir $OSCMT
366         mount -t lustre_lite -o device=`find_devno OSCDEV` none $OSCMT
367 }
368
369 setup_client() {
370         setup_osc && setup_mount
371 }
372
373 DEBUG_ON="echo 0xffffffff > /proc/sys/portals/debug"
374 DEBUG_OFF="echo 0 > /proc/sys/portals/debug"
375
376 debug_server_off() {
377         [ "$MDS_RSH" ] && echo "Turn OFF debug on MDS" && $MDS_RSH "$DEBUG_OFF"
378         [ "$OST_RSH" ] && echo "Turn OFF debug on OST" && $OST_RSH "$DEBUG_OFF"
379 }
380
381 debug_server_on() {
382         [ "$MDS_RSH" ] && echo "Turn ON debug on MDS" && $MDS_RSH "$DEBUG_ON"
383         [ "$OST_RSH" ] && echo "Turn ON debug on OST" && $OST_RSH "$DEBUG_ON"
384 }
385
386 debug_client_off() {
387         echo "Turning OFF debug on client" && $OSC_RSH "$DEBUG_OFF"
388 }
389
390 debug_client_on() {
391         echo "Turning ON debug on client" && $OSC_RSH "$DEBUG_ON"
392 }
393
394 cleanup_portals() {
395         $PTLCTL <<- EOF
396         setup tcp
397         disconnect
398         del_uuid self
399         del_uuid mds
400         del_uuid ost
401         del_uuid ldlm
402         quit
403         EOF
404
405         rmmod kqswnal
406         rmmod ksocknal
407         rmmod portals
408 }
409
410 cleanup_lustre() {
411         killall acceptor
412
413         losetup -d ${LOOP}0
414         losetup -d ${LOOP}1
415         losetup -d ${LOOP}2
416
417         rmmod llite
418         rmmod mdc
419
420         rmmod mds_extN
421         rmmod mds_ext3
422         rmmod mds_ext2
423         rmmod mds
424         rmmod ost
425         rmmod osc
426         rmmod obdecho
427         rmmod obdfilter
428         rmmod obdext2
429         rmmod extN
430
431         rmmod ldlm
432         rmmod ptlrpc
433         rmmod obdclass
434 }
435
436 cleanup_ldlm() {
437         [ "$SETUP" -a -z "$SETUP_LDLM" ] && return 0
438
439         LDLMDEVNO=`find_devno LDLMDEV`
440         if [ "$LDLMDEVNO" ]; then
441                 $OBDCTL <<- EOF
442                 device $LDLMDEVNO
443                 cleanup
444                 detach
445                 quit
446                 EOF
447         fi
448 }
449
450 cleanup_mds() {
451         [ "$SETUP" -a -z "$SETUP_MDS" ] && return 0
452
453         MDSDEVNO=`find_devno MDSDEV`
454         if [ "$MDSDEVNO" ]; then
455                 $OBDCTL <<- EOF
456                 device $MDSDEVNO
457                 cleanup
458                 detach
459                 quit
460                 EOF
461         fi
462 }
463
464 cleanup_ost() {
465         [ "$SETUP" -a -z "$SETUP_OST" ] && return 0
466
467         OSTDEVNO=`find_devno OSTDEV`
468         if [ "$OSTDEVNO" ]; then
469                 $OBDCTL <<- EOF
470                 device $OSTDEVNO
471                 cleanup
472                 detach
473                 quit
474                 EOF
475         fi
476
477         OBDDEVNO=`find_devno OBDDEV`
478         if [ "$OBDDEVNO" ]; then
479                 $OBDCTL <<- EOF
480                 device $OBDDEVNO
481                 cleanup
482                 detach
483                 quit
484                 EOF
485         fi
486 }
487
488 cleanup_server() {
489         cleanup_ost && cleanup_mds
490 }
491
492 cleanup_mount() {
493         [ "$SETUP" -a -z "$SETUP_MOUNT" ] && return 0
494
495         [ "$OSCMT" ] || OSCMT=/mnt/lustre
496         if [ "`mount | grep $OSCMT`" ]; then
497                 umount $OSCMT || fail "unable to unmount $OSCMT"
498         fi
499 }
500
501 cleanup_osc() {
502         [ "$SETUP" -a -z "$SETUP_OSC" ] && return 0
503
504         OSCDEVNO=`find_devno OSCDEV`
505         if [ "$OSCDEVNO" ]; then
506                 $OBDCTL <<- EOF
507                 device $OSCDEVNO
508                 cleanup
509                 detach
510                 quit
511                 EOF
512         fi
513 }
514
515 cleanup_rpc() {
516         RPCDEVNO=`find_devno RPCDEV`
517         if [ "$RPCDEVNO" ]; then
518                 $OBDCTL <<- EOF
519                 device $RPCDEVNO
520                 cleanup
521                 detach
522                 quit
523                 EOF
524         fi
525 }
526
527 cleanup_client() {
528         cleanup_mount && cleanup_osc && cleanup_rpc
529 }
530
531 fail() { 
532     echo "ERROR: $1" 1>&2
533     [ $2 ] && RC=$2 || RC=1
534     exit $RC
535 }