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