Whamcloud - gitweb
b=21670 revert new version of find_linux_devel_paths
[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     if ! $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 --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
547         fatal 1 "Error building kernel-ib"
548     fi
549
550     pushd "$TOPDIR" >/dev/null
551     rm -rf kernel-ib-devel
552     mkdir kernel-ib-devel
553     cd kernel-ib-devel
554     local rpm=$(ls $(lbuild_topdir)/RPMS/*/kernel-ib-devel-*-${FULL_VERSION//-/_}.*.rpm)
555     rpm2cpio -itv < $rpm | cpio -id
556     CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
557     popd >/dev/null
558 }
559
560
561 configure_lustre()
562 {
563     return 0
564     (( $BUILD_LUSTRE )) || return 0
565     pushd "$TOPDIR" >/dev/null
566     [ -f Makefile ] && make -s clean
567     [ -f configure ] || sh ./autogen.sh
568     ./configure --with-linux=$PWD/linux $CONFIGURE_FLAGS || \
569         fatal 1 "Error configuring Lustre."
570     popd >/dev/null
571 }
572
573 build_lustre()
574 {
575     (( $BUILD_LUSTRE )) || return 0
576     set_make
577     pushd "$TOPDIR" >/dev/null
578     sed \
579         -e s^@VERSION@^${LUSTRE_VERSION}^g \
580         -e s^@LINUXRELEASE@^${FULL_VERSION}^g \
581         -e s^@RELEASE@^${FULL_VERSION//-/_}^g \
582         -e s^@ac_configure_args@^"--with-linux=${PWD}/linux ${CONFIGURE_FLAGS}"^g \
583         < lustre.spec.in \
584         > lustre.spec
585     $RPMBUILD --target ${TARGET_ARCH} -bb lustre.spec \
586         ${PATCHLESS:+--define "lustre_name lustre-client"} \
587         --define "_tmppath $TMPDIR" \
588         --define "_topdir $(lbuild_topdir)" 2>&1 || \
589         fatal 1 "Error building Lustre rpms."
590     # $MAKE_J "$MAKE_CC" || fatal 1 "Error building Lustre."
591     popd >/dev/null
592 }
593
594 install_kernel()
595 {
596     (( $INSTALL_KERNEL )) || return 0
597     set_make
598     pushd "$TOPDIR/linux" >/dev/null
599     mkdir -p "$DESTDIR/boot"
600
601     install -m 644 System.map "$DESTDIR/boot/System.map-${FULL_VERSION}"
602     # install -m 644 module-info ...
603     install -m 644 "$CONFIG_FILE" "$DESTDIR/boot/config-${FULL_VERSION}"
604
605     mkdir -p "$DESTDIR/dev/shm"
606     mkdir -p "$DESTDIR/lib/modules/${FULL_VERSION}"
607
608     $MAKE "$MAKE_CC" INSTALL_MOD_PATH="$DESTDIR" KERNELRELEASE="$FULL_VERSION" \
609         -s modules_install || \
610         fatal 1 "Error installing modules."
611
612     if $XEN; then
613         cp vmlinuz "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
614         cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}"
615         ln -sf "../lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
616     else
617         case "$TARGET_ARCH" in
618             i386 | i586 | i686 | athlon)
619                 cp arch/i386/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
620                 cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
621                 ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
622                 ;;
623             x86_64)
624                 cp arch/x86_64/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
625                 cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
626                 ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
627                 ;;
628             ppc | ppc64)
629                 cp vmlinux "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
630                 ln -sf "$DESTDIR/boot/vmlinux-${FULL_VERSION}" "../lib/modules/${FULL_VERSION}/vmlinux" 
631                 ;;
632             ia64)
633                 gzip -cfv vmlinux > vmlinuz
634                 mkdir -p "$DESTDIR/boot/efi/redhat"
635                 install -m 755 vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
636                 install -m 755 vmlinuz "$DESTDIR/boot/efi/redhat/vmlinuz-${FULL_VERSION}"
637                 ln -sf "../../../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/efi/redhat/vmlinux-${FULL_VERSION}"
638                 ln -sf "efi/redhat/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
639                 ln -sf "efi/redhat/vmlinuz-${FULL_VERSION}" "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
640                 ;;
641             *)
642                 cp vmlinuz "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
643                 cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}"
644                 ln -sf "../lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
645                 ;;
646         esac
647     fi
648     if [ -e init/kerntypes.o ] ; then
649         cp init/kerntypes.o "$DESTDIR/boot/Kerntypes-${FULL_VERSION}"
650     fi
651
652     popd >/dev/null
653 }
654
655 cleanup_libmodules()
656 {
657     (( $INSTALL_LUSTRE )) || return 0
658
659     KVERREL="${VERSION}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}"
660     i="$DESTDIR/lib/modules/${FULL_VERSION}"
661
662     rm -f $i/build
663     rm -f $i/source
664
665     if (( $LINUX26 )) ; then
666         ln -sf ../../../usr/src/linux-${KVERREL}-obj/${TARGET_ARCH}/${TARGET_CONFIG} $i/build
667         ln -sf ../../../usr/src/linux-${KVERREL} $i/source
668     else
669         ln -sf ../../../usr/src/linux-${KVERREL} $i/build
670     fi
671 }
672
673 install_lustre()
674 {
675     (( $INSTALL_LUSTRE )) || return 0
676     return 0
677     set_make
678     pushd "$TOPDIR" >/dev/null
679     $MAKE "$MAKE_CC" -s install "DESTDIR=$DESTDIR" KERNELRELEASE="$FULL_VERSION" || fatal 1 "Error installing Lustre."
680     popd >/dev/null
681 }
682
683 build_kms()
684 {
685     (( $BUILD_KERNEL )) || return 0
686     (( $SUSEBUILD )) || return 0
687     set_make
688     mkdir -p "${TOPDIR}/modules-${FULL_VERSION}"
689     for dir in /usr/src/kernel-modules/* ; do
690         # we are replacing lustre-lite, so don't include it
691         if [ "${dir##*/}" != "lustre-lite" -a -e $dir/Makefile ]; then
692             build_dir="${TOPDIR}/modules-${FULL_VERSION}/${dir##*/}"
693             cp -a $dir $build_dir
694             # these modules are terrible, and don't all build
695             $MAKE_J "$MAKE_CC" -C $build_dir modules KERNEL_SOURCE="${TOPDIR}/linux"
696         fi
697     done
698 }
699
700 symver()
701 {
702     local file=$1 name=${1%.ko}
703     nm $file \
704     | sed -ne 's,^0*\([0-9a-f]\{8\}\) A __crc_\(.*\),0x\1\t\2\t'"$name"',p'
705 }
706
707 install_kms()
708 {
709     (( $INSTALL_KERNEL )) || return 0
710     (( $LINUX26 )) || return 0
711     set_make
712     for build_dir in "${TOPDIR}/modules-${FULL_VERSION}/*" ; do
713         [ -d $build_dir ] || continue
714         # these modules are terrible, and don't all build
715         $MAKE "$MAKE_CC" -C $build_dir KERNEL_SOURCE="${TOPDIR}/linux" INSTALL_MOD_PATH="$DESTDIR" 
716     done
717     local symvers_file="${DESTDIR}/boot/symvers-${VERSION}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}-${TARGET_ARCH}"
718     if [ -n "$TARGET_CONFIG" ]; then
719         symvers_file="${symvers_file}${TARGET_DELIMITER}${TARGET_CONFIG}"
720     fi
721     symvers_file="$symvers_file.gz"
722     (   symver vmlinux
723         moddir="${DESTDIR}/lib/modules/${FULL_VERSION}"
724         cd $moddir/kernel
725         for module in $(find * -name '*.ko'); do
726             symver $module
727         done
728         cd $moddir
729         for module in $(find * -path 'kernel/*' -prune -o \
730                            -name '*.ko' -print); do
731             symver $module
732         done
733     ) | sort -u -k2 | gzip -c9 > $symvers_file
734 }
735
736 save_headers()
737 {
738     (( $SAVE_HEADERS )) || return 0
739
740     echo "Saving headers for ${TARGET_CONFIG:-up} ${TARGET_ARCH}..."
741     pushd linux >/dev/null
742
743     KVERREL="${VERSION}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}"
744     # deal with the kernel headers that are version specific
745     
746     saveddir="$RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/${TARGET_ARCH}/${TARGET_CONFIG:-up}"
747     mkdir -p "$saveddir"
748     install -m 644 include/linux/autoconf.h "$saveddir/autoconf.h"
749     install -m 644 include/linux/version.h  "$saveddir/version.h"
750     mv include/linux/modules "$saveddir/"
751     echo ${TARGET_ARCH} ${TARGET_CONFIG} ../../savedheaders/${TARGET_ARCH}/${TARGET_CONFIG:-up}/ \
752         >>  "$RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/list"
753     popd >/dev/null
754 }
755
756 save_all_headers()
757 {
758     (( $SAVE_HEADERS )) || return 0
759
760     for arch in $BIGMEM_ARCHS ; do
761         save_headers bigmem $arch
762     done
763
764     for arch in $BOOT_ARCHS ; do
765         save_headers BOOT $arch
766     done
767
768     for arch in $JENSEN_ARCHS ; do
769         save_headers jensen $arch
770     done
771
772     for arch in $SMP_ARCHS ; do
773         save_headers smp $arch
774     done
775
776     for arch in $BIGSMP_ARCHS ; do
777         save_headers bigsmp $arch
778     done
779     for arch in $PSERIES64_ARCHS ; do
780         save_headers pseries64 $arch
781     done
782     for arch in $UP_ARCHS ; do
783         save_headers up $arch
784     done
785 }
786
787 longopts="build,build-lustre,build-kernel,depend-kernel,destdir:,extraversion:"
788 longopts="$longopts,help,install,install-lustre,install-kernel,kerneldir:"
789 longopts="$longopts,save-headers,target:,target-arch:,target-config:,unpack-kernel,xen"
790
791 options=$(getopt -o hj: -l "$longopts" -- "$@")
792
793 eval set -- "$options"
794     
795 while [ "$1" ] ; do
796     case "$1" in
797         '')
798             usage 1
799             ;;
800         --build)
801             BUILD_LUSTRE=1
802             BUILD_KERNEL=1
803             DEPEND_KERNEL=1
804             UNPACK_KERNEL=1
805             shift
806             ;;
807         --build-lustre)
808             BUILD_LUSTRE=1
809             shift
810             ;;
811         --build-kernel)
812             BUILD_KERNEL=1
813             DEPEND_KERNEL=1
814             shift
815             ;;
816         --depend-kernel)
817             DEPEND_KERNEL=1
818             shift
819             ;;
820         --destdir)
821             DESTDIR=$2
822             shift 2
823             ;;
824         --extraversion)
825             EXTRA_VERSION=$2
826             shift 2
827             ;;
828         --help | -h)
829             usage 0
830             ;;
831         --install)
832             INSTALL_LUSTRE=1
833             INSTALL_KERNEL=1
834             shift
835             ;;
836         --install-lustre)
837             INSTALL_LUSTRE=1
838             shift
839             ;;
840         --install-kernel)
841             INSTALL_KERNEL=1
842             shift
843             ;;
844         -j)
845             JOBS=$2
846             shift 2
847             ;;
848         --kerneldir)
849             KERNELDIR=$2
850             shift 2
851             ;;
852         --save-headers)
853             SAVE_HEADERS=1
854             shift
855             ;;
856         --target)
857             TARGET=$2
858             shift 2
859             ;;
860         --target-arch)
861             TARGET_ARCH=$2
862             shift 2
863             ;;
864         --target-config)
865             TARGET_CONFIG=$2
866             shift 2
867             ;;
868         --unpack-kernel)
869             UNPACK_KERNEL=1
870             shift
871             ;;
872         --xen)
873             XEN=true
874             shift
875             ;;
876         --)
877             shift
878             CONFIGURE_FLAGS=$@
879             break
880             ;; 
881         *)
882             usage 1 "Unrecognized option: $1"
883             ;;
884     esac
885 done
886
887 check_options
888 load_target
889 EXTRA_VERSION_DELIMITER=${EXTRA_VERSION_DELIMITER:-"-"}
890 FULL_VERSION="${VERSION}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}"
891 if [ -n "$TARGET_CONFIG" ]; then
892     FULL_VERSION="${FULL_VERSION}${TARGET_DELIMITER}${TARGET_CONFIG}"
893 fi
894 setup_ccache_distcc
895
896 extract_kernel
897 patch_kernel
898
899 depend_kernel
900 build_kernel
901
902 if [ -n "$OFED_VERSION" ]; then
903     if [ "$OFED_VERSION" = "inkernel" ]; then
904         CONFIGURE_FLAGS="--with-o2ib=yes ${CONFIGURE_FLAGS}"
905     else
906         build_kernel_ib
907     fi
908 fi
909
910 configure_lustre
911 build_lustre
912
913 build_kms
914
915 install_kernel
916 install_lustre
917
918 install_kms
919
920 cleanup_libmodules
921
922 save_headers
923
924 exit 0