#!/bin/sh #=============================================================================== # scst, (c) 2010 Navixia SA #------------------------------------------------------------------------------- # $Id$ #------------------------------------------------------------------------------- # This resource agent will configure an iSCSI target and export a single LUN # via that target. The iSCSI implementation being used is SCST, and the RA # requires the SYSFS management interface to be available (not compatible # with the PROCFS management interface). # # OCF instance parameters # OCF_RESKEY_iqn # OCF_RESKEY_path # OCF_RESKEY_allowed_portals #------------------------------------------------------------------------------- #=============================================================================== # Initialization #------------------------------------------------------------------------------- . ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs export LANG=C LANGUAGE=C LC_ALL=C #=============================================================================== # Configuration #------------------------------------------------------------------------------- SCST_base="/sys/kernel/scst_tgt" ISCSI_base="${SCST_base}/targets/iscsi" #=============================================================================== # Meta-data #------------------------------------------------------------------------------- meta_data() { cat < 0.1 Configures an iSCSI target and export a single LUN via that target. The iSCSI implementation being used is SCST, and the RA requires the SYSFS management interface to be available (not compatible with the PROCFS management interface). Exports a device via an SCST iSCSI Target/Lun pair The iSCSI Qualified Name (IQN) of the target that will be enabled. iSCSI target IQN The path to the block device exposed as LUN0. Block device path that will be exposed END } #=============================================================================== # do_cmd #------------------------------------------------------------------------------- do_cmd() { # Run a command, return its exit code, capture any output, and log # everything if appropriate. local cmd="$*" cmd_out ret ocf_log debug "$DRBD_RESOURCE: Calling $cmd" cmd_out=$( "$@" ) ret=$? if [ $ret != 0 ]; then ocf_log err "$DRBD_RESOURCE: Called $cmd" ocf_log err "$DRBD_RESOURCE: Exit code $ret" ocf_log err "$DRBD_RESOURCE: Command output: $cmd_out" else ocf_log debug "$DRBD_RESOURCE: Exit code $ret" ocf_log debug "$DRBD_RESOURCE: Command output: $cmd_out" fi echo "$cmd_out" return $ret } #=============================================================================== # usage #------------------------------------------------------------------------------- scst_usage() { cat <${SCST_base}/handlers/vdisk_fileio/mgmt if [ $? -ne 0 ]; then ocf_log err "start: FAILED to configure backend device ${devname}" return $OCF_ERR_GENERIC fi # Add backend device to target ocf_log debug "start: adding device ${devname} as LUN 0 to target ${OCF_RESKEY_iqn}" echo "add ${devname} 0" >${ISCSI_base}/${OCF_RESKEY_iqn}/luns/mgmt if [ $? -ne 0 ]; then ocf_log err "start: FAILED to add device ${devname} as LUN 0 to target ${OCF_RESKEY_iqn}" return $OCF_ERR_GENERIC fi # Finally, enable the target ocf_log debug "start: enabling target ${OCF_RESKEY_iqn}" echo 1 >${ISCSI_base}/${OCF_RESKEY_iqn}/enabled if [ $? -ne 0 ]; then ocf_log err "start: FAILED to enable target ${OCF_RESKEY_iqn}" return $OCF_ERR_GENERIC fi return $OCF_SUCCESS } #=============================================================================== # stop #------------------------------------------------------------------------------- scst_stop() { ocf_log debug "stop: entering stop action for target ${OCF_RESKEY_iqn}" scst_monitor if [ $? != $OCF_SUCCESS ]; then return $OCF_SUCCESS fi # Build SCST device name from path devname=`basename ${OCF_RESKEY_path}` # Disable the target ocf_log debug "stop: disabling target ${OCF_RESKEY_iqn}" echo 0 >${ISCSI_base}/${OCF_RESKEY_iqn}/enabled if [ $? -ne 0 ]; then ocf_log err "stop: FAILED to disable target ${OCF_RESKEY_iqn}" return $OCF_ERR_GENERIC fi # Close all the existing sessions for s in `ls -1 ${ISCSI_base}/${OCF_RESKEY_iqn}/sessions` ; do ocf_log debug "stop: disabling session $s on target ${OCF_RESKEY_iqn}" echo 1 >${ISCSI_base}/${OCF_RESKEY_iqn}/sessions/$s/force_close if [ $? -ne 0 ]; then ocf_log err "stop: FAILED to disable session $s on target ${OCF_RESKEY_iqn}" return $OCF_ERR_GENERIC fi done # Remove LUN from target ocf_log debug "stop: removing LUN 0 from target ${OCF_RESKEY_iqn}" echo "del 0" >${ISCSI_base}/${OCF_RESKEY_iqn}/luns/mgmt if [ $? -ne 0 ]; then ocf_log err "stop: FAILED to remove LUN 0 from target ${OCF_RESKEY_iqn}" return $OCF_ERR_GENERIC fi ocf_log debug "stop: removing device ${devname}" echo "del_device ${devname}" >${SCST_base}/handlers/vdisk_fileio/mgmt if [ $? -ne 0 ]; then ocf_log err "stop: FAILED to remove device ${devname}" return $OCF_ERR_GENERIC fi return $OCF_SUCCESS } #=============================================================================== # monitor #------------------------------------------------------------------------------- scst_monitor() { ocf_log debug "monitor: entering monitor action for target ${OCF_RESKEY_iqn}" # Build SCST device name from path devname=`basename ${OCF_RESKEY_path}` # Check if underlying device is configured ocf_log debug "monitor: checking existence of ${SCST_base}/handlers/vdisk_fileio/${devname}" [ -e "${SCST_base}/handlers/vdisk_fileio/${devname}" ] if [ $? -ne 0 ]; then ocf_log debug "monitor: Device ${devname} not configured" return $OCF_NOT_RUNNING fi # Check if device is mapped to LUN ocf_log debug "monitor: checking existence of ${ISCSI_base}/${OCF_RESKEY_iqn}/luns/0" [ -e "${SCST_base}/targets/iscsi/${OCF_RESKEY_iqn}/luns/0" ] if [ $? -ne 0 ]; then ocf_log debug "monitor: LUN is not configured for target ${OCF_RESKEY_iqn}" return $OCF_NOT_RUNNING fi ocf_log debug "monitor: success" return $OCF_SUCCESS } #=============================================================================== # validate #------------------------------------------------------------------------------- scst_validate() { ocf_log debug "validate: entering validate action for target ${OCF_RESKEY_iqn}" # Do we have all required variables? ocf_log debug "validate: checking variables" for var in iqn path; do param="OCF_RESKEY_${var}" if [ -z "${!param}" ]; then ocf_log error "Missing resource parameter \"$var\"!" return $OCF_ERR_CONFIGURED fi done ocf_log debug "validate: checking if target ${OCF_RESKEY_iqn} exists" [ -d "${ISCSI_base}/${OCF_RESKEY_iqn}" ] if [ $? -ne 0 ]; then ocf_log err "validate: target ${OCF_RESKEY_iqn} does not exist" return $OCF_ERR_INSTALLED fi return $OCF_SUCCESS } #=============================================================================== # main #------------------------------------------------------------------------------- case $1 in meta-data) meta_data exit $OCF_SUCCESS ;; usage|help) scst_usage exit $OCF_SUCCESS ;; esac # Everything except usage and meta-data must pass the validate test scst_validate || exit $? case $__OCF_ACTION in start) scst_start;; stop) scst_stop;; monitor) scst_monitor;; reload) ocf_log err "Reloading..." scst_start ;; validate-all) ;; *) scst_usage exit $OCF_ERR_UNIMPLEMENTED ;; esac rc=$? ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc" exit $rc