#!/bin/sh

set -e

# Edit lvm.conf to add filters, so that we don't see nested PV
# (if our cloud users setup LVM inside their volumes)
# TODO: Support NVME devices (and other types?).

LVMCONF=/etc/lvm/lvm.conf

if ! [ -r /etc/oci/data-disks ] ; then
	exit 0
fi

ALLOW_FILTER=""
for i in $(cat /etc/oci/data-disks) ; do
	if [ -z "${ALLOW_FILTER}" ] ; then
		ALLOW_FILTER="\"a|${i}|\","
	else
		ALLOW_FILTER="${ALLOW_FILTER} \"a|${i}|\","
	fi
done

if ! cat ${LVMCONF} | grep -v -P '^\t#|^$|^\t\t#|^#' | grep -q -P '^[ \t]*global_filter' ; then
	sed -i "/^devices {/a global_filter = [ ${ALLOW_FILTER} \"r|.*|\" ]" ${LVMCONF}
fi

if ! cat ${LVMCONF} | grep -v -P '^\t#|^$|^\t\t#|^#' | grep -q -P '^[ \t]*filter' ; then
	sed -i "/^devices {/a filter = [ ${ALLOW_FILTER} \"r|.*|\" ]" ${LVMCONF}
fi
