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