#!/bin/sh
#
# geninitrd functions

# Find root device from fstab.
#
# @param	string	$fstab location of /etc/fstab
# @return	false on failure
# 
# Sets global variables:
# - $rootdev
# - $rootdev_add
# - $rootFS
#
find_root() {
	local fstab="$1"
	local function="${PROGRAM:+$PROGRAM: }find_root"
	local rootopt

	eval $(awk '!/^[\t ]*#/ && $2 == "/" {printf("rootdev=\"%s\"\nrootFs=\"%s\"\nrootopt=\"%s\"\n", $1, $3, $4)}' $fstab)
	if [ -z "$rootdev" ]; then
		echo >&2 "$function: can't find fstab entry for root mountpoint"
		return 1
	fi

	# additional devices needed (xfs logdev)
	rootdev_add=$(echo "$rootopt" | awk -F',' '{ for (i=1; i<=NF; i++) { if ($i ~ /^logdev=/) { gsub(/^logdev=/, NIL, $i); print $i; } } }')

	case $rootdev in
	LABEL=*)
		if [ ! -x /sbin/blkid ]; then
			echo >&2 "$function: /sbin/blkid is missing"
			return 2
		fi

		local label=${rootdev#"LABEL="}
		local dev=$(/sbin/blkid -l -t LABEL="$label" -o device)
		if [ "$dev" ]; then
			if [ ! -r "$dev" ]; then
				echo >&2 "$function: blkid returned device $dev which doesn't exist"
				return 2
			fi
			rootdev=$dev
		fi
		;;

	UUID=*)
		if [ ! -x /sbin/blkid ]; then
			echo >&2 "$function: /sbin/blkid is missing"
			return 2
		fi

		local uuid=${rootdev#"UUID="}
		local dev=$(/sbin/blkid -l -t UUID="$uuid" -o device)

		if [ "$dev" ]; then
			if [ ! -r "$dev" ]; then
				echo >&2 "$function: blkid returned device $dev which doesn't exist"
				return 2
			fi
			rootdev=$dev
		fi
		;;
	esac

	case $rootdev in
	/dev/dm-*)
		local node
		node=$(dm_node "$rootdev") || return 1
		if [ "$node" ]; then
			rootdev="$node"
		fi
		;;
	esac

	case $rootdev in
	/dev/mapper/*)
		local dm_subsystem=$(dm_subsystem "$rootdev")
		case $dm_subsystem in
		LVM)
			local node
			node=$(dm_lvm2_name "$rootdev") || return 1
			if [ "$node" ]; then
				rootdev="$node"
			fi
			return 0
			;;
		CRYPT)
			return 0
			;;
		esac
	esac

	if [ "$rootFs" = "nfs" ]; then
		rootdev="/dev/nfs"
		return 0
	fi

	if [ ! -r "$rootdev" ]; then
		echo >&2 "$function: rootfs device file $rootdev doesn't exist"
		return 1
	fi

	return 0
}

# resolve /dev/dm-0 to lvm2 node
# which they got from blkid program fs was specifed as UUID= in fstab
dm_lvm2_name() {
	local node="$1"
	local dm_minor stat

	if [ ! -b "$node" ]; then
		echo >&2 "dm_lvm2_name: $node is not a block device"
		return 1
	fi

	case $node in
	/dev/dm-*)
		dm_minor=${node#/dev/dm-}
		;;
	/dev/mapper/*)
		stat=$(stat -L -c %T "$node") || die "stat failed: $node"
		dm_minor=$((0x$stat))
	;;
	esac

	local lvm_path=$(/sbin/lvdisplay -c 2>/dev/null | awk -F: -vn=$dm_minor '{node=$1; major=$12; minor=$13; if (n == minor) print node}' | xargs)
	if [ -z "$lvm_path" ]; then
		# XXX: this could happen also for non-lvm nodes?
		cat >&2 <<-EOF
		LVM doesn't recognize device-mapper node with minor $dm_minor

		In case your Physical Volumes are device mapper nodes, you should add to lvm.conf:
		    types = [ "device-mapper", 254 ]

		In case the LVM nodes are not present yet, it could be fixed by running:
		# vgscan --mknodes
		EOF
		return 2
	fi
	if [ ! -r "$lvm_path" ]; then
		echo >&2 "lvdisplay returned $lvm_path which doesn't exist in filesystem; try running 'vgscan --mknodes'."
		return 2
	fi
	echo $lvm_path
}

# resolve /dev/dm-0, /dev/mapper/name
# @return	DM name
dm_name() {
	local node="$1"
	dmsetup info -c --noheadings -o name $node
}

# get subsystem name for DM node
# node can be /dev/dm-0, /dev/mapper/name
# @return	subsystem name
dm_subsystem() {
	local node="$1"
	dmsetup info -c --noheadings -o subsystem $node
}

# resolve any dm node to it's full path in /dev/mapper
dm_node() {
	local node="$1"
	printf "/dev/mapper/%s" $(dm_name "$node")
}

# find modules by class eg
# find_modules_by_class 0106 - finds modules for SATA devices in the system
# find_modules_by_class 0c03 - finds modules for USB controllers
find_modules_by_class() {
	local req_class="$1"

	pcimap="/lib/modules/$kernel/modules.pcimap"

	lspci=$(find_tool /sbin/lspci)
	if [ ! -x "$lspci" ]; then
		warn "Failed to execute lspci. Is pciutils package installed?"
	fi

	# no pcimap, nothing to lookup from
	if [ ! -f "$pcimap" ]; then
		return
	fi

	if [ -z "$lspci" ]; then
		return
	fi

	LC_ALL=C lspci -p "$pcimap" -kvmmn | awk -vreq_class="${req_class}" '
					BEGIN      { skip_modules[1]=""; modules[1]=""; xhci=""; ehci=""; ohci=""; uhci="" }
					/^Slot:/   { found=0 }
					/^Class:/  { if (req_class == $2) { found=1 } }
					/^Driver:/ { if (found) {
								module=$2;
								if (module == "xhci_hcd") {
									xhci="xhci_hcd"
								} else if (module == "ehci_hcd") {
									ehci="ehci_hcd"
								} else if (module == "ohci_hcd") {
									ohci="ohci_hcd"
								} else if (module == "uhci_hcd") {
									uhci="uhci_hcd"
								} else if (!(module in skip_modules)) {
									modules[cnt]=module
								}
								skip_modules[cnt]=module;
								cnt++;
						   };
						   found=0
					}
					END { 
						   # xhci/ehci/ohci/uhci hack to preserve such order
						   printf "%s %s %s %s ", xhci, ehci, ohci, uhci;
						   for (i in modules) { printf "%s ", modules[i]; };
					}
	'
}

