# ZFS root filesystem mounting			-*- shell-script -*-

mountroot() {
	wait_for_udev 10

	# Default delay is around 30s
	delay=${ROOTDELAY:-30}

	# loop until mount succeeds
	retry_nr=0
	modprobe zfs
	zpool import -c /etc/zfs/zpool.cache -N -a
	mount -t zfs $ROOT $rootmnt
	while [ $retry_nr -lt $delay -a ! -e $rootmnt/etc/zfs ]
	do
		[ "$quiet" != y ] && log_begin_msg "Retrying zfs mount"
		/bin/sleep 1
		rmmod zfs
		modprobe zfs
		zpool import -c /etc/zfs/zpool.cache -N -a
		mount -t zfs $ROOT $rootmnt
		retry_nr=$(( retry_nr + 1 ))
		[ "$quiet" != y ] && log_end_msg
	done

	if [ -e $rootmnt/etc/zfs ]
	then
		updateneeded=false
		for p in $rootmnt/etc/zfs/*
		do
			case $p in */zpool.cache|*/zdev.conf)
				cmp -s /etc/zfs/${p##*/} $p || updateneeded=true
			esac
		done

		if $updateneeded
		then
			printf 'Warning: /etc/zfs out of date. You may need to update-initramfs -k all -u!\n'
			# update zpool cache and restart from scratch
			rm -rf /etc/zfs
			cp -a $rootmnt/etc/zfs /etc/
			umount $rootmnt
			rmmod zfs
			modprobe zfs
			zpool import -c /etc/zfs/zpool.cache -N -a
			mount -t zfs $ROOT $rootmnt
		fi
	else
		panic "Unable to find ZFS root on '$ROOT'. Dropping you into a shell"
	fi
}
