#!/bin/bash
#
# kpartx_id
#
# Checks whether a given dm table is a table for a partition
# on a multipathed device as created by kpartx.
#
# A table has been created by kpartx if
# 1) it is of type 'linear'
# 2) has exactly one dependency
# 3) this dependency is of type 'multipath'
#

DMSETUP=/sbin/dmsetup

MAJOR=$1
MINOR=$2

if [ -z "$MAJOR" -o -z "$MINOR" ]; then
    echo "usage: $0 major minor"
    exit 1;
fi

# 1) Check for dependencies
tbldeps=$($DMSETUP deps -j $MAJOR -m $MINOR)
if [ $? -ne 0 ] || [ -z "$tbldeps" ]; then
    exit $?
fi

numdeps=${tbldeps%dependen*}
if [ "$numdeps" -ne 1 ]; then
    exit $numdeps
fi

# 2) Check for table type
tbldef=$($DMSETUP table -j $MAJOR -m $MINOR)
if [ $? -ne 0 ] || [ -z "$tbldef" ]; then
    exit $?
fi
set -- $tbldef

if [ "$3" != "linear" ]; then
    echo "Not a linear table"
    exit 10
fi

# 3) Check if the container is a multipath table
tblmajor=${4%:*}
tblminor=${4#*:}

tbldef=$($DMSETUP table -j $tblmajor -m $tblminor)
if [ $? -ne 0 ] || [ -z "$tbldef" ]; then
    exit $?
fi
set -- $tbldef

if [ "$3" != "multipath" ]; then
    exit 20
fi

exit 0
