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