Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take disks online/offline

I have a program that is doing raw IO to disks within Windows.

All works fine if the target disk is online. However, the default behavior in some Windows OSes is to have new disks initially offline.

I am having a hard time finding the correct API to do this on Windows. The command line equivalent would be something like:

"select disk 2", "online disk" | diskpart

However I need to be able to do this in code. I looked through the DeviceIoControl Win32 API (which I think is right) but cannot determine which control code to use. The fact that I can't find it makes me think I might be missing a better API to use.

like image 319
Joe Avatar asked Sep 23 '11 16:09

Joe


2 Answers

For future generations, the answer (on Win 2k3/Vista and later) is the Virtual Disk Service (VDS). There's some work getting it all together, especially if you don't use COM objects within .NET that much.

Disk online/offline is done with IVdsDrive::SetStatus. At least it should; I found that I could solve my problem with simply disabling read-only status on my disk. I was able to do this with IVdsDisk::SetFlags with the appropriate flag value.

like image 52
Joe Avatar answered Oct 09 '22 12:10

Joe


This question has a couple useful links to the Windows API, including the DeviceIOControl method.

After looking through all of the enumerations, I could not find anything related to bringing a disk online, or make any interesting change to the disk beyond formatting/partitions. This is likely because only hot-swappable hard drives are supported by this functionality. The market for hot-swappable hard drives is very small, and the vast majority of those situations there are drivers to support any needed operations. Finally the remainder should be able to use the diskpart tool for whatever is necessary.

You need to look again at your requirements I think. You are running a process that has the rights necessary to online a hard disk, but cannot access a command line program? Here are some suggestions for common reasons to not use a command line program:

  • Can't have a black screen pop up - tons of solutions to this problem available online
  • Security team won't allow it - you are already running the process as an administrator so you trust it, why wouldn't you trust the built in Windows function
  • Technical problems preclude calling other processes - I would be interested in how this was managed given the process is running as an administrator
  • Coding guidelines such as "Always use the API" - there isn't one due to lack of need
like image 23
Guvante Avatar answered Oct 09 '22 11:10

Guvante