- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在.NET Core 中使用 NTFS 创建虚拟硬盘。
我在其 GitHub page 上找到了 DiscUtils NuGet 包和示例代码可以很好地创建 FAT 格式的 VHD。
long diskSize = 30 * 1024 * 1024; //30MB
using (Stream vhdStream = File.Create(@"C:\TEMP\mydisk.vhd"))
{
Disk disk = Disk.InitializeDynamic(vhdStream, diskSize);
BiosPartitionTable.Initialize(disk, WellKnownPartitionType.WindowsFat);
using (FatFileSystem fs = FatFileSystem.FormatPartition(disk, 0, null))
{
fs.CreateDirectory(@"TestDir\CHILD");
// do other things with the file system...
}
}
但对于我的用例,我需要大于 2 GB 的文件。因为无论如何我们都使用 Windows,所以 NTFS 没问题。所以我尝试了这段代码
long diskSize = 300 * 1024 * 1024; //300 MB
var vhdPath = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetRandomFileName(), "vhd"));
using (Stream vhdStream = File.Create(vhdPath))
{
var disk = DiscUtils.Vhd.Disk.InitializeFixed(vhdStream, Ownership.None, diskSize);
BiosPartitionTable.Initialize(disk, WellKnownPartitionType.WindowsNtfs);
using (var ntfs = NtfsFileSystem.Format(vhdStream, "Virtual NTFS drive", Geometry.FromCapacity(diskSize), 0, diskSize / Sizes.Sector))
{
ntfs.CreateDirectory(@"TestDir\CHILD");
// do other things with the file system...
}
}
此代码创建了一个 300 MB 的 VHD,我可以使用 7zip 打开该 VHD,但它包含一个约 300 MB 的 *.mbr 文件。如果我尝试打开它,它会在临时文件夹中打开一个新的 7zip 窗口。如果我对该 vhd 加倍时钟,则会出现 Windows 错误“驱动器镜像未初始化,包含无法识别的分区或包含未分配驱动器号的卷。使用驱动器管理管理单元确保驱动器、分区和卷处于可用状态。”(免费翻译自德语)
此后我无法再访问该文件,因为某些 Windows 进程仍然保持忙碌状态。
我在这里误解了什么?
还有其他方法可以使用 C# 和 .NET Core 创建/挂载 VHD吗?
最佳答案
我从 EricZimmerman 那里得到了有关项目的答案 GitHub page :
var diskSize = 2000 * 1024 * 1024;
var asVhd = false;
using (var fs = new FileStream(_vhfxFileName, FileMode.OpenOrCreate))
{
VirtualDisk destDisk = Disk.InitializeDynamic(fs, Ownership.None, diskSize);
if (asVhd)
{
destDisk = DiscUtils.Vhd.Disk.InitializeDynamic(fs, Ownership.None, diskSize);
}
BiosPartitionTable.Initialize(destDisk, WellKnownPartitionType.WindowsNtfs);
var volMgr = new VolumeManager(destDisk);
var label = $"ZZZZZZ ({dateStamp})";
using (var destNtfs = NtfsFileSystem.Format(volMgr.GetLogicalVolumes()[0], label, new NtfsFormatOptions()))
{
destNtfs.NtfsOptions.ShortNameCreation = ShortFileNameOption.Disabled;
var destDirectory = Path.GetDirectoryName(@"Some file");
destNtfs.CreateDirectory(destDirectory, nfo);
using (Stream source = new FileStream(@"Some file2", FileMode.Open, FileAccess.Read))
{
var destFileName = @"foo";
using (Stream dest = destNtfs.OpenFile(destFileName, FileMode.Create, FileAccess.ReadWrite))
{
source.CopyTo(dest);
dest.Flush();
}
}
// do more stuff here
}
// commit everything to the stream before closing
fs.Flush();
}
对我有用!享受吧:)
关于c# - DiscUtils 创建 NTFS vhd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54762062/
我想将一些 VHDX 文件转换为 VHD,以便我可以将它们上传到 Microsoft Azure,因为我必须将公司的一些服务器迁移到云。问题是,我的服务器似乎不知道 PowerShell 中的 VHD
用户可以将 vhd 文件上传到我的服务器。我想验证他们上传的文件,并确保它们实际上是有效的 vhd 文件,而不是使用 .vhd 文件扩展名等重新命名的 jpeg 文件。有没有办法做到这一点? 最佳答案
我正在尝试自动复制一组 VHD,同时跳过正在使用的 VHD。 为此,我尝试创建所有未使用的 VHD 的列表。如果未使用 VHD,我将能够运行 Get-VHD 并检查 .Attached 属性是否为 f
我正在使用虚拟机自动执行基于服务器的产品的集成测试。 我正在开发人员计算机上使用“Windows XP 模式和虚拟 PC”。 我正在使用 PowerShell 做所有事情。我希望: 挂载 VHD(磁盘
我在 C: 驱动器上安装了一台带有磁盘的虚拟机。它令人讨厌地满了,我不得不移动 vhd。到另一个位置: C:\..\VMs\vm1 F:\..\VMs\vm2 然后我像这样为 HD 重新生成了 uui
我尝试使用 convert-vhd 命令将 VHD 转换为 VHDX,但出现以下错误: The Hyper-V Management Tools could not access an expecte
是否可以创建 2 GB 的 vhd , 其中包含 1 GB 数据,其余 1 GB 仅为零。 因为,我可以从 Windows Azure 门户中已有的基本 URL 创建一个 1GB 的磁盘, 但我不确定
我创建了一个 .vhd 镜像并使用 Edge 通过门户进行下载。 门户中的 .vhd 文件显示其大小为 127GB(这是虚拟机上硬盘驱动器的大小)。 实际使用量为 33GB - 如下所示。问题是,除了
启动到 VHD 文件是否可以加密并仍然在 Windows 7 或 Windows Server 2008 R2 下启动? 最佳答案 不符合 requently Asked Questions: Vir
有人可以帮我理解这一点吗?我创建了Virtual Machine in Azure running Windows Server 2012 。我注意到 Azure 自动创建了一个存储帐户。当我进入该存
清理我的 Azure 基础设施后,有一些剩余的 VHD 被列为“已租用”,但我确信它们没有被任何虚拟机/磁盘引用。 我已将每个列出的 VM 与“磁盘”下列出的每个磁盘与存储帐户 Blob 中列出的每个
清理我的 Azure 基础设施后,有一些剩余的 VHD 被列为“已租用”,但我确信它们没有被任何虚拟机/磁盘引用。 我已将每个列出的 VM 与“磁盘”下列出的每个磁盘与存储帐户 Blob 中列出的每个
我正在尝试使用存储 blob 上的现有 VHD 创建虚拟机。我运行了以下命令 Add-AzureDisk -DiskName“Keldc1”-MediaLocation“https://*****.b
我正在使用OpenVirtualDisk和 AttachVirtualDisk函数挂载VHD文件,但我找不到如何在挂载vhd文件时指定驱动器号。 我想指定要安装的驱动器号,例如(G:、H:、K: 等)
我使用 Microsoft Hypervisor 创建了一些虚拟机。我有这些虚拟机的 vhdx 文件。现在我想使用这些相同的 vhdx 文件在其他计算机上或在具有不同名称的同一台计算机上部署新的虚拟机
我想要执行以下操作: 在美国东部的 Azure 中,捕获当前的 Windows VM 镜像(我知道该怎么做) 下载 .vhd 图像(我知道该怎么做) 现在,我想将(我下载的图像)加载到美国西部的第二个
我在同一个 Blob 服务中有 2 个容器。一个包含一个 VHD,另一个包含一个测试 PDF。 在附加 VHD 的虚拟机中运行进程时,我得到以下数字。 VHD 读取: // VHD = e: driv
我的团队有多个运行 Cassandra 的虚拟机,托管在功能相当强大的物理机上。每个虚拟机都被赋予一个动态大小的 VHD。 在我们启用增量备份并开始拍摄快照后不久,我们注意到虚拟机扩展至填满所有可用空
我正在遵循这个脚本http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-Sample-0daf6d9d用于备份 vhd
我尝试将 VHD 文件从托管操作系统磁盘从东南亚导出到美国东部位置,但出现错误 403 身份验证失败“读取源时复制失败”。我是订阅租户和所有者的全局管理员。请找到相同的屏幕截图和脚本。 Login-A
我是一名优秀的程序员,十分优秀!