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