Whamcloud - gitweb
Add LDLM setup/cleanup to subsystems set up via new configuration scripts.
[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 1
31         insmod $MODULE || exit -1
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 new_fs () {
54         EFILE="$1_$3.gz"
55         MKFS="mkfs.$1"
56         MKFSOPT="-b 4096"
57
58         [ "$1" = "ext3" ] && MKFS="mkfs.ext2 -j"
59         if [ "$1" = "extN" ]; then
60                 MKFS="mkfs.ext2 -j"
61                 EFILE="$1_ext3.gz"
62                 do_insmod $LUSTRE/extN/extN.o
63         fi
64
65         if [ -b "$2" ]; then
66                 [ $# -lt 2 -o $# -gt 3 ] && \
67                         echo "usage: $0 <fstype> <file> [size]" 1>&2 && exit -1
68
69                 PM="/proc/mounts"
70                 [ -r "$PM" ] || PM="/etc/mtab"
71
72                 grep "$2 " $PM 1>&2 && echo "$0: $2 is in $PM!" 1>&2 && exit -1
73
74                 $MKFS $MKFSOPT $2 $3 || exit -1
75                 LOOPDEV=$2      # Not really a loop device
76         else
77                 [ $# -ne 3 ] && \
78                         echo "usage: $0 <fstype> <file> <size>" 1>&2 && exit -1
79
80                 if [ -r "$EFILE" ]; then
81                         echo "using prepared filesystem $EFILE for $2"
82                         zcat "$EFILE" > $2 || exit -1
83                         sync
84                 else
85                         echo "creating new sparse filesystem on $2"
86                         dd if=/dev/zero of=$2 bs=1k seek=$3 count=1 1>&2 || exit -1
87                         $MKFS $MKFSOPT -F $2 1>&2 || exit -1
88                 fi
89                 LOOPDEV=`next_loop_dev`
90                 losetup ${LOOPDEV} $2 1>&2 || exit -1
91         fi
92 }
93
94 # Set up to use an existing filesystem.  We take the same parameters as
95 # new_fs, even though we only use the <fstype> and <file> parameters, to
96 # make it easy to convert between new_fs and old_fs in testing scripts.
97 old_fs () {
98         [ -e $2 ] || exit -1
99
100         [ "$1" = "extN" ] && do_insmod $LUSTRE/extN/extN.o
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
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
168                 ;;
169         tcp)    [ "$PORT" ] || fail "$0: NETWORK is tcp but PORT is not set"
170                 do_insmod $PORTALS/linux/socknal/ksocknal.o
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
194         do_insmod $LUSTRE/rpc/ptlrpc.o
195         do_insmod $LUSTRE/ldlm/ldlm.o
196         do_insmod $LUSTRE/extN/extN.o
197         do_insmod $LUSTRE/mds/mds.o
198         do_insmod $LUSTRE/obdecho/obdecho.o
199         do_insmod $LUSTRE/ext2obd/obdext2.o
200         do_insmod $LUSTRE/filterobd/obdfilter.o
201         do_insmod $LUSTRE/ost/ost.o
202         do_insmod $LUSTRE/osc/osc.o
203         do_insmod $LUSTRE/mdc/mdc.o
204         do_insmod $LUSTRE/llight/llite.o
205
206         list_mods
207
208         if $OBDCTL name2dev RPCDEV > /dev/null 2>&1; then
209                 echo "$0: RPCDEV is already configured, skipping"
210                 return 0
211         fi
212
213         $OBDCTL <<- EOF || return $rc
214         newdev
215         attach ptlrpc RPCDEV
216         setup
217         quit
218         EOF
219
220         [ -d /mnt/lustre ] || mkdir /mnt/lustre
221 }
222
223 setup_ldlm() {
224         [ "$SETUP_LDLM" = "y" ] || return 0
225
226         [ -c /dev/portals ] || mknod /dev/portals c 10 240
227
228         $OBDCTL <<- EOF || return $rc
229         newdev
230         attach ldlm LDLMDEV
231         setup
232         quit
233         EOF
234
235 }
236
237 find_devno() {
238         if [ -z "$1" ]; then
239                 echo "usage: $0 <devname>" 1>&2
240                 return -1
241         fi
242
243         $OBDCTL name2dev $1
244 }
245
246 setup_mds() {
247         [ "$SETUP_MDS" = "y" ] || return 0
248
249         if [ -z "$MDSFS" -o -z "$MDSDEV" ]; then
250                 echo "error: setup_mds: MDSFS or MDSDEV unset" 1>&2
251                 return -1
252         fi
253
254         [ "$1" ] && DO_FS=$1
255         if [ "$DO_FS" != "new_fs" -a "$DO_FS" != "old_fs" ]; then
256                 echo "usage: setup_mds {new_fs|old_fs}" 1>&2
257                 return -1
258         fi
259
260         if $OBDCTL name2dev MDSDEV > /dev/null 2>&1; then
261                 echo "$0: MDSDEV is already configured"
262                 return 0
263         fi
264
265         $DO_FS ${MDSFS} ${MDSDEV} ${MDSSIZE}
266         MDS=${LOOPDEV}
267
268         $OBDCTL <<- EOF || return $rc
269         newdev
270         attach mds MDSDEV
271         setup ${MDS} ${MDSFS}
272         quit
273         EOF
274 }
275
276 setup_ost() {
277         [ "$SETUP_OST" = "y" ] || return 0
278
279         if [ -z "$OSTTYPE" ]; then
280                 echo "error: setup_ost: OSTTYPE unset" 1>&2
281                 return -1
282         fi
283
284         case $OSTTYPE in
285         obdecho)        OBD=
286                         OBDARG=
287                         NEED_FS=n
288                 ;;
289         obdext2)        OBDARG=
290                         NEED_FS=y
291                 ;;
292         obdfilter)      OBDARG=$OSTFS
293                         NEED_FS=y
294                 ;;
295         *)      echo "error: setup_ost: unknown OSTTYPE '$OSTTYPE'" 1>&2
296                 return -1
297                 ;;
298         esac
299
300         if $OBDCTL name2dev OBDDEV > /dev/null 2>&1; then
301                 echo "$0: OBDDEV is already configured"
302                 return 0
303         fi
304
305         if [ "$NEED_FS" = "y" ]; then
306                 [ "$1" ] && DO_FS=$1
307                 if [ -z "$OSTFS" -o -z "$OSTDEV" ]; then
308                         echo "error: setup_ost: OSTFS or OSTDEV unset" 1>&2
309                         return -1
310                 fi
311
312                 if [ "$DO_FS" != "new_fs" -a "$DO_FS" != "old_fs" ]; then
313                         echo "usage: setup_ost {new_fs|old_fs}" 1>&2
314                         return -1
315                 fi
316
317                 $DO_FS ${OSTFS} ${OSTDEV} ${OSTSIZE}
318                 OBD=${LOOPDEV}
319         fi
320
321         $OBDCTL <<- EOF || return $rc
322         newdev
323         attach ${OSTTYPE} OBDDEV
324         setup ${OBD} ${OBDARG}
325         newdev
326         attach ost OSTDEV
327         setup \$OBDDEV
328         quit
329         EOF
330 }
331
332 setup_server() {
333         setup_mds $1 && setup_ost $1
334 }
335
336 setup_osc() {
337         [ "$SETUP_OSC" != "y" ] && return 0
338
339         if $OBDCTL name2dev OSCDEV > /dev/null 2>&1; then
340                 echo "$0: OSCDEV is already configured"
341                 return 0
342         fi
343
344         $OBDCTL <<- EOF || return $rc
345         newdev
346         attach osc OSCDEV
347         setup -1
348         quit
349         EOF
350 }
351
352 setup_mount() {
353         [ "$SETUP_MOUNT" != "y" ] && return 0
354
355         [ "$OSCMT" ] || fail "error: $0: OSCMT unset"
356
357         if mount | grep -q $OSCMT; then
358                 echo "$0: $OSCMT is already mounted"
359                 return 0
360         fi
361
362         [ ! -d $OSCMT ] && mkdir $OSCMT
363         mount -t lustre_lite -o device=`find_devno OSCDEV` none $OSCMT
364 }
365
366 setup_client() {
367         setup_osc && setup_mount
368 }
369
370 DEBUG_ON="echo 0xffffffff > /proc/sys/portals/debug"
371 DEBUG_OFF="echo 0 > /proc/sys/portals/debug"
372
373 debug_server_off() {
374         [ "$MDS_RSH" ] && echo "Turn OFF debug on MDS" && $MDS_RSH "$DEBUG_OFF"
375         [ "$OST_RSH" ] && echo "Turn OFF debug on OST" && $OST_RSH "$DEBUG_OFF"
376 }
377
378 debug_server_on() {
379         [ "$MDS_RSH" ] && echo "Turn ON debug on MDS" && $MDS_RSH "$DEBUG_ON"
380         [ "$OST_RSH" ] && echo "Turn ON debug on OST" && $OST_RSH "$DEBUG_ON"
381 }
382
383 debug_client_off() {
384         echo "Turning OFF debug on client" && $OSC_RSH "$DEBUG_OFF"
385 }
386
387 debug_client_on() {
388         echo "Turning ON debug on client" && $OSC_RSH "$DEBUG_ON"
389 }
390
391 cleanup_portals() {
392         $PTLCTL <<- EOF
393         setup tcp
394         disconnect
395         del_uuid self
396         del_uuid mds
397         del_uuid ost
398         del_uuid ldlm
399         quit
400         EOF
401
402         rmmod kqswnal
403         rmmod ksocknal
404         rmmod portals
405 }
406
407 cleanup_lustre() {
408         killall acceptor
409
410         losetup -d ${LOOP}0
411         losetup -d ${LOOP}1
412         losetup -d ${LOOP}2
413
414         rmmod llite
415         rmmod mdc
416
417         rmmod mds
418         rmmod ost
419         rmmod osc
420         rmmod obdecho
421         rmmod obdfilter
422         rmmod obdext2
423         rmmod extN
424
425         rmmod ldlm
426         rmmod ptlrpc
427         rmmod obdclass
428 }
429
430 cleanup_ldlm() {
431         [ "$SETUP" -a -z "$SETUP_LDLM" ] && return 0
432
433         LDLMDEVNO=`find_devno LDLMDEV`
434         if [ "$LDLMDEVNO" ]; then
435                 $OBDCTL <<- EOF
436                 device $LDLMDEVNO
437                 cleanup
438                 detach
439                 quit
440                 EOF
441         fi
442 }
443
444 cleanup_mds() {
445         [ "$SETUP" -a -z "$SETUP_MDS" ] && return 0
446
447         MDSDEVNO=`find_devno MDSDEV`
448         if [ "$MDSDEVNO" ]; then
449                 $OBDCTL <<- EOF
450                 device $MDSDEVNO
451                 cleanup
452                 detach
453                 quit
454                 EOF
455         fi
456 }
457
458 cleanup_ost() {
459         [ "$SETUP" -a -z "$SETUP_OST" ] && return 0
460
461         OSTDEVNO=`find_devno OSTDEV`
462         if [ "$OSTDEVNO" ]; then
463                 $OBDCTL <<- EOF
464                 device $OSTDEVNO
465                 cleanup
466                 detach
467                 quit
468                 EOF
469         fi
470
471         OBDDEVNO=`find_devno OBDDEV`
472         if [ "$OBDDEVNO" ]; then
473                 $OBDCTL <<- EOF
474                 device $OBDDEVNO
475                 cleanup
476                 detach
477                 quit
478                 EOF
479         fi
480 }
481
482 cleanup_server() {
483         cleanup_ost && cleanup_mds
484 }
485
486 cleanup_mount() {
487         [ "$SETUP" -a -z "$SETUP_MOUNT" ] && return 0
488
489         [ "$OSCMT" ] || OSCMT=/mnt/lustre
490         if [ "`mount | grep $OSCMT`" ]; then
491                 umount $OSCMT || fail "unable to unmount $OSCMT"
492         fi
493 }
494
495 cleanup_osc() {
496         [ "$SETUP" -a -z "$SETUP_OSC" ] && return 0
497
498         OSCDEVNO=`find_devno OSCDEV`
499         if [ "$OSCDEVNO" ]; then
500                 $OBDCTL <<- EOF
501                 device $OSCDEVNO
502                 cleanup
503                 detach
504                 quit
505                 EOF
506         fi
507 }
508
509 cleanup_rpc() {
510         RPCDEVNO=`find_devno RPCDEV`
511         if [ "$RPCDEVNO" ]; then
512                 $OBDCTL <<- EOF
513                 device $RPCDEVNO
514                 cleanup
515                 detach
516                 quit
517                 EOF
518         fi
519 }
520
521 cleanup_client() {
522         cleanup_mount && cleanup_osc && cleanup_rpc
523 }
524
525 fail() { 
526     echo "ERROR: $1" 1>&2
527     [ $2 ] && RC=$2 || RC=1
528     exit $RC
529 }