File iucv_configure of Package s390-tools

#! /bin/sh
#
# iucv_configure
#
# Configures an IUCV device
#
# Usage:
#   iucv_configure <router> <online>
#
# Return values:
#   1  sysfs not mounted
#   2  Invalid status for <online>
#   3  Could not create iucv device
#   4  Could not remove iucv device
#

DEBUG=no

mesg () {
    echo "$@"
}

debug_mesg () {
    case "$DEBUG" in
        yes) mesg "$@" ;;
        *) ;;
    esac
}

if [ $# -ne 2 ] ; then
    echo "Usage: $0  <router> <online>"
    exit 1
fi

# Get the mount point for sysfs
while read MNTPT MNTDIR MNTSYS MNTTYPE; do
    if test "$MNTSYS" = "sysfs"; then
	SYSFS="$MNTDIR"
	break;
    fi 
done </proc/mounts

if [ -z "$SYSFS" ]; then
    exit 1
fi

ROUTER=$1
ONLINE=$2

if [ -z "$ROUTER" ] ; then
    debug_mesg "No IUCV user name given"
    exit 2
fi

if [ -z "$ONLINE" ] ; then
    ONLINE=1
fi

_iucv_dir=${SYSFS}/bus/iucv/devices

_iucv_drv=${SYSFS}/bus/iucv/drivers/netiucv
if [ ! -d "$_iucv_drv" ] ; then
    modprobe -q netiucv
fi

debug_mesg "Configuring IUCV device ${ROUTER}"

for _iucv_dev in $_iucv_dir/* ; do
    [ -d $_iucv_dev ] || continue
    read user < $_iucv_dev/user
    if [ "$user" = "$ROUTER" ] ; then
	# Already configured, ok
	iucvdev=${_iucv_dev##*/}
	break;
    fi
done

if [ -z "$iucvdev" -a $ONLINE -eq 1  ] ; then
    echo $ROUTER > $_iucv_drv/connection
    if [ $? -ne 0 ] ; then
	exit 3
    fi
    for _iucv_dev in $_iucv_dir/* ; do
	[ -d $_iucv_dev ] || continue
	read user < $_iucv_dev/user
	if [ "$user" = "$ROUTER" ] ; then
	    iucvdev=${_iucv_dev##*/}
	    break;
	fi
    done
    if [ "$iucvdev" ] ; then
	debug_mesg "Configured device $iucvdev"
    fi
elif [ "$iucvdev" -a $ONLINE -eq 0 ] ; then
    for _net_dev in $_iucv_dir/$iucvdev/net/* ; do
	[ -d $_net_dev ] || continue
	netdev=${_net_dev##*/}
	break;
    done
    if [ "$netdev" ] ; then
	echo $netdev > $_iucv_drv/remove
	if [ $? -ne 0 ] ; then
	    exit 4
	else
	    debug_mesg "Removed device $iucvdev"
	    rm -f /etc/udev/rules.d/51-iucv-$ROUTER.rules
	    iucvdev=
	fi
    fi
    
fi

if [ "$iucvdev" ] ; then
    cat > /etc/udev/rules.d/51-iucv-$ROUTER.rules <<EOF
ACTION=="add", SUBSYSTEM=="subsystem", KERNEL=="iucv", RUN+="/sbin/modprobe netiucv"
ACTION=="add", SUBSYSTEM=="drivers", KERNEL=="netiucv", ATTR{connection}="$ROUTER"
EOF
fi