Whamcloud - gitweb
b=19208 Add ext3_get_inode_flags()
[fs/lustre-release.git] / build / lbuild
1 #!/bin/bash
2
3 # vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:
4
5 # this is an alternative FD for stdout, to be used especially when we are
6 # taking stdout from a function as it's return value.  i.e. foo=$(bar)
7 # this is a workaround until a version of bash where we can put xtrace
8 # on a specific FD
9 exec 3>&1; STDOUT=3
10
11 #set -x
12 xtrace="+x"
13 if [[ $SHELLOPTS = *xtrace* ]]; then
14     xtrace="-x"
15 fi
16 shopt -s extdebug
17
18 # include the exit_traps library
19 . ${0%/lbuild}/exit_traps.sh
20
21 # our children should die when we do
22 push_exit_trap "kill -INT -$$ || true" kill_children
23
24 TOPDIR=$PWD
25
26 # CVSROOT is inherited from the environment
27 KERNELDIR=
28 LINUX=
29 LUSTRE=
30 RELEASE=false
31 # XXX - some recent hacking has pretty much neutered this option.
32 #       search through this file (and lbuild.old_school -- but that will
33 #       be going away soon) for "-bb" and see how many places
34 #       simply don't account for this option
35 DO_SRC=true
36 DOWNLOAD=true
37 TAG=
38 CANONICAL_TARGET=
39 TARGET=
40 TARGET_ARCH=$(uname -m)
41 TARGET_ARCHS=
42 TARGET_ARCHS_ALL=$TARGET_ARCH
43 [ "$TARGET_ARCH" = "i686" ] && TARGET_ARCHS_ALL="i686 i586 i386"
44 CONFIGURE_FLAGS=
45 EXTERNAL_PATCHES=
46 EXTRA_VERSION=
47 LUSTRE_EXTRA_VERSION=
48 STAGEDIR=
49 TMPDIR=${TMPDIR:-"/var/tmp"}
50 TIMESTAMP=
51 # XXX - i think these two parameters/arguments/variables need to be
52 #       cleaned up and merged.  they effectively do the same thing
53 REUSERPM=
54 REUSEBUILD=
55 # what does this do exactly?  does it imply no kernel build?
56 NORPM=false
57 LDISKFSRPM=true
58 SKIPLDISKFSRPM="v1_4_* b1_4"
59 SMPTYPES="smp bigsmp default ''"
60 PATCHLESS=false
61 XEN=false
62 LINUXOBJ=
63 DISTRO=
64 KERNELTREE=
65 # default to not adding -lustre- into the kernel RPM package names
66 KERNEL_LUSTRE_NAMING=false
67
68 # patchless build
69 KERNELRPMSBASE=
70 RPMSMPTYPE=
71
72 # from target file
73 SERIES=
74 BASE_ARCHS=
75 BIGMEM_ARCHS=
76 BOOT_ARCHS=
77 JENSEN_ARCHS=
78 SMP_ARCHS=
79 BIGSMP_ARCHS=
80 PSERIES64_ARCHS=
81 UP_ARCHS=
82
83 # not in the target file any more
84 CONFIG=
85
86 DATE=$(date)
87
88 USE_DATESTAMP=1
89 RPMBUILD=
90
91 OLD_SCHOOL=false
92
93 export CC=${CC:-gcc}
94
95 # Readlink is not present on some older distributions: emulate it.
96 readlink() {
97     local path=$1 ll
98
99     if [ -L "$path" ]; then
100         ll="$(LC_ALL=C ls -l "$path" 2> /dev/null)" &&
101         echo "${ll/* -> }"
102     else
103         return 1
104     fi
105 }
106
107 cleanup() {
108
109     true
110 }
111
112 error() {
113     local msg="$1"
114
115     [ -n "$msg" ] && echo -e "\n${0##*/}: $msg" >&3
116
117 }
118
119 fatal() {
120
121     cleanup
122     error "$2"
123     exit $1
124
125 }
126
127 usage() {
128     cat <<EOF
129 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
130
131   -d CVSROOT
132     Specifies the CVS Root to use when pulling files from CVS.  The
133     environment variable \$CVSROOT is used if this option is not
134     present.
135
136   --external-patches=EXTERNAL_PATCHES
137     Directory similar to lustre/lustre/kernel_patches/ that lbuild should
138     look for seres and config files in before looking in the lustre
139     tree.
140
141   --extraversion=EXTRAVERSION
142     Text to use for the rpm release and kernel extraversion.
143
144   --timestamp=TIMESTAMP
145     Date of building lustre in format YYYYMMDDhhmmss
146
147   --reuserpm=DIR
148     Try to reuse old kernel RPMs from DIR
149
150   --reusebuild=DIR
151     Try to reuse old kernel builds from DIR
152
153   --kernelrpm=DIR
154     Path to distro kernel RPM collection
155
156   --ccache
157     Use ccache
158
159   --norpm
160     Do not build RPMs (compile only mode)
161
162   --patchless
163     Build lustre client only
164
165   --distro=DISTRO
166     Which distro using. Autodetect by default
167
168   --kerneldir=KERNELDIR
169     Directory containing Linux source tarballs referenced by target
170     files.
171
172   --kerneltree=KERNELTREE
173     Directory containing dirs with Linux source tarballs referenced by target
174     files. Dir names in format kernel version ('2.6.9', etc.)
175
176   --linux=LINUX --with-linux=LINUX
177     Directory of Linux kernel sources.  When this option is used, only
178     Lustre modules and userspace are built.
179
180   --lustre=LUSTRE
181     Path to an existing lustre source tarball to use instead of
182     pulling from CVS.
183
184   --nodownload
185     Do not try to download a kernel from downloads.lustre.org
186
187   --nosrc
188     Do not build a .src.rpm, a full kernel patch, or a patched kernel
189     tarball.
190
191   --ldiskfs
192     Do ldiskfs RPM. Now true by default
193
194   --publish
195     Unused.
196
197   --release
198     Specifies that the files generated do not include timestamps, and
199     that this is an official release.
200
201   --src
202     Build a .src.rpm, a full kernel patch, and a patched kernel tarball.
203
204   --stage=DIR
205     Directory used to stage packages for release.  RPMs will be placed
206     more or less in DIR/<target>-<arch>, and the tarball will be
207     placed in DIR.
208
209   --tag=TAG
210     A CVS branch/tag name to build from when pulling from CVS.
211
212   --target=TARGET
213     The name of the target to build.  The available targets are listed
214     below.
215
216   --target-archs=TARGET_ARCHS
217     A (space delimited) list of architectures to build.  By default,
218     all of the archs supported by the TARGET will be built, in
219     addition to a .src.rpm.  This option can limit those, for machines
220     that can only build certain archs or if you only want a certain
221     arch built (for testing, or a one-off kernel).
222
223     Also note that by using a non-"base" arch (eg, i386) only kernels
224     will be built - there will be no lustre-lite-utils package.
225
226   --disable-datestamp
227     Prevents the datestamp flag (-D) from being passed to cvs for
228     checkouts. This is a workaround for a problem encountered when
229     using lbuild with tinderbox.
230
231   --xen
232     Builds a Xen domX kernel.
233
234 EOF
235
236 #   list_targets
237
238     fatal "$1" "$2"
239 }
240
241 # canonicalize a relative path
242 canon_path() {
243     local PATH="$1"
244
245     if [ ! -d "$PATH" ]; then
246         return 1
247     fi
248
249     pushd "$PATH" >/dev/null || return 1
250     local CANONPATH=$PWD
251     popd >/dev/null
252
253     echo "$CANONPATH"
254     return 0
255 }
256
257 check_options() {
258
259     if [ "$LUSTRE" ]; then
260         [ -r "$LUSTRE" ] || \
261             usage 1 "Could not find Lustre source tarball '$LUSTRE'."
262     else
263         [ "$CVSROOT" ] || \
264             usage 1 "Either specify a CVS Root with -d, or a Lustre source tarball with --lustre."
265         [ "$TAG" ] || \
266             usage 1 "A branch/tag name must be specified with --tag when not building from a tarball."
267     fi
268
269     if [ -z "$LINUX" ]; then
270         [ "$KERNELDIR" -o "$KERNELTREE" ] || \
271             usage 1 "A kernel directory must be specified with --kerneldir or --kerneltree."
272
273         [ -d "$KERNELDIR" -o -d "$KERNELTREE" ] || \
274             usage 1 "$KERNELDIR and $KERNELTREE are not a directory."
275
276         if ! $RELEASE; then
277             [ "$TAG" ] || \
278                 usage 1 "When building a snapshot, a tag name must be used."
279         fi
280
281         [ "$TARGET" ] || usage 1 "A target must be specified with --target."
282 #       TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target"
283 #       [ -r "$TARGET_FILE" ] || \
284 #               usage 1 "Target '$TARGET' was not found."
285     fi
286
287     case $TARGET in
288         2.6-rhel5)
289             CANONICAL_TARGET="rhel5"
290             ;;
291         2.6-rhel4)
292             CANONICAL_TARGET="rhel-2.6"
293             ;;
294         2.6-suse)
295             CANONICAL_TARGET="sles-2.6"
296             ;;
297         2.6-sles10)
298             CANONICAL_TARGET="sles10-2.6"
299             ;;
300         2.6-sles11)
301             CANONICAL_TARGET="sles11"
302             ;;
303         2.6-oel5)
304             CANONICAL_TARGET="oel5"
305             ;;
306         hp_pnnl-2.4)
307             CANONICAL_TARGET="hp-pnnl-2.4"
308             ;;
309         2.6-vanilla \
310             | suse-2.4.21-2 \
311             | rh-2.4 \
312             | rhel-2.4 \
313             | sles-2.4 \
314             | 2.6-patchless)
315                 CANONICAL_TARGET="$TARGET"
316                 ;;
317     esac
318
319     local timestampnodig=$(echo $TIMESTAMP | sed -e s/[0-9]*//g)
320     [ "$timestampnodig" = "" ] || TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M%S")
321     local timestamplength="${#TIMESTAMP}"
322     if [ $timestamplength -eq 12 ]; then
323         TIMESTAMP="${TIMESTAMP}00"
324     elif [ $timestamplength -ne 14 ]; then
325         TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M%S")
326     fi
327
328     RPMBUILD=$(which rpmbuild 2>/dev/null | head -1)
329     if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
330         RPMBUILD=$(which rpm 2>/dev/null | head -1)
331         if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
332             usage 1 "Could not find binary for making rpms (tried rpmbuild and rpm)."
333         fi
334     fi
335
336     if [ -n "$CCACHE" ]; then
337         which "$DISTCC" &>/dev/null && export DISTCC RPM_BUILD_NCPUS
338
339         if which "$CCACHE" &>/dev/null; then
340             local ccache=$(which "$CCACHE")
341             local bindir="$TOPDIR/bin"
342
343             if [ ! -d $bindir ]; then
344                 mkdir -p $bindir || fatal 1 "error trying to create $bindir"
345             else
346                 rm ${bindir}/* > /dev/null 2>&1 || true
347             fi
348             ln -s "$ccache" ${bindir}/ccache
349             ln -s "$ccache" ${bindir}/cc
350             ln -s "$ccache" ${bindir}/$CC
351             export PATH=$bindir:$PATH
352             export CCACHE && export CC="ccache $CC"
353             # zero the cache so we can see how effective we are being with it
354             echo -n "ccache "
355             ccache -z
356
357             # get some ccache stats when we are done
358             push_exit_trap '[ -n "$CCACHE" ] && ccache -s' "ccache_summary"
359             # should remove the ccache trap if lbuild is interrupted
360             trap 'echo "Received an INT TERM or HUP signal, terminating."; delete_exit_trap "ccache_summary"; exit 1' INT TERM HUP
361         fi
362     fi
363
364     [ -z "$DISTRO" ] && DISTRO=$(autodetect_distro)
365
366     return 0
367
368 }
369
370 # autodetect used Distro
371 autodetect_distro() {
372
373     local name
374     local version
375
376     if [ -f /etc/SuSE-release ]; then
377         name=sles
378         version=$(grep ^VERSION /etc/SuSE-release)
379         version=${version#*= }
380     elif [ -f /etc/redhat-release ]; then
381         name=$(head -1 /etc/redhat-release)
382         version=$(echo "$distroname" |
383                   sed -e 's/^[^0-9.]*//g' | sed -e 's/[ \.].*//')
384     fi
385     if [ -z "$name" -o -z "$version" ]; then
386         fatal 1 "I don't know how to determine distro type/version.\nEither update autodetect_distro() or use the --distro argument"
387     fi
388
389     echo ${name}${version}
390     return 0
391
392 }
393
394 uniqify() {
395
396     echo $(echo "$*" | xargs -n 1 | sort -u)
397
398 }
399
400 download_srpm() {
401     local target=$1
402     local srpm=$2
403     local force="${3:-false}"
404
405     if $force || [ ! -r "$KERNELDIR/$srpm" ] ||
406        [ ! -s "$KERNELDIR/$srpm" ]; then
407         if $DOWNLOAD; then
408             local location="http://downloads.lustre.org/public/kernels/$target/old"
409             echo "Downloading $location/$srpm..."
410             if ! wget -nv "$location/$srpm" -O "$KERNELDIR/$srpm" 2>&1 ||
411                [ ! -s "$KERNELDIR/$srpm" ]; then
412                 rm -f $KERNELDIR/$srpm
413                 fatal 1 "Could not download target $target's kernel SRPM $srpm from $location."
414             fi
415         else
416             fatal 1 "$srpm not found in directory $KERNELDIR."
417         fi
418     fi
419
420 }
421
422 download_file() {
423     local from="$1"
424     local to="$2"
425     local force="$3"
426
427     local file=${from##*/}
428
429     if [ -d $to ]; then
430         to="$to/$file"
431     fi
432
433     local semaphore="$to-downloading"
434
435     is_downloading() {
436         if [ ! -f $semaphore ]; then
437             return 1
438         fi
439
440         # make sure the download has not been aborted
441         local now=$(date +%s)
442         local file_mtime=$(stat -c %Y "$to")
443         local staleness=$((now - file_mtime))
444         # let's assume an active download will write at least once a minute
445         if [ $staleness -gt 60 ]; then
446             return 1
447         fi
448
449         return 0
450     }
451
452     is_downloaded() {
453         # if the semaphore file exists, the file is either still downloading
454         # or a download was aborted and we cannot trust the target file
455         if [ -f $semaphore ]; then
456             return 1
457         fi
458
459         if ! is_downloading && [ -r "$to" ] && [ -s "$to" ]; then
460             return 0
461         fi
462
463         return 1
464     }
465
466     if $force || ! is_downloaded; then
467         if is_downloading; then
468             echo "Somebody else is downloading $from..."
469             while is_downloading; do
470                 echo "Waiting for $to to finish downloading"
471                 sleep 60
472             done
473             if is_downloaded; then
474                 return 0
475             else
476                 echo "The download we were waiting for seems to have been aborted"
477             fi
478
479         fi
480
481         if $DOWNLOAD; then
482             echo "Downloading $from..."
483             # flag others so they don't try to download also
484             push_exit_trap "rm -f $to $semaphore" "download"
485             touch $semaphore
486             if ! wget -nv "$from" -O "$to" || [ ! -s "$to" ]; then
487                 # the trap will remove the files via the fatal below
488                 fatal 1 "Could not download ${to##*/} from ${from%%/*}/."
489             fi
490             rm -f $semaphore
491             delete_exit_trap "download"
492         else
493             fatal 1 "${to##*/} not found in directory ${to%/*}."
494         fi
495     fi
496
497     return 0
498
499 }
500
501 download_ofed() {
502     local force="${1:-false}"
503
504     local location="http://downloads.lustre.org/public/OFED/"
505
506     local Mmv daily
507     if [[ $OFED_VERSION = daily-* ]]; then
508         OFED_VERSION=${OFED_VERSION/daily-/}
509         Mmv=${OFED_VERSION%%-*}
510         daily=${OFED_VERSION##$Mmv-}
511         location="http://www.openfabrics.org/downloads/OFED/ofed-${Mmv}-daily/"
512         # find the filename for the version for the date specified
513         OFED_VERSION=$(curl -s "$location" | sed -nre "/${daily}-/s/.*href=\"OFED-([0-9]+\.[0-9]+-${daily}-[0-9]{4,4}).tgz.*$/\1/p" | tail -1)
514         if [ -z "$OFED_VERSION" ]; then
515             fatal 1 "Could not determine the filename of the OFED snapshot for ${daily}"
516         fi
517     fi
518
519     if [ -z "$OFED_VERSION" -o "$OFED_VERSION" = "inkernel" ]; then
520         return 0
521     fi
522
523     local file="OFED-${OFED_VERSION}.tgz"
524     download_file "$location/$file" "$KERNELTREE" "$force"
525
526 }
527
528 download_rdac() {
529     local force="${1:-false}"
530
531     local location="http://downloads.lustre.org/public/RDAC/"
532
533     if [ -z "$RDAC_VERSION" -o "$RDAC_VERSION" = "inkernel" ]; then
534         return 0
535     fi
536
537     local file="rdac-LINUX-${RDAC_VERSION}-source.tar.gz"
538     download_file "$location/$file" "$KERNELTREE" "$force"
539
540 }
541
542 download_mptlinux() {
543     local force="${1:-false}"
544
545     local location="http://downloads.lustre.org/public/MPTLINUX/"
546
547     if [ -z "$MPTLINUX_VERSION" -o "$MPTLINUX_VERSION" = "inkernel" ]; then
548         return 0
549     fi
550
551     file="MPTLINUX_RHEL5_SLES10_PH15-${MPTLINUX_VERSION}.zip"
552     download_file "$location/$file" "$KERNELTREE" "$force"
553
554 }
555
556 load_target() {
557
558     EXTRA_VERSION_save="$EXTRA_VERSION"
559     for patchesdir in "$EXTERNAL_PATCHES" \
560                       "$TOPDIR/lustre/lustre/kernel_patches"; do
561         TARGET_FILE="$patchesdir/targets/$TARGET.target"
562         [ -r "$TARGET_FILE" ] && break
563     done
564     [ -r "$TARGET_FILE" ] || fatal 1 "Target $TARGET was not found."
565
566     echo "Loading target config file $TARGET.target..."
567
568     # if the caller specified an OFED_VERSION it should override whatever
569     # the target file specifies
570     local env_OFED_VERSION="$OFED_VERSION"
571
572     . "$TARGET_FILE"
573
574     if [ -n "$env_OFED_VERSION" ]; then
575         OFED_VERSION="$env_OFED_VERSION"
576     fi
577
578     # doesn't make any sense to build OFED for xen domX's
579     if $XEN; then
580         OFED_VERSION=""
581     fi
582
583     # XXX - set_rpm_smp_type is an ugly undeterministic hack.  it needs to
584     #       go away and the target just specify the $RPMSMPTYPE
585     [ -z "$RPMSMPTYPE" ] && set_rpm_smp_type
586
587     # CC might have been overwriten in TARGET_FILE
588     if [[ $CC != ccache\ * ]] && which "$CCACHE" &>/dev/null; then
589         export CCACHE && export CC="ccache $CC"
590     fi
591
592     if [ ! "$KERNELTREE" = "" ] && [ -d "$KERNELTREE" ]; then
593         KERNELDIR="$KERNELTREE/${lnxmaj}"
594         [ -d "$KERNELDIR" ] || mkdir "$KERNELDIR"
595     fi
596
597     # verify the series is available
598     if [ "$SERIES" ]; then
599         for series in $SERIES; do
600             for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches"; do
601                 [ -r "$patchesdir/series/$series" ] && continue 2
602             done
603             fatal 1 "Target $TARGET's series $SERIES could not be found.\nSearched:\n\t$EXTERNAL_PATCHES/series\n\t$TOPDIR/lustre/lustre/kernel_patches/series."
604         done
605     fi
606
607     # set the location of the .config file
608     local XENPOSTFIX=""
609     if $XEN; then
610         XENPOSTFIX="-xen"
611     fi
612
613     if [ -f $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET-$TARGET_ARCH.config ]; then
614         CONFIG_FILE="$TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET$XENPOSTFIX-$TARGET_ARCH${RPMSMPTYPE:+-}${RPMSMPTYPE}.config"
615     fi
616
617     local lnxrelnew=${lnxrel//-/_}
618
619     # remember the EXTRA_VERSION before we diddle it here
620     # XXX - we really should not diddle with any values read in from the
621     #       target file.  if we want to modify a value, we should create
622     #       a new variable.
623     PRISTINE_EXTRA_VERSION=$EXTRA_VERSION
624
625     if ! $PATCHLESS && [ ! -f "$CONFIG_FILE" ]; then
626         fatal 1 "Config file for target $TARGET missing from $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/."
627     fi
628
629     if [ "$EXTRA_VERSION_save" ]; then
630         EXTRA_VERSION="$EXTRA_VERSION_save"
631     elif ! $RELEASE; then
632         # if there is no patch series, then this is not a lustre specific
633         # kernel.  don't make it look like one
634         if [ -n "$SERIES" ]; then
635             #remove the @VERSION@ (lustre version)
636 #            EXTRA_VERSION=$(echo $EXTRA_VERSION | sed -e "s/\(.*_lustre\)\..*/\1/")
637 #            EXTRA_VERSION="${EXTRA_VERSION}-${TAG}.${TIMESTAMP}"
638             ! ( $PATCHLESS ) && EXTRA_VERSION="${EXTRA_VERSION}.${TIMESTAMP}"
639         fi
640     fi
641     # EXTRA_VERSION=${EXTRA_VERSION//-/_}
642
643     ALL_ARCHS="$BASE_ARCHS $BIGMEM_ARCHS $BOOT_ARCHS $JENSEN_ARCHS $SMP_ARCHS $BIGSMP_ARCHS $PSERIES64_ARCHS $UP_ARCHS"
644
645     BUILD_ARCHS=
646     for arch in $(uniqify "$ALL_ARCHS"); do
647         if [ -z "$TARGET_ARCHS" ] ||
648            [[ \ $TARGET_ARCHS\  = *\ $arch\ * ]]; then
649             BUILD_ARCHS="$BUILD_ARCHS $arch"
650         fi
651     done
652     [ "$BUILD_ARCHS" ] || usage 1 "No available target archs to build."
653     echo "Building for: $BUILD_ARCHS"
654 }
655
656 tarflags() {
657     local file="$1"
658
659     case "$file" in
660         '')
661             fatal 1 "tarflags(): File name argument missing."
662             ;;
663         *.tar.gz | *.tgz)
664             echo 'zxf'
665             ;;
666         *.tar.bz2)
667             echo 'jxf'
668             ;;
669         *.tar)
670             echo 'xf'
671             ;;
672         *)
673             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
674             ;;
675     esac
676
677 }
678
679 untar() {
680     local file="$1"
681
682     echo "Untarring ${file##*/}..."
683     tar $(tarflags "$file") "$file"
684
685 }
686
687 unpack_ofed() {
688
689     if ! untar "$KERNELTREE/OFED-${OFED_VERSION}.tgz"; then
690         return 1
691     fi
692     [ -d OFED ] || ln -sf OFED-[0-9].[0-9]* OFED
693
694 }
695
696 unpack_rdac() {
697
698     if ! untar "$KERNELTREE/rdac-LINUX-${RDAC_VERSION}-source.tar.gz"; then
699         return 1
700     fi
701     [ -d rdac-LINUX ] || ln -sf rdac-LINUX-[0-9][0-9].* rdac-LINUX
702
703 }
704
705 unpack_mptlinux() {
706
707     if ! unzip -p $KERNELTREE/MPTLINUX_RHEL5_SLES10_PH15-4.16.00.00-2.zip | tar xzvf - srpms-2/mptlinux-4.16.00.00-2.src.rpm; then
708         return 1
709     fi
710     mv srpms-2/mptlinux-4.16.00.00-2.src.rpm .
711
712 }
713
714 unpack_lustre() {
715
716     if [ -z "$LUSTRE" ]; then
717         local DATESTAMP=""
718
719         if [ -n "$USE_DATESTAMP" ]; then
720             DATESTAMP="-D '$DATE'"
721         fi
722
723         local DIRNAME="lustre-$TAG-$TIMESTAMP"
724
725         cvs -d "$CVSROOT" -qz3 co $DATESTAMP -d "$DIRNAME" lustre || \
726             fatal 1 "There was an error checking out toplevel Lustre from CVS."
727         pushd "$DIRNAME" > /dev/null
728         ./lustrecvs "$TAG" || \
729             fatal 1 "There was an error checking out Lustre/Portals/Build from CVS."
730         echo "Creating lustre tarball..."
731         sh autogen.sh || fatal 1 "There was an error running autogen.sh."
732         ./configure --disable-{modules,utils,liblustre,tests,doc} || \
733             fatal 1 "There was an error running ./configure to create makefiles."
734         make dist || fatal 1 "There was an error running 'make dist'."
735         LUSTRE=$PWD/lustre-*.tar.gz
736         popd > /dev/null
737     fi
738
739     untar "$LUSTRE" || fatal 1 "Error unpacking Lustre tarball"
740     [ -d lustre ] || ln -sf lustre-[0-9].[0-9]* lustre
741
742 }
743
744 do_patch_linux() {
745
746     local do_patch=${1:-true}
747
748     FULL_PATCH="$PWD/lustre-kernel-${TARGET}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}.patch"
749     [ -f "$FULL_PATCH" ] && rm -f "$FULL_PATCH"
750     $do_patch && pushd linux >/dev/null
751     for series in $SERIES; do
752         echo -n "Applying series $series:"
753         for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches"; do
754             [ -r "$patchesdir/series/$series" ] || continue
755             SERIES_FILE="$patchesdir/series/$series"
756             for patch in $(<"$SERIES_FILE"); do
757                 echo -n " $patch"
758                 PATCH_FILE="$patchesdir/patches/$patch"
759                 [ -r "$PATCH_FILE" ] || \
760                     fatal 1 "Patch $patch does not exist in Lustre tree."
761                 cat "$PATCH_FILE" >> "$FULL_PATCH" || {
762                     rm -f $FULL_PATCH
763                     fatal 1 "Error adding patch $patch to full patch."
764                 }
765                 if $do_patch; then
766                     patch -s -p1 < "$PATCH_FILE" 2>&1 || {
767                         rm -f $FULL_PATCH
768                         fatal 1 "Error applying patch $patch."
769                     }
770                 fi
771             done
772             break
773         done
774         echo
775     done
776     $do_patch && popd >/dev/null
777     echo "Full patch has been saved in ${FULL_PATCH##*/}."
778
779 }
780
781 build_lustre() {
782     local linux="$1"
783     local linuxobj="$2"
784
785     cp "$LUSTRE" SOURCES
786
787     pushd lustre >/dev/null
788
789     echo "Building Lustre RPMs for: $BUILD_ARCHS..."
790     local targets arch
791     for arch in $BUILD_ARCHS; do
792         targets="--target $arch $targets"
793     done
794
795     local confoptions="--with-linux=${linux}"
796     if $PATCHLESS; then
797         confoptions="--with-linux=${linux} --disable-server"
798     fi
799     if [ "$linuxobj" != "" ]; then
800         confoptions="$confoptions --with-linux-obj=${linuxobj}"
801     fi
802
803     ./configure $confoptions ${CONFIGURE_FLAGS} 2>&1
804     if [ "$?" != "0" ]; then
805         local saved_config="../config.log.$(date +%s)"
806         cp config.log $saved_config
807         chmod a+r $saved_config
808         echo "Saved config.log is at $saved_config"
809         cat /proc/mounts
810         ls -l /proc/$$
811         pwd
812         echo "config.log contents:"
813         cat config.log
814         popd
815         return 255
816     fi
817
818     gen_lustre_version
819
820     # hack. Somebody move build/lustre.spec to lustre.spec for b1_6
821     local lustre_spec
822     [ -f lustre.spec ] && lustre_spec=lustre.spec
823     [ -f build/lustre.spec ] && lustre_spec=build/lustre.spec
824
825     [ -f "$lustre_spec" ] && sed \
826         -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
827         < $lustre_spec \
828         > ../lustre.spec
829
830     local rpmbuildopt='-bb'
831     if $NORPM; then
832         rpmbuildopt='-bc'
833         echo NORPM mode. Only compiling.
834     fi
835
836     # convert the $PATCHLESS boolean to an empty/no-empty boolean
837     # as silly as this seems, it makes the syntax of the rpmbuild command
838     # simpler and not need an eval to deal with the quotes in the quotes
839     local is_patchless=""
840     if $PATCHLESS; then
841         is_patchless="yes"
842     fi
843     $RPMBUILD $targets $rpmbuildopt ../lustre.spec \
844         ${is_patchless:+--define "lustre_name lustre-client"} \
845         --define "_tmppath $TMPDIR" \
846         --define "_topdir $TOPDIR" 2>&1 || \
847         fatal 1 "Error building rpms for $BUILD_ARCHS."
848
849     popd >/dev/null
850     ( $(skeep_ldiskfs_rpm $TAG) ) && return
851
852     pushd lustre/ldiskfs || return 255
853     make dist 2>&1
854     if [ "$?" != "0" ]; then
855         popd
856         return 255
857     fi
858     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
859
860     gen_lustre_version
861
862     local ldiskfs_spec=lustre-ldiskfs.spec
863     [ -f "$ldiskfs_spec" ] && sed \
864     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
865     < $ldiskfs_spec \
866     > ../lustre-ldiskfs.spec
867
868     if ! $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
869         --define "_tmppath /var/tmp" \
870         --define "_topdir $TOPDIR" 2>&1; then
871         popd
872         return 255
873     fi
874
875     if $DO_SRC; then
876         if ! $RPMBUILD -bs ../lustre-ldiskfs.spec \
877             --define "_tmppath /var/tmp" \
878             --define "_topdir $TOPDIR" 2>&1; then
879             popd
880             return 255
881         fi
882     fi
883     popd
884
885 }
886
887 stage() {
888
889     [ "$STAGEDIR" ] || return 0
890
891     for arch in $BUILD_ARCHS; do
892         rpmdir="${STAGEDIR}/${CANONICAL_TARGET}-${arch}"
893         echo "${0##*/}: Copying RPMs into ${rpmdir}"
894         mkdir -p "${rpmdir}"
895         cp -v RPMS/${arch}/*.rpm "${rpmdir}"
896         if [ -d RPMS/noarch ]; then
897             cp -v RPMS/noarch/*.rpm "${rpmdir}"
898         fi
899     done
900
901     cp -v "$LUSTRE" "$STAGEDIR"
902
903 }
904
905 #check if we need to build separate ldiskfs RPM
906 skeep_ldiskfs_rpm() {
907         local tag="$1"
908
909         local skip=false
910
911         if ! $LDISKFSRPM; then
912             skip=true
913         elif $PATCHLESS; then
914             skip=true
915         else
916             for skiptag in $SKIPLDISKFSRPM; do
917                 [[ $tag == $skiptag ]] && skip=true && break
918             done
919         fi
920
921         pushd $TOPDIR/BUILD/lustre-[1-9]* >/dev/null
922         grep -q '^SERVER_TRUE[ \t]=[ \t]#$' autoMakefile && skip=true
923         popd >/dev/null
924
925         echo $skip
926
927 }
928
929 #generate LUSTRE_EXTRA_VERSION from EXTRA_VERSION
930 gen_lustre_version() {
931
932     LUSTRE_EXTRA_VERSION="${lnxmaj}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}${TARGET_DELIMITER}${RPMSMPTYPE}"
933     LUSTRE_EXTRA_VERSION=${LUSTRE_EXTRA_VERSION//-/_}
934
935 }
936
937 set_rpm_smp_type() {
938
939     local infact_arch="${TARGET_ARCH}"
940
941     RPMSMPTYPE=""
942     [ "$infact_arch" == "i586" ] && infact_arch="i686"
943
944     local smp_type
945     for smp_type in $SMP_ARCHS; do
946         [ $infact_arch == $smp_type ] && RPMSMPTYPE=smp && break
947     done
948
949     for smp_type in $BIGSMP_ARCHS; do
950         [ $infact_arch == $smp_type ] && RPMSMPTYPE=bigsmp && break
951     done
952
953     for smp_type in $DEFAULT_ARCHS; do
954         [ $infact_arch == $smp_type ] && RPMSMPTYPE=default && break
955     done
956
957 }
958
959 # This function takes a linux source pool and digs out the linux release
960 # from it
961 find_linux_release() {
962     local SRCDIR="$1"
963
964     local LINUXRELEASEHEADER=$SRCDIR/include/linux/version.h
965     if [ -s $SRCDIR/include/linux/utsrelease.h ]; then
966         LINUXRELEASEHEADER=$SRCDIR/include/linux/utsrelease.h
967     fi
968
969     sed -ne 's/#define UTS_RELEASE "\(.*\)"$/\1/p' $LINUXRELEASEHEADER
970
971 }
972
973 # XXX this needs to be re-written as a wrapper around find_rpm
974 #     or just gotten rid of.  :-)
975 find_linux_rpm() {
976     local prefix="$1"
977     local delimiter=${2:-"-"}
978
979     local pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
980     [ -d $pathtorpms ] || return 255
981
982     local kernelbinaryrpm rpmfile
983     local wanted_kernel="${lnxmaj}${delimiter}${lnxrel}"
984
985     local arch ret=1
986     for arch in $TARGET_ARCHS_ALL; do
987         local found_rpm="" rpm
988         for rpm in ${pathtorpms}/${arch}/*.rpm; do
989             if rpm -q --provides -p "$rpm" 2>&3 | grep -q "kernel${prefix} = $wanted_kernel" 2>&3; then
990
991                 found_rpm="$rpm"
992                 ret=0
993                 break
994             fi
995         done
996         [ -f "$found_rpm" ] && break
997     done
998
999     echo "$found_rpm"
1000     return $ret
1001
1002 }
1003
1004 # unpack kernel(/source/devel) RPM
1005 #
1006 # This function and it's setting of $LINUX and $LINUXOBJ is a total hack that
1007 # needs to completely refactored.  It completely ingores that $BUILD_ARCHS may
1008 # contain a list of arches for which rpmbuild commands (including the one for
1009 # lustre itself)
1010 unpack_linux_devel_rpm() {
1011     local kernelrpm="${1}"
1012     # it's worth noting that neither sles10 nor rhel5 appear to use their
1013     # extra_version delimiter for the dirname under /usr/src, so we could
1014     # probably just get rid of this parameter
1015     local delimiter=${2:-"-"}
1016
1017     [ -f "$kernelrpm" ] || return 255
1018     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused || return 255
1019
1020     pushd $TOPDIR/reused &>/dev/null || return 255
1021
1022     if ! rpm2cpio < "$kernelrpm" | cpio -id > /dev/null 2>&1; then
1023         return 255
1024     fi
1025
1026     # call a distro specific hook, if available
1027     if type -p unpack_linux_devel_rpm-$DISTRO; then
1028         unpack_linux_devel_rpm-$DISTRO "$kernelrpm"
1029     fi
1030
1031     popd &>/dev/null
1032
1033     find_linux_devel_paths $TOPDIR/reused
1034
1035     return 0
1036
1037 }
1038
1039 # XXX - this rhel/sles goop needs abstracting out into the
1040 #       lbuild-{rhel5,sles10} method files
1041 find_linux_devel_paths() {
1042     local path="$1"
1043
1044     local RC=0
1045
1046     pushd $path
1047         # RHEL-style and SLES-style rpms
1048         # XXX - until bug 19336 cleans this up, we need to extricate the
1049         #       ${lnxmin}- from the $lnxrel
1050         local paths="kernels/${lnxmaj}${lnxmin}${delimiter}${lnxrel}-${TARGET_ARCH} linux-${lnxmaj}${lnxmin}${delimiter}${lnxrel##${lnxmin#.}-}"
1051
1052         local path
1053         for path in $paths; do
1054             local src='usr/src'
1055
1056             if [ -d "$src/$path/" ]; then
1057                 LINUX="$(pwd)/$src/$path"
1058             fi
1059             # SLES has a separate -obj tree
1060             if [ -d "$src/${path}-obj" ]; then
1061                 local src="$src/${path}-obj"
1062                 local objects="$TARGET_ARCH/$RPMSMPTYPE"
1063
1064                 # Novell, are you *TRYING* to make life hard for me?
1065                 if [ -d "$src/powerpc" ]; then
1066                     objects="powerpc/$TARGET_ARCH"
1067                 elif [ $TARGET_ARCH == 'i686' ]; then
1068                     objects="i386/$RPMSMPTYPE"
1069                 fi
1070
1071                 LINUXOBJ="$(pwd)/$src/$objects"
1072             fi
1073         done
1074         if [ -z "$LINUX" ]; then
1075             RC=255
1076         else
1077             # dig out the release version
1078             LINUXRELEASE=$(find_linux_release ${LINUXOBJ:-$LINUX})
1079             if [ -z "$LINUXRELEASE" ]; then
1080                 echo "Failed to find linux release in ${LINUXOBJ:-$LINUX}"
1081                 RC=255
1082             fi
1083         fi
1084     popd
1085     return $RC
1086 }
1087
1088 build_kernel_ib() {
1089     local linux="$1"
1090
1091     # build kernel-ib{,-devel}
1092     local K_SRC="K_SRC"
1093     # ofed 1.3 had a bug in the rpm spec
1094     if [ "$OFED_VERSION" = "1.3" ]; then
1095         K_SRC="KSRC"
1096     fi
1097
1098     local OFED_CORE="--with-core-mod --with-ipoib-mod --with-sdp-mod --with-user_mad-mod --with-user_access-mod --with-addr_trans-mod --with-rds-mod --with-qlgc_vnic-mod --with-madeye-mod"
1099     local OFED_HARDWARE="--with-mthca-mod --with-mlx4-mod --with-mlx4_en-mod --with-cxgb3-mod --with-nes-mod"
1100     # some I/B drivers are architecture dependent and kernel-ib's configure
1101     # does not figure it out for us ~sigh~
1102     case "$TARGET_ARCH" in
1103         ppc64)
1104             OFED_HARDWARE="$OFED_HARDWARE --with-ehca-mod"
1105             ;;
1106     esac
1107     # we're no longer shipping the OFED iSCSI
1108     #OFED_ISCSI="--with-srp-mod --with-srp-target-mod"
1109     ## ISER module has no backport support as of OFED 1.5 (i.e. only builds on
1110     ##kernels >= 2.6.30)
1111     #if [[ $OFED_VERSION = 1.[0-4]* ]]; then
1112     #   OFED_ISCSI="$OFED_ISCSI --with-iser-mod"
1113     #fi
1114
1115     # assume we are just rebuilding the SRPM
1116     local BUILD_TYPE=${BUILD_TYPE:-"--rebuild"}
1117     local SOURCE="${TOPDIR}/OFED/SRPMS/ofa_kernel-*.src.rpm"
1118
1119     # but switch to building from the SPEC if we need to apply patches
1120     if ls ${TOPDIR}/lustre/build/patches/ofed/* >/dev/null; then
1121         BUILD_TYPE="-bb"
1122         rpm --define "_topdir ${TOPDIR}" -ivh $SOURCE
1123         SOURCE="${TOPDIR}/SPECS/ofa_kernel.spec"
1124         local file ed_fragment1 ed_fragment2 n=1
1125         for file in ${TOPDIR}/lustre/build/patches/ofed/*; do
1126             ed_fragment1="$ed_fragment1
1127 Patch$n: ${file%%*/}"
1128             ed_fragment2="$ed_fragment2
1129 %patch$n -p0"
1130             cp $file ${TOPDIR}/SOURCES
1131             let n=$n+1
1132         done
1133
1134         ed $SOURCE <<EOF
1135 /^Source: /a
1136 $ed_fragment1
1137 .
1138 /^%setup /a
1139 $ed_fragment2
1140 .
1141 wq
1142 EOF
1143     fi
1144
1145     local linuxrelease=$(find_linux_release "$linux")
1146     if ! $RPMBUILD $BUILD_TYPE --define 'build_kernel_ib 1' --define 'build_kernel_ib_devel 1' \
1147                   --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
1148                   --define "KVERSION ${linuxrelease}" \
1149                   --define "$K_SRC ${linux}" \
1150                   --define "LIB_MOD_DIR /lib/modules/${linuxrelease}/updates" \
1151                   ${OFA_KERNEL_RELEASE:+--define "_release $OFA_KERNEL_RELEASE"} \
1152                   --define "configure_options --without-quilt $OFED_CORE $OFED_HARDWARE $OFED_ISCSI" \
1153                   ${SOURCE} 2>&1; then
1154         fatal 1 "Error building kernel-ib"
1155     fi
1156
1157 }
1158
1159 store_for_reuse() {
1160         local articles="$1"
1161         local module="$2"
1162         local location="$3"
1163         local signature="$4"
1164         local use_links="$5"
1165
1166         local linkflag=""
1167         if $use_links; then
1168             linkflag="l"
1169         fi
1170
1171         location="$location"/"$signature"/"$module"
1172         mkdir -p "$location"
1173         # the cleanup script removes any directory that doesn't have a
1174         # .lastused, so let's try to prevent that as soon as we can
1175         # this solution still slightly racy with the cleanup script
1176         # but the race is a lot tighter now
1177         touch -t 197001010000 "$location/.lastused"
1178         ## use eval/echo here to make sure shell expansions are performed
1179         #if ! cp -a${linkflag} $(eval echo $articles) "$location"; then
1180         local article
1181         for article in $(eval echo $articles); do
1182             if ! cp -a${linkflag} "$article" "$location"; then
1183                 error "Failed to copy \"$article\" to \"$location\" in store_for_reuse()"
1184                 # rename the cache location so that it's not cached
1185                 # product, but is around for analysis
1186                 mv "$location"{,-bad-$(date +%s)} ||
1187                     error "failed to clean up a failed cache attempt" \
1188                           "in \"$location\" -- manual cleanup will be" \
1189                           "necessary"
1190                 return 1
1191             fi
1192         done
1193
1194         # flag the cache as complete (i.e. in case lbuild was previously
1195         # interrupted while caching)
1196         touch "$location/.lastused"
1197
1198         return 0
1199
1200 }
1201
1202 reuse() {
1203     local module="$1"
1204     local dest="$2"
1205     local use_links="${3:-false}"
1206     local signature="$4"
1207
1208     if [ -n "$REUSEBUILD" ] && [ -d "$REUSEBUILD/$signature/$module" ]; then
1209         if [ ! -f "$REUSEBUILD/$signature/$module/.lastused" ]; then
1210             # the .lastused flag is populated at the end of the caching to
1211             # signal that the caching was completed.  if that flag is not
1212             # there, then the cache is invalid (and should be removed in fact)
1213             mv "$REUSEBUILD/$signature/$module"{,-bad-$(date +%s)} ||
1214                 fatal 1 "failed to clean up a bad cache in location $REUSEBUILD/$signature/$module\" -- manual cleanup will be necessary"
1215             return 1
1216         fi
1217
1218         # so that we know how stale this entry is
1219         touch $REUSEBUILD/$signature/$module/.lastused
1220
1221         if $use_links; then
1222             if ls $REUSEBUILD/$signature/$module/* >/dev/null 2>&1; then
1223                 cp -al $REUSEBUILD/$signature/$module/* $dest/
1224             fi
1225         else
1226             # copying is pretty heavy
1227             # cp -a $REUSEBUILD/$signature/$module/* $dest/
1228             # do some creative symlinking instead
1229             local dir
1230             for dir in BUILD SRPMS SPECS; do
1231                 if ls $REUSEBUILD/$signature/$module/$dir/* >/dev/null 2>&1; then
1232                     ln -s $REUSEBUILD/$signature/$module/$dir/* $dest/$dir
1233                 fi
1234             done
1235             # sources have to be copied by file because we need SOURCES to
1236             # be a dir we can write into
1237 # could overrun ls's arg list here
1238             #ls $REUSEBUILD/$signature/$module/SOURCES/* |
1239             find $REUSEBUILD/$signature/$module/SOURCES/ -type f |
1240                 xargs ln -t $dest/SOURCES -s
1241
1242             # same for RPMS/* dirs
1243 # could overrun ls's arg list here
1244             #ls $REUSEBUILD/$signature/$module/RPMS/$TARGET_ARCH/* |
1245             local dir
1246             for dir in $REUSEBUILD/$signature/$module/RPMS/*; do
1247                 mkdir -p $dest/RPMS/${dir##*/}
1248                 find $dir -type f |
1249                   xargs ln -t $dest/RPMS/${dir##*/} -s
1250             done
1251
1252         fi
1253         return 0
1254     else
1255         return 1
1256     fi
1257 }
1258
1259 basearch() {
1260     local arch="$1"
1261
1262     if [[ $arch = i[3456]86 ]]; then
1263         echo "i386"
1264     else
1265         echo "$arch"
1266     fi
1267
1268 }
1269
1270 #
1271 # in a given directory, find the first rpm matching given requirements
1272 #
1273 find_rpm() {
1274     local dir="$1"
1275     local match_type="$2"
1276     local match="$3"
1277
1278     pushd "$dir" > /dev/null || \
1279         fatal 1 "Unable to chdir to directory \"$dir\" in find_rpm()"
1280
1281     local file
1282     for file in $(ls *.rpm); do
1283         if [ ! -f "$file" ]; then
1284             continue
1285         fi
1286         case "$match_type" in
1287             provides)
1288                 # match is any valid ERE (i.e. given to egrep) match
1289                 if rpm -q --provides -p "$file" 2>&3 | egrep -q "$match"; then
1290                     echo "$file"
1291                     popd >/dev/null
1292                     return 0
1293                 fi
1294                 ;;
1295             *)
1296                 popd >/dev/null
1297                 fatal 1 "Unknown match type \"$match_type\" given to find_rpm()"
1298                 ;;
1299         esac
1300     done
1301
1302     popd >/dev/null
1303     return 1
1304 }
1305
1306 build_kernel_with_srpm() {
1307     local outfd=$1
1308
1309     if [ -z "$outfd" ] || [ $outfd = 1 ]; then
1310         fatal 1 "You must supply a file descriptor to ${FUNCNAME[0]} and it cannot be 1"
1311     fi
1312
1313     # need to generate the patch for this target
1314     do_patch_linux false >&${outfd}    # sets global $FULL_PATCH (yeah, yuck)
1315
1316     # get an md5sum of the kernel patch + config for reuse check
1317     # XXX really, there needs to be a signature and a CONFIG_FILE per arch
1318     #     in BUILD_ARCHS
1319     local release_str
1320     if $RELEASE; then
1321         local release_str="RELEASE=$RELEASE\n"
1322     fi
1323     local REUSE_SIGNATURE=$({ echo -en $release_str; echo $BUILD_GEN; cat $CONFIG_FILE $TARGET_FILE $FULL_PATCH; } | md5sum | cut -d" " -f1)
1324
1325     # see if we can link to the reuse pool
1326     # XXX - hrm.  i'm not convinced this doesn't belong in the reuse "library"
1327     local CAN_LINK_FOR_REUSE=false
1328     touch $REUSEBUILD/$$
1329     if cp -al $REUSEBUILD/$$ $TOPDIR/ 2>/dev/null; then
1330         CAN_LINK_FOR_REUSE=true
1331     fi
1332     rm $REUSEBUILD/$$
1333
1334     # the extra version string to use for the kernel (which might be a reused
1335     # kernel, remember)
1336     local kernel_extra_version=""
1337     if $REUSERPM && ! reuse kernel "$TOPDIR" "$CAN_LINK_FOR_REUSE" \
1338                                    "$REUSE_SIGNATURE"; then
1339         # nothing cached, build from scratch
1340         if [ ! -r "$KERNELDIR/$KERNEL_SRPM" ]; then
1341             echo "Downloading kernel SRPM"
1342             download_srpm "$CANONICAL_TARGET" "$KERNEL_SRPM" >&${outfd}
1343         fi
1344
1345         if ! rpm -ivh $KERNELDIR/$KERNEL_SRPM \
1346                   --define "_topdir $TOPDIR" >&${outfd} 2>&1; then
1347             # should we clean this up or leave it for analysis?
1348             #rm -rf $RPMTOPDIR
1349             fatal 1 "Error installing kernel SRPM."
1350         fi
1351
1352         # put the Lustre kernel patch into the RPM build tree
1353         cp $FULL_PATCH $TOPDIR/SOURCES/linux-${lnxmaj}-lustre.patch
1354         prepare_and_build_srpm >&${outfd} ||
1355             fatal 1 "failed to prepare_and_build_srpm"
1356
1357         # store the resulting kernel RPM build tree for future use
1358         echo "caching the built kenel for future builds..." >&${outfd}
1359         if ! store_for_reuse "$TOPDIR/{SPECS,SOURCES,BUILD,SRPMS,RPMS}" \
1360                              "kernel" "$REUSEBUILD" "$REUSE_SIGNATURE" \
1361                              "$CAN_LINK_FOR_REUSE"; then
1362             error "Failed to store kernel RPMS for reuse"
1363             echo "unknown"
1364             return 1
1365         fi
1366     fi  # build reuse
1367
1368     # figure out the EXTRA_VERSION of the kernel we built or are re-using
1369     local KERNEL_RPM
1370     if ! KERNEL_RPM=$(find_rpm "$TOPDIR/RPMS/$TARGET_ARCH/" provides "^kernel ="); then
1371         fatal 1 "Failed to find a kernel RPM in $TOPDIR/RPMS/$TARGET_ARCH/"
1372     fi
1373     kernel_extra_version=$(rpm -q --queryformat "%{RELEASE}" -p $TOPDIR/RPMS/$TARGET_ARCH/$KERNEL_RPM)
1374
1375     # should now have the following RPMs
1376     # $TOPDIR/RPMS/$arch/kernel-lustre-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1377     # $TOPDIR/RPMS/$arch/kernel-lustre-devel-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1378     # $TOPDIR/RPMS/$arch/kernel-lustre-headers-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1379     # $TOPDIR/RPMS/$arch/kernel-lustre-debuginfo-common-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1380     # $TOPDIR/RPMS/$arch/kernel-lustre-debuginfo-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1381
1382     echo $kernel_extra_version
1383     return 0
1384
1385 }
1386
1387 build_mptlinux() {
1388     local linux="$1"
1389     local version="$2"
1390
1391     if [ -z "$version" -o "$version" = "inkernel" ]; then
1392         return 0
1393     fi
1394
1395     local targets arch
1396     for arch in $BUILD_ARCHS; do
1397         targets="--target $arch $targets"
1398     done
1399
1400     local rpmbuildopt='-bb'
1401     if $NORPM; then
1402         rpmbuildopt='-bc'
1403         echo NORPM mode. Only compiling.
1404     fi
1405
1406     # if only we could just rebuild the src.rpm.  but the included spec
1407     # is a real pig's breakfast.  just check out the patch we need to
1408     # apply to it to make it useful.
1409     #$RPMBUILD --rebuild \
1410     #          --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
1411     #          ${TOPDIR}/mptlinux-*.src.rpm
1412     rpm -ivh --define "_topdir ${TOPDIR}" ${TOPDIR}/mptlinux-*.src.rpm
1413
1414     # now the big honkin' patch to the spec file
1415     pushd ${TOPDIR}/SPECS
1416     # to regen this patch use:
1417     # !!cd ~/rpm/SPECS/ && diff -u mptlinux.spec{.dist,}
1418     patch -p0 < ${TOPDIR}/lustre/build/mptlinux.spec.patch || fatal 1 "failed to patch mptlinux.spec"
1419     popd
1420
1421     local targets arch
1422         for arch in $BUILD_ARCHS; do
1423         targets="--target $arch $targets"
1424     done
1425
1426     local rpmbuildopt='-bb'
1427     if $NORPM; then
1428         rpmbuildopt='-bc'
1429         echo NORPM mode. Only compiling.
1430     fi
1431
1432     if ! $RPMBUILD $targets $rpmbuildopt \
1433                   --define "_tmppath /var/tmp" \
1434                   --define "_topdir ${TOPDIR}" \
1435                   --define "kernel_obj $linux" \
1436                   ${TOPDIR}/SPECS/mptlinux.spec 2>&1; then
1437         return 1
1438     fi
1439     if $DO_SRC; then
1440         if ! $RPMBUILD -bs \
1441                       --define "_tmppath /var/tmp" \
1442                       --define "_topdir ${TOPDIR}" \
1443                       --define "kernel_obj $linux" \
1444                       ${TOPDIR}/SPECS/mptlinux.spec 2>&1; then
1445             return 1
1446         fi
1447     fi
1448
1449     return 0
1450
1451 }
1452
1453 # build RDAC
1454 build_rdac() {
1455     local linux="$1"
1456     local version="$2"
1457
1458     if [ -z "$version" -o "$version" = "inkernel" ]; then
1459         return 0
1460     fi
1461
1462     # note that we use an _, not a . before the spec on purpose.  we are not
1463     # allowed to have more than one file with a .spec trailer in a tarball
1464     # that is supposed to be usable with rpmbuild
1465     cp lustre/build/rdac_spec ${TOPDIR}/SPECS/rdac.spec || fatal 1 "Could not find rdac.spec in lustre/build" 
1466
1467     local targets arch
1468     for arch in $BUILD_ARCHS; do
1469         targets="--target $arch $targets"
1470     done
1471
1472     local rpmbuildopt='-bb'
1473     if $NORPM; then
1474         rpmbuildopt='-bc'
1475         echo NORPM mode. Only compiling.
1476     fi
1477
1478     local distro
1479     case $DISTRO in
1480      rhel5)     distro="REDHAT"
1481                 ;;
1482     sles1*)     distro="SUSE"
1483                 ;;
1484          *)     echo "$DISTRO not supported by RDAC, skipping"
1485                 return 0
1486                 ;;
1487     esac
1488     if ! $RPMBUILD $targets $rpmbuildopt --define "dist $distro" \
1489                   --define "_tmppath /var/tmp" \
1490                   --define "_topdir ${TOPDIR}" \
1491                   --define "kernel_obj $linux" \
1492                   ${TOPDIR}/SPECS/rdac.spec 2>&1; then
1493         return 1
1494     fi
1495     if $DO_SRC; then
1496         if ! $RPMBUILD -bs --define "dist $distro" \
1497                       --define "_tmppath /var/tmp" \
1498                       --define "_topdir ${TOPDIR}" \
1499                       --define "kernel_obj $linux" \
1500                       ${TOPDIR}/SPECS/rdac.spec 2>&1; then
1501             return 1
1502         fi
1503     fi
1504
1505     return 0
1506 }
1507
1508 # build OFED
1509 # globals used:
1510 #    TOPDIR
1511 #    REUSEBUILD, REUSERPM
1512 #    CONFIGURE_FLAGS
1513
1514 build_ofed() {
1515     local linux="$1"
1516     local ofed_version="$2"
1517
1518     # before lustre, build kernel-ib
1519     if [ -z "$ofed_version" -o "$ofed_version" = "inkernel" ]; then
1520         return 0
1521     fi
1522
1523     if [ -n "$REUSEBUILD" ]; then
1524         # see if we can link to the reuse pool
1525             # XXX - hrm.  i'm not convinced this doesn't belong in the reuse
1526             #       "library"
1527         local CAN_LINK_FOR_REUSE=false
1528         touch $REUSEBUILD/$$
1529         if cp -al $REUSEBUILD/$$ $TOPDIR/; then
1530             CAN_LINK_FOR_REUSE=true
1531         fi
1532         rm $REUSEBUILD/$$
1533     fi
1534
1535     local REUSE_SIGNATURE=$({ echo "$ofed_version";
1536                               echo "$(find_linux_release ${linux})";
1537                               cat "${linux}/include/linux/autoconf.h"; } |
1538                             md5sum | cut -d" " -f1)
1539     if ! $REUSERPM || ! reuse ofed "$TOPDIR" "$CAN_LINK_FOR_REUSE" \
1540                                    "$REUSE_SIGNATURE"; then
1541         # stash away the existing built articles for a moment
1542         mkdir bak
1543         mv {BUILD,{S,}RPMS,S{OURCE,PEC}S} bak
1544         function mv_back {
1545             pushd bak
1546             find . | cpio -pudlm ..
1547             popd
1548             rm -rf bak
1549         }
1550         create_rpmbuild_dirs
1551         # build it
1552         build_kernel_ib "${linux}"
1553         if ! store_for_reuse "$TOPDIR/{SPECS,SOURCES,BUILD,SRPMS,RPMS}" \
1554                              "ofed" "$REUSEBUILD" "$REUSE_SIGNATURE" \
1555                              "$CAN_LINK_FOR_REUSE"; then
1556             error "Failed to store OFED RPMS for reuse"
1557             mv_back
1558             return 1
1559         fi
1560         # put the stuff we stashed away back
1561         mv_back
1562     fi
1563
1564     pushd "$TOPDIR" >/dev/null
1565     rm -rf kernel-ib-devel
1566     mkdir kernel-ib-devel
1567     cd kernel-ib-devel
1568     # the actual ofed RPMs don't have the -rc$n or -$date string appened that
1569     # might be present on the file
1570     local linuxrelease=$(find_linux_release "$linux")
1571     ofed_version=$(echo $ofed_version |
1572                    sed -re 's/-(20[0-9]{6,6}-[0-9]{4,4}|rc[0-9]*)$//')
1573     local rpm=$(ls $TOPDIR/RPMS/*/kernel-ib-devel-${ofed_version}-${linuxrelease//-/_}.*.rpm)
1574     rpm2cpio -itv < $rpm | cpio -id
1575     CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
1576     popd >/dev/null
1577
1578 }
1579
1580 build_with_srpm() {
1581
1582     if ! $PATCHLESS; then
1583         local kernel_extra_version
1584         if ! kernel_extra_version=$(build_kernel_with_srpm ${STDOUT}); then
1585             fatal 1 "Failed to build the kernel from it's SRPM"
1586         fi
1587
1588         for arch in $BUILD_ARCHS; do
1589             local kernel_devel_rpm
1590             if ! kernel_devel_rpm=$(find_rpm "$TOPDIR/RPMS/$arch/" provides "^$(devel_kernel_name $KERNEL_LUSTRE_NAMING) ="); then
1591                 fatal 1 "Failed to find a kernel development RPM in $TOPDIR/RPMS/$arch/"
1592             fi
1593
1594             # install the -devel RPM in preparation for the lustre build
1595             # note that the EXTRA_VERSION_DELIMITER is *NOT* used in the
1596             # version of the directory name under /usr/src
1597             if ! lnxrel="$kernel_extra_version" unpack_linux_devel_rpm \
1598                            "$TOPDIR/RPMS/$arch/$kernel_devel_rpm" "-"; then
1599                 fatal 1 "Could not find the Linux tree in $TOPDIR/RPMS/$arch/$kernel_devel_rpm"
1600             fi
1601         done
1602     else
1603         # need to find and unpack the vendor's own kernel-devel for patchless
1604         # client build
1605         local kernelrpm
1606         if ! kernelrpm=$(find_linux_rpm "-$DEVEL_KERNEL_TYPE" ${EXTRA_VERSION_DELIMITER:-"-"}); then
1607             fatal 1 "Could not find the kernel-$DEVEL_KERNEL_TYPE RPM in ${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
1608         fi
1609         if ! lnxrel="$lnxrel" unpack_linux_devel_rpm "$kernelrpm" "-"; then
1610             fatal 1 "Could not find the Linux tree in $kernelrpm"
1611         fi
1612     fi
1613
1614     build_ofed "${LINUXOBJ:-$LINUX}" "$OFED_VERSION" ||
1615         fatal 1 "error building OFED"
1616
1617     if ! $PATCHLESS; then
1618         # only need RDAC for the server
1619         build_rdac "${LINUXOBJ:-$LINUX}" "$RDAC_VERSION" ||
1620             fatal 1 "error building RDAC"
1621     fi
1622
1623     build_mptlinux "${LINUXOBJ:-$LINUX}" "$MPTLINUX_VERSION" ||
1624         fatal 1 "error building mptlinux"
1625
1626     # now build Lustre
1627     if build_lustre "$LINUX" "$LINUXOBJ"; then
1628         # the build worked.  resolve any symlinked files (i.e. from reuse)
1629         # in RPMS/$arch to real files so that that that huge mess of
1630         # complication known as LTS can copy them yet somewhere else.
1631         # is it any wonder this whole process is so damn so?  anyone ever
1632         # heard of hardlinks?  it this cool new thing that allows you save
1633         # tons of time and space by creating... well you can go read about
1634         # them if you have not heard about them yet.
1635         # can i say how much the implemenation of all of this really impedes
1636         # RPM reuse?
1637         pushd RPMS/$TARGET_ARCH
1638             for file in *; do
1639                 if [ -h $file ]; then
1640                     cp $file foo
1641                     mv foo $file
1642                 fi
1643             done
1644         popd
1645     else
1646         return 1
1647     fi
1648
1649 }
1650
1651 create_rpmbuild_dirs() {
1652
1653     [ -d RPMS ] || mkdir RPMS
1654     for arch in $BUILD_ARCHS; do
1655         if [[ $arch = i?86 ]]; then
1656             # some stupidity in the sles11 kernel spec requires an RPMS/i386
1657             # even if the target arch is i686
1658             [ -d RPMS/i386 ] || mkdir RPMS/i386
1659         fi
1660         [ -d RPMS/$arch ] || mkdir RPMS/$arch
1661     done
1662     [ -d BUILD ] || mkdir BUILD
1663     [ -d SOURCES ] || mkdir SOURCES
1664     [ -d SPECS ] || mkdir SPECS
1665     [ -d SRPMS ] || mkdir SRPMS
1666
1667 }
1668
1669 new_list() {
1670
1671     echo ""
1672
1673 }
1674
1675 add_list() {
1676     local list="$1"
1677     local item="$2"
1678
1679     echo "$list $item"
1680
1681 }
1682
1683 is_list_member() {
1684     local list="$1"
1685     local item="$2"
1686
1687     [[ $list\  == *\ $item\ * ]]
1688
1689 }
1690
1691 #########################################################################
1692 # Generate a backtrace through the call stack.
1693 #
1694 # Input: None
1695 # Output: None
1696 #########################################################################
1697 backtrace() {
1698     local strip=${1:-1}
1699
1700     local funcname="" sourcefile="" lineno="" n
1701
1702     echo "Call stack: (most recent first)"
1703     for (( n = $strip ; n < ${#FUNCNAME[@]} ; ++n )) ; do
1704         funcname=${FUNCNAME[$n - 1]}
1705         sourcefile=$(basename ${BASH_SOURCE[$n]})
1706         lineno=${BASH_LINENO[$n - 1]}
1707         if [ $n = 1 ]; then
1708             let lineno-=11
1709         fi
1710         # Display function arguments
1711         if [[ ! -z "${BASH_ARGV[@]}" ]]; then
1712             local args newarg j p=0
1713             for (( j = ${BASH_ARGC[$n - 1]}; j > 0; j-- )); do
1714                 newarg=${BASH_ARGV[$j + $p - 1]}
1715                 args="${args:+${args} }'${newarg}'"
1716             done
1717             let p+=${BASH_ARGC[$n - 1]}
1718         fi
1719         echo "  ${funcname} ${args:+${args} }at ${sourcefile}:${lineno}"
1720     done
1721
1722     echo
1723     echo "BEGIN BACKTRACE"
1724
1725     #echo ${BASH_LINENO[*]}
1726     #echo ${BASH_SOURCE[*]}
1727     #echo ${FUNCNAME[*]}
1728     local i=$((${#FUNCNAME[@]} - 1))
1729     while [ $i -ge 0 ]; do
1730         local lineno=${BASH_LINENO[$i]}
1731         if [ $i = 0 ]; then
1732             let lineno-=11
1733         fi
1734         local SOURCELINE="${BASH_SOURCE[$i + 1]}:${lineno}"
1735         # Can't figure out how to get function args from other frames...
1736         local FUNCTION="${FUNCNAME[$i]}()"
1737         echo "$SOURCELINE:$FUNCTION"
1738         i=$((i - 1))
1739     done
1740
1741     echo "END BACKTRACE"
1742
1743     echo $BACKTRACE
1744
1745 }
1746
1747 seen_list=$(new_list)
1748 trap 'set +x;
1749 echo "An unexpected error has occurred at ${BASH_SOURCE[0]##*/}:$((LINENO-1)).
1750 Unfortunately the above line number in the message may or may not be correct,
1751 but details have been send to the lbuild maintainer.  Attempting to continue."; (echo "Untrapped error"
1752 echo
1753 # have we seen this one
1754 echo "checking seen list for ${BASH_SOURCE[0]}:${BASH_LINENO[0]}"
1755
1756 if is_list_member "$seen_list" "${BASH_SOURCE[0]}:${BASH_LINENO[0]}"; then
1757   echo "seen this one already"
1758 else
1759   seen_list=$(add_list "$seen_list" "${BASH_SOURCE[0]}:${BASH_LINENO[0]}")
1760 fi
1761 backtrace
1762 echo
1763 echo "Environment:"
1764 set
1765 ) | mail -s "Untrapped error at ${BASH_SOURCE[0]##*/}:$((LINENO-15)) on $HOSTNAME" brian@sun.com >&2; set $xtrace' ERR
1766 set -E
1767
1768 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
1769
1770 options=$(getopt -o d:D:h -l kerneltree:,distro:,kernelrpm:,reusebuild:,patchless,ldiskfs,ccache,reuse:,norpm,disable-datestamp,external-patches:,timestamp:,extraversion:,kerneldir:,linux:,lustre:,nodownload,nosrc,ofed-version:,publish,release,set-value:,src,stage:,tag:,target:,target-archs:,with-linux:,xen -- "$@")
1771
1772 if [ $? != 0 ]; then
1773     usage 1
1774 fi
1775
1776 eval set -- "$options"
1777
1778 while [ "$1" ]; do
1779     case "$1" in
1780         '')
1781             usage 1
1782             ;;
1783         --ccache)
1784             CCACHE='ccache'
1785             shift
1786             ;;
1787         -d)
1788             CVSROOT=$2
1789             shift 2
1790             ;;
1791         -D)
1792             DATE=$2
1793             shift 2
1794             ;;
1795         --external-patches)
1796             EXTERNAL_PATCHES=$2
1797             shift 2
1798             ;;
1799         --extraversion)
1800             EXTRA_VERSION=$2
1801             shift 2
1802             ;;
1803         --help | -h)
1804             usage 0
1805             ;;
1806         --kerneldir)
1807             KERNELDIR=$2
1808             shift 2
1809             ;;
1810         --kerneltree)
1811             if ! KERNELTREE=$(canon_path "$2"); then
1812                 fatal 1 "Could not determine the canonical location of $2"
1813             fi
1814             shift 2
1815             ;;
1816         --linux | --with-linux)
1817             if ! LINUX=$(canon_path "$2"); then
1818                 fatal 1 "Could not determine the canonical location of $2"
1819             fi
1820             shift 2
1821             ;;
1822         --distro)
1823             DISTRO=$2
1824             shift 2
1825             ;;
1826         --reuserpm)
1827             REUSERPM=$2
1828             shift 2
1829             ;;
1830         --reusebuild)
1831             if ! REUSEBUILD=$(canon_path "$2"); then
1832                 fatal 1 "Could not determine the canonical location of $2"
1833             fi
1834             shift 2
1835             ;;
1836         --norpm)
1837             NORPM=true
1838             shift
1839             ;;
1840         --ldiskfs)
1841             LDISKFSRPM=true
1842             shift
1843             ;;
1844         --patchless)
1845             PATCHLESS=true
1846             shift
1847             ;;
1848         --kernelrpm)
1849             if ! KERNELRPMSBASE=$(canon_path "$2"); then
1850                 fatal 1 "Could not determine the canonical location of $2"
1851             fi
1852             shift 2
1853             ;;
1854         --timestamp)
1855             TIMESTAMP=$2
1856             shift 2
1857             ;;
1858         --lustre)
1859             LUSTRE=$2
1860             shift 2
1861             ;;
1862         --nodownload)
1863             DOWNLOAD=false
1864             shift 1
1865             ;;
1866         --nosrc)
1867             DO_SRC=false
1868             shift 1
1869             ;;
1870         --ofed-version)
1871             OFED_VERSION="$2"
1872             shift 2
1873             ;;
1874         --publish)
1875             shift
1876             ;;
1877         --release)
1878             RELEASE=true
1879             shift
1880             ;;
1881         --src)
1882             DO_SRC=true
1883             shift 1
1884             ;;
1885         --stage)
1886             STAGEDIR=$2
1887             shift 2
1888             ;;
1889         --tag)
1890             TAG=$2
1891             shift 2
1892             ;;
1893         --target)
1894             TARGET=$2
1895             shift 2
1896             ;;
1897         --target-archs)
1898             TARGET_ARCHS=$2
1899             shift 2
1900             ;;
1901         --disable-datestamp)
1902             USE_DATESTAMP=
1903             shift
1904             ;;
1905         --xen)
1906             XEN=true
1907             shift
1908             ;;
1909         --set-value)
1910             eval $2
1911             shift 2
1912             ;;
1913         --)
1914             shift
1915             CONFIGURE_FLAGS=$@
1916             CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-liblustre --enable-liblustre-tests"
1917             break
1918             ;;
1919         *)
1920             usage 1 "Unrecognized option: $1"
1921             ;;
1922     esac
1923 done
1924
1925 check_options
1926
1927 unpack_lustre
1928
1929 load_target
1930 EXTRA_VERSION_DELIMITER=${EXTRA_VERSION_DELIMITER:-"-"}
1931
1932 if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1933     download_ofed
1934     unpack_ofed || fatal 1 "Error unpacking OFED tarball"
1935 fi
1936
1937 if [ -n "$MPTLINUX_VERSION" -a "$MPTLINUX_VERSION" != "inkernel" ]; then
1938     download_mptlinux
1939     unpack_mptlinux || fatal 1 "Error unpacking MPTLINUX distribution"
1940 fi
1941
1942 # make sure the RPM build environment is set up
1943 create_rpmbuild_dirs
1944
1945 if [ -n "$RDAC_VERSION" -a "$RDAC_VERSION" != "inkernel" ]; then
1946     download_rdac
1947     # we don't actually need to unpack this.  just put it in the SOURCES dir
1948     #unpack_rdac || fatal 1 "Error unpacking RDAC tarball"
1949     cp "$KERNELTREE/rdac-LINUX-${RDAC_VERSION}-source.tar.gz" ${TOPDIR}/SOURCES/ ||
1950         fatal 1 "Error copying RDAC source tarball to RPM SOURCES dir"
1951 fi
1952
1953 # if an unpacked kernel source tree was given on the command line
1954 # just build lustre with it (nothing distro kernel specific here)
1955 if [ -n "$LINUX" ]; then
1956     build_mptlinux "${LINUXOBJ:-$LINUX}" "$MPTLINUX_VERSION" ||
1957         fatal 1 "error building mptlinux"
1958     build_ofed "${LINUXOBJ:-$LINUX}" "$OFED_VERSION" ||
1959         fatal 1 "error building OFED"
1960     if ! $PATCHLESS; then
1961         build_rdac "${LINUXOBJ:-$LINUX}" "$RDAC_VERSION" ||
1962             fatal 1 "error building RDAC"
1963     fi
1964     build_lustre "$LINUX" "$LINUXOBJ"
1965 else
1966     if [ -f "${0%/*}/lbuild-$DISTRO" ]; then
1967         source ${0%/*}/lbuild-$DISTRO
1968
1969         build_with_srpm || fatal 1 "Failed to build_with_srpm"
1970     else
1971         source ${0%/*}/lbuild.old_school
1972
1973         old_school_download_kernel
1974
1975         build_success=false
1976         if $PATCHLESS; then
1977             patchless_build_sequence && build_success=true
1978         else
1979             [ "$DISTRO" = "sles9" ] && build_sequence_rpm_reuse && build_success=true
1980             if ! $build_success; then
1981                 build_sequence_reuse && build_success=true
1982                 if ! $build_success; then
1983                     build_sequence && build_success=true
1984                     if $build_success; then
1985                         store_for_reuse || echo "Cannot store for future reuse"
1986                     fi
1987                 fi
1988             fi
1989         fi
1990         ( $build_success ) || fatal 1 "Cannot build lustre"
1991     fi
1992 fi
1993
1994 stage