#! /bin/sh # # addpatches - a script to apply a kernel patch set. # # In a patch directory, the patches are organized with a numeric prefix: # 00_* # 01_* # 02_* # ... # 99_* # They should be sortable by the wildcard expression '*_*'. # # Usage: addpatches [ patcharg [ sourcedir [ patchdir ] ] ] # The source directory defaults to /usr/src/linux, and the patch # directory defaults to the current directory # # addpatches determines the current kernel version from the top-level Makefile. # It then looks for patches for the next sublevel in the patch directory. # This is applied using "patch -p1 -s" from within the kernel directory. # A check is then made for "*.rej" files to see if the patch was # successful. If it is, then all of the "*.orig" files are removed. # # Thanks to # Nick Holloway # Adam Sulmicki # Dave Gilbert # for the scripts/patch-kernel script. # Set directories from arguments, or use defaults. patcharg=${1-default} sourcedir=${2-/usr/src/linux} patchdir=${3-.} PATCHES="" # Find a file, first parameter is basename of file # it tries many compression mechanisms and sets variables to say how to get it function findFile { filebase=$1; if [ -r `basename ${filebase} .gz`.gz ]; then name="gzip format" uncomp="zcat" elif [ -r `basename ${filebase} .bz2`.bz2 ]; then name="bzip2 format" uncomp="bzcat" elif [ -r ${filebase} ]; then name="plain text format" uncomp="cat" else return 1; fi return 0; } # Apply a patch and check it goes in cleanly # First param is patch name function applyPatch { echo -n "Checking $1 (${name})... " if $uncomp ${patchdir}/$1 | patch -p1 --dry-run -s -N -E -d $sourcedir then echo -n "looks good. Applying... " $uncomp ${patchdir}/$1 | patch -p1 -s -N -E -d $sourcedir PATCHES="$1 $PATCHES" echo "done." else echo "can't be applied. Please modify the patch." unroll="unroll" if [ $patcharg == $unroll ] ; then echo "Trying to unroll patches applied so far" for i in $PATCHES do echo -n "Reverting $i ..." findFile $patchdir/$i || break $uncomp ${patchdir}/$i | patch -p1 -R -s -E -d $sourcedir echo "done." done fi return 1; fi if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] then echo "Hmm. Reject files found." return 1; fi # Remove backup files find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \; return 0; } # set current VERSION, PATCHLEVEL, SUBLEVEL, EXTERVERSION eval `sed -n -e 's/^\([A-Z]*\) = \([0-9]*\)$/\1=\2/p' -e 's/^\([A-Z]*\) = \(-[-a-z0-9]*\)$/\1=\2/p' $sourcedir/Makefile` if [ -z "$VERSION" -o -z "$PATCHLEVEL" -o -z "$SUBLEVEL" ] then echo "unable to determine current kernel version" >&2 exit 1 fi echo "Current kernel version is $VERSION.$PATCHLEVEL.$SUBLEVEL${EXTRAVERSION}" if [ -d $patchdir ] then echo "Scanning patch directory: '$patchdir'" for i in `ls *_*` do findFile $patchdir/$i || exit 1 applyPatch $i || exit 1 done else echo "Patch directory not found" exit 1 fi