// create_vhd.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <Windows.h>
#include <Shlwapi.h>
#include <initguid.h>
#include <virtdisk.h>
#include <sddl.h> DWORD
SampleCreateVirtualDisk(
_In_ LPCWSTR VirtualDiskPath,
_In_opt_ LPCWSTR ParentPath,
_In_ CREATE_VIRTUAL_DISK_FLAG Flags,
_In_ ULONGLONG FileSize,
_In_ DWORD BlockSize,
_In_ DWORD LogicalSectorSize,
_In_ DWORD PhysicalSectorSize)
{
VIRTUAL_STORAGE_TYPE storageType;
CREATE_VIRTUAL_DISK_PARAMETERS parameters;
HANDLE vhdHandle = INVALID_HANDLE_VALUE;
DWORD opStatus;
GUID uniqueId; if (RPC_S_OK != UuidCreate((UUID*)&uniqueId))
{
opStatus = ERROR_NOT_ENOUGH_MEMORY;
goto Cleanup;
} //
// Specify UNKNOWN for both device and vendor so the system will use the
// file extension to determine the correct VHD format.
// storageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN;
storageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN; memset(&parameters, , sizeof(parameters)); //
// CREATE_VIRTUAL_DISK_VERSION_2 allows specifying a richer set a values and returns
// a V2 handle.
//
// VIRTUAL_DISK_ACCESS_NONE is the only acceptable access mask for V2 handle opens.
//
// Valid BlockSize values are as follows (use 0 to indicate default value):
// Fixed VHD: 0
// Dynamic VHD: 512kb, 2mb (default)
// Differencing VHD: 512kb, 2mb (if parent is fixed, default is 2mb; if parent is dynamic or differencing, default is parent blocksize)
// Fixed VHDX: 0
// Dynamic VHDX: 1mb, 2mb, 4mb, 8mb, 16mb, 32mb (default), 64mb, 128mb, 256mb
// Differencing VHDX: 1mb, 2mb (default), 4mb, 8mb, 16mb, 32mb, 64mb, 128mb, 256mb
//
// Valid LogicalSectorSize values are as follows (use 0 to indicate default value):
// VHD: 512 (default)
// VHDX: 512 (for fixed or dynamic, default is 512; for differencing, default is parent logicalsectorsize), 4096
//
// Valid PhysicalSectorSize values are as follows (use 0 to indicate default value):
// VHD: 512 (default)
// VHDX: 512, 4096 (for fixed or dynamic, default is 4096; for differencing, default is parent physicalsectorsize)
//
parameters.Version = CREATE_VIRTUAL_DISK_VERSION_2;
parameters.Version2.UniqueId = uniqueId;
parameters.Version2.MaximumSize = FileSize;
parameters.Version2.BlockSizeInBytes = BlockSize;
parameters.Version2.SectorSizeInBytes = LogicalSectorSize;
parameters.Version2.PhysicalSectorSizeInBytes = PhysicalSectorSize;
parameters.Version2.ParentPath = ParentPath; opStatus = CreateVirtualDisk(
&storageType,
VirtualDiskPath,
VIRTUAL_DISK_ACCESS_NONE,
NULL,
Flags,
,
&parameters,
NULL,
&vhdHandle); if (opStatus != ERROR_SUCCESS)
{
goto Cleanup;
} Cleanup: if (opStatus == ERROR_SUCCESS)
{
wprintf(L"Create success\n");
}
else
{
wprintf(L"Create error = %u\n", opStatus);
} if (vhdHandle != INVALID_HANDLE_VALUE)
{
printf("%s: Handle closed.\n", __func__);
CloseHandle(vhdHandle);
} return opStatus;
} int make_vhd()
{
GUID myGUID;
HRESULT h = CoCreateGuid(&myGUID);
if (h != S_OK)
printf("Failed to generate GUID.\n"); OLECHAR* guidString;
StringFromCLSID(myGUID, &guidString);
printf("%ls\n", guidString); VIRTUAL_STORAGE_TYPE VirtualStorageType;
VirtualStorageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN;
VirtualStorageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN; PCWSTR Path = L"c:\\vhd_test.vhd"; VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask = VIRTUAL_DISK_ACCESS_NONE; PSECURITY_DESCRIPTOR SecurityDescriptor = NULL; CREATE_VIRTUAL_DISK_FLAG Flags = CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION;
//CREATE_VIRTUAL_DISK_FLAG Flags = CREATE_VIRTUAL_DISK_FLAG_NONE; ULONG ProviderSpecificFlags = ; printf("sizeof(PCREATE_VIRTUAL_DISK_PARAMETERS) %zd\n", sizeof(_CREATE_VIRTUAL_DISK_PARAMETERS));
PCREATE_VIRTUAL_DISK_PARAMETERS Parameters = (PCREATE_VIRTUAL_DISK_PARAMETERS)malloc(sizeof(_CREATE_VIRTUAL_DISK_PARAMETERS));
memset(Parameters, , sizeof(_CREATE_VIRTUAL_DISK_PARAMETERS)); Parameters->Version = CREATE_VIRTUAL_DISK_VERSION_2; Parameters->Version2.UniqueId = myGUID;
StringFromCLSID(Parameters->Version2.UniqueId, &guidString);
printf("Parameters->Version2.UniqueId %ls\n", guidString); Parameters->Version2.MaximumSize = 0x200000000;
Parameters->Version2.BlockSizeInBytes = ;
Parameters->Version2.SectorSizeInBytes = ;
Parameters->Version2.PhysicalSectorSizeInBytes = ;
Parameters->Version2.OpenFlags = OPEN_VIRTUAL_DISK_FLAG_NONE;
Parameters->Version2.ParentPath = NULL;
Parameters->Version2.SourcePath = NULL; LPOVERLAPPED Overlapped = NULL; PHANDLE Handle = NULL; DWORD ret = CreateVirtualDisk(
&VirtualStorageType,
Path,
VirtualDiskAccessMask,
SecurityDescriptor,
Flags,
ProviderSpecificFlags,
Parameters,
Overlapped,
Handle
); if(ret != ERROR_SUCCESS)
{
printf("%s: CreateVirtualDisk failed (%d)\n", __func__, ret);
} return ;
} DWORD
SampleAttachVirtualDisk(
_In_ LPCWSTR VirtualDiskPath,
_In_ BOOLEAN ReadOnly)
{
OPEN_VIRTUAL_DISK_PARAMETERS openParameters;
VIRTUAL_DISK_ACCESS_MASK accessMask;
ATTACH_VIRTUAL_DISK_PARAMETERS attachParameters;
PSECURITY_DESCRIPTOR sd;
VIRTUAL_STORAGE_TYPE storageType;
LPCTSTR extension;
HANDLE vhdHandle;
ATTACH_VIRTUAL_DISK_FLAG attachFlags;
DWORD opStatus; vhdHandle = INVALID_HANDLE_VALUE;
sd = NULL; //
// Specify UNKNOWN for both device and vendor so the system will use the
// file extension to determine the correct VHD format.
// storageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN;
storageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN; memset(&openParameters, , sizeof(openParameters)); extension = ::PathFindExtension(VirtualDiskPath); if (extension != NULL && _wcsicmp(extension, L".iso") == )
{
//
// ISO files can only be mounted read-only and using the V1 API.
// if (ReadOnly != TRUE)
{
opStatus = ERROR_NOT_SUPPORTED;
goto Cleanup;
} openParameters.Version = OPEN_VIRTUAL_DISK_VERSION_1;
accessMask = VIRTUAL_DISK_ACCESS_READ;
}
else
{
//
// VIRTUAL_DISK_ACCESS_NONE is the only acceptable access mask for V2 handle opens.
// openParameters.Version = OPEN_VIRTUAL_DISK_VERSION_2;
openParameters.Version2.GetInfoOnly = FALSE;
accessMask = VIRTUAL_DISK_ACCESS_NONE;
} //
// Open the VHD or ISO.
//
// OPEN_VIRTUAL_DISK_FLAG_NONE bypasses any special handling of the open.
// opStatus = OpenVirtualDisk(
&storageType,
VirtualDiskPath,
accessMask,
OPEN_VIRTUAL_DISK_FLAG_NONE,
&openParameters,
&vhdHandle); if (opStatus != ERROR_SUCCESS)
{
goto Cleanup;
} //
// Create the world-RW SD.
// if (!::ConvertStringSecurityDescriptorToSecurityDescriptor(
L"O:BAG:BAD:(A;;GA;;;WD)",
SDDL_REVISION_1,
&sd,
NULL))
{
opStatus = ::GetLastError();
goto Cleanup;
} //
// Attach the VHD/VHDX or ISO.
// memset(&attachParameters, , sizeof(attachParameters));
attachParameters.Version = ATTACH_VIRTUAL_DISK_VERSION_1; //
// A "Permanent" surface persists even when the handle is closed.
// attachFlags = ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME; if (ReadOnly)
{
// ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY specifies a read-only mount.
attachFlags |= ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY;
} opStatus = AttachVirtualDisk(
vhdHandle,
sd,
attachFlags,
,
&attachParameters,
NULL); if (opStatus != ERROR_SUCCESS)
{
goto Cleanup;
} Cleanup: if (opStatus == ERROR_SUCCESS)
{
wprintf(L"Attach success\n");
}
else
{
wprintf(L"Attach error = %u\n", opStatus);
} if (sd != NULL)
{
LocalFree(sd);
sd = NULL;
} if (vhdHandle != INVALID_HANDLE_VALUE)
{
printf("%s: Handle closed.\n", __func__);
CloseHandle(vhdHandle);
} return opStatus;
} HANDLE DoOpenVHD(LPCWSTR VirtualDiskPath, BOOL GetInfoOnly)
{
HANDLE vhdHandle;
OPEN_VIRTUAL_DISK_PARAMETERS openParameters;
openParameters.Version = OPEN_VIRTUAL_DISK_VERSION_2;
openParameters.Version2.GetInfoOnly = GetInfoOnly; VIRTUAL_STORAGE_TYPE storageType;
storageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN;
storageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN; int ret = OpenVirtualDisk(&storageType, VirtualDiskPath,
VIRTUAL_DISK_ACCESS_NONE, OPEN_VIRTUAL_DISK_FLAG_NONE,
&openParameters, &vhdHandle);
if (ret != ERROR_SUCCESS) {
// If return value of OpenVirtualDisk isn't ERROR_SUCCESS, there was a problem opening the VHD
printf("Failed to open VHD %ls. (%d)\n", VirtualDiskPath, ret);
return NULL;
}
return vhdHandle;
} int getPhyDrive(HANDLE vhdHandle)
{
wchar_t physicalDrive[MAX_PATH];
ULONG bufferSize = sizeof(physicalDrive);
GetVirtualDiskPhysicalPath(vhdHandle, &bufferSize, physicalDrive); printf("physicalDrive %ls\n", physicalDrive); if (vhdHandle != INVALID_HANDLE_VALUE)
{
printf("%s: Handle closed.\n", __func__);
CloseHandle(vhdHandle);
} char name[];
memset(name, , );
snprintf(name, , "%ls", physicalDrive);
DWORD driveNumber = name[strlen(name) - ] - '';
printf("Drive idx %d\n", driveNumber);
return driveNumber;
} int __cdecl wmain(_In_ int argc, _In_reads_(argc) WCHAR *argv[])
{
int rc = ;
LPCWSTR virtualDiskPath = argv[];
LPCWSTR parentPath = NULL; ULONGLONG fileSize = _wtoi64(argv[]);
DWORD blockSize = _wtoi(argv[]);
DWORD logicalSectorSize = _wtoi(argv[]);
DWORD physicalSectorSize = _wtoi(argv[]); /*rc = SampleCreateVirtualDisk(
virtualDiskPath,
parentPath,
CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION,
fileSize,
blockSize,
logicalSectorSize,
physicalSectorSize); printf("SampleCreateVirtualDisk %d\n", rc); rc = SampleAttachVirtualDisk(virtualDiskPath, FALSE);
printf("SampleAttachVirtualDisk %d\n", rc);*/ //wchar_t physicalDrive[MAX_PATH];
//ULONG bufferSize = sizeof(physicalDrive);
HANDLE vhdHandle = DoOpenVHD(virtualDiskPath, TRUE);
int drive_idx = getPhyDrive(vhdHandle);
/*char name[1024];
memset(name, 0, 1024);
snprintf(name, 1024, "%ls", physicalDrive);
DWORD driveNumber = name[strlen(name) - 1] - '0';
printf("Drive idx %d\n", driveNumber);*/
//MountVol(drive_idx); getchar();
return ;
}

Windows VHD Create, Attach, 获得Disk序号的更多相关文章

  1. 准备好要上传到 Azure 的 Windows VHD 或 VHDX

    在将 Windows 虚拟机 (VM) 从本地上传到 Azure 之前,必须准备好虚拟硬盘(VHD 或 VHDX). Azure 仅支持采用 VHD 文件格式且具有固定大小磁盘的第 1 代 VM. V ...

  2. 从 Azure 下载 Windows VHD

    本文介绍如何使用 Azure 门户从 Azure 下载 Windows 虚拟硬盘 (VHD) 文件. Azure 中的虚拟机 (VM) 将磁盘用作存储操作系统.应用程序和数据的位置. 所有 Azure ...

  3. [Windows Azure] Create a Virtual Network for Site-to-Site Cross-Premises Connectivity

    Create a Virtual Network for Site-to-Site Cross-Premises Connectivity This tutorial walks you throug ...

  4. [Windows Azure] Create a Virtual Network in Windows Azure

    Create a Virtual Network in Windows Azure This tutorial walks you through the steps to create a basi ...

  5. [Windows Azure] Create and use a reporting service in Windows Azure SQL Reporting

    Create and use a reporting service in Windows Azure SQL Reporting In this tutorial you will learn ab ...

  6. Windows性能计数器--磁盘性能分析Disk

    Physical Disk: 单次IO大小 Avg.Disk Bytes/Read Avg.Disk Bytes/Write IO响应时间 Avg.Disk sec/Read Avg.Disk sec ...

  7. 教你一招:解决windows xp系统开机出现“disk checking has been canceled”

    问题重现: 问题分析: 系统的注册表被修改了. 问题解决: 1.(临时解决)开机时,按ESC或ENTER键取消. 2.(彻底解决)修改注册表文件. Win + R 打开运行 Regedit ,进入注册 ...

  8. windows目录create、isExsit、remove

    RemoveDirectoryA( __in LPCSTR lpPathName ); PathFileExistsA(LPCSTR pszPath); CreateDirectoryA(strDir ...

  9. windows 基于命令行制作vhd虚拟磁盘

    什么是VHD? VHD是Virtual Hard Disk的简称,就是虚拟硬盘,就是能把VHD文件直接虚拟成一个硬盘,在其中能像真实硬盘一样操作,读取.写入.创建分区.格式化.如果你用过虚拟机,就会知 ...

随机推荐

  1. vi文本编辑器的学习

    vi文本编辑器的启动与退出 启动:快捷键Ctrl+Alt+t进入终端, 在系统提示符($或#)的提示下,输入vi <文件名称>,可以自动载入你要编辑的文件或者新建一个文件. 退出:在指令模 ...

  2. Vertica的这些事(五)——-谈谈vertica的flex-table

    Json格式对于现在所有的软件开发者都不陌生,很多数据格式都用他来存储,我们来看一下vertica是怎么处理json数据的.这就是vertica的flex table! 首先创建一个json文件: { ...

  3. 关于MySQL Notifiter的简单使用

    MySQL Notifiter是MySQL 数据库的辅助工具. 1.打开MySQL Notifiter Command Line Client,输入密码:123456(这是我自己的) 2.创建一个名为 ...

  4. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  5. Nordic nRF52820超低功耗蓝牙5.2 SoC芯片-低端无线连接方案首选

    nRF52820是功耗超低的低功耗蓝牙 (Bluetooth Low Energy /Bluetooth LE).蓝牙mesh.Thread.Zigbee和2.4 GHz专有低端无线连接解决方案.nR ...

  6. Jmeter 压力测试笔记(3)--脚本调试/签名/cookie/提升吞吐量/降低异常率/提升单机并发性能

    import XXXsign.Openapi2sign;---导入jar包中的签名方法 String str1 = "12121"; ---需要被签名的字段:向开发了解需要哪些哪些 ...

  7. [Python] 字符串加密解密

    1. 最简单的方法是用base64: import base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1 ...

  8. MODIS系列之NDVI(MOD13Q1)一:数据下载(一)基于插件

    引言: 写MODIS数据处理这个系列文章的初衷,主要是为了分享本人处理MODIS数据方面的一些经验.鉴于网上对这方面系统性的总结还比较少,我搜集资料时也是走了许多的弯路,因此希望通过此文让初学者能够更 ...

  9. 中阶d03 JDBC 使用

    1.首先在数据库中创建表 2.安装mysql驱动 java开发环境中导入jdbc连接mysql的jar包 mysql-connector-java-5.1.7-bin.jar 下载地址:https:/ ...

  10. poj3585 Accumulation Degree(换根dp)

    传送门 换根dp板子题(板子型选手 题意: 一棵树确定源点和汇点找到最大的流量(拿出一整套最大瘤板子orz ; int head[maxn],tot; struct node { int nt,to; ...