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