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