How to get the number of physical disks in Linux?

What is the command line command to get the number of physical disks in Linux Server and how much space is used on each?


Solution 1:

sudo fdisk -l will list your disks and a bunch of stats about them, including the partitions. The disks are generally in the form of /dev/sdx and partitions /dev/sdxn, where x is a letter and n is a number (so sda is the first physical disk and sda1 is the first partition on that disk).

sudo df -h gives you the size and usage stats per partition. Drop the -h and you get usage in blocks, with it it's human readable.

I put the sudos in there because I got no output from fdisk and only partial output from df when I ran the commands as a regular user, I suppose because the commands read from somewhere off limits to non-admins.

Solution 2:

If you really want to display only the hardware, and not RAID volumes and partitions that might be seen by the OS as physical drives. You might want to try lshw

lshw -class disk -short
H/W path        Device      Class       Description
===================================================
/0/1/0.0.0      /dev/cdrom  disk        DVD-RAM GSA-H55N
/0/1/0.1.0      /dev/sda    disk        160GB ST3160021A
/0/2/0.0.0      /dev/sdb    disk        160GB ST3160815AS

Or a bit much verbose

lshw -class disk
  *-cdrom                 
   description: DVD-RAM writer
   product: DVD-RAM GSA-H55N
   vendor: HL-DT-ST
   physical id: 0.0.0
   bus info: scsi@0:0.0.0
   logical name: /dev/cdrom
   logical name: /dev/sr0
   version: 1.04
   serial: [
   capabilities: removable audio cd-r cd-rw dvd dvd-r dvd-ram
   configuration: ansiversion=5 status=nodisc
  *-disk
   description: ATA Disk
   product: ST3160021A
   vendor: Seagate
   physical id: 0.1.0
   bus info: scsi@0:0.1.0
   logical name: /dev/sda
   version: 8.01
   serial: 5JS97CFY
   size: 149GiB (160GB)
   capabilities: partitioned partitioned:dos
   configuration: ansiversion=5 sectorsize=512 signature=000f3a2f
  *-disk
   description: ATA Disk
   product: ST3160815AS
   vendor: Seagate
   physical id: 0.0.0
   bus info: scsi@2:0.0.0
   logical name: /dev/sdb
   version: 3.AA
   serial: 9RX7AK36
   size: 149GiB (160GB)
   capabilities: partitioned partitioned:dos
   configuration: ansiversion=5 sectorsize=512 signature=000b6d91

Solution 3:

I think the easiest way (at least concerning parsing effort) on a recent Linux installation would be

$ lsblk -S

which outputs something like this:

tremendous:~# lsblk -S
NAME HCTL       TYPE VENDOR   MODEL             REV TRAN
sda  0:0:0:0    disk ATA      WDC WD5000AUDX-6 01.0 sata
sdb  1:0:0:0    disk ATA      WDC WD5000AUDX-6 01.0 sata
tremendous:~# 

Solution 4:

Depending on your distribution (in this case Centos 7) lsblk -d will show you (for example) three physical disks.

NAME MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0   11:0    1  1024M  0 rom  
sr1   11:1    1  1024M  0 rom  
sdf    8:80   0 372.6G  0 disk 
sde    8:64   0 372.6G  0 disk 
sdg    8:96   0   1.8T  0 disk 

iostat will also show similar results (ignore the dm devices as they are part of LVM)

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sdf               1.85        41.74        53.80   68524791   88335482
sde               0.00         0.01         0.00      20219          0
dm-0              7.75        40.17        52.24   65945186   85767784
dm-1              0.31         0.94         1.56    1543416    2567312
sdg               0.86         1.89       171.04    3096240  280813864

Solution 5:

You'd think there'd be a simple answer to this, but it actually depends on what you mean by "physical disk". iSCSI volumes and RAID devices (for example) appear as physical disks, but the tools one uses to examine these differ.

If you just mean a plain SATA or SCSI drive attached directly to a host controller on the motherboard of the server, you're looking for actively used devices located at /dev/sdN, so you can grep through your dmesg for them (dmesg | grep sd) or you could look in /dev/disk/by-id or yet again you can look in /proc/diskstats.

Once you've identified the /dev/ entry for devices that are present, you can use the appropriate tool to check for free space. This again depends on other info, such as how they were partitioned, whether they use the lvm, and so on.