#!/bin/sh

# This script collects list of iommu groups used by devices in ${VFIO_PCI_DEVS}
# variable of the vfio-bind service configuration.
#
# Name of the vfio-bind configuration file can be passed as 1st argument.

get_groups() {
	for _dev in $@; do
		find /sys/kernel/iommu_groups -name ${_dev} | \
			sed -e 's/^\/sys\/kernel\/iommu_groups\///' -e 's/\/devices.*//'
	done | sort | uniq
}

printf "#\n# Generated by $0, do not edit!\n#\n\n"
printf "clear_emulator_capabilities=0\nuser=\"root\"\ngroup=\"root\"\n\n"
printf "cgroup_device_acl = [\n"
printf "\t\"/dev/null\", \"/dev/full\", \"/dev/zero\", \"/dev/random\", \"/dev/urandom\",\n"
printf "\t\"/dev/ptmx\", \"/dev/kvm\", \"/dev/kqemu\",\n"
printf "\t\"/dev/rtc\", \"/dev/hpet\", \"/dev/net/tun\",\n"
printf "\t\"/dev/vfio/vfio\""

[ -r "$1" ] && . $1 || . /opt/etc/conf.d/vfio-bind

for _grp in $(get_groups ${VFIO_PCI_DEVS}); do
	printf ",\n\t\"/dev/vfio/${_grp}\""
done

printf "\n]\n"
