genkernel: Add --kconfig to set specific kernel config options
[genkernel.git] / gen_funcs.sh
1 #!/bin/bash
2 # $Id$
3
4 isTrue() {
5         case "$1" in
6                 [Tt][Rr][Uu][Ee])
7                         return 0
8                 ;;
9                 [Tt])
10                         return 0
11                 ;;
12                 [Yy][Ee][Ss])
13                         return 0
14                 ;;
15                 [Yy])
16                         return 0
17                 ;;
18                 1)
19                         return 0
20                 ;;
21         esac
22         return 1
23 }
24
25 setColorVars() {
26 if isTrue ${USECOLOR}
27 then
28         GOOD=$'\e[32;01m'
29         WARN=$'\e[33;01m'
30         BAD=$'\e[31;01m'
31         NORMAL=$'\e[0m'
32         BOLD=$'\e[0;01m'
33         UNDER=$'\e[4m'
34 else
35         GOOD=''
36         WARN=''
37         BAD=''
38         NORMAL=''
39         BOLD=''
40         UNDER=''
41 fi
42 }
43 setColorVars
44
45 dump_debugcache() {
46         TODEBUGCACHE=0
47         echo "${DEBUGCACHE}" >> ${LOGFILE}
48 }
49
50 # print_info(loglevel, print [, newline [, prefixline [, forcefile ] ] ])
51 print_info() {
52         local NEWLINE=1
53         local FORCEFILE=0
54         local PREFIXLINE=1
55         local SCRPRINT=0
56         local STR=''
57
58         # NOT ENOUGH ARGS
59         if [ "$#" -lt '2' ] ; then return 1; fi
60
61         # IF 3 OR MORE ARGS, CHECK IF WE WANT A NEWLINE AFTER PRINT
62         if [ "$#" -gt '2' ]
63         then
64                 if isTrue "$3"
65                 then
66                         NEWLINE='1';
67                 else
68                         NEWLINE='0';
69                 fi
70         fi
71
72         # IF 4 OR MORE ARGS, CHECK IF WE WANT TO PREFIX WITH A *
73         if [ "$#" -gt '3' ]
74         then
75                 if isTrue "$4"
76                 then
77                         PREFIXLINE='1'
78                 else
79                         PREFIXLINE='0'
80                 fi
81         fi
82
83         # IF 5 OR MORE ARGS, CHECK IF WE WANT TO FORCE OUTPUT TO DEBUG
84         # FILE EVEN IF IT DOESN'T MEET THE MINIMUM DEBUG REQS
85         if [ "$#" -gt '4' ]
86         then
87                 if isTrue "$5"
88                 then
89                         FORCEFILE='1'
90                 else
91                         FORCEFILE='0'
92                 fi
93         fi
94
95         # PRINT TO SCREEN ONLY IF PASSED LOGLEVEL IS HIGHER THAN
96         # OR EQUAL TO SET DEBUG LEVEL
97         if [ "$1" -lt "${LOGLEVEL}" -o "$1" = "${LOGLEVEL}" ]
98         then
99                 SCRPRINT='1'
100         fi
101
102         # RETURN IF NOT OUTPUTTING ANYWHERE
103         if [ "${SCRPRINT}" != '1' -a "${FORCEFILE}" != '1' ]
104         then
105                 return 0
106         fi
107
108         # STRUCTURE DATA TO BE OUTPUT TO SCREEN, AND OUTPUT IT
109         if [ "${SCRPRINT}" = '1' ]
110         then
111                 if [ "${PREFIXLINE}" = '1' ]
112                 then
113                         STR="${GOOD}*${NORMAL} ${2}"
114                 else
115                         STR="${2}"
116                 fi
117
118                 if [ "${NEWLINE}" = '0' ]
119                 then
120                         echo -ne "${STR}"
121                 else
122                         echo "${STR}"
123                 fi
124         fi
125
126         # STRUCTURE DATA TO BE OUTPUT TO FILE, AND OUTPUT IT
127         if [ "${SCRPRINT}" = '1' -o "${FORCEFILE}" = '1' ]
128         then
129                 STRR=${2//${WARN}/}
130                 STRR=${STRR//${BAD}/}
131                 STRR=${STRR//${BOLD}/}
132                 STRR=${STRR//${NORMAL}/}
133
134                 if [ "${PREFIXLINE}" = '1' ]
135                 then
136                         STR="* ${STRR}"
137                 else
138                         STR="${STRR}"
139                 fi
140
141                 if [ "${NEWLINE}" = '0' ]
142                 then
143                         if [ "${TODEBUGCACHE}" = '1' ]; then
144                                 DEBUGCACHE="${DEBUGCACHE}${STR}"
145                         else
146                                 echo -ne "${STR}" >> ${LOGFILE}
147                         fi
148                 else
149                         if [ "${TODEBUGCACHE}" = '1' ]; then
150                                 DEBUGCACHE="${DEBUGCACHE}${STR}"$'\n'
151                         else
152                                 echo "${STR}" >> ${LOGFILE}
153                         fi
154                 fi
155         fi
156
157         return 0
158 }
159
160 print_error()
161 {
162         GOOD=${BAD} print_info "$@"
163 }
164
165 print_warning()
166 {
167         GOOD=${WARN} print_info "$@"
168 }
169
170 # var_replace(var_name, var_value, string)
171 # $1 = variable name
172 # $2 = variable value
173 # $3 = string
174
175 var_replace()
176 {
177   # Escape '\' and '.' in $2 to make it safe to use
178   # in the later sed expression
179   local SAFE_VAR
180   SAFE_VAR=`echo "${2}" | sed -e 's/\([\/\.]\)/\\\\\\1/g'`
181
182   echo "${3}" | sed -e "s/%%${1}%%/${SAFE_VAR}/g" -
183 }
184
185 arch_replace() {
186   var_replace "ARCH" "${ARCH}" "${1}"
187 }
188
189 cache_replace() {
190   var_replace "CACHE" "${CACHE_DIR}" "${1}"
191 }
192
193 clear_log() {
194     if [ -f "${LOGFILE}" ]
195     then
196         (echo > "${LOGFILE}") 2>/dev/null || small_die "Genkernel: Could not write to ${LOGFILE}."
197     fi
198 }
199
200 gen_die() {
201         dump_debugcache
202
203         if [ "$#" -gt '0' ]
204         then
205                 print_error 1 "ERROR: ${1}"
206         fi
207         print_error 1 ''
208         print_error 1 "-- Grepping log... --"
209         print_error 1 ''
210
211         if isTrue ${USECOLOR}
212         then
213                 GREP_COLOR='1' grep -B5 -E --colour=always "([Ww][Aa][Rr][Nn][Ii][Nn][Gg]|[Ee][Rr][Rr][Oo][Rr][ :,!]|[Ff][Aa][Ii][Ll][Ee]?[Dd]?)" ${LOGFILE} \
214                                 | sed -s "s|^\(*\)\?|${BAD}*${NORMAL}|"
215         else
216                 grep -B5 -E "([Ww][Aa][Rr][Nn][Ii][Nn][Gg]|[Ee][Rr][Rr][Oo][Rr][ :,!]|[Ff][Aa][Ii][Ll][Ee]?[Dd]?)" ${LOGFILE}
217         fi
218         print_error 1 ''
219         print_error 1 "-- End log... --"
220         print_error 1 ''
221         print_error 1 "Please consult ${LOGFILE} for more information and any"
222         print_error 1 "errors that were reported above."
223         print_error 1 ''
224         print_error 1 "Report any genkernel bugs to bugs.gentoo.org and"
225         print_error 1 "assign your bug to genkernel@gentoo.org. Please include"
226         print_error 1 "as much information as you can in your bug report; attaching"
227         print_error 1 "${LOGFILE} so that your issue can be dealt with effectively."
228         print_error 1 ''
229         print_error 1 'Please do *not* report compilation failures as genkernel bugs!'
230         print_error 1 ''
231
232         # Cleanup temp dirs and caches if requested
233         cleanup
234         exit 1
235 }
236
237 isBootRO()
238 {
239         return $(awk '( $2 == "'${BOOTDIR}'" && $4 ~ /(^|,)ro(,|$)/){ I=1; exit }END{print !I }' /proc/mounts);
240 }
241
242 setup_cache_dir()
243 {
244
245 [ ! -d "${CACHE_DIR}" ] && mkdir -p "${CACHE_DIR}"
246
247 if [ "${CLEAR_CACHE_DIR}" == 'yes' ]
248 then
249         print_info 1 "Clearing cache dir contents from ${CACHE_DIR}"
250         while read i
251         do
252                 print_info 1 "   >> removing ${i}"
253                 rm "${i}"
254         done < <(find "${CACHE_DIR}" -maxdepth 1 -type f -name '*.tar.*' -o -name '*.bz2')
255 fi
256
257 }
258
259 clear_tmpdir()
260 {
261 if isTrue ${CMD_INSTALL}
262 then
263         TMPDIR_CONTENTS=`ls ${TMPDIR}`
264         print_info 1 "Removing tmp dir contents"
265         for i in ${TMPDIR_CONTENTS}
266         do
267                 print_info 1 "   >> removing ${i}"
268                 rm ${TMPDIR}/${i}
269         done
270 fi
271 }
272
273 #
274 # Function to copy various kernel boot image products to the boot directory,
275 # preserve a generation of old images (just like the manual kernel build's
276 # "make install" does), and maintain the symlinks (if enabled).
277 #
278 # Arguments:
279 #     $1  Symlink name.  Symlink on the boot directory. Path not included.
280 #     $2  Source image.  Fully qualified path name of the source image.
281 #     $3  Dest image.    Name of the destination image in the boot directory,
282 #         no path included.  This script pushd's into ${BOOTDIR} in order to
283 #         create relative symlinks just like the manual kernel build.
284 #
285 # - JRG
286 #
287 copy_image_with_preserve() {
288         local symlinkName=$1
289         local newSrceImage=$2
290         local fullDestName=$3
291
292         local currDestImage
293         local prevDestImage
294         local currDestImageExists=0
295         local prevDestImageExists=0
296
297         print_info 4 "Copying new ${symlinkName} image, " 0
298
299         # Old product might be a different version.  If so, we need to read
300         # the symlink to see what it's name is, if there are symlinks.
301         cd ${KERNEL_OUTPUTDIR}
302         if [ "${SYMLINK}" = '1' ]
303         then
304                 print_info 4 "automatically managing symlinks and old images." 1 0
305                 if [ -e "${BOOTDIR}/${symlinkName}" ]
306                 then
307                         # JRG: Do I need a special case here for when the standard symlink
308                         # name is, in fact, not a symlink?
309                         currDestImage=`readlink --no-newline ${BOOTDIR}/${symlinkName}`
310                         print_info 5 "  Current ${symlinkName} symlink exists:"
311                         print_info 5 "    ${currDestImage}"
312                 else
313                         currDestImage="${fullDestName}"
314                         print_info 5 "  Current ${symlinkName} symlink did not exist."
315                         print_info 5 "    Defaulted to: ${currDestImage}"
316                 fi
317                 if [ -e "${BOOTDIR}/${currDestImage}" ]
318                 then
319                         currDestImageExists=1
320                         print_info 5 "  Actual image file exists."
321                 fi
322
323                 if [ -e "${BOOTDIR}/${symlinkName}.old" ]
324                 then
325                         # JRG: Do I need a special case here for when the standard symlink
326                         # name is, in fact, not a symlink?
327                         prevDestImage=`readlink --no-newline ${BOOTDIR}/${symlinkName}.old`
328                         print_info 5 "  Old ${symlinkName} symlink exists:"
329                         print_info 5 "    ${prevDestImage}"
330                 else
331                         prevDestImage="${fullDestName}.old"
332                         print_info 5 "  Old ${symlinkName} symlink did not exist."
333                         print_info 5 "    Defaulted to: ${prevDestImage}"
334                 fi
335                 if [ -e "${BOOTDIR}/${prevDestImage}" ]
336                 then
337                         prevDestImageExists=1
338                         print_info 5 "  Actual old image file exists."
339                 fi
340         else
341                 print_info 4 "symlinks not being handled by genkernel." 1 0
342                 currDestImage="${fullDestName}"
343                 prevDestImage="${fullDestName}.old"
344         fi
345
346         # When symlinks are not being managed by genkernel, old symlinks might
347     # still be useful.  Leave 'em alone unless managed.
348         if [ "${SYMLINK}" = '1' ]
349         then
350                 print_info 5 "  Deleting old symlinks, if any."
351                 rm -f "${BOOTDIR}/${symlinkName}"
352                 rm -f "${BOOTDIR}/${symlinkName}.old"
353         fi
354
355         # We only erase the .old image when it is the exact same version as the
356         # current and new images.  Different version .old (and current) images are
357         # left behind.  This is consistent with how "make install" of the manual
358         # kernel build works.
359         if [ "${currDestImage}" == "${fullDestName}" ]
360         then
361                 #
362                 # Case for new and currrent of the same base version.
363                 #
364                 print_info 5 "  Same base version.  May have to delete old image to make room."
365
366                 if [ "${currDestImageExists}" = '1' ]
367                 then
368                         if [ -e "${BOOTDIR}/${currDestImage}.old" ]
369                         then
370                                 print_info 5 "  Deleting old identical version ${symlinkName}."
371                                 rm -f "${BOOTDIR}/${currDestImage}.old"
372                         fi
373                         print_info 5 "  Moving ${BOOTDIR}/${currDestImage}"
374                         print_info 5 "    to ${BOOTDIR}/${currDestImage}.old"
375                         mv "${BOOTDIR}/${currDestImage}" "${BOOTDIR}/${currDestImage}.old" ||
376                             gen_die "Could not rename the old ${symlinkName} image!"
377                         prevDestImage="${currDestImage}.old"
378                         prevDestImageExists=1
379                 fi
380         else
381                 #
382                 # Case for new / current not of the same base version.
383                 #
384                 print_info 5 "  Different base version.  Do not delete old images."
385                 prevDestImage="${currDestImage}"
386                 currDestImage="${fullDestName}"
387         fi
388
389         print_info 5 "  Copying ${symlinkName}: ${newSrceImage}"
390         print_info 5 "    to ${BOOTDIR}/${currDestImage}"
391         cp "${newSrceImage}" "${BOOTDIR}/${currDestImage}" ||
392             gen_die "Could not copy the ${symlinkName} image to ${BOOTDIR}!"
393
394         if [ "${SYMLINK}" = '1' ]
395         then
396                 print_info 5 "  Make new symlink(s) (from ${BOOTDIR}):"
397                 print_info 5 "    ${symlinkName} -> ${currDestImage}"
398                 pushd ${BOOTDIR} >/dev/null
399                 ln -s "${currDestImage}" "${symlinkName}" || 
400                     gen_die "Could not create the ${symlinkName} symlink!"
401                 if [ "${prevDestImageExists}" = '1' ]
402                 then
403                         print_info 5 "    ${symlinkName}.old -> ${prevDestImage}"
404                         ln -s "${prevDestImage}" "${symlinkName}.old" ||
405                             gen_die "Could not create the ${symlinkName}.old symlink!"
406                 fi
407                 popd >/dev/null
408         fi
409 }
410
411 #
412 # Helper function to allow command line arguments to override configuration
413 # file specified values and to apply defaults.
414 #
415 # Arguments:
416 #     $1  Argument type:
417 #           1  Switch type arguments (e.g., --color / --no-color).
418 #           2  Value type arguments (e.g., --debuglevel=5).
419 #     $2  Config file variable name.
420 #     $3  Command line variable name.
421 #     $4  Default.  If both the config file variable and the command line
422 #         option are not present, then the config file variable is set to
423 #         this default value.  Optional.
424 #
425 # The order of priority of these three sources (highest first) is:
426 #     Command line, which overrides
427 #     Config file (/etc/genkernel.conf), which overrides
428 #     Default.
429 #
430 # Arguments $2 and $3 are variable *names*, not *values*.  This function uses
431 # various forms of indirection to access the values.
432 #
433 # For switch type arguments, all forms of "True" are converted to a numeric 1
434 # and all forms of "False" (everything else, really) to a numeric 0.
435 #
436 # - JRG
437 #
438 set_config_with_override() {
439         local VarType=$1
440         local CfgVar=$2
441         local OverrideVar=$3
442         local Default=$4
443         local Result
444
445         #
446         # Syntax check the function arguments.
447         #
448         case "$VarType" in
449                 BOOL|STRING)
450                         ;;
451                 *)
452                         gen_die "Illegal variable type \"$VarType\" passed to set_config_with_override()."
453                         ;;
454         esac
455
456         if [ -n "${!OverrideVar}" ]
457         then
458                 Result=${!OverrideVar}
459                 if [ -n "${!CfgVar}" ]
460                 then
461                         print_info 5 "  $CfgVar overridden on command line to \"$Result\"."
462                 else
463                         print_info 5 "  $CfgVar set on command line to \"$Result\"."
464                 fi
465         else
466                 if [ -n "${!CfgVar}" ]
467                 then
468                         Result=${!CfgVar}
469                         # we need to set the CMD_* according to configfile...
470                         eval ${OverrideVar}=\"${Result}\"
471                         print_info 5 "  $CfgVar set in config file to \"${Result}\"."
472                 else
473                         if [ -n "$Default" ]
474                         then
475                                 Result=${Default}
476                                 # set OverrideVar to Result, otherwise CMD_* may not be initialized...
477                                 eval ${OverrideVar}=\"${Result}\"
478                                 print_info 5 "  $CfgVar defaulted to \"${Result}\"."
479                         else
480                                 print_info 5 "  $CfgVar not set."
481                         fi
482                 fi
483         fi
484
485         if [ "${VarType}" = BOOL ]
486         then
487                 if isTrue "${Result}"
488                 then
489                         Result=1
490                 else
491                         Result=0
492                 fi
493         fi
494
495         eval ${CfgVar}=\"${Result}\"
496 }
497
498 check_distfiles() {
499         for i in $BUSYBOX_SRCTAR $MULTIPATH_SRCTAR $LVM_SRCTAR $DMRAID_SRCTAR $ISCSI_SRCTAR $GPG_SRCTAR
500         do
501                 if [ ! -f "${i}" ]
502                 then
503                         small_die "Could not find source tarball ${i}. Please refetch."
504                 fi
505         done
506 }
507
508 find_kernel_binary() {
509         local kernel_binary=$*
510         local curdir=$(pwd)
511
512         cd "${KERNEL_OUTPUTDIR}"
513         for i in ${kernel_binary}
514         do
515                 if [ -e "${i}" ]
516                 then
517                         tmp_kernel_binary=$i
518                         break
519                 fi
520         done
521 #       if [ -z "${tmp_kernel_binary}" ]
522 #       then
523 #               gen_die "Cannot locate kernel binary!"
524 #       fi
525         cd "${curdir}"
526         echo "${tmp_kernel_binary}"
527 }