CreateVirtualDisk API?
-
Tuesday, June 28, 2011 3:35 PM
Not strictly a Hyper-V question but definitely a virtual one.
I am battling with the virtual disk API, CreateVirtualDisk() in particular, on Windows 7x64Ultimate using VS2008SP1 and native C++.
The idea is to use a VSS snapshot as the source for a new disk - I'm trying to duplicate some of the functionality found in the SysInternals Disk2VHD app which lets you sanp from a live, bootable, volume. Please see code below. The VSS snap (wrapped in VSSCopierW7 object) the completes sucessfull. The app is running with elevated privileges- whatever parameters I try (as suggested in the API docs) the error code is always ERROR_INVALID_PARAMETER. What I am actually trying to do is create a dynamic VHD that allocates only as much storage as it needs for my bootable C drive, so it can be mounted in Virtual PC or attached to be a bootable virtual drive.
Thx++
Jerry
P.S.
m_options.drive == "C:\\";
// destructor handles cleanup of shadow volume Chordia::VSSCopier* pCopier = new Chordia::VSSCopierW7(m_options.drive,m_options.drive); // handles destruction and thus closure of VSS std::auto_ptr<Chordia::VSSCopier> copy_ptr(pCopier); // this handles VSS creation etc copy_ptr->Copy(); // get the fully qualified UNC name for the snapshot string_t root = copy_ptr->root_name(); // set up for VirtDisk calls VIRTUAL_STORAGE_TYPE storageType = { VIRTUAL_STORAGE_TYPE_DEVICE_VHD, VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT }; 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 = { CREATE_VIRTUAL_DISK_VERSION_1 }; // parameters.Version1.UniqueId = GUID_NULL; parameters.Version1.MaximumSize = 0x40000000; // 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; // our new disk handle HANDLE handle = 0;
// call
dwRet = CreateVirtualDisk(&storageType,_T("t:\\test.vhd"),vdam,NULL,flags,0,¶meters,0,&handle); // dump debug DBMSG2(_T("CreateVirtualDisk: 0x") << Chordia::toHex(dwRet) << " VSS snapshot: " << root); if (dwRet != ERROR_SUCCESS) { Chordia::CSysError se(dwRet); if (dwRet == ERROR_INVALID_PARAMETER) { int i = 0; } DBMSG2("Error: " << se.data()); }
All Replies
-
Wednesday, June 29, 2011 8:16 AMModerator
Hi,
The following blogs may be not what you want, but they are maybe helpful for you.
Hyper-V: Scripting Fixed VHD Creation
http://blogs.msdn.com/b/virtual_pc_guy/archive/2008/09/30/hyper-v-scripting-fixed-vhd-creation.aspx
Hyper-V: Scripting Dynamic VHD Creation
Vincent Hu
-
Wednesday, June 29, 2011 11:54 AMThanks Vincent but does not help.

