#!/bin/bash -x

#GENERIC FUNCTIONS WHICH WILL USED FOR LINUX_P2V PROJECT.
_UUID_=""
_LABEL_=""
_disk_name_="" #contains Target device Name,
_base_diskname_=""
_dev_name_="" #Contains device name for a UUID or Label. Either it could be a partition or entire device.
_real_partition_name_="" #It contains partition name corresponding to sym-link.
_lv_dev_name_="" #Contains alias for an LV.

#FINDS UUID INFORMATION FROM FSTAB-BLKID INFORMATION
function Find_UUID_FSTABINFO
{
    _UUID_=""
    _partition_name_=$1
    _uuid_=`sed -n  "/^[=]* FSTAB UUID Information - START [=]*/,/^[=]* FSTAB UUID Information - END [=]*/p" "$_INPUT_CONFIG_FILE_" | grep "$_partition_name_" | head -1`
    if [ "${_uuid_}" ]; then
        _uuid_=`echo $_uuid_ | awk -F"UUID=" '{ print $2 }' | awk '{ print $1 }' | sed -e "s/\"//g"`
        _UUID_=$_uuid_
    fi
    return $_SUCCESS_
}

#FINDS UUID INFORMATION FROM BLKID INFORMATION
function Find_UUID_BLKID
{
    _UUID_=""
    _partition_name_=$1
    _uuid_=`sed -n  "/^[=]* BLKID Information - START [=]*/,/^[=]* BLKID Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | grep "$_partition_name_"`
        if [ "${_uuid_}" ]; then
                _uuid_=`echo $_uuid_ | awk -F"UUID=" '{ print $2 }' | awk '{ print $1 }' | sed -e "s/\"//g"`
                _UUID_=$_uuid_
        fi
        if [ -z $_UUID_ ]; then
            return $_FAILED_
        fi
        return $_SUCCESS_
}

#FINDS LABEL INFORMATION FROM BLKID INFORMATION
function Find_LABEL_BLKID
{
    _partition_name_=$1
    _label_=`sed -n  "/^[=]* BLKID Information - START [=]*/,/^[=]* BLKID Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | grep "$_partition_name_"`
        if [ "${_label_}" ]; then
                _label_=`echo $_label_ | awk -F"LABEL=" '{ print $2 }' | awk '{ print $1 }' | sed -e "s/\"//g"`
                _LABEL_=$_label_
        fi
        if [ -z $_LABEL_ ]; then
            return $_FAILED_
        fi
        return $_SUCCESS_
}

function Get_Partition_Name_BY_ID
{
    _partition_name_=$1
    _real_partition_name_=""
    echo "Finding disk for partition : $_partition_name_ using by-id information"

    _temp_partition_name_=`echo $_partition_name_ | sed 's:\/dev\/disk\/by-id\/::g'`
    echo "temp partition name = $_temp_partition_name_"
    _real_partition_name_=`sed -n  "/^[=]* BY-ID Information - START [=]*/,/^[=]* BY-ID Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | egrep -w "$_temp_partition_name_ "`
    echo "real partition details = $_real_partition_name_"
    _real_partition_name_=`echo  $_real_partition_name_ | awk -F"-> " '{ print $2 }'| sed -e "s:\.\.\/::g"`
    if [ ! "${_real_partition_name_}" ]; then
        echo "ERROR :: Failed to find sym link for partition $_partition_name_."
        return $_FAILED_
    fi

    # Add '/dev/' prefix to _real_partition_name_ if its not already present.
    [[ "$_real_partition_name_" =~ ^\/dev\/ ]] || _real_partition_name_="/dev/$_real_partition_name_"
    echo "Found sym link for partition $_real_partition_name_ , sym link = $_partition_name_"
    return  $_SUCCESS_
}

function Get_Partition_Name_BY_UUID
{
    _partition_name_=$1
    _real_partition_name_=""
    echo "Finding disk for partition : $_by_uuid_ using by-uuid information"

    #NOW WE HAVE TO REMOVE /DEV/DISK/BY-UUID fROM PARTITION NAME PASSED.
    _temp_partition_name_=`echo $_partition_name_ | sed 's:\/dev\/disk\/by-uuid\/::g'`

     _real_partition_name_=`sed -n  "/^[=]* BY-UUID Information - START [=]*/,/^[=]* BY-UUID Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | egrep -w "$_temp_partition_name_ "`

    _real_partition_name_=`echo  $_real_partition_name_ | awk -F"-> " '{ print $2 }'| sed -e "s:\.\.\/::g"`
        if [ ! "${_real_partition_name_}" ]; then
                echo "ERROR :: Failed to find sym link for partition $_partition_name_."
                return $_FAILED_
        fi
    _real_partition_name_="/dev/$_real_partition_name_"
        echo "Found sym link for partition $_partition_name_ , sym link = $_real_partition_name_."
        return  $_SUCCESS_
}

function Get_partition_Name_BY_LABEL
{
    _partition_name_=$1
    _real_partition_name_=""
    echo "Finding disk for partition : $_by_label_ using by-label information"

        _temp_partition_name_=`echo $_partition_name_ | sed 's:\/dev\/disk\/by-label\/::g'`

    _real_partition_name_=`sed -n  "/^[=]* BY-LABEL Information - START [=]*/,/^[=]* BY-LABEL Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | egrep -w "$_temp_partition_name_ "`

    _real_partition_name_=`echo  $_real_partition_name_ | awk -F"-> " '{ print $2 }'| sed -e "s:\.\.\/::g"`
        if [ ! "${_real_partition_name_}" ]; then
                echo "ERROR :: Failed to find sym link for partition $_partition_name_."
                return $_FAILED_
        fi
    _real_partition_name_="/dev/$_real_partition_name_"
        echo "Found sym link for partition $_partition_name_ , sym link = $_real_partition_name_."
        return  $_SUCCESS_
}
function Get_Partition_Name_BY_PATH
{
    _partition_name_=$1
    _real_partition_name_=""
    echo "Finding disk for partition : $_by_path_ using by-path information"

        _temp_partition_name_=`echo $_partition_name_ | sed 's:\/dev\/disk\/by-path\/::g'`

    _real_partition_name_=`sed -n  "/^[=]* BY-PATH Information - START [=]*/,/^[=]* BY-PATH Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | egrep -w "$_temp_partition_name_ "`

    _real_partition_name_=`echo  $_real_partition_name_ | awk -F"-> " '{ print $2 }'| sed -e "s:\.\.\/::g"`
        if [ ! "${_real_partition_name_}" ]; then
                echo "ERROR :: Failed to find sym link for partition $_partition_name_."
                return $_FAILED_
        fi
    _real_partition_name_="/dev/$_real_partition_name_"
        echo "Found sym link for partition $_partition_name_ , sym link = $_real_partition_name_."
        return  $_SUCCESS_
}

function Deref_SymLink
{
    _partition_name_=$1

    _tmp_disk_by_var=`echo $_partition_name_ | grep "by-id"`
    if [ $? -eq 0 ]; then
        Get_Partition_Name_BY_ID "$_partition_name_"

    else
        _tmp_disk_by_var=`echo $_partition_name_ | grep "by-label"`
        if [ $? -eq 0 ]; then
            Get_Partition_Name_BY_LABEL "$_partition_name_"
        else
            _tmp_disk_by_var=`echo $_partition_name_ | grep "by-path"`
            if [ $? -eq 0 ]; then
                Get_Partition_Name_BY_PATH "$_partition_name_"
            else
                _tmp_disk_by_var=`echo $_partition_name_ | grep "by-uuid"`
                if [ $? -eq 0 ]; then
                    Get_Partition_Name_BY_UUID "$_partition_name_"
                else
                    echo "partition sent is a real partition."
                    return $_FAILED_
                fi
            fi
        fi
    fi
    return $_SUCCESS_
}

#IT FINDS UUID. IT JUST MAKES BEST POSSIBLE COMBINATIONS TO FIND UUID FOR A DEVICE PASSED.
function Find_UUID
{
    _UUID_="" #CLEAR LAST ITERATION UUID
    _partition_name_=$1

    #FIRST CHECK IT"S UUID IN FSTAB -BLKID MAPPING TABLE
    Find_UUID_FSTABINFO "$_partition_name_"

    #IF NOT FOUND ABOVE,CHECK IN BLKID TABLE
    if [ -z "${_UUID_}" ]; then
        Find_UUID_BLKID "$_partition_name_"
    fi

    if [ -z "${_UUID_}" ]; then
        #FIRST WE HAVE TO DEREF SYMLINK
        Deref_SymLink "$_partition_name_"
        if [ 0 -eq $? ]; then
            echo "Found partition name : $_real_partition_name_"
            Find_UUID_FSTABINFO "$_real_partition_name_"
            if [ ! "${_UUID_}"  ]; then
                Find_UUID_BLKID "$_real_partition_name_"
            fi
        fi
    fi

    if [ -z "${_UUID_}" ]; then
        echo "ERROR :: Failed to find UUID for partition $_partition_name_."
        return $_FAILED_
    fi
    #WE NEED TO CHOP OFF QUOTES TO UUID.
    echo "UUID = $_UUID_."
    echo "Found UUID for partition $_partition_name_, UUID = $_UUID_."
    return $_SUCCESS_
}

#THIS FUNCTION IS USED TO DETERMINE SOURCE DISK NAME ON TARGET SIDE.
function GetTargetDiskName_old
{
    _disk_num_=$1
    _disk_name_=""
    echo "Generating disk name for disk at number $_disk_num_"
    #I need to check this Path.Whether it invokes or not.Else better it could be to pass the _VX_INSTALL_PATH_ as second input.
    if [ -d "$_FILES_PATH_/bin/" ]; then
        _disk_name_=`$_FILES_PATH_/bin/generate_device_name $_disk_num_`
    else
        _disk_name_=`$_FILES_PATH_/generate_device_name $_disk_num_`
    fi
    echo "Disk Name = $_disk_name_"
    if [ ! -z "${_disk_name_}" ]; then
        _disk_name_="/dev/$_disk_name_"
    fi
    return $_SUCCESS_
}

#THIS FUNCTION IS USED TO DETERMINE SOURCE DISK NAME ON TARGET SIDE.
function GetTargetDiskName
{
    _device_pos_=$1
    _DEVICE_NAME_=""

        while true; do
                if [ 0 -gt ${_device_pos_} ]; then
                        break
                fi

                _ascii_index_of_a_=$(printf "%d" \'a)
                if [ 26 -gt ${_device_pos_} ]; then
                        _ascii_index_of_dev_pos_=$((${_ascii_index_of_a_}+${_device_pos_}))
                        _DEVICE_NAME_=$(printf 'sd%s' $(printf "\x$(printf %x ${_ascii_index_of_dev_pos_})"))
                        break
                elif [ 702 -gt ${_device_pos_} ]; then
                        _position1_=$(( ${_ascii_index_of_a_} + ${_device_pos_}/26 -1 ))
                        _position2_=$(( ${_ascii_index_of_a_} + ${_device_pos_}%26 ))
                        echo ${_position1_} ${_position2_}
                        _DEVICE_NAME_=$(printf 'sd%s%s' $(printf "\x$(printf %x ${_position1_})") $(printf "\x$(printf %x ${_position2_})"))
                        break
                else
                        break
                fi
        done

    _disk_name_=${_DEVICE_NAME_}
        echo "Disk Name = $_disk_name_"
        if [ ! -z "${_disk_name_}" ]; then
                _disk_name_="/dev/$_disk_name_"
        fi
        return $_SUCCESS_
}

# MapDeviceHierarchyToBaseDevice takes two input arguments
# $1 - Inpout file
# $2 - Input device name
# Returns a device name
function MapDeviceHierarchyToBaseDevice
{
    _base_diskname_=""
    dev_hierarchy=$2
    if [ ! -f $1 ]; then
        echo "MapDeviceHierarchyToBaseDevice - input file $1 does not exists"
        return $_FAILED_;
    fi

    # Check if dev_hierarchy is a disk.
    grep "Parameter Name=\"DiskName\" Value=\"$dev_hierarchy\"" $1 > /dev/null 2>&1
    if [ $? -eq 0 ];then
            echo "$dev_hierarchy is a disk, and its found in $1."
            _base_diskname_=$dev_hierarchy
            return $_SUCCESS_
    fi

    # Check if the $dev_hierarchy exists in host-info, if not then return.
    grep "Parameter Name=\"Name\" Value=\"$dev_hierarchy\"" $1 > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Search device $dev_hierarchy not found in $1."
        return $_SUCCESS_
    fi

    # It is possible that multiple dev_hierarchy entries may exists and we can pickup first one.
    # With above check, there will be at-least one entry with dev_hierarchy in host-info.
    _search_device_line_number_=`grep -n "Parameter Name=\"Name\" Value=\"$dev_hierarchy\"" $1 | head -n 1 | awk -F":" '{print $1}'`

    # Change the IFS to make deli-meter as '\n' in for loop.
    OLDIF=$IFS
    IFS=$'\n'
    for _disk_entry in `grep -n "DiskName.*\/dev\/" $1 | sort -n`
    do
        # Get line number of the current disk entry.
        _disk_line_number_=`echo $_disk_entry | awk -F":" '{print $1}'`

        # Get the disk name from the current disk line entry.
        current_diskname=`echo $_disk_entry | awk -F'Value=' '{ print $2 }'`
        current_diskname=`echo $current_diskname | sed -e 's:["]*::g' | sed -e 's:/>::g' | sed -e 's:\r::g'`

        # Check if we crossed the required disk section in host-info.
        if [ $_search_device_line_number_ -lt $_disk_line_number_ ];then
            # We crossed the search device's disk line.
            # _base_diskname_ holds the base disk name.
            echo "Base device for $dev_hierarchy is $_base_diskname_."
            break
        fi

        # Store disk name in _base_diskname_ before going for next disk entry.
        _base_diskname_=$current_diskname
    done
    IFS=$OLDIFS # Restore the IFS

    return $_SUCCESS_
}

#FINDS DEVICE NAME WITH UUID INFORMATION FROM BLKID INFORMATION
# $1 - Input UUID
# Returns a device name
function Find_Device_From_UUID
{
    _dev_name_=""
    _dev_uuid_=$1

    _uuid_=`sed -n  "/^[=]* BLKID Information - START [=]*/,/^[=]* BLKID Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | grep "$_dev_uuid_"`
        if [ "${_uuid_}" ]; then
                _dev_name_=`echo $_uuid_ | awk -F':' '{print $1 }'`
        fi

    return $_SUCCESS_
}

#FINDS DEVICE NAME WITH LABEL INFORMATION FROM BLKID INFORMATION
# $1 - Input LABEL
# Returns a device name
function Find_Device_From_LABEL
{
    _dev_name_=""
    _dev_label=$1

    _label_=`sed -n  "/^[=]* BLKID Information - START [=]*/,/^[=]* BLKID Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | grep "$_dev_label"`
        if [ "${_uuid_}" ]; then
                _dev_name_=`echo $_label_ | awk -F':' '{print $1 }'`
        fi

    return $_SUCCESS_
}

# FIND IF A GIVEN DEVICE IS A LOGICAL VOLUME
# $1 - Input block device
# Returns 0 or 1
function IsDevice_LV
{
    _lv_dev_name_=""
    _is_dev_lv_=$1
    for _dev_in_dmsetup in `sed -n  "/^[=]* DMESETUP TABLE Information - START [=]*/,/^[=]* DMSETUP TABLE Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | awk -F':' '{ print $1 }' | grep -v "="`
    do
        is_multipath=`echo $_dev_in_dmsetup | awk '{ print $4 }'`
        if [ "${is_multipath}" = "multipath" ]; then
            continue
        fi
        _temp_vg=`echo $_dev_in_dmsetup | awk -F'-' '{ print $1 }'`
        if [ 0 -ne $? ]; then
            echo "Device \"${_lv_dev_name_}\" is not a logical volume."
            return $_FAILED_
        fi

        _temp_lv=`echo $_dev_in_dmsetup | awk -F'-' '{ print $2 }'`
        if [ 0 -ne $? ]; then
            echo "Device \"${_lv_dev_name_}\" is not a logical volume."
            return $_FAILED_
        fi

        _temp_dev_name1="/dev/$_temp_vg/$_temp_lv"
        _temp_dev_name2="/dev/mapper/$_temp_vg-$_temp_lv"

        if [ "${_temp_dev_name1}" = "${_is_dev_lv_}" ]; then
            _lv_dev_name_=$_temp_dev_name2
            break
        fi
        if [ "${_temp_dev_name2}" = "${_is_dev_lv_}" ]; then
            _lv_dev_name_=$_temp_dev_name1
            break
        fi
    done
    #Mean to say Device is not an LV.
    return $_FAILED_;
}

# FIND IF A GIVEN DEVICE IS A MULTIPATH DEVICE
# $1 - Input block device
# Returns 0 or 1
function IsDevice_Multipath
{
    _dev_name_=$1
    for _dev_in_dmsetup in `sed -n  "/^[=]* DMESETUP TABLE Information - START [=]*/,/^[=]* DMSETUP TABLE Information - END [=]*/p" $_INPUT_CONFIG_FILE_ | awk -F':' '{ print $1 }' | grep -v "="`
    do
        is_multipath=`echo $_dev_in_dmsetup | awk '{ print $4 }'`
        if [ "${is_multipath}" = "multipath" ]; then
            multipath_dev=`echo $_dev_in_dmsetup | awk '{ print $1 }'`
            # Form a multipath device - TODO other naming schemes
            multipath_dev="/dev/mapper/$multipath_dev"
            if [ "${multipath_dev}" = "${_dev_name_}" ]; then
                return $_SUCCESS_
            fi
        fi
    done
    return $_FAILED_;
}
# FIND IF A GIVEN DEVICE IS PROTECTED OR NOT
# $1 - Input block device
# Returns 0 or 1
function GetProtectedDeviceEntry
{
    _dev_name_=$1
    # Return 0 if the input device name is empty.
    if [ -z $_dev_name_ ]; then
        echo "Invalid input: Empty device name provided."
        return 0
    fi
    _protected_disklist="$_VX_FAILOVER_DATA_PATH_/${_PROTECTED_DISKS_LIST_FILE_NAME_}"
    entry=`cat $_protected_disklist | grep -wn "$_dev_name_" | awk -F":" '{print $1}'`
    echo "Entry = $entry"
    if [ -z "${entry}" ]; then
        return 0
    fi
    return $entry
}

function IsDiskProtected
{
    _dev_name_=$1
    echo "checking whether device \"$_dev_name_\" is protected or not."
    _protected_disklist_="$_VX_FAILOVER_DATA_PATH_/${_PROTECTED_DISKS_LIST_FILE_NAME_}"
    _isProtected_=`cat $_protected_disklist_ | grep "$_dev_name_"`
    echo "is protected = $_isProtected_"
    if [ -z "${_isProtected_}" ]; then
        return $_FAILED_
    fi
    return $_SUCCESS_
}

function COPY
{
    _source_=$1
    _target_=$2

    echo "Copying file \"$_source_\" to \"$_target_\"."
    /bin/cp -f $_source_ $_target_
    if [ 0 -ne $? ]; then
        return $?
    fi
    return $_SUCCESS_
}

function MOVE
{
    _source_=$1
    _target_=$2

    echo "Moving file \"$_source_\" to \"$_target_\"."
    /bin/mv -f $_source_ $_target_
    if [ 0 -ne $? ]; then
        return $?
    fi
    return $_SUCCESS_
}


#!/bin/bash

#DBG="set -x"

# TODO: Use Find_Device_From_UUID() from Linux_P2V_Lib.sh
get_original_dev()
{
	local uuid=$(blkid -o value -s UUID $1)
	local pat="FSTAB UUID Information"

	sed -n "/^[=]* $pat - START [=]*/,/^[=]* $pat - END [=]*/p" $2 |\
	grep $uuid | cut -f 1 -d ":" | sort | uniq
}

get_fs_opt()
{
	local pat="Filesystem table"

	sed -n "/^[=]* $pat - START [=]*/,/^[=]* $pat - END [=]*/p" $2 |\
	grep "$1\s" | awk '{print $4}'
}

is_root_mnt()
{
	local rdev=$(df $1 | sed -n 2p | cut -f 1 -d " ")

	rdev="$(readlink -f $rdev)"

	if [ "$rdev" = "`readlink -f $2`" ]; then
		echo "Root Mount"
		return 0
	else
		echo "Non Root Mount"
		return 1
	fi
}

replace_pat_in_file()
{
	$DBG

	local opat="$1"
	local npat="$2"
	local file="$3"

	echo "Convert $opat to $npat"

	if [ ! -f "$file" ]; then
		return 0
	fi

	sed -i "s/$opat/$npat/" "$file"
}

fstab_convert_label_to_uuid()
{
	$DBG
	local label=$(blkid -o value -s LABEL $1)
	local uuid=$(blkid -o value -s UUID $1)

	if [ "$label" = "" ]; then
		return
	fi

	# LABEL=label -> UUID=uuid
	replace_pat_in_file "LABEL=$label" "UUID=$uuid" "$2"
	# LABEL="label" -> UUID=uuid
	replace_pat_in_file "LABEL=\\\"$label\\\"" "UUID=$uuid" "$2"
}

fstab_convert_uuid_to_uuid()
{
	$DBG
	local uuid=$(blkid -o value -s UUID $1)

	replace_pat_in_file "UUID=\\\"$uuid\\\"" "UUID=$uuid" "$2"
}

fstab_convert_dev_to_uuid()
{
	$DBG
	local uuid=$(blkid -o value -s UUID $1)
	local odevlist="$(get_original_dev $1 $2)"
	local odev=""

	echo "Original Dev: $1 -> $odevlist"

	for odev in $odevlist; do
		odev="$(echo $odev | sed 's/\//\\\//g')"

		# /dev/name... -> UUID=uuid
		replace_pat_in_file "$odev" "UUID=$uuid" "$3"
	done
}

fstab_mount_all_uuid()
{
	$DBG
	local fstab=$1
	local uuid="$2"
	local mnt="$3"

	echo "Mount all FS with UUID = $uuid"

	cat $fstab | grep ^"UUID=$uuid" | tr -s " " | sort -k2,2 |\
	while read line; do
		submnt="$(echo $line | tr -s " " | cut -f 2 -d " ")"
		opt="$(echo $line | tr -s " " | cut -f 4 -d " ")"

		if [ "$submnt" = "/" ]; then
			echo "Skipping /"
			continue
		fi

		submnt="$(echo $submnt | sed "s/\/$//")"

		echo "mount -o $opt -U $uuid $mnt$submnt"
		mount -o $opt -U $uuid $mnt$submnt
	done
}

generic_mount_all()
{
	$DBG
	local rootmnt="$1"
	local dev="$2"

	local ofile="$rootmnt/etc/fstab"
	local mfile="/tmp/fstab.$$"
	local cfile="$rootmnt/etc/Configuration.info"

	local uuid=$(blkid -o value -s UUID $dev)

	cp $ofile $mfile || {
		echo "Cannot copy fstab"
		return 1
	}

	fstab_convert_uuid_to_uuid "$dev" "$mfile"
	fstab_convert_label_to_uuid "$dev" "$mfile"
	fstab_convert_dev_to_uuid "$dev" "$cfile" "$mfile"

	fstab_mount_all_uuid "$mfile" "$uuid" "$rootmnt"

	cat $mfile
	rm -f $mfile
}

btrfs_find_mount_root()
{
	local vid=""
	local vidlist=""
	local cfile="$1/etc/Configuration.info"

	echo "Trying to find root subvolume"

	# Check for subvolumes with "root" in name
	vidlist=$(btrfs subvolume list $1 | awk '{print $2}')

	for vid in $vidlist; do
		echo "Mounting $vid"
		umount -f $1
		mount -t btrfs -o subvolid=$vid $2 $1
		test -f $cfile && {
			echo "Root VID: $vid"
			break
		}
	done
}

btrfs_mount_all()
{
	local cfile="$1/etc/Configuration.info"

	test -f $cfile || btrfs_find_mount_root $1 $2

	# In case it is a root snapshot, find the root
	# device from Configuration.info
	is_root_mnt $1 $2 && {
		fsopt=$(get_fs_opt "/" "$cfile")

		echo "Remounting root: -o $fsopt"
		umount $1
		mount -t btrfs -o $fsopt $2 $1
	}

    #exec &> >(tee -a "$1/var/log/MountFS.log")
	generic_mount_all $@
	df
	return $?
}

echo "Mount subvolumes for $2: $1"

if [ "`dirname $0`" != "/tmp" -a -x "$1/MountFS.sh" ]; then
    echo "Executing local script"
    cp $1/MountFS.sh /tmp || {
    	echo "Cannot copy"
	    exit -1
    }
    /tmp/MountFS.sh $@
else
    fstype="$(blkid -o value -s TYPE $2)"
    if [ "$fstype" = "btrfs" ]; then
	    ${fstype}_mount_all "$1" "$2"
    	exit $?
    else
	    exit 0
    fi
fi


#!/bin/bash

case `uname` in
  Linux)
        if [ -f /etc/enterprise-release ] && [ -f /etc/redhat-release ] ; then
            if grep -q 'Enterprise Linux Enterprise Linux Server release 5.* (Carthage)' /etc/enterprise-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="OL5-32"
                else
                    OS="OL5-64"
                fi
            fi
        elif [ -f /etc/oracle-release ] && [ -f /etc/redhat-release ] ; then
            if grep -q 'Oracle Linux Server release 6.*' /etc/oracle-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="OL6-32"
                else
                    OS="OL6-64"
                fi
            fi
            if grep -q 'Oracle Linux Server release 7.*' /etc/oracle-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="OL7-32"
                else
                    OS="OL7-64"
                fi
            fi
        elif [ -f /etc/redhat-release ]; then
            if grep -q 'Red Hat Enterprise Linux [AE]S release 4 (Nahant Update [4-9])' /etc/redhat-release || \
                grep -q 'CentOS release 4.[4-9] (Final)' /etc/redhat-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="RHEL4-32"
                else
                    OS="RHEL4-64"
                fi
            elif grep -q 'Red Hat Enterprise Linux Server release 5.* (Tikanga)' /etc/redhat-release || \
                grep -q 'CentOS release 5.* (Final)' /etc/redhat-release; then
                if uname -a | grep -q i[0-9]86; then
                        OS="RHEL5-32"
                else
                        OS="RHEL5-64"
                fi
            elif grep -q 'Red Hat Enterprise Linux Server release 6.* (Santiago)' /etc/redhat-release || \
                grep -q 'CentOS Linux release 6.* (Final)' /etc/redhat-release || \
                grep -q 'CentOS release 6.* (Final)' /etc/redhat-release; then
                if uname -a | grep -q i[0-9]86; then
                    # Don't assign OS value if OS is RHEL6U3-32/CentOS6.3-32 as we don't support agent on it.
                    if grep -q 'Red Hat Enterprise Linux Server release 6.3 (Santiago)' /etc/redhat-release || \
                        grep -q 'CentOS Linux release 6.3 (Final)' /etc/redhat-release || \
                        grep -q 'CentOS release 6.3 (Final)' /etc/redhat-release; then
                        :
                    else
                        OS="RHEL6-32"
                    fi
                else
                    OS="RHEL6-64"
                    if grep -q 'CentOS release 6.4 (Final)' /etc/redhat-release || grep -q 'Red Hat Enterprise Linux Server release 6.4 (Santiago)' /etc/redhat-release ; then
                        if echo `uname -r`| grep -E '2.6.32-358|2.6.32-358.23.2' >/dev/null 2>&1 ; then
                            OS="RHEL6U4-64"
                        fi
                    fi
                fi
            elif grep -q 'XenServer release 4.1.0-7843p (xenenterprise)' /etc/redhat-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-4.1.0-7843p-32"
                else
                    OS="CITRIX-XEN-4.1.0-7843p-64"
                fi
            elif grep -q 'XenServer release 5.0.0-10918p (xenenterprise)' /etc/redhat-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-5.0.0-10918p-32"
                else
                    OS="CITRIX-XEN-5.0.0-10918p-64"
                fi
            elif grep -q 'XenServer release 4.0.1-4249p (xenenterprise)' /etc/redhat-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-4.0.1-4249p-32"
                else
                    OS="CITRIX-XEN-4.0.1-4249p-64"
                fi
            elif grep -q 'XenServer release 5.5.0-15119p (xenenterprise)' /etc/redhat-release ||
                grep -q 'XenServer SDK release 5.5.0-15119p (xenenterprise)' /etc/redhat-release ; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-5.5.0-15119p-32"
                else
                    OS="CITRIX-XEN-5.5.0-15119p-64"
                fi
            elif grep -q 'XenServer release 5.5.0-25727p (xenenterprise)' /etc/redhat-release ||
                grep -q 'XenServer SDK release 5.5.0-25727p (xenenterprise)' /etc/redhat-release ; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-5.5.0-25727p-32"
                else
                    OS="CITRIX-XEN-5.5.0-25727p-64"
                fi
            elif grep -q 'XenServer release 5.6.0-31188p (xenenterprise)' /etc/redhat-release ||
                grep -q 'XenServer SDK release 5.6.0-31188p (xenenterprise)' /etc/redhat-release ; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-5.6.0-31188p-32"
                else
                    OS="CITRIX-XEN-5.6.0-31188p-64"
                fi
            elif grep -q 'XenServer release 5.6.100-39265p (xenenterprise)' /etc/redhat-release ||
                grep -q 'XenServer SDK release 5.6.100-39265p (xenenterprise)' /etc/redhat-release ; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-5.6.100-39265p-32"
                fi
            elif grep -q 'XenServer release 5.6.100-47101p (xenenterprise)' /etc/redhat-release ||
                grep -q 'XenServer SDK release 5.6.100-47101p (xenenterprise)' /etc/redhat-release ; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-5.6.100-47101p-32"
                fi
            elif grep -q 'XenServer release 6.0.0-50762p (xenenterprise)' /etc/redhat-release ||
                grep -q 'XenServer SDK release 6.0.0-50762p (xenenterprise)' /etc/redhat-release ; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-6.0.0-50762p-32"
                fi
            elif grep -q 'XenServer release 6.0.2-53456p (xenenterprise)' /etc/redhat-release ||
                grep -q 'XenServer SDK release 6.0.2-53456p (xenenterprise)' /etc/redhat-release ; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-6.0.2-53456p-32"
                fi
            elif grep -q 'XenServer release 6.1.0-59235p (xenenterprise)' /etc/redhat-release ||
                grep -q 'XenServer SDK release 6.1.0-59235p (xenenterprise)' /etc/redhat-release ; then
                if uname -a | grep -q i[0-9]86; then
                    OS="CITRIX-XEN-6.1.0-59235p-32"
                fi
            fi
        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 10' /etc/SuSE-release && ! grep -q PATCHLEVEL /etc/SuSE-release ; then
            # This one is for SLES10 or OpenSuSE10 series
            if grep -q "SUSE LINUX 10.0" /etc/SuSE-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="OPENSUSE10-32"
                else
                    OS="OPENSUSE10-64"
                fi
            elif grep -q "SUSE Linux Enterprise Server 10" /etc/SuSE-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="SLES10-32"
                elif uname -a | grep -q x86_64; then
                    OS="SLES10-64"
                fi
            fi
        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 10' /etc/SuSE-release && grep -q "SUSE Linux Enterprise Server 10" /etc/SuSE-release && grep -q 'PATCHLEVEL = 1' /etc/SuSE-release; then
            if uname -a | grep -q i[0-9]86; then
                OS="SLES10-SP1-32"
            elif uname -a | grep -q x86_64; then
                OS="SLES10-SP1-64"
            fi

        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 10' /etc/SuSE-release && grep -q "SUSE Linux Enterprise Server 10" /etc/SuSE-release && grep -q 'PATCHLEVEL = 2' /etc/SuSE-release; then
            if uname -a | grep -q i[0-9]86; then
                OS="SLES10-SP2-32"
            elif uname -a | grep -q x86_64; then
                OS="SLES10-SP2-64"
            fi
        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 10' /etc/SuSE-release && grep -q "SUSE Linux Enterprise Server 10" /etc/SuSE-release && grep -q 'PATCHLEVEL = 3' /etc/SuSE-release; then
            if uname -a | grep -q i[0-9]86; then
                OS="SLES10-SP3-32"
            elif uname -a | grep -q x86_64; then
                OS="SLES10-SP3-64"
            fi
        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 10' /etc/SuSE-release && grep -q "SUSE Linux Enterprise Server 10" /etc/SuSE-release && grep -q 'PATCHLEVEL = 4' /etc/SuSE-release; then
            if uname -a | grep -q i[0-9]86; then
                    OS="SLES10-SP4-32"
            elif uname -a | grep -q x86_64; then
                    OS="SLES10-SP4-64"
            fi
        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 10' /etc/SuSE-release && \
            grep -q 'PATCHLEVEL = 1' /etc/SuSE-release ; then
            # This one is for SLES10 SP1 or OpenSuSE10 SP1 series
            if grep -q "SUSE LINUX 10.0" /etc/SuSE-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="OPENSUSE10-SP1-32"
                elif uname -a | grep -q x86_64; then
                    OS="OPENSUSE10-SP1-64"
                fi
            fi
        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 11' /etc/SuSE-release && grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release && grep -q 'PATCHLEVEL = 0' /etc/SuSE-release; then
            # This one is for SLES11 Base
            if grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="SLES11-32"
                elif uname -a | grep -q x86_64; then
                    OS="SLES11-64"
                fi
            fi
        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 11' /etc/SuSE-release && grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release && grep -q 'PATCHLEVEL = 1' /etc/SuSE-release; then
            # This one is for SLES11 Base with SP1
            if grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="SLES11-SP1-32"
                elif uname -a | grep -q x86_64; then
                    OS="SLES11-SP1-64"
                fi
            fi
        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 11' /etc/SuSE-release && grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release && grep -q 'PATCHLEVEL = 2' /etc/SuSE-release; then
            # This one is for SLES11 Base with SP2
            if grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="SLES11-SP2-32"
                elif uname -a | grep -q x86_64; then
                    OS="SLES11-SP2-64"
                fi
            fi
        elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 11' /etc/SuSE-release && grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release && grep -q 'PATCHLEVEL = 3' /etc/SuSE-release; then
            # This one is for SLES11 Base with SP2
            if grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="SLES11-SP3-32"
                elif uname -a | grep -q x86_64; then
                    OS="SLES11-SP3-64"
                fi
            fi
		elif [ -f /etc/SuSE-release ] && grep -q 'VERSION = 11' /etc/SuSE-release && grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release && grep -q 'PATCHLEVEL = 4' /etc/SuSE-release; then
            if grep -q "SUSE Linux Enterprise Server 11" /etc/SuSE-release; then
                if uname -a | grep -q i[0-9]86; then
                    OS="SLES11-SP4-32"
                elif uname -a | grep -q x86_64; then
                    OS="SLES11-SP4-64"
                fi
            fi
        elif [ -f /etc/SuSE-release ]; then
            # This one is for SLES9 with SP3
            if grep -q 'VERSION = 9' /etc/SuSE-release; then
                if grep -q 'PATCHLEVEL = 3' /etc/SuSE-release; then
                    if uname -a | grep -q i[0-9]86; then
                        OS="SLES9-SP3-32"
                    elif uname -a | grep -q x86_64; then
                        OS="SLES9-SP3-64"
                    fi
                fi
            fi
        elif [ -f /etc/lsb-release ] ; then
            if grep -q 'Ubuntu 8.04.2' /etc/lsb-release ||
                grep -q 'Ubuntu 8.04.3' /etc/lsb-release ; then
                if uname -a | grep -q x86_64; then
                    OS="UBUNTU8-64"
                fi
            elif grep -q 'Ubuntu 10.04.4 LTS' /etc/lsb-release ; then
                if uname -a | grep -q x86_64; then
                    OS="UBUNTU-10.04.4-64"
                fi
	    elif grep -q 'Ubuntu 12.04.4 LTS' /etc/lsb-release ; then
                if uname -a | grep -q x86_64; then
                    OS="UBUNTU-12.04.4-64"
                fi
            fi
        elif [ -f /etc/debian_version ]; then
            if grep -q '4.0' /etc/debian_version; then
                if uname -a | grep -q i[0-9]86; then
                    OS="DEBIAN-ETCH-32"
                elif uname -a | grep -q x86_64; then
                    OS="DEBIAN-ETCH-64"
                fi
            fi
            if grep -q '6.0.1' /etc/debian_version; then
                if uname -a | grep -q x86_64; then
                    OS="DEBIAN6U1-64"
                fi
            fi
        fi
    ;;

 HP-UX)
        # Itanium is identified by 'ia64' in the output of 'uname -a'
        if [ `uname -r` = "B.11.11" ] && [ `uname -m` != "ia64" ]; then
            OS="HP-UX-PA-RISC"
        elif [ `uname -r` = "B.11.23" ] && [ `uname -m` = "ia64" ]; then
            OS="HP-UX-Itanium-11iv2"
        elif [ `uname -r` = "B.11.31" ] && [ `uname -m` = "ia64" ]; then
            OS="HP-UX-Itanium-11iv3"
        fi
    ;;

 AIX)
        case `uname -v` in
            5)
                if [ `uname -r` = "3" ]; then
                    case `uname -p` in
                        powerpc*)
                            if [ `getconf KERNEL_BITMODE` = "64" ]; then
                                OS="AIX53"
                            fi
                        ;;
                    esac
                fi
            ;;
            6)
                if [ `uname -r` = "1" ] ; then
                    case `uname -p` in
                        powerpc*)
                            if [ `getconf KERNEL_BITMODE` = "64" ]; then
                                OS="AIX61"
                            fi
                        ;;
                    esac
                fi
            ;;
            7)
                if [ `uname -r` = "1" ] ; then
                    case `uname -p` in
                        powerpc*)
                            if [ `getconf KERNEL_BITMODE` = "64" ]; then
                                    OS="AIX71"
                            fi
                        ;;
                    esac
                fi
            ;;
        esac
    ;;

 SunOS)
        if [ `uname -r` = "5.8" ]; then
            case `uname -p` in
                sparc*)
                    if [ `isainfo -b` = "64" ]; then
                        OS="Solaris-5-8-Sparc"
                    fi
                ;;
            esac
        elif [ `uname -r` = "5.9" ]; then
            case `uname -p` in
                sparc*)
                    if [ `isainfo -b` = "64" ]; then
                        OS="Solaris-5-9-Sparc"
                    fi
                ;;
            esac
        elif [ `uname -r` = "5.10" ]; then
            case `uname -p` in
                i386|x86*)
                    if [ `isainfo -b` = "64" ]; then
                        OS="Solaris-5-10-x86-64"
                    fi
                ;;
                sparc*)
                    if [ `isainfo -b` = "64" ]; then
                        OS="Solaris-5-10-Sparc"
                    fi
                ;;
            esac
        elif [ `uname -r` = "5.11" ] && grep 'Oracle Solaris' /etc/release >/dev/null ; then
            case `uname -p` in
                i386|x86*)
                    if [ `isainfo -b` = "64" ]; then
                        OS="Solaris-5-11-x86-64"
                    fi
                ;;
                sparc*)
                    if [ `isainfo -b` = "64" ]; then
                        OS="Solaris-5-11-Sparc"
                    fi
                ;;
            esac
        elif [ `uname -r` = "5.11" ] && grep 'OpenIndiana Development oi_151.1.7 X86' /etc/release >/dev/null ; then
            case `uname -p` in
                i386|x86*)
                    if [ `isainfo -b` = "64" ]; then
                        OS="OpenIndiana-151a7-x86-64"
                    fi
                ;;
            esac
        elif [ `uname -r` = "5.11" ] && grep 'OpenSolaris' /etc/release >/dev/null ; then
            case `uname -p` in
                i386|x86*)
                    if [ `isainfo -b` = "64" ]; then
                        OS="OpenSolaris-5-11-x86-64"
                    fi
                ;;
                sparc*)
                    if [ `isainfo -b` = "64" ]; then
                        OS="OpenSolaris-5-11-Sparc"
                    fi
                ;;
            esac
        fi
    ;;
esac

if [ $# -gt 0 ]
then
    echo $OS
fi


#!/bin/bash
#
# Usage: OS_details_target.sh <root-path> [--formated-output]
#
# Formated-output will display below details:
# os=<detected-os-string>
# release-file=<release-string-file-path>
#
# Default output will display only detected os string.
# <os>
#

# 1st argument: root path.
mntpath=$1

# 2nd argument: formated-output
formated_output="no"
os_release_file=""
if [ "$2" = "--formated-output" ]; then
	formated_output="yes"
fi

if [ -f $mntpath/etc/enterprise-release ] && [ -f $mntpath/etc/redhat-release ] ; then
	if grep -q 'Enterprise Linux Enterprise Linux Server release 5.*' $mntpath/etc/enterprise-release; then
		OS="OL5-64"
	fi
	os_release_file=$mntpath/etc/enterprise-release
elif [ -f $mntpath/etc/oracle-release ] && [ -f $mntpath/etc/redhat-release ] ; then
	if grep -q 'Oracle Linux Server release 6.*' $mntpath/etc/oracle-release ; then
		OS="OL6-64"
	elif grep -q 'Oracle Linux Server release 7.*' $mntpath/etc/oracle-release ; then
		OS="OL7-64"
	elif grep -q 'Oracle Linux Server release 8.*' $mntpath/etc/oracle-release ; then
		OS="OL8-64"
    elif grep -q 'Oracle Linux Server release 9.*' $mntpath/etc/oracle-release ; then
        OS="OL9-64"
	fi
	os_release_file=$mntpath/etc/oracle-release
elif [ -f $mntpath/etc/redhat-release ]; then
	if grep -q 'Red Hat Enterprise Linux Server release 5.*' $mntpath/etc/redhat-release || \
	   grep -q 'CentOS release 5.*' $mntpath/etc/redhat-release; then
			OS="RHEL5-64"
	elif grep -q 'Red Hat Enterprise Linux Server release 6.*' $mntpath/etc/redhat-release || \
		 grep -q 'Red Hat Enterprise Linux Workstation release 6.*' $mntpath/etc/redhat-release || \
		 grep -q 'CentOS Linux release 6.*' $mntpath/etc/redhat-release || \
		 grep -q 'CentOS release 6.*' $mntpath/etc/redhat-release; then
			OS="RHEL6-64"
	elif grep -q 'Red Hat Enterprise Linux Server release 7.*' $mntpath/etc/redhat-release ||
		 grep -q 'Red Hat Enterprise Linux Workstation release 7.*' $mntpath/etc/redhat-release; then
			OS="RHEL7-64"
	elif grep -q 'CentOS Linux release 7.*' $mntpath/etc/redhat-release; then
			OS="CENTOS7-64"
	elif grep -q 'Red Hat Enterprise Linux release 8.*' $mntpath/etc/redhat-release; then
			OS="RHEL8-64"
	elif grep -q 'Red Hat Enterprise Linux release 9.*' $mntpath/etc/redhat-release; then
            OS="RHEL9-64"
	elif grep -q 'CentOS Linux release 8.*' $mntpath/etc/redhat-release ||
         grep -q 'CentOS Stream release 8.*' $mntpath/etc/redhat-release; then
           OS="CENTOS8-64"
	elif grep -q 'CentOS Linux release 9.*' $mntpath/etc/redhat-release ||
         grep -q 'CentOS Stream release 9.*' $mntpath/etc/redhat-release; then
           OS="CENTOS9-64"
	fi
	os_release_file=$mntpath/etc/redhat-release
elif ( [ -f $mntpath/etc/SuSE-release ] && ( grep -q 'VERSION = 11' $mntpath/etc/SuSE-release || grep -q 'VERSION = 12' $mntpath/etc/SuSE-release ) ) ; then
	if grep -q 'VERSION = 11' $mntpath/etc/SuSE-release && grep -q 'PATCHLEVEL = 3' $mntpath/etc/SuSE-release; then
		OS="SLES11-SP3-64"
	elif grep -q 'VERSION = 11' $mntpath/etc/SuSE-release && grep -q 'PATCHLEVEL = 4' $mntpath/etc/SuSE-release; then
		OS="SLES11-SP4-64"
	fi
	if grep -q 'VERSION = 12' $mntpath/etc/SuSE-release; then
		OS="SLES12-64"
	fi
	os_release_file=$mntpath/etc/SuSE-release
elif [ -f $mntpath/etc/os-release ] && grep -q 'SLES' $mntpath/etc/os-release; then
	if grep -q 'VERSION="12' $mntpath/etc/os-release; then
		OS="SLES12-64"
	elif grep -q 'VERSION="15' $mntpath/etc/os-release; then
		OS="SLES15-64"
	fi
	os_release_file=$mntpath/etc/os-release
elif [ -f $mntpath/etc/lsb-release ]; then
	if grep -q 'Ubuntu 12.*' $mntpath/etc/lsb-release ; then
		OS="UBUNTU12-64"
	elif grep -q 'Ubuntu 14.*' $mntpath/etc/lsb-release ; then
		OS="UBUNTU14-64"
	elif grep -q 'Ubuntu 16.*' $mntpath/etc/lsb-release ; then
		OS="UBUNTU16-64"
	elif grep -q 'Ubuntu 18.*' $mntpath/etc/lsb-release ; then
		OS="UBUNTU18-64"
	elif grep -q 'Ubuntu 19.*' $mntpath/etc/lsb-release ; then
		OS="UBUNTU19-64"
	elif grep -q 'Ubuntu 20.*' $mntpath/etc/lsb-release ; then
		OS="UBUNTU20-64"
	elif grep -q 'Ubuntu 21.*' $mntpath/etc/lsb-release ; then
		OS="UBUNTU21-64"
	elif grep -q 'Ubuntu 22.*' $mntpath/etc/lsb-release ; then
		OS="UBUNTU22-64"
	fi
	os_release_file=$mntpath/etc/lsb-release
elif [ -f $mntpath/etc/debian_version ]; then
	if grep -q '^7.*' $mntpath/etc/debian_version; then
        OS="DEBIAN7-64"
    elif grep -q '^8.*' $mntpath/etc/debian_version; then
        OS="DEBIAN8-64"
    elif grep -q '^9.*' $mntpath/etc/debian_version; then
        OS="DEBIAN9-64"
    elif grep -q '^10.*' $mntpath/etc/debian_version; then
        OS="DEBIAN10-64"
    elif grep -q '^11.*' $mntpath/etc/debian_version; then
        OS="DEBIAN11-64"
	elif grep -q '^kali-rolling*' $mntpath/etc/debian_version; then
	    OS="KALI-ROLLING-64"
    fi
	os_release_file=$mntpath/etc/debian_version
fi

if [ $# -gt 0 ]
then
	if [ "$formated_output" = "yes" ]; then
		echo "os=$OS"
		echo "release-file=$os_release_file"
	else
	echo $OS
	fi
fi