Whamcloud - gitweb
b=23120 eliminate defunct changelog_send_thread processes
[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="MPTLINUX_RHEL5_SLES10_PH15-${MPTLINUX_VERSION}.zip"
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 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}.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/not-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
849     # ditto for the lustre-tests boolean
850     local lustre_tests=""
851     if ! $LUSTRE_TESTS; then
852         lustre_tests="no"
853     fi
854
855     $RPMBUILD $targets $rpmbuildopt ../lustre.spec \
856         ${is_patchless:+--define "lustre_name lustre-client"} \
857         ${lustre_tests:+--define "build_lustre_tests 0"} \
858         ${FIND_REQUIRES:+--define "__find_requires $FIND_REQUIRES"} \
859         --define "configure_args $confoptions ${CONFIGURE_FLAGS}" \
860         --define "_tmppath $TMPDIR" \
861         --define "_topdir $TOPDIR" 2>&1 || \
862         fatal 1 "Error building rpms for $BUILD_ARCHS."
863
864     popd >/dev/null
865     ( $(skeep_ldiskfs_rpm $TAG) ) && return
866
867     pushd lustre/ldiskfs || return 255
868     make dist 2>&1
869     if [ "$?" != "0" ]; then
870         popd
871         return 255
872     fi
873     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
874
875     gen_lustre_version
876
877     local ldiskfs_spec=lustre-ldiskfs.spec
878     [ -f "$ldiskfs_spec" ] && sed \
879     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
880     < $ldiskfs_spec \
881     > ../lustre-ldiskfs.spec
882
883     if ! $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
884         --define "_tmppath /var/tmp" \
885         --define "_topdir $TOPDIR" 2>&1; then
886         popd
887         return 255
888     fi
889
890     if $DO_SRC; then
891         if ! $RPMBUILD -bs ../lustre-ldiskfs.spec \
892             --define "_tmppath /var/tmp" \
893             --define "_topdir $TOPDIR" 2>&1; then
894             popd
895             return 255
896         fi
897     fi
898     popd
899
900 }
901
902 stage() {
903
904     [ "$STAGEDIR" ] || return 0
905
906     for arch in $BUILD_ARCHS; do
907         rpmdir="${STAGEDIR}/${CANONICAL_TARGET}-${arch}"
908         echo "${0##*/}: Copying RPMs into ${rpmdir}"
909         mkdir -p "${rpmdir}"
910         cp -v RPMS/${arch}/*.rpm "${rpmdir}"
911         if [ -d RPMS/noarch ]; then
912             cp -v RPMS/noarch/*.rpm "${rpmdir}"
913         fi
914     done
915
916     cp -v "$LUSTRE" "$STAGEDIR"
917
918 }
919
920 #check if we need to build separate ldiskfs RPM
921 skeep_ldiskfs_rpm() {
922         local tag="$1"
923
924         local skip=false
925
926         if ! $LDISKFSRPM; then
927             skip=true
928         elif $PATCHLESS; then
929             skip=true
930         else
931             for skiptag in $SKIPLDISKFSRPM; do
932                 [[ $tag == $skiptag ]] && skip=true && break
933             done
934         fi
935
936         pushd $TOPDIR/BUILD/lustre-[1-9]* >/dev/null
937         grep -q '^SERVER_TRUE[ \t]=[ \t]#$' autoMakefile && skip=true
938         popd >/dev/null
939
940         echo $skip
941
942 }
943
944 #generate LUSTRE_EXTRA_VERSION from EXTRA_VERSION
945 gen_lustre_version() {
946
947     LUSTRE_EXTRA_VERSION="${lnxmaj}-${EXTRA_VERSION}${FLAVOR_DELIMITER}${RPMSMPTYPE}"
948     LUSTRE_EXTRA_VERSION=${LUSTRE_EXTRA_VERSION//-/_}
949
950 }
951
952 set_rpm_smp_type() {
953
954     local infact_arch="${TARGET_ARCH}"
955
956     RPMSMPTYPE=""
957     [ "$infact_arch" == "i586" ] && infact_arch="i686"
958
959     local smp_type
960     for smp_type in $SMP_ARCHS; do
961         [ $infact_arch == $smp_type ] && RPMSMPTYPE=smp && break
962     done
963
964     for smp_type in $BIGSMP_ARCHS; do
965         [ $infact_arch == $smp_type ] && RPMSMPTYPE=bigsmp && break
966     done
967
968     for smp_type in $PPC64_ARCHS; do
969         [ $infact_arch == $smp_type ] && RPMSMPTYPE=ppc64 && break
970     done
971
972     for smp_type in $DEFAULT_ARCHS; do
973         [ $infact_arch == $smp_type ] && RPMSMPTYPE=default && break
974     done
975
976 }
977
978 # This function takes a linux source pool and digs out the linux release
979 # from it
980 find_linux_release() {
981     local SRCDIR="$1"
982
983     local LINUXRELEASEHEADER=$SRCDIR/include/linux/version.h
984     if [ -s $SRCDIR/include/linux/utsrelease.h ]; then
985         LINUXRELEASEHEADER=$SRCDIR/include/linux/utsrelease.h
986     fi
987
988     sed -ne 's/#define UTS_RELEASE "\(.*\)"$/\1/p' $LINUXRELEASEHEADER
989
990 }
991
992 # XXX this needs to be re-written as a wrapper around find_rpm
993 #     or just gotten rid of.  :-)
994 find_linux_rpm() {
995     local prefix="$1"
996
997     local pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
998     [ -d $pathtorpms ] || return 255
999
1000     local kernelbinaryrpm rpmfile
1001     local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}"
1002
1003     local arch ret=1
1004     for arch in $TARGET_ARCHS_ALL; do
1005         local found_rpm="" rpm
1006         for rpm in ${pathtorpms}/${arch}/*.rpm; do
1007             if rpm -q --provides -p "$rpm" 2>&3 | grep -q "kernel${prefix} = $wanted_kernel" 2>&3; then
1008
1009                 found_rpm="$rpm"
1010                 ret=0
1011                 break
1012             fi
1013         done
1014         [ -f "$found_rpm" ] && break
1015     done
1016
1017     echo "$found_rpm"
1018     return $ret
1019
1020 }
1021
1022 # unpack kernel(/source/devel) RPM
1023 #
1024 # This function and it's setting of $LINUX and $LINUXOBJ is a total hack that
1025 # needs to completely refactored.  It completely ingores that $BUILD_ARCHS may
1026 # contain a list of arches for which rpmbuild commands (including the one for
1027 # lustre itself)
1028 unpack_linux_devel_rpm() {
1029     local kernelrpm="${1}"
1030
1031     [ -f "$kernelrpm" ] || return 255
1032     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused || return 255
1033
1034     pushd $TOPDIR/reused &>/dev/null || return 255
1035
1036     if ! rpm2cpio < "$kernelrpm" | cpio -id > /dev/null 2>&1; then
1037         return 255
1038     fi
1039
1040     # call a distro specific hook, if available
1041     if type -p unpack_linux_devel_rpm-$DISTRO; then
1042         if ! unpack_linux_devel_rpm-$DISTRO "$kernelrpm"; then
1043             return 255
1044         fi
1045     fi
1046
1047     popd &>/dev/null
1048
1049     find_linux_devel_paths $TOPDIR/reused
1050
1051     return 0
1052
1053 }
1054
1055 build_kernel_ib() {
1056     local linux="$1"
1057
1058     # build kernel-ib{,-devel}
1059     local K_SRC="K_SRC"
1060     # ofed 1.3 had a bug in the rpm spec
1061     if [ "$OFED_VERSION" = "1.3" ]; then
1062         K_SRC="KSRC"
1063     fi
1064
1065     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"
1066     local OFED_HARDWARE="--with-mthca-mod --with-mlx4-mod --with-mlx4_en-mod --with-cxgb3-mod --with-nes-mod"
1067     # some I/B drivers are architecture dependent and kernel-ib's configure
1068     # does not figure it out for us ~sigh~
1069     case "$TARGET_ARCH" in
1070         ppc64)
1071             OFED_HARDWARE="$OFED_HARDWARE --with-ehca-mod"
1072             ;;
1073     esac
1074     # we're no longer shipping the OFED iSCSI
1075     #OFED_ISCSI="--with-srp-mod --with-srp-target-mod"
1076     ## ISER module has no backport support as of OFED 1.5 (i.e. only builds on
1077     ##kernels >= 2.6.30)
1078     #if [[ $OFED_VERSION = 1.[0-4]* ]]; then
1079     #   OFED_ISCSI="$OFED_ISCSI --with-iser-mod"
1080     #fi
1081
1082     # assume we are just rebuilding the SRPM
1083     local BUILD_TYPE=${BUILD_TYPE:-"--rebuild"}
1084     local SOURCE="${TOPDIR}/OFED/SRPMS/ofa_kernel-*.src.rpm"
1085
1086     # but switch to building from the SPEC if we need to apply patches
1087     if ls ${TOPDIR}/lustre/build/patches/ofed/* >/dev/null; then
1088         BUILD_TYPE="-bb"
1089         rpm --define "_topdir ${TOPDIR}" -ivh $SOURCE
1090         SOURCE="${TOPDIR}/SPECS/ofa_kernel.spec"
1091         local file ed_fragment1 ed_fragment2 n=1
1092         for file in $(ls ${TOPDIR}/lustre/build/patches/ofed/*); do
1093             ed_fragment1="$ed_fragment1
1094 Patch$n: ${file%%*/}"
1095             ed_fragment2="$ed_fragment2
1096 %patch$n -p0"
1097             cp $file ${TOPDIR}/SOURCES
1098             let n=$n+1
1099         done
1100
1101         if [ $n -gt 1 ]; then
1102             ed $SOURCE <<EOF
1103 /^Source: /a
1104 $ed_fragment1
1105 .
1106 /^%setup /a
1107 $ed_fragment2
1108 .
1109 wq
1110 EOF
1111         fi
1112     fi
1113
1114     local linuxrelease=$(find_linux_release "$linux")
1115     if ! $RPMBUILD $BUILD_TYPE --define 'build_kernel_ib 1' --define 'build_kernel_ib_devel 1' \
1116                   ${FIND_REQUIRES:+--define "__find_requires $FIND_REQUIRES"} \
1117                   --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
1118                   --define "KVERSION ${linuxrelease}" \
1119                   --define "$K_SRC ${linux}" \
1120                   --define "LIB_MOD_DIR /lib/modules/${linuxrelease}/updates" \
1121                   ${OFA_KERNEL_RELEASE:+--define "_release $OFA_KERNEL_RELEASE"} \
1122                   --define "configure_options --without-quilt $OFED_CORE $OFED_HARDWARE $OFED_ISCSI" \
1123                   ${SOURCE} 2>&1; then
1124         fatal 1 "Error building kernel-ib"
1125     fi
1126
1127 }
1128
1129 store_for_reuse() {
1130         local articles="$1"
1131         local module="$2"
1132         local location="$3"
1133         local signature="$4"
1134         local use_links="$5"
1135
1136         local linkflag=""
1137         if $use_links; then
1138             linkflag="l"
1139         fi
1140
1141         location="$location"/"$signature"/"$module"
1142         mkdir -p "$location"
1143         # the cleanup script removes any directory that doesn't have a
1144         # .lastused, so let's try to prevent that as soon as we can
1145         # this solution still slightly racy with the cleanup script
1146         # but the race is a lot tighter now
1147         touch -t 197001010000 "$location/.lastused"
1148         ## use eval/echo here to make sure shell expansions are performed
1149         #if ! cp -a${linkflag} $(eval echo $articles) "$location"; then
1150         local article
1151         for article in $(eval echo $articles); do
1152             if ! cp -a${linkflag} "$article" "$location"; then
1153                 error "Failed to copy \"$article\" to \"$location\" in store_for_reuse()"
1154                 # rename the cache location so that it's not cached
1155                 # product, but is around for analysis
1156                 mv "$location"{,-bad-$(date +%s)} ||
1157                     error "failed to clean up a failed cache attempt" \
1158                           "in \"$location\" -- manual cleanup will be" \
1159                           "necessary"
1160                 return 1
1161             fi
1162         done
1163
1164         # flag the cache as complete (i.e. in case lbuild was previously
1165         # interrupted while caching)
1166         touch "$location/.lastused"
1167
1168         return 0
1169
1170 }
1171
1172 reuse() {
1173     local module="$1"
1174     local dest="$2"
1175     local use_links="${3:-false}"
1176     local signature="$4"
1177
1178     if [ -n "$REUSEBUILD" ] && [ -d "$REUSEBUILD/$signature/$module" ]; then
1179         if [ ! -f "$REUSEBUILD/$signature/$module/.lastused" ]; then
1180             # the .lastused flag is populated at the end of the caching to
1181             # signal that the caching was completed.  if that flag is not
1182             # there, then the cache is invalid (and should be removed in fact)
1183             mv "$REUSEBUILD/$signature/$module"{,-bad-$(date +%s)} ||
1184                 fatal 1 "failed to clean up a bad cache in location $REUSEBUILD/$signature/$module\" -- manual cleanup will be necessary"
1185             return 1
1186         fi
1187
1188         # so that we know how stale this entry is
1189         touch $REUSEBUILD/$signature/$module/.lastused
1190
1191         if $use_links; then
1192             if ls $REUSEBUILD/$signature/$module/* >/dev/null 2>&1; then
1193                 cp -al $REUSEBUILD/$signature/$module/* $dest/
1194             fi
1195         else
1196             # copying is pretty heavy
1197             # cp -a $REUSEBUILD/$signature/$module/* $dest/
1198             # do some creative symlinking instead
1199             local dir
1200             for dir in BUILD SRPMS SPECS; do
1201                 if ls $REUSEBUILD/$signature/$module/$dir/* >/dev/null 2>&1; then
1202                     ln -s $REUSEBUILD/$signature/$module/$dir/* $dest/$dir
1203                 fi
1204             done
1205             # sources have to be copied by file because we need SOURCES to
1206             # be a dir we can write into
1207 # could overrun ls's arg list here
1208             #ls $REUSEBUILD/$signature/$module/SOURCES/* |
1209             find $REUSEBUILD/$signature/$module/SOURCES/ -type f |
1210                 xargs ln -t $dest/SOURCES -s
1211
1212             # same for RPMS/* dirs
1213 # could overrun ls's arg list here
1214             #ls $REUSEBUILD/$signature/$module/RPMS/$TARGET_ARCH/* |
1215             local dir
1216             for dir in $REUSEBUILD/$signature/$module/RPMS/*; do
1217                 mkdir -p $dest/RPMS/${dir##*/}
1218                 find $dir -type f |
1219                   xargs ln -t $dest/RPMS/${dir##*/} -s
1220             done
1221
1222         fi
1223         return 0
1224     else
1225         return 1
1226     fi
1227 }
1228
1229 basearch() {
1230     local arch="$1"
1231
1232     if [[ $arch = i[3456]86 ]]; then
1233         echo "i386"
1234     else
1235         echo "$arch"
1236     fi
1237
1238 }
1239
1240 #
1241 # in a given directory, find the first rpm matching given requirements
1242 #
1243 find_rpm() {
1244     local dir="$1"
1245     local match_type="$2"
1246     local match="$3"
1247
1248     pushd "$dir" > /dev/null || \
1249         fatal 1 "Unable to chdir to directory \"$dir\" in find_rpm()"
1250
1251     local file
1252     for file in $(ls *.rpm); do
1253         if [ ! -f "$file" ]; then
1254             continue
1255         fi
1256         case "$match_type" in
1257             provides)
1258                 # match is any valid ERE (i.e. given to egrep) match
1259                 if rpm -q --provides -p "$file" 2>&3 | egrep -q "$match"; then
1260                     echo "$file"
1261                     popd >/dev/null
1262                     return 0
1263                 fi
1264                 ;;
1265             *)
1266                 popd >/dev/null
1267                 fatal 1 "Unknown match type \"$match_type\" given to find_rpm()"
1268                 ;;
1269         esac
1270     done
1271
1272     popd >/dev/null
1273     return 1
1274 }
1275
1276 build_kernel_with_srpm() {
1277     local outfd=$1
1278
1279     if [ -z "$outfd" ] || [ $outfd = 1 ]; then
1280         fatal 1 "You must supply a file descriptor to ${FUNCNAME[0]} and it cannot be 1"
1281     fi
1282
1283     # need to generate the patch for this target
1284     do_patch_linux false >&${outfd}    # sets global $FULL_PATCH (yeah, yuck)
1285
1286     # get an md5sum of the kernel patch + config for reuse check
1287     # XXX really, there needs to be a signature and a CONFIG_FILE per arch
1288     #     in BUILD_ARCHS
1289     local release_str
1290     if $RELEASE; then
1291         local release_str="RELEASE=$RELEASE\n"
1292     fi
1293
1294     if $USE_BUILD_CACHE && [ -n "$REUSEBUILD" ]; then
1295         local REUSE_SIGNATURE=$({ echo -en $release_str;
1296                                   echo $BUILD_GEN;
1297                                   cat $CONFIG_FILE $TARGET_FILE $FULL_PATCH; } |
1298                                 md5sum | cut -d" " -f1)
1299         # see if we can link to the reuse pool
1300         # XXX - hrm.  i'm not convinced this doesn't belong in the reuse
1301         #       "library"
1302         local CAN_LINK_FOR_REUSE=false
1303         touch $REUSEBUILD/$$
1304         if cp -al $REUSEBUILD/$$ $TOPDIR/ 2>/dev/null; then
1305             CAN_LINK_FOR_REUSE=true
1306         fi
1307         rm $REUSEBUILD/$$
1308     fi
1309
1310     # the extra version string to use for the kernel (which might be a reused
1311     # kernel, remember)
1312     local kernel_extra_version=""
1313     if ! $USE_BUILD_CACHE || ! reuse kernel "$TOPDIR" "$CAN_LINK_FOR_REUSE" \
1314                                    "$REUSE_SIGNATURE"; then
1315         # nothing cached, build from scratch
1316         if [ ! -r "$KERNELDIR/$KERNEL_SRPM" ]; then
1317             echo "Downloading kernel SRPM"
1318             download_srpm "$CANONICAL_TARGET" "$KERNEL_SRPM" >&${outfd}
1319         fi
1320
1321         if ! rpm -ivh $KERNELDIR/$KERNEL_SRPM \
1322                   --define "_topdir $TOPDIR" >&${outfd} 2>&1; then
1323             # should we clean this up or leave it for analysis?
1324             #rm -rf $RPMTOPDIR
1325             fatal 1 "Error installing kernel SRPM."
1326         fi
1327
1328         # put the Lustre kernel patch into the RPM build tree
1329         cp $FULL_PATCH $TOPDIR/SOURCES/linux-${lnxmaj}-lustre.patch
1330         prepare_and_build_srpm >&${outfd} ||
1331             fatal 1 "failed to prepare_and_build_srpm"
1332
1333         if [ -z "$REUSE_SIGNATURE" ]; then
1334             echo "No reuse signature was caculated so not storing the built kernel" >&${outfd}
1335         else
1336             # store the resulting kernel RPM build tree for future use
1337             echo "Storing the built kernel for future reuse" >&${outfd}
1338             if ! store_for_reuse "$TOPDIR/{SPECS,SOURCES,BUILD,SRPMS,RPMS}" \
1339                                  "kernel" "$REUSEBUILD" "$REUSE_SIGNATURE" \
1340                                  "$CAN_LINK_FOR_REUSE"; then
1341                 error "Failed to store kernel RPMS for reuse"
1342                 echo "unknown"
1343                 return 1
1344             fi
1345         fi
1346     fi  # build reuse
1347
1348     # figure out the EXTRA_VERSION of the kernel we built or are re-using
1349     local KERNEL_RPM
1350     if ! KERNEL_RPM=$(find_rpm "$TOPDIR/RPMS/$TARGET_ARCH/" provides "^kernel ="); then
1351         fatal 1 "Failed to find a kernel RPM in $TOPDIR/RPMS/$TARGET_ARCH/"
1352     fi
1353     kernel_extra_version=$(rpm -q --queryformat "%{RELEASE}" -p $TOPDIR/RPMS/$TARGET_ARCH/$KERNEL_RPM)
1354
1355     # should now have the following RPMs
1356     # $TOPDIR/RPMS/$arch/kernel-lustre-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1357     # $TOPDIR/RPMS/$arch/kernel-lustre-devel-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1358     # $TOPDIR/RPMS/$arch/kernel-lustre-headers-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1359     # $TOPDIR/RPMS/$arch/kernel-lustre-debuginfo-common-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1360     # $TOPDIR/RPMS/$arch/kernel-lustre-debuginfo-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1361
1362     echo $kernel_extra_version
1363     return 0
1364
1365 }
1366
1367 build_mptlinux() {
1368     local linux="$1"
1369     local version="$2"
1370
1371     if [ -z "$version" -o "$version" = "inkernel" ]; then
1372         return 0
1373     fi
1374
1375     local targets arch
1376     for arch in $BUILD_ARCHS; do
1377         targets="--target $arch $targets"
1378     done
1379
1380     local rpmbuildopt='-bb'
1381     if $NORPM; then
1382         rpmbuildopt='-bc'
1383         echo NORPM mode. Only compiling.
1384     fi
1385
1386     # if only we could just rebuild the src.rpm.  but the included spec
1387     # is a real pig's breakfast.  just check out the patch we need to
1388     # apply to it to make it useful.
1389     #$RPMBUILD --rebuild \
1390     #          --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
1391     #          ${TOPDIR}/mptlinux-*.src.rpm
1392     rpm -ivh --define "_topdir ${TOPDIR}" ${TOPDIR}/mptlinux-*.src.rpm
1393
1394     # now the big honkin' patch to the spec file
1395     pushd ${TOPDIR}/SPECS
1396     # to regen this patch use:
1397     # !!cd ~/rpm/SPECS/ && diff -u mptlinux.spec{.dist,}
1398     patch -p0 < ${TOPDIR}/lustre/build/mptlinux.spec.patch || fatal 1 "failed to patch mptlinux.spec"
1399     popd
1400
1401     local targets arch
1402         for arch in $BUILD_ARCHS; do
1403         targets="--target $arch $targets"
1404     done
1405
1406     local rpmbuildopt='-bb'
1407     if $NORPM; then
1408         rpmbuildopt='-bc'
1409         echo NORPM mode. Only compiling.
1410     fi
1411
1412     if ! $RPMBUILD $targets $rpmbuildopt \
1413                   ${FIND_REQUIRES:+--define "__find_requires $FIND_REQUIRES"} \
1414                   --define "_tmppath /var/tmp" \
1415                   --define "_topdir ${TOPDIR}" \
1416                   --define "kernel_obj $linux" \
1417                   ${TOPDIR}/SPECS/mptlinux.spec 2>&1; then
1418         return 1
1419     fi
1420     if $DO_SRC; then
1421         if ! $RPMBUILD -bs \
1422                       ${FIND_REQUIRES:+--define "__find_requires $FIND_REQUIRES"} \
1423                       --define "_tmppath /var/tmp" \
1424                       --define "_topdir ${TOPDIR}" \
1425                       --define "kernel_obj $linux" \
1426                       ${TOPDIR}/SPECS/mptlinux.spec 2>&1; then
1427             return 1
1428         fi
1429     fi
1430
1431     return 0
1432
1433 }
1434
1435 # build RDAC
1436 build_rdac() {
1437     local linux="$1"
1438     local version="$2"
1439
1440     if [ -z "$version" -o "$version" = "inkernel" ]; then
1441         return 0
1442     fi
1443
1444     # note that we use an _, not a . before the spec on purpose.  we are not
1445     # allowed to have more than one file with a .spec trailer in a tarball
1446     # that is supposed to be usable with rpmbuild
1447     cp lustre/build/rdac_spec ${TOPDIR}/SPECS/rdac.spec || fatal 1 "Could not find rdac.spec in lustre/build" 
1448
1449     local targets arch
1450     for arch in $BUILD_ARCHS; do
1451         targets="--target $arch $targets"
1452     done
1453
1454     local rpmbuildopt='-bb'
1455     if $NORPM; then
1456         rpmbuildopt='-bc'
1457         echo NORPM mode. Only compiling.
1458     fi
1459
1460     local distro
1461     case $DISTRO in
1462      rhel5)     distro="REDHAT"
1463                 ;;
1464     sles1*)     distro="SUSE"
1465                 ;;
1466          *)     echo "$DISTRO not supported by RDAC, skipping"
1467                 return 0
1468                 ;;
1469     esac
1470     if ! $RPMBUILD $targets $rpmbuildopt --define "dist $distro" \
1471                   ${FIND_REQUIRES:+--define "__find_requires $FIND_REQUIRES"} \
1472                   --define "_tmppath /var/tmp" \
1473                   --define "_topdir ${TOPDIR}" \
1474                   --define "kernel_obj $linux" \
1475                   ${TOPDIR}/SPECS/rdac.spec 2>&1; then
1476         return 1
1477     fi
1478     if $DO_SRC; then
1479         if ! $RPMBUILD -bs --define "dist $distro" \
1480                       ${FIND_REQUIRES:+--define "__find_requires $FIND_REQUIRES"} \
1481                       --define "_tmppath /var/tmp" \
1482                       --define "_topdir ${TOPDIR}" \
1483                       --define "kernel_obj $linux" \
1484                       ${TOPDIR}/SPECS/rdac.spec 2>&1; then
1485             return 1
1486         fi
1487     fi
1488
1489     return 0
1490 }
1491
1492 # build OFED
1493 # globals used:
1494 #    TOPDIR
1495 #    REUSEBUILD, USE_BUILD_CACHE
1496 #    CONFIGURE_FLAGS
1497
1498 build_ofed() {
1499     local linux="$1"
1500     local ofed_version="$2"
1501
1502     # before lustre, build kernel-ib
1503     if [ -z "$ofed_version" -o "$ofed_version" = "inkernel" ]; then
1504         return 0
1505     fi
1506
1507     if $USE_BUILD_CACHE && [ -n "$REUSEBUILD" ]; then
1508         local REUSE_SIGNATURE=$({ echo "$ofed_version";
1509                                   echo "$(find_linux_release ${linux})";
1510                                   cat "${linux}/include/linux/autoconf.h"; } |
1511                                 md5sum | cut -d" " -f1)
1512         # see if we can link to the reuse pool
1513         # XXX - hrm.  i'm not convinced this doesn't belong in the reuse
1514         #       "library"
1515         local CAN_LINK_FOR_REUSE=false
1516         touch $REUSEBUILD/$$
1517         if cp -al $REUSEBUILD/$$ $TOPDIR/; then
1518             CAN_LINK_FOR_REUSE=true
1519         fi
1520         rm $REUSEBUILD/$$
1521     fi
1522
1523     if ! $USE_BUILD_CACHE || ! reuse ofed "$TOPDIR" "$CAN_LINK_FOR_REUSE" \
1524                                    "$REUSE_SIGNATURE"; then
1525         if [ -n "$REUSE_SIGNATURE" ]; then
1526             # stash away the existing built articles for a moment
1527             mkdir bak
1528             mv {BUILD,{S,}RPMS,S{OURCE,PEC}S} bak
1529             function mv_back {
1530                 pushd bak
1531                 find . | cpio -pudlm ..
1532                 popd
1533                 rm -rf bak
1534             }
1535             create_rpmbuild_dirs
1536         fi
1537         # build it
1538         build_kernel_ib "${linux}"
1539
1540         if [ -z "$REUSE_SIGNATURE" ]; then
1541             echo "No reuse signature was caculated so not storing the built ofed"
1542         else
1543             # store the resulting RPM build tree for future use
1544             echo "Storing the built ofed for future reuse"
1545             if ! store_for_reuse "$TOPDIR/{SPECS,SOURCES,BUILD,SRPMS,RPMS}" \
1546                                  "ofed" "$REUSEBUILD" "$REUSE_SIGNATURE" \
1547                                  "$CAN_LINK_FOR_REUSE"; then
1548                 error "Failed to store OFED RPMS for reuse"
1549                 mv_back
1550                 return 1
1551             fi
1552             # put the stuff we stashed away back
1553             mv_back
1554         fi
1555     fi
1556
1557     pushd "$TOPDIR" >/dev/null
1558     rm -rf kernel-ib-devel
1559     mkdir kernel-ib-devel
1560     cd kernel-ib-devel
1561     # the actual ofed RPMs don't have the -rc$n or -$date string appened that
1562     # might be present on the file
1563     local linuxrelease=$(find_linux_release "$linux")
1564     ofed_version=$(echo $ofed_version |
1565                    sed -re 's/-(20[0-9]{6,6}-[0-9]{4,4}|rc[0-9]*)$//')
1566     local rpm=$(ls $TOPDIR/RPMS/*/kernel-ib-devel-${ofed_version}-${linuxrelease//-/_}.*.rpm)
1567     rpm2cpio -itv < $rpm | cpio -id
1568     CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
1569     popd >/dev/null
1570
1571 }
1572
1573 build_with_srpm() {
1574
1575     if ! $PATCHLESS; then
1576         local kernel_extra_version
1577         if ! kernel_extra_version=$(build_kernel_with_srpm ${STDOUT}); then
1578             fatal 1 "Failed to build the kernel from it's SRPM"
1579         fi
1580
1581         for arch in $BUILD_ARCHS; do
1582             local kernel_devel_rpm
1583             if ! kernel_devel_rpm=$(find_rpm "$TOPDIR/RPMS/$arch/" provides "^$(devel_kernel_name $KERNEL_LUSTRE_NAMING) ="); then
1584                 fatal 1 "Failed to find a kernel development RPM in $TOPDIR/RPMS/$arch/"
1585             fi
1586
1587             # install the -devel RPM in preparation for modules builds
1588             if ! lnxrel="$kernel_extra_version" unpack_linux_devel_rpm \
1589                            "$TOPDIR/RPMS/$arch/$kernel_devel_rpm"; then
1590                 fatal 1 "Could not find the Linux tree in $TOPDIR/RPMS/$arch/$kernel_devel_rpm"
1591             fi
1592         done
1593     else
1594         # need to find and unpack the vendor's own kernel-devel for patchless
1595         # client build
1596         local kernelrpm
1597         if ! kernelrpm=$(find_linux_rpm "-$DEVEL_KERNEL_TYPE"); then
1598             fatal 1 "Could not find the kernel-$DEVEL_KERNEL_TYPE RPM in ${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
1599         fi
1600         if ! lnxrel="$lnxrel" unpack_linux_devel_rpm "$kernelrpm" "-"; then
1601             fatal 1 "Could not find the Linux tree in $kernelrpm"
1602         fi
1603     fi
1604
1605     # ~sigh~  have to make copies of and modify some of the rpm
1606     # infrastructure files so that find-requires can find our unpacked
1607     # kernel-devel artifacts
1608     cp $RPM_HELPERS_DIR/{symset-table,find-requires{,.ksyms}} .
1609     FIND_REQUIRES="$(pwd)/find-requires"
1610     chmod 755 {symset-table,find-requires{,.ksyms}}
1611     local tmp="$(pwd)"
1612     tmp="${tmp//\//\\/}"
1613     ed find-requires <<EOF
1614 1a
1615 set -x
1616 .
1617 /|.*find-requires.ksyms/s/|/| bash -x/
1618 g/ [^ ]*\/\(find-requires\.ksyms\)/s// $tmp\/\1/g
1619 wq
1620 EOF
1621     ed find-requires.ksyms <<EOF
1622 1a
1623 set -x
1624 .
1625 g/\/.*\/\(symset-table\)/s//$tmp\/\1/g
1626 wq
1627 EOF
1628     ed symset-table <<EOF
1629 1a
1630 set -x
1631 .
1632 g/\(\/boot\/\)/s//$tmp\/reused\1/g
1633 g/\(\/usr\/src\/kernels\/\)/s//$tmp\/reused\1/g
1634 wq
1635 EOF
1636
1637     build_ofed "${LINUXOBJ:-$LINUX}" "$OFED_VERSION" ||
1638         fatal 1 "error building OFED"
1639
1640     if ! $PATCHLESS; then
1641         # only need RDAC for the server
1642         build_rdac "${LINUXOBJ:-$LINUX}" "$RDAC_VERSION" ||
1643             fatal 1 "error building RDAC"
1644     fi
1645
1646     build_mptlinux "${LINUXOBJ:-$LINUX}" "$MPTLINUX_VERSION" ||
1647         fatal 1 "error building mptlinux"
1648
1649     # now build Lustre
1650     if build_lustre "$LINUX" "$LINUXOBJ"; then
1651         # the build worked.  resolve any symlinked files (i.e. from reuse)
1652         # in RPMS/$arch to real files so that that that huge mess of
1653         # complication known as LTS can copy them yet somewhere else.
1654         # is it any wonder this whole process is so damn so?  anyone ever
1655         # heard of hardlinks?  it this cool new thing that allows you save
1656         # tons of time and space by creating... well you can go read about
1657         # them if you have not heard about them yet.
1658         # can i say how much the implemenation of all of this really impedes
1659         # RPM reuse?
1660         pushd RPMS/$TARGET_ARCH
1661             for file in *; do
1662                 if [ -h $file ]; then
1663                     cp $file foo
1664                     mv foo $file
1665                 fi
1666             done
1667         popd
1668     else
1669         return 1
1670     fi
1671
1672 }
1673
1674 create_rpmbuild_dirs() {
1675
1676     [ -d RPMS ] || mkdir RPMS
1677     for arch in $BUILD_ARCHS; do
1678         if [[ $arch = i?86 ]]; then
1679             # some stupidity in the sles11 kernel spec requires an RPMS/i386
1680             # even if the target arch is i686
1681             [ -d RPMS/i386 ] || mkdir RPMS/i386
1682         fi
1683         [ -d RPMS/$arch ] || mkdir RPMS/$arch
1684     done
1685     [ -d BUILD ] || mkdir BUILD
1686     [ -d SOURCES ] || mkdir SOURCES
1687     [ -d SPECS ] || mkdir SPECS
1688     [ -d SRPMS ] || mkdir SRPMS
1689
1690 }
1691
1692 new_list() {
1693
1694     echo ""
1695
1696 }
1697
1698 add_list() {
1699     local list="$1"
1700     local item="$2"
1701
1702     echo "$list $item"
1703
1704 }
1705
1706 is_list_member() {
1707     local list="$1"
1708     local item="$2"
1709
1710     [[ $list\  == *\ $item\ * ]]
1711
1712 }
1713
1714 #########################################################################
1715 # Generate a backtrace through the call stack.
1716 #
1717 # Input: None
1718 # Output: None
1719 #########################################################################
1720 backtrace() {
1721     local strip=${1:-1}
1722
1723     local funcname="" sourcefile="" lineno="" n
1724
1725     echo "Call stack: (most recent first)"
1726     for (( n = $strip ; n < ${#FUNCNAME[@]} ; ++n )) ; do
1727         funcname=${FUNCNAME[$n - 1]}
1728         sourcefile=$(basename ${BASH_SOURCE[$n]})
1729         lineno=${BASH_LINENO[$n - 1]}
1730         if [ $n = 1 ]; then
1731             let lineno-=11
1732         fi
1733         # Display function arguments
1734         if [[ ! -z "${BASH_ARGV[@]}" ]]; then
1735             local args newarg j p=0
1736             for (( j = ${BASH_ARGC[$n - 1]}; j > 0; j-- )); do
1737                 newarg=${BASH_ARGV[$j + $p - 1]}
1738                 args="${args:+${args} }'${newarg}'"
1739             done
1740             let p+=${BASH_ARGC[$n - 1]}
1741         fi
1742         echo "  ${funcname} ${args:+${args} }at ${sourcefile}:${lineno}"
1743     done
1744
1745     echo
1746     echo "BEGIN BACKTRACE"
1747
1748     #echo ${BASH_LINENO[*]}
1749     #echo ${BASH_SOURCE[*]}
1750     #echo ${FUNCNAME[*]}
1751     local i=$((${#FUNCNAME[@]} - 1))
1752     while [ $i -ge 0 ]; do
1753         local lineno=${BASH_LINENO[$i]}
1754         if [ $i = 0 ]; then
1755             let lineno-=11
1756         fi
1757         local SOURCELINE="${BASH_SOURCE[$i + 1]}:${lineno}"
1758         # Can't figure out how to get function args from other frames...
1759         local FUNCTION="${FUNCNAME[$i]}()"
1760         echo "$SOURCELINE:$FUNCTION"
1761         i=$((i - 1))
1762     done
1763
1764     echo "END BACKTRACE"
1765
1766     echo $BACKTRACE
1767
1768 }
1769
1770 seen_list=$(new_list)
1771 trap 'set +x;
1772 echo "An unexpected error has occurred at ${BASH_SOURCE[0]##*/}:$((LINENO-1)).
1773 Unfortunately the above line number in the message may or may not be correct,
1774 but details have been send to the lbuild maintainer.  Attempting to continue."; (echo "Untrapped error"
1775 echo
1776 # have we seen this one
1777 echo "checking seen list for ${BASH_SOURCE[0]}:${BASH_LINENO[0]}"
1778
1779 if is_list_member "$seen_list" "${BASH_SOURCE[0]}:${BASH_LINENO[0]}"; then
1780   echo "seen this one already"
1781 else
1782   seen_list=$(add_list "$seen_list" "${BASH_SOURCE[0]}:${BASH_LINENO[0]}")
1783 fi
1784 backtrace
1785 echo
1786 echo "Environment:"
1787 set
1788 ) | mail -s "Untrapped error at ${BASH_SOURCE[0]##*/}:$((LINENO-15)) on $HOSTNAME" brian@sun.com >&2; set $xtrace' ERR
1789 set -E
1790
1791 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
1792
1793 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 -- "$@")
1794
1795 if [ $? != 0 ]; then
1796     usage 1
1797 fi
1798
1799 eval set -- "$options"
1800
1801 while [ "$1" ]; do
1802     case "$1" in
1803         '')
1804             usage 1
1805             ;;
1806         --ccache)
1807             CCACHE='ccache'
1808             shift
1809             ;;
1810         -d)
1811             CVSROOT=$2
1812             shift 2
1813             ;;
1814         -D)
1815             DATE=$2
1816             shift 2
1817             ;;
1818         --external-patches)
1819             EXTERNAL_PATCHES=$2
1820             shift 2
1821             ;;
1822         --extraversion)
1823             EXTRA_VERSION=$2
1824             shift 2
1825             ;;
1826         --help | -h)
1827             usage 0
1828             ;;
1829         --kerneldir)
1830             KERNELDIR=$2
1831             shift 2
1832             ;;
1833         --kerneltree)
1834             if ! KERNELTREE=$(canon_path "$2"); then
1835                 fatal 1 "Could not determine the canonical location of $2"
1836             fi
1837             shift 2
1838             ;;
1839         --linux | --with-linux)
1840             if ! LINUX=$(canon_path "$2"); then
1841                 fatal 1 "Could not determine the canonical location of $2"
1842             fi
1843             shift 2
1844             ;;
1845         --distro)
1846             DISTRO=$2
1847             shift 2
1848             ;;
1849         --reuserpm)
1850             REUSERPM=$2
1851             shift 2
1852             ;;
1853         --reusebuild)
1854             if ! REUSEBUILD=$(canon_path "$2"); then
1855                 fatal 1 "Could not determine the canonical location of $2"
1856             fi
1857             shift 2
1858             ;;
1859         --norpm)
1860             NORPM=true
1861             shift
1862             ;;
1863         --ldiskfs)
1864             LDISKFSRPM=true
1865             shift
1866             ;;
1867         --patchless)
1868             PATCHLESS=true
1869             shift
1870             ;;
1871         --kernelrpm)
1872             if ! KERNELRPMSBASE=$(canon_path "$2"); then
1873                 fatal 1 "Could not determine the canonical location of $2"
1874             fi
1875             shift 2
1876             ;;
1877         --timestamp)
1878             TIMESTAMP=$2
1879             shift 2
1880             ;;
1881         --lustre)
1882             LUSTRE=$2
1883             shift 2
1884             ;;
1885         --nodownload)
1886             DOWNLOAD=false
1887             shift 1
1888             ;;
1889         --nosrc)
1890             DO_SRC=false
1891             shift 1
1892             ;;
1893         --ofed-version)
1894             OFED_VERSION="$2"
1895             shift 2
1896             ;;
1897         --publish)
1898             shift
1899             ;;
1900         --release)
1901             RELEASE=true
1902             shift
1903             ;;
1904         --src)
1905             DO_SRC=true
1906             shift 1
1907             ;;
1908         --stage)
1909             STAGEDIR=$2
1910             shift 2
1911             ;;
1912         --tag)
1913             TAG=$2
1914             shift 2
1915             ;;
1916         --target)
1917             TARGET=$2
1918             shift 2
1919             ;;
1920         --target-archs)
1921             TARGET_ARCHS=$2
1922             shift 2
1923             ;;
1924         --disable-datestamp)
1925             USE_DATESTAMP=
1926             shift
1927             ;;
1928         --xen)
1929             XEN=true
1930             shift
1931             ;;
1932         --set-value)
1933             eval $2
1934             shift 2
1935             ;;
1936         --)
1937             shift
1938             # there are actually some lustre configure flags that we need to
1939             # handle ourselves (but we still give them to configure)
1940             if [[ \ $@\  == *\ --disable-tests\ * ]]; then
1941                 LUSTRE_TESTS=false
1942             fi
1943             CONFIGURE_FLAGS=$@
1944             CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-liblustre --enable-liblustre-tests"
1945             break
1946             ;;
1947         *)
1948             usage 1 "Unrecognized option: $1"
1949             ;;
1950     esac
1951 done
1952
1953 check_options
1954
1955 unpack_lustre
1956
1957 load_target
1958
1959 if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1960     download_ofed
1961     unpack_ofed || fatal 1 "Error unpacking OFED tarball"
1962 fi
1963
1964 if [ -n "$MPTLINUX_VERSION" -a "$MPTLINUX_VERSION" != "inkernel" ]; then
1965     download_mptlinux
1966     unpack_mptlinux || fatal 1 "Error unpacking MPTLINUX distribution"
1967 fi
1968
1969 # make sure the RPM build environment is set up
1970 create_rpmbuild_dirs
1971
1972 if [ -n "$RDAC_VERSION" -a "$RDAC_VERSION" != "inkernel" ]; then
1973     download_rdac
1974     # we don't actually need to unpack this.  just put it in the SOURCES dir
1975     #unpack_rdac || fatal 1 "Error unpacking RDAC tarball"
1976     cp "$KERNELTREE/rdac-LINUX-${RDAC_VERSION}-source.tar.gz" ${TOPDIR}/SOURCES/ ||
1977         fatal 1 "Error copying RDAC source tarball to RPM SOURCES dir"
1978 fi
1979
1980 # if an unpacked kernel source tree was given on the command line
1981 # just build lustre with it (nothing distro kernel specific here)
1982 if [ -n "$LINUX" ]; then
1983     build_mptlinux "${LINUXOBJ:-$LINUX}" "$MPTLINUX_VERSION" ||
1984         fatal 1 "error building mptlinux"
1985     build_ofed "${LINUXOBJ:-$LINUX}" "$OFED_VERSION" ||
1986         fatal 1 "error building OFED"
1987     if ! $PATCHLESS; then
1988         build_rdac "${LINUXOBJ:-$LINUX}" "$RDAC_VERSION" ||
1989             fatal 1 "error building RDAC"
1990     fi
1991     build_lustre "$LINUX" "$LINUXOBJ"
1992 else
1993     if [ -f "${0%/*}/lbuild-$DISTRO" ]; then
1994         source ${0%/*}/lbuild-$DISTRO
1995
1996         build_with_srpm || fatal 1 "Failed to build_with_srpm"
1997     else
1998         EXTRA_VERSION_DELIMITER=${EXTRA_VERSION_DELIMITER:-"-"}
1999         source ${0%/*}/lbuild.old_school
2000
2001         old_school_download_kernel
2002
2003         build_success=false
2004         if $PATCHLESS; then
2005             patchless_build_sequence && build_success=true
2006         else
2007             [ "$DISTRO" = "sles9" ] && build_sequence_rpm_reuse && build_success=true
2008             if ! $build_success; then
2009                 build_sequence_reuse && build_success=true
2010                 if ! $build_success; then
2011                     build_sequence && build_success=true
2012                     if $build_success; then
2013                         store_for_reuse || echo "Cannot store for future reuse"
2014                     fi
2015                 fi
2016             fi
2017         fi
2018         ( $build_success ) || fatal 1 "Cannot build lustre"
2019     fi
2020 fi
2021
2022 stage