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