Disk size calculation question

When I use IOCTL_DISK_GET_DRIVE_GEOMETRY to calculate disk size I get:

Cylinders: 6527
Tracks/Cylinder: 255
Sectors/Track: 63
Bytes/Sector: 512

I calculate disk size to this:

Sectors: 104856255
Bytes: 53686402560

However, when I call IOCTL_DISK_GET_LENGTH_INFO I get a different value than what I calculated above:

DiskInfo Bytes: 53687091200

Close, but no cigar. Should I be using IOCTL_DISK_GET_LENGTH_INFO and not IOCTL_DISK_GET_DRIVE_GEOMETRY to get physical disk size?

I see that the second values is rounded up to 4096 bytes (which happens to be the volumes cluster size). The first values rounds to sector size. But the disk should not know about cluster sizes.

– Jamey

I’m sure you realize that the disk geometry is essentially made up information, and may not have even a remote resemblance to the actual physical construction of the drive.

Given this, I would use it for anything. Nothing at all.

IOCTL_DISK_GET_LENGTH_INFO Seems to be the way to go.

Peter

Thanks! The larger size seemed to be the correct one to use.

It is also interesting that you can call IOCTL_DISK_GET_DRIVE_GEOMETRY requires no access rights to the disk, but IOCTL_DISK_GET_LENGTH_INFO requires read access rights. Just a subtle observation.

LOL… you need no access rights to obtain useless information, I guess! :wink:
Peter

I’ve settled on IOCTL_DISK_GET_DRIVE_GEOMETRY_EX. It has both the CHS and real size values you get from _GET_LENGTH_INFO. And, using IOCTL_DISK_GET_DRIVE_GEOMETRY_EX also does not require read access to the device.

I’ve written an FS filter that creates a virtual VHD file that gets assigned to a physical disk; \.\PhysicalDrive#. When the file is queries for size information it returns the size of the physical disk + 512 bytes for the fake (in memory) VHD footer. The file looks like any normal fixed VHD file. Reads and writes are redirected to the physical disk.

Unlike VMWare, Hyper-V does not allow the importing or migration of VMs that have physical disks attached to them. Now it does :slight_smile: You can even attach the VHD file in Disk Manager to mount up the volumes. Works if you take the source disk offline too; so there are no conflicts.

Nice project. You’re open sourcing that?

Peter

Nice project. You’re open sourcing that?

OMG…

I am afraid my observation may provoke “The Hanging Judge’s” wrath, but the whole thing sounds like “Filedisk (or whatever it is called) - 2” project is on its way. All the potential “thieves” in existence must be rubbing their hands in anticipation, and all “NTDEV professional ethics and high moral standards guardians” are already testing their CapsLock keys…

Anton Bassov

Building up a fake VHD footer was trivial. However, you cannot attach a VHD file to a version 2 Hyper-V VM. It will only accept VHDX files. I poured over the VHDX specification with respect to fixed size disks, and found very little information. Does anyone know of any samples in GitHub or elsewhere that can give me a good example of accessing a VHDX file from C++; not via the CreateVirtualFile APIs? I really don’t want to have to spend the next few days tearing that sucker appart if someone else has already done so. Are all the logging features and such implemented for fixed VHDX files?

OK, a little more searching and I found something of interest. Didn’t come up in my searches last night. https://github.com/0xbadfca11/makevhdx