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