Obtain list of all physical hard drives (SSD, NVMe, VDA, HDD)

I came across an issue recently whereby I had a need to display all physical hard drives on a system whether it be HDD, VHD, or SSD (NVMe) drives that were above a certain size, excluding any USB or CD-ROM/DVD drives.

In the following example I needed to obtain the first drive that was larger than 50GB

#!/bin/bash
DIR=/sys/block
MINSIZE=50                  #Min size of drive in GB to look for
declare drive_type
drive_type=( "nvme" "sd" "hd" "vd" )
for t in ${drive_type[@]}; do
        if [ $(ls $DIR/|grep -q "^$t";echo $?) == 0 ]; then
                for d in $DIR/${t}*; do
                  DEV=`basename "$d"`
                  if [[ (  -d $DIR/$DEV  ||  -L $DIR/$DEV )  &&   ( $(cat $DIR/$DEV/removable) == 0 ) ]]; then
                    if [ -f $DIR/$DEV/size ]; then
                        GB=$((`cat $DIR/$DEV/size`/2**21))
                        if [ $GB -gt $MINSIZE ]; then
                                echo "Disk device $DEV has size $GB GB"
                                break 2
                        fi
                    fi
                  fi
                done
        fi
done

Example

$ bash drivesize.sh
Disk device nvme0n1 has size 1788 GB

Another variation is if I wanted to list all physical drives and their sizes I can modify the above to simplify:

#!/bin/bash
DIR=/sys/block
declare drive_type
drive_type=( "nvme" "sd" "hd" "vd" )
for t in ${drive_type[@]}; do
        if [ $(ls $DIR/|grep -q "^$t";echo $?) == 0 ]; then
                for d in $DIR/${t}*; do
                  DEV=`basename "$d"`
                  if [[ (  -d $DIR/$DEV  ||  -L $DIR/$DEV )  &&   ( $(cat $DIR/$DEV/removable) == 0 ) ]]; then
                    if [ -f $DIR/$DEV/size ]; then
                        GB=$((`cat $DIR/$DEV/size`/2**21))
                        echo "Disk device $DEV has size $GB GB"
                    fi
                  fi
                done
        fi
done

Example

$ bash drivesize_all.sh
Disk device nvme0n1 has size 1788 GB
Disk device nvme10n1 has size 1788 GB
Disk device nvme11n1 has size 1788 GB
Disk device nvme12n1 has size 1788 GB
Disk device nvme13n1 has size 1788 GB
Disk device nvme14n1 has size 1788 GB
Disk device nvme15n1 has size 1788 GB
Disk device nvme16n1 has size 1788 GB
Disk device nvme17n1 has size 1788 GB
Disk device nvme18n1 has size 1788 GB
Disk device nvme19n1 has size 1788 GB
Disk device nvme1n1 has size 1788 GB
Disk device nvme20n1 has size 1788 GB
Disk device nvme21n1 has size 1788 GB
Disk device nvme22n1 has size 1788 GB
Disk device nvme23n1 has size 1788 GB
Disk device nvme2n1 has size 1788 GB
Disk device nvme3n1 has size 1788 GB
Disk device nvme4n1 has size 1788 GB
Disk device nvme5n1 has size 1788 GB
Disk device nvme6n1 has size 1788 GB
Disk device nvme7n1 has size 1788 GB
Disk device nvme8n1 has size 1788 GB
Disk device nvme9n1 has size 1788 GB
Disk device sda has size 894 GB
Disk device sdb has size 894 GB

This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *