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