Whamcloud - gitweb
Split out the various common configurations into simple config files.
[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         [ -c /dev/portals ] || mknod /dev/portals c 10 240
124
125         insmod $PORTALS/linux/oslib/portals.o || exit -1
126
127         case $NETWORK in
128         elan)   if [ "$PORT" ]; then
129                         echo "$0: NETWORK is elan but PORT is set" 1>&2
130                         exit -1
131                 fi
132                 insmod $PORTALS/linux/qswnal/kqswnal.o
133                 ;;
134         tcp)    if [ -z "$PORT" ]; then
135                         echo "$0: NETWORK is tcp but PORT is not set" 1>&2
136                         exit -1
137                 fi
138                 insmod $PORTALS/linux/socknal/ksocknal.o || exit -1
139                 $ACCEPTOR $PORT
140                 ;;
141         *)      echo "$0: unknown NETWORK '$NETWORK'" 1>&2
142                 exit -1
143                 ;;
144         esac
145
146         $PTLCTL <<- EOF
147         setup $NETWORK
148         mynid $LOCALHOST
149         connect $SERVER $PORT
150         add_uuid self
151         add_uuid mds
152         add_uuid ost
153         add_uuid ldlm
154         quit
155         EOF
156 }
157
158 setup_lustre() {
159         [ -c /dev/obd ] || mknod /dev/obd c 10 241
160
161         insmod $LUSTRE/class/obdclass.o || exit -1
162         insmod $LUSTRE/rpc/ptlrpc.o || exit -1
163         insmod $LUSTRE/ldlm/ldlm.o || exit -1
164         insmod $LUSTRE/ext2obd/obdext2.o || exit -1
165         insmod $LUSTRE/filterobd/obdfilter.o || exit -1
166         insmod $LUSTRE/ost/ost.o || exit -1
167         insmod $LUSTRE/osc/osc.o || exit -1
168         insmod $LUSTRE/obdecho/obdecho.o || exit -1
169         insmod $LUSTRE/mds/mds.o || exit -1
170         insmod $LUSTRE/mdc/mdc.o || exit -1
171         insmod $LUSTRE/llight/llite.o || exit -1
172
173         list_mods
174
175         [ -d /mnt/lustre ] || mkdir /mnt/lustre
176 }
177
178 setup_ldlm() {
179         [ -c /dev/portals ] || mknod /dev/portals c 10 240
180
181         insmod $PORTALS/linux/oslib/portals.o || exit -1
182
183         insmod $LUSTRE/class/obdclass.o || exit -1
184         insmod $LUSTRE/ldlm/ldlm.o || exit -1
185
186         list_mods
187         echo "Press Enter to continue"
188         read
189 }
190
191 DEVNO=0; export DEVNO
192
193 setup_mds() {
194         [ "$SETUP_MDS" = "y" ] || return 0
195
196         if [ -z "$MDSFS" -o -z "$MDSDEV" ]; then
197                 echo "error: setup_mds: MDSFS or MDSDEV unset" 1>&2
198                 return -1
199         fi
200
201         if [ "$1" != "new_fs" -a "$1" != "old_fs" ]; then
202                 echo "usage: setup_mds {new_fs|old_fs}" 1>&2
203                 return -1
204         fi
205
206         $1 ${MDSFS} ${MDSDEV} ${MDSSIZE}
207         MDS=${LOOPDEV}
208
209         MDS_DEVNO=$DEVNO; DEVNO=`expr $DEVNO + 1`
210         
211         $OBDCTL <<- EOF
212         device ${MDS_DEVNO}
213         attach mds
214         setup ${MDS} ${MDSFS}
215         quit
216         EOF
217 }
218
219 setup_ost() {
220         [ "$SETUP_OST" = "y" ] || return 0
221
222         if [ -z "$OSTTYPE" ]; then
223                 echo "error: setup_ost: OSTTYPE unset" 1>&2
224                 return -1
225         fi
226
227         case $OSTTYPE in
228         obdecho)        OBD=
229                         OBDARG=
230                         NEED_FS=n
231                 ;;
232         obdext2)        OBDARG=
233                         NEED_FS=y
234                 ;;
235         obdfilter)      OBDARG=$OSTFS
236                         NEED_FS=y
237                 ;;
238         *)      echo "error: setup_ost: unknown OSTTYPE '$OSTTYPE'" 1>&2
239                 return -1
240                 ;;
241         esac
242
243         if [ "$NEED_FS" = "y" ]; then
244                 [ "$1" ] && DO_FS=$1
245                 if [ -z "$OSTFS" -o -z "$OSTDEV" ]; then
246                         echo "error: setup_ost: OSTFS or OSTDEV unset" 1>&2
247                         return -1
248                 fi
249
250                 if [ "$DO_FS" != "new_fs" -a "$DO_FS" != "old_fs" ]; then
251                         echo "usage: setup_ost {new_fs|old_fs}" 1>&2
252                         return -1
253                 fi
254
255                 $DO_FS ${OSTFS} ${OSTDEV} ${OSTSIZE}
256                 OBD=${LOOPDEV}
257         fi
258
259         OBD_DEVNO=$DEVNO; DEVNO=`expr $DEVNO + 1`
260         OST_DEVNO=$DEVNO; DEVNO=`expr $DEVNO + 1`
261
262         $OBDCTL <<- EOF
263         device ${OBD_DEVNO}
264         attach ${OSTTYPE}
265         setup ${OBD} ${OBDARG}
266         device ${OST_DEVNO}
267         attach ost
268         setup ${OBD_DEVNO}
269         quit
270         EOF
271 }
272
273 setup_server() {
274         setup_mds $1 && setup_ost $1
275 }
276
277 setup_osc() {
278         [ "$SETUP_OSC" != "y" ] && return 0
279
280         OSC_DEVNO=$DEVNO; DEVNO=`expr $DEVNO + 1`
281         $OBDCTL <<- EOF || return $rc
282         device ${OSC_DEVNO}
283         attach osc
284         setup -1
285         quit
286         EOF
287 }
288
289 setup_mount() {
290         [ "$SETUP_MOUNT" != "y" ] && return 0
291
292         if [ -z "$OSCMT" ]; then
293                 echo "error: setup_mount: OSCMT unset" 1>&2
294                 return -1
295         fi
296
297         [ ! -d $OSCMT ] && mkdir $OSCMT
298         mount -t lustre_lite -o device=$OSC_DEVNO none $OSCMT
299 }
300
301 setup_client() {
302         setup_osc && setup_mount
303 }
304
305 DEBUG_ON="echo 0xffffffff > /proc/sys/portals/debug"
306 DEBUG_OFF="echo 0 > /proc/sys/portals/debug"
307
308 debug_server_off() {
309         if [ "$SERVER" != "$LOCALHOST" ]; then
310                 $RSH $SERVER "$DEBUG_OFF"
311         else
312                 $DEBUG_OFF
313         fi
314 }
315
316 debug_server_on() {
317         if [ "$SERVER" != "$LOCALHOST" ]; then
318                 $RSH $SERVER "$DEBUG_ON"
319         else
320                 $DEBUG_ON
321         fi
322 }
323
324 debug_client_off() {
325         $DEBUG_OFF
326 }
327
328 debug_client_on() {
329         $DEBUG_ON
330 }
331
332 cleanup_portals() {
333         $PTLCTL <<- EOF
334         setup tcp
335         disconnect
336         del_uuid self
337         del_uuid mds
338         del_uuid ost
339         del_uuid ldlm
340         quit
341         EOF
342
343         rmmod kqswnal
344         rmmod ksocknal
345         rmmod portals
346 }
347
348 cleanup_lustre() {
349         killall acceptor
350
351         $OBDCTL <<- EOF
352         device 3
353         cleanup
354         detach
355         device 2
356         cleanup
357         detach
358         device 1
359         cleanup
360         detach
361         device 0
362         cleanup
363         detach
364         quit
365         EOF
366
367
368         losetup -d ${LOOP}0
369         losetup -d ${LOOP}1
370         losetup -d ${LOOP}2
371
372         rmmod llite
373         rmmod mdc
374
375         rmmod mds
376         rmmod ost
377         rmmod osc
378         rmmod obdecho
379         rmmod obdfilter
380         rmmod obdext2
381
382         rmmod ldlm
383         rmmod ptlrpc
384         rmmod obdclass
385 }
386
387 cleanup_mds() {
388         [ "$SETUP" -a -z "$SETUP_MDS" ] && return 0
389
390         [ "$SETUP" ] || MDS_DEVNO=0
391
392         $OBDCTL <<- EOF
393         device ${MDS_DEVNO}
394         cleanup
395         detach
396         quit
397         EOF
398 }
399
400 cleanup_ost() {
401         [ "$SETUP" -a -z "$SETUP_OST" ] && return 0
402
403         if [ -z "$SETUP" ]; then
404                 OST_DEVNO=2
405                 OBD_DEVNO=1
406         fi
407
408         $OBDCTL <<- EOF
409         device ${OST_DEVNO}
410         cleanup
411         detach
412         device ${OBD_DEVNO}
413         cleanup
414         detach
415         quit
416         EOF
417 }
418
419 cleanup_server() {
420         cleanup_mds && cleanup_ost
421         DEVNO=0
422 }
423
424 cleanup_mount() {
425         [ "$SETUP" -a -z "$SETUP_MOUNT" ] && return 0
426
427         [ "$OSCMT" ] || OSCMT=/mnt/lustre
428         umount $OSCMT
429 }
430
431 cleanup_osc() {
432         [ "$SETUP" -a -z "$SETUP_OSC" ] && return 0
433
434         [ "$SETUP" ] || OSC_DEVNO=3
435
436         $OBDCTL <<- EOF
437         device ${OSC_DEVNO}
438         cleanup
439         detach
440         quit
441         EOF
442 }
443
444 cleanup_client() {
445         cleanup_mount && cleanup_osc
446         DEVNO=0
447 }