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