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