patch-kernel 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #! /bin/sh
  2. # Script to apply kernel patches.
  3. # usage: patch-kernel [ sourcedir [ patchdir [ stopversion ] [ -acxx ] ] ]
  4. # The source directory defaults to /usr/src/linux, and the patch
  5. # directory defaults to the current directory.
  6. # e.g.
  7. # scripts/patch-kernel . ..
  8. # Update the kernel tree in the current directory using patches in the
  9. # directory above to the latest Linus kernel
  10. # scripts/patch-kernel . .. -ac
  11. # Get the latest Linux kernel and patch it with the latest ac patch
  12. # scripts/patch-kernel . .. 2.4.9
  13. # Gets standard kernel 2.4.9
  14. # scripts/patch-kernel . .. 2.4.9 -ac
  15. # Gets 2.4.9 with latest ac patches
  16. # scripts/patch-kernel . .. 2.4.9 -ac11
  17. # Gets 2.4.9 with ac patch ac11
  18. # Note: It uses the patches relative to the Linus kernels, not the
  19. # ac to ac relative patches
  20. #
  21. # It determines the current kernel version from the top-level Makefile.
  22. # It then looks for patches for the next sublevel in the patch directory.
  23. # This is applied using "patch -p1 -s" from within the kernel directory.
  24. # A check is then made for "*.rej" files to see if the patch was
  25. # successful. If it is, then all of the "*.orig" files are removed.
  26. #
  27. # Nick Holloway <Nick.Holloway@alfie.demon.co.uk>, 2nd January 1995.
  28. #
  29. # Added support for handling multiple types of compression. What includes
  30. # gzip, bzip, bzip2, zip, compress, and plaintext.
  31. #
  32. # Adam Sulmicki <adam@cfar.umd.edu>, 1st January 1997.
  33. #
  34. # Added ability to stop at a given version number
  35. # Put the full version number (i.e. 2.3.31) as the last parameter
  36. # Dave Gilbert <linux@treblig.org>, 11th December 1999.
  37. # Fixed previous patch so that if we are already at the correct version
  38. # not to patch up.
  39. #
  40. # Added -ac option, use -ac or -ac9 (say) to stop at a particular version
  41. # Dave Gilbert <linux@treblig.org>, 29th September 2001.
  42. #
  43. # Add support for (use of) EXTRAVERSION (to support 2.6.8.x, e.g.);
  44. # update usage message;
  45. # fix some whitespace damage;
  46. # be smarter about stopping when current version is larger than requested;
  47. # Randy Dunlap <rddunlap@osdl.org>, 2004-AUG-18.
  48. # Set directories from arguments, or use defaults.
  49. sourcedir=${1-/usr/src/linux}
  50. patchdir=${2-.}
  51. stopvers=${3-default}
  52. if [ "$1" == -h -o "$1" == --help -o ! -r "$sourcedir/Makefile" ]; then
  53. cat << USAGE
  54. usage: patch-kernel [-h] [ sourcedir [ patchdir [ stopversion ] [ -acxx ] ] ]
  55. source directory defaults to /usr/src/linux,
  56. patch directory defaults to the current directory,
  57. stopversion defaults to <all in patchdir>.
  58. USAGE
  59. exit 1
  60. fi
  61. # See if we have any -ac options
  62. for PARM in $*
  63. do
  64. case $PARM in
  65. -ac*)
  66. gotac=$PARM;
  67. esac;
  68. done
  69. # ---------------------------------------------------------------------------
  70. # Find a file, first parameter is basename of file
  71. # it tries many compression mechanisms and sets variables to say how to get it
  72. findFile () {
  73. filebase=$1;
  74. if [ -r ${filebase}.gz ]; then
  75. ext=".gz"
  76. name="gzip"
  77. uncomp="gunzip -dc"
  78. elif [ -r ${filebase}.bz ]; then
  79. ext=".bz"
  80. name="bzip"
  81. uncomp="bunzip -dc"
  82. elif [ -r ${filebase}.bz2 ]; then
  83. ext=".bz2"
  84. name="bzip2"
  85. uncomp="bunzip2 -dc"
  86. elif [ -r ${filebase}.zip ]; then
  87. ext=".zip"
  88. name="zip"
  89. uncomp="unzip -d"
  90. elif [ -r ${filebase}.Z ]; then
  91. ext=".Z"
  92. name="uncompress"
  93. uncomp="uncompress -c"
  94. elif [ -r ${filebase} ]; then
  95. ext=""
  96. name="plaintext"
  97. uncomp="cat"
  98. else
  99. return 1;
  100. fi
  101. return 0;
  102. }
  103. # ---------------------------------------------------------------------------
  104. # Apply a patch and check it goes in cleanly
  105. # First param is patch name (e.g. patch-2.4.9-ac5) - without path or extension
  106. applyPatch () {
  107. echo -n "Applying $1 (${name})... "
  108. if $uncomp ${patchdir}/$1${ext} | patch -p1 -s -N -E -d $sourcedir
  109. then
  110. echo "done."
  111. else
  112. echo "failed. Clean up yourself."
  113. return 1;
  114. fi
  115. if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]
  116. then
  117. echo "Aborting. Reject files found."
  118. return 1;
  119. fi
  120. # Remove backup files
  121. find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;
  122. return 0;
  123. }
  124. # set current VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION
  125. TMPFILE=`mktemp .tmpver.XXXXXX` || { echo "cannot make temp file" ; exit 1; }
  126. grep -E "^(VERSION|PATCHLEVEL|SUBLEVEL|EXTRAVERSION)" $sourcedir/Makefile > $TMPFILE
  127. tr -d [:blank:] < $TMPFILE > $TMPFILE.1
  128. source $TMPFILE.1
  129. rm -f $TMPFILE*
  130. if [ -z "$VERSION" -o -z "$PATCHLEVEL" -o -z "$SUBLEVEL" ]
  131. then
  132. echo "unable to determine current kernel version" >&2
  133. exit 1
  134. fi
  135. NAME=`grep ^NAME $sourcedir/Makefile`
  136. NAME=${NAME##*=}
  137. echo "Current kernel version is $VERSION.$PATCHLEVEL.$SUBLEVEL${EXTRAVERSION} ($NAME)"
  138. # strip EXTRAVERSION to just a number (drop leading '.' and trailing additions)
  139. EXTRAVER=
  140. if [ x$EXTRAVERSION != "x" ]
  141. then
  142. if [ ${EXTRAVERSION:0:1} == "." ]; then
  143. EXTRAVER=${EXTRAVERSION:1}
  144. else
  145. EXTRAVER=$EXTRAVERSION
  146. fi
  147. EXTRAVER=${EXTRAVER%%[[:punct:]]*}
  148. #echo "patch-kernel: changing EXTRAVERSION from $EXTRAVERSION to $EXTRAVER"
  149. fi
  150. #echo "stopvers=$stopvers"
  151. if [ $stopvers != "default" ]; then
  152. STOPSUBLEVEL=`echo $stopvers | cut -d. -f3`
  153. STOPEXTRA=`echo $stopvers | cut -d. -f4`
  154. #echo "STOPSUBLEVEL=$STOPSUBLEVEL, STOPEXTRA=$STOPEXTRA"
  155. else
  156. STOPSUBLEVEL=9999
  157. STOPEXTRA=9999
  158. fi
  159. while : # incrementing SUBLEVEL (s in v.p.s)
  160. do
  161. if [ x$EXTRAVER != "x" ]; then
  162. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$EXTRAVER"
  163. else
  164. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  165. fi
  166. if [ $stopvers == $CURRENTFULLVERSION ]; then
  167. echo "Stopping at $CURRENTFULLVERSION base as requested."
  168. break
  169. fi
  170. while : # incrementing EXTRAVER (x in v.p.s.x)
  171. do
  172. EXTRAVER=$((EXTRAVER + 1))
  173. FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$EXTRAVER"
  174. #echo "... trying $FULLVERSION ..."
  175. patch=patch-$FULLVERSION
  176. # See if the file exists and find extension
  177. findFile $patchdir/${patch} || break
  178. # Apply the patch and check all is OK
  179. applyPatch $patch || break
  180. continue 2
  181. done
  182. EXTRAVER=
  183. SUBLEVEL=$((SUBLEVEL + 1))
  184. FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  185. #echo "___ trying $FULLVERSION ___"
  186. if [ $((SUBLEVEL)) -gt $((STOPSUBLEVEL)) ]; then
  187. echo "Stopping since sublevel ($SUBLEVEL) is beyond stop-sublevel ($STOPSUBLEVEL)"
  188. exit 1
  189. fi
  190. patch=patch-$FULLVERSION
  191. # See if the file exists and find extension
  192. findFile $patchdir/${patch} || break
  193. # Apply the patch and check all is OK
  194. applyPatch $patch || break
  195. done
  196. #echo "base all done"
  197. if [ x$gotac != x ]; then
  198. # Out great user wants the -ac patches
  199. # They could have done -ac (get latest) or -acxx where xx=version they want
  200. if [ $gotac == "-ac" ]; then
  201. # They want the latest version
  202. HIGHESTPATCH=0
  203. for PATCHNAMES in $patchdir/patch-${CURRENTFULLVERSION}-ac*\.*
  204. do
  205. ACVALUE=`echo $PATCHNAMES | sed -e 's/^.*patch-[0-9.]*-ac\([0-9]*\).*/\1/'`
  206. # Check it is actually a recognised patch type
  207. findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${ACVALUE} || break
  208. if [ $ACVALUE -gt $HIGHESTPATCH ]; then
  209. HIGHESTPATCH=$ACVALUE
  210. fi
  211. done
  212. if [ $HIGHESTPATCH -ne 0 ]; then
  213. findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH} || break
  214. applyPatch patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH}
  215. else
  216. echo "No -ac patches found"
  217. fi
  218. else
  219. # They want an exact version
  220. findFile $patchdir/patch-${CURRENTFULLVERSION}${gotac} || {
  221. echo "Sorry, I couldn't find the $gotac patch for $CURRENTFULLVERSION. Hohum."
  222. exit 1
  223. }
  224. applyPatch patch-${CURRENTFULLVERSION}${gotac}
  225. fi
  226. fi