#! /bin/sh

PATH=/bin:/sbin:/usr/sbin:/usr/bin:$PATH
export PATH

# Get the following details during installation
##### Configuration #####
ACTL_KERNEL_VER="SET_ACTL_KERNEL_VER"
S_FILE_SUFFIX="SET_S_FILE_SUFFIX"
SEP="SET_SEP"
KERNEL_VER_SUFFIX="SET_KERNEL_VER_SUFFIX"
DEPLOY_DIR="SET_DEPLOY_DIR"
OS="SET_OS"
DRIVERS="involflt"
##### Configuration #####

regenerate_list=""

echo >> /var/log/InMage_drivers.log
echo `date` >> /var/log/InMage_drivers.log
echo "----------------------------" >> /var/log/InMage_drivers.log

is_supported_rhel8_kernel()
{
    CURR_KERNEL=$1
    EXTRA_VER1=$2
    EXTRA_VER2=$3

    ret=1
    extra_minor1=`echo ${CURR_KERNEL} | awk -F"." '{print $4}'`
    extra_minor2=`echo ${CURR_KERNEL} | awk -F"." '{print $5}'`
    if [ ! -z $extra_minor1 -a $extra_minor1 -eq $extra_minor1 ] 2> /dev/null; then
        if [ $extra_minor1 -gt $EXTRA_VER1 ]; then
            ret=0
        elif [ $extra_minor1 -eq $EXTRA_VER1 ]; then
            if [ ! -z $extra_minor2 -a $extra_minor2 -eq $extra_minor2 ] 2> /dev/null; then
                if [ $extra_minor2 -ge $EXTRA_VER2 ]; then
                    ret=0
                fi
            fi
        fi
    fi

    return $ret
}

copy_rhel8_drivers()
{
    local _suffix="$2"
    local _drv_dir="$3"

    RHEL8_KMV_V0="80"
    RHEL8_KMV_V1="147"
    RHEL8_KMV_V2="193"
    RHEL8_KMV_V3="240"
    RHEL8_KMV_V4="305"
    RHEL8_KMV_V5="348"

    VER_DIR=$(dirname $(dirname $(dirname $k_dir)))
    K_VER=${VER_DIR##*/}

    KERNEL_MINOR_VERSION=`echo "$1" | cut -d"-" -f2 | cut -d"." -f1`
    KERNEL_COPY_VERSION=""
    ret=1
    case $KERNEL_MINOR_VERSION in
        $RHEL8_KMV_V0)
            KERNEL_COPY_VERSION=$RHEL8_KMV_V0
        ;;
        $RHEL8_KMV_V1|$RHEL8_KMV_V2|$RHEL8_KMV_V3)
            KERNEL_COPY_VERSION=$RHEL8_KMV_V1
        ;;
        $RHEL8_KMV_V4)
            is_supported_rhel8_kernel $K_VER 30 1
            if [ $? -eq "0" ]; then
                KERNEL_COPY_VERSION=$RHEL8_KMV_V4
            fi
        ;;
        $RHEL8_KMV_V5)
            is_supported_rhel8_kernel $K_VER 5 1
            if [ $? -eq "0" ]; then
                KERNEL_COPY_VERSION=$RHEL8_KMV_V5
            fi
        ;;
        *)
            KERNEL_COPY_VERSION=$RHEL8_KMV_V5
        ;;
    esac
    if [ -z $KERNEL_COPY_VERSION ]; then
        echo "Not copying involflt driver to kernel $1" >> /var/log/InMage_drivers.log
    else
        echo "Copying ${_drv_dir}/involflt.ko.${S_FILE_SUFFIX}-${KERNEL_COPY_VERSION}*${SEP}${_suffix} to ${k_dir}" >> /var/log/InMage_drivers.log
        cp -f ${_drv_dir}/involflt.ko.${S_FILE_SUFFIX}-${KERNEL_COPY_VERSION}*${SEP}${_suffix} ${k_dir}/involflt.ko
        ret=$?
    fi

    return $ret
}

copy_rhel7_drivers()
{
    local _suffix="$2"
    local _drv_dir="$3"

    RHEL7_KMV_BASE="123"
    RHEL7_KMV_U3="514"
    RHEL7_KMV_U4="693"

    KERNEL_MINOR_VERSION=`echo ${1} | cut -d"-" -f2 | cut -d"." -f1`
    if [ $KERNEL_MINOR_VERSION -lt "$RHEL7_KMV_U4" ]; then
        if [ $KERNEL_MINOR_VERSION -lt "$RHEL7_KMV_U3" ]; then
            cp -f ${_drv_dir}/involflt.ko.${S_FILE_SUFFIX}-${RHEL7_KMV_BASE}*${SEP}${_suffix} ${k_dir}/involflt.ko
        else
            cp -f ${_drv_dir}/involflt.ko.${S_FILE_SUFFIX}-${RHEL7_KMV_U3}*${SEP}${_suffix} ${k_dir}/involflt.ko
        fi
    else
        cp -f ${_drv_dir}/involflt.ko.${S_FILE_SUFFIX}-${RHEL7_KMV_U4}*${SEP}${_suffix} ${k_dir}/involflt.ko
    fi

    return $?
}

is_sles_kernel_supported()
{
    local ker_ver=$1
    local deploy_dir=$2
    local supported_kernels="${deploy_dir}/bin/drivers/supported_kernels"
    local ret=1

    if [ -f "${supported_kernels}" ]; then
        grep -q "^${ker_ver}:" ${supported_kernels}
        if [ $? -eq "0" ]; then
            ret=0
            ker=`grep "^${ker_ver}:" ${supported_kernels} | awk -F":" '{print $1}'`
            patch_level=`grep "^${ker_ver}:" ${supported_kernels} | awk -F":" '{print $2}'`
            echo "Fetched kernel : $ker and patch level: $patch_level from supported kernels file" >> /var/log/InMage_drivers.log
        fi
    fi

    echo ${ker_ver} | grep -q "\-azure$"
    if [ $? -eq "0" ]; then
        echo "Validating for azure kernel" >> /var/log/InMage_drivers.log
        if [ ${ret} -eq "0" ]; then
            echo ${ker_ver}
            echo "$ker_ver found in supported kernels list" >> /var/log/InMage_drivers.log
            return 0
        fi

        return 1
    fi

    if [ ${ret} -eq "0" ]; then
        echo "SP${patch_level}"
        return 0
    fi

    if [ -f /etc/SuSE-release ] ; then
        patch_level=`grep "PATCHLEVEL = " /etc/SuSE-release | awk '{print $3}'`
        echo "Fetched patch level: $patch_level from SuSE-release file." >> /var/log/InMage_drivers.log
    else
        patch_level=`grep "VERSION=" /etc/os-release | grep -o SP[0-9] | grep -o [0-9]`
        echo "Fetched patch level: $patch_level from os-release file." >> /var/log/InMage_drivers.log
        if [ -z "${patch_level}" ]; then
            echo "No patch level info obtained, dumping os-release file." >> /var/log/InMage_drivers.log
            cat /etc/os-release >> /var/log/InMage_drivers.log
            patch_level=`grep "VERSION=" /etc/os-release | grep -o "SP[0-9]" | grep -o "[0-9]"`
            echo "Fetched patch level: $patch_level from os-release file." >> /var/log/InMage_drivers.log
        fi
        if [ -z "${patch_level}" ]; then
            local os_ver=`grep "VERSION=" /etc/os-release`
            local sp_ver=`echo $os_ver | grep -o "SP[0-9]"`
            local sp_num_ver=`echo $sp_ver | grep -o "[0-9]"`
            echo "os version = $os_ver, sp version = $sp_ver, sp_num version=$sp_num_ver" >> /var/log/InMage_drivers.log
            patch_level=`grep "VERSION=" /etc/os-release | awk -F '[-"]' '{print $3}' | tr -d 'SP'`
            echo "Fetched patch level: $patch_level from os-release file using awk." >> /var/log/InMage_drivers.log
        fi
        if [ -z "${patch_level}" ]; then
            echo "No patch level info obtained from os-release file, marking it as SP0" >> /var/log/InMage_drivers.log
            patch_level=0
        fi
        echo "Final patch level = $patch_level" >> /var/log/InMage_drivers.log
    fi
    if [ ! -z "${patch_level}" ]; then
        echo "SP${patch_level}"
        return 0
    fi

    return 1
}

echo "OS is $OS" >> /var/log/InMage_drivers.log
if echo $OS | grep -q 'RHEL\|SLES11' ; then
    for driver in $DRIVERS
    do
        case $driver in
            involflt) driver_dir="kernel/drivers/char" ;;
        esac

        for suffix in $KERNEL_VER_SUFFIX
        do
            for TOP_DIR in `echo /lib/modules/${ACTL_KERNEL_VER}*${SEP}${suffix}`
            do
                base_kdir=`basename $TOP_DIR`
                test -f /boot/vmlinuz-$base_kdir || continue
                k_dir=$TOP_DIR/$driver_dir
                if [ -d ${k_dir} ]; then
                    echo >> /var/log/InMage_drivers.log
                    echo "Kernel Directory : ${k_dir}" >> /var/log/InMage_drivers.log
                    if [ -f ${k_dir}/${driver}.ko ]; then
                        echo "${driver}.ko already exists in ${k_dir}" >> /var/log/InMage_drivers.log
                        RET_VAL=256
                    else
                        if [ "$OS" = "RHEL7-64" ]; then
                            copy_rhel7_drivers "$k_dir" "$suffix" "${DEPLOY_DIR}/bin"
                        elif [ "$OS" = "RHEL8-64" ]; then
                            copy_rhel8_drivers "$k_dir" "$suffix" "${DEPLOY_DIR}/bin"
                        else
                            cp -f ${DEPLOY_DIR}/bin/${driver}.ko.${S_FILE_SUFFIX}*${SEP}${suffix} ${k_dir}/${driver}.ko
                        fi
                        RET_VAL=$?
                    fi
                    if [ "${RET_VAL}" = "0" ]; then
                        echo "Installed ${driver}.ko successfully under ${k_dir}" >> /var/log/InMage_drivers.log
                        VER_DIR=$(dirname $(dirname $(dirname $k_dir)))
                        K_VER=${VER_DIR##*/}
                        echo "Running depmod for kernel $K_VER ..." >> /var/log/InMage_drivers.log
                        depmod $K_VER
                        if [ $? -eq 0 ]; then
                            echo "depmod successful for above kernel..." >> /var/log/InMage_drivers.log
                            regenerate_list="$K_VER $regenerate_list"
                        else
                            echo "depmod did not succeed for kernel $K_VER ..." >> /var/log/InMage_drivers.log
                            echo "This may affect loading of drivers when you boot this machine using the kernel $K_VER!" >> /var/log/InMage_drivers.log
                        fi
                    elif [ "${RET_VAL}" = "256" ]; then
                        :
                    else
                        echo "Could not install ${driver}.ko under ${k_dir}" >> /var/log/InMage_drivers.log
                    fi
                fi
            done
        done
    done
elif [ "${OS}" = "OL7-64" -o "${OS}" = "OL8-64" ]; then
	# RH compatible modules
    for TOP_DIR in `echo /lib/modules/${ACTL_KERNEL_VER}*${SEP}${KERNEL_VER_SUFFIX}`
    do
        base_kdir=`basename $TOP_DIR`
        test -f /boot/vmlinuz-$base_kdir || continue
        k_dir=$TOP_DIR/kernel/drivers/char
        test -f $k_dir/involflt.ko && continue
        if [ "$OS" = "OL7-64" ]; then
            copy_rhel7_drivers "$k_dir" "$KERNEL_VER_SUFFIX" "${DEPLOY_DIR}/bin/drivers"
        else
            copy_rhel8_drivers "$k_dir" "$KERNEL_VER_SUFFIX" "${DEPLOY_DIR}/bin/drivers"
        fi
        RET_VAL=$?
        if [ "${RET_VAL}" = "0" ]; then
            echo "Installed involflt.ko successfully under ${k_dir}" >> /var/log/InMage_drivers.log
            VER_DIR=$(dirname $(dirname $(dirname $k_dir)))
            K_VER=${VER_DIR##*/}
            echo "Running depmod for kernel $K_VER ..." >> /var/log/InMage_drivers.log
            depmod $K_VER
            if [ $? -eq 0 ]; then
                echo "depmod successful for above kernel..." >> /var/log/InMage_drivers.log
                regenerate_list="$K_VER $regenerate_list"
            else
                echo "depmod did not succeed for kernel $K_VER ..." >> /var/log/InMage_drivers.log
                echo "This may affect loading of drivers when you boot this machine using the kernel $K_VER!" >> /var/log/InMage_drivers.log
            fi
        else
            echo "Could not install involflt.ko under ${k_dir}" >> /var/log/InMage_drivers.log
        fi
    done

    # UEK kernels
    for TOP_DIR in `find /lib/modules/*uek.x86_64 -maxdepth 0 -type d 2>/dev/null`; do
        base_kdir=`basename $TOP_DIR`
        test -f /boot/vmlinuz-$base_kdir || continue
        k_dir=$TOP_DIR/kernel/drivers/char
        test -f $k_dir/involflt.ko && continue
        kernel="`echo $k_dir | cut -f 4 -d "/" | cut -f 1 -d "-"`"
        if [ "$kernel" = "4.14.35" ]; then
            echo $k_dir | grep -q "\-18" && kernel="${kernel}-1818" || kernel="${kernel}-1902"
        fi
        echo "Copying involflt.ko.${kernel}*uek.x86_64" >> /var/log/InMage_drivers.log
        cp -f ${DEPLOY_DIR}/bin/drivers/involflt.ko.${kernel}*uek.x86_64 ${k_dir}/involflt.ko
        RET_VAL=$?
        if [ "${RET_VAL}" = "0" ]; then
            echo "Installed involflt.ko successfully under ${k_dir}" >> /var/log/InMage_drivers.log
            VER_DIR=$(dirname $(dirname $(dirname $k_dir)))
            K_VER=${VER_DIR##*/}
            echo "Running depmod for kernel $K_VER ..." >> /var/log/InMage_drivers.log
            depmod $K_VER
            if [ $? -eq 0 ]; then
                echo "depmod successful for above kernel..." >> /var/log/InMage_drivers.log
                regenerate_list="$K_VER $regenerate_list"
            else
                echo "depmod did not succeed for kernel $K_VER ..." >> /var/log/InMage_drivers.log
                echo "This may affect loading of drivers when you boot this machine using the kernel $K_VER!" >> /var/log/InMage_drivers.log
            fi
        else
            echo "Could not install involflt.ko under ${k_dir}" >> /var/log/InMage_drivers.log
        fi
    done
elif [ "${OS}" = "OL6-32" -o "${OS}" = "OL6-64" ]; then
    echo "OS is OL6-32 or OL6-64. Copying involflt.ko to new kernels which are installed after agent installation." >> /var/log/InMage_drivers.log
    # Copy involflt.ko to RHEL standard kernels matching 2.6.32-*el6.i686 2.6.32-*el6.x86_64
    # Copy involflt.ko to uek kernels matching 2.6.32-100*el6uek.i686, 2.6.32-200*el6uek.i686, 2.6.32-300*el6uek.i686 and 2.6.32-400.*el6uek.i686
    # Copy involflt.ko to uek2 kernels matching 2.6.39-100*el6uek.i686, 2.6.39-200*el6uek.i686, 2.6.39-300*el6uek.i686 and 2.6.39-400.*el6uek.i686
    # Copy involflt.ko to uek kernels matching 2.6.32-100*el6.x86_64, 2.6.32-100*el6uek.x86_64, 2.6.32-200*el6uek.x86_64, 2.6.32-300*el6uek.x86_64 and 2.6.32-400*el6uek.x86_64
    # Copy involflt.ko to uek2 kernels matching 2.6.39-100*el6uek.x86_64, 2.6.39-200*el6uek.x86_64, 2.6.39-300*el6uek.x86_64 and 2.6.39-400*el6uek.x86_64

    for kernel in 2.6.32-131.0.15.el6.i686 2.6.32-131.0.15.el6.x86_64 \
                  2.6.32-100.34.1.el6uek.i686 2.6.32-200.16.1.el6uek.i686 2.6.32-300.3.1.el6uek.i686 2.6.32-400.26.1.el6uek.i686 \
                  2.6.39-100.5.1.el6uek.i686 2.6.39-200.24.1.el6uek.i686 2.6.39-300.17.1.el6uek.i686 2.6.39-400.17.1.el6uek.i686 \
                  2.6.32-100.28.5.el6.x86_64 2.6.32-100.34.1.el6uek.x86_64 2.6.32-200.16.1.el6uek.x86_64 2.6.32-300.3.1.el6uek.x86_64 2.6.32-400.26.1.el6uek.x86_64 \
                  2.6.39-100.5.1.el6uek.x86_64 2.6.39-200.24.1.el6uek.x86_64 2.6.39-300.17.1.el6uek.x86_64 2.6.39-400.17.1.el6uek.x86_64 3.8.13-16.2.1.el6uek.x86_64 \
                  4.1.12-32.1.2.el6uek.x86_64
    do
        # Based on kernel version decide kernel and suffix
        if [ "${kernel}" = "2.6.32-131.0.15.el6.i686" ]; then
            KER=2.6.32
            SUF=el6.i686
        elif [ "${kernel}" = "2.6.32-131.0.15.el6.x86_64" ]; then
            KER=2.6.32
            SUF=el6.x86_64
        elif [ "${kernel}" = "2.6.32-100.34.1.el6uek.i686" ]; then
            KER=2.6.32-100
            SUF=el6uek.i686
        elif [ "${kernel}" = "2.6.32-200.16.1.el6uek.i686" ]; then
            KER=2.6.32-200
            SUF=el6uek.i686
        elif [ "${kernel}" = "2.6.32-300.3.1.el6uek.i686" ]; then
            KER=2.6.32-300
            SUF=el6uek.i686
        elif [ "${kernel}" = "2.6.32-400.26.1.el6uek.i686" ]; then
            KER=2.6.32-400
            SUF=el6uek.i686
        elif [ "${kernel}" = "2.6.39-100.5.1.el6uek.i686" ]; then
            KER=2.6.39-100
            SUF=el6uek.i686
        elif [ "${kernel}" = "2.6.39-200.24.1.el6uek.i686" ]; then
            KER=2.6.39-200
            SUF=el6uek.i686
        elif [ "${kernel}" = "2.6.39-300.17.1.el6uek.i686" ]; then
            KER=2.6.39-300
            SUF=el6uek.i686
        elif [ "${kernel}" = "2.6.39-400.17.1.el6uek.i686" ]; then
            KER=2.6.39-400
            SUF=el6uek.i686
        elif [ "${kernel}" = "2.6.32-100.28.5.el6.x86_64" ]; then
            KER=2.6.32-100
            SUF=el6.x86_64
        elif [ "${kernel}" = "2.6.32-100.34.1.el6uek.x86_64" ]; then
            KER=2.6.32-100
            SUF=el6uek.x86_64
        elif [ "${kernel}" = "2.6.32-200.16.1.el6uek.x86_64" ]; then
            KER=2.6.32-200
            SUF=el6uek.x86_64
        elif [ "${kernel}" = "2.6.32-300.3.1.el6uek.x86_64" ]; then
            KER=2.6.32-300
            SUF=el6uek.x86_64
        elif [ "${kernel}" = "2.6.32-400.26.1.el6uek.x86_64" ]; then
            KER=2.6.32-400
            SUF=el6uek.x86_64
        elif [ "${kernel}" = "2.6.39-100.5.1.el6uek.x86_64" ]; then
            KER=2.6.39-100
            SUF=el6uek.x86_64
        elif [ "${kernel}" = "2.6.39-200.24.1.el6uek.x86_64" ]; then
            KER=2.6.39-200
            SUF=el6uek.x86_64
        elif [ "${kernel}" = "2.6.39-300.17.1.el6uek.x86_64" ]; then
            KER=2.6.39-300
            SUF=el6uek.x86_64
        elif [ "${kernel}" = "2.6.39-400.17.1.el6uek.x86_64" ]; then
            KER=2.6.39-400
            SUF=el6uek.x86_64
        elif [ "${kernel}" = "3.8.13-16.2.1.el6uek.x86_64" ]; then
            KER=3.8.13
            SUF=el6uek.x86_64
        elif $(echo $kernel | grep -q "el6uek.x86_64"); then
            KER=$(echo $kernel | cut -d. -f1-3 | cut -f 1 -d "-")
            SUF="el6uek.x86_64"
        fi

        # Find all available kernel directories matching kernel version and suffix
        for k_dir in `find /lib/modules/${KER}*${SUF} -maxdepth 0 -type d 2>/dev/null`
        do
            base_kdir=`basename $k_dir`
            test -f /boot/vmlinuz-$base_kdir || continue
            NEW_KERNEL=0

            # Copy involflt.ko to each kernel directory
            if [ -f ${k_dir}/kernel/drivers/char/involflt.ko ]; then
                echo "${k_dir}/kernel/drivers/char/involflt.ko already exists" >> /var/log/InMage_drivers.log
            else
                # Copy involflt.ko to each kernel directory
                if [ -d ${k_dir}/kernel/drivers/char ]; then
                    echo "Copying ${DEPLOY_DIR}/bin/involflt.ko.${kernel} to ${k_dir}/kernel/drivers/char/involflt.ko" >> /var/log/InMage_drivers.log
                    cp -f ${DEPLOY_DIR}/bin/involflt.ko.${kernel} ${k_dir}/kernel/drivers/char/involflt.ko
                    if [ $? -eq 0 ]; then
                        echo "Installed involflt.ko successfully under ${k_dir}" >> /var/log/InMage_drivers.log
                        NEW_KERNEL=1
                    fi
                else
                    echo "Could not find ${k_dir}/kernel/drivers/char directory" >> /var/log/InMage_drivers.log
                fi
            fi

            if [ "${NEW_KERNEL}" -eq 1 ]; then
                # Run depmod if it is new kernel
                echo "Running depmod for kernel $k_dir ..." >> /var/log/InMage_drivers.log
                echo "" >> /var/log/InMage_drivers.log
                depmod `basename $k_dir`
                regenerate_list="`basename $k_dir` $regenerate_list"
            fi
        done
    done
elif [ "${OS}" = "SLES12-64" -o "${OS}" = "SLES15-64" ]; then
    echo "OS is SLES12-64 or SLES15-64. Copying involflt.ko to new kernels which are installed after agent installation." >> /var/log/InMage_drivers.log
    for TOP_DIR in `echo /lib/modules/*`
    do
        base_kdir=`basename $TOP_DIR`
        test -f /boot/vmlinuz-$base_kdir || continue
        DRV_DIR=$TOP_DIR/kernel/drivers/char
        if [ -d ${DRV_DIR} ]; then
            KER_DIR=$(dirname $(dirname $(dirname $DRV_DIR)))
            KER_VER=${KER_DIR##*/}
            if [ -f ${DRV_DIR}/involflt.ko ]; then
                echo "involflt.ko already exits under ${DRV_DIR}." >> /var/log/InMage_drivers.log
            else
                vaild_ker=`is_sles_kernel_supported $KER_VER $DEPLOY_DIR`
                echo "The kernel returned from is_sles_kernel_supported  is ${vaild_ker} and return value is $?" >> /var/log/InMage_drivers.log
                if [ -f ${DEPLOY_DIR}/bin/drivers/involflt.ko.${vaild_ker} ]; then
                    echo "Copying ${DEPLOY_DIR}/bin/drivers/involflt.ko.${vaild_ker} as ${DRV_DIR}/involflt.ko" >> /var/log/InMage_drivers.log
                    cp -f ${DEPLOY_DIR}/bin/drivers/involflt.ko.${vaild_ker} ${DRV_DIR}/involflt.ko >> /var/log/InMage_drivers.log
                    if [ $? -eq 0 ]; then
                        echo "Installed involflt.ko successfully under ${DRV_DIR}." >> /var/log/InMage_drivers.log
                        echo "Running depmod for kernel $KER_VER ..." >> /var/log/InMage_drivers.log
                        depmod $KER_VER
                        if [ $? -eq 0 ]; then
                            echo "depmod successful for above kernel..." >> /var/log/InMage_drivers.log
                            regenerate_list="$KER_VER $regenerate_list"
                        else
                            echo "depmod did not succeed for kernel ${KER_VER}..." >> /var/log/InMage_drivers.log
                            echo "This may affect loading of drivers when you boot this system using the kernel $KER_VER." >> /var/log/InMage_drivers.log
                        fi
                    else
                        echo "---> Couldn't install involflt.ko under ${DRV_DIR}" >> /var/log/InMage_drivers.log
                    fi
                else
                    echo "Did not find driver for ${KER_VER}. It is not yet supported." >> /var/log/InMage_drivers.log
                fi
            fi
        else
            echo "---> Directory $DRV_DIR could not be found on this system - skipping installation of driver involflt." >> /var/log/InMage_drivers.log
        fi
    done
else
    echo "Copying involflt.ko to new kernels which are installed after agent installation." >> /var/log/InMage_drivers.log
    # Install involflt driver in new kernels. Skip driver installation when involflt.ko already exists.
    for TOP_DIR in `echo /lib/modules/*`
    do
        base_kdir=`basename $TOP_DIR`
        test -f /boot/vmlinuz-$base_kdir || continue
        DRV_DIR=$TOP_DIR/kernel/drivers/char
        if [ -d ${DRV_DIR} ]; then
            KER_DIR=$(dirname $(dirname $(dirname $DRV_DIR)))
            KER_VER=${KER_DIR##*/}
            if [ -f ${DRV_DIR}/involflt.ko ]; then
                echo "involflt.ko already exits under ${DRV_DIR}." >> /var/log/InMage_drivers.log
            else
                if [ -f ${DEPLOY_DIR}/bin/drivers/involflt.ko.${KER_VER} ]; then
                    echo "Copying ${DEPLOY_DIR}/bin/drivers/involflt.ko.${KER_VER} as ${DRV_DIR}/involflt.ko" >> /var/log/InMage_drivers.log
                    cp -f ${DEPLOY_DIR}/bin/drivers/involflt.ko.${KER_VER} ${DRV_DIR}/involflt.ko >> /var/log/InMage_drivers.log
                    if [ $? -eq 0 ]; then
                        echo "Installed involflt.ko successfully under ${DRV_DIR}." >> /var/log/InMage_drivers.log
                        echo "Running depmod for kernel $KER_VER ..." >> /var/log/InMage_drivers.log
                        depmod $KER_VER
                        if [ $? -eq 0 ]; then
                            echo "depmod successful for above kernel..." >> /var/log/InMage_drivers.log
                            regenerate_list="$KER_VER $regenerate_list"
                        else
                            echo "depmod did not succeed for kernel ${KER_VER}..." >> /var/log/InMage_drivers.log
                            echo "This may affect loading of drivers when you boot this system using the kernel $KER_VER." >> /var/log/InMage_drivers.log
                        fi
                    else
                        echo "---> Couldn't install involflt.ko under ${DRV_DIR}" >> /var/log/InMage_drivers.log
                    fi
                else
                    echo "Did not find driver for ${KER_VER}. It is not yet supported." >> /var/log/InMage_drivers.log
                fi
            fi
        else
            echo "---> Directory $DRV_DIR could not be found on this system - skipping installation of driver involflt." >> /var/log/InMage_drivers.log
        fi
    done
fi

if [ "$1" != "noinitrd" ]; then
    echo "Regenerating initrd for $regenerate_list" >> /var/log/InMage_drivers.log
    for K_VER in $regenerate_list; do
        $DEPLOY_DIR/scripts/initrd/install.sh regenerate $K_VER >> /var/log/InMage_drivers.log
    done
fi


#!/bin/sh
#
# Copyright (c) 2011 InMage
# This file contains proprietary and confidential information and
# remains the unpublished property of InMage. Use, disclosure,
# or reproduction is prohibited except as permitted by express
# written license aggreement with InMage.
#
# File       : collect_support_materials.sh
#
# Usage      : collect_support_materials.sh <directory-for-support-materials>
#
# Collect support materials for Unix based system
#
# Description:
# Script helps user to collect support materials on a given system based on Unix.
# It collects following information
# 1. Agent logs under /var/log
# 2. System logs user /var/
# 3. Get list of crash dumps if available
# 4. Get contents of persistent store
# 5. Collect release build information
# 6. drscout.conf
# 7. Get driver and target context stats
# 8. Get Bitmap files on the system
# 9. Get dmesg output to a file
# 10. Get h/w , s/w configuration information
#
# Author - Prashant Kharche (prkharch@inmage.com)

# Persisten tstore directory
LANG=en_US.UTF-8
export LANG
LC_NUMERIC=en_US.UTF-8
export LC_NUMERIC

pstore=/etc/vxagent/involflt
procstore=/proc/involflt
hostname="`hostname`"
filename="support-materials-$hostname-`date +'%b_%d_%H_%M_%S'`"
vx_dir=`grep INSTALLATION_DIR /usr/local/.vx_version | awk -F'=' '{print $2 }'`
inm_dmit=$vx_dir/bin/inm_dmit
inmage_logs="/var/log/s2* /var/log/svagent*  /var/log/cdpcli* /var/log/CDPMgr* /var/log/CacheMgr* /var/log/appservice* /var/log/involflt* /var/log/linvsnap* /var/log/InMage_Old_Logs $vx_dir/resync $vx_dir/AppliedInfo  /var/log/Scouttunning* /var/log/failovecommandutil* /var/log/cxpsclient* /var/log/vacps* /var/log/customdevicecli*
/var/log/fabricagent* /var/log/oradisc* /var/log/db2out* /var/log/ua_uninstall* /var/log/InMage_drivers* /var/log/ua_install* /var/log/svagents*"
get_scsid_cmd=/etc/vxagent/bin/inm_scsi_id
os=`uname -s`
script_path=/etc/init.d/
partition=""
inm_dbg_str=""
MirrorFilterDevType="5"
INVOLFLT_DRVNAME="involflt"
INVOLFLT_CTRLNODE="/dev/involflt"
src_drv_name=""
dst_drv_name=""
DF_COMMAND="df -hk"

# err logger for the script
inm_log() {
   inm_dbg_str="${inm_dbg_str} |"
   inm_date_str=`date +"%b %d %H:%M:%S"`
   inm_dbg_str="${inm_dbg_str} ${inm_date_str}"
   inm_dbg_str="${inm_dbg_str} $*"
}

# FUNCTION: Invokes inm_logger to write into kernel ring buffer
cleanup() {
   INM_LOGGER=/etc/init.d/inm_logger
   if [ -f $INM_LOGGER ] ; then
      $INM_LOGGER "inm_hotplug: " $inm_dbg_str
   else
      echo $inm_dbg_str
   fi
   inm_dbg_str=""
}

consistency_log()
{
    logPath=`grep -w LogPathname /usr/local/drscout.conf | cut -d= -f2`
    logDir="${logPath}/ApplicationPolicyLogs"
    if [ -d $logDir ]
    then

        # delete policy log files older than 12 hours
        find $logDir -mtime +0 -name "*olicy*.log" -delete

        echo
        echo "consistency_log START"
        find $logDir -mtime -0.01 -exec grep -l vacp {} \; | sort | xargs awk '
        BEGIN{
            IGNORECASE = 1
            fname="tmpfile"
        }
        function prtName() {
            if (fname!=FILENAME){
                "stat -c %y "FILENAME |getline fsize
                printf "\n\t%s, %s\n", FILENAME, fsize
                fname=FILENAME
            }
        }
        /Command Line/ || /Exiting/ || /failed/ || /Error/ {prtName(); printf "\t\t"; print}
        END{
        }'
        echo "consistency_log END"
    fi
}

mds_log()
{
    local _syslog_="/var/log/messages"
    local _disk_=""

    # get the run time in local and UTC
    date &&  date -u

    # Get Driver Stats
    $inm_dmit --get_drv_stat | cut -f 2- -d ":" | sed '/^$/d' | sed 's/^[ \t]*//'

    for _disk_ in `$inm_dmit --get_protected_volume_list`
    do
        echo "#### $_disk_ ####"
        $inm_dmit --get_volume_stat $_disk_ | cut -f 2- -d ":" | sed '/^$/d' | sed 's/^[ \t]*//'
    done

    # Extract all driver logs from syslog
    echo "#### Syslog ####"

    if [ ! -f "$_syslog_" ]
    then
        _syslog_="/var/log/syslog"
    fi

    echo "#### HEAD ####"
    grep "involflt" "$_syslog_" | head -50 > /var/log/involflt_head.cur
    cmp /var/log/involflt_head.cur /var/log/involflt_head.prev > /dev/null 2>&1 && {
        echo "Same as before"
    } || {
        cat /var/log/involflt_head.cur
        cp /var/log/involflt_head.cur /var/log/involflt_head.prev
    }
    echo "#### TAIL ####"
    grep "involflt" "$_syslog_" | tail -50 > /var/log/involflt_tail.cur
    cmp /var/log/involflt_tail.cur /var/log/involflt_tail.prev > /dev/null 2>&1 && {
        echo "Same as before"
    } || {
        cat /var/log/involflt_tail.cur
        cp /var/log/involflt_tail.cur /var/log/involflt_tail.prev
    }

    chmod 640 /var/log/involflt_head.cur
    chmod 640 /var/log/involflt_head.prev
    chmod 640 /var/log/involflt_tail.cur
    chmod 640 /var/log/involflt_tail.prev

    cat /var/log/InMage_drivers.log
    cat /var/log/InMage_drivers.log >> /var/log/InMage_drivers.old.log
    rm -f /var/log/InMage_drivers.log

}

# Main entry point to the script
if [ "$1" = "_mds_" ]
then
    mds_log | tr '\n' '|'
    exit
fi

inm_log "Executing support material collection script "
cleanup
case "${os}" in
    SunOS)
        if /usr/sbin/modinfo | grep -i involflt > /dev/null 2>&1; then
            inm_log " ${INVOLFLT_DRVNAME} driver is loaded"
            cleanup
        else
            echo " ${INVOLFLT_DRVNAME} driver is not loaded"
            inm_log " ${INVOLFLT_DRVNAME} driver is not loaded"
            cleanup
            exit
        fi
    ;;
    Linux)
        if ! lsmod | grep -i involflt > /dev/null 2>&1; then
            inm_log " ${INVOLFLT_DRVNAME} driver is not loaded"
            cleanup
            echo " ${INVOLFLT_DRVNAME} driver is not loaded"
        fi
    ;;
    AIX)
        DF_COMMAND="df -P"
        if ! /usr/bin/genkex | grep -i involflt > /dev/null 2>&1; then
            inm_log " ${INVOLFLT_DRVNAME} driver is not loaded"
            cleanup
            echo " ${INVOLFLT_DRVNAME} driver is not loaded"
            exit
        fi
    ;;
    *)
        echo "OS is not supported"
        exit
    ;;
esac

# Description:
# Script helps user to collection support materials on a given system based on Unix.
# It collects following information
# 1. Agent logs under /var/log
# 2. System logs user /var/
# 3. Get list of crash dumps if available
# 4. Get contents of persistent store
# 5. Collect release build information
# 6. drscout.conf
# 7. Get driver and target context stats
# 8. Get Bitmap files on the system
# 9. Get dmesg output to a file
#

supportpath_input=$1
# Store support materials in directory provided by user
if [ -d "${supportpath_input}" ]
then
    #echo "User input $supportpath_input"
    supportstore=$supportpath_input/$filename
else
    if [ "${supportpath_input}" ]
    then
        echo "Path $supportpath_input for support materials is not valid"
        exit;
    fi
    supportstore="/root/$filename"
fi

# Scout protection model - currently used only for MaxRep
if [ "$2" != "MaxRep" ]
then
    supportmodel_input=
    echo "Evaluating free space in chosen support store location $supportstore"
    $DF_COMMAND $supportpath_input /root
    echo
    echo "Please press (Y/y) if you think chosen support store location has"
    echo "sufficient free space for generating logs: "
    read input

    if [ "$input" != "y" ] && [ "$input" != "Y" ]; then
        echo "Free up some space on desired location for support materials.."
        exit 1
    fi

else
    supportmodel_input=$2
fi

if [ "$supportmodel_input" ]; then
    echo "MaxRep configuration..: $supportmodel_input"
fi

supportstore=`echo $supportstore | sed -e "s: :_:g"`;
filename=`echo $filename | sed -e "s: :_:g"`;
mkdir $supportstore
echo "Support materials will be stored in temporary directory $supportstore";

if [ "${os}" = "SunOS" ]; then
     ID=/usr/xpg4/bin/id
else
     ID=/usr/bin/id
fi

user=`$ID -un`

echo "Collecting agent logs, system logs & dmesg output"
# 1. Agent logs under /var/log
# 2. System logs user /var/
# 9. Get dmesg output to a file
mkdir $supportstore/system_logs
mkdir $supportstore/config
case "${os}" in
    SunOS)
        dmesg > $supportstore/dmesg.log
        cp /var/adm/message* $inmage_logs $supportstore/system_logs/ > /dev/null 2>&1
        cp /var/svc/log/InMage* $supportstore/system_logs/ > /dev/null 2>&1
        acctadm > $supportstore/system_logs/acctadm.log  > /dev/null 2>&1
        cp /var/log/syslog* $supportstore/system_logs/  > /dev/null 2>&1
        if [ -f /var/log/vormetric/vmd.log ]; then
            cp /var/log/vormetric/vmd.log $supportstore/system_logs/  > /dev/null 2>&1
        fi
        if [ -f /var/log/vormetric/secfs.log ]; then
            cp /var/log/vormetric/secfs.log $supportstore/system_logs/  > /dev/null 2>&1
        fi
    ;;
    Linux)
        dmesg > $supportstore/dmesg.log
        if [ "$supportmodel_input" ]; then
            echo "MaxRep configuration..: $supportmodel_input"
            cp $vx_dir/resync $vx_dir/AppliedInfo $supportstore/system_logs/ > /dev/null 2>&1
        else
            cp /var/log/message* /var/log/syslog* $inmage_logs $vx_dir/resync $vx_dir/AppliedInfo $supportstore/system_logs/ > /dev/null 2>&1
        fi
        cp /var/log/syslog* $supportstore/system_logs/  > /dev/null 2>&1
        #tar -cvf $supportstore/system_logs.tar /var/log/message* $inmage_logs $vx_dir/resync $vx_dir/AppliedInfo > /dev/null 2>&1
        cat /proc/cpuinfo > $supportstore/cpuinfo.log
        dmidecode > $supportstore/dmidecode.log
    ;;
    AIX)
        $inm_dmit --op=dmesg  > $supportstore/dmesg.log
        /usr/bin/cp /var/log/involflt_driver.log* $inmage_logs $supportstore/system_logs/ > /dev/null 2>&1
        /usr/bin/cp /var/log/.inmboot_log $supportstore/config/ > /dev/null 2>&1
    ;;
    *)
        echo "OS is not supported"
        exit
    ;;
esac

echo "Collecting persistent store configuration files"
# 4. Get contents of persistent store
# copy whatever is stored in persistent store /etc/vxagent/involflt
mkdir $supportstore/persistentstore
cp -R /etc/vxagent/involflt $supportstore/persistentstore
#tar -cvf $supportstore/persistentstore.tar /etc/vxagent/involflt > /dev/null 2>&1

echo "Collecting Scout configuration files"
# 5. Collect release build information
# 6. drscout.conf
# Get configuration logs under config directory
cp /usr/local/drscout.conf $supportstore/config/
cp /usr/local/.vx_version $supportstore/config/vx_version
cp /usr/local/.fx_version $supportstore/config/fx_version
cp /.vx* $supportstore/config/ > /dev/null 2>&1
cp /.boottimemirroring* $supportstore/config/ > /dev/null 2>&1
#tar -cvf $supportstore/config/other.tar /.vx* /.boottimemirroring* > /dev/null 2>&1
case "${os}" in
    SunOS)
        cp /etc/system $supportstore/config/
        cp /kernel/drv/involflt.conf /kernel/drv/linvsnap.conf $supportstore/config/
        ls -l /var/crash/* > $supportstore/crash_dump-list
    ;;
    Linux)
        ls -l /var/crash/* > $supportstore/crash_dump-list
        /usr/bin/lsscsi > $supportstore/config/lsscsi.txt
        if [ "$supportmodel_input" ]; then
            cp /proc/scsi_tgt/sessions $supportstore/config/
            cp /proc/scsi_tgt/scsi_tgt $supportstore/config/
            cp /proc/scsi_tgt/sgv $supportstore/config/
            ls -l /proc/scsi_tgt/groups/ > $supportstore/config/groups.list
        fi
    ;;
    AIX)
        ls -l /var/adm/ras/ > $supportstore/crash_dump-list
    ;;
    *)
        echo "OS is not supported"
        exit
    ;;
esac

# 7. Get driver and target context stats
mkdir $supportstore/stats 2>&1

echo "Collecting driver related statistics";
case "${os}" in
      SunOS)
      $inm_dmit --get_drv_stat > $supportstore/stats/driver-stats
      $inm_dmit --get_protected_volume_list > $supportstore/stats/pvlist-driver.log
      for volume in `cat $supportstore/stats/pvlist-driver.log | grep -v "IOCTL_INMAGE_GET_PROTECTED_VOLUME_LIST" | awk '{print $1}'`
      do
         tmp_fname=`echo $volume | sed -e "s:/:_:g"`;
         $inm_dmit --get_volume_stat $volume > $supportstore/stats/$tmp_fname;
         $inm_dmit --op=additional_stats --src_vol=$volume > $supportstore/stats/${tmp_fname}_bitmap_changes 2>&1 > /dev/null;
         $inm_dmit --op=latency_stats --src_vol=$volume > $supportstore/stats/${tmp_fname}_latency_stats 2>&1 > /dev/null;
      done

       ;;
      Linux)
      if [ -d /proc/involflt ]
      then
         ls -l $procstore/* > $supportstore/stats/pvlist-driver.log
         for file in $procstore/*
         do
            echo "Stats for entry $file"
            tmpfilename=`echo $file | sed -e "s:/:_:g"`
            cat $file > $supportstore/stats/$tmpfilename
         done
      else
         $inm_dmit --get_drv_stat > $supportstore/stats/driver-stats
         $inm_dmit --get_protected_volume_list > $supportstore/stats/pvlist-driver.log
         for volume in `cat $supportstore/stats/pvlist-driver.log | awk '{print $1}'`
         do
            tmp_fname=`echo $volume | sed -e "s:/:_:g"`;
            $inm_dmit --get_volume_stat $volume > $supportstore/stats/$tmp_fname;
            if [ "$volume" != "DUMMY_LUN_ZERO" ]; then
               $inm_dmit --op=additional_stats --src_vol=$volume > $supportstore/stats/${tmp_fname}_bitmap_changes 2>&1 > /dev/null;
               $inm_dmit --op=latency_stats --src_vol=$volume > $supportstore/stats/${tmp_fname}_latency_stats 2>&1 > /dev/null;
            fi
         done
      fi
        # Add vCon logs if any
        mkdir $supportstore/vCon
        if [ -d /etc/vxagent/vcon ]; then
            cp -R /etc/vxagent/vcon $supportstore/vCon/ 2>&1 > /dev/null
        fi
        if [ -d /etc/vxagent/logs ]; then
            cp -R /etc/vxagent/logs $supportstore/vCon/ 2>&1 > /dev/null
        fi
		if [ -d $vx_dir/etc ]; then
            cp -R $vx_dir/etc $supportstore/vCon/ 2>&1 > /dev/null
		fi

		if [ -f $vx_dir/scripts/azure/PrepareTarget.log ]; then
            	cp  $vx_dir/scripts/azure/PrepareTarget.log $supportstore/vCon/ 2>&1 > /dev/null
		fi
        if [ -d $vx_dir/failover_data ]; then
            cp -R $vx_dir/failover_data/ $supportstore/vCon/ 2>&1 > /dev/null
            if [ -f $vx_dir/scripts/vCon/PrepareTarget.log ]; then
            	cp  $vx_dir/scripts/vCon/PrepareTarget.log $supportstore/vCon/ 2>&1 > /dev/null
            	echo "Role: MT" > $supportstore/vCon/Machine_Role.log 2>&1 > /dev/null
            fi
            if [ -f $vx_dir/scripts/vCon/LinuxP2V.log ]; then
            	cp  $vx_dir/scripts/vCon/LinuxP2V.log $supportstore/vCon/ 2>&1 > /dev/null
            	echo "Role: Source" > $supportstore/vCon/Machine_Role.log 2>&1 > /dev/null
            fi
        fi
		#Data protection logs
		if [ -d /var/log/vxlogs ]; then
			mkdir $supportstore/dataProtection
			cp -R /var/log/vxlogs $supportstore/dataProtection/ 2>&1 > /dev/null
		fi

        mkdir $supportstore/vCon/network
        if [ -d /etc/sysconfig/network-scripts/ ]
        then
            cp -R /etc/sysconfig/network-scripts/*eth* $supportstore/vCon/network 2>&1 > /dev/null
        fi
        # Applicable only for vCon protections
        if [ -f /boot/Configuration.info ]; then
            mkdir $supportstore/vCon/boot
            cp /boot/Configuration.info $supportstore/vCon/boot/ 2>&1 > /dev/null
            md5sum /boot/init*.* 2>&1 > $supportstore/vCon/boot/initrd_md5sum.log
            stat /boot/init*.* 2>&1 > $supportstore/vCon/boot/init_stat.log
            if [ -d /boot/init*INMAGE* ]; then
                cp /boot/init*INMAGE* $supportstore/vCon/boot/ 2>&1 > /dev/null
            fi
            if [ -d /boot/init*InMage* ]; then
                cp /boot/init*InMage* $supportstore/vCon/boot/ 2>&1 > /dev/null
            fi
        fi

        if [ -f /etc/vxagent/logs ]; then
            cp /var/log/inmage_mount.log $supportstore/vCon/ 2>&1 > /dev/null
        fi
		if [ -d $fx_dir ]; then
		    mkdir -p $supportstore/fx_logs/ 2>&1 > /dev/null
            cp $fx_dir/inmage_job*.log $fx_dir/sv*.log $supportstore/fx_logs/ 2>&1 > /dev/null

			cp -R $fx_dir/transport $supportstore/fx_logs/ 2>&1 > /dev/null
		fi

		if [ -d /home/svsystems/var/ApplicationPolicyLogs ]; then
			cp -R /home/svsystems/var/ApplicationPolicyLogs  $supportstore/ 2>&1 > /dev/null
		fi

       ;;
   AIX)
      $inm_dmit --get_drv_stat > $supportstore/stats/driver-stats
      $inm_dmit --get_protected_volume_list > $supportstore/stats/pvlist-driver.log
      for volume in `cat $supportstore/stats/pvlist-driver.log | awk '{print $1}'`
      do
         tmp_fname=`echo $volume | sed -e "s:/:_:g"`;
         $inm_dmit --get_volume_stat $volume > $supportstore/stats/$tmp_fname;
         $inm_dmit --op=additional_stats --src_vol=$volume > $supportstore/stats/${tmp_fname}_bitmap_changes;
         $inm_dmit --op=latency_stats --src_vol=$volume > $supportstore/stats/${tmp_fname}_latency_stats;
      done
       ;;
      *)
         echo "OS is not supported"
         return 1
         ;;
esac

# 8. Get Bitmap files on the system
mkdir ${supportstore}/bitmap_files
cp /root/InMage*.VolumeLog ${supportstore}/bitmap_files/  > /dev/null 2>&1

#10. Get sysyem information..
mkdir ${supportstore}/sysinfo/
echo "Collecting system software/hardware configuration information.. "
cp /etc/*release ${supportstore}/sysinfo/
case "${os}" in
      SunOS)
          cp /etc/vfstab ${supportstore}/sysinfo/
          /usr/platform/`uname -i`/sbin/prtdiag -v > ${supportstore}/sysinfo/prtdiag.txt
          /usr/sbin/modinfo > ${supportstore}/sysinfo/modinfo.log
          /usr/sbin/prtconf -v > ${supportstore}/sysinfo/prtconf.txt
          /usr/bin/pkginfo -l > ${supportstore}/sysinfo/pkginfo.log
          /usr/sbin/patchadd -p > ${supportstore}/sysinfo/patchadd-p.txt
          ps -eo zone,uid,pid,ppid,time,comm,vsz,osz,pmem,rss > ${supportstore}/sysinfo/ps_detailed.txt
          /usr/sbin/cryptoadm list -v > ${supportstore}/sysinfo/cryptoadm.txt
          if [ "$user" = "root" ]
          then
              echo ::nm | /usr/bin/mdb -k > ${supportstore}/sysinfo/mdb-nm.log
          fi
          iostat -xdm -e > ${supportstore}/sysinfo/iostat_xdm.txt
          echo "Executing sar command. It takes about 15 seconds"
          sar -aAbcdgkmpqruvwy 5 3 > ${supportstore}/sysinfo/sar_A_5_3.log
          uptime > $supportstore/uptime.txt
          ipcs -a > ${supportstore}/sysinfo/ipcs_a.log
          fmadm faulty -a -v> ${supportstore}/sysinfo/fmadm_faulty.log
          svcs > ${supportstore}/sysinfo/svcs.log
          svcs -xv > ${supportstore}/sysinfo/svcs_xv.log
          vmstat -p > ${supportstore}/sysinfo/vmstat_p.log
          vmstat -s > ${supportstore}/sysinfo/vmstat_s.log
          mpstat > ${supportstore}/sysinfo/mpstat.log
      ;;
      Linux)
          ls -l /boot > ${supportstore}/sysinfo/boot.txt
          for i in /boot/init*
          do
              echo $i > ${supportstore}/sysinfo/initrd.txt
              echo "===================" >> ${supportstore}/sysinfo/initrd.txt
              which lsinitramfs > /dev/null 2>&1
              if [ $? -eq 0 ]; then
                  lsinitramfs $i >> ${supportstore}/sysinfo/initrd.txt
              else
                  which lsinitrd > /dev/null 2>&1
                  if [ $? -eq 0 ]; then
                      lsinitrd $i >>  ${supportstore}/sysinfo/initrd.txt
                  else
                      zcat $i | cpio -itv >> ${supportstore}/sysinfo/initrd.txt
                  fi
              fi
              echo "===================" >> ${supportstore}/sysinfo/initrd.txt
          done
          /sbin/sfdisk -luS 2> /dev/null | grep -e '/dev/' | grep -v Empty > ${supportstore}/sysinfo/sfdisk.txt
          dmsetup table > ${supportstore}/sysinfo/dmsetup.txt 2>&1
          vgdisplay -v > ${supportstore}/sysinfo/vgdisplay_v.txt 2>&1
          /bin/cp /etc/fstab ${supportstore}/sysinfo/
          /bin/cp /etc/mtab ${supportstore}/sysinfo/
          /sbin/lspci  -v > ${supportstore}/sysinfo/lspci.txt
          /sbin/lsmod > ${supportstore}/sysinfo/lsmod.txt
          cat /proc/meminfo  > ${supportstore}/sysinfo/meminfo.txt
          cat /proc/cpuinfo  > ${supportstore}/sysinfo/cpuinfo.txt
          cat /proc/mounts > ${supportstore}/sysinfo/proc_mounts.txt
          rpm -qa  > ${supportstore}/sysinfo/rpmlist.txt
          ps -aef > ${supportstore}/sysinfo/ps.txt
          ps -eo zone,uid,pid,ppid,time,comm,vsz,osz,pmem,rss > ${supportstore}/sysinfo/ps_detailed.txt
          mount > ${supportstore}/sysinfo/mount.log
          last > ${supportstore}/sysinfo/last.log
          uname -a > ${supportstore}/sysinfo/uname-a.log
          cp /etc/sysctl.conf  ${supportstore}/sysinfo/
          sysctl -p  > ${supportstore}/sysinfo/sysctl_p.log
          ipcs -a > ${supportstore}/sysinfo/ipcs_a.log
          iostat -xdm > ${supportstore}/sysinfo/iostat_xdm.txt
          > $supportstore/disk-stats
          for i in `ls /sys/block/`
          do
            echo "Stats for $i" >> $supportstore/disk-stats
            cat /sys/block/$i/stat >> $supportstore/disk-stats
          done
          echo "Executing sar command and it takes about 15 seconds"
          sar -A 5 3 2>&1 > ${supportstore}/sysinfo/sar_A_5_3.log
          uptime > $supportstore/uptime.txt
          top -b -d 1 -n 3 > $supportstore/sysinfo/top_b_d_1_n_3.log
          chkconfig --list > ${supportstore}/sysinfo/chkconfig_list.log
          vmstat -m > ${supportstore}/sysinfo/vmstat_m.log
          vmstat -s > ${supportstore}/sysinfo/vmstat_s.log
          vmstat -d > ${supportstore}/sysinfo/vmstat_d.log
          cat /proc/mdstat > ${supportstore}/sysinfo/mdstat
		  service --status-all > ${supportstore}/RunningServices_list.log
		  lsmod | sort > ${supportstore}/LoadedModules_list.log
		  find /lib/modules -type f -name linvsnap.ko > ${supportstore}/LinuxUA-linvsnapDriver_list.log
		  find /lib/modules -type f -name involflt.ko > ${supportstore}/LinuxUA-involfltDriver_list.log
		  which rpm && {
            rpm -qa | sort > ${supportstore}/InstalledRPMS_list.log
          } || {
            dpkg -l | sort > ${supportstore}/InstalledRPMS_list.log
          }
		  chkconfig --list > ${supportstore}/AllServices_list.log
      ;;
      AIX)
          /usr/bin/cp /etc/filesystems ${supportstore}/sysinfo/
          /usr/bin/lslpp -L > ${supportstore}/sysinfo/lslpp.lst
          /usr/bin/genkex > ${supportstore}/sysinfo/genkex.lst
          /usr/sbin/lspv > $supportstore/lspv.output
          /usr/sbin/lsdev > $supportstore/lsdev.output
          /usr/sbin/lsconf > $supportstore/sysinfo/lsconf.output
          /usr/bin/mkdir $supportstore/odm_info
          /usr/bin/odmget Config_Rules > $supportstore/odm_info/Config_Rules.lst
          /usr/bin/odmget CuVPD > $supportstore/odm_info/CuVPD.lst
          /usr/bin/odmget CuDv > $supportstore/odm_info/CuDv.lst
          /usr/bin/odmget CuAt > $supportstore/odm_info/CuAt.lst
          /usr/bin/odmget CuDvDr > $supportstore/odm_info/CuDvDr.lst
          /usr/bin/odmget PdDv > $supportstore/odm_info/PdDv.lst
          /usr/bin/odmget PdAt > $supportstore/odm_info/PdAt.lst
          /usr/bin/ps -aef > ${supportstore}/sysinfo/ps_aef.txt
          /usr/bin/ps auxw > ${supportstore}/sysinfo/ps_auxw.txt
          uptime > $supportstore/uptime.txt
          sar -A 5 3 > ${supportstore}/sysinfo/sar_A_5_3.log
          errpt -A > ${supportstore}/sysinfo/errpt_A.log
          ipcs -a > ${supportstore}/sysinfo/ipcs_a.log
          vmstat -v > ${supportstore}/sysinfo/vmstat_v.log
          vmstat -l > ${supportstore}/sysinfo/vmstat_l.log
          vmstat -s > ${supportstore}/sysinfo/vmstat_s.log
          svmon -G > ${supportstore}/sysinfo/svmon_G.log
          svmon -P > ${supportstore}/sysinfo/svmon_P.log
          mpstat > ${supportstore}/sysinfo/mpstat.log
          vmo -a > ${supportstore}/sysinfo/vmo_a.log
          pagesize -af > ${supportstore}/sysinfo/pagesizes.log
          iostat -alDRT 1 1 > ${supportstore}/sysinfo/iostat_alDRT_1_1.log
       ;;
       *)
         echo "OS is not supported"
         return 1
         ;;
esac

mkdir $supportstore/target
$vx_dir/bin/cdpcli  --listtargetvolumes 2>&1 > $supportstore/target/cdpcli-listtargetvolumes
$vx_dir/bin/cdpcli  --showreplicationpairs 2>&1 > $supportstore/target/cdpcli-showreplicationpairs
$vx_dir/bin/cdpcli  --showsnapshotrequests 2>&1 > $supportstore/target/cdpcli-showsnapshotrequests

$vx_dir/bin/cdpcli  --showreplicationpairs |grep Catalogue > $supportstore/target/retentionpath.log 2>&1

install_path=`echo $vx_dir | sed -e "s/\/[^\/]*$//"`;
cp $install_path/patch.log $supportstore/

if [ -d $install_path/ApplicationData/etc ]; then
    cp  $install_path/ApplicationData/etc/*.log $supportstore/ 2>&1 > /dev/null
fi
filename="$supportstore/target/retentionpath.log"
while read -r line
do
    retname=$line
	retdbname=`echo $retname | awk -F ':' '{print $2}'`
	retpath=`echo $retdbname | awk -F 'cdp' '{print $1}'`
	mkdir -p $supportstore/$retpath;
	cp $retdbname $supportstore/$retpath
done < "$filename"

#tar -cvf ${supportstore}/bitmap_files.tar /root/InMage*.VolumeLog  > /dev/null 2>&1
tar -cvf $supportstore.tar $supportstore > /dev/null 2>&1
gzip $supportstore.tar > /dev/null 2>&1
rm -rf $supportstore
echo "Compressed support materials file $supportstore.tar.gz"
exit;


#!/bin/bash

if [[ $# -ne 1 ]]; then
    echo "Insufficient number of args"
    exit 1
fi

INSTALL_DIR=$1
inm_dmit=$INSTALL_DIR/inm_dmit

sd_devices=`ls -1 /dev/sd*`
for i in $sd_devices; do
    op=`$inm_dmit --op=get_blk_mq_status --src_vol=$i | grep -i 'enabled'`
    ret=$?
    #echo "i: $i, ret: $ret"
    if [[ $ret == 0 ]]; then
        echo "blk_mq enabled"
        exit 1
    fi
done

echo "blk_mq disabled"


#! /bin/sh

########################
## DECLARED VARIABLES ##
########################

LOG_FILE=/var/log/Post_Process_Linux_MT.log

if [ ! -f /usr/local/.vx_version ] && [ ! -f /usr/local/.fx_version ]; then
    echo "FX/VX is not installed on this setup. Aborting the Post Process script execution."
    exit 1
fi

if [ -f /usr/local/.vx_version ]; then
    VX_INST_DIR=`grep 'INSTALLATION_DIR' /usr/local/.vx_version |awk -F"=" '{print $2}'`
else
    echo "VX is not installed on this setup."
fi

if [ -f /usr/local/.fx_version ]; then
    FX_INST_DIR=`grep 'INSTALLATION_DIR' /usr/local/.fx_version |awk -F"=" '{print $2}'`
else
    echo "FX is not installed on this setup."
fi

###############
## FUNCTIONS ##
###############

DO()
{
 eval "$1 2>> $LOG_FILE | tee -a $LOG_FILE"
}

LOG()
{
 echo "$1" >> $LOG_FILE
}

Validate_IP()
{
    IP=$1
    echo $IP | grep "\.$" >/dev/null 2>&1
    if [ "$?" -eq "0" ]; then
        DO 'echo "Invalid IP:$IP. Try again with the correct IP"'
        exit 1
    else

        FLD1=`echo "$IP" | awk -F'.' '{print $1}'`
        FLD2=`echo "$IP" | awk -F'.' '{print $2}'`
        FLD3=`echo "$IP" | awk -F'.' '{print $3}'`
        FLD4=`echo "$IP" | awk -F'.' '{print $4}'`
        if [ -z "${FLD1}" -o -z "${FLD2}" -o -z "${FLD3}" -o -z "${FLD4}" ];then
            DO 'echo -e "Invalid IP:$IP. Try again with correct IP "'
            exit 1
        else
            oldIFS=$IFS
            IFS=.
            set -- $IP

            if [ "$#" -ne "4" ]; then
                DO 'echo "Invalid IP:$IP. Try again with the correct IP"'
                exit 1
            elif [ "$1" -eq "0" ]; then
                DO 'echo "Invalid IP:$IP. Try again with the correct IP"'
                exit 1
            fi
            IFS=$oldIFS
        fi

        for oct in $FLD1 $FLD2 $FLD3 $FLD4;
        do
            echo $oct | egrep "^[0-9]+$" >/dev/null 2>&1
            if [ "$?" -ne "0" ]; then
                DO 'echo -e "Invalid IP:$IP. Try again with correct IP"'
                exit 1
            else
                if [ "$oct" -lt "0" -o "$oct" -gt "255" ]; then
                    DO 'echo -e "Invalid IP:$IP. Try again with correct IP"'
                    exit 1
                fi
            fi
        done
    fi
}

Validate_Port()
{
    CX_PORT=$1
    echo $CX_PORT | grep -q "^[0-9]\+$"
    if [ $? -ne 0 ]; then
        DO 'echo "Invalid port. Try again with correct port in the range 1-65535."'
        exit 1
    fi
    if [ "$CX_PORT" -lt "1" -o "$CX_PORT" -gt "65535" ]; then
        DO 'echo "Invalid port. Try again with correct port in the range 1-65535."'
        exit 1
    fi
}

Success_Msg()
{
    DO 'echo '
    DO 'echo "Post Processing steps for Linux MT are successfully completed."'
}

Replace()
{

    DO 'echo "Chosen CX IP is $SERVER_IP_ADDRESS"'
    DO 'echo "Chosen CX PORT is $SERVER_PORT_NUMBER"'
    DO 'echo "Chosen Agent is $AGENT_IP"'

    if [ ! -z "$VX_INST_DIR" ]; then
	DO 'echo '
        DO 'echo "Keeping ${AGENT_IP} in /etc/hosts"'
	sed -i -e "s|10.80.253.124|${AGENT_IP}|g" /etc/hosts

	DO 'echo '
        DO 'echo "Changing Hostname key in drscout.conf with chosen CX IP : $SERVER_IP_ADDRESS"'
        sed -i -e "s|^Hostname.*|Hostname=$SERVER_IP_ADDRESS|g" $VX_INST_DIR/etc/drscout.conf
        DO 'echo "Changing Port key in drscout.conf with chosen CX PORT : $SERVER_PORT_NUMBER"'
        sed -i -e "s|^Port.*|Port=$SERVER_PORT_NUMBER|g" $VX_INST_DIR/etc/drscout.conf

        if [ ! -z "$GUID" ];then
            DO 'echo "Adding HostId key in drscout.conf"'
            sed -i -e "s|^HostId.*|HostId=$GUID|g" $VX_INST_DIR/etc/drscout.conf
        fi

        DO 'echo '
        DO 'echo "Enabling vxagent service"'
        chkconfig --level 1235 vxagent on

        DO 'echo '
        DO 'echo "Starting the vxagent service"'
        /etc/init.d/vxagent start
    fi

    if [ ! -z "$FX_INST_DIR" ]; then
        DO 'echo '
        DO 'echo "Changing SVServer key in config.ini with chosen CX IP : $SERVER_IP_ADDRESS"'
        sed -i -e "s|^SVServer =.*|SVServer = $SERVER_IP_ADDRESS|g" $FX_INST_DIR/config.ini

        DO 'echo "Changing SVServerPort key in config.ini with chosen CX PORT : $SERVER_PORT_NUMBER"'
        sed -i -e "s|^SVServerPort =.*|SVServerPort = $SERVER_PORT_NUMBER|g" $FX_INST_DIR/config.ini

        if [ ! -z "$GUID" ];then
            DO 'echo "Adding HostId key in config.ini"'
            sed -i -e "s|^HostId =.*|HostId = $GUID|g" $FX_INST_DIR/config.ini
        fi

        DO 'echo '
        DO 'echo "Moving /etc/svagent to /etc/init.d/"'
        mv /etc/svagent /etc/init.d/svagent >> $LOG_FILE 2>&1

        DO 'echo "Starting svagent service"'
        /etc/init.d/svagent start
    fi

    # If HOST_NAME argument is passed, we are changing the hostname and rebooting the system.
    if [ ! -z "$HOST_NAME" ]; then
        DO 'echo "Changing the HOSTNAME value in /etc/sysconfig/network and invoking hostname command."'
        sed -i -e "s|HOSTNAME.*|HOSTNAME=$HOST_NAME|g" /etc/sysconfig/network
        hostname "$HOST_NAME"

        #Rebooting the system to change the hostname.
        DO 'echo "Rebooting the system to change the hostname"'
        reboot
    fi

}

##########
## MAIN ##
##########

DO 'echo '
LOG "-------------------------------------------------------------------"
LOG "`date`"
LOG "-------------------------------------------------------------------"

if [ ! -z $1 ]; then
    if echo $1 | grep -q ^- ; then
        while getopts :h:a:i:p:g:H: opt
        do
            case $opt in
                i)SERVER_IP_ADDRESS="$OPTARG";;
                p)SERVER_PORT_NUMBER="$OPTARG" ;;
                a)AGENT_IP="$OPTARG";;
                g)GUID="$OPTARG";;
                H)HOST_NAME="$OPTARG";;
                h|-*|*) echo "Usage: $0 [ -i <IP Address of the CX> ] [ -p <CX Server Port> ] [ -a <IP Address of the Agent> ] [ -g <GUID of CX Server> ] [ -H <HOTNAME> ]"
                    exit 1
                    ;;
            esac
        done
    fi
else
    echo "Please specify the installation options on the command-line."
    exit 1
fi

# If SERVER_IP_ADDRESS, SERVER_PORT_NUMBER and AGENT_IP are not empty, we are validating them.
if [ ! -z "$SERVER_IP_ADDRESS" -a ! -z "$SERVER_PORT_NUMBER" -a ! -z "$AGENT_IP" ]; then
    Validate_IP "$SERVER_IP_ADDRESS"

    Validate_Port "$SERVER_PORT_NUMBER"

    Validate_IP "$AGENT_IP"
else
    DO 'echo "please enter -i <IP Address of the CX> -p <CX Server Port> -a <IP Address of the Agent> -g <GUID of CX Server> -H <HOSTNAME> as command line arguments to the script."'
    exit 1
fi

EXISTING_GUID=`grep "HostId" $VX_INST_DIR/etc/drscout.conf|awk -F'=' '{print$2}'`

if [ -z "$EXISTING_GUID" -a -z "$GUID" ];then
    DO 'echo "Generating fresh Host Guid."'
    GUID=`uuidgen`
fi

Replace

Success_Msg


#!/bin/sh

echo
echo "========================================" >> /var/log/TuneMaxRep.log
echo `date` >> /var/log/TuneMaxRep.log

HomeLun=`mount |grep -w "/home" |awk '{print $1}'`
echo "HomeLun : ${HomeLun}" >> /var/log/TuneMaxRep.log
if [ -n "${HomeLun}" ];then
        blockdev --setra 65536 $HomeLun #32MB  readahead.
	mount -o noatime,nodiratime,remount,rw /home
fi

echo "cat dirty_background_ratio, default is 10" >> /var/log/TuneMaxRep.log
cat /proc/sys/vm/dirty_background_ratio >> /var/log/TuneMaxRep.log

echo "cat dirty_ratio, default is 40" >> /var/log/TuneMaxRep.log
cat /proc/sys/vm/dirty_ratio >> /var/log/TuneMaxRep.log

echo "cat dirty_writeback_centisecs, default is 499" >> /var/log/TuneMaxRep.log
cat /proc/sys/vm/dirty_writeback_centisecs >> /var/log/TuneMaxRep.log

echo "cat dirty_expire_centisecs, default is 2999" >> /var/log/TuneMaxRep.log
cat /proc/sys/vm/dirty_expire_centisecs >> /var/log/TuneMaxRep.log

echo "Tuning Linux VM parameters..." >> /var/log/TuneMaxRep.log
echo "setting dirty_background_ratio to 100" >> /var/log/TuneMaxRep.log
echo 100 > /proc/sys/vm/dirty_background_ratio

echo "setting dirty_ratio to 99 on Source" >> /var/log/TuneMaxRep.log
echo 99 > /proc/sys/vm/dirty_ratio

echo "setting dirty_writeback_centisecs to 499" >> /var/log/TuneMaxRep.log
echo 499 > /proc/sys/vm/dirty_writeback_centisecs

echo "setting dirty_expire_centisecs to 48000" >> /var/log/TuneMaxRep.log
echo 48000 > /proc/sys/vm/dirty_expire_centisecs

echo "========================================" >> /var/log/TuneMaxRep.log