[vbox-dev] Create RAW vmdk using API only

Ribhi Kamal rbhkamal at gmail.com
Tue May 7 18:54:44 GMT 2013


It there any way to implement creating RAW vmdk images using the vbox API
only? At the moment I have the following code that attempts to create a raw
VMDK disk from a raw image file. The resulting vmdk image is useless, no
partitions visible to the guest and wrong file system size.

My next step is to strip down CmdCreateRawVMDK from vboxmange code and use
it directly. However, I really don't want to do that because that's what I
was doing originally.... using vboxmanage's code in a DLL (I know now it is
bad)

My code below.

Thanks,
Ribhi

HRESULT CVboxMgr::CreateHDFromRaw(LPCTSTR rawImage, LPCTSTR diskFile)
{
HRESULT rc = NO_ERROR;
if (!m_bInit) {
_tprintf(_T("CreateHDFromRaw: not initialized!\n"));
 rc = -1;
}

// Validate the parameters
 if (rc == NO_ERROR)
if (rawImage == NULL) {
_ftprintf_s(m_pOutput, _T("CreateHDFromRaw: Incorrect parameter
(rawImage=NULL)\n"));
 rc = -2;
}

if (rc == NO_ERROR)
 if (diskFile == NULL) {
_ftprintf_s(m_pOutput, _T("CreateHDFromRaw: Incorrect parameter
(diskFile=NULL)\n"));
 rc = -3;
}

// Do an early return for parameter/initialization failures
 if (rc != NO_ERROR) {
return rc;
}

 _ftprintf_s(m_pOutput, _T("CreateHDFromRaw: Converting raw image '%s' to
'%s'\n"), rawImage, diskFile);

// Before anything get all the information that we need from the raw image
 LARGE_INTEGER nFSize;
HANDLE hRaw = NULL;
 hRaw = CreateFile( rawImage,
 FILE_GENERIC_READ,
FILE_SHARE_READ,
NULL,
 OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
 if (hRaw == INVALID_HANDLE_VALUE) {
_ftprintf_s(m_pOutput, _T("CreateHDFromRaw: Failed to open the raw image.
%s\n"),  GetLastErrorString().GetBuffer());
 rc = -4;
} else {
if (GetFileSizeEx(hRaw, &nFSize) == 0) {
 _ftprintf_s(m_pOutput, _T("ConvertFromRaw: Failed to get file size of raw
image %s\n"), GetLastErrorString().GetBuffer());
rc = -5;
 }
}

IMedium * pMedium = NULL;
 IProgress * pProgress = NULL;
 BSTR bsType = SysAllocString(_T("vmdk"));
 BSTR bsDst = SysAllocString(diskFile);

if (rc == NO_ERROR) {
rc = m_VirtualBox->CreateHardDisk(bsType, bsDst, &pMedium);
 if (rc != NO_ERROR) {
_ftprintf_s(m_pOutput, _T("ConvertFromRaw: CreateHardDisk failed %s\n"),
GetLastErrorString(rc).GetBuffer());
 }
}
 if (rc == NO_ERROR) {
 _ftprintf_s(m_pOutput, _T("ConvertFromRaw: Creating base storage with size
%ld\n"), nFSize.QuadPart);
rc = pMedium->CreateBaseStorage(nFSize.QuadPart, MediumVariant_VmdkRawDisk,
&pProgress);
 if (rc != NO_ERROR) {
_ftprintf_s(m_pOutput, _T("CreateBaseStorage failed %s\n"),
GetLastErrorString(rc).GetBuffer());
 }
}

if (rc == NO_ERROR) {
pProgress->WaitForCompletion(0);
 }

SAFE_RELEASE(pProgress);
SAFE_RELEASE(pMedium);

if (bsType)
SysFreeString(bsType);

if (bsDst)
 SysFreeString(bsDst);
 return rc;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.virtualbox.org/pipermail/vbox-dev/attachments/20130507/6aa8a48c/attachment.html>


More information about the vbox-dev mailing list