Whamcloud - gitweb
- chaos kernel only needs half of the old inode-max-readahead patch, so added a new...
[fs/lustre-release.git] / lustre / scripts / lbuild
1 #!/bin/sh
2
3 TOPDIR=$PWD
4
5 # CVSROOT is inherited from the environment
6 KERNELDIR=
7 LUSTRE=
8 PUBLISH=0
9 RELEASE=0
10 DO_SRC=1
11 TAG=
12 TARGET=
13 TARGET_ARCHS=
14 CONFIGURE_FLAGS=
15 EXTRA_VERSION=
16
17 # from target file
18 KERNEL=
19 SERIES=
20 CONFIG=
21 VERSION=
22
23 BASE_ARCH=
24 BIGMEM_ARCHS=
25 BOOT_ARCHS=
26 JENSEN_ARCHS=
27 SMP_ARCHS=
28 UP_ARCHS=
29
30 DATE=$(date)
31
32 RPMBUILD=
33
34 cleanup()
35 {
36     true
37 }
38
39 fatal()
40 {
41     cleanup
42     [ "$2" ] && echo
43     [ "$2" ] && echo "${0##*/}: $2"
44     exit $1
45 }
46
47 publishing()
48 {
49     (( $PUBLISH )) || return 0
50 }
51
52 is_release()
53 {
54     (( $RELEASE )) || return 0
55 }
56
57 list_targets()
58 {
59     echo -n "Available targets:"
60     for target in $TOPDIR/lustre/kernel_patches/targets/*.target ; do
61         target_file=${target##*/}
62         echo -n " ${target_file%%.target}"
63     done
64     echo
65 }
66
67 usage()
68 {
69     cat <<EOF
70 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
71
72   -d CVSROOT
73     Specifies the CVS Root to use when pulling files from CVS.  The
74     environment variable \$CVSROOT is used if this option is not
75     present.
76
77   --extraversion=EXTRAVERSION
78     Text to use for the rpm release and kernel extraversion.
79
80   --kerneldir=KERNELDIR
81     Directory containing Linux source tarballs referenced by target
82     files.
83
84   --lustre=LUSTRE
85     Path to an existing lustre source tarball to use instead of
86     pulling from CVS.
87
88   --nosrc
89     Do not build a .src.rpm, a full kernel patch, or a patched kernel
90     tarball.
91
92   --publish
93     Publish the packages, patches, and tarballs on the ftp server.
94
95   --release
96     Specifies that the files generated do not include timestamps, and
97     that this is an official release.
98
99   --tag=TAG
100     A CVS branch/tag name to build from when pulling from CVS.
101
102   --target=TARGET
103     The name of the target to build.  The available targets are listed
104     below.
105
106   --target-archs=TARGET_ARCHS
107     A (space delimited) list of architectures to build.  By default,
108     all of the archs supported by the TARGET will be built, in
109     addition to a .src.rpm.  This option can limit those, for machines
110     that can only build certain archs or if you only want a certain
111     arch built (for testing, or a one-off kernel).
112
113     Also note that by using a non-"base" arch (eg, i386) only kernels
114     will be built - there will be no lustre-lite-utils package.
115
116 EOF
117
118 #   list_targets
119
120     fatal "$1" "$2"
121 }
122
123 check_options()
124 {
125     if [ "$LUSTRE" ] ; then
126         [ -r "$LUSTRE" ] || \
127             usage 1 "Could not find Lustre source tarball '$LUSTRE'."
128     else
129         [ "$CVSROOT" ] || \
130             usage 1 "Either specify a CVS Root with -d, or a Lustre source tarball with --lustre."
131         [ "$TAG" ] || \
132             usage 1 "A branch/tag name must be specified with --tag when not building from a tarball."
133     fi
134
135     [ "$KERNELDIR" ] || \
136         usage 1 "A kernel directory must be specified with --kerneldir."
137
138     [ -d "$KERNELDIR" ] || \
139         usage 1 "$KERNELDIR is not a directory."
140
141     if ! (( $RELEASE )) ; then
142         [ "$TAG" ] || \
143             usage 1 "When building a snapshot, a tag name must be used."
144     fi
145
146     TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M")
147
148     [ "$TARGET" ] || usage 1 "A target must be specified with --target."
149 #    TARGET_FILE="$TOPDIR/kernel_patches/targets/$TARGET.target"
150 #    [ -r "$TARGET_FILE" ] || \
151 #       usage 1 "Target '$TARGET' was not found."
152
153     RPMBUILD=`which rpmbuild`
154     if [ $? -ne 0 -o ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
155         RPMBUILD=`which rpm`
156         if [ $? -ne 0 -o ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
157             usage 1 "Could not find binary for making rpms (tried rpmbuild and rpm)."
158         fi
159     fi
160 }
161
162 uniqify()
163 {
164     echo $(echo "$*" | xargs -n 1 | sort -u)
165 }
166
167 load_target()
168 {
169     EXTRA_VERSION_save="$EXTRA_VERSION"
170     TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target"
171     [ -r "$TARGET_FILE" ] || \
172         fatal 1 "Target $TARGET was not found."
173
174     echo "Loading target config file $TARGET.target..." 
175
176     . "$TARGET_FILE"
177
178     [ "$KERNEL"  ] || fatal 1 "Target $TARGET did not specify a kernel."
179     [ "$SERIES"  ] || fatal 1 "Target $TARGET did not specify a kernel patch series."
180 #    [ "$CONFIG"  ] || fatal 1 "Target $TARGET did not specify a kernel config."
181     [ "$VERSION" ] || fatal 1 "Target $TARGET did not specify a kernel version."
182
183     if [ "$KERNELDIR" ] ; then
184         KERNEL_FILE="$KERNELDIR/$KERNEL"
185         [ -r "$KERNELDIR/$KERNEL" ] || \
186             fatal 1 "Target $TARGET's kernel file $KERNEL not found in kernel directory $KERNELDIR."
187     fi
188
189     SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$SERIES"
190     [ -r "$SERIES_FILE" ] || \
191         fatal 1 "Target $TARGET's series $SERIES missing from $TOPDIR/lustre/kernel_patches/series."
192
193     CONFIG_FILE="$TOPDIR/lustre/kernel_patches/kernel_configs/$CONFIG"
194     [ -r "$CONFIG_FILE" ] || \
195         fatal 1 "Target $TARGET's config file $CONFIG missing from $TOPDIR/lustre/kernel_patches/kernel_configs/configs."
196
197     if [ "$EXTRA_VERSION_save" ] ; then
198         EXTRA_VERSION="$EXTRA_VERSION_save"
199     elif ! (( $RELEASE )) ; then
200         EXTRA_VERSION="${EXTRA_VERSION}-${TAG//_/}.${TIMESTAMP}"
201     fi
202     # EXTRA_VERSION=${EXTRA_VERSION//-/_}
203
204     ALL_ARCHS="$BASE_ARCHS $BIGMEM_ARCHS $BOOT_ARCHS $JENSEN_ARCHS $SMP_ARCHS $UP_ARCHS"
205
206     BUILD_ARCHS=
207     for arch in $(uniqify "$ALL_ARCHS") ; do
208         if [ -z "$TARGET_ARCHS" ] || echo "$TARGET_ARCHS" | grep -s "$arch" ; then
209             BUILD_ARCHS="$BUILD_ARCHS $arch"
210         fi
211     done
212     [ "$BUILD_ARCHS" ] || usage 1 "No available target archs to build."
213     echo "Building for: $BUILD_ARCHS"
214 }
215
216 tarflags()
217 {
218     case "$1" in
219         '')
220             fatal 1 "tarflags(): File name argument missing."
221             ;;
222         *.tar.gz)
223             echo 'zxf'
224             ;;
225         *.tar.bz2)
226             echo 'jxf'
227             ;;
228         *)
229             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
230             ;;
231     esac
232 }
233
234 untar()
235 {
236     echo "Untarring ${1##*/}..."
237     tar $(tarflags "$1") "$1"
238 }
239
240 unpack_lustre()
241 {
242     DIRNAME="lustre-$TAG-$TIMESTAMP"
243     if [ "$LUSTRE" ] ; then
244         untar "$LUSTRE"
245         [ -d lustre ] || ln -sf lustre* lustre
246     else
247         cvs -d "$CVSROOT" -qz3 co -r "$TAG" -d "$DIRNAME" lustre || \
248             fatal 1 "There was an error checking out Lustre from CVS."
249         echo "Creating lustre tarball..."
250         tar zcf "$DIRNAME.tar.gz" "$DIRNAME" \
251             --exclude "CVS" --exclude "*~" --exclude ".cvsignore" || \
252             fatal 1 "Could not create Lustre tarball."
253         LUSTRE="$PWD/$DIRNAME.tar.gz"
254         ln -sf "$DIRNAME" lustre
255     fi
256 }
257
258 unpack_linux()
259 {
260     untar "$KERNEL_FILE"
261     [ -d linux ] || ln -sf linux* linux
262 }
263
264 patch_linux()
265 {
266     FULL_PATCH="$PWD/lustre-kernel-${TARGET}-${EXTRA_VERSION}.patch"
267     [ -f "$FULL_PATCH" ] && rm -f "$FULL_PATCH"
268     pushd linux >/dev/null
269     echo -n "Applying patches:"
270     for patch in $(<"$SERIES_FILE") ; do
271         echo -n " $patch"
272         PATCH_FILE="$TOPDIR/lustre/kernel_patches/patches/$patch"
273         [ -r "$PATCH_FILE" ] || \
274             fatal 1 "Patch $patch does not exist in Lustre tree."
275         cat "$PATCH_FILE" >> "$FULL_PATCH" || \
276             fatal 1 "Error adding patch $patch to full patch."
277         patch -s -p1 < "$PATCH_FILE" || fatal 1 "Error applying patch $patch."
278     done
279     echo
280     popd >/dev/null
281     echo "Full patch has been saved in ${FULL_PATCH##*/}."
282     echo "Replacing .config files..."
283     [ -d linux/configs ] || mkdir linux/configs
284     rm -f linux/configs/*
285     cp -v lustre/kernel_patches/kernel_configs/kernel-${VERSION}-${TARGET}*.config linux/configs/
286 }
287
288 pack_linux()
289 {
290     TARBALL="$(readlink linux)-$EXTRA_VERSION.tar.gz"
291     echo "Creating patched linux tarball $TARBALL..."
292     tar zcf "$TARBALL" "$(readlink linux)" \
293         --exclude "CVS" --exclude ".cvsignore" || \
294         --exclude "*.orig" --exclude "*~" --exclude "*.rej" || \
295         fatal 1 "Error creating patched Linux tarball."
296 }
297
298 clean_linux()
299 {
300     [ -d linux ] || return 0
301     echo "Cleaning linux..."
302     [ -L linux ] && rm -rf $(readlink linux)
303     rm -rf linux
304 }
305
306 prep_build()
307 {
308     # make .spec file
309     sed -e "s/@KERNEL_VERSION@/$VERSION/g" \
310         -e "s/@KERNEL_EXTRA_VERSION@/$EXTRA_VERSION/g" \
311         -e "s^@KERNEL_RELEASE@^${EXTRA_VERSION//-/_}^g" \
312         -e "s/@KERNEL_SOURCE@/$KERNEL/g" \
313         -e "s/@LUSTRE_SOURCE@/${LUSTRE##*/}/g" \
314         -e "s/@LUSTRE_TARGET@/$TARGET/g" \
315         -e "s#@CONFIGURE_FLAGS@#$CONFIGURE_FLAGS#g" \
316         -e "s/@BASE_ARCHS@/$BASE_ARCHS/g" \
317         -e "s/@BIGMEM_ARCHS@/$BIGMEM_ARCHS/g" \
318         -e "s/@BOOT_ARCHS@/$BOOT_ARCHS/g" \
319         -e "s/@JENSEN_ARCHS@/$BOOT_ARCHS/g" \
320         -e "s/@SMP_ARCHS@/$SMP_ARCHS/g" \
321         -e "s/@UP_ARCHS@/$UP_ARCHS/g" \
322         -e "s/@RHBUILD@/$RHBUILD/g" \
323         < $TOPDIR/lustre/scripts/lustre-kernel-2.4.spec.in \
324         > lustre-kernel-2.4.spec
325     [ -d SRPMS ] || mkdir SRPMS
326     [ -d RPMS ] || mkdir RPMS
327     [ -d BUILD ] || mkdir BUILD
328     [ -d SOURCES ] || mkdir SOURCES
329     cp $TOPDIR/lustre/scripts/linux-rhconfig.h SOURCES
330     cp $TOPDIR/lustre/scripts/linux-merge-config.awk SOURCES
331     cp $TOPDIR/lustre/scripts/linux-merge-modules.awk SOURCES
332     cp "$LUSTRE" "$KERNEL_FILE" SOURCES
333 }
334
335 clean_lustre()
336 {
337     [ -d lustre ] || return 0
338     echo "Cleaning lustre..."
339     [ -L lustre ] && rm -rf $(readlink lustre)
340     rm -rf lustre
341 }
342
343 build()
344 {
345     echo "Building rpms for: $BUILD_ARCHS..."
346     targets=
347     for arch in $BUILD_ARCHS ; do
348         targets="--target $arch $targets"
349     done
350
351     $RPMBUILD $targets -bb lustre-kernel-2.4.spec \
352         --define "_topdir $TOPDIR" || \
353         fatal 1 "Error building rpms for $arch."
354
355     (( $DO_SRC )) && $RPMBUILD -bs lustre-kernel-2.4.spec \
356         --define "_topdir $TOPDIR" || \
357         fatal 1 "Error building .src.rpm."
358 }
359
360 publish()
361 {
362     publishing || return 0
363 }
364
365 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
366
367 options=$(getopt -o d:D:h -l extraversion:,kerneldir:,lustre:,nosrc,publish,release,tag:,target:,target-archs: -- "$@")
368
369 eval set -- "$options"
370     
371 while [ "$1" ] ; do
372     case "$1" in
373         '')
374             usage 1
375             ;;
376         -d)
377             CVSROOT=$2
378             shift 2
379             ;;
380         -D)
381             DATE=$2
382             shift 2
383             ;;
384         --extraversion)
385             EXTRA_VERSION=$2
386             shift 2
387             ;;
388         --help | -h)
389             usage 0
390             ;;
391         --kerneldir)
392             KERNELDIR=$2
393             shift 2
394             ;;
395         --lustre)
396             LUSTRE=$2
397             shift 2
398             ;;
399         --nosrc)
400             DO_SRC=0
401             shift 1
402             ;;
403         --publish)
404             PUBLISH=1
405             shift
406             ;;
407         --release)
408             RELEASE=1
409             shift
410             ;;
411         --tag)
412             TAG=$2
413             shift 2
414             ;;
415         --target)
416             TARGET=$2
417             shift 2
418             ;;
419         --target-archs)
420             TARGET_ARCHS=$2
421             shift 2
422             ;;
423         --)
424             shift
425             CONFIGURE_FLAGS=$@
426             break
427             ;; 
428         *)
429             usage 1 "Unrecognized option: $1"
430             ;;
431     esac
432 done
433
434 check_options
435
436 unpack_lustre
437 load_target
438
439 if (( $DO_SRC )) ; then
440     unpack_linux
441     patch_linux
442     pack_linux
443     clean_linux
444 fi
445
446 # prep_build needs the .spec.in from the lustre source
447 prep_build
448 clean_lustre
449
450 build
451 publish