4 # $Id: geninitrd,v 1.38 2001/08/22 15:04:52 areq Exp $
6 # by Jacek Konieczny <jajcus@pld.org.pl>
8 # based on mkinitrd from RedHat
15 . /etc/rc.d/init.d/functions
22 if [ -f /etc/sysconfig/geninitrd ] ; then
23 . /etc/sysconfig/geninitrd
26 if is_yes $USEBSP ; then
27 [ ! -x /sbin/bsp ] && USEBSP="no"
35 modulefile=/etc/modules.conf
37 if [ "`uname -m`" = "ia64" ]; then
42 PRESCSIMODS="scsi_mod unknown sd_mod"
43 PREIDEMODS="ide-mod ide-probe ide-probe-mod ide-disk"
47 echo "usage: `basename $0` [--version] [-v] [-f] [--ifneeded] [--preload <module>]" 1>&2
48 echo " [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]" 1>&2
49 echo " [--fs=rom|ext2|cram] [--no-bsp] [--modules-conf=<modules.conf>" 1>&2
50 echo " <initrd-image> <kernel-version>" 1>&2
51 echo " (ex: `basename $0` /boot/initrd-2.2.5-15.img 2.2.5-15)" 1>&2
60 if [ "$name" != "build" ]; then
61 (cd $name;my_find $1/$name $2)
64 if [ -f $name -a "$name" = "$2" ]; then
72 echo $1|awk -F/ '{print substr($0, 0, length($0) - length($NF));}'
78 if [ "$(echo $modName | awk '{print(substr($0,1,1));}')" = "-" ]; then
80 modName="$(echo $modName | awk '{print(substr($0,2));}')"
83 if [ "$modName" = "pluto" ]; then
87 if [ "$modName" = "fcal" ]; then
92 fmPath="`(cd /lib/modules/$kernel; my_find . "$modName.o")`"
94 if [ ! -f "/lib/modules/$kernel/$fmPath" ]; then
95 if [ -n "$skiperrors" ]; then
99 # ignore the absence of the scsi modules
100 for n in $PRESCSIMODS; do
101 if [ "$n" = "$modName" ]; then
106 # ignore the absence of the ide modules
107 for n in $PREIDEMODS; do
108 if [ "$n" = "$modName" ]; then
113 echo "No module $modName found for kernel $kernel" 1>&2
117 # only need to add each module once
118 tmpFmPath="`echo "$fmPath"|awk -F/ '{for(i=1;i<NF;i++) { printf("%s\\\\/",$i); } { print $NF; }}'`"
119 if is_yes "`echo "$MODULES" | awk '/"'$tmpFmPath'"/ { print "yes"; }' `" ; then : ; else
120 MODULES="$MODULES $fmPath"
125 if [ "$#" != "2" ];then
126 echo "usage: inst <file> <destination>"
129 [ -n "$verbose" ] && echo "$1 -> $2"
134 /sbin/e2label $1 2> /dev/null
138 /sbin/tune2fs -l $1 2> /dev/null | awk -F: '/UUID:/ {gsub(" ",""); print $2}'
142 /usr/sbin/xfs_db -x -p xfs_admin -c label -r "$1"|awk -F= '{sub("^\"","", $2); sub("\"$", "", $2); print $2}'
147 /usr/sbin/xfs_db -x -p xfs_admin -c uuid -r "$1"|awk -F= '{print $2}'
151 eval `awk '/^[\t ]*#/ {next} {if ( $2 == "/" ) {print "rootdev=\"" $1 "\"\nrootFs=\"" $3 "\""}}' $fstab`
156 if [ ! -x /sbin/e2label ] ; then
157 echo "/sbin/e2label is missing" 1>&2
160 get_label="get_label_ext2"
163 if [ ! -x /usr/sbin/xfs_db ] ; then
164 echo "/usr/sbin/xfs_db is missing" 1>&2
167 get_label="get_label_xfs"
170 echo "LABEL on $rootFs in not supported by geninitrd" 1>&2
174 if [ ! -r /proc/partitions ] ; then
175 echo '/proc/partitions is not readable'; 1>&2
178 label=${rootdev#"LABEL="}
179 for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
180 if [ -r $dev ] && [ "$label" = "`$get_label $dev`" ] ; then
181 if [ -n "$verbose" ] ; then
182 echo "Using $dev as device for rootfs"
189 if [ "$rootdev_found" != 1 ] ; then
190 echo "geninitrd can't find real device for LABEL=$label" 1>&2
197 if [ ! -x /sbin/tune2fs ] ; then
198 echo "/sbin/tune2fs is missing" 1>&2
201 get_uuid="get_uuid_ext2"
204 if [ ! -x /usr/sbin/xfs_db ] ; then
205 echo "/usr/sbin/xfs_db is missing" 1>&2
208 get_label="get_uuid_xfs"
211 echo "UUID on $rootFs in not supported by geninitrd" 1>&2
215 if [ ! -r /proc/partitions ] ; then
216 echo '/proc/partitions is not readable'; 1>&2
219 uuid=${rootdev#"UUID="}
220 for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
221 if [ -r $dev ] && [ "$uuid" = "`$get_uuid $dev`" ] ; then
222 if [ -n "$verbose" ] ; then
223 echo "Using $dev as device for rootfs"
230 if [ "$rootdev_found" != 1 ] ; then
231 echo "geninitrd can't find real device for UUID=$uuid" 1>&2
239 while [ $# -gt 0 ]; do
245 if is_yes "`echo $1 | awk '/=/ { print "yes"; }'`" ; then
246 fstab="`echo $1 | awk -F= '{print $2;}'`"
253 modulefile="`echo $1 | awk -F= '{print $2;}'`"
260 raidtab="`echo $1 | awk -F= '{print $2;}'`"
267 if is_yes "`echo $1 | awk '/=/ { print "yes"; }'`" ; then
268 modname="`echo $1 | awk -F= '{print $2;}'`"
274 BASICMODULES="$BASICMODULES $modname"
277 echo "geninitrd: version $VERSION"
293 if is_yes "`echo $1 | awk '/=/ { print "yes"; }'`" ; then
294 modname="`echo $1 | awk -F= '{print $2;}'`"
299 PREMODS="$PREMODS $modname"
302 FS="`echo $1 | awk -F= '{print $2;}'`"
315 if [ -z "$target" ]; then
317 elif [ -z "$kernel" ]; then
328 if [ -z "$target" -o -z "$kernel" ]; then
334 if [ ! -x /sbin/mke2fs ]; then
335 echo "/sbin/mke2fs is missing" 1>&2
340 if [ ! -x /sbin/genromfs ]; then
341 echo "/sbin/genromfs is missing" 1>&2
346 if [ ! -x /sbin/mkcramfs ]; then
347 echo "/sbin/mkcramfs is missing" 1>&2
352 echo "Filesystem $FS on initrd is not supported" 1>&2
357 if [ -n "$img_vers" ]; then
358 target="$target-$kernel"
361 if [ -z "$force" -a -f "$target" ]; then
362 echo "$target already exists." 1>&2
366 if [ ! -d "/lib/modules/$kernel" ]; then
367 echo "/lib/modules/$kernel is not a directory." 1>&2
371 for n in $PREMODS; do
377 if is_yes "`echo "$rootdev" | awk '/^\/dev\/md/ { print "yes"; }'`"; then
378 eval `awk -v rootdev=$rootdev 'BEGIN {
394 if (found == "yes") {
399 if (found == "yes") {
400 if ($1 == "device") {
401 if ($2 ~ /\/dev\/(sd|scsi)/) {
404 if ($2 ~ /\/dev\/(hd|ide)/) {
408 if ($1 == "raid-level") {
415 print "raidfound=" found "\nADDSCSI=" addscsi "\nADDIDE=" addide "\nraidlevel=" raidlevel "\n";
417 if is_yes "$raidfound" ; then
420 findmodule "raid$raidlevel"
426 echo "raid level $number (in $raidtab) not recognized" 1>&2
435 if is_yes "$ADDSCSI" || is_yes "$(echo "$rootdev" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')" ; then
437 for n in $PRESCSIMODS; do
438 if [ "$n" = "unknown" ] ; then
439 if [ ! -f "$modulefile" ]; then
440 modulefile=/etc/conf.modules
442 if [ -f "$modulefile" ]; then
443 scsimodules="`awk '/scsi_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile| LC_ALL=C sort -u`"
444 for n in $scsimodules; do
445 # for now allow scsi modules to come from anywhere. There are some
446 # RAID controllers with drivers in block/
457 if is_yes "$ADDIDE" || is_yes "`echo "$rootdev" | awk '/^\/dev\/(hd|ide)/ { print "yes"; }'`" ; then
458 for n in $PREIDEMODS; do
463 if is_yes "`echo "$rootdev" | awk '/\/dev\/rd\// { print "yes"; }'`" ; then
467 if is_yes "`echo "$rootdev" | awk '/\/dev\/ida\// { print "yes"; }'`" ; then
468 findmodule "cpqarray"
471 if is_yes "`echo "$rootdev" | awk '/\/dev\/cciss\// { print "yes"; }'`" ; then
475 # check to see if we need to set up a loopback filesystem
476 if is_yes "`echo "$rootdev" | awk -F/ '{print($3);}' | awk '/loop/ { print "yes"; }'`" ; then
477 if [ ! -x /sbin/losetup ]; then
478 echo "losetup is missing"
481 key="^# $(echo $rootdev | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
482 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`" ; then
483 echo "The root filesystem is on a $rootdev, but there is no magic entry in $fstab" 1>&2
484 echo "for this device. Consult the geninitrd man page for more information" 1>&2
488 line="`awk '/'$key'/ { print $0; }' $fstab`"
489 loopDev="$(echo $line | awk '{print $3}')"
490 loopFs="$(echo $line | awk '{print $4}')"
491 loopFile="$(echo $line | awk '{print $5}')"
493 BASICMODULES="$BASICMODULES -loop"
494 findmodule "-$loopFs"
495 BASICMODULES="$BASICMODULES -${loopFs}"
496 # don't have any clue, how is this supposed to work,
499 else # Check for other filesystems
500 findmodule "-$rootFs"
503 for n in $BASICMODULES; do
507 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
508 if [ -n "$verbose" ]; then
509 echo "No modules are needed -- not building initrd image."
514 if [ -n "$verbose" ]; then
515 echo "Using modules: $MODULES"
518 MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
519 IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
520 MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
521 RCFILE="$MNTIMAGE/linuxrc"
523 if [ -f "$MNTIMAGE" ]; then
524 echo "$MNTIMAGE already exists. Remove it and try again" 1>&2
528 if [ -f "$IMAGE" ]; then
529 echo "$IMAGE already exists. Remove it and try again" 1>&2
533 if [ "$FS" = "ext2" ] ; then
534 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
536 # We have to "echo y |" so that it doesn't complain about $IMAGE not
537 # being a block device
538 echo y | mke2fs -F "$IMAGE" "$IMAGESIZE" >/dev/null 2>/dev/null
541 mount -o loop -t ext2 "$IMAGE" "$MNTPOINT"
547 mkdir -p "$MNTIMAGE"/{lib,bin,etc,dev,loopfs}
550 # We don't need this directory, so let's save space
551 rm -rf "$MNTPOINT"/lost+found
553 for MODULE in $MODULES; do
554 MODULEDIR="`my_dirname "$MODULE"`"
555 mkdir -p "$MNTIMAGE/lib/modules/$kernel/$MODULEDIR"
556 cp $verbose -a "/lib/modules/$kernel/$MODULE" "$MNTIMAGE/lib/modules/$kernel/$MODULE"
559 # mknod'ing the devices instead of copying them works both with and
561 mknod "$MNTIMAGE/dev/console" c 5 1
563 if is_yes "$USEBSP" ; then
564 s="$MNTIMAGE/etc/startup"
565 inst /sbin/bsp "$RCFILE"
566 echo "# autogenerated startup" > "$s"
569 for MODULE in $MODULES; do
570 module="`echo $MODULE | awk -F/ '{ $0=$NF } /.o$/ { $0=substr($0,1,length($0)-2); } { print $0; }'`"
571 options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"
573 if [ -n "$verbose" ]; then
574 /bin/echo -n "Loading module $module "
575 if [ -n "$options" ] ; then
576 echo "with options $options."
578 echo "without options."
581 echo "insmod /lib/modules/$kernel/$MODULE $options" >> "$s"
584 inst /bin/trash "$MNTIMAGE/bin/sh"
585 inst /sbin/insmod.static "$MNTIMAGE/bin/insmod"
587 mknod "$MNTIMAGE/dev/null" c 1 3
588 mknod "$MNTIMAGE/dev/ram" b 1 1
589 mknod "$MNTIMAGE/dev/systty" c 4 0
591 mknod "$MNTIMAGE/dev/tty$i" c 4 1
593 mknod "$MNTIMAGE/dev/zero" c 1 5
595 echo "#!/bin/sh" > "$RCFILE"
598 for MODULE in $MODULES; do
599 module="`echo $MODULE | awk -F/ '{ $0=$NF } /.o$/ { $0=substr($0,1,length($0)-2); } { print $0; }'`"
600 if [ -f "$modulefile" ] ; then
601 options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"
606 if [ -n "$verbose" ]; then
607 echo "Loading module $module with options $options"
609 echo "Add module $module to initrd."
610 echo "echo \"Loading $module module\"" >> "$RCFILE"
611 echo "insmod -k /lib/modules/$kernel/$MODULE $options" >> "$RCFILE"
615 if [ -n "$loopDev" ]; then
616 if [ ! -d /initrd ]; then
620 cp -a "$loopDev" "$MNTIMAGE/dev"
621 cp -a "$rootdev" "$MNTIMAGE/dev"
622 echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
623 echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
624 echo "echo Setting up loopback device $rootdev" >> $RCFILE
625 echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE"
629 if is_yes "$USERAIDSTART" ; then
630 if [ -x /sbin/raidstart.static ] ; then
631 [ -n "$verbose" ] && echo "Add raidstart to initrd"
632 mkdir -p "$MNTIMAGE/etc" || true
633 inst /sbin/raidstart.static "$MNTIMAGE/bin/raidstart"
634 inst "$raidtab" "$MNTIMAGE/etc/raidtab"
635 echo "echo \"Starting RAID\"" >> "$RCFILE"
636 echo "/bin/raidstart $rootdev" >> "$RCFILE"
638 echo "/sbin/raidstart.static is missing" 1>&2
645 (cd "$MNTIMAGE"; tar cf - .) | (cd "$MNTPOINT"; tar xf -)
652 genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
655 mkcramfs "$MNTPOINT" "$IMAGE"
658 echo "Filesystem $FS not supported by $0";
661 if is_yes "$COMPRESS" ; then
662 gzip -9 < "$IMAGE" > "$target"
664 cp -a "$IMAGE" "$target"
666 rm -rf "$MNTIMAGE" "$MNTPOINT" "$IMAGE"