98f24d9a097cd006cd1a6b91577392259b90a2e2
[genkernel.git] / genkernel
1 #!/bin/bash
2 # $Id$
3
4 PATH="${PATH}:/sbin:/usr/sbin"
5 GK_V='3.4.40'
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 LOGFILE is fully qualified.
12
13 small_die() {
14   echo $1
15   exit 1
16 }
17
18 parse_opt() {
19         case "$1" in
20                 *\=*)
21                         echo "$1" | cut -f2- -d=
22                 ;;
23         esac
24 }
25
26 # We don't know where our config is, so we check for it, and default to using
27 # /etc/genkernel.conf if nobody has specified one.
28
29 # NOTE: We are look for --config=... in a way that doesn't modify $@ since we access that again, later
30 for (( i=1; i<=$# ; i=i+1 )); do
31         eval arg="\$$i"
32         [[ "${arg}" = --config=* ]] && CMD_GK_CONFIG=`parse_opt "${arg}"`
33 done
34
35 # Pull in our configuration
36 _GENKERNEL_CONF=${CMD_GK_CONFIG:-/etc/genkernel.conf}
37 source "${_GENKERNEL_CONF}" || small_die "Could not read ${_GENKERNEL_CONF}"
38
39 # Start sourcing other scripts
40 source ${GK_SHARE}/gen_funcs.sh || small_die "Could not read ${GK_SHARE}/gen_funcs.sh"
41 source ${GK_SHARE}/gen_cmdline.sh || gen_die "Could not read ${GK_SHARE}/gen_cmdline.sh"
42 source ${GK_SHARE}/gen_arch.sh || gen_die "Could not read ${GK_SHARE}/gen_arch.sh"
43 source ${GK_SHARE}/gen_determineargs.sh || gen_die "Could not read ${GK_SHARE}/gen_determineargs.sh"
44 source ${GK_SHARE}/gen_compile.sh || gen_die "Could not read ${GK_SHARE}/gen_compile.sh"
45 source ${GK_SHARE}/gen_configkernel.sh || gen_die "Could not read ${GK_SHARE}/gen_configkernel.sh"
46 source ${GK_SHARE}/gen_initramfs.sh || gen_die "Could not read ${GK_SHARE}/gen_initramfs.sh"
47 source ${GK_SHARE}/gen_moddeps.sh || gen_die "Could not read ${GK_SHARE}/gen_moddeps.sh"
48 source ${GK_SHARE}/gen_package.sh || gen_die "Could not read ${GK_SHARE}/gen_package.sh"
49 source ${GK_SHARE}/gen_bootloader.sh || gen_die "Could not read ${GK_SHARE}/gen_bootloader.sh"
50
51 TEMP=${TMPDIR}/$RANDOM.$RANDOM.$RANDOM.$$
52
53 trap_cleanup(){
54         # Call exit code of 1 for failure
55         cleanup
56         exit 1
57 }
58
59 cleanup(){
60     if [ -n "$TEMP" -a -d "$TEMP" ]; then
61         rm -rf "$TEMP"
62     fi
63
64     if isTrue ${POSTCLEAR}
65     then
66             echo
67             print_info 1 'RUNNING FINAL CACHE/TMP CLEANUP'
68             print_info 1 "CACHE_DIR: ${CACHE_DIR}"
69             CLEAR_CACHE_DIR='yes'
70             setup_cache_dir
71             echo
72             print_info 1 "TMPDIR: ${TMPDIR}"
73             clear_tmpdir
74             fi
75 }
76
77 trap trap_cleanup SIGHUP SIGQUIT SIGINT SIGTERM SIGKILL
78 BUILD_KERNEL=0
79 BUILD_RAMDISK=0
80 BUILD_MODULES=0
81
82 # Parse all command line options...
83 Options=$* # Save for later
84 while [ $# -gt 0 ]
85 do
86         Option=$1; shift
87         parse_cmdline $Option
88 done
89
90 # Check if no action is specified...
91 if [ "${BUILD_KERNEL}" = '0' -a "${BUILD_RAMDISK}" = '0' ]
92 then
93         usage
94         exit 1
95 fi
96
97 clear_log
98 NORMAL=${GOOD} print_info 1 "Gentoo Linux Genkernel; Version ${GK_V}${NORMAL}"
99 print_info 1 "Running with options: ${Options}"
100 echo
101
102 # Set ${ARCH}
103 get_official_arch
104
105 # Read arch-specific config
106 print_info 1 "Using genkernel.conf from ${_GENKERNEL_CONF}"
107 print_info 1 "Sourcing arch-specific config.sh from ${ARCH_CONFIG} .."
108 source ${ARCH_CONFIG} || gen_die "Could not read ${ARCH_CONFIG}"
109 _MODULES_LOAD=${GK_SHARE}/arch/${ARCH}/modules_load
110 print_info 1 "Sourcing arch-specific modules_load from ${_MODULES_LOAD} .."
111 source "${_MODULES_LOAD}" || gen_die "Could not read ${_MODULES_LOAD}"
112 echo
113
114 # Merge modules_load from config
115 for group_modules in ${!AMODULES_*}; do
116         group="$(echo $group_modules | cut -d_ -f2)"
117         eval cmodules="\$$group_modules"
118         eval MODULES_${group}=\"\${MODULES_${group}} ${cmodules}\"
119         print_info 1 "<config> Merged AMODULES_${group}:'${cmodules}' into MODULES_${group}"
120 done
121
122
123 # Based on genkernel.conf, arch-specific configs, and commandline options,
124 # get the real arguments for usage...
125
126 determine_real_args
127 determine_config_file
128
129 set_kernel_arch
130
131 [ ! -f "${TEMP}" ] && mkdir -p "${TEMP}"
132
133 setup_cache_dir
134
135 check_distfiles
136
137 dump_debugcache
138
139 NORMAL=${BOLD} print_info 1 "Linux Kernel ${BOLD}${KV}${NORMAL} for ${BOLD}${ARCH}${NORMAL}..."
140 print_info 1 ".. with config file ${KERNEL_CONFIG}"
141
142 # Check BOOTDIR is mounted
143 if ! isTrue ${CMD_INSTALL}
144 then
145         isTrue ${MOUNTBOOT} && print_info 2 'Skipping automatic mount of boot'
146 else
147         [[ -d ${BOOTDIR} ]] || gen_die "${BOOTDIR} is not a directory"
148         
149         if ! egrep -q "[[:space:]]${BOOTDIR}[[:space:]]" /proc/mounts
150         then
151                 if egrep -q "^[^#].+[[:space:]]${BOOTDIR}[[:space:]]" /etc/fstab
152                 then
153                         if isTrue ${MOUNTBOOT}
154                         then
155                                 if ! mount ${BOOTDIR}
156                                 then
157                                         print_warning 1 "${BOLD}WARNING${NORMAL}: Failed to mount ${BOOTDIR}!"
158                                         echo
159                                 else
160                                         print_info 1 "mount: ${BOOTDIR} mounted successfully!"
161                                 fi
162                         else
163                                 print_warning 1 "${BOLD}WARNING${NORMAL}: No mounted ${BOOTDIR} partition detected!"
164                                 print_warning 1 "         Run ``mount ${BOOTDIR}`` to mount it!"
165                                 echo
166                         fi
167                 fi
168         elif isBootRO
169         then
170                 if isTrue ${MOUNTBOOT}
171                 then
172                         if ! mount -o remount,rw ${BOOTDIR}
173                         then
174                                 print_warning 1 "${BOLD}WARNING${NORMAL}: Failed to remount ${BOOTDIR} RW!"
175                                 echo
176                         else
177                                 print_info 1 "mount: ${BOOTDIR} remounted read/write successfully!"
178                                 BOOTRW=1
179                         fi
180                 fi
181         fi
182 fi
183
184 ## Check whether another Genkernel is running
185 #GENPIDS="`ps -C genkernel --no-headers -o pid | wc -l`"
186 #if [ "${GENPIDS}" -gt '3' ]
187 #then
188 #       [ "${GENPIDS}" -gt '4' ] && EX='s'
189 #       print_warning 1 "${BOLD}WARNING${NORMAL}: Another Genkernel instance is running under"
190 #       print_warning 1 "         process ID${EX} " 0
191 #       GENPIDS=`ps -C genkernel --no-headers -o pid`
192 #       echo -n "${GENPIDS}" | sed -e "s/$$//; s/ /, /g"
193 #       echo 'halting...'
194 #       echo
195 #       print_warning 1 'Running multiple genkernels on the same source tree will cause data loss!'
196 #       print_info 1 "Press ^C to halt; ^D to continue [ ${BOLD}if${NORMAL} you know what you're doing! ]"
197 #       echo
198 #       CTEMP="${TEMP}"
199 #       TEMP=${TMPDIR-/tmp}
200 #       TEMP=${TEMP}/genkernel.$RANDOM.$RANDOM.$RANDOM.$$
201 #       print_info 1 'thread: Running multiple genkernels may cause problems!'
202 #       print_info 1 "thread: Temporary files reallocated to ${TEMP}..."
203 #       echo
204 #fi
205
206 KERNCACHE_IS_VALID=0
207 if [ "${KERNCACHE}" != "" ] 
208 then
209     gen_kerncache_is_valid
210 fi
211
212 if [ ${BUILD_KERNEL} = '1' -a "${KERNCACHE_IS_VALID}" = '0' ]
213 then
214         # Configure kernel
215         config_kernel
216         
217         # Make prepare
218         if [ "${ARCH_HAVENOPREPARE}" = '' ]
219         then
220                 compile_generic prepare kernel
221         fi
222         
223         # KV may have changed due to the configuration
224         get_KV
225
226         compile_kernel
227
228         # Compile modules
229         if [ "${BUILD_MODULES}" = '1' -a "${BUILD_STATIC}" = '0' ]
230         then
231                 compile_modules
232         fi
233
234         if [ "${SAVE_CONFIG}" = '1' ]
235         then
236                 print_info 1 "Copying config for successful build to /etc/kernels/kernel-config-${ARCH}-${KV}"
237                 [ ! -e '/etc/kernels' ] && mkdir -p /etc/kernels
238                 cp "${KERNEL_DIR}/.config" "/etc/kernels/kernel-config-${ARCH}-${KV}"
239         fi
240 fi
241
242 if isTrue "${CMD_INSTALL}"
243 then
244         if [ "${KERNCACHE}" != "" -a "${KERNCACHE_IS_VALID}" != "0" ] 
245         then
246                 gen_kerncache_extract_kernel
247         fi
248 fi
249
250 if [ "${KERNCACHE}" != "" -a "${KERNCACHE_IS_VALID}" != "0" ] 
251 then
252         [ "${BUILD_STATIC}" = '0' ] && gen_kerncache_extract_modules
253         gen_kerncache_extract_config
254 fi
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}" = '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}" = '0' ]
273         then
274                 echo
275                 echo
276                 eval ${CMD_CALLBACK} | tee -a ${LOGFILE}
277                 CMD_STATUS="${PIPESTATUS[0]}"
278                 echo
279                 print_info 1 "<<< Callback exit status: ${CMD_STATUS}"
280                 [ "${CMD_STATUS}" != '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_RAMDISK}" = '1' ]
290 then
291         if [ "${BUSYBOX}" = '1' ]
292         then
293                 # Compile Busybox
294                 compile_busybox
295         fi
296
297         if isTrue "${UNIONFS}"
298         then
299                 compile_unionfs_fuse
300         fi
301
302         # Compile initramfs
303         create_initramfs
304 else
305         print_info 1 'initrd: Not building since only the kernel was requested...'
306 fi
307
308 if isTrue "${INTEGRATED_INITRAMFS}" #|| [ "${BUILD_KERNEL}" = '1' -a "${KERNCACHE_IS_VALID}" == "0" ]
309 then
310         # We build the kernel a second time to include the initramfs
311         compile_kernel
312 fi
313
314 [ "${KERNCACHE}" != "" ] && gen_kerncache
315 [ "${MINKERNPACKAGE}" != '' ] && gen_minkernpackage
316 [ "${MODULESPACKAGE}" != '' ] && gen_modulespackage
317
318 # Clean up...
319 [ -n "${CTEMP}" ] && rm -rf "${TEMP}"
320
321 if [ "${BUILD_KERNEL}" = '1' ]
322 then
323         if isTrue "${CMD_INSTALL}"
324         then
325                 set_bootloader
326         fi
327         print_info 1 ''
328         print_info 1 "Kernel compiled successfully!"
329         print_info 1 ''
330         print_info 1 'Required Kernel Parameters:'
331         if [ "${BUILD_RAMDISK}" = '0' ]
332         then
333                 print_info 1 '    root=/dev/$ROOT'
334                 print_info 1 '    [ And "vga=0x317 splash=verbose" if you use a framebuffer ]'
335                 print_info 1 ''
336                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
337                 print_info 1 '    one specified in /etc/fstab'
338         else
339                 print_info 1 '    real_root=/dev/$ROOT'
340                 print_info 1 ''
341                 print_info 1 '    Where $ROOT is the device node for your root partition as the'
342                 print_info 1 '    one specified in /etc/fstab'
343                 print_info 1 ''
344                 print_info 1 "If you require Genkernel's hardware detection features; you MUST"
345                 print_info 1 'tell your bootloader to use the provided INITRAMFS file. Otherwise;'
346                 print_info 1 'substitute the root argument for the real_root argument if you are'
347                 print_info 1 'not planning to use the initramfs...'
348         fi
349 fi
350
351 if [ "${BUILD_RAMDISK}" = '1' ]
352 then
353         echo
354         print_warning 1 'WARNING... WARNING... WARNING...'
355         print_warning 1 'Additional kernel cmdline arguments that *may* be required to boot properly...'
356         [ "${SPLASH}" = '1' ] && print_warning 1 "add \"vga=791 splash=silent,theme:${SPLASH_THEME} console=tty1 quiet\" if you use a splash framebuffer ]"
357         [ "${LVM}" = '1' ] && print_warning 1 'add "dolvm" for lvm support'
358         [ "${DMRAID}" = '1' ] && print_warning 1 'add "dodmraid" for dmraid support'
359         [ "${MDADM}" = '1' ] && print_warning 1 'add "domdadm" for RAID support'
360         [ "${DMRAID}" = '1' ] && print_warning 1 '      or "dodmraid=<additional options>"'
361         [ "${ZFS}" = '1' ] && print_warning 1 'add "dozfs" for ZFS volume management support'
362         [ "${ZFS}" = '1' ] && print_warning 1 'add either "real_root=ZFS" (bootfs autodetection) or "real_root=ZFS=<dataset>" to boot from a ZFS dataset'
363         [ "${ISCSI}" = '1' ] && print_warning 1 'add at least "iscsi_initiatorname=<initiator name> iscsi_target=<target name> and iscsi_address=<target ip>" for iscsi support'
364
365         if [[ "$(file --brief --mime-type "${KERNEL_CONFIG}")" == application/x-gzip ]]; then
366                 # Support --kernel-config=/proc/config.gz, mainly
367                 CONFGREP=zgrep
368         else
369                 CONFGREP=grep
370         fi
371
372         if [ `${CONFGREP} 'CONFIG_EXT[0-9]_FS=' "${KERNEL_CONFIG}" | wc -l` -ge 2 ]; then
373                 print_warning 1 'With support for several ext* filesystems available, it may be needed to'
374                 print_warning 1 'add "rootfstype=ext3" or "rootfstype=ext4" to the list of boot parameters.'
375         fi
376
377         unset CONFGREP
378 fi
379
380 [ "${BOOTRW}" != '' ] && mount -o remount,ro ${BOOTDIR}
381
382 echo
383 print_info 1 'Do NOT report kernel bugs as genkernel bugs unless your bug'
384 print_info 1 'is about the default genkernel configuration...'
385 print_info 1 ''
386 print_info 1 'Make sure you have the latest ~arch genkernel before reporting bugs.'
387
388 #Final Cleanup
389 cleanup