Moved the MAKEOPTS check to the top of the file for parisc, so it matches other archi...
[genkernel.git] / genkernel
1 #!/bin/bash
2 # Genkernel v3
3
4 PATH="/bin:/usr/bin:/sbin:/usr/sbin"
5 GK_V='3.4.7_pre3'
6
7 TMPDIR='/var/tmp/genkernel'
8 TEMP=${TMPDIR}/$RANDOM.$RANDOM.$RANDOM.$$
9 TODEBUGCACHE=1 # Until an error occurs or DEBUGFILE is fully qualified.
10
11 small_die() {
12   echo $1
13   exit 1
14 }
15
16 source /etc/genkernel.conf || small_die "Could not read /etc/genkernel.conf"
17 source ${GK_BIN}/gen_funcs.sh || small_die "Could not read ${GK_BIN}/gen_funcs.sh"
18 source ${GK_BIN}/gen_cmdline.sh || gen_die "Could not read ${GK_BIN}/gen_cmdline.sh"
19 source ${GK_BIN}/gen_arch.sh || gen_die "Could not read ${GK_BIN}/gen_arch.sh"
20 source ${GK_BIN}/gen_determineargs.sh || gen_die "Could not read ${GK_BIN}/gen_determineargs.sh"
21 source ${GK_BIN}/gen_compile.sh || gen_die "Could not read ${GK_BIN}/gen_compile.sh"
22 source ${GK_BIN}/gen_configkernel.sh || gen_die "Could not read ${GK_BIN}/gen_configkernel.sh"
23 source ${GK_BIN}/gen_initrd.sh || gen_die "Could not read ${GK_BIN}/gen_initrd.sh"
24 source ${GK_BIN}/gen_initramfs.sh || gen_die "Could not read ${GK_BIN}/gen_initramfs.sh"
25 source ${GK_BIN}/gen_moddeps.sh || gen_die "Could not read ${GK_BIN}/gen_moddeps.sh"
26 source ${GK_BIN}/gen_package.sh || gen_die "Could not read ${GK_BIN}/gen_package.sh"
27 source ${GK_BIN}/gen_bootloader.sh || gen_die "Could not read ${GK_BIN}/gen_bootloader.sh"
28
29 trap_cleanup(){
30         # Call exit code of 1 for failure
31         cleanup
32         exit 1
33 }
34
35 cleanup(){
36     if [ -n "$TEMP" -a -d "$TEMP" ]; then
37         rm -rf "$TEMP"
38     fi
39
40     if isTrue ${POSTCLEAR}
41     then
42             echo
43             print_info 1 'RUNNING FINAL CACHE/TMP CLEANUP'
44             print_info 1 "CACHE_DIR: ${CACHE_DIR}"
45             CLEAR_CACHE_DIR='yes'
46             setup_cache_dir
47             echo
48             print_info 1 "TMPDIR: ${TMPDIR}"
49             clear_tmpdir
50             fi
51 }
52
53 trap trap_cleanup SIGHUP SIGQUIT SIGINT SIGTERM SIGKILL
54 BUILD_KERNEL=0
55 BUILD_INITRD=0
56 BUILD_MODULES=0
57
58 # Parse all command line options...
59 Options=$* # Save for later
60 while [ $# -gt 0 ]
61 do
62         Option=$1; shift
63         parse_cmdline $Option
64 done
65
66 # Check if no action is specified...
67 if [ "${BUILD_KERNEL}" -eq 0 -a "${BUILD_INITRD}" -eq 0 ]
68 then
69         usage
70         exit 1
71 fi
72
73 clear_log
74 NORMAL=${GOOD} print_info 1 "Gentoo Linux Genkernel; Version ${GK_V}${NORMAL}"
75 print_info 1 "Running with options: ${Options}"
76 echo
77
78 # Set ${ARCH}
79 get_official_arch
80
81 # Read arch-specific config
82 source ${ARCH_CONFIG} || gen_die "Could not read ${ARCH_CONFIG}"
83 source ${GK_SHARE}/${ARCH}/modules_load || gen_die "Could not read ${GK_SHARE}/${ARCH}/modules_load"
84
85 # Merge modules_load from config
86 for group_modules in ${!AMODULES_*}; do
87         group="$(echo $group_modules | cut -d_ -f2)"
88         eval cmodules="\$$group_modules"
89         eval MODULES_${group}=\"\${MODULES_${group}} ${cmodules}\"
90         print_info 1 "<config> Merged AMODULES_${group}:'${cmodules}' into MODULES_${group}"
91 done
92
93
94 # Based on genkernel.conf, arch-specific configs, and commandline options,
95 # get the real arguments for usage...
96
97 determine_real_args
98
99 [ ! -f "${TEMP}" ] && mkdir -p "${TEMP}"
100
101 setup_cache_dir
102
103
104 dump_debugcache
105
106 NORMAL=${BOLD} print_info 1 "Linux Kernel ${BOLD}${KV}${NORMAL} for ${BOLD}${ARCH}${NORMAL}..."
107
108 if [ "${BUILD_INITRD}" -ne '0' ]
109 then
110         if ! has_loop
111         then
112                 modprobe loop 2>/dev/null
113                 sleep 3
114                 if ! has_loop
115                 then
116                         print_error 1 'The build-host kernel does not appear to have loop device support.'
117                         print_error 1 'Please load loop support before running genkernel!'
118                         gen_die 'Load loop support!'
119                 else
120                         print_info 1 'loop: "loop" module loaded successfully...'
121                 fi
122         fi
123 fi
124
125 # Check BOOTDIR is mounted
126 if isTrue ${CMD_NOINSTALL}
127 then
128         isTrue ${MOUNTBOOT} && print_info 2 'Skipping automatic mount of boot'
129 else
130         [[ -d ${BOOTDIR} ]] || gen_die "${BOOTDIR} is not a directory"
131         
132         if ! egrep -q "[[:space:]]${BOOTDIR}[[:space:]]" /proc/mounts
133         then
134                 if egrep -q "^[^#].+[[:space:]]${BOOTDIR}[[:space:]]" /etc/fstab
135                 then
136                         if isTrue ${MOUNTBOOT}
137                         then
138                                 if ! mount ${BOOTDIR}
139                                 then
140                                         print_warning 1 "${BOLD}WARNING${NORMAL}: Failed to mount ${BOOTDIR}!"
141                                         echo
142                                 else
143                                         print_info 1 "mount: ${BOOTDIR} mounted successfully!"
144                                 fi
145                         else
146                                 print_warning 1 "${BOLD}WARNING${NORMAL}: No mounted ${BOOTDIR} partition detected!"
147                                 print_warning 1 "         Run ``mount ${BOOTDIR}`` to mount it!"
148                                 echo
149                         fi
150                 fi
151         elif isBootRO
152         then
153                 if isTrue ${MOUNTBOOT}
154                 then
155                         if ! mount -o remount,rw ${BOOTDIR}
156                         then
157                                 print_warning 1 "${BOLD}WARNING${NORMAL}: Failed to remount ${BOOTDIR} RW!"
158                                 echo
159                         else
160                                 print_info 1 "mount: ${BOOTDIR} remounted read/write successfully!"
161                                 BOOTRW=1
162                         fi
163                 fi
164         fi
165 fi
166
167 ## Check whether another Genkernel is running
168 #GENPIDS="`ps -C genkernel --no-headers -o pid | wc -l`"
169 #if [ "${GENPIDS}" -gt '3' ]
170 #then
171 #       [ "${GENPIDS}" -gt '4' ] && EX='s'
172 #       print_warning 1 "${BOLD}WARNING${NORMAL}: Another Genkernel instance is running under"
173 #       print_warning 1 "         process ID${EX} " 0
174 #       GENPIDS=`ps -C genkernel --no-headers -o pid`
175 #       echo -n "${GENPIDS}" | sed -e "s/$$//; s/ /, /g"
176 #       echo 'halting...'
177 #       echo
178 #       print_warning 1 'Running multiple genkernels on the same source tree will cause data loss!'
179 #       print_info 1 "Press ^C to halt; ^D to continue [ ${BOLD}if${NORMAL} you know what you're doing! ]"
180 #       echo
181 #       CTEMP="${TEMP}"
182 #       TEMP=${TMPDIR-/tmp}
183 #       TEMP=${TEMP}/genkernel.$RANDOM.$RANDOM.$RANDOM.$$
184 #       print_info 1 'thread: Running multiple genkernels may cause problems!'
185 #       print_info 1 "thread: Temporary files reallocated to ${TEMP}..."
186 #       echo
187 #fi
188
189 KERNCACHE_IS_VALID=0
190 if [ "${KERNCACHE}" != "" ] 
191 then
192     gen_kerncache_is_valid
193 fi
194
195 if [ ${BUILD_KERNEL} -eq 1 -a "${KERNCACHE_IS_VALID}" == "0" ]
196 then
197         # Configure kernel
198         config_kernel
199         
200         # Make deps
201         compile_dep
202
203         # Make prepare [2.6]
204         if [ "${ARCH_HAVENOPREPARE}" = '' ]
205         then
206                 [ "${VER}" -gt '2' ] || [ "${VER}" -eq '2' -a "${PAT}" -ge '6' ] && compile_generic prepare kernel
207         fi
208         
209         # KV may have changed due to the configuration
210         get_KV
211
212         # Compile kernel; If using --genzimage, or building a mips kernel, skip compile
213         # till after initrd/initramfs is done
214         [ "${GENERATE_Z_IMAGE}" = '' -a ${BUILD_INITRAMFS} -eq 0 ] && compile_kernel
215
216         # Compile modules
217         if [ ${BUILD_MODULES} -eq 1 -a ${BUILD_STATIC} -eq 0 ]
218         then
219                 compile_modules
220         fi
221
222         if [ ${SAVE_CONFIG} -eq 1 ]
223         then
224                 print_info 1 "Copying config for successful build to /etc/kernels/kernel-config-${ARCH}-${KV}"
225                 [ ! -e '/etc/kernels' ] && mkdir -p /etc/kernels
226                 cp "${KERNEL_DIR}/.config" "/etc/kernels/kernel-config-${ARCH}-${KV}"
227         fi
228         if [ "${KERNCACHE}" != "" ]
229         then
230                 if [ "${GENERATE_Z_IMAGE}" = '' -a ${BUILD_INITRAMFS} -eq 0 ]
231                 then
232                         gen_kerncache
233                 fi
234         fi
235 fi
236
237 if ! isTrue "${CMD_NOINSTALL}"
238 then
239         if [ "${KERNCACHE}" != "" -a "${KERNCACHE_IS_VALID}" != "0" ] 
240         then
241                 gen_kerncache_extract_kernel
242         fi
243 fi
244
245 if [ "${KERNCACHE}" != "" -a "${KERNCACHE_IS_VALID}" != "0" ] 
246 then
247         [ ${BUILD_STATIC} -eq 0 ] && gen_kerncache_extract_modules
248         gen_kerncache_extract_config
249 fi
250
251
252 # Run callback
253 if [ "${CMD_CALLBACK}" != "" ]
254 then
255         print_info 1 "" 1 0
256         print_info 1 "Preparing to run callback: \"${CMD_CALLBACK}\"" 0
257
258         CALLBACK_ESCAPE=0
259         CALLBACK_COUNT=0
260
261         trap "CALLBACK_ESCAPE=1" TERM KILL INT QUIT ABRT
262         while [[ ${CALLBACK_ESCAPE} -eq 0 && ${CALLBACK_COUNT} -lt 5 ]]
263         do
264                 sleep 1; echo -n '.';
265                 let CALLBACK_COUNT=${CALLBACK_COUNT}+1
266         done
267
268         if [ "${CALLBACK_ESCAPE}" -eq 0 ]
269         then
270                 echo
271                 echo
272                 eval ${CMD_CALLBACK} | tee -a ${DEBUGFILE}
273                 CMD_STATUS="${PIPESTATUS[0]}"
274                 echo
275                 print_info 1 "<<< Callback exit status: ${CMD_STATUS}"
276                 [ "${CMD_STATUS}" -ne 0 ] && gen_die '--callback failed!'
277         else
278                 echo
279                 print_info 1 ">>> Callback cancelled..."
280         fi
281         trap - TERM KILL INT QUIT ABRT
282         print_info 1 "" 1 0
283 fi
284
285 if [ "${BUILD_INITRD}" -eq '1' ]
286 then
287         # Only compile insmod if we're installing modules onto the initrd
288         if [ "${NOINITRDMODULES}" = '' ]
289         then
290                 if [ "${KERN_24}" != '1' ]
291                 then
292                         # Compile module-init-tools
293                         [ ${BUILD_STATIC} -eq 0 ] && compile_module_init_tools
294                 else
295                         [ ${BUILD_STATIC} -eq 0 ] && compile_modutils
296                 fi
297         fi
298         
299         [ "${DISKLABEL}" -eq '1' ] && compile_e2fsprogs
300
301         if [ "${KERN_24}" != '1' -a "${UDEV}" -eq '1' ] 
302         then
303                 :
304 #               compile_udev 
305         else
306                 DEVFS=1
307                 UDEV=0
308                 compile_devfsd
309         fi
310
311         if [ "${KERN_24}" != '1' -a  "${CMD_BOOTSPLASH}" != '1' ]
312         then
313             if [ "${BUSYBOX}" -eq '1' ]
314             then
315                 # Compile Busybox
316                 compile_busybox
317             fi
318             
319             # Compile initramfs
320             create_initramfs
321         else
322             # Create initrd
323             compile_busybox
324             create_initrd
325         fi
326 else
327         print_info 1 'initrd: Not building since only the kernel was requested...'
328 fi
329
330 # Pegasos fix
331 if [ "${GENERATE_Z_IMAGE}" != '' -o ${BUILD_INITRAMFS} -eq 1 ]
332 then
333         # Compile kernel, intergrating the initrd into it for Pegasos & mips
334         compile_kernel
335
336         # We skipped the kernel build and kerncache generation
337         # So do it here
338         [ "${KERNCACHE}" != "" ] && gen_kerncache
339 fi
340
341 [ "${MINKERNPACKAGE}" != '' ] && gen_minkernpackage
342 [ "${MODULESPACKAGE}" != '' ] && gen_modulespackage
343
344 # Clean up...
345 [ -n "${CTEMP}" ] && rm -rf "${TEMP}"
346
347 if [ "${BUILD_KERNEL}" -eq '1' ]
348 then
349         set_bootloader
350         print_info 1 ''
351         print_info 1 "Kernel compiled successfully!"
352         print_info 1 ''
353         print_info 1 'Required Kernel Parameters:'
354         if [ "${BUILD_INITRD}" -eq '0' ]
355         then
356                 print_info 1 '    root=/dev/$ROOT'
357                 print_info 1 '    [ And "vga=0x317 splash=verbose" if you use a framebuffer ]'
358                 print_info 1 ''
359                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
360                 print_info 1 '    one specified in /etc/fstab'
361         elif [ "${KERN_24}" != '1' -a  "${CMD_BOOTSPLASH}" != '1' ]
362         then
363                 print_info 1 '    real_root=/dev/$ROOT'
364                 print_info 1 ''
365                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
366                 print_info 1 '    one specified in /etc/fstab'
367                 print_info 1 ''
368                 print_info 1 "If you require Genkernel's hardware detection features; you MUST"
369                 print_info 1 'tell your bootloader to use the provided INITRAMFS file. Otherwise;'
370                 print_info 1 'substitute the root argument for the real_root argument if you are'
371                 print_info 1 'not planning to use the initrd...'
372         else    
373                 print_info 1 '    root=/dev/ram0 real_root=/dev/$ROOT init=/linuxrc'
374                 [ "${INITRD_SIZE}" -ge 4096 ] && print_info 1 "    ramdisk_size=${INITRD_SIZE}"
375                 print_info 1 ''
376                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
377                 print_info 1 '    one specified in /etc/fstab'
378                 print_info 1 ''
379                 print_info 1 "If you require Genkernel's hardware detection features; you MUST"
380                 print_info 1 'tell your bootloader to use the provided INITRD file. Otherwise;'
381                 print_info 1 'substitute the root argument for the real_root argument if you are'
382                 print_info 1 'not planning to use the initrd...'
383         fi
384 fi
385
386 if [ "${BUILD_INITRD}" -eq '1' ]
387 then
388         echo
389         print_info 1 'WARNING... WARNING... WARNING...'
390         print_info 1 'Additional kernel cmdline arguments that *may* be required to boot properly...'
391         [ "${GENSPLASH}" -eq '1' ] && print_info 1 "add \"vga=791 splash=silent,theme:${GENSPLASH_THEME} CONSOLE=/dev/tty1 quiet\" if you use a gensplash framebuffer ]"
392         [ "${BOOTSPLASH}" -eq '1' ] && print_info 1 'add "vga=791 splash=silent" if you use a bootsplash framebuffer'
393         [ "${LVM2}" -eq '1' ] && print_info 1 'add "dolvm2" for lvm2 support'
394         [ "${EVMS2}" -eq '1' ] && print_info 1 'add "doevms2" for evms support'
395         [ "${DMRAID}" -eq '1' ] && print_info 1 'add "dodmraid" for dmraid support'
396         [ "${DMRAID}" -eq '1' ] && print_info 1 '       or "dodmraid=<additional options>"'
397         [ "${UNIONFS}" -eq '1' ] && print_info 1 'add "unionfs" for unionfs support'
398         [ "${UNIONFS}" -eq '1' ] && print_info 1 '      or "unionfs=<block_device>"'
399 fi
400
401
402 symlinker() {
403         local base=$1
404         local fullVer=${KNAME}-${ARCH}-${KV}
405
406         local newThing=${BOOTDIR}/${base}-${fullVer}
407         local newSym=${BOOTDIR}/${base}
408         local oldSym=${newSym}.old
409
410         local prevLink
411         local ret=0
412
413         print_info 1 "      creating ${base} name symlink!"
414         if [ -e ${newThing} ] ; then
415                 if [ -L ${newSym} ] ; then
416                         prevLink=`readlink --no-newline ${newSym}`
417                         if [ ${prevLink} != ${newThing} ] ; then
418                                 if [ -L ${oldSym} ] ; then
419                                         rm ${oldSym}
420                                 fi
421                                 ln -s ${prevLink} ${oldSym}
422                                 [ $((ret += $?)) ]
423                         fi
424                         rm ${newSym}
425                 fi
426                 ln -s ${newThing} ${newSym}
427                 [ $((ret += $?)) ]
428         fi
429         
430         if [[ ${ret} > 0 ]] ; then
431                 print_error 1 "     $base link failed: ${ret}"
432         fi
433
434         return ${ret}
435 }
436
437
438 if [ "${CMD_NOINSTALL}" != '1' -a "${SYMLINK}" = '1' ]
439 then 
440     echo
441     print_info 1 'Creating symlinks'
442     
443     
444         symlinker kernel
445         symlinker System.map 
446
447         if [ "${KERN_24}" != '1' -a  "${CMD_BOOTSPLASH}" != '1' ] ; then
448                 symlinker initramfs
449         else
450                 symlinker initrd
451     fi
452 fi
453
454
455 [ "${BOOTRW}" != '' ] && mount -o remount,ro ${BOOTDIR}
456
457 echo
458 print_info 1 'Do NOT report kernel bugs as genkernel bugs unless your bug'
459 print_info 1 'is about the default genkernel configuration...'
460 print_info 1 ''
461 print_info 1 'Make sure you have the latest genkernel before reporting bugs.'
462
463 #Final Cleanup
464 cleanup