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