#!/bin/sh

. /usr/share/debconf/confmodule

db_register partman-zfs/confirm partman-zfs/confirm_nooverwrite

# Load modules and scan volume groups if it was not done before
if [ ! -f /var/lib/partman/zfs ] && [ "$(udpkg --print-os)" = kfreebsd ] ; then
	# load required kernel modules
	kldload zfs >/dev/null 2>&1

	# make sure that zfs is available
	if ! test -e /dev/zfs ; then
		db_input critical partman-zfs/nozfs
		db_go
		exit 0
	fi

	# scan for zfs pools
	log-output -t partman zpool import -a -f -o altroot=/target

	# Some ZFS versions don't create cachefile when "-o altroot" is used.
	# Request it explicitly.
	for vg in $(zpool list -H -o name) ; do
		log-output -t partman-zfs zpool set cachefile=/boot/zfs/zpool.cache "$vg" || return 1
	done

	touch /var/lib/partman/zfs
fi
