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