5 # by Jacek Konieczny <jajcus@pld.org.pl>
7 # based on mkinitrd from RedHat
9 RCSID='$Id: geninitrd,v 2.10 2001/12/15 14:33:09 blues Exp $'
13 VERSION="`echo "$RCSID"|awk '{print $3}'`"
15 . /etc/rc.d/init.d/functions
21 PRESCSIMODS="scsi_mod unknown sd_mod"
22 PREIDEMODS="ide-mod ide-probe ide-probe-mod ide-disk"
29 modulefile=/etc/modules.conf
34 echo "usage: `basename $0` [--version] [-v] [-f] [--ifneeded] [--preload <module>]" 1>&2
35 echo " [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]" 1>&2
36 echo " [--fs=rom|ext2|cram] [--no-bsp] [--modules-conf=<modules.conf>]" 1>&2
37 echo " <initrd-image> <kernel-version>" 1>&2
38 echo " (ex: `basename $0` /boot/initrd-2.2.5-15.img 2.2.5-15)" 1>&2
47 if [ "$name" != "build" ]; then
48 (cd $name;my_find $1/$name $2)
51 if [ -f $name -a "$name" = "$2" ]; then
59 echo $1|awk -F/ '{print substr($0, 0, length($0) - length($NF));}'
65 if [ "$(echo $modName | awk '{print(substr($0,1,1));}')" = "-" ]; then
67 modName="$(echo $modName | awk '{print(substr($0,2));}')"
70 if [ "$modName" = "pluto" ]; then
74 if [ "$modName" = "fcal" ]; then
79 fmPath="`(cd /lib/modules/$kernel; my_find . "$modName.o")`"
81 if [ ! -f "/lib/modules/$kernel/$fmPath" ]; then
82 if [ -n "$skiperrors" ]; then
86 # ignore the absence of the scsi modules
87 for n in $PRESCSIMODS; do
88 if [ "$n" = "$modName" ]; then
93 # ignore the absence of the ide modules
94 for n in $PREIDEMODS; do
95 if [ "$n" = "$modName" ]; then
100 echo "No module $modName found for kernel $kernel" 1>&2
104 # only need to add each module once
105 tmpFmPath="`echo "$fmPath"|awk -F/ '{for(i=1;i<NF;i++) { printf("%s\\\\/",$i); } { print $NF; }}'`"
106 if is_yes "`echo "$MODULES" | awk '/"'$tmpFmPath'"/ { print "yes"; }' `" ; then : ; else
107 MODULES="$MODULES $fmPath"
112 if [ "$#" != "2" ];then
113 echo "usage: inst <file> <destination>"
116 [ -n "$verbose" ] && echo "$1 -> $2"
121 /sbin/e2label $1 2> /dev/null
125 /sbin/tune2fs -l $1 2> /dev/null | awk -F: '/UUID:/ {gsub(" ",""); print $2}'
129 /usr/sbin/xfs_db -x -p xfs_admin -c label -r "$1"|awk -F= '{sub("^\"","", $2); sub("\"$", "", $2); print $2}'
134 /usr/sbin/xfs_db -x -p xfs_admin -c uuid -r "$1"|awk -F= '{print $2}'
138 eval `awk '/^[\t ]*#/ {next} {if ( $2 == "/" ) {print "rootdev=\"" $1 "\"\nrootFs=\"" $3 "\""}}' $fstab`
143 if [ ! -x /sbin/e2label ] ; then
144 echo "/sbin/e2label is missing" 1>&2
147 get_label="get_label_ext2"
150 if [ ! -x /usr/sbin/xfs_db ] ; then
151 echo "/usr/sbin/xfs_db is missing" 1>&2
154 get_label="get_label_xfs"
157 echo "LABEL on $rootFs in not supported by geninitrd" 1>&2
161 if [ ! -r /proc/partitions ] ; then
162 echo '/proc/partitions is not readable'; 1>&2
165 label=${rootdev#"LABEL="}
166 for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
167 if [ -r $dev ] && [ "$label" = "`$get_label $dev`" ] ; then
168 if [ -n "$verbose" ] ; then
169 echo "Using $dev as device for rootfs"
176 if [ "$rootdev_found" != 1 ] ; then
177 echo "geninitrd can't find real device for LABEL=$label" 1>&2
184 if [ ! -x /sbin/tune2fs ] ; then
185 echo "/sbin/tune2fs is missing" 1>&2
188 get_uuid="get_uuid_ext2"
191 if [ ! -x /usr/sbin/xfs_db ] ; then
192 echo "/usr/sbin/xfs_db is missing" 1>&2
195 get_label="get_uuid_xfs"
198 echo "UUID on $rootFs in not supported by geninitrd" 1>&2
202 if [ ! -r /proc/partitions ] ; then
203 echo '/proc/partitions is not readable'; 1>&2
206 uuid=${rootdev#"UUID="}
207 for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
208 if [ -r $dev ] && [ "$uuid" = "`$get_uuid $dev`" ] ; then
209 if [ -n "$verbose" ] ; then
210 echo "Using $dev as device for rootfs"
217 if [ "$rootdev_found" != 1 ] ; then
218 echo "geninitrd can't find real device for UUID=$uuid" 1>&2
226 if [ -r /etc/sysconfig/geninitrd ] ; then
227 . /etc/sysconfig/geninitrd
230 if is_yes $USEBSP ; then
231 [ ! -x /sbin/bsp ] && USEBSP="no"
233 if [ "`uname -m`" = "ia64" ]; then
238 while [ $# -gt 0 ]; do
244 if is_yes "`echo $1 | awk '/=/ { print "yes"; }'`" ; then
245 fstab="`echo $1 | awk -F= '{print $2;}'`"
252 modulefile="`echo $1 | awk -F= '{print $2;}'`"
259 raidtab="`echo $1 | awk -F= '{print $2;}'`"
266 if is_yes "`echo $1 | awk '/=/ { print "yes"; }'`" ; then
267 modname="`echo $1 | awk -F= '{print $2;}'`"
273 BASICMODULES="$BASICMODULES $modname"
276 echo "geninitrd: version $VERSION"
292 if is_yes "`echo $1 | awk '/=/ { print "yes"; }'`" ; then
293 modname="`echo $1 | awk -F= '{print $2;}'`"
298 PREMODS="$PREMODS $modname"
301 FS="`echo $1 | awk -F= '{print $2;}'`"
314 if [ -z "$target" ]; then
316 elif [ -z "$kernel" ]; then
327 if [ -z "$target" -o -z "$kernel" ]; then
333 if [ ! -x /sbin/mke2fs ]; then
334 echo "/sbin/mke2fs is missing" 1>&2
339 if [ ! -x /sbin/genromfs ]; then
340 echo "/sbin/genromfs is missing" 1>&2
345 if [ ! -x /sbin/mkcramfs ]; then
346 echo "/sbin/mkcramfs is missing" 1>&2
351 echo "Filesystem $FS on initrd is not supported" 1>&2
356 if [ -n "$img_vers" ]; then
357 target="$target-$kernel"
360 if [ -z "$force" -a -f "$target" ]; then
361 echo "$target already exists." 1>&2
365 if [ ! -d "/lib/modules/$kernel" ]; then
366 echo "/lib/modules/$kernel is not a directory." 1>&2
370 for n in $PREMODS; do
376 if is_yes "`echo "$rootdev" | awk '/^\/dev\/md/ { print "yes"; }'`"; then
377 eval `awk -v rootdev=$rootdev 'BEGIN {
393 if (found == "yes") {
398 if (found == "yes") {
399 if ($1 == "device") {
400 if ($2 ~ /\/dev\/(sd|scsi)/) {
403 if ($2 ~ /\/dev\/(hd|ide)/) {
407 if ($1 == "raid-level") {
414 print "raidfound=" found "\nADDSCSI=" addscsi "\nADDIDE=" addide "\nraidlevel=" raidlevel "\n";
416 if is_yes "$raidfound" ; then
419 findmodule "raid$raidlevel"
425 echo "raid level $number (in $raidtab) not recognized" 1>&2
434 if is_yes "$ADDSCSI" || is_yes "$(echo "$rootdev" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')" ; then
436 for n in $PRESCSIMODS; do
437 if [ "$n" = "unknown" ] ; then
438 if [ ! -f "$modulefile" ]; then
439 modulefile=/etc/conf.modules
441 if [ -f "$modulefile" ]; then
442 scsimodules="`awk '/scsi_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile| LC_ALL=C sort -u`"
443 for n in $scsimodules; do
444 # for now allow scsi modules to come from anywhere. There are some
445 # RAID controllers with drivers in block/
456 if is_yes "$ADDIDE" || is_yes "`echo "$rootdev" | awk '/^\/dev\/(hd|ide)/ { print "yes"; }'`" ; then
457 for n in $PREIDEMODS; do
462 if is_yes "`echo "$rootdev" | awk '/\/dev\/rd\// { print "yes"; }'`" ; then
466 if is_yes "`echo "$rootdev" | awk '/\/dev\/ida\// { print "yes"; }'`" ; then
467 findmodule "cpqarray"
470 if is_yes "`echo "$rootdev" | awk '/\/dev\/cciss\// { print "yes"; }'`" ; then
474 # check to see if we need to set up a loopback filesystem
475 if is_yes "`echo "$rootdev" | awk -F/ '{print($3);}' | awk '/loop/ { print "yes"; }'`" ; then
476 if [ ! -x /sbin/losetup ]; then
477 echo "losetup is missing"
480 key="^# $(echo $rootdev | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
481 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`" ; then
482 echo "The root filesystem is on a $rootdev, but there is no magic entry in $fstab" 1>&2
483 echo "for this device. Consult the geninitrd man page for more information" 1>&2
487 line="`awk '/'$key'/ { print $0; }' $fstab`"
488 loopDev="$(echo $line | awk '{print $3}')"
489 loopFs="$(echo $line | awk '{print $4}')"
490 loopFile="$(echo $line | awk '{print $5}')"
492 BASICMODULES="$BASICMODULES -loop"
493 findmodule "-$loopFs"
494 BASICMODULES="$BASICMODULES -${loopFs}"
495 # don't have any clue, how is this supposed to work,
498 else # Check for other filesystems
499 findmodule "-$rootFs"
502 for n in $BASICMODULES; do
506 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
507 if [ -n "$verbose" ]; then
508 echo "No modules are needed -- not building initrd image."
513 if [ -n "$verbose" ]; then
514 echo "Using modules: $MODULES"
517 MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
518 IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
519 MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
520 RCFILE="$MNTIMAGE/linuxrc"
522 if [ -f "$MNTIMAGE" ]; then
523 echo "$MNTIMAGE already exists. Remove it and try again" 1>&2
527 if [ -f "$IMAGE" ]; then
528 echo "$IMAGE already exists. Remove it and try again" 1>&2
532 if [ "$FS" = "ext2" ] ; then
533 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
535 # We have to "echo y |" so that it doesn't complain about $IMAGE not
536 # being a block device
537 echo y | mke2fs -F "$IMAGE" "$IMAGESIZE" >/dev/null 2>/dev/null
540 mount -o loop -t ext2 "$IMAGE" "$MNTPOINT"
546 mkdir -p "$MNTIMAGE"/{lib,bin,etc,dev,loopfs}
549 # We don't need this directory, so let's save space
550 rm -rf "$MNTPOINT"/lost+found
552 for MODULE in $MODULES; do
553 MODULEDIR="`my_dirname "$MODULE"`"
554 mkdir -p "$MNTIMAGE/lib/modules/$kernel/$MODULEDIR"
555 cp $verbose -a "/lib/modules/$kernel/$MODULE" "$MNTIMAGE/lib/modules/$kernel/$MODULE"
558 # mknod'ing the devices instead of copying them works both with and
560 mknod "$MNTIMAGE/dev/console" c 5 1
562 if is_yes "$USEBSP" ; then
563 s="$MNTIMAGE/etc/startup"
564 inst /sbin/bsp "$RCFILE"
565 echo "# autogenerated startup" > "$s"
568 for MODULE in $MODULES; do
569 module="`echo $MODULE | awk -F/ '{ $0=$NF } /.o$/ { $0=substr($0,1,length($0)-2); } { print $0; }'`"
570 options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"
572 if [ -n "$verbose" ]; then
573 /bin/echo -n "Loading module $module "
574 if [ -n "$options" ] ; then
575 echo "with options $options."
577 echo "without options."
580 echo "insmod /lib/modules/$kernel/$MODULE $options" >> "$s"
583 inst /bin/trash "$MNTIMAGE/bin/sh"
584 inst /sbin/insmod.static "$MNTIMAGE/bin/insmod"
586 mknod "$MNTIMAGE/dev/null" c 1 3
587 mknod "$MNTIMAGE/dev/ram" b 1 1
588 mknod "$MNTIMAGE/dev/systty" c 4 0
590 mknod "$MNTIMAGE/dev/tty$i" c 4 1
592 mknod "$MNTIMAGE/dev/zero" c 1 5
594 echo "#!/bin/sh" > "$RCFILE"
597 for MODULE in $MODULES; do
598 module="`echo $MODULE | awk -F/ '{ $0=$NF } /.o$/ { $0=substr($0,1,length($0)-2); } { print $0; }'`"
599 if [ -f "$modulefile" ] ; then
600 options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"
605 if [ -n "$verbose" ]; then
606 echo "Loading module $module with options $options"
608 echo "Add module $module to initrd."
609 echo "echo \"Loading $module module\"" >> "$RCFILE"
610 echo "insmod -k /lib/modules/$kernel/$MODULE $options" >> "$RCFILE"
614 if [ -n "$loopDev" ]; then
615 if [ ! -d /initrd ]; then
619 cp -a "$loopDev" "$MNTIMAGE/dev"
620 cp -a "$rootdev" "$MNTIMAGE/dev"
621 echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
622 echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
623 echo "echo Setting up loopback device $rootdev" >> $RCFILE
624 echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE"
628 if is_yes "$USERAIDSTART" ; then
629 if [ -x /sbin/raidstart.static ] ; then
630 [ -n "$verbose" ] && echo "Add raidstart to initrd"
631 mkdir -p "$MNTIMAGE/etc" || true
632 inst /sbin/raidstart.static "$MNTIMAGE/bin/raidstart"
633 inst "$raidtab" "$MNTIMAGE/etc/raidtab"
634 echo "echo \"Starting RAID\"" >> "$RCFILE"
635 echo "/bin/raidstart $rootdev" >> "$RCFILE"
637 echo "/sbin/raidstart.static is missing" 1>&2
644 (cd "$MNTIMAGE"; tar cf - .) | (cd "$MNTPOINT"; tar xf -)
651 genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
654 mkcramfs "$MNTPOINT" "$IMAGE"
657 echo "Filesystem $FS not supported by $0";
660 if is_yes "$COMPRESS" ; then
661 gzip -9 < "$IMAGE" > "$target"
663 cp -a "$IMAGE" "$target"
665 rm -rf "$MNTIMAGE" "$MNTPOINT" "$IMAGE"