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