#!/bin/sh

# Copyright C 2002-2007 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# This needs to run before isi_mcp starts isi_iscsi_d

# This script can be run from either rc or from iSCSI package installation

# Try a couple of times to make sure ifs is mounted before running
skip=0
/sbin/mount -p -t efs | /usr/bin/grep efs 2>&1 >> /dev/null
if [ $? -ne 0 ] ;  then
  /usr/bin/logger -i -p ifs.err "Attempting to run isi_iscsi when IFS is not mounted.";
  count=0
  while [ $count -lt 5 ]; do
    count=$(($count + 1))
    /sbin/mount -p -t efs | /usr/bin/grep efs 2>&1 >> /dev/null
    if [ $? -eq 0 ]; then
      break
    fi
    /bin/sleep 1
  done
  /usr/bin/logger -i -p ifs.err "IFS still unmounted. Not performing iscsi setup."
  skip=1
fi

# Only perform operations on /ifs if we have a quorum
quorum=`sysctl -n efs.gmp.has_quorum`
if [ ${quorum} -eq 1 -a ${skip} -ne 1 ]; then

    # Create the configuration directory, if necessary
    if [ ! -d /ifs/.ifsvar/iscsi ]; then
        /bin/mkdir /ifs/.ifsvar/iscsi
    fi

    # Create the initial config and lock files, if necessary
    for f in iscsi.conf access.conf options.conf isns.state ; do
        dest_file=/ifs/.ifsvar/iscsi/$f
        sorc_file=/etc/mcp/templates/iscsi_$f
        lock_file=/ifs/.ifsvar/iscsi/$f.lock

        if [ ! -f $dest_file ] ; then
            /bin/cp $sorc_file $dest_file
        fi

        if [ ! -f $lock_file ] ; then
            /usr/bin/touch $lock_file
            /bin/chmod 0600 $lock_file
        fi
    done

    # In 6.0.1 the LU SmartCache setting has to be moved from the LU extent
    # files' coalescer settings to the iSCSI pool configuration file
    if [ -f /usr/libdata/patchmgr/iscsi_fix_smartcache ]; then
        /usr/libdata/patchmgr/iscsi_fix_smartcache
    fi
fi
