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