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