#!/bin/sh

# Restart tgtd to make sure modules are loaded
invoke-rc.d tgt restart || echo "Failed to restart tgt" >&2

# Test tgtd module usability
supported_bs=$(tgtadm --mode sys --op show | \
		awk '
			BEGIN { p = 0 };
			/^[a-zA-Z]/ { p = 0 };
			/^Backing stores/ { p = 1; getline; };
			{ if (p) { gsub("^ +", ""); print };
		}')
ret=0

bs_list="aio"
# glusterfs, and thus tgt-glusterfs, is not available in all architectures, so
# let's exclude it if it's not installed
if dpkg -l tgt-glusterfs 2>/dev/null | grep -q ^ii; then
    bs_list="glfs $bs_list"
else
    echo "WARNING: glusterfs not available on arch $(dpkg --print-architecture)"
fi
# rbd, and thus tgt-rbd, is not available in all architectures, so
# let's exclude it if it's not installed
if dpkg -l tgt-rbd 2>/dev/null | grep -q ^ii; then
    bs_list="rbd $bs_list"
else
    echo "WARNING: rbd not available on arch $(dpkg --print-architecture)"
fi
for bs in ${bs_list}; do
	if echo "$supported_bs" | grep -q "\b$bs\b"; then
		echo "OK - $bs supported"
	else
		echo "ERROR - $bs not supported" >&2
		ret=1
	fi
done

exit $ret
