#!/bin/bash
####################################################################
#
#      File: S99load-isan
#      Name:   Chandan Mishra 
#
#       Description:
#  initialization script for vegas isan software to be run at boot up from
#	inittab
#	Actual location on disk: /etc/rc.d/rcS.d/S99load-isan
#
#
# Copyright (c) 1985-2021 by cisco Systems, Inc.
# All rights reserved.
#
#
# $Id: S99load-isan,v 1.31 2003/11/13 21:20:41 hzhong Exp $
# ---------------------------------------------------------------
#####################################################################

. /etc/rc.d/include/rcS.include
. /etc/rc.d/include/lowmemory-devices-prune

send_lacp() {
    if [[ "$CHOPAKA_FAMILY_ASIC" == "y" ]]; then
        KLM_NAME="klm_clscl_dchal.o"
        KLM_PROC_PATH="/proc/cloudscale/kclscl/"
    else
        KLM_NAME="klm_tah.o"
        KLM_PROC_PATH="/proc/tahoe/ktah/"
    fi
    if [ -n "$IS_LXC_TOR" ]; then
        tar -C / -xf  /dklm_lcv.tar lc/isan/lib/modules/$KLM_NAME
        /sbin/insmod /lc/isan/lib/modules/$KLM_NAME skip_edma=0 rx_copybreak=256 fastboot_mode=1 2> /dev/null
    else
        /sbin/insmod /lc/isan/lib/modules/$KLM_NAME skip_ema=0 rx_copybreak=256 fastboot_mode=1 2> /dev/null
        if [[ "$KLM_NAME" == "klm_tah.o" ]]; then
            #This may be needed for cloudscale in LXC mode
            /sbin/insmod /lc/isan/lib/modules/klm_tah_sse.o 2> /dev/null
        fi
    fi
    echo "Inside fastboot mode" >> /mnt/pss/sendlacp.log

    if [ -f "/mnt/pss/issu_lacp_pdu" ] || [ -f "/mnt/pss/fspf_issu_neighbour_db" ]; then
        ifconfig tahoe0 up
        echo 8 > $KLM_PROC_PATH/channel_sel
        echo 1 > $KLM_PROC_PATH/channel_en
        if [ "${torissu:=0}"  = "1" ]; then
            echo 0 > $KLM_PROC_PATH/channel_sel
            echo 1 > $KLM_PROC_PATH/channel_en
        fi
    fi

    if [ -f "/mnt/pss/fspf_issu_neighbour_db" ]; then
        echo "Sending FSPF Hellos `date`" >> /mnt/pss/fspf_issu_dbg.txt
        echo 1 > /proc/isan/sw/fcfwd/fspf/hello_send_en
    fi

    if [ -f "/mnt/pss/issu_lacp_pdu" ]; then
        echo "Inside lacp mode at `date`" >> /mnt/pss/sendlacp.log
        ifconfig tahoe0 >> /mnt/pss/sendlacp.log
        ifconfig ps-inb >> /mnt/pss/sendlacp.log
        echo 8 > $KLM_PROC_PATH/channel_sel
        cat /proc/tahoe/ktah/regdump >> /mnt/pss/sendlacp.log
        echo 0 > $KLM_PROC_PATH/channel_sel
        cat $KLM_PROC_PATH/regdump >> /mnt/pss/sendlacp.log
        if [[ "$ASIC_GT_256_PORTS" == "y" ]]; then
            /usr/bin/python /isan/bin/tahoe_sendlacp.py -s Tahoe -l /mnt/pss/issu_lacp_pdu -a 1 >> /mnt/pss/sendlacp.log 
        else
            /usr/bin/python /isan/bin/tahoe_sendlacp.py -s Tahoe -l /mnt/pss/issu_lacp_pdu -a 0 >> /mnt/pss/sendlacp.log 
        fi
        ifconfig >> /mnt/pss/sendlacp.log
        echo 8 > $KLM_PROC_PATH/channel_sel
        cat /proc/tahoe/ktah/regdump >> /mnt/pss/sendlacp.log
        echo 0 > $KLM_PROC_PATH/channel_sel
        cat $KLM_PROC_PATH/regdump >> /mnt/pss/sendlacp.log
        sleep 1
        if [[ "$ASIC_GT_256_PORTS" == "y" ]]; then
            /usr/bin/python /isan/bin/tahoe_sendlacp.py -s Tahoe -l /mnt/pss/issu_lacp_pdu -a 1 >> /mnt/pss/sendlacp.log 
        else
            /usr/bin/python /isan/bin/tahoe_sendlacp.py -s Tahoe -l /mnt/pss/issu_lacp_pdu -a 0 >> /mnt/pss/sendlacp.log 
        fi
        echo `date +"%s"` > /mnt/pss/upgrade/send_lacppdu.txt 
        echo 8 > $KLM_PROC_PATH/channel_sel
        cat $KLM_PROC_PATH/regdump >> /mnt/pss/sendlacp.log
        echo 0 > $KLM_PROC_PATH/channel_sel
        cat $KLM_PROC_PATH/regdump >> /mnt/pss/sendlacp.log
        ifconfig >> /mnt/pss/sendlacp.log
        echo 8 > $KLM_PROC_PATH/channel_sel
        if [ "${fastboot:=0}"  = "1" ]; then
            echo "Triggering lacp_sendpdu script fastboot mode" >> /mnt/pss/sendlacp.log
            /isan/bin/lacp_sendpdu.sh 1 & >> /mnt/pss/sendlacp.log
            echo "DONE Triggering lacp_sendpdu script fastboot mode" >> /mnt/pss/sendlacp.log
        else
            echo "Triggering lacp_sendpdu script non-fastboot mode" >> /mnt/pss/sendlacp.log
            /isan/bin/lacp_sendpdu.sh 0 & >> /mnt/pss/sendlacp.log
            echo "DONE Triggering lacp_sendpdu script non-fastboot mode " >> /mnt/pss/sendlacp.log
        fi
        if [ "${fastboot:=0}"  = "1" ]; then
            echo "Inside lacp mode - Disabling tahoe0 for fastboot"
            ifconfig tahoe0 down
            echo 0 > $KLM_PROC_PATH/channel_en
        fi
    fi
}

extract_ucsm() {

MKDIR="/bin/mkdir"
CHMOD="/bin/chmod"
RM="/bin/rm"
MV="/bin/mv"
DD="/bin/dd"
TAR="/bin/tar"
AWK="/bin/awk"
GREP="/bin/grep"
HEAD="/usr/bin/head"
IMG_HDR="/isanboot/bin/imghdr"
LN="/bin/ln"

umask 0022
${MKDIR} -p /logflash/sam/db
${MKDIR} -p /logflash/sam/workspace
${LN} -sf /logflash/sam /opt
${LN} -sf /logflash/sam/workspace /workspace
ls -l /opt

echo "Extracting UCSM .."

# calculate the header length for management image
if [ -z "$mgmtimg" ] ; then
    mgmtimg="/bootflash/nuova-sim-mgmt-nsg.0.1.0.001.bin"
fi

if [ -f ${mgmtimg} -o -L ${mgmtimg} ]; then
    if [ -L ${mgmtimg} ]; then    
        mgmtimg=`readlink ${mgmtimg}`
        if [ ! -f ${mgmtimg} ]; then
            boot_debug "No management image link is broken"
            logprint echo "No management image link is broken"
            exit 1
        fi
    fi

    BS=`/isanboot/bin/imghdr len ${mgmtimg}`
    if [[ "$BS" == *"Error opening the image"* ]]; then
        boot_debug "No valid management image found!"
        logprint echo "No valid management image found!"
        exit 1
    fi

    sam_extractdir=/opt/tmpextractdir
    ${RM} -Rf $sam_extractdir
    ${MKDIR} -p $sam_extractdir
    ( cd $sam_extractdir; ${DD} if=${mgmtimg} skip=1 bs=$BS | ${TAR} zxkpmvf - ) >>/dev/null 2>>"$BOOTLOG_FILE" | \
     ${AWK} '{if ((FNR % 20)==0) printf(".")}' 
 
    if [ $? != 0 ]; then
        boot_debug "No valid management image found"
        logprint echo "No valid management image found"
        start_vshboot "isan-ready"
        exit 1
    fi

    # Extract SAM Plugin entries here. We donot use plugin infrastructure
    UCSM_EXTRACT_DIR="/ucs"
    mkdir -p $UCSM_EXTRACT_DIR
    sam_plugin=$sam_extractdir/isan/plugin_img/sam_plugin
    BS=`/isanboot/bin/imghdr len ${sam_plugin}`

    ( cd $UCSM_EXTRACT_DIR; ${DD} if=${sam_plugin} skip=1 bs=$BS | ${TAR} zxkpmvf - ) 2>>"$BOOTLOG_FILE" | \
     ${AWK} '{if ((FNR % 200)==0) printf(".")}' 
 
    if [ $? != 0 ]; then
        boot_debug "Error while extracting sam_plugin"
        logprint echo "Error while extracting sam_plugin"
        start_vshboot "isan-ready"
        exit 1
    fi

    # Extract SAM main Plugin entries here. We donot use plugin infrastructure
    sam_plugin_main=$sam_extractdir/isan/plugin_img/sam_plugin_main
    BS=`/isanboot/bin/imghdr len ${sam_plugin_main}`

    ( cd $UCSM_EXTRACT_DIR; ${DD} if=${sam_plugin_main} skip=1 bs=$BS | ${TAR} zxkpmvf - ) 2>>"$BOOTLOG_FILE" | \
     ${AWK} '{if ((FNR % 200)==0) printf(".")}' 
 
    if [ $? != 0 ]; then
        boot_debug "Error while extracting sam_plugin_main"
        logprint echo "Error while extracting sam_plugin_main"
        start_vshboot "isan-ready"
        exit 1
    fi

    # Make a list of extracted files so that they can be cleaned up during upgrade
    # SASI: Can we change this to find . instead ???
    cd /; ${DD} if=${sam_plugin} skip=1 bs=$BS 2> /dev/null | ${TAR} ztf -  > /isan/etc/sam_image.cat
    cd /; ${DD} if=${sam_plugin_main} skip=1 bs=$BS 2> /dev/null | ${TAR} ztf -  >> /isan/etc/sam_image.cat

    # temp dir is no longer needed. delete it
    ${RM} -Rf ${sam_extractdir}

else
    boot_debug "No management image found"
    logprint echo "No management image found"
    exit 1
fi

}

setup_load_isan() {
boot_debug "Executing /etc/rc.d/rcS.d/S99load-isan"
echo "load isan:Start - `date`" >> /tmp/boot_uptime.log

# We use this variable for isanimg-select. Let's initialize it here
# to make sure it's always defined.
last_netboot_ip=0.0.0.0

# The variable isanimg is used by this script to know the URI of the
# iSAN image. This script can be invoked during the initialization,
# in which case isamimg is not defined, and must be read from
# the kernel command line; or it can be invoked by vshboot, in which
# case isanimg is already defined when the script is started, so that
# the isanimg defined in the kernel command line can be overridden.


# need to change isanimg variable if kexec, so that isanimg_select will
# coy the isanimg from / instead of CF.  This is done by changing
# the isnaimg variable name to be mem:<filename>.  isanimg_select 
# will then deal with mem: appropriately. 
if [ -n "$isanimg_loc" ]; then
   bar="${isanimg#isanimg=}"
   export isanimg="mem:$bar"
   echo Modifying isanimg to be $isanimg 
fi  

isanimg=
boot_debug "recoverymode=$recoverymode"

# 1, -recoverymode (fallback to kick whether single image or not)
# 2, isan from single image
if [ -z "$recoverymode" ]; then
    isanimg="package:/isanboot/bin/images/sys"
fi

# until boot loader nosinglimg switch works
# provide an alternative to drop to kick
if [ -e "/bootflash/platform.inf" ] ; then
    droptokick=`grep droptokick /bootflash/platform.inf | cut -d= -f2`
    if [ "$droptokick" == "1" ] ; then
        logprint echo "PLATFORM_INF has droptokick. DROPPING TO KICKSTART.."
        isanimg=
    fi
fi 

# if poap is enabled then we need to delete startup config so that
# poap is invoked in every reboot
POAP_REPLAY_FILE_COUNT=`ls /bootflash/poap_replay??.cfg 2>/dev/null | wc -l`
if [[ -z "$IS_N3K" || $CARD_INDEX == 11120 ]]; then
    POAP_BOOT_ENABLED=`ls /mnt/pss/poap_boot_enable 2>/dev/null | wc -l`
else
    POAP_BOOT_ENABLED=`/isanboot/bin/cmosio -r poap | head -n 1`
fi
POAP_BOOT_ENABLE_FILE="/volatile/poap_boot_enable"
if [ $POAP_BOOT_ENABLED -eq 1 ]; then
    echo "Bootstrapping via POAP overriding existing startup-config"
    /bin/touch $POAP_BOOT_ENABLE_FILE
else
    boot_debug "Bootstrapping via POAP if startup-config doesn't exist"
    /bin/rm -f $POAP_BOOT_ENABLE_FILE
fi
if [ $POAP_REPLAY_FILE_COUNT -eq 0 ]; then
    if [ $POAP_BOOT_ENABLED -eq 1 ]; then
        /isanboot/bin/vshboot -c "write erase non-interactive"
    fi
fi

if [ -z "$isanimg_passed_by_syscli" ] ; then
    regular_boot=yes
else
    # overwrite /proc/cmdline
    isanimg=$isanimg_passed_by_syscli
    regular_boot=no
fi

# right now execute only for n9k.
if [ -z "$IS_N3K" ]; then
	proc_file="/proc/cmdline"
	if [ -f $proc_file ]; then
		shell_search=`cat /proc/cmdline`
		for x in $shell_search;
			do [[ $x = defaultshell=* ]] || continue;
			shelltype="${x#defaultshell=}";
		done
	fi
	if [ -n "${shelltype}" ]; then
		logprint echo "set shelltype to $shelltype from defaultshell arg in /proc/cmdline"
	fi

	user_add_file="/etc/default/useradd"
	if [ -f $user_add_file ]; then
		if [ "$shelltype" = "/bin/bash" ]; then
			sed -i.bak "s|/isan/bin/vsh_perm|$shelltype|" /etc/default/useradd
			logprint echo "set vsh perm of ${shelltype} in useradd"
		fi
	fi
fi

# PNP_TEMP
# Invoking PnP to install scripts
/bin/bash /isan/bin/install_pnp.sh &

boot_debug "ksimg=$ksimg"
boot_debug "isanimg=$isanimg"
if [ -z "$isanimg" ] ; then
    # The iSAN image has not been specified before calling this
    # script, and it wasn't in the kernel command line either
    # Normally, this means that we must jump in vshboot.
    # However, when the 'reload mod <N> force-dnld' command is used,
    # we end up in the same case, as we use the BIOS to boot the
    # kickstart image. In this case, we need to take the iSAN image
    # running on the active supervisor. To recognize this force-dnld
    # case, there's a byte in CMOS that tells us whether or not there
    # has been a EOBC netboot. That's what we check here.

    if [ -n "$ksimg" ]; then
	# The image was loaded by the bootloader, as we have the 'ksimg'
	# string in the kernel command line.
	boot_debug "no system image provided (through bootloader), entering vshboot"
    logprint echo "No system image is specified"
	start_vshboot "isan-ready"
	exit 1
    else
	# The image was loaded by the BIOS. Let's see if it was a EOBC
	# netboot or a mgmt0 netboot.
	last_netboot_ip=`/isanboot/bin/cmosio -r last`
	boot_debug "last_netboot_ip = $last_neboot_ip"

	if [ "$last_netboot_ip" == "0.0.0.0" ]; then
	    # The BIOS did a mgmt0 netboot
	    boot_debug "no system image provided (through BIOS mgmt0 netboot), entering vshboot"
        logprint echo "No system image is specified"
	    start_vshboot "isan-ready"
	    exit 1
	else
	    # The BIOS did a EOBC netboot. We can try to download the iSAN
	    # image from the active supervisor.
	    isanimg="image://sup-remote/running-system"
	fi
    fi
fi

# isanimg contains a list of URIs, and one URI must be selected
boot_debug "Selecting system image among \"$isanimg\""
if [ "$DEBUG" = "yes" ]; then
    extra_param="-i $last_netboot_ip -V"
else
    extra_param="-i $last_netboot_ip"
fi

if [ "${torissu:=0}"  = "1" ]; then
    logprint echo "TOR ISSU is TRUE. Touching /isan-upgrade/isan.bin at `date`"
    mkdir /isan-upgrade
    touch /isan-upgrade/isan.bin

    if [ -d /cont_id ] ; then
      mkdir -p /var/sysmgr/ftp  
      cp /mnt/pss/upgrade/*.mod1 /var/sysmgr/ftp  
    fi
fi

if [ "${fastboot:=0}"  = "1" ]; then
    touch /var/sysmgr/fastboot_enabled
elif [ -f /mnt/pss/partial.cfg ]; then
    # if it is not fastboot
    # remove any stale ascii repave config file.
    rm -rf /mnt/pss/partial.cfg
fi

###
# This code is unnecessary with single image and it does not work
# Disable the code in n9k/n3k
###
## Some messages are always printed to the console, regardless of the
## "quiet-ness" of the boot sequence
#logprint echo "Uncompressing system image: $isanuri `date`"
#
## calculate the header length
#BS=`/isanboot/bin/imghdr len $isanimg`
#
##/isanboot/bin/blogger -F'C' -l$BOOTLOG_FILE --  \
#logprint
#        ( cd /; dd if=$isanimg skip=1 bs=$BS | tar zxkPpmvf - ) 2>>"$BOOTLOG_FILE" | \
#	awk '{if ((FNR % 20)==0) printf("C")}'
#
#if [ $? != 0 ]; then
#    boot_debug "No valid image found"
#    logprint echo "No valid image found"
#    start_vshboot "isan-ready"
#    exit 1
#fi

if [ -d "/isan/plugin_img" ]; then
action "" mkdir -p /isan/plugin
action "" chmod 755 /isan/plugin
action "" mkdir -p /isan/plugin_temp
action "" chmod 755 /isan/plugin_temp
action "" mkdir -p /isan/plugin_extract_log
action "" chmod 755 /isan/plugin_extract_log
action "" mkdir -p /isan/plugin_extract_log_temp
action "" chmod 755 /isan/plugin_extract_log_temp
action "" mkdir -p /isan/plugin_srg
action "" chmod 755 /isan/plugin_srg
action "" mkdir -p /isan/plugin_uri
action "" chmod 755 /isan/plugin_uri

#code coverage begin
#Code coverage has to be done only for gdb images
if [ "`/isanboot/bin/bldreport type`" = "gdb" ]; then

# make a copy of coverage files so they do not get replaced
COVERAGE_DIR=/bootflash/code_coverage
TMP_COVERAGE_DIR=`mktemp -d`
code_cov_boot_firsttime=0
echo "GDB image. Will perform basic code coverage related checks.. " > /tmp/code_coverage.log
if [ ! -d "$COVERAGE_DIR" ]
then
    # core plugin extraction needs code_coverage dir to be already present.
   
    echo "No code_coverage directory. code_cov_boot_firsttime = 1" >> /tmp/code_coverage.log
    mkdir $COVERAGE_DIR 
    code_cov_boot_firsttime=1
else
    # extaction of core plugin replaces the contents of code_coverage dir.
    # make a copy of the existing files to restore the last state.
    echo "Code coverege dir exists. code_cov_boot_firsttime = 0" >> /tmp/code_coverage.log
    echo "Copy all files from COVERAGE_DIR to TMP_COVERAGE_DIR" >> /tmp/code_coverage.log 2>&1
    cp -fH $COVERAGE_DIR/* $TMP_COVERAGE_DIR >> /tmp/code_coverage.log 2>&1
    rm -f $COVERAGE_DIR/* >> /tmp/code_coverage.log 2>&1
    code_cov_boot_firsttime=0
fi    

echo "Plugin extracted. Copy back files, if any." >> /tmp/code_coverage.log
if [ $code_cov_boot_firsttime -eq 1 ]
then
    if [ "$(ls -A $COVERAGE_DIR)" ]
    then
        # code coverage image. make a copy of the files in the dir  and 
        # copy back to break the link.
        echo "code coverage image. Copy files to tmp and back to break link" >> /tmp/code_coverage.log
        cp -fH $COVERAGE_DIR/* $TMP_COVERAGE_DIR >> /tmp/code_coverage.log 2>&1
        mv -f $TMP_COVERAGE_DIR/* $COVERAGE_DIR >> /tmp/code_coverage.log 2>&1
    else
        # not a code coverage image. remove the code_coverage dir.
        echo "Not a code coverage image. Removing the code coverage dir" >> /tmp/code_coverage.log
        rm -rf $COVERAGE_DIR >> /tmp/code_coverage.log 2>&1
    fi
else
    # Not first time boot. Copy files to TMP dir and then copy back the files in TMP DIR.
    # Only copy those files to TMP dir that are not already there. This is
    # done to break the links of .cov's in a new code_cov image while
    # retaining the .cov's of an earlier code_cov image.
    echo "Code cov image. Not first time boot. Copy out files not in tmp_dir" >> /tmp/code_coverage.log
      for file in `dir $COVERAGE_DIR`; do
          if [ ! -f $TMP_COVERAGE_DIR/$file ]
          then
              cp -fH $COVERAGE_DIR/$file $TMP_COVERAGE_DIR  >> /tmp/code_coverage.log 2>&1
          fi
      done
    echo "Move all files back to coverage_dir" >> /tmp/code_coverage.log
    mv -f $TMP_COVERAGE_DIR/* $COVERAGE_DIR >> /tmp/code_coverage.log 2>&1
fi

echo "Removing TMP_COVERAGE_DIR" >> /tmp/code_coverage.log 2>&1
rm -rf $TMP_COVERAGE_DIR  

echo "Changing permission of .cov files" >> /tmp/code_coverage.log 2>&1
chmod 777 $COVERAGE_DIR/* >> /tmp/code_coverage.log 2>&1

echo "End of code coverage related checks" >> /tmp/code_coverage.log 2>&1
fi
#code coverage end

#action "" cp /isan/plugin_img/img.conf /var/tmp/img.conf
#action "" rm -fr /isan/plugin_img
fi

# newer-fs is a security hole. Only if the kernel command line says
# 'use_newer_fs', then we enable this security hole.

if [ "$use_newer_fs" = "yes" -a "`/isanboot/bin/bldreport type`" = "gdb" ]; then
    
    # Update the loaded iSAN image with any patch in newer-fs

    if [ -d $MNT_BOOTFLASH_DIR/newer-fs ]; then
	echo "***************************************************************************"
	echo "WARNING: adding patches from bootflash:/newer-fs"
	boot_debug "Adding patches from bootflash:"
	action "Adding patches from bootflash:" cp -af $MNT_BOOTFLASH_DIR/newer-fs/* /
	echo "***************************************************************************"
    fi

    # Patching the system image with newer-fs/autorun

    if [ -x $MNT_BOOTFLASH_DIR/newer-fs/autorun ]; then
        echo "***************************************************************************"
        echo "WARNING: patching system image with autorun"
        boot_debug "Adding autorun patches"
        action "patching system image with autorun" cp $MNT_BOOTFLASH_DIR/newer-fs/autorun /autorun; /autorun
        echo "***************************************************************************"
    fi
fi

# Call all of the isan specific system initialization scripts in order
# location of isan scripts will be in /isan/etc/rc.d/rcS.d
script_debug=$(proc_cmdline_get script_debug)
for i in $(ls /isan/etc/rc.d/rcS.d/S??*) ; do
	# Ignore dangling symlinks for now.
	[ ! -f "$i" ] && continue

    if [ -z "$script_debug" ] ; then
      boot_debug "S99load-isan: executing $i start"
    else
      echo "Executing $i ... at `date +'%Y %a %b %d %H:%M:%S:%N'`" | tee -a /tmp/issu_debug.log
    fi

	case "$i" in
		*.sh)
			# Source shell script for speed.
			(
				set "start"
				. $i
			)
			;;
		*)
			# No sh extension, so fork subprocess.
			$i "start"
			;;
	esac
done

if [ "$isan_boot_stop" == "1" ]; then
    exit 0
fi

# Tune swappiness. This is a kernel parameter that controls the relative
# weight given to swapping out runtime memory as opposed to dropping
# pages from system page cache. swappiness can be set to values between
# 0 and 100. A low value causes kernel to avoid swapping, a higher value
# causes kernel to try to use swap space. The default is 60. Setting it
# to 0 may have issues : 
# http://www.percona.com/blog/2014/04/28/oom-relation-vm-swappiness0-new-kernel/
# But since we don't have any swap on n9k and 3k, setting it to 1.
echo 1 > /proc/sys/vm/swappiness

#
# Give a chance to the user to disable DME
#
#if [ -e "/bootflash/disable_dme" ]; then
#    echo "Disabling DME"
#    touch /var/dme_disabled
#fi

if [[ -n "$IS_N3K" && -z "$IS_DME_ENABLED_N3K" ]] || 
  [[ -n "$IS_WBOX_4G" && -z "$IS_DME_ENABLED_WBOX" ]] ||
  [[ -n "$IS_THURN" && -z "$IS_DME_ENABLED_THURN" ]] ; then
     echo "Removing dme lib" >> /tmp/presysmgr.log
     if [ -f "/isan/lib/libdme_orig.so" ]; then
         rm /isan/lib/libdme_orig.so
         boot_debug "Removed dme lib"
     fi

     if [ -f "/isan/lib/libcliaccess.so" ]; then
         rm /isan/lib/libcliaccess.so
         ln -s /isan/lib/libcliaccess_stub.so /isan/lib/libcliaccess.so
         boot_debug "Removed cliaccess lib"
     fi

     echo "Moving N3K specific syslog config file" >> /tmp/presysmgr.log
     if [ -f "/isan/etc/sysmgr.d/syslogd_n3k.conf" ]; then
         mv /isan/etc/sysmgr.d/syslogd_n3k.conf /isan/etc/sysmgr.d/syslogd.conf
     fi
else
     echo "Overwriting dme stub lib" >> /tmp/presysmgr.log
     if [ -f "/isan/lib/libdme_orig.so" ]; then
         mv /isan/lib/libdme_orig.so /isan/lib/libdme_stub.so
         boot_debug "Replaced dme stub lib"
     fi

     echo "rm libcliaccess stub lib" >> /tmp/presysmgr.log
     if [ -f "/isan/lib/libcliaccess.so" ]; then
         rm /isan/lib/libcliaccess_stub.so
         ln -s /isan/lib/libcliaccess.so  /isan/lib/libcliaccess_stub.so
         boot_debug "Removed libcliaccess stub lib"
     fi

     if [ -f "/isan/etc/sysmgr.d/syslogd_n3k.conf" ]; then
         rm /isan/etc/sysmgr.d/syslogd_n3k.conf
     fi
fi

if [[ -z "$IS_N3K" || -n "$IS_DME_ENABLED_N3K" ]]; then
    #extract the usrpython.
    #cd / && tar zxvf /isan/bin/usrpystuff.tgz >&  /dev/null

    # find the right dme side init for this
    #setup nginx/dme
    mkdir -p /nginx/logs
    mkdir -p /var/sysmgr/tmp_logs
    chmod 777 /var/sysmgr/tmp_logs
    mkdir -p /logflash/debug_logs
    chmod 777 /logflash/debug_logs
    #echo "extract ..nginx tar"
    cd / && tar zxvf /isan/etc/dme/nginx.tar.gz &> /dev/null
    cp -r /nginx/html /var/sysmgr_dme
    mkdir -p /var/run/mgmt/devicescript
    mkdir -p /mnt/ifc/log

    cd /
    dme_meta_file=/isan/etc/dme/shmetafiles.tar.gz
    size=`stat -c%s $dme_meta_file 2>/dev/null`
    if ((size < 512)) ; then 
        # need to generate dme_meta_file in target
        echo "generate dme_meta_file" >> /tmp/presysmgr.log
        rmdir -rf /mgmt/shmetafiles &> /dev/null
        mkdir -p /mgmt/shmetafiles 
        /isan/bin/shmemgen -c /mgmt/shmetafiles
        ls -l /mgmt/shmetafiles >> /tmp/presysmgr.log
    else 
        # dme_meta_file have been generated in host
        echo "untar dme_meta_file" >> /tmp/presysmgr.log
        tar zxvf /isan/etc/dme/shmetafiles.tar.gz &> /dev/null
    fi

    rm -f /isan/etc/dme/shmetafiles.tar.gz
    ln -s /isan/etc/mgmt/sharedmeta-hash /var/run/mgmt/sharedmeta-hash
    ln -s /isan/etc/mgmt/dme-objstores.conf /var/run/mgmt/dme-objstores.conf 
    ln -s /isan/etc/mgmt/samlog.config /var/run/mgmt/samlog.config

    echo "Running ishell init" >> /tmp/presysmgr.log
    action "" mkdir -p /mnt/ifc/cfg
fi

# Let's get rid of vshboot here...
action "" mv /isanboot/bin/vshboot /isanboot/bin/dont-use-vshboot

if [ -z "$IS_UNIFIED_PORT_SUPPORTED" ]; then
   rm -rf /lc/isan/bin/fcpc
   rm -rf /lc/isan/etc/sysmgr.d/fcpc.conf
   rm -rf /lc/isan/etc/sysmgr.d/tor/fcpc.conf
   rm -rf /lc/isan/etc/sysmgr.d/lc/fcpc.conf
   rm -rf /lc/isan/etc/cli/fcpc.cli__
   rm -rf /lc/isan/lib/libfcpc.so
   rm -rf /lc/isan/lib/libfcpc_obfl.so
   rm -rf /lc/isan/etc/cli/fcmac_cli.cli_
   rm -rf /lc/isan/lib/libfcmac_cli.so
   rm -rf /lc/isan/lib/libsat.so
   rm -rf /lc/isan/lib/libsatcfg.so
   rm -rf /lc/isan/lib/libxcvr_cli.so
   rm -rf /lc/isan/etc/cli/fcxcvr_cli.cli_
   rm -rf /lc/isan/lib/modules/klm_fcxcvr.o
   rm -rf /lc/isan/lib/modules/klm_fcmac.o
   rm -rf /isan/etc/rc.d/rc.isan-start/S43fcxcvr
   rm -rf /isan/etc/rc.d/rc.isan-start/S44fcmac
   rm -rf /isan/etc/rc.d/rc.isan-stop/S43fcxcvr
   rm -rf /isan/etc/rc.d/rc.isan-stop/S44fcmac
fi

# Now, if we're doing a regular boot, init will jump automatically to
# runlevel 3. Otherwise we need to explicitly jump there.

droptobash=$(proc_cmdline_get droptobash)
if [ "$droptobash" == "yes" ]; then
  ulimit -v unlimited
  echo "Dropping to bash ...."
  /bin/bash
fi

if [ "$regular_boot" != "yes" ]; then
    boot_debug "Explicitly entering runlevel 3"
    # Temporarily put a barrier so that we can get bash before 
    # we go to runlevel 3 for debugging purposes.
    # We can exit bash and bootup sequence will proceed
    ulimit -v unlimited
    # Put this back when we need to debug from bash
    #/bin/bash
    action "Entering runlevel 3" /sbin/telinit 3
fi

# By default, the asap backend is disabled, unless explicitly enabled in platform.inf
enable_dme_asap_backend=0
if [ -e "/bootflash/platform.inf" ] ; then
        enable_dme_asap_backend=`grep enable_dme_asap_backend /bootflash/platform.inf | cut -d= -f2`
fi
if [ "$enable_dme_asap_backend" != "1" ] ; then
    #logprint echo "dme asap backend not explicitly enabled in platform.inf, removing it..."
    conf_files=`find /isan/etc -name dme_asap_backend.conf`
    [[ -n "$conf_files" ]] && rm -f $conf_files;
fi

# TODO: This whole section move to another file
if [[ -n "$IS_N3K" && -z "$IS_DME_ENABLED_N3K" ]] || 
    [[ -n "$IS_WBOX_4G" && -z "$IS_DME_ENABLED_WBOX" ]] ||
    [[ -n "$IS_THURN" && -z "$IS_DME_ENABLED_THURN" ]] ; then
     conf_files=`find /isan | egrep "(svc_ifc_policyelem.conf)|(svc_ifc_confelem.conf)|(svc_ifc_opflexelem.conf)|(svc_ifc_observerelem.conf)|(svc_ifc_dbgrelem.conf)|(svc_ifc_eventmgr.conf)|(svc_ifc_nginx.conf)|(svc_ifc_nxosmock.conf)|(svc_ifc_reader.conf)|(dme_proxy.conf)|(dme_ha_backend.conf)|(dme_test_backend.conf)|(dme_asap_backend.conf)|(dme_example_backend.conf)|(dme_copyrs_backend.conf)|(dme_restart_backend.conf)|(dme_motl_backend.conf)|(dme_motl_example_backend.conf)|(dme_bootup_proxy.conf)"`
    if [ -n "$conf_files" ]; then
       rm $conf_files
    fi

    # Remove svc_ifc related libs and binary
    # Remove unused libs
    for i in $(find /isan | grep librest);
    do
       rm $i
    done
    
    #nginx/conf/uwsgi_config is required for DME nginx; it is not extracted on n3k.
    if [ -n "$IS_N3K" ] || [ -n "$IS_WBOX_4G" ]; then
        for i in $(find /isan | grep uwsgi | grep -v nginx);
        do
           rm $i
        done
    fi
    
    for i in $(find /isan | grep libevents_local);
    do
       rm $i
    done
    
    for i in $(find /isan | grep nsusd\.conf);
     do
        rm $i
        $ECHO "Removed $i"
     done
     
     for i in $(find /isan | grep sharedmemmgr);
     do
        rm $i
     done
     for i in $(find /isan | grep shm_init);
     do
         sed -i '/sharedmemmgr/ s?^?#?' /isan/bin/shm_init
     done
     rm -f /isan/bin/usrpystuff.tgz >&  /dev/null
     if [ -n "$IS_N3K" ] || [ -n "$IS_WBOX_4G" ]; then
        rm -f /isan/dme/nginx.tar.gz >& /dev/null
     fi
fi

if [ -z "$IS_N3K" ]; then
     for i in $(find /isan | grep neutron_usd\.conf);
     do
        rm $i
        $ECHO "Removed $i"
     done
     for i in $(find /lc/isan 2> /dev/null | grep neutron_usd_uc\.conf);
     do
        rm $i
        $ECHO "Removed $i"
     done
     for i in $(find /lc/isan 2> /dev/null | grep mtc_usd\.conf);
     do
        rm $i
        $ECHO "Removed $i"
     done
     for i in $(find /lc/isan 2> /dev/null | grep mtc_cr_usd\.conf);
     do
        rm $i
        $ECHO "Removed $i"
     done
fi

# Create the ins_local URL auth token for NXAPI CLI
(/isan/bin/nginx_ins_local_token_gen.sh ; chown admin /isan/bin/nginx_ins_local_token_gen.sh ; chown admin /isan/bin/nginx_get_ins_local_token.sh) &  

if [ -n "$IS_VIRTUAL" -a -z "$IS_TITAN_EOR" ]; then
    # Remove services not applicable to N9Kv.
    conf_files=`find / | egrep "(nf.conf)|(fcoelc.conf)" | grep -v _ro`
    if [ -n "$conf_files" ]; then
       rm $conf_files
    fi

    # vntagc - is for FEX ports, not applicable to N9Kv.
    # xbar_client - is for managing backplane ports when we have multiple ASICs, not applicable to N9Kv.
    conf_files=`find / | egrep "(vntagc.conf)|(xbar_client_lc.conf)" | grep -v _ro`
    if [ -n "$conf_files" ]; then
       rm $conf_files
    fi
    # Remove vntagc dependancy for sync point 4
    sed -i '/vntagc/ d' /lc/isan/etc/sysmgr.d/synctask4.conf
    sed -i '/vntagc/ d' /lc/isan/etc/sysmgr.d/tor/synctask4.conf
    sed -i '/vntagc/ d' /lc/isan/etc/sysmgr.d/sc/synctask4.conf
    # Remove xbar_client dependancy for sync point 4
    sed -i '/xbar_client/ d' /lc/isan/etc/sysmgr.d/synctask4.conf
    sed -i '/xbar_client/ d' /lc/isan/etc/sysmgr.d/tor/synctask4.conf
    sed -i '/xbar_client/ d' /lc/isan/etc/sysmgr.d/sc/synctask4.conf
fi

if [ -n "$IS_N3K" ] || [ -n "$IS_WBOX_4G" ]; then
     if [ -f "/mnt/pss/switch_chmod_reload_pending" ]; then
         rm /mnt/pss/switch_chmod_reload_pending
         boot_debug "Cleared switch mode reload pending state."
     fi
     # Are we loading a compact image
	 total_mem=`head -n 1 /proc/meminfo | awk {'print $2'}`
     if [ -f "/isan/etc/n3k_compacted_image" ]; then
         if [[ -n "$IS_DME_ENABLED_N3K" && ! -f "/isan/etc/n3k_xl_compacted_image" && $total_mem -ge 7000000 ]]; then
             invalid_compact_image="yes"
         elif [[ -n "$IS_N3K_TPLUS" && ! -f "/isan/etc/n3k_tplus_compacted_image" ]]; then
             invalid_compact_image="yes"
         elif [[ -z "$IS_N3K_TPLUS" && -z "$IS_MTC" && ! -f "/isan/etc/n3k_t2_compacted_image" ]]; then
             invalid_compact_image="yes"
         elif [[ -n "$IS_MTC" && ! -f "/isan/etc/n3k_mtc_compacted_image" ]]; then
             invalid_compact_image="yes"
         fi

         if [ -z "$invalid_compact_image" ]; then
             echo "Loading a compact image"
         else
             echo "Compact image being loaded is ***NOT compatible*** for current platform"
         fi
     fi
     # Lot of old 3k customer scripts need the name of bcm shell
     # to be bcm-sdk-shell
     ln -s /lc/isan/bcm/bcm-shell /isan/bin/bcm-sdk-shell

     # CSCuu26491: Remove panic-on-oom for now on n3k.
     # oom_kill_allocating_task is now enabled, and oom_dump_tasks
     # is already enabled.
     #echo 0 > /proc/sys/vm/panic_on_oom
     #echo 1 > /proc/sys/vm/oom_kill_allocating_task

     # Added for CSCuu29348 - cleanup t2usd LC online indication
     /bin/rm -fr /mnt/pss/t2usd_lc_online

    conf_files=`find /isan | egrep "(nf.conf)|(fcoelc.conf)"`
    if [ -n "$conf_files" ]; then
       rm $conf_files
    fi

    conf_files=`find /lc/isan/ 2> /dev/null| egrep "(nf.conf)|(fcoelc.conf)"`
    if [ -n "$conf_files" ]; then
       rm $conf_files
    fi

    conf_files=`find /lc/isan/ 2> /dev/null | egrep "(vntagc.conf)|(xbar_client_lc.conf)"`
    if [ -n "$conf_files" ]; then
       rm $conf_files
    fi
    if [ $CARD_INDEX != 11060 ]; then
        conf_files=`find /lc/isan/ 2> /dev/null | egrep "neutron_usd.conf"`
        if [ -n "$conf_files" ]; then
            rm -f $conf_files
        fi
    fi
    if [[ $CARD_INDEX != 11066 && $CARD_INDEX != 11119 ]]; then
        conf_files=`find /lc/isan/ 2> /dev/null | egrep "neutron_usd_uc.conf"`
        if [ -n "$conf_files" ]; then
            rm -f $conf_files
        fi
    fi
    if [ $CARD_INDEX != 11066 ]; then
        conf_files=`find /lc/isan/ 2> /dev/null | egrep "mtc_usd.conf"`
        if [ -n "$conf_files" ]; then
            rm -f $conf_files
        fi
    fi
    if [ $CARD_INDEX != 11119 ]; then
        conf_files=`find /lc/isan/ 2> /dev/null | egrep "mtc_cr_usd.conf"`
        if [ -n "$conf_files" ]; then
            rm -f $conf_files
        fi
    fi
    if [[ $CARD_INDEX == 11066 || $CARD_INDEX == 11119 ]]; then
        conf_files=`find /lc/isan/ 2> /dev/null | egrep "t2usd.conf"`
        if [ -n "$conf_files" ]; then
            rm -f $conf_files
        fi
    
        for i in $(find /lc/isan/ 2> /dev/null | grep bcm-shell);
        do
            rm -f $i 2> /dev/null
        done

        for i in $(find /lc/isan/ 2> /dev/null | grep bcm-sdk-shell);
        do
            rm -f $i 2> /dev/null
        done
    fi

    if [ -n "$IS_LXC_TOR" -o -n "$IS_TITAN_EOR" ]; then
        # Remove vntagc dependancy for sync point 4
        sed -i '/vntagc/ d' /lc/isan/etc/sysmgr.d/synctask4.conf 2> /dev/null
        sed -i '/vntagc/ d' /lc/isan/etc/sysmgr.d/tor/synctask4.conf 2> /dev/null
        sed -i '/vntagc/ d' /lc/isan/etc/sysmgr.d/sc/synctask4.conf 2> /dev/null
        # Remove xbar_client dependancy for sync point 4
        sed -i '/xbar_client/ d' /lc/isan/etc/sysmgr.d/synctask4.conf 2> /dev/null
        sed -i '/xbar_client/ d' /lc/isan/etc/sysmgr.d/tor/synctask4.conf 2> /dev/null
        sed -i '/xbar_client/ d' /lc/isan/etc/sysmgr.d/sc/synctask4.conf 2> /dev/null
    else
        # Remove vntagc dependancy for sync point 4
        sed -i '/vntagc/ d' /lc/isan/etc/sysmgr.d/synctask4.conf
        sed -i '/vntagc/ d' /lc/isan/etc/sysmgr.d/tor/synctask4.conf
        sed -i '/vntagc/ d' /lc/isan/etc/sysmgr.d/sc/synctask4.conf
        # Remove xbar_client dependancy for sync point 4
        sed -i '/xbar_client/ d' /lc/isan/etc/sysmgr.d/synctask4.conf
        sed -i '/xbar_client/ d' /lc/isan/etc/sysmgr.d/tor/synctask4.conf
        sed -i '/xbar_client/ d' /lc/isan/etc/sysmgr.d/sc/synctask4.conf
    fi

     if [ -f "/lac_emu.out" ]; then
        rm /lac_emu.out
        boot_debug "removed lac_emu.out"
     fi
     
     if [ -f "/dav_emu.out" ]; then
        rm /dav_emu.out
        boot_debug "removed dav_emu.out"
     fi
    
     if [ -f "/chmnx_lac_emu.out" ]; then
        rm /chmnx_lac_emu.out
        boot_debug "removed chmnx_lac_emu.out"
     fi

     if [ -z "$IS_DME_ENABLED_N3K" ] && [ -z "$IS_DME_ENABLED_WBOX" ]; then
         # Removing sysmgr n9k-sepecific nginx directories and processes
         /bin/rm -fr /var/sysmgr_dme
         /bin/rm -fr /isan/etc/sysmgr.d/sysmgr_dme.conf
         /bin/rm -fr /isan/etc/sysmgr.d/sysmgr_nxapi.conf
     fi
fi

# dme is disabled by default on Thurn
if [[ -n "$IS_THURN" && -z "$IS_DME_ENABLED_THURN" ]] ; then
     # Removing sysmgr DME related directories and processes
     /bin/rm -fr /var/sysmgr_dme
     /bin/rm -fr /isan/etc/sysmgr.d/sysmgr_dme.conf
     /bin/rm -fr /isan/etc/sysmgr.d/dme_applied_backend.conf
fi

#conf_files=`find /lc/isan/ 2> /dev/null | egrep 'mtc_cr_usd.conf|mtc_usd.conf'`

if [ ! -e /cont_id ] ; then
# Only do this in Native TOR mode. In LXC mode, do this in S34kexec_lxc_early_inband from LC container
if [[ -z "$TAHOE_CARD" && "${torissu:=0}" = "1" ]] || [[ "${fastboot:=0}" = "1" && ( "$CARD_INDEX" = "21043" || -n $IS_N3K_TPLUS ) ]]; then
  /lc/isan/bcm/bcm_bde.sh > /tmp/t2usd_boot.log 2>&1
  /lc/isan/bin/t2usd_boot $CARD_INDEX $IS_N3K $IS_N3K_TPLUS > /tmp/t2usd_boot1.log &
  if [ $? -ne 0 ]
  then
    echo "t2usdboot failed ret_val = $?"
  else
    echo `date +"%s"` > /mnt/pss/upgrade/send_lacppdu.txt 
  fi
fi
fi

if [ -n "$IS_MTC" ] ; then
    /bin/rm -rf /isan/etc/sysmgr.d.hybrid_lc/bcm_bde_klm.conf
    /bin/rm -rf /lc/isan/etc/sysmgr.d/tor/bcm_bde_klm.conf
    /bin/rm -rf /lc/isan/etc/sysmgr.d/bcm_bde_klm.conf
    /bin/rm -rf /lc/isan/etc/sysmgr.d/sc/bcm_bde_klm.conf
    /bin/rm -rf /lc/isan/bcm/
fi

if [ -n "$TAHOE_CARD" ] ; then
    boot_debug  "Executing interrupt affinity script********** For tahoe  platforms"
    if [ -z "$IS_LXC_TOR" -a -z "$IS_TITAN_EOR" ]; then
        /lc/isan/bin/set-intr-affinity.sh
    fi

    # insmod klm_tahoe to send out lacp hellos.
    if [[ "${fastboot:=0}" = "1" ]] || [[ "${torissu:=0}" = "1" ]]; then
       echo "fastboot or torissu"

       send_lacp &
    fi
fi
# Only for boddingtons 
if [ "$CARD_INDEX" == "21155" ]; then
    echo "Insmod tofino klm" >> /tmp/tofino_klm.log
    /lc/isan/bin/tofino_klm.sh start 2&> /tmp/insmod.log
fi

# Removing sysmgr n3k-specific nginx process 
if [[ -z "$IS_N3K" || -n "$IS_DME_ENABLED_N3K" ]]; then
     /bin/rm -fr /isan/etc/sysmgr.d/sysmgr_n3k_nxapi.conf
fi

for i in $(find /isan | grep pltfm_mgr_pe\.conf);
    do
        rm $i
    done

# N3K is running below the threshold of 1.55GB Free memory and hence cleaning up unused files for the switches which has less than 4G RAM 
if [[ -n "$IS_N3K" && $total_mem -le 4194304 ]] ; then
	rm -rf /isanboot/lib/modules/*
	rm -rf /isan/lib/modules/dklm.tar
	rm -rf /dklm_lcv.tar
fi

# Camden releases before and including 2.2a have a bug due to which if
# guestshell is destroyed since bootup, only /etc/shared/localtime is
# updated with the correct timezone value. If guestshell is enabled, both
# files are symlinks. Hence if /etc/shared/localtime exists, copy it into
# /etc/localtime - host only looks at /etc/localtime.
if [ -f "/etc/shared/localtime" -a ! -L "/etc/shared/localtime" ]; then
   #echo "File /etc/shared/localtime exists."
   cp /etc/shared/localtime /etc/localtime
fi

if [ -f "/usr/sbin/dmidecode" ]; then
    /bin/ln /usr/sbin/dmidecode /usr/bin/dmidecode
fi

if [ "$(is_vsup)" == "1" ]; then
    # for VSUP remove unwanted files
    cleanup_vsup_files
fi

# store boot time reserved memory detail using 'dmesg'.
# sysinfo would parse this info during its start-up.
if [ ! -f "/tmp/resrv_mem_info.txt" ]; then
    touch /tmp/resrv_mem_info.txt
fi
dmesg | grep Memory > /tmp/resrv_mem_info.txt 2>&1 &

ln -s /var/sysmgr/srv_logs/xport /debug/log-dump
ln -s /bootflash/eem_snapshots   /debug/log-snapshot-auto
ln -s /logflash/evt_log_snapshot /debug/log-snapshot-user


if [ -f "/isan/lib/libcpa_sb_nxos.so" ]; then
rm /isan/lib/libcpa_sb.so
ln -s /isan/lib/libcpa_sb_nxos.so  /isan/lib/libcpa_sb.so
boot_debug "Linked /isan/lib/libcpa_sb_nxos.so"
fi

if [ -f "/lc/isan/lib/libcpa_sb_nxos.so" ]; then
rm /lc/isan/lib/libcpa_sb.so
ln -s /lc/isan/lib/libcpa_sb_nxos.so  /lc/isan/lib/libcpa_sb.so
boot_debug "Linked /lc/isan/lib/libcpa_sb_nxos.so"
fi


echo "load isan:End - `date`" >> /tmp/boot_uptime.log
}

cleanup_load_isan() {
    if [ -n "$IS_N3K" ]; then
        rm /isan/bin/bcm-sdk-shell
    fi

    # Source necessary infra in-order before stopping in reverse order
    for i in $(ls /isan/etc/rc.d/rcS.d/S??*) ; do
        [ ! -f "$i" ] && continue
        case "$i" in
            *.sh)
                # Source infra
                (
                set "stop"
                . $i
                )
                ;;
            *)
                # SKIP clean-up
                ;;
        esac
    done
    for i in $(ls /isan/etc/rc.d/rcS.d/S??* | sort -r) ; do
        [ ! -f "$i" ] && continue
        case "$i" in
            *.sh)
                # SKIP infra source
                ;;
            *)
                # clean-up
                boot_debug "S99load-isan: executing $i stop"
                $i "stop"
                ;;
        esac
    done
}

cleanup_vhost_files() {
    #lc_images not required in vhost
    mv /lcimages/lc2_n9000.nbi /  # for vLC extractpython32
    rm -rf /lcimages/*
    mv /lc2_n9000.nbi /lcimages   # move it back
}

cleanup_vsup_files() {
    # lc images not required in vsup
    # lc container uses ins_tor.bin
    rm -f /lcimages/lc*
    rm -f /lcimages/*fretta*
    rm -rf /var/sysmgr/ftp/ins_tor*.bin
    rm -rf /var/sysmgr/ftp/sflc*.img
}
create_lc_log_dir() {

    if [[ "$IS_TOR" != "y" ]] ; then

       for x in {1..30}
       do
          mkdir -p /var/sysmgr/LC_LOGS/LC_$x
       done
       chmod -R 777 /var/sysmgr/LC_LOGS
       ## removing the SUP directories. Already present as /mnt/bootlogs/
       rm -rf /var/sysmgr/LC_LOGS/LC_27
       rm -rf /var/sysmgr/LC_LOGS/LC_28

    fi

}
if [ "$(is_vhost)" == "1" ]; then
   boot_debug "--> skipping $0 ..."
   cleanup_vhost_files
   exit
fi

case "$1" in
    start)
        /bin/touch /etc/local_reserved_ports
        /bin/echo "56000,56001,50060,50070,27000,32765,32767,32768,33434-33534" > /etc/local_reserved_ports
        setup_load_isan
        create_lc_log_dir
        ##### This is the post prune for LOW mem devices  ######
        low_memory_deviceprune & 
        ;;
    stop)
        cleanup_load_isan
        ;;
esac

