#! /usr/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_config_dump.sh 1.5.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,2010 
# 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 
# @(#)52    1.5.1.1  src/bos/usr/lib/nim/methods/c_config_dump.sh, cmdnim, bos720  3/9/10  11:57:27
############################################################################
# c_config_dump -a configdump=[selective | full |none ]
#               -a target=targetname
#               -a lun_id=lun_id
#               -a dump_port=port
#               -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

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

############################################################################
# FUNCTION: get_current_dump
#
# PARAMETERS: input : 
#             
# DESCRIPTION: print the current iscsi dump configration if any 
#              (target_iqn, port, ipaddr, lun_id)
#            1- get the current configure dump device
#            2- check it is a iscsi disk 
#            3- print the disk name and its iscsi attributes 
#               (target_iqn, port, ipaddr, lun_id)
#
############################################################################
get_current_dump() { 
	${ECHO} "Entering get_current_dump function: $*" >> ${REMOTEDUMPLOG}

	primary=`${SYSDUMPDEV} -l | ${GREP} primary | ${CUT} -f "3" -d /`
	[ -z "$primary" -o "$primary" = "sysdumpnull" ] && return 0

	parent=`${LSDEV} -l $primary -F parent`
	[ "$parent" != "iscsi0" ] && return 0

	${LSATTR} -El $primary -F value -a target_name\
		-a port_num\
		-a host_addr\
		-a lun_id\
		| tr '\n' ' '\
		| read target_iqn portx ipaddr lun_id
	port=`printf "%d" $portx`
	
	${ECHO}  "$primary $target_iqn $port $ipaddr $lun_id"
	return 0
} 
############################################################################
# FUNCTION: configure_dump
#
# PARAMETERS: input : target_iqn port ipaddr lun_id configdump
# DESCRIPTION: 
#            1- get the current configure iscsi disk ( if any)
#            2- configure the new  device
#            3- destroy old disk
#
#############################################################################
configure_dump() {
	${ECHO} "Entering configure_dump function: $*" >> ${REMOTEDUMPLOG}

	target_iqn=$1
	port=$2
	ip=$3
	lun_id=$4
	mode=$5

	# 1- get the current configure iscsi disk ( if any)
	get_current_dump\
		| read old_disk old_target old_dump_port old_ipaddr old_lun_id

	# 2- configure the new dump device
	disk=`${NIM_METHODS}/c_disc_target -a operation=discover\
		 -a target=$target\
		 -a dump_port=$dump_port\
		 -a ipaddr=$ipaddr\
		 -a lun_id=$lun_id`
	[ -z "$disk" ] && {
		${C_ERRMSG} ${ERR_DUMP_NO_DISK_AVAIL} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}	
		
	case $mode in
		full) 
			ERR=`${SYSDUMPDEV} -f require 2>&1 1>/dev/null`
			[ $? -ne 0 ] && {
				${C_ERRMSG} ${ERR_SYSDUMPDEV_F_FAIL} ${C_ERRMSG_ERR} "require" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
				${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
				return 1
			}
			;;
		selective)
			ERR=`${SYSDUMPDEV} -f disallow 2>&1 1>/dev/null`
			[ $? -ne 0 ] && {
				${C_ERRMSG} ${ERR_SYDDUMPDEV_F_FAIL} ${C_ERRMSG_ERR} "disallow" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
				${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
				return 1
			}
			;;
		*) 
			${C_ERRMSG} ${ERR_WRONG_PARAM} ${C_ERRMSG_ERR} "$mode" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
			return 1
			;;
	esac
	
	ERR=`${SYSDUMPDEV} -Pp /dev/$disk 2>&1 1>/dev/null`
	[ $? -ne 0 ] && {
		${C_ERRMSG} ${ERR_SYSDUMPDEV_P_FAIL} ${C_ERRMSG_ERR} "$disk" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}
	
	# 3- unconfigure the old device if not current
	[ -n "$old_disk" -a\
	  "$old_disk" != "$disk" -a\
	  -n "$old_target" -a\
	  -n "$old_dump_port" -a\
	  -n "$old_ipaddr" -a\
	  -n "$old_lun_id" ] && {
		${NIM_METHODS}/c_disc_target -a operation=destroy\
			-a target=$old_target\
			-a dump_port=$old_dump_port\
			-a ipaddr=old_ipaddr\
			-a lun_id=$old_lun_id

		return $?
    }
	
	return 0
}
############################################################################
# FUNCTION: unconfigure_dump
#
# PARAMETERS: input : 
# DESCRIPTION: 
#            1- get the current configure iscsi disk ( if any)
#            2- unconfigure device
#            3- destroy old configure disk
#
#############################################################################
unconfigure_dump() {
	${ECHO} "Entering unconfigure_dump function: $*" >> ${REMOTEDUMPLOG}


	# 1- get the current configure iscsi disk ( if any)
	get_current_dump | \
		read old_disk old_target old_dump_port old_ipaddr old_lun_id

	
	# 2- unconfigure the new dump device
	ERR=`${SYSDUMPDEV} -Pp /dev/sysdumpnull  2>&1 1>/dev/null`
	[ $? -ne 0 ] && {
		${C_ERRMSG} ${ERR_SYSDUMPDEV_P_FAIL} ${C_ERRMSG_ERR} "sysdumpnull" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		${ECHO} ${ERR} | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
		return 1
	}
	# 3- unconfigure the old device
	[ -n "$old_target" -a \
	  -n "$old_dump_port" -a \
	  -n "$old_ipaddr" -a \
	  -n "$old_lun_id" -a \
	  -z "$FIRMWARE_LIMITATION" ] && {
		${NIM_METHODS}/c_disc_target  -a operation=destroy\
			-a target=$old_target\
			-a dump_port=$old_dump_port\
			-a ipaddr=$old_ipaddr\
			-a lun_id=$old_lun_id

		return $?
    }	
	
	return 0
}
#############################################################################
# FUNCTION:main c_config_dump
#
# PARAMETERS:   -a configdump=[selective | full |none ]
#               -a target=targetname
#               -a lun_id=lun_id
#               -a dump_port=port
#               -a ipaddr=ip_address
#               -v 
#
# DESCRIPTION: configure the dump on the disk identified by 
#              (targetname, lun_id)
#              the disk is exported as a iscsi disk on the server whose
#              ip address is ipaddr. It is accessible from the dump_port.
#
#############################################################################
${ECHO} "" >> ${REMOTEDUMPLOG}
${DATE} >> ${REMOTEDUMPLOG}
${ECHO} "Entering c_config_dump script: $*" >> ${REMOTEDUMPLOG}

CONFIGURE_REQUIRED_ATTRS="target lun_id dump_port ipaddr configdump"
UNCONFIGURE_REQUIRED_ATTRS="configdump"

target=""
dump_port=""
lun_id=""
ipaddr=""
configdump=""
configdumpisset=""

    while getopts a:v c 
    do
	    case ${c} in
		    a)
			    configdumpisset=${OPTARG#configdump=}
			    if [ -z "$configdumpisset" ]
			    then 
				    ${C_ERRMSG} ${ERR_SYNTAX_CONFIGDUMP} ${C_ERRMSG_ERR} "" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
			    elif [ -z "$configdump" ] 
		    then 
					    configdump=${OPTARG#*=}
					    case $configdump in
						    "selective" | "full")
							    REQUIRED_ATTRS=$CONFIGURE_REQUIRED_ATTRS;;
						    "none")
							    REQUIRED_ATTRS=$UNCONFIGURE_REQUIRED_ATTRS;;
						    *)
						    ${C_ERRMSG} ${ERR_WRONG_PARAM_CHOICE} ${C_ERRMSG_ERR} "$configdump" "" "" "" | ${TEE} -a ${REMOTEDUMPLOG} 1>&2
						    ;;
					    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
	       ${C_ERRMSG} ${ERR_BAD_OPT} ${C_ERRMSG_ERR} "${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 $configdump in
	    "selective" | "full") 
			    configure_dump $target $dump_port $ipaddr $lun_id $configdump
			    [ $? -ne 0 ] && exit 1
		;;
	    "none")
			     unconfigure_dump
			    [ $? -ne 0 ] && exit 1
		;;
	    *)
		    ;;
	    esac
    exit 0

