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