Removed save_args/reset_args on kernel. This is 3.3.11_pre7.
[genkernel.git] / genkernel
1 #!/bin/bash
2 # Genkernel v3
3
4 PATH="/bin:/usr/bin:/sbin:/usr/sbin"
5 GK_V='3.3.11_pre7'
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 KERNCACHE_IS_VALID=0
193 if [ "${KERNCACHE}" != "" ] 
194 then
195     gen_kerncache_is_valid
196 fi
197
198 if [ ${BUILD_KERNEL} -eq 1 -a "${KERNCACHE_IS_VALID}" == "0" ]
199 then
200         # Configure kernel
201         config_kernel
202         
203         # Make deps
204         compile_dep
205
206         # Make prepare [2.6]
207         if [ "${ARCH_HAVENOPREPARE}" = '' ]
208         then
209                 [ "${VER}" -gt '2' ] || [ "${VER}" -eq '2' -a "${PAT}" -ge '6' ] && compile_generic prepare kernel
210         fi
211         
212         # KV may have changed due to the configuration
213         get_KV
214
215         # Compile kernel; If using --genzimage, or building a mips kernel, skip compile
216         # till after initrd/initramfs is done
217         [ "${GENERATE_Z_IMAGE}" = '' -a "${MIPS_EMBEDDED_IMAGE}" = '' ] && compile_kernel
218
219         # Compile modules
220         if [ ${BUILD_MODULES} -eq 1 -a ${BUILD_STATIC} -eq 0 ]
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         if [ "${KERNCACHE}" != "" ]
232         then
233                 if [ "${GENERATE_Z_IMAGE}" = '' -a "${MIPS_EMBEDDED_IMAGE}" = '' ]
234                 then
235                         gen_kerncache
236                 fi
237         fi
238 fi
239
240 if ! isTrue "${CMD_NOINSTALL}"
241 then
242         if [ "${KERNCACHE}" != "" -a "${KERNCACHE_IS_VALID}" != "0" ] 
243         then
244                 gen_kerncache_extract_kernel
245         fi
246 fi
247
248 if [ "${KERNCACHE}" != "" -a "${KERNCACHE_IS_VALID}" != "0" ] 
249 then
250         [ ${BUILD_STATIC} -eq 0 ] && gen_kerncache_extract_modules
251         gen_kerncache_extract_config
252 fi
253
254
255 # Run callback
256 if [ "${CMD_CALLBACK}" != "" ]
257 then
258         print_info 1 "" 1 0
259         print_info 1 "Preparing to run callback: \"${CMD_CALLBACK}\"" 0
260
261         CALLBACK_ESCAPE=0
262         CALLBACK_COUNT=0
263
264         trap "CALLBACK_ESCAPE=1" TERM KILL INT QUIT ABRT
265         while [[ ${CALLBACK_ESCAPE} -eq 0 && ${CALLBACK_COUNT} -lt 5 ]]
266         do
267                 sleep 1; echo -n '.';
268                 let CALLBACK_COUNT=${CALLBACK_COUNT}+1
269         done
270
271         if [ "${CALLBACK_ESCAPE}" -eq 0 ]
272         then
273                 echo
274                 echo
275                 eval ${CMD_CALLBACK} | tee -a ${DEBUGFILE}
276                 CMD_STATUS="${PIPESTATUS[0]}"
277                 echo
278                 print_info 1 "<<< Callback exit status: ${CMD_STATUS}"
279                 [ "${CMD_STATUS}" -ne 0 ] && gen_die '--callback failed!'
280         else
281                 echo
282                 print_info 1 ">>> Callback cancelled..."
283         fi
284         trap - TERM KILL INT QUIT ABRT
285         print_info 1 "" 1 0
286 fi
287
288 if [ "${BUILD_INITRD}" -eq '1' ]
289 then
290         # Only compile insmod if we're installing modules onto the initrd
291         if [ "${NOINITRDMODULES}" = '' ]
292         then
293                 if [ "${KERN_24}" != '1' ]
294                 then
295                         # Compile module-init-tools
296                         [ ${BUILD_STATIC} -eq 0 ] && compile_module_init_tools
297                 else
298                         [ ${BUILD_STATIC} -eq 0 ] && compile_modutils
299                 fi
300         fi
301         
302         [ "${DISKLABEL}" -eq '1' ] && compile_e2fsprogs
303
304         if [ "${KERN_24}" != '1' -a "${UDEV}" -eq '1' ] 
305         then
306                 compile_udev 
307         else
308                 DEVFS=1
309                 UDEV=0
310                 compile_devfsd
311         fi
312
313         if [ "${KERN_24}" != '1' -a  "${CMD_BOOTSPLASH}" != '1' ]
314         then
315             if [ "${BUSYBOX}" -eq '1' ]
316             then
317                 # Compile Busybox
318                 compile_busybox
319             fi
320             
321             # Compile initramfs
322             create_initramfs
323         else
324             # Create initrd
325             compile_busybox
326             create_initrd
327         fi
328 else
329         print_info 1 'initrd: Not building since only the kernel was requested...'
330 fi
331
332 # Pegasos fix
333 if [ "${GENERATE_Z_IMAGE}" != '' -o "${MIPS_EMBEDDED_IMAGE}" != '' ]
334 then
335         # Compile kernel, intergrating the initrd into it for Pegasos & mips
336         compile_kernel
337
338         # We skipped the kernel build and kerncache generation
339         # So do it here
340         [ "${KERNCACHE}" != "" ] && gen_kerncache
341 fi
342
343 [ "${MINKERNPACKAGE}" != '' ] && gen_minkernpackage
344 [ "${MODULESPACKAGE}" != '' ] && gen_modulespackage
345
346 # Clean up...
347 [ -n "${CTEMP}" ] && rm -rf "${TEMP}"
348
349 if [ "${BUILD_KERNEL}" -eq '1' ]
350 then
351         set_bootloader
352         print_info 1 ''
353         print_info 1 "Kernel compiled successfully!"
354         print_info 1 ''
355         print_info 1 'Required Kernel Parameters:'
356         if [ "${BUILD_INITRD}" -eq '0' ]
357         then
358                 print_info 1 '    root=/dev/$ROOT'
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         elif [ "${KERN_24}" != '1' -a  "${CMD_BOOTSPLASH}" != '1' ]
364         then
365                 print_info 1 '    real_root=/dev/$ROOT'
366                 print_info 1 ''
367                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
368                 print_info 1 '    one specified in /etc/fstab'
369                 print_info 1 ''
370                 print_info 1 "If you require Genkernel's hardware detection features; you MUST"
371                 print_info 1 'tell your bootloader to use the provided INITRAMFS file. Otherwise;'
372                 print_info 1 'substitute the root argument for the real_root argument if you are'
373                 print_info 1 'not planning to use the initrd...'
374         else    
375                 print_info 1 '    root=/dev/ram0 real_root=/dev/$ROOT init=/linuxrc'
376                 [ "${INITRD_SIZE}" -ge 4096 ] && print_info 1 "    ramdisk_size=${INITRD_SIZE}"
377                 print_info 1 ''
378                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
379                 print_info 1 '    one specified in /etc/fstab'
380                 print_info 1 ''
381                 print_info 1 "If you require Genkernel's hardware detection features; you MUST"
382                 print_info 1 'tell your bootloader to use the provided INITRD file. Otherwise;'
383                 print_info 1 'substitute the root argument for the real_root argument if you are'
384                 print_info 1 'not planning to use the initrd...'
385         fi
386 fi
387
388 if [ "${BUILD_INITRD}" -eq '1' ]
389 then
390         echo
391         print_info 1 'WARNING... WARNING... WARNING...'
392         print_info 1 'Additional kernel cmdline arguments that *may* be required to boot properly...'
393         [ "${GENSPLASH}" -eq '1' ] && print_info 1 "add \"vga=791 splash=silent,theme:${GENSPLASH_THEME} CONSOLE=/dev/tty1 quiet\" if you use a gensplash framebuffer ]"
394         [ "${BOOTSPLASH}" -eq '1' ] && print_info 1 'add "vga=791 splash=silent" if you use a bootsplash framebuffer'
395         [ "${LVM2}" -eq '1' ] && print_info 1 'add "dolvm2" for lvm2 support'
396         [ "${EVMS2}" -eq '1' ] && print_info 1 'add "doevms2" for evms support'
397         [ "${DMRAID}" -eq '1' ] && print_info 1 'add "dodmraid" for dmraid support'
398         [ "${DMRAID}" -eq '1' ] && print_info 1 '       or "dodmraid=<additional options>"'
399         [ "${UNIONFS}" -eq '1' ] && print_info 1 'add "unionfs" for unionfs support'
400         [ "${UNIONFS}" -eq '1' ] && print_info 1 '      or "unionfs=<block_device>"'
401 fi
402
403 if [ "${CMD_NOINSTALL}" != '1' -a "${SYMLINK}" = '1' ]
404 then 
405     echo
406     print_info 1 'Creating symlinks'
407     
408     
409     print_info 1 '          creating kernel name symlink!'
410     if [ -e /boot/kernel-${KNAME}-${ARCH}-${KV} ]
411     then
412         /bin/rm /boot/kernel
413         ln -s /boot/kernel-${KNAME}-${ARCH}-${KV} /boot/kernel
414         ret=$?
415         [ ${ret} = '1' ] && print_error 1 'kernel link failed'
416     fi
417     
418     if [ "${KERN_24}" != '1' -a  "${CMD_BOOTSPLASH}" != '1' ]
419     then
420         print_info 1 '      creating initramfs name symlink!'
421         if [ -e /boot/initramfs-${KNAME}-${ARCH}-${KV} ]
422         then
423             /bin/rm /boot/initramfs
424             ln -s /boot/initramfs-${KNAME}-${ARCH}-${KV} /boot/initramfs
425             ret=$?
426             [ ${ret} = '1' ] && print_error 1 'initramfs link failed'
427         fi
428     else
429         print_info 1 '      creating initrd name symlink!'
430         if [ -e /boot/initrd-${KNAME}-${ARCH}-${KV} ]
431         then
432             /bin/rm /boot/initrd
433             ln -s /boot/initrd-${KNAME}-${ARCH}-${KV} /boot/initrd
434             ret=$?
435             [ ${ret} = '1' ] && print_error 1 'initrd link failed'
436         fi
437     fi
438 fi
439
440
441 [ "${BOOTRW}" != '' ] && mount -o remount,ro /boot
442
443 echo
444 print_info 1 'Do NOT report kernel bugs as genkernel bugs unless your bug'
445 print_info 1 'is about the default genkernel configuration...'
446 print_info 1 ''
447 print_info 1 'Make sure you have the latest genkernel before reporting bugs.'
448
449 #Final Cleanup
450 cleanup