patch-kernel 9.7 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. TMPFILE=`mktemp .tmpver.XXXXXX` || { echo "cannot make temp file" ; exit 1; }
  169. grep -E "^(VERSION|PATCHLEVEL|SUBLEVEL|EXTRAVERSION)" $sourcedir/Makefile > $TMPFILE
  170. tr -d [:blank:] < $TMPFILE > $TMPFILE.1
  171. source $TMPFILE.1
  172. rm -f $TMPFILE*
  173. if [ -z "$VERSION" -o -z "$PATCHLEVEL" -o -z "$SUBLEVEL" ]
  174. then
  175. echo "unable to determine current kernel version" >&2
  176. exit 1
  177. fi
  178. NAME=`grep ^NAME $sourcedir/Makefile`
  179. NAME=${NAME##*=}
  180. echo "Current kernel version is $VERSION.$PATCHLEVEL.$SUBLEVEL${EXTRAVERSION} ($NAME)"
  181. # strip EXTRAVERSION to just a number (drop leading '.' and trailing additions)
  182. EXTRAVER=
  183. if [ x$EXTRAVERSION != "x" ]
  184. then
  185. if [ ${EXTRAVERSION:0:1} == "." ]; then
  186. EXTRAVER=${EXTRAVERSION:1}
  187. else
  188. EXTRAVER=$EXTRAVERSION
  189. fi
  190. EXTRAVER=${EXTRAVER%%[[:punct:]]*}
  191. #echo "$PNAME: changing EXTRAVERSION from $EXTRAVERSION to $EXTRAVER"
  192. fi
  193. #echo "stopvers=$stopvers"
  194. if [ $stopvers != "default" ]; then
  195. STOPSUBLEVEL=`echo $stopvers | cut -d. -f3`
  196. STOPEXTRA=`echo $stopvers | cut -d. -f4`
  197. #echo "#___STOPSUBLEVEL=/$STOPSUBLEVEL/, STOPEXTRA=/$STOPEXTRA/"
  198. else
  199. STOPSUBLEVEL=9999
  200. STOPEXTRA=9999
  201. fi
  202. # This all assumes a 2.6.x[.y] kernel tree.
  203. # Don't allow backwards/reverse patching.
  204. if [ $STOPSUBLEVEL -lt $SUBLEVEL ]; then
  205. backwards
  206. fi
  207. if [ x$EXTRAVER != "x" ]; then
  208. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$EXTRAVER"
  209. else
  210. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  211. fi
  212. if [ x$EXTRAVER != "x" ]; then
  213. echo "backing up to: $VERSION.$PATCHLEVEL.$SUBLEVEL"
  214. patch="patch-${CURRENTFULLVERSION}"
  215. findFile $patchdir/${patch} || noFile ${patch}
  216. reversePatch ${patch} || exit 1
  217. fi
  218. # now current is 2.6.x, with no EXTRA applied,
  219. # so update to target SUBLEVEL (2.6.SUBLEVEL)
  220. # and then to target EXTRAVER (2.6.SUB.EXTRAVER) if requested.
  221. # If not ending sublevel is specified, it is incremented until
  222. # no further sublevels are found.
  223. if [ $STOPSUBLEVEL -gt $SUBLEVEL ]; then
  224. while : # incrementing SUBLEVEL (s in v.p.s)
  225. do
  226. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  227. EXTRAVER=
  228. if [ $stopvers == $CURRENTFULLVERSION ]; then
  229. echo "Stopping at $CURRENTFULLVERSION base as requested."
  230. break
  231. fi
  232. SUBLEVEL=$((SUBLEVEL + 1))
  233. FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  234. #echo "#___ trying $FULLVERSION ___"
  235. if [ $((SUBLEVEL)) -gt $((STOPSUBLEVEL)) ]; then
  236. echo "Stopping since sublevel ($SUBLEVEL) is beyond stop-sublevel ($STOPSUBLEVEL)"
  237. exit 1
  238. fi
  239. patch=patch-$FULLVERSION
  240. # See if the file exists and find extension
  241. findFile $patchdir/${patch} || noFile ${patch}
  242. # Apply the patch and check all is OK
  243. applyPatch $patch || break
  244. done
  245. #echo "#___sublevel all done"
  246. fi
  247. # There is no incremental searching for extraversion...
  248. if [ "$STOPEXTRA" != "" ]; then
  249. while : # just to allow break
  250. do
  251. # apply STOPEXTRA directly (not incrementally) (x in v.p.s.x)
  252. FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$STOPEXTRA"
  253. #echo "#... trying $FULLVERSION ..."
  254. patch=patch-$FULLVERSION
  255. # See if the file exists and find extension
  256. findFile $patchdir/${patch} || noFile ${patch}
  257. # Apply the patch and check all is OK
  258. applyPatch $patch || break
  259. #echo "#___extraver all done"
  260. break
  261. done
  262. fi
  263. if [ x$gotac != x ]; then
  264. # Out great user wants the -ac patches
  265. # They could have done -ac (get latest) or -acxx where xx=version they want
  266. if [ $gotac == "-ac" ]; then
  267. # They want the latest version
  268. HIGHESTPATCH=0
  269. for PATCHNAMES in $patchdir/patch-${CURRENTFULLVERSION}-ac*\.*
  270. do
  271. ACVALUE=`echo $PATCHNAMES | sed -e 's/^.*patch-[0-9.]*-ac\([0-9]*\).*/\1/'`
  272. # Check it is actually a recognised patch type
  273. findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${ACVALUE} || break
  274. if [ $ACVALUE -gt $HIGHESTPATCH ]; then
  275. HIGHESTPATCH=$ACVALUE
  276. fi
  277. done
  278. if [ $HIGHESTPATCH -ne 0 ]; then
  279. findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH} || break
  280. applyPatch patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH}
  281. else
  282. echo "No -ac patches found"
  283. fi
  284. else
  285. # They want an exact version
  286. findFile $patchdir/patch-${CURRENTFULLVERSION}${gotac} || {
  287. echo "Sorry, I couldn't find the $gotac patch for $CURRENTFULLVERSION. Hohum."
  288. exit 1
  289. }
  290. applyPatch patch-${CURRENTFULLVERSION}${gotac}
  291. fi
  292. fi