]> git.pld-linux.org Git - projects/geninitrd.git/blame - geninitrd
fix verbose via -v -v
[projects/geninitrd.git] / geninitrd
CommitLineData
bb529f94
JK
1#!/bin/sh
2
3# geninitrd
4#
45d4d8cb 5# by PLD Linux Team
bb529f94 6#
9e1ceabe 7# based on mkinitrd from RedHat Linux
e4b07ddc 8#
c31050f3 9
476f99fc 10GENINITRD_RCSID='$Revision$ $Date:: $'
96fbf9e6 11R=${GENINITRD_RCSID#* *}; VERSION=${R%% *}
ca2c2012 12PROGRAM=${0##*/}
bb529f94
JK
13
14. /etc/rc.d/init.d/functions
ded87775 15. /lib/geninitrd/functions
e2405b29 16. /etc/sysconfig/system
bb529f94 17
c124d0cf
ER
18# list of geninitrd modules which need setup routine after commandline args parsing
19GENINITRD_MODS=""
c3667d07 20COMPRESS=yes
897d14b7 21STRIP=/usr/bin/strip
bb529f94
JK
22target=""
23kernel=""
24force=""
25verbose=""
26MODULES=""
27img_vers=""
c3667d07 28fstab=/etc/fstab
c3667d07 29modext=.o
4e9eb79c 30rootdev_nr=0
738c05d8
ER
31# device node for rootfs from fstab
32rootdev=""
bb529f94 33
8bd582f1
ER
34# internal variables
35# is /dev on tmpfs
b64f015b
ER
36dev_mounted=no
37# is /proc mounted
38proc_mounted=no
39# is /sys mounted
40sys_mounted=no
41# is /tmp mounted on tmpfs
42tmp_mounted=no
43
44# are /dev nodes already created from /proc/devices info?
8bd582f1 45proc_partitions=no
6b013929 46
b64f015b 47usage() {
46768f45 48 echo "Usage: $PROGRAM [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
ac085800 49 echo " [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
8a6f083b 50 echo " [--compress=yes|xz|lzma|bzip2|gzip|lzo]"
76a48507 51 echo " [--nostrip ] [--strip PATH/strip] [--strip=PATH/strip]"
92ed99b6 52 echo " [--initrdfs=rom|initramfs|ext2|cram] [--modules-conf=<modules.conf>]"
ba5d09f0 53 echo " [--with-bootsplash] [--without-bootsplash]"
6c69a2d4
ER
54 echo " [--with-fbsplash] [--without-fbsplash]"
55 echo " [--with-fbcondecor] [--without-fbcondecor]"
e4b07ddc 56 echo " [--lvmtoolsversion=1|2] [--with-udev] [--without-udev]"
ba5d09f0 57 echo " [--with-suspend] [--without-suspend]"
1cd2aabd 58 echo " [--with-tuxonice] [--without-tuxonice]"
ecd7bf46
ER
59 echo " [--without-dmraid]"
60 echo " [--with-multipath=DEVPATH] [--without-multipath]"
3ef3d717 61 echo " [--without-blkid] [--without-luks]"
ac085800 62 echo " <initrd-image> <kernel-version>"
553786c4 63 echo ""
46768f45
ER
64 echo "Example:"
65
0ad676eb 66 local kdir kver dir=${target:-/boot}
46768f45
ER
67 for kdir in /lib/modules/*; do
68 [ -d $kdir ] || continue
69 kver=${kdir##*/}
dfe1a27c 70 echo " $0 -f --initrdfs=initramfs $dir/initrd-$kver.gz $kver $verbose"
63514069 71 done | sort -V
bb529f94
JK
72}
73
779a218a 74msg() {
f4010f7f
AM
75 echo "$PROGRAM: $*"
76}
77
78warn() {
4a60c01f 79 msg "WARNING: $*" >&2
779a218a
AM
80}
81
02ba8ab7 82verbose() {
34f3f0e9 83 [ -n "$verbose" ] && msg "$*" >&3
02ba8ab7
ER
84}
85
94769f11 86debug() {
02ba8ab7 87 [ x"$verbose" = x"-v -v" ] && msg "$*" >&3
94769f11 88}
aa69da6e 89
bf6c3fcb
ER
90# aborts program abnormally
91die() {
b5f5c089 92 local rc=${2:-1}
046c68c9 93 msg "ERROR: $1" >&2
bf6c3fcb
ER
94 exit $rc
95}
96
684d5d2a
ER
97# find program from specified paths
98find_tool() {
99 local x
100 for x in "$@"; do
101 if [ -x "$x" ]; then
102 echo $x
02ba8ab7 103 verbose "find_tool: found $x"
684d5d2a
ER
104 return 0
105 fi
106 done
1e246840 107 debug "find_tool: did not find any of: $@"
684d5d2a
ER
108 return 1
109}
110
c124d0cf
ER
111# loads geninitrd modules
112geninitrd_load_mods() {
113 local mod
114 for mod in "$@"; do
115 if [ ! -f /lib/geninitrd/mod-$mod.sh ]; then
116 die "$mod geninitrd module can't be loaded"
117 fi
118 . /lib/geninitrd/mod-$mod.sh
119
c34c6a69 120 GENINITRD_MODS="$GENINITRD_MODS $mod"
c124d0cf
ER
121 done
122}
123
124# setup geninitrd modules
125geninitrd_setup_mods() {
c34c6a69 126 local mod rcsid
c124d0cf
ER
127
128 for mod in $GENINITRD_MODS; do
c34c6a69
ER
129 eval rcsid=$(echo \$$mod | LC_ALL=C tr '[a-z]' '[A-Z]')_RCSID
130 debug "# $rcsid (mod-$mod)"
131
132 # some mods want init
133 if type setup_mod_$mod > /dev/null; then
134 eval setup_mod_$mod
135 fi
c124d0cf
ER
136 done
137}
138
c6c6ce01
ER
139# append text to /linuxrc
140# takes STDIN as input
141add_linuxrc() {
b64f015b 142 cat >> "$RCFILE"
c6c6ce01
ER
143}
144
b64f015b
ER
145# generate code to mount /dev on tmpfs and create initial nodes
146# can be called multiple times. /dev is cleaned up (umounted) automatically at
147# the end of script.
148mount_dev() {
b64f015b
ER
149 # we already generated tmpfs code; return
150 if is_yes "$dev_mounted"; then
151 return
152 fi
153
154 dev_mounted=yes
155
156 busybox_applet mount mknod mkdir
157 add_linuxrc <<-EOF
158 : 'Creating /dev'
621695f6
AF
159 if ! mount -t devtmpfs -o mode=0755,nosuid devtmpfs /dev > /dev/null 2>&1; then
160 mount -o mode=0755,nosuid -t tmpfs tmpfs /dev
161 mknod /dev/console c 5 1
162 mknod /dev/null c 1 3
163 mknod /dev/zero c 1 5
164 mknod /dev/random c 1 8
165 mknod /dev/snapshot c 10 231
166 mknod /dev/urandom c 1 9
167 mknod /dev/ptmx c 5 2
168 mknod /dev/kmsg c 1 11
169 fi
b64f015b
ER
170 mkdir /dev/pts
171 mkdir /dev/shm
172 EOF
173}
174
175# generate code to mount /proc on initrd
176# can be called multiple times
177mount_proc() {
178 if is_yes "$proc_mounted"; then
179 return
180 fi
181
182 proc_mounted=yes
2bface63
ER
183 if [ "$INITRDFS" = "initramfs" ]; then
184 # /proc is mounted with initramfs 2.6.22.14 kernel
185 # XXX: remove when it is clear why proc was already mounted
186 echo "[ -f /proc/cmdline ] || mount -t proc none /proc" | add_linuxrc
187 else
188 echo "mount -t proc none /proc" | add_linuxrc
189 fi
b64f015b
ER
190}
191
192# generate code to mount /sys on initrd
193# can be called multiple times
194mount_sys() {
195 if is_yes "$sys_mounted"; then
196 return
197 fi
198
199 sys_mounted=yes
200 echo "mount -t sysfs none /sys" | add_linuxrc
201}
202
203# generate code to mount /tmp on initrd
204# can be called multiple times
205mount_tmp() {
206 if [ "$INITRDFS" = "initramfs" ]; then
207 # initramfs is read-write filesystem, no need for tmpfs
208 return
209 fi
210
211 if is_yes "$tmp_mounted"; then
212 return
213 fi
214
215 tmp_mounted=yes
216 echo "mount -t tmpfs none /tmp" | add_linuxrc
217}
218
d63131e0
AF
219# generate code to mount /run on initrd
220# can be called multiple times
221mount_run() {
222 if is_yes "$run_mounted"; then
223 return
224 fi
225
226 run_mounted=yes
227 echo "mount -t tmpfs run /run" | add_linuxrc
228}
229
b64f015b
ER
230# unmount all mountpoints mounted by geninitrd
231umount_all() {
ec49b7e6
ER
232
233 add_linuxrc <<-'EOF'
903f21ea 234 : Last shell before umounting all and giving control over to real init.
ec49b7e6
ER
235 debugshell
236 EOF
5b70f84d 237
d63131e0
AF
238 if is_yes "$run_mounted"; then
239 echo 'mount --bind /run /newroot/run' | add_linuxrc
240 echo 'umount /run' | add_linuxrc
241 run_mounted=no
242 fi
b64f015b
ER
243 if is_yes "$dev_mounted"; then
244 echo 'umount /dev' | add_linuxrc
245 dev_mounted=no
246 fi
b64f015b
ER
247 if is_yes "$sys_mounted"; then
248 echo 'umount /sys' | add_linuxrc
249 sys_mounted=no
250 fi
251 if is_yes "$tmp_mounted"; then
252 echo 'umount /tmp' | add_linuxrc
253 tmp_mounted=no
254 fi
3c42f0a4
ER
255 if is_yes "$proc_mounted"; then
256 echo 'umount /proc' | add_linuxrc
257 proc_mounted=no
258 fi
b64f015b
ER
259}
260
261
9299682f
ER
262# Checks if busybox has support for APPLET(s)
263# Exits from geninitrd if the support is not present.
264#
265# NB! XXX do not output to STDOUT, it will appear in initrd images in some cases!
266busybox_applet() {
30495bbf 267 local err=0 applet
9299682f
ER
268
269 if [ -z "$busybox_functions" ]; then
684d5d2a 270 local tmp=$($busybox 2>&1)
9b1373fb
ER
271
272 # BusyBox v1.1.3 says applet not found if it's not called 'busybox'.
273 if [[ "$tmp" = *applet\ not\ found* ]]; then
274 local t=$(mktemp -d)
684d5d2a 275 ln -s $busybox $t/busybox
9b1373fb
ER
276 local tmp=$($t/busybox 2>&1)
277 rm -rf $t
278 fi
279
280 busybox_functions=$(echo "$tmp" | \
9299682f
ER
281 sed -ne '/Currently defined functions:/,$p' | \
282 xargs | sed -e 's,.*Currently defined functions: ,,'
283 )
284 fi
285 for applet in $*; do
286 local have
287 # try cache
288 eval have='$'busybox_have_$applet
289 if [ -z "$have" ]; then
290 have=$(echo "$busybox_functions" | egrep -c "( |^)$applet(,|$)")
291 if [ "$have" = 0 ]; then
f4010f7f 292 warn "This setup requires busybox-initrd compiled with applet '$applet' support"
9299682f
ER
293 err=1
294 fi
295 eval busybox_have_$applet=$have
296 fi
297 done
298 if [ $err = 1 ]; then
00eaa938 299 die "Aborted"
9299682f
ER
300 fi
301}
302
755e5cdc
ER
303# Extract the .config file from a kernel image
304# uses extract-ikconfig from kernel sources (scripts/extract-ikconfig)
305ikconfig() {
61d20b49 306 local kofile=$(modinfo -k $kernel -n configs 2> /dev/null)
755e5cdc
ER
307 if [ -n "$kofile" ]; then
308 /lib/geninitrd/extract-ikconfig $kofile
7283bf35 309 return
25cb53df 310 fi
7283bf35
ER
311
312 # see if config available as separate file
313 if [ -f /boot/config-$kernel ]; then
314 cat /boot/config-$kernel
315 return
316 fi
317
318 # finally try vmlinuz itself
319 /lib/geninitrd/extract-ikconfig /boot/vmlinuz-$kernel
25cb53df
ER
320}
321
7ffba534
ER
322# Finds module dependencies
323#
7ffba534
ER
324# @param $module
325#
046c68c9 326# Outputs full path to module and it's dependencies
7ffba534 327find_depmod() {
fe280785 328 local module="$1"
046c68c9
ER
329 local skiperrors=0
330
331 # if module is prefixed with dash, we should ignore errors if the module
332 # can't be found.
333 if [ ${module#-} != $module ]; then
334 skiperrors=1
335 module=${module#-}
336 fi
337
046c68c9
ER
338 # This works when user has module-init-tools installed even on 2.4 kernels
339 local modprobe
340 modprobe=$(modprobe --set-version $kernel --show-depends $module --ignore-install 2>&1)
341
342 if [ $? != 0 ]; then
343 if [ $skiperrors = 1 ]; then
0c7e9123 344 return 0
7cac5014 345 fi
046c68c9 346 echo >&2 "$modprobe"
7cac5014 347
7ffba534 348 if ! is_no "$EXIT_IF_MISSING"; then
046c68c9 349 die "$module: module not found for $kernel kernel"
7ffba534 350 fi
046c68c9
ER
351
352 warn "$module: module not found for $kernel kernel"
353 warn "If $module isn't compiled in kernel then this initrd may not start your system."
7ffba534
ER
354 fi
355
046c68c9 356 echo "$modprobe" | \
c25765ed 357 while read insmod modpath options; do
59a68735 358 [ "$insmod" = "insmod" ] && echo $modpath
7ffba534 359 done
0c7e9123 360 return 0
7ffba534
ER
361}
362
4a60c01f
AM
363find_firmware() {
364 local module="$1"
601127ea
ER
365
366 # no firmware support in 2.4 kernels
367 if [ "$kernel_version_long" -lt "002005048" ]; then
368 return
369 fi
370 echo -n $(NEW_MODINFO=1 modinfo -k $kernel -F firmware $module 2>/dev/null | xargs)
4a60c01f
AM
371}
372
046c68c9
ER
373# @param $module
374find_module() {
375 local mod depmod module=$1
ac085800 376
046c68c9 377 depmod=$(find_depmod $module) || exit 1
fe280785 378 for mod in $depmod; do
7ffba534 379 mod=${mod#/lib/modules/$kernel/}
fe280785
ER
380
381 # add each module only once
382 local m have=0
383 for m in $MODULES; do
384 [ $m = $mod ] && have=1
385 done
386 if [ $have = 0 ]; then
c2eb7d4f 387 MODULES="$MODULES $mod"
fe280785 388 fi
10c3df06 389 done
bb529f94
JK
390}
391
034fdd5d
ER
392# install a file to temporary mount image.
393# it will operate recursively (copying directories)
394# and will symlink destinations if source is symlink.
bb529f94 395inst() {
17e97aec 396 if [ $# -lt 2 ]; then
a9ace64a 397 die 'Usage: inst <file> [<file>] <destination>'
c31050f3 398 fi
17e97aec
ER
399
400 local src i=0 c=$(($# - 1))
401 while [ $i -lt $c ]; do
402 src="$src $1"
403 i=$((i + 1))
404 shift
405 done
406 local dest=$1
407 set -- $src
ce40ba21 408 local parentDir=$(dirname $DESTDIR$dest)
02ba8ab7
ER
409 if [ ! -d "$parentDir" ]; then
410 verbose "+ mkdir -p $parentDir"
411 mkdir -p $parentDir
412 fi
413 verbose "+ cp $* $DESTDIR$dest"
9b557a09 414 cp -HR "$@" "$DESTDIR$dest"
bb529f94
JK
415}
416
dab92b1d 417inst_d() {
17e97aec 418 if [ $# = 0 ]; then
a9ace64a 419 die 'Usage: inst_d <destination> <destination>'
034fdd5d
ER
420 fi
421 for dir in "$@"; do
9b557a09 422 install -d "$DESTDIR$dir"
034fdd5d
ER
423 done
424}
425
209061e3
ER
426# install executable and it's shared libraries
427inst_exec() {
17e97aec 428 if [ $# -lt 2 ]; then
3601c2fa 429 die "Invalid params ($@), Usage: inst_exec <file>[, <file>] <destination>"
17e97aec 430 fi
209061e3
ER
431 local src i=0 c=$(($# - 1))
432 while [ $i -lt $c ]; do
433 src="$src $1"
434 i=$((i + 1))
435 shift
436 done
9b532fe6 437 local dest=$1
209061e3
ER
438 set -- $src
439
9b532fe6
ER
440 inst "$@" $dest
441
10e8125f
AF
442 local obj lib libs
443 for obj in "$@"; do
66aec48b
ER
444 case "$obj" in
445 /lib/ld-linux.so.2 | /lib64/ld-linux-x86-64.so.2)
446 continue
447 esac
448
449 libs=$(ldd "$obj" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
450 for lib in $libs; do
451 if [ ! -f "$DESTDIR/$_lib/${lib##*/}" ]; then
452 inst_d /$_lib
453 inst_exec $lib /$_lib
454 fi
455 done
209061e3 456 done
c7ade647
ER
457
458 # hack for uclibc linked binaries requiring this fixed path
459 # XXX: shouldn't rpath be used here instead so th
460 if [ -f $DESTDIR/$_lib/libc.so.0 ]; then
461 local lib=$DESTDIR/$_lib/libc.so.0
462 lib=$(ldd "$lib" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
463 local libdir=$(cd $(dirname "$lib"); pwd)
464 if [ ! -e $DESTDIR$libdir ]; then
465 libdir=$(dirname "$libdir")
466 inst_d $libdir
02ba8ab7 467 verbose "+ ln -s /$_lib $DESTDIR$libdir"
c7ade647
ER
468 ln -s /$_lib $DESTDIR$libdir
469 fi
470 fi
209061e3
ER
471}
472
82474db9
ER
473# output modules.conf / modprobe.conf
474modprobe_conf() {
475 echo "$modprobe_conf_cache"
476}
477
38ac83a5
ER
478# return options for MODULE
479# @param $1 module name
480modprobe_options() {
481 local module=$1
915ff812 482 local options=$(modprobe_conf | awk -vmodule="$module" '{ if ($1 == "options" && $2 == module) { for(i=3;i<=NF;i++) printf("%s ",$i); }}')
38ac83a5
ER
483 echo ${options# }
484}
485
82474db9
ER
486#
487# defaults to modprobe -c if not told otherwise, this means include statements
488# work from there.
489cache_modprobe_conf() {
707f5e60 490 if [ "$kernel_version" -lt "002005" ]; then
82474db9
ER
491 modulefile=/etc/modules.conf
492 if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
493 modulefile=/etc/conf.modules
494 fi
495 fi
496
497 if [ -n "$modulefile" ]; then
d8056591 498 debug "Using $modulefile for modules config"
99e7251e
ER
499 modprobe_conf_cache=$(cat $modulefile | awk '!/^[\t ]*#/ { print }')
500
82474db9 501 else
d8056591 502 debug "Using modprobe -c to get modules config"
99e7251e 503 modprobe_conf_cache=$(modprobe -c --set-version $kernel | awk '!/^[\t ]*#/ { print }')
82474db9
ER
504 fi
505}
506
bc0d6f2d 507# find modules for $devpath
d8056591 508find_modules_for_devpath() {
bc0d6f2d
ER
509 local devpath="$1"
510 if [ -z "$devpath" ]; then
d8056591 511 die "No argument passed to find_modules_for_devpath() - is your /etc/fstab correct?"
f6536797 512 fi
ac085800 513
c2eb7d4f
ER
514 if [[ "$devpath" = /dev/dm-* ]]; then
515 # /dev/dm-3 -> /dev/mapper/sil_ahbgadcbchfc3
d2cb46cf 516 devpath=$(dm_node "$devpath")
c2eb7d4f
ER
517 fi
518
336018a5 519 if [ -L "$devpath" ] && ! is_lvm "$devpath" && ! is_luks "$devpath"; then
c2eb7d4f
ER
520 # sanitize things like:
521 # /dev/block/104:2 -> /dev/cciss/c0d0p2
522 devpath=$(readlink -f "$devpath")
523 fi
524
02ba8ab7 525 verbose "Finding modules for device path $devpath"
bc0d6f2d 526
3ef3d717
ER
527 if is_luks "$devpath"; then
528 find_modules_luks "$devpath"
529 return
530 fi
531
74d45ce1
ER
532 if is_nfs "$devpath"; then
533 find_modules_nfs "$devpath"
d9179777
ER
534 return
535 fi
536
e16414d9 537 if is_md "$devpath"; then
df738638 538 find_modules_md "$devpath"
d9179777
ER
539 return
540 fi
541
c3b54060 542 if is_multipath "$devpath"; then
07137fe3 543 if find_modules_multipath "$devpath"; then
9baf4f3f 544 return
9baf4f3f 545 fi
ecd7bf46 546
9baf4f3f
ER
547 # fallback
548 fi
549
36523626 550 if is_dmraid "$devpath"; then
c083ae23
ER
551 if find_modules_dmraid "$devpath"; then
552 return
553 fi
554 # fallback
555 fi
556
35043b20
ER
557 if is_scsi "$devpath"; then
558 find_modules_scsi "$devpath"
d9179777
ER
559 return
560 fi
561
30ca4815 562 if is_ide "$devpath"; then
bc0d6f2d 563 find_modules_ide "$devpath"
d9179777
ER
564 return
565 fi
566
07b09cf9 567 if [[ "$devpath" == /dev/rd/* ]]; then
046c68c9 568 find_module "DAC960"
c3313cd6 569 rootdev_add=/dev/rd/
d9179777
ER
570 return
571 fi
572
07b09cf9 573 if [[ "$devpath" == /dev/ida/* ]]; then
046c68c9 574 find_module "cpqarray"
c3313cd6 575 rootdev_add=/dev/ida/
d9179777
ER
576 return
577 fi
578
08651ad1 579 if [[ "$devpath" == /dev/cciss/* ]]; then
c3313cd6 580 rootdev_add=/dev/cciss/
df11fbe8
ER
581
582 # load hpsa for future kernels, cciss for backwards compat
583 if [ "$kernel_version_long" -ge "003000000" ]; then
584 find_module "hpsa" "-cciss"
649df3de 585 find_modules_scsi "$devpath"
df11fbe8
ER
586 else
587 find_module "cciss"
588 fi
589
d9179777
ER
590 return
591 fi
592
07b09cf9 593 if [[ "$devpath" == /dev/ataraid/* ]]; then
ac085800 594 find_modules_ide
046c68c9 595 find_module "ataraid"
99e7251e 596 ataraidmodules=$(modprobe_conf | awk '/ataraid_hostadapter/ { print $3 }')
9ae446b9 597 if [ -n "$ataraidmodules" ]; then
7c38b114 598 # FIXME: think about modules compiled in kernel
82474db9 599 die "ataraid_hostadapter alias not defined in modprobe.conf! Please set it and run $PROGRAM again."
7c38b114
AF
600 fi
601 for n in $ataraidmodules; do
046c68c9 602 find_module "$n"
7c38b114 603 done
c3313cd6 604 rootdev_add=/dev/ataraid/
d9179777
ER
605 return
606 fi
607
7c38b114 608 # check to see if we need to set up a loopback filesystem
07b09cf9 609 if [[ "$devpath" == /dev/loop* ]]; then
00eaa938 610 die "Sorry, root on loop device isn't supported."
7c38b114
AF
611 # TODO: rewrite for bsp and make nfs ready
612 if [ ! -x /sbin/losetup ]; then
00eaa938 613 die "losetup is missing"
7c38b114 614 fi
bc0d6f2d 615 key="^# $(echo $devpath | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
82474db9 616 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`"; then
42820142 617 die "The root filesystem is on a $devpath, but there is no magic entry in $fstab for this device. Consult the $PROGRAM man page for more information"
7c38b114
AF
618 fi
619
620 line="`awk '/'$key'/ { print $0; }' $fstab`"
621 loopDev="$(echo $line | awk '{print $3}')"
622 loopFs="$(echo $line | awk '{print $4}')"
623 loopFile="$(echo $line | awk '{print $5}')"
624
625 BASICMODULES="$BASICMODULES -loop"
046c68c9 626 find_module "-$loopFs"
7c38b114 627 BASICMODULES="$BASICMODULES -${loopFs}"
d9179777
ER
628 return
629 fi
630
67aa84bd
ER
631 if is_lvm "$devpath"; then
632 find_modules_lvm "$devpath"
d9179777 633 return
7c38b114
AF
634 fi
635}
636
b64f015b 637firmware_install_module() {
c3667d07 638 local module="$1"
c6c6ce01 639 local firmware_files="$2"
9ed6e1db 640
02ba8ab7 641 verbose "Adding Firmwares ($firmware_files) to initrd for module $module"
9ed6e1db 642 # firmware not yet installed
9b557a09 643 if [ ! -f "$DESTDIR/lib/firmware/firmware.sh" ]; then
034fdd5d 644 inst_d /lib/firmware
9b557a09 645cat << 'EOF' >> "$DESTDIR/lib/firmware/firmware.sh"
9ed6e1db 646#!/bin/sh -e
8639f99a
AM
647echo 1 > /sys$DEVPATH/loading
648cat "/lib/firmware/$FIRMWARE" > /sys$DEVPATH/data
649echo 0 > /sys$DEVPATH/loading
650exit 0
9ed6e1db 651EOF
9b557a09 652 chmod 755 "$DESTDIR/lib/firmware/firmware.sh"
63cb815e
ER
653
654 # setup firmware loader agent
655 echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" | add_linuxrc
9ed6e1db
AM
656 fi
657
658 for firmware in $firmware_files; do
80b23733 659 if [ -f "/lib/firmware/$kernel/$firmware" ]; then
486d26a5 660 FIRMWAREDIR=${firmware%/*}
4a9920e7 661 [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR
80b23733
ER
662 inst /lib/firmware/$kernel/$firmware /lib/firmware/$firmware
663 elif [ -f "/lib/firmware/$firmware" ]; then
c661b23c 664 FIRMWAREDIR=${firmware%/*}
665 [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR
80b23733 666 inst /lib/firmware/$firmware /lib/firmware/$firmware
c661b23c 667 else
6652b089 668 warn "Possible missing firmware file /lib/firmware/$firmware or /lib/firmware/$kernel/$firmware for module $module."
4a60c01f 669 fi
9ed6e1db
AM
670 done
671
b64f015b 672 mount_sys
9ed6e1db
AM
673}
674
2a5bcca9 675modules_install() {
c3667d07
ER
676 local modules="$1"
677 local mod
2a5bcca9
AM
678
679 for mod in $modules; do
fe280785 680 MODULEDIR=${mod%/*}
4a9920e7 681 inst_d "/lib/modules/$kernel/$MODULEDIR"
9b557a09
ER
682 cp -a "/lib/modules/$kernel/$mod" "$DESTDIR/lib/modules/$kernel/$mod"
683 gunzip "$DESTDIR/lib/modules/$kernel/$mod" 2> /dev/null
897d14b7 684 if [ "$STRIP" ] && [ -x "$STRIP" ]; then
a6974a17 685 $STRIP -g --remove-section=.comment "$DESTDIR/lib/modules/$kernel/${mod%.gz}"
ccd1f65f 686 fi
2a5bcca9
AM
687 done
688}
689
690modules_add_linuxrc() {
fe280785 691 local mod modpath
ac085800 692
fe280785
ER
693 for mod in "$@"; do
694 # module path without optional compression
695 modpath=${mod%.gz}
696
697 # name of the module
8011a76c 698 local module=${modpath##*/}; module=${module%$modext}
38ac83a5 699 local options=$(modprobe_options "$module")
8011a76c
ER
700 local genericname=$(echo $module | tr - _)
701 local usleep=$(eval echo \$MODULE_${genericname}_USLEEP)
702 local firmware=$(eval echo \$MODULE_${genericname}_FIRMWARE)
2a5bcca9 703
3e1d0df9
AM
704 if [ "$module" = "scsi_mod" -a "$kernel_version_long" -ge "002006030" ]; then
705 options="scan=sync $options"
706 fi
707
02ba8ab7 708 if [ x"$verbose" = x"-v" ]; then
8a47b72c
ER
709 s=""
710 if [ "$options" ]; then
711 s="$s with options [$options]"
712 fi
8011a76c
ER
713 if [ "$usleep" ]; then
714 s="$s and $usleep usleep"
8a47b72c 715 fi
02ba8ab7 716 verbose "Loading module [$module]$s"
ac085800
ER
717 fi
718
8011a76c
ER
719 if [ -n "$firmware" ]; then
720 firmware_install_module "$module" "$firmware"
4a60c01f 721 else
06e481d7
ER
722 for file in $(find_firmware "$module"); do
723 firmware_install_module "$module" "$file"
724 done
9ed6e1db 725 fi
4a60c01f 726
18ece493 727 echo "insmod /lib/modules/$kernel/$modpath $options" | add_linuxrc
8011a76c
ER
728 if [ -n "$usleep" ]; then
729 echo "usleep $usleep" | add_linuxrc
8e598759 730 fi
e00dcfcb 731 if [ "$module" = "scsi_wait_scan" ]; then
d29583e0
ER
732 if [ "$(busybox_applet rmmod 2>/dev/null; echo $?)" = 0 ]; then
733 echo "rmmod scsi_wait_scan" | add_linuxrc
734 fi
e00dcfcb
AM
735 fi
736
2a5bcca9
AM
737 done
738}
739
82b2dba2
ER
740# Generates /dev nodes based on /proc/partitions information.
741# Needs /proc mounted.
742# Can be called multiple times.
743initrd_gen_devices() {
744 if is_yes "$proc_partitions"; then
745 return
746 fi
747 proc_partitions=yes
335cd101 748
82b2dba2
ER
749 mount_dev
750 add_linuxrc <<-'EOF'
751 : 'Making device nodes'
752 cat /proc/partitions | (
8d4aba01 753 # ignore first two lines: header, empty line
82b2dba2 754 read b; read b
2cc3ae8b 755
82b2dba2
ER
756 while read major minor blocks dev rest; do
757 node=/dev/$dev
758 mkdir -p ${node%/*}
8d4aba01 759 [ -e $node ] || mknod $node b $major $minor
82b2dba2
ER
760 done
761 )
762 EOF
763}
bb529f94 764
82b2dba2 765
4828c787 766initrd_gen_setrootdev() {
02ba8ab7 767 verbose "Adding rootfs finding based on kernel cmdline root= option support."
71b2d771 768 busybox_applet ls
2990ac33 769 debug "Current /proc/partitions:\n$(sed -e 's,^,> ,' /proc/partitions)"
82b2dba2 770 add_linuxrc <<-'EOF'
dcdf6b49
ER
771 if [ "${ROOT##/dev/}" != "${ROOT}" ]; then
772 rootnr="$(busybox awk -v rootnode="${ROOT##/dev/}" '$4 == rootnode { print 256 * $1 + $2 }' /proc/partitions)"
71b2d771
AM
773 # fallback to ls
774 if [ -z "$rootnr" ]; then
5efa9a2e 775 rootnr="$(busybox ls -lL ${ROOT} | busybox awk '{if (/^b/) { print 256 * $3 + $4; }}')"
71b2d771 776 fi
82b2dba2
ER
777 if [ -n "$rootnr" ]; then
778 echo "$rootnr" > /proc/sys/kernel/real-root-dev
779 fi
780 fi
b64f015b 781 EOF
1606e343
AM
782}
783
4671d086
ER
784initrd_gen_initramfs_switchroot() {
785 inst_d /newroot
786 if [ "$rootdev" = "/dev/nfs" ]; then
787 echo "rootfs on NFS root=/dev/nfs"
788 else
789 [ ! -e "$DESTDIR/$rootdev" ] && inst $rootdev $rootdev
790 fi
5845b321
ER
791
792 # parse 'root=xxx' kernel commandline
4671d086 793 # We support passing root as hda3 /dev/hda3 0303 0x0303 and 303
ec21900e
ER
794
795 # from lilo-23.2/readme/README:
796 # root=<device> changes the root device. This overrides settings that may
797 # have been made in the boot image and on the LILO command line. <device> is
798 # either the hexadecimal device number or the full path name of the device,
799 # e.g. /dev/hda3 [*]
800 #
801 # * The device names are hard-coded in the kernel. Therefore, only the
802 # "standard" names are supported and some less common devices may not be
803 # recognized. In those cases, only numbers can be used.
4671d086 804 add_linuxrc <<-'EOF'
7f187a08 805 device=
3564e6b6 806 eval "$(busybox awk -v root="$ROOT" '
f9194b15
ER
807 # http://9fans.net/archive/2006/09/261
808 function h2d(str, hstr, res, num, n, digit, i) {
809 hstr = "0123456789abdcef";
810 res = 0;
811 n = split(tolower(str), digit, "");
812
813 for (i = 1; i <= n; i++) {
814 num = index(hstr, digit[i]) - 1;
815 res = res + (num * 16 ^ (n - i));
816 }
817
818 return res;
819 }
4671d086 820 BEGIN {
f9194b15
ER
821
822 num_pattern_short = "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]";
823 num_pattern = "[0-9a-fA-F]" num_pattern_short;
4671d086 824 dev_pattern = "[hms][a-z][a-z]([0-9])+";
7f187a08 825 partition = "";
4671d086
ER
826 min = -1; maj = -1;
827
3564e6b6
ER
828 # see if we have /dev/hdX or hdX, we can just take partition name
829 if (root ~ "^\/dev\/" dev_pattern "$" || root ~ "^" dev_pattern "$") {
830 partition = root
831 sub("^/dev/", "", partition);
832
833 } else {
834 # unify values first
835 if (root ~ "^" num_pattern_short "$") {
836 # change "303" => "0x0303"
837 root = "0x0" root
838 } else if (root ~ "^" num_pattern "$") {
839 # change "0303" => "0x0303"
840 root = "0x" root
841 }
842
f9194b15
ER
843 maj = h2d(substr(root, 3, 2));
844 min = h2d(substr(root, 5, 2));
4671d086 845 }
4671d086
ER
846 }
847
7f187a08 848 partition && $4 == partition { maj = $1; min = $2; }
afa32d0e 849 $1 == maj && $2 == min { partition = $4; }
4671d086
ER
850
851 END {
fa3a5452 852 if (maj >= 0 && min >= 0) {
7f187a08
ER
853 printf("maj=%s; min=%s;\n", maj, min);
854 }
855 if (partition) {
856 printf("device=/dev/%s;\n", partition);
fa3a5452 857 }
4671d086
ER
858 }
859 ' /proc/partitions)"
4671d086 860
7f187a08 861 if [ -z "$device" ]; then
0fb9dda3 862 device=$ROOT
4671d086
ER
863 fi
864
7f187a08
ER
865 if [ "$device" -a ! -b $device ]; then
866 mknod $device b $maj $min
867 fi
868
aebd3ecb 869 [ -n "$ROOTFSFLAGS" ] && ROOTFSFLAGS="-o $ROOTFSFLAGS"
41d3645c 870
0fb9dda3
ER
871 mount -t $ROOTFS -r $device $ROOTFSFLAGS /newroot || echo "Mount of rootfs failed."
872 init=$INIT
4671d086
ER
873 if [ -z "$init" -o ! -x "/newroot$init" ]; then
874 init=/sbin/init
875 fi
876 EOF
877
878 umount_all
879 busybox_applet switch_root
880 add_linuxrc <<-'EOF'
278e5013 881 [ ! -e /newroot/dev/console ] && mknod -m 660 /newroot/dev/console c 5 1
f8ea6a63 882 exec switch_root /newroot $init ${1:+"$@"}
4671d086
ER
883
884 echo "Error! initramfs should not reach this place."
885 echo "It probably means you've got old version of busybox, with broken"
886 echo "initramfs support. Trying to boot anyway, but won't promise anything."
887
f8ea6a63 888 exec chroot /newroot $init ${1:+"$@"}
4671d086
ER
889
890 echo "Failed to chroot!"
b61aa273 891 debugshell
4671d086
ER
892 EOF
893 # we need /init being real file, not symlink, otherwise the initramfs will
894 # not be ran by pid 1 which is required for switch_root
895 mv $DESTDIR/linuxrc $DESTDIR/init
896 ln -s init $DESTDIR/linuxrc
897}
898
c552503c
ER
899# find if $symbol exists in System.map $mapfile
900sym_exists() {
901 local mapfile="$1"
902 local symbol="$2"
903 if [ ! -f $mapfile ]; then
904 # missing mapfile (not a pld kernel?)
905 return 1
906 fi
907
908 awk -vc=1 -vsymbol="$symbol" '$2 == "T" && $3 == symbol {c = 0} END {exit c}' $mapfile
909}
910
911# find best compressor (or forced one) for initrd
912find_compressor() {
913 local mode="$1"
1b89c904
AM
914 # fastest initrd decompression speed is first
915 local compressors='lzo gzip xz lzma bzip2'
c552503c
ER
916
917 # a specified one, take it
55aaf2fe
AM
918 if ! is_yes "$mode"; then
919 compressors="$mode"
c552503c
ER
920 fi
921
02ba8ab7 922 verbose "finding compressor: $compressors (via $mode)"
c552503c
ER
923 # check for compressor validity
924 local c prog map=/boot/System.map-$kernel
925 for c in $compressors; do
926 case $c in
66d146a5 927 xz)
29f7d622
AM
928 sym=unxz
929 prog=/usr/bin/xz
930 ;;
931 lzma)
c552503c 932 sym=unlzma
66d146a5 933 prog=/usr/bin/xz
c552503c
ER
934 ;;
935 bzip2)
936 sym=bzip2
c42692d4 937 prog=/usr/bin/bzip2
c552503c
ER
938 ;;
939 gzip)
940 sym=gunzip
c42692d4 941 prog=/bin/gzip
c552503c 942 ;;
8a6f083b
AM
943 lzo)
944 sym=unlzo
945 prog=/usr/bin/lzop
946 ;;
5fcc3d04
AM
947 none|no)
948 # any existing sym will work
949 sym=initrd_load
950 prog=/bin/cat
951 ;;
c552503c
ER
952 *)
953 die "Unknown compressor $c"
954 ;;
955 esac
c454c66e 956 if sym_exists $map $sym && [ -x $prog ]; then
c552503c
ER
957 echo $c
958 return
959 fi
960 done
961
02ba8ab7 962 verbose "using gzip for compressor (fallback)"
c552503c
ER
963 echo gzip
964}
965
97f51b6e
ER
966# compresses kernel image image
967# in function so we could retry with other compressor on failure
968compress_image() {
969 local compressor="$1" IMAGE="$2" target="$3" tmp
970 tmp=$(mktemp "$target".XXXXXX) || die "mktemp failed"
971
972 case "$compressor" in
973 xz)
974 # don't use -9 here since kernel won't understand it
975 xz --format=xz --check=crc32 --lzma2=preset=6e,dict=1MiB < "$IMAGE" > "$tmp" || return $?
976 ;;
977 lzma)
978 xz --format=lzma -9 < "$IMAGE" > "$tmp" || return $?
979 ;;
980 bzip2)
981 bzip2 -9 < "$IMAGE" > "$tmp" || return $?
982 ;;
983 gzip)
984 gzip -9 < "$IMAGE" > "$tmp" || return $?
985 ;;
8a6f083b
AM
986 lzo)
987 lzop -9 < "$IMAGE" > "$tmp" || return $?
988 ;;
5fcc3d04
AM
989 none|no)
990 cat < "$IMAGE" > "$tmp" || return $?
991 ;;
97f51b6e
ER
992 esac
993
994 mv -f "$tmp" "$target"
995}
996
82b2dba2
ER
997if [ -r /etc/sysconfig/geninitrd ]; then
998 . /etc/sysconfig/geninitrd
999fi
2cc3ae8b 1000
c215292c
AG
1001if [ ! -f /proc/mounts ]; then
1002 warn "/proc filesystem not mounted, may cause wrong results or failure."
1003fi
1004
cefcd7bb 1005geninitrd_load_mods ide luks multipath dmraid lvm md blkid udev tuxonice suspend fbsplash condecor bootsplash uvesafb nfs sata scsi usbkbd
c124d0cf 1006
82b2dba2
ER
1007while [ $# -gt 0 ]; do
1008 case $1 in
1009 --fstab=*)
1010 fstab=${1#--fstab=}
1011 ;;
1012 --fstab)
1013 fstab=$2
1014 shift
1015 ;;
1016 --modules-conf=*)
1017 modulefile=${1#--modules-conf=}
1018 ;;
1019 --modules-conf)
1020 modulefile=$2
1021 shift
1022 ;;
82b2dba2
ER
1023 --with-bootsplash)
1024 BOOT_SPLASH=yes
1025 ;;
1026 --without-bootsplash)
1027 BOOT_SPLASH=no
1028 ;;
1029 --with-fbsplash)
1030 FB_SPLASH=yes
1031 ;;
1032 --without-fbsplash)
1033 FB_SPLASH=no
1034 ;;
6c69a2d4
ER
1035 --with-fbcondecor)
1036 FB_CON_DECOR=yes
1037 ;;
1038 --without-fbcondecor)
1039 FB_CON_DECOR=no
1040 ;;
82b2dba2
ER
1041 --with-suspend)
1042 USE_SUSPEND=yes
1043 ;;
1044 --without-suspend)
1045 USE_SUSPEND=no
1046 ;;
1047 --with-suspend2 | --with-tuxonice)
1048 USE_TUXONICE=yes
1049 ;;
1050 --without-suspend2 | --without-tuxonice)
1051 USE_TUXONICE=no
1052 ;;
80b1ed79 1053 --lvmversion=*)
7308edee 1054 LVMTOOLSVERSION=${1#--lvmversion=}
82b2dba2 1055 ;;
80b1ed79 1056 --lvmtoolsversion=*)
1057 LVMTOOLSVERSION=${1#--lvmtoolsversion=}
1058 ;;
82b2dba2
ER
1059 --lvmtoolsversion|--lvmversion)
1060 LVMTOOLSVERSION=$2
1061 shift
1062 ;;
1063 --without-udev)
1064 USE_UDEV=no
1065 ;;
1066 --with-udev)
1067 USE_UDEV=yes
1068 ;;
1069 --without-dmraid)
1070 USE_DMRAID=no
1071 ;;
1072 --without-multipath)
ecd7bf46
ER
1073 USE_MULTIPATH=no
1074 ;;
1075 --with-multipath=*)
1076 USE_MULTIPATH=${1#--with-multipath=}
82b2dba2 1077 ;;
af075488 1078 --without-blkid)
1079 USE_BLKID=no
289fbc9b 1080 ;;
3ef3d717
ER
1081 --without-luks)
1082 USE_LUKS=no
1083 ;;
82b2dba2
ER
1084 --with=*)
1085 BASICMODULES="$BASICMODULES ${1#--with=}"
1086 ;;
1087 --with)
1088 BASICMODULES="$BASICMODULES $2"
1089 shift
1090 ;;
1091 --version)
1092 echo "$PROGRAM: version $VERSION"
1093 exit 0
1094 ;;
1095 -v)
02ba8ab7
ER
1096 if [ x"$verbose" = x"-v" ]; then
1097 verbose="-v -v"
1098 else
1099 verbose="-v"
1100 fi
11ab0dea 1101 exec 3>&1
82b2dba2 1102 ;;
c552503c
ER
1103 --compress)
1104 COMPRESS=$2
1105 ;;
1106 --compress=*)
1107 COMPRESS="${1#--compress=}"
1108 ;;
82b2dba2
ER
1109 --nocompress)
1110 COMPRESS=no
1111 ;;
ccd1f65f 1112 --nostrip)
76a48507
ER
1113 STRIP=
1114 ;;
1115 --strip=*)
1116 STRIP="${1#--strip=}"
1117 ;;
1118 --strip)
1119 STRIP=$2
1120 shift
ccd1f65f 1121 ;;
82b2dba2
ER
1122 --ifneeded)
1123 ifneeded=1
1124 ;;
1125 -f)
1126 force=1
1127 ;;
1128 --preload=*)
1129 PREMODS="$PREMODS ${1#--preload=}"
1130 ;;
1131 --preload)
1132 PREMODS="$PREMODS $2"
1133 shift
1134 ;;
2965cab9 1135 --fs=* | --fs)
bbbc32d0 1136 die "--fs option is obsoleted. Use --initrdfs instead"
82b2dba2
ER
1137 ;;
1138 --initrdfs=*)
1139 INITRDFS=${1#--initrdfs=}
1140 ;;
1141 --initrdfs)
1142 INITRDFS=$2
1143 shift
1144 ;;
1145 --image-version)
1146 img_vers=yes
1147 ;;
1148 --ide-only-root)
1149 ide_only_root="yes"
1150 ;;
1151 *)
1152 if [ -z "$target" ]; then
1153 target="$1"
1154 elif [ -z "$kernel" ]; then
1155 kernel="$1"
1156 else
7f6e5359 1157 usage
36523626 1158 exit 1
82b2dba2
ER
1159 fi
1160 ;;
1161 esac
f5db170b 1162
82b2dba2
ER
1163 shift
1164done
1165
1166if [ -z "$target" -o -z "$kernel" ]; then
7f6e5359 1167 usage
36523626 1168 exit 1
82b2dba2
ER
1169fi
1170
93a38d1a
ER
1171# main()
1172if [ "$(id -u)" != 0 ]; then
1173 die "You need to be root to generate initrd"
1174fi
1175
a48c38c7 1176if [ -d /lib64 -a -d /usr/lib64 ]; then
c124d0cf
ER
1177 _lib=lib64
1178else
1179 _lib=lib
130aadc1
ER
1180fi
1181
c124d0cf 1182initrd_dir=/usr/$_lib/initrd
e4487862
AM
1183kernel_version=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d",$1,$2)}')
1184kernel_version_long=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d%03d",$1,$2,$3)}')
82b2dba2 1185
02ba8ab7 1186verbose "# $GENINITRD_RCSID (geninitrd)"
c124d0cf
ER
1187debug "Using _lib: $_lib"
1188debug "Using initrd_dir: $initrd_dir"
1189
6ec4aea6 1190busybox=$(find_tool $initrd_dir/busybox $initrd_dir/initrd-busybox /bin/initrd-busybox) || die "Couldn't find busybox suitable for initrd"
c124d0cf
ER
1191
1192# we setup mods after parsing command line args
1193geninitrd_setup_mods
1194
1195if [ ! -f /boot/vmlinuz-"$kernel" ]; then
1196 warn "/boot/vmlinuz-$kernel doesn't exist, is your /boot mounted?"
1197fi
1198
82b2dba2 1199if [ -z "$INITRDFS" ]; then
bbbc32d0
ER
1200 if [ -n "$FS" ]; then
1201 # FS= can came only via /etc/sysconfig/geninitrd likely?
1202 die "FS configuration option is obsoleted. Use INITRDFS instead"
f5db170b 1203 fi
f5db170b 1204
bbbc32d0
ER
1205 # default value
1206 if [ "$kernel_version" -ge "002005" ]; then
1207 INITRDFS="initramfs"
1208 else
1209 INITRDFS="rom"
1210 fi
82b2dba2 1211fi
966c32cc 1212
82b2dba2 1213case "$INITRDFS" in
4877276e
ER
1214 ext2)
1215 [ -x /sbin/mke2fs ] || die "/sbin/mke2fs is missing"
1216 ;;
1217 rom|romfs)
1218 [ -x /sbin/genromfs ] || die "/sbin/genromfs is missing"
1219 ;;
1220 cram|cramfs)
1221 [ -x /sbin/mkcramfs ] || die "/sbin/mkcramfs is missing"
1222 ;;
1223 initramfs)
1224 [ -x /bin/cpio ] || die "/bin/cpio is missing"
1225 [ -x /usr/bin/find ] || die "/usr/bin/find is missing"
1226 ;;
1227 *)
1228 die "Filesystem $INITRDFS on initrd is not supported"
1229 ;;
82b2dba2 1230esac
f5db170b 1231
9323ada3 1232if [ -L "$target" ]; then
1233 target=$(readlink -f "$target")
1234fi
1235
82b2dba2
ER
1236if [ -n "$img_vers" ]; then
1237 target="$target-$kernel"
1238fi
8bd582f1 1239
82b2dba2
ER
1240if [ -z "$force" -a -f "$target" ]; then
1241 die "$target already exists."
1242fi
c6c6ce01 1243
82b2dba2
ER
1244if [ ! -d "/lib/modules/$kernel" ]; then
1245 die "/lib/modules/$kernel is not a directory."
1246fi
2ffb1734 1247
bbbc32d0
ER
1248if [ "$kernel_version" -ge "002005" ]; then
1249 modext=".ko"
1250fi
1251
82b2dba2
ER
1252cache_modprobe_conf
1253
1254for n in $PREMODS; do
046c68c9 1255 find_module "$n"
82b2dba2
ER
1256done
1257
ed30e3a8 1258if [ "$FBMODULE" ]; then
046c68c9 1259 find_module "$FBMODULE"
ed30e3a8
ER
1260fi
1261
cefcd7bb
AM
1262# autodetect USB keyboards
1263find_modules_usbkbd
1264
82b2dba2 1265# allow forcing loading SCSI and/or IDE modules
35043b20 1266# XXX: where ADDSCSI cames from? drop?
82b2dba2
ER
1267if is_yes "$ADDSCSI"; then
1268 find_modules_scsi
1269fi
1270
3f53322c 1271# autodetect SATA modules
1272find_modules_sata
e2d6ca6c 1273
35043b20 1274# XXX: where ADDIDE cames from? drop?
82b2dba2
ER
1275if is_yes "$ADDIDE"; then
1276 find_modules_ide
1277fi
1278
4c16dac4
ER
1279if is_yes "$USE_SUSPEND"; then
1280 find_modules_suspend
1281fi
1282
82b2dba2 1283find_root "$fstab" || exit
02ba8ab7 1284verbose "Using $rootdev as device for rootfs"
82b2dba2 1285
d8056591 1286find_modules_for_devpath "$rootdev"
ac085800 1287
ecd7bf46
ER
1288# if USE_MULTIPATH is path to device, scan that too
1289# this is to bootstrap multipath setup into initrd.
1290if ! is_no "$USE_MULTIPATH" && ! is_yes "$USE_MULTIPATH"; then
1291 find_modules_multipath $USE_MULTIPATH
1292fi
1293
046c68c9 1294find_module "-$rootFs"
ac085800 1295
82b2dba2 1296for n in $BASICMODULES; do
046c68c9 1297 find_module "$n"
82b2dba2 1298done
4e9eb79c 1299
82b2dba2 1300if is_yes "$USE_TUXONICE"; then
046c68c9 1301 find_module "-lzf"
82b2dba2 1302fi
33d24e12 1303
bf6d9c64 1304find_modules_fbsplash
2968c9dd 1305
82b2dba2 1306if [ -n "$ifneeded" -a -z "$MODULES" ]; then
02ba8ab7 1307 verbose "No modules are needed -- not building initrd image."
82b2dba2
ER
1308 exit 0
1309fi
c6c6ce01 1310
02ba8ab7 1311verbose "Building initrd..."
d8056591 1312DESTDIR=$(mktemp -d -t initrd.XXXXXX) || die "mktemp failed"
9b557a09 1313RCFILE="$DESTDIR/linuxrc"
82b2dba2
ER
1314> "$RCFILE"
1315chmod a+rx "$RCFILE"
9b557a09 1316ln -s linuxrc $DESTDIR/init
e8d178ff 1317
82b2dba2 1318# create dirs that we really need
d63131e0 1319inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,run,sys}
e8d178ff 1320
82b2dba2 1321modules_install "$MODULES"
b64f015b 1322
82b2dba2
ER
1323# mknod'ing the devices instead of copying them works both with and
1324# without devfs...
9b557a09
ER
1325mknod "$DESTDIR/dev/console" c 5 1
1326mknod "$DESTDIR/dev/null" c 1 3
1327mknod "$DESTDIR/dev/zero" c 1 5
8aa2b6bd 1328mknod "$DESTDIR/dev/random" c 1 8
94dce45c 1329mknod "$DESTDIR/dev/urandom" c 1 9
be2f3ecc 1330
6ec4aea6
JR
1331inst_exec $busybox /bin/busybox
1332ln -s busybox $DESTDIR/bin/sh
1333# for older busyboxes who had /bin/initrd-busybox as EXEPATH
1334ln -s busybox $DESTDIR/bin/initrd-busybox
2ffb1734 1335
82b2dba2
ER
1336add_linuxrc <<EOF
1337#!/bin/sh
1338# initrd generated by:
96fbf9e6 1339# $GENINITRD_RCSID
c6c6ce01 1340
82b2dba2
ER
1341EOF
1342mount_proc
0fb9dda3
ER
1343add_linuxrc <<-EOF
1344 # builtin defaults from geninitrd
1345 ROOT=$rootdev
1346 ROOTFS=$rootFs
1347EOF
82b2dba2 1348add_linuxrc <<-'EOF'
48d98595 1349 read CMDLINE < /proc/cmdline
c6c6ce01 1350
82b2dba2
ER
1351 for arg in $CMDLINE; do
1352 if [ "${arg}" = "debuginitrd" ]; then
1353 DEBUGINITRD=yes
1354 fi
1355 if [ "${arg##debuginitrd=}" != "${arg}" ]; then
1356 DEBUGINITRD=${arg##debuginitrd=}
1357 fi
fc787d45 1358 if [ "${arg##root=}" != "${arg}" ]; then
1359 ROOT=${arg##root=}
1360 fi
41d3645c
AM
1361 if [ "${arg##rootfsflags=}" != "${arg}" ]; then
1362 ROOTFSFLAGS=${arg##rootfsflags=}
1363 fi
0fb9dda3
ER
1364 if [ "${arg##init=}" != "${arg}" ]; then
1365 INIT=${arg##init=}
1366 fi
82b2dba2 1367 done
c6c6ce01 1368
82b2dba2
ER
1369 # make debugshell() invoke subshell if $DEBUGINITRD=sh
1370 if [ "$DEBUGINITRD" = "sh" ]; then
1371 debugshell() {
e2405b29
AM
1372EOF
1373if is_yes "$RUN_SULOGIN_ON_ERR"; then
1374add_linuxrc <<-'EOF'
0fb9dda3 1375 echo "debug shell disabled by /etc/sysconfig/system: RUN_SULOGIN_ON_ERR setting"
e2405b29
AM
1376EOF
1377else
1378add_linuxrc <<-'EOF'
1379 sh
1380EOF
1381fi
1382add_linuxrc <<-'EOF'
82b2dba2
ER
1383 }
1384 else
1385 debugshell() {
1386 :
1387 }
1388 fi
e934d044 1389
82b2dba2
ER
1390 if [ "$DEBUGINITRD" ]; then
1391 set -x
cff3058d 1392 fi
82b2dba2 1393EOF
2df2e995 1394
fe280785 1395modules_add_linuxrc $MODULES
82b2dba2
ER
1396
1397# TODO: rewrite for busybox
1398#if [ -n "$loopDev" ]; then
1399# if [ ! -d /initrd ]; then
1400# mkdir /initrd
1401# fi
1402#
9b557a09
ER
1403# cp -a "$loopDev" "$DESTDIR/dev"
1404# cp -a "$rootdev" "$DESTDIR/dev"
82b2dba2
ER
1405# echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1406# echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1407# echo "echo Setting up loopback device $rootdev" >> $RCFILE
1408# echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE"
1409#fi
2b1a3707 1410
1606e343
AM
1411if is_yes "$USE_UDEV"; then
1412 initrd_gen_udev
1413fi
1414
3dd50160
ER
1415find_modules_uvesafb
1416initrd_gen_uvesafb
43258faf 1417
1b481849
ER
1418initrd_gen_luks
1419initrd_gen_dmraid
1420initrd_gen_multipath
1421initrd_gen_blkid
289fbc9b 1422
69b1e935 1423if is_yes "$have_nfs"; then
2df2e995 1424 initrd_gen_nfs
1b481849
ER
1425else
1426 initrd_gen_md
1427 initrd_gen_lvm
1428 initrd_gen_luks
4828c787 1429 initrd_gen_setrootdev
7c38b114
AF
1430fi
1431
1b481849
ER
1432initrd_gen_tuxonice
1433initrd_gen_suspend
4c16dac4 1434
5101a385 1435# additional devs always needed
9b557a09 1436[ ! -e "$DESTDIR/$rootdev_add" ] && inst $rootdev_add /dev
5101a385 1437
1b481849 1438initrd_gen_stop_udevd
6fadace4 1439initrd_gen_stop_uvesafb
82de999f 1440
52a92a2a
ER
1441# clean up env
1442add_linuxrc <<-'EOF'
1443 ifs=$IFS
1444 IFS="
1445 "
1446 for i in $(export -p); do
1447 i=${i#declare -x } # ksh/bash
1448 i=${i#export } # busybox
1449
1450 case "$i" in
1451 *=*)
1452 : ;;
1453 *)
1454 continue ;;
1455 esac
1456
1457 i=${i%%=*}
1458
1459 [ -z "$i" ] && continue
1460
1461 case "$i" in
62028605 1462 ROOT|PATH|HOME|TERM)
52a92a2a
ER
1463 :
1464 ;;
1465 *)
62028605 1466 unset $i
52a92a2a
ER
1467 ;;
1468 esac
1469 done
1470 IFS=$ifs
1471EOF
1472
f8f9e56d 1473if [ "$INITRDFS" = "initramfs" ]; then
4671d086 1474 initrd_gen_initramfs_switchroot
b64f015b 1475else
b64f015b 1476 umount_all
f8f9e56d
AM
1477fi
1478
1b481849
ER
1479initrd_gen_fbsplash
1480initrd_gen_fbcondecor
6c69a2d4 1481
2990ac33
ER
1482debug "Current /linuxrc:\n$(sed -e 's,^,> ,' $DESTDIR/linuxrc)"
1483
d8056591 1484IMAGE=$(mktemp -t initrd.img-XXXXXX) || die "mktemp failed"
51dc1fe6 1485
5c7359e3 1486IMAGESIZE=$(du -ks $DESTDIR | awk '{print int(($1+1023+512)/1024)*1024}')
02ba8ab7 1487verbose "image size: $IMAGESIZE KiB ($DESTDIR)"
5c7359e3 1488
02ba8ab7 1489verbose "Creating $INITRDFS image $IMAGE"
2ad94d8a 1490case "$INITRDFS" in
4877276e 1491 ext2)
4877276e 1492 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
c091a192
ER
1493 # NOTE: ext2 label is max 16 chars
1494 mke2fs -q -F -b 1024 -m 0 -L "PLD/$kernel" "$IMAGE" 2>/dev/null 1>&2
4877276e
ER
1495 tune2fs -i 0 "$IMAGE" >/dev/null 2>&1
1496
1497 local tmpmnt=$(mktemp -d -t initrd.mnt-XXXXXX)
1498 debug "Mounting ext2 image $IMAGE to $tmpmnt"
5c7359e3 1499 mount -o loop -t ext2 "$IMAGE" "$tmpmnt" || die "mount failed, check dmesg(1)"
4877276e
ER
1500 # We don't need this directory, so let's save space
1501 rm -rf "$tmpmnt"/lost+found
1502
1503 debug "Copy recursively $DESTDIR -> $tmpmnt"
1504 cp -a $DESTDIR/* $tmpmnt
1505 umount "$IMAGE"
1506 rmdir "$tmpmnt"
1507
1508 ;;
1509 rom|romfs)
c091a192 1510 genromfs -f "$IMAGE" -d "$DESTDIR" -V "PLD Linux/$kernel (geninitrd/$VERSION)"
4877276e
ER
1511 ;;
1512 cram|cramfs)
1513 mkcramfs "$DESTDIR" "$IMAGE"
1514 ;;
1515 initramfs)
1516 (cd $DESTDIR; find . | cpio --quiet -H newc -o > "$IMAGE")
1517 ;;
1518 *)
93a38d1a 1519 die "Filesystem $INITRDFS not supported by $PROGRAM"
c31050f3 1520esac
bb529f94 1521
755e5cdc 1522CONFIG_BLK_DEV_RAM_SIZE=$(ikconfig | awk -F= '/^CONFIG_BLK_DEV_RAM_SIZE/{print $2}')
25cb53df
ER
1523if [ -z "$CONFIG_BLK_DEV_RAM_SIZE" ]; then
1524 CONFIG_BLK_DEV_RAM_SIZE=4096
3fbd853a 1525 warn "No CONFIG_BLK_DEV_RAM_SIZE detected, fallback to $CONFIG_BLK_DEV_RAM_SIZE"
25cb53df 1526fi
e3bc44e2 1527
e3bc44e2
ER
1528if [ "$IMAGESIZE" -gt $CONFIG_BLK_DEV_RAM_SIZE ]; then
1529 warn "Your image size is larger than $CONFIG_BLK_DEV_RAM_SIZE, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!"
1530fi
1531
c552503c 1532if ! is_no "$COMPRESS"; then
c552503c 1533 compressor=$(find_compressor "$COMPRESS")
02ba8ab7 1534 verbose "Compressing $target with $compressor"
c552503c
ER
1535
1536 # TODO: the image name (specified from kernel.spec) already contains
1537 # extension, which is .gz most of the time.
97f51b6e
ER
1538 compress_image "$compressor" "$IMAGE" "$target" || {
1539 if [ $compressor != gzip ]; then
1540 warn "Could not compress with $compressor, retrying with gzip"
1541 compress_image gzip "$IMAGE" "$target" || die "compress failed with gzip" $?
1542 else
1543 die "Could not compress image with $compressor"
1544 fi
1545 }
bb529f94 1546else
7d2fc5eb 1547 cp -a "$IMAGE" "$target"
bb529f94 1548fi
5b71959c 1549
7992356a 1550# XXX. check if bootsplash can output data to tmp dir not directly to initramfs image.
1b481849 1551initrd_gen_bootsplash "$target"
5b71959c 1552
d8056591 1553rm -rf "$DESTDIR" "$IMAGE"
ac085800
ER
1554
1555# vim:ts=4:sw=4:noet:fdm=marker
This page took 0.534952 seconds and 4 git commands to generate.