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