Renamed gensplash to splash and marked gensplash as deprecated. This is 3.4.9_pre3...
[genkernel.git] / genkernel
1 #!/bin/bash
2 # Genkernel v3
3
4 PATH="/bin:/usr/bin:/sbin:/usr/sbin"
5 GK_V='3.4.9_pre3'
6
7 # Set the default for TMPDIR.  May be modified by genkernel.conf or the
8 # --tempdir command line option.
9 TMPDIR='/var/tmp/genkernel'
10
11 TODEBUGCACHE=1 # Until an error occurs or DEBUGFILE is fully qualified.
12
13 small_die() {
14   echo $1
15   exit 1
16 }
17
18 source /etc/genkernel.conf || small_die "Could not read /etc/genkernel.conf"
19 source ${GK_BIN}/gen_funcs.sh || small_die "Could not read ${GK_BIN}/gen_funcs.sh"
20 source ${GK_BIN}/gen_cmdline.sh || gen_die "Could not read ${GK_BIN}/gen_cmdline.sh"
21 source ${GK_BIN}/gen_arch.sh || gen_die "Could not read ${GK_BIN}/gen_arch.sh"
22 source ${GK_BIN}/gen_determineargs.sh || gen_die "Could not read ${GK_BIN}/gen_determineargs.sh"
23 source ${GK_BIN}/gen_compile.sh || gen_die "Could not read ${GK_BIN}/gen_compile.sh"
24 source ${GK_BIN}/gen_configkernel.sh || gen_die "Could not read ${GK_BIN}/gen_configkernel.sh"
25 source ${GK_BIN}/gen_initrd.sh || gen_die "Could not read ${GK_BIN}/gen_initrd.sh"
26 source ${GK_BIN}/gen_initramfs.sh || gen_die "Could not read ${GK_BIN}/gen_initramfs.sh"
27 source ${GK_BIN}/gen_moddeps.sh || gen_die "Could not read ${GK_BIN}/gen_moddeps.sh"
28 source ${GK_BIN}/gen_package.sh || gen_die "Could not read ${GK_BIN}/gen_package.sh"
29 source ${GK_BIN}/gen_bootloader.sh || gen_die "Could not read ${GK_BIN}/gen_bootloader.sh"
30
31 TEMP=${TMPDIR}/$RANDOM.$RANDOM.$RANDOM.$$
32
33 trap_cleanup(){
34         # Call exit code of 1 for failure
35         cleanup
36         exit 1
37 }
38
39 cleanup(){
40     if [ -n "$TEMP" -a -d "$TEMP" ]; then
41         rm -rf "$TEMP"
42     fi
43
44     if isTrue ${POSTCLEAR}
45     then
46             echo
47             print_info 1 'RUNNING FINAL CACHE/TMP CLEANUP'
48             print_info 1 "CACHE_DIR: ${CACHE_DIR}"
49             CLEAR_CACHE_DIR='yes'
50             setup_cache_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         [ "${DISKLABEL}" -eq '1' ] && compile_e2fsprogs
292
293         if [ "${KERN_24}" -eq '1' ] 
294         then
295                 compile_devfsd
296         fi
297
298         if [ "${KERN_24}" != '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}" != '' -o ${BUILD_INITRAMFS} -eq 1 ]
319 then
320         # Compile kernel, intergrating the initrd into it for Pegasos & mips
321         compile_kernel
322
323         # We skipped the kernel build and kerncache generation
324         # So do it here
325         [ "${KERNCACHE}" != "" ] && gen_kerncache
326 fi
327
328 [ "${MINKERNPACKAGE}" != '' ] && gen_minkernpackage
329 [ "${MODULESPACKAGE}" != '' ] && gen_modulespackage
330
331 # Clean up...
332 [ -n "${CTEMP}" ] && rm -rf "${TEMP}"
333
334 if [ "${BUILD_KERNEL}" -eq '1' ]
335 then
336         set_bootloader
337         print_info 1 ''
338         print_info 1 "Kernel compiled successfully!"
339         print_info 1 ''
340         print_info 1 'Required Kernel Parameters:'
341         if [ "${BUILD_INITRD}" -eq '0' ]
342         then
343                 print_info 1 '    root=/dev/$ROOT'
344                 print_info 1 '    [ And "vga=0x317 splash=verbose" if you use a framebuffer ]'
345                 print_info 1 ''
346                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
347                 print_info 1 '    one specified in /etc/fstab'
348         elif [ "${KERN_24}" != '1' ]
349         then
350                 print_info 1 '    real_root=/dev/$ROOT'
351                 print_info 1 ''
352                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
353                 print_info 1 '    one specified in /etc/fstab'
354                 print_info 1 ''
355                 print_info 1 "If you require Genkernel's hardware detection features; you MUST"
356                 print_info 1 'tell your bootloader to use the provided INITRAMFS file. Otherwise;'
357                 print_info 1 'substitute the root argument for the real_root argument if you are'
358                 print_info 1 'not planning to use the initrd...'
359         else    
360                 print_info 1 '    root=/dev/ram0 real_root=/dev/$ROOT init=/linuxrc'
361                 [ "${INITRD_SIZE}" -ge 4096 ] && print_info 1 "    ramdisk_size=${INITRD_SIZE}"
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                 print_info 1 ''
366                 print_info 1 "If you require Genkernel's hardware detection features; you MUST"
367                 print_info 1 'tell your bootloader to use the provided INITRD file. Otherwise;'
368                 print_info 1 'substitute the root argument for the real_root argument if you are'
369                 print_info 1 'not planning to use the initrd...'
370         fi
371 fi
372
373 if [ "${BUILD_INITRD}" -eq '1' ]
374 then
375         echo
376         print_info 1 'WARNING... WARNING... WARNING...'
377         print_info 1 'Additional kernel cmdline arguments that *may* be required to boot properly...'
378         [ "${SPLASH}" -eq '1' ] && print_info 1 "add \"vga=791 splash=silent,theme:${SPLASH_THEME} console=tty1 quiet\" if you use a splash framebuffer ]"
379         [ "${LVM}" -eq '1' ] && print_info 1 'add "dolvm" for lvm support'
380         [ "${EVMS}" -eq '1' ] && print_info 1 'add "doevms" for evms support'
381         [ "${DMRAID}" -eq '1' ] && print_info 1 'add "dodmraid" for dmraid support'
382         [ "${DMRAID}" -eq '1' ] && print_info 1 '       or "dodmraid=<additional options>"'
383         [ "${UNIONFS}" -eq '1' ] && print_info 1 'add "unionfs" for unionfs support'
384         [ "${UNIONFS}" -eq '1' ] && print_info 1 '      or "unionfs=<block_device>"'
385 fi
386
387 [ "${BOOTRW}" != '' ] && mount -o remount,ro ${BOOTDIR}
388
389 echo
390 print_info 1 'Do NOT report kernel bugs as genkernel bugs unless your bug'
391 print_info 1 'is about the default genkernel configuration...'
392 print_info 1 ''
393 print_info 1 'Make sure you have the latest genkernel before reporting bugs.'
394
395 #Final Cleanup
396 cleanup