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