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