#!/bin/sh

# Copyright C 2002-2004 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.

# PROVIDE: isi_post_ifs
# REQUIRE: isi_mount_ifs

. /etc/rc.subr

# This script can be run from either rc or from the isi_group_change_d daemon
# when nodes join/rejoin the cluster

# Default permissions for /ifs, /ifs/.ifsvar, and /ifs/data
PERM=777
IFSVAR_PERM=775

# 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_post_ifs 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 ifs operations."
  skip=1
fi

# Some sysctls could be efs specific. Should be no harm in reseting them all.

if [ -r /etc/sysctl.conf ]; then
	/bin/sh /etc/rc.d/sysctl start
fi

# Check CTO support
/usr/bin/isi_hwtools/isi_hal_predicate supportsCto > /dev/null 2>&1
supports_cto=$?

# set the efs.bam.layout.directories_at_one_higher sysctl
/etc/mcp/scripts/gconfig.py

# Only perform operations on /ifs if ifsvar is ready
# Check if ifsvar is ready

/usr/bin/isi_ifsvar_ready
ifsvar_ready=$?

if [ ${ifsvar_ready} -eq 0 -a ${skip} -ne 1 ]; then


    # Root inode starts at 1x on new fs, so reprotect ASAP.
    isi set -r /ifs

    # Create the .ifsvar directory
    if [ ! -d /ifs/.ifsvar ]; then
        /bin/mkdir -p /ifs/.ifsvar
        if [ $? -eq 0 ]; then
            # The smartpools apply behavior for /ifs/.ifsvar is hardcoded and
            # doesn't need configuration
            /usr/bin/isi filepool apply -q /ifs/.ifsvar
            /bin/chmod ${IFSVAR_PERM} /ifs/.ifsvar
            /bin/chflags sunlnk /ifs/.ifsvar
        fi
    fi

    # Check for the magic file that tells us that permissions on ifs have been
    # set and if not, set.
    if [ ! -f /ifs/.ifsvar/ifs/default_perms_set ]; then
        chmod ${PERM} /ifs
        if /bin/mkdir -p /ifs/.ifsvar/ifs; then
            ( echo "magic file for isi_post_ifs" > \
                /ifs/.ifsvar/ifs/default_perms_set )
            chmod 640 /ifs/.ifsvar/ifs/default_perms_set
        fi
    fi
    # Create an IFS data directory in accordance with bug 6474

    if [ ! -d /ifs/data ]; then
        /bin/mkdir -p /ifs/data
        if [ $? -eq 0 ]; then
            /bin/chmod ${PERM} /ifs/data
            /usr/bin/isi set -Fc on -A off /ifs/data
            if [ -e /etc/README.Isilon -a ! -e /ifs/README.txt ]; then
                /bin/cp /etc/README.Isilon /ifs/README.txt
            fi
        fi
    fi

    # Check for the magic file that tells us that coalescing has been set.
    # If it is not there set coalescing for /ifs.
    if [ ! -f /ifs/.ifsvar/ifs/default_coalescing_set ]; then
        /usr/bin/isi set -Fc on -A off /ifs
        if /bin/mkdir -p /ifs/.ifsvar/ifs; then
            ( echo "magic file for isi_post_ifs" > \
                /ifs/.ifsvar/ifs/default_coalescing_set )
            chmod 640 /ifs/.ifsvar/ifs/default_coalescing_set
        fi
    fi

    # Create the run subdirectory to store wait-for-master locks
    if [ ! -d /ifs/.ifsvar/run ]; then
        /bin/mkdir -p /ifs/.ifsvar/run
        if [ $? -eq 0 ]; then
            /bin/chmod 755 /ifs/.ifsvar/run
        fi
    fi

    # Create the etc subdirectory since we need it below
    if [ ! -d /ifs/.ifsvar/etc ]; then
        /bin/mkdir -p /ifs/.ifsvar/etc
        if [ $? -eq 0 ]; then
            /bin/chmod 755 /ifs/.ifsvar/etc
        fi
    fi

    # Create the usr/local/private subdirectory since we need it as well
    if [ ! -d /ifs/.ifsvar/usr/local/private ]; then
        /bin/mkdir -p /ifs/.ifsvar/usr/local/private
        if [ $? -eq 0 ]; then
            /bin/chmod 755 /ifs/.ifsvar/usr/local/private
        fi
    fi

    if [ ! -d /ifs/home ]; then
        /bin/mkdir -p /ifs/home
	if [ $? -eq 0 ]; then
            /bin/chmod 775 /ifs/home
        fi
    fi

    # Create user directories and protect
    if [ ! -d /ifs/home/admin ]; then
        # protect /ifs/home
        /bin/mkdir -p /ifs/home/admin
        if [ $? -eq 0 ]; then
            /bin/cp /usr/share/skel/dot.zshrc /ifs/home/admin/.zshrc
            /bin/chmod u+wrX,go-w /ifs/home/admin/.zshrc
            /usr/sbin/chown -R admin:admin /ifs/home/admin
        fi
    fi

    # Create anonymous FTP home directory
    if [ ! -d /ifs/home/ftp ]; then
        /bin/mkdir -p /ifs/home/ftp
        if [ $? -eq 0 ]; then
            # add internal ftp directories
            /bin/mkdir -p /ifs/home/ftp/incoming
            /bin/mkdir -p /ifs/home/ftp/pub
            # change ownership and permissions
            /usr/sbin/chown -R root:wheel /ifs/home/ftp
            /bin/chmod -R 755 /ifs/home/ftp
            # write and execute perms on anonymous incoming directory
            /bin/chmod 753 /ifs/home/ftp/incoming
        fi
    fi

    # Create dirs relied upon by SyncIQ, and move files from Cherry locations
    REPLDIR="/ifs/.ifsvar/modules/replication"
    [ -d $REPLDIR/repeat ] || /bin/mkdir -p $REPLDIR/repeat
    for f in replicate.conf bandwidth.conf throttle.conf; do
        if [ -f $REPLDIR/$f ]; then
            mv $REPLDIR/$f /etc/mcp/sync/$f
        fi
    done

    # Create SyncIQ configuration directory
    SIQCONFIGDIR="/ifs/.ifsvar/modules/tsm/config"
    if [ ! -d $SIQCONFIGDIR ]; then
        /bin/mkdir -p $SIQCONFIGDIR
    fi

    # Created run directory on /ifs for cluster wide run state.
    if [ ! -d /ifs/.ifsvar/run ]; then
        /bin/mkdir /ifs/.ifsvar/run
    fi

    # Create dirs relied upon by Quotas and copy necessary files.
    QUOTASDIR="/ifs/.ifsvar/modules/quotas"
    [ -d $QUOTASDIR/qnr ] || /bin/mkdir -p $QUOTASDIR/qnr
    [ -d $QUOTASDIR/qgc ] || /bin/mkdir -p $QUOTASDIR/qgc
    [ -d $QUOTASDIR/notify ] || /bin/mkdir -p $QUOTASDIR/notify
    if [ ! -f $QUOTASDIR/qnr/qnr_map_default.xml ]; then
        cp /etc/mcp/sys/qnr_map_default.xml $QUOTASDIR/qnr
        chmod 444 $QUOTASDIR/qnr/qnr_map_default.xml
    fi
    if [ ! -f $QUOTASDIR/qgc/qgc_default.xml ]; then
        cp /etc/mcp/sys/qgc_default.xml $QUOTASDIR/qgc
        chmod 444 $QUOTASDIR/qgc/qgc_default.xml
    fi

    RESTRIPERDIR="/ifs/.ifsvar/modules/restriper"
    [ -d $RESTRIPERDIR ] || /bin/mkdir -p $RESTRIPERDIR

    JOBDIR="/ifs/.ifsvar/modules/jobengine"
    [ -d $JOBDIR ] || /bin/mkdir -p $JOBDIR

    FLEXNETDIR="/ifs/.ifsvar/modules/flexnet"
    [ -d $FLEXNETDIR ] || /bin/mkdir -p $FLEXNETDIR

    # Create anti-virus scanning directory
    AVSCANDIR="/ifs/.ifsvar/modules/avscan"
    [ -d $AVSCANDIR ] || /bin/mkdir -p $AVSCANDIR

    # Create smartpools directory
    SMARTPOOLSDIR="/ifs/.ifsvar/modules/smartpools"
    [ -d $SMARTPOOLSDIR ] || /bin/mkdir -p $SMARTPOOLSDIR

    # Create celog directory
    CELOGDIR="/ifs/.ifsvar/modules/celog"
    [ -d $CELOGDIR ] || /bin/mkdir -p $CELOGDIR

    # Create auth configuration directory
    AUTHCONFIGDIR="/ifs/.ifsvar/modules/auth"
    if [ ! -d $AUTHCONFIGDIR ]; then
        /bin/mkdir -p $AUTHCONFIGDIR
        /bin/chmod 700 $AUTHCONFIGDIR
    fi
    AUTHSAMCONFIGDIR="/ifs/.ifsvar/modules/auth/sam"
    if [ ! -d $AUTHSAMCONFIGDIR ]; then
        /bin/mkdir -p $AUTHSAMCONFIGDIR
        /bin/chmod 700 $AUTHSAMCONFIGDIR
        /usr/bin/isi set -c on -F $AUTHSAMCONFIGDIR
    fi

    HDFSCONFIGDIR="/ifs/.ifsvar/modules/hdfs_d/config"
    [ -d $HDFSCONFIGDIR ] || /bin/mkdir -p $HDFSCONFIGDIR

    # Create krb5 configuration directory
    KRB5CONFIGDIR="/ifs/.ifsvar/modules/krb5"
    [ -d $KRB5CONFIGDIR ] || /bin/mkdir -p $KRB5CONFIGDIR

    # Create httpd configuration directory
    HTTPCONFIGDIR="/ifs/.ifsvar/modules/http"
    [ -d $HTTPCONFIGDIR ] || /bin/mkdir -p $HTTPCONFIGDIR

    # Create NTP configuration directory
    NTPCONFIGDIR="/ifs/.ifsvar/modules/ntp"
    [ -d $NTPCONFIGDIR ] || /bin/mkdir -p $NTPCONFIGDIR

    # Create cbind directory
    CBINDDIR="/ifs/.ifsvar/modules/cbind"
    [ -d $CBINDDIR ] || /bin/mkdir -p $CBINDDIR

    # Create dedupe/bedrs directory
    BEDRSDIR="/ifs/.ifsvar/modules/bedrs"
    [ -d $BEDRSDIR ] || /bin/mkdir -p $BEDRSDIR

    # Create cloudpools directory
    CPOOLDIR="/ifs/.ifsvar/modules/cloud"
    [ -d $CPOOLDIR ] || /bin/mkdir -p $CPOOLDIR

    # Create hardware directories (for driveconfig, etc.)
    HARDWAREDIR="/ifs/.ifsvar/modules/hardware"
    MODELSDIR=$HARDWAREDIR/drives/config/models
    VENDORSDIR=$HARDWAREDIR/drives/config/vendors
    [ -d $MODELSDIR ] || /bin/mkdir -p $MODELSDIR
    [ -d $VENDORSDIR ] || /bin/mkdir -p $VENDORSDIR

    # Create default NFS configuration directory
    NFSDIR="/ifs/.ifsvar/modules/nfs"
    [ -d $NFSDIR ] || /bin/mkdir -p $NFSDIR

    # Create the NFSv4 stable file
    if [ ! -f /var/db/nfs-stablerestart ]; then
        touch /var/db/nfs-stablerestart
        chmod 640 /var/db/nfs-stablerestart
    fi

    # Create dirs relied on by FSA
    FSADIR="/ifs/.ifsvar/modules/fsa"
    if [ ! -d $FSADIR ]; then
	/bin/mkdir -p $FSADIR
    fi

    # Create dirs relied on by stats
    STATSDIR="/ifs/.ifsvar/modules/stats"
    if [ ! -d $STATSDIR ]; then
	/bin/mkdir -p -m0770 $STATSDIR
    fi

    # Create the etc/ifs subdirectory since we need it below
    if [ ! -d /ifs/.ifsvar/etc/ifs ]; then
	/bin/mkdir /ifs/.ifsvar/etc/ifs
    fi

    if [ ! -d /ifs/.ifsvar/etc/mcp ]; then
	/bin/mkdir /ifs/.ifsvar/etc/mcp
    fi

    if [ ! -d /ifs/.ifsvar/tmp ]; then
	/bin/mkdir /ifs/.ifsvar/tmp
    fi

    if [ ! -d /ifs/.ifsvar/db ]; then
	/bin/mkdir /ifs/.ifsvar/db
    fi
    if [ ! -d /ifs/.ifsvar/db/pkg ]; then
	/bin/mkdir /ifs/.ifsvar/db/pkg
    fi
    if [ ! -d /ifs/.ifsvar/db/celog ]; then
	/bin/mkdir /ifs/.ifsvar/db/celog
    fi

    if [ ! -d /ifs/.ifsvar/modules/ndmp ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/ndmp
	cp /etc/mcp/sys/ndmp_default.xml /ifs/.ifsvar/modules/ndmp/config.xml
	chmod 0640 /ifs/.ifsvar/modules/ndmp/config.xml
	/bin/mkdir -p /ifs/.ifsvar/modules/ndmp/contexts
	cp /etc/mcp/sys/ndmp_context_list_default.xml /ifs/.ifsvar/modules/ndmp/context_list.xml
	chmod 0640 /ifs/.ifsvar/modules/ndmp/context_list.xml
    fi

    if [ ! -d /ifs/.ifsvar/modules/fc ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/fc
	cp /etc/mcp/sys/fc_default.xml /ifs/.ifsvar/modules/fc/devices.xml
	chmod 400 /ifs/.ifsvar/modules/fc/devices.xml
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_vc ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_vc
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_vc/locks ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_vc/locks
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_vc/gconfig ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_vc/gconfig
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_vc/jobs ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_vc/jobs
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_sra ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_sra
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_sra/gconfig ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_sra/gconfig
    fi

    if [ ! -d /ifs/.ifsvar/modules/l3_cache ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/l3_cache
    fi

    if [ ! -d /ifs/.ifsvar/modules/acs ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/acs
    fi

    # Used by snapshot PAPI handler and snapshot daemon, snapshot_manage.h.
    if [ ! -d /ifs/.ifsvar/snapshots ]; then
	/bin/mkdir -p /ifs/.ifsvar/snapshots
    fi

    # Used by snapshot daemon.
    if [ ! -d /ifs/.ifsvar/modules/isi_snapshots ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_snapshots
    fi

    # If this is a backup accelerator, perform FC device discovery.
    if [ -e /dev/isp0 ]; then
	/usr/bin/isi_fc_rescan
    fi

    # Signal rpc.lockd to check to see if there's a non-default value
    # for its RPC timeout. The lockd RPC timeout variable is stored in
    # /ifs/.ifsvar/etc/lockd_rpc_timeout
    # this is no longer supported with our new NLM in 5.5
    # /usr/bin/killall -USR1 rpc.lockd > /dev/null 2>&1

    # Update the hardware info cache
    echo "import isi.sys.version as v; v.update_cache()" | /usr/bin/python

    # Update cached ABR for CTO nodes as well as
    if [ ${supports_cto} -eq 1 ]; then
        /usr/bin/isi_inventory_tool --cache --fileType=asbuilt
    fi

    # Make sure we're not in an auto-suspend pool (unless we're updating)
    /usr/bin/isi_sc_auto_resume > /dev/null 2>&1

    # This will make the update lock files if needed
    if [ ! -e /ifs/.ifsvar/isi_update_slash_pkg.status -o \
            ! -e /ifs/.ifsvar/isi_update_slash_pkg.lock ]; then
	/bin/cat /dev/null | /usr/sbin/isi_grab_update_lock > /dev/null
    fi

    # Kick off drive purposing
    if [ -f /usr/bin/isi_evaluate_purpose_drive ]; then
        /usr/bin/isi_evaluate_purpose_drive
    fi

    # SmartPools Upgrade
    if [ -f /usr/libdata/patchmgr/smartpools-upgrade ]; then
        /usr/libdata/patchmgr/smartpools-upgrade
    fi

    # For 6.0 initialize Smartpools.
    if [ -f /usr/libdata/patchmgr/init_smartpools ]; then
        /usr/libdata/patchmgr/init_smartpools
    fi

    # Update CELOG settings with support notification changes, if any
    /usr/bin/isi_promptsupport -c

    # Run one-time upgrade script after IFS is mounted.
    # ***Should be kept at the end of isi_post_ifs***
    /usr/sbin/isi_first_post_ifs
    first_post_ifs=$?

    # Dirty hack to read in kern_osrevision
    eval `grep kern_osrevision /boot/kernel.amd64/version.rc`

    # Run final upgrade script
    # ***Should be kept right after isi_first_post_ifs***
    # Requires isi_first_post_ifs to complete
    # and final upgrade to not have run before
    if [ ${first_post_ifs} -eq 0 ] && \
        [ ! -f /ifs/.ifsvar/shell_uapi/last_upgrade ] || \
        [ "`cat /ifs/.ifsvar/shell_uapi/last_upgrade`" != \
        "${kern_osrevision}" ]; then

        # Mark this node as upgraded for "Shell Upgrade API"
        mkdir -p /ifs/.ifsvar/shell_uapi
        echo ${kern_osrevision} > \
            /ifs/.ifsvar/shell_uapi/`isi_nodes -L "%{devid}"`

        # Check if all nodes are upgraded
        upgrade_done=1
        for DEVID in `isi_nodes "%{devid}"`; do
            if [ ! -f /ifs/.ifsvar/shell_uapi/$DEVID ] || \
                [ "`cat /ifs/.ifsvar/shell_uapi/$DEVID`" != \
                "${kern_osrevision}" ]; then
                upgrade_done=0
            fi
        done

        if [ ${upgrade_done} -eq 1 ]; then
            /usr/sbin/isi_first_post_ifs_upgrade

            python -c "import isi.sys.update.upgrade_api_mod as api; api.remove_config()"
            echo "Cleared upgrade config"

            # Mark upgrade as done (even for fresh install)
            echo ${kern_osrevision} > /ifs/.ifsvar/shell_uapi/last_upgrade
        fi
    fi

    # Complete upgrade (Last node up)
    if [ -f /etc/disable_isi_upgrade_d ]; then
        /bin/rm -f /etc/disable_isi_upgrade_d

        /usr/bin/isi services isi_upgrade_d disable
        echo "Disabled upgrade d"
    fi


    # Post ACS(auto-cluster setup) tasks
    echo "import isi.ui.shell.AutoClusterSetup as acs; acs.post_acs()" | /usr/bin/python

fi

# These bits below get executed whether or not IFS was mounted
# which begs the question of why they are here instead of somewhere else

# Make sure things that are based on the list of group membership
# are taken care of.
/usr/libexec/isilon/on_group_change

# backup the drives.xml file only on storage node
/usr/bin/isi_run_script -s /usr/sbin/isi_backup /etc/ifs/drives.xml

# Run isi_bootdisk_finish every time
/usr/sbin/isi_bootdisk_finish
