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