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