Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list physical disks?

How to list physical disks in Windows? In order to obtain a list of "\\\\.\PhysicalDrive0" available.

like image 290
CiNN Avatar asked Nov 29 '08 17:11

CiNN


People also ask

How do I list physical disks in Linux?

List Disks on Linux using lsblk. The easiest way to list disks on Linux is to use the “lsblk” command with no options. The “type” column will mention the “disk” as well as optional partitions and LVM available on it. Optionally, you can use the “-f” option for “filesystems“.

How do I list a disk drive?

Right-click on "Command Prompt" and choose "Run as Administrator". At the prompt, type "diskpart" and hit Enter. At the diskpart prompt type "list disk". This will list all the hard drives in the system.

How do I find the number of physical disks in Windows?

You can enumerate the drive letters with (or without) GetLogicalDrives, then call QueryDosDevice() to find out which physical drive the letter is mapped to. Alternatively, you can decode the information in the registry at HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices.


2 Answers

#WMIC wmic is a very complete tool

wmic diskdrive list 

provide a (too much) detailed list, for instance

for less info

wmic diskdrive list brief  

#C Sebastian Godelet mentions in the comments:

In C:

system("wmic diskdrive list"); 

As commented, you can also call the WinAPI, but... as shown in "How to obtain data from WMI using a C Application?", this is quite complex (and generally done with C++, not C).


#PowerShell Or with PowerShell:

Get-WmiObject Win32_DiskDrive 

Update Feb. 2022, Microsoft announces in "Windows 10 features we're no longer developing"

The WMIC tool is deprecated in Windows 10, version 21H1 and the 21H1 General Availability Channel release of Windows Server.

This tool is superseded by Windows PowerShell for WMI.

Note: This deprecation only applies to the command-line management tool. WMI itself is not affected.

like image 151
9 revs, 4 users 67% Avatar answered Sep 19 '22 11:09

9 revs, 4 users 67%


One way to do it:

  1. Enumerate logical drives using GetLogicalDrives

  2. For each logical drive, open a file named "\\.\X:" (without the quotes) where X is the logical drive letter.

  3. Call DeviceIoControl passing the handle to the file opened in the previous step, and the dwIoControlCode parameter set to IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS:

    HANDLE hHandle; VOLUME_DISK_EXTENTS diskExtents; DWORD dwSize; [...]  iRes = DeviceIoControl(     hHandle,     IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,     NULL,     0,     (LPVOID) &diskExtents,     (DWORD) sizeof(diskExtents),     (LPDWORD) &dwSize,     NULL); 

This returns information of the physical location of a logical volume, as a VOLUME_DISK_EXTENTS structure.

In the simple case where the volume resides on a single physical drive, the physical drive number is available in diskExtents.Extents[0].DiskNumber

like image 29
Grodriguez Avatar answered Sep 19 '22 11:09

Grodriguez