[readonly/geninitrd: 510/1068] - move functions start of the script

draenog draenog at pld-linux.org
Sat Nov 2 19:55:16 CET 2013


commit 82b2dba24b5a19e915570a4e8ebe1e373ab9d0d6
Author: Elan Ruusamäe <glen at pld-linux.org>
Date:   Mon Nov 5 21:05:52 2007 +0000

    - move functions start of the script
    
    svn-id: @8993

 geninitrd | 1402 ++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 701 insertions(+), 701 deletions(-)
---
diff --git a/geninitrd b/geninitrd
index 8da70f0..587c5de 100755
--- a/geninitrd
+++ b/geninitrd
@@ -79,19 +79,6 @@ have_multipath=no
 # dm-multipath id which is used for rootfs
 MULTIPATH_ID=
 
-if [ -f /etc/udev/udev.conf -a -x /sbin/initrd-udevd ]; then
-	USE_UDEV=yes
-	. /etc/udev/udev.conf
-fi
-
-if [ -x /sbin/dmraid-initrd ]; then
-	USE_DMRAID=yes
-fi
-
-if [ -x /sbin/multipath ]; then
-	USE_MULTIPATH=yes
-fi
-
 usage() {
 	uname_r=$(uname -r)
 	echo "usage: $PROGRAM [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
@@ -980,803 +967,816 @@ modules_add_linuxrc() {
 	done
 }
 
-if [ -r /etc/sysconfig/geninitrd ]; then
-	. /etc/sysconfig/geninitrd
-fi
-
-if [ -r /etc/sysconfig/bootsplash ]; then
-	. /etc/sysconfig/bootsplash
-fi
-
-if [ -r /etc/sysconfig/fbsplash ]; then
-	. /etc/sysconfig/fbsplash
-fi
-
-if [ ! -x /bin/initrd-busybox ]; then
-	die "/bin/initrd-busybox is missing!"
-fi
-
-# backwards compatible
-if [ "$USE_SUSPEND2" ]; then
-	USE_TUXONICE=$USE_SUSPEND2
-	echo >&2 "USE_SUSPEND2 is deprecated, use USE_TUXONICE now instead."
-fi
+initrd_gen_suspend() {
+	if [ ! -x /usr/${_lib}/suspend/resume -a ! -x /usr/sbin/resume ]; then
+		die "/usr/${_lib}/suspend/resume is missing!"
+	fi
+	resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
+	mknod $MNTIMAGE/dev/snapshot c 10 231
+	inst $resume_dev /dev
+	inst /etc/suspend.conf /etc/suspend.conf
+	if [ -x /usr/${_lib}/suspend/resume ]; then
+		inst /usr/${_lib}/suspend/resume /bin/resume
+	else
+		inst /usr/sbin/resume /bin/resume
+	fi
+	echo "resume" | add_linuxrc
+}
 
-while [ $# -gt 0 ]; do
-	case $1 in
-	--fstab=*)
-		fstab=${1#--fstab=}
-		;;
-	--fstab)
-		fstab=$2
-		shift
-		;;
-	--modules-conf=*)
-		modulefile=${1#--modules-conf=}
-		;;
-	--modules-conf)
-		modulefile=$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
-		;;
-	--with-bootsplash)
-		BOOT_SPLASH=yes
-		;;
-	--without-bootsplash)
-		BOOT_SPLASH=no
-		;;
-	--with-fbsplash)
-		FB_SPLASH=yes
-		;;
-	--without-fbsplash)
-		FB_SPLASH=no
-		;;
-	--with-suspend)
-		USE_SUSPEND=yes
-		;;
-	--without-suspend)
-		USE_SUSPEND=no
-		;;
-	--with-suspend2 | --with-tuxonice)
-		USE_TUXONICE=yes
-		;;
-	--without-suspend2 | --without-tuxonice)
-		USE_TUXONICE=no
-		;;
-	--lvmtoolsversion=|--lvmversion=)
-		LVMTOOLSVERSION="`echo $1 | awk -F= '{print $2;}'`"
-		;;
-	--lvmtoolsversion|--lvmversion)
-		LVMTOOLSVERSION=$2
-		shift
-		;;
-	--without-udev)
-		USE_UDEV=no
-		;;
-	--with-udev)
-		USE_UDEV=yes
-		;;
-	--without-dmraid)
-		USE_DMRAID=no
-		;;
-	--without-multipath)
-		USE_MULTPATH=no
-		;;
-	--with=*)
-		BASICMODULES="$BASICMODULES ${1#--with=}"
-		;;
-	--with)
-		BASICMODULES="$BASICMODULES $2"
-		shift
-		;;
-	--version)
-		echo "$PROGRAM: version $VERSION"
-		exit 0
-		;;
-	-v)
-		verbose=-v
-		;;
-	--nocompress)
-		COMPRESS=no
-		;;
-	--ifneeded)
-		ifneeded=1
-		;;
-	-f)
-		force=1
-		;;
-	--preload=*)
-		PREMODS="$PREMODS ${1#--preload=}"
-		;;
-	--preload)
-		PREMODS="$PREMODS $2"
-		shift
-		;;
-	--fs=*)
-		echo >&2 "Warning: --fs option is obsoleted. Use --initrdfs instead"
-		INITRDFS=${1#--fs=}
-		;;
-	--fs)
-		echo >&2 "Warning: --fs option is obsoleted. Use --initrdfs instead"
-		INITRDFS=$2
-		shift
-		;;
-	--initrdfs=*)
-		INITRDFS=${1#--initrdfs=}
-		;;
-	--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
+initrd_gen_tuxonice() {
+	mount_sys
+	add_linuxrc <<-'EOF'
+		if [ "$(echo "$CMDLINE" | awk ' /resume2=/  { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
+			[ -e /proc/suspend2/do_resume ] && echo > /proc/suspend2/do_resume
+			[ -e /sys/power/suspend2/do_resume ] && echo > /sys/power/suspend2/do_resume
+			[ -e /sys/power/tuxonice/do_resume ] && echo >  /sys/power/tuxonice/do_resume
 		fi
-		;;
-	esac
+	EOF
+}
 
-	shift
-done
+initrd_gen_udev() {
+	debug "Setting up udev..."
+	inst_d /sbin /etc/udev
 
-if [ -z "$target" -o -z "$kernel" ]; then
-	usage
-fi
+	if [ ! -x /sbin/initrd-udevd ]; then
+		die "/sbin/initrd-udevd not present"
+	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)}'`"
+	inst /sbin/initrd-udevd /sbin/udevd
+	inst /etc/udev/udev.conf /etc/udev/udev.conf
 
-if [ -z "$INITRDFS" ]; then
-	if [ -z = "$FS" ]; then
-		# default value
-		if [ "$pack_version" -ge "002005" ]; then
-			INITRDFS="initramfs"
-		else
-			INITRDFS="rom"
-		fi
-	else
-		echo >&2 "Warning: FS configuration options is obsoleted. Use INITRDFS instead"
-		INITRDFS="$FS"
+	mount_dev
+	mount_sys
+	add_linuxrc <<-'EOF'
+		: 'Starting udev'
+		/sbin/udevd --daemon
+	EOF
+	if is_yes "$PROBESTATICMODS"; then
+		inst /sbin/initrd-udevtrigger /sbin/udevtrigger
+		inst /sbin/initrd-udevsettle /sbin/udevsettle
+		add_linuxrc <<-'EOF'
+			/sbin/udevtrigger
+			/sbin/udevsettle
+		EOF
 	fi
-fi
 
-if [ "$pack_version" -lt "002006" ]; then
-	USE_UDEV=no
-	USE_DMRAID=no
-fi
+	busybox_applet killall
+	add_linuxrc	<<-'EOF'
+		killall udevd
+	EOF
+}
 
-if [ "$pack_version" -ge "002005" ]; then
-	modext=".ko"
-	insmod="insmod"
-fi
+initrd_gen_multipath() {
+	inst_d /sbin /lib/udev
+	inst_exec /sbin/kpartx /sbin
+	inst_exec /sbin/multipath /sbin
+	# for udev callouts
+	inst_exec /sbin/scsi_id /lib/udev
+	inst_exec /sbin/mpath* /sbin
+	inst /etc/multipath.conf /etc
+	if [ -f /var/lib/multipath/bindings ]; then
+		inst_d /var/lib/multipath
+		inst /var/lib/multipath/bindings /var/lib/multipath
+	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
-		die "insmod.static requested but /sbin/insmod.static not found!"
-	fi
-fi
-
-case "$INITRDFS" in
-	ext2)
-		if [ ! -x /sbin/mke2fs ]; then
-			die "/sbin/mke2fs is missing"
-		fi
-		;;
-	rom|romfs)
-		if [ ! -x /sbin/genromfs ]; then
-			die "/sbin/genromfs is missing"
-		fi
-		;;
-	cram|cramfs)
-		if [ ! -x /sbin/mkcramfs ]; then
-			die "/sbin/mkcramfs is missing"
-		fi
-		;;
-	initramfs)
-		if [ ! -x /bin/cpio ]; then
-			die "/bin/cpio is missing"
-		fi
-		if [ ! -x /usr/bin/find ]; then
-			die "/usr/bin/find is missing"
-		fi
-		;;
-	*)
-		die "Filesystem $INITRDFS on initrd is not supported"
-		;;
-esac
-
-if [ -n "$img_vers" ]; then
-	target="$target-$kernel"
-fi
-
-if [ -z "$force" -a -f "$target" ]; then
-	die "$target already exists."
-fi
+	mount_dev
+	initrd_gen_devices
 
-if [ ! -d "/lib/modules/$kernel" ]; then
-	die "/lib/modules/$kernel is not a directory."
-fi
+	mount_sys
+	echo "export multipath_id=$MULTIPATH_ID" | add_linuxrc
+	add_linuxrc <<-'EOF'
+		debugshell
+		/sbin/multipath -v 0 $multipath_id
 
-if is_yes "$USE_SUSPEND" && is_yes "$USE_TUXONICE"; then
-	die "Tuxonice shouldn't be used in parallel with mainline suspend!."
-fi
+		for a in /dev/mapper/*; do
+			[ $a = /dev/mapper/control ] && continue
+			/sbin/kpartx -a $a
+		done
+		debugshell
+	EOF
+}
 
-if is_yes "$FB_SPLASH"; then
-	if is_yes "$BOOT_SPLASH"; then
-		die "You can't use both bootsplash and fbsplash! Please choose one."
-	elif [ "$INITRDFS" != "initramfs" ]; then
-		die "FB_SPLASH works only if INITRDFS is initramfs!."
+initrd_gen_dmraid() {
+	if [ ! -x /sbin/dmraid-initrd ]; then
+		die "/sbin/dmraid-initrd is missing!"
 	fi
-fi
 
-if [ ! -f /proc/mounts ]; then
-	echo >&2 "WARNING: /proc filesystem not mounted, may cause wrong results or failure."
-fi
+	inst_d /sbin
+	inst /sbin/dmraid-initrd /sbin/dmraid
 
-if [ -d /usr/lib64 ]; then
-	_lib=lib64
-else
-	_lib=lib
-fi
-debug "_lib: $_lib"
+	mount_dev
+	mount_sys
+	initrd_gen_devices
+	add_linuxrc <<-EOF
+		# 2 secs was enough for my system to initialize. but really this is udev issue?
+		usleep 2000000
+		: 'Activating Device-Mapper RAID(s)'
+		/sbin/dmraid -ay -i
 
-cache_modprobe_conf
+		debugshell
+	EOF
+}
 
-for n in $PREMODS; do
-	findmodule "$n"
-done
+initrd_gen_bootsplash() {
+	local target="$1"
 
-# allow forcing loading SCSI and/or IDE modules
-if is_yes "$ADDSCSI"; then
-	find_modules_scsi
-fi
+	debug "generating bootsplash"
+	if [ ! -x /bin/splash.bin ]; then
+		echo >&2 "Failed to execute /bin/splash.bin. Is bootsplash package installed?"
+	elif [ -z "$THEME" ]; then
+		echo >&2 "Please configure your /etc/sysconfig/bootsplash first."
+		echo >&2 "Generating bootsplashes skipped."
+	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" && \
+					debug "Added $res $THEME theme to initrd."
+				else
+					echo >&2 "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped"
+				fi
+			done
+		else
+			echo >&2 "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash."
+			echo >&2 "Not adding bootsplash to initrd."
+		fi
+	fi
+}
 
-if is_yes "$ADDIDE"; then
-	find_modules_ide
-fi
+initrd_gen_fbsplash() {
+	debug "generating fbsplash"
 
-find_root "$fstab" || exit
-debug "Using $rootdev as device for rootfs"
+	if [ ! -x /usr/bin/splash_geninitramfs ]; then
+		echo >&2 "Failed to execute /usr/bin/splash_geninitramfs. Is splashutils package installed?"
+		return
+	fi
 
-find_modules_for_device "$rootdev"
-[ -n "$rootdev_add" ] && find_modules_for_device "$rootdev_add"
+	if [ -z "$SPLASH_THEME" ]; then
+		echo >&2 "Please configure your /etc/sysconfig/fbsplash first."
+		echo >&2 "Generating fbsplashes skipped."
+		return
+	fi
 
-findmodule "-$rootFs"
+	if [ -z "$FB_SPLASH_RESOLUTIONS" ]; then
+		echo >&2 "No FB_SPLASH_RESOLUTIONS specified in /etc/sysconfig/fbsplash."
+		echo >&2 "Not adding fbsplash to initramfs."
+		return
+	fi
 
-for n in $BASICMODULES; do
-	findmodule "$n"
-done
+	for res in $FB_SPLASH_RESOLUTIONS; do
+		if [ -f "/etc/splash/$SPLASH_THEME/$res.cfg" ]; then
+			/usr/bin/splash_geninitramfs -c $MNTIMAGE -r $res $SPLASH_THEME && \
+			debug "Added $res $SPLASH_THEME theme to initramfs."
+		else
+			echo >&2 "/etc/splash/$SPLASH_THEME/$res.cfg doesn't exist, skipped"
+		fi
+	done
+}
 
-if is_yes "$USE_TUXONICE"; then
-	findmodule "-lzf"
-fi
+# Generates /dev nodes based on /proc/partitions information.
+# Needs /proc mounted.
+# Can be called multiple times.
+initrd_gen_devices() {
+	if is_yes "$proc_partitions"; then
+		return
+	fi
+	proc_partitions=yes
 
-if is_yes "$FB_SPLASH"; then
-	findmodule "-evdev"
-fi
+	mount_dev
+	add_linuxrc <<-'EOF'
+		: 'Making device nodes'
+		cat /proc/partitions | (
+			# ignore first two lines, header, empty line and process rest
+			read b; read b
 
-if [ -n "$ifneeded" -a -z "$MODULES" ]; then
-	debug "No modules are needed -- not building initrd image."
-	exit 0
-fi
-debug "Using modules: $MODULES"
+			while read major minor blocks dev rest; do
+				node=/dev/$dev
+				mkdir -p ${node%/*}
+				mknod $node b $major $minor
+			done
+		)
+	EOF
+}
 
-MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
-IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
-MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
-RCFILE="$MNTIMAGE/linuxrc"
+initrd_gen_md() {
+	debug "Setting up mdadm..."
 
-if [ -f "$MNTIMAGE" ]; then
-	die "$MNTIMAGE already exists. Remove it and try again"
-fi
+	if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ]; then
+		die "/sbin/mdadm or /sbin/initrd-mdassemble is missing!"
+	fi
 
-if [ -f "$IMAGE" ]; then
-	die "$IMAGE already exists. Remove it and try again"
-fi
+	inst /sbin/initrd-mdassemble /bin/mdassemble
 
-mkdir -p "$MNTPOINT"
-# We don't need this directory, so let's save space
-rm -rf "$MNTPOINT"/lost+found
+	# LVM on RAID case
+	dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf)
+	for ex_dev in $dev_list_extra; do
+		echo "DEVICE $ex_dev" >> "$MNTIMAGE/etc/mdadm.conf"
+	done
+	do_md0=1
+	for nr in `seq 1 $rootdev_nr`; do
+		eval cr_rootdev="\$rootdev${nr}"
+		eval cr_dev_list="\$dev_list${nr}"
+		debug echo "Setting up array ($cr_rootdev = $cr_dev_list)"
 
-> "$RCFILE"
-chmod a+rx "$RCFILE"
-ln -s linuxrc $MNTIMAGE/init
+		[ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
 
-# create dirs that we really need
-inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,sys}
+		echo "DEVICE $cr_dev_list" >> "$MNTIMAGE/etc/mdadm.conf"
+		cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
+		cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev | awk ' { gsub(/spares=[0-9]+/, "", $0); print $0; }')
+		if [ -n "$cr_md_conf" ]; then
+			echo "$cr_md_conf" >> "$MNTIMAGE/etc/mdadm.conf"
+		else
+			echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$MNTIMAGE/etc/mdadm.conf"
+		fi
 
-modules_install "$MODULES"
+		for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
+			# mkdir in case of devfs name
+			inst_d $(dirname $f)
+			[ -e "$MNTIMAGE/$f" ] && continue
+			# this works fine with and without devfs
+			inst $f $f
+		done
+	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
+	echo "mdassemble" | add_linuxrc
 
-inst /bin/initrd-busybox /bin/initrd-busybox
-ln -s initrd-busybox $MNTIMAGE/bin/sh
-ln -s initrd-busybox $MNTIMAGE/bin/busybox # for older busyboxes who had /bin/busybox as EXEPATH
+	# needed to determine md-version
+	if [ "$do_md0" -eq 1 ]; then
+		mknod $MNTIMAGE/dev/md0 b 9 0
+	fi
+}
 
-if is_yes "$USEINSMODSTATIC"; then
-	inst "$INSMOD" /bin/insmod.static
-fi
+initrd_gen_nfs() {
+	# use root=/dev/ram0 init=/linuxrc when starting kernel or you will
+	# have problems like init(XX) being child process of swapper(1).
+	debug "Adding rootfs on NFS support to initrd (dhcp)"
+	mknod "$MNTIMAGE/dev/urandom" c 1 9
+	mkdir "$MNTIMAGE/newroot"
+	mkdir "$MNTIMAGE/proc"
+	echo "ifconfig lo 127.0.0.1 up" | add_linuxrc
+	echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" | add_linuxrc
+	echo "ifconfig eth0 0.0.0.0 up" | add_linuxrc
+	echo "udhcpc -i eth0 -f -q -s /bin/setdhcp" | add_linuxrc
 
-add_linuxrc <<EOF
-#!/bin/sh
-# initrd generated by:
-# $RCSID
+	cat <<-'EOF' > "$MNTIMAGE/bin/setdhcp"
+		#!/bin/sh
+		[ "$1" != "bound" ] && exit
+		[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
+		[ -n "$subnet" ] && NETMASK="netmask $subnet"
+		ifconfig $interface $ip $BROADCAST $NETMASK up
+		if [ -n "$router" ]; then
+			for r in $router; do
+				route add default gw $r dev $interface
+			done
+		fi
+	EOF
 
-EOF
-mount_proc
-add_linuxrc <<-'EOF'
-	export CMDLINE="$(cat /proc/cmdline)"
+	cat <<-'EOF' > "$MNTIMAGE/bin/setdhcp"
+		for o in $(cat /proc/cmdline); do
+			case $o in
+			nfsroot=*)
+				rootpath=${o#nfsroot=}
+				;;
+			esac
+		done
 
-	for arg in $CMDLINE; do
-		if [ "${arg}" = "debuginitrd" ]; then
-			DEBUGINITRD=yes
-		fi
-		if [ "${arg##debuginitrd=}" != "${arg}" ]; then
-			DEBUGINITRD=${arg##debuginitrd=}
+		if [ -n "$rootpath" ]; then
+			mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
+		else
+			echo "Missing rootpath in what DHCP server sent to us. Failing..."
+			echo "All seen variables are listed below:"
+			set
 		fi
-	done
+	EOF
 
-	# make debugshell() invoke subshell if $DEBUGINITRD=sh
-	if [ "$DEBUGINITRD" = "sh" ]; then
-		debugshell() {
-			sh
-		}
-	else
-		debugshell() {
-			:
-		}
+	chmod 755 "$MNTIMAGE/bin/setdhcp"
+	add_linuxrc <<-'EOF'
+		cd /newroot
+		pivot_root . initrd
+		[ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1
+		exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1
+	EOF
+}
+
+initrd_gen_lvm() {
+	debug "Adding LVM support to initrd"
+	inst_d /tmp /newroot
+	inst /sbin/initrd-lvm /bin/lvm.static
+
+	# always make /dev on tmpfs for LVM2
+	if [ "$LVMTOOLSVERSION" = "2" ]; then
+		mount_dev
 	fi
 
-	if [ "$DEBUGINITRD" ]; then
-		set -x
+	if ! is_yes "$dev_mounted"; then
+		inst_d /dev/mapper
+		mknod $MNTIMAGE/dev/mapper/control c 10 63
+		for device in $PVDEVICES; do
+			# if LVM on RAID then device might be copied already in gen_md
+			[ -e "$MNTIMAGE/dev/$(basename $device)" ] && continue
+			inst $device /dev
+		done
 	fi
-EOF
 
-modules_add_linuxrc "$MODULES"
+	mount_tmp
+	if [ "$LVMTOOLSVERSION" = "1" ]; then
+		add_linuxrc <<-EOF
+			lvm vgscan -T
+			lvm vgchange -T -a y $VGVOLUME
+		EOF
+	else
+		echo "cat /etc/lvm.conf > /tmp/lvm.conf" | add_linuxrc
+		echo "global {" > "$MNTIMAGE/etc/lvm.conf"
+		echo "	locking_type = 0" >> "$MNTIMAGE/etc/lvm.conf"
+		echo "	locking_dir = \"/tmp\"" >> "$MNTIMAGE/etc/lvm.conf"
+		echo "}" >> "$MNTIMAGE/etc/lvm.conf"
+		echo "devices {" >> "$MNTIMAGE/etc/lvm.conf"
+		echo "	sysfs_scan=0" >> "$MNTIMAGE/etc/lvm.conf"
+		if is_yes "$have_md"; then
+			echo "	md_component_detection = 1" >> "$MNTIMAGE/etc/lvm.conf"
+		fi
+		if is_yes "$have_dmraid" || is_yes "$have_multipath"; then
+			echo '	types = [ "device-mapper", 254 ]' >> "$MNTIMAGE/etc/lvm.conf"
+		fi
+		if [ "$lvm_ignore_devices" ]; then
+			# TODO: think of merging with lvm dumpconfig output
+			echo '	filter = [' >> "$MNTIMAGE/etc/lvm.conf"
+			local dev
+			for dev in $lvm_ignore_devices; do
+				debug "LVM2: ignore device $dev"
+				printf '  "r|^%s.*|",\n' $dev
+			done >> "$MNTIMAGE/etc/lvm.conf"
+			echo ']' >> "$MNTIMAGE/etc/lvm.conf"
+		fi
+		# XXX filter= must be on one line!
+		lvm dumpconfig | awk '/filter=/' >> "$MNTIMAGE/etc/lvm.conf"
+		echo "}" >> "$MNTIMAGE/etc/lvm.conf"
 
-# TODO: rewrite for busybox
-#if [ -n "$loopDev" ]; then
-#	if [ ! -d /initrd ]; then
-#		mkdir /initrd
-#	fi
-#
-#	cp -a "$loopDev" "$MNTIMAGE/dev"
-#	cp -a "$rootdev" "$MNTIMAGE/dev"
-#	echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
-#	echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
-#	echo "echo Setting up loopback device $rootdev" >> $RCFILE
-#	echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE"
-#fi
+		initrd_gen_devices
 
-initrd_gen_suspend() {
-	if [ ! -x /usr/${_lib}/suspend/resume -a ! -x /usr/sbin/resume ]; then
-		die "/usr/${_lib}/suspend/resume is missing!"
-	fi
-	resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
-	mknod $MNTIMAGE/dev/snapshot c 10 231
-	inst $resume_dev /dev
-	inst /etc/suspend.conf /etc/suspend.conf
-	if [ -x /usr/${_lib}/suspend/resume ]; then
-		inst /usr/${_lib}/suspend/resume /bin/resume
-	else
-		inst /usr/sbin/resume /bin/resume
-	fi
-	echo "resume" | add_linuxrc
-}
+		add_linuxrc <<-EOF
+			export LVM_ROOTDEV=$rootdev
+			export LVM_VGVOLUME=$VGVOLUME
+		EOF
+		add_linuxrc <<-'EOF'
+			# disable noise from lvm accessing devices that aren't ready.
+			printk=$(cat /proc/sys/kernel/printk)
+			echo 0 > /proc/sys/kernel/printk
 
-initrd_gen_tuxonice() {
-	mount_sys
-	add_linuxrc <<-'EOF'
-		if [ "$(echo "$CMDLINE" | awk ' /resume2=/  { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
-			[ -e /proc/suspend2/do_resume ] && echo > /proc/suspend2/do_resume
-			[ -e /sys/power/suspend2/do_resume ] && echo > /sys/power/suspend2/do_resume
-			[ -e /sys/power/tuxonice/do_resume ] && echo >  /sys/power/tuxonice/do_resume
-		fi
-	EOF
-}
+			export LVM_SYSTEM_DIR=/tmp
+			: 'Scanning for Volume Groups'
+			lvm.static vgscan --mknodes --ignorelockingfailure 2>/dev/null
 
-initrd_gen_udev() {
-	debug "Setting up udev..."
-	inst_d /sbin /etc/udev
+			: 'Activating Volume Groups'
+			lvm.static vgchange --ignorelockingfailure -a y $LVM_VGVOLUME 2>/dev/null
 
-	if [ ! -x /sbin/initrd-udevd ]; then
-		die "/sbin/initrd-udevd not present"
-	fi
+			echo "$printk" > /proc/sys/kernel/printk
 
-	inst /sbin/initrd-udevd /sbin/udevd
-	inst /etc/udev/udev.conf /etc/udev/udev.conf
+			# Find out major/minor
+			attrs="$(lvm.static lvdisplay --ignorelockingfailure -c $LVM_ROOTDEV 2>/dev/null)"
+			if [ "$attrs" ]; then
+				majmin="${attrs#*$LVM_ROOTDEV*:*:*:*:*:*:*:*:*:*:*:*}"
+				if [ "$majmin" != "$attrs" ]; then
+					major="${majmin%:*}"
+					minor="${majmin#*:}"
+				fi
+			fi
 
-	mount_dev
-	mount_sys
-	add_linuxrc <<-'EOF'
-		: 'Starting udev'
-		/sbin/udevd --daemon
-	EOF
-	if is_yes "$PROBESTATICMODS"; then
-		inst /sbin/initrd-udevtrigger /sbin/udevtrigger
-		inst /sbin/initrd-udevsettle /sbin/udevsettle
-		add_linuxrc <<-'EOF'
-			/sbin/udevtrigger
-			/sbin/udevsettle
+			if [ "$major" -a "$minor" ]; then
+				# Pass it to kernel
+				echo $((256 * $major + $minor)) > /proc/sys/kernel/real-root-dev
+			else
+				echo 2>&1 "Error figuring out real root device for $LVM_ROOTDEV!"
+				echo 2>&1 "System will not most likely boot up! So dropping you to a shell!"
+				echo 2>&1 ""
+				sh
+			fi
 		EOF
 	fi
+}
 
-	busybox_applet killall
-	add_linuxrc	<<-'EOF'
-		killall udevd
+initrd_gen_procdata() {
+	debug "Adding rootfs finding based on root= option support."
+	add_linuxrc <<-'EOF'
+		root="$(busybox awk ' /root=\/dev\// { gsub(/.*root=\/dev\//,NIL,$0); gsub(/ .*/,NIL,$0); print $0; } ' /proc/cmdline)"
+		if [ -n "$root" ]; then
+			rootnr="$(busybox awk -v root="$root" ' { if ($4 == root) { print 256*$1+$2; } } ' /proc/partitions)"
+			if [ -n "$rootnr" ]; then
+				echo "$rootnr" > /proc/sys/kernel/real-root-dev
+			fi
+		fi
 	EOF
 }
 
-initrd_gen_multipath() {
-	inst_d /sbin /lib/udev
-	inst_exec /sbin/kpartx /sbin
-	inst_exec /sbin/multipath /sbin
-	# for udev callouts
-	inst_exec /sbin/scsi_id /lib/udev
-	inst_exec /sbin/mpath* /sbin
-	inst /etc/multipath.conf /etc
-	if [ -f /var/lib/multipath/bindings ]; then
-		inst_d /var/lib/multipath
-		inst /var/lib/multipath/bindings /var/lib/multipath
-	fi
+if [ -f /etc/udev/udev.conf -a -x /sbin/initrd-udevd ]; then
+	USE_UDEV=yes
+	. /etc/udev/udev.conf
+fi
 
-	mount_dev
-	initrd_gen_devices
+if [ -x /sbin/dmraid-initrd ]; then
+	USE_DMRAID=yes
+fi
 
-	mount_sys
-	echo "export multipath_id=$MULTIPATH_ID" | add_linuxrc
-	add_linuxrc <<-'EOF'
-		debugshell
-		/sbin/multipath -v 0 $multipath_id
+if [ -x /sbin/multipath ]; then
+	USE_MULTIPATH=yes
+fi
 
-		for a in /dev/mapper/*; do
-			[ $a = /dev/mapper/control ] && continue
-			/sbin/kpartx -a $a
-		done
-		debugshell
-	EOF
-}
+if [ -r /etc/sysconfig/geninitrd ]; then
+	. /etc/sysconfig/geninitrd
+fi
 
-initrd_gen_dmraid() {
-	if [ ! -x /sbin/dmraid-initrd ]; then
-		die "/sbin/dmraid-initrd is missing!"
-	fi
+if [ -r /etc/sysconfig/bootsplash ]; then
+	. /etc/sysconfig/bootsplash
+fi
 
-	inst_d /sbin
-	inst /sbin/dmraid-initrd /sbin/dmraid
+if [ -r /etc/sysconfig/fbsplash ]; then
+	. /etc/sysconfig/fbsplash
+fi
 
-	mount_dev
-	mount_sys
-	initrd_gen_devices
-	add_linuxrc <<-EOF
-		# 2 secs was enough for my system to initialize. but really this is udev issue?
-		usleep 2000000
-		: 'Activating Device-Mapper RAID(s)'
-		/sbin/dmraid -ay -i
+if [ ! -x /bin/initrd-busybox ]; then
+	die "/bin/initrd-busybox is missing!"
+fi
 
-		debugshell
-	EOF
-}
+# backwards compatible
+if [ "$USE_SUSPEND2" ]; then
+	USE_TUXONICE=$USE_SUSPEND2
+	echo >&2 "USE_SUSPEND2 is deprecated, use USE_TUXONICE now instead."
+fi
 
-initrd_gen_bootsplash() {
-	local target="$1"
+while [ $# -gt 0 ]; do
+	case $1 in
+	--fstab=*)
+		fstab=${1#--fstab=}
+		;;
+	--fstab)
+		fstab=$2
+		shift
+		;;
+	--modules-conf=*)
+		modulefile=${1#--modules-conf=}
+		;;
+	--modules-conf)
+		modulefile=$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
+		;;
+	--with-bootsplash)
+		BOOT_SPLASH=yes
+		;;
+	--without-bootsplash)
+		BOOT_SPLASH=no
+		;;
+	--with-fbsplash)
+		FB_SPLASH=yes
+		;;
+	--without-fbsplash)
+		FB_SPLASH=no
+		;;
+	--with-suspend)
+		USE_SUSPEND=yes
+		;;
+	--without-suspend)
+		USE_SUSPEND=no
+		;;
+	--with-suspend2 | --with-tuxonice)
+		USE_TUXONICE=yes
+		;;
+	--without-suspend2 | --without-tuxonice)
+		USE_TUXONICE=no
+		;;
+	--lvmtoolsversion=|--lvmversion=)
+		LVMTOOLSVERSION="`echo $1 | awk -F= '{print $2;}'`"
+		;;
+	--lvmtoolsversion|--lvmversion)
+		LVMTOOLSVERSION=$2
+		shift
+		;;
+	--without-udev)
+		USE_UDEV=no
+		;;
+	--with-udev)
+		USE_UDEV=yes
+		;;
+	--without-dmraid)
+		USE_DMRAID=no
+		;;
+	--without-multipath)
+		USE_MULTPATH=no
+		;;
+	--with=*)
+		BASICMODULES="$BASICMODULES ${1#--with=}"
+		;;
+	--with)
+		BASICMODULES="$BASICMODULES $2"
+		shift
+		;;
+	--version)
+		echo "$PROGRAM: version $VERSION"
+		exit 0
+		;;
+	-v)
+		verbose=-v
+		;;
+	--nocompress)
+		COMPRESS=no
+		;;
+	--ifneeded)
+		ifneeded=1
+		;;
+	-f)
+		force=1
+		;;
+	--preload=*)
+		PREMODS="$PREMODS ${1#--preload=}"
+		;;
+	--preload)
+		PREMODS="$PREMODS $2"
+		shift
+		;;
+	--fs=*)
+		echo >&2 "Warning: --fs option is obsoleted. Use --initrdfs instead"
+		INITRDFS=${1#--fs=}
+		;;
+	--fs)
+		echo >&2 "Warning: --fs option is obsoleted. Use --initrdfs instead"
+		INITRDFS=$2
+		shift
+		;;
+	--initrdfs=*)
+		INITRDFS=${1#--initrdfs=}
+		;;
+	--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
 
-	debug "generating bootsplash"
-	if [ ! -x /bin/splash.bin ]; then
-		echo >&2 "Failed to execute /bin/splash.bin. Is bootsplash package installed?"
-	elif [ -z "$THEME" ]; then
-		echo >&2 "Please configure your /etc/sysconfig/bootsplash first."
-		echo >&2 "Generating bootsplashes skipped."
-	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" && \
-					debug "Added $res $THEME theme to initrd."
-				else
-					echo >&2 "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped"
-				fi
-			done
+	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 [ -z "$INITRDFS" ]; then
+	if [ -z = "$FS" ]; then
+		# default value
+		if [ "$pack_version" -ge "002005" ]; then
+			INITRDFS="initramfs"
 		else
-			echo >&2 "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash."
-			echo >&2 "Not adding bootsplash to initrd."
+			INITRDFS="rom"
 		fi
+	else
+		echo >&2 "Warning: FS configuration options is obsoleted. Use INITRDFS instead"
+		INITRDFS="$FS"
 	fi
-}
+fi
 
-initrd_gen_fbsplash() {
-	debug "generating fbsplash"
+if [ "$pack_version" -lt "002006" ]; then
+	USE_UDEV=no
+	USE_DMRAID=no
+fi
 
-	if [ ! -x /usr/bin/splash_geninitramfs ]; then
-		echo >&2 "Failed to execute /usr/bin/splash_geninitramfs. Is splashutils package installed?"
-		return
-	fi
+if [ "$pack_version" -ge "002005" ]; then
+	modext=".ko"
+	insmod="insmod"
+fi
 
-	if [ -z "$SPLASH_THEME" ]; then
-		echo >&2 "Please configure your /etc/sysconfig/fbsplash first."
-		echo >&2 "Generating fbsplashes skipped."
-		return
+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 [ -z "$FB_SPLASH_RESOLUTIONS" ]; then
-		echo >&2 "No FB_SPLASH_RESOLUTIONS specified in /etc/sysconfig/fbsplash."
-		echo >&2 "Not adding fbsplash to initramfs."
-		return
+	if [ ! -f "$INSMOD" ]; then
+		die "insmod.static requested but /sbin/insmod.static not found!"
 	fi
+fi
 
-	for res in $FB_SPLASH_RESOLUTIONS; do
-		if [ -f "/etc/splash/$SPLASH_THEME/$res.cfg" ]; then
-			/usr/bin/splash_geninitramfs -c $MNTIMAGE -r $res $SPLASH_THEME && \
-			debug "Added $res $SPLASH_THEME theme to initramfs."
-		else
-			echo >&2 "/etc/splash/$SPLASH_THEME/$res.cfg doesn't exist, skipped"
+case "$INITRDFS" in
+	ext2)
+		if [ ! -x /sbin/mke2fs ]; then
+			die "/sbin/mke2fs is missing"
 		fi
-	done
-}
+		;;
+	rom|romfs)
+		if [ ! -x /sbin/genromfs ]; then
+			die "/sbin/genromfs is missing"
+		fi
+		;;
+	cram|cramfs)
+		if [ ! -x /sbin/mkcramfs ]; then
+			die "/sbin/mkcramfs is missing"
+		fi
+		;;
+	initramfs)
+		if [ ! -x /bin/cpio ]; then
+			die "/bin/cpio is missing"
+		fi
+		if [ ! -x /usr/bin/find ]; then
+			die "/usr/bin/find is missing"
+		fi
+		;;
+	*)
+		die "Filesystem $INITRDFS on initrd is not supported"
+		;;
+esac
 
-# Generates /dev nodes based on /proc/partitions information.
-# Needs /proc mounted.
-# Can be called multiple times.
-initrd_gen_devices() {
-	if is_yes "$proc_partitions"; then
-		return
-	fi
-	proc_partitions=yes
+if [ -n "$img_vers" ]; then
+	target="$target-$kernel"
+fi
 
-	mount_dev
-	add_linuxrc <<-'EOF'
-		: 'Making device nodes'
-		cat /proc/partitions | (
-			# ignore first two lines, header, empty line and process rest
-			read b; read b
+if [ -z "$force" -a -f "$target" ]; then
+	die "$target already exists."
+fi
 
-			while read major minor blocks dev rest; do
-				node=/dev/$dev
-				mkdir -p ${node%/*}
-				mknod $node b $major $minor
-			done
-		)
-	EOF
-}
+if [ ! -d "/lib/modules/$kernel" ]; then
+	die "/lib/modules/$kernel is not a directory."
+fi
 
-initrd_gen_md() {
-	debug "Setting up mdadm..."
+if is_yes "$USE_SUSPEND" && is_yes "$USE_TUXONICE"; then
+	die "Tuxonice shouldn't be used in parallel with mainline suspend!."
+fi
 
-	if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ]; then
-		die "/sbin/mdadm or /sbin/initrd-mdassemble is missing!"
+if is_yes "$FB_SPLASH"; then
+	if is_yes "$BOOT_SPLASH"; then
+		die "You can't use both bootsplash and fbsplash! Please choose one."
+	elif [ "$INITRDFS" != "initramfs" ]; then
+		die "FB_SPLASH works only if INITRDFS is initramfs!."
 	fi
+fi
 
-	inst /sbin/initrd-mdassemble /bin/mdassemble
+if [ ! -f /proc/mounts ]; then
+	echo >&2 "WARNING: /proc filesystem not mounted, may cause wrong results or failure."
+fi
 
-	# LVM on RAID case
-	dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf)
-	for ex_dev in $dev_list_extra; do
-		echo "DEVICE $ex_dev" >> "$MNTIMAGE/etc/mdadm.conf"
-	done
-	do_md0=1
-	for nr in `seq 1 $rootdev_nr`; do
-		eval cr_rootdev="\$rootdev${nr}"
-		eval cr_dev_list="\$dev_list${nr}"
-		debug echo "Setting up array ($cr_rootdev = $cr_dev_list)"
+if [ -d /usr/lib64 ]; then
+	_lib=lib64
+else
+	_lib=lib
+fi
+debug "_lib: $_lib"
 
-		[ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
+cache_modprobe_conf
+
+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 "$fstab" || exit
+debug "Using $rootdev as device for rootfs"
+
+find_modules_for_device "$rootdev"
+[ -n "$rootdev_add" ] && find_modules_for_device "$rootdev_add"
 
-		echo "DEVICE $cr_dev_list" >> "$MNTIMAGE/etc/mdadm.conf"
-		cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
-		cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev | awk ' { gsub(/spares=[0-9]+/, "", $0); print $0; }')
-		if [ -n "$cr_md_conf" ]; then
-			echo "$cr_md_conf" >> "$MNTIMAGE/etc/mdadm.conf"
-		else
-			echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$MNTIMAGE/etc/mdadm.conf"
-		fi
+findmodule "-$rootFs"
 
-		for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
-			# mkdir in case of devfs name
-			inst_d $(dirname $f)
-			[ -e "$MNTIMAGE/$f" ] && continue
-			# this works fine with and without devfs
-			inst $f $f
-		done
-	done
+for n in $BASICMODULES; do
+	findmodule "$n"
+done
 
-	echo "mdassemble" | add_linuxrc
+if is_yes "$USE_TUXONICE"; then
+	findmodule "-lzf"
+fi
 
-	# needed to determine md-version
-	if [ "$do_md0" -eq 1 ]; then
-		mknod $MNTIMAGE/dev/md0 b 9 0
-	fi
-}
+if is_yes "$FB_SPLASH"; then
+	findmodule "-evdev"
+fi
 
-initrd_gen_nfs() {
-	# use root=/dev/ram0 init=/linuxrc when starting kernel or you will
-	# have problems like init(XX) being child process of swapper(1).
-	debug "Adding rootfs on NFS support to initrd (dhcp)"
-	mknod "$MNTIMAGE/dev/urandom" c 1 9
-	mkdir "$MNTIMAGE/newroot"
-	mkdir "$MNTIMAGE/proc"
-	echo "ifconfig lo 127.0.0.1 up" | add_linuxrc
-	echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" | add_linuxrc
-	echo "ifconfig eth0 0.0.0.0 up" | add_linuxrc
-	echo "udhcpc -i eth0 -f -q -s /bin/setdhcp" | add_linuxrc
+if [ -n "$ifneeded" -a -z "$MODULES" ]; then
+	debug "No modules are needed -- not building initrd image."
+	exit 0
+fi
+debug "Using modules: $MODULES"
 
-	cat <<-'EOF' > "$MNTIMAGE/bin/setdhcp"
-		#!/bin/sh
-		[ "$1" != "bound" ] && exit
-		[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
-		[ -n "$subnet" ] && NETMASK="netmask $subnet"
-		ifconfig $interface $ip $BROADCAST $NETMASK up
-		if [ -n "$router" ]; then
-			for r in $router; do
-				route add default gw $r dev $interface
-			done
-		fi
-	EOF
+MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
+IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
+MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
+RCFILE="$MNTIMAGE/linuxrc"
 
-	cat <<-'EOF' > "$MNTIMAGE/bin/setdhcp"
-		for o in $(cat /proc/cmdline); do
-			case $o in
-			nfsroot=*)
-				rootpath=${o#nfsroot=}
-				;;
-			esac
-		done
+if [ -f "$MNTIMAGE" ]; then
+	die "$MNTIMAGE already exists. Remove it and try again"
+fi
 
-		if [ -n "$rootpath" ]; then
-			mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
-		else
-			echo "Missing rootpath in what DHCP server sent to us. Failing..."
-			echo "All seen variables are listed below:"
-			set
-		fi
-	EOF
+if [ -f "$IMAGE" ]; then
+	die "$IMAGE already exists. Remove it and try again"
+fi
 
-	chmod 755 "$MNTIMAGE/bin/setdhcp"
-	add_linuxrc <<-'EOF'
-		cd /newroot
-		pivot_root . initrd
-		[ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1
-		exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1
-	EOF
-}
+mkdir -p "$MNTPOINT"
+# We don't need this directory, so let's save space
+rm -rf "$MNTPOINT"/lost+found
 
-initrd_gen_lvm() {
-	debug "Adding LVM support to initrd"
-	inst_d /tmp /newroot
-	inst /sbin/initrd-lvm /bin/lvm.static
+> "$RCFILE"
+chmod a+rx "$RCFILE"
+ln -s linuxrc $MNTIMAGE/init
 
-	# always make /dev on tmpfs for LVM2
-	if [ "$LVMTOOLSVERSION" = "2" ]; then
-		mount_dev
-	fi
+# create dirs that we really need
+inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,sys}
 
-	if ! is_yes "$dev_mounted"; then
-		inst_d /dev/mapper
-		mknod $MNTIMAGE/dev/mapper/control c 10 63
-		for device in $PVDEVICES; do
-			# if LVM on RAID then device might be copied already in gen_md
-			[ -e "$MNTIMAGE/dev/$(basename $device)" ] && continue
-			inst $device /dev
-		done
-	fi
+modules_install "$MODULES"
 
-	mount_tmp
-	if [ "$LVMTOOLSVERSION" = "1" ]; then
-		add_linuxrc <<-EOF
-			lvm vgscan -T
-			lvm vgchange -T -a y $VGVOLUME
-		EOF
-	else
-		echo "cat /etc/lvm.conf > /tmp/lvm.conf" | add_linuxrc
-		echo "global {" > "$MNTIMAGE/etc/lvm.conf"
-		echo "	locking_type = 0" >> "$MNTIMAGE/etc/lvm.conf"
-		echo "	locking_dir = \"/tmp\"" >> "$MNTIMAGE/etc/lvm.conf"
-		echo "}" >> "$MNTIMAGE/etc/lvm.conf"
-		echo "devices {" >> "$MNTIMAGE/etc/lvm.conf"
-		echo "	sysfs_scan=0" >> "$MNTIMAGE/etc/lvm.conf"
-		if is_yes "$have_md"; then
-			echo "	md_component_detection = 1" >> "$MNTIMAGE/etc/lvm.conf"
-		fi
-		if is_yes "$have_dmraid" || is_yes "$have_multipath"; then
-			echo '	types = [ "device-mapper", 254 ]' >> "$MNTIMAGE/etc/lvm.conf"
-		fi
-		if [ "$lvm_ignore_devices" ]; then
-			# TODO: think of merging with lvm dumpconfig output
-			echo '	filter = [' >> "$MNTIMAGE/etc/lvm.conf"
-			local dev
-			for dev in $lvm_ignore_devices; do
-				debug "LVM2: ignore device $dev"
-				printf '  "r|^%s.*|",\n' $dev
-			done >> "$MNTIMAGE/etc/lvm.conf"
-			echo ']' >> "$MNTIMAGE/etc/lvm.conf"
-		fi
-		# XXX filter= must be on one line!
-		lvm dumpconfig | awk '/filter=/' >> "$MNTIMAGE/etc/lvm.conf"
-		echo "}" >> "$MNTIMAGE/etc/lvm.conf"
+# 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
 
-		initrd_gen_devices
+inst /bin/initrd-busybox /bin/initrd-busybox
+ln -s initrd-busybox $MNTIMAGE/bin/sh
+ln -s initrd-busybox $MNTIMAGE/bin/busybox # for older busyboxes who had /bin/busybox as EXEPATH
 
-		add_linuxrc <<-EOF
-			export LVM_ROOTDEV=$rootdev
-			export LVM_VGVOLUME=$VGVOLUME
-		EOF
-		add_linuxrc <<-'EOF'
-			# disable noise from lvm accessing devices that aren't ready.
-			printk=$(cat /proc/sys/kernel/printk)
-			echo 0 > /proc/sys/kernel/printk
+if is_yes "$USEINSMODSTATIC"; then
+	inst "$INSMOD" /bin/insmod.static
+fi
 
-			export LVM_SYSTEM_DIR=/tmp
-			: 'Scanning for Volume Groups'
-			lvm.static vgscan --mknodes --ignorelockingfailure 2>/dev/null
+add_linuxrc <<EOF
+#!/bin/sh
+# initrd generated by:
+# $RCSID
 
-			: 'Activating Volume Groups'
-			lvm.static vgchange --ignorelockingfailure -a y $LVM_VGVOLUME 2>/dev/null
+EOF
+mount_proc
+add_linuxrc <<-'EOF'
+	export CMDLINE="$(cat /proc/cmdline)"
 
-			echo "$printk" > /proc/sys/kernel/printk
+	for arg in $CMDLINE; do
+		if [ "${arg}" = "debuginitrd" ]; then
+			DEBUGINITRD=yes
+		fi
+		if [ "${arg##debuginitrd=}" != "${arg}" ]; then
+			DEBUGINITRD=${arg##debuginitrd=}
+		fi
+	done
 
-			# Find out major/minor
-			attrs="$(lvm.static lvdisplay --ignorelockingfailure -c $LVM_ROOTDEV 2>/dev/null)"
-			if [ "$attrs" ]; then
-				majmin="${attrs#*$LVM_ROOTDEV*:*:*:*:*:*:*:*:*:*:*:*}"
-				if [ "$majmin" != "$attrs" ]; then
-					major="${majmin%:*}"
-					minor="${majmin#*:}"
-				fi
-			fi
+	# make debugshell() invoke subshell if $DEBUGINITRD=sh
+	if [ "$DEBUGINITRD" = "sh" ]; then
+		debugshell() {
+			sh
+		}
+	else
+		debugshell() {
+			:
+		}
+	fi
 
-			if [ "$major" -a "$minor" ]; then
-				# Pass it to kernel
-				echo $((256 * $major + $minor)) > /proc/sys/kernel/real-root-dev
-			else
-				echo 2>&1 "Error figuring out real root device for $LVM_ROOTDEV!"
-				echo 2>&1 "System will not most likely boot up! So dropping you to a shell!"
-				echo 2>&1 ""
-				sh
-			fi
-		EOF
+	if [ "$DEBUGINITRD" ]; then
+		set -x
 	fi
-}
+EOF
 
-initrd_gen_procdata() {
-	debug "Adding rootfs finding based on root= option support."
-	add_linuxrc <<-'EOF'
-		root="$(busybox awk ' /root=\/dev\// { gsub(/.*root=\/dev\//,NIL,$0); gsub(/ .*/,NIL,$0); print $0; } ' /proc/cmdline)"
-		if [ -n "$root" ]; then
-			rootnr="$(busybox awk -v root="$root" ' { if ($4 == root) { print 256*$1+$2; } } ' /proc/partitions)"
-			if [ -n "$rootnr" ]; then
-				echo "$rootnr" > /proc/sys/kernel/real-root-dev
-			fi
-		fi
-	EOF
-}
+modules_add_linuxrc "$MODULES"
+
+# TODO: rewrite for busybox
+#if [ -n "$loopDev" ]; then
+#	if [ ! -d /initrd ]; then
+#		mkdir /initrd
+#	fi
+#
+#	cp -a "$loopDev" "$MNTIMAGE/dev"
+#	cp -a "$rootdev" "$MNTIMAGE/dev"
+#	echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
+#	echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
+#	echo "echo Setting up loopback device $rootdev" >> $RCFILE
+#	echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE"
+#fi
 
 # main generation
 if is_yes "$USE_UDEV"; then
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/readonly/geninitrd.git/commitdiff/147754ca159d40ca5eb541074dc043d8cbd92090




More information about the pld-cvs-commit mailing list