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