Whamcloud - gitweb
LU-5641 tests: ensure user daemon is in group bin
[fs/lustre-release.git] / contrib / lbuild / lbuild
1 #!/bin/bash
2
3 # vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:
4
5 # this is an alternative FD for stdout, to be used especially when we are
6 # taking stdout from a function as it's return value.  i.e. foo=$(bar)
7 # this is a workaround until a version of bash where we can put xtrace
8 # on a specific FD
9 exec 3>&1; STDOUT=3
10
11 #set -x
12 xtrace="+x"
13 if [[ $SHELLOPTS = *xtrace* ]]; then
14     xtrace="-x"
15 fi
16 shopt -s extdebug
17
18 # Assume that lbuild's support files can be found in the same
19 # canonicalized path as this very script.
20 LBUILD_SCRIPT=$(readlink -f ${0})
21 LBUILD_DIR=${LBUILD_DIR:-$(dirname ${LBUILD_SCRIPT})}
22
23 # include the exit_traps library
24 . ${LBUILD_DIR}/exit_traps.sh
25 . ${LBUILD_DIR}/funcs.sh
26
27 # our children should die when we do
28 push_exit_trap "kill -INT -$$ || true" kill_children
29
30 # increment this if you have made a change that should force a new kernel
31 # to build built
32 #BUILD_GEN=1
33 #BUILD_GEN=2    # bz19952: remove -lustre tag from kernel RPM names
34 #BUILD_GEN=3    # bz19975: enable the building of src.rpms by default
35 #BUILD_GEN=4    # bz22281: use the git hash in the kernel extra version
36 #BUILD_GEN=5    # TT-107: don't cache the BUILD dir
37 BUILD_GEN=6     # TT-1092: don't cache the BUILD dir, to rebuild external OFED
38
39 TOPDIR=$PWD
40
41 KERNELDIR=
42 LINUX=
43 LUSTRE=
44 RELEASE=false
45 # XXX - some recent hacking has pretty much neutered this option.
46 #       search through this file for "-bb" and see how many places
47 #       simply don't account for this option
48 DO_SRC=true
49 DOWNLOAD=true
50 CANONICAL_TARGET=
51 TARGET=
52 TARGET_ARCH="$(uname -m)"
53 # change default behavior to only build for the current arch
54 TARGET_ARCHS="$TARGET_ARCH"
55 TARGET_ARCHS_ALL="$TARGET_ARCH"
56 [ "$TARGET_ARCH" = "i686" ] && TARGET_ARCHS_ALL="i686 i586 i386"
57 CONFIGURE_FLAGS=
58 EXTERNAL_PATCHES=
59 EXTRA_VERSION=
60 STAGEDIR=
61 TMPDIR=${TMPDIR:-"/var/tmp"}
62 TIMESTAMP=
63 # this is the dir that should be used to store reuse products
64 REUSEBUILD=
65 # should cached products be used or force rebuilding?
66 USE_BUILD_CACHE=true
67 # what does this do exactly?  does it imply no kernel build?
68 NORPM=false
69 IOKITRPM=true
70 OSDLDISKFSRPM=true
71 OSDZFSRPM=false
72 SMPTYPES="smp bigsmp default ''"
73 PATCHLESS=false
74 XEN=false
75 LINUXOBJ=
76 DISTRO=
77 KERNELTREE=
78 # default to not adding -lustre- into the kernel RPM package names
79 KERNEL_LUSTRE_NAMING=false
80 # default not use kabi check.
81 USE_KABI=false
82
83 # patchless build
84 KERNELRPMSBASE=
85 RPMSMPTYPE=
86
87 # from target file
88 SERIES=
89 BASE_ARCHS=
90 BIGMEM_ARCHS=
91 BOOT_ARCHS=
92 JENSEN_ARCHS=
93 SMP_ARCHS=
94 BIGSMP_ARCHS=
95 PSERIES64_ARCHS=
96 UP_ARCHS=
97
98 # not in the target file any more
99 CONFIG=
100
101 # build the lustre-tests rpm?
102 LUSTRE_TESTS=true
103
104 DATE=$(date)
105
106 RPMBUILD=
107
108 export CC=${CC:-gcc}
109
110 # Readlink is not present on some older distributions: emulate it.
111 readlink() {
112     local path=$1 ll
113
114     if [ -L "$path" ]; then
115         ll="$(LC_ALL=C ls -l "$path" 2> /dev/null)" &&
116         echo "${ll/* -> }"
117     else
118         return 1
119     fi
120 }
121
122 usage() {
123     cat <<EOF
124 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
125
126   --external-patches=EXTERNAL_PATCHES
127     Directory similar to lustre/lustre/kernel_patches/ that lbuild should
128     look for seres and config files in before looking in the lustre
129     tree.
130
131   --extraversion=EXTRAVERSION
132     Text to use for the rpm release and kernel extraversion.
133
134   --timestamp=TIMESTAMP
135     Date of building lustre in format YYYYMMDDhhmmss
136
137   --reusebuild=DIR
138     Try to reuse old kernel builds from DIR
139
140   --kernelrpm=DIR
141     Path to distro kernel RPM collection
142
143   --ccache
144     Use ccache
145
146   --norpm
147     Do not build RPMs (compile only mode)
148
149   --patchless
150     Build lustre client only
151
152   --distro=DISTRO
153     Which distro using. Autodetect by default
154
155   --kerneldir=KERNELDIR
156     Directory containing Linux source tarballs referenced by target
157     files.
158
159   --kerneltree=KERNELTREE
160     Directory containing dirs with Linux source tarballs referenced by target
161     files. Dir names in format kernel version ('2.6.9', etc.)
162
163   --linux=LINUX --with-linux=LINUX
164     Directory of Linux kernel sources.  When this option is used, only
165     Lustre modules and userspace are built.
166
167   --lustre=LUSTRE
168     Path to an existing lustre source tarball to use.
169
170   --nodownload
171     Do not try to download a kernel from downloads.hpdd.intel.com
172
173   --nosrc
174     Do not build a .src.rpm, a full kernel patch, or a patched kernel
175     tarball.
176
177   --ldiskfs
178     Build with ldiskfs support. (Deprecated, always true)
179
180   --noiokit
181     Do not build lustre-iokit RPM. Now true by default
182
183   --publish
184     Unused.
185
186   --release
187     Specifies that the files generated do not include timestamps, and
188     that this is an official release.
189
190   --src
191     Build a .src.rpm, a full kernel patch, and a patched kernel tarball.
192
193   --stage=DIR
194     Directory used to stage packages for release.  RPMs will be placed
195     more or less in DIR/<target>-<arch>, and the tarball will be
196     placed in DIR.
197
198   --target=TARGET
199     The name of the target to build.  The available targets are listed
200     below.
201
202   --target-archs=TARGET_ARCHS
203     A (space delimited) list of architectures to build.  By default,
204     all of the archs supported by the TARGET will be built, in
205     addition to a .src.rpm.  This option can limit those, for machines
206     that can only build certain archs or if you only want a certain
207     arch built (for testing, or a one-off kernel).
208
209     Also note that by using a non-"base" arch (eg, i386) only kernels
210     will be built - there will be no lustre-lite-utils package.
211
212   --xen
213     Builds a Xen domX kernel.
214
215   --set-value
216     Sets a variable to a given value.
217
218 EOF
219
220 #   list_targets
221
222     fatal "$1" "$2"
223 }
224
225 # canonicalize a relative path to a file
226 canon_filepath() {
227     local PATH="$1"
228
229     if [ ! -f "$PATH" ]; then
230         return 1
231     fi
232
233     local FILE=${PATH##*/}
234     local DIR=${PATH%/*}
235
236     echo $(canon_path "$DIR")/$FILE
237     return 0
238 }
239
240 # canonicalize a relative path to a dir
241 canon_path() {
242     local PATH="$1"
243
244     if [ ! -d "$PATH" ]; then
245         return 1
246     fi
247
248     pushd "$PATH" >/dev/null || return 1
249     local CANONPATH=$PWD
250     popd >/dev/null
251
252     echo "$CANONPATH"
253     return 0
254 }
255
256 check_options() {
257
258     if [ -z "$LUSTRE" -o ! -r "$LUSTRE" ]; then
259         usage 1 "Could not find Lustre source tarball '$LUSTRE'."
260     fi
261
262     if [ -z "$DISTRO" ] ; then
263         DISTRO=$(autodetect_distro)
264         # remove separator
265         DISTRO=${DISTRO/-/}
266     fi
267     DISTROMAJ=${DISTRO%%.*}
268
269     [ -z "$TARGET" ] && TARGET=$(autodetect_target "$DISTRO")
270
271     if [ -z "$LINUX" ]; then
272         [ "$KERNELDIR" -o "$KERNELTREE" ] || \
273             usage 1 "A kernel directory must be specified with --kerneldir or --kerneltree."
274
275         [ -d "$KERNELDIR" -o -d "$KERNELTREE" ] || \
276             usage 1 "$KERNELDIR and $KERNELTREE are not a directory."
277
278 #       TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target"
279 #       [ -r "$TARGET_FILE" ] || \
280 #               usage 1 "Target '$TARGET' was not found."
281     fi
282
283     case $TARGET in
284         3.10-rhel7)
285             CANONICAL_TARGET="rhel7"
286             ;;
287         2.6-rhel6)
288             CANONICAL_TARGET="rhel6"
289             ;;
290         2.6-rhel5)
291             CANONICAL_TARGET="rhel5"
292             ;;
293         2.6-rhel4)
294             CANONICAL_TARGET="rhel-2.6"
295             ;;
296         2.6-suse)
297             CANONICAL_TARGET="sles-2.6"
298             ;;
299         2.6-sles10)
300             CANONICAL_TARGET="sles10-2.6"
301             ;;
302         2.6-sles11 | 3.0-sles11)
303             CANONICAL_TARGET="sles11"
304             ;;
305         2.6-oel5)
306             CANONICAL_TARGET="oel5"
307             ;;
308         hp_pnnl-2.4)
309             CANONICAL_TARGET="hp-pnnl-2.4"
310             ;;
311         2.6-vanilla \
312             | suse-2.4.21-2 \
313             | rh-2.4 \
314             | rhel-2.4 \
315             | sles-2.4 \
316             | 2.6-patchless)
317                 CANONICAL_TARGET="$TARGET"
318                 ;;
319     esac
320
321     local timestampnodig=$(echo $TIMESTAMP | sed -e s/[0-9]*//g)
322     [ "$timestampnodig" = "" ] || TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M%S")
323     local timestamplength="${#TIMESTAMP}"
324     if [ $timestamplength -eq 12 ]; then
325         TIMESTAMP="${TIMESTAMP}00"
326     elif [ $timestamplength -ne 14 ]; then
327         TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M%S")
328     fi
329
330     RPMBUILD=$(which rpmbuild 2>/dev/null | head -1)
331     if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
332         RPMBUILD=$(which rpm 2>/dev/null | head -1)
333         if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
334             usage 1 "Could not find binary for making rpms (tried rpmbuild and rpm)."
335         fi
336     fi
337
338     if [ -n "$CCACHE" ]; then
339         which "$DISTCC" &>/dev/null && export DISTCC RPM_BUILD_NCPUS
340
341         if which "$CCACHE" &>/dev/null; then
342             local ccache=$(which "$CCACHE")
343             local bindir="$TOPDIR/bin"
344
345             if [ ! -d $bindir ]; then
346                 mkdir -p $bindir || fatal 1 "error trying to create $bindir"
347             else
348                 rm ${bindir}/* > /dev/null 2>&1 || true
349             fi
350             ln -s "$ccache" ${bindir}/ccache
351             ln -s "$ccache" ${bindir}/cc
352             ln -s "$ccache" ${bindir}/$CC
353             export PATH=$bindir:$PATH
354             export CCACHE && export CC="ccache $CC"
355             # zero the cache so we can see how effective we are being with it
356             echo -n "ccache "
357             ccache -z
358
359             # get some ccache stats when we are done
360             push_exit_trap '[ -n "$CCACHE" ] && ccache -s' "ccache_summary"
361             # should remove the ccache trap if lbuild is interrupted
362             trap 'echo "Received an INT TERM or HUP signal, terminating."; delete_exit_trap "ccache_summary"; exit 1' INT TERM HUP
363         fi
364     fi
365
366     return 0
367
368 }
369
370 # compare two versions $1 and $2. if $1 < $2, return 0 otherwise return 1.
371 compare_version () {
372     [[ $1 == $2 ]] && return 1
373     local IFS=.
374     local i val1=($1) val2=($2)
375
376     # padding zero to val1 if it needs
377     for ((i=${#val1[@]}; i<${#val2[@]}; i++)); do
378         val1[i]=0
379     done
380     for ((i=0; i<${#val1[@]}; i++)); do
381         [[ -z ${val2[i]} ]] && return 1
382
383         if [[ ${val1[i]} < ${val2[i]} ]]; then
384             return 0
385         elif [[ ${val1[i]} > ${val2[i]} ]]; then
386             return 1
387         fi
388     done
389 }
390
391 uniqify() {
392
393     echo $(echo "$*" | xargs -n 1 | sort -u)
394
395 }
396
397 fetch_url() {
398     local url="$1"
399     local target="$2"
400
401     if [ -z "$target" ]; then
402         fatal 1 "fetch_url() called without a target to fetch to"
403     fi
404
405     if [ -d $target ]; then
406         target+="/${url##*/}"
407     fi
408
409     local rc=0
410     if which wget >/dev/null 2>&1; then
411         if ! wget -nv "$url" -O "$target"; then
412             rc=${PIPESTATUS[0]}
413         fi
414     elif which curl >/dev/null 2>&1; then
415         if ! curl -n -L -s -o "$target" "$url"; then
416             rc=${PIPESTATUS[0]}
417         fi
418     else
419         fatal 1 "Could not find either wget or curl to fetch URLs."
420     fi
421
422     return $rc
423
424 }
425
426 download_srpm() {
427     local target=$1
428     local srpm=$2
429     local force="${3:-false}"
430
431     if $force || [ ! -r "$KERNELDIR/$srpm" ] ||
432        [ ! -s "$KERNELDIR/$srpm" ]; then
433         if $DOWNLOAD; then
434             local location="https://downloads.hpdd.intel.com/public/kernels/$target/old"
435             # get the location from a distro specific method if it exists
436             if type -p kernel_srpm_location; then
437                 location=$(kernel_srpm_location)
438             fi
439             echo "Downloading $location/$srpm..."
440             if ! fetch_url "$location/$srpm" "$KERNELDIR/$srpm" 2>&1 ||
441                [ ! -s "$KERNELDIR/$srpm" ]; then
442                 rm -f $KERNELDIR/$srpm
443                 # punt to a distro specific method if it exists
444                 if ! type -p download_srpm-$DISTROMAJ; then
445                     fatal 1 "Could not download target $target's kernel SRPM $srpm from $location."
446                 else
447                     if ! download_srpm-$DISTROMAJ "$target" "$srpm" "$force"; then
448                         fatal 1 "Could not download target $target's kernel SRPM $srpm using download_srpm-$DISTROMAJ."
449                     fi
450                 fi
451             fi
452         else
453             fatal 1 "$srpm not found in directory $KERNELDIR."
454         fi
455     fi
456
457 }
458
459 download_file() {
460     local from="$1"
461     local to="$2"
462     local force="$3"
463
464     local file=${from##*/}
465
466     if [ -d $to ]; then
467         to="$to/$file"
468     fi
469
470     local semaphore="$to-downloading"
471
472     is_downloading() {
473         if [ ! -f $semaphore ]; then
474             return 1
475         fi
476
477         # make sure the download has not been aborted
478         local now=$(date +%s)
479         local file_mtime=$(stat -c %Y "$to")
480         local staleness=$((now - file_mtime))
481         # let's assume an active download will write at least once a minute
482         if [ $staleness -gt 60 ]; then
483             return 1
484         fi
485
486         return 0
487     }
488
489     is_downloaded() {
490         # if the semaphore file exists, the file is either still downloading
491         # or a download was aborted and we cannot trust the target file
492         if [ -f $semaphore ]; then
493             return 1
494         fi
495
496         if ! is_downloading && [ -r "$to" ] && [ -s "$to" ]; then
497             return 0
498         fi
499
500         return 1
501     }
502
503     if $force || ! is_downloaded; then
504         if is_downloading; then
505             echo "Somebody else is downloading $from..."
506             while is_downloading; do
507                 echo "Waiting for $to to finish downloading"
508                 sleep 60
509             done
510             if is_downloaded; then
511                 return 0
512             else
513                 echo "The download we were waiting for seems to have been aborted"
514             fi
515
516         fi
517
518         if $DOWNLOAD; then
519             echo "Downloading $from..."
520             # flag others so they don't try to download also
521             push_exit_trap "rm -f $to $semaphore" "download"
522             touch $semaphore
523             if ! fetch_url "$from" "$to" || [ ! -s "$to" ]; then
524                 # the trap will remove the files via the fatal below
525                 fatal 1 "Could not download ${to##*/} from ${from%/*}/."
526             fi
527             rm -f $semaphore
528             delete_exit_trap "download"
529         else
530             fatal 1 "${to##*/} not found in directory ${to%/*}."
531         fi
532     fi
533
534     return 0
535
536 }
537
538 download_ofed() {
539     local force="${1:-false}"
540
541     if [ -z "$OFED_VERSION" -o "$OFED_VERSION" = "inkernel" ]; then
542         return 0
543     fi
544
545     local OFED_BASE_VERSION=$OFED_VERSION
546     if [[ $OFED_VERSION = *.*.*.* ]]; then
547         OFED_BASE_VERSION=${OFED_VERSION%.*}
548     fi
549
550     local location="https://www.openfabrics.org/downloads/OFED/ofed-${OFED_BASE_VERSION}/"
551
552     if [[ $OFED_VERSION = *-[rR][cC][0-9] ]]; then
553         local Mmv
554         Mmv=${OFED_VERSION%%-[rR][cC][0-9]}
555         location="https://www.openfabrics.org/downloads/OFED/ofed-${Mmv}/"
556     fi
557
558     if [[ $OFED_VERSION = daily-* ]]; then
559         local Mmv
560         Mmv=${OFED_VERSION/daily-/}
561         daily=${OFED_VERSION##$Mmv-}
562         location="https://www.openfabrics.org/downloads/OFED/ofed-${Mmv}-daily/"
563         # find the filename for the version for the date specified
564         OFED_VERSION=$(curl -s "$location" | sed -nre "/${Mmv}-/s/.*href=\"OFED-(${Mmv}-[0-9]{8,8}-[0-9]{4,4}).tgz.*$/\1/p" | tail -1)
565         if [ -z "$OFED_VERSION" ]; then
566             fatal 1 "Could not determine the filename of the OFED snapshot for ${daily}"
567         fi
568     fi
569
570     local file="OFED-${OFED_VERSION}.tgz"
571     download_file "$location/$file" "$KERNELTREE" "$force"
572
573 }
574
575 parse_mpss_info() {
576     local file="$1"
577     local build="$2"
578     local distro="$3"
579
580     [ -r "$file" ] || fatal 1 "Could not find MPSS info file"
581
582     # convert to MPSS distro naming
583     distro=${distro/sles/suse}
584     distro_name=${distro%%-*}
585
586     # extract links from MPSS info file
587     local urls=$(cat "$file" | \
588         tr -d '\t\r\n' | sed -e 's?\(</[a-zA-Z]*>\)?\1\n?g' | \
589         grep '.intel.com/' | \
590         sed -e 's/^.*[hH][rR][eE][fF]="\([^>"]\+\)".*$/\1/g' | \
591         grep '.tar')
592     local res2=""
593     local res3=""
594     for url in $urls; do
595         local ver2=""
596         local ver3=""
597         # Try to match with MPSS 3.3 package
598         if [[ $url =~ mpss-[0-9].[0-9](.[0-9]*)?-linux.tar ]]; then
599             ver3=${url##*mpss-}
600             ver3=${ver3%%-linux.tar}
601         # Try to match with MPSS 3.x or 3.x.x distro package
602         elif [[ $url =~ mpss-[0-9].[0-9](.[0-9]*)?-$distro.tar ]]; then
603             ver3=${url##*mpss-}
604             ver3=${ver3%%-$distro.tar}
605         # Try to match with MPSS 3.x or 3.x.x source package
606         elif [[ $url =~ mpss-src-[0-9].[0-9](.[0-9]*)?.tar ]]; then
607             ver3=${url##*mpss-src-}
608             ver3=${ver3%%.tar}
609         # Try to match with MPSS 3.x or 3.x.x cross compiler package
610         elif [[ $url =~ mpss-[0-9].[0-9](.[0-9]*)?-k1om.tar ]]; then
611             ver3=${url##*mpss-}
612             ver3=${ver3%%-k1om.tar}
613         # Try to match with MPSS 2.1.x source package
614         elif [[ $url =~ _src-[0-9].[0-9].[0-9]*-[0-9]*_$distro_name.tar ]]; then
615             ver2=${url##*_src-}
616             ver2=${ver2%%_$distro_name.tar}
617         fi
618
619         if [ -n "$ver3" ]; then
620             ver3="${ver3}-0"
621             if [ -z "$build" ]; then
622                 res3="$ver3"
623             elif [ "${build%%-*}" = "${ver3%%-*}" ]; then
624                 res3="$res3 $url"
625             fi
626         elif [ -n "$ver2" ]; then
627             if [ -z "$build" ]; then
628                 res2="$ver2"
629             elif [ "$build" = "$ver2" ]; then
630                 res2="$res2 $url"
631             fi
632         fi
633
634     done
635
636     if [ -z "$build" ]; then
637         # return a last version of MPSS
638         if [ -n "$res3" ]; then
639             echo "$res3"
640         else
641             echo "$res2"
642         fi
643     elif [ "${build%%.*}" = "3" ]; then
644         # return URLs from MPSS info file
645         echo "$res3"
646     else
647         # return URLs from MPSS info file
648         echo "$res2"
649     fi
650 }
651
652 # Get public information about last releases of
653 # Intel Manycore Platform Software Stack (MPSS)
654 download_mpss_info() {
655     local file="$1"
656     local force="${2:-true}"
657     local url=${MPSS_URL:-"http://software.intel.com/en-us/articles/intel-manycore-platform-software-stack-mpss"}
658
659     download_file "$url" "$file" "$force"
660 }
661
662 download_mpss() {
663     local file="$1"
664     local force="${2:-false}"
665     local urls=$(parse_mpss_info "$file" ${MPSS_VERSION} ${MPSS_DISTRO})
666
667     [ -z "$urls" ] && return 0
668
669     local url
670     local i=0
671     for url in $urls; do
672         file="$KERNELTREE/mpss_src-${MPSS_VER}-${MPSS_BLD}-${MPSS_DISTRO}-part$i.tar"
673         download_file "$url" "$file" "$force"
674         i=$((i+1))
675     done
676 }
677
678 load_target() {
679
680     EXTRA_VERSION_save="$EXTRA_VERSION"
681     for patchesdir in "$EXTERNAL_PATCHES" \
682                       "$TOPDIR/lustre/lustre/kernel_patches"; do
683         TARGET_FILE="$patchesdir/targets/$TARGET.target"
684         [ -r "$TARGET_FILE" ] && break
685     done
686     [ -r "$TARGET_FILE" ] || fatal 1 "Target $TARGET was not found."
687
688     echo "Loading target config file $TARGET.target..."
689
690     # if the caller specified an OFED_VERSION it should override whatever
691     # the target file specifies
692     local env_OFED_VERSION="$OFED_VERSION"
693
694     . "$TARGET_FILE"
695
696     if [ -n "$env_OFED_VERSION" ]; then
697         OFED_VERSION="$env_OFED_VERSION"
698     fi
699
700     # doesn't make any sense to build OFED for xen domX's
701     if $XEN; then
702         OFED_VERSION=""
703     fi
704
705     # XXX - set_rpm_smp_type is an ugly undeterministic hack.  it needs to
706     #       go away and the target just specify the $RPMSMPTYPE
707     [ -z "$RPMSMPTYPE" ] && set_rpm_smp_type
708
709     # CC might have been overwriten in TARGET_FILE
710     if [[ $CC != ccache\ * ]] && which "$CCACHE" &>/dev/null; then
711         export CCACHE && export CC="ccache $CC"
712     fi
713
714     if [ ! "$KERNELTREE" = "" ] && [ -d "$KERNELTREE" ]; then
715         KERNELDIR="$KERNELTREE/${lnxmaj}"
716         [ -d "$KERNELDIR" ] || mkdir "$KERNELDIR"
717     fi
718
719     # verify the series is available
720     if [ "$SERIES" ]; then
721         for series in $SERIES; do
722             for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches"; do
723                 [ -r "$patchesdir/series/$series" ] && continue 2
724             done
725             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."
726         done
727     fi
728
729     # set the location of the .config file
730     local XENPOSTFIX=""
731     if $XEN; then
732         XENPOSTFIX="-xen"
733     fi
734
735     if [ -f $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET-$TARGET_ARCH.config ]; then
736         CONFIG_FILE="$TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET$XENPOSTFIX-$TARGET_ARCH.config"
737     fi
738
739     local lnxrelnew=${lnxrel//-/_}
740
741     # remember the EXTRA_VERSION before we diddle it here
742     # XXX - we really should not diddle with any values read in from the
743     #       target file.  if we want to modify a value, we should create
744     #       a new variable.
745     PRISTINE_EXTRA_VERSION=$EXTRA_VERSION
746
747     if ! $PATCHLESS && [ ! -f "$CONFIG_FILE" ]; then
748         fatal 1 "Config file for target $TARGET missing from $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/."
749     fi
750
751     if [ "$EXTRA_VERSION_save" ]; then
752         EXTRA_VERSION="$EXTRA_VERSION_save"
753     elif ! $RELEASE; then
754         # if there is no patch series, then this is not a lustre specific
755         # kernel.  don't make it look like one
756         if $PATCHLESS || [ -n "$SERIES" ]; then
757             EXTRA_VERSION=$(echo $EXTRA_VERSION | sed -e "s/\(.*_lustre\)\..*/\1/")
758             if ! $PATCHLESS && [ -n "$BUILDID" ]; then
759                 EXTRA_VERSION="${EXTRA_VERSION}.${BUILDID}"
760             fi
761         fi
762     fi
763     # EXTRA_VERSION=${EXTRA_VERSION//-/_}
764
765     ALL_ARCHS="$BASE_ARCHS $BIGMEM_ARCHS $BOOT_ARCHS $JENSEN_ARCHS $SMP_ARCHS $BIGSMP_ARCHS $PSERIES64_ARCHS $UP_ARCHS"
766
767     BUILD_ARCHS=
768     for arch in $(uniqify "$ALL_ARCHS"); do
769         if [ -z "$TARGET_ARCHS" ] ||
770            [[ \ $TARGET_ARCHS\  = *\ $arch\ * ]]; then
771             BUILD_ARCHS="$BUILD_ARCHS $arch"
772         fi
773     done
774     [ "$BUILD_ARCHS" ] || usage 1 "No available target archs to build."
775     echo "Building for: $BUILD_ARCHS"
776 }
777
778 tarflags() {
779     local file="$1"
780
781     echo -n '--wildcards '
782     case "$file" in
783         '')
784             fatal 1 "tarflags(): File name argument missing."
785             ;;
786         *.tar.gz | *.tgz)
787             echo '-zxf'
788             ;;
789         *.tar.bz2)
790             echo '-jxf'
791             ;;
792         *.tar)
793             echo '-xf'
794             ;;
795         *)
796             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
797             ;;
798     esac
799
800 }
801
802 untar() {
803     local tarfile="$1"
804     shift
805     local extractfile="$@"
806
807     echo "Untarring ${tarfile##*/}..."
808     tar $(tarflags "$tarfile") "$tarfile" $extractfile
809
810 }
811
812 unpack_ofed() {
813
814     if ! untar "$KERNELTREE/OFED-${OFED_VERSION}.tgz"; then
815         return 1
816     fi
817     [ -d OFED ] || ln -sf OFED-[0-9].[0-9]* OFED
818
819 }
820
821 unpack_lustre() {
822
823     untar "$LUSTRE" || fatal 1 "Error unpacking Lustre tarball"
824     [ -d lustre ] || ln -sf lustre-[0-9].[0-9]* lustre
825
826 }
827
828 unpack_mpss() {
829
830     [ -d mpss ] && return 0
831
832     if [ "$MPSS_MAJ" = "3" ]; then
833         local file
834         local i
835         for i in $(seq 0 9); do
836             file="$KERNELTREE/mpss_src-${MPSS_VER}-${MPSS_BLD}-${MPSS_DISTRO}-part$i.tar"
837             if [ -r "$file" ]; then
838                 untar "$file" \
839                     "mpss-$MPSS_VER/*/linux-*.tar.bz2" \
840                     "mpss-$MPSS_VER/*/kernel-dev-*.rpm" \
841                     "mpss-$MPSS_VER/*/ofed-driver-devel-*.rpm" \
842                     "mpss-$MPSS_VER/*/ofed-driver-*-devel-*.rpm"
843             fi
844         done
845         # Extract kernel configs
846         file=$(find mpss-$MPSS_VER -type f -path "*/kernel-dev-*.rpm" -print -quit)
847         if ! rpm2cpio "$file" | cpio -idm; then
848             echo "Error extracting MPSS kernel configs"
849             return 1
850         fi
851         # Unpack kernel sources
852         file=$(find mpss-$MPSS_VER -type f -path "*/linux-*.tar.bz2" -print -quit)
853         if ! untar "$file"; then
854             echo "Error unpacking MPSS kernel sources"
855             return 1
856         fi
857         # Extract OFED headers
858         file=$(find mpss-$MPSS_VER -type f -path "*/ofed-driver-*-devel-*.rpm" -print -quit)
859         if ! rpm2cpio "$file" | cpio -idm; then
860             # Name was changed in MPSS 3.3
861             file=$(find mpss-$MPSS_VER -type f -path "*/ofed-driver-devel-*.rpm" -print -quit)
862             if ! rpm2cpio "$file" | cpio -idm; then
863                 echo "Error extracting MPSS OFED headers"
864                 return 1
865             fi
866         fi
867         # Remove unpacked archives to save space
868         rm -rf mpss-$MPSS_VER
869         # Make link to MPSS kernel sources
870         ln -sf linux-* mpss
871     else
872         local dir="mpss-${MPSS_VER}-${MPSS_BLD}-${MPSS_DISTRO%%-*}"
873
874         mkdir $dir || return 255
875         pushd $dir >/dev/null || return 255
876         if ! untar "$KERNELTREE/mpss_src-${MPSS_VER}-${MPSS_BLD}-${MPSS_DISTRO}-part0.tar" "*/gpl/*full_src*"; then
877             popd >/dev/null
878             rm -rf $dir
879             echo "Error unpacking MPSS tarball 1"
880             return 1
881         fi
882         local file=$(find . -type f -path "*/gpl/*full_src*")
883         if ! untar "$file"; then
884             popd >/dev/null
885             rm -rf $dir
886             echo "Error unpacking MPSS tarball 2"
887             return 1
888         fi
889         popd >/dev/null
890         ln -sf $dir mpss
891     fi
892
893 }
894
895 do_patch_linux() {
896
897     local do_patch=${1:-true}
898
899     FULL_PATCH="$PWD/lustre-kernel-${TARGET}-${EXTRA_VERSION}.patch"
900     [ -f "$FULL_PATCH" ] && rm -f "$FULL_PATCH"
901     $do_patch && pushd linux >/dev/null
902     for series in $SERIES; do
903         echo -n "Applying series $series:"
904         for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches"; do
905             [ -r "$patchesdir/series/$series" ] || continue
906             SERIES_FILE="$patchesdir/series/$series"
907             for patch in $(<"$SERIES_FILE"); do
908                 echo -n " $patch"
909                 PATCH_FILE="$patchesdir/patches/$patch"
910                 [ -r "$PATCH_FILE" ] || \
911                     fatal 1 "Patch $patch does not exist in Lustre tree."
912                 cat "$PATCH_FILE" >> "$FULL_PATCH" || {
913                     rm -f $FULL_PATCH
914                     fatal 1 "Error adding patch $patch to full patch."
915                 }
916                 if $do_patch; then
917                     patch -s -p1 < "$PATCH_FILE" 2>&1 || {
918                         rm -f $FULL_PATCH
919                         fatal 1 "Error applying patch $patch."
920                     }
921                 fi
922             done
923             break
924         done
925         echo
926     done
927     $do_patch && popd >/dev/null
928     echo "Full patch has been saved in ${FULL_PATCH##*/}."
929
930 }
931
932 build_lustre() {
933     local linux="$1"
934     local linuxobj="$2"
935
936     cp "$LUSTRE" SOURCES
937
938     pushd lustre >/dev/null
939
940     if ! build_lustre_dkms; then
941         popd >/dev/null # pushd lustre
942         return 255
943     fi
944
945     echo "Building Lustre RPMs for: $BUILD_ARCHS..."
946     local targets arch
947     for arch in $BUILD_ARCHS; do
948         targets="--target $arch $targets"
949     done
950
951     local confoptions=""
952
953     if $PATCHLESS; then
954         confoptions="$confoptions --disable-server"
955     fi
956
957     local rpmbuildopt='-tb'
958     if $NORPM; then
959         rpmbuildopt='-tc'
960         echo NORPM mode. Only compiling.
961     fi
962
963     # If server we now build the spl and zfs modules against the lustre kernel.
964     # These are required prior to the building of lustre server. Client does
965     # not require spl/zfs. Use !PATCHLESS to indicate server which follows the
966     # line above so is at least consistant.
967     if [ $PATCHLESS == false ] && [ "x$ZFSNOTSUPPORTED" == "x" ]; then
968         if ! build_spl_zfs; then
969             popd >/dev/null # pushd lustre
970             return 255
971         fi
972     fi
973
974     if $PATCHLESS; then
975         RPMBUILD_DEFS="$RPMBUILD_DEFS --without servers"
976         if [ -n "$CROSS_SUFFIX" ]; then
977             RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"lustre_name lustre-client$CROSS_SUFFIX\""
978         fi
979     fi
980
981     # ditto for the lustre-tests boolean
982     if ! $LUSTRE_TESTS; then
983         RPMBUILD_DEFS="$RPMBUILD_DEFS --without lustre_tests"
984     fi
985
986     if ! $IOKITRPM; then
987         RPMBUILD_DEFS="$RPMBUILD_DEFS --without lustre_iokit"
988     fi
989
990     local osd_zfs=""
991     if $OSDZFSRPM; then
992         osd_zfs="yes"
993     fi
994
995     local osd_ldiskfs=""
996     if $OSDLDISKFSRPM; then
997         osd_ldiskfs="yes"
998     fi
999
1000     RPMBUILD_DEFS="$RPMBUILD_DEFS ${FIND_REQUIRES:+--define \"__find_requires $FIND_REQUIRES\"}"
1001     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"configure_args $confoptions ${CONFIGURE_FLAGS}\""
1002     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"kdir $linux\""
1003     RPMBUILD_DEFS="$RPMBUILD_DEFS ${linuxobj:+--define \"kobjdir $linuxobj\"}"
1004     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"_tmppath $TMPDIR\""
1005     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"_topdir $TOPDIR\""
1006
1007     eval $RPMBUILD $targets $rpmbuildopt "$LUSTRE" $RPMBUILD_DEFS \
1008         ${osd_zfs:+--with zfs} \
1009         ${osd_ldiskfs:+--with ldiskfs} 2>&1 || \
1010         fatal 1 "Error building rpms for $BUILD_ARCHS."
1011
1012     if $DO_SRC; then
1013         if ! eval $RPMBUILD -ts "$LUSTRE" $RPMBUILD_DEFS 2>&1; then
1014             popd >/dev/null
1015             return 255
1016         fi
1017     fi
1018     popd >/dev/null
1019
1020 }
1021
1022 build_lustre_dkms() {
1023     [ -n "$MPSS_VERSION" ] && return 0
1024     local ver=$(eval echo  $(awk '/LUSTRE_VERSION_STRING/ {print $3}'  lustre/include/lustre_ver.h))
1025     echo "Building Lustre DKMS RPMs for: $BUILD_ARCHS..."
1026     ./configure --enable-dist || return 255
1027     $RPMBUILD --define="_topdir $TOPDIR" -bs lustre-dkms.spec || return 255
1028     $RPMBUILD --rebuild --define="_topdir $TOPDIR" $TOPDIR/SRPMS/lustre-dkms-$ver-*.src.rpm || return 255
1029     return 0
1030 }
1031
1032 ###
1033 # build_spl_zfs
1034 #
1035 # Fetch spl/zfs from the git repo and prepare for lustre build
1036 #
1037 # Overrides:
1038 #   SPLZFSGITREPO - URI of directory where spl.git and zfs.git are located
1039 #   SPLZFSTAG     - Tag to checkout of clone repositories
1040 #   SPLZFSVER     - Version to checkout of both (format zfs/spl-$SPLZFSVER)
1041 #
1042 # return 0 if successful, else 255
1043 build_spl_zfs() {
1044     # make sure the RPM build environment is set up
1045     pushd $TOPDIR
1046     create_rpmbuild_dirs
1047     popd
1048
1049     # The spl/zfs spec files expect RPM_BUILD_ROOT to point to the root of the
1050     # destination for the rpms
1051     export RPM_BUILD_ROOT=$TOPDIR
1052     SPLZFSVER=${SPLZFSVER:-0.6.3}
1053     SPLZFSTAG=${SPLZFSTAG:-}
1054
1055     # The files expect a kver to be set to the kernel version .
1056     local kver=$(find_linux_release)
1057
1058     # build and install the spl and zfs (and -devel) RPMs for lustre to use
1059     local pkg
1060     for pkg in spl zfs; do
1061
1062         local rpmpkg
1063
1064         [ "$pkg" == "zfs" ] && spldir="$(ls -d $TOPDIR/usr/src/spl-*/|tail -1)"
1065
1066         # need to fetch the repo in order to build it.
1067         # default to github but allow override
1068         git clone ${SPLZFSGITREPO:-"https://github.com/zfsonlinux"}/$pkg.git $pkg 2>&1
1069
1070         pushd $pkg || return 255
1071         if [ -n "$SPLZFSTAG" ]; then
1072             git checkout $SPLZFSTAG
1073         else
1074             git checkout -b lbuild $pkg-$SPLZFSVER
1075         fi
1076
1077         # This differentiates between older zfs versions
1078         if [ -f $pkg-modules.spec.in ]; then
1079             rpmpkg=$pkg-modules
1080             specdir=.
1081             speclist="$pkg.spec $rpmpkg.spec"
1082         else
1083             rpmpkg=kmod-$pkg-devel
1084             specdir=rpm/generic
1085             speclist="$pkg.spec $pkg-kmod.spec $pkg-dkms.spec"
1086         fi
1087
1088         sh autogen.sh || return 255
1089
1090         if  ! ./configure --with-linux=${LINUX} --with-linux-obj=${LINUXOBJ:-$LINUX} \
1091                           ${spldir:+--with-spl="${spldir}"} 2>&1 ||
1092             ! make dist 2>&1; then
1093             popd
1094             return 255
1095         fi
1096         popd
1097
1098         ln -f $pkg/$pkg-*.tar.gz $TOPDIR/SOURCES ||
1099            error "failed to link $pkg/$pkg-*.tar.gz into $TOPDIR/SOURCES"
1100         if [ -f $pkg/scripts/kmodtool ]; then
1101             ln -f $pkg/scripts/kmodtool $TOPDIR/SOURCES/
1102         fi
1103
1104         local rpmb
1105         if $DO_SRC; then
1106             rpmb=-ba
1107         else
1108             rpmb=-bb
1109         fi
1110
1111         # set search dir for our own kmodtool to find correct
1112         # directories
1113         export KERNELSOURCE=$(dirname ${LINUX})
1114         # Manually build rpms
1115         for spec in $speclist; do
1116             echo "Building RPMs from $pkg/$specdir/$spec"
1117             if ! $RPMBUILD $rpmb $pkg/$specdir/$spec \
1118                 --nodeps -v \
1119                 --define "require_kdir ${LINUX}" \
1120                 ${LINUXOBJ:+--define "require_kobj ${LINUXOBJ}"} \
1121                 ${spldir:+--define "require_spldir ${spldir}"} \
1122                 --define "kver $kver" \
1123                 --define "kernels $kver" \
1124                 --define "_tmppath /var/tmp" \
1125                 --define "kernelbuildroot $TOPDIR/reused" \
1126                 --define "_topdir $TOPDIR" 2>&1; then
1127                 return 255
1128             fi
1129         done
1130
1131         # We have built the rpms for the package. Now we need to extract the
1132         # contained files so we can build further things against them
1133         local rpms=$(ls -1 $TOPDIR/RPMS/*/$rpmpkg-*.rpm)
1134
1135         # cpio only extract to pwd so we need to go there.
1136         pushd $TOPDIR
1137         local rpm
1138         for rpm in $rpms; do
1139             rpm2cpio $rpm | cpio -id
1140         done
1141
1142         if [ "$pkg" == "zfs" ]; then
1143             # We also need to extract both the zfs and zfs-devel rpms
1144             # the zfs rpm is needed because it has the actual libraries in
1145             # it and the zfs-devel rpm only has unversioned symlinks to the
1146             # libraries in the zfs rpm
1147             # this will all change one day when we have a libzfs rpm per
1148             # https://github.com/zfsonlinux/zfs/issues/2329
1149             # and it looks like it could be one day soon:
1150             # https://github.com/zfsonlinux/zfs/pull/2341
1151             local devel_rpms=$(ls -1 $TOPDIR/RPMS/*/{$pkg-devel,$pkg-$SPLZFSVER,lib*}-*.rpm)
1152             for rpm in $devel_rpms; do
1153                 rpm2cpio $rpm | cpio -id
1154             done
1155             CONFIGURE_FLAGS="--with-$pkg-devel=$TOPDIR ${CONFIGURE_FLAGS}"
1156         fi
1157         popd
1158
1159         CONFIGURE_FLAGS="--with-$pkg=$(ls -d $TOPDIR/usr/src/$pkg-*/|tail -1) ${CONFIGURE_FLAGS}"
1160         CONFIGURE_FLAGS="--with-$pkg-obj=$(ls -d $TOPDIR/usr/src/$pkg-*/$kver*|tail -1) ${CONFIGURE_FLAGS}"
1161     done
1162
1163     OSDZFSRPM=true
1164
1165     return 0
1166 }
1167
1168 prepare_mpss() {
1169
1170     pushd mpss >/dev/null || return 255
1171     if [ "$MPSS_MAJ" = "3" ]; then
1172         cp -f ../boot/config-* .config
1173         if ! make ARCH=k1om silentoldconfig ; then
1174             popd >/dev/null
1175             return 1
1176         fi
1177         if ! make ARCH=k1om modules_prepare ; then
1178             popd >/dev/null
1179             return 1
1180         fi
1181         cp -f ../boot/Module.symvers-* Module.symvers
1182     else
1183         if ! make defconfig-miclinux; then
1184             popd >/dev/null
1185             return 1
1186         fi
1187         if ! make -C card/kernel ARCH=k1om modules_prepare ; then
1188             popd >/dev/null
1189             return 1
1190         fi
1191     fi
1192     popd >/dev/null
1193
1194 }
1195
1196 stage() {
1197
1198     [ "$STAGEDIR" ] || return 0
1199
1200     for arch in $BUILD_ARCHS; do
1201         rpmdir="${STAGEDIR}/${CANONICAL_TARGET}-${arch}"
1202         echo "${0##*/}: Copying RPMs into ${rpmdir}"
1203         mkdir -p "${rpmdir}"
1204         cp -v RPMS/${arch}/*.rpm "${rpmdir}"
1205         if [ -d RPMS/noarch ]; then
1206             cp -v RPMS/noarch/*.rpm "${rpmdir}"
1207         fi
1208     done
1209
1210     cp -v "$LUSTRE" "$STAGEDIR"
1211
1212 }
1213
1214 set_rpm_smp_type() {
1215
1216     local infact_arch="${TARGET_ARCH}"
1217
1218     RPMSMPTYPE=""
1219     [ "$infact_arch" == "i586" ] && infact_arch="i686"
1220
1221     local smp_type
1222     for smp_type in $SMP_ARCHS; do
1223         [ $infact_arch == $smp_type ] && RPMSMPTYPE=smp && break
1224     done
1225
1226     for smp_type in $BIGSMP_ARCHS; do
1227         [ $infact_arch == $smp_type ] && RPMSMPTYPE=bigsmp && break
1228     done
1229
1230     for smp_type in $PPC64_ARCHS; do
1231         [ $infact_arch == $smp_type ] && RPMSMPTYPE=ppc64 && break
1232     done
1233
1234     for smp_type in $DEFAULT_ARCHS; do
1235         [ $infact_arch == $smp_type ] && RPMSMPTYPE=default && break
1236     done
1237
1238 }
1239
1240 # This function takes a linux include tree and digs out the linux release
1241 # from it. It is never called directly, only called from the distro
1242 # specific function find_linux_release() in lbuild-{rhel,sles}.
1243 _find_linux_release() {
1244     local SRC="$1"
1245     local LINUXRELEASEHEADER=""
1246
1247     LINUXRELEASEHEADER=$SRC/include/linux/version.h
1248     if [ -s $SRC/include/generated/utsrelease.h ]; then
1249         LINUXRELEASEHEADER=$SRC/include/generated/utsrelease.h
1250     elif [ -s $SRC/include/linux/utsrelease.h ]; then
1251         LINUXRELEASEHEADER=$SRC/include/linux/utsrelease.h
1252     fi
1253
1254     if [ ! -s $LINUXRELEASEHEADER ]; then
1255         fatal 1 "could not find UTS_RELEASE"
1256     fi
1257
1258     sed -ne 's/#define UTS_RELEASE "\(.*\)"$/\1/p' $LINUXRELEASEHEADER
1259 }
1260
1261 # unpack kernel(/source/devel) RPM
1262 #
1263 # This function and it's setting of $LINUX and $LINUXOBJ is a total hack that
1264 # needs to completely refactored.  It completely ingores that $BUILD_ARCHS may
1265 # contain a list of arches for which rpmbuild commands (including the one for
1266 # lustre itself)
1267 unpack_linux_devel_rpm() {
1268     local kernelrpm="${1}"
1269
1270     [ -f "$kernelrpm" ] || return 255
1271     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused || return 255
1272
1273     pushd $TOPDIR/reused &>/dev/null || return 255
1274
1275     if ! rpm2cpio < "$kernelrpm" | cpio -id > /dev/null 2>&1; then
1276         return 255
1277     fi
1278
1279     # call a distro specific hook, if available
1280     if type -p unpack_linux_devel_rpm-$DISTROMAJ; then
1281         if ! unpack_linux_devel_rpm-$DISTROMAJ "$kernelrpm"; then
1282             return 255
1283         fi
1284     fi
1285
1286     popd &>/dev/null
1287
1288     find_linux_devel_paths $TOPDIR/reused
1289
1290     return 0
1291
1292 }
1293
1294 build_kernel_ib() {
1295     local linux="$1"
1296     local kib_prefix="$2"
1297     local kib_rpm="$3"
1298
1299     # build kernel-ib{,-devel}/compat-rdma{,-devel}
1300     local K_SRC="K_SRC"
1301     # ofed 1.3 had a bug in the rpm spec
1302     if [ "$OFED_VERSION" = "1.3" ]; then
1303         K_SRC="KSRC"
1304     fi
1305
1306     local OFED_CORE="--with-core-mod --with-ipoib-mod --with-user_mad-mod --with-user_access-mod --with-addr_trans-mod"
1307     local OFED_HARDWARE="--with-mthca-mod --with-mlx4-mod --with-mlx4_en-mod --with-cxgb3-mod --with-nes-mod --with-qib-mod"
1308
1309     if compare_version $OFED_VERSION 3.0; then
1310         OFED_CORE="$OFED_CORE --with-madeye-mod --with-rds-mod"
1311     else
1312         OFED_HARDWARE="$OFED_HARDWARE --with-mlx5-mod --with-cxgb4-mod --with-ocrdma-mod --with-qib-mod"
1313     fi
1314
1315     # some I/B drivers are architecture dependent and kernel-ib's configure
1316     # does not figure it out for us ~sigh~
1317     case "$TARGET_ARCH" in
1318         ppc64)
1319             OFED_HARDWARE="$OFED_HARDWARE --with-ehca-mod"
1320             ;;
1321     esac
1322     # we're no longer shipping the OFED iSCSI target
1323     #OFED_ISCSI="--with-srp-mod --with-srp-target-mod"
1324     OFED_ISCSI="--with-srp-mod"
1325     ## ISER module has no backport support as of OFED 1.5 (i.e. only builds on
1326     ##kernels >= 2.6.30)
1327     #if [[ $OFED_VERSION = 1.[0-4]* ]]; then
1328     #   OFED_ISCSI="$OFED_ISCSI --with-iser-mod"
1329     #fi
1330
1331     # assume we are just rebuilding the SRPM
1332     local BUILD_TYPE=${BUILD_TYPE:-"--rebuild"}
1333     local SOURCE="${TOPDIR}/OFED/SRPMS/${kib_prefix}-*.src.rpm"
1334
1335     # but switch to building from the SPEC if we need to apply patches
1336     if ls ${TOPDIR}/lustre/contrib/patches/ofed/* >/dev/null; then
1337         BUILD_TYPE="-bb"
1338         rpm --define "_topdir ${TOPDIR}" -ivh $SOURCE
1339         SOURCE="${TOPDIR}/SPECS/${kib_prefix}.spec"
1340         local file ed_fragment1 ed_fragment2 n=1
1341         for file in $(ls ${TOPDIR}/lustre/contrib/patches/ofed/*.patch); do
1342             ed_fragment1="$ed_fragment1
1343 Patch$n: ${file%%*/}"
1344             ed_fragment2="$ed_fragment2
1345 %patch$n -p0"
1346             cp $file ${TOPDIR}/SOURCES
1347             let n=$n+1
1348         done
1349         for file in $(ls ${TOPDIR}/lustre/contrib/patches/ofed/*.ed); do
1350             # Only apply the ed-scripts that should be used for the canonical target
1351             # ed-files in ${TOPDIR}/lustre/contrib/patches/ofed/ have to follow the naming
1352             # convention
1353             # <two-digits>-<descriptive-name>:<canonical_target_1>: ...:<canonical_target_N>.ed
1354             # To apply the same change to multiple canonical target simply specify
1355             # a list of colon separated canoncial target names in the file name.
1356             echo "$file" | grep -q -e ":${CANONICAL_TARGET}:" \
1357                                    -e ":${CANONICAL_TARGET}.ed$"
1358             if [ $? -eq 0 ] ; then
1359                 ed_fragment3="$ed_fragment3
1360 $(cat $file)"
1361                 let n=$n+1
1362             fi
1363         done
1364
1365         if [ $n -gt 1 ]; then
1366             ed $SOURCE <<EOF
1367 /^Source: /a
1368 $ed_fragment1
1369 .
1370 /^%setup /a
1371 $ed_fragment2
1372 .
1373 $ed_fragment3
1374 wq
1375 EOF
1376         fi
1377     fi
1378
1379     local linuxrelease=$(find_linux_release)
1380     if compare_version $OFED_VERSION 3.0; then
1381         local OFA_KERNEL_RELEASE=$(echo -n ${linuxrelease} | sed -e 's/-/_/g')
1382     fi
1383     if ! $RPMBUILD $BUILD_TYPE --define 'build_kernel_ib 1' --define 'build_kernel_ib_devel 1' \
1384                   ${FIND_REQUIRES:+--define "__find_requires $FIND_REQUIRES"} \
1385                   --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
1386                   --define "KVERSION ${linuxrelease}" \
1387                   --define "$K_SRC ${linux}" \
1388                   --define "LIB_MOD_DIR /lib/modules/${linuxrelease}/updates" \
1389                   ${OFA_KERNEL_RELEASE:+--define "_release $OFA_KERNEL_RELEASE"} \
1390                   --define "configure_options --without-quilt $OFED_CORE $OFED_HARDWARE $OFED_ISCSI" \
1391                   ${SOURCE} 2>&1; then
1392         fatal 1 "Error building ${kib_rpm}"
1393     fi
1394
1395 }
1396
1397 store_for_reuse() {
1398     local articles="$1"
1399     local module="$2"
1400     local location="$3"
1401     local signature="$4"
1402     local use_links="$5"
1403
1404     local linkflag=""
1405     if $use_links; then
1406         linkflag="l"
1407     fi
1408
1409     local default_iface=$(/sbin/ip route get 192.1.1.1 | sed -ne 's/.* dev \(.*\)  * src .*/\1/p')
1410     if [ -z "$default_iface" ]; then
1411         fatal 1 "Failed to determine the default route interface"
1412     fi
1413     local unique_id=$(/sbin/ip addr show dev $default_iface | sed -ne '/ inet /s/ *inet \(.*\)\/.*/\1/p' | head -1)
1414     if [ -z "$unique_id" ]; then
1415         fatal 1 "Failed to determine a unique id from interface $default_interface"
1416     fi
1417
1418     local finallocation="$location"/"$signature"/"$module"
1419     location="$location"/"$signature-${unique_id}"/"$module"
1420     mkdir -p "$location"
1421     # the cleanup script removes any directory that doesn't have a
1422     # .lastused, so let's try to prevent that as soon as we can
1423     # this solution still slightly racy with the cleanup script
1424     # but the race is a lot tighter now
1425     touch -t 197001010000 "$location/.lastused"
1426     ## use eval/echo here to make sure shell expansions are performed
1427     #if ! cp -a${linkflag} $(eval echo $articles) "$location"; then
1428     local article
1429     for article in $(eval echo $articles); do
1430         if ! cp -a${linkflag} "$article" "$location"; then
1431             error "Failed to copy \"$article\" to \"$location\" in store_for_reuse()"
1432             # rename the cache location so that it's not cached
1433             # product, but is around for analysis
1434             mv "$location"{,-bad-$(date +%s)} ||
1435                 error "failed to clean up a failed cache attempt" \
1436                       "in \"$location\" -- manual cleanup will be" \
1437                       "necessary"
1438             return 1
1439         fi
1440     done
1441
1442     # flag the cache as complete (i.e. in case lbuild was previously
1443     # interrupted while caching)
1444     touch "$location/.lastused"
1445
1446     # put the temporary location into the final location
1447     # (last one wins)
1448     mkdir -p "${finallocation%/*}"
1449     mv "$location" "$finallocation"
1450     rmdir "${location%/*}"
1451     return 0
1452
1453 }
1454
1455 reuse() {
1456     local module="$1"
1457     local dest="$2"
1458     local use_links="${3:-false}"
1459     local signature="$4"
1460
1461     if [ -n "$REUSEBUILD" ] && [ -d "$REUSEBUILD/$signature/$module" ]; then
1462         if [ ! -f "$REUSEBUILD/$signature/$module/.lastused" ]; then
1463             # the .lastused flag is populated at the end of the caching to
1464             # signal that the caching was completed.  if that flag is not
1465             # there, then the cache is invalid (and should be removed in fact)
1466             mv "$REUSEBUILD/$signature/$module"{,-bad-$(date +%s)} ||
1467                 fatal 1 "failed to clean up a bad cache in location $REUSEBUILD/$signature/$module\" -- manual cleanup will be necessary"
1468             return 1
1469         fi
1470
1471         # so that we know how stale this entry is
1472         touch $REUSEBUILD/$signature/$module/.lastused
1473
1474         if $use_links; then
1475             if ls $REUSEBUILD/$signature/$module/* >/dev/null 2>&1; then
1476                 cp -al $REUSEBUILD/$signature/$module/* $dest/
1477             fi
1478         else
1479             # copying is pretty heavy
1480             # cp -a $REUSEBUILD/$signature/$module/* $dest/
1481             # do some creative symlinking instead
1482             local dir
1483             for dir in BUILD SRPMS SPECS; do
1484                 if ls $REUSEBUILD/$signature/$module/$dir/* >/dev/null 2>&1; then
1485                     ln -s $REUSEBUILD/$signature/$module/$dir/* $dest/$dir
1486                 fi
1487             done
1488             # sources have to be copied by file because we need SOURCES to
1489             # be a dir we can write into
1490 # could overrun ls's arg list here
1491             #ls $REUSEBUILD/$signature/$module/SOURCES/* |
1492             find $REUSEBUILD/$signature/$module/SOURCES/ -type f |
1493                 xargs ln -t $dest/SOURCES -s
1494
1495             # same for RPMS/* dirs
1496 # could overrun ls's arg list here
1497             #ls $REUSEBUILD/$signature/$module/RPMS/$TARGET_ARCH/* |
1498             local dir
1499             for dir in $REUSEBUILD/$signature/$module/RPMS/*; do
1500                 mkdir -p $dest/RPMS/${dir##*/}
1501                 find $dir -type f |
1502                   xargs ln -t $dest/RPMS/${dir##*/} -s
1503             done
1504         fi
1505         return 0
1506     else
1507         return 1
1508     fi
1509 }
1510
1511 basearch() {
1512     local arch="$1"
1513
1514     if [[ $arch = i[3456]86 ]]; then
1515         echo "i386"
1516     else
1517         echo "$arch"
1518     fi
1519
1520 }
1521
1522 build_kernel_with_srpm() {
1523     local outfd=$1
1524
1525     if [ -z "$outfd" ] || [ $outfd = 1 ]; then
1526         fatal 1 "You must supply a file descriptor to ${FUNCNAME[0]} and it cannot be 1"
1527     fi
1528
1529     # need to generate the patch for this target
1530     do_patch_linux false >&${outfd}    # sets global $FULL_PATCH (yeah, yuck)
1531
1532     # get an md5sum of the kernel patch + config for reuse check
1533     # XXX really, there needs to be a signature and a CONFIG_FILE per arch
1534     #     in BUILD_ARCHS
1535     local release_str
1536     if $RELEASE; then
1537         local release_str="RELEASE=$RELEASE\n"
1538     fi
1539
1540     if $USE_BUILD_CACHE && [ -n "$REUSEBUILD" ]; then
1541         local REUSE_SIGNATURE=$({ echo -en $release_str;
1542                                   echo $BUILD_GEN;
1543                                   cat "$CONFIG_FILE";
1544                                   cat "$TARGET_FILE";
1545                                   cat "$FULL_PATCH";
1546                                   cat "$LBUILD_DIR/lbuild";
1547                                   cat "$LBUILD_DIR/lbuild-$DISTROMAJ"; } |
1548                                 md5sum | cut -d" " -f1)
1549         # see if we can link to the reuse pool
1550         # XXX - hrm.  i'm not convinced this doesn't belong in the reuse
1551         #       "library"
1552         local CAN_LINK_FOR_REUSE=false
1553         touch $REUSEBUILD/$$
1554         if cp -al $REUSEBUILD/$$ $TOPDIR/ 2>/dev/null; then
1555             CAN_LINK_FOR_REUSE=true
1556         fi
1557         rm $REUSEBUILD/$$
1558     fi
1559
1560     # the extra version string to use for the kernel (which might be a reused
1561     # kernel, remember)
1562     local kernel_extra_version=""
1563     if ! $USE_BUILD_CACHE || ! reuse kernel "$TOPDIR" "$CAN_LINK_FOR_REUSE" \
1564                                    "$REUSE_SIGNATURE"; then
1565         # nothing cached, build from scratch
1566         if [ ! -r "$KERNELDIR/$KERNEL_SRPM" ]; then
1567             echo "Downloading kernel SRPM" >&${outfd}
1568             download_srpm "$CANONICAL_TARGET" "$KERNEL_SRPM" >&${outfd}
1569         fi
1570
1571         if ! rpm -ivh $KERNELDIR/$KERNEL_SRPM \
1572                   --define "_topdir $TOPDIR" >&${outfd} 2>&1; then
1573             # should we clean this up or leave it for analysis?
1574             #rm -rf $RPMTOPDIR
1575             fatal 1 "Error installing kernel SRPM."
1576         fi
1577
1578         # put the Lustre kernel patch into the RPM build tree
1579         cp $FULL_PATCH $TOPDIR/SOURCES/linux-${lnxmaj}-lustre.patch
1580         prepare_and_build_srpm >&${outfd} ||
1581             fatal 1 "failed to prepare_and_build_srpm"
1582
1583         if [ -z "$REUSE_SIGNATURE" ]; then
1584             echo "No reuse signature was caculated so not storing the built kernel" >&${outfd}
1585         else
1586             # store the resulting kernel RPM build tree for future use
1587             echo "Storing the built kernel for future reuse" >&${outfd}
1588             if ! store_for_reuse "$TOPDIR/{SPECS,SOURCES,SRPMS,RPMS}" \
1589                                  "kernel" "$REUSEBUILD" "$REUSE_SIGNATURE" \
1590                                  "$CAN_LINK_FOR_REUSE"; then
1591                 error "Failed to store kernel RPMS for reuse"
1592                 echo "unknown" >&${outfd}
1593                 return 1
1594             fi
1595         fi
1596     fi  # build reuse
1597
1598     # figure out the EXTRA_VERSION of the kernel we built or are re-using
1599     local KERNEL_RPM
1600     if ! KERNEL_RPM=$(find_rpm "$TOPDIR/RPMS/$TARGET_ARCH/" provides "^kernel(-default)? ="); then
1601         fatal 1 "Failed to find a kernel RPM in $TOPDIR/RPMS/$TARGET_ARCH/"
1602     fi
1603     kernel_extra_version=$(rpm -q --queryformat "%{RELEASE}" -p $TOPDIR/RPMS/$TARGET_ARCH/$KERNEL_RPM)
1604
1605     # should now have the following RPMs
1606     # $TOPDIR/RPMS/$arch/kernel-lustre-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1607     # $TOPDIR/RPMS/$arch/kernel-lustre-devel-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1608     # $TOPDIR/RPMS/$arch/kernel-lustre-headers-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1609     # $TOPDIR/RPMS/$arch/kernel-lustre-debuginfo-common-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1610     # $TOPDIR/RPMS/$arch/kernel-lustre-debuginfo-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1611
1612     echo $kernel_extra_version
1613     return 0
1614
1615 }
1616
1617 # build OFED
1618 # globals used:
1619 #    TOPDIR
1620 #    REUSEBUILD, USE_BUILD_CACHE
1621 #    CONFIGURE_FLAGS
1622
1623 build_ofed() {
1624     local linux="$1"
1625     local ofed_version="$2"
1626     local kib_prefix
1627     local kib_rpm
1628
1629     # if an ofed version is given, then it means use OFED proper,
1630     # not any vendor specific "inkernel" version
1631     if [ -z "$ofed_version" ]; then
1632         return 0
1633     fi
1634
1635     if [ "$ofed_version" = "inkernel" ]; then
1636         # see if there is a distro specific override for this and use
1637         # that if it exists
1638         # XXX we need to better integrate a distro specific override with
1639         #     the rest of this function so that all of the reuse cache
1640         #     stuff is leveraged given that 80% of this function is reuse
1641         if type -p build_ofed-$DISTROMAJ; then
1642             local ofed_location
1643             ofed_location=$(build_ofed-$DISTROMAJ ${STDOUT})
1644             local rc=${PIPESTATUS[0]}
1645             CONFIGURE_FLAGS="--with-o2ib=${ofed_location} ${CONFIGURE_FLAGS}"
1646             return $rc
1647         else
1648             return 0
1649         fi
1650     elif compare_version $OFED_VERSION 3.0; then
1651         kib_prefix="ofa_kernel"
1652         kib_rpm="kernel-ib"
1653     else
1654         kib_prefix="compat-rdma"
1655         kib_rpm="compat-rdma"
1656     fi
1657
1658     # build kernel-ib/compat-rdma
1659     if $USE_BUILD_CACHE && [ -n "$REUSEBUILD" ]; then
1660         local REUSE_SIGNATURE=$({ echo "$ofed_version";
1661                                   echo "$(find_linux_release;
1662                                   echo "$BUILD_GEN")";
1663                                   cat "${linux}/include/linux/autoconf.h";
1664                                   cat "$LBUILD_DIR/lbuild";
1665                                   cat "$LBUILD_DIR/lbuild-$DISTROMAJ"; } |
1666                                 md5sum | cut -d" " -f1)
1667         # see if we can link to the reuse pool
1668         # XXX - hrm.  i'm not convinced this doesn't belong in the reuse
1669         #       "library"
1670         local CAN_LINK_FOR_REUSE=false
1671         touch $REUSEBUILD/$$
1672         if cp -al $REUSEBUILD/$$ $TOPDIR/; then
1673             CAN_LINK_FOR_REUSE=true
1674         fi
1675         rm $REUSEBUILD/$$
1676     fi
1677
1678     if ! $USE_BUILD_CACHE || ! reuse ofed "$TOPDIR" "$CAN_LINK_FOR_REUSE" \
1679                                    "$REUSE_SIGNATURE"; then
1680         if [ -n "$REUSE_SIGNATURE" ]; then
1681             # stash away the existing built articles for a moment
1682             mkdir bak
1683             mv {BUILD,{S,}RPMS,S{OURCE,PEC}S} bak
1684             function mv_back {
1685                 pushd bak
1686                 find . | cpio -pudlm ..
1687                 popd
1688                 rm -rf bak
1689             }
1690             create_rpmbuild_dirs
1691         fi
1692         # build it
1693         build_kernel_ib "${linux}" "${kib_prefix}" "${kib_rpm}"
1694
1695         if [ -z "$REUSE_SIGNATURE" ]; then
1696             echo "No reuse signature was caculated so not storing the built ofed"
1697         else
1698             # store the resulting RPM build tree for future use
1699             echo "Storing the built ofed for future reuse"
1700             if ! store_for_reuse "$TOPDIR/{SPECS,SOURCES,BUILD,SRPMS,RPMS}" \
1701                                  "ofed" "$REUSEBUILD" "$REUSE_SIGNATURE" \
1702                                  "$CAN_LINK_FOR_REUSE"; then
1703                 error "Failed to store OFED RPMS for reuse"
1704                 mv_back
1705                 return 1
1706             fi
1707             # put the stuff we stashed away back
1708             mv_back
1709         fi
1710     fi
1711
1712     pushd "$TOPDIR" >/dev/null
1713     rm -rf ${kib_rpm}-devel
1714     mkdir ${kib_rpm}-devel
1715     cd ${kib_rpm}-devel
1716     # the actual ofed RPMs don't have the -rc$n or -$date string appened that
1717     # might be present on the file
1718     #local linuxrelease=$(find_linux_release)
1719     #ofed_version=$(echo $ofed_version |
1720     #               sed -re 's/-(20[0-9]{6,6}-[0-9]{4,4}|rc[0-9]*)$//')
1721     # FIXME
1722     # OFED version will have 'hyphen' for minor release. (e.g. 3.5-1, instead
1723     # of 3.5.1) compat-rdma and compat-rdma-devel could have same version
1724     # number, but currectly not. Once OFED fix this in the future release, we
1725     # can remove following filter.
1726     #ofed_version=$(echo $ofed_version |
1727     #               sed -re 's/-([0-9]*-[rR][cC][0-9]*)$//')
1728     #local rpm=$(ls $TOPDIR/RPMS/*/${kib_rpm}-devel-${ofed_version}-${linuxrelease//-/_}.*.rpm)
1729     # I dont' know why we have gone through the trouble to filter out the name
1730     # of the rpm there should only be one ${kib_rpm}-devel built
1731     local rpm=$(ls $TOPDIR/RPMS/*/${kib_rpm}-devel-*.rpm)
1732     if ! rpm2cpio < $rpm | cpio -id; then
1733         fatal 1 "could not unpack the ${kib_rpm}-devel rpm."
1734     fi
1735     CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/${kib_prefix} ${CONFIGURE_FLAGS}"
1736     popd >/dev/null
1737
1738 }
1739
1740 build_with_srpm() {
1741
1742     if ! $PATCHLESS; then
1743         local kernel_extra_version
1744         if ! kernel_extra_version=$(build_kernel_with_srpm ${STDOUT}); then
1745             fatal 1 "Failed to build the kernel from it's SRPM"
1746         fi
1747
1748         for arch in $BUILD_ARCHS; do
1749             local kernel_devel_rpm
1750             if ! kernel_devel_rpm=$(find_rpm "$TOPDIR/RPMS/$arch/" provides "^$(devel_kernel_name $KERNEL_LUSTRE_NAMING) ="); then
1751                 fatal 1 "Failed to find a kernel development RPM in $TOPDIR/RPMS/$arch/"
1752             fi
1753
1754             # install the -devel RPM in preparation for modules builds
1755             if ! lnxrel="$kernel_extra_version" unpack_linux_devel_rpm \
1756                            "$TOPDIR/RPMS/$arch/$kernel_devel_rpm"; then
1757                 fatal 1 "Could not find the Linux tree in $TOPDIR/RPMS/$arch/$kernel_devel_rpm"
1758             fi
1759         done
1760     else
1761         # need to find and unpack the vendor's own kernel-devel for patchless
1762         # client build
1763         local kernelrpm
1764         if ! kernelrpm=$(find_linux_rpm "-$DEVEL_KERNEL_TYPE"); then
1765             fatal 1 "Could not find the kernel-$DEVEL_KERNEL_TYPE RPM in $KERNELRPMSBASE/$lnxmaj/$DISTROMAJ"
1766         fi
1767         if ! lnxrel="$lnxrel" unpack_linux_devel_rpm "$kernelrpm" "-"; then
1768             fatal 1 "Could not find the Linux tree in $kernelrpm"
1769         fi
1770     fi
1771
1772     # ~sigh~  have to make copies of and modify some of the rpm
1773     # infrastructure files so that find-requires can find our unpacked
1774     # kernel-devel artifacts
1775     cp $RPM_HELPERS_DIR/{symset-table,find-requires{,.ksyms}} .
1776     FIND_REQUIRES="$(pwd)/find-requires"
1777     chmod 755 {symset-table,find-requires{,.ksyms}}
1778     local tmp="$(pwd)"
1779     tmp="${tmp//\//\\/}"
1780     ed find-requires <<EOF
1781 1a
1782 set -x
1783 .
1784 /|.*find-requires.ksyms/s/|/| bash -x/
1785 g/ [^ ]*\/\(find-requires\.ksyms\)/s// $tmp\/\1/g
1786 wq
1787 EOF
1788     ed find-requires.ksyms <<EOF
1789 1a
1790 set -x
1791 .
1792 g/\/.*\/\(symset-table\)/s//$tmp\/\1/g
1793 wq
1794 EOF
1795     ed symset-table <<EOF
1796 1a
1797 set -x
1798 .
1799 g/\(\/boot\/\)/s//$tmp\/reused\1/g
1800 g/\(\/usr\/src\/kernels\/\)/s//$tmp\/reused\1/g
1801 wq
1802 EOF
1803
1804     build_ofed "${LINUXOBJ:-$LINUX}" "$OFED_VERSION" ||
1805         fatal 1 "error building OFED"
1806
1807     # now build Lustre
1808     if build_lustre "$LINUX" "$LINUXOBJ"; then
1809         # the build worked.  resolve any symlinked files (i.e. from reuse)
1810         # in RPMS/$arch to real files so that that that huge mess of
1811         # complication known as LTS can copy them yet somewhere else.
1812         # is it any wonder this whole process is so damn so?  anyone ever
1813         # heard of hardlinks?  it's this cool new thing that allows you save
1814         # tons of time and space by creating... well you can go read about
1815         # them if you have not heard about them yet.
1816         # can i say how much the implemenation of all of this really impedes
1817         # RPM reuse?
1818         local dir
1819         for dir in RPMS/*; do
1820             pushd $dir
1821             for file in $(ls); do
1822                 if [ -h $file ]; then
1823                     cp $file foo
1824                     mv foo $file
1825                 fi
1826             done
1827             popd
1828         done
1829         # also, for i?86, make sure all of the RPMs are in RPMS/$TARGET_ARCH
1830         # as that's where LTS expects to find them
1831         for dir in RPMS/*; do
1832             if [ $dir = RPMS/$TARGET_ARCH ]; then
1833                 continue
1834             fi
1835             pushd $dir
1836             local files=$(ls)
1837             if [ -n "$files" ]; then
1838                 cp -al $files ../$TARGET_ARCH
1839             fi
1840             popd
1841         done
1842     else
1843         return 1
1844     fi
1845
1846 }
1847
1848 create_rpmbuild_dirs() {
1849
1850     [ -d RPMS ] || mkdir RPMS
1851     for arch in $BUILD_ARCHS; do
1852         if [[ $arch = i?86 ]]; then
1853             # some stupidity in the sles11 kernel spec requires an RPMS/i386
1854             # even if the target arch is i686
1855             [ -d RPMS/i386 ] || mkdir RPMS/i386
1856         fi
1857         [ -d RPMS/$arch ] || mkdir RPMS/$arch
1858     done
1859     [ -d RPMS/noarch ] || mkdir RPMS/noarch
1860     [ -d BUILD ] || mkdir BUILD
1861     [ -d SOURCES ] || mkdir SOURCES
1862     [ -d SPECS ] || mkdir SPECS
1863     [ -d SRPMS ] || mkdir SRPMS
1864
1865 }
1866
1867 new_list() {
1868
1869     echo ""
1870
1871 }
1872
1873 add_list() {
1874     local list="$1"
1875     local item="$2"
1876
1877     echo "$list $item"
1878
1879 }
1880
1881 is_list_member() {
1882     local list="$1"
1883     local item="$2"
1884
1885     [[ $list\  == *\ $item\ * ]]
1886
1887 }
1888
1889 #########################################################################
1890 # Generate a backtrace through the call stack.
1891 #
1892 # Input: None
1893 # Output: None
1894 #########################################################################
1895 backtrace() {
1896     local strip=${1:-1}
1897
1898     local funcname="" sourcefile="" lineno="" n
1899
1900     echo "Call stack: (most recent first)"
1901     for (( n = $strip ; n < ${#FUNCNAME[@]} ; ++n )) ; do
1902         funcname=${FUNCNAME[$n - 1]}
1903         sourcefile=$(basename ${BASH_SOURCE[$n]})
1904         lineno=${BASH_LINENO[$n - 1]}
1905         if [ $n = 1 ]; then
1906             let lineno-=11
1907         fi
1908         # Display function arguments
1909         if [[ ! -z "${BASH_ARGV[@]}" ]]; then
1910             local args newarg j p=0
1911             for (( j = ${BASH_ARGC[$n - 1]}; j > 0; j-- )); do
1912                 newarg=${BASH_ARGV[$j + $p - 1]}
1913                 args="${args:+${args} }'${newarg}'"
1914             done
1915             let p+=${BASH_ARGC[$n - 1]}
1916         fi
1917         echo "  ${funcname} ${args:+${args} }at ${sourcefile}:${lineno}"
1918     done
1919
1920     echo
1921     echo "BEGIN BACKTRACE"
1922
1923     #echo ${BASH_LINENO[*]}
1924     #echo ${BASH_SOURCE[*]}
1925     #echo ${FUNCNAME[*]}
1926     local i=$((${#FUNCNAME[@]} - 1))
1927     while [ $i -ge 0 ]; do
1928         local lineno=${BASH_LINENO[$i]}
1929         if [ $i = 0 ]; then
1930             let lineno-=11
1931         fi
1932         local SOURCELINE="${BASH_SOURCE[$i + 1]}:${lineno}"
1933         # Can't figure out how to get function args from other frames...
1934         local FUNCTION="${FUNCNAME[$i]}()"
1935         echo "$SOURCELINE:$FUNCTION"
1936         i=$((i - 1))
1937     done
1938
1939     echo "END BACKTRACE"
1940
1941     echo $BACKTRACE
1942
1943 }
1944
1945 seen_list=$(new_list)
1946 trap 'set +x;
1947 echo "An unexpected error has occurred at ${BASH_SOURCE[0]##*/}:$((LINENO-1)).
1948 Unfortunately the above line number in the message may or may not be correct,
1949 but details have been send to the lbuild maintainer.  Attempting to continue."; (echo "Untrapped error"
1950 echo
1951 # have we seen this one
1952 echo "checking seen list for ${BASH_SOURCE[0]}:${BASH_LINENO[0]}"
1953
1954 if is_list_member "$seen_list" "${BASH_SOURCE[0]}:${BASH_LINENO[0]}"; then
1955   echo "seen this one already"
1956 else
1957   seen_list=$(add_list "$seen_list" "${BASH_SOURCE[0]}:${BASH_LINENO[0]}")
1958 fi
1959 backtrace
1960 ) ; set $xtrace' ERR
1961 set -E
1962
1963 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
1964
1965 options=$(getopt -o D:h -l kerneltree:,distro:,kernelrpm:,reusebuild:,patchless,ccache,norpm,external-patches:,timestamp:,extraversion:,kerneldir:,linux:,lustre:,nodownload,nosrc,noiokit,ofed-version:,mpss-version:,publish,release,set-value:,src,stage:,target:,target-archs:,with-linux:,xen -- "$@")
1966
1967 if [ $? != 0 ]; then
1968     usage 1
1969 fi
1970
1971 eval set -- "$options"
1972
1973 while [ "$1" ]; do
1974     case "$1" in
1975         '')
1976             usage 1
1977             ;;
1978         --ccache)
1979             CCACHE='ccache'
1980             shift
1981             ;;
1982         -D)
1983             DATE=$2
1984             shift 2
1985             ;;
1986         --external-patches)
1987             EXTERNAL_PATCHES=$2
1988             shift 2
1989             ;;
1990         --extraversion)
1991             EXTRA_VERSION=$2
1992             shift 2
1993             ;;
1994         --help | -h)
1995             usage 0
1996             ;;
1997         --kerneldir)
1998             KERNELDIR=$2
1999             shift 2
2000             ;;
2001         --kerneltree)
2002             if ! KERNELTREE=$(canon_path "$2"); then
2003                 fatal 1 "Could not determine the canonical location of $2"
2004             fi
2005             shift 2
2006             ;;
2007         --linux | --with-linux)
2008             if ! LINUX=$(canon_path "$2"); then
2009                 fatal 1 "Could not determine the canonical location of $2"
2010             fi
2011             shift 2
2012             ;;
2013         --distro)
2014             DISTRO=$2
2015             shift 2
2016             ;;
2017         --reusebuild)
2018             if ! REUSEBUILD=$(canon_path "$2"); then
2019                 fatal 1 "Could not determine the canonical location of $2"
2020             fi
2021             shift 2
2022             ;;
2023         --norpm)
2024             NORPM=true
2025             shift
2026             ;;
2027         --noiokit)
2028             IOKITRPM=false
2029             shift
2030             ;;
2031         --patchless)
2032             PATCHLESS=true
2033             shift
2034             ;;
2035         --kernelrpm)
2036             if ! KERNELRPMSBASE=$(canon_path "$2"); then
2037                 fatal 1 "Could not determine the canonical location of $2"
2038             fi
2039             shift 2
2040             ;;
2041         --timestamp)
2042             TIMESTAMP=$2
2043             shift 2
2044             ;;
2045         --lustre)
2046             if ! LUSTRE=$(canon_filepath "$2"); then
2047                 fatal 1 "Could not determine the canonical location of $2"
2048             fi
2049             shift 2
2050             ;;
2051         --nodownload)
2052             DOWNLOAD=false
2053             shift 1
2054             ;;
2055         --nosrc)
2056             DO_SRC=false
2057             shift 1
2058             ;;
2059         --ofed-version)
2060             OFED_VERSION="$2"
2061             shift 2
2062             ;;
2063         --mpss-version)
2064             MPSS_VERSION="$2"
2065             shift 2
2066             ;;
2067         --publish)
2068             shift
2069             ;;
2070         --release)
2071             RELEASE=true
2072             shift
2073             ;;
2074         --src)
2075             DO_SRC=true
2076             shift 1
2077             ;;
2078         --stage)
2079             STAGEDIR=$2
2080             shift 2
2081             ;;
2082         --target)
2083             TARGET=$2
2084             shift 2
2085             ;;
2086         --target-archs)
2087             TARGET_ARCHS=$2
2088             shift 2
2089             ;;
2090         --xen)
2091             XEN=true
2092             shift
2093             ;;
2094         --set-value)
2095             eval $2
2096             shift 2
2097             ;;
2098         --)
2099             shift
2100             # there are actually some lustre configure flags that we need to
2101             # handle ourselves (but we still give them to configure)
2102             if [[ \ $@\  == *\ --disable-tests\ * ]]; then
2103                 LUSTRE_TESTS=false
2104             fi
2105             CONFIGURE_FLAGS=$@
2106             break
2107             ;;
2108         *)
2109             usage 1 "Unrecognized option: $1"
2110             ;;
2111     esac
2112 done
2113
2114 check_options
2115
2116 unpack_lustre
2117
2118 # XXX - should we _always_ get the buildid from the META file?  what are the
2119 # other (i.e. non-lustre-tarball use cases of lbuild)?
2120 BUILDID=$(sed -n -e 's/^BUILDID = //p' lustre/META)
2121
2122 load_target
2123
2124 if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
2125     download_ofed
2126     unpack_ofed || fatal 1 "Error unpacking OFED tarball"
2127 fi
2128
2129 if [ -n "$MPSS_VERSION" ]; then
2130     [ -z "$MPSS_DISTRO" ] && MPSS_DISTRO=$(autodetect_distro)
2131
2132     MPSS_INFO=$(mktemp -u mpss-info-XXXXXXXXXX.html)
2133     download_mpss_info "$MPSS_INFO"
2134
2135     if [ "$MPSS_VERSION" = "last" ]; then
2136         MPSS_VERSION=$(parse_mpss_info "$MPSS_INFO" "" ${MPSS_DISTRO})
2137         [ -z "$MPSS_VERSION" ] &&
2138             fatal 1 "Could not determine the last MPSS version"
2139     fi
2140
2141     MPSS_MAJ=${MPSS_VERSION%%.*}
2142     MPSS_VER=${MPSS_VERSION%%-*}
2143     MPSS_BLD=${MPSS_VERSION##*-}
2144     [ "$MPSS_VER" = "$MPSS_BLD" ] && MPSS_BLD=0
2145
2146     download_mpss "$MPSS_INFO"
2147     [ -r "$MPSS_INFO" ] && rm -f "$MPSS_INFO"
2148     echo "Building with MPSS $MPSS_VER-$MPSS_BLD"
2149     unpack_mpss  || fatal 1 "Error unpacking MPSS tarballs"
2150     prepare_mpss || fatal 1 "Error preparing MPSS for kernel modules build"
2151
2152     if [ -z "$MPSS_OFED" ]; then
2153         MPSS_OFED=$(find $PWD -type f -path "*/ofed-driver*/Module.symvers" -print -quit)
2154         if [ -n "$MPSS_OFED" ]; then
2155             MPSS_OFED="--with-o2ib=$(dirname $MPSS_OFED)"
2156         else
2157             MPSS_OFED="--without-o2ib"
2158         fi
2159     fi
2160
2161     # disable unsupported parts:
2162     PATCHLESS=true
2163     IOKITRPM=false
2164     LDISKFSRPM=false
2165     ZFSNOTSUPPORTED="yes"
2166
2167     # define variables for cross compilation:
2168     CROSS_SUFFIX="-mic"
2169     if [ "$MPSS_MAJ" = "3" ]; then
2170         CC_TARGET_ARCH=k1om-mpss-linux
2171         LINUX="$TOPDIR/mpss"
2172         RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"cross_requires /bin/sh\""
2173     else
2174         CC_TARGET_ARCH=x86_64-k1om-linux
2175         LINUX="$TOPDIR/mpss/card/kernel"
2176         RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"cross_requires intel-mic-gpl = $MPSS_VER\""
2177     fi
2178     LUSTRE_VERSION=$(sed -n -e 's/^VERSION = //p' lustre/META)
2179     CONFIGURE_FLAGS="$CONFIGURE_FLAGS $MPSS_OFED --host=$CC_TARGET_ARCH --build=x86_64-pc-linux"
2180     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"post_script build/gen_filelist.sh\""
2181     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"make_args ARCH=k1om CROSS_COMPILE=${CC_TARGET_ARCH}-\""
2182     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"cross_path ${CROSS_PATH:=/opt/lustre/${LUSTRE_VERSION}/${CC_TARGET_ARCH}}\""
2183     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"rootdir %{cross_path}\""
2184     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"_prefix %{cross_path}/usr\""
2185     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"_mandir %{_prefix}/share/man\""
2186     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"_sysconfdir %{cross_path}/etc\""
2187     RPMBUILD_DEFS="$RPMBUILD_DEFS --define \"kmoddir extra\""
2188
2189     # redefine CC for proper ./configure during rpmbuild
2190     export CC=${CC_TARGET_ARCH}-gcc
2191 fi
2192
2193 # make sure the RPM build environment is set up
2194 create_rpmbuild_dirs
2195
2196 # if an unpacked kernel source tree was given on the command line
2197 # just build lustre with it (nothing distro kernel specific here)
2198 if [ -n "$LINUX" ]; then
2199     find_linux_release() {
2200         _find_linux_release $LINUX
2201     }
2202     build_ofed "${LINUXOBJ:-$LINUX}" "$OFED_VERSION" ||
2203         fatal 1 "error building OFED"
2204     build_lustre "$LINUX" "$LINUXOBJ"
2205 else
2206     if [ ! -f "${LBUILD_DIR}/lbuild-$DISTROMAJ" ]; then
2207         fatal 1 "${LBUILD_DIR}/lbuild-$DISTROMAJ not found"
2208     fi
2209     source ${LBUILD_DIR}/lbuild-$DISTROMAJ
2210     build_with_srpm || fatal 1 "Failed to build_with_srpm"
2211 fi
2212
2213 stage