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