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