#!/bin/sh

. /lib/partman/lib/base.sh

db_register partman-md/confirm partman-md/confirm_nooverwrite

# Load modules and scan arrays if it was not done before
if [ ! -f /var/lib/partman/md ] && [ "$(udpkg --print-os)" = linux ] ; then
	#  load required kernel modules 
	depmod -a >/dev/null 2>&1
	modprobe md-mod >/dev/null 2>&1

	# make sure that md is available
	if [ ! -e /proc/mdstat ]; then
		db_input critical partman-md/nomd
		db_go
		exit 0
	fi

	# Try to detect MD devices, and start them
	# mdadm will fail if /dev/md does not already exist
	mkdir -p /dev/md

	log-output -t md-devices --pass-stdout \
		mdadm --examine --scan --config=partitions >/tmp/mdadm.conf

	log-output -t md-devices \
		mdadm --assemble --scan --run --config=/tmp/mdadm.conf --auto=yes

	touch /var/lib/partman/md
fi
