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