Fix --no-install cleaning the tmpdir bug that plasmaroo reported
[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 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 cleanup(){
31     if [ -n "$TEMP" -a -d "$TEMP" ]; then
32         rm -rf "$TEMP"
33     fi
34
35     if isTrue ${POSTCLEAR}
36     then
37             echo
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             echo
43             print_info 1 "CACHE_CPIO_DIR: ${CACHE_CPIO_DIR}"
44             CLEAR_CPIO_CACHE='yes'
45             clear_cpio_dir
46             echo
47             print_info 1 "TMPDIR: ${TMPDIR}"
48             clear_tmpdir
49             fi
50
51     exit 1
52 }
53 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 /boot is mounted
126 if isTrue ${CMD_NOINSTALL}
127 then
128         isTrue ${MOUNTBOOT} && print_info 2 'Skipping automatic mount of boot'
129 else
130         if ! egrep -q ' /boot ' /proc/mounts
131         then
132                 if egrep -q '^[^#].+    /boot   ' /etc/fstab
133                 then
134                         if isTrue ${MOUNTBOOT}
135                         then
136                                 if ! mount /boot
137                                 then
138                                         print_warning 1 "${BOLD}WARNING${NORMAL}: Failed to mount /boot!"
139                                         echo
140                                 else
141                                         print_info 1 'mount: /boot mounted successfully!'
142                                 fi
143                         else
144                                 print_warning 1 "${BOLD}WARNING${NORMAL}: No mounted /boot partition detected!"
145                                 print_warning 1 '         Run ``mount /boot`` to mount it!'
146                                 echo
147                         fi
148                 fi
149         elif isBootRO
150         then
151                 if isTrue ${MOUNTBOOT}
152                 then
153                         if ! mount -o remount,rw /boot
154                         then
155                                 print_warning 1 "${BOLD}WARNING${NORMAL}: Failed to remount /boot RW!"
156                                 echo
157                         else
158                                 print_info 1 "mount: /boot remounted read/write successfully!"
159                                 BOOTRW=1
160                         fi
161                 fi
162         fi
163 fi
164
165 # Check whether another Genkernel is running
166 GENPIDS="`ps -C genkernel --no-headers -o pid | wc -l`"
167 if [ "${GENPIDS}" -gt '3' ]
168 then
169         [ "${GENPIDS}" -gt '4' ] && EX='s'
170         print_warning 1 "${BOLD}WARNING${NORMAL}: Another Genkernel instance is running under"
171         print_warning 1 "         process ID${EX} " 0
172         GENPIDS=`ps -C genkernel --no-headers -o pid`
173         echo -n "${GENPIDS}" | sed -e "s/$$//; s/ /, /g"
174         echo 'halting...'
175         echo
176         print_warning 1 'Running multiple genkernels on the same source tree will cause data loss!'
177         print_info 1 "Press ^C to halt; ^D to continue [ ${BOLD}if${NORMAL} you know what you're doing! ]"
178         echo
179         CTEMP="${TEMP}"
180         TEMP=${TMPDIR-/tmp}
181         TEMP=${TEMP}/genkernel.$RANDOM.$RANDOM.$RANDOM.$$
182         print_info 1 'thread: Running multiple genkernels may cause problems!'
183         print_info 1 "thread: Temporary files reallocated to ${TEMP}..."
184         echo
185 fi
186
187
188 KERNCACHE_IS_VALID=0
189 if [ "${KERNCACHE}" != "" ] 
190 then
191     gen_kerncache_is_valid
192 fi
193
194 if [ ${BUILD_KERNEL} -eq 1 -a "${KERNCACHE_IS_VALID}" == "0" ]
195 then
196         # Configure kernel
197         config_kernel
198         
199         # Make deps
200         compile_dep
201
202         # Make prepare [2.6]
203         if [ "${ARCH_HAVENOPREPARE}" = '' ]
204         then
205                 [ "${VER}" -gt '2' ] || [ "${VER}" -eq '2' -a "${PAT}" -ge '6' ] && compile_generic prepare kernel
206         fi
207         
208         # KV may have changed due to the configuration
209         get_KV
210
211         # Compile kernel
212         [ "${GENERATE_Z_IMAGE}" = '' ] && compile_kernel
213
214         # Compile modules
215         if [ ${BUILD_MODULES} -eq 1 ]
216         then
217                 compile_modules
218         fi
219
220         if [ ${SAVE_CONFIG} -eq 1 ]
221         then
222                 print_info 1 "Copying config for successful build to /etc/kernels/kernel-config-${ARCH}-${KV}"
223                 [ ! -e '/etc/kernels' ] && mkdir -p /etc/kernels
224                 cp "${KERNEL_DIR}/.config" "/etc/kernels/kernel-config-${ARCH}-${KV}"
225         fi
226         [ "${KERNCACHE}" != "" ] && gen_kerncache
227 fi
228
229 if ! isTrue "${CMD_NOINSTALL}"
230 then
231         if [ "${KERNCACHE}" != "" -a "${KERNCACHE_IS_VALID}" != "0" ] 
232         then
233                 gen_kerncache_extract_kernel
234         fi
235 fi
236
237 if [ "${KERNCACHE}" != "" -a "${KERNCACHE_IS_VALID}" != "0" ] 
238 then
239         gen_kerncache_extract_modules
240 fi
241
242 # Run callback
243 if [ "${CMD_CALLBACK}" != "" ]
244 then
245         print_info 1 "" 1 0
246         print_info 1 "Preparing to run callback: \"${CMD_CALLBACK}\"" 0
247
248         CALLBACK_ESCAPE=0
249         CALLBACK_COUNT=0
250
251         trap "CALLBACK_ESCAPE=1" TERM KILL INT QUIT ABRT
252         while [[ ${CALLBACK_ESCAPE} -eq 0 && ${CALLBACK_COUNT} -lt 5 ]]
253         do
254                 sleep 1; echo -n '.';
255                 let CALLBACK_COUNT=${CALLBACK_COUNT}+1
256         done
257
258         if [ "${CALLBACK_ESCAPE}" -eq 0 ]
259         then
260                 echo
261                 echo
262                 eval ${CMD_CALLBACK} | tee -a ${DEBUGFILE}
263                 CMD_STATUS="${PIPESTATUS[0]}"
264                 echo
265                 print_info 1 "<<< Callback exit status: ${CMD_STATUS}"
266                 [ "${CMD_STATUS}" -ne 0 ] && gen_die '--callback failed!'
267         else
268                 echo
269                 print_info 1 ">>> Callback cancelled..."
270         fi
271         trap - TERM KILL INT QUIT ABRT
272         print_info 1 "" 1 0
273 fi
274
275 if [ "${BUILD_INITRD}" -eq '1' ]
276 then
277         # Only compile insmod if we're installing modules onto the initrd
278         if [ "${NOINITRDMODULES}" = '' ]
279         then
280                 if [ "${KERN_24}" != '1' ]
281                 then
282                         # Compile module-init-tools
283                         compile_module_init_tools
284                 else
285                         compile_modutils
286                 fi
287         fi
288         
289         [ "${DISKLABEL}" -eq '1' ] && compile_e2fsprogs
290
291         if [ "${KERN_24}" != '1' -a "${UDEV}" -eq '1' ] 
292         then
293                 compile_udev 
294         else
295                 DEVFS=1
296                 UDEV=0
297                 compile_devfsd
298         fi
299
300         if [ "${KERN_24}" != '1' -a  "${CMD_BOOTSPLASH}" != '1' ]
301         then
302             if [ "${BUSYBOX}" -eq '1' ]
303             then
304                 # Compile Busybox
305                 compile_busybox
306             fi
307             
308             # Compile initramfs
309             create_initramfs
310         else
311             # Create initrd
312             compile_busybox
313             create_initrd
314         fi
315 else
316         print_info 1 'initrd: Not building since only the kernel was requested...'
317 fi
318
319 # Pegasos fix
320 if [ "${GENERATE_Z_IMAGE}" != '' ]
321 then
322         # Compile kernel, intergrating the initrd into it for Pegasos
323         compile_kernel
324 fi
325
326 [ "${MINKERNPACKAGE}" != '' ] && gen_minkernpackage
327 [ "${MODULESPACKAGE}" != '' ] && gen_modulespackage
328
329 # Clean up...
330 [ -n "${CTEMP}" ] && rm -rf "${TEMP}"
331
332 if [ "${BUILD_KERNEL}" -eq '1' ]
333 then
334         set_bootloader
335         print_info 1 ''
336         print_info 1 "Kernel compiled successfully!"
337         print_info 1 ''
338         print_info 1 'Required Kernel Parameters:'
339         if [ "${BUILD_INITRD}" -eq '0' ]
340         then
341                 print_info 1 '    root=/dev/$ROOT'
342                 print_info 1 '    [ And "vga=0x317 splash=verbose" if you use a framebuffer ]'
343                 print_info 1 ''
344                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
345                 print_info 1 '    one specified in /etc/fstab'
346         elif [ "${KERN_24}" != '1' -a  "${CMD_BOOTSPLASH}" != '1' ]
347         then
348                 print_info 1 '    real_root=/dev/$ROOT'
349                 print_info 1 ''
350                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
351                 print_info 1 '    one specified in /etc/fstab'
352                 print_info 1 ''
353                 print_info 1 "If you require Genkernel's hardware detection features; you MUST"
354                 print_info 1 'tell your bootloader to use the provided INITRAMFS file. Otherwise;'
355                 print_info 1 'substitute the root argument for the real_root argument if you are'
356                 print_info 1 'not planning to use the initrd...'
357         else    
358                 print_info 1 '    root=/dev/ram0 real_root=/dev/$ROOT init=/linuxrc'
359                 [ "${INITRD_SIZE}" -ge 4096 ] && print_info 1 "    ramdisk_size=${INITRD_SIZE}"
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         [ "${GENSPLASH}" -eq '1' ] && print_info 1 "add \"vga=791 splash=silent,theme:${GENSPLASH_THEME} CONSOLE=/dev/tty1 quiet\" if you use a gensplash framebuffer ]"
377         [ "${BOOTSPLASH}" -eq '1' ] && print_info 1 'add "vga=791 splash=silent" if you use a bootsplash framebuffer'
378         [ "${LVM2}" -eq '1' ] && print_info 1 'add "dolvm2" for lvm2 support'
379         [ "${EVMS2}" -eq '1' ] && print_info 1 'add "doevms" for evms support'
380         [ "${DMRAID}" -eq '1' ] && print_info 1 'add "dodmraid" for dmraid support'
381         [ "${DMRAID}" -eq '1' ] && print_info 1 '       or "dodmraid=<additional options>"'
382         [ "${UNIONFS}" -eq '1' ] && print_info 1 'add "unionfs" for unionfs support'
383         [ "${UNIONFS}" -eq '1' ] && print_info 1 '      or "unionfs=<block_device>"'
384 fi
385
386 if [ "${CMD_NOINSTALL}" != '1' -a "${SYMLINK}" = '1' ]
387 then 
388     echo
389     print_info 1 'Creating symlinks'
390     
391     
392     print_info 1 '          creating kernel name symlink!'
393     if [ -e /boot/kernel-${KNAME}-${ARCH}-${KV} ]
394     then
395         /bin/rm /boot/kernel
396         ln -s /boot/kernel-${KNAME}-${ARCH}-${KV} /boot/kernel
397         ret=$?
398         [ ${ret} = '1' ] && print_error 1 'kernel link failed'
399     fi
400     
401     if [ "${KERN_24}" != '1' -a  "${CMD_BOOTSPLASH}" != '1' ]
402     then
403         print_info 1 '      creating initramfs name symlink!'
404         if [ -e /boot/initramfs-${KNAME}-${ARCH}-${KV} ]
405         then
406             /bin/rm /boot/initramfs
407             ln -s /boot/initramfs-${KNAME}-${ARCH}-${KV} /boot/initramfs
408             ret=$?
409             [ ${ret} = '1' ] && print_error 1 'initramfs link failed'
410         fi
411     else
412         print_info 1 '      creating initrd name symlink!'
413         if [ -e /boot/initrd-${KNAME}-${ARCH}-${KV} ]
414         then
415             /bin/rm /boot/initrd
416             ln -s /boot/initrd-${KNAME}-${ARCH}-${KV} /boot/initrd
417             ret=$?
418             [ ${ret} = '1' ] && print_error 1 'initrd link failed'
419         fi
420     fi
421 fi
422
423
424 [ "${BOOTRW}" != '' ] && mount -o remount,ro /boot
425
426 echo
427 print_info 1 'Do NOT report kernel bugs as genkernel bugs unless your bug'
428 print_info 1 'is about the default genkernel configuration...'
429 print_info 1 ''
430 print_info 1 'Make sure you have the latest genkernel before reporting bugs.'
431
432 #Final Cleanup
433 cleanup