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