How to prevent windows from accessing and detecting new volumes while writing a raw image file to PhysicalDrive?

て烟熏妆下的殇ゞ 提交于 2021-02-10 03:22:09

问题


After lots and lots of try and error I am now able to flash/write a raw image file (containing multiple partitions) to \\.\PhysicalDriveN (SD-Card in my case) by just using Windows Api calls (see below).

But as soon as I have some explorer windows open and click around, the write process fails (error 433). Another annoying problem are popup messages telling me that a new volume has to be formatted (at the beginning, while I am still writing!) or autoplay asks me to select an action (after I'm finished).

How to do it right?

Here is what I'm doing now (any improvements welcome!):

  • for each command, a new Handle is opened and closed, using CreateFile("\\.\PhysicalDrive1", ...) (also tried doing everything with the same Handle, and experimented with various flags like FILE_FLAG_NO_BUFFERING, dwShareMode=0, ...)
  • DeviceIoControl(Handle, IOCTL_DISK_DELETE_DRIVE_LAYOUT, ...)
  • Sleep 5s
  • while(...) WriteFile(Handle, ...)
  • DeviceIoControl(Handle, IOCTL_DISK_UPDATE_PROPERTIES, ...)
  • DeviceIoControl(Handle, IOCTL_DISK_ARE_VOLUMES_READY, ...)

If I don't touch anything (and ignore the popups), this works most of the time.

How can I get exclusive access and prevent windows from disturbing me?

Additional question: how can I know how long to wait after deleting the drive layout? If I start too early, writing fails after some seconds (but some data has already been written).

This should all run unattended. At the end I need the just flashed partition to be available, so I can add some files. Even though I'm running "are volumes ready", this doesn't always work immediately and I need multiple retries. Todo: after the files are added, the disk should be ejected.

I tried diskpart / automount disable, but this seems not to change anything.

I tried diskpart / offline disk, but it says this isn't supported for removable media.

I tried various command line tools, like dd.exe from the GNU core utils, but they all have the same or even more problems.

EDIT @DrakeWu-MSFT pointed me to a similar question - yes, enumerating the volumes and dismounting them is something I could try, but I would call it a workaround, since the first thing I already do is deleting all volumes. Is that the wrong way? I would prefer to prevent Windows 10 from automatically creating new volumes after I deleted them, so I can just work on physical disks and not logical volumes.

I analyzed the open source Win32 Disk Imager - it doesn't have the problem I have: while it is writing the image, other processes get access denied as it should be. It achieves this by locking and dismounting the volume, which is exactly the accepted solution from above:

  • DeviceIoControl(Handle, FSCTL_LOCK_VOLUME, ...)
  • DeviceIoControl(Handle, FSCTL_DISMOUNT_VOLUME, ...)

However, what if there is no volume? Then that tool doesn't work at all, it requires a drive letter (and I guess it would fail if there are multiple volumes and drive letters assigned to that disk).

So, if only volumes can be locked, but not disks, I had to create a dummy volume first, just to be able to get a lock? I don't like that! Still hoping for a better solution...

来源:https://stackoverflow.com/questions/65225665/how-to-prevent-windows-from-accessing-and-detecting-new-volumes-while-writing-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!