Link to home
Start Free TrialLog in
Avatar of Ezchdahal
Ezchdahal

asked on

Initialize/Partition/Format

I am writing a Windows front-end to a new storage array product. One of the software requirements is to provide a "Setup" Wizard that will initialize, partition, and format the array.

Unfortunately, I can't find a way to implement this. I've dug around a bit and the DDK routines IOCTL_DISK_CREATE_DISK looks promising but I can't seem to get it to work. The return code indicates success but examining the drive within the Disk Management snap-in after the call shows no visible change. Does anyone have an example of IOCTL_DISK_CREATE_DISK? Are there other alternatives? What am I missing?

Help me Obi-Wan-Experts Exchange, you're my only hope... =)
Avatar of lakshman_ce
lakshman_ce

The IOCTL_DISK_CREATE_DISK control code initializes the specified disk and disk partition table using the information in the CREATE_DISK structure.
To perform this operation, call the DeviceIoControl function with the following parameters.
Read more at
http://msdn.net/library/default.asp?url=/library/en-us/fileio/fs/ioctl_disk_create_disk.asp

Look at the sample calling the API DeviceIOControl
http://msdn.net/library/default.asp?url=/library/en-us/devio/base/calling_deviceiocontrol.asp
Avatar of Ezchdahal

ASKER

Yep. I found those two links before posting. The sample code works but does not involve IOCTL_DISK_CREATE_DISK. I've written some code based on the sample but, while returning a positive return code, nothing else appears to happen. Here's my code.

HANDLE hDevice;              
BOOL bResult;                
DWORD junk;                  

hDevice = CreateFile("\\\\.\\PhysicalDrive2",  
      FILE_ALL_ACCESS,        
      FILE_SHARE_READ |
      FILE_SHARE_WRITE,
      NULL,            
      OPEN_EXISTING,  
      0,                
      NULL);          

if (hDevice != INVALID_HANDLE_VALUE)
{
  CREATE_DISK cd;  
  cd.PartitionStyle = PARTITION_STYLE_MBR;
  cd.Mbr.Signature = 0x28;

  bResult = DeviceIoControl(hDevice,  
      IOCTL_DISK_CREATE_DISK,  
      &cd, sizeof(cd),    
      NULL, 0,                  
      &junk,                
      (LPOVERLAPPED) NULL);  

  CloseHandle(hDevice);
}

Perhaps the signature field has something to do with the problem. It wasn't
clear what I should set it to. I tried 0 and 0x28.
You said you get a positive return code. Did you mean DeviceIoControl returned non zero value?

I am not sure what the signature value should be. You can try to do this way.

CREATE_DISK cd;  
CREATE_DISK_MBR cdMBR;

cdMbr.Signature = 0x28;

cd.PartitionStyle = PARTITION_STYLE_MBR;
cd.Mbr = cdMBR;
 
Yes. DeviceIoControl returns 1.

I went ahead and tried your suggestion for completeness, the union semantics should be equivalent. The same response was generated.

My assumptions must be wrong somewhere along the line. I wonder what the Disk Management Snap-In actually does when you select "Initialize Disk"?
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Dan,

To keep the post simple I didn't go into all the detail regarding solutions I had already explored but you have mentioned many of them. Specifically, diskpart, FormatX, and SHFormatDrive. Diskpart scripting is too scary as a production solution. That tool is dangerous. SHFormatDrive brings up UI, which I'd like to avoid. (Not because I want to write a virus, because I want the wizard to be as simple, tailored and foolproof as possible.) FormatX was my current favorite if I couldn't find a DDK solution for formatting. BTW. There's a couple useful Experts Exchange questions on FormatX for anyone researching the subject

https://www.experts-exchange.com/questions/21861608/c-call-to-FormatEx-results-in-error.html
https://www.experts-exchange.com/questions/10500798/Quickformat-Disk-using-API.html

The initializing and partitioning comes first though. I noticed just a couple minutes ago that after disconnecting and reconnecting my test array, it showed up as initialized in Disk Management. This is promising. Perhaps, there is another call I need to make to flush the drive information and reload it. That will be an investigation for Monday...

Oh and thanks for the APIMonitor suggestion. I was thinking StraceNT but APIMonitor might be a better way to go.

The Formatx source code is in easy-to-understand C code.
After formatting, it calls
   GetVolumeInformationW(...)
   GetDiskFreeSpaceExW()
which might be what makes up the Explorer to the new drive (for obvious reasons, I am not testing any of these suggestions :-).

THis looks promising.  Maybe you need to mount the frive:

   Mounting a Volume at a Mount Point
   http://msdn.microsoft.com/library/en-us/fileio/fs/mounting_a_volume_at_a_mount_point.asp
Sure, I'll give Dan the points. The puzzle still remains unfinished (mainly because I've been working on other topics) but hopefully we've gathered most of the pieces to solve it.

Thanks for the help Lakshmanan and Dan,

John