patch-kernel 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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}.xz ]; then
  110. ext=".xz"
  111. name="xz"
  112. uncomp="xz -dc"
  113. elif [ -r ${filebase}.zip ]; then
  114. ext=".zip"
  115. name="zip"
  116. uncomp="unzip -d"
  117. elif [ -r ${filebase}.Z ]; then
  118. ext=".Z"
  119. name="uncompress"
  120. uncomp="uncompress -c"
  121. elif [ -r ${filebase} ]; then
  122. ext=""
  123. name="plaintext"
  124. uncomp="cat"
  125. else
  126. return 1;
  127. fi
  128. return 0;
  129. }
  130. # ---------------------------------------------------------------------------
  131. # Apply a patch and check it goes in cleanly
  132. # First param is patch name (e.g. patch-2.4.9-ac5) - without path or extension
  133. applyPatch () {
  134. echo -n "Applying $1 (${name})... "
  135. if $uncomp ${patchdir}/$1${ext} | patch -p1 -s -N -E -d $sourcedir
  136. then
  137. echo "done."
  138. else
  139. echo "failed. Clean up yourself."
  140. return 1;
  141. fi
  142. if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]
  143. then
  144. echo "Aborting. Reject files found."
  145. return 1;
  146. fi
  147. # Remove backup files
  148. find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;
  149. return 0;
  150. }
  151. # ---------------------------------------------------------------------------
  152. # arg1 is patch filename
  153. reversePatch () {
  154. echo -n "Reversing $1 (${name}) ... "
  155. if $uncomp ${patchdir}/"$1"${ext} | patch -p1 -Rs -N -E -d $sourcedir
  156. then
  157. echo "done."
  158. else
  159. echo "failed. Clean it up."
  160. exit 1
  161. fi
  162. if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]
  163. then
  164. echo "Aborting. Reject files found."
  165. return 1
  166. fi
  167. # Remove backup files
  168. find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;
  169. return 0
  170. }
  171. # set current VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION
  172. # force $TMPFILEs below to be in local directory: a slash character prevents
  173. # the dot command from using the search path.
  174. TMPFILE=`mktemp ./.tmpver.XXXXXX` || { echo "cannot make temp file" ; exit 1; }
  175. grep -E "^(VERSION|PATCHLEVEL|SUBLEVEL|EXTRAVERSION)" $sourcedir/Makefile > $TMPFILE
  176. tr -d [:blank:] < $TMPFILE > $TMPFILE.1
  177. . $TMPFILE.1
  178. rm -f $TMPFILE*
  179. if [ -z "$VERSION" -o -z "$PATCHLEVEL" -o -z "$SUBLEVEL" ]
  180. then
  181. echo "unable to determine current kernel version" >&2
  182. exit 1
  183. fi
  184. NAME=`grep ^NAME $sourcedir/Makefile`
  185. NAME=${NAME##*=}
  186. echo "Current kernel version is $VERSION.$PATCHLEVEL.$SUBLEVEL${EXTRAVERSION} ($NAME)"
  187. # strip EXTRAVERSION to just a number (drop leading '.' and trailing additions)
  188. EXTRAVER=
  189. if [ x$EXTRAVERSION != "x" ]
  190. then
  191. EXTRAVER=${EXTRAVERSION#.}
  192. EXTRAVER=${EXTRAVER%%[[:punct:]]*}
  193. #echo "$PNAME: changing EXTRAVERSION from $EXTRAVERSION to $EXTRAVER"
  194. fi
  195. #echo "stopvers=$stopvers"
  196. if [ $stopvers != "default" ]; then
  197. STOPSUBLEVEL=`echo $stopvers | cut -d. -f3`
  198. STOPEXTRA=`echo $stopvers | cut -d. -f4`
  199. STOPFULLVERSION=${stopvers%%.$STOPEXTRA}
  200. #echo "#___STOPSUBLEVEL=/$STOPSUBLEVEL/, STOPEXTRA=/$STOPEXTRA/"
  201. else
  202. STOPSUBLEVEL=9999
  203. STOPEXTRA=9999
  204. fi
  205. # This all assumes a 2.6.x[.y] kernel tree.
  206. # Don't allow backwards/reverse patching.
  207. if [ $STOPSUBLEVEL -lt $SUBLEVEL ]; then
  208. backwards
  209. fi
  210. if [ x$EXTRAVER != "x" ]; then
  211. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$EXTRAVER"
  212. else
  213. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  214. fi
  215. if [ x$EXTRAVER != "x" ]; then
  216. echo "backing up to: $VERSION.$PATCHLEVEL.$SUBLEVEL"
  217. patch="patch-${CURRENTFULLVERSION}"
  218. findFile $patchdir/${patch} || noFile ${patch}
  219. reversePatch ${patch} || exit 1
  220. fi
  221. # now current is 2.6.x, with no EXTRA applied,
  222. # so update to target SUBLEVEL (2.6.SUBLEVEL)
  223. # and then to target EXTRAVER (2.6.SUB.EXTRAVER) if requested.
  224. # If not ending sublevel is specified, it is incremented until
  225. # no further sublevels are found.
  226. if [ $STOPSUBLEVEL -gt $SUBLEVEL ]; then
  227. while : # incrementing SUBLEVEL (s in v.p.s)
  228. do
  229. CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  230. EXTRAVER=
  231. if [ x$STOPFULLVERSION = x$CURRENTFULLVERSION ]; then
  232. echo "Stopping at $CURRENTFULLVERSION base as requested."
  233. break
  234. fi
  235. SUBLEVEL=$(($SUBLEVEL + 1))
  236. FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
  237. #echo "#___ trying $FULLVERSION ___"
  238. if [ $(($SUBLEVEL)) -gt $(($STOPSUBLEVEL)) ]; then
  239. echo "Stopping since sublevel ($SUBLEVEL) is beyond stop-sublevel ($STOPSUBLEVEL)"
  240. exit 1
  241. fi
  242. patch=patch-$FULLVERSION
  243. # See if the file exists and find extension
  244. findFile $patchdir/${patch} || noFile ${patch}
  245. # Apply the patch and check all is OK
  246. applyPatch $patch || break
  247. done
  248. #echo "#___sublevel all done"
  249. fi
  250. # There is no incremental searching for extraversion...
  251. if [ "$STOPEXTRA" != "" ]; then
  252. while : # just to allow break
  253. do
  254. # apply STOPEXTRA directly (not incrementally) (x in v.p.s.x)
  255. FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$STOPEXTRA"
  256. #echo "#... trying $FULLVERSION ..."
  257. patch=patch-$FULLVERSION
  258. # See if the file exists and find extension
  259. findFile $patchdir/${patch} || noFile ${patch}
  260. # Apply the patch and check all is OK
  261. applyPatch $patch || break
  262. #echo "#___extraver all done"
  263. break
  264. done
  265. fi
  266. if [ x$gotac != x ]; then
  267. # Out great user wants the -ac patches
  268. # They could have done -ac (get latest) or -acxx where xx=version they want
  269. if [ $gotac = "-ac" ]; then
  270. # They want the latest version
  271. HIGHESTPATCH=0
  272. for PATCHNAMES in $patchdir/patch-${CURRENTFULLVERSION}-ac*\.*
  273. do
  274. ACVALUE=`echo $PATCHNAMES | sed -e 's/^.*patch-[0-9.]*-ac\([0-9]*\).*/\1/'`
  275. # Check it is actually a recognised patch type
  276. findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${ACVALUE} || break
  277. if [ $ACVALUE -gt $HIGHESTPATCH ]; then
  278. HIGHESTPATCH=$ACVALUE
  279. fi
  280. done
  281. if [ $HIGHESTPATCH -ne 0 ]; then
  282. findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH} || break
  283. applyPatch patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH}
  284. else
  285. echo "No -ac patches found"
  286. fi
  287. else
  288. # They want an exact version
  289. findFile $patchdir/patch-${CURRENTFULLVERSION}${gotac} || {
  290. echo "Sorry, I couldn't find the $gotac patch for $CURRENTFULLVERSION. Hohum."
  291. exit 1
  292. }
  293. applyPatch patch-${CURRENTFULLVERSION}${gotac}
  294. fi
  295. fi