views:

86

answers:

3

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:

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.

+1  A: 

If you can imagine moving from safe haven of userspace and the Windows API (win32) to coding a device driver with NTTDK, you could try IoReadPartitionTableEx or some other low level disk function.

Amigable Clark Kant
@Amigable Clark Kant: Seems very similar to what win32 provides, but it lacks the ability to lookup the available disks in the first place. There's also a note: _IoReadPartitionTableEx must only be used by disk drivers._
Matt Joiner
I am upvoting this because it's useful info in the (obscure) area of the question, even if not a full answer for @Matt.
Steve Townsend
+1  A: 

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.

Steve Townsend
Looks decent at least to me. +1.
Amigable Clark Kant
How do you map the partitions to paths?
Matt Joiner
@Matt Joiner - this info is available via `GetVolumePathName`, based on filename input. Still looking for a way to enumerate the drives.
Steve Townsend
@Matt Joiner - you can enumerate the volumes using `FindFirstVolume/FindNextVolume`. Given that, you can enumerate the partitions as above, and implicitly this gives you all the disks. See example here - http://msdn.microsoft.com/en-us/library/cc542456(v=VS.85).aspx
Steve Townsend
A: 

I think you're slightly mistaken in an earlier phase. For instance, you seem to assume that "mounting" works in Windows like it works in Unix. It's a bit different.

Let's start at the most familiar end. Paths like C:\ use drive letters. Those are essentially just a set of symbolic links nowadays (On Windows, they're more formally known as "junctions"). There's a base set for all users, and each user can add their own. Even if there is no drive letter for a volume, there will still be a volume name like \\?\Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}\. You can use this volume name in calls to CreateFile() etc. I'm not sure if fopen() likes them, though.

The function QueryDosDevice will get you the Windows device name for a drive letter or a volume name. A device name looks like "\Device\HarddiskVolume1", but you can't pass it to CreateFile

Microsoft has example code to enumerate all partitions.

On Windows, like on Linux, you can open the partition itself as if it were a file. This is quite well documented under CreateFile.

MSalters
The example code you've linked to is already in my list of leads, and only appears to work for mounted volumes. Furthermore it isn't specific to partitions. I need to access unmounted stuff.
Matt Joiner
Well, the concepts don't really translate. Windows doesn't have a "mount" concept, so the concept of an unmounted partition doesn't make sense either. Are you talking about the collection of sectors on a disk which are part of a filesystem for which Windows doesn't have a driver? There's no object in Windows that represents those sectors, AFAIK.
MSalters
@MSalters: That's what I'm after.
Matt Joiner