Back to home page

LXR

 
 

    


0001 #! /bin/sh
0002 # Script to apply kernel patches.
0003 #   usage: patch-kernel [ sourcedir [ patchdir [ stopversion ] [ -acxx ] ] ]
0004 #     The source directory defaults to /usr/src/linux, and the patch
0005 #     directory defaults to the current directory.
0006 # e.g.
0007 #   scripts/patch-kernel . ..
0008 #      Update the kernel tree in the current directory using patches in the
0009 #      directory above to the latest Linus kernel
0010 #   scripts/patch-kernel . .. -ac
0011 #      Get the latest Linux kernel and patch it with the latest ac patch
0012 #   scripts/patch-kernel . .. 2.4.9
0013 #      Gets standard kernel 2.4.9
0014 #   scripts/patch-kernel . .. 2.4.9 -ac
0015 #      Gets 2.4.9 with latest ac patches
0016 #   scripts/patch-kernel . .. 2.4.9 -ac11
0017 #      Gets 2.4.9 with ac patch ac11
0018 #   Note: It uses the patches relative to the Linus kernels, not the
0019 #   ac to ac relative patches
0020 #
0021 # It determines the current kernel version from the top-level Makefile.
0022 # It then looks for patches for the next sublevel in the patch directory.
0023 # This is applied using "patch -p1 -s" from within the kernel directory.
0024 # A check is then made for "*.rej" files to see if the patch was
0025 # successful.  If it is, then all of the "*.orig" files are removed.
0026 #
0027 #       Nick Holloway <Nick.Holloway@alfie.demon.co.uk>, 2nd January 1995.
0028 #
0029 # Added support for handling multiple types of compression. What includes
0030 # gzip, bzip, bzip2, zip, compress, and plaintext.
0031 #
0032 #       Adam Sulmicki <adam@cfar.umd.edu>, 1st January 1997.
0033 #
0034 # Added ability to stop at a given version number
0035 # Put the full version number (i.e. 2.3.31) as the last parameter
0036 #       Dave Gilbert <linux@treblig.org>, 11th December 1999.
0037 
0038 # Fixed previous patch so that if we are already at the correct version
0039 # not to patch up.
0040 #
0041 # Added -ac option, use -ac or -ac9 (say) to stop at a particular version
0042 #       Dave Gilbert <linux@treblig.org>, 29th September 2001.
0043 #
0044 # Add support for (use of) EXTRAVERSION (to support 2.6.8.x, e.g.);
0045 # update usage message;
0046 # fix some whitespace damage;
0047 # be smarter about stopping when current version is larger than requested;
0048 #       Randy Dunlap <rdunlap@xenotime.net>, 2004-AUG-18.
0049 #
0050 # Add better support for (non-incremental) 2.6.x.y patches;
0051 # If an ending version number if not specified, the script automatically
0052 # increments the SUBLEVEL (x in 2.6.x.y) until no more patch files are found;
0053 # however, EXTRAVERSION (y in 2.6.x.y) is never automatically incremented
0054 # but must be specified fully.
0055 #
0056 # patch-kernel does not normally support reverse patching, but does so when
0057 # applying EXTRAVERSION (x.y) patches, so that moving from 2.6.11.y to 2.6.11.z
0058 # is easy and handled by the script (reverse 2.6.11.y and apply 2.6.11.z).
0059 #       Randy Dunlap <rdunlap@xenotime.net>, 2005-APR-08.
0060 
0061 PNAME=patch-kernel
0062 
0063 # Set directories from arguments, or use defaults.
0064 sourcedir=${1-/usr/src/linux}
0065 patchdir=${2-.}
0066 stopvers=${3-default}
0067 
0068 if [ "$1" = -h -o "$1" = --help -o ! -r "$sourcedir/Makefile" ]; then
0069 cat << USAGE
0070 usage: $PNAME [-h] [ sourcedir [ patchdir [ stopversion ] [ -acxx ] ] ]
0071   source directory defaults to /usr/src/linux,
0072   patch directory defaults to the current directory,
0073   stopversion defaults to <all in patchdir>.
0074 USAGE
0075 exit 1
0076 fi
0077 
0078 # See if we have any -ac options
0079 for PARM in $*
0080 do
0081   case $PARM in
0082           -ac*)
0083                   gotac=$PARM;
0084 
0085         esac;
0086 done
0087 
0088 # ---------------------------------------------------------------------------
0089 # arg1 is filename
0090 noFile () {
0091         echo "cannot find patch file: ${patch}"
0092         exit 1
0093 }
0094 
0095 # ---------------------------------------------------------------------------
0096 backwards () {
0097         echo "$PNAME does not support reverse patching"
0098         exit 1
0099 }
0100 
0101 # ---------------------------------------------------------------------------
0102 # Find a file, first parameter is basename of file
0103 # it tries many compression mechanisms and sets variables to say how to get it
0104 findFile () {
0105   filebase=$1;
0106 
0107   if [ -r ${filebase}.gz ]; then
0108                 ext=".gz"
0109                 name="gzip"
0110                 uncomp="gunzip -dc"
0111   elif [ -r ${filebase}.bz  ]; then
0112                 ext=".bz"
0113                 name="bzip"
0114                 uncomp="bunzip -dc"
0115   elif [ -r ${filebase}.bz2 ]; then
0116                 ext=".bz2"
0117                 name="bzip2"
0118                 uncomp="bunzip2 -dc"
0119   elif [ -r ${filebase}.xz ]; then
0120                 ext=".xz"
0121                 name="xz"
0122                 uncomp="xz -dc"
0123   elif [ -r ${filebase}.zip ]; then
0124                 ext=".zip"
0125                 name="zip"
0126                 uncomp="unzip -d"
0127   elif [ -r ${filebase}.Z ]; then
0128                 ext=".Z"
0129                 name="uncompress"
0130                 uncomp="uncompress -c"
0131   elif [ -r ${filebase} ]; then
0132                 ext=""
0133                 name="plaintext"
0134                 uncomp="cat"
0135   else
0136         return 1;
0137   fi
0138 
0139   return 0;
0140 }
0141 
0142 # ---------------------------------------------------------------------------
0143 # Apply a patch and check it goes in cleanly
0144 # First param is patch name (e.g. patch-2.4.9-ac5) - without path or extension
0145 
0146 applyPatch () {
0147   echo -n "Applying $1 (${name})... "
0148   if $uncomp ${patchdir}/$1${ext} | patch -p1 -s -N -E -d $sourcedir
0149   then
0150     echo "done."
0151   else
0152     echo "failed.  Clean up yourself."
0153     return 1;
0154   fi
0155   if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]
0156   then
0157     echo "Aborting.  Reject files found."
0158     return 1;
0159   fi
0160   # Remove backup files
0161   find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;
0162 
0163   return 0;
0164 }
0165 
0166 # ---------------------------------------------------------------------------
0167 # arg1 is patch filename
0168 reversePatch () {
0169         echo -n "Reversing $1 (${name}) ... "
0170         if $uncomp ${patchdir}/"$1"${ext} | patch -p1 -Rs -N -E -d $sourcedir
0171         then
0172                 echo "done."
0173         else
0174                 echo "failed.  Clean it up."
0175                 exit 1
0176         fi
0177         if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]
0178         then
0179                 echo "Aborting.  Reject files found."
0180                 return 1
0181         fi
0182         # Remove backup files
0183         find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;
0184 
0185         return 0
0186 }
0187 
0188 # set current VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION
0189 # force $TMPFILEs below to be in local directory: a slash character prevents
0190 # the dot command from using the search path.
0191 TMPFILE=`mktemp ./.tmpver.XXXXXX` || { echo "cannot make temp file" ; exit 1; }
0192 grep -E "^(VERSION|PATCHLEVEL|SUBLEVEL|EXTRAVERSION)" $sourcedir/Makefile > $TMPFILE
0193 tr -d [:blank:] < $TMPFILE > $TMPFILE.1
0194 . $TMPFILE.1
0195 rm -f $TMPFILE*
0196 if [ -z "$VERSION" -o -z "$PATCHLEVEL" -o -z "$SUBLEVEL" ]
0197 then
0198     echo "unable to determine current kernel version" >&2
0199     exit 1
0200 fi
0201 
0202 NAME=`grep ^NAME $sourcedir/Makefile`
0203 NAME=${NAME##*=}
0204 
0205 echo "Current kernel version is $VERSION.$PATCHLEVEL.$SUBLEVEL${EXTRAVERSION} ($NAME)"
0206 
0207 # strip EXTRAVERSION to just a number (drop leading '.' and trailing additions)
0208 EXTRAVER=
0209 if [ x$EXTRAVERSION != "x" ]
0210 then
0211         EXTRAVER=${EXTRAVERSION#.}
0212         EXTRAVER=${EXTRAVER%%[[:punct:]]*}
0213         #echo "$PNAME: changing EXTRAVERSION from $EXTRAVERSION to $EXTRAVER"
0214 fi
0215 
0216 #echo "stopvers=$stopvers"
0217 if [ $stopvers != "default" ]; then
0218         STOPSUBLEVEL=`echo $stopvers | cut -d. -f3`
0219         STOPEXTRA=`echo $stopvers | cut -d. -f4`
0220         STOPFULLVERSION=${stopvers%%.$STOPEXTRA}
0221         #echo "#___STOPSUBLEVEL=/$STOPSUBLEVEL/, STOPEXTRA=/$STOPEXTRA/"
0222 else
0223         STOPSUBLEVEL=9999
0224         STOPEXTRA=9999
0225 fi
0226 
0227 # This all assumes a 2.6.x[.y] kernel tree.
0228 # Don't allow backwards/reverse patching.
0229 if [ $STOPSUBLEVEL -lt $SUBLEVEL ]; then
0230         backwards
0231 fi
0232 
0233 if [ x$EXTRAVER != "x" ]; then
0234         CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$EXTRAVER"
0235 else
0236         CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
0237 fi
0238 
0239 if [ x$EXTRAVER != "x" ]; then
0240         echo "backing up to: $VERSION.$PATCHLEVEL.$SUBLEVEL"
0241         patch="patch-${CURRENTFULLVERSION}"
0242         findFile $patchdir/${patch} || noFile ${patch}
0243         reversePatch ${patch} || exit 1
0244 fi
0245 
0246 # now current is 2.6.x, with no EXTRA applied,
0247 # so update to target SUBLEVEL (2.6.SUBLEVEL)
0248 # and then to target EXTRAVER (2.6.SUB.EXTRAVER) if requested.
0249 # If not ending sublevel is specified, it is incremented until
0250 # no further sublevels are found.
0251 
0252 if [ $STOPSUBLEVEL -gt $SUBLEVEL ]; then
0253 while :                         # incrementing SUBLEVEL (s in v.p.s)
0254 do
0255     CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
0256     EXTRAVER=
0257     if [ x$STOPFULLVERSION = x$CURRENTFULLVERSION ]; then
0258         echo "Stopping at $CURRENTFULLVERSION base as requested."
0259         break
0260     fi
0261 
0262     SUBLEVEL=$(($SUBLEVEL + 1))
0263     FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
0264     #echo "#___ trying $FULLVERSION ___"
0265 
0266     if [ $(($SUBLEVEL)) -gt $(($STOPSUBLEVEL)) ]; then
0267         echo "Stopping since sublevel ($SUBLEVEL) is beyond stop-sublevel ($STOPSUBLEVEL)"
0268         exit 1
0269     fi
0270 
0271     patch=patch-$FULLVERSION
0272     # See if the file exists and find extension
0273     findFile $patchdir/${patch} || noFile ${patch}
0274 
0275     # Apply the patch and check all is OK
0276     applyPatch $patch || break
0277 done
0278 #echo "#___sublevel all done"
0279 fi
0280 
0281 # There is no incremental searching for extraversion...
0282 if [ "$STOPEXTRA" != "" ]; then
0283 while :                         # just to allow break
0284 do
0285 # apply STOPEXTRA directly (not incrementally) (x in v.p.s.x)
0286         FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$STOPEXTRA"
0287         #echo "#... trying $FULLVERSION ..."
0288         patch=patch-$FULLVERSION
0289 
0290         # See if the file exists and find extension
0291         findFile $patchdir/${patch} || noFile ${patch}
0292 
0293         # Apply the patch and check all is OK
0294         applyPatch $patch || break
0295         #echo "#___extraver all done"
0296         break
0297 done
0298 fi
0299 
0300 if [ x$gotac != x ]; then
0301   # Out great user wants the -ac patches
0302         # They could have done -ac (get latest) or -acxx where xx=version they want
0303         if [ $gotac = "-ac" ]; then
0304           # They want the latest version
0305                 HIGHESTPATCH=0
0306                 for PATCHNAMES in $patchdir/patch-${CURRENTFULLVERSION}-ac*\.*
0307                 do
0308                         ACVALUE=`echo $PATCHNAMES | sed -e 's/^.*patch-[0-9.]*-ac\([0-9]*\).*/\1/'`
0309                         # Check it is actually a recognised patch type
0310                         findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${ACVALUE} || break
0311 
0312                   if [ $ACVALUE -gt $HIGHESTPATCH ]; then
0313                           HIGHESTPATCH=$ACVALUE
0314                   fi
0315                 done
0316 
0317                 if [ $HIGHESTPATCH -ne 0 ]; then
0318                         findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH} || break
0319                         applyPatch patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH}
0320                 else
0321                   echo "No -ac patches found"
0322                 fi
0323         else
0324           # They want an exact version
0325                 findFile $patchdir/patch-${CURRENTFULLVERSION}${gotac} || {
0326                   echo "Sorry, I couldn't find the $gotac patch for $CURRENTFULLVERSION.  Hohum."
0327                         exit 1
0328                 }
0329                 applyPatch patch-${CURRENTFULLVERSION}${gotac}
0330         fi
0331 fi