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