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