#!/bin/sh

# geninitrd
#
#	by Jacek Konieczny <jajcus@pld-linux.org>
#
# based on mkinitrd from RedHat

RCSID='$Id: geninitrd 3865 2004-04-03 01:01:20Z arekm $'
PATH=/sbin:$PATH
export PATH

VERSION="`echo "$RCSID"|awk '{print $3}'`"

. /etc/rc.d/init.d/functions

COMPRESS="yes"
# INITRDFS is set later (catch obsoleted FS option)
#INITRDFS="rom"
USERAIDSTART="yes"
USEMDADMSTATIC="no"
USEINSMODSTATIC="no"
uselvm="no"
# it should be safe to remove scsi_mod from here, but I'm not sure...
PRESCSIMODS="-scsi_mod unknown -sd_mod"
PREIDEMODS="-ide-core unknown -ide-detect -ide-disk"
PREIDEMODSOLD="-ide-probe -ide-probe-mod -ide-disk"
target=""
kernel=""
force=""
verbose=""
MODULES=""
img_vers=""
modulefile="/etc/modules.conf"
raidtab="/etc/raidtab"
fstab="/etc/fstab"
insmod="insmod"
modext=".o"
rootdev_nr=0
# default bootsplash is off, if it have to be on, install bootsplash package
BOOT_SPLASH=no

usage () {
	echo "usage: `basename $0` [--version] [-v] [-f] [--ifneeded] [--preload <module>]" 
	echo "       [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]" 
	echo "       [--initrdfs=rom|ext2|cram] [--modules-conf=<modules.conf>]" 
	echo "       [--with-raidstart] [--without-raidstart] [--with-insmod-static]" 
	echo "       [--without-bootsplash] [--lvmtoolsversion=1|2]" 
	echo "       <initrd-image> <kernel-version>" 
	echo "       (ex: `basename $0` /boot/initrd-2.2.5-15.img 2.2.5-15)" 
	exit 1
}


my_dirname() {
	echo $1|awk -F/ '{print substr($0, 0, length($0) - length($NF));}'
}

find_depmod () {
	typeset mods module f level depfile first

	depfile=/lib/modules/$kernel/modules.dep
	
	if [ -f $depfile ] ; then
		: ok
	else
		echo "Error: no $depfile ! Run depmod and rerun geninitrd." 1>&2
		exit 1
	fi
	
	# prepend / if no path given, append $modext.gz if not given,
	# quote /
	origmodule="$2"
	module=$(echo "$2" | \
		 awk '/\// {print;next} {print "/" $0}' | \
		 awk '/\./ {print;next} {print $0 "'$modext'.gz"}' |
		 awk '{gsub("/","\\/");print}')
	mods=$(awk '
BEGIN { here = 0 }
/'"$module"':(.*)/ { gsub(/:/," "); gsub(/\\/," "); print; here = 1; next }
/:/ { here = 0 }
/(.*)/ { gsub(/\\/," "); if (here) print }
' $depfile | xargs)

	# fallback to $modext
	if [ "$mods" = "" ] ; then
	    module=$(echo "$module" | \
		    awk '{gsub("\'$modext'\.gz$","\'$modext'",$0);print}')
	fi 
	mods=$(awk '
BEGIN { here = 0 }
/'"$module"':(.*)/ { gsub(/:/," "); gsub(/\\/," "); print; here = 1; next }
/:/ { here = 0 }
/(.*)/ { gsub(/\\/," "); if (here) print }
' $depfile | xargs)

	if [ "$mods" = "" ] ; then
		if [ "$1" != silent ] ; then
			echo "$origmodule: module not found in $depfile" 1>&2
		fi
		if ! is_no "$EXIT_IF_MISSING" ; then 
			exit 1
		else
			echo "If $origmodule isn't compiled in kernel then this initrd may not start your system". 1>&2
		fi
	fi
	
	level=$3
	if [ "$level" = "" ] ; then
		level=0
	fi
	level=$(awk "BEGIN{print $level + 1}")
	if [ $level -gt 20 ] ; then
		echo "$origmodule: cycle in $depfile" 1>&2
		exit 1
	fi
	
	first=
	for f in $mods ; do
		if [ "$first" = "" ] ; then
			first=$f
		else
			find_depmod $1 $f $level
		fi
	done
	
	echo $first
}

addmodule() {
	fmPath=$1
	skiperrors=$2

	if [ ! -f "/lib/modules/$kernel/$fmPath" ]; then
		if [ -n "$skiperrors" ]; then
			return
		fi

		echo "module $fmPath present in modules.dep, but not in filesystem (kernel = $kernel)" 1>&2
		exit 1
	fi

	# only need to add each module once
	# quote /
	tmpFmPath=$(echo $fmPath | awk '{gsub(/\//,"\\/");print}')
	if echo "$MODULES" | awk '/'"$tmpFmPath"'/ {exit 1}' ; then
		MODULES="$MODULES $fmPath"
	fi
}

findmodule() {
	skiperrors=""
	modName=$1

	if [ "$(echo $modName | awk '{print(substr($0,1,1));}')" = "-" ]; then
		skiperrors=1
		modName="$(echo $modName | awk '{print(substr($0,2));}')"
	fi

	# what's that?
	if [ "$modName" = "pluto" ]; then
		findmodule fc4
		findmodule soc
	fi
	if [ "$modName" = "fcal" ]; then
		findmodule fc4
		findmodule socal
	fi

	if [ -n "$skiperrors" ]; then
		allModulesToFind=`find_depmod silent $modName`
	else
		allModulesToFind=`find_depmod normal $modName`
		if [ $? != 0 ] ; then
			exit 1
		fi
	fi
	
	for mod in $allModulesToFind ; do
		mod=$(echo $mod | \
		      awk '{sub(/^\/lib\/modules\/[^\/]*\//,"");print}')
		addmodule $mod "$skiperrors"
	done
}

inst() {
	if [ "$#" != "2" ];then
		echo "usage: inst <file> <destination>"
		return
	fi
	[ -n "$verbose" ] && echo "$1 -> $2"
	cp "$1" "$2"
}

get_label_ext2 () {
	/sbin/e2label $1 2> /dev/null
}

get_uuid_ext2 () {
	/sbin/tune2fs -l $1 2> /dev/null | awk -F: '/UUID:/ {gsub(" ",""); print $2}'
}

get_label_xfs () {
	/usr/sbin/xfs_db -x -p xfs_admin -c label -r "$1"|awk -F= '{sub("^\"","", $2); sub("\"$", "", $2); print $2}'

}

get_uuid_xfs () {
	/usr/sbin/xfs_db -x -p xfs_admin -c uuid -r "$1"|awk -F= '{print $2}'
}

find_root() {
	eval `awk '/^[\t ]*#/ {next} {if ( $2 == "/" ) {print "rootdev=\"" $1 "\"\nrootFs=\"" $3 "\""}}' $fstab`
	case $rootdev in
	LABEL=*)
		if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
			rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
			if [ -n "$rootdev2" ] ; then
				rootdev=$rootdev2
				rootdev_found=1
			fi
		fi
		if [ "$rootdev_found." != "1." ] ; then
			case $rootFs in
			ext2|ext3)
				if [ ! -x /sbin/e2label ] ; then
					echo "/sbin/e2label is missing" 1>&2
					exit 1
				fi
				get_label="get_label_ext2"
				;;
			xfs)
				if [ ! -x /usr/sbin/xfs_db ] ; then
					echo "/usr/sbin/xfs_db is missing" 1>&2
					exit 1
				fi
				get_label="get_label_xfs"
				;;
			*)
				echo "LABEL on $rootFs in not supported by geninitrd" 1>&2
				exit 1
				;;
			esac
			if [ ! -r /proc/partitions ] ; then
				echo '/proc/partitions is not readable'; 1>&2
				exit 1
			fi
			label=${rootdev#"LABEL="}
			for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
				if [ -r $dev ] && [ "$label" = "`$get_label $dev`" ] ; then
					if [ -n "$verbose" ] ; then
						echo "Using $dev as device for rootfs"
					fi
					rootdev=$dev
					rootdev_found=1
					break
				fi
			done
			if [ "$rootdev_found." != "1." ] ; then
				echo "geninitrd can't find real device for LABEL=$label" 1>&2
				exit 1
			fi
		fi
		;;
	UUID=*)
		if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
			rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
			if [ -n "$rootdev2" ] ; then
				rootdev=$rootdev2
				rootdev_found=1
			fi
		fi
		if [ "$rootdev_found." != "1." ] ; then
			case $rootFs in
			ext2|ext3)
				if [ ! -x /sbin/tune2fs ] ; then
					echo "/sbin/tune2fs is missing" 1>&2
					exit 1
				fi
				get_uuid="get_uuid_ext2"
				;;
			xfs)
				if [ ! -x /usr/sbin/xfs_db ] ; then
					echo "/usr/sbin/xfs_db is missing" 1>&2
					exit 1
				fi
				get_label="get_uuid_xfs"
				;;
			*)
				echo "UUID on $rootFs in not supported by geninitrd" 1>&2
				exit 1
				;;
			esac
			if [ ! -r /proc/partitions ] ; then
				echo '/proc/partitions is not readable'; 1>&2
				exit 1
			fi
			uuid=${rootdev#"UUID="}
			for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
				if [ -r $dev ] && [ "$uuid" = "`$get_uuid $dev`" ] ; then
					if [ -n "$verbose" ] ; then
						echo "Using $dev as device for rootfs"
					fi
					rootdev=$dev
					rootdev_found=1
					break
				fi
			done
			if [ "$rootdev_found" != 1 ] ; then
				echo "geninitrd can't find real device for UUID=$uuid" 1>&2
				exit 1
			fi
		fi
		;;
	esac
	rootdev1=${rootdev}
}

find_modules_softraid() {
        if [ -f /etc/mdadm.conf ] ; then
	    [ -n "$verbose" ] && echo "Finding RAID details using mdadm for rootdev=$1"
	    eval `/sbin/mdadm --examine  --scan --config=/etc/mdadm.conf | \
	    	  awk -v rootdev="$1" '
	BEGIN {
		found = "no";
		dev_list = "";
		raidlevel = ""
		rootdev_devfs = rootdev;
		if (rootdev ~ /\/dev\/md\/[0-9]/) {
		    gsub(/\/dev\/md\//,"/dev/md",rootdev_devfs);
		}
	}

	/^ARRAY/ {
	    if (($2 == rootdev) || ($2 == rootdev_devfs)) {
		raidlevel=$3;
		gsub(/level=/,NUL,raidlevel);
		if (raidlevel ~ /^raid[0-5]/) {
		    gsub(/raid/,NUL,raidlevel);
		};
		found="yes";
		getline x;
		if (x ~ /devices=/) {
			dev_list = x;
			gsub("devices=", NUL, dev_list);
			gsub(",", " ", dev_list);
		}
	    }
	}
 
	END {
		print "raidfound=" found;
		print "raidlevel=" raidlevel;
		print "dev_list=\"" dev_list "\"";
	}'`
    	fi
	if [ "$raidfound" != "yes" ]; then
	    [ -n "$verbose" ] && echo "Finding RAID details using raidtab for rootdev=$1"
	    eval `awk -v rootdev="$1" 'BEGIN {
	found="no";
	dev_list="";
	rootdev_devfs = rootdev;
	if (rootdev ~ /\/dev\/md\/[0-9]/) {
	    gsub(/\/dev\/md\//,"/dev/md",rootdev_devfs);
	}
}

{
	gsub("\t"," ");
	gsub("  +", " ");
	gsub("^ ","");
	if (/^[\t ]*#/)
		next;
	if (/^raiddev/) {
		if (($2 == rootdev) || ($2 == rootdev_devfs)) {
			found="yes";
		} else {
			if (found == "yes") {
				exit 0;
			};
		};
	};
	if (found == "yes") {
		if ($1 == "device") {
			dev_list = dev_list " " $2;
		};
		if ($1 == "raid-level") {
			raidlevel=$2;
		}
	};
}

END {
	print "raidfound=" found "\nraidlevel=" raidlevel "\ndev_list=\"" dev_list "\"\n";
}' $raidtab `
	fi
	
	if is_yes "$raidfound" ; then
		case "$raidlevel" in
		[0145])
			findmodule "raid$raidlevel"
			;;
		linear)
			findmodule "linear"
			;;
		*)
			echo "raid level $number (in $raidtab) not recognized" 1>&2
			;;
		esac
	fi

	rootdev_nr=$(( $rootdev_nr + 1 ))
	eval "rootdev${rootdev_nr}=\"$1\""
	eval "dev_list${rootdev_nr}=\"${dev_list}\""
	
	for device in $dev_list; do
		find_modules_for $device
	done
}

find_modules_scsi() {
	for n in $PRESCSIMODS; do
		if [ "X$n" = "Xunknown" ] ; then
		    	if [ "$pack_version" -ge "002005" ]; then
			    	modulefile=/etc/modprobe.conf
			fi
			if [ ! -f "$modulefile" ]; then
				modulefile=/etc/conf.modules
			fi
			if [ -f "$modulefile" ]; then
				scsimodules="`awk '/scsi_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
				for n in $scsimodules; do
			# for now allow scsi modules to come from anywhere. There are some
			# RAID controllers with drivers in block
					findmodule "$n"
				done
			fi
		else
			findmodule "$n"
		fi
	done
}

find_modules_ide() {
    	typeset rootdev

    	rootdev="$(echo "$1" | awk ' { gsub(/\/dev\//,NUL); gsub(/[0-9].*/, NUL); print $0 } ')"
    	if [ "$pack_version_long" -lt "002004021" ]; then
	    [ -n "$verbose" ] && echo "Finding IDE modules for kernels <= 2.4.20"
	    for n in $PREIDEMODSOLD; do
		findmodule "$n"
	    done
	else
	    tryauto=1
	    for n in $PREIDEMODS; do
		if [ "X$n" = "Xunknown" ] ; then
		    if [ "$pack_version" -ge "002005" ]; then
			    	modulefile=/etc/modprobe.conf
		    fi
    		    if [ ! -f "$modulefile" ]; then
			modulefile=/etc/conf.modules
    		    fi
    		    if [ -f "$modulefile" ]; then
			 [ -n "$verbose" ] && echo "Finding IDE modules using ide_hostadapter"
			idemodules="`awk '/ide_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
			for na in $idemodules; do
			    tryauto=0;
			    findmodule "$na"
			done
		    fi

		    if [ "$tryauto" -eq 1 ]; then
		      if [ -r /usr/share/pci-database/ide.pci -a -r /proc/bus/pci/devices  ]; then
			[ -n "$verbose" ] && echo "Finding IDE modules using PCI ID database"
			if is_yes "${ide_only_root}"; then
			  if [ -f /sys/block/${rootdev}/device/../../vendor -a -f /sys/block/${rootdev}/device/../../device ] ; then
			    vendorid="$(awk ' { gsub(/0x/,NUL); print $0 } ' /sys/block/${rootdev}/device/../../vendor)"
			    deviceid="$(awk ' { gsub(/0x/,NUL); print $0 } ' /sys/block/${rootdev}/device/../../device)"
			    searchpciid="${vendorid}${deviceid}"
			  elif [ -f /proc/ide/${rootdev}/../config ]; then
			    searchpciid="$(awk ' /pci bus/ { print $7$9 } ' /proc/ide/${rootdev}/../config)"
			  fi
		        fi
			if [ -z "${searchpciid}" ]; then
			    searchpciid="$(awk ' { print $2 } ' /proc/bus/pci/devices)"
			fi
			idemodules=""
		    	for nb in $searchpciid; do
	    		    eval `awk -v pciid="$nb" 'BEGIN {
}
		
{
	gsub("\t"," ");
	gsub("  +", " ");
	gsub("^ ","");
	if (/^[\t ]*#/)
		next;
	compmod = $1 "";	# make sure comparison type will be string
				# cause pci IDs are hexadecimal numeric
	if (compmod == pciid) {
		module=$2;
#		min_kernel=$3;  # now in ide.pci $3,$4 = vendor and device name
#		max_kernel=$4;  #
		exit 0;
	}
}

END {
	print "module=" module "\nmin_kernel=" min_kernel "\nmax_kernel=\"" max_kernel "\"\n";
}' /usr/share/pci-database/ide.pci`
			    [ -n "$module" ] && idemodules="$idemodules $module"
	    		done
			if is_yes "$(awk ' /ide=reverse/ { print "yes" } ' /proc/cmdline)"; then
			    	new_idemodules=""
			    	for nc in idemodules; do
				    new_idemodules="$nc $new_idemodules"
				done
    				idemodules="${new_idemodules}"
			fi
			if [ -z "$idemodules" ]; then
			    echo "WARNING: rootfs on IDE device but no related modules found, loading ide-generic."
			    idemodules="ide-generic"
			fi
    			for nd in $idemodules; do
    			    findmodule "-$nd"
    			done
		      else
			 echo -n "WARNING: "
			 [ -r /usr/share/pci-database/ide.pci ] || echo -n "/usr/share/pci-database/ide.pci missing."
			 [ -r /proc/bus/pci/devices ] || echo -n "/proc/bus/pci/devices missing."
			 echo "Automatic IDE modules finding not available."
		      fi
		    fi
		else
    			findmodule "$n"
		fi
	    done
	fi
}

find_modules_for() {
        if [ -z "$1" ]; then
	    	echo "ERROR: no argument passed to find_modules_for()" >&2
		exit
	elif is_yes "`echo "$1" | awk '/^\/dev\/md/ { print "yes"; }'`"; then
		find_modules_softraid "$1"
	elif is_yes "$(echo "$1" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')" ; then
		find_modules_scsi
	elif is_yes "`echo "$1" | awk '/^\/dev\/(hd|ide)/ { print "yes"; }'`" ; then
		find_modules_ide "$1"
	elif is_yes "`echo "$1" | awk '/\/dev\/rd\// { print "yes"; }'`" ; then
		findmodule "DAC960"
	elif is_yes "`echo "$1" | awk '/\/dev\/ida\// { print "yes"; }'`" ; then
		findmodule "cpqarray"
	elif is_yes "`echo "$1" | awk '/\/dev\/cciss\// { print "yes"; }'`" ; then
		findmodule "cciss"
	elif is_yes "`echo "$1" | awk '/\/dev\/ataraid\// { print "yes"; }'`"; then
	    	find_modules_ide
		findmodule "ataraid"
		ataraidmodules="`awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
		if -n "$ataraidmodules" ; then
			# FIXME: think about modules compiled in kernel
			echo "ERROR: ataraid_hostadapter alias not defined in $modulefile !"
			echo "Please set it and run $0 again."
			exit 1
		fi
		for n in $ataraidmodules; do
			findmodule "$n"
		done
	# check to see if we need to set up a loopback filesystem
	elif is_yes "`echo "$1" | awk -F/ '{print($3);}' | awk '/loop/ { print "yes"; }'`" ; then
		echo "Sorry, root on loop device isn't supported." 1>&2
		exit 1
		# TODO: rewrite for bsp and make nfs ready
		if [ ! -x /sbin/losetup ]; then
			echo "losetup is missing"
			exit 1
		fi
		key="^# $(echo $1 | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
		if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`" ; then
			echo "The root filesystem is on a $1, but there is no magic entry in $fstab" 1>&2
			echo "for this device. Consult the geninitrd man page for more information" 1>&2
			exit 1
		fi

		line="`awk '/'$key'/ { print $0; }' $fstab`"
		loopDev="$(echo $line | awk '{print $3}')"
		loopFs="$(echo $line | awk '{print $4}')"
		loopFile="$(echo $line | awk '{print $5}')"

		BASICMODULES="$BASICMODULES -loop"
		findmodule "-$loopFs"
		BASICMODULES="$BASICMODULES -${loopFs}"
		# don't have any clue, how is this supposed to work
	elif ls -l "$1" 2> /dev/null | awk '{if (/^b/) { if ($5 == "58,") { exit 0; } else { exit 1; } } else { exit 1; }}' || /sbin/lvdisplay "$1" > /dev/null 2>&1 ; then
		if [ ! -f /sbin/initrd-lvm -o ! -x /sbin/lvdisplay -o ! -x /sbin/pvdisplay ] ; then
			echo "ERROR: root on LVM but /sbin/initrd-lvm, /sbin/lvdisplay and /sbin/pvdisplay not found." >&2
			echo "Please install lvm(2) and lvm(2)-initrd package and rerun $0." >&2
			exit 1
		fi
		if [ -z "$LVMTOOLSVERSION" ] ; then 
		    LVMTOOLSVERSION=$(/sbin/initrd-lvm vgchange --version 2>/dev/null|head -n 1|awk '{gsub("vgchange: Logical Volume Manager ",NIL); gsub("LVM version:     ",NIL); gsub(/\..*/,NIL); print $1}')
		    if [ -z "$LVMTOOLSVERSION" ] ; then
			    echo "ERROR: Can't determine LVM tools version. Please set LVMTOOLSVERSION" >&2
			    echo "and rerun $0." >&2
			    exit 1
		    fi
		fi
		if [ -z "$PVDEVICES" ] ; then
    		    VGVOLUME=$(/sbin/lvdisplay -c "$1" 2> /dev/null | awk -F":" ' { print $2 } ')
    		    PVDEVICES=$(/sbin/pvdisplay -c | awk -F":" -v vg="$VGVOLUME" ' BEGIN { devices="" } { if ($2 == vg) { devices = devices " " $1 } } END { print devices } ')
		fi
		if [ -n "$PVDEVICES" ] ; then
			for device in $PVDEVICES; do
				find_modules_for $device
			done
		else
		    	echo "ERROR: I wasn't able to find PV (via lvdisplay and pvdisplay)." >&2 
			echo "You can try to set PVDEVICES in /etc/sysconfig/geninitrd." >&2
			exit 1
		fi
		if [ "$LVMTOOLSVERSION" = "2" ]; then
    		    findmodule "-dm-mod"
		elif [ "$LVMTOOLSVERSION" = "1" ]; then
    		    findmodule "-lvm"
    		    findmodule "-lvm-mod"
		else
			echo "ERROR: LVM version $LVMTOOLSVERSION is not supported yet." >&2
			exit 1
		fi
		uselvm="yes"
	fi
}

if [ -r /etc/sysconfig/geninitrd ] ; then
	. /etc/sysconfig/geninitrd
fi

if [ -r /etc/sysconfig/bootsplash ] ; then
	. /etc/sysconfig/bootsplash
fi

if [ "x" = "x$INITRDFS" ] ; then
	if [ "x" = "x$FS" ] ; then
		# default value
		INITRDFS="rom"
	else
		echo "Warning: FS configuration options is obsoleted. Use INITRDFS instead" 1>&2
		INITRDFS="$FS"
	fi
fi

if [ ! -x /bin/initrd-busybox ] ; then 
	echo "/bin/initrd-busybox is missing !"
	exit 1
fi
if [ "`uname -m`" = "ia64" ]; then
	IMAGESIZE=3000
else
	IMAGESIZE=1500
fi
while [ $# -gt 0 ]; do
	case $1 in
	--fstab=*)
		fstab="`echo $1 | awk -F= '{print $2;}'`"
		;;
	--fstab)
		fstab="$2"
		shift
		;;
	--modules-conf=*)
		modulefile="`echo $1 | awk -F= '{print $2;}'`"
		;;
	--modules-conf)
		modulefile="$2"
		shift
		;;
	--raidtab=*)
		raidtab="`echo $1 | awk -F= '{print $2;}'`"
		;;
	--raidtab)
		raidtab="$2"
		shift
		;;
	--use-raidstart|--with-raidstart)
		USERAIDSTART="yes"
		;;
	--without-raidstart)
		USERAIDSTART="no"
		;;
	--use-insmod-static|--with-insmod-static)
		USEINSMODSTATIC="yes"
		;;
	--without-insmod-static)
		USEINSMODSTATIC="no"
		;;
	--without-bootsplash)
		BOOT_SPLASH="no"
		;;
	--lvmtoolsversion=|--lvmversion=)
		LVMTOOLSVERSION="`echo $1 | awk -F= '{print $2;}'`"
		;;
	--lvmtoolsversion|--lvmversion)
		LVMTOOLSVERSION="$2"
		shift
		;;
	--with=*)
		BASICMODULES="$BASICMODULES `echo $1 | awk -F= '{print $2;}'`"
		;;
	--with)
		BASICMODULES="$BASICMODULES $2"
		shift
		;;
	--version)
		echo "geninitrd: version $VERSION"
		exit 0
		;;
	-v)
		verbose=-v
		;;
	--nocompress)
		COMPRESS="no"
		;;
	--ifneeded)
		ifneeded=1
		;;
	-f)
		force=1
		;;
	--preload=*)
		PREMODS="$PREMODS `echo $1 | awk -F= '{print $2;}'`"
		;;
	--preload)
		PREMODS="$PREMODS $2"
		shift
		;;
	--fs=*)
		echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
		INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
		;;
	--fs)
		echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
		INITRDFS="$2"
		shift
		;;
	--initrdfs=*)
		INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
		;;
	--initrdfs)
		INITRDFS="$2"
		shift
		;;
	--image-version)
		img_vers=yes
		;;
	--ide-only-root)
		ide_only_root="yes"
		;;
	*)
		if [ -z "$target" ]; then
			target="$1"
		elif [ -z "$kernel" ]; then
			kernel="$1"
		else
			usage
		fi
		;;
	esac

	shift
done

if [ -z "$target" -o -z "$kernel" ]; then
	usage
fi

pack_version="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d",$1,$2)}'`"
pack_version_long="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}'`"

if [ "$pack_version" -ge "002005" ] ; then
	modext=".ko"
	insmod="insmod"
fi

if is_yes "$USEINSMODSTATIC" ; then
	insmod="insmod.static"
	INSMOD="/sbin/insmod.static"
	if [ "$pack_version" -lt "002005" -a -f /sbin/insmod.static.modutils ] ; then
		INSMOD="/sbin/insmod.static.modutils"
	fi
	if [ ! -f "$INSMOD" ] ; then
		echo "insmod.static requested but /sbin/insmod.static not found !" >&2
		exit 1
	fi
fi

case "$INITRDFS" in
	ext2)
		if [ ! -x /sbin/mke2fs ]; then
			echo "/sbin/mke2fs is missing" 1>&2
			exit 1
		fi
		;;
	rom)
		if [ ! -x /sbin/genromfs ]; then
			echo "/sbin/genromfs is missing" 1>&2
			exit 1
		fi
		;;
	cram)
		if [ ! -x /sbin/mkcramfs ]; then
			echo "/sbin/mkcramfs is missing" 1>&2
			exit 1
		fi
		;;
	*)
		echo "Filesystem $INITRDFS on initrd is not supported" 1>&2
		exit 1
		;;
esac

if [ -n "$img_vers" ]; then
	target="$target-$kernel"
fi

if [ -z "$force" -a -f "$target" ]; then
	echo "$target already exists." 1>&2
	exit 1
fi

if [ ! -d "/lib/modules/$kernel" ]; then
	echo "/lib/modules/$kernel is not a directory." 1>&2
	exit 1
fi

for n in $PREMODS; do
	findmodule "$n"
done

# allow forcing loading SCSI and/or IDE modules
if is_yes "$ADDSCSI" ; then
	find_modules_scsi
fi

if is_yes "$ADDIDE" ; then
	find_modules_ide
fi

find_root

find_modules_for "$rootdev1"

findmodule "-$rootFs"

for n in $BASICMODULES; do
	findmodule "$n"
done


if [ -n "$ifneeded" -a -z "$MODULES" ]; then
	if [ -n "$verbose" ]; then
		echo "No modules are needed -- not building initrd image."
	fi
	exit 0
fi

if [ -n "$verbose" ]; then
	echo "Using modules: $MODULES"
fi

MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
RCFILE="$MNTIMAGE/linuxrc"

if [ -f "$MNTIMAGE" ]; then
	echo "$MNTIMAGE already exists. Remove it and try again" 1>&2
	exit 1
fi

if [ -f "$IMAGE" ]; then
	echo "$IMAGE already exists. Remove it and try again" 1>&2
	exit 1
fi

if [ "$INITRDFS" = "ext2" ] ; then
	dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null

	# We have to "echo y |" so that it doesn't complain about $IMAGE not
	# being a block device
	echo y | mke2fs -F "$IMAGE" "$IMAGESIZE" >/dev/null 2>/dev/null

	mkdir -p "$MNTPOINT"
	mount -o loop -t ext2 "$IMAGE" "$MNTPOINT"
else
	mkdir -p "$MNTPOINT"
fi


mkdir -p "$MNTIMAGE"/{lib,bin,etc,dev,loopfs,var}


# We don't need this directory, so let's save space
rm -rf "$MNTPOINT"/lost+found

for MODULE in $MODULES; do
	MODULEDIR="`my_dirname "$MODULE"`"
	mkdir -p "$MNTIMAGE/lib/modules/$kernel/$MODULEDIR"
	cp $verbose -a "/lib/modules/$kernel/$MODULE" "$MNTIMAGE/lib/modules/$kernel/$MODULE"
	gunzip "$MNTIMAGE/lib/modules/$kernel/$MODULE" 2> /dev/null
done

# mknod'ing the devices instead of copying them works both with and
# without devfs...
mknod "$MNTIMAGE/dev/console" c 5 1
mknod "$MNTIMAGE/dev/null" c 1 3
mknod "$MNTIMAGE/dev/zero" c 1 5

s="$RCFILE"

inst /bin/initrd-busybox "$MNTIMAGE/bin/sh"
ln -s sh "$MNTIMAGE/bin/busybox"

if is_yes "$USEINSMODSTATIC" ; then
	inst "$INSMOD" $MNTIMAGE/bin/insmod.static
fi

cat > "$s" <<EOF
#! /bin/sh

set -x
EOF
chmod 755 "$s"

for MODULE in $MODULES; do
	MODULE2="`my_dirname "$MODULE"`"
	NAME2=`basename "$MODULE" .gz`
	MODULE2=$MODULE2/$NAME2
	module="`echo $MODULE | awk -F/ '{ $0=$NF } /'$modext'$/ { $0=substr($0,1,length($0)-length("'$modext'")); } { print $0; }'`"
	options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"

	if [ -n "$verbose" ]; then
		/bin/echo -n "Loading module $module "
		if [ -n "$options" ] ; then
			echo "with options $options."
		else
			echo "without options."
		fi
	fi
	echo "$insmod /lib/modules/$kernel/$MODULE2 $options" >> "$s"
done

# TODO: rewrite for busybox
#if [ -n "$loopDev" ]; then
#	if [ ! -d /initrd ]; then
#		mkdir /initrd
#	fi
#
#	cp -a "$loopDev" "$MNTIMAGE/dev"
#	cp -a "$rootdev1" "$MNTIMAGE/dev"
#	echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
#	echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
#	echo "echo Setting up loopback device $rootdev1" >> $RCFILE
#	echo "losetup $rootdev1 /loopfs$loopFile" >> "$RCFILE"
#fi

if is_yes "$USERAIDSTART" && is_yes "$raidfound" ; then
	[ -n "$verbose" ] && echo "Setting up mdadm..."
	
	if [ ! -x /sbin/initrd-mdadm -a ! -x /sbin/initrd-mdassemble ] ; then
	    	echo "/sbin/initrd-mdadm and /sbin/initrd-mdassemble are missing !"
		exit 1
	fi

	if [ -x /sbin/initrd-mdassemble ] ; then
    	    do_mdassemble=1
	    inst /sbin/initrd-mdassemble "$MNTIMAGE/bin/mdassemble"
	else
	    do_mdassemble=0
	    inst /sbin/initrd-mdadm "$MNTIMAGE/bin/mdadm"
	fi
	    
	# LVM on RAID case
        do_md0=1
	for nr in `seq 1 $rootdev_nr`; do
	    eval cr_rootdev="\$rootdev${nr}"
	    eval cr_dev_list="\$dev_list${nr}"
	    [ -n "$verbose" ] && echo "Setting up array ($cr_rootdev = $cr_dev_list)"

	    [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
	    
	    if [ "$do_mdassemble" -eq 1 ] ; then
	    	echo "DEVICE $cr_dev_list" >> "$MNTIMAGE/etc/mdadm.conf"
	    	echo "ARRAY $cr_rootdev" >> "$MNTIMAGE/etc/mdadm.conf"
	    	echo "mdassemble" >> "$s"
    	    else
    		echo "mdadm --assemble $cr_rootdev $cr_dev_list" >> "$s"
    	    fi

	    for f in $cr_dev_list $cr_rootdev ; do
		# mkdir in case of devfs name
		mkdir -p $MNTIMAGE/`my_dirname $f`
		[ -n "$verbose" ] && echo "copying $f"
		# this works fine with and without devfs
		cp -HR $f $MNTIMAGE/$f
	    done
	done

	# needed to determine md-version
	if [ "$do_md0" -eq 1 ] ; then
		mknod $MNTIMAGE/dev/md0 b 9 0
	fi
fi

if is_yes "$uselvm" ; then
	[ -n "$verbose" ] && echo "Adding LVM support to initrd"
	inst /sbin/initrd-lvm $MNTIMAGE/bin/lvm
	mkdir $MNTIMAGE/tmp
	mkdir $MNTIMAGE/proc
	echo "mount -t proc none /proc" >> "$s"
	echo "mount -t devfs none /dev" >> "$s"
	echo "mount -t tmpfs none /tmp" >> "$s"
	if [ "$LVMTOOLSVERSION" = "1" ] ; then
		echo "lvm vgscan -T" >> "$s"
		echo "lvm vgchange -T -a y $VGVOLUME" >> "$s"
	else
		echo "LVM_SYSTEM_DIR=/tmp lvm vgscan --mknodes --ignorelockingfailure" >> "$s"
		echo "LVM_SYSTEM_DIR=/tmp lvm vgchange --ignorelockingfailure -a y $VGVOLUME" >> "$s"
	fi
	echo "umount /tmp" >> "$s"
	# fail to umount
	echo "umount /dev" >> "$s"
	echo "umount /proc" >> "$s"
fi

chmod +x "$RCFILE"

(cd "$MNTIMAGE"; tar cf - .) | (cd "$MNTPOINT"; tar xf -)

case "$INITRDFS" in
	ext2)
		umount "$IMAGE"
		;;
	rom)
		genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
		;;
	cram)
		mkcramfs "$MNTPOINT" "$IMAGE"
		;;
	*)
		echo "Filesystem $INITRDFS not supported by $0";
esac

if is_yes "$COMPRESS" ; then
	gzip -9 < "$IMAGE" > "$target"
else
	cp -a "$IMAGE" "$target"
fi

if is_yes "$BOOT_SPLASH"; then
	if [ ! -x /bin/splash.bin ]; then
		echo "Failed to execute /bin/splash.bin. Is bootsplash package installed?" 1>&2
	elif [ -z "$THEME" ]; then
		echo "Please configure your /etc/sysconfig/bootsplash first." 1>&2
		echo "Generating bootsplashes skipped." 1>&2
	else
	    if [ -n "$BOOT_SPLASH_RESOLUTIONS" ]; then
		for res in $BOOT_SPLASH_RESOLUTIONS; do
			if [ -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
				/bin/splash.bin -s -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> "$target" && \
				[ -n "$verbose" ] && echo "Added $res $THEME theme to initrd."
			else
				echo "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped" 1>&2
			fi
		done
	     else
		 echo "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash." 1>&2
		 echo "Not adding bootsplash to initrd." 1>&2
	     fi
	fi
fi

rm -rf "$MNTIMAGE" "$MNTPOINT" "$IMAGE"
