# ***** BEGIN LICENSE BLOCK *****
# * Copyright (C) 2007  Alexey Gladkov <legion@altlinux.org>
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
# ***** END LICENSE BLOCK *****

raidtypes=
raidboot=

detect_raidroot() {
	local raid_list= bootdev= md= mddev= uuid= boot_uuid=

	raidtypes="$(sed -ne 's/^md.* \<\(raid[[:alnum:]]\+\)\>.*/\1/p' /proc/mdstat|sort -u)"
	[ -n "$raidtypes" ] ||
		return 0

	raid_list="$(sed -ne 's,^\(md[^[:space:]]\+\) :.*,\1,p' /proc/mdstat |sort)"
	[ -n "$raid_list" ] ||
		return 0

	bootdev=$(get_part_with_mntpt "/boot")

	[ -n "$bootdev" ] ||
		bootdev=$(get_part_with_mntpt "/")

	[ -n "$bootdev" ] ||
		return 0

	boot_uuid="$(blkid -c /dev/null -w /dev/null -o value -s UUID "$bootdev" 2>/dev/null)" ||
		return 0

	for md in $raid_list; do
		[ -e "/dev/$md" ] &&
			uuid="$(blkid -c /dev/null -w /dev/null -o value -s UUID "/dev/$md" 2>/dev/null)" &&
			[ "$boot_uuid" = "$uuid" ] ||
			continue

		mddev="$md"
	done

	if [ -n "$mddev" ]; then
		raidboot="/dev/$mddev"
	fi
}
