Whamcloud - gitweb
b=15316
[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" ] && \
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     [ -f "$CONFIG_FILE" ] || \
481         fatal 1 "Config file for target $TARGET missing from $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/."
482     if [ "$EXTRA_VERSION_save" ] ; then
483         EXTRA_VERSION="$EXTRA_VERSION_save"
484     elif ! $RELEASE; then
485         # if there is no patch series, then this is not a lustre specific
486         # kernel.  don't make it look like one
487         if [ -n "$SERIES" ]; then
488             #remove the @VERSION@ (lustre version)
489 #            EXTRA_VERSION=$(echo $EXTRA_VERSION | sed -e "s/\(.*_lustre\)\..*/\1/")
490 #            EXTRA_VERSION="${EXTRA_VERSION}-${TAG}.${TIMESTAMP}"
491             ! ( $PATCHLESS ) && EXTRA_VERSION="${EXTRA_VERSION}.${TIMESTAMP}"
492         fi
493     fi
494     # EXTRA_VERSION=${EXTRA_VERSION//-/_}
495
496     [ -z $REUSEDKERNELMASK ] && \
497         # create mask for kernel RPM/builddir which could be reused
498         if $NORPM; then
499             REUSEDKERNELMASK=${KERNCONFSMPTYPE}-${lnxmaj}-${lnxrelnew}_lustre.${LUSTRE_VERSION}.*${TARGET_ARCH}
500         elif $RELEASE; then
501             REUSEDKERNELMASK=${KERNCONFSMPTYPE}-${lnxmaj}-${lnxrelnew}_lustre.${LUSTRE_VERSION}.${TARGET_ARCH}
502         else
503             REUSEDKERNELMASK=${KERNCONFSMPTYPE}-${lnxmaj}-${lnxrelnew}_lustre.${LUSTRE_VERSION}.${TIMESTAMP}.${TARGET_ARCH}
504         fi
505         # kernel-lustre-smp-2.6.9-55.0.2.EL_lustre.1.6.1.i686.rpm 
506
507     ALL_ARCHS="$BASE_ARCHS $BIGMEM_ARCHS $BOOT_ARCHS $JENSEN_ARCHS $SMP_ARCHS $BIGSMP_ARCHS $PSERIES64_ARCHS $UP_ARCHS"
508
509     BUILD_ARCHS=
510     for arch in $(uniqify "$ALL_ARCHS") ; do
511         if [ -z "$TARGET_ARCHS" ] || echo "$TARGET_ARCHS" | grep "$arch" >/dev/null 2>/dev/null ; then
512             BUILD_ARCHS="$BUILD_ARCHS $arch"
513         fi
514     done
515     [ "$BUILD_ARCHS" ] || usage 1 "No available target archs to build."
516     echo "Building for: $BUILD_ARCHS"
517 }
518
519 tarflags()
520 {
521     case "$1" in
522         '')
523             fatal 1 "tarflags(): File name argument missing."
524             ;;
525         *.tar.gz | *.tgz)
526             echo 'zxf'
527             ;;
528         *.tar.bz2)
529             echo 'jxf'
530             ;;
531         *.tar)
532             echo 'xf'
533             ;;
534         *)
535             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
536             ;;
537     esac
538 }
539
540 untar()
541 {
542     echo "Untarring ${1##*/}..."
543     tar $(tarflags "$1") "$1"
544 }
545
546 unpack_ofed()
547 {
548     untar "$KERNELTREE/../OFED-${OFED_VERSION}.tgz"
549     [ -d OFED ] || ln -sf OFED-[0-9].[0-9]* OFED
550     pwd
551     ls -ld OFED OFED-[0-9].[0-9]*
552     ls -l OFED OFED-[0-9].[0-9]*
553 }
554
555 unpack_lustre()
556 {
557     DIRNAME="lustre-$TAG-$TIMESTAMP"
558     if [ "$LUSTRE" ] ; then
559         untar "$LUSTRE"
560         [ -d lustre ] || ln -sf lustre-[0-9].[0-9]* lustre
561     else
562         if [ "$USE_DATESTAMP" ]; then
563             DATESTAMP="-D '$DATE'"
564         else
565             DATESTAMP=""
566         fi            
567
568         cvs -d "$CVSROOT" -qz3 co $DATESTAMP -d "$DIRNAME" lustre || \
569             fatal 1 "There was an error checking out toplevel Lustre from CVS."
570         pushd "$DIRNAME" > /dev/null
571         ./lustrecvs "$TAG" || \
572             fatal 1 "There was an error checking out Lustre/Portals/Build from CVS."
573         echo "Creating lustre tarball..."
574         sh autogen.sh || fatal 1 "There was an error running autogen.sh."
575         ./configure --disable-{modules,utils,liblustre,tests,doc} || \
576             fatal 1 "There was an error running ./configure to create makefiles."
577         make dist || fatal 1 "There was an error running 'make dist'."
578         popd > /dev/null
579         fname=$(basename $DIRNAME/lustre-*.tar.gz)
580         cp $DIRNAME/$fname . || fatal 1 "There was an error copying lustre tarball."
581         LUSTRE="$PWD/$fname"
582         ln -sf "$DIRNAME" lustre
583     fi
584 }
585
586 unpack_linux()
587 {
588     untar "$KERNEL_FILE"
589     [ -d linux ] || ln -sf linux* linux
590 }
591
592 patch_linux()
593 {
594     [ "$SERIES" ] || return 0
595     FULL_PATCH="$PWD/lustre-kernel-${TARGET}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}.patch"
596     [ -f "$FULL_PATCH" ] && rm -f "$FULL_PATCH"
597     pushd linux >/dev/null
598     for series in $SERIES ; do
599         echo -n "Applying series $series:"
600         for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches" ; do
601             [ -r "$patchesdir/series/$series" ] || continue
602             SERIES_FILE="$patchesdir/series/$series"
603             for patch in $(<"$SERIES_FILE") ; do
604                 echo -n " $patch"
605                 PATCH_FILE="$patchesdir/patches/$patch"
606                 [ -r "$PATCH_FILE" ] || \
607                     fatal 1 "Patch $patch does not exist in Lustre tree."
608                 cat "$PATCH_FILE" >> "$FULL_PATCH" || \
609                     fatal 1 "Error adding patch $patch to full patch."
610                 patch -s -p1 < "$PATCH_FILE" || fatal 1 "Error applying patch $patch."
611             done
612             break
613         done
614         echo
615     done
616     popd >/dev/null
617     echo "Full patch has been saved in ${FULL_PATCH##*/}."
618     echo "Replacing .config files..."
619     [ -d linux/configs ] || mkdir linux/configs || \
620         fatal 1 "Error creating configs directory."
621     rm -f linux/configs/*
622     copysuccess=0
623     for patchesdir in "$EXTERNAL_PATCHES" "lustre/lustre/kernel_patches" ; do
624         [ "$patchesdir" ] && \
625             cp -v $patchesdir/kernel_configs/kernel-${VERSION}-${TARGET}*.config linux/configs/ >/dev/null && copysuccess=1
626     done
627     [ "$copysuccess" = "1" ] || \
628         fatal 1 "Error copying in kernel configs."
629 }
630
631 pack_linux()
632 {
633     TARBALL="$(readlink linux)-$EXTRA_VERSION.tar.gz"
634     echo "Creating patched linux tarball $TARBALL..."
635     tar zcf "$TARBALL" "$(readlink linux)" \
636         --exclude "CVS" --exclude ".cvsignore" || \
637         --exclude "*.orig" --exclude "*~" --exclude "*.rej" || \
638         fatal 1 "Error creating patched Linux tarball."
639 }
640
641 clean_linux()
642 {
643     [ -d linux ] || return 0
644     echo "Cleaning linux..."
645     [ -L linux ] && rm -rf $(readlink linux)
646     rm -rf linux
647 }
648
649 prep_kernel_build()
650 {
651     # make .spec file
652     ENABLE_INIT_SCRIPTS=""
653     sed \
654         -e "s^@BASE_ARCHS@^$BASE_ARCHS^g" \
655         -e "s^@BIGMEM_ARCHS@^$BIGMEM_ARCHS^g" \
656         -e "s^@BIGSMP_ARCHS@^$BIGSMP_ARCHS^g" \
657         -e "s^@BOOT_ARCHS@^$BOOT_ARCHS^g" \
658         -e "s^@CONFIGURE_FLAGS@^$CONFIGURE_FLAGS^g" \
659         -e "s^@ENABLE_INIT_SCRIPTS@^$ENABLE_INIT_SCRIPTS^g" \
660         -e "s^@JENSEN_ARCHS@^$BOOT_ARCHS^g" \
661         -e "s^@KERNEL_EXTRA_VERSION@^$EXTRA_VERSION^g" \
662         -e "s^@KERNEL_EXTRA_VERSION_DELIMITER@^$EXTRA_VERSION_DELIMITER^g" \
663         -e "s^@KERNEL_TARGET_DELIMITER@^$TARGET_DELIMITER^g" \
664         -e "s^@KERNEL_RELEASE@^${EXTRA_VERSION//-/_}^g" \
665         -e "s^@KERNEL_SOURCE@^$KERNEL^g" \
666         -e "s^@KERNEL_VERSION@^$VERSION^g" \
667         -e "s^@LINUX26@^$LINUX26^g" \
668         -e "s^@LUSTRE_SOURCE@^${LUSTRE##*/}^g" \
669         -e "s^@LUSTRE_TARGET@^$TARGET^g" \
670         -e "s^@PSERIES64_ARCHS@^$PSERIES64_ARCHS^g" \
671         -e "s^@RHBUILD@^$RHBUILD^g" \
672         -e "s^@SMP_ARCHS@^$SMP_ARCHS^g" \
673         -e "s^@SUSEBUILD@^$SUSEBUILD^g" \
674         -e "s^@UP_ARCHS@^$UP_ARCHS^g" \
675         < $TOPDIR/lustre/build/lustre-kernel-2.4.spec.in \
676         > lustre-kernel-2.4.spec
677     [ -d SRPMS ] || mkdir SRPMS
678     [ -d RPMS ] || mkdir RPMS
679     [ -d BUILD ] || mkdir BUILD
680     [ -d SOURCES ] || mkdir SOURCES
681     for script in linux-{rhconfig.h,merge-config.awk,merge-modules.awk} \
682         suse-{functions.sh,post.sh,postun.sh,trigger-script.sh.in} \
683         sles8-{pre,post,postun,update_{INITRD_MODULES,rcfile_setting}}.sh ; do
684         cp $TOPDIR/lustre/build/$script SOURCES
685     done
686     cp "$LUSTRE" "$KERNEL_FILE" SOURCES
687     if [ "$EXTERNAL_PATCHES" -a -d "$EXTERNAL_PATCHES" ] ; then
688         tar zcf SOURCES/external-patches.tar.gz -C "$EXTERNAL_PATCHES" series targets patches kernel_configs
689     else
690         touch SOURCES/external-patches.tar.gz
691     fi
692 }
693
694 clean_lustre()
695 {
696     [ -d lustre ] || return 0
697     echo "Cleaning Lustre..."
698     [ -L lustre ] && rm -rf $(readlink lustre)
699     rm -rf lustre
700 }
701
702 build_kernel()
703 {
704     echo "Building kernel + Lustre RPMs for: $BUILD_ARCHS..."
705     targets=
706     for arch in $BUILD_ARCHS ; do
707         targets="--target $arch $targets"
708     done
709
710     local rpmbuildopt='-bb'
711 #    if $NORPM; then
712 #        rpmbuildopt='-bc'
713 #        echo NORPM mode. Only compiling.
714 #        echo "XXX: need to fix lmake - add options to do rpmbuild -bc instead of -bb"
715 #    fi
716
717     $RPMBUILD $targets $rpmbuildopt lustre-kernel-2.4.spec \
718         --define "_tmppath $TMPDIR" \
719         --define "_topdir $TOPDIR" || \
720         fatal 1 "Error building rpms for $BUILD_ARCHS."
721
722     if (( $DO_SRC )) ; then
723         $RPMBUILD -bs lustre-kernel-2.4.spec \
724             --define "_tmppath $TMPDIR" \
725             --define "_topdir $TOPDIR" || \
726             fatal 1 "Error building .src.rpm."
727     fi
728
729     ( $(skeep_ldiskfs_rpm $TAG) ) && return
730
731     pushd $TOPDIR/BUILD/lustre*/ldiskfs || return 255
732     make dist
733     if [ "$?" != "0" ] ; then
734         popd
735         return 255
736     fi
737     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
738
739     gen_lustre_version
740
741     local ldiskfs_spec=lustre-ldiskfs.spec
742     [ -f "$ldiskfs_spec" ] && sed \
743     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
744     < $ldiskfs_spec \
745     > ../lustre-ldiskfs.spec
746
747     $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
748         --define "_tmppath /var/tmp" \
749         --define "_topdir $TOPDIR"
750     if [ "$?" != "0" ] ; then
751         popd
752         return 255
753     fi
754
755     if (( $DO_SRC )) ; then
756             $RPMBUILD -bs ../lustre-ldiskfs.spec \
757             --define "_tmppath /var/tmp" \
758             --define "_topdir $TOPDIR"
759         if [ "$?" != "0" ] ; then
760             popd
761             return 255
762         fi
763     fi
764     popd
765 }
766
767 build_lustre()
768 {
769     [ -d SRPMS ] || mkdir SRPMS
770     [ -d RPMS ] || mkdir RPMS
771     [ -d BUILD ] || mkdir BUILD
772     [ -d SOURCES ] || mkdir SOURCES
773
774     cp "$LUSTRE" SOURCES
775
776     pushd lustre >/dev/null
777
778     echo "Building Lustre RPMs for: $BUILD_ARCHS..."
779     targets=
780     for arch in $BUILD_ARCHS ; do
781         targets="--target $arch $targets"
782     done
783
784     local confoptions="--with-linux=${LINUX}"
785     if $PATCHLESS; then
786         confoptions="--with-linux=${LINUX} --disable-server"
787     fi
788     if [ ! "$LINUXOBJ" = "" ]; then
789         confoptions="$confoptions --with-linux-obj=${LINUXOBJ}" 
790     fi
791     
792     ./configure $confoptions ${CONFIGURE_FLAGS}
793     if [ "$?" != "0" ] ; then
794         local saved_config="../config.log.$(date +%s)"
795         cp config.log $saved_config
796         chmod a+r $saved_config
797         echo "Saved config.log is at $saved_config"
798         popd
799         return 255
800     fi
801
802     gen_lustre_version
803
804     # hack. Somebody move build/lustre.spec to lustre.spec for b1_6
805     local lustre_spec=
806     [ -f lustre.spec ] && lustre_spec=lustre.spec
807     [ -f build/lustre.spec ] && lustre_spec=build/lustre.spec
808
809     [ -f "$lustre_spec" ] && sed \
810         -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
811         < $lustre_spec \
812         > ../lustre.spec
813
814     local rpmbuildopt='-bb'
815     if $NORPM; then
816         rpmbuildopt='-bc'
817         echo NORPM mode. Only compiling.
818     fi
819
820     $RPMBUILD $targets $rpmbuildopt ../lustre.spec \
821         --define "_tmppath $TMPDIR" \
822         --define "_topdir $TOPDIR" || \
823         fatal 1 "Error building rpms for $BUILD_ARCHS."
824
825     popd >/dev/null
826     ( $(skeep_ldiskfs_rpm $TAG) ) && return
827
828     pushd lustre/ldiskfs || return 255
829     make dist
830     if [ "$?" != "0" ] ; then
831         popd
832         return 255
833     fi
834     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
835
836     gen_lustre_version
837
838     local ldiskfs_spec=lustre-ldiskfs.spec
839     [ -f "$ldiskfs_spec" ] && sed \
840     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
841     < $ldiskfs_spec \
842     > ../lustre-ldiskfs.spec
843
844     $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
845         --define "_tmppath /var/tmp" \
846         --define "_topdir $TOPDIR"
847     if [ "$?" != "0" ] ; then
848         popd
849         return 255
850     fi
851
852     if (( $DO_SRC )) ; then
853             $RPMBUILD -bs ../lustre-ldiskfs.spec \
854             --define "_tmppath /var/tmp" \
855             --define "_topdir $TOPDIR"
856         if [ "$?" != "0" ] ; then
857             popd
858             return 255
859         fi
860     fi
861     popd
862 }
863
864 stage()
865 {
866     [ "$STAGEDIR" ] || return 0
867
868     for arch in $BUILD_ARCHS ; do
869         rpmdir="${STAGEDIR}/${CANONICAL_TARGET}-${arch}"
870         echo "${0##*/}: Copying RPMs into ${rpmdir}"
871         mkdir -p "${rpmdir}"
872         cp -v RPMS/${arch}/*.rpm "${rpmdir}"
873         if [ -d RPMS/noarch ] ; then
874             cp -v RPMS/noarch/*.rpm "${rpmdir}"
875         fi
876     done
877
878     cp -v "$LUSTRE" "$STAGEDIR"
879 }
880
881 #check if we need to build separate ldiskfs RPM
882 skeep_ldiskfs_rpm()
883 {
884         local tag=$1
885         local skip=false
886         if ! $LDISKFSRPM; then
887             skip=true
888         elif $PATCHLESS; then
889             skip=true
890         else
891             for skiptag in $SKIPLDISKFSRPM; do
892                 [[ $tag == $skiptag ]] && skip=true && break
893             done
894         fi
895         echo $skip
896 }
897
898 #get date of last changed target/config/series/patches
899 get_last_source_date()
900 {
901     local filelist="${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} \
902         $CONFIG_FILE"
903     local TOPDIRnew=$(echo ${TOPDIR} | sed -e s/\\//\\\\\\//g)
904     filelist="$filelist $( \
905         cat ${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} | \
906         sed -e s/^/${TOPDIRnew}\\/lustre\\/lustre\\/kernel_patches\\/patches\\// 2>&1)"
907     local sourcelastdate=$( find ${filelist} -name CVS -prune -o \
908                 -type f -printf "%T@\n" 2>&1 | sort | tail -1 )
909     is_integer $sourcelastdate && echo $sourcelastdate
910 }
911
912 #check if variable is integer
913 is_integer()
914 {
915     local invariable=$1
916     [ "$invariable" = "" ] && return 255
917     local invariableint=$( echo $invariable | sed -e s/[^0-9]//g )
918     [ "$invariable" = "$invariableint" ] || return 255
919 }
920
921 #generate LUSTRE_EXTRA_VERSION from EXTRA_VERSION
922 gen_lustre_version()
923 {
924     local smptype=smp
925     [ "$KERNCONFSMPTYPE" = "" ] || smptype=$KERNCONFSMPTYPE
926     [ "$RPMSMPTYPE" = "" ] || smptype=$RPMSMPTYPE
927
928     LUSTRE_EXTRA_VERSION="${lnxmaj}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}${TARGET_DELIMITER}${smptype}"
929     LUSTRE_EXTRA_VERSION=${LUSTRE_EXTRA_VERSION//-/_}
930 }
931
932 #store RPMs and/or BUILD dir for future reuse
933 store_for_reuse()
934 {
935     local rpmonly=$1
936     if [ ! "$REUSEBUILD" = "" ] && [ -d  "/$REUSEBUILD/" ] ; then
937         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || mkdir "${REUSEBUILD}/${TIMESTAMP}"
938         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || return 255
939     else
940         return 255
941     fi
942
943     local lnxrelnew=${lnxrel//-/_}
944     local EXTRA_VERSIONnew=${EXTRA_VERSION//-/_}
945     local KERNELRPMnew=$(basename "$KERNELRPM")
946     if [ ! "$rpmonly" = "rpmonly" ]; then
947             local builddir=
948         if [ ! "$KERNELCOMPILEDIR" = "" ]; then
949             builddir="$KERNELCOMPILEDIR"
950         else
951             builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}"
952             [ "$KERNELCOMPILEDIR" = "" ] || builddir="$KERNELCOMPILEDIR"
953             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}.${lnxrel}"
954             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}-${lnxrel}"
955             if [ ! -d "$builddir" ]; then
956                 pushd "BUILD/lustre-kernel-${lnxmaj}/lustre/" || return 255
957                 local basebuilddir=$(ls -d linux-${lnxmaj}* | head -1)
958                 [ "$basebuilddir" = "" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/${basebuilddir}"
959                 popd
960             fi
961         fi
962         [ -d "$builddir" ] || return 255
963         local dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}"
964         ( $PATCHLESS ) && dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-$KERNELRPMnew" && \
965                 dstdir="${dstdir%.rpm}"
966         [ -d "$dstdir" ] && rm -rf "$dstdir"
967         mv "${builddir}" "$dstdir" || return 255
968         if [ -n "$OFED_VERSION" ]; then
969             # move the OFED kernel-ib-devel tree as well
970             mv "${builddir%/*}/kernel-ib-devel/usr/src/ofa_kernel" "${dstdir%/*}" || return 255
971         fi
972     fi
973     #store kernel rpm
974     local kernelrpmname="kernel-lustre-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
975     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSNnew}.${TARGET_ARCH}.rpm"
976     ( $PATCHLESS ) && [ -f "$KERNELRPM" ] && kernelrpmname="$KERNELRPMnew"
977     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
978         echo "RPM already exist in store directory tree"
979     else
980         [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
981     fi
982     #store kernel source rpm
983     kernelrpmname="kernel-lustre-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
984     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
985     ( $PATCHLESS ) && [ -f "$KERNELSOURCERPM" ] && kernelrpmname=$(basename "$KERNELSOURCERPM")
986     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
987         echo "RPM already exist in store directory tree"
988     else
989             [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
990     fi
991     if [ -n "$OFED_VERSION" ]; then
992         # store kernel-ib RPMs
993         local rpmname
994         for rpmname in "kernel-ib" "kernel-ib-devel"; do
995             rpmname="${rpmname}-${OFED_VERSION}"
996             if $PATCHLESS; then
997                 rpmname="${rpmname}-${LINUXRELEASE//-/_}"
998             else
999                 rpmname="${rpmname}-${lnxmaj}${EXTRA_VERSION_DELIMITER//-/_}${EXTRA_VERSIONnew}${TARGET_DELIMITER//-/_}${KERNCONFSMPTYPE}"
1000             fi
1001             rpmname="${rpmname}.${TARGET_ARCH}.rpm"
1002             if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${rpmname}" ]; then
1003                 echo "RPM already exist in store directory tree"
1004             else
1005                 [ -f "RPMS/${TARGET_ARCH}/${rpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${rpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
1006             fi
1007         done
1008     fi
1009 }
1010
1011 set_rpm_smp_type()
1012 {
1013     local infact_arch=${TARGET_ARCH}
1014     RPMSMPTYPE=default
1015     [ "$infact_arch" == "i586" ] && infact_arch="i686"
1016     for smp_type in $SMP_ARCHS; do
1017         [ $infact_arch == $smp_type ] && RPMSMPTYPE=smp && break
1018     done
1019     for smp_type in $BIGSMP_ARCHS; do
1020         [ $infact_arch == $smp_type ] && RPMSMPTYPE=bigsmp && break
1021     done
1022 }
1023
1024 #unpack kernel(/source/devel) RPM
1025 unpack_linux_rpm()
1026 {
1027     local prefix=$1
1028     local delimiter=${2:-"-"}
1029     local pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
1030     local kernelbinaryrpm=
1031     [ -d $pathtorpms ] || return 255
1032     local rpmfile=
1033     local wanted_kernel="${lnxmaj}${delimiter}${lnxrel}"
1034     for arch in $TARGET_ARCHS_ALL; do
1035         for rpm in ${pathtorpms}/${arch}/*.rpm; do
1036             local provides=$(rpm -q --provides -p $rpm)
1037             if (echo $provides | grep -q "kernel${prefix} = $wanted_kernel"); then
1038                 KERNELRPM=$rpm
1039             fi
1040         done
1041         [ -f "$KERNELRPM" ] && TARGET_ARCH="$arch" && BUILD_ARCHS="$arch" && break
1042     done
1043
1044     # bleah
1045     set_rpm_smp_type
1046
1047     [ -f "$KERNELRPM" ] || return 255
1048     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
1049     pushd $TOPDIR/reused || return 255
1050     RC=0
1051     rpm2cpio < "$KERNELRPM" | cpio -idc > /dev/null 2>&1
1052     if [ ${PIPESTATUS[0]} -eq 0 ]; then
1053         # RHEL-style and SLES-style rpms
1054         local paths="kernels/${lnxmaj}${delimiter}${lnxrel}-${TARGET_ARCH} linux-${lnxmaj}${delimiter}${lnxrel}"
1055
1056         for path in $paths; do
1057             local src='usr/src'
1058
1059             if [ -d "$src/$path/" ]; then
1060                 LINUX="$(pwd)/$src/$path"
1061             fi
1062             # SLES has a separate -obj tree
1063             if [ -d "$src/${path}-obj" ]; then
1064                 src="$src/${path}-obj"
1065                 local objects="$TARGET_ARCH/$RPMSMPTYPE"
1066
1067                 # Novell, are you *TRYING* to make life hard for me?
1068                 if [ -d "$src/powerpc" ]; then
1069                     objects="powerpc/$TARGET_ARCH"
1070                 elif [ $TARGET_ARCH == 'i686' ]; then
1071                     objects="i386/$RPMSMPTYPE"
1072                 fi
1073
1074                 LINUXOBJ="$(pwd)/$src/$objects"
1075             fi
1076         done
1077         if [ -z "$LINUX" ]; then
1078             RC=255
1079         else
1080             # dig out the release version
1081             local LINUXRELEASEHEADER=version.h
1082             if test -s ${LINUXOBJ:-$LINUX}/include/linux/utsrelease.h ; then
1083                 LINUXRELEASEHEADER=utsrelease.h
1084             fi
1085             LINUXRELEASE=$(sed -ne 's/#define UTS_RELEASE "\(.*\)"$/\1/p' ${LINUXOBJ:-$LINUX}/include/linux/$LINUXRELEASEHEADER)
1086             if [ -z "$LINUXRELEASE" ]; then
1087                 echo "Failed to find linux release in ${LINUXOBJ:-$LINUX}/include/linux/$LINUXRELEASEHEADER"
1088                 RC=255
1089             fi
1090         fi
1091     else
1092         RC=255
1093     fi
1094     popd
1095     return $RC
1096 }
1097
1098 #look for kernel source RPM
1099 find_linux_source_rpm()
1100 {
1101     local rpmfile=
1102     local findarch=true
1103     local arch=
1104     local pathtorpms=
1105     [ ! "$TARGET_ARCH" = "" ] && arch=$TARGET_ARCH && findarch=false
1106     
1107     if ! $findarch; then
1108         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
1109         [ -d $pathtorpms ] || return 255
1110         case "$DISTRO" in
1111             rhel4)
1112                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
1113             ;;
1114             sles10)
1115                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
1116             ;;
1117             *)
1118                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
1119             ;;
1120         esac
1121         [ -f "${pathtorpms}/${rpmfile}" ] || return 255
1122         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
1123     else
1124         for arch in $TARGET_ARCHS_ALL; do
1125         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
1126         [ -d $pathtorpms ] || continue
1127         case "$DISTRO" in
1128             rhel4)
1129                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
1130             ;;
1131             sles10)
1132                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
1133             ;;
1134             *)
1135                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
1136             ;;
1137         esac
1138         [ -f "${pathtorpms}/${rpmfile}" ] || continue
1139         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
1140         TARGET_ARCH=${arch}
1141         break
1142         done
1143     fi
1144     [ -f "${KERNELSOURCERPM}" ] || return 255
1145 }
1146
1147 #unpack and make symlinks for reusing kernel RPM
1148 reuse_kernel_rpm()
1149 {
1150     local pathtorpm=$1
1151     local pathtokernelibrpm=$2
1152     [ "$pathtorpm" = "" ] && return 255
1153     [ -f "$pathtorpm" ] || return 255
1154     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
1155     pushd $TOPDIR/reused || return 255
1156
1157     rpm2cpio < $pathtorpm | cpio -idc
1158     [ ${PIPESTATUS[0]} -eq 0 ] || return 255
1159
1160     if [ -n "$pathtokernelibrpm" ] && [ -f "$pathtokernelibrpm" ]; then
1161         rpm2cpio < $pathtokernelibrpm | cpio -idc
1162         [ ${PIPESTATUS[0]} -eq 0 -o ${PIPESTATUS[1]} -eq 0 ] || return 255
1163         CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
1164     fi
1165
1166     local smptype=
1167     if pushd usr/src/linux-*-obj/${TARGET_ARCH}; then
1168         local smptypes="$SMPTYPES"
1169         [ "$RPMSMPTYPE" = "" ] || smptypes=$RPMSMPTYPE
1170         ( ! $PATCHLESS ) && [ ! "$KERNCONFSMPTYPE" = "" ] && smptypes="$KERNCONFSMPTYPE"
1171         local cursmptype=
1172         for cursmptype in $smptypes; do
1173             [ "$cursmptype" = "''" ] && continue
1174             [ -d $cursmptype ] && smptype=$cursmptype
1175             [ -d $smptype ] && break
1176         done
1177         popd
1178     fi
1179     if [ "${smptype}" = "" ]; then
1180         popd
1181         return 255 # cannot detect smp type
1182     fi
1183     if pushd usr/src/linux-*-obj/${TARGET_ARCH}/$smptype/include2; then
1184         local base=$(readlink asm)
1185         if [ ! -d "/${base}/" ]; then
1186             rm -f asm
1187             base=$(basename "$base")
1188             if pushd ../../../../linux-*/include; then
1189                 local lsrc=$(pwd)
1190                 popd
1191                 [ -d "$lsrc/${base}" ] && ln -s $lsrc/${base} asm
1192             fi
1193         fi
1194         popd
1195         read a b < <(echo $(pwd)/usr/src/linux-*)
1196         if [[ $a == $(pwd)/* && $b = $(pwd)/* ]]; then
1197             cp -f $a/include/linux/config.h $b/${TARGET_ARCH}/$smptype/include/linux/
1198             cp $b/${TARGET_ARCH}/$smptype/.config $a/
1199             [ -f "$b/${TARGET_ARCH}/$smptype/.config" ] && KERNELRPMCONFIG="$b/${TARGET_ARCH}/$smptype/.config"
1200             cp $b/${TARGET_ARCH}/$smptype/.kernelrelease $a/
1201             [ -f "$b/${TARGET_ARCH}/$smptype/.kernelrelease" ] && KERNELRPMRELEASE="$b/${TARGET_ARCH}/$smptype/.kernelrelease"
1202             LINUX=$a
1203             LINUXOBJ=$b/${TARGET_ARCH}/$smptype
1204 #            local fname=$(basename $kernel_rpm)
1205 #            KERNELRPMSDIR=${kernel_rpm%$fname}
1206         fi
1207     fi
1208     popd
1209     [ "$LINUX" = "" ] && return 255
1210     [ -d "$LINUX" ] || return 255
1211 }
1212
1213 #build linux kernel rpm
1214 build_linux_rpm()
1215 {
1216     pushd $LINUX || return 255
1217     make binrpm-pkg || ( popd ; return 255 )
1218     local addlnxrel=
1219     [ -f ".version" ] && addlnxrel="-$(cat .version)"
1220     popd
1221     local arch=
1222     for arch in $TARGET_ARCHS_ALL; do
1223         [ -f "/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
1224             KERNELRPM="/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
1225             TARGET_ARCH="$arch"
1226         [ -f "/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
1227             KERNELRPM="/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
1228             TARGET_ARCH="$arch"
1229     done
1230     [ "$KERNELRPM" = "" ] || return
1231     return 255
1232 }
1233
1234 #build linux kernel
1235 build_linux()
1236 {
1237     local nofullmake=$1
1238     local nocopykernel=$2
1239     pushd $LINUX || fatal 1 "Kernel source not found"
1240     [ "$nofullmake" = "nofullmake" ] || make mrproper
1241     [ "$nofullmake" = "nofullmake" ] || rm -f rpm-release
1242 #    [ "$nocopykernel" = "copyrpmkernel" ] || rm -f localversion-*
1243     [ "$nocopykernel" = "copykernel" ] && [ -f "$CONFIG_FILE" ] && cp $CONFIG_FILE .config
1244     if [ "$nocopykernel" = "copyrpmkernel" ]; then
1245         [ -f "$KERNELRPMCONFIG" ] && cp $KERNELRPMCONFIG .config
1246         if [ -f "$KERNELRPMRELEASE" ]; then
1247             cp $KERNELRPMRELEASE .
1248         else
1249             sed -e "s/^EXTRAVERSION\s\+=\s\+.*$/EXTRAVERSION = -${lnxrel}/" < Makefile > Makefile.new
1250             [ -f "Makefile.new" ] && mv Makefile.new Makefile
1251         fi
1252     fi
1253     [ -f ".config" ] || ( popd ; echo "Cannot find .config file"; return 255 )
1254     make oldconfig || ( popd ; return 255 )
1255     make include/linux/version.h
1256     if [ ! "$nofullmake" = "nofullmake" ]; then
1257         make || ( popd ; return 255 )
1258     fi
1259     popd
1260     return
1261 }
1262
1263 build_kernel_ib()
1264 {
1265     # build kernel-ib{,-devel}
1266     # some I/B drivers are architecture dependent and kernel-ib's configure
1267     # does not figure it out for us ~sigh~
1268     local configure_options=""
1269     case "$TARGET_ARCH" in
1270         x86_64 | ia64)
1271             configure_options="--with-ipath_inf-mod"
1272             ;;
1273         ppc64)
1274             configure_options="--with-ipath_inf-mod --with-ehca-mod"
1275             ;;
1276     esac
1277     $RPMBUILD --rebuild --define 'build_kernel_ib 1' --define 'build_kernel_ib_devel 1' \
1278               --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
1279               --define "KVERSION ${LINUXRELEASE}" \
1280               --define "KSRC ${LINUXOBJ:-${LINUX}}" \
1281               --define "LIB_MOD_DIR /lib/modules/${LINUXRELEASE}/updates" \
1282               --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
1283
1284     if [ ${PIPESTATUS[0]} != 0 ]; then
1285         fatal 1 "Error building kernel-ib"
1286     fi
1287
1288     pushd "$TOPDIR" >/dev/null
1289     rm -rf kernel-ib-devel
1290     mkdir kernel-ib-devel
1291     cd kernel-ib-devel
1292     local rpm=$(ls $TOPDIR/RPMS/*/kernel-ib-devel-${OFED_VERSION}-${LINUXRELEASE//-/_}.*.rpm)
1293     rpm2cpio -itv < $rpm | cpio -id
1294     CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
1295     popd >/dev/null
1296 }
1297
1298 #build patchless lustre
1299 patchless_build_sequence()
1300 {
1301     #try to build from kernel-devel RPM (RHEL)
1302     LINUX=
1303     TARGET_ARCH=
1304     local rpmfound=false
1305     local buildsuccess=false
1306     local storeforreuse=false
1307     if [ "$KERNELRPMSBASE" = "" ] || [ ! -d "$KERNELRPMSBASE" ]; then
1308         return 255
1309     fi
1310     [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
1311
1312     local delimiter=${EXTRA_VERSION_DELIMITER:-"-"}
1313     # default to source type -source and special case below
1314     local type=-source
1315     case "$DISTRO" in
1316         rhel*)
1317             type=-devel
1318             ;;
1319     esac
1320
1321     unpack_linux_rpm $type $delimiter && rpmfound=true
1322
1323     [ -d SRPMS ] || mkdir SRPMS
1324     [ -d RPMS ] || mkdir RPMS
1325     [ -d BUILD ] || mkdir BUILD
1326     [ -d SOURCES ] || mkdir SOURCES
1327
1328     # first build kernel-ib
1329     if [ -n "$OFED_VERSION" ]; then
1330         $rpmfound && build_kernel_ib
1331     fi
1332     ( $rpmfound ) && build_lustre && buildsuccess=true && find_linux_source_rpm
1333
1334     if $buildsuccess; then
1335         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELRPM" ] && \
1336             cp "$KERNELRPM" RPMS/${TARGET_ARCH}/
1337         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELSOURCERPM" ] && \
1338             cp "$KERNELSOURCERPM" RPMS/${TARGET_ARCH}/
1339         KERNELCOMPILEDIR="$LINUX"
1340         if $storeforreuse; then
1341             store_for_reuse || echo "Cannot store for future reuse"
1342         fi
1343
1344         return
1345     elif ! $rpmfound; then
1346         echo "COULD NOT FIND VENDOR -devel or -source RPM for $DISTRO/$TARGET_ARCH: $lnxmaj-$lnxrel in $KERNELRPMSBASE"
1347         return 255
1348     else
1349         echo "Patchless build failed."
1350         return 255
1351     fi
1352 }
1353
1354 #check timestamp value. should bi 14-digits string
1355 check_timestamp()
1356 {
1357     local invalue=$1
1358     local timestampnodig=$(echo $invalue | sed -e s/[0-9]*//g)
1359     [ "$timestampnodig" = "" ] || return 255
1360     local timestamplength="${#invalue}"
1361     [ $timestamplength -eq 14 ] || return 255
1362 }
1363
1364 # get list of suitable directories with potential reused staff
1365 get_reuse_dir_list()
1366 {
1367     local rpmonly=$1
1368     local reusedkernelmasknew=$2
1369     local buildtimestamp=
1370     local dirsforreuse=
1371     local sourcelastdate=$(get_last_source_date)
1372     for buildtimestamp in $(ls "$REUSEBUILD/" 2>&1); do
1373         [ -d "$REUSEBUILD/$buildtimestamp" ] || continue
1374         check_timestamp "$buildtimestamp" || continue
1375         local buildtimestampstr=$(echo $buildtimestamp | \
1376             sed -e "s^\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)^\1-\2-\3 \4:\5:\6 GMT^g")
1377         local buildtimestampepoch=$(date --date="$buildtimestampstr"  +%s )
1378         #check for suitable date
1379             if ! $PATCHLESS; then
1380             [ $buildtimestampepoch -ge $sourcelastdate ] || continue
1381             fi
1382         #check for suitable version
1383         if [ "$rpmonly" = "rpmonly" ]; then
1384             local reusedkernelprefix="kernel-lustre-"
1385             ( $PATCHLESS ) && reusedkernelprefix=
1386             local rpmmask="${reusedkernelprefix}${REUSEDKERNELMASK}"
1387             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
1388             [ -f $REUSEBUILD/$buildtimestamp/${rpmmask}.rpm ] && \
1389                 dirsforreuse="$dirsforreuse $REUSEBUILD/$buildtimestamp"
1390         else
1391             local rpmmask="$REUSEDKERNELMASK"
1392             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
1393             pushd $REUSEBUILD/$buildtimestamp/linux-${rpmmask} > /dev/null 2>&1 || continue
1394             local curdir=$(pwd)
1395             dirsforreuse="$dirsforreuse $curdir"
1396             popd
1397         fi
1398     done
1399     echo "$dirsforreuse"
1400 }
1401
1402 #try to reuse old RPM
1403 build_sequence_rpm_reuse()
1404 {
1405     local sourcerpm=$1
1406     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
1407     local dirsforreuse=
1408     if ! [ "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse RPM
1409         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
1410         REUSEDKERNELMASKnew="kernel-lustre-source-${REUSEDKERNELMASKnew}"
1411         local dirsforreuse="$(get_reuse_dir_list rpmonly $REUSEDKERNELMASKnew)"
1412         local buildsuccess=false
1413         LINUXOBJ=
1414         for curdir in $(echo $dirsforreuse); do
1415             [ -d "$curdir" ] || continue
1416             local reusedkernelprefix="kernel-lustre-"
1417             local reusedkernelrpm=
1418             [ -f ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
1419                 reusedkernelrpm=$(ls ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
1420             [ -f "$reusedkernelrpm" ] || continue
1421
1422             local reusedkernelsourcerpm=
1423             [ -f ${curdir}/${REUSEDKERNELMASKnew}.rpm ] && \
1424                 reusedkernelsourcerpm=$(ls ${curdir}/${REUSEDKERNELMASKnew}.rpm | head -1 )
1425             [ -f "$reusedkernelsourcerpm" ] || continue
1426
1427             # don't need to check for kernel-ib RPM reuse here because sles9 is not supported
1428             # by OFED >= 1.3.0 and this function appears to only be used for sles9
1429
1430             [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
1431             reuse_kernel_rpm "$reusedkernelsourcerpm" "" && build_linux nofullmake copyrpmkernel && build_lustre && buildsuccess=true
1432             ( $buildsuccess ) || continue
1433             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
1434                 [ -f "$reusedkernelrpm" ] && \
1435                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1436  
1437                 [ -f "$reusedkernelsourcerpm" ] && \
1438                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
1439                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
1440             fi
1441             return
1442         done
1443     fi
1444     return 255
1445 }
1446
1447 #try to reuse old BUILD dir
1448 build_sequence_reuse()
1449 {
1450     local sourcerpm=$1
1451     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
1452     local dirsforreuse=
1453     if [ ! "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse old kernel build directory
1454         local dirsforreuse="$(get_reuse_dir_list)"
1455         local buildsuccess=false
1456         LINUXOBJ=
1457         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
1458         for curdir in $(echo $dirsforreuse); do
1459             local reusedkernelrpm=
1460             local reusedkernelsourcerpm=
1461             local reusedkernelibrpm=
1462             [ -d "$curdir" ] || continue
1463             [ -n "$OFED_VERSION" -a ! -d "${curdir%/*}/ofa_kernel" ] && continue
1464             local reusedkernelprefix="kernel-lustre-"
1465             ( $PATCHLESS ) && reusedkernelprefix=
1466             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
1467                 reusedkernelrpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
1468             reusedkernelprefix="kernel-lustre-source-"
1469             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm ] && \
1470                 reusedkernelsourcerpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm | head -1 ) 
1471             if [ -n "$OFED_VERSION" ]; then
1472                 gen_lustre_version
1473                 reusedkernelprefix="kernel-ib-"
1474                 [ -f ${curdir}/../${reusedkernelprefix}${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm ] && \
1475                     reusedkernelibrpm=$(ls ${curdir}/../${reusedkernelprefix}${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm | head -1 ) 
1476                     reusedkernelibdevelrpm=$(ls ${curdir}/../${reusedkernelprefix}devel-${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm | head -1 ) 
1477             fi
1478             if ! ( $NORPM ) && ! [ -f "$reusedkernelrpm" ]; then #kernel rpm not found. Build all
1479                 continue
1480             fi
1481             if ! ( $NORPM ) && ! [ -f "$reusedkernelsourcerpm" ]; then #kernel source rpm not found. Build all
1482                 continue
1483             fi
1484             if [ -n "$OFED_VERSION" ]; then
1485                 if ! ( $NORPM ) && [ ! -f "$reusedkernelibrpm" -o ! -f "$reusedkernelibdevelrpm"]; then #kernel-ib{,-devel} rpm not found. Build all
1486                     continue
1487                 fi
1488                 CONFIGURE_FLAGS="--with-o2ib=${curdir%/*}/ofa_kernel ${CONFIGURE_FLAGS}"
1489             fi
1490             LINUX="$curdir"
1491             build_lustre || continue
1492             touch "$curdir/../"
1493             buildsuccess=true
1494             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
1495                 [ -f "$reusedkernelrpm" ] && \
1496                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
1497                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
1498                 [ -f "$reusedkernelsourcerpm" ] && \
1499                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1500                 [ -f "$reusedkernelibrpm" ] && \
1501                     cp -f  "$reusedkernelibrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1502                     cp -f  "$reusedkernelibdevelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1503             fi
1504             return
1505         done
1506     fi
1507     return 255
1508 }
1509
1510
1511 build_sequence()
1512 {
1513     if (( $DO_SRC )) ; then
1514         unpack_linux
1515         patch_linux
1516         pack_linux
1517         clean_linux
1518     fi
1519     prep_kernel_build || return 255
1520     clean_lustre || return 255
1521
1522     build_kernel || return 255
1523 }
1524
1525 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
1526
1527 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: -- "$@")
1528
1529 if [ $? != 0 ] ; then
1530     usage 1
1531 fi
1532
1533 eval set -- "$options"
1534     
1535 while [ "$1" ] ; do
1536     case "$1" in
1537         '')
1538             usage 1
1539             ;;
1540         --ccache)
1541             CCACHE='ccache'
1542             shift
1543             ;;
1544         -d)
1545             CVSROOT=$2
1546             shift 2
1547             ;;
1548         -D)
1549             DATE=$2
1550             shift 2
1551             ;;
1552         --external-patches)
1553             EXTERNAL_PATCHES=$2
1554             shift 2
1555             ;;
1556         --extraversion)
1557             EXTRA_VERSION=$2
1558             shift 2
1559             ;;
1560         --help | -h)
1561             usage 0
1562             ;;
1563         --kerneldir)
1564             KERNELDIR=$2
1565             shift 2
1566             ;;
1567         --kerneltree)
1568             KERNELTREE=$2
1569             shift 2
1570             ;;
1571         --linux | --with-linux)
1572             LINUX=$2
1573             shift 2
1574             ;;
1575         --distro)
1576             DISTRO=$2
1577             shift 2
1578             ;;
1579         --reuserpm)
1580             REUSERPM=$2
1581             shift 2
1582             ;;
1583         --reusebuild)
1584             REUSEBUILD=$2
1585             shift 2
1586             ;;
1587         --norpm)
1588             NORPM=true
1589             shift
1590             ;;
1591         --ldiskfs)
1592             LDISKFSRPM=true
1593             shift
1594             ;;
1595         --patchless)
1596             PATCHLESS=true
1597             shift
1598             ;;
1599         --kernelrpm)
1600             KERNELRPMSBASE=$2
1601             shift 2
1602             ;;
1603         --timestamp)
1604             TIMESTAMP=$2
1605             shift 2
1606             ;;
1607         --lustre)
1608             LUSTRE=$2
1609             shift 2
1610             ;;
1611         --nodownload)
1612             DOWNLOAD=0
1613             shift 1
1614             ;;
1615         --nosrc)
1616             DO_SRC=0
1617             shift 1
1618             ;;
1619         --publish)
1620             shift
1621             ;;
1622         --release)
1623             RELEASE=true
1624             shift
1625             ;;
1626         --src)
1627             DO_SRC=1
1628             shift 1
1629             ;;
1630         --stage)
1631             STAGEDIR=$2
1632             shift 2
1633             ;;
1634         --tag)
1635             TAG=$2
1636             shift 2
1637             ;;
1638         --target)
1639             TARGET=$2
1640             shift 2
1641             ;;
1642         --target-archs)
1643             TARGET_ARCHS=$2
1644             shift 2
1645             ;;
1646         --disable-datestamp)
1647             USE_DATESTAMP=
1648             shift
1649             ;;
1650         --)
1651             shift
1652             CONFIGURE_FLAGS=$@
1653             CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-liblustre --enable-liblustre-tests"
1654             break
1655             ;; 
1656         *)
1657             usage 1 "Unrecognized option: $1"
1658             ;;
1659     esac
1660 done
1661
1662 check_options
1663
1664 unpack_lustre
1665
1666 load_target
1667 EXTRA_VERSION_DELIMITER=${EXTRA_VERSION_DELIMITER:-"-"}
1668
1669 if [ -n "$OFED_VERSION" ]; then
1670     unpack_ofed
1671 fi
1672
1673 build_success=false
1674 if $PATCHLESS; then
1675     patchless_build_sequence && build_success=true 
1676 elif [ -z "$LINUX" ] ; then
1677     [ "$DISTRO" = "sles9" ] && build_sequence_rpm_reuse && build_success=true
1678     if ! $build_success; then
1679         build_sequence_reuse && build_success=true
1680         if ! $build_success; then
1681             build_sequence && build_success=true
1682             if $build_success; then
1683                 store_for_reuse || echo "Cannot store for future reuse"
1684             fi
1685         fi
1686     fi
1687 else
1688     build_lustre && build_success=true
1689 fi
1690 ( $build_success ) || fatal 1 "Cannot build lustre"
1691
1692 stage