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