How to list the physical disk on Linux machine?

Q. How to list the physical disks present in a linux machine? List the file names like /dev/sdX of all the physical disk.

Ans:
There are many commands to do that.

A. lsblk:
 $ sudo lsblk -d | grep disk
 fd0 2:0 1 4K 0 disk
 sda 8:0 0 128G 0 disk
 sdb 8:16 0 5G 0 disk
 sdc 8:32 0 50G 0 disk

This will list all the physical disk. It also includes the md / lvm devices you need to filter them out. Change the command to get the physical disk list like below:

$ sudo lsblk -d | grep disk | grep “^sd” | tr -s ‘ ‘ | cut -d ‘ ‘ -f 1
sda
sdb
sdc

B. blkid:

$ sudo blkid
 /dev/sr0: LABEL="VMGUEST" TYPE="iso9660"
 /dev/sda1: UUID="202b983a-02c4-4ee0-9a18-3d3b38f7bc7a" TYPE="swap"
 /dev/sda2: UUID="d7441b33-1aa1-459c-af17-6459b0d18a19" TYPE="ext4"
 /dev/sda3: UUID="611a5df3-16bc-4240-97bd-c8bbdd8ebf2c" TYPE="ext4"
 /dev/sdb1: UUID="6ed027ff-04ff-42a7-afad-08726ada10f9" TYPE="ext4"
 /dev/sdc1: UUID="9a1c60cf-7790-4ed9-9da3-d7c1a9471d78" TYPE="ext4"
 /dev/sdc2: UUID="z32bc1-xzAH-yUvM-xVJp-3hLe-QAj2-Zxp6uN" TYPE="LVM2_member"
 /dev/sdc3: UUID="NWBkAy-Nxtw-OKkq-wdxM-N6db-VzOP-t76PLT" TYPE="LVM2_member"
 /dev/sdc5: UUID="aC1WB9-5p0Y-EsPD-yPBD-m1x0-Q0qW-kuZxXw" TYPE="LVM2_member"
 /dev/sdc6: UUID="bO8SNW-YOWd-6qke-yZIf-5ekT-FbQJ-W2OFnC" TYPE="LVM2_member"

We can modify this command to list physical disk like below:

 $ sudo blkid | grep "^/dev/sd" | cut -d ':' -f 1 | sed s'/[0-9]//g' | sort | uniq
 /dev/sda
 /dev/sdb
 /dev/sdc

C. fdisk

 $ sudo fdisk -l
Disk /dev/sda: 137.4 GB, 137438953472 bytes
255 heads, 63 sectors/track, 16709 cylinders, total 268435456 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003a379
Device Boot Start End Blocks Id System
/dev/sda1 2048 62500863 31249408 82 Linux swap / Solaris
/dev/sda2 * 62500864 64454655 976896 83 Linux
/dev/sda3 64454656 268433407 101989376 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
181 heads, 40 sectors/track, 1448 cylinders, total 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000497c4
Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 10485759 5241856 83 Linux
Disk /dev/sdc: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000dc579
Device Boot Start End Blocks Id System
/dev/sdc1 * 2048 12290047 6144000 83 Linux
/dev/sdc2 12290048 32770047 10240000 8e Linux LVM
/dev/sdc3 32770048 53250047 10240000 8e Linux LVM
/dev/sdc4 53250048 104857599 25803776 5 Extended
/dev/sdc5 53254144 73734143 10240000 8e Linux LVM
/dev/sdc6 73736192 94216191 10240000 8e Linux LVM
/dev/sdc7 94218240 104458239 5120000 83 Linux

You can change this command to get the physical disk like below:

 $ sudo fdisk -l | grep "^Disk /dev/sd" | cut -d ' ' -f 2 | sed 's/://'
 /dev/sda
 /dev/sdb
 /dev/sdc

Windows how to get type (IDE/ATA/SATA/SCSI/USB/Virtual) of physical disk?

Q. On Windows server given the physical disk \\.\PHYSICALDRIVEn identify whether the disk is ATA, SATA. SCSI disk, USB disk or it is a mounted VHD file.

Ans:
There is no straightforward way in C++ to do this. Windows provide SetupDiEnumDeviceInterfaces APIs to get the properties of devices. But I found it bit complex to solve the problem mentioned.

On the other hand diskpart.exe provides simple interface to identify the type of the disk. Use following commands to get the type of disk:

C:\Users\Administrator>diskpart
Microsoft DiskPart version 6.1.7601
 Copyright (C) 1999-2008 Microsoft Corporation.
 On computer: SYSTEMS11
DISKPART> list disk
  Disk ###  Status         Size     Free     Dyn  Gpt
   --------  -------------  -------  -------  ---  ---
   Disk 0    Online          465 GB  1024 KB
   Disk 1    Online          100 GB  2048 KB
   Disk 2    Online         7633 MB      0 B
DISKPART> select disk 0
Disk 0 is now the selected disk.
DISKPART> detail disk
WDC WD5000AAKX-001CA0 ATA Device
 Disk ID: 84BF18DB
 Type   : SATA
 Status : Online
 Path   : 0
 Target : 0
 LUN ID : 0
 Location Path : PCIROOT(0)#PCI(1F02)#ATA(C00T00L00)
 Current Read-only State : No
 Read-only  : No
 Boot Disk  : Yes
 Pagefile Disk  : Yes
 Hibernation File Disk  : No
 Crashdump Disk  : Yes
 Clustered Disk  : No
  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
   ----------  ---  -----------  -----  ----------  -------  ---------  --------
   Volume 0                      NTFS   Partition    100 MB  Healthy    System
   Volume 1     C                NTFS   Partition    165 GB  Healthy    Boot
   Volume 2     S   VM           NTFS   Partition    299 GB  Healthy
DISKPART>

In the information provided by “detail disk” check the entry:
Type   : SATA 

If the physical disk is ATA drive the type entry in “detail disk” appear as:
Type   : ATA

If the physical disk is SCSI drive the type entry in “detail disk” appear as:
Type   : SCSI

If the physical disk is USB drive the type entry in “detail disk” appear as:
Type   : USB

If the physical disk is mounted VHD/VHDX file the type entry in “detail disk” appear as:
Type   : Virtual