#!/bin/sh

[ -f /proc/mdstat ] || exit 0

WITHOUT_RC_COMPAT=1
. /etc/init.d/functions

if [ -f /etc/mdadm.conf ] && absolute mdadm >/dev/null 2>&1; then
	raidstop='mdadm --stop'
elif [ -f /etc/raidtab ] && absolute raidstop >/dev/null 2>&1; then
	raidstop=raidstop
else
	exit 1
fi

mdlist="$(sed -n 's/^\(md[^:[:space:]]*\).*/\1/p' /proc/mdstat)"
[ -n "$mdlist" ] || exit 0

rootdev="$(mount 2>/dev/null |awk '$2=="on" && $3=="/" {print $1}')"
rootdev="${rootdev#/dev/}"

for mddev in ${mdlist}; do
	if [ "$mddev" = "$rootdev" -o -z "${rootdev##${mddev}p*}" ]; then
		continue
	fi
	action "Turning off RAID for $mddev:" $raidstop "/dev/$mddev"
done
