#!/bin/bash
# Determine disk device name
# Currently we can only support 1 disk.
# Disk with label 'config-2' or of type USB will
# be skipped.

MINSIZE=80000  # 80GB (1GB=1000MB)

# find any activation engine drive
AEDISK=$(blkid -L config-2)
DISKNUM=${1:-1}
count=1

# If no arguments are specified, or multipathd is in our path
# Then echo the wwid of the first map
if [ -z "$1" ] && hash multipathd 2>/dev/null; then
    mpathdev=$(multipathd show maps raw format %n | head -n 1)
    if [ -b "/dev/mapper/$mpathdev" ]; then
        echo "mapper/$mpathdev"
        exit 0
    fi
fi
while read -r dev sz type tran; do
    # Only accept devices that are the disk type
    if [ "$type" == "disk" ]; then
        # Only accept disks that are not attached to usb
        # or an AEDisk
        if [[ "$tran" != "usb" && "$AEDISK" != "/dev/${dev}*" ]]; then
            # Check disk size
            sz=$((sz/1000/1000)) # convert to MB
            if [ "$sz" -gt "$MINSIZE" ]; then
                if [ "$count" = "$DISKNUM" ]; then
                    echo "$dev"
                    exit 0
                fi
                ((count++))
            fi
        fi
    fi

done < <(lsblk -r -n -d -b -o name,size,type,tran)
exit 1
