Score:1

How do I get an array of currently attached (but not formatted/mounted) hard drives sorted by size (but without the sizing data in the array)

es flag

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.

Score:1
hr flag

I don't really understand what you are trying to do (or why), but you could consider using lsblk with its --json output format specifier, piping the result to jq where you can implement custom filtering / sorting.

For example:

$ lsblk -b --json | jq -r '
    .blockdevices[] | select(.name == "sda") |
    [ .children[] | select(.mountpoint == null and .name != "sda4") ] | sort_by(.size) | .[].name
'
sda1
sda2
Score:0
cn flag

I think you want systemd.mount for this. Adding x-systemd.requires-mounts-for= to your fstab entry creates a transient mount unit:

# fstab(5):
/dev/loop1 /opt/mnt      ext4 noauto 0 0  
/dev/loop2 /opt/mnt/data ext4 noauto,x-systemd.requires-mounts-for=/opt/mnt 0 0

which you can start and stop as a systemd.unit:

systemctl start opt-mnt-data.mount

this will mount the filesystem in a certain order.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.