#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_iscsi_lun.sh 1.7.1.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)54    1.7.1.2  src/bos/usr/lib/nim/methods/c_iscsi_lun.sh, cmdnim, bos720  7/15/11  02:24:34
# @(#)54    1.7.1.2  src/bos/usr/lib/nim/methods/c_iscsi_lun.sh, cmdnim, bos720  7/15/11  02:24:34
#
##############################################################################
# - Remaining issue: serialization on mapping and acl files.
#
###############################################################################
# c_iscsi_lun -a operation ="configure|unconfigure|update|grant_acl|revoke_acl"
#             -a target=targetname
#             -a dump=dump_location
#             -a dumpsize=dump_size
#             -a dump_port=port
#             -a lun_id=lun_id
#             -a ipaddr=ip_address
#
###############################################################################
#
# c_iscsi_lun -a operation=configure -a target=targetname -a dump=dump_location
#             -a dumpsize=dump_size -a dump_port=port
#
# c_iscsi_lun -a operation=unconfigure -a target=targetname -a lun_id=lunid
#
# c_iscsi_lun -a operation=update -a target=targetname -a dump=dump_location
#              -a dumpsize=dump_size -a lun_id=lun_id
#
# c_iscsi_lun -a operation=grant_acl -a target=targetname -a lun_id=lun_id 
#             -a ipaddr=ip_address
#
# c_iscsi_lun -a operation=revoke_acl -a target=targetname -a lun_id=lun_id 
#             -a ipaddr=ip_address
 
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib

MAPPINGFILE=/etc/tmiscsi/mappings
BACKMAPPINGFILE=/etc/tmiscsi/mappings.back
# format: luname startblk:nbblk:perm:filename

ACLFILE=/etc/tmiscsi/access_lists
BACKACLFILE=/etc/tmiscsi/access_lists.back
# format: luname ip@,ip@

ONEGB=1073741824 #bytes
ONEGBBLOCK=` ${EXPR} $ONEGB / 512`

REMOTEDUMPLOG="/var/adm/ras/remotedump.log"
FIRMWARE_LIMITATION=1

#############################################################################
# FUNCTION: getluname 
#
# PARAMETERS: input : target_iqn
#             input : lun_id
#
# DESCRIPTION:returns the luname of the lun_id defined in the target_iqn 
#############################################################################
getluname() {
	${ECHO} "Entering getluname function: $*" >> ${REMOTEDUMPLOG}

	target_iqn=$1
	lun_id=$2 
	luname=""

	target_name=`${ODMGET} -q "attribute=iscsi_name and value=$target_iqn" CuAt | ${GREP} -w name | ${CUT} -d\" -f2` 
	lunamelist=`${ODMGET} -q "attribute=lun_id and value=$lun_id" CuAt | ${GREP} name | ${CUT} -d\" -f 2` 
	set -- $lunamelist
	while [ $1 -a -z "$luname" ]
	do
		if [ `${ODMGET} -q name=$1 CuDv | ${GREP}  parent | ${CUT} -d\" -f2` = "$target_name" ] 
		then 
			luname=$1
		fi
		shift
	done
	
	${ECHO} $luname
}
#############################################################################
# FUNCTION: add_acl 
#
# PARAMETERS: input : object(target or lun) on which acl has to be added
#             input : ip address ( acl)
#
# DESCRIPTION: add an acl (ip value)on the object. 
#############################################################################
add_acl() {
	${ECHO} "Entering add_acl function: $*" >> ${REMOTEDUMPLOG}

    obj=$1
    ip=$2

    aclline=`${GREP} -w "^$obj" $ACLFILE`
    [ -z "$aclline" ]  && {
		# add new line
		${ECHO} "$obj\t$ip" >> $ACLFILE
		return 0
    }
	# is ACL already present ?
	[ `${ECHO} $aclline | ${GREP} -c $ip` -ne 0 ] && return 0
 
    # modify line
    newaclline="$aclline,$ip" 
    ${SED} "s/$aclline/$newaclline/" $ACLFILE > $BACKACLFILE
    ${MV} $BACKACLFILE $ACLFILE

    return 0 
}

#############################################################################
# FUNCTION: del_acl 
#
# PARAMETERS: input : object(target orlun) on which acl has to be deleted
#             input : ip address ( acl)
#
# DESCRIPTION: delete the acl (ip value) on the object. 
#############################################################################
del_acl() {   
	${ECHO} "Entering del_acl function: $*" >> ${REMOTEDUMPLOG}

    obj=$1
    ip=$2

    oldaclline=`${GREP}  "^$obj.*$ip" $ACLFILE | ${GREP} -w $obj`
    [ -z "$oldaclline" ]  && {
		${C_ERRMSG} ${ERR_FAIL_ACL_DELETE} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
    }
    aclline=`${ECHO} $oldaclline | tr ',' ' '`
    set -- $aclline

    newaclline=`${ECHO} "$1\t"`
    shift
    iplist=
    while [ "$1" ] 
    do
		[ "$1" != "$ip" ] && {
			if [ -z "$iplist" ] 
			then 
				iplist=$1;
			else
				iplist="$iplist,$1"
			fi
		}
		shift
    done
	if [ -n "$iplist" ] 
	then
		newaclline="$newaclline $iplist"
		${SED} "s/$oldaclline/$newaclline/" $ACLFILE > $BACKACLFILE
	else
		${GREP} -wv "^$obj" $ACLFILE > $BACKACLFILE	
	fi
    ${MV} $BACKACLFILE $ACLFILE

    return 0
}
#############################################################################
# FUNCTION: del_lunacls
#
# PARAMETERS: input : lun on which acl has to be deleted
#
# DESCRIPTION: delete all the acls  on the object. 
#############################################################################
del_lunacls() {   
	${ECHO} "Entering del_lunacls function: $*" >> ${REMOTEDUMPLOG}

	luname=$1
	${GREP} -wv "^$luname" $ACLFILE > $BACKACLFILE
	${MV} $BACKACLFILE $ACLFILE
}
#############################################################################
# FUNCTION: add_mapping
#
# PARAMETERS: input : luname
#             input : dump_location 
#             input : dumpsize
#
# DESCRIPTION: add the mappings relative to luname in the mapping file.
#
#############################################################################
add_mapping (){
	${ECHO} "Entering add_mapping function: $*" >> ${REMOTEDUMPLOG}

	luname=$1
    	dumplocation=$2
    	dumpsize=$3
	
	cp $MAPPINGFILE $BACKMAPPINGFILE >/dev/null 2>&1
	[ $? -ne 0 ] && {
		${C_ERRMSG} ${ERR_FAIL_MAPPING_ADD} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a  ${REMOTEDUMPLOG} 1>&2
		return 1 
	}
	# fill in mapping file
	# FIRMWARE_LIMITATION=1 if f/w is not lifted.
	if [ -n "$FIRMWARE_LIMITATION" ] 
    	then 
		# lu0 0:1:RO:/tftpboot/172.16.129.213.iplrecord.
		# At the spot definition, severals iplrecord are created 
		# (one for each networkinterface) 
		# /tftpboot/<myspot>.iplrecord.$netinterface.
		# When the spot is allocated to a client, a link 
		# /tftpboot/<client_ipaddr>.iplrecord is created and points to 
		# the right iplrecord depending on the network interface.
		# Finally when the client connects to the iSCSI Target, the filename 
		# /tftpboot/%i.iplrecord is expanded to 
		# /tftpboot/<client_ipaddr>.iplrecord
		
		${ECHO} "$luname\t0:1:RO:/tftpboot/%i.iplrecord" >> $BACKMAPPINGFILE

		# lu0 1:7:RO:/export/eznim/dump/dumpdiskhdr
		${ECHO} "$luname\t1:7:RO:$dumplocation/dumpdiskhdr" >> $BACKMAPPINGFILE

		# lu0 8:2097144:RO:/tftpboot/172.16.129.213.boot
		# 2097152-8= 2097144
		# At the spot definition, several boot images are created 
		# (one for each network interface) 
		# /tftpboot/<myspot>.boot.$netinterface.
		# When the spot is allocated to a client, a link 
		# /tftpboot/<client_ipaddr>.boot is created and points to the right
		# bootimage depending on the network interface.
		# Finally when the client connects to the iSCSI Target, the filename 
		# /tftpboot/%i.boot is expanded to /tftpboot/<client_ipaddr>.boot 
		bootsize=` ${EXPR} $ONEGBBLOCK - 8 `
		${ECHO} "$luname\t8:$bootsize:RO:/tftpboot/%i.boot" >> $BACKMAPPINGFILE

		# lu0 2097152:104857600:RW:/export/eznim/dump/client/dump
		# when the client connects to the iSCSI Target, the filename 
		# /export/<mydump>/%i/dump is expanded  to 
		# /export/<mydump>/client_ipaddr/dump
		nbblock=` ${EXPR} $dumpsize / 512 `
		${ECHO} "$luname\t$ONEGBBLOCK:$nbblock:RW:$dumplocation/%i/dump"  >> $BACKMAPPINGFILE
	else
		# lu0 0:8:RO:/export/eznim/dump/dumpdiskhdr
		${ECHO} "$luname\t0:8:RO:$dumplocation/dumpdiskhdr" >> $BACKMAPPINGFILE

		# lu0 8:104857600:RW:/export/eznim/dump/client/dump
		# when the client connects to the iSCSI Target, the filename 
		# /export/<mydump>/%i/dump is expanded  to 
		# /export/<mydump>/client_ipaddr/dump
		nbblock=` ${EXPR} $dumpsize / 512 `
		${ECHO} "$luname\t8:$nbblock:RW:$dumplocation/%i/dump" >> $BACKMAPPINGFILE
	fi
	${MV} $BACKMAPPINGFILE $MAPPINGFILE
}

#############################################################################
# FUNCTION: del_mapping
#
# PARAMETERS: input : luname
#
# DESCRIPTION: delete the mappings relative to a luname
#
#############################################################################
del_mapping (){
	${ECHO} "Entering del_mapping function: $*" >> ${REMOTEDUMPLOG}

	luname=$1
	${GREP} -wv "^$luname" $MAPPINGFILE > $BACKMAPPINGFILE
	${MV} $BACKMAPPINGFILE $MAPPINGFILE
}
############################################################################
# FUNCTION: confirm_fileset
#
# PARAMETERS: input: fileset name
#
# DESCRIPTION: confirm that the prerequite fileset has been installed
#              If the fileset has not been installed, returns an error.
############################################################################
confirm_fileset() {
        ${ECHO} "Entering confirm_fileset function: $*" >> ${REMOTEDUMPLOG}
        prereq=$1
        ERR=`${LSLPP} -L $prereq 2>&1 1>/dev/null`
        [ $? -ne 0 ] && {
                ${C_ERRMSG} ${ERR_FILESET_REQUIRED} ${C_ERRMSG_ERR} "${prereq}" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
                        return 1
                }
        return 0
}


#############################################################################
# FUNCTION: config_target
#
# PARAMETERS: input: target
#             input: iscsi_port
#
# DESCRIPTION: Configure the target 
#              Enable ACL mode
#              Add target in acl data base 
#              If the acl can not be turned or if port is already inuse 
#              returns an error.
#############################################################################
config_target() {
	${ECHO} "Entering config_target function: $*" >> ${REMOTEDUMPLOG}

	target_iqn=$1
	port=$2
	new=0

	[ `${LSDEV} -l tmsw0 | wc -l` -eq 0 ] && {
	    ERR=`${MKDEV} -c driver -s node -t tmsw -l tmsw0 -a access_control=on 2>&1 1>/dev/null`
		[ $? -ne 0 ] && {
			${C_ERRMSG} ${ERR_FAIL_CONFIGURE_DEV} ${C_ERRMSG_ERR} "tmisci driver"  "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
			${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
			return 1
		}
	}
  
    if [ "`${LSDEV} -l tmsw0 -F status`" != "Available" ] 
    then 
		# turn on ACL
		ERR=`${CHDEV} -l tmsw0 -a access_control=on 2>&1 1>/dev/null`
		[ $? -ne 0 ] && {
			${C_ERRMSG} ${ERR_FAIL_ACL_SET} ${C_ERRMSG_ERR} "tmsw" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
			${ECHO} ${ERR} | ${TEE} -a  ${REMOTEDUMPLOG} 1>&2
			return 1
		}

		ERR=`/usr/sbin/cfgmgr -l tmsw0 2>&1 1>/dev/null`
		[ $? -ne 0 ] && {
			${C_ERRMSG} ${ERR_FAIL_CONFIGURE_DEV} ${C_ERRMSG_ERR} "tmsw driver" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
			${ECHO} ${ERR} | ${TEE} -a  ${REMOTEDUMPLOG} 1>&2
			return 1
		}
    else
		# check ACL is enable
		[ `${LSATTR} -El tmsw0 -a access_control -F value` != "on" ] && {
			${C_ERRMSG} ${ERR_FAIL_RESET_TMSW} ${C_ERRMSG_ERR} "tmsw0" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
			return 1
		}
    fi

	# already defined ?
	target_name=`${ODMGET} -q "attribute=iscsi_name and value=$target_iqn" CuAt | ${GREP} -w name | ${CUT} -d\" -f2` 
	[ -z "$target_name" ] && {
		target_name=`${MKDEV} -c tmiscsi -s tmtarget  -t target \
				-p tmiscsi0 -a owner=tmsw0 \
				-a iscsi_name=$target_iqn \
				-a paths=any:$port,1 \
				-a force_logout=hard | ${AWK} '{print \$1}'`
		[ -z "$target_name" ] && {
			${C_ERRMSG} ${ERR_FAIL_CONFIGURE_DEV} ${C_ERRMSG_ERR} "iscsi_dump port" "" "" "" | ${TEE} -a  ${REMOTEDUMPLOG} 1>&2
			return 1
		}
		new=1
	}

	add_acl $target_name any
	[ $? -ne 0 ] && {
		[ -n "$new" ] && ${RMDEV} -dl $target_name >/dev/null 2>&1
		${C_ERRMSG} ${ERR_FAIL_ACL_ADD} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1	
	}

	# reconfigure the target in order to load new acl list.
	ERR=`/usr/sbin/cfgmgr -l $target_name 2>&1 1>/dev/null`
	[ $? -ne 0 ] && {
		${C_ERRMSG} ${ERR_FAIL_CONFIGURE_DEV} ${C_ERRMSG_ERR} "$target_name" "" "" ""  | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}
    return 0
}


#############################################################################
# FUNCTION: unconfig_target
#
# PARAMETERS: input: target_iqn
#
# DESCRIPTION: unConfigure the target 
#              If the target is no more use, remove its ACL
#############################################################################
unconfig_target() {
	${ECHO} "Entering unconfig_target function: $*" >> ${REMOTEDUMPLOG}

	target_iqn=$1
  	target_name=`${ODMGET} -q "attribute=iscsi_name and value=$target_iqn" CuAt | ${GREP} -w name | ${CUT} -d\" -f2` 

	# any AVAILABLE child ?
	[ `${LSDEV} -p $target_name -S Available | wc -l ` -eq 0 ] && {  
		del_acl $target_name any
		[ $? -ne 0 ] && {
			${C_ERRMSG} ${ERR_FAIL_ACL_DELETE} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
		}
		ERR=`${RMDEV} -dl $target_name 2>&1 1>/dev/null`
		[ $? -ne 0 ] && {  
			${C_ERRMSG} ${ERR_FAIL_CONFIGURE_DEV} ${C_ERRMSG_ERR} "$target_name" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
			${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
			return 1
		}
	}

	return 0
}

#############################################################################
# FUNCTION: config_lun
#
# PARAMETERS: input: target_iqn
#             input : dump_location,  /export/<mydump>
#             input : dumpsize
#
# DESCRIPTION:  add a lun , fill in the mappings file and
#              reconfigures it .
#
#############################################################################
config_lun (){
	${ECHO} "Entering config_lun function: $*" >> ${REMOTEDUMPLOG}

	target_iqn=$1
  	dumplocation=$2
	dumpsize=$3

	target_name=`${ODMGET} -q "attribute=iscsi_name and value=$target_iqn" CuAt\
				| ${GREP} -w name | ${CUT} -d\" -f2`

	# dump arbitrary starts at the block number ONEGBBLOCK : ONEGB bytes
	# So dev_size shall include this size:
	back_dev_size=` ${EXPR} $dumpsize + $ONEGB `
	luname=`${MKDEV} -c tmiscsi -s tmdev -t lu -p $target_name\
	    -a back_dev_type="file"\
	    -a back_dev_option="multirange"\
		-a dev_size=$back_dev_size  | ${AWK} '{print \$1}'`
	[ -z "$luname" ] && {  
		    ${C_ERRMSG} ${ERR_FAIL_CONFIGURE_DEV} ${C_ERRMSG_ERR} "$luname" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		    return 1
	}

	# fix mappings and acl file for potentiel data base inconsistencies
	del_mapping $luname
	del_lunacls $luname

	add_mapping $luname $dumplocation $dumpsize 
	[ $? -ne 0 ] && { 
	    ${RMDEV} -dl $luname >/dev/null 2>&1
		    ${C_ERRMSG} ${ERR_FAIL_MAPPING_ADD} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		    return 1
	}

	# reconfigure the lun in order to load new acl list.
	ERR=`/usr/sbin/cfgmgr -l $luname 2>&1 1>/dev/null`
	[ $? -ne 0 ] && {
		${C_ERRMSG} ${ERR_FAIL_CONFIGURE_DEV} ${C_ERRMSG_ERR} "$luname" "" "" "" | ${TEE} -a  ${REMOTEDUMPLOG} 1>&2
		${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}
	# return lun_id
	${ECHO} "dump_lunid=`${LSATTR} -El $luname -a lun_id | ${CUT} -d' ' -f2`" | ${TEE} -a  ${REMOTEDUMPLOG}
	return 0
}

#############################################################################
# FUNCTION: unconfig_lun
#
# PARAMETERS: input: target_iqn
#             input : lun_id
#
# DESCRIPTION: Delete the mappings relative to this lun_id, target_iqn and
#              unconfi     gures it .
#
#############################################################################
unconfig_lun (){
	${ECHO} "Entering upconfig_lun function: $*" >> ${REMOTEDUMPLOG}


	target_iqn=$1
	lun_id=$2 
	luname=""

	luname=`getluname $target_iqn $lun_id`
	[ -z "$luname" ] && {
		${C_ERRMSG} ${ERR_FAIL_UNDEFINED_LUN} ${C_ERRMSG_ERR} "$lun_id" "$target_iqn" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}

	# remove mapping 
	del_mapping $luname
    [ $? -ne 0 ] && {
		${C_ERRMSG} ${ERR_FAIL_MAPPING_DELETE} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}
	
	# back_file parameter to be removed
    ERR=`${RMDEV} -dl $luname 2>&1 1>/dev/null`
    [ $? -ne 0 ] && {  
		${C_ERRMSG} ${ERR_FAIL_LUN_DELETE} ${C_ERRMSG_ERR} "$luname" "" "" ""	| ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
    }
     
   return 0
}

#############################################################################
# FUNCTION: update_lun
#
# PARAMETERS: input : target_iqn
#             input : lun_id
#             input : dumplocation
#             input : dumpsize
#
#
# DESCRIPTION: update the configuration of the lun. (unconfigure and configure
#             it).
#
#############################################################################
update_lun (){
	${ECHO} "Entering update_lun function: $*" >> ${REMOTEDUMPLOG}

	target_iqn=$1
	lun_id=$2 
	dump=$3
	dumpsize=$4

	#
	# May be we could check here that the lun_id has no defined ACL .
	# This check would be equivalent to "dump resource not allocated".
	# 

	unconfig_lun $target_iqn $lun_id
	[ $? -ne 0 ] && return 1

	config_lun $target $dump $dumpsize 
	[ $? -ne 0 ] && return 1
	
	return 0
}

#############################################################################
# FUNCTION: grant_access
#
# PARAMETERS: input : target_iqn
#             input : lun_id
#             input : ip
#
# DESCRIPTION: Permit to the machine whose address is ip to access the lun_id
#             of the target_iqn
#############################################################################
grant_access () {
	${ECHO} "Entering grant_access function: $*" >> ${REMOTEDUMPLOG}

	target_iqn=$1
	lun_id=$2
	ip=$3
	luname=""

	luname=`getluname $target_iqn $lun_id`
	[ -z "$luname" ] && {
		${C_ERRMSG} ${ERR_FAIL_UNDEFINED_LUN} ${C_ERRMSG_ERR} "$lun_id" "$target_iqn" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}

	add_acl $luname $ip
	[ $? -ne 0 ] && {
		${C_ERRMSG} ${ERR_FAIL_ACL_ADD} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}
	#reconfigure the lun
	ERR=`/usr/sbin/cfgmgr -l $luname 2>&1 1>/dev/null`
	[ $? -ne 0 ] && {
		${C_ERRMSG} ${ERR_FAIL_CONFIGURE_DEV} ${C_ERRMSG_ERR} "$luname" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}

	return 0
}

#############################################################################
# FUNCTION: revoke_access
#
# PARAMETERS: input : target_iqn
#             input : lun_id
#             input : ip
#
# DESCRIPTION: Forbid to the machine whose address is ip to access the lun_id
#             of the target_iqn
#############################################################################
revoke_access () {
	${ECHO} "Entering revoke_access function: $*" >> ${REMOTEDUMPLOG}
	
	target_iqn=$1
	lun_id=$2
	ip=$3
	luname=""

	luname=`getluname $target_iqn $lun_id`
	[ -z "$luname" ] && {
		${C_ERRMSG} ${ERR_FAIL_UNDEFINED_LUN} ${C_ERRMSG_ERR} "$lun_id" "$target_iqn" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}

	del_acl $luname $ip
	[ $? -ne 0 ] && {
		${C_ERRMSG} ${ERR_FAIL_ACL_DELETE} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}

	#reconfigure the lun
	ERR=`/usr/sbin/cfgmgr -l $luname 2>&1 1>/dev/null`
	[ $? -ne 0 ] && { 
		${C_ERRMSG} ${ERR_FAIL_CONFIGURE_DEV} ${C_ERRMSG_ERR} "$luname" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}
	return 0
}

#############################################################################
# FUNCTION:main c_iscsi_lun
#
# PARAMETERS: -a operation="configure|unconfigure|update|grant_acl|revoke_acl"
#             -a target=targetname(iqn)
#             -a dump=dump_location
#             -a dumpsize=dump_size
#             -a dump_port=port
#             -a lun_id=lun_id
#             -a ipaddr=ip_address
#             -v 
#
#
# DESCRIPTION: c_iscsi_lun main function.
# targetname=iqn.2009-19.com.ibm.master.nim:AIX.NIM.resource.server:`uname -n`
# should be given as parameter
#############################################################################
${ECHO} "" >> ${REMOTEDUMPLOG}
${DATE} >> ${REMOTEDUMPLOG}
${ECHO} "Entering c_iscsi_lun script: $*" >> ${REMOTEDUMPLOG}

CONFIGURE_REQUIRED_ATTRS="operation target dump dumpsize dump_port"
UNCONFIGURE_REQUIRED_ATTRS="operation target lun_id"
UPDATE_REQUIRED_ATTRS="operation target dump dumpsize lun_id"
GRANT_REQUIRED_ATTRS="operation target lun_id ipaddr"
REVOKE_REQUIRED_ATTRS="operation target lun_id ipaddr"

operation=""
target=""
dump=""
dumpsize=""
dump_port=""
lun_id=""
ipaddr=""
operationisset=""


while getopts a:v c 
do
	case ${c} in
		a)   # validate the attr ass
		   operationisset=${OPTARG#operation=}
		   if [ -z "$operationisset" ]
		   then 
				print "wrong syntax: operation has to be 1rst parameter"
		   elif [ -z "$operation" ] 
                then 
					operation=${OPTARG#*=}
					case $operation in
						"configure") 
							REQUIRED_ATTRS=$CONFIGURE_REQUIRED_ATTRS;;
						"unconfigure")
							REQUIRED_ATTRS=$UNCONFIGURE_REQUIRED_ATTRS;;
						"update")
							REQUIRED_ATTRS=$UPDATE_REQUIRED_ATTRS;;
						"grant_acl")
							REQUIRED_ATTRS=$GRANT_REQUIRED_ATTRS;;
						"revoke_acl") 
							REQUIRED_ATTRS=$REVOKE_REQUIRED_ATTRS;;
						*) 
						print "Wrong operation: $operation \n\
Only configure, unconfigure, update, grant_acl, revoke_acl\
are allowed" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
							exit 1;;
					esac
             fi
			
			parse_attr_ass "${OPTARG}"
			eval ${variable}=\"${value}\"
			;;
		v)   # verbose mode (for debugging)
			set -x
			for i in $(typeset +f)
			do
				typeset -ft $i
			done
	 		  ;;
	    \?)     # unknown option
           print ${ERR_BAD_OPT} ${OPTARG} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
          ;;
        esac
done

# check for missing attrs
ck_attrs

# consider lun_id as hexa number even if 0x is missing
# add 0x if missing for ODM compliance check
[ -n "$lun_id" ] && lun_id=0x${lun_id#0x} 

case $operation in
	"configure") 
                confirm_fileset "devices.tmiscsw.rte"
                [ $? -ne 0 ] && exit 1
		config_target $target $dump_port
		[ $? -ne 0 ] && exit 1
 		config_lun $target $dump $dumpsize 
		[ $? -ne 0 ] && {
			unconfig_target $target 
			exit 1
		}
		;;
	"unconfigure")
		unconfig_lun $target $lun_id
		[ $? -ne 0 ] && exit 1
		unconfig_target $target 
		[ $? -ne 0 ] &&  exit 1
 		;;
	"update")
		update_lun $target $lun_id $dump $dumpsize 
		[ $? -ne 0 ] && exit 1
		;;
	"grant_acl")
		grant_access $target $lun_id $ipaddr
		[ $? -ne 0 ] && exit 1
		;;
	"revoke_acl")
		revoke_access $target $lun_id $ipaddr
		[ $? -ne 0 ] && exit 1
		;;
	*)
		;;
esac
exit 0
