Whamcloud - gitweb
- merge 2 weeks of b1_4 fixes onto HEAD
[fs/lustre-release.git] / lustre / scripts / 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
14 # commands to run
15 BUILD_LUSTRE=0
16 BUILD_KERNEL=0
17 DEPEND_KERNEL=0
18 INSTALL_LUSTRE=0
19 INSTALL_KERNEL=0
20 SAVE_HEADERS=0
21 UNPACK_KERNEL=0
22
23 # provided by target file
24 KERNEL=
25 SERIES=
26 CONFIG=
27 VERSION=
28 EXTRA_VERSION=
29
30 BASE_ARCH=
31 BIGMEM_ARCHS=
32 BOOT_ARCHS=
33 JENSEN_ARCHS=
34 SMP_ARCHS=
35 UP_ARCHS=
36
37 # flat-out globals
38 TOPDIR=
39 TARGET_FILE=
40 KERNEL_FILE=
41 SERIES_FILE=
42 CONFIG_FILE=
43 CC=${CC:-gcc}
44
45 canon()
46 {
47     pushd $1 >/dev/null
48     echo $PWD
49     popd >/dev/null
50 }
51 TOPDIR=$(canon "${0%%${0##*/}}/..")
52
53 cleanup()
54 {
55     true
56 }
57
58 fatal()
59 {
60     cleanup
61     [ "$2" ] && echo
62     [ "$2" ] && echo "${0##*/}: $2"
63     exit $1
64 }
65
66 list_targets()
67 {
68     echo -n "Available targets:"
69     for target in $TOPDIR/kernel_patches/targets/*.target ; do
70         target_file=${target##*/}
71         echo -n " ${target_file%%.target}"
72     done
73     echo
74 }
75
76
77 usage()
78 {
79     cat <<EOF
80 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
81
82 Options:
83
84   --build
85     same as --build-kernel --build-lustre --unpack-kernel
86
87   --build-lustre
88     configure and compile lustre.  Requires that --build-kernel was
89     already run.
90
91   --build-kernel
92     configure and compile a kernel.  Implies --depend-kernel.
93     Requires that --unpack-kernel was already run.
94
95   --depend-kernel)
96     Prepares a kernel tree for building (similar to make mrproper
97     oldconfig dep).  Requires that --unpack-kernel was already run.
98
99   --destdir=DESTDIR
100     Root directory to install into (like DESTDIR with auto*).
101
102   --extraversion=EXTRAVERSION
103     Overrides the target kernel\'s EXTRAVERSION text.
104
105   -h, --help
106     Display this message.
107
108   --install
109     same as --install-kernel --install-lustre
110
111   --install-lustre
112     run make install in the Lustre tree.
113
114   --install-kernel
115     install the kernel image and modules.
116
117   -j jobs
118     This works just like the -j option to make, and is passed to make
119     when building.
120
121   --kerneldir=KERNELDIR
122     Directory containing linux source tarballs.
123
124   --target=TARGET
125     Name of the configuration to use.  The available targets are
126     listed below.
127
128   --target-arch=ARCH
129     Specifies an architecture to use when choosing a kernel config
130     file.  Default is i386.
131
132   --target-config=CONFIG
133     Specifies a special option (such as smp, bigmem, or BOOT) to use
134     when choosing a kernel config file.  This also modifies the kernel
135     version and modules directory.
136
137   --unpack-kernel
138     Untars and patches the kernel source.
139
140   The order that commands (--build-lustre, --unpack-kernel) are
141   specified on the command line is ignored; ${0##*/} will always
142   execute them in the correct order (unpack, then build, then install
143   etc.).
144
145 EOF
146     list_targets
147
148     fatal "$1" "$2"
149 }
150
151 check_options()
152 {
153     (( $BUILD_LUSTRE || $BUILD_KERNEL || $DEPEND_KERNEL || \
154             $INSTALL_LUSTRE || $INSTALL_KERNEL || $SAVE_HEADERS || \
155             $UNPACK_KERNEL )) || \
156                 fatal 1 "No commands specified."
157
158     if (( $UNPACK_KERNEL )) ; then
159         [ "$KERNELDIR" ] || \
160             fatal 1 "A kernel directory must be specified with --kerneldir."
161         [ -d "$KERNELDIR" ] || \
162             fatal 1 "$KERNELDIR is not a directory."
163     fi
164
165     if (( $INSTALL_LUSTRE || $INSTALL_KERNEL || $SAVE_HEADERS )) ; then
166         [ -z "$DESTDIR" -o -d "$DESTDIR" ] || \
167             fatal 1 "$DESTDIR is not a directory."
168     fi
169
170     [ "$TARGET" ] || usage 1 "A target must be specified with --target."
171     TARGET_FILE="$TOPDIR/kernel_patches/targets/$TARGET.target"
172     [ -r "$TARGET_FILE" ] || \
173         fatal 1 "Target '$TARGET' was not found.  Try --list-targets."
174
175     if [ -z "$JOBS" -o "$JOBS" -lt "1" ] ; then
176         JOBS=1
177     fi
178 }
179
180 get_lustre_version()
181 {
182     lustre_patch=$(grep lustre_version "$SERIES_FILE" 2>/dev/null)
183     [ "$lustre_patch" ] || \
184         fatal 1 "Could not determine Lustre version from $SERIES series."
185
186     awk '/^\+#define LUSTRE_KERNEL_VERSION /{ print $3 }' \
187         "$TOPDIR/kernel_patches/patches/$lustre_patch" 2>/dev/null
188 }
189
190 load_target()
191 {
192     EXTRA_VERSION_save="$EXTRA_VERSION"
193
194     . "$TARGET_FILE"
195
196     [ "$KERNEL" ] || fatal 1 "Target $TARGET did not specify a kernel."
197     [ "$SERIES" ] || fatal 1 "Target $TARGET did not specify a patch series."
198 #    [ "$CONFIG" ] || fatal 1 "Target $TARGET did not specify a kernel config."
199     [ "$VERSION" ] || fatal 1 "Target $TARGET did not specify the kernel version."
200     
201     if [ "$KERNELDIR" ] ; then
202         KERNEL_FILE="$KERNELDIR/$KERNEL"
203         [ -r "$KERNELDIR/$KERNEL" ] || \
204             fatal 1 "Target $TARGET's kernel file $KERNEL not found in kernel directory $KERNELDIR."
205     fi
206
207     SERIES_FILE="$TOPDIR/kernel_patches/series/$SERIES"
208     [ -r "$SERIES_FILE" ] || \
209         fatal 1 "Target $TARGET's series $SERIES missing from $TOPDIR/kernel_patches/series."
210
211     TARGET_ARCH=${TARGET_ARCH:-$BASE_ARCHS}
212     CONFIG_TARGET="$TARGET-${TARGET_ARCH}${TARGET_CONFIG:+-$TARGET_CONFIG}"
213     CONFIG_FILE="$TOPDIR/kernel_patches/kernel_configs/kernel-$VERSION-$CONFIG_TARGET.config"
214     [ -r "$CONFIG_FILE" ] ||
215         fatal 1 "Target $TARGET's config file $CONFIG_FILE missing from $TOPDIR/kernel_patches/configs."
216
217     if [ "$EXTRA_VERSION_save" ] ; then
218         EXTRA_VERSION="$EXTRA_VERSION_save"
219     else
220         EXTRA_VERSION="${EXTRA_VERSION}_lustre.$(get_lustre_version)"
221     fi
222 }
223
224 tarflags()
225 {
226     case "$1" in
227         '')
228             fatal 1 "tarflags(): File name argument missing."
229             ;;
230         *.tar.gz)
231             echo 'zxf'
232             ;;
233         *.tar.bz2)
234             echo 'jxf'
235             ;;
236         *)
237             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
238             ;;
239     esac
240 }
241
242 untar()
243 {
244     echo "Untarring ${1##*/}..."
245     tar $(tarflags $1) $1
246 }
247
248
249 extract_kernel()
250 {
251     (( $UNPACK_KERNEL )) || return 0
252     pushd "$TOPDIR" >/dev/null
253     if [ -d linux ] ; then
254         [ -L linux ] && rm -rf $(readlink linux)
255         rm -rf linux
256     fi
257     untar "$KERNEL_FILE"
258     [ -d linux ] || ln -sf linux* linux
259     popd >/dev/null
260 }
261
262 patch_kernel()
263 {
264     (( $UNPACK_KERNEL )) || return 0
265     pushd "$TOPDIR/linux" >/dev/null
266     echo -n "Applying patch"
267     for patch in $(<"$SERIES_FILE") ; do
268         PATCH_FILE="$TOPDIR/kernel_patches/patches/$patch"
269         [ -r "$PATCH_FILE" ] || \
270             fatal 1 "Patch file not found: $patch"
271         echo -n " $patch"
272         patch -s -p1 < "$PATCH_FILE" || fatal 1 "Error applying patch $patch."
273     done
274     echo
275     popd >/dev/null
276 }
277
278 set_make()
279 {
280     MAKE="make -s CC=$CC"
281     if [ "$ARCH" ] ; then
282         MAKE="$MAKE ARCH=$ARCH"
283     fi
284     MAKE_J="$MAKE -j $JOBS"
285 }
286
287 depend_kernel()
288 {
289     (( $DEPEND_KERNEL )) || return 0
290     # we need to override $CC at make time, since there is no
291     # configure
292     set_make
293     pushd "$TOPDIR/linux" >/dev/null
294     echo "Overriding EXTRAVERSION in kernel..."
295     perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -${EXTRA_VERSION}${TARGET_CONFIG}/" Makefile
296     echo "Making depend in $PWD..."
297     $MAKE mrproper || fatal 1 "Error running make mrproper"
298     cp "$CONFIG_FILE" .config
299     if grep -q oldconfig_nonint Makefile ; then
300         OLDCONFIG='oldconfig_nonint'
301     else
302         OLDCONFIG='oldconfig'
303     fi
304     $MAKE $OLDCONFIG || fatal 1 "Error running make oldconfig"
305     $MAKE_J dep || fatal 1 "Error running make dep"
306     $MAKE include/linux/version.h || fatal 1 "Error making include/linux/version.h"
307 }
308
309 build_kernel()
310 {
311     (( $BUILD_KERNEL )) || return 0
312     # we need to override $CC at make time, since there is no
313     # configure
314     set_make
315     echo "Building kernel in $PWD..."
316     case "$TARGET_ARCH" in
317         i386 | i586 | i686 | athlon | x86_64)
318             $MAKE_J bzImage || fatal 1 "Error making bzImage."
319             ;;
320         *)
321             $MAKE_J boot || fatal 1 "Error making boot."
322             ;;
323     esac
324     $MAKE_J modules || fatal 1 "Error building modules."
325
326     popd >/dev/null
327 }
328
329 configure_lustre()
330 {
331     (( $BUILD_LUSTRE )) || return 0
332     pushd "$TOPDIR" >/dev/null
333     [ -f Makefile ] && make -s clean
334     [ -f configure ] || sh ./autogen.sh
335     ./configure --with-linux=$PWD/linux $CONFIGURE_FLAGS || \
336         fatal 1 "Error configuring Lustre."
337     popd >/dev/null
338 }
339
340 build_lustre()
341 {
342     (( $BUILD_LUSTRE )) || return 0
343     set_make
344     pushd "$TOPDIR" >/dev/null
345     $MAKE_J || fatal 1 "Error building Lustre."
346     popd >/dev/null
347 }
348
349 install_kernel()
350 {
351     (( $INSTALL_KERNEL )) || return 0
352     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
353     pushd "$TOPDIR/linux" >/dev/null
354     mkdir -p "$DESTDIR/boot"
355
356     install -m 644 System.map "$DESTDIR/boot/System.map-${FULL_VERSION}"
357     # install -m 644 module-info ...
358     install -m 644 "$CONFIG_FILE" "$DESTDIR/boot/config-${FULL_VERSION}"
359
360     mkdir -p "$DESTDIR/dev/shm"
361     mkdir -p "$DESTDIR/lib/modules/${FULL_VERSION}"
362
363     make CC="$CC" INSTALL_MOD_PATH="$DESTDIR" KERNELRELEASE="$FULL_VERSION" \
364         -s modules_install || \
365         fatal 1 "Error installing modules."
366
367     case "$TARGET_ARCH" in
368         i386 | i586 | i686 | athlon)
369             cp arch/i386/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
370             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
371             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
372             ;;
373         x86_64)
374             cp arch/x86_64/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
375             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
376             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
377             ;;
378         ia64)
379             gzip -cfv vmlinux > vmlinuz
380             mkdir -p "$DESTDIR/boot/efi/redhat"
381             install -m 755 vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
382             install -m 755 vmlinuz "$DESTDIR/boot/efi/redhat/vmlinuz-${FULL_VERSION}"
383             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/efi/redhat/vmlinux-${FULL_VERSION}"
384             ln -sf "efi/redhat/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
385             ln -sf "efi/redhat/vmlinuz-${FULL_VERSION}" "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
386             ;;
387         *)
388             cp vmlinuz "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
389             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}"
390             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
391
392             ;;
393     esac
394
395     popd >/dev/null
396 }
397
398 install_lustre()
399 {
400     (( $INSTALL_LUSTRE )) || return 0
401     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
402     pushd "$TOPDIR" >/dev/null
403     make -s install "DESTDIR=$DESTDIR" KERNELRELEASE="$FULL_VERSION" || fatal 1 "Error installing Lustre."
404     popd >/dev/null
405 }
406
407 save_headers()
408 {
409     echo "Saving headers for $1 $2..."
410     pushd linux >/dev/null
411
412     KVERREL="${VERSION}-${EXTRA_VERSION}"
413     # deal with the kernel headers that are version specific
414     
415     saveddir="$RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/$2/$1"
416     mkdir -p "$saveddir"
417     install -m 644 include/linux/autoconf.h "$saveddir/autoconf.h"
418     install -m 644 include/linux/version.h  "$saveddir/version.h"
419     mv include/linux/modules "$saveddir/"
420     echo $2 $1 ../../savedheaders/$2/$1/ >>  $RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/list
421     popd >/dev/null
422 }
423
424 save_all_headers()
425 {
426     (( $SAVE_HEADERS )) || return 0
427
428     for arch in $BIGMEM_ARCHS ; do
429         save_headers bigmem $arch
430     done
431
432     for arch in $BOOT_ARCHS ; do
433         save_headers BOOT $arch
434     done
435
436     for arch in $JENSEN_ARCHS ; do
437         save_headers jensen $arch
438     done
439
440     for arch in $SMP_ARCHS ; do
441         save_headers smp $arch
442     done
443
444     for arch in $UP_ARCHS ; do
445         save_headers up $arch
446     done
447 }
448
449 longopts="build,build-lustre,build-kernel,depend-kernel,destdir:,extraversion:"
450 longopts="$longopts,help,install,install-lustre,install-kernel,kerneldir:"
451 longopts="$longopts,save-headers,target:,target-arch:,target-config:,unpack-kernel"
452
453 options=$(getopt -o hj: -l "$longopts" -- "$@")
454
455 eval set -- "$options"
456     
457 while [ "$1" ] ; do
458     case "$1" in
459         '')
460             usage 1
461             ;;
462         --build)
463             BUILD_LUSTRE=1
464             BUILD_KERNEL=1
465             DEPEND_KERNEL=1
466             UNPACK_KERNEL=1
467             shift
468             ;;
469         --build-lustre)
470             BUILD_LUSTRE=1
471             shift
472             ;;
473         --build-kernel)
474             BUILD_KERNEL=1
475             DEPEND_KERNEL=1
476             shift
477             ;;
478         --depend-kernel)
479             DEPEND_KERNEL=1
480             shift
481             ;;
482         --destdir)
483             DESTDIR=$2
484             shift 2
485             ;;
486         --extraversion)
487             EXTRA_VERSION=$2
488             shift 2
489             ;;
490         --help | -h)
491             usage 0
492             ;;
493         --install)
494             INSTALL_LUSTRE=1
495             INSTALL_KERNEL=1
496             shift
497             ;;
498         --install-lustre)
499             INSTALL_LUSTRE=1
500             shift
501             ;;
502         --install-kernel)
503             INSTALL_KERNEL=1
504             shift
505             ;;
506         -j)
507             JOBS=$2
508             shift 2
509             ;;
510         --kerneldir)
511             KERNELDIR=$2
512             shift 2
513             ;;
514         --save-headers)
515             SAVE_HEADERS=1
516             shift
517             ;;
518         --target)
519             TARGET=$2
520             shift 2
521             ;;
522         --target-arch)
523             TARGET_ARCH=$2
524             shift 2
525             ;;
526         --target-config)
527             TARGET_CONFIG=$2
528             shift 2
529             ;;
530         --unpack-kernel)
531             UNPACK_KERNEL=1
532             shift
533             ;;
534         --)
535             shift
536             CONFIGURE_FLAGS=$@
537             break
538             ;; 
539         *)
540             usage 1 "Unrecognized option: $1"
541             ;;
542     esac
543 done
544
545 check_options
546 load_target
547
548 extract_kernel
549 patch_kernel
550
551 depend_kernel
552 build_kernel
553
554 configure_lustre
555 build_lustre
556
557 install_kernel
558 install_lustre
559
560 save_all_headers
561
562 exit 0