使磁盘联机/脱机

发布于 2024-12-06 10:51:18 字数 332 浏览 1 评论 0原文

我有一个程序正在 Windows 中对磁盘进行原始 IO 操作。

如果目标磁盘在线,则一切正常。但是,某些 Windows 操作系统中的默认行为是让新磁盘最初处于脱机状态。

我很难找到正确的 API 在 Windows 上执行此操作。等效的命令行类似于:

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

但是我需要能够在代码中执行此操作。我查看了DeviceIoControl Win32 API(我认为是正确的),但无法确定要使用哪个控制代码。事实上,我找不到它,这让我觉得我可能缺少一个更好的 API 来使用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

违心° 2024-12-13 10:51:18

对于后代来说,答案(在 Win 2k3/Vista 及更高版本上)是虚拟磁盘服务 (VDS)。需要做一些工作才能将它们整合在一起,特别是如果您在 .NET 中不经常使用 COM 对象的话。

磁盘联机/脱机通过 IVdsDrive::SetStatus 完成。至少应该;我发现只需禁用磁盘上的只读状态即可解决我的问题。我可以使用 IVdsDisk::SetFlags 来执行此操作 具有适当的标志值。

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.

情定在深秋 2024-12-13 10:51:18

不确定 C#,但我在 C++ 中使用它:
尝试使用 IOCTL_DISK_SET_DISK_ATTRIBUTES 调用 DeviceIoControl()。文件句柄必须具有读写访问权限。我认为它至少需要 Windows 7。它不适用于 Windows 2003 x64。 Windows 8 成功使磁盘脱机,然后您可以从备份重写它。

BOOL disk_offline(HANDLE h_file, bool enable){
DWORD bytes_returned = 0;
BOOL b_offline = 0;
if(get_size_volume_disk(h_file)){
    SET_DISK_ATTRIBUTES disk_attr;
    ZeroMemory(&disk_attr, sizeof(disk_attr));
    disk_attr.Version = sizeof(SET_DISK_ATTRIBUTES);
    disk_attr.Attributes = enable? DISK_ATTRIBUTE_OFFLINE: 0;
    disk_attr.AttributesMask = DISK_ATTRIBUTE_OFFLINE;
    b_offline = DeviceIoControl(h_file, IOCTL_DISK_SET_DISK_ATTRIBUTES, &disk_attr, disk_attr.Version, NULL, 0, &bytes_returned, NULL);
    // Invalidates the cached partition table and re-enumerates the device.
    if(!enable) BOOL b_update = DeviceIoControl(h_file, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &bytes_returned, NULL);
}
return b_offline;
}

Not sure about C#, but I'm using this in C++:
Try calling DeviceIoControl() with IOCTL_DISK_SET_DISK_ATTRIBUTES. The file handle must have read and write access. I think it requires at least Windows 7. It doesn't work on Windows 2003 x64. Windows 8 successfully takes the disk offline and then you can rewrite it from a backup.

BOOL disk_offline(HANDLE h_file, bool enable){
DWORD bytes_returned = 0;
BOOL b_offline = 0;
if(get_size_volume_disk(h_file)){
    SET_DISK_ATTRIBUTES disk_attr;
    ZeroMemory(&disk_attr, sizeof(disk_attr));
    disk_attr.Version = sizeof(SET_DISK_ATTRIBUTES);
    disk_attr.Attributes = enable? DISK_ATTRIBUTE_OFFLINE: 0;
    disk_attr.AttributesMask = DISK_ATTRIBUTE_OFFLINE;
    b_offline = DeviceIoControl(h_file, IOCTL_DISK_SET_DISK_ATTRIBUTES, &disk_attr, disk_attr.Version, NULL, 0, &bytes_returned, NULL);
    // Invalidates the cached partition table and re-enumerates the device.
    if(!enable) BOOL b_update = DeviceIoControl(h_file, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &bytes_returned, NULL);
}
return b_offline;
}
本王不退位尔等都是臣 2024-12-13 10:51:18

这个问题 有几个指向 Windows API 的有用链接,包括 DeviceIOControl 方法。

查看所有枚举后,我找不到与使磁盘联机相关的任何内容,也找不到除格式化/分区之外对磁盘进行任何有趣的更改。这可能是因为此功能仅支持热插拔硬盘驱动器。热插拔硬盘驱动器的市场非常小,并且绝大多数情况下都有驱动程序来支持任何所需的操作。最后,其余的人应该能够使用 diskpart 工具来完成任何需要的事情。

我认为您需要再次考虑您的要求。您正在运行的进程具有使硬盘联机所需的权限,但无法访问命令行程序?以下是一些不使用命令行程序的常见原因的建议:

  • 无法弹出黑屏 - 网上有大量针对此问题的解决方案
  • 安全团队不允许这样做 - 您已经以管理员身份运行该进程所以你信任它,为什么你不信任内置的 Windows 函数
  • 技术问题阻止调用其他进程 - 我对如何管理它感兴趣,因为该进程以管理员身份运行
  • 编码指南,例如“始终使用 API” - 由于缺乏需要而没有

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
俯瞰星空 2024-12-13 10:51:18

使用DeviceIoControl和IOCTL_DISK_IS_WRITABLE控制代码,可以检查磁盘是否可写。如果磁盘离线则返回 false。这意味着可以确定磁盘是否脱机,并且在 Windows 2003 及更高版本中工作正常。但是,我找不到任何有用的 IOCTL 在 Windows 2003 上使磁盘联机。IOCTL_DISK_SET_DISK_ATTRIBUTES 仅适用于 Windows 2008 及更高版本。

Using DeviceIoControl and IOCTL_DISK_IS_WRITABLE control code, it is possible to check if disk is writable. If the disk is offline it returns false. This means that it is possible to determine if disk is offline and it works fine with Windows 2003 and after. However, I could not find any useful IOCTL to bring the disk online on Windows 2003. IOCTL_DISK_SET_DISK_ATTRIBUTES only works with Windows 2008 and after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文