Whamcloud - gitweb
b=18656
[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         --define "_tmppath $TMPDIR" \
842         --define "_topdir $TOPDIR" || \
843         fatal 1 "Error building rpms for $BUILD_ARCHS."
844
845     popd >/dev/null
846     ( $(skeep_ldiskfs_rpm $TAG) ) && return
847
848     pushd lustre/ldiskfs || return 255
849     make dist
850     if [ "$?" != "0" ] ; then
851         popd
852         return 255
853     fi
854     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
855
856     gen_lustre_version
857
858     local ldiskfs_spec=lustre-ldiskfs.spec
859     [ -f "$ldiskfs_spec" ] && sed \
860     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
861     < $ldiskfs_spec \
862     > ../lustre-ldiskfs.spec
863
864     $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
865         --define "_tmppath /var/tmp" \
866         --define "_topdir $TOPDIR"
867     if [ "$?" != "0" ] ; then
868         popd
869         return 255
870     fi
871
872     if (( $DO_SRC )) ; then
873             $RPMBUILD -bs ../lustre-ldiskfs.spec \
874             --define "_tmppath /var/tmp" \
875             --define "_topdir $TOPDIR"
876         if [ "$?" != "0" ] ; then
877             popd
878             return 255
879         fi
880     fi
881     popd
882 }
883
884 stage()
885 {
886     [ "$STAGEDIR" ] || return 0
887
888     for arch in $BUILD_ARCHS ; do
889         rpmdir="${STAGEDIR}/${CANONICAL_TARGET}-${arch}"
890         echo "${0##*/}: Copying RPMs into ${rpmdir}"
891         mkdir -p "${rpmdir}"
892         cp -v RPMS/${arch}/*.rpm "${rpmdir}"
893         if [ -d RPMS/noarch ] ; then
894             cp -v RPMS/noarch/*.rpm "${rpmdir}"
895         fi
896     done
897
898     cp -v "$LUSTRE" "$STAGEDIR"
899 }
900
901 #check if we need to build separate ldiskfs RPM
902 skeep_ldiskfs_rpm()
903 {
904         local tag=$1
905         local skip=false
906         if ! $LDISKFSRPM; then
907             skip=true
908         elif $PATCHLESS; then
909             skip=true
910         else
911             for skiptag in $SKIPLDISKFSRPM; do
912                 [[ $tag == $skiptag ]] && skip=true && break
913             done
914         fi
915         echo $skip
916 }
917
918 #get date of last changed target/config/series/patches
919 get_last_source_date()
920 {
921     local filelist="${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} \
922         $CONFIG_FILE"
923     local TOPDIRnew=$(echo ${TOPDIR} | sed -e s/\\//\\\\\\//g)
924     filelist="$filelist $( \
925         cat ${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} | \
926         sed -e s/^/${TOPDIRnew}\\/lustre\\/lustre\\/kernel_patches\\/patches\\// 2>&1)"
927     local sourcelastdate=$( find ${filelist} -name CVS -prune -o \
928                 -type f -printf "%T@\n" 2>&1 | sort | tail -1 )
929     is_integer $sourcelastdate && echo $sourcelastdate
930 }
931
932 #check if variable is integer
933 is_integer()
934 {
935     local invariable=$1
936     [ "$invariable" = "" ] && return 255
937     local invariableint=$( echo $invariable | sed -e s/[^0-9]//g )
938     [ "$invariable" = "$invariableint" ] || return 255
939 }
940
941 #generate LUSTRE_EXTRA_VERSION from EXTRA_VERSION
942 gen_lustre_version()
943 {
944     local smptype=smp
945     [ "$KERNCONFSMPTYPE" = "" ] || smptype=$KERNCONFSMPTYPE
946     [ "$RPMSMPTYPE" = "" ] || smptype=$RPMSMPTYPE
947
948     LUSTRE_EXTRA_VERSION="${lnxmaj}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}${TARGET_DELIMITER}${smptype}"
949     LUSTRE_EXTRA_VERSION=${LUSTRE_EXTRA_VERSION//-/_}
950 }
951
952 #store RPMs and/or BUILD dir for future reuse
953 store_for_reuse()
954 {
955     local rpmonly=$1
956     if [ ! "$REUSEBUILD" = "" ] && [ -d  "/$REUSEBUILD/" ] ; then
957         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || mkdir "${REUSEBUILD}/${TIMESTAMP}"
958         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || return 255
959     else
960         return 255
961     fi
962
963     local lnxrelnew=${lnxrel//-/_}
964     local EXTRA_VERSIONnew=${EXTRA_VERSION//-/_}
965     local KERNELRPMnew=$(basename "$KERNELRPM")
966     if [ ! "$rpmonly" = "rpmonly" ]; then
967             local builddir=
968         if [ ! "$KERNELCOMPILEDIR" = "" ]; then
969             builddir="$KERNELCOMPILEDIR"
970         else
971             builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}"
972             [ "$KERNELCOMPILEDIR" = "" ] || builddir="$KERNELCOMPILEDIR"
973             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}.${lnxrel}"
974             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}-${lnxrel}"
975             if [ ! -d "$builddir" ]; then
976                 pushd "BUILD/lustre-kernel-${lnxmaj}/lustre/" || return 255
977                 local basebuilddir=$(ls -d linux-${lnxmaj}* | head -1)
978                 [ "$basebuilddir" = "" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/${basebuilddir}"
979                 popd
980             fi
981         fi
982         [ -d "$builddir" ] || return 255
983         local dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}"
984         ( $PATCHLESS ) && dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-$KERNELRPMnew" && \
985                 dstdir="${dstdir%.rpm}"
986         [ -d "$dstdir" ] && rm -rf "$dstdir"
987         mv "${builddir}" "$dstdir" || return 255
988         if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
989             # move the OFED kernel-ib-devel tree as well
990             mv "${builddir%/*}/kernel-ib-devel/usr/src/ofa_kernel" "${dstdir%/*}" || return 255
991         fi
992     fi
993     #store kernel rpm
994     local kernelrpmname="kernel-lustre-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
995     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSNnew}.${TARGET_ARCH}.rpm"
996     ( $PATCHLESS ) && [ -f "$KERNELRPM" ] && kernelrpmname="$KERNELRPMnew"
997     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
998         echo "RPM already exist in store directory tree"
999     else
1000         [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
1001     fi
1002     #store kernel source rpm
1003     kernelrpmname="kernel-lustre-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
1004     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
1005     ( $PATCHLESS ) && [ -f "$KERNELSOURCERPM" ] && kernelrpmname=$(basename "$KERNELSOURCERPM")
1006     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
1007         echo "RPM already exist in store directory tree"
1008     else
1009             [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
1010     fi
1011     if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1012         # store kernel-ib RPMs
1013         local rpmname
1014         for rpmname in "kernel-ib" "kernel-ib-devel"; do
1015             rpmname="${rpmname}-${OFED_VERSION}"
1016             if $PATCHLESS; then
1017                 rpmname="${rpmname}-${LINUXRELEASE//-/_}"
1018             else
1019                 rpmname="${rpmname}-${lnxmaj}${EXTRA_VERSION_DELIMITER//-/_}${EXTRA_VERSIONnew}${TARGET_DELIMITER//-/_}${KERNCONFSMPTYPE}"
1020             fi
1021             rpmname="${rpmname}.${TARGET_ARCH}.rpm"
1022             if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${rpmname}" ]; then
1023                 echo "RPM already exist in store directory tree"
1024             else
1025                 [ -f "RPMS/${TARGET_ARCH}/${rpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${rpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
1026             fi
1027         done
1028     fi
1029 }
1030
1031 set_rpm_smp_type()
1032 {
1033     local infact_arch=${TARGET_ARCH}
1034     RPMSMPTYPE=default
1035     [ "$infact_arch" == "i586" ] && infact_arch="i686"
1036     for smp_type in $SMP_ARCHS; do
1037         [ $infact_arch == $smp_type ] && RPMSMPTYPE=smp && break
1038     done
1039     for smp_type in $BIGSMP_ARCHS; do
1040         [ $infact_arch == $smp_type ] && RPMSMPTYPE=bigsmp && break
1041     done
1042 }
1043
1044 # This function takes a linux source pool and digs out the linux release
1045 # from it
1046 find_linux_release() {
1047     local SRCDIR="$1"
1048
1049     local LINUXRELEASEHEADER=$SRCDIR/include/linux/version.h
1050     if [ -s $SRCDIR/include/linux/utsrelease.h ]; then
1051         LINUXRELEASEHEADER=$SRCDIR/include/linux/utsrelease.h
1052     fi
1053
1054     sed -ne 's/#define UTS_RELEASE "\(.*\)"$/\1/p' $LINUXRELEASEHEADER
1055
1056 }
1057
1058 #unpack kernel(/source/devel) RPM
1059 unpack_linux_rpm()
1060 {
1061     local prefix=$1
1062     local delimiter=${2:-"-"}
1063     local pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
1064     local kernelbinaryrpm=
1065     [ -d $pathtorpms ] || return 255
1066     local rpmfile=
1067     local wanted_kernel="${lnxmaj}${delimiter}${lnxrel}"
1068     for arch in $TARGET_ARCHS_ALL; do
1069         for rpm in ${pathtorpms}/${arch}/*.rpm; do
1070             local provides=$(rpm -q --provides -p $rpm)
1071             if (echo $provides | grep -q "kernel${prefix} = $wanted_kernel"); then
1072                 KERNELRPM=$rpm
1073             fi
1074         done
1075         [ -f "$KERNELRPM" ] && TARGET_ARCH="$arch" && BUILD_ARCHS="$arch" && break
1076     done
1077
1078     # bleah
1079     set_rpm_smp_type
1080
1081     [ -f "$KERNELRPM" ] || return 255
1082     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
1083     pushd $TOPDIR/reused || return 255
1084     RC=0
1085     rpm2cpio < "$KERNELRPM" | cpio -idc > /dev/null 2>&1
1086     if [ ${PIPESTATUS[0]} -eq 0 ]; then
1087         # RHEL-style and SLES-style rpms
1088         local paths="kernels/${lnxmaj}${delimiter}${lnxrel}-${TARGET_ARCH} linux-${lnxmaj}${delimiter}${lnxrel}"
1089
1090         for path in $paths; do
1091             local src='usr/src'
1092
1093             if [ -d "$src/$path/" ]; then
1094                 LINUX="$(pwd)/$src/$path"
1095             fi
1096             # SLES has a separate -obj tree
1097             if [ -d "$src/${path}-obj" ]; then
1098                 src="$src/${path}-obj"
1099                 local objects="$TARGET_ARCH/$RPMSMPTYPE"
1100
1101                 # Novell, are you *TRYING* to make life hard for me?
1102                 if [ -d "$src/powerpc" ]; then
1103                     objects="powerpc/$TARGET_ARCH"
1104                 elif [ $TARGET_ARCH == 'i686' ]; then
1105                     objects="i386/$RPMSMPTYPE"
1106                 fi
1107
1108                 LINUXOBJ="$(pwd)/$src/$objects"
1109             fi
1110         done
1111         if [ -z "$LINUX" ]; then
1112             RC=255
1113         else
1114             # dig out the release version
1115             LINUXRELEASE=$(find_linux_release ${LINUXOBJ:-$LINUX})
1116             if [ -z "$LINUXRELEASE" ]; then
1117                 echo "Failed to find linux release in ${LINUXOBJ:-$LINUX}"
1118                 RC=255
1119             fi
1120         fi
1121     else
1122         RC=255
1123     fi
1124     popd
1125     return $RC
1126 }
1127
1128 #look for kernel source RPM
1129 find_linux_source_rpm()
1130 {
1131     local rpmfile=
1132     local findarch=true
1133     local arch=
1134     local pathtorpms=
1135     [ ! "$TARGET_ARCH" = "" ] && arch=$TARGET_ARCH && findarch=false
1136     
1137     if ! $findarch; then
1138         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
1139         [ -d $pathtorpms ] || return 255
1140         case "$DISTRO" in
1141             rhel4)
1142                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
1143             ;;
1144             sles10)
1145                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
1146             ;;
1147             *)
1148                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
1149             ;;
1150         esac
1151         [ -f "${pathtorpms}/${rpmfile}" ] || return 255
1152         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
1153     else
1154         for arch in $TARGET_ARCHS_ALL; do
1155         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
1156         [ -d $pathtorpms ] || continue
1157         case "$DISTRO" in
1158             rhel4)
1159                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
1160             ;;
1161             sles10)
1162                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
1163             ;;
1164             *)
1165                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
1166             ;;
1167         esac
1168         [ -f "${pathtorpms}/${rpmfile}" ] || continue
1169         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
1170         TARGET_ARCH=${arch}
1171         break
1172         done
1173     fi
1174     [ -f "${KERNELSOURCERPM}" ] || return 255
1175 }
1176
1177 #unpack and make symlinks for reusing kernel RPM
1178 reuse_kernel_rpm()
1179 {
1180     local pathtorpm=$1
1181     local pathtokernelibrpm=$2
1182     [ "$pathtorpm" = "" ] && return 255
1183     [ -f "$pathtorpm" ] || return 255
1184     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
1185     pushd $TOPDIR/reused || return 255
1186
1187     rpm2cpio < $pathtorpm | cpio -idc
1188     [ ${PIPESTATUS[0]} -eq 0 ] || return 255
1189
1190     if [ -n "$pathtokernelibrpm" ] && [ -f "$pathtokernelibrpm" ]; then
1191         rpm2cpio < $pathtokernelibrpm | cpio -idc
1192         [ ${PIPESTATUS[0]} -eq 0 -o ${PIPESTATUS[1]} -eq 0 ] || return 255
1193         CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
1194     fi
1195
1196     local smptype=
1197     if pushd usr/src/linux-*-obj/${TARGET_ARCH}; then
1198         local smptypes="$SMPTYPES"
1199         [ "$RPMSMPTYPE" = "" ] || smptypes=$RPMSMPTYPE
1200         ( ! $PATCHLESS ) && [ ! "$KERNCONFSMPTYPE" = "" ] && smptypes="$KERNCONFSMPTYPE"
1201         local cursmptype=
1202         for cursmptype in $smptypes; do
1203             [ "$cursmptype" = "''" ] && continue
1204             [ -d $cursmptype ] && smptype=$cursmptype
1205             [ -d $smptype ] && break
1206         done
1207         popd
1208     fi
1209     if [ "${smptype}" = "" ]; then
1210         popd
1211         return 255 # cannot detect smp type
1212     fi
1213     if pushd usr/src/linux-*-obj/${TARGET_ARCH}/$smptype/include2; then
1214         local base=$(readlink asm)
1215         if [ ! -d "/${base}/" ]; then
1216             rm -f asm
1217             base=$(basename "$base")
1218             if pushd ../../../../linux-*/include; then
1219                 local lsrc=$(pwd)
1220                 popd
1221                 [ -d "$lsrc/${base}" ] && ln -s $lsrc/${base} asm
1222             fi
1223         fi
1224         popd
1225         read a b < <(echo $(pwd)/usr/src/linux-*)
1226         if [[ $a == $(pwd)/* && $b = $(pwd)/* ]]; then
1227             cp -f $a/include/linux/config.h $b/${TARGET_ARCH}/$smptype/include/linux/
1228             cp $b/${TARGET_ARCH}/$smptype/.config $a/
1229             [ -f "$b/${TARGET_ARCH}/$smptype/.config" ] && KERNELRPMCONFIG="$b/${TARGET_ARCH}/$smptype/.config"
1230             cp $b/${TARGET_ARCH}/$smptype/.kernelrelease $a/
1231             [ -f "$b/${TARGET_ARCH}/$smptype/.kernelrelease" ] && KERNELRPMRELEASE="$b/${TARGET_ARCH}/$smptype/.kernelrelease"
1232             LINUX=$a
1233             LINUXOBJ=$b/${TARGET_ARCH}/$smptype
1234 #            local fname=$(basename $kernel_rpm)
1235 #            KERNELRPMSDIR=${kernel_rpm%$fname}
1236         fi
1237     fi
1238     popd
1239     [ "$LINUX" = "" ] && return 255
1240     [ -d "$LINUX" ] || return 255
1241 }
1242
1243 #build linux kernel rpm
1244 build_linux_rpm()
1245 {
1246     pushd $LINUX || return 255
1247     make binrpm-pkg || ( popd ; return 255 )
1248     local addlnxrel=
1249     [ -f ".version" ] && addlnxrel="-$(cat .version)"
1250     popd
1251     local arch=
1252     for arch in $TARGET_ARCHS_ALL; do
1253         [ -f "/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
1254             KERNELRPM="/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
1255             TARGET_ARCH="$arch"
1256         [ -f "/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
1257             KERNELRPM="/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
1258             TARGET_ARCH="$arch"
1259     done
1260     [ "$KERNELRPM" = "" ] || return
1261     return 255
1262 }
1263
1264 #build linux kernel
1265 build_linux()
1266 {
1267     local nofullmake=$1
1268     local nocopykernel=$2
1269     pushd $LINUX || fatal 1 "Kernel source not found"
1270     [ "$nofullmake" = "nofullmake" ] || make mrproper
1271     [ "$nofullmake" = "nofullmake" ] || rm -f rpm-release
1272 #    [ "$nocopykernel" = "copyrpmkernel" ] || rm -f localversion-*
1273     [ "$nocopykernel" = "copykernel" ] && [ -f "$CONFIG_FILE" ] && cp $CONFIG_FILE .config
1274     if [ "$nocopykernel" = "copyrpmkernel" ]; then
1275         [ -f "$KERNELRPMCONFIG" ] && cp $KERNELRPMCONFIG .config
1276         if [ -f "$KERNELRPMRELEASE" ]; then
1277             cp $KERNELRPMRELEASE .
1278         else
1279             sed -e "s/^EXTRAVERSION\s\+=\s\+.*$/EXTRAVERSION = -${lnxrel}/" < Makefile > Makefile.new
1280             [ -f "Makefile.new" ] && mv Makefile.new Makefile
1281         fi
1282     fi
1283     [ -f ".config" ] || ( popd ; echo "Cannot find .config file"; return 255 )
1284     make oldconfig || ( popd ; return 255 )
1285     make include/linux/version.h
1286     if [ ! "$nofullmake" = "nofullmake" ]; then
1287         make || ( popd ; return 255 )
1288     fi
1289     popd
1290     return
1291 }
1292
1293 build_kernel_ib()
1294 {
1295     # build kernel-ib{,-devel}
1296     # some I/B drivers are architecture dependent and kernel-ib's configure
1297     # does not figure it out for us ~sigh~
1298     local configure_options=""
1299     case "$TARGET_ARCH" in
1300         x86_64 | ia64)
1301             configure_options="--with-ipath_inf-mod"
1302             ;;
1303         ppc64)
1304             configure_options="--with-ipath_inf-mod --with-ehca-mod"
1305             ;;
1306     esac
1307     local K_SRC="K_SRC"
1308     # ofed 1.3 had a bug in the rpm spec
1309     if [ "$OFED_VERSION" = "1.3" ]; then
1310         K_SRC="KSRC"
1311     fi
1312     $RPMBUILD --rebuild --define 'build_kernel_ib 1' --define 'build_kernel_ib_devel 1' \
1313               --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
1314               --define "KVERSION ${LINUXRELEASE}" \
1315               --define "$K_SRC ${LINUXOBJ:-${LINUX}}" \
1316               --define "LIB_MOD_DIR /lib/modules/${LINUXRELEASE}/updates" \
1317               --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
1318
1319     if [ ${PIPESTATUS[0]} != 0 ]; then
1320         fatal 1 "Error building kernel-ib"
1321     fi
1322
1323     pushd "$TOPDIR" >/dev/null
1324     rm -rf kernel-ib-devel
1325     mkdir kernel-ib-devel
1326     cd kernel-ib-devel
1327     local rpm=$(ls $TOPDIR/RPMS/*/kernel-ib-devel-${OFED_VERSION}-${LINUXRELEASE//-/_}.*.rpm)
1328     rpm2cpio -itv < $rpm | cpio -id
1329     CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
1330     popd >/dev/null
1331 }
1332
1333 #build patchless lustre
1334 patchless_build_sequence()
1335 {
1336     if [ -f $LINUX/Makefile ]; then
1337         # Get the correct kernel release - I'm unsure how this can ever
1338         # work otherwise, unless you're using the exact same kernel version
1339         # Lustre is shipped with.
1340
1341         local LINUXRELEASE=$(find_linux_release ${LINUXOBJ:-$LINUX})
1342         if [ -z "$LINUXRELEASE" ]; then
1343             echo "Failed to find linux release in ${LINUXOBJ:-$LINUX}"
1344             RC=255
1345         fi
1346
1347         lnxmaj=$(echo $LINUXRELEASE | cut -f1 -d-)
1348         EXTRA_VERSION=$(echo $LINUXRELEASE | cut -f2 -d-)_lustre.$LUSTRE_VERSION
1349         RPMSMPTYPE=" "
1350         build_lustre && buildsuccess=true
1351     else
1352     #try to build from kernel-devel RPM (RHEL)
1353     LINUX=
1354     TARGET_ARCH=
1355     local rpmfound=false
1356     local buildsuccess=false
1357     local storeforreuse=false
1358     if [ "$KERNELRPMSBASE" = "" ] || [ ! -d "$KERNELRPMSBASE" ]; then
1359         return 255
1360     fi
1361     [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
1362
1363     local delimiter=${EXTRA_VERSION_DELIMITER:-"-"}
1364     # default to source type -source and special case below
1365     local type=-source
1366     case "$DISTRO" in
1367         rhel*)
1368             type=-devel
1369             ;;
1370     esac
1371
1372     unpack_linux_rpm $type $delimiter && rpmfound=true
1373
1374     [ -d SRPMS ] || mkdir SRPMS
1375     [ -d RPMS ] || mkdir RPMS
1376     [ -d BUILD ] || mkdir BUILD
1377     [ -d SOURCES ] || mkdir SOURCES
1378
1379     # first build kernel-ib
1380     if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1381         $rpmfound && build_kernel_ib
1382     fi
1383     ( $rpmfound ) && build_lustre && buildsuccess=true && find_linux_source_rpm
1384     fi
1385
1386     if $buildsuccess; then
1387         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELRPM" ] && \
1388             cp "$KERNELRPM" RPMS/${TARGET_ARCH}/
1389         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELSOURCERPM" ] && \
1390             cp "$KERNELSOURCERPM" RPMS/${TARGET_ARCH}/
1391         KERNELCOMPILEDIR="$LINUX"
1392         if $storeforreuse; then
1393             store_for_reuse || echo "Cannot store for future reuse"
1394         fi
1395
1396         return
1397     elif ! $rpmfound; then
1398         echo "COULD NOT FIND VENDOR -devel or -source RPM for $DISTRO/$TARGET_ARCH: $lnxmaj-$lnxrel in $KERNELRPMSBASE"
1399         return 255
1400     else
1401         echo "Patchless build failed."
1402         return 255
1403     fi
1404 }
1405
1406 #check timestamp value. should bi 14-digits string
1407 check_timestamp()
1408 {
1409     local invalue=$1
1410     local timestampnodig=$(echo $invalue | sed -e s/[0-9]*//g)
1411     [ "$timestampnodig" = "" ] || return 255
1412     local timestamplength="${#invalue}"
1413     [ $timestamplength -eq 14 ] || return 255
1414 }
1415
1416 # get list of suitable directories with potential reused staff
1417 get_reuse_dir_list()
1418 {
1419     local rpmonly=$1
1420     local reusedkernelmasknew=$2
1421     local buildtimestamp=
1422     local dirsforreuse=
1423     local sourcelastdate=$(get_last_source_date)
1424     for buildtimestamp in $(ls "$REUSEBUILD/" 2>&1); do
1425         [ -d "$REUSEBUILD/$buildtimestamp" ] || continue
1426         check_timestamp "$buildtimestamp" || continue
1427         local buildtimestampstr=$(echo $buildtimestamp | \
1428             sed -e "s^\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)^\1-\2-\3 \4:\5:\6 GMT^g")
1429         local buildtimestampepoch=$(date --date="$buildtimestampstr"  +%s )
1430         #check for suitable date
1431             if ! $PATCHLESS; then
1432             [ $buildtimestampepoch -ge $sourcelastdate ] || continue
1433             fi
1434         #check for suitable version
1435         if [ "$rpmonly" = "rpmonly" ]; then
1436             local reusedkernelprefix="kernel-lustre-"
1437             ( $PATCHLESS ) && reusedkernelprefix=
1438             local rpmmask="${reusedkernelprefix}${REUSEDKERNELMASK}"
1439             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
1440             [ -f $REUSEBUILD/$buildtimestamp/${rpmmask}.rpm ] && \
1441                 dirsforreuse="$dirsforreuse $REUSEBUILD/$buildtimestamp"
1442         else
1443             local rpmmask="$REUSEDKERNELMASK"
1444             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
1445             pushd $REUSEBUILD/$buildtimestamp/linux-${rpmmask} > /dev/null 2>&1 || continue
1446             local curdir=$(pwd)
1447             dirsforreuse="$dirsforreuse $curdir"
1448             popd
1449         fi
1450     done
1451     echo "$dirsforreuse"
1452 }
1453
1454 #try to reuse old RPM
1455 build_sequence_rpm_reuse()
1456 {
1457     local sourcerpm=$1
1458     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
1459     local dirsforreuse=
1460     if ! [ "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse RPM
1461         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
1462         REUSEDKERNELMASKnew="kernel-lustre-source-${REUSEDKERNELMASKnew}"
1463         local dirsforreuse="$(get_reuse_dir_list rpmonly $REUSEDKERNELMASKnew)"
1464         local buildsuccess=false
1465         LINUXOBJ=
1466         for curdir in $(echo $dirsforreuse); do
1467             [ -d "$curdir" ] || continue
1468             local reusedkernelprefix="kernel-lustre-"
1469             local reusedkernelrpm=
1470             [ -f ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
1471                 reusedkernelrpm=$(ls ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
1472             [ -f "$reusedkernelrpm" ] || continue
1473
1474             local reusedkernelsourcerpm=
1475             [ -f ${curdir}/${REUSEDKERNELMASKnew}.rpm ] && \
1476                 reusedkernelsourcerpm=$(ls ${curdir}/${REUSEDKERNELMASKnew}.rpm | head -1 )
1477             [ -f "$reusedkernelsourcerpm" ] || continue
1478
1479             # don't need to check for kernel-ib RPM reuse here because sles9 is not supported
1480             # by OFED >= 1.3.0 and this function appears to only be used for sles9
1481
1482             [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
1483             reuse_kernel_rpm "$reusedkernelsourcerpm" "" && build_linux nofullmake copyrpmkernel && build_lustre && buildsuccess=true
1484             ( $buildsuccess ) || continue
1485             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
1486                 [ -f "$reusedkernelrpm" ] && \
1487                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1488  
1489                 [ -f "$reusedkernelsourcerpm" ] && \
1490                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
1491                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
1492             fi
1493             return
1494         done
1495     fi
1496     return 255
1497 }
1498
1499 #try to reuse old BUILD dir
1500 build_sequence_reuse()
1501 {
1502     local sourcerpm=$1
1503     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
1504     local dirsforreuse=
1505     if [ ! "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse old kernel build directory
1506         local dirsforreuse="$(get_reuse_dir_list)"
1507         local buildsuccess=false
1508         LINUXOBJ=
1509         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
1510         for curdir in $(echo $dirsforreuse); do
1511             local reusedkernelrpm=
1512             local reusedkernelsourcerpm=
1513             local reusedkernelibrpm=
1514             [ -d "$curdir" ] || continue
1515             [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" -a 
1516               ! -d "${curdir%/*}/ofa_kernel" ] && continue
1517             local reusedkernelprefix="kernel-lustre-"
1518             ( $PATCHLESS ) && reusedkernelprefix=
1519             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
1520                 reusedkernelrpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
1521             reusedkernelprefix="kernel-lustre-source-"
1522             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm ] && \
1523                 reusedkernelsourcerpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm | head -1 ) 
1524             if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1525                 gen_lustre_version
1526                 reusedkernelprefix="kernel-ib-"
1527                 [ -f ${curdir}/../${reusedkernelprefix}${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm ] && \
1528                     reusedkernelibrpm=$(ls ${curdir}/../${reusedkernelprefix}${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm | head -1 ) 
1529                     reusedkernelibdevelrpm=$(ls ${curdir}/../${reusedkernelprefix}devel-${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm | head -1 ) 
1530             fi
1531             if ! ( $NORPM ) && ! [ -f "$reusedkernelrpm" ]; then #kernel rpm not found. Build all
1532                 continue
1533             fi
1534             if ! ( $NORPM ) && ! [ -f "$reusedkernelsourcerpm" ]; then #kernel source rpm not found. Build all
1535                 continue
1536             fi
1537             if [ -n "$OFED_VERSION" ]; then
1538                 if [ "$OFED_VERSION" != "inkernel" ]; then
1539                 if ! ( $NORPM ) && [ ! -f "$reusedkernelibrpm" -o ! -f "$reusedkernelibdevelrpm"]; then #kernel-ib{,-devel} rpm not found. Build all
1540                     continue
1541                 fi
1542                 CONFIGURE_FLAGS="--with-o2ib=${curdir%/*}/ofa_kernel ${CONFIGURE_FLAGS}"
1543                 else
1544                     CONFIGURE_FLAGS="--with-o2ib=yes ${CONFIGURE_FLAGS}"
1545                 fi
1546             fi
1547             LINUX="$curdir"
1548             build_lustre || continue
1549             touch "$curdir/../"
1550             buildsuccess=true
1551             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
1552                 [ -f "$reusedkernelrpm" ] && \
1553                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
1554                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
1555                 [ -f "$reusedkernelsourcerpm" ] && \
1556                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1557                 [ -f "$reusedkernelibrpm" ] && \
1558                     cp -f  "$reusedkernelibrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1559                     cp -f  "$reusedkernelibdevelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1560             fi
1561             return
1562         done
1563     fi
1564     return 255
1565 }
1566
1567
1568 build_sequence()
1569 {
1570     if (( $DO_SRC )) ; then
1571         unpack_linux
1572         patch_linux
1573         pack_linux
1574         clean_linux
1575     fi
1576     prep_kernel_build || return 255
1577     clean_lustre || return 255
1578
1579     build_kernel || return 255
1580 }
1581
1582 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
1583
1584 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 -- "$@")
1585
1586 if [ $? != 0 ] ; then
1587     usage 1
1588 fi
1589
1590 eval set -- "$options"
1591     
1592 while [ "$1" ] ; do
1593     case "$1" in
1594         '')
1595             usage 1
1596             ;;
1597         --ccache)
1598             CCACHE='ccache'
1599             shift
1600             ;;
1601         -d)
1602             CVSROOT=$2
1603             shift 2
1604             ;;
1605         -D)
1606             DATE=$2
1607             shift 2
1608             ;;
1609         --external-patches)
1610             EXTERNAL_PATCHES=$2
1611             shift 2
1612             ;;
1613         --extraversion)
1614             EXTRA_VERSION=$2
1615             shift 2
1616             ;;
1617         --help | -h)
1618             usage 0
1619             ;;
1620         --kerneldir)
1621             KERNELDIR=$2
1622             shift 2
1623             ;;
1624         --kerneltree)
1625             KERNELTREE=$2
1626             shift 2
1627             ;;
1628         --linux | --with-linux)
1629             LINUX=$2
1630             shift 2
1631             ;;
1632         --distro)
1633             DISTRO=$2
1634             shift 2
1635             ;;
1636         --reuserpm)
1637             REUSERPM=$2
1638             shift 2
1639             ;;
1640         --reusebuild)
1641             REUSEBUILD=$2
1642             shift 2
1643             ;;
1644         --norpm)
1645             NORPM=true
1646             shift
1647             ;;
1648         --ldiskfs)
1649             LDISKFSRPM=true
1650             shift
1651             ;;
1652         --patchless)
1653             PATCHLESS=true
1654             shift
1655             ;;
1656         --kernelrpm)
1657             KERNELRPMSBASE=$2
1658             shift 2
1659             ;;
1660         --timestamp)
1661             TIMESTAMP=$2
1662             shift 2
1663             ;;
1664         --lustre)
1665             LUSTRE=$2
1666             shift 2
1667             ;;
1668         --nodownload)
1669             DOWNLOAD=0
1670             shift 1
1671             ;;
1672         --nosrc)
1673             DO_SRC=0
1674             shift 1
1675             ;;
1676         --publish)
1677             shift
1678             ;;
1679         --release)
1680             RELEASE=true
1681             shift
1682             ;;
1683         --src)
1684             DO_SRC=1
1685             shift 1
1686             ;;
1687         --stage)
1688             STAGEDIR=$2
1689             shift 2
1690             ;;
1691         --tag)
1692             TAG=$2
1693             shift 2
1694             ;;
1695         --target)
1696             TARGET=$2
1697             shift 2
1698             ;;
1699         --target-archs)
1700             TARGET_ARCHS=$2
1701             shift 2
1702             ;;
1703         --disable-datestamp)
1704             USE_DATESTAMP=
1705             shift
1706             ;;
1707         --xen)
1708             XEN=true
1709             shift
1710             ;;
1711         --)
1712             shift
1713             CONFIGURE_FLAGS=$@
1714             CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-liblustre --enable-liblustre-tests"
1715             break
1716             ;; 
1717         *)
1718             usage 1 "Unrecognized option: $1"
1719             ;;
1720     esac
1721 done
1722
1723 check_options
1724
1725 unpack_lustre
1726
1727 load_target
1728 EXTRA_VERSION_DELIMITER=${EXTRA_VERSION_DELIMITER:-"-"}
1729
1730 if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1731     unpack_ofed
1732 fi
1733
1734 build_success=false
1735 if $PATCHLESS; then
1736     patchless_build_sequence && build_success=true 
1737 elif [ -z "$LINUX" ] ; then
1738     [ "$DISTRO" = "sles9" ] && build_sequence_rpm_reuse && build_success=true
1739     if ! $build_success; then
1740         build_sequence_reuse && build_success=true
1741         if ! $build_success; then
1742             build_sequence && build_success=true
1743             if $build_success; then
1744                 store_for_reuse || echo "Cannot store for future reuse"
1745             fi
1746         fi
1747     fi
1748 else
1749     build_lustre && build_success=true
1750 fi
1751 ( $build_success ) || fatal 1 "Cannot build lustre"
1752
1753 stage