Whamcloud - gitweb
Branch b1_6
[fs/lustre-release.git] / build / lmake
1 #!/bin/sh
2
3 # option variables
4 DESTDIR=
5 KERNELDIR=
6 TARGET=
7 # Not sure what to put here
8 # TARGET_ARCH=$(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)
9 TARGET_ARCH=
10 TARGET_CONFIG=
11 JOBS=1
12 CONFIGURE_FLAGS=
13 TMPDIR=${TMPDIR:-"/var/tmp"}
14
15 # commands to run
16 BUILD_LUSTRE=0
17 BUILD_KERNEL=0
18 DEPEND_KERNEL=0
19 INSTALL_LUSTRE=0
20 INSTALL_KERNEL=0
21 SAVE_HEADERS=0
22 UNPACK_KERNEL=0
23
24 # provided by target file
25 KERNEL=
26 SERIES=
27 CONFIG=
28 VERSION=
29 EXTRA_VERSION=
30
31 BASE_ARCHS=
32 BIGMEM_ARCHS=
33 BOOT_ARCHS=
34 JENSEN_ARCHS=
35 SMP_ARCHS=
36 BIGSMP_ARCHS=
37 PSERIES64_ARCHS=
38 UP_ARCHS=
39
40 RHBUILD=0
41 SUSEBUILD=0
42
43 # flat-out globals
44 TOPDIR=
45 TARGET_FILE=
46 KERNEL_FILE=
47 SERIES_FILE=
48 CONFIG_FILE=
49 RPMBUILD=
50
51 canon()
52 {
53     pushd $1 >/dev/null
54     echo $PWD
55     popd >/dev/null
56 }
57 TOPDIR="${0%%${0##*/}}"
58 if [ "${TOPDIR}" ] ; then
59     TOPDIR=$(canon "${TOPDIR}/..")
60 else
61     TOPDIR=$(canon "..")
62 fi
63
64 lbuild_topdir()
65 {
66     retdir=$TOPDIR
67     while [ ! -d $retdir/BUILD ] ; do
68         retdir=$(canon "$retdir/..")
69         if [ "$retdir" = "/" ] ; then
70             break;
71         fi
72     done
73     echo "$retdir"
74 }
75
76 cleanup()
77 {
78     true
79 }
80
81 fatal()
82 {
83     cleanup
84     [ "$2" ] && echo
85     [ "$2" ] && echo "${0##*/}: $2"
86     exit $1
87 }
88
89 list_targets()
90 {
91     echo -n "Available targets:"
92     for target in $TOPDIR/lustre/kernel_patches/targets/*.target ; do
93         target_file=${target##*/}
94         echo -n " ${target_file%%.target}"
95     done
96     echo
97 }
98
99
100 usage()
101 {
102     cat <<EOF
103 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
104
105 Options:
106
107   --build
108     same as --build-kernel --build-lustre --unpack-kernel
109
110   --build-lustre
111     configure and compile lustre.  Requires that --build-kernel was
112     already run.
113
114   --build-kernel
115     configure and compile a kernel.  Implies --depend-kernel.
116     Requires that --unpack-kernel was already run.
117
118   --depend-kernel)
119     Prepares a kernel tree for building (similar to make mrproper
120     oldconfig dep).  Requires that --unpack-kernel was already run.
121
122   --destdir=DESTDIR
123     Root directory to install into (like DESTDIR with auto*).
124
125   --extraversion=EXTRAVERSION
126     Overrides the target kernel\'s EXTRAVERSION text.
127
128   -h, --help
129     Display this message.
130
131   --install
132     same as --install-kernel --install-lustre
133
134   --install-lustre
135     run make install in the Lustre tree.
136
137   --install-kernel
138     install the kernel image and modules.
139
140   -j jobs
141     This works just like the -j option to make, and is passed to make
142     when building.
143
144   --kerneldir=KERNELDIR
145     Directory containing linux source tarballs.
146
147   --target=TARGET
148     Name of the configuration to use.  The available targets are
149     listed below.
150
151   --target-arch=ARCH
152     Specifies an architecture to use when choosing a kernel config
153     file.  Default is i386.
154
155   --target-config=CONFIG
156     Specifies a special option (such as smp, bigsmp, bigmem, or BOOT)
157     to use when choosing a kernel config file.  This also modifies the
158     kernel version and modules directory.
159
160   --unpack-kernel
161     Untars and patches the kernel source.
162
163   The order that commands (--build-lustre, --unpack-kernel) are
164   specified on the command line is ignored; ${0##*/} will always
165   execute them in the correct order (unpack, then build, then install
166   etc.).
167
168 EOF
169     list_targets
170
171     fatal "$1" "$2"
172 }
173
174 check_options()
175 {
176     (( $BUILD_LUSTRE || $BUILD_KERNEL || $DEPEND_KERNEL || \
177             $INSTALL_LUSTRE || $INSTALL_KERNEL || $SAVE_HEADERS || \
178             $UNPACK_KERNEL )) || \
179                 fatal 1 "No commands specified."
180
181     if (( $UNPACK_KERNEL )) ; then
182         [ "$KERNELDIR" ] || \
183             fatal 1 "A kernel directory must be specified with --kerneldir."
184         [ -d "$KERNELDIR" ] || \
185             fatal 1 "$KERNELDIR is not a directory."
186     fi
187
188     if (( $INSTALL_LUSTRE || $INSTALL_KERNEL || $SAVE_HEADERS )) ; then
189         [ -z "$DESTDIR" -o -d "$DESTDIR" ] || \
190             fatal 1 "$DESTDIR is not a directory."
191     fi
192
193     [ "$TARGET" ] || usage 1 "A target must be specified with --target."
194     TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target"
195     [ -r "$TARGET_FILE" ] || \
196         fatal 1 "Target '$TARGET' was not found.  Try --list-targets."
197
198     if [ -z "$JOBS" -o "$JOBS" -lt "1" ] ; then
199         JOBS=1
200     fi
201
202     RPMBUILD=$(which rpmbuild 2>/dev/null | head -1)
203     if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
204         RPMBUILD=$(which rpm 2>/dev/null | head -1)
205         if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
206             usage 1 "Could not find binary for making rpms (tried rpmbuild and rpm)."
207         fi
208     fi
209 }
210
211 get_lustre_version()
212 {
213     for series in $SERIES ; do
214         SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$series"
215         lustre_patch=$(grep lustre_version "$SERIES_FILE" 2>/dev/null)
216         [ "$lustre_patch" ] && break
217     done
218     [ "$lustre_patch" ] || \
219         fatal 1 "Could not determine Lustre version from $SERIES series."
220
221     awk '/^\+#define LUSTRE_KERNEL_VERSION /{ print $3 }' \
222         "$TOPDIR/lustre/kernel_patches/patches/$lustre_patch" 2>/dev/null
223 }
224
225 load_target()
226 {
227     EXTRA_VERSION_save="$EXTRA_VERSION"
228
229     . "$TARGET_FILE"
230
231     [ "$KERNEL" ] || fatal 1 "Target $TARGET did not specify a kernel."
232 # Suse 2.6 has our patches in already
233 #    [ "$SERIES" ] || fatal 1 "Target $TARGET did not specify a patch series."
234 #    [ "$CONFIG" ] || fatal 1 "Target $TARGET did not specify a kernel config."
235     [ "$VERSION" ] || fatal 1 "Target $TARGET did not specify the kernel version."
236     
237     if [ "$KERNELDIR" ] ; then
238         KERNEL_FILE="$KERNELDIR/$KERNEL"
239         [ -r "$KERNELDIR/$KERNEL" ] || \
240             fatal 1 "Target $TARGET's kernel file $KERNEL not found in kernel directory $KERNELDIR."
241     fi
242
243     if [ "$SERIES" ] ; then
244         for series in $SERIES ; do
245             SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$series"
246             [ -r "$SERIES_FILE" ] || \
247                 fatal 1 "Target $TARGET's series $SERIES missing from $TOPDIR/lustre/kernel_patches/series."
248         done
249     fi
250
251     TARGET_ARCH=${TARGET_ARCH:-$BASE_ARCHS}
252     CONFIG_TARGET="$TARGET-${TARGET_ARCH}${TARGET_CONFIG:+-$TARGET_CONFIG}"
253     CONFIG_FILE="$TOPDIR/lustre/kernel_patches/kernel_configs/kernel-$VERSION-$CONFIG_TARGET.config"
254     [ -r "$CONFIG_FILE" ] ||
255         fatal 1 "Target $TARGET's config file $CONFIG_FILE missing from $TOPDIR/lustre/kernel_patches/configs."
256
257     if [ "$EXTRA_VERSION_save" ] ; then
258         EXTRA_VERSION="$EXTRA_VERSION_save"
259     else
260         EXTRA_VERSION="${EXTRA_VERSION}_lustre.$(get_lustre_version)"
261     fi
262 }
263
264 # do these after load_target(), which maybe export CC
265 setup_ccache_distcc()
266 {
267     # distcc can't handle ".incbin"
268     if [ "$TARGET" == "2.6-suse" -o "$TARGET" == "2.6-rhel4" ]; then
269         if [ "$TARGET_ARCH" == "x86_64" ]; then
270             unset DISTCC
271         fi
272     fi
273     
274     CC=${CC:-gcc}
275     if [ "$CCACHE" ]; then
276         CC="$CCACHE $CC"
277         [ "$DISTCC" ] && export CCACHE_PREFIX="$DISTCC"
278     else
279         [ "$DISTCC" ] && CC="$DISTCC $CC"
280     fi
281 }
282
283 tarflags()
284 {
285     case "$1" in
286         '')
287             fatal 1 "tarflags(): File name argument missing."
288             ;;
289         *.tar.gz | *.tgz)
290             echo 'zxf'
291             ;;
292         *.tar.bz2)
293             echo 'jxf'
294             ;;
295         *.tar)
296             echo 'xf'
297             ;;
298         *)
299             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
300             ;;
301     esac
302 }
303
304 untar()
305 {
306     echo "Untarring ${1##*/}..."
307     tar $(tarflags $1) $1
308 }
309
310
311 extract_kernel()
312 {
313     (( $UNPACK_KERNEL )) || return 0
314     pushd "$TOPDIR" >/dev/null
315     if [ -d linux ] ; then
316         [ -L linux ] && rm -rf $(readlink linux)
317         rm -rf linux
318     fi
319     untar "$KERNEL_FILE"
320     [ -d linux ] || ln -sf linux* linux
321     popd >/dev/null
322 }
323
324 patch_kernel()
325 {
326     (( $UNPACK_KERNEL )) || return 0
327     [ "$SERIES" ] || return 0
328     pushd "$TOPDIR/linux" >/dev/null
329     for series in $SERIES ; do
330         echo -n "Applying series $series:"
331         SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$series"
332         for patch in $(<"$SERIES_FILE") ; do
333             PATCH_FILE="$TOPDIR/lustre/kernel_patches/patches/$patch"
334             [ -r "$PATCH_FILE" ] || \
335                 fatal 1 "Patch file not found: $patch"
336             echo -n " $patch"
337             patch -s -p1 < "$PATCH_FILE" || fatal 1 "Error applying patch $patch."
338         done
339         echo
340     done
341     popd >/dev/null
342 }
343
344 set_make()
345 {
346     MAKE="make -s"
347     [ "$CC" ] && {
348         if [ "$TARGET_ARCH" == "ppc64" ] ; then
349                 MAKE_CC="CC=$CC -m64"
350         else 
351                 MAKE_CC="CC=$CC"
352         fi
353     }
354     if [ "$ARCH" ] ; then
355         MAKE_ARCH="$MAKE ARCH=$ARCH"
356     else
357         case $TARGET_ARCH in
358             i?86)
359                 ;;
360             *)
361                 MAKE_ARCH="$MAKE ARCH=$TARGET_ARCH"
362                 ;;
363         esac
364     fi
365     MAKE_J="$MAKE -j $JOBS"
366 }
367
368 timed_run() {
369     SLEEP_TIME=$1
370     shift
371
372     set -o monitor
373
374     #bash -c "$@" &
375     ("$@") &
376     child_pid=$!
377     
378     (sleep $SLEEP_TIME
379     kill -TERM -$child_pid 2>/dev/null
380     sleep 5
381     kill -KILL -$child_pid 2>/dev/null
382     echo "$1 was killed due to timeout") &
383     dog_pid=$!
384
385     wait $child_pid
386     # status will be set to 143 if the process had to be killed due to timeout
387     status=${PIPESTATUS[0]}
388     kill -KILL -$dog_pid
389     return $status
390 }
391
392 depend_kernel()
393 {
394     (( $DEPEND_KERNEL )) || return 0
395     # we need to override $CC at make time, since there is no
396     # configure
397     set_make
398     pushd "$TOPDIR/linux" >/dev/null
399     echo "Overriding EXTRAVERSION in kernel..."
400     local extra_version="${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}"
401     if [ -n "${TARGET_CONFIG}" ]; then
402         extra_version="${extra_version}${TARGET_DELIMITER}${TARGET_CONFIG}"
403     fi
404     perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = ${extra_version}/" Makefile
405     echo "Making depend in $PWD..."
406     $MAKE "$MAKE_CC" mrproper || fatal 1 "Error running make mrproper"
407     rm -f rpm-release
408     # remove localversion-* files to avoid kernel release string 
409     # srewing up by the top-level Makefile
410     rm -f localversion-*
411     cp "$CONFIG_FILE" .config
412     local UPDATE_OLDCONFIG=
413     for oc in oldconfig_nonint silentoldconfig oldconfig ; do                   
414         if grep -q "$oc" Makefile ; then                                        
415             timed_run 300 $MAKE "$MAKE_CC" $oc || UPDATE_OLDCONFIG=1
416             break
417         fi
418     done
419
420     if [ "$UPDATE_OLDCONFIG" ] ; then
421         # use the expect script to "make oldconfig" and answer the questions for
422         # new items conservatively.  QA will get notified on anything newly added
423         # for them to review and adjust accordingly.
424         local logfile=$(mktemp /tmp/XXXXXX)
425         #timed_run 300 $TOPDIR/build/update_oldconfig $logfile
426         #local RC=${PIPESTATUS[0]}
427         #local RC=$(strace -f -o update_oldconfig.strace bash -c "$TOPDIR/build/update_oldconfig $logfile; echo \$?")
428         $TOPDIR/build/update_oldconfig $logfile
429         local RC=${PIPESTATUS[0]}
430         #$TOPDIR/build/update_oldconfig $logfile
431         #local RC=${PIPESTATUS[0]}
432         if [ $RC -eq 143 ]; then
433             fatal 1 "update_oldconfig timed out"
434         elif [ $RC -ne 0 ]; then
435             # dump the log
436             cat $logfile
437             rm -f $logfile
438             if [ -f update_oldconfig.strace ]; then
439                 cat update_oldconfig.strace
440                 rm -f update_oldconfig.strace
441             fi
442             fatal 1 "update_oldconfig failed: $RC. See log above."
443         fi
444     fi
445     rm -f $logfile
446     # now notify if resulting .config is different than $CONFIG_FILE
447     local tmpfile=$(mktemp /tmp/XXXXXX)
448     diff -I '^#.*' -u "$CONFIG_FILE" .config >$tmpfile
449     if [ -s $tmpfile ]; then
450         { cat <<EOF
451 To: lustre-qa-team@sun.com
452 Subject: kernel_config change 
453
454 The result of a make oldconfig on file $CONFIG_FILE resulted in a
455 difference when compared to .config in the following way:
456
457 EOF
458         cat $tmpfile
459         echo -e "\n\nPlease consider updating $CONFIG_FILE for version: $extra_version."
460         # not sure these are entirely useful.  the above and "patch" are good
461         #echo -e "\nThe entire new .config file:\n"
462         #cat .config
463         # sadly, the build roots can't e-mail out, so we can only display this
464         # to stderr for an interested party to inspect
465         #} | sendmail -flustre-qa-team@sun.com -t
466         } >&2
467     fi
468     rm -f $tmpfile
469
470     case "$VERSION" in
471         2.6*)
472             $MAKE "$MAKE_CC" include/asm
473             ;;
474         2.4*)
475             $MAKE "$MAKE_CC" symlinks
476             $MAKE "$MAKE_CC" dep || fatal 1 "Error running make dep"
477             ;;
478     esac
479     $MAKE "$MAKE_CC" include/linux/version.h || fatal 1 "Error making include/linux/version.h"
480 }
481
482 build_kernel()
483 {
484     (( $BUILD_KERNEL )) || return 0
485     set_make
486     echo "Building kernel in $PWD..."
487     case "$TARGET_ARCH" in
488         i386 | i586 | i686 | athlon | x86_64)
489             $MAKE_J "$MAKE_CC" bzImage || fatal 1 "Error making bzImage."
490             ;;
491     ia64 | ppc | ppc64)
492             $MAKE_J "$MAKE_CC" vmlinux || fatal 1 "Error making vmlinux."
493             ;;
494         *)
495             $MAKE_J "$MAKE_CC" boot || fatal 1 "Error making boot."
496             ;;
497     esac
498     $MAKE_J "$MAKE_CC" modules || fatal 1 "Error building modules."
499
500     popd >/dev/null
501 }
502
503 build_kernel_ib()
504 {
505     (( $BUILD_KERNEL )) || return 0
506     # build kernel-ib{,-devel}
507     # some I/B drivers are architecture dependent and kernel-ib's configure
508     # does not figure it out for us ~sigh~
509     local configure_options=""
510     case "$TARGET_ARCH" in
511         x86_64 | ia64)
512             configure_options="--with-ipath_inf-mod"
513             ;;
514         ppc64)
515             configure_options="--with-ipath_inf-mod --with-ehca-mod"
516             ;;
517     esac
518     local K_SRC="K_SRC"
519     # ofed 1.3 had a bug in the rpm spec
520     if [ "$OFED_VERSION" = "1.3" ]; then
521         K_SRC="KSRC"
522     fi
523     $RPMBUILD --rebuild --define 'build_kernel_ib 1' --define 'build_kernel_ib_devel 1' \
524               --define "_topdir $(lbuild_topdir)" --target ${TARGET_ARCH} \
525               --define "KVERSION ${FULL_VERSION}" \
526               --define "$K_SRC ${PWD}/linux" \
527               --define "LIB_MOD_DIR /lib/modules/${FULL_VERSION}" \
528               --define "configure_options --without-quilt --with-core-mod --with-user_mad-mod --with-user_access-mod --with-addr_trans-mod --with-srp-target-mod --with-core-mod --with-mthca-mod --with-mlx4-mod --with-cxgb3-mod --with-nes-mod --with-ipoib-mod --with-sdp-mod --with-srp-mod --without-srp-target-mod --with-rds-mod --with-iser-mod --with-qlgc_vnic-mod --with-madeye-mod $configure_options" $(lbuild_topdir)/OFED/SRPMS/ofa_kernel-${OFED_VERSION}-ofed${OFED_VERSION}.src.rpm
529
530     if [ ${PIPESTATUS[0]} != 0 ]; then
531         fatal 1 "Error building kernel-ib"
532     fi
533
534     pushd "$TOPDIR" >/dev/null
535     rm -rf kernel-ib-devel
536     mkdir kernel-ib-devel
537     cd kernel-ib-devel
538     local rpm=$(ls $(lbuild_topdir)/RPMS/*/kernel-ib-devel-${OFED_VERSION}-${FULL_VERSION//-/_}.*.rpm)
539     rpm2cpio -itv < $rpm | cpio -id
540     CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
541     popd >/dev/null
542 }
543
544
545 configure_lustre()
546 {
547     return 0
548     (( $BUILD_LUSTRE )) || return 0
549     pushd "$TOPDIR" >/dev/null
550     [ -f Makefile ] && make -s clean
551     [ -f configure ] || sh ./autogen.sh
552     ./configure --with-linux=$PWD/linux $CONFIGURE_FLAGS || \
553         fatal 1 "Error configuring Lustre."
554     popd >/dev/null
555 }
556
557 build_lustre()
558 {
559     (( $BUILD_LUSTRE )) || return 0
560     set_make
561     pushd "$TOPDIR" >/dev/null
562     sed \
563         -e s^@VERSION@^${LUSTRE_VERSION}^g \
564         -e s^@LINUXRELEASE@^${FULL_VERSION}^g \
565         -e s^@RELEASE@^${FULL_VERSION//-/_}^g \
566         -e s^@ac_configure_args@^"--with-linux=${PWD}/linux ${CONFIGURE_FLAGS}"^g \
567         < lustre.spec.in \
568         > lustre.spec
569     $RPMBUILD --target ${TARGET_ARCH} -bb lustre.spec \
570         --define "_tmppath $TMPDIR" \
571         --define "_topdir $(lbuild_topdir)" || \
572         fatal 1 "Error building Lustre rpms."
573     # $MAKE_J "$MAKE_CC" || fatal 1 "Error building Lustre."
574     popd >/dev/null
575 }
576
577 install_kernel()
578 {
579     (( $INSTALL_KERNEL )) || return 0
580     set_make
581     pushd "$TOPDIR/linux" >/dev/null
582     mkdir -p "$DESTDIR/boot"
583
584     install -m 644 System.map "$DESTDIR/boot/System.map-${FULL_VERSION}"
585     # install -m 644 module-info ...
586     install -m 644 "$CONFIG_FILE" "$DESTDIR/boot/config-${FULL_VERSION}"
587
588     mkdir -p "$DESTDIR/dev/shm"
589     mkdir -p "$DESTDIR/lib/modules/${FULL_VERSION}"
590
591     $MAKE "$MAKE_CC" INSTALL_MOD_PATH="$DESTDIR" KERNELRELEASE="$FULL_VERSION" \
592         -s modules_install || \
593         fatal 1 "Error installing modules."
594
595     case "$TARGET_ARCH" in
596         i386 | i586 | i686 | athlon)
597             cp arch/i386/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
598             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
599             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
600             ;;
601         x86_64)
602             cp arch/x86_64/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
603             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
604             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
605             ;;
606         ppc | ppc64)
607             cp vmlinux "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
608             ln -sf "$DESTDIR/boot/vmlinux-${FULL_VERSION}" "../lib/modules/${FULL_VERSION}/vmlinux" 
609             ;;
610         ia64)
611             gzip -cfv vmlinux > vmlinuz
612             mkdir -p "$DESTDIR/boot/efi/redhat"
613             install -m 755 vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
614             install -m 755 vmlinuz "$DESTDIR/boot/efi/redhat/vmlinuz-${FULL_VERSION}"
615             ln -sf "../../../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/efi/redhat/vmlinux-${FULL_VERSION}"
616             ln -sf "efi/redhat/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
617             ln -sf "efi/redhat/vmlinuz-${FULL_VERSION}" "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
618             ;;
619         *)
620             cp vmlinuz "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
621             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}"
622             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
623
624             ;;
625     esac
626     if [ -e init/kerntypes.o ] ; then
627         cp init/kerntypes.o "$DESTDIR/boot/Kerntypes-${FULL_VERSION}"
628     fi
629
630     popd >/dev/null
631 }
632
633 cleanup_libmodules()
634 {
635     (( $INSTALL_LUSTRE )) || return 0
636
637     KVERREL="${VERSION}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}"
638     i="$DESTDIR/lib/modules/${FULL_VERSION}"
639
640     rm -f $i/build
641     rm -f $i/source
642
643     if (( $LINUX26 )) ; then
644         ln -sf ../../../usr/src/linux-${KVERREL}-obj/${TARGET_ARCH}/${TARGET_CONFIG} $i/build
645         ln -sf ../../../usr/src/linux-${KVERREL} $i/source
646     else
647         ln -sf ../../../usr/src/linux-${KVERREL} $i/build
648     fi
649 }
650
651 install_lustre()
652 {
653     (( $INSTALL_LUSTRE )) || return 0
654     return 0
655     set_make
656     pushd "$TOPDIR" >/dev/null
657     $MAKE "$MAKE_CC" -s install "DESTDIR=$DESTDIR" KERNELRELEASE="$FULL_VERSION" || fatal 1 "Error installing Lustre."
658     popd >/dev/null
659 }
660
661 build_kms()
662 {
663     (( $BUILD_KERNEL )) || return 0
664     (( $SUSEBUILD )) || return 0
665     set_make
666     mkdir -p "${TOPDIR}/modules-${FULL_VERSION}"
667     for dir in /usr/src/kernel-modules/* ; do
668         # we are replacing lustre-lite, so don't include it
669         if [ "${dir##*/}" != "lustre-lite" -a -e $dir/Makefile ]; then
670             build_dir="${TOPDIR}/modules-${FULL_VERSION}/${dir##*/}"
671             cp -a $dir $build_dir
672             # these modules are terrible, and don't all build
673             $MAKE_J "$MAKE_CC" -C $build_dir modules KERNEL_SOURCE="${TOPDIR}/linux"
674         fi
675     done
676 }
677
678 symver()
679 {
680     local file=$1 name=${1%.ko}
681     nm $file \
682     | sed -ne 's,^0*\([0-9a-f]\{8\}\) A __crc_\(.*\),0x\1\t\2\t'"$name"',p'
683 }
684
685 install_kms()
686 {
687     (( $INSTALL_KERNEL )) || return 0
688     (( $LINUX26 )) || return 0
689     set_make
690     for build_dir in "${TOPDIR}/modules-${FULL_VERSION}/*" ; do
691         [ -d $build_dir ] || continue
692         # these modules are terrible, and don't all build
693         $MAKE "$MAKE_CC" -C $build_dir KERNEL_SOURCE="${TOPDIR}/linux" INSTALL_MOD_PATH="$DESTDIR" 
694     done
695     local symvers_file="${DESTDIR}/boot/symvers-${VERSION}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}-${TARGET_ARCH}"
696     if [ -n "$TARGET_CONFIG" ]; then
697         symvers_file="${symvers_file}${TARGET_DELIMITER}${TARGET_CONFIG}"
698     fi
699     symvers_file="$symvers_file.gz"
700     (   symver vmlinux
701         moddir="${DESTDIR}/lib/modules/${FULL_VERSION}"
702         cd $moddir/kernel
703         for module in $(find * -name '*.ko'); do
704             symver $module
705         done
706         cd $moddir
707         for module in $(find * -path 'kernel/*' -prune -o \
708                            -name '*.ko' -print); do
709             symver $module
710         done
711     ) | sort -u -k2 | gzip -c9 > $symvers_file
712 }
713
714 save_headers()
715 {
716     (( $SAVE_HEADERS )) || return 0
717
718     echo "Saving headers for ${TARGET_CONFIG:-up} ${TARGET_ARCH}..."
719     pushd linux >/dev/null
720
721     KVERREL="${VERSION}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}"
722     # deal with the kernel headers that are version specific
723     
724     saveddir="$RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/${TARGET_ARCH}/${TARGET_CONFIG:-up}"
725     mkdir -p "$saveddir"
726     install -m 644 include/linux/autoconf.h "$saveddir/autoconf.h"
727     install -m 644 include/linux/version.h  "$saveddir/version.h"
728     mv include/linux/modules "$saveddir/"
729     echo ${TARGET_ARCH} ${TARGET_CONFIG} ../../savedheaders/${TARGET_ARCH}/${TARGET_CONFIG:-up}/ \
730         >>  "$RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/list"
731     popd >/dev/null
732 }
733
734 save_all_headers()
735 {
736     (( $SAVE_HEADERS )) || return 0
737
738     for arch in $BIGMEM_ARCHS ; do
739         save_headers bigmem $arch
740     done
741
742     for arch in $BOOT_ARCHS ; do
743         save_headers BOOT $arch
744     done
745
746     for arch in $JENSEN_ARCHS ; do
747         save_headers jensen $arch
748     done
749
750     for arch in $SMP_ARCHS ; do
751         save_headers smp $arch
752     done
753
754     for arch in $BIGSMP_ARCHS ; do
755         save_headers bigsmp $arch
756     done
757     for arch in $PSERIES64_ARCHS ; do
758         save_headers pseries64 $arch
759     done
760     for arch in $UP_ARCHS ; do
761         save_headers up $arch
762     done
763 }
764
765 longopts="build,build-lustre,build-kernel,depend-kernel,destdir:,extraversion:"
766 longopts="$longopts,help,install,install-lustre,install-kernel,kerneldir:"
767 longopts="$longopts,save-headers,target:,target-arch:,target-config:,unpack-kernel"
768
769 options=$(getopt -o hj: -l "$longopts" -- "$@")
770
771 eval set -- "$options"
772     
773 while [ "$1" ] ; do
774     case "$1" in
775         '')
776             usage 1
777             ;;
778         --build)
779             BUILD_LUSTRE=1
780             BUILD_KERNEL=1
781             DEPEND_KERNEL=1
782             UNPACK_KERNEL=1
783             shift
784             ;;
785         --build-lustre)
786             BUILD_LUSTRE=1
787             shift
788             ;;
789         --build-kernel)
790             BUILD_KERNEL=1
791             DEPEND_KERNEL=1
792             shift
793             ;;
794         --depend-kernel)
795             DEPEND_KERNEL=1
796             shift
797             ;;
798         --destdir)
799             DESTDIR=$2
800             shift 2
801             ;;
802         --extraversion)
803             EXTRA_VERSION=$2
804             shift 2
805             ;;
806         --help | -h)
807             usage 0
808             ;;
809         --install)
810             INSTALL_LUSTRE=1
811             INSTALL_KERNEL=1
812             shift
813             ;;
814         --install-lustre)
815             INSTALL_LUSTRE=1
816             shift
817             ;;
818         --install-kernel)
819             INSTALL_KERNEL=1
820             shift
821             ;;
822         -j)
823             JOBS=$2
824             shift 2
825             ;;
826         --kerneldir)
827             KERNELDIR=$2
828             shift 2
829             ;;
830         --save-headers)
831             SAVE_HEADERS=1
832             shift
833             ;;
834         --target)
835             TARGET=$2
836             shift 2
837             ;;
838         --target-arch)
839             TARGET_ARCH=$2
840             shift 2
841             ;;
842         --target-config)
843             TARGET_CONFIG=$2
844             shift 2
845             ;;
846         --unpack-kernel)
847             UNPACK_KERNEL=1
848             shift
849             ;;
850         --)
851             shift
852             CONFIGURE_FLAGS=$@
853             break
854             ;; 
855         *)
856             usage 1 "Unrecognized option: $1"
857             ;;
858     esac
859 done
860
861 check_options
862 load_target
863 EXTRA_VERSION_DELIMITER=${EXTRA_VERSION_DELIMITER:-"-"}
864 FULL_VERSION="${VERSION}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}"
865 if [ -n "$TARGET_CONFIG" ]; then
866     FULL_VERSION="${FULL_VERSION}${TARGET_DELIMITER}${TARGET_CONFIG}"
867 fi
868 setup_ccache_distcc
869
870 extract_kernel
871 patch_kernel
872
873 depend_kernel
874 build_kernel
875
876 if [ -n "$OFED_VERSION" ]; then
877     if [ "$OFED_VERSION" = "inkernel" ]; then
878         CONFIGURE_FLAGS="--with-o2ib=yes ${CONFIGURE_FLAGS}"
879     else
880     build_kernel_ib
881     fi
882 fi
883
884 configure_lustre
885 build_lustre
886
887 build_kms
888
889 install_kernel
890 install_lustre
891
892 install_kms
893
894 cleanup_libmodules
895
896 save_headers
897
898 exit 0