使用 VirtualDisk API 创建 VHD 文件

发布于 2024-11-17 14:43:15 字数 3026 浏览 2 评论 0原文

我正在尝试使用 VSS 快照作为 CreateVirtualDisk() 的源。环境/工具是 W7x64Ultimate 上的 C++ VS2008SP1 和 7.1 SDK

[已编辑]

这适用于 Windows 7 x64

BOOL CreateVHD_Fixed(PCWSTR pszVhdPath, ULONG sizeInMB)
{
    BOOL bRet = FALSE;
    HANDLE hvhd;
    CREATE_VIRTUAL_DISK_PARAMETERS  params;
    VIRTUAL_DISK_ACCESS_MASK        mask;
    VIRTUAL_STORAGE_TYPE            vst =
    {
        VIRTUAL_STORAGE_TYPE_DEVICE_VHD,
        VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
    };

    wprintf(L"CreateVHD_Fixed %s, size (MB) %d\n", pszVhdPath, sizeInMB);

    params.Version1.UniqueId            = GUID_NULL;
    params.Version1.BlockSizeInBytes    = 0;
    params.Version1.MaximumSize         = sizeInMB * 1024 * 1024;
    params.Version1.ParentPath          = NULL;
    params.Version1.SourcePath          = NULL;
    params.Version1.SectorSizeInBytes   = 512;
    params.Version          = CREATE_VIRTUAL_DISK_VERSION_1;
    mask                                = VIRTUAL_DISK_ACCESS_CREATE;

    DWORD ret = CreateVirtualDisk(&vst, 
        pszVhdPath, 
        mask, 
        NULL, 
        // To create a dynamic disk, use CREATE_VIRTUAL_DISK_FLAG_NONE instead.
        CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION, 
        0, 
        &params, 
        NULL, 
        &hvhd);

    if (ret == ERROR_SUCCESS)
    {
        bRet = TRUE;
    }
    else
    {
        bRet = FALSE;
        printf("failed to create vdisk...err 0x%x\n", ret);
        PrintErrorMessage(GetLastError());
    }

    if (INVALID_HANDLE_VALUE != hvhd)
    {
        CloseHandle(hvhd);
    }

    return bRet;
}

[已编辑] - 现在以不同的方式失败,并显示 ERROR_INVALID_PARAMETER。下面是参数,根路径为“\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy64”

    VIRTUAL_STORAGE_TYPE storageType =
    {
        VIRTUAL_STORAGE_TYPE_DEVICE_VHD,
        // do not use any other GUID else you get an unknown provider error
        VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT // **critical!**
    };

    VIRTUAL_DISK_ACCESS_MASK vdam = (VIRTUAL_DISK_ACCESS_MASK)(VIRTUAL_DISK_ACCESS_CREATE); // |VIRTUAL_DISK_ACCESS_WRITABLE|VIRTUAL_DISK_ACCESS_READ|VIRTUAL_DISK_ACCESS_GET_INFO);
    CREATE_VIRTUAL_DISK_FLAG flags = CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION; // CREATE_VIRTUAL_DISK_FLAG_NONE;
    CREATE_VIRTUAL_DISK_PARAMETERS parameters;
    //
    parameters.Version = CREATE_VIRTUAL_DISK_VERSION_1;
    parameters.Version1.UniqueId = GUID_NULL;
    parameters.Version1.MaximumSize = 0;
    parameters.Version1.BlockSizeInBytes = CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_BLOCK_SIZE;
    parameters.Version1.ParentPath = 0;
    parameters.Version1.SourcePath = root.c_str();
    parameters.Version1.SectorSizeInBytes = CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_SECTOR_SIZE;
    ULONG ProviderSpecificFlags = 0;

HANDLE handle = 0;
dwRet = CreateVirtualDisk(&storageType,
_T("t:\\test.vhd"),
vdam,
NULL,
flags,
ProviderSpecificFlags,
&parameters,0,&handle);

有什么想法吗?虚拟磁盘API似乎没有太多示例代码。

谢谢++

杰瑞。

I'm trying to use a VSS snapshot as the source for CreateVirtualDisk(). Environment/tools are C++ VS2008SP1 and 7.1 SDK on W7x64Ultimate

[Edited]

This works on Windows 7 x64

BOOL CreateVHD_Fixed(PCWSTR pszVhdPath, ULONG sizeInMB)
{
    BOOL bRet = FALSE;
    HANDLE hvhd;
    CREATE_VIRTUAL_DISK_PARAMETERS  params;
    VIRTUAL_DISK_ACCESS_MASK        mask;
    VIRTUAL_STORAGE_TYPE            vst =
    {
        VIRTUAL_STORAGE_TYPE_DEVICE_VHD,
        VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
    };

    wprintf(L"CreateVHD_Fixed %s, size (MB) %d\n", pszVhdPath, sizeInMB);

    params.Version1.UniqueId            = GUID_NULL;
    params.Version1.BlockSizeInBytes    = 0;
    params.Version1.MaximumSize         = sizeInMB * 1024 * 1024;
    params.Version1.ParentPath          = NULL;
    params.Version1.SourcePath          = NULL;
    params.Version1.SectorSizeInBytes   = 512;
    params.Version          = CREATE_VIRTUAL_DISK_VERSION_1;
    mask                                = VIRTUAL_DISK_ACCESS_CREATE;

    DWORD ret = CreateVirtualDisk(&vst, 
        pszVhdPath, 
        mask, 
        NULL, 
        // To create a dynamic disk, use CREATE_VIRTUAL_DISK_FLAG_NONE instead.
        CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION, 
        0, 
        ¶ms, 
        NULL, 
        &hvhd);

    if (ret == ERROR_SUCCESS)
    {
        bRet = TRUE;
    }
    else
    {
        bRet = FALSE;
        printf("failed to create vdisk...err 0x%x\n", ret);
        PrintErrorMessage(GetLastError());
    }

    if (INVALID_HANDLE_VALUE != hvhd)
    {
        CloseHandle(hvhd);
    }

    return bRet;
}

[Edited] - now failing in a different way with ERROR_INVALID_PARAMETER. Parameters are below with a root path of "\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy64"

    VIRTUAL_STORAGE_TYPE storageType =
    {
        VIRTUAL_STORAGE_TYPE_DEVICE_VHD,
        // do not use any other GUID else you get an unknown provider error
        VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT // **critical!**
    };

    VIRTUAL_DISK_ACCESS_MASK vdam = (VIRTUAL_DISK_ACCESS_MASK)(VIRTUAL_DISK_ACCESS_CREATE); // |VIRTUAL_DISK_ACCESS_WRITABLE|VIRTUAL_DISK_ACCESS_READ|VIRTUAL_DISK_ACCESS_GET_INFO);
    CREATE_VIRTUAL_DISK_FLAG flags = CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION; // CREATE_VIRTUAL_DISK_FLAG_NONE;
    CREATE_VIRTUAL_DISK_PARAMETERS parameters;
    //
    parameters.Version = CREATE_VIRTUAL_DISK_VERSION_1;
    parameters.Version1.UniqueId = GUID_NULL;
    parameters.Version1.MaximumSize = 0;
    parameters.Version1.BlockSizeInBytes = CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_BLOCK_SIZE;
    parameters.Version1.ParentPath = 0;
    parameters.Version1.SourcePath = root.c_str();
    parameters.Version1.SectorSizeInBytes = CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_SECTOR_SIZE;
    ULONG ProviderSpecificFlags = 0;

HANDLE handle = 0;
dwRet = CreateVirtualDisk(&storageType,
_T("t:\\test.vhd"),
vdam,
NULL,
flags,
ProviderSpecificFlags,
¶meters,0,&handle);

Any ideas? The virtual disk API does not seem to have much example code.

Thx++

Jerry.

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

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

发布评论

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

评论(1

﹏半生如梦愿梦如真 2024-11-24 14:43:15

Jerry,

使用 CreateVirtualDisk API 创建 VHD 或差异 VHD,请确保发送正确的参数。您必须指定父硬盘,但不能指定源硬盘。使用旗帜时也必须小心。

请参考以下链接:
http://code.msdn.microsoft.com/windowshardware/CppVhdAPI-4412d182

http://code.msdn.microsoft.com/windowsdesktop/Virtual-hard-disk -03108ed3"

Jerry,

Use CreateVirtualDisk API to create a VHD or a differential VHD, make sure u send the right parameters. You have to specify the parent hard disk but not the source hard disk. Care must also be taken while using the flags.

Refer the links below:
"http://code.msdn.microsoft.com/windowshardware/CppVhdAPI-4412d182"

and

"http://code.msdn.microsoft.com/windowsdesktop/Virtual-hard-disk-03108ed3"

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