Whamcloud - gitweb
With 1.6 heading towards maintenance mode, use a dedicated branch of the
[fs/lustre-release.git] / build / lbuild
1 #!/bin/bash
2
3 # vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:
4
5 #set -x
6
7 TOPDIR=$PWD
8
9 # CVSROOT is inherited from the environment
10 KERNELDIR=
11 LINUX=
12 LUSTRE=
13 RELEASE=false
14 DO_SRC=0
15 DOWNLOAD=1
16 TAG=
17 CANONICAL_TARGET=
18 TARGET=
19 TARGET_ARCH=$(uname -m)
20 TARGET_ARCHS=
21 TARGET_ARCHS_ALL=$TARGET_ARCH
22 [ "$TARGET_ARCH" = "i686" ] && TARGET_ARCHS_ALL="i686 i586 i386"
23 CONFIGURE_FLAGS=
24 EXTERNAL_PATCHES=
25 EXTRA_VERSION=
26 LUSTRE_EXTRA_VERSION=
27 STAGEDIR=
28 TMPDIR=${TMPDIR:-"/var/tmp"}
29 TIMESTAMP=
30 REUSERPM=
31 REUSEBUILD=
32 NORPM=false
33 LDISKFSRPM=true
34 SKIPLDISKFSRPM="v1_4_* b1_4"
35 SMPTYPES="smp bigsmp default ''"
36 KERNCONFSMPTYPE=
37 PATCHLESS=false
38 XEN=false
39 LINUXOBJ=
40 REUSEDKERNELMASK=
41 DISTRO=
42 KERNELTREE=
43
44 # patchless build
45 KERNELRPMSBASE=
46 KERNELRPM=
47 KERNELSOURCERPM=
48 RPMSMPTYPE=
49 KERNELRPMCONFIG=
50 KERNELRPMRELEASE=
51 KERNELCOMPILEDIR=
52
53 # from target file
54 KERNEL=
55 SERIES=
56 CONFIG=
57 VERSION=
58
59 RHBUILD=0
60 SUSEBUILD=0
61 LINUX26=0
62 SUSEBUILD=0
63
64 BASE_ARCHS=
65 BIGMEM_ARCHS=
66 BOOT_ARCHS=
67 JENSEN_ARCHS=
68 SMP_ARCHS=
69 BIGSMP_ARCHS=
70 PSERIES64_ARCHS=
71 UP_ARCHS=
72
73 DATE=$(date)
74
75 USE_DATESTAMP=1
76 RPMBUILD=
77
78 export CC=${CC:-gcc}
79
80 # Readlink is not present on some older distributions: emulate it.
81 readlink() {
82     local path=$1 ll
83
84     if [ -L "$path" ]; then
85         ll="$(LC_ALL=C ls -l "$path" 2> /dev/null)" &&
86         echo "${ll/* -> }"
87     else
88         return 1
89     fi
90 }
91
92 cleanup()
93 {
94     true
95 }
96
97 error()
98 {
99     [ "$1" ] && echo -e "\n${0##*/}: $1"
100 }
101
102 fatal()
103 {
104     cleanup
105     error "$2"
106     exit $1
107 }
108
109 list_targets()
110 {
111     echo -n "Available targets:"
112     for target in $TOPDIR/lustre/lustre/kernel_patches/targets/*.target ; do
113         target_file=${target##*/}
114         echo -n " ${target_file%%.target}"
115     done
116     echo
117 }
118
119 usage()
120 {
121     cat <<EOF
122 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
123
124   -d CVSROOT
125     Specifies the CVS Root to use when pulling files from CVS.  The
126     environment variable \$CVSROOT is used if this option is not
127     present.
128
129   --external-patches=EXTERNAL_PATCHES
130     Directory similar to lustre/lustre/kernel_patches/ that lbuild should
131     look for seres and config files in before looking in the lustre
132     tree.
133
134   --extraversion=EXTRAVERSION
135     Text to use for the rpm release and kernel extraversion.
136
137   --kerneldir=KERNELDIR
138     Directory containing Linux source tarballs referenced by target
139     files.
140
141   --timestamp=TIMESTAMP
142     Date of building lustre in format YYYYMMDDhhmmss
143
144   --reuserpm=DIR
145     Try to reuse old kernel RPMs from DIR
146
147   --reusebuild=DIR
148     Try to reuse old kernel builds from DIR
149
150   --kernelrpm=DIR
151     Path to distro kernel RPM collection
152
153   --ccache
154     Use ccache
155
156   --norpm
157     Do not build RPMs (compile only mode)
158
159   --patchless
160     Build lustre client only
161
162   --distro=DISTRO
163     Which distro using. Autodetect by default
164
165   --kerneltree=KERNELTREE
166     Directory containing dirs with Linux source tarballs referenced by target
167     files. Dir names in format kernel version ('2.6.9', etc.)
168
169   --linux=LINUX
170     Directory of Linux kernel sources.  When this option is used, only
171     Lustre modules and userspace are built.
172
173   --lustre=LUSTRE
174     Path to an existing lustre source tarball to use instead of
175     pulling from CVS.
176
177   --nodownload
178     Do not try to download a kernel from downloads.lustre.org
179
180   --nosrc
181     Do not build a .src.rpm, a full kernel patch, or a patched kernel
182     tarball.
183
184   --ldiskfs
185     Do ldiskfs RPM. Now true by default
186
187   --publish
188     Unused.
189
190   --release
191     Specifies that the files generated do not include timestamps, and
192     that this is an official release.
193
194   --src
195     Build a .src.rpm, a full kernel patch, and a patched kernel tarball.
196
197   --stage=DIR
198     Directory used to stage packages for release.  RPMs will be placed
199     more or less in DIR/<target>-<arch>, and the tarball will be
200     placed in DIR.
201
202   --tag=TAG
203     A CVS branch/tag name to build from when pulling from CVS.
204
205   --target=TARGET
206     The name of the target to build.  The available targets are listed
207     below.
208
209   --target-archs=TARGET_ARCHS
210     A (space delimited) list of architectures to build.  By default,
211     all of the archs supported by the TARGET will be built, in
212     addition to a .src.rpm.  This option can limit those, for machines
213     that can only build certain archs or if you only want a certain
214     arch built (for testing, or a one-off kernel).
215
216     Also note that by using a non-"base" arch (eg, i386) only kernels
217     will be built - there will be no lustre-lite-utils package.
218
219   --disable-datestamp
220     Prevents the datestamp flag (-D) from being passed to cvs for 
221     checkouts. This is a workaround for a problem encountered when 
222     using lbuild with tinderbox.
223
224   --xen
225     Builds a Xen domX kernel.
226
227 EOF
228
229 #   list_targets
230
231     fatal "$1" "$2"
232 }
233
234 check_options()
235 {
236     if [ "$LUSTRE" ] ; then
237         [ -r "$LUSTRE" ] || \
238             usage 1 "Could not find Lustre source tarball '$LUSTRE'."
239     else
240         [ "$CVSROOT" ] || \
241             usage 1 "Either specify a CVS Root with -d, or a Lustre source tarball with --lustre."
242         [ "$TAG" ] || \
243             usage 1 "A branch/tag name must be specified with --tag when not building from a tarball."
244     fi
245
246     if [ -z "$LINUX" ] ; then
247         [ "$KERNELDIR" -o "$KERNELTREE" ] || \
248             usage 1 "A kernel directory must be specified with --kerneldir or --kerneltree."
249
250         [ -d "$KERNELDIR" -o -d "$KERNELTREE" ] || \
251             usage 1 "$KERNELDIR and $KERNELTREE are not a directory."
252
253         if ! $RELEASE; then
254             [ "$TAG" ] || \
255                 usage 1 "When building a snapshot, a tag name must be used."
256         fi
257
258         [ "$TARGET" ] || usage 1 "A target must be specified with --target."
259 #       TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target"
260 #       [ -r "$TARGET_FILE" ] || \
261 #               usage 1 "Target '$TARGET' was not found."
262     fi
263
264     case $TARGET in
265         2.6-rhel5)
266             CANONICAL_TARGET="rhel5"
267             ;;
268         2.6-rhel4)
269             CANONICAL_TARGET="rhel-2.6"
270             ;;
271         2.6-suse)
272             CANONICAL_TARGET="sles-2.6"
273             ;;
274         2.6-sles10)
275             CANONICAL_TARGET="sles10-2.6"
276             ;;
277         hp_pnnl-2.4)
278             CANONICAL_TARGET="hp-pnnl-2.4"
279             ;;
280         2.6-vanilla \
281             | suse-2.4.21-2 \
282             | rh-2.4 \
283             | rhel-2.4 \
284             | sles-2.4 \
285             | 2.6-patchless)
286                 CANONICAL_TARGET="$TARGET"
287                 ;;
288     esac
289
290     local timestampnodig=$(echo $TIMESTAMP | sed -e s/[0-9]*//g)
291     [ "$timestampnodig" = "" ] || TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M%S")
292     local timestamplength="${#TIMESTAMP}"
293     if [ $timestamplength -eq 12 ]; then
294         TIMESTAMP="${TIMESTAMP}00"
295     elif [ $timestamplength -ne 14 ]; then
296         TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M%S")
297     fi
298
299     RPMBUILD=$(which rpmbuild 2>/dev/null | head -1)
300     if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
301         RPMBUILD=$(which rpm 2>/dev/null | head -1)
302         if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
303             usage 1 "Could not find binary for making rpms (tried rpmbuild and rpm)."
304         fi
305     fi
306     
307     if [ -n "$CCACHE" ]; then 
308         which "$CCACHE" 2>/dev/null && export CCACHE && export CC="ccache gcc"
309         which "$DISTCC" 2>/dev/null && export DISTCC RPM_BUILD_NCPUS
310         local bindir="/cache/build/bin"
311         [ -d $bindir ] || mkdir -p $bindir
312         [ -d $bindir ] && rm ${bindir}/* > /dev/null 2>&1
313         which "$CCACHE" 2>/dev/null && [ -d $bindir ] && ln -s `which "$CCACHE"` ${bindir}/ccache
314         which "$CCACHE" 2>/dev/null && [ -d $bindir ] && ln -s `which "$CCACHE"` ${bindir}/cc
315         which "$CCACHE" 2>/dev/null && [ -d $bindir ] && ln -s `which "$CCACHE"` ${bindir}/gcc
316         [ -d $bindir ] && export PATH=$bindir:$PATH
317     fi
318
319     [ -z "$DISTRO" ] && autodetect_distro
320 }
321
322 #autodetect used Distro
323 autodetect_distro()
324 {
325     if [ -f /etc/SuSE-release ]; then
326         DISTRO=sles10
327     elif [ -f /etc/redhat-release ]; then
328         local distroname=$(head -1 /etc/redhat-release | grep -e "CentOS\|Red")
329         if [ ! "$distroname" = "" ]; then
330             local version=$(echo "$distroname" | sed -e s/[^0-9.]*//g | sed -e s/\\..*// )
331             distroname="rhel"
332             [ "$version" = "" ] || DISTRO="${distroname}${version}"
333         fi
334     fi
335     [ "$DISTRO" = "" ] && DISTRO="sles9" #default distro
336 }
337
338 uniqify()
339 {
340     echo $(echo "$*" | xargs -n 1 | sort -u)
341 }
342
343 build_tarball() {
344     local TARGET=$1
345     local SRPM=$2
346
347     if [ "$TARGET" = "rhel-2.6" -o "$TARGET" = "rhel-2.4" ]; then
348         local SPEC=""
349         if [ "$TARGET" = "rhel-2.6" ]; then
350             SPEC=kernel-2.6.spec
351             OLDCONFIG=nonint_oldconfig
352         elif [ "$TARGET" = "rhel-2.4" ]; then
353             SPEC=kernel-2.4.spec
354             OLDCONFIG=oldconfig
355         fi
356
357         RPMTOPDIR=$(mktemp -d $KERNELDIR/rpm_XXXXXX)
358         mkdir $RPMTOPDIR/BUILD/
359         rpm -ivh $KERNELDIR/$SRPM --define "_topdir $RPMTOPDIR" || \
360             { rm -rf $RPMTOPDIR; fatal 1 "Error installing kernel SRPM."; }
361         $RPMBUILD -bp --nodeps --target i686 $RPMTOPDIR/SPECS/$SPEC --define "_topdir $RPMTOPDIR"
362         pushd $RPMTOPDIR/BUILD/kernel-${lnxmaj}/linux-${lnxmaj} && {
363             make mrproper
364             cp configs/kernel-${lnxmaj}-i686-smp.config .config
365             if ! make $OLDCONFIG > /dev/null; then
366                 fatal 1 "error trying to make $OLDCONFIG while building a tarball from SRPM."
367             fi
368             make include/linux/version.h 
369             rm -f .config
370             cd ..
371             tar cjf $KERNEL_FILE linux-${lnxmaj}
372         }
373         popd
374         rm -rf $RPMTOPDIR
375     fi
376 }
377
378 download_and_build_tarball() {
379     local target=$1
380     local kernel_file=$2
381
382     local srpm=kernel-${lnxmaj}-${lnxrel}.src.rpm
383
384     echo "Downloading http://downloads.lustre.org/public/kernels/$target/old/$srpm..."
385     if ! wget -nv "http://downloads.lustre.org/public/kernels/$target/old/$srpm" \
386         -O "$KERNELDIR/$srpm" ; then
387         fatal 1 "Could not download target $kernel_file's kernel SRPM $srpm from downloads.lustre.org."
388     fi
389     [ -s "$KERNELDIR/$srpm" ] || {
390         rm -rf $KERNELDIR/$srpm
391         fatal 1 "Could not download target $kernel_file's kernel SRPM $srpm from downloads.lustre.org."
392     }
393
394     build_tarball $target $srpm
395 }
396
397 load_target()
398 {
399     EXTRA_VERSION_save="$EXTRA_VERSION"
400     for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches" ; do
401         TARGET_FILE="$patchesdir/targets/$TARGET.target"
402         [ -r "$TARGET_FILE" ] && break
403     done
404     [ -r "$TARGET_FILE" ] || \
405         fatal 1 "Target $TARGET was not found."
406
407     echo "Loading target config file $TARGET.target..."        
408
409     . "$TARGET_FILE"
410
411     # doesn't make any sense to build OFED for xen domX's
412     if $XEN; then
413         OFED_VERSION=""
414     fi
415
416     [ "$KERNEL"  ] || fatal 1 "Target $TARGET did not specify a kernel."
417     [ "$VERSION" ] || fatal 1 "Target $TARGET did not specify a kernel version."
418
419     #CC was overwriten in TARGET_FILE
420     which "$CCACHE" 2>/dev/null && export CCACHE && export CC="ccache gcc"
421
422     if [ ! "$KERNELTREE" = "" ] && [ -d "$KERNELTREE" ]; then
423         KERNELDIR="$KERNELTREE/${lnxmaj}"
424         [ -d "$KERNELDIR" ] || mkdir "$KERNELDIR"
425     fi
426
427     if [ "$KERNELDIR" ] ; then
428         KERNEL_FILE="$KERNELDIR/$KERNEL"
429         if [ ! -r "$KERNEL_FILE" ] ; then
430             # see if we have an SRPM we can build a tarball for
431             KERNEL_SRPM=kernel-${lnxmaj}-${lnxrel}.src.rpm
432             if [ -r "$KERNELDIR/$KERNEL_SRPM" ] ; then
433                 build_tarball $CANONICAL_TARGET $KERNEL_SRPM
434             else
435                 if (( $DOWNLOAD )) ; then
436                     echo "Downloading http://downloads.lustre.org/public/kernels/$DISTRO/old/$KERNEL..."
437                     if ! wget -nv "http://downloads.lustre.org/public/kernels/$DISTRO/old/$KERNEL" -O "$KERNELDIR/$KERNEL" ; then
438                         # see if we can do it with an SRPM from the download site
439                         download_and_build_tarball $CANONICAL_TARGET $KERNEL_FILE
440                     else
441                         [ -s "$KERNELDIR/$KERNEL" ] || {
442                             rm -rf "$KERNELDIR/$KERNEL"
443                             fatal 1 "Target $TARGET's kernel $KERNEL not found in directory $KERNELDIR."
444                         }
445                     fi
446                 else
447                     fatal 1 "Target $TARGET's kernel file $KERNEL not found in kernel directory $KERNELDIR."
448                 fi
449             fi
450         fi
451         if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ] && \
452            [ ! -r "$KERNELTREE/../OFED-${OFED_VERSION}.tgz" ] ; then
453             if (( $DOWNLOAD )) ; then
454                 local location="http://downloads.lustre.org/public/OFED/"
455                 echo "Downloading $location/OFED-${OFED_VERSION}.tgz..."
456                 if ! wget -nv "$location/OFED-${OFED_VERSION}.tgz" \
457                     -O "$KERNELTREE/../OFED-${OFED_VERSION}.tgz" ; then
458                     fatal 1 "Could not download OFED-${OFED_VERSION}.tgz from downloads.lustre.org."
459                 fi
460             else
461                 fatal 1 "OFED-${OFED_VERSION}.tgz not found in kernel directory $KERNELDIR."
462             fi
463         fi
464     fi
465
466     if [ "$SERIES" ] ; then
467         for series in $SERIES ; do
468             for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches" ; do
469                 [ -r "$patchesdir/series/$series" ] && continue 2
470             done
471             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."
472         done
473     fi
474
475     local XENPOSTFIX=""
476     if $XEN; then
477         XENPOSTFIX="-xen"
478     fi
479     if [ -f $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET-$TARGET_ARCH.config ]; then
480         CONFIG_FILE="$TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET$XENPOSTFIX-$TARGET_ARCH.config"
481     fi
482     local smptype
483     for smptype in $SMPTYPES; do
484         [ "$smptype" = "''" ] && smptype=
485         if [ -f $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET-$TARGET_ARCH-${smptype}.config ]; then
486         CONFIG_FILE="$TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET$XENPOSTFIX-$TARGET_ARCH-${smptype}.config"
487         KERNCONFSMPTYPE=$smptype
488     fi
489     done
490
491     local lnxrelnew=${lnxrel//-/_}
492
493     if ! $PATCHLESS && [ ! -f "$CONFIG_FILE" ]; then
494         fatal 1 "Config file for target $TARGET missing from $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/."
495     fi
496
497     if [ "$EXTRA_VERSION_save" ] ; then
498         EXTRA_VERSION="$EXTRA_VERSION_save"
499     elif ! $RELEASE; then
500         # if there is no patch series, then this is not a lustre specific
501         # kernel.  don't make it look like one
502         if [ -n "$SERIES" ]; then
503             #remove the @VERSION@ (lustre version)
504 #            EXTRA_VERSION=$(echo $EXTRA_VERSION | sed -e "s/\(.*_lustre\)\..*/\1/")
505 #            EXTRA_VERSION="${EXTRA_VERSION}-${TAG}.${TIMESTAMP}"
506             ! ( $PATCHLESS ) && EXTRA_VERSION="${EXTRA_VERSION}.${TIMESTAMP}"
507         fi
508     fi
509     # EXTRA_VERSION=${EXTRA_VERSION//-/_}
510
511     [ -z $REUSEDKERNELMASK ] && \
512         # create mask for kernel RPM/builddir which could be reused
513         if $NORPM; then
514             REUSEDKERNELMASK=${KERNCONFSMPTYPE}-${lnxmaj}-${lnxrelnew}_lustre.${LUSTRE_VERSION}.*${TARGET_ARCH}
515         elif $RELEASE; then
516             REUSEDKERNELMASK=${KERNCONFSMPTYPE}-${lnxmaj}-${lnxrelnew}_lustre.${LUSTRE_VERSION}.${TARGET_ARCH}
517         else
518             REUSEDKERNELMASK=${KERNCONFSMPTYPE}-${lnxmaj}-${lnxrelnew}_lustre.${LUSTRE_VERSION}.${TIMESTAMP}.${TARGET_ARCH}
519         fi
520         # kernel-lustre-smp-2.6.9-55.0.2.EL_lustre.1.6.1.i686.rpm 
521
522     ALL_ARCHS="$BASE_ARCHS $BIGMEM_ARCHS $BOOT_ARCHS $JENSEN_ARCHS $SMP_ARCHS $BIGSMP_ARCHS $PSERIES64_ARCHS $UP_ARCHS"
523
524     BUILD_ARCHS=
525     for arch in $(uniqify "$ALL_ARCHS") ; do
526         if [ -z "$TARGET_ARCHS" ] || echo "$TARGET_ARCHS" | grep -w "$arch" >/dev/null 2>/dev/null ; then
527             BUILD_ARCHS="$BUILD_ARCHS $arch"
528         fi
529     done
530     [ "$BUILD_ARCHS" ] || usage 1 "No available target archs to build."
531     echo "Building for: $BUILD_ARCHS"
532 }
533
534 tarflags()
535 {
536     case "$1" in
537         '')
538             fatal 1 "tarflags(): File name argument missing."
539             ;;
540         *.tar.gz | *.tgz)
541             echo 'zxf'
542             ;;
543         *.tar.bz2)
544             echo 'jxf'
545             ;;
546         *.tar)
547             echo 'xf'
548             ;;
549         *)
550             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
551             ;;
552     esac
553 }
554
555 untar()
556 {
557     echo "Untarring ${1##*/}..."
558     tar $(tarflags "$1") "$1"
559 }
560
561 unpack_ofed()
562 {
563     untar "$KERNELTREE/../OFED-${OFED_VERSION}.tgz"
564     [ -d OFED ] || ln -sf OFED-[0-9].[0-9]* OFED
565     pwd
566     ls -ld OFED OFED-[0-9].[0-9]*
567     ls -l OFED OFED-[0-9].[0-9]*
568 }
569
570 unpack_lustre()
571 {
572     DIRNAME="lustre-$TAG-$TIMESTAMP"
573     if [ "$LUSTRE" ] ; then
574         untar "$LUSTRE"
575         [ -d lustre ] || ln -sf lustre-[0-9].[0-9]* lustre
576     else
577         if [ "$USE_DATESTAMP" ]; then
578             DATESTAMP="-D '$DATE'"
579         else
580             DATESTAMP=""
581         fi            
582
583         cvs -d "$CVSROOT" -qz3 co $DATESTAMP -d "$DIRNAME" lustre || \
584             fatal 1 "There was an error checking out toplevel Lustre from CVS."
585         pushd "$DIRNAME" > /dev/null
586         ./lustrecvs "$TAG" || \
587             fatal 1 "There was an error checking out Lustre/Portals/Build from CVS."
588         echo "Creating lustre tarball..."
589         sh autogen.sh || fatal 1 "There was an error running autogen.sh."
590         ./configure --disable-{modules,utils,liblustre,tests,doc} || \
591             fatal 1 "There was an error running ./configure to create makefiles."
592         make dist || fatal 1 "There was an error running 'make dist'."
593         popd > /dev/null
594         fname=$(basename $DIRNAME/lustre-*.tar.gz)
595         cp $DIRNAME/$fname . || fatal 1 "There was an error copying lustre tarball."
596         LUSTRE="$PWD/$fname"
597         ln -sf "$DIRNAME" lustre
598     fi
599 }
600
601 unpack_linux()
602 {
603     untar "$KERNEL_FILE"
604     [ -d linux ] || ln -sf linux* linux
605 }
606
607 patch_linux()
608 {
609     [ "$SERIES" ] || return 0
610     FULL_PATCH="$PWD/lustre-kernel-${TARGET}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}.patch"
611     [ -f "$FULL_PATCH" ] && rm -f "$FULL_PATCH"
612     pushd linux >/dev/null
613     for series in $SERIES ; do
614         echo -n "Applying series $series:"
615         for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches" ; do
616             [ -r "$patchesdir/series/$series" ] || continue
617             SERIES_FILE="$patchesdir/series/$series"
618             for patch in $(<"$SERIES_FILE") ; do
619                 echo -n " $patch"
620                 PATCH_FILE="$patchesdir/patches/$patch"
621                 [ -r "$PATCH_FILE" ] || \
622                     fatal 1 "Patch $patch does not exist in Lustre tree."
623                 cat "$PATCH_FILE" >> "$FULL_PATCH" || \
624                     fatal 1 "Error adding patch $patch to full patch."
625                 patch -s -p1 < "$PATCH_FILE" || fatal 1 "Error applying patch $patch."
626             done
627             break
628         done
629         echo
630     done
631     popd >/dev/null
632     echo "Full patch has been saved in ${FULL_PATCH##*/}."
633     echo "Replacing .config files..."
634     [ -d linux/configs ] || mkdir linux/configs || \
635         fatal 1 "Error creating configs directory."
636     rm -f linux/configs/*
637     copysuccess=0
638     for patchesdir in "$EXTERNAL_PATCHES" "lustre/lustre/kernel_patches" ; do
639         [ "$patchesdir" ] && \
640             cp -v $patchesdir/kernel_configs/kernel-${VERSION}-${TARGET}*.config linux/configs/ >/dev/null && copysuccess=1
641     done
642     [ "$copysuccess" = "1" ] || \
643         fatal 1 "Error copying in kernel configs."
644 }
645
646 pack_linux()
647 {
648     TARBALL="$(readlink linux)-$EXTRA_VERSION.tar.gz"
649     echo "Creating patched linux tarball $TARBALL..."
650     tar zcf "$TARBALL" "$(readlink linux)" \
651         --exclude "CVS" --exclude ".cvsignore" || \
652         --exclude "*.orig" --exclude "*~" --exclude "*.rej" || \
653         fatal 1 "Error creating patched Linux tarball."
654 }
655
656 clean_linux()
657 {
658     [ -d linux ] || return 0
659     echo "Cleaning linux..."
660     [ -L linux ] && rm -rf $(readlink linux)
661     rm -rf linux
662 }
663
664 prep_kernel_build()
665 {
666     local lmakeopts="\"\""
667     if $XEN; then
668         lmakeopts="--xen"
669     fi
670     # make .spec file
671     ENABLE_INIT_SCRIPTS=""
672     sed \
673         -e "s^@BASE_ARCHS@^$BASE_ARCHS^g" \
674         -e "s^@BIGMEM_ARCHS@^$BIGMEM_ARCHS^g" \
675         -e "s^@BIGSMP_ARCHS@^$BIGSMP_ARCHS^g" \
676         -e "s^@BOOT_ARCHS@^$BOOT_ARCHS^g" \
677         -e "s^@CONFIGURE_FLAGS@^$CONFIGURE_FLAGS^g" \
678         -e "s^@ENABLE_INIT_SCRIPTS@^$ENABLE_INIT_SCRIPTS^g" \
679         -e "s^@JENSEN_ARCHS@^$BOOT_ARCHS^g" \
680         -e "s^@KERNEL_EXTRA_VERSION@^$EXTRA_VERSION^g" \
681         -e "s^@KERNEL_EXTRA_VERSION_DELIMITER@^$EXTRA_VERSION_DELIMITER^g" \
682         -e "s^@KERNEL_TARGET_DELIMITER@^$TARGET_DELIMITER^g" \
683         -e "s^@KERNEL_RELEASE@^${EXTRA_VERSION//-/_}^g" \
684         -e "s^@KERNEL_SOURCE@^$KERNEL^g" \
685         -e "s^@KERNEL_VERSION@^$VERSION^g" \
686         -e "s^@LINUX26@^$LINUX26^g" \
687         -e "s^@LUSTRE_SOURCE@^${LUSTRE##*/}^g" \
688         -e "s^@LUSTRE_TARGET@^$TARGET^g" \
689         -e "s^@PSERIES64_ARCHS@^$PSERIES64_ARCHS^g" \
690         -e "s^@RHBUILD@^$RHBUILD^g" \
691         -e "s^@SMP_ARCHS@^$SMP_ARCHS^g" \
692         -e "s^@SUSEBUILD@^$SUSEBUILD^g" \
693         -e "s^@UP_ARCHS@^$UP_ARCHS^g" \
694         -e "s^@LMAKEOPTS@^$lmakeopts^g" \
695         < $TOPDIR/lustre/build/lustre-kernel-2.4.spec.in \
696         > lustre-kernel-2.4.spec
697     [ -d SRPMS ] || mkdir SRPMS
698     [ -d RPMS ] || mkdir RPMS
699     [ -d BUILD ] || mkdir BUILD
700     [ -d SOURCES ] || mkdir SOURCES
701     for script in linux-{rhconfig.h,merge-config.awk,merge-modules.awk} \
702         suse-{functions.sh,post.sh,postun.sh,trigger-script.sh.in} \
703         sles8-{pre,post,postun,update_{INITRD_MODULES,rcfile_setting}}.sh ; do
704         cp $TOPDIR/lustre/build/$script SOURCES
705     done
706     cp "$LUSTRE" "$KERNEL_FILE" SOURCES
707     if [ "$EXTERNAL_PATCHES" -a -d "$EXTERNAL_PATCHES" ] ; then
708         tar zcf SOURCES/external-patches.tar.gz -C "$EXTERNAL_PATCHES" series targets patches kernel_configs
709     else
710         touch SOURCES/external-patches.tar.gz
711     fi
712 }
713
714 clean_lustre()
715 {
716     [ -d lustre ] || return 0
717     echo "Cleaning Lustre..."
718     [ -L lustre ] && rm -rf $(readlink lustre)
719     rm -rf lustre
720 }
721
722 build_kernel()
723 {
724     echo "Building kernel + Lustre RPMs for: $BUILD_ARCHS..."
725     targets=
726     for arch in $BUILD_ARCHS ; do
727         targets="--target $arch $targets"
728     done
729
730     local rpmbuildopt='-bb'
731 #    if $NORPM; then
732 #        rpmbuildopt='-bc'
733 #        echo NORPM mode. Only compiling.
734 #        echo "XXX: need to fix lmake - add options to do rpmbuild -bc instead of -bb"
735 #    fi
736
737     $RPMBUILD $targets $rpmbuildopt lustre-kernel-2.4.spec \
738         --define "_tmppath $TMPDIR" \
739         --define "_topdir $TOPDIR" || \
740         fatal 1 "Error building rpms for $BUILD_ARCHS."
741
742     if (( $DO_SRC )) ; then
743         $RPMBUILD -bs lustre-kernel-2.4.spec \
744             --define "_tmppath $TMPDIR" \
745             --define "_topdir $TOPDIR" || \
746             fatal 1 "Error building .src.rpm."
747     fi
748
749     ( $(skeep_ldiskfs_rpm $TAG) ) && return
750
751     pushd $TOPDIR/BUILD/lustre*/ldiskfs || return 255
752     make dist
753     if [ "$?" != "0" ] ; then
754         popd
755         return 255
756     fi
757     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
758
759     gen_lustre_version
760
761     local ldiskfs_spec=lustre-ldiskfs.spec
762     [ -f "$ldiskfs_spec" ] && sed \
763     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
764     < $ldiskfs_spec \
765     > ../lustre-ldiskfs.spec
766
767     $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
768         --define "_tmppath /var/tmp" \
769         --define "_topdir $TOPDIR"
770     if [ "$?" != "0" ] ; then
771         popd
772         return 255
773     fi
774
775     if (( $DO_SRC )) ; then
776             $RPMBUILD -bs ../lustre-ldiskfs.spec \
777             --define "_tmppath /var/tmp" \
778             --define "_topdir $TOPDIR"
779         if [ "$?" != "0" ] ; then
780             popd
781             return 255
782         fi
783     fi
784     popd
785 }
786
787 build_lustre()
788 {
789     [ -d SRPMS ] || mkdir SRPMS
790     [ -d RPMS ] || mkdir RPMS
791     [ -d BUILD ] || mkdir BUILD
792     [ -d SOURCES ] || mkdir SOURCES
793
794     cp "$LUSTRE" SOURCES
795
796     pushd lustre >/dev/null
797
798     echo "Building Lustre RPMs for: $BUILD_ARCHS..."
799     targets=
800     for arch in $BUILD_ARCHS ; do
801         targets="--target $arch $targets"
802     done
803
804     local confoptions="--with-linux=${LINUX}"
805     if $PATCHLESS; then
806         confoptions="--with-linux=${LINUX} --disable-server"
807     fi
808     if [ ! "$LINUXOBJ" = "" ]; then
809         confoptions="$confoptions --with-linux-obj=${LINUXOBJ}" 
810     fi
811     
812     ./configure $confoptions ${CONFIGURE_FLAGS}
813     if [ "$?" != "0" ] ; then
814         local saved_config="../config.log.$(date +%s)"
815         cp config.log $saved_config
816         chmod a+r $saved_config
817         echo "Saved config.log is at $saved_config"
818         popd
819         return 255
820     fi
821
822     gen_lustre_version
823
824     # hack. Somebody move build/lustre.spec to lustre.spec for b1_6
825     local lustre_spec=
826     [ -f lustre.spec ] && lustre_spec=lustre.spec
827     [ -f build/lustre.spec ] && lustre_spec=build/lustre.spec
828
829     [ -f "$lustre_spec" ] && sed \
830         -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
831         < $lustre_spec \
832         > ../lustre.spec
833
834     local rpmbuildopt='-bb'
835     if $NORPM; then
836         rpmbuildopt='-bc'
837         echo NORPM mode. Only compiling.
838     fi
839
840     $RPMBUILD $targets $rpmbuildopt ../lustre.spec \
841         ${PATCHLESS:+--define "lustre_name lustre-client"} \
842         --define "_tmppath $TMPDIR" \
843         --define "_topdir $TOPDIR" || \
844         fatal 1 "Error building rpms for $BUILD_ARCHS."
845
846     popd >/dev/null
847     ( $(skeep_ldiskfs_rpm $TAG) ) && return
848
849     pushd lustre/ldiskfs || return 255
850     make dist
851     if [ "$?" != "0" ] ; then
852         popd
853         return 255
854     fi
855     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
856
857     gen_lustre_version
858
859     local ldiskfs_spec=lustre-ldiskfs.spec
860     [ -f "$ldiskfs_spec" ] && sed \
861     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
862     < $ldiskfs_spec \
863     > ../lustre-ldiskfs.spec
864
865     $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
866         --define "_tmppath /var/tmp" \
867         --define "_topdir $TOPDIR"
868     if [ "$?" != "0" ] ; then
869         popd
870         return 255
871     fi
872
873     if (( $DO_SRC )) ; then
874             $RPMBUILD -bs ../lustre-ldiskfs.spec \
875             --define "_tmppath /var/tmp" \
876             --define "_topdir $TOPDIR"
877         if [ "$?" != "0" ] ; then
878             popd
879             return 255
880         fi
881     fi
882     popd
883 }
884
885 stage()
886 {
887     [ "$STAGEDIR" ] || return 0
888
889     for arch in $BUILD_ARCHS ; do
890         rpmdir="${STAGEDIR}/${CANONICAL_TARGET}-${arch}"
891         echo "${0##*/}: Copying RPMs into ${rpmdir}"
892         mkdir -p "${rpmdir}"
893         cp -v RPMS/${arch}/*.rpm "${rpmdir}"
894         if [ -d RPMS/noarch ] ; then
895             cp -v RPMS/noarch/*.rpm "${rpmdir}"
896         fi
897     done
898
899     cp -v "$LUSTRE" "$STAGEDIR"
900 }
901
902 #check if we need to build separate ldiskfs RPM
903 skeep_ldiskfs_rpm()
904 {
905         local tag=$1
906         local skip=false
907         if ! $LDISKFSRPM; then
908             skip=true
909         elif $PATCHLESS; then
910             skip=true
911         else
912             for skiptag in $SKIPLDISKFSRPM; do
913                 [[ $tag == $skiptag ]] && skip=true && break
914             done
915         fi
916         echo $skip
917 }
918
919 #get date of last changed target/config/series/patches
920 get_last_source_date()
921 {
922     local filelist="${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} \
923         $CONFIG_FILE"
924     local TOPDIRnew=$(echo ${TOPDIR} | sed -e s/\\//\\\\\\//g)
925     filelist="$filelist $( \
926         cat ${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} | \
927         sed -e s/^/${TOPDIRnew}\\/lustre\\/lustre\\/kernel_patches\\/patches\\// 2>&1)"
928     local sourcelastdate=$( find ${filelist} -name CVS -prune -o \
929                 -type f -printf "%T@\n" 2>&1 | sort | tail -1 )
930     is_integer $sourcelastdate && echo $sourcelastdate
931 }
932
933 #check if variable is integer
934 is_integer()
935 {
936     local invariable=$1
937     [ "$invariable" = "" ] && return 255
938     local invariableint=$( echo $invariable | sed -e s/[^0-9]//g )
939     [ "$invariable" = "$invariableint" ] || return 255
940 }
941
942 #generate LUSTRE_EXTRA_VERSION from EXTRA_VERSION
943 gen_lustre_version()
944 {
945     local smptype=smp
946     [ "$KERNCONFSMPTYPE" = "" ] || smptype=$KERNCONFSMPTYPE
947     [ "$RPMSMPTYPE" = "" ] || smptype=$RPMSMPTYPE
948
949     LUSTRE_EXTRA_VERSION="${lnxmaj}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}${TARGET_DELIMITER}${smptype}"
950     LUSTRE_EXTRA_VERSION=${LUSTRE_EXTRA_VERSION//-/_}
951 }
952
953 #store RPMs and/or BUILD dir for future reuse
954 store_for_reuse()
955 {
956     local rpmonly=$1
957     if [ ! "$REUSEBUILD" = "" ] && [ -d  "/$REUSEBUILD/" ] ; then
958         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || mkdir "${REUSEBUILD}/${TIMESTAMP}"
959         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || return 255
960     else
961         return 255
962     fi
963
964     local lnxrelnew=${lnxrel//-/_}
965     local EXTRA_VERSIONnew=${EXTRA_VERSION//-/_}
966     local KERNELRPMnew=$(basename "$KERNELRPM")
967     if [ ! "$rpmonly" = "rpmonly" ]; then
968             local builddir=
969         if [ ! "$KERNELCOMPILEDIR" = "" ]; then
970             builddir="$KERNELCOMPILEDIR"
971         else
972             builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}"
973             [ "$KERNELCOMPILEDIR" = "" ] || builddir="$KERNELCOMPILEDIR"
974             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}.${lnxrel}"
975             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}-${lnxrel}"
976             if [ ! -d "$builddir" ]; then
977                 pushd "BUILD/lustre-kernel-${lnxmaj}/lustre/" || return 255
978                 local basebuilddir=$(ls -d linux-${lnxmaj}* | head -1)
979                 [ "$basebuilddir" = "" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/${basebuilddir}"
980                 popd
981             fi
982         fi
983         [ -d "$builddir" ] || return 255
984         local dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}"
985         ( $PATCHLESS ) && dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-$KERNELRPMnew" && \
986                 dstdir="${dstdir%.rpm}"
987         [ -d "$dstdir" ] && rm -rf "$dstdir"
988         mv "${builddir}" "$dstdir" || return 255
989         if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
990             # move the OFED kernel-ib-devel tree as well
991             mv "${builddir%/*}/kernel-ib-devel/usr/src/ofa_kernel" "${dstdir%/*}" || return 255
992         fi
993     fi
994     #store kernel rpm
995     local kernelrpmname="kernel-lustre-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
996     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSNnew}.${TARGET_ARCH}.rpm"
997     ( $PATCHLESS ) && [ -f "$KERNELRPM" ] && kernelrpmname="$KERNELRPMnew"
998     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
999         echo "RPM already exist in store directory tree"
1000     else
1001         [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
1002     fi
1003     #store kernel source rpm
1004     kernelrpmname="kernel-lustre-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
1005     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
1006     ( $PATCHLESS ) && [ -f "$KERNELSOURCERPM" ] && kernelrpmname=$(basename "$KERNELSOURCERPM")
1007     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
1008         echo "RPM already exist in store directory tree"
1009     else
1010             [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
1011     fi
1012     if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1013         # store kernel-ib RPMs
1014         local rpmname
1015         for rpmname in "kernel-ib" "kernel-ib-devel"; do
1016             rpmname="${rpmname}-${OFED_VERSION}"
1017             if $PATCHLESS; then
1018                 rpmname="${rpmname}-${LINUXRELEASE//-/_}"
1019             else
1020                 rpmname="${rpmname}-${lnxmaj}${EXTRA_VERSION_DELIMITER//-/_}${EXTRA_VERSIONnew}${TARGET_DELIMITER//-/_}${KERNCONFSMPTYPE}"
1021             fi
1022             rpmname="${rpmname}.${TARGET_ARCH}.rpm"
1023             if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${rpmname}" ]; then
1024                 echo "RPM already exist in store directory tree"
1025             else
1026                 [ -f "RPMS/${TARGET_ARCH}/${rpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${rpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
1027             fi
1028         done
1029     fi
1030 }
1031
1032 set_rpm_smp_type()
1033 {
1034     local infact_arch=${TARGET_ARCH}
1035     RPMSMPTYPE=default
1036     [ "$infact_arch" == "i586" ] && infact_arch="i686"
1037     for smp_type in $SMP_ARCHS; do
1038         [ $infact_arch == $smp_type ] && RPMSMPTYPE=smp && break
1039     done
1040     for smp_type in $BIGSMP_ARCHS; do
1041         [ $infact_arch == $smp_type ] && RPMSMPTYPE=bigsmp && break
1042     done
1043 }
1044
1045 # This function takes a linux source pool and digs out the linux release
1046 # from it
1047 find_linux_release() {
1048     local SRCDIR="$1"
1049
1050     local LINUXRELEASEHEADER=$SRCDIR/include/linux/version.h
1051     if [ -s $SRCDIR/include/linux/utsrelease.h ]; then
1052         LINUXRELEASEHEADER=$SRCDIR/include/linux/utsrelease.h
1053     fi
1054
1055     sed -ne 's/#define UTS_RELEASE "\(.*\)"$/\1/p' $LINUXRELEASEHEADER
1056
1057 }
1058
1059 #unpack kernel(/source/devel) RPM
1060 unpack_linux_rpm()
1061 {
1062     local prefix=$1
1063     local delimiter=${2:-"-"}
1064     local pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
1065     local kernelbinaryrpm=
1066     [ -d $pathtorpms ] || return 255
1067     local rpmfile=
1068     local wanted_kernel="${lnxmaj}${delimiter}${lnxrel}"
1069     for arch in $TARGET_ARCHS_ALL; do
1070         for rpm in ${pathtorpms}/${arch}/*.rpm; do
1071             local provides=$(rpm -q --provides -p $rpm)
1072             if (echo $provides | grep -q "kernel${prefix} = $wanted_kernel"); then
1073                 KERNELRPM=$rpm
1074             fi
1075         done
1076         [ -f "$KERNELRPM" ] && TARGET_ARCH="$arch" && BUILD_ARCHS="$arch" && break
1077     done
1078
1079     # bleah
1080     set_rpm_smp_type
1081
1082     [ -f "$KERNELRPM" ] || return 255
1083     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
1084     pushd $TOPDIR/reused || return 255
1085     RC=0
1086     rpm2cpio < "$KERNELRPM" | cpio -idc > /dev/null 2>&1
1087     if [ ${PIPESTATUS[0]} -eq 0 ]; then
1088         # RHEL-style and SLES-style rpms
1089         local paths="kernels/${lnxmaj}${delimiter}${lnxrel}-${TARGET_ARCH} linux-${lnxmaj}${delimiter}${lnxrel}"
1090
1091         for path in $paths; do
1092             local src='usr/src'
1093
1094             if [ -d "$src/$path/" ]; then
1095                 LINUX="$(pwd)/$src/$path"
1096             fi
1097             # SLES has a separate -obj tree
1098             if [ -d "$src/${path}-obj" ]; then
1099                 src="$src/${path}-obj"
1100                 local objects="$TARGET_ARCH/$RPMSMPTYPE"
1101
1102                 # Novell, are you *TRYING* to make life hard for me?
1103                 if [ -d "$src/powerpc" ]; then
1104                     objects="powerpc/$TARGET_ARCH"
1105                 elif [ $TARGET_ARCH == 'i686' ]; then
1106                     objects="i386/$RPMSMPTYPE"
1107                 fi
1108
1109                 LINUXOBJ="$(pwd)/$src/$objects"
1110             fi
1111         done
1112         if [ -z "$LINUX" ]; then
1113             RC=255
1114         else
1115             # dig out the release version
1116             LINUXRELEASE=$(find_linux_release ${LINUXOBJ:-$LINUX})
1117             if [ -z "$LINUXRELEASE" ]; then
1118                 echo "Failed to find linux release in ${LINUXOBJ:-$LINUX}"
1119                 RC=255
1120             fi
1121         fi
1122     else
1123         RC=255
1124     fi
1125     popd
1126     return $RC
1127 }
1128
1129 #look for kernel source RPM
1130 find_linux_source_rpm()
1131 {
1132     local rpmfile=
1133     local findarch=true
1134     local arch=
1135     local pathtorpms=
1136     [ ! "$TARGET_ARCH" = "" ] && arch=$TARGET_ARCH && findarch=false
1137     
1138     if ! $findarch; then
1139         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
1140         [ -d $pathtorpms ] || return 255
1141         case "$DISTRO" in
1142             rhel4)
1143                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
1144             ;;
1145             sles10)
1146                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
1147             ;;
1148             *)
1149                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
1150             ;;
1151         esac
1152         [ -f "${pathtorpms}/${rpmfile}" ] || return 255
1153         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
1154     else
1155         for arch in $TARGET_ARCHS_ALL; do
1156         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
1157         [ -d $pathtorpms ] || continue
1158         case "$DISTRO" in
1159             rhel4)
1160                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
1161             ;;
1162             sles10)
1163                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
1164             ;;
1165             *)
1166                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
1167             ;;
1168         esac
1169         [ -f "${pathtorpms}/${rpmfile}" ] || continue
1170         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
1171         TARGET_ARCH=${arch}
1172         break
1173         done
1174     fi
1175     [ -f "${KERNELSOURCERPM}" ] || return 255
1176 }
1177
1178 #unpack and make symlinks for reusing kernel RPM
1179 reuse_kernel_rpm()
1180 {
1181     local pathtorpm=$1
1182     local pathtokernelibrpm=$2
1183     [ "$pathtorpm" = "" ] && return 255
1184     [ -f "$pathtorpm" ] || return 255
1185     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
1186     pushd $TOPDIR/reused || return 255
1187
1188     rpm2cpio < $pathtorpm | cpio -idc
1189     [ ${PIPESTATUS[0]} -eq 0 ] || return 255
1190
1191     if [ -n "$pathtokernelibrpm" ] && [ -f "$pathtokernelibrpm" ]; then
1192         rpm2cpio < $pathtokernelibrpm | cpio -idc
1193         [ ${PIPESTATUS[0]} -eq 0 -o ${PIPESTATUS[1]} -eq 0 ] || return 255
1194         CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
1195     fi
1196
1197     local smptype=
1198     if pushd usr/src/linux-*-obj/${TARGET_ARCH}; then
1199         local smptypes="$SMPTYPES"
1200         [ "$RPMSMPTYPE" = "" ] || smptypes=$RPMSMPTYPE
1201         ( ! $PATCHLESS ) && [ ! "$KERNCONFSMPTYPE" = "" ] && smptypes="$KERNCONFSMPTYPE"
1202         local cursmptype=
1203         for cursmptype in $smptypes; do
1204             [ "$cursmptype" = "''" ] && continue
1205             [ -d $cursmptype ] && smptype=$cursmptype
1206             [ -d $smptype ] && break
1207         done
1208         popd
1209     fi
1210     if [ "${smptype}" = "" ]; then
1211         popd
1212         return 255 # cannot detect smp type
1213     fi
1214     if pushd usr/src/linux-*-obj/${TARGET_ARCH}/$smptype/include2; then
1215         local base=$(readlink asm)
1216         if [ ! -d "/${base}/" ]; then
1217             rm -f asm
1218             base=$(basename "$base")
1219             if pushd ../../../../linux-*/include; then
1220                 local lsrc=$(pwd)
1221                 popd
1222                 [ -d "$lsrc/${base}" ] && ln -s $lsrc/${base} asm
1223             fi
1224         fi
1225         popd
1226         read a b < <(echo $(pwd)/usr/src/linux-*)
1227         if [[ $a == $(pwd)/* && $b = $(pwd)/* ]]; then
1228             cp -f $a/include/linux/config.h $b/${TARGET_ARCH}/$smptype/include/linux/
1229             cp $b/${TARGET_ARCH}/$smptype/.config $a/
1230             [ -f "$b/${TARGET_ARCH}/$smptype/.config" ] && KERNELRPMCONFIG="$b/${TARGET_ARCH}/$smptype/.config"
1231             cp $b/${TARGET_ARCH}/$smptype/.kernelrelease $a/
1232             [ -f "$b/${TARGET_ARCH}/$smptype/.kernelrelease" ] && KERNELRPMRELEASE="$b/${TARGET_ARCH}/$smptype/.kernelrelease"
1233             LINUX=$a
1234             LINUXOBJ=$b/${TARGET_ARCH}/$smptype
1235 #            local fname=$(basename $kernel_rpm)
1236 #            KERNELRPMSDIR=${kernel_rpm%$fname}
1237         fi
1238     fi
1239     popd
1240     [ "$LINUX" = "" ] && return 255
1241     [ -d "$LINUX" ] || return 255
1242 }
1243
1244 #build linux kernel rpm
1245 build_linux_rpm()
1246 {
1247     pushd $LINUX || return 255
1248     make binrpm-pkg || ( popd ; return 255 )
1249     local addlnxrel=
1250     [ -f ".version" ] && addlnxrel="-$(cat .version)"
1251     popd
1252     local arch=
1253     for arch in $TARGET_ARCHS_ALL; do
1254         [ -f "/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
1255             KERNELRPM="/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
1256             TARGET_ARCH="$arch"
1257         [ -f "/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
1258             KERNELRPM="/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
1259             TARGET_ARCH="$arch"
1260     done
1261     [ "$KERNELRPM" = "" ] || return
1262     return 255
1263 }
1264
1265 #build linux kernel
1266 build_linux()
1267 {
1268     local nofullmake=$1
1269     local nocopykernel=$2
1270     pushd $LINUX || fatal 1 "Kernel source not found"
1271     [ "$nofullmake" = "nofullmake" ] || make mrproper
1272     [ "$nofullmake" = "nofullmake" ] || rm -f rpm-release
1273 #    [ "$nocopykernel" = "copyrpmkernel" ] || rm -f localversion-*
1274     [ "$nocopykernel" = "copykernel" ] && [ -f "$CONFIG_FILE" ] && cp $CONFIG_FILE .config
1275     if [ "$nocopykernel" = "copyrpmkernel" ]; then
1276         [ -f "$KERNELRPMCONFIG" ] && cp $KERNELRPMCONFIG .config
1277         if [ -f "$KERNELRPMRELEASE" ]; then
1278             cp $KERNELRPMRELEASE .
1279         else
1280             sed -e "s/^EXTRAVERSION\s\+=\s\+.*$/EXTRAVERSION = -${lnxrel}/" < Makefile > Makefile.new
1281             [ -f "Makefile.new" ] && mv Makefile.new Makefile
1282         fi
1283     fi
1284     [ -f ".config" ] || ( popd ; echo "Cannot find .config file"; return 255 )
1285     make oldconfig || ( popd ; return 255 )
1286     make include/linux/version.h
1287     if [ ! "$nofullmake" = "nofullmake" ]; then
1288         make || ( popd ; return 255 )
1289     fi
1290     popd
1291     return
1292 }
1293
1294 build_kernel_ib()
1295 {
1296     # build kernel-ib{,-devel}
1297     # some I/B drivers are architecture dependent and kernel-ib's configure
1298     # does not figure it out for us ~sigh~
1299     local configure_options=""
1300     case "$TARGET_ARCH" in
1301         x86_64 | ia64)
1302             configure_options="--with-ipath_inf-mod"
1303             ;;
1304         ppc64)
1305             configure_options="--with-ipath_inf-mod --with-ehca-mod"
1306             ;;
1307     esac
1308     local K_SRC="K_SRC"
1309     # ofed 1.3 had a bug in the rpm spec
1310     if [ "$OFED_VERSION" = "1.3" ]; then
1311         K_SRC="KSRC"
1312     fi
1313     $RPMBUILD --rebuild --define 'build_kernel_ib 1' --define 'build_kernel_ib_devel 1' \
1314               --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
1315               --define "KVERSION ${LINUXRELEASE}" \
1316               --define "$K_SRC ${LINUXOBJ:-${LINUX}}" \
1317               --define "LIB_MOD_DIR /lib/modules/${LINUXRELEASE}/updates" \
1318               --define "configure_options --without-quilt --with-core-mod --with-user_mad-mod --with-user_access-mod --with-addr_trans-mod --with-srp-target-mod --with-core-mod --with-mthca-mod --with-mlx4-mod --with-cxgb3-mod --with-nes-mod --with-ipoib-mod --with-sdp-mod --with-srp-mod --without-srp-target-mod --with-rds-mod --with-iser-mod --with-qlgc_vnic-mod --with-madeye-mod $configure_options" ${TOPDIR}/OFED/SRPMS/ofa_kernel-${OFED_VERSION}-ofed${OFED_VERSION}.src.rpm
1319
1320     if [ ${PIPESTATUS[0]} != 0 ]; then
1321         fatal 1 "Error building kernel-ib"
1322     fi
1323
1324     pushd "$TOPDIR" >/dev/null
1325     rm -rf kernel-ib-devel
1326     mkdir kernel-ib-devel
1327     cd kernel-ib-devel
1328     local rpm=$(ls $TOPDIR/RPMS/*/kernel-ib-devel-${OFED_VERSION}-${LINUXRELEASE//-/_}.*.rpm)
1329     rpm2cpio -itv < $rpm | cpio -id
1330     CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
1331     popd >/dev/null
1332 }
1333
1334 #build patchless lustre
1335 patchless_build_sequence()
1336 {
1337     if [ -f $LINUX/Makefile ]; then
1338         # Get the correct kernel release - I'm unsure how this can ever
1339         # work otherwise, unless you're using the exact same kernel version
1340         # Lustre is shipped with.
1341
1342         local LINUXRELEASE=$(find_linux_release ${LINUXOBJ:-$LINUX})
1343         if [ -z "$LINUXRELEASE" ]; then
1344             echo "Failed to find linux release in ${LINUXOBJ:-$LINUX}"
1345             RC=255
1346         fi
1347
1348         lnxmaj=$(echo $LINUXRELEASE | cut -f1 -d-)
1349         EXTRA_VERSION=$(echo $LINUXRELEASE | cut -f2 -d-)_lustre.$LUSTRE_VERSION
1350         RPMSMPTYPE=" "
1351         build_lustre && buildsuccess=true
1352     else
1353     #try to build from kernel-devel RPM (RHEL)
1354     LINUX=
1355     TARGET_ARCH=
1356     local rpmfound=false
1357     local buildsuccess=false
1358     local storeforreuse=false
1359     if [ "$KERNELRPMSBASE" = "" ] || [ ! -d "$KERNELRPMSBASE" ]; then
1360         return 255
1361     fi
1362     [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
1363
1364     local delimiter=${EXTRA_VERSION_DELIMITER:-"-"}
1365     # default to source type -source and special case below
1366     local type=-source
1367     case "$DISTRO" in
1368         rhel*)
1369             type=-devel
1370             ;;
1371     esac
1372
1373     unpack_linux_rpm $type $delimiter && rpmfound=true
1374
1375     [ -d SRPMS ] || mkdir SRPMS
1376     [ -d RPMS ] || mkdir RPMS
1377     [ -d BUILD ] || mkdir BUILD
1378     [ -d SOURCES ] || mkdir SOURCES
1379
1380     # first build kernel-ib
1381     if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1382         $rpmfound && build_kernel_ib
1383     fi
1384     ( $rpmfound ) && build_lustre && buildsuccess=true && find_linux_source_rpm
1385     fi
1386
1387     if $buildsuccess; then
1388         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELRPM" ] && \
1389             cp "$KERNELRPM" RPMS/${TARGET_ARCH}/
1390         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELSOURCERPM" ] && \
1391             cp "$KERNELSOURCERPM" RPMS/${TARGET_ARCH}/
1392         KERNELCOMPILEDIR="$LINUX"
1393         if $storeforreuse; then
1394             store_for_reuse || echo "Cannot store for future reuse"
1395         fi
1396
1397         return
1398     elif ! $rpmfound; then
1399         echo "COULD NOT FIND VENDOR -devel or -source RPM for $DISTRO/$TARGET_ARCH: $lnxmaj-$lnxrel in $KERNELRPMSBASE"
1400         return 255
1401     else
1402         echo "Patchless build failed."
1403         return 255
1404     fi
1405 }
1406
1407 #check timestamp value. should bi 14-digits string
1408 check_timestamp()
1409 {
1410     local invalue=$1
1411     local timestampnodig=$(echo $invalue | sed -e s/[0-9]*//g)
1412     [ "$timestampnodig" = "" ] || return 255
1413     local timestamplength="${#invalue}"
1414     [ $timestamplength -eq 14 ] || return 255
1415 }
1416
1417 # get list of suitable directories with potential reused staff
1418 get_reuse_dir_list()
1419 {
1420     local rpmonly=$1
1421     local reusedkernelmasknew=$2
1422     local buildtimestamp=
1423     local dirsforreuse=
1424     local sourcelastdate=$(get_last_source_date)
1425     for buildtimestamp in $(ls "$REUSEBUILD/" 2>&1); do
1426         [ -d "$REUSEBUILD/$buildtimestamp" ] || continue
1427         check_timestamp "$buildtimestamp" || continue
1428         local buildtimestampstr=$(echo $buildtimestamp | \
1429             sed -e "s^\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)^\1-\2-\3 \4:\5:\6 GMT^g")
1430         local buildtimestampepoch=$(date --date="$buildtimestampstr"  +%s )
1431         #check for suitable date
1432             if ! $PATCHLESS; then
1433             [ $buildtimestampepoch -ge $sourcelastdate ] || continue
1434             fi
1435         #check for suitable version
1436         if [ "$rpmonly" = "rpmonly" ]; then
1437             local reusedkernelprefix="kernel-lustre-"
1438             ( $PATCHLESS ) && reusedkernelprefix=
1439             local rpmmask="${reusedkernelprefix}${REUSEDKERNELMASK}"
1440             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
1441             [ -f $REUSEBUILD/$buildtimestamp/${rpmmask}.rpm ] && \
1442                 dirsforreuse="$dirsforreuse $REUSEBUILD/$buildtimestamp"
1443         else
1444             local rpmmask="$REUSEDKERNELMASK"
1445             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
1446             pushd $REUSEBUILD/$buildtimestamp/linux-${rpmmask} > /dev/null 2>&1 || continue
1447             local curdir=$(pwd)
1448             dirsforreuse="$dirsforreuse $curdir"
1449             popd
1450         fi
1451     done
1452     echo "$dirsforreuse"
1453 }
1454
1455 #try to reuse old RPM
1456 build_sequence_rpm_reuse()
1457 {
1458     local sourcerpm=$1
1459     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
1460     local dirsforreuse=
1461     if ! [ "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse RPM
1462         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
1463         REUSEDKERNELMASKnew="kernel-lustre-source-${REUSEDKERNELMASKnew}"
1464         local dirsforreuse="$(get_reuse_dir_list rpmonly $REUSEDKERNELMASKnew)"
1465         local buildsuccess=false
1466         LINUXOBJ=
1467         for curdir in $(echo $dirsforreuse); do
1468             [ -d "$curdir" ] || continue
1469             local reusedkernelprefix="kernel-lustre-"
1470             local reusedkernelrpm=
1471             [ -f ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
1472                 reusedkernelrpm=$(ls ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
1473             [ -f "$reusedkernelrpm" ] || continue
1474
1475             local reusedkernelsourcerpm=
1476             [ -f ${curdir}/${REUSEDKERNELMASKnew}.rpm ] && \
1477                 reusedkernelsourcerpm=$(ls ${curdir}/${REUSEDKERNELMASKnew}.rpm | head -1 )
1478             [ -f "$reusedkernelsourcerpm" ] || continue
1479
1480             # don't need to check for kernel-ib RPM reuse here because sles9 is not supported
1481             # by OFED >= 1.3.0 and this function appears to only be used for sles9
1482
1483             [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
1484             reuse_kernel_rpm "$reusedkernelsourcerpm" "" && build_linux nofullmake copyrpmkernel && build_lustre && buildsuccess=true
1485             ( $buildsuccess ) || continue
1486             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
1487                 [ -f "$reusedkernelrpm" ] && \
1488                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1489  
1490                 [ -f "$reusedkernelsourcerpm" ] && \
1491                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
1492                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
1493             fi
1494             return
1495         done
1496     fi
1497     return 255
1498 }
1499
1500 #try to reuse old BUILD dir
1501 build_sequence_reuse()
1502 {
1503     local sourcerpm=$1
1504     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
1505     local dirsforreuse=
1506     if [ ! "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse old kernel build directory
1507         local dirsforreuse="$(get_reuse_dir_list)"
1508         local buildsuccess=false
1509         LINUXOBJ=
1510         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
1511         for curdir in $(echo $dirsforreuse); do
1512             local reusedkernelrpm=
1513             local reusedkernelsourcerpm=
1514             local reusedkernelibrpm=
1515             [ -d "$curdir" ] || continue
1516             [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" -a 
1517               ! -d "${curdir%/*}/ofa_kernel" ] && continue
1518             local reusedkernelprefix="kernel-lustre-"
1519             ( $PATCHLESS ) && reusedkernelprefix=
1520             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
1521                 reusedkernelrpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
1522             reusedkernelprefix="kernel-lustre-source-"
1523             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm ] && \
1524                 reusedkernelsourcerpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm | head -1 ) 
1525             if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1526                 gen_lustre_version
1527                 reusedkernelprefix="kernel-ib-"
1528                 [ -f ${curdir}/../${reusedkernelprefix}${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm ] && \
1529                     reusedkernelibrpm=$(ls ${curdir}/../${reusedkernelprefix}${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm | head -1 ) 
1530                     reusedkernelibdevelrpm=$(ls ${curdir}/../${reusedkernelprefix}devel-${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm | head -1 ) 
1531             fi
1532             if ! ( $NORPM ) && ! [ -f "$reusedkernelrpm" ]; then #kernel rpm not found. Build all
1533                 continue
1534             fi
1535             if ! ( $NORPM ) && ! [ -f "$reusedkernelsourcerpm" ]; then #kernel source rpm not found. Build all
1536                 continue
1537             fi
1538             if [ -n "$OFED_VERSION" ]; then
1539                 if [ "$OFED_VERSION" != "inkernel" ]; then
1540                 if ! ( $NORPM ) && [ ! -f "$reusedkernelibrpm" -o ! -f "$reusedkernelibdevelrpm"]; then #kernel-ib{,-devel} rpm not found. Build all
1541                     continue
1542                 fi
1543                 CONFIGURE_FLAGS="--with-o2ib=${curdir%/*}/ofa_kernel ${CONFIGURE_FLAGS}"
1544                 else
1545                     CONFIGURE_FLAGS="--with-o2ib=yes ${CONFIGURE_FLAGS}"
1546                 fi
1547             fi
1548             LINUX="$curdir"
1549             build_lustre || continue
1550             touch "$curdir/../"
1551             buildsuccess=true
1552             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
1553                 [ -f "$reusedkernelrpm" ] && \
1554                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
1555                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
1556                 [ -f "$reusedkernelsourcerpm" ] && \
1557                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1558                 [ -f "$reusedkernelibrpm" ] && \
1559                     cp -f  "$reusedkernelibrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1560                     cp -f  "$reusedkernelibdevelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1561             fi
1562             return
1563         done
1564     fi
1565     return 255
1566 }
1567
1568
1569 build_sequence()
1570 {
1571     if (( $DO_SRC )) ; then
1572         unpack_linux
1573         patch_linux
1574         pack_linux
1575         clean_linux
1576     fi
1577     prep_kernel_build || return 255
1578     clean_lustre || return 255
1579
1580     build_kernel || return 255
1581 }
1582
1583 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
1584
1585 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,publish,release,src,stage:,tag:,target:,target-archs:,with-linux:,xen -- "$@")
1586
1587 if [ $? != 0 ] ; then
1588     usage 1
1589 fi
1590
1591 eval set -- "$options"
1592     
1593 while [ "$1" ] ; do
1594     case "$1" in
1595         '')
1596             usage 1
1597             ;;
1598         --ccache)
1599             CCACHE='ccache'
1600             shift
1601             ;;
1602         -d)
1603             CVSROOT=$2
1604             shift 2
1605             ;;
1606         -D)
1607             DATE=$2
1608             shift 2
1609             ;;
1610         --external-patches)
1611             EXTERNAL_PATCHES=$2
1612             shift 2
1613             ;;
1614         --extraversion)
1615             EXTRA_VERSION=$2
1616             shift 2
1617             ;;
1618         --help | -h)
1619             usage 0
1620             ;;
1621         --kerneldir)
1622             KERNELDIR=$2
1623             shift 2
1624             ;;
1625         --kerneltree)
1626             KERNELTREE=$2
1627             shift 2
1628             ;;
1629         --linux | --with-linux)
1630             LINUX=$2
1631             shift 2
1632             ;;
1633         --distro)
1634             DISTRO=$2
1635             shift 2
1636             ;;
1637         --reuserpm)
1638             REUSERPM=$2
1639             shift 2
1640             ;;
1641         --reusebuild)
1642             REUSEBUILD=$2
1643             shift 2
1644             ;;
1645         --norpm)
1646             NORPM=true
1647             shift
1648             ;;
1649         --ldiskfs)
1650             LDISKFSRPM=true
1651             shift
1652             ;;
1653         --patchless)
1654             PATCHLESS=true
1655             shift
1656             ;;
1657         --kernelrpm)
1658             KERNELRPMSBASE=$2
1659             shift 2
1660             ;;
1661         --timestamp)
1662             TIMESTAMP=$2
1663             shift 2
1664             ;;
1665         --lustre)
1666             LUSTRE=$2
1667             shift 2
1668             ;;
1669         --nodownload)
1670             DOWNLOAD=0
1671             shift 1
1672             ;;
1673         --nosrc)
1674             DO_SRC=0
1675             shift 1
1676             ;;
1677         --publish)
1678             shift
1679             ;;
1680         --release)
1681             RELEASE=true
1682             shift
1683             ;;
1684         --src)
1685             DO_SRC=1
1686             shift 1
1687             ;;
1688         --stage)
1689             STAGEDIR=$2
1690             shift 2
1691             ;;
1692         --tag)
1693             TAG=$2
1694             shift 2
1695             ;;
1696         --target)
1697             TARGET=$2
1698             shift 2
1699             ;;
1700         --target-archs)
1701             TARGET_ARCHS=$2
1702             shift 2
1703             ;;
1704         --disable-datestamp)
1705             USE_DATESTAMP=
1706             shift
1707             ;;
1708         --xen)
1709             XEN=true
1710             shift
1711             ;;
1712         --)
1713             shift
1714             CONFIGURE_FLAGS=$@
1715             CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-liblustre --enable-liblustre-tests"
1716             break
1717             ;; 
1718         *)
1719             usage 1 "Unrecognized option: $1"
1720             ;;
1721     esac
1722 done
1723
1724 check_options
1725
1726 unpack_lustre
1727
1728 load_target
1729 EXTRA_VERSION_DELIMITER=${EXTRA_VERSION_DELIMITER:-"-"}
1730
1731 if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1732     unpack_ofed
1733 fi
1734
1735 build_success=false
1736 if $PATCHLESS; then
1737     patchless_build_sequence && build_success=true 
1738 elif [ -z "$LINUX" ] ; then
1739     [ "$DISTRO" = "sles9" ] && build_sequence_rpm_reuse && build_success=true
1740     if ! $build_success; then
1741         build_sequence_reuse && build_success=true
1742         if ! $build_success; then
1743             build_sequence && build_success=true
1744             if $build_success; then
1745                 store_for_reuse || echo "Cannot store for future reuse"
1746             fi
1747         fi
1748     fi
1749 else
1750     build_lustre && build_success=true
1751 fi
1752 ( $build_success ) || fatal 1 "Cannot build lustre"
1753
1754 stage