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