Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain a list of partitions on Windows

Goal

I'm porting a filesystem to Windows, and am writing a more Windows-like interface for the mounter executable. Part of this process is letting the user locate a partition and pick a drive letter. Ultimately the choice of partition has to result in something I can open using CreateFile(), open(), fopen() or similar.

Leads

Windows seems to revolve around the concept of volumes, which don't seem quite analogous to disks, and only occur for already mounted filesystems.

Promising leads I've had include:

  • IOCTL_DISK_GET_DRIVE_LAYOUT_EX
  • Physical Disks and Volumes
  • Displaying Volume Paths

However these all end in volumes or offsets thereof, not the /dev/sda1 partition-specific-style handle I'm after.

This question is after a very similar thing, I considered a bounty until I observed the OP is after physical disk names, not partitions. This answer contains a method to brute force partition names, I'd like to avoid that (or see documentation containing bounds for the possible paths).

Question

I'd like:

  • Correct terminology and documentation for unmounted partitions in Windows.
  • An effective and documented method to reliably retrieve all available partitions.
  • The closest fit to the partition file abstraction as available in Linux, wherein all IO is bound to the appropriate area of the disk for the partition opened.

Update0

While the main goal is still opening raw partitions, it appears the solution may involve first acquiring a handle to each disk drive, and then using that in turn to acquire each partition. How to enumerate all the disk drives (even those without mounted volumes on them already) is required.

like image 634
Matt Joiner Avatar asked Oct 28 '10 11:10

Matt Joiner


People also ask

How do I see all my partitions?

To see all of your partitions, right-click the Start button and select Disk Management.

How do I see disk list in CMD?

From the command prompt, type diskpart and press Enter. The diskpart prompt will open. From the diskpart prompt, type list disk and press Enter. A list of disks will appear in a text format.

How can I see partitions in Windows 10?

Check MBR or GPT partition style using Disk ManagementRight-click the disk (not the partition) and select the Properties option. Click the Volumes tab. Check the “Partition style” field, which will tell you if the hard drive is formatted using the Master Boot Record (MBR) or GUID Partition Table (GPT) style.


1 Answers

As you noted, you can use IOCTL_DISK_GET_DRIVE_LAYOUT_EX to get a list of partitions.

There's a good overview of the related concepts here. I wonder if the missing link for you is

Detecting the Type of Disk

There is no specific function to programmatically detect the type of disk a particular file or directory is located on. There is an indirect method.

First, call GetVolumePathName. Then, call CreateFile to open the volume using the path. Next, use IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS with the volume handle to obtain the disk number and use the disk number to construct the disk path, such as "\?\PhysicalDriveX". Finally, use IOCTL_DISK_GET_DRIVE_LAYOUT_EX to obtain the partition list, and check the PartitionType for each entry in the partition list.

The full list of disk management control codes may have more that would be useful. To be honest I'm not sure how the Unix partition name maps onto Windows, maybe it just doesn't directly.

like image 161
Steve Townsend Avatar answered Sep 22 '22 23:09

Steve Townsend