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