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