7 # based on mkinitrd from RedHat Linux
10 GENINITRD_RCSID='$Revision$ $Date:: $'
11 R=${GENINITRD_RCSID#* *}; VERSION=${R%% *}
14 . /etc/rc.d/init.d/functions
15 . /lib/geninitrd/functions
16 . /etc/sysconfig/system
18 # list of geninitrd modules which need setup routine after commandline args parsing
30 # device node for rootfs from fstab
40 # is /tmp mounted on tmpfs
43 # are /dev nodes already created from /proc/devices info?
47 echo "Usage: $PROGRAM [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
48 echo " [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
49 echo " [--compress=yes|xz|lzma|bzip2|gzip|lzo]"
50 echo " [--initrdfs=rom|initramfs|ext2|cram] [--modules-conf=<modules.conf>]"
51 echo " [--with-bootsplash] [--without-bootsplash]"
52 echo " [--with-fbsplash] [--without-fbsplash]"
53 echo " [--with-fbcondecor] [--without-fbcondecor]"
54 echo " [--lvmtoolsversion=1|2] [--with-udev] [--without-udev]"
55 echo " [--with-suspend] [--without-suspend]"
56 echo " [--with-tuxonice] [--without-tuxonice]"
57 echo " [--without-dmraid]"
58 echo " [--with-multipath=DEVPATH] [--without-multipath]"
59 echo " [--without-blkid] [--without-luks]"
60 echo " <initrd-image> <kernel-version>"
65 for kdir in /lib/modules/*; do
66 [ -d $kdir ] || continue
68 echo " $PROGRAM -f --initrdfs=rom /boot/initrd-$kver.gz $kver"
82 [ -n "$verbose" ] && msg "$*" >&2
85 # aborts program abnormally
92 # find program from specified paths
98 debug "find_tool: found $x"
102 debug "find_tool: did not found any of: $@"
106 # loads geninitrd modules
107 geninitrd_load_mods() {
110 if [ ! -f /lib/geninitrd/mod-$mod.sh ]; then
111 die "$mod geninitrd module can't be loaded"
113 . /lib/geninitrd/mod-$mod.sh
115 GENINITRD_MODS="$GENINITRD_MODS $mod"
119 # setup geninitrd modules
120 geninitrd_setup_mods() {
123 for mod in $GENINITRD_MODS; do
124 eval rcsid=$(echo \$$mod | LC_ALL=C tr '[a-z]' '[A-Z]')_RCSID
125 debug "# $rcsid (mod-$mod)"
127 # some mods want init
128 if type setup_mod_$mod > /dev/null; then
134 # append text to /linuxrc
135 # takes STDIN as input
140 # generate code to mount /dev on tmpfs and create initial nodes
141 # can be called multiple times. /dev is cleaned up (umounted) automatically at
144 if [ "$INITRDFS" = "initramfs" ]; then
145 # initramfs is read-write filesystem, no need for tmpfs
149 # we already generated tmpfs code; return
150 if is_yes "$dev_mounted"; then
156 busybox_applet mount mknod mkdir
159 mount -o mode=0755 -t tmpfs none /dev
160 mknod /dev/console c 5 1
161 mknod /dev/null c 1 3
162 mknod /dev/zero c 1 5
163 mknod /dev/random c 1 8
164 mknod /dev/snapshot c 10 231
165 mknod /dev/urandom c 1 9
171 # generate code to mount /proc on initrd
172 # can be called multiple times
174 if is_yes "$proc_mounted"; then
179 if [ "$INITRDFS" = "initramfs" ]; then
180 # /proc is mounted with initramfs 2.6.22.14 kernel
181 # XXX: remove when it is clear why proc was already mounted
182 echo "[ -f /proc/cmdline ] || mount -t proc none /proc" | add_linuxrc
184 echo "mount -t proc none /proc" | add_linuxrc
188 # generate code to mount /sys on initrd
189 # can be called multiple times
191 if is_yes "$sys_mounted"; then
196 echo "mount -t sysfs none /sys" | add_linuxrc
199 # generate code to mount /tmp on initrd
200 # can be called multiple times
202 if [ "$INITRDFS" = "initramfs" ]; then
203 # initramfs is read-write filesystem, no need for tmpfs
207 if is_yes "$tmp_mounted"; then
212 echo "mount -t tmpfs none /tmp" | add_linuxrc
215 # unmount all mountpoints mounted by geninitrd
219 : Last shell before umounting all and giving control over to real init.
223 if is_yes "$dev_mounted"; then
224 echo 'umount /dev' | add_linuxrc
227 if is_yes "$sys_mounted"; then
228 echo 'umount /sys' | add_linuxrc
231 if is_yes "$tmp_mounted"; then
232 echo 'umount /tmp' | add_linuxrc
235 if is_yes "$proc_mounted"; then
236 echo 'umount /proc' | add_linuxrc
242 # Checks if busybox has support for APPLET(s)
243 # Exits from geninitrd if the support is not present.
245 # NB! XXX do not output to STDOUT, it will appear in initrd images in some cases!
249 if [ -z "$busybox_functions" ]; then
250 local tmp=$($busybox 2>&1)
252 # BusyBox v1.1.3 says applet not found if it's not called 'busybox'.
253 if [[ "$tmp" = *applet\ not\ found* ]]; then
255 ln -s $busybox $t/busybox
256 local tmp=$($t/busybox 2>&1)
260 busybox_functions=$(echo "$tmp" | \
261 sed -ne '/Currently defined functions:/,$p' | \
262 xargs | sed -e 's,.*Currently defined functions: ,,'
268 eval have='$'busybox_have_$applet
269 if [ -z "$have" ]; then
270 have=$(echo "$busybox_functions" | egrep -c "( |^)$applet(,|$)")
271 if [ "$have" = 0 ]; then
272 warn "This setup requires busybox-initrd compiled with applet '$applet' support"
275 eval busybox_have_$applet=$have
278 if [ $err = 1 ]; then
283 # Extract the .config file from a kernel image
284 # uses extract-ikconfig from kernel sources (scripts/extract-ikconfig)
286 local kofile=$(modinfo -k $kernel -n configs 2> /dev/null)
287 if [ -n "$kofile" ]; then
288 /lib/geninitrd/extract-ikconfig $kofile
291 /lib/geninitrd/extract-ikconfig /boot/vmlinuz-$kernel
295 # Finds module dependencies
299 # Outputs full path to module and it's dependencies
304 # if module is prefixed with dash, we should ignore errors if the module
306 if [ ${module#-} != $module ]; then
311 # This works when user has module-init-tools installed even on 2.4 kernels
313 modprobe=$(modprobe --set-version $kernel --show-depends $module --ignore-install 2>&1)
316 if [ $skiperrors = 1 ]; then
321 if ! is_no "$EXIT_IF_MISSING"; then
322 die "$module: module not found for $kernel kernel"
325 warn "$module: module not found for $kernel kernel"
326 warn "If $module isn't compiled in kernel then this initrd may not start your system."
330 while read insmod modpath options; do
331 [ "$insmod" = "insmod" ] && echo $modpath
339 # no firmware support in 2.4 kernels
340 if [ "$kernel_version_long" -lt "002005048" ]; then
343 echo -n $(NEW_MODINFO=1 modinfo -k $kernel -F firmware $module 2>/dev/null | xargs)
348 local mod depmod module=$1
350 depmod=$(find_depmod $module) || exit 1
351 for mod in $depmod; do
352 mod=${mod#/lib/modules/$kernel/}
354 # add each module only once
356 for m in $MODULES; do
357 [ $m = $mod ] && have=1
359 if [ $have = 0 ]; then
360 MODULES="$MODULES $mod"
365 # install a file to temporary mount image.
366 # it will operate recursively (copying directories)
367 # and will symlink destinations if source is symlink.
369 if [ $# -lt 2 ]; then
370 die 'Usage: inst <file> [<file>] <destination>'
373 local src i=0 c=$(($# - 1))
374 while [ $i -lt $c ]; do
381 local parentDir=$(dirname $DESTDIR$dest)
382 [ ! -d "$parentDir" ] && (debug "+ mkdir -p $parentDir"; mkdir -p $parentDir)
383 debug "+ cp $* $DESTDIR$dest"
384 cp -HR "$@" "$DESTDIR$dest"
389 die 'Usage: inst_d <destination> <destination>'
392 install -d "$DESTDIR$dir"
396 # install executable and it's shared libraries
398 if [ $# -lt 2 ]; then
399 die "Invalid params ($@), Usage: inst_exec <file>[, <file>] <destination>"
401 local src i=0 c=$(($# - 1))
402 while [ $i -lt $c ]; do
412 local lib libs=$(ldd "$@" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
414 if [ ! -f "$DESTDIR/$_lib/${lib##*/}" ]; then
416 inst_exec $lib /$_lib
420 # hack for uclibc linked binaries requiring this fixed path
421 # XXX: shouldn't rpath be used here instead so th
422 if [ -f $DESTDIR/$_lib/libc.so.0 ]; then
423 local lib=$DESTDIR/$_lib/libc.so.0
424 lib=$(ldd "$lib" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
425 local libdir=$(cd $(dirname "$lib"); pwd)
426 if [ ! -e $DESTDIR$libdir ]; then
427 libdir=$(dirname "$libdir")
429 debug "+ ln -s /$_lib $DESTDIR$libdir"
430 ln -s /$_lib $DESTDIR$libdir
435 # output modules.conf / modprobe.conf
437 echo "$modprobe_conf_cache"
441 # defaults to modprobe -c if not told otherwise, this means include statements
443 cache_modprobe_conf() {
444 if [ "$kernel_version" -lt "002005" ]; then
445 modulefile=/etc/modules.conf
446 if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
447 modulefile=/etc/conf.modules
451 if [ -n "$modulefile" ]; then
452 debug "Using $modulefile for modules config"
453 modprobe_conf_cache=$(cat $modulefile)
455 debug "Using modprobe -c to get modules config"
456 modprobe_conf_cache=$(modprobe -c --set-version $kernel)
460 # find modules for $devpath
461 find_modules_for_devpath() {
463 if [ -z "$devpath" ]; then
464 die "No argument passed to find_modules_for_devpath() - is your /etc/fstab correct?"
467 if [[ "$devpath" = /dev/dm-* ]]; then
468 # /dev/dm-3 -> /dev/mapper/sil_ahbgadcbchfc3
469 devpath=$(dm_node "$devpath")
472 if [ -L "$devpath" ] && ! is_lvm "$devpath" && ! is_luks "$devpath"; then
473 # sanitize things like:
474 # /dev/block/104:2 -> /dev/cciss/c0d0p2
475 devpath=$(readlink -f "$devpath")
478 debug "Finding modules for device path $devpath"
480 if is_luks "$devpath"; then
481 find_modules_luks "$devpath"
485 if is_nfs "$devpath"; then
486 find_modules_nfs "$devpath"
490 if is_md "$devpath"; then
491 find_modules_md "$devpath"
495 if is_multipath "$devpath"; then
496 if find_modules_multipath "$devpath"; then
503 if is_dmraid "$devpath"; then
504 if find_modules_dmraid "$devpath"; then
510 if is_scsi "$devpath"; then
511 find_modules_scsi "$devpath"
515 if is_ide "$devpath"; then
516 find_modules_ide "$devpath"
520 if [[ "$devpath" == /dev/rd/* ]]; then
526 if [[ "$devpath" == /dev/ida/* ]]; then
527 find_module "cpqarray"
528 rootdev_add=/dev/ida/
532 if [[ "$devpath" == /dev/cciss/* ]]; then
534 rootdev_add=/dev/cciss/
538 if [[ "$devpath" == /dev/ataraid/* ]]; then
540 find_module "ataraid"
541 ataraidmodules=$(modprobe_conf | awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }')
542 if [ -n "$ataraidmodules" ]; then
543 # FIXME: think about modules compiled in kernel
544 die "ataraid_hostadapter alias not defined in modprobe.conf! Please set it and run $PROGRAM again."
546 for n in $ataraidmodules; do
549 rootdev_add=/dev/ataraid/
553 # check to see if we need to set up a loopback filesystem
554 if [[ "$devpath" == /dev/loop* ]]; then
555 die "Sorry, root on loop device isn't supported."
556 # TODO: rewrite for bsp and make nfs ready
557 if [ ! -x /sbin/losetup ]; then
558 die "losetup is missing"
560 key="^# $(echo $devpath | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
561 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`"; then
562 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"
565 line="`awk '/'$key'/ { print $0; }' $fstab`"
566 loopDev="$(echo $line | awk '{print $3}')"
567 loopFs="$(echo $line | awk '{print $4}')"
568 loopFile="$(echo $line | awk '{print $5}')"
570 BASICMODULES="$BASICMODULES -loop"
571 find_module "-$loopFs"
572 BASICMODULES="$BASICMODULES -${loopFs}"
576 if is_lvm "$devpath"; then
577 find_modules_lvm "$devpath"
582 firmware_install_module() {
584 local firmware_files="$2"
586 debug "Adding Firmwares ($firmware_files) to initrd for module $module"
587 # firmware not yet installed
588 if [ ! -f "$DESTDIR/lib/firmware/firmware.sh" ]; then
590 cat << 'EOF' >> "$DESTDIR/lib/firmware/firmware.sh"
592 echo 1 > /sys$DEVPATH/loading
593 cat "/lib/firmware/$FIRMWARE" > /sys$DEVPATH/data
594 echo 0 > /sys$DEVPATH/loading
597 chmod 755 "$DESTDIR/lib/firmware/firmware.sh"
600 for firmware in $firmware_files; do
601 if [ -f "/lib/firmware/$kernel/$firmware" ]; then
602 FIRMWAREDIR=${firmware%/*}
603 [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR
604 inst /lib/firmware/$kernel/$firmware /lib/firmware/$firmware
605 elif [ -f "/lib/firmware/$firmware" ]; then
606 FIRMWAREDIR=${firmware%/*}
607 [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR
608 inst /lib/firmware/$firmware /lib/firmware/$firmware
610 die "firmware file /lib/firmware/$firmware nor /lib/firmware/$kernel/$firmware found."
615 echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" | add_linuxrc
622 for mod in $modules; do
624 inst_d "/lib/modules/$kernel/$MODULEDIR"
625 cp -a "/lib/modules/$kernel/$mod" "$DESTDIR/lib/modules/$kernel/$mod"
626 gunzip "$DESTDIR/lib/modules/$kernel/$mod" 2> /dev/null
630 modules_add_linuxrc() {
634 # module path without optional compression
638 local module=${modpath##*/}; module=${module%$modext}
639 local options=$(modprobe_conf | awk -vmodule="$module" '{ if ($1 == "options" && $2 == module) { for(i=3;i<=NF;i++) printf("%s ",$i); }}' | xargs)
640 local genericname=$(echo $module | tr - _)
641 local usleep=$(eval echo \$MODULE_${genericname}_USLEEP)
642 local firmware=$(eval echo \$MODULE_${genericname}_FIRMWARE)
644 if [ "$module" = "scsi_mod" -a "$kernel_version_long" -ge "002006030" ]; then
645 options="scan=sync $options"
648 if [ -n "$verbose" ]; then
650 if [ "$options" ]; then
651 s="$s with options [$options]"
653 if [ "$usleep" ]; then
654 s="$s and $usleep usleep"
656 debug "Loading module [$module]$s"
659 if [ -n "$firmware" ]; then
660 firmware_install_module "$module" "$firmware"
662 for file in $(find_firmware "$module"); do
663 firmware_install_module "$module" "$file"
667 echo "insmod /lib/modules/$kernel/$modpath $options" | add_linuxrc
668 if [ -n "$usleep" ]; then
669 echo "usleep $usleep" | add_linuxrc
671 if [ "$module" = "scsi_wait_scan" ]; then
672 if [ "$(busybox_applet rmmod 2>/dev/null; echo $?)" = 0 ]; then
673 echo "rmmod scsi_wait_scan" | add_linuxrc
680 # Generates /dev nodes based on /proc/partitions information.
681 # Needs /proc mounted.
682 # Can be called multiple times.
683 initrd_gen_devices() {
684 if is_yes "$proc_partitions"; then
691 : 'Making device nodes'
692 cat /proc/partitions | (
693 # ignore first two lines: header, empty line
696 while read major minor blocks dev rest; do
699 [ -e $node ] || mknod $node b $major $minor
706 initrd_gen_setrootdev() {
707 debug "Adding rootfs finding based on kernel cmdline root= option support."
710 if [ "${ROOT##/dev/}" != "${ROOT}" ]; then
711 rootnr="$(busybox awk -v rootnode="${ROOT##/dev/}" '$4 == rootnode { print 256 * $1 + $2 }' /proc/partitions)"
713 if [ -z "$rootnr" ]; then
714 rootnr="$(busybox ls -lL ${ROOT} | busybox awk '{if (/^b/) { print 256 * $3 + $4; }}')"
716 if [ -n "$rootnr" ]; then
717 echo "$rootnr" > /proc/sys/kernel/real-root-dev
723 initrd_gen_initramfs_switchroot() {
725 if [ "$rootdev" = "/dev/nfs" ]; then
726 echo "rootfs on NFS root=/dev/nfs"
728 [ ! -e "$DESTDIR/$rootdev" ] && inst $rootdev $rootdev
731 # parse 'root=xxx' kernel commandline
732 # We support passing root as hda3 /dev/hda3 0303 0x0303 and 303
734 device=/dev/no_partition_found
735 eval "$(busybox awk -v c="$ROOT" '
737 num_pattern_short = "[0-9a-f][0-9a-f][0-9a-f]";
738 num_pattern = "[0-9a-f]" num_pattern_short;
739 dev_pattern = "[hms][a-z][a-z]([0-9])+";
740 partition = "no_partition_found";
744 if (c ~ "^" num_pattern_short "$") sub("^", "0", c);
745 if (c ~ "^" num_pattern "$") {
746 maj = sprintf("%s",substr(c,1,2));
747 min = sprintf("%s",substr(c,3));
749 if (c ~ "^\/dev\/" dev_pattern "$") sub("^/dev/","", c);
750 if (c ~ "^" dev_pattern "$") partition = c;
753 $4 ~ partition { maj = $1; min = $2; }
754 $1 ~ maj && $2 ~ min { partition = $4; }
757 if (maj >= 0 && min >= 0) {
758 printf("device=/dev/%s; maj=%s; min=%s;\n", partition, maj, min);
762 if [ "$device" != '/dev/no_partition_found' -a ! -b $device ]; then
763 mknod $device b $maj $min
773 if [ "$device" = '/dev/no_partition_found' ]; then
777 [ -n "$ROOTFSFLAGS" ] && ROOTFSFLAGS="-o $ROOTFSFLAGS"
779 mount -t $rootfs -r $device $ROOTFSFLAGS /newroot || echo "Mount of rootfs failed."
780 init="$(echo "$CMDLINE" | busybox awk '/init=\// { gsub(/.*init=/,NIL,$0); gsub(/ .*/,NIL,$0); print }')"
781 if [ -z "$init" -o ! -x "/newroot$init" ]; then
787 busybox_applet switch_root
789 exec switch_root /newroot $init ${1:+"$@"}
791 echo "Error! initramfs should not reach this place."
792 echo "It probably means you've got old version of busybox, with broken"
793 echo "initramfs support. Trying to boot anyway, but won't promise anything."
795 exec chroot /newroot $init ${1:+"$@"}
797 echo "Failed to chroot!"
800 # we need /init being real file, not symlink, otherwise the initramfs will
801 # not be ran by pid 1 which is required for switch_root
802 mv $DESTDIR/linuxrc $DESTDIR/init
803 ln -s init $DESTDIR/linuxrc
806 # find if $symbol exists in System.map $mapfile
810 if [ ! -f $mapfile ]; then
811 # missing mapfile (not a pld kernel?)
815 awk -vc=1 -vsymbol="$symbol" '$2 == "T" && $3 == symbol {c = 0} END {exit c}' $mapfile
818 # find best compressor (or forced one) for initrd
821 # fastest initrd decompression speed is first
822 local compressors='lzo gzip xz lzma bzip2'
824 # a specified one, take it
825 if ! is_yes "$mode"; then
829 debug "finding compressor: $compressors (via $mode)"
830 # check for compressor validity
831 local c prog map=/boot/System.map-$kernel
832 for c in $compressors; do
855 # any existing sym will work
860 die "Unknown compressor $c"
863 if sym_exists $map $sym && [ -x $prog ]; then
869 debug "using gzip for compressor (fallback)"
873 # compresses kernel image image
874 # in function so we could retry with other compressor on failure
876 local compressor="$1" IMAGE="$2" target="$3" tmp
877 tmp=$(mktemp "$target".XXXXXX) || die "mktemp failed"
879 case "$compressor" in
881 # don't use -9 here since kernel won't understand it
882 xz --format=xz --check=crc32 --lzma2=preset=6e,dict=1MiB < "$IMAGE" > "$tmp" || return $?
885 xz --format=lzma -9 < "$IMAGE" > "$tmp" || return $?
888 bzip2 -9 < "$IMAGE" > "$tmp" || return $?
891 gzip -9 < "$IMAGE" > "$tmp" || return $?
894 lzop -9 < "$IMAGE" > "$tmp" || return $?
897 cat < "$IMAGE" > "$tmp" || return $?
901 mv -f "$tmp" "$target"
904 if [ -r /etc/sysconfig/geninitrd ]; then
905 . /etc/sysconfig/geninitrd
908 if [ ! -f /proc/mounts ]; then
909 warn "/proc filesystem not mounted, may cause wrong results or failure."
912 geninitrd_load_mods ide luks multipath dmraid lvm md blkid udev tuxonice suspend fbsplash condecor bootsplash uvesafb nfs sata scsi
914 while [ $# -gt 0 ]; do
924 modulefile=${1#--modules-conf=}
933 --without-bootsplash)
945 --without-fbcondecor)
954 --with-suspend2 | --with-tuxonice)
957 --without-suspend2 | --without-tuxonice)
961 LVMTOOLSVERSION=${1#--lvmversion=}
964 LVMTOOLSVERSION=${1#--lvmtoolsversion=}
966 --lvmtoolsversion|--lvmversion)
983 USE_MULTIPATH=${1#--with-multipath=}
992 BASICMODULES="$BASICMODULES ${1#--with=}"
995 BASICMODULES="$BASICMODULES $2"
999 echo "$PROGRAM: version $VERSION"
1009 COMPRESS="${1#--compress=}"
1021 PREMODS="$PREMODS ${1#--preload=}"
1024 PREMODS="$PREMODS $2"
1028 die "--fs option is obsoleted. Use --initrdfs instead"
1031 INITRDFS=${1#--initrdfs=}
1044 if [ -z "$target" ]; then
1046 elif [ -z "$kernel" ]; then
1058 if [ -z "$target" -o -z "$kernel" ]; then
1064 if [ "$(id -u)" != 0 ]; then
1065 die "You need to be root to generate initrd"
1068 if [ -d /lib64 -a -d /usr/lib64 ]; then
1074 initrd_dir=/usr/$_lib/initrd
1075 kernel_version=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d",$1,$2)}')
1076 kernel_version_long=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d%03d",$1,$2,$3)}')
1078 debug "# $GENINITRD_RCSID (geninitrd)"
1079 debug "Using _lib: $_lib"
1080 debug "Using initrd_dir: $initrd_dir"
1082 busybox=$(find_tool $initrd_dir/busybox $initrd_dir/initrd-busybox /bin/initrd-busybox) || die "Couldn't find busybox suitable for initrd"
1084 # we setup mods after parsing command line args
1085 geninitrd_setup_mods
1087 if [ ! -f /boot/vmlinuz-"$kernel" ]; then
1088 warn "/boot/vmlinuz-$kernel doesn't exist, is your /boot mounted?"
1091 if [ -z "$INITRDFS" ]; then
1092 if [ -n "$FS" ]; then
1093 # FS= can came only via /etc/sysconfig/geninitrd likely?
1094 die "FS configuration option is obsoleted. Use INITRDFS instead"
1098 if [ "$kernel_version" -ge "002005" ]; then
1099 INITRDFS="initramfs"
1107 [ -x /sbin/mke2fs ] || die "/sbin/mke2fs is missing"
1110 [ -x /sbin/genromfs ] || die "/sbin/genromfs is missing"
1113 [ -x /sbin/mkcramfs ] || die "/sbin/mkcramfs is missing"
1116 [ -x /bin/cpio ] || die "/bin/cpio is missing"
1117 [ -x /usr/bin/find ] || die "/usr/bin/find is missing"
1120 die "Filesystem $INITRDFS on initrd is not supported"
1124 if [ -L "$target" ]; then
1125 target=$(readlink -f "$target")
1128 if [ -n "$img_vers" ]; then
1129 target="$target-$kernel"
1132 if [ -z "$force" -a -f "$target" ]; then
1133 die "$target already exists."
1136 if [ ! -d "/lib/modules/$kernel" ]; then
1137 die "/lib/modules/$kernel is not a directory."
1140 if [ "$kernel_version" -ge "002005" ]; then
1146 for n in $PREMODS; do
1150 if [ "$FBMODULE" ]; then
1151 find_module "$FBMODULE"
1154 # allow forcing loading SCSI and/or IDE modules
1155 # XXX: where ADDSCSI cames from? drop?
1156 if is_yes "$ADDSCSI"; then
1160 # autodetect SATA modules
1163 # XXX: where ADDIDE cames from? drop?
1164 if is_yes "$ADDIDE"; then
1168 if is_yes "$USE_SUSPEND"; then
1169 find_modules_suspend
1172 find_root "$fstab" || exit
1173 debug "Using $rootdev as device for rootfs"
1175 find_modules_for_devpath "$rootdev"
1177 # if USE_MULTIPATH is path to device, scan that too
1178 # this is to bootstrap multipath setup into initrd.
1179 if ! is_no "$USE_MULTIPATH" && ! is_yes "$USE_MULTIPATH"; then
1180 find_modules_multipath $USE_MULTIPATH
1183 find_module "-$rootFs"
1185 for n in $BASICMODULES; do
1189 if is_yes "$USE_TUXONICE"; then
1193 find_modules_fbsplash
1195 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
1196 debug "No modules are needed -- not building initrd image."
1200 debug "Building initrd..."
1201 DESTDIR=$(mktemp -d -t initrd.XXXXXX) || die "mktemp failed"
1202 RCFILE="$DESTDIR/linuxrc"
1204 chmod a+rx "$RCFILE"
1205 ln -s linuxrc $DESTDIR/init
1207 # create dirs that we really need
1208 inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,sys}
1210 modules_install "$MODULES"
1212 # mknod'ing the devices instead of copying them works both with and
1214 mknod "$DESTDIR/dev/console" c 5 1
1215 mknod "$DESTDIR/dev/null" c 1 3
1216 mknod "$DESTDIR/dev/zero" c 1 5
1217 mknod "$DESTDIR/dev/random" c 1 8
1218 mknod "$DESTDIR/dev/urandom" c 1 9
1220 inst_exec $busybox /bin/busybox
1221 ln -s busybox $DESTDIR/bin/sh
1222 # for older busyboxes who had /bin/initrd-busybox as EXEPATH
1223 ln -s busybox $DESTDIR/bin/initrd-busybox
1227 # initrd generated by:
1232 add_linuxrc <<-'EOF'
1233 read CMDLINE < /proc/cmdline; export CMDLINE
1235 for arg in $CMDLINE; do
1236 if [ "${arg}" = "debuginitrd" ]; then
1239 if [ "${arg##debuginitrd=}" != "${arg}" ]; then
1240 DEBUGINITRD=${arg##debuginitrd=}
1242 if [ "${arg##root=}" != "${arg}" ]; then
1245 if [ "${arg##rootfsflags=}" != "${arg}" ]; then
1246 ROOTFSFLAGS=${arg##rootfsflags=}
1250 # make debugshell() invoke subshell if $DEBUGINITRD=sh
1251 if [ "$DEBUGINITRD" = "sh" ]; then
1254 if is_yes "$RUN_SULOGIN_ON_ERR"; then
1255 add_linuxrc <<-'EOF'
1256 echo "debug shell disabled by /etc/sysconfig/system:RUN_SULOGIN_ON_ERR setting"
1259 add_linuxrc <<-'EOF'
1263 add_linuxrc <<-'EOF'
1271 if [ "$DEBUGINITRD" ]; then
1276 modules_add_linuxrc $MODULES
1278 # TODO: rewrite for busybox
1279 #if [ -n "$loopDev" ]; then
1280 # if [ ! -d /initrd ]; then
1284 # cp -a "$loopDev" "$DESTDIR/dev"
1285 # cp -a "$rootdev" "$DESTDIR/dev"
1286 # echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1287 # echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1288 # echo "echo Setting up loopback device $rootdev" >> $RCFILE
1289 # echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE"
1292 if is_yes "$USE_UDEV"; then
1296 find_modules_uvesafb
1301 initrd_gen_multipath
1304 if is_yes "$have_nfs"; then
1310 initrd_gen_setrootdev
1316 # additional devs always needed
1317 [ ! -e "$DESTDIR/$rootdev_add" ] && inst $rootdev_add /dev
1319 initrd_gen_stop_udevd
1321 if [ "$INITRDFS" = "initramfs" ]; then
1322 initrd_gen_initramfs_switchroot
1328 initrd_gen_fbcondecor
1330 IMAGE=$(mktemp -t initrd.img-XXXXXX) || die "mktemp failed"
1332 IMAGESIZE=$(du -ks $DESTDIR | awk '{print int(($1+1023+512)/1024)*1024}')
1333 debug "image size: $IMAGESIZE KiB ($DESTDIR)"
1335 debug "Creating $INITRDFS image $IMAGE"
1338 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
1339 # NOTE: ext2 label is max 16 chars
1340 mke2fs -q -F -b 1024 -m 0 -L "PLD/$kernel" "$IMAGE" 2>/dev/null 1>&2
1341 tune2fs -i 0 "$IMAGE" >/dev/null 2>&1
1343 local tmpmnt=$(mktemp -d -t initrd.mnt-XXXXXX)
1344 debug "Mounting ext2 image $IMAGE to $tmpmnt"
1345 mount -o loop -t ext2 "$IMAGE" "$tmpmnt" || die "mount failed, check dmesg(1)"
1346 # We don't need this directory, so let's save space
1347 rm -rf "$tmpmnt"/lost+found
1349 debug "Copy recursively $DESTDIR -> $tmpmnt"
1350 cp -a $DESTDIR/* $tmpmnt
1356 genromfs -f "$IMAGE" -d "$DESTDIR" -V "PLD Linux/$kernel (geninitrd/$VERSION)"
1359 mkcramfs "$DESTDIR" "$IMAGE"
1362 (cd $DESTDIR; find . | cpio --quiet -H newc -o > "$IMAGE")
1365 die "Filesystem $INITRDFS not supported by $PROGRAM"
1368 CONFIG_BLK_DEV_RAM_SIZE=$(ikconfig | awk -F= '/^CONFIG_BLK_DEV_RAM_SIZE/{print $2}')
1369 if [ -z "$CONFIG_BLK_DEV_RAM_SIZE" ]; then
1370 CONFIG_BLK_DEV_RAM_SIZE=4096
1373 if [ "$IMAGESIZE" -gt $CONFIG_BLK_DEV_RAM_SIZE ]; then
1374 warn "Your image size is larger than $CONFIG_BLK_DEV_RAM_SIZE, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!"
1377 if ! is_no "$COMPRESS"; then
1378 compressor=$(find_compressor "$COMPRESS")
1379 debug "Compressing $target with $compressor"
1381 # TODO: the image name (specified from kernel.spec) already contains
1382 # extension, which is .gz most of the time.
1383 compress_image "$compressor" "$IMAGE" "$target" || {
1384 if [ $compressor != gzip ]; then
1385 warn "Could not compress with $compressor, retrying with gzip"
1386 compress_image gzip "$IMAGE" "$target" || die "compress failed with gzip" $?
1388 die "Could not compress image with $compressor"
1392 cp -a "$IMAGE" "$target"
1395 # XXX. check if bootsplash can output data to tmp dir not directly to initramfs image.
1396 initrd_gen_bootsplash "$target"
1398 rm -rf "$DESTDIR" "$IMAGE"
1400 # vim:ts=4:sw=4:noet:fdm=marker