#!/bin/bash
####################################################################
#
#      File: S10mount-ramfs.sup2dc3
#      Name: Amit Das 
#
#      Description:
#       Mounting of the RAM-FS partitions for supdc3x
#	Actual location on disk: /etc/rc.d/rcS.d/S10mount-ramfs.supdc3x
#
#
# Copyright (c) 1985-2004, 2015 by cisco Systems, Inc.
# All rights reserved.
#
#
# $Id: S10mount-ramfs,v 1.10 2004/04/24 00:57:53 ralfo Exp $
#
#####################################################################

. /etc/init.d/mod_ins/module-load-functions
. /etc/init.d/sup.include

boot_debug "Executing /etc/rc.d/rcS.d/S10mount-ramfs"
    
total_mem=`head -n 1 /proc/meminfo | awk {'print $2'}`
boot_debug "Total memory: $total_mem"

if [ $total_mem -ge 13000000 ] ; then
    boot_debug "Using new partition sizes as sup memory is 32G for sup2 heavy"
    var_sysmgr_size=3584m
    dev_shm_size=3072m
    debug_size=5m
    nxos_tmp_size=40m
elif [ $total_mem -ge 8000000 -a $total_mem -le 13000000 ] ; then
    boot_debug "Using new partition sizes as sup memory is 12G for sup2 lite"
    var_sysmgr_size=2048m
    dev_shm_size=1536m
    debug_size=2m
    nxos_tmp_size=20m
else
    var_sysmgr_size=1536m
    dev_shm_size=1536m
    debug_size=2m
    nxos_tmp_size=10m
fi

# mount /isan as ramFS
boot_debug "Mounting /isan commented"

#
# CSCte78552: To avoid login or other critical faliures, lets make 
# /var, /etc, /var/home, /var/log as separate partitions
#

boot_debug "Mounting /var"
action "" tar -pzcf /varbak.tgz /var > /dev/null 2>/dev/null
action "" chmod 777 /var
action "" mount -t tmpfs none /var -o size=200m,mode=777
if [ $? != 0 ] ; then
    boot_debug "Unable to mount /var"
fi
action "" tar  -pzxf /varbak.tgz -C / 2>/dev/null
action "" rm -rf /varbak.tgz

boot_debug "Mounting /etc"
action "" tar -pzcf /etcbak.tgz /etc > /dev/null 2>/dev/null
action "" chmod 777 /etc
action "" mount -t tmpfs none /etc -o size=5m,mode=777 > /dev/null 2>/dev/null
#action "" cp /etc/mtab /etcmtab.bak
action "" tar -pzxf /etcbak.tgz -C / 2>/dev/null
#cat /etcmtab.bak >> /etc/mtab
action "" rm -rf /etcbak.tgz 


boot_debug "Mounting /nxos/tmp"
action "" mkdir -p /nxos/tmp
action "" chmod 777 /nxos/tmp
action "" mount -t tmpfs none /nxos/tmp -o size=$nxos_tmp_size,mode=777

boot_debug "Mounting /var/log"
action "" mkdir -p /var/log
action "" chmod 777 /var/log
action "" mount -t tmpfs none /var/log -o size=50m,mode=777

boot_debug "Mounting /var/home"
action "" mkdir -p /var/home
action "" chmod 777 /var/home
if [ -n "$IS_N3K" ]; then
    action "" mount -t tmpfs none /var/home -o size=2m,mode=777
else
    action "" mount -t tmpfs none /var/home -o size=5m,mode=777
fi

action "" mkdir -p /var/log/external
action "" chmod 777 /var/log/external 
action "" mkdir -p /var/tmp
action "" chmod 777 /var/tmp

boot_debug "Mounting /var/tmp"
if [ -n "$IS_N3K" ]; then
    action "" mount -t tmpfs none /var/tmp -o size=120m,mode=777
else
    action "" mount -t tmpfs none /var/tmp -o size=300m,mode=777
fi
action "" mkdir -p /var/tmp/vsh
action "" chmod 777 /var/tmp/vsh
action "" mkdir -p /var/lock
action "" mkdir -p /var/run
action "" mkdir -p /var/sysmgr
action "" chmod 777 /var/sysmgr 
boot_debug "Mounting /var/sysmgr"
action "" mount -t tmpfs none /var/sysmgr -o size=$var_sysmgr_size,mode=777

if [ $? != 0 ] ; then
    boot_debug "Unable to mount /var/sysmgr"
else
    action "" mkdir -p /var/sysmgr/logs
    action "" chmod 777 /var/sysmgr/logs 
    action "" mkdir -p /var/sysmgr/work
    action "" chmod 777 /var/sysmgr/work 
    action "" mkdir -p /var/sysmgr/ftp
    action "" chmod 777 /var/sysmgr/ftp 
    action "" mkdir -p /var/sysmgr/tmp
    action "" chmod 777 /var/sysmgr/tmp
    action "" mkdir -p /var/sysmgr/srv_logs
    action "" chmod 777 /var/sysmgr/srv_logs
fi

boot_debug "Mounting /var/sysmgr/ftp"
if [ -n "$IS_N3K" ]; then
    action "" mount -t tmpfs none /var/sysmgr/ftp -o size=600m,mode=777
else
    action "" mount -t tmpfs none /var/sysmgr/ftp -o size=800m,mode=777
fi
if [ $? != 0 ] ; then
    boot_debug "Unable to mount /var/sysmgr/ftp"
else
    action "" mkdir -p /var/sysmgr/ftp/img-sync
    action "" chmod 777 /var/sysmgr/ftp/img-sync
    action "" touch /var/sysmgr/ftp/img-sync/vdb_system.data
    action "" mkdir -p /var/sysmgr/ftp/debug_logs
    if [ $? != 0 ] ; then
        boot_debug "Unable to create /var/sysmgr/ftp/debug_logs"
    else
        action "" chmod 777 /var/sysmgr/ftp/debug_logs
    fi
fi

boot_debug "Mounting /var/sysmgr/srv_logs"
if [ -n "$IS_N3K" ]; then
    action "" mount -t tmpfs none /var/sysmgr/srv_logs -o size=20m,mode=777
else
    action "" mount -t tmpfs none /var/sysmgr/srv_logs -o size=100m,mode=777
fi
if [ $? != 0 ] ; then
        boot_debug "Unable to mount /var/sysmgr/srv_logs"
fi

boot_debug "Mounting /var/sysmgr/ftp/debug_logs"
if [ -n "$IS_N3K" ]; then
    action "" mount -t tmpfs none /var/sysmgr/ftp/debug_logs -o size=2m,mode=777
else
    action "" mount -t tmpfs none /var/sysmgr/ftp/debug_logs -o size=10m,mode=777
fi
if [ $? != 0 ] ; then
        boot_debug "Unable to mount /var/sysmgr/ftp/debug_logs"
fi

# Umount old root and flush its buffer
umount /old-root 2>/dev/null
rm -rf /old-root 2>/dev/null

if [ ! -b "/dev/ram0" ] ; then
    mknod /dev/ram0 b 1 0
    mknod /dev/ram1 b 1 1
    ln -s /dev/ram1 /dev/ram
fi

blockdev --flushbufs /dev/ram0 2>/dev/null    # /dev/rd/0 if using devfs

# Mount other isan related directories
boot_debug "Mounting /dev/shm"
action "" mkdir -p /dev/shm

#
# XXX /dev/shm should be sized at a 100m for all platforms other
# than C3. The C3 routing code uses shared memory heavily, and
# requires a bigger /dev/shm.
#
# Increasing /dev/shm to 1.5G. See bug CSCtf46774
action "" mount -t tmpfs none /dev/shm -o size=$dev_shm_size,mode=777

#
# CSCti11338: show tech brief requires dumping show tech of each "thread" into
# some area where that can be concatenated later and displayed for user. Lets
# increase volatile to 600m for this.
#
boot_debug "Mounting /volatile"    
action "" mount -t tmpfs none /volatile -o size=600m,mode=777
boot_debug "Mounting /debug"    
action "" mount -t tmpfs none /debug -o size=$debug_size,mode=777

#DME db file
action "" mkdir -p /mnt/ifc/cfg/db
action "" mount -t tmpfs none /mnt/ifc/cfg/db -o size=1g,mode=777

# Mount related directories for routing-sw components
action "" mkdir -p /dev/mqueue
if [ $? != 0 ] ; then
    boot_debug "Could not create /dev/mqueue"
    exit 1
fi
action "" mount -t mqueue none /dev/mqueue
if [ $? != 0 ] ; then
    boot_debug "Could not mount /dev/mqueue"
    exit 1
fi
action "" mkdir -p /rd
if [ $? != 0 ] ; then
    boot_debug "Could not create /rd"
    exit 1
fi
action "" mkdir -p /rd/fifo
if [ $? != 0 ] ; then
    boot_debug "Could not create /rd/fifo"
    exit 1
fi
action "" chmod 1777 /rd/fifo
if [ $? != 0 ] ; then
    boot_debug "Could not set permissions for /rd/fifo"
    exit 1
fi
action "" mkdir -p /rd/tmp
if [ $? != 0 ] ; then
    boot_debug "Could not create /rd/tmp"
    exit 1
fi

#make vdc directories at root along with e directory needed for linux files lock
MAX_ALLOWED_VDCS=17
VDC_PREFIX=vdc_
if [ $MAX_ALLOWED_VDCS -gt 1 ]; then
    vdc_idx=2
    while [ $vdc_idx -le $MAX_ALLOWED_VDCS ]
    do
        boot_debug "VDC_PREFIX = $VDC_PREFIX"
        action "" mkdir -m 777 /vdc_$vdc_idx
        if [ $? != 0 ] ; then
            boot_debug "Could not create /vdc_$vdc_idx"
            #exit 1
        fi
        action "" mkdir -m 777 /vdc_$vdc_idx/isan
        if [ $? != 0 ] ; then
            boot_debug "Could not create /vdc_$vdc_idx/isan"
            #exit 1
        fi
        action "" mkdir -m 777 /vdc_$vdc_idx/isan/etc
        if [ $? != 0 ] ; then
            boot_debug "Could not create /vdc_$vdc_idx/isan/etc"
            #exit 1
        fi
        vdc_idx=`expr $vdc_idx + 1`   #increment vdc_idx
    done
fi

boot_debug "End of S10-mount-ramfs"
