Whamcloud - gitweb
- use $() for execing
[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
362     case "$TARGET_ARCH" in
363         i386 | i586 | i686 | athlon)
364             cp arch/i386/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
365             cp vmlinux "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
366             ;;
367         x86_64)
368             cp arch/x86_64/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
369             cp vmlinux "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
370             ;;
371         ia64)
372             gzip -cfv vmlinux > vmlinuz
373             mkdir -p "$DESTDIR/boot/efi/redhat"
374             install -m 755 vmlinux "$DESTDIR/boot/efi/redhat/vmlinux-${FULL_VERSION}"
375             install -m 755 vmlinuz "$DESTDIR/boot/efi/redhat/vmlinuz-${FULL_VERSION}"
376             ln -sf "efi/redhat/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
377             ln -sf "efi/redhat/vmlinuz-${FULL_VERSION}" "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
378             ;;
379         *)
380             cp vmlinuz "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
381             cp vmlinux "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
382             ;;
383     esac
384
385     mkdir -p "$DESTDIR/lib/modules/${FULL_VERSION}"
386     make CC="$CC" INSTALL_MOD_PATH="$DESTDIR" KERNELRELEASE="$FULL_VERSION" \
387         -s modules_install || \
388         fatal 1 "Error installing modules."
389
390     popd >/dev/null
391 }
392
393 install_lustre()
394 {
395     (( $INSTALL_LUSTRE )) || return 0
396     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
397     pushd "$TOPDIR" >/dev/null
398     make -s install "DESTDIR=$DESTDIR" KERNELRELEASE="$FULL_VERSION" || fatal 1 "Error installing Lustre."
399     popd >/dev/null
400 }
401
402 save_headers()
403 {
404     echo "Saving headers for $1 $2..."
405     pushd linux >/dev/null
406
407     KVERREL="${VERSION}-${EXTRA_VERSION}"
408     # deal with the kernel headers that are version specific
409     
410     saveddir="$RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/$2/$1"
411     mkdir -p "$saveddir"
412     install -m 644 include/linux/autoconf.h "$saveddir/autoconf.h"
413     install -m 644 include/linux/version.h  "$saveddir/version.h"
414     mv include/linux/modules "$saveddir/"
415     echo $2 $1 ../../savedheaders/$2/$1/ >>  $RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/list
416     popd >/dev/null
417 }
418
419 save_all_headers()
420 {
421     (( $SAVE_HEADERS )) || return 0
422
423     for arch in $BIGMEM_ARCHS ; do
424         save_headers bigmem $arch
425     done
426
427     for arch in $BOOT_ARCHS ; do
428         save_headers BOOT $arch
429     done
430
431     for arch in $JENSEN_ARCHS ; do
432         save_headers jensen $arch
433     done
434
435     for arch in $SMP_ARCHS ; do
436         save_headers smp $arch
437     done
438
439     for arch in $UP_ARCHS ; do
440         save_headers up $arch
441     done
442 }
443
444 longopts="build,build-lustre,build-kernel,depend-kernel,destdir:,extraversion:"
445 longopts="$longopts,help,install,install-lustre,install-kernel,kerneldir:"
446 longopts="$longopts,save-headers,target:,target-arch:,target-config:,unpack-kernel"
447
448 options=$(getopt -o hj: -l "$longopts" -- "$@")
449
450 eval set -- "$options"
451     
452 while [ "$1" ] ; do
453     case "$1" in
454         '')
455             usage 1
456             ;;
457         --build)
458             BUILD_LUSTRE=1
459             BUILD_KERNEL=1
460             DEPEND_KERNEL=1
461             UNPACK_KERNEL=1
462             shift
463             ;;
464         --build-lustre)
465             BUILD_LUSTRE=1
466             shift
467             ;;
468         --build-kernel)
469             BUILD_KERNEL=1
470             DEPEND_KERNEL=1
471             shift
472             ;;
473         --depend-kernel)
474             DEPEND_KERNEL=1
475             shift
476             ;;
477         --destdir)
478             DESTDIR=$2
479             shift 2
480             ;;
481         --extraversion)
482             EXTRA_VERSION=$2
483             shift 2
484             ;;
485         --help | -h)
486             usage 0
487             ;;
488         --install)
489             INSTALL_LUSTRE=1
490             INSTALL_KERNEL=1
491             shift
492             ;;
493         --install-lustre)
494             INSTALL_LUSTRE=1
495             shift
496             ;;
497         --install-kernel)
498             INSTALL_KERNEL=1
499             shift
500             ;;
501         -j)
502             JOBS=$2
503             shift 2
504             ;;
505         --kerneldir)
506             KERNELDIR=$2
507             shift 2
508             ;;
509         --save-headers)
510             SAVE_HEADERS=1
511             shift
512             ;;
513         --target)
514             TARGET=$2
515             shift 2
516             ;;
517         --target-arch)
518             TARGET_ARCH=$2
519             shift 2
520             ;;
521         --target-config)
522             TARGET_CONFIG=$2
523             shift 2
524             ;;
525         --unpack-kernel)
526             UNPACK_KERNEL=1
527             shift
528             ;;
529         --)
530             shift
531             CONFIGURE_FLAGS=$@
532             break
533             ;; 
534         *)
535             usage 1 "Unrecognized option: $1"
536             ;;
537     esac
538 done
539
540 check_options
541 load_target
542
543 extract_kernel
544 patch_kernel
545
546 depend_kernel
547 build_kernel
548
549 configure_lustre
550 build_lustre
551
552 install_kernel
553 install_lustre
554
555 save_all_headers
556
557 exit 0