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