I wanted a way to script the mounting of hard drives to a specific mount point. After some browsing, I found: https://gist.github.com/trentmswanson/9c22bb71182e982bd36f
This was for MS Azure and did not cover specific hard drives types in the AWS Infrastructure. It used sdf in needed nvm. I made those changes and the result worked. I have an array that holds the mount points:
# Custom Mount Points for first 2 HDDs
typeset -A MountPoint
MountPoint[1]="/FileMakerData"
MountPoint[2]="/FileMakerData/Backups"
and a line that tries to read "this" HDD size and mount it appropriately:
# Tests the size of volume to test for the proper disk size
DiskSize=$(sudo fdisk -s ${DISK})
# Calculates the number of GB and resets the variable as that number
let DiskSize=DiskSize/1048576
echo "$DiskSize"
# Uses the disk size to determine disk type.
# Assumes that the Backup drive will never be less than 50 GB and Data will never start GT 50 GB
if [ "$DiskSize" -lt 50 ];
then DiskType="Data"
elif ["$DiskSize" -ge 50 ];
then DiskType="Backup"
fi
A function then gets the mount point from the array based on the value.
get_next_mountpoint() {
DeviceType=${1}
if ( test "$DeviceType" == "Data" ); then echo "${MountPoint[1]}"
elif ( test "$DeviceType" == "Backup" ); then echo "${MountPoint[2]}"
else
# Echos blank so the script will know to exit loop
echo ""
fi
}
There is a lot more to the script. The issue with this process, though, is that the HDD to be mounted to /FileMakerData must be created/mounted first.
When I mount /FileMakerData/Backups then mount /FileMakerData I get a single directory in /FileMakerData that is labeled "lost+found". It makes sense that this happens since I am suddenly changing the middle of the directory structure. But, I cannot figure out a way to list the Hard Drives by increasing size so that when the script gets an arbitrary order from the folders (Because AWS mounted them backward) I can still mount the correct size drive to the correct folder in the correct order.
My current code is:
# An set of disks to ignore from partitioning and formatting; Use pipe to seperate (may act as OR in this case)
BLACKLIST="/dev/nvme0"
# Gets a list of disks filters out the listed ones then searches for others
DEVS=($(ls -1 /dev/nvme*|egrep -v "${BLACKLIST}"|egrep "[0-9]n[0-9]$"))
I found: https://linuxhint.com/list_disks_ubuntu/
But it does not tell me how to get them in a simple array and I cannot also sort by size with this command.
I tried:
ls -1 /dev/nvme*|egrep -v "${BLACKLIST}"|egrep "[0-9]n[0-9]$" | sort
and
ls -1 /dev/nvme*|egrep -v "${BLACKLIST}"|egrep "[0-9]n[0-9]$" | sort -r
but (as expected) this does not sort on size. It sorts on the HDD name that is passed:
/dev/nvme1n1
/dev/nvme2n1
and
/dev/nvme2n1
/dev/nvme1n1
Respectively. That is exactly what I want, just sorted by size, not name.
Other googling has not turned up much useful help for extracting the size and sorting the main value by size. I am running an Ubuntu 18.04 minimal install as this is a web server. I have access to df and fdisk and I am trying to install many more packages but will consider it if it should prove to be significantly easier.