Whamcloud - gitweb
windows porting: add private lnet tag to b_winnt_port branch
[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
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://downloads.lustre.org/public/kernels/$CANONICAL_TARGET/old/$KERNEL..."
424                     if ! wget -nv "http://downloads.lustre.org/public/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_TARGET_DELIMITER@^$TARGET_DELIMITER^g" \
633         -e "s^@KERNEL_RELEASE@^${EXTRA_VERSION//-/_}^g" \
634         -e "s^@KERNEL_SOURCE@^$KERNEL^g" \
635         -e "s^@KERNEL_VERSION@^$VERSION^g" \
636         -e "s^@LINUX26@^$LINUX26^g" \
637         -e "s^@LUSTRE_SOURCE@^${LUSTRE##*/}^g" \
638         -e "s^@LUSTRE_TARGET@^$TARGET^g" \
639         -e "s^@PSERIES64_ARCHS@^$PSERIES64_ARCHS^g" \
640         -e "s^@RHBUILD@^$RHBUILD^g" \
641         -e "s^@SMP_ARCHS@^$SMP_ARCHS^g" \
642         -e "s^@SUSEBUILD@^$SUSEBUILD^g" \
643         -e "s^@UP_ARCHS@^$UP_ARCHS^g" \
644         < $TOPDIR/lustre/build/lustre-kernel-2.4.spec.in \
645         > lustre-kernel-2.4.spec
646     [ -d SRPMS ] || mkdir SRPMS
647     [ -d RPMS ] || mkdir RPMS
648     [ -d BUILD ] || mkdir BUILD
649     [ -d SOURCES ] || mkdir SOURCES
650     for script in linux-{rhconfig.h,merge-config.awk,merge-modules.awk} \
651         suse-{functions.sh,post.sh,postun.sh,trigger-script.sh.in} \
652         sles8-{pre,post,postun,update_{INITRD_MODULES,rcfile_setting}}.sh ; do
653         cp $TOPDIR/lustre/build/$script SOURCES
654     done
655     cp "$LUSTRE" "$KERNEL_FILE" SOURCES
656     if [ "$EXTERNAL_PATCHES" -a -d "$EXTERNAL_PATCHES" ] ; then
657         tar zcf SOURCES/external-patches.tar.gz -C "$EXTERNAL_PATCHES" series targets patches kernel_configs
658     else
659         touch SOURCES/external-patches.tar.gz
660     fi
661 }
662
663 clean_lustre()
664 {
665     [ -d lustre ] || return 0
666     echo "Cleaning Lustre..."
667     [ -L lustre ] && rm -rf $(readlink lustre)
668     rm -rf lustre
669 }
670
671 build_kernel()
672 {
673     echo "Building kernel + Lustre RPMs for: $BUILD_ARCHS..."
674     targets=
675     for arch in $BUILD_ARCHS ; do
676         targets="--target $arch $targets"
677     done
678
679     local rpmbuildopt='-bb'
680 #    if $NORPM; then
681 #        rpmbuildopt='-bc'
682 #        echo NORPM mode. Only compiling.
683 #        echo "XXX: need to fix lmake - add options to do rpmbuild -bc instead of -bb"
684 #    fi
685
686     $RPMBUILD $targets $rpmbuildopt lustre-kernel-2.4.spec \
687         --define "_tmppath $TMPDIR" \
688         --define "_topdir $TOPDIR" || \
689         fatal 1 "Error building rpms for $BUILD_ARCHS."
690
691     if (( $DO_SRC )) ; then
692         $RPMBUILD -bs lustre-kernel-2.4.spec \
693             --define "_tmppath $TMPDIR" \
694             --define "_topdir $TOPDIR" || \
695             fatal 1 "Error building .src.rpm."
696     fi
697
698     ( $(skeep_ldiskfs_rpm $TAG) ) && return
699
700     pushd $TOPDIR/BUILD/lustre*/ldiskfs || return 255
701     make dist
702     if [ "$?" != "0" ] ; then
703         popd
704         return 255
705     fi
706     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
707
708     gen_lustre_version
709
710     local ldiskfs_spec=lustre-ldiskfs.spec
711     [ -f "$ldiskfs_spec" ] && sed \
712     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
713     < $ldiskfs_spec \
714     > ../lustre-ldiskfs.spec
715
716     $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
717         --define "_tmppath /var/tmp" \
718         --define "_topdir $TOPDIR"
719     if [ "$?" != "0" ] ; then
720         popd
721         return 255
722     fi
723
724     if (( $DO_SRC )) ; then
725             $RPMBUILD -bs ../lustre-ldiskfs.spec \
726             --define "_tmppath /var/tmp" \
727             --define "_topdir $TOPDIR"
728         if [ "$?" != "0" ] ; then
729             popd
730             return 255
731         fi
732     fi
733     popd
734 }
735
736 build_lustre()
737 {
738     [ -d SRPMS ] || mkdir SRPMS
739     [ -d RPMS ] || mkdir RPMS
740     [ -d BUILD ] || mkdir BUILD
741     [ -d SOURCES ] || mkdir SOURCES
742
743     cp "$LUSTRE" SOURCES
744
745     pushd lustre >/dev/null
746
747     echo "Building Lustre RPMs for: $BUILD_ARCHS..."
748     targets=
749     for arch in $BUILD_ARCHS ; do
750         targets="--target $arch $targets"
751     done
752
753     local confoptions="--with-linux=${LINUX}"
754     if $PATCHLESS; then
755         confoptions="--with-linux=${LINUX} --disable-server"
756     fi
757     if [ ! "$LINUXOBJ" = "" ]; then
758         confoptions="$confoptions --with-linux-obj=${LINUXOBJ}" 
759     fi
760     
761     ./configure $confoptions ${CONFIGURE_FLAGS}
762     if [ "$?" != "0" ] ; then
763         local saved_config="../config.log.$(date +%s)"
764         cp config.log $saved_config
765         chmod a+r $saved_config
766         echo "Saved config.log is at $saved_config"
767         popd
768         return 255
769     fi
770
771     gen_lustre_version
772
773     # hack. Somebody move build/lustre.spec to lustre.spec for b1_6
774     local lustre_spec=
775     [ -f lustre.spec ] && lustre_spec=lustre.spec
776     [ -f build/lustre.spec ] && lustre_spec=build/lustre.spec
777
778     [ -f "$lustre_spec" ] && sed \
779         -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
780         < $lustre_spec \
781         > ../lustre.spec
782
783     local rpmbuildopt='-bb'
784     if $NORPM; then
785         rpmbuildopt='-bc'
786         echo NORPM mode. Only compiling.
787     fi
788
789     $RPMBUILD $targets $rpmbuildopt ../lustre.spec \
790         --define "_tmppath $TMPDIR" \
791         --define "_topdir $TOPDIR" || \
792         fatal 1 "Error building rpms for $BUILD_ARCHS."
793
794     popd >/dev/null
795     ( $(skeep_ldiskfs_rpm $TAG) ) && return
796
797     pushd lustre/ldiskfs || return 255
798     make dist
799     if [ "$?" != "0" ] ; then
800         popd
801         return 255
802     fi
803     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
804
805     gen_lustre_version
806
807     local ldiskfs_spec=lustre-ldiskfs.spec
808     [ -f "$ldiskfs_spec" ] && sed \
809     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
810     < $ldiskfs_spec \
811     > ../lustre-ldiskfs.spec
812
813     $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
814         --define "_tmppath /var/tmp" \
815         --define "_topdir $TOPDIR"
816     if [ "$?" != "0" ] ; then
817         popd
818         return 255
819     fi
820
821     if (( $DO_SRC )) ; then
822             $RPMBUILD -bs ../lustre-ldiskfs.spec \
823             --define "_tmppath /var/tmp" \
824             --define "_topdir $TOPDIR"
825         if [ "$?" != "0" ] ; then
826             popd
827             return 255
828         fi
829     fi
830     popd
831 }
832
833 stage()
834 {
835     [ "$STAGEDIR" ] || return 0
836
837     for arch in $BUILD_ARCHS ; do
838         rpmdir="${STAGEDIR}/${CANONICAL_TARGET}-${arch}"
839         echo "${0##*/}: Copying RPMs into ${rpmdir}"
840         mkdir -p "${rpmdir}"
841         cp -v RPMS/${arch}/*.rpm "${rpmdir}"
842         if [ -d RPMS/noarch ] ; then
843             cp -v RPMS/noarch/*.rpm "${rpmdir}"
844         fi
845     done
846
847     cp -v "$LUSTRE" "$STAGEDIR"
848 }
849
850 #check if we need to build separate ldiskfs RPM
851 skeep_ldiskfs_rpm()
852 {
853         local tag=$1
854         local skip=false
855         if ! $LDISKFSRPM; then
856             skip=true
857         elif $PATCHLESS; then
858             skip=true
859         else
860             for skiptag in $SKIPLDISKFSRPM; do
861                 [[ $tag == $skiptag ]] && skip=true && break
862             done
863         fi
864         echo $skip
865 }
866
867 #get date of last changed target/config/series/patches
868 get_last_source_date()
869 {
870     local filelist="${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} \
871         $CONFIG_FILE"
872     local TOPDIRnew=$(echo ${TOPDIR} | sed -e s/\\//\\\\\\//g)
873     filelist="$filelist $( \
874         cat ${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} | \
875         sed -e s/^/${TOPDIRnew}\\/lustre\\/lustre\\/kernel_patches\\/patches\\// 2>&1)"
876     local sourcelastdate=$( find ${filelist} -name CVS -prune -o \
877                 -type f -printf "%T@\n" 2>&1 | sort | tail -1 )
878     is_integer $sourcelastdate && echo $sourcelastdate
879 }
880
881 #check if variable is integer
882 is_integer()
883 {
884     local invariable=$1
885     [ "$invariable" = "" ] && return 255
886     local invariableint=$( echo $invariable | sed -e s/[^0-9]//g )
887     [ "$invariable" = "$invariableint" ] || return 255
888 }
889
890 #generate LUSTRE_EXTRA_VERSION from EXTRA_VERSION
891 gen_lustre_version()
892 {
893     local smptype=smp
894     [ "$KERNCONFSMPTYPE" = "" ] || smptype=$KERNCONFSMPTYPE
895     [ "$RPMSMPTYPE" = "" ] || smptype=$RPMSMPTYPE
896
897     LUSTRE_EXTRA_VERSION="${lnxmaj}-${EXTRA_VERSION}"
898     if [ "$PATCHLESS" = "true" -a  "$DISTRO" = "sles10" ]; then
899         LUSTRE_EXTRA_VERSION="${LUSTRE_EXTRA_VERSION}-${smptype}"
900     else
901         LUSTRE_EXTRA_VERSION="${LUSTRE_EXTRA_VERSION}${smptype}"
902     fi
903     LUSTRE_EXTRA_VERSION=$( echo $LUSTRE_EXTRA_VERSION | sed -e "s^-^_^g" )
904 }
905
906 #store RPMs and/or BUILD dir for future reuse
907 store_for_reuse()
908 {
909     local rpmonly=$1
910     if [ ! "$REUSEBUILD" = "" ] && [ -d  "/$REUSEBUILD/" ] ; then
911         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || mkdir "${REUSEBUILD}/${TIMESTAMP}"
912         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || return 255
913     else
914         return 255
915     fi
916
917     local lnxrelnew=$( echo ${lnxrel} | sed s/-/_/g )
918         local EXTRA_VERSIONnew=$( echo ${EXTRA_VERSION} | sed s/-/_/g )
919         local KERNELRPMnew=$(basename "$KERNELRPM")
920     if [ ! "$rpmonly" = "rpmonly" ]; then
921             local builddir=
922         if [ ! "$KERNELCOMPILEDIR" = "" ]; then
923             builddir="$KERNELCOMPILEDIR"
924         else
925             builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}"
926                 [ "$KERNELCOMPILEDIR" = "" ] || builddir="$KERNELCOMPILEDIR"
927             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}.${lnxrel}"
928             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}-${lnxrel}"
929                 if [ ! -d "$builddir" ]; then
930                 pushd "BUILD/lustre-kernel-${lnxmaj}/lustre/" || return 255
931                 local basebuilddir=$(ls -d linux-${lnxmaj}* | head -1)
932                 [ "$basebuilddir" = "" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/${basebuilddir}"
933                 popd
934                 fi
935         fi
936         [ -d "$builddir" ] || return 255
937             local dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}"
938             ( $PATCHLESS ) && dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-$KERNELRPMnew" && \
939                     dstdir="${dstdir%.rpm}"
940             [ -d "$dstdir" ] && rm -rf "$dstdir"
941         mv "${builddir}" "$dstdir" || return 255
942     fi
943     #store kernel rpm
944     local kernelrpmname="kernel-lustre-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
945     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSNnew}.${TARGET_ARCH}.rpm"
946         ( $PATCHLESS ) && [ -f "$KERNELRPM" ] && kernelrpmname="$KERNELRPMnew"
947     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
948         echo "RPM already exist in store directory tree"
949     else
950         [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
951     fi
952     #store kernel source rpm
953     kernelrpmname="kernel-lustre-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
954     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
955     ( $PATCHLESS ) && [ -f "$KERNELSOURCERPM" ] && kernelrpmname=$(basename "$KERNELSOURCERPM")
956     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
957         echo "RPM already exist in store directory tree"
958     else
959             [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
960     fi
961 }
962
963 set_rpm_smp_type()
964 {
965     RPMSMPTYPE=default
966     for smp_type in $SMP_ARCHS; do
967         [ $TARGET_ARCH == $smp_type ] && RPMSMPTYPE=smp && break
968     done
969
970     for smp_type in $BIGSMP_ARCHS; do
971         [ $TARGET_ARCH == $smp_type ] && RPMSMPTYPE=bigsmp && break
972     done
973 }
974
975 #unpack kernel(/source/devel) RPM
976 unpack_linux_rpm()
977 {
978     local prefix=$1
979     local delimiter=${2:-"-"}
980     local pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
981     local kernelbinaryrpm=
982     [ -d $pathtorpms ] || return 255
983     local rpmfile=
984     local wanted_kernel="${lnxmaj}${delimiter}${lnxrel}"
985     for arch in $TARGET_ARCHS_ALL; do
986         for rpm in ${pathtorpms}/${arch}/*.rpm; do
987             local provides=$(rpm -q --provides -p $rpm)
988             if (echo $provides | grep -q "kernel${prefix} = $wanted_kernel"); then
989                 KERNELRPM=$rpm
990             fi
991         done
992         [ -f "$KERNELRPM" ] && TARGET_ARCH="$arch" && BUILD_ARCHS="$arch" && break
993     done
994
995     # bleah
996     set_rpm_smp_type
997
998     [ -f "$KERNELRPM" ] || return 255
999     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
1000     pushd $TOPDIR/reused || return 255
1001     RC=0
1002     rpm2cpio < "$KERNELRPM" | cpio -idc > /dev/null 2>&1
1003     if [ ${PIPESTATUS[0]} -eq 0 ]; then
1004         # RHEL-style and SLES-style rpms
1005         local paths="kernels/${lnxmaj}${delimiter}${lnxrel}-${TARGET_ARCH} linux-${lnxmaj}${delimiter}${lnxrel}"
1006
1007         for path in $paths; do
1008             local src='usr/src'
1009
1010             if [ -d "$src/$path/" ]; then
1011                 LINUX="$(pwd)/$src/$path"
1012             fi
1013             # SLES has a separate -obj tree
1014             if [ -d "$src/${path}-obj" ]; then
1015                 src="$src/${path}-obj"
1016                 local objects="$TARGET_ARCH/$RPMSMPTYPE"
1017
1018                 # Novell, are you *TRYING* to make life hard for me?
1019                 if [ -d "$src/powerpc" ]; then
1020                     objects="powerpc/$TARGET_ARCH"
1021                 elif [ $TARGET_ARCH == 'i686' ]; then
1022                     objects="i386/$RPMSMPTYPE"
1023                 fi
1024
1025                 LINUXOBJ="$(pwd)/$src/$objects"
1026             fi
1027         done
1028         [ -z "$LINUX" ] && RC=255
1029     else
1030         RC=255
1031     fi
1032     popd
1033     return $RC
1034 }
1035
1036 #look for kernel source RPM
1037 find_linux_source_rpm()
1038 {
1039     local rpmfile=
1040     local findarch=true
1041     local arch=
1042     local pathtorpms=
1043     [ ! "$TARGET_ARCH" = "" ] && arch=$TARGET_ARCH && findarch=false
1044     
1045     if ! $findarch; then
1046         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
1047         [ -d $pathtorpms ] || return 255
1048         case "$DISTRO" in
1049             rhel4)
1050                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
1051             ;;
1052             sles10)
1053                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
1054             ;;
1055             *)
1056                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
1057             ;;
1058         esac
1059         [ -f "${pathtorpms}/${rpmfile}" ] || return 255
1060         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
1061     else
1062         for arch in $TARGET_ARCHS_ALL; do
1063         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
1064         [ -d $pathtorpms ] || continue
1065         case "$DISTRO" in
1066             rhel4)
1067                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
1068             ;;
1069             sles10)
1070                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
1071             ;;
1072             *)
1073                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
1074             ;;
1075         esac
1076         [ -f "${pathtorpms}/${rpmfile}" ] || continue
1077         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
1078         TARGET_ARCH=${arch}
1079         break
1080         done
1081     fi
1082     [ -f "${KERNELSOURCERPM}" ] || return 255
1083 }
1084
1085 #unpack and make symlinks for reusing kernel RPM
1086 reuse_kernel_rpm()
1087 {
1088     local pathtorpm=$1
1089     [ "$pathtorpm" = "" ] && return 255
1090     [ -f "$pathtorpm" ] || return 255
1091     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
1092     pushd $TOPDIR/reused || return 255
1093
1094     rpm2cpio < $pathtorpm | cpio -idc
1095     [ ${PIPESTATUS[0]} -eq 0 ] || return 255
1096
1097     local smptype=
1098     if pushd usr/src/linux-*-obj/${TARGET_ARCH}; then
1099         local smptypes="$SMPTYPES"
1100         [ "$RPMSMPTYPE" = "" ] || smptypes=$RPMSMPTYPE
1101         ( ! $PATCHLESS ) && [ ! "$KERNCONFSMPTYPE" = "" ] && smptypes="$KERNCONFSMPTYPE"
1102         local cursmptype=
1103         for cursmptype in $smptypes; do
1104             [ "$cursmptype" = "''" ] && continue
1105             [ -d $cursmptype ] && smptype=$cursmptype
1106             [ -d $smptype ] && break
1107         done
1108         popd
1109     fi
1110     if [ "${smptype}" = "" ]; then
1111         popd
1112         return 255 # cannot detect smp type
1113     fi
1114     if pushd usr/src/linux-*-obj/${TARGET_ARCH}/$smptype/include2; then
1115         local base=$(readlink asm)
1116         if [ ! -d "/${base}/" ]; then
1117             rm -f asm
1118             base=$(basename "$base")
1119             if pushd ../../../../linux-*/include; then
1120                 local lsrc=$(pwd)
1121                 popd
1122                 [ -d "$lsrc/${base}" ] && ln -s $lsrc/${base} asm
1123             fi
1124         fi
1125         popd
1126         read a b < <(echo $(pwd)/usr/src/linux-*)
1127         if [[ $a == $(pwd)/* && $b = $(pwd)/* ]]; then
1128             cp -f $a/include/linux/config.h $b/${TARGET_ARCH}/$smptype/include/linux/
1129             cp $b/${TARGET_ARCH}/$smptype/.config $a/
1130             [ -f "$b/${TARGET_ARCH}/$smptype/.config" ] && KERNELRPMCONFIG="$b/${TARGET_ARCH}/$smptype/.config"
1131             cp $b/${TARGET_ARCH}/$smptype/.kernelrelease $a/
1132             [ -f "$b/${TARGET_ARCH}/$smptype/.kernelrelease" ] && KERNELRPMRELEASE="$b/${TARGET_ARCH}/$smptype/.kernelrelease"
1133             LINUX=$a
1134             LINUXOBJ=$b/${TARGET_ARCH}/$smptype
1135 #            local fname=$(basename $kernel_rpm)
1136 #            KERNELRPMSDIR=${kernel_rpm%$fname}
1137         fi
1138     fi
1139     popd
1140     [ "$LINUX" = "" ] && return 255
1141     [ -d "$LINUX" ] || return 255
1142 }
1143
1144 #build linux kernel rpm
1145 build_linux_rpm()
1146 {
1147     pushd $LINUX || return 255
1148     make binrpm-pkg || ( popd ; return 255 )
1149     local addlnxrel=
1150     [ -f ".version" ] && addlnxrel="-$(cat .version)"
1151     popd
1152     local arch=
1153     for arch in $TARGET_ARCHS_ALL; do
1154         [ -f "/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
1155             KERNELRPM="/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
1156             TARGET_ARCH="$arch"
1157         [ -f "/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
1158             KERNELRPM="/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
1159             TARGET_ARCH="$arch"
1160     done
1161     [ "$KERNELRPM" = "" ] || return
1162     return 255
1163 }
1164
1165 #build linux kernel
1166 build_linux()
1167 {
1168     local nofullmake=$1
1169     local nocopykernel=$2
1170     pushd $LINUX || fatal 1 "Kernel source not found"
1171     [ "$nofullmake" = "nofullmake" ] || make mrproper
1172     [ "$nofullmake" = "nofullmake" ] || rm -f rpm-release
1173 #    [ "$nocopykernel" = "copyrpmkernel" ] || rm -f localversion-*
1174     [ "$nocopykernel" = "copykernel" ] && [ -f "$CONFIG_FILE" ] && cp $CONFIG_FILE .config
1175     if [ "$nocopykernel" = "copyrpmkernel" ]; then
1176         [ -f "$KERNELRPMCONFIG" ] && cp $KERNELRPMCONFIG .config
1177         if [ -f "$KERNELRPMRELEASE" ]; then
1178             cp $KERNELRPMRELEASE .
1179         else
1180             sed -e "s/^EXTRAVERSION\s\+=\s\+.*$/EXTRAVERSION = -${lnxrel}/" < Makefile > Makefile.new
1181             [ -f "Makefile.new" ] && mv Makefile.new Makefile
1182         fi
1183     fi
1184     [ -f ".config" ] || ( popd ; echo "Cannot find .config file"; return 255 )
1185     make oldconfig || ( popd ; return 255 )
1186     make include/linux/version.h
1187     if [ ! "$nofullmake" = "nofullmake" ]; then
1188         make || ( popd ; return 255 )
1189     fi
1190     popd
1191     return
1192 }
1193
1194 #build patchless lustre
1195 patchless_build_sequence()
1196 {
1197     #try to build from kernel-devel RPM (RHEL)
1198     LINUX=
1199     TARGET_ARCH=
1200     local rpmfound=false
1201     local buildsuccess=false
1202     local storeforreuse=false
1203     if [ "$KERNELRPMSBASE" = "" ] || [ ! -d "$KERNELRPMSBASE" ]; then
1204         return 255
1205     fi
1206     [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
1207
1208     local delimiter=${EXTRA_VERSION_DELIMITER:-"-"}
1209     # default to source type -source and special case below
1210     local type=-source
1211     case "$DISTRO" in
1212         rhel*)
1213             type=-devel
1214             ;;
1215     esac
1216
1217     unpack_linux_rpm $type $delimiter && rpmfound=true
1218     ( $rpmfound ) && build_lustre && buildsuccess=true && find_linux_source_rpm
1219
1220     if $buildsuccess; then
1221         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELRPM" ] && \
1222             cp "$KERNELRPM" RPMS/${TARGET_ARCH}/
1223         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELSOURCERPM" ] && \
1224             cp "$KERNELSOURCERPM" RPMS/${TARGET_ARCH}/
1225         KERNELCOMPILEDIR="$LINUX"
1226         if $storeforreuse; then
1227             store_for_reuse || echo "Cannot store for feature reuse"
1228         fi
1229
1230         return
1231     elif ! $rpmfound; then
1232         echo "COULD NOT FIND VENDOR -devel or -source RPM for $DISTRO/$TARGET_ARCH: $lnxmaj-$lnxrel in $KERNELRPMSBASE"
1233         return 255
1234     else
1235         echo "Patchless build failed."
1236         return 255
1237     fi
1238 }
1239
1240 #check timestamp value. should bi 14-digits string
1241 check_timestamp()
1242 {
1243     local invalue=$1
1244     local timestampnodig=$(echo $invalue | sed -e s/[0-9]*//g)
1245     [ "$timestampnodig" = "" ] || return 255
1246     local timestamplength="${#invalue}"
1247     [ $timestamplength -eq 14 ] || return 255
1248 }
1249
1250 # get list of suitable directories with potential reused staff
1251 get_reuse_dir_list()
1252 {
1253     local rpmonly=$1
1254     local reusedkernelmasknew=$2
1255     local buildtimestamp=
1256     local dirsforreuse=
1257     local sourcelastdate=$(get_last_source_date)
1258     for buildtimestamp in $(ls "$REUSEBUILD/" 2>&1); do
1259         [ -d "$REUSEBUILD/$buildtimestamp" ] || continue
1260         check_timestamp "$buildtimestamp" || continue
1261         local buildtimestampstr=$(echo $buildtimestamp | \
1262             sed -e "s^\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)^\1-\2-\3 \4:\5:\6 GMT^g")
1263         local buildtimestampepoch=$(date --date="$buildtimestampstr"  +%s )
1264         #check for suitable date
1265             if ! $PATCHLESS; then
1266             [ $buildtimestampepoch -ge $sourcelastdate ] || continue
1267             fi
1268         #check for suitable version
1269         if [ "$rpmonly" = "rpmonly" ]; then
1270             local reusedkernelprefix="kernel-lustre-"
1271             ( $PATCHLESS ) && reusedkernelprefix=
1272             local rpmmask="${reusedkernelprefix}${REUSEDKERNELMASK}"
1273             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
1274             [ -f $REUSEBUILD/$buildtimestamp/${rpmmask}.rpm ] && \
1275                 dirsforreuse="$dirsforreuse $REUSEBUILD/$buildtimestamp"
1276         else
1277             local rpmmask="$REUSEDKERNELMASK"
1278             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
1279             pushd $REUSEBUILD/$buildtimestamp/linux-${rpmmask} > /dev/null 2>&1 || continue
1280             local curdir=$(pwd)
1281             dirsforreuse="$dirsforreuse $curdir"
1282             popd
1283         fi
1284     done
1285     echo "$dirsforreuse"
1286 }
1287
1288 #try to reuse old RPM
1289 build_sequence_rpm_reuse()
1290 {
1291     local sourcerpm=$1
1292     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
1293     local dirsforreuse=
1294     if ! [ "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse RPM
1295         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
1296         REUSEDKERNELMASKnew="kernel-lustre-source-${REUSEDKERNELMASKnew}"
1297         local dirsforreuse="$(get_reuse_dir_list rpmonly $REUSEDKERNELMASKnew)"
1298         local buildsuccess=false
1299         LINUXOBJ=
1300         for curdir in $(echo $dirsforreuse); do
1301             [ -d "$curdir" ] || continue
1302             local reusedkernelprefix="kernel-lustre-"
1303             local reusedkernelrpm=
1304             [ -f ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
1305                 reusedkernelrpm=$(ls ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
1306             [ -f "$reusedkernelrpm" ] || continue
1307
1308             local reusedkernelsourcerpm=
1309             [ -f ${curdir}/${REUSEDKERNELMASKnew}.rpm ] && \
1310                 reusedkernelsourcerpm=$(ls ${curdir}/${REUSEDKERNELMASKnew}.rpm | head -1 )
1311             [ -f "$reusedkernelsourcerpm" ] || continue
1312
1313             [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
1314             reuse_kernel_rpm "$reusedkernelsourcerpm" && build_linux nofullmake copyrpmkernel && build_lustre && buildsuccess=true
1315             ( $buildsuccess ) || continue
1316             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
1317                 [ -f "$reusedkernelrpm" ] && \
1318                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1319  
1320                 [ -f "$reusedkernelsourcerpm" ] && \
1321                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
1322                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
1323             fi
1324             return
1325         done
1326     fi
1327     return 255
1328 }
1329
1330 #try to reuse old BUILD dir
1331 build_sequence_reuse()
1332 {
1333     local sourcerpm=$1
1334     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
1335     local dirsforreuse=
1336     if [ ! "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse old kernel build directory
1337         local dirsforreuse="$(get_reuse_dir_list)"
1338         local buildsuccess=false
1339         LINUXOBJ=
1340         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
1341         for curdir in $(echo $dirsforreuse); do
1342             local reusedkernelrpm=
1343             local reusedkernelsourcerpm=
1344             [ -d "$curdir" ] || continue
1345             local reusedkernelprefix="kernel-lustre-"
1346             ( $PATCHLESS ) && reusedkernelprefix=
1347             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
1348                 reusedkernelrpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
1349             reusedkernelprefix="kernel-lustre-source-"
1350             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm ] && \
1351                 reusedkernelsourcerpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm | head -1 ) 
1352             if ! ( $NORPM ) && ! [ -f "$reusedkernelrpm" ]; then #kernel rpm not found. Build all
1353                 continue
1354             fi
1355             if ! ( $NORPM ) && ! [ -f "$reusedkernelsourcerpm" ]; then #kernel source rpm not found. Build all
1356                 continue
1357             fi
1358             LINUX="$curdir"
1359             build_lustre || continue
1360             touch "$curdir/../"
1361             buildsuccess=true
1362             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
1363                 [ -f "$reusedkernelrpm" ] && \
1364                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
1365                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
1366                 [ -f "$reusedkernelsourcerpm" ] && \
1367                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
1368             fi
1369             return
1370         done
1371     fi
1372     return 255
1373 }
1374
1375
1376 build_sequence()
1377 {
1378     if (( $DO_SRC )) ; then
1379         unpack_linux
1380         patch_linux
1381         pack_linux
1382         clean_linux
1383     fi
1384     prep_kernel_build || return 255
1385     clean_lustre || return 255
1386
1387     build_kernel || return 255
1388 }
1389
1390 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
1391
1392 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: -- "$@")
1393
1394 if [ $? != 0 ] ; then
1395     usage 1
1396 fi
1397
1398 eval set -- "$options"
1399     
1400 while [ "$1" ] ; do
1401     case "$1" in
1402         '')
1403             usage 1
1404             ;;
1405         --ccache)
1406             CCACHE='ccache'
1407             shift
1408             ;;
1409         -d)
1410             CVSROOT=$2
1411             shift 2
1412             ;;
1413         -D)
1414             DATE=$2
1415             shift 2
1416             ;;
1417         --external-patches)
1418             EXTERNAL_PATCHES=$2
1419             shift 2
1420             ;;
1421         --extraversion)
1422             EXTRA_VERSION=$2
1423             shift 2
1424             ;;
1425         --help | -h)
1426             usage 0
1427             ;;
1428         --kerneldir)
1429             KERNELDIR=$2
1430             shift 2
1431             ;;
1432         --kerneltree)
1433             KERNELTREE=$2
1434             shift 2
1435             ;;
1436         --linux | --with-linux)
1437             LINUX=$2
1438             shift 2
1439             ;;
1440         --distro)
1441             DISTRO=$2
1442             shift 2
1443             ;;
1444         --reuserpm)
1445             REUSERPM=$2
1446             shift 2
1447             ;;
1448         --reusebuild)
1449             REUSEBUILD=$2
1450             shift 2
1451             ;;
1452         --norpm)
1453             NORPM=true
1454             shift
1455             ;;
1456         --ldiskfs)
1457             LDISKFSRPM=true
1458             shift
1459             ;;
1460         --patchless)
1461             PATCHLESS=true
1462             shift
1463             ;;
1464         --kernelrpm)
1465             KERNELRPMSBASE=$2
1466             shift 2
1467             ;;
1468         --timestamp)
1469             TIMESTAMP=$2
1470             shift 2
1471             ;;
1472         --lustre)
1473             LUSTRE=$2
1474             shift 2
1475             ;;
1476         --nodownload)
1477             DOWNLOAD=0
1478             shift 1
1479             ;;
1480         --nosrc)
1481             DO_SRC=0
1482             shift 1
1483             ;;
1484         --publish)
1485             shift
1486             ;;
1487         --release)
1488             RELEASE=true
1489             shift
1490             ;;
1491         --src)
1492             DO_SRC=1
1493             shift 1
1494             ;;
1495         --stage)
1496             STAGEDIR=$2
1497             shift 2
1498             ;;
1499         --tag)
1500             TAG=$2
1501             shift 2
1502             ;;
1503         --target)
1504             TARGET=$2
1505             shift 2
1506             ;;
1507         --target-archs)
1508             TARGET_ARCHS=$2
1509             shift 2
1510             ;;
1511         --disable-datestamp)
1512             USE_DATESTAMP=
1513             shift
1514             ;;
1515         --)
1516             shift
1517             CONFIGURE_FLAGS=$@
1518             CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-liblustre --enable-liblustre-tests"
1519             break
1520             ;; 
1521         *)
1522             usage 1 "Unrecognized option: $1"
1523             ;;
1524     esac
1525 done
1526
1527 check_options
1528
1529 unpack_lustre
1530
1531 load_target
1532 EXTRA_VERSION_DELIMITER=${EXTRA_VERSION_DELIMITER:-"-"}
1533
1534 build_success=false
1535 if $PATCHLESS; then
1536     patchless_build_sequence && build_success=true 
1537 elif [ -z "$LINUX" ] ; then
1538     [ "$DISTRO" = "sles9" ] && build_sequence_rpm_reuse && build_success=true
1539     if ! $build_success; then
1540         build_sequence_reuse && build_success=true
1541         if ! $build_success; then
1542             build_sequence && build_success=true
1543             if $build_success; then
1544                 store_for_reuse || echo "Cannot store for feature reuse"
1545             fi
1546         fi
1547     fi
1548 else
1549     build_lustre
1550 fi
1551 ( $build_success ) || fatal 1 "Cannot build lustre"
1552
1553 stage