patch-kernel 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <rdunlap@xenotime.net>, 2004-AUG-18.
  48. #
  49. # Add better support for (non-incremental) 2.6.x.y patches;
  50. # If an ending version number if not specified, the script automatically
  51. # increments the SUBLEVEL (x in 2.6.x.y) until no more patch files are found;
  52. # however, EXTRAVERSION (y in 2.6.x.y) is never automatically incremented
  53. # but must be specified fully.
  54. #
  55. # patch-kernel does not normally support reverse patching, but does so when
  56. # applying EXTRAVERSION (x.y) patches, so that moving from 2.6.11.y to 2.6.11.z
  57. # is easy and handled by the script (reverse 2.6.11.y and apply 2.6.11.z).
  58. # Randy Dunlap <rdunlap@xenotime.net>, 2005-APR-08.
  59. PNAME=patch-kernel
  60. # Set directories from arguments, or use defaults.
  61. sourcedir=${1-/usr/src/linux}
  62. patchdir=${2-.}
  63. stopvers=${3-default}
  64. if [ "$1" = -h -o "$1" = --help -o ! -r "$sourcedir/Makefile" ]; then
  65. cat << USAGE
  66. usage: $PNAME [-h] [ sourcedir [ patchdir [ stopversion ] [ -acxx ] ] ]
  67. source directory defaults to /usr/src/linux,
  68. patch directory defaults to the current directory,
  69. stopversion defaults to <all in patchdir>.
  70. USAGE
  71. exit 1
  72. fi
  73. # See if we have any -ac options
  74. for PARM in $*
  75. do
  76. case $PARM in
  77. -ac*)
  78. gotac=$PARM;
  79. esac;
  80. done
  81. # ---------------------------------------------------------------------------
  82. # arg1 is filename
  83. noFile () {
  84. echo "cannot find patch file: ${patch}"
  85. exit 1
  86. }
  87. # ---------------------------------------------------------------------------
  88. backwards () {
  89. echo "$PNAME does not support reverse patching"
  90. exit 1
  91. }
  92. # ---------------------------------------------------------------------------
  93. # Find a file, first parameter is basename of file
  94. # it tries many compression mechanisms and sets variables to say how to get it
  95. findFile () {
  96. filebase=$1;
  97. if [ -r ${filebase}.gz ]; then
  98. ext=".gz"
  99. name="gzip"
  100. uncomp="gunzip -dc"
  101. elif [ -r ${filebase}.bz ]; then
  102. ext=".bz"
  103. name="bzip"
  104. uncomp="bunzip -dc"
  105. elif [ -r ${filebase}.bz2 ]; then
  106. ext=".bz2"
  107. name="bzip2"
  108. uncomp="bunzip2 -dc"
  109. elif [ -r ${filebase}.zip ]; then
  110. ext=".zip"
  111. name="zip"
  112. uncomp="unzip -d"
  113. elif [ -r ${filebase}.Z ]; then
  114. ext=".Z"
  115. name="uncompress"
  116. uncomp="uncompress -c"
  117. elif [ -r ${filebase} ]; then
  118. ext=""
  119. name="plaintext"
  120. uncomp="cat"
  121. else
  122. return 1;
  123. fi
  124. return 0;
  125. }
  126. # ---------------------------------------------------------------------------
  127. # Apply a patch and check it goes in cleanly
  128. # First param is patch name (e.g. patch-2.4.9-ac5) - without path or extension
  129. applyPatch () {
  130. echo -n "Applying $1 (${name})... "
  131. if $uncomp ${patchdir}/$1${ext} | patch -p1 -s -N -E -d $sourcedir
  132. then
  133. echo "done."
  134. else
  135. echo "failed. Clean up yourself."
  136. return 1;
  137. fi
  138. if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]
  139. then
  140. echo "Aborting. Reject files found."
  141. return 1;
  142. fi
  143. # Remove backup files
  144. find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;
  145. return 0;
  146. }
  147. # ---------------------------------------------------------------------------
  148. # arg1 is patch filename
  149. reversePatch () {
  150. echo -n "Reversing $1 (${name}) ... "
  151. if $uncomp ${patchdir}/"$1"${ext} | patch -p1 -Rs -N -E -d $sourcedir
  152. then
  153. echo "done."
  154. else
  155. echo "failed. Clean it up."
  156. exit 1
  157. fi
  158. if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]
  159. then
  160. echo "Aborting. Reject files found."
  161. return 1
  162. fi
  163. # Remove backup files
  164. find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;
  165. return 0
  166. }
  167. # set current VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION
  168. # force $TMPFILEs below to be in local directory: a slash character prevents
  169. # the dot command from using the search path.
  170. TMPFILE=`mktemp ./.tmpver.XXXXXX` || { echo "cannot make temp file" ; exit 1; }
  171. grep -E "^(VERSION|PATCHLEVEL|SUBLEVEL|EXTRAVERSION)" $sourcedir/Makefile > $TMPFILE
  172. tr -d [:blank:] < $TMPFILE > $TMPFILE.1
  173. . $TMPFILE.1
  174. rm -f $TMPFILE*
  175. if [ -z "$VERSION" -o -z "$PATCHLEVEL" -o -z "$SUBLEVEL" ]
  176. then
  177. echo "unable to determine current kernel version" >&2
  178. exit 1
  179. fi
  180. NAME=`grep ^NAME $sourcedir/Makefile`
  181. NAME=${NAME##*=}
  182. echo "Current kernel version is $VERSION.$PATCHLEVEL.$SUBLEVEL${EXTRAVERSION} ($NAME)"
  183. # strip EXTRAVERSION to just a number (drop leading '.' and trailing additions)
  184. EXTRAVER=
  185. if [ x$EXTRAVERSION != "x" ]
  186. then
  187. EXTRAVER=${EXTRAVERSION#.}
  188. EXTRAVER=${EXTRAVER%%[[:punct:]]*}
  189. #echo "$PNAME: changing EXTRAVERSION from $EXTRAVERSION to $EXTRAVER"
  190. fi
  191. #echo "stopvers=$stopvers"
  192. if [ $stopvers != "default" ]; then
  193. STOPSUBLEVEL=`echo $stopvers | cut -d. -f3`
  194. STOPEXTRA=`echo $stopvers | cut -d. -f4`
  195. STOPFULLVERSION=${stopvers%%.$STOPEXTRA}
  196. #echo "#___STOPSUBLEVEL=/$STOPSUBLEVEL/, STOPEXTRA=/$STOPEXTRA/"
  197. else
  198. STOPSUBLEVEL=9999
  199. STOPEXTRA=9999
  200. fi
  201. # This all assumes a 2.6.x[.y] kernel tree.
  202. # Don't allow backwards/reverse patching.
  203. if [ $STOPSUBLEVEL -lt $SUBLEVEL ]; then
  204. backwards
  205. fi
  206. if [ x$EXTRAVER != "x" ]; then
  207. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$EXTRAVER"
  208. else
  209. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  210. fi
  211. if [ x$EXTRAVER != "x" ]; then
  212. echo "backing up to: $VERSION.$PATCHLEVEL.$SUBLEVEL"
  213. patch="patch-${CURRENTFULLVERSION}"
  214. findFile $patchdir/${patch} || noFile ${patch}
  215. reversePatch ${patch} || exit 1
  216. fi
  217. # now current is 2.6.x, with no EXTRA applied,
  218. # so update to target SUBLEVEL (2.6.SUBLEVEL)
  219. # and then to target EXTRAVER (2.6.SUB.EXTRAVER) if requested.
  220. # If not ending sublevel is specified, it is incremented until
  221. # no further sublevels are found.
  222. if [ $STOPSUBLEVEL -gt $SUBLEVEL ]; then
  223. while : # incrementing SUBLEVEL (s in v.p.s)
  224. do
  225. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  226. EXTRAVER=
  227. if [ x$STOPFULLVERSION = x$CURRENTFULLVERSION ]; then
  228. echo "Stopping at $CURRENTFULLVERSION base as requested."
  229. break
  230. fi
  231. SUBLEVEL=$(($SUBLEVEL + 1))
  232. FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  233. #echo "#___ trying $FULLVERSION ___"
  234. if [ $(($SUBLEVEL)) -gt $(($STOPSUBLEVEL)) ]; then
  235. echo "Stopping since sublevel ($SUBLEVEL) is beyond stop-sublevel ($STOPSUBLEVEL)"
  236. exit 1
  237. fi
  238. patch=patch-$FULLVERSION
  239. # See if the file exists and find extension
  240. findFile $patchdir/${patch} || noFile ${patch}
  241. # Apply the patch and check all is OK
  242. applyPatch $patch || break
  243. done
  244. #echo "#___sublevel all done"
  245. fi
  246. # There is no incremental searching for extraversion...
  247. if [ "$STOPEXTRA" != "" ]; then
  248. while : # just to allow break
  249. do
  250. # apply STOPEXTRA directly (not incrementally) (x in v.p.s.x)
  251. FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$STOPEXTRA"
  252. #echo "#... trying $FULLVERSION ..."
  253. patch=patch-$FULLVERSION
  254. # See if the file exists and find extension
  255. findFile $patchdir/${patch} || noFile ${patch}
  256. # Apply the patch and check all is OK
  257. applyPatch $patch || break
  258. #echo "#___extraver all done"
  259. break
  260. done
  261. fi
  262. if [ x$gotac != x ]; then
  263. # Out great user wants the -ac patches
  264. # They could have done -ac (get latest) or -acxx where xx=version they want
  265. if [ $gotac = "-ac" ]; then
  266. # They want the latest version
  267. HIGHESTPATCH=0
  268. for PATCHNAMES in $patchdir/patch-${CURRENTFULLVERSION}-ac*\.*
  269. do
  270. ACVALUE=`echo $PATCHNAMES | sed -e 's/^.*patch-[0-9.]*-ac\([0-9]*\).*/\1/'`
  271. # Check it is actually a recognised patch type
  272. findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${ACVALUE} || break
  273. if [ $ACVALUE -gt $HIGHESTPATCH ]; then
  274. HIGHESTPATCH=$ACVALUE
  275. fi
  276. done
  277. if [ $HIGHESTPATCH -ne 0 ]; then
  278. findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH} || break
  279. applyPatch patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH}
  280. else
  281. echo "No -ac patches found"
  282. fi
  283. else
  284. # They want an exact version
  285. findFile $patchdir/patch-${CURRENTFULLVERSION}${gotac} || {
  286. echo "Sorry, I couldn't find the $gotac patch for $CURRENTFULLVERSION. Hohum."
  287. exit 1
  288. }
  289. applyPatch patch-${CURRENTFULLVERSION}${gotac}
  290. fi
  291. fi