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