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