如何在 Windows 环境中模拟磁盘已满错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/528567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I simulate a disk full error in a Windows environment?
提问by Alceu Costa
I have to write a bat script for a test scenario where the software that we are testing fails to write to file due to a disk full error. The test script must be automated, so that we can run it on overnight tests, for example. The test script must also work at different computers, so installing a software like a virtual machine wouldn't be the best solution in this case.
我必须为我们正在测试的软件由于磁盘已满错误而无法写入文件的测试场景编写一个 bat 脚本。测试脚本必须是自动化的,以便我们可以在夜间测试中运行它,例如。测试脚本还必须在不同的计算机上运行,因此在这种情况下安装虚拟机等软件并不是最佳解决方案。
How can I simulate that error in a Windows environment?
如何在 Windows 环境中模拟该错误?
采纳答案by Patrick McDonald
You could try writing to a full floppy disk.
您可以尝试写入完整的软盘。
Edit:
编辑:
In light of your edited question, you could set up a network share with no disk space quota and write to that. The error will then be produced regardless of the logged on user or machine.
根据您编辑的问题,您可以设置一个没有磁盘空间配额的网络共享并写入该文件。无论登录的用户或机器如何,都会产生错误。
回答by Patrick Cuff
For Windows XP or later:
对于 Windows XP 或更高版本:
This command can get the amount of free space for the c:\ drive:
此命令可以获取 c:\ 驱动器的可用空间量:
for /f "usebackq tokens=1-5" %%A in (`dir c:\ ^| find "bytes free"`) do (
set FREE_SPACE=%%C
)
Replace c:\ with your drive, as needed.
根据需要将 c:\ 替换为您的驱动器。
You can then take some space away from this value so you have a little room to work with:
然后,您可以从该值中腾出一些空间,以便有一点空间可以使用:
set /a FREE_SPACE=FREE_SPACE-1024
or however much space you want to keep free.
或者您想保留多少空间。
You can use the fsutil
command to create a file to fill up the free space on the disk:
您可以使用该fsutil
命令创建一个文件来填充磁盘上的可用空间:
fsutil file createnew c:\spacehog.dat %FREE_SPACE%
Run your test, writing to the drive. After you write 1024 bytes or so you should run out of space.
运行测试,写入驱动器。写入 1024 字节左右后,您应该会耗尽空间。
回答by Rad
回答by ryeguy
It might seem like a bit much, but one thing I can think of is to use a virtual machine, and set its virtual disk to just big enough to fit the OS on. Fill it with some garbage files to tip it over the edge, then run your program.
这可能看起来有点多,但我能想到的一件事是使用虚拟机,并将其虚拟磁盘设置为足够大以适应操作系统。用一些垃圾文件填充它以使其超出边缘,然后运行您的程序。
回答by Peter Tseng
Best Option: Microsoft's consume program
最佳选择:微软的消费计划
Reasons:
原因:
- It tests the system disk(vs a separate drive)
- It's fast - run the program to fill the disk instantly, stop when no longer needed
- It's easy - No creating and deleting files. No extra test partition hanging around. Installation is required, but you can use a simple command afterward.
- It's scriptable
- 它测试系统磁盘(与单独的驱动器相比)
- 速度很快-运行程序立即填充磁盘,不再需要时停止
- 很简单 - 无需创建和删除文件。没有额外的测试分区。需要安装,但之后您可以使用一个简单的命令。
- 它是可编写脚本的
Steps:
脚步:
- Install the Windows Server 2003 Resource Kit Tools(Works fine on Windows 7)
cd "%ProgramFiles(x86)%\Windows Resource Kits\Tools"
(or whereever it's installed)consume.exe -disk-space
- 安装Windows Server 2003 Resource Kit Tools(在 Windows 7 上工作正常)
cd "%ProgramFiles(x86)%\Windows Resource Kits\Tools"
(或安装在任何地方)consume.exe -disk-space
Command output:
命令输出:
C:\Program Files (x86)\Windows Resource Kits\Tools>consume.exe
Universal Resource Consumer - Just an innocent stress program, v 0.1.0
Copyright (c) 1998, 1999, Microsoft Corporation
consume RESOURCE [-time SECONDS]
RESOURCE can be one of the following:
-physical-memory
-page-file
-disk-space
-cpu-time
-kernel-pool
C:\Program Files (x86)\Windows Resource Kits\Tools>consume.exe -disk-space
Consume: Message: Total disk space: 96049 Mb
Consume: Message: Free disk space: 14705 Mb
Consume: Message: Free per user space: 14705 Mb
Consume: Message: Attempting to use: 14705 Mb
Consume: Message: Reattempting to use: 14705 Mb
Consume: Message: Sleeping ...
Other Options:
其他选项:
- Windows 7 has a virtual hard drive feature. Basically do the following: Computer Management > Disk Management > Action Menu > Create VHD > Right click disk and Initialize > Right click
- Generate large files (should be instant) until your disk is full using a shell command or Dummy File Generatorprogram. Another prorgram: SpaceHog.
- Windows 7 具有虚拟硬盘驱动器功能。基本上执行以下操作:计算机管理 > 磁盘管理 > 操作菜单 > 创建 VHD > 右键单击磁盘并初始化 > 右键单击
- 使用 shell 命令或Dummy File Generator程序生成大文件(应该是即时的),直到您的磁盘已满。另一个程序:SpaceHog。
回答by Mizipzor
Create a secondary partition, fill it with junk and then run your program there.
创建一个二级分区,用垃圾填满它,然后在那里运行你的程序。
回答by M4N
You could setup a small ramdisk and write to that. See this page for some free ramdisk products.
您可以设置一个小型 ramdisk 并写入其中。有关一些免费的 ramdisk 产品,请参阅此页面。
回答by Derek
I have made a modification to the above script to make it compatiable with Windows 7... Essentially adding the switch "/-c" to the for statement. This removes the thousands seperator as fsutil does not like it in the statement.
我对上面的脚本进行了修改,使其与 Windows 7 兼容...本质上是将开关“/-c”添加到 for 语句中。这将删除千位分隔符,因为 fsutil 在语句中不喜欢它。
for /f "usebackq tokens=1-5" %%A in (`dir /-c d:\ ^| find "bytes free"`) do (set FREE_SPACE=%%C)
fsutil file createnew d:\largefile.txt %FREE_SPACE%
回答by Leonidas
Best thing that works on every computer (as testing is not neceessarily done on a dedicated machine) would be a ramdrive/ramdisk that could be set up on the fly.
可以在每台计算机上运行的最好的东西(因为测试不一定在专用机器上完成)是可以即时设置的 ramdrive/ramdisk。
Only found a Virtual Disk SDK so far see herethat maybe could be included in your buzild process.
到目前为止只找到了一个虚拟磁盘 SDK,请参阅此处,也许可以将其包含在您的 buzild 进程中。
Different idea: maybe your testing computers could be set up to write to a shared network folder (that is full) mount as a drive?
不同的想法:也许您的测试计算机可以设置为写入共享网络文件夹(即已满)作为驱动器安装?
回答by Leonidas
Create a new user accout, set a quota for it, and use runas to run your app as that user. (not exactly the same as disk full, but should have similar consequences.)
创建一个新用户帐户,为其设置配额,然后使用 runas 以该用户身份运行您的应用程序。(与磁盘已满不完全相同,但应该有类似的结果。)