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