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