>> 3.1.0k -- Add MODULES_SATA, fix udev on non-x86 archs where there seem to be probl...
[genkernel.git] / genkernel
1 #!/bin/bash
2 # Genkernel v3
3
4 GK_V='3.1.0l'
5 TEMP='/var/tmp/genkernel'
6 TODEBUGCACHE=1 # Until an error occurs or DEBUGFILE is fully qualified.
7
8 small_die() {
9   echo $1
10   exit 1
11 }
12
13 source /etc/genkernel.conf || small_die "Could not read /etc/genkernel.conf"
14 source ${GK_BIN}/gen_funcs.sh || small_die "Could not read ${GK_BIN}/gen_funcs.sh"
15 source ${GK_BIN}/gen_cmdline.sh || gen_die "Could not read ${GK_BIN}/gen_cmdline.sh"
16 source ${GK_BIN}/gen_arch.sh || gen_die "Could not read ${GK_BIN}/gen_arch.sh"
17 source ${GK_BIN}/gen_determineargs.sh || gen_die "Could not read ${GK_BIN}/gen_determineargs.sh"
18 source ${GK_BIN}/gen_compile.sh || gen_die "Could not read ${GK_BIN}/gen_compile.sh"
19 source ${GK_BIN}/gen_configkernel.sh || gen_die "Could not read ${GK_BIN}/gen_configkernel.sh"
20 source ${GK_BIN}/gen_initrd.sh || gen_die "Could not read ${GK_BIN}/gen_initrd.sh"
21 source ${GK_BIN}/gen_moddeps.sh || gen_die "Could not read ${GK_BIN}/gen_moddeps.sh"
22 source ${GK_BIN}/gen_package.sh || gen_die "Could not read ${GK_BIN}/gen_package.sh"
23 source ${GK_BIN}/gen_bootloader.sh || gen_die "Could not read ${GK_BIN}/gen_bootloader.sh"
24
25 BUILD_KERNEL=0
26 BUILD_INITRD=0
27
28 # Parse all command line options...
29 while [ $# -gt 0 ]
30 do
31         Option=$1; shift
32         parse_cmdline $Option
33 done
34
35 # Check if no action is specified...
36 if [ "${BUILD_KERNEL}" -eq 0 -a "${BUILD_INITRD}" -eq 0 ]
37 then
38         usage
39         exit 1
40 fi
41
42 clear_log
43 NORMAL=${GOOD} print_info 1 "Gentoo Linux Genkernel; Version ${GK_V}${NORMAL}"
44
45 # Set ${ARCH}
46 get_official_arch
47
48 # Read arch-specific config
49 source ${ARCH_CONFIG} || gen_die "Could not read ${ARCH_CONFIG}"
50 source ${GK_SHARE}/${ARCH}/modules_load || gen_die "Could not read ${GK_SHARE}/${ARCH}/modules_load"
51
52 [ ! -f "${TEMP}" ] && mkdir -p "${TEMP}"
53 [ ! -f "${CACHE_DIR}" ] && mkdir -p "${CACHE_DIR}"
54
55 # Based on genkernel.conf, arch-specific configs, and commandline options,
56 # get the real arguments for usage...
57
58 determine_real_args
59 dump_debugcache
60
61 NORMAL=${BOLD} print_info 1 "Compiling Linux Kernel ${BOLD}${KV}${NORMAL} for ${BOLD}${ARCH}${NORMAL}..."
62 echo
63
64 if [ "${BUILD_INITRD}" -ne '0' ]
65 then
66         if ! has_loop
67         then
68                 modprobe loop 2>/dev/null
69                 if ! has_loop
70                 then
71                         print_error 1 'Your kernel does not appear to have loop device support.'
72                         print_error 1 'Please load loop support before running genkernel!'
73                         gen_die 'Load loop support!'
74                 else
75                         print_info 1 'loop: "loop" module loaded successfully...'
76                 fi
77         fi
78 fi
79
80 # Check /boot is mounted
81 if isTrue ${CMD_NOINSTALL}
82 then
83         isTrue ${MOUNTBOOT} && print_info 2 'Skipping automatic mount of boot'
84 else
85         if ! egrep -q ' /boot ' /proc/mounts
86         then
87                 if egrep -q '^[^#].+    /boot   ' /etc/fstab
88                 then
89                         if isTrue ${MOUNTBOOT}
90                         then
91                                 if ! mount /boot
92                                 then
93                                         print_warning 1 "${BOLD}WARNING${NORMAL}: Failed to mount /boot!"
94                                         echo
95                                 else
96                                         print_info 1 'mount: /boot mounted successfully!'
97                                 fi
98                         else
99                                 print_warning 1 "${BOLD}WARNING${NORMAL}: No mounted /boot partition detected!"
100                                 print_warning 1 '         Run ``mount /boot`` to mount it!'
101                                 echo
102                         fi
103                 fi
104         elif isBootRO
105         then
106                 if isTrue ${MOUNTBOOT}
107                 then
108                         if ! mount -o remount,rw /boot
109                         then
110                                 print_warning 1 "${BOLD}WARNING${NORMAL}: Failed to remount /boot RW!"
111                                 echo
112                         else
113                                 print_info 1 "mount: /boot remounted read/write successfully!"
114                                 BOOTRW=1
115                         fi
116                 fi
117         fi
118 fi
119
120 # Check whether another Genkernel is running
121 GENPIDS="`ps -C genkernel --no-headers -o pid | wc -l`"
122 if [ "${GENPIDS}" -gt '3' ]
123 then
124         [ "${GENPIDS}" -gt '4' ] && EX='s'
125         print_warning 1 "${BOLD}WARNING${NORMAL}: Another Genkernel instance is running under"
126         print_warning 1 "         process ID${EX} " 0
127         GENPIDS=`ps -C genkernel --no-headers -o pid`
128         echo -n "${GENPIDS}" | sed -e "s/$$//; s/ /, /g"
129         echo 'halting...'
130         echo
131         print_warning 1 'Running multiple genkernels on the same source tree will cause data loss!'
132         print_info 1 "Press ^C to halt; ^D to continue [ ${BOLD}if${NORMAL} you know what you're doing! ]"
133         cat
134         echo
135         CTEMP="${TEMP}"
136         TEMP="/var/tmp/genkernel-`dd if=/dev/random count=1 2>/dev/null | md5sum | cut -d\  -f1`"
137         print_info 1 'thread: Running multiple genkernels may cause problems!'
138         print_info 1 "thread: Temporary files reallocated to ${TEMP}..."
139         echo
140 fi
141
142 if [ ${BUILD_KERNEL} -eq 1 ]
143 then
144         # Configure kernel
145         config_kernel
146
147         # Make deps
148         compile_dep
149
150         # Make prepare [2.6]
151         [ "${VER}" -gt '2' ] || [ "${VER}" -eq '2' -a "${PAT}" -ge '6' ] && compile_generic prepare kernel
152
153         # Run postconf
154         if [ "${CMD_POSTCONF}" != '' ]
155         then
156                 print_info 1 '' 1 0
157                 print_info 1 "Preparing to run postconf: \"${CMD_POSTCONF}\"" 0
158
159                 CALLBACK_ESCAPE=0
160                 CALLBACK_COUNT=0
161
162                 trap "CALLBACK_ESCAPE=1" TERM KILL INT QUIT ABRT
163                 while [[ ${CALLBACK_ESCAPE} -eq 0 && ${CALLBACK_COUNT} -lt 5 ]]
164                 do
165                         sleep 1; echo -n '.';
166                         let CALLBACK_COUNT=${CALLBACK_COUNT}+1
167                 done
168
169                 if [ "${CALLBACK_ESCAPE}" -eq 0 ]
170                 then
171                         echo
172                         echo
173                         eval ${CMD_POSTCONF} | tee -a ${DEBUGFILE}
174                         CMD_STATUS="${PIPESTATUS[0]}"
175                         echo
176                         print_info 1 "<<< Postconf callback exit status: ${CMD_STATUS}"
177                         [ "${CMD_STATUS}" -ne 0 ] && gen_die '--postconf failed!'
178                 else
179                         echo
180                         print_info 1 '>>> Postconf cancelled...'
181                 fi
182                 trap - TERM KILL INT QUIT ABRT
183                 print_info 1 '' 1 0
184         fi
185
186         if [ "${GENSPLASH}" -eq '1' ]
187         then
188                 if [ -x /sbin/splash ]
189                 then
190                         [ -z "${GENSPLASH_THEME}" ] && [ -e /etc/conf.d/splash ] && source /etc/conf.d/splash
191                         [ -z "${GENSPLASH_THEME}" ] && GENSPLASH_THEME=default
192                         print_info 1 "  >> Installing gensplash [ using the ${GENSPLASH_THEME} theme ]..."
193                         # 2.6.9
194                         sed -i -e 's:$(obj)/gen_init_cpio $(initramfs-y) FORCE:$(if $(shell [ ! -f $(obj)/initramfs_data.cpio ] \&\& echo 1 ), $(obj)/gen_init_cpio $(initramfs-y)):' ${KERNEL_DIR}/usr/Makefile
195                         # 2.6.10
196                         sed -i -e 's:$(obj)/gen_init_cpio \\:$(if $(shell [ ! -f $(obj)/initramfs_data.cpio ] \&\& echo 1 ), $(obj)/gen_init_cpio $(initramfs-y) $(obj)/initramfs_list):' -e 's:$(initramfs-y) $(obj)/initramfs_list FORCE::' ${KERNEL_DIR}/usr/Makefile
197                         splash_geninitramfs -g ${KERNEL_DIR}/usr/initramfs_data.cpio.gz ${GENSPLASH_THEME}
198                         gunzip ${KERNEL_DIR}/usr/initramfs_data.cpio.gz
199                 else
200                         print_warning 1 '               >> No splash detected; skipping!'
201                 fi
202         fi
203
204         # Compile kernel
205         compile_kernel
206
207         # Compile modules
208         compile_modules
209
210         if [ ${SAVE_CONFIG} -eq 1 ]
211         then
212                 print_info 1 "Copying config for successful build to /etc/kernels/kernel-config-${ARCH}-${KV}"
213                 [ ! -e '/etc/kernels' ] && mkdir -p /etc/kernels
214                 cp "${KERNEL_DIR}/.config" "/etc/kernels/kernel-config-${ARCH}-${KV}"
215         fi
216 fi
217
218 # Run callback
219 if [ "${CMD_CALLBACK}" != "" ]
220 then
221         print_info 1 "" 1 0
222         print_info 1 "Preparing to run callback: \"${CMD_CALLBACK}\"" 0
223
224         CALLBACK_ESCAPE=0
225         CALLBACK_COUNT=0
226
227         trap "CALLBACK_ESCAPE=1" TERM KILL INT QUIT ABRT
228         while [[ ${CALLBACK_ESCAPE} -eq 0 && ${CALLBACK_COUNT} -lt 5 ]]
229         do
230                 sleep 1; echo -n '.';
231                 let CALLBACK_COUNT=${CALLBACK_COUNT}+1
232         done
233
234         if [ "${CALLBACK_ESCAPE}" -eq 0 ]
235         then
236                 echo
237                 echo
238                 eval ${CMD_CALLBACK} | tee -a ${DEBUGFILE}
239                 CMD_STATUS="${PIPESTATUS[0]}"
240                 echo
241                 print_info 1 "<<< Callback exit status: ${CMD_STATUS}"
242                 [ "${CMD_STATUS}" -ne 0 ] && gen_die '--callback failed!'
243         else
244                 echo
245                 print_info 1 ">>> Callback cancelled..."
246         fi
247         trap - TERM KILL INT QUIT ABRT
248         print_info 1 "" 1 0
249 fi
250
251 if [ "${BUILD_INITRD}" -eq '1' ]
252 then
253         # Compile dietlibc
254         # if [ "${USE_DIETLIBC}" = '1' ]
255         # then
256         #       compile_dietlibc
257         # fi
258
259         # Compile Busybox
260         compile_busybox
261
262         # Only compile insmod if we're installing modules onto the initrd
263         if [ "${NOINITRDMODULES}" = '' ]
264         then
265                 if [ "${PAT}" -gt '4' ]
266                 then
267                         # Compile module-init-tools
268                         compile_module_init_tools
269                 else
270                         compile_modutils
271                 fi
272         fi
273
274         compile_devfsd
275         if [ "${DEVFS}" -eq '0' ]
276         then
277                 [ "${VER}" -gt '2' ] || [ "${VER}" -eq '2' -a "${PAT}" -ge '6' ] && UDEV=1
278         fi
279         [ "${UDEV}" -eq '1' ] && compile_udev
280
281         # Create initrd
282         create_initrd
283 else
284         print_info 1 'initrd: Not building since only the kernel was requested...'
285 fi
286 [ "${MINKERNPACKAGE}" != "" ] && gen_minkernpackage
287 [ "${MAXKERNPACKAGE}" != "" ] && gen_maxkernpackage
288
289 # Clean up...
290 [ -n "${CTEMP}" ] && rm -rf "${TEMP}"
291
292 if [ "${BUILD_KERNEL}" -eq '1' ]
293 then
294         set_bootloader
295         print_info 1 ''
296         print_info 1 "Kernel compiled successfully!"
297         print_info 1 ''
298         print_info 1 'Required Kernel Parameters:'
299         if [ "${BUILD_INITRD}" -eq '0' ]
300         then
301                 print_info 1 '    root=/dev/$ROOT'
302                 print_info 1 '    [ And "vga=0x317 splash=verbose" if you use a framebuffer ]'
303                 print_info 1 ''
304                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
305                 print_info 1 '    one specified in /etc/fstab'
306         else    
307                 print_info 1 '    root=/dev/ram0 real_root=/dev/$ROOT init=/linuxrc'
308                 [ "${INITRD_SIZE}" -ge 4096 ] && print_info 1 "    ramdisk_size=8192"
309                 print_info 1 '    [ And "vga=0x317 splash=verbose" if you use a framebuffer ]'
310                 print_info 1 ''
311                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
312                 print_info 1 '    one specified in /etc/fstab'
313                 print_info 1 ''
314                 print_info 1 "If you require Genkernel's hardware detection features; you MUST"
315                 print_info 1 'tell your bootloader to use the provided initrd file. Otherwise;'
316                 print_info 1 'substitute the root argument for the real_root argument if you are'
317                 print_info 1 'not planning to use the initrd...'
318         fi
319 else
320         print_info 1 ''
321         print_info 1 'initrd compiled successfully!'
322 fi
323
324 [ "${BOOTRW}" != '' ] && mount -o remount,ro /boot
325
326 print_info 1 ''
327 print_info 1 'Do NOT report kernel bugs as genkernel bugs unless your bug'
328 print_info 1 'is about the default genkernel configuration...'
329 print_info 1 ''
330 print_info 1 'Make sure you have the latest genkernel before reporting bugs.'