Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / build / lbuild.old_school
1 # vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:
2
3 OLD_SCHOOL=true
4
5 RHBUILD=${RHBUILD:-0}
6 SUSEBUILD=${SUSEBUILD:-0}
7 LINUX26=${LINUX26:-0}
8 SUSEBUILD=${SUSEBUILD:-0}
9
10 build_tarball() {
11     local TARGET=$1
12     local SRPM=$2
13
14     if [ "$TARGET" = "rhel-2.6" -o "$TARGET" = "rhel-2.4" ]; then
15         local SPEC=""
16         if [ "$TARGET" = "rhel-2.6" ]; then
17             SPEC=kernel-2.6.spec
18             OLDCONFIG=nonint_oldconfig
19         elif [ "$TARGET" = "rhel-2.4" ]; then
20             SPEC=kernel-2.4.spec
21             OLDCONFIG=oldconfig
22         fi
23
24         RPMTOPDIR=$(mktemp -d $KERNELDIR/rpm_XXXXXX)
25         mkdir $RPMTOPDIR/BUILD/
26         rpm -ivh $KERNELDIR/$SRPM --define "_topdir $RPMTOPDIR" || \
27             { rm -rf $RPMTOPDIR; fatal 1 "Error installing kernel SRPM."; }
28         $RPMBUILD -bp --nodeps --target i686 $RPMTOPDIR/SPECS/$SPEC --define "_topdir $RPMTOPDIR"
29         pushd $RPMTOPDIR/BUILD/kernel-${lnxmaj}/linux-${lnxmaj} && {
30             make mrproper
31             cp configs/kernel-${lnxmaj}-i686-smp.config .config
32             if ! make $OLDCONFIG > /dev/null; then
33                 fatal 1 "error trying to make $OLDCONFIG while building a tarball from SRPM."
34             fi
35             make include/linux/version.h
36             rm -f .config
37             cd ..
38             tar cjf $KERNEL_FILE linux-${lnxmaj}
39         }
40         popd
41         rm -rf $RPMTOPDIR
42     fi
43 }
44
45 download_and_build_tarball() {
46     local target=$1
47     local kernel_file=$2
48
49     local srpm=kernel-${lnxmaj}-${lnxrel}.src.rpm
50
51     echo "Downloading http://downloads.lustre.org/public/kernels/$target/old/$srpm..."
52     if ! wget -nv "http://downloads.lustre.org/public/kernels/$target/old/$srpm" \
53         -O "$KERNELDIR/$srpm" ; then
54         fatal 1 "Could not download target $kernel_file's kernel SRPM $srpm from downloads.lustre.org."
55     fi
56     [ -s "$KERNELDIR/$srpm" ] || {
57         rm -rf $KERNELDIR/$srpm
58         fatal 1 "Could not download target $kernel_file's kernel SRPM $srpm from downloads.lustre.org."
59     }
60
61     build_tarball $target $srpm
62 }
63
64 unpack_linux() {
65
66     untar "$KERNEL_FILE" || fatal 1 "Error unpacking Linux tarball"
67     [ -d linux ] || ln -sf linux* linux
68
69 }
70
71 patch_linux() {
72
73     [ "$SERIES" ] || return 0
74
75     do_patch_linux true
76
77     echo "Replacing .config files..."
78     [ -d linux/configs ] || mkdir linux/configs || \
79         fatal 1 "Error creating configs directory."
80     rm -f linux/configs/*
81     copysuccess=0
82     for patchesdir in "$EXTERNAL_PATCHES" "lustre/lustre/kernel_patches" ; do
83         [ "$patchesdir" ] && \
84             cp -v $patchesdir/kernel_configs/kernel-${VERSION}-${TARGET}*.config linux/configs/ >/dev/null && copysuccess=1
85     done
86     [ "$copysuccess" = "1" ] || \
87         fatal 1 "Error copying in kernel configs."
88 }
89
90 pack_linux() {
91     TARBALL="$(readlink linux)-$EXTRA_VERSION.tar.gz"
92     echo "Creating patched linux tarball $TARBALL..."
93     tar zcf "$TARBALL" "$(readlink linux)" \
94         --exclude "CVS" --exclude ".cvsignore" || \
95         --exclude "*.orig" --exclude "*~" --exclude "*.rej" || \
96         fatal 1 "Error creating patched Linux tarball."
97 }
98
99 clean_linux() {
100     [ -d linux ] || return 0
101     echo "Cleaning linux..."
102     [ -L linux ] && rm -rf $(readlink linux)
103     rm -rf linux
104 }
105
106 prep_kernel_build() {
107     local lmakeopts="\"\""
108     if $XEN; then
109         lmakeopts="--xen"
110     fi
111     # make .spec file
112     ENABLE_INIT_SCRIPTS=""
113     sed \
114         -e "s^@BASE_ARCHS@^$BASE_ARCHS^g" \
115         -e "s^@BIGMEM_ARCHS@^$BIGMEM_ARCHS^g" \
116         -e "s^@BIGSMP_ARCHS@^$BIGSMP_ARCHS^g" \
117         -e "s^@BOOT_ARCHS@^$BOOT_ARCHS^g" \
118         -e "s^@CONFIGURE_FLAGS@^$CONFIGURE_FLAGS^g" \
119         -e "s^@ENABLE_INIT_SCRIPTS@^$ENABLE_INIT_SCRIPTS^g" \
120         -e "s^@JENSEN_ARCHS@^$BOOT_ARCHS^g" \
121         -e "s^@KERNEL_EXTRA_VERSION@^$EXTRA_VERSION^g" \
122         -e "s^@KERNEL_EXTRA_VERSION_DELIMITER@^$EXTRA_VERSION_DELIMITER^g" \
123         -e "s^@KERNEL_TARGET_DELIMITER@^$TARGET_DELIMITER^g" \
124         -e "s^@KERNEL_RELEASE@^${EXTRA_VERSION//-/_}^g" \
125         -e "s^@KERNEL_SOURCE@^$KERNEL^g" \
126         -e "s^@KERNEL_VERSION@^$VERSION^g" \
127         -e "s^@LINUX26@^$LINUX26^g" \
128         -e "s^@LUSTRE_SOURCE@^${LUSTRE##*/}^g" \
129         -e "s^@LUSTRE_TARGET@^$TARGET^g" \
130         -e "s^@PSERIES64_ARCHS@^$PSERIES64_ARCHS^g" \
131         -e "s^@RHBUILD@^$RHBUILD^g" \
132         -e "s^@SMP_ARCHS@^$SMP_ARCHS^g" \
133         -e "s^@SUSEBUILD@^$SUSEBUILD^g" \
134         -e "s^@UP_ARCHS@^$UP_ARCHS^g" \
135         -e "s^@LMAKEOPTS@^$lmakeopts^g" \
136         < $TOPDIR/lustre/build/lustre-kernel-2.4.spec.in \
137         > lustre-kernel-2.4.spec
138     [ -d SRPMS ] || mkdir SRPMS
139     [ -d RPMS ] || mkdir RPMS
140     [ -d BUILD ] || mkdir BUILD
141     [ -d SOURCES ] || mkdir SOURCES
142     for script in linux-{rhconfig.h,merge-config.awk,merge-modules.awk} \
143         suse-{functions.sh,post.sh,postun.sh,trigger-script.sh.in} \
144         sles8-{pre,post,postun,update_{INITRD_MODULES,rcfile_setting}}.sh ; do
145         cp $TOPDIR/lustre/build/$script SOURCES
146     done
147     cp "$LUSTRE" "$KERNEL_FILE" SOURCES
148     if [ "$EXTERNAL_PATCHES" -a -d "$EXTERNAL_PATCHES" ] ; then
149         tar zcf SOURCES/external-patches.tar.gz -C "$EXTERNAL_PATCHES" series targets patches kernel_configs
150     else
151         touch SOURCES/external-patches.tar.gz
152     fi
153 }
154
155 clean_lustre() {
156     [ -d lustre ] || return 0
157     echo "Cleaning Lustre..."
158     [ -L lustre ] && rm -rf $(readlink lustre)
159     rm -rf lustre
160 }
161
162 build_kernel() {
163     echo "Building kernel + Lustre RPMs for: $BUILD_ARCHS..."
164     targets=
165     for arch in $BUILD_ARCHS ; do
166         targets="--target $arch $targets"
167     done
168
169     local rpmbuildopt='-bb'
170 #    if $NORPM; then
171 #        rpmbuildopt='-bc'
172 #        echo NORPM mode. Only compiling.
173 #        echo "XXX: need to fix lmake - add options to do rpmbuild -bc instead of -bb"
174 #    fi
175
176     $RPMBUILD $targets $rpmbuildopt lustre-kernel-2.4.spec \
177         --define "_tmppath $TMPDIR" \
178         --define "_topdir $TOPDIR" || \
179         fatal 1 "Error building rpms for $BUILD_ARCHS."
180
181     if $DO_SRC; then
182         $RPMBUILD -bs lustre-kernel-2.4.spec \
183             --define "_tmppath $TMPDIR" \
184             --define "_topdir $TOPDIR" || \
185             fatal 1 "Error building .src.rpm."
186     fi
187
188     ( $(skeep_ldiskfs_rpm $TAG) ) && return
189
190     pushd $TOPDIR/BUILD/lustre*/ldiskfs || return 255
191     make dist
192     if [ "$?" != "0" ] ; then
193         popd
194         return 255
195     fi
196     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
197
198     gen_lustre_version
199
200     local ldiskfs_spec=lustre-ldiskfs.spec
201     [ -f "$ldiskfs_spec" ] && sed \
202     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
203     < $ldiskfs_spec \
204     > ../lustre-ldiskfs.spec
205
206     $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
207         --define "_tmppath /var/tmp" \
208         --define "_topdir $TOPDIR"
209     if [ "$?" != "0" ] ; then
210         popd
211         return 255
212     fi
213
214     if $DO_SRC; then
215             $RPMBUILD -bs ../lustre-ldiskfs.spec \
216             --define "_tmppath /var/tmp" \
217             --define "_topdir $TOPDIR"
218         if [ "$?" != "0" ] ; then
219             popd
220             return 255
221         fi
222     fi
223     popd
224 }
225
226 #get date of last changed target/config/series/patches
227 get_last_source_date() {
228     local filelist="${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} \
229         $CONFIG_FILE"
230     local TOPDIRnew=$(echo ${TOPDIR} | sed -e s/\\//\\\\\\//g)
231     filelist="$filelist $( \
232         cat ${TOPDIR}/lustre/lustre/kernel_patches/series/${SERIES} | \
233         sed -e s/^/${TOPDIRnew}\\/lustre\\/lustre\\/kernel_patches\\/patches\\// 2>&1)"
234     local sourcelastdate=$( find ${filelist} -name CVS -prune -o \
235                 -type f -printf "%T@\n" 2>&1 | sort | tail -1 )
236     is_integer $sourcelastdate && echo $sourcelastdate
237 }
238
239 #check if variable is integer
240 is_integer() {
241     local invariable=$1
242     [ "$invariable" = "" ] && return 255
243     local invariableint=$( echo $invariable | sed -e s/[^0-9]//g )
244     [ "$invariable" = "$invariableint" ] || return 255
245 }
246
247 #store RPMs and/or BUILD dir for future reuse
248 store_for_reuse() {
249     local rpmonly=$1
250     if [ ! "$REUSEBUILD" = "" ] && [ -d  "/$REUSEBUILD/" ] ; then
251         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || mkdir "${REUSEBUILD}/${TIMESTAMP}"
252         [ -d "${REUSEBUILD}/${TIMESTAMP}" ] || return 255
253     else
254         return 255
255     fi
256
257     local lnxrelnew=${lnxrel//-/_}
258     local EXTRA_VERSIONnew=${EXTRA_VERSION//-/_}
259     local KERNELRPMnew=$(basename "$KERNELRPM")
260     if [ ! "$rpmonly" = "rpmonly" ]; then
261             local builddir=
262         if [ ! "$KERNELCOMPILEDIR" = "" ]; then
263             builddir="$KERNELCOMPILEDIR"
264         else
265             builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}"
266             [ "$KERNELCOMPILEDIR" = "" ] || builddir="$KERNELCOMPILEDIR"
267             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}.${lnxrel}"
268             [ -d "$builddir" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/linux-${lnxmaj}-${lnxrel}"
269             if [ ! -d "$builddir" ]; then
270                 pushd "BUILD/lustre-kernel-${lnxmaj}/lustre/" || return 255
271                 local basebuilddir=$(ls -d linux-${lnxmaj}* | head -1)
272                 [ "$basebuilddir" = "" ] || builddir="BUILD/lustre-kernel-${lnxmaj}/lustre/${basebuilddir}"
273                 popd
274             fi
275         fi
276         [ -d "$builddir" ] || return 255
277         local dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}"
278         ( $PATCHLESS ) && dstdir="${REUSEBUILD}/${TIMESTAMP}/linux-$KERNELRPMnew" && \
279                 dstdir="${dstdir%.rpm}"
280         [ -d "$dstdir" ] && rm -rf "$dstdir"
281         mv "${builddir}" "$dstdir" || return 255
282         if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
283             # move the OFED kernel-ib-devel tree as well
284             mv "${builddir%/*}/kernel-ib-devel/usr/src/ofa_kernel" "${dstdir%/*}" || return 255
285         fi
286     fi
287     #store kernel rpm
288     local kernelrpmname="kernel-lustre-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
289     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-${KERNCONFSMPTYPE}-${lnxmaj}-${EXTRA_VERSNnew}.${TARGET_ARCH}.rpm"
290     ( $PATCHLESS ) && [ -f "$KERNELRPM" ] && kernelrpmname="$KERNELRPMnew"
291     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
292         echo "RPM already exist in store directory tree"
293     else
294         [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
295     fi
296     #store kernel source rpm
297     kernelrpmname="kernel-lustre-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
298     [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] || kernelrpmname="kernel-source-${lnxmaj}-${EXTRA_VERSIONnew}.${TARGET_ARCH}.rpm"
299     ( $PATCHLESS ) && [ -f "$KERNELSOURCERPM" ] && kernelrpmname=$(basename "$KERNELSOURCERPM")
300     if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${kernelrpmname}" ]; then
301         echo "RPM already exist in store directory tree"
302     else
303             [ -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${kernelrpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
304     fi
305     if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
306         # store kernel-ib RPMs
307         local rpmname
308         local ofed_version=$(echo $OFED_VERSION |
309                              sed -re 's/-(20[0-9]{6,6}-[0-9]{4,4}|rc[0-9]*)$//')
310         for rpmname in "kernel-ib" "kernel-ib-devel"; do
311             rpmname="${rpmname}-${ofed_version}"
312             if $PATCHLESS; then
313                 rpmname="${rpmname}-${LINUXRELEASE//-/_}"
314             else
315                 rpmname="${rpmname}-${lnxmaj}${EXTRA_VERSION_DELIMITER//-/_}${EXTRA_VERSIONnew}${TARGET_DELIMITER//-/_}${KERNCONFSMPTYPE}"
316             fi
317             rpmname="${rpmname}.${TARGET_ARCH}.rpm"
318             if [ "$rpmonly" = "rpmonly" ] && [ -f "${REUSEBUILD}/${TIMESTAMP}/${rpmname}" ]; then
319                 echo "RPM already exist in store directory tree"
320             else
321                 [ -f "RPMS/${TARGET_ARCH}/${rpmname}" ] && cp -f "RPMS/${TARGET_ARCH}/${rpmname}" "${REUSEBUILD}/${TIMESTAMP}/"
322             fi
323         done
324     fi
325 }
326
327 #look for kernel source RPM
328 find_linux_source_rpm() {
329     local rpmfile=
330     local findarch=true
331     local arch=
332     local pathtorpms=
333     [ ! "$TARGET_ARCH" = "" ] && arch=$TARGET_ARCH && findarch=false
334     
335     if ! $findarch; then
336         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
337         [ -d $pathtorpms ] || return 255
338         case "$DISTRO" in
339             rhel4)
340                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
341             ;;
342             sles10)
343                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
344             ;;
345             *)
346                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
347             ;;
348         esac
349         [ -f "${pathtorpms}/${rpmfile}" ] || return 255
350         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
351     else
352         for arch in $TARGET_ARCHS_ALL; do
353         pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${arch}"
354         [ -d $pathtorpms ] || continue
355         case "$DISTRO" in
356             rhel4)
357                 rpmfile="kernel-${lnxmaj}-${lnxrel}.src.rpm"
358             ;;
359             sles10)
360                 rpmfile="kernel-source-${lnxmaj}.${lnxrel}.${arch}.rpm"
361             ;;
362             *)
363                 rpmfile="kernel-source-${lnxmaj}-${lnxrel}.${arch}.rpm"
364             ;;
365         esac
366         [ -f "${pathtorpms}/${rpmfile}" ] || continue
367         KERNELSOURCERPM="${pathtorpms}/${rpmfile}"
368         TARGET_ARCH=${arch}
369         break
370         done
371     fi
372     [ -f "${KERNELSOURCERPM}" ] || return 255
373 }
374
375 #unpack and make symlinks for reusing kernel RPM
376 reuse_kernel_rpm() {
377     local pathtorpm=$1
378     local pathtokernelibrpm=$2
379     [ "$pathtorpm" = "" ] && return 255
380     [ -f "$pathtorpm" ] || return 255
381     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
382     pushd $TOPDIR/reused || return 255
383
384     rpm2cpio < $pathtorpm | cpio -idc
385     [ ${PIPESTATUS[0]} -eq 0 ] || return 255
386
387     if [ -n "$pathtokernelibrpm" ] && [ -f "$pathtokernelibrpm" ]; then
388         rpm2cpio < $pathtokernelibrpm | cpio -idc
389         [ ${PIPESTATUS[0]} -eq 0 -o ${PIPESTATUS[1]} -eq 0 ] || return 255
390         CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
391     fi
392
393     local smptype=
394     if pushd usr/src/linux-*-obj/${TARGET_ARCH}; then
395         local smptypes="$SMPTYPES"
396         [ "$RPMSMPTYPE" = "" ] || smptypes=$RPMSMPTYPE
397         ( ! $PATCHLESS ) && [ ! "$KERNCONFSMPTYPE" = "" ] && smptypes="$KERNCONFSMPTYPE"
398         local cursmptype=
399         for cursmptype in $smptypes; do
400             [ "$cursmptype" = "''" ] && continue
401             [ -d $cursmptype ] && smptype=$cursmptype
402             [ -d $smptype ] && break
403         done
404         popd
405     fi
406     if [ "${smptype}" = "" ]; then
407         popd
408         return 255 # cannot detect smp type
409     fi
410     if pushd usr/src/linux-*-obj/${TARGET_ARCH}/$smptype/include2; then
411         local base=$(readlink asm)
412         if [ ! -d "/${base}/" ]; then
413             rm -f asm
414             base=$(basename "$base")
415             if pushd ../../../../linux-*/include; then
416                 local lsrc=$(pwd)
417                 popd
418                 [ -d "$lsrc/${base}" ] && ln -s $lsrc/${base} asm
419             fi
420         fi
421         popd
422         read a b < <(echo $(pwd)/usr/src/linux-*)
423         if [[ $a == $(pwd)/* && $b = $(pwd)/* ]]; then
424             cp -f $a/include/linux/config.h $b/${TARGET_ARCH}/$smptype/include/linux/
425             cp $b/${TARGET_ARCH}/$smptype/.config $a/
426             [ -f "$b/${TARGET_ARCH}/$smptype/.config" ] && KERNELRPMCONFIG="$b/${TARGET_ARCH}/$smptype/.config"
427             cp $b/${TARGET_ARCH}/$smptype/.kernelrelease $a/
428             [ -f "$b/${TARGET_ARCH}/$smptype/.kernelrelease" ] && KERNELRPMRELEASE="$b/${TARGET_ARCH}/$smptype/.kernelrelease"
429             LINUX=$a
430             LINUXOBJ=$b/${TARGET_ARCH}/$smptype
431 #            local fname=$(basename $kernel_rpm)
432 #            KERNELRPMSDIR=${kernel_rpm%$fname}
433         fi
434     fi
435     popd
436     [ "$LINUX" = "" ] && return 255
437     [ -d "$LINUX" ] || return 255
438 }
439
440 #build linux kernel rpm
441 build_linux_rpm() {
442     pushd $LINUX || return 255
443     make binrpm-pkg || ( popd ; return 255 )
444     local addlnxrel=
445     [ -f ".version" ] && addlnxrel="-$(cat .version)"
446     popd
447     local arch=
448     for arch in $TARGET_ARCHS_ALL; do
449         [ -f "/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
450             KERNELRPM="/usr/src/rpm/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
451             TARGET_ARCH="$arch"
452         [ -f "/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" ] && \
453             KERNELRPM="/usr/src/packages/RPMS/$arch/kernel-$lnxmaj.${lnxrel}${addlnxrel}.$arch.rpm" && \
454             TARGET_ARCH="$arch"
455     done
456     [ "$KERNELRPM" = "" ] || return
457     return 255
458 }
459
460 #build linux kernel
461 build_linux() {
462     local nofullmake=$1
463     local nocopykernel=$2
464     pushd $LINUX || fatal 1 "Kernel source not found"
465     [ "$nofullmake" = "nofullmake" ] || make mrproper
466     [ "$nofullmake" = "nofullmake" ] || rm -f rpm-release
467 #    [ "$nocopykernel" = "copyrpmkernel" ] || rm -f localversion-*
468     [ "$nocopykernel" = "copykernel" ] && [ -f "$CONFIG_FILE" ] && cp $CONFIG_FILE .config
469     if [ "$nocopykernel" = "copyrpmkernel" ]; then
470         [ -f "$KERNELRPMCONFIG" ] && cp $KERNELRPMCONFIG .config
471         if [ -f "$KERNELRPMRELEASE" ]; then
472             cp $KERNELRPMRELEASE .
473         else
474             sed -e "s/^EXTRAVERSION\s\+=\s\+.*$/EXTRAVERSION = -${lnxrel}/" < Makefile > Makefile.new
475             [ -f "Makefile.new" ] && mv Makefile.new Makefile
476         fi
477     fi
478     [ -f ".config" ] || ( popd ; echo "Cannot find .config file"; return 255 )
479     make oldconfig || ( popd ; return 255 )
480     make include/linux/version.h
481     if [ ! "$nofullmake" = "nofullmake" ]; then
482         make || ( popd ; return 255 )
483     fi
484     popd
485     return
486 }
487
488 #build patchless lustre
489 patchless_build_sequence() {
490     if [ -f $LINUX/Makefile ]; then
491         # Get the correct kernel release - I'm unsure how this can ever
492         # work otherwise, unless you're using the exact same kernel version
493         # Lustre is shipped with.
494
495         local LINUXRELEASE=$(find_linux_release ${LINUXOBJ:-$LINUX})
496         if [ -z "$LINUXRELEASE" ]; then
497             echo "Failed to find linux release in ${LINUXOBJ:-$LINUX}"
498             RC=255
499         fi
500
501         lnxmaj=$(echo $LINUXRELEASE | cut -f1 -d-)
502         EXTRA_VERSION=$(echo $LINUXRELEASE | cut -f2 -d-)_lustre.$LUSTRE_VERSION
503         RPMSMPTYPE=" "
504         build_lustre && buildsuccess=true
505     else
506     #try to build from kernel-devel RPM (RHEL)
507     LINUX=
508     TARGET_ARCH=
509     local rpmfound=false
510     local buildsuccess=false
511     local storeforreuse=false
512     if [ "$KERNELRPMSBASE" = "" ] || [ ! -d "$KERNELRPMSBASE" ]; then
513         return 255
514     fi
515     [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
516
517     local delimiter=${EXTRA_VERSION_DELIMITER:-"-"}
518
519     # default to source type -source and special case below
520     local type=-source
521     case "$DISTRO" in
522         rhel*)
523             type=-devel
524             ;;
525     esac
526
527     local kernel_devel_rpm
528
529     if ! kernel_devel_rpm=$(find_linux_rpm "$type" "$delimiter"); then
530         fatal 1 "Could not find the kernel$type RPM in ${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
531     fi
532
533     # find_linux_rpm() used to set these globals, but now it's a function
534     # run in a subshell so it's unable to set globals in our namespace
535     TARGET_ARCH=$(rpm -q --queryformat "%{ARCH}" -p "$kernel_devel_rpm")
536     BUILD_ARCHS="$TARGET_ARCH"
537
538     unpack_linux_devel_rpm "$kernel_devel_rpm" "$delimiter" && rpmfound=true
539
540     [ -d SRPMS ] || mkdir SRPMS
541     [ -d RPMS ] || mkdir RPMS
542     [ -d BUILD ] || mkdir BUILD
543     [ -d SOURCES ] || mkdir SOURCES
544
545     # first build kernel-ib
546     if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
547         $rpmfound && build_kernel_ib
548     fi
549     ( $rpmfound ) && build_lustre && buildsuccess=true && find_linux_source_rpm
550     fi
551
552     if $buildsuccess; then
553         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELRPM" ] && \
554             cp "$KERNELRPM" RPMS/${TARGET_ARCH}/
555         [ -d "RPMS/${TARGET_ARCH}" ] && [ -f "$KERNELSOURCERPM" ] && \
556             cp "$KERNELSOURCERPM" RPMS/${TARGET_ARCH}/
557         KERNELCOMPILEDIR="$LINUX"
558         if $storeforreuse; then
559             store_for_reuse || echo "Cannot store for future reuse"
560         fi
561
562         return
563     elif ! $rpmfound; then
564         echo "COULD NOT FIND VENDOR -devel or -source RPM for $DISTRO/$TARGET_ARCH: $lnxmaj-$lnxrel in $KERNELRPMSBASE"
565         return 255
566     else
567         echo "Patchless build failed."
568         return 255
569     fi
570 }
571
572 #check timestamp value. should bi 14-digits string
573 check_timestamp() {
574     local invalue=$1
575     local timestampnodig=$(echo $invalue | sed -e s/[0-9]*//g)
576     [ "$timestampnodig" = "" ] || return 255
577     local timestamplength="${#invalue}"
578     [ $timestamplength -eq 14 ] || return 255
579 }
580
581 # get list of suitable directories with potential reused staff
582 get_reuse_dir_list() {
583     local rpmonly=$1
584     local reusedkernelmasknew=$2
585     local buildtimestamp=
586     local dirsforreuse=
587     local sourcelastdate=$(get_last_source_date)
588     for buildtimestamp in $(ls "$REUSEBUILD/" 2>&1); do
589         [ -d "$REUSEBUILD/$buildtimestamp" ] || continue
590         check_timestamp "$buildtimestamp" || continue
591         local buildtimestampstr=$(echo $buildtimestamp | \
592             sed -e "s^\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)^\1-\2-\3 \4:\5:\6 GMT^g")
593         local buildtimestampepoch=$(date --date="$buildtimestampstr"  +%s )
594         #check for suitable date
595             if ! $PATCHLESS; then
596             [ $buildtimestampepoch -ge $sourcelastdate ] || continue
597             fi
598         #check for suitable version
599         if [ "$rpmonly" = "rpmonly" ]; then
600             local reusedkernelprefix="kernel-lustre-"
601             ( $PATCHLESS ) && reusedkernelprefix=
602             local rpmmask="${reusedkernelprefix}${REUSEDKERNELMASK}"
603             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
604             [ -f $REUSEBUILD/$buildtimestamp/${rpmmask}.rpm ] && \
605                 dirsforreuse="$dirsforreuse $REUSEBUILD/$buildtimestamp"
606         else
607             local rpmmask="$REUSEDKERNELMASK"
608             [ "$reusedkernelmasknew" = "" ] || rpmmask="$reusedkernelmasknew"
609             pushd $REUSEBUILD/$buildtimestamp/linux-${rpmmask} > /dev/null 2>&1 || continue
610             local curdir=$(pwd)
611             dirsforreuse="$dirsforreuse $curdir"
612             popd
613         fi
614     done
615     echo "$dirsforreuse"
616 }
617
618 #try to reuse old RPM
619 build_sequence_rpm_reuse() {
620     local sourcerpm=$1
621     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
622     local dirsforreuse=
623     if ! [ "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse RPM
624         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
625         REUSEDKERNELMASKnew="kernel-lustre-source-${REUSEDKERNELMASKnew}"
626         local dirsforreuse="$(get_reuse_dir_list rpmonly $REUSEDKERNELMASKnew)"
627         local buildsuccess=false
628         LINUXOBJ=
629         for curdir in $(echo $dirsforreuse); do
630             [ -d "$curdir" ] || continue
631             local reusedkernelprefix="kernel-lustre-"
632             local reusedkernelrpm=
633             [ -f ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
634                 reusedkernelrpm=$(ls ${curdir}/${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
635             [ -f "$reusedkernelrpm" ] || continue
636
637             local reusedkernelsourcerpm=
638             [ -f ${curdir}/${REUSEDKERNELMASKnew}.rpm ] && \
639                 reusedkernelsourcerpm=$(ls ${curdir}/${REUSEDKERNELMASKnew}.rpm | head -1 )
640             [ -f "$reusedkernelsourcerpm" ] || continue
641
642             # don't need to check for kernel-ib RPM reuse here because sles9 is not supported
643             # by OFED >= 1.3.0 and this function appears to only be used for sles9
644
645             [ -d $TOPDIR/reused ] && rm -rf $TOPDIR/reused
646             reuse_kernel_rpm "$reusedkernelsourcerpm" "" && build_linux nofullmake copyrpmkernel && build_lustre && buildsuccess=true
647             ( $buildsuccess ) || continue
648             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
649                 [ -f "$reusedkernelrpm" ] && \
650                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
651
652                 [ -f "$reusedkernelsourcerpm" ] && \
653                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
654                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
655             fi
656             return
657         done
658     fi
659     return 255
660 }
661
662 #try to reuse old BUILD dir
663 build_sequence_reuse() {
664     local sourcerpm=$1
665     [ "$REUSERPM" = "" ] && [ "$REUSEBUILD" = "" ] && return 255
666     local dirsforreuse=
667     if [ ! "$REUSEBUILD" = "" ] && [ -d "$REUSEBUILD" ]; then #try to reuse old kernel build directory
668         local dirsforreuse="$(get_reuse_dir_list)"
669         local buildsuccess=false
670         LINUXOBJ=
671         local REUSEDKERNELMASKnew=$(echo $REUSEDKERNELMASK | sed -e "s/^[^0-9]*//")
672         for curdir in $(echo $dirsforreuse); do
673             local reusedkernelrpm=
674             local reusedkernelsourcerpm=
675             local reusedkernelibrpm=
676             [ -d "$curdir" ] || continue
677             [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" -a 
678               ! -d "${curdir%/*}/ofa_kernel" ] && continue
679             local reusedkernelprefix="kernel-lustre-"
680             ( $PATCHLESS ) && reusedkernelprefix=
681             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm ] && \
682                 reusedkernelrpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASK}.rpm | head -1 )
683             reusedkernelprefix="kernel-lustre-source-"
684             [ -f ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm ] && \
685                 reusedkernelsourcerpm=$(ls ${curdir}/../${reusedkernelprefix}${REUSEDKERNELMASKnew}.rpm | head -1 ) 
686             if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
687                 gen_lustre_version
688                 reusedkernelprefix="kernel-ib-"
689                 [ -f ${curdir}/../${reusedkernelprefix}${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm ] && \
690                     reusedkernelibrpm=$(ls ${curdir}/../${reusedkernelprefix}${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm | head -1 ) 
691                     reusedkernelibdevelrpm=$(ls ${curdir}/../${reusedkernelprefix}devel-${OFED_VERSION}-${LUSTRE_EXTRA_VERSION}.${TARGET_ARCH}.rpm | head -1 ) 
692             fi
693             if ! ( $NORPM ) && ! [ -f "$reusedkernelrpm" ]; then #kernel rpm not found. Build all
694                 continue
695             fi
696             if ! ( $NORPM ) && ! [ -f "$reusedkernelsourcerpm" ]; then #kernel source rpm not found. Build all
697                 continue
698             fi
699             if [ -n "$OFED_VERSION" ]; then
700                 if [ "$OFED_VERSION" != "inkernel" ]; then
701                     if ! ( $NORPM ) && [ ! -f "$reusedkernelibrpm" -o ! -f "$reusedkernelibdevelrpm"]; then #kernel-ib{,-devel} rpm not found. Build all
702                         continue
703                     fi
704                     CONFIGURE_FLAGS="--with-o2ib=${curdir%/*}/ofa_kernel ${CONFIGURE_FLAGS}"
705                 else
706                     CONFIGURE_FLAGS="--with-o2ib=yes ${CONFIGURE_FLAGS}"
707                 fi
708             fi
709             LINUX="$curdir"
710             build_lustre || continue
711             touch "$curdir/../"
712             buildsuccess=true
713             if ( ! $NORPM ) && ( ! $PATCHLESS ) ; then
714                 [ -f "$reusedkernelrpm" ] && \
715                     cp -f  "$reusedkernelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1 && \
716                     touch RPMS/${TARGET_ARCH}/kernel_was_reused
717                 [ -f "$reusedkernelsourcerpm" ] && \
718                     cp -f  "$reusedkernelsourcerpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
719                 [ -f "$reusedkernelibrpm" ] && \
720                     cp -f  "$reusedkernelibrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
721                     cp -f  "$reusedkernelibdevelrpm"  RPMS/${TARGET_ARCH}/ > /dev/null 2>&1
722             fi
723             return
724         done
725     fi
726     return 255
727 }
728
729 build_sequence() {
730     if $DO_SRC; then
731         unpack_linux
732         patch_linux
733         pack_linux
734         clean_linux
735     fi
736     prep_kernel_build || return 255
737     clean_lustre || return 255
738
739     build_kernel || return 255
740 }
741
742 old_school_download_kernel() {
743
744     if [ "$KERNELDIR" ] ; then
745         KERNEL_FILE="$KERNELDIR/$KERNEL"
746         if [ ! -r "$KERNEL_FILE" ] ; then
747             # see if we have an SRPM we can build a tarball for
748             KERNEL_SRPM=kernel-${lnxmaj}-${lnxrel}.src.rpm
749             if [ -r "$KERNELDIR/$KERNEL_SRPM" ] ; then
750                 build_tarball $CANONICAL_TARGET $KERNEL_SRPM
751             else
752                 if $DOWNLOAD; then
753                     echo "Downloading http://downloads.lustre.org/public/kernels/$DISTRO/old/$KERNEL..."
754                     if ! wget -nv "http://downloads.lustre.org/public/kernels/$DISTRO/old/$KERNEL" -O "$KERNELDIR/$KERNEL" ; then
755                         # see if we can do it with an SRPM from the download site
756                         download_and_build_tarball $CANONICAL_TARGET $KERNEL_FILE
757                     else
758                         [ -s "$KERNELDIR/$KERNEL" ] || {
759                             rm -rf "$KERNELDIR/$KERNEL"
760                             fatal 1 "Target $TARGET's kernel $KERNEL not found in directory $KERNELDIR."
761                         }
762                     fi
763                 else
764                     fatal 1 "Target $TARGET's kernel file $KERNEL not found in kernel directory $KERNELDIR."
765                 fi
766             fi
767         fi
768     fi
769 }