#!/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-* | /dev/mapper/*)
		local node
		node=$(dm_lvm2_name "$rootdev") || return 1
		if [ "$node" ]; then
			rootdev="$node"
		fi
		;;
	esac

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

	if [ ! -r "$rootdev" ]; then
		echo >&2 "$function: can't find real device for $rootdev"
		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

	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/*)
		dm_minor=$(ls -l $node | awk '{print $6}')
	;;
	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
}

# try to resolve /dev/dm-0 to item from /dev/mapper dir
dm_longname() {
	local node="$1"
	local ret
	local dm_major=$(awk '$2 == "device-mapper" {print $1}' /proc/devices)
	local dm_minor=${node#/dev/dm-}

	local dm_name=$(dmsetup info -c --noheadings -j $dm_major -m $dm_minor | awk -F: '{print $1}')
	echo "/dev/mapper/$dm_name"
}
