ApplianceImplImport.cpp revision 3a1349858045a27f445c3647a586cd14a4e1254d
/* $Id$ */
/** @file
* IAppliance and IVirtualSystem COM class implementations.
*/
/*
* Copyright (C) 2008-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <iprt/manifest.h>
#include "ApplianceImpl.h"
#include "VirtualBoxImpl.h"
#include "GuestOSTypeImpl.h"
#include "ProgressImpl.h"
#include "MachineImpl.h"
#include "MediumImpl.h"
#include "MediumFormatImpl.h"
#include "SystemPropertiesImpl.h"
#include "HostImpl.h"
#include "AutoCaller.h"
#include "Logging.h"
#include "ApplianceImplPrivate.h"
#include <VBox/settings.h>
#include <iprt/x509-branch-collision.h>
#include <set>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
//
// IAppliance public methods
//
////////////////////////////////////////////////////////////////////////////////
/**
* Public method implementation. This opens the OVF with ovfreader.cpp.
* Thread implementation is in Appliance::readImpl().
*
* @param aFile
* @return
*/
{
if (!i_isApplianceIdle())
return E_ACCESSDENIED;
if (m->pReader)
{
delete m->pReader;
}
return setError(VBOX_E_FILE_ERROR,
tr("Appliance file must have .ovf extension"));
try
{
/* Parse all necessary info out of the URI */
}
{
}
/* Return progress to the caller */
return S_OK;
}
/**
* Public method implementation. This looks at the output of ovfreader.cpp and creates
* VirtualSystemDescription instances.
* @return
*/
{
// @todo:
// - don't use COM methods but the methods directly (faster, but needs appropriate
// locking of that objects itself (s. HardDisk))
// - Appropriate handle errors like not supported file formats
if (!i_isApplianceIdle())
return E_ACCESSDENIED;
/* Clear any previous virtual system descriptions */
if (!m->pReader)
tr("Cannot interpret appliance without reading it first (call read() before interpret())"));
// Change the appliance state so we can safely leave the lock while doing time-consuming
// disk imports; also the below method calls do all kinds of locking which conflicts with
// the appliance object lock
try
{
/* Iterate through all virtual systems */
++it)
{
// if the virtual system in OVF had a <vbox:Machine> element, have the
// VirtualBox settings code parse that XML now
if (vsysThis.pelmVBoxMachine)
// Guest OS type
// This is taken from one of three places, in this order:
// 1) If there is a <vbox:Machine>, then use the type from there.
if ( vsysThis.pelmVBoxMachine
)
// 2) Otherwise, if there is OperatingSystemSection/vbox:OSType, use that one.
// 3) Otherwise, make a best guess what the vbox type is from the OVF (CIM) OS type.
else
"",
/* VM name */
/* If there is a <vbox:Machine>, we always prefer the setting from there. */
if ( vsysThis.pelmVBoxMachine
else
/* If there isn't any name specified create a default one out
* of the OS type */
"",
nameVBox);
/* Based on the VM name, create a target machine path. */
NULL /* aGroup */,
NULL /* aCreateFlags */,
NULL /* aBaseFolder */,
/* Determine the machine folder from that */
/* VM Product */
"",
/* VM Vendor */
"",
/* VM Version */
"",
/* VM ProductUrl */
"",
/* VM VendorUrl */
"",
/* VM description */
"",
/* VM license */
"",
/* Now that we know the OS type, get our internal defaults based on that. */
/* CPU count */
/* If there is a <vbox:Machine>, we always prefer the setting from there. */
if ( vsysThis.pelmVBoxMachine
else
/* Check for the constraints */
{
i_addWarning(tr("The virtual system \"%s\" claims support for %u CPU's, but VirtualBox has support for "
"max %u CPU's only."),
}
cpuCountVBox = 1;
"",
/* RAM */
/* If there is a <vbox:Machine>, we always prefer the setting from there. */
if ( vsysThis.pelmVBoxMachine
else
/* Check for the constraints */
if ( ullMemSizeVBox != 0
&& ( ullMemSizeVBox < MM_RAM_MIN_IN_MB
)
)
{
i_addWarning(tr("The virtual system \"%s\" claims support for %llu MB RAM size, but VirtualBox has "
"support for min %u & max %u MB RAM size only."),
}
if (vsysThis.ullMemorySize == 0)
{
/* If the RAM of the OVF is zero, use our predefined values */
/* VBox stores that in MByte */
}
"",
/* Audio */
/* If there is a <vbox:Machine>, we always prefer the setting from there. */
if ( vsysThis.pelmVBoxMachine
{
}
{
/* Set the AC97 always for the simple OVF case.
* @todo: figure out the hardware which could be possible */
}
if (strSoundCard.isNotEmpty())
"",
#ifdef VBOX_WITH_USB
/* USB Controller */
/* If there is a <vbox:Machine>, we always prefer the setting from there. */
if ( ( vsysThis.pelmVBoxMachine
#endif /* VBOX_WITH_USB */
/* Network Controller */
/* If there is a <vbox:Machine>, we always prefer the setting from there. */
if (vsysThis.pelmVBoxMachine)
{
uint32_t maxNetworkAdapters = Global::getMaxNetworkAdapters(pNewDesc->m->pConfig->hardwareMachine.chipsetType);
const settings::NetworkAdaptersList &llNetworkAdapters = pNewDesc->m->pConfig->hardwareMachine.llNetworkAdapters;
/* Check for the constrains */
i_addWarning(tr("The virtual system \"%s\" claims support for %zu network adapters, but VirtualBox "
"has support for max %u network adapter only."),
/* Iterate through all network adapters. */
size_t a = 0;
++it1, ++a)
{
{
"", // ref
strMode, // orig
0,
}
}
}
/* else we use the ovf configuration. */
{
/* Check for the constrains */
i_addWarning(tr("The virtual system \"%s\" claims support for %zu network adapters, but VirtualBox "
"has support for max %u network adapter only."),
/* Get the default network adapter type for the selected guest OS */
/* Iterate through all abstract networks. Ignore network cards
* which exceed the limit of VirtualBox. */
size_t a = 0;
++itEA, ++a)
{
// make sure it's one of these two
)
/* Figure out the hardware type */
{
/* If the default adapter is already one of the two
* PCNet adapters use the default one. If not use the
* Am79C970A as fallback. */
if (!(defaultAdapterVBox == NetworkAdapterType_Am79C970A ||
}
#ifdef VBOX_WITH_E1000
/* VMWare accidentally write this with VirtualCenter 3.5,
so make sure in this case always to use the VMWare one */
{
/* Check if this OVF was written by VirtualBox */
{
/* If the default adapter is already one of the three
* E1000 adapters use the default one. If not use the
* I82545EM as fallback. */
if (!(defaultAdapterVBox == NetworkAdapterType_I82540EM ||
}
else
/* Always use this one since it's what VMware uses */
}
#endif /* VBOX_WITH_E1000 */
"", // ref
0,
}
}
/* If there is a <vbox:Machine>, we always prefer the setting from there. */
bool fFloppy = false;
bool fDVD = false;
if (vsysThis.pelmVBoxMachine)
{
settings::StorageControllersList &llControllers = pNewDesc->m->pConfig->storageMachine.llStorageControllers;
++it3)
{
++it4)
{
break;
}
break;
}
}
else
{
}
/* Floppy Drive */
if (fFloppy)
/* CD Drive */
if (fDVD)
/* Hard disk Controller */
/* Iterate through all hard disk controllers */
++hdcIt)
{
{
/* Check for the constrains */
if (cIDEused < 4)
{
// @todo: figure out the IDE types
/* Use PIIX4 as default */
strType = "PIIX3";
strType = "ICH6";
strControllerID, // strRef
strType); // aVBoxValue
}
else
/* Warn only once */
if (cIDEused == 2)
"IDE controller channels, but VirtualBox supports only two."),
++cIDEused;
break;
/* Check for the constrains */
if (cSATAused < 1)
{
// @todo: figure out the SATA types
/* We only support a plain AHCI controller, so use them always */
"AHCI");
}
else
{
/* Warn only once */
if (cSATAused == 1)
"SATA controller, but VirtualBox has support for only one"),
}
++cSATAused;
break;
/* Check for the constrains */
if (cSCSIused < 1)
{
{
// OVF considers SAS a variant of SCSI but VirtualBox considers it a class of its own
hdcController = "LsiLogicSas";
}
hdcController = "BusLogic";
}
else
"SCSI controller of type \"%s\" with ID %s, but VirtualBox presently "
"supports only one SCSI controller."),
strControllerID.c_str());
++cSCSIused;
break;
}
}
/* Hard disks */
{
/* Iterate through all hard disks ()*/
++itVD)
{
/* Get the associated disk image */
continue;
else
{
}
/*
* Figure out from URI which format the image of disk has.
* URI must have inside section <Disk> .
* But there aren't strong requirements about correspondence one URI for one disk virtual format.
* So possibly, we aren't able to recognize some URIs.
*/
throw rc;
throw rc;
// @todo:
// - figure out all possible vmdk formats we also support
// - figure out if there is a url specifier for vhd already
// - we need a url specifier for the vdi format
{
/* If the href is empty use the VM name as filename */
if (!strFilename.length())
/* find the description for the hard disk controller
* that has the same ID as hd.idController */
tr("Cannot find hard disk controller with OVF instance ID %RI32 "
"to which disk \"%s\" should be attached"),
/* controller to attach to, and the bus within that controller */
}
{
/* If the href is empty use the VM name as filename */
if (!strFilename.length())
/* find the description for the hard disk controller
* that has the same ID as hd.idController */
tr("Cannot find disk controller with OVF instance ID %RI32 "
"to which disk \"%s\" should be attached"),
/* controller to attach to, and the bus within that controller */
}
else
throw setError(VBOX_E_FILE_ERROR,
tr("Unsupported format for virtual disk image %s in OVF: \"%s\""),
}
}
}
}
{
/* On error we clear the list & return */
}
// reset the appliance state
return rc;
}
/**
* Public method implementation. This creates one or more new machines according to the
* VirtualSystemScription instances created by Appliance::Interpret().
* Thread implementation is in Appliance::i_importImpl().
* @param aProgress
* @return
*/
{
{
{
}
}
AssertReturn(!(m->optListImport.contains(ImportOptions_KeepAllMACs) && m->optListImport.contains(ImportOptions_KeepNATMACs)), E_INVALIDARG);
// do not allow entering this method if the appliance is busy reading or writing
if (!i_isApplianceIdle())
return E_ACCESSDENIED;
if (!m->pReader)
tr("Cannot import machines without reading it first (call read() before i_importMachines())"));
try
{
}
{
}
/* Return progress to the caller */
return rc;
}
////////////////////////////////////////////////////////////////////////////////
//
// Appliance private methods
//
////////////////////////////////////////////////////////////////////////////////
{
const char *pszFilename;
if (RT_SUCCESS(vrc))
{
{
return S_OK;
}
throw setError(VBOX_E_FILE_ERROR, tr("Empty directory folder (%s) isn't allowed in the OVA package (%Rrc)"),
}
throw setError(VBOX_E_FILE_ERROR, tr("Could not open the current file in the OVA package (%Rrc)"), vrc);
}
/*******************************************************************************
* Read stuff
******************************************************************************/
/**
* Implementation for reading an OVF (via task).
*
* This starts a new thread which will call
* Appliance::taskThreadImportOrExport() which will then call readFS() or
* readS3(). This will then open the OVF with ovfreader.cpp.
*
* This is in a separate private method because it is used from three locations:
*
* 1) from the public Appliance::Read().
*
* 2) in a second worker thread; in that case, Appliance::ImportMachines() called Appliance::i_importImpl(), which
* called Appliance::readFSOVA(), which called Appliance::i_importImpl(), which then called this again.
*
* 3) from Appliance::readS3(), which got called from a previous instance of Appliance::taskThreadImportOrExport().
*
* @param aLocInfo The OVF location.
* @param aProgress Where to return the progress object.
* @return COM success status code. COM error codes will be thrown.
*/
{
/* Create the progress object */
/* 1 operation only */
TRUE /* aCancelable */);
else
/* 4/5 is downloading, 1/5 is reading */
TRUE /* aCancelable */,
2, // ULONG cOperations,
5, // ULONG ulTotalOperationsWeight,
4); // ULONG ulFirstOperationWeight,
/* Initialize our worker task */
/* Don't destruct on success */
return rc;
}
/**
* Actual worker code for reading an OVF from disk. This is called from Appliance::taskThreadImportOrExport()
* and therefore runs on the OVF read worker thread. This opens the OVF with ovfreader.cpp.
*
* This runs in two contexts:
*
* 1) in a first worker thread; in that case, Appliance::Read() called Appliance::readImpl();
*
* 2) in a second worker thread; in that case, Appliance::Read() called Appliance::readImpl(), which
* called Appliance::readS3(), which called Appliance::readImpl(), which then called this.
*
* @param pTask
* @return
*/
{
LogFlowFunc(("Appliance %p\n", this));
AutoCaller autoCaller(this);
else
return rc;
}
{
int vrc = VINF_SUCCESS;
PVDINTERFACEIO pShaIo = 0;
PVDINTERFACEIO pFileIo = 0;
do
{
try
{
/* Create the necessary file access interfaces. */
if (!pFileIo)
{
rc = E_OUTOFMEMORY;
break;
}
{
pShaIo = ShaCreateInterface();
if (!pShaIo)
{
rc = E_OUTOFMEMORY;
break;
}
//read the manifest file and find a type of used digest
{
void *pBuf; /** @todo r=bird: You leak this buffer! throwing stuff is evil. */
if (cbFile > maxFileSize)
throw setError(VBOX_E_FILE_ERROR,
tr("Size of the manifest file '%s' is bigger than 1Mb. Check it, please."),
if (RT_SUCCESS(vrc))
else
throw setError(VBOX_E_FILE_ERROR,
tr("Could not get size of the manifest file '%s' "),
if (RT_FAILURE(vrc))
{
if (pBuf)
throw setError(VBOX_E_FILE_ERROR,
tr("Could not read the manifest file '%s' (%Rrc)"),
}
if (pBuf)
if (RT_FAILURE(vrc))
{
throw setError(VBOX_E_FILE_ERROR,
tr("Could not verify supported digest types in the manifest file '%s' (%Rrc)"),
}
storage.fCreateDigest = true;
if (digestType == RTDIGESTTYPE_SHA256)
{
}
VDINTERFACETYPE_IO, 0, sizeof(VDINTERFACEIO),
if (RT_FAILURE(vrc))
break;
}
else
{
throw setError(VBOX_E_FILE_ERROR,
tr("Could not open the manifest file '%s' (%Rrc)"),
}
}
else
{
storage.fCreateDigest = false;
break;
}
}
{
}
}while (0);
/* Cleanup */
if (pShaIo)
if (pFileIo)
return rc;
}
{
/*
* Open the tar file and get a VD I/O interface for it.
*/
if (RT_SUCCESS(vrc))
{
/*
* Check that the first file is has an .ovf suffix.
*/
const char *pszName;
if (RT_SUCCESS(vrc))
{
if ( cchName >= sizeof(".ovf")
{
/*
* Stack the rest of the expected VD I/O stuff.
*/
if (pShaIo)
{
if (RT_SUCCESS(vrc))
/*
* Read and parse the OVF.
*/
else
}
else
hrc = E_OUTOFMEMORY;
}
else
tr("First file in the OVA package must have the extension 'ovf'. But the file '%s' has a different extension."),
pszName);
}
else
hrc = setError(VBOX_E_FILE_ERROR, tr("Error reading OVA file '%s' (%Rrc)"), pTask->locInfo.strPath.c_str(), vrc);
}
else
hrc = setError(VBOX_E_FILE_ERROR, tr("Could not open the OVA file '%s' (%Rrc)"), pTask->locInfo.strPath.c_str(), vrc);
return hrc;
}
HRESULT Appliance::i_readFSImpl(TaskOVF *pTask, const RTCString &strFilename, PVDINTERFACEIO pIfIo, PSHASTORAGE pStorage)
{
pStorage->fCreateDigest = true;
void *pvTmpBuf = 0;
try
{
/* Read the OVF into a memory buffer */
if (RT_FAILURE(vrc)
|| !pvTmpBuf)
throw setError(VBOX_E_FILE_ERROR,
tr("Could not read OVF file '%s' (%Rrc)"),
/* Read & parse the XML structure of the OVF file */
{
m->fSha256 = true;
char *pszDigest;
if (RT_FAILURE(vrc))
if (RT_SUCCESS(vrc))
/* Copy the SHA256 sum of the OVF file for later validation */
m->strOVFSHADigest = pszDigest;
else
throw setError(VBOX_E_FILE_ERROR, tr("Converting SHA256 digest to a string was failed (%Rrc)"), vrc);
}
else
{
m->fSha256 = false;
/* Copy the SHA1 sum of the OVF file for later validation */
}
}
catch (RTCError &x) // includes all XML exceptions
{
x.what());
}
{
}
/* Cleanup */
if (pvTmpBuf)
return rc;
}
#ifdef VBOX_WITH_S3
/**
* Worker code for reading OVF from the cloud. This is called from Appliance::taskThreadImportOrExport()
* in S3 mode and therefore runs on the OVF read worker thread. This then starts a second worker
* thread to create temporary files (see Appliance::readFS()).
*
* @param pTask
* @return
*/
{
LogFlowFunc(("Appliance %p\n", this));
AutoCaller autoCaller(this);
int vrc = VINF_SUCCESS;
char szOSTmpDir[RTPATH_MAX];
/* The template for the temporary directory created below */
try
{
/* Extract the bucket */
/* We need a temporary directory which we can put the OVF file & all
* disk images in */
if (RT_FAILURE(vrc))
throw setError(VBOX_E_FILE_ERROR,
/* The temporary name of the target OVF file */
/* Next we have to download the OVF */
"virtualbox-agent/" VBOX_VERSION_STRING);
if (RT_FAILURE(vrc))
throw setError(VBOX_E_IPRT_ERROR,
tr("Cannot create S3 service handler"));
/* Get it */
if (RT_FAILURE(vrc))
{
if (vrc == VERR_S3_CANCELED)
throw S_OK; /* todo: !!!!!!!!!!!!! */
else if (vrc == VERR_S3_ACCESS_DENIED)
throw setError(E_ACCESSDENIED,
tr("Cannot download file '%s' from S3 storage server (Access denied). Make sure that "
"your credentials are right. "
"Also check that your host clock is properly synced"),
else if (vrc == VERR_S3_NOT_FOUND)
throw setError(VBOX_E_FILE_ERROR,
else
throw setError(VBOX_E_IPRT_ERROR,
}
/* Close the connection early */
/* Prepare the temporary reading of the OVF */
/* Start the reading from the fs */
/* Unlock the appliance for the reading thread */
/* Wait until the reading is done, but report the progress back to the
caller */
/* Again lock the appliance for the next steps */
}
{
}
/* Cleanup */
/* Delete all files which where temporary created */
{
if (RT_FAILURE(vrc))
}
/* Delete the temporary directory */
if (RTPathExists(pszTmpDir))
{
if (RT_FAILURE(vrc))
}
if (pszTmpDir)
return rc;
}
#endif /* VBOX_WITH_S3 */
/*******************************************************************************
* Import stuff
******************************************************************************/
/**
* Implementation for importing OVF data into VirtualBox. This starts a new thread which will call
* Appliance::taskThreadImportOrExport().
*
* This creates one or more new machines according to the VirtualSystemScription instances created by
* Appliance::Interpret().
*
* This is in a separate private method because it is used from two locations:
*
* 1) from the public Appliance::ImportMachines().
* 2) from Appliance::i_importS3(), which got called from a previous instance of Appliance::taskThreadImportOrExport().
*
* @param aLocInfo
* @param aProgress
* @return
*/
{
mode = ImportFile;
else
mode);
/* Initialize our worker task */
/* Don't destruct on success */
return rc;
}
/**
* Actual worker code for importing OVF data into VirtualBox.
*
* This is called from Appliance::taskThreadImportOrExport() and therefore runs
* on the OVF import worker thread. This creates one or more new machines
* according to the VirtualSystemScription instances created by
* Appliance::Interpret().
*
* This runs in three contexts:
*
* 1) in a first worker thread; in that case, Appliance::ImportMachines() called Appliance::i_importImpl();
*
* 2) in a second worker thread; in that case, Appliance::ImportMachines() called Appliance::i_importImpl(), which
* called Appliance::i_i_importFSOVA(), which called Appliance::i_importImpl(), which then called this again.
*
* 3) in a second worker thread; in that case, Appliance::ImportMachines() called Appliance::i_importImpl(), which
* called Appliance::i_importS3(), which called Appliance::i_importImpl(), which then called this again.
*
* @param pTask The OVF task data.
* @return COM status code.
*/
{
LogFlowFunc(("Appliance %p\n", this));
/* Change the appliance state so we can safely leave the lock while doing
* time-consuming disk imports; also the below method calls do all kinds of
* locking which conflicts with the appliance object lock. */
/* Check if the appliance is currently busy. */
if (!i_isApplianceIdle())
return E_ACCESSDENIED;
/* Set the internal state to importing. */
/* Clear the list of imported machines, if any */
m->llGuidsMachinesCreated.clear();
else
{
/* With _whatever_ error we've had, do a complete roll-back of
* machines and disks we've created */
++itID)
{
{
rc2 = failedMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly, ComSafeArrayAsOutParam(aMedia));
}
}
}
/* Reset the state so others can call methods again */
return rc;
}
{
/* Create the import stack for the rollback on errors. */
try
{
/* Create the necessary file access interfaces. */
if (!pFileIo)
throw setError(E_OUTOFMEMORY);
VDINTERFACETYPE_IO, 0, sizeof(VDINTERFACEIO),
if (RT_FAILURE(vrc))
{
pShaIo = ShaCreateInterface();
if (!pShaIo)
throw setError(E_OUTOFMEMORY);
/* Fill out interface descriptor. */
storage.fCreateDigest = true;
/* Now import the appliance. */
/* Read & verify the manifest file. */
/* Add the ovf file to the digest list. */
size_t cbCertFile = 0;
/* Save the SHA digest of the manifest file for the next validation */
{
/* verify Certificate */
}
}
else
{
storage.fCreateDigest = false;
}
}
{
/*
* Restoring original UUID from OVF description file.
* During import VBox creates new UUIDs for imported images and
* assigns them to the images. In case of failure we have to restore
* the original UUIDs because those new UUIDs are obsolete now and
* won't be used anymore.
*/
{
/* Iterate through all virtual systems of that appliance */
++itvsd)
{
}
}
}
/* Cleanup */
if (pvMfBuf)
if (pvCertBuf)
if (pShaIo)
if (pFileIo)
return rc;
}
{
/*
* Open the OVA (TAR) file.
*/
if (RT_FAILURE(vrc))
return setError(VBOX_E_FILE_ERROR,
tr("Could not open OVA file '%s' (%Rrc)"),
PVDINTERFACEIO pShaIo = 0;
void *pSignatureRSA = NULL;
/* Create the import stack for the rollback on errors. */
try
{
/* Create the necessary file access interfaces. */
pShaIo = ShaCreateInterface();
if (!pShaIo)
throw setError(E_OUTOFMEMORY);
if (RT_FAILURE(vrc))
throw setError(VBOX_E_IPRT_ERROR,
/* Fill out interface descriptor. */
/*
* File #1 - the .ova file.
*
* Read the name of the first file. This is how all internal files
* are named.
*/
const char *pszFilename;
if (RT_FAILURE(vrc))
throw setError(VBOX_E_IPRT_ERROR,
if (vrc == VINF_TAR_DIR_PATH)
throw setError(VBOX_E_FILE_ERROR,
tr("Empty directory folder (%s) isn't allowed in the OVA package (%Rrc)"),
pszFilename, vrc);
/* save original OVF filename */
/* Skip the OVF file, cause this was read in IAppliance::Read already. */
if (RT_SUCCESS(vrc))
if ( RT_FAILURE(vrc)
/* We always need to create the digest, cause we don't know if there
* is a manifest file in the stream. */
pStorage->fCreateDigest = true;
/*
* File #2 - the manifest file (.mf), optional.
*
* Note: This isn't fatal if the file is not found. The standard
* defines 3 cases:
* 1. no manifest file
* 2. manifest file after the OVF file
* 3. manifest file after all disk files
*
* If we want streaming capabilities, we can't check if it is there by
* searching for it. We have to try to open it on all possible places.
* If it fails here, we will try it again after all disks where read.
*/
throw rc;
/*
* File #3 - certificate file (.cer), optional.
*
* Logic is the same as with manifest file. This only makes sense if
* there is a manifest file.
*/
size_t cbCertFile = 0;
if (RT_SUCCESS(vrc))
{
if (pvMfBuf)
{
{
if (pvCertBuf)
{
/* verify the certificate */
}
}
}
}
/*
* Now import the appliance.
*/
/*
* The certificate and mainifest files may alternatively be stored
* after the disk files, so look again if we didn't find them already.
*/
if (!pvMfBuf)
{
/*
* File #N-1 - The manifest file, optional.
*/
/* If we were able to read a manifest file we can check it now. */
if (pvMfBuf)
{
/* Add the ovf file to the digest list. */
/*
* File #N - The certificate file, optional.
* (Requires mainfest, as mention before.)
*/
if (RT_SUCCESS(vrc))
{
{
if (pvCertBuf)
{
/* verify the certificate */
}
}
}
}
}
/** @todo else: Verify the manifest! */
}
{
/*
* Restoring original UUID from OVF description file.
* During import VBox creates new UUIDs for imported images and
* assigns them to the images. In case of failure we have to restore
* the original UUIDs because those new UUIDs are obsolete now and
* won't be used anymore.
*/
/* Iterate through all virtual systems of that appliance */
++itvsd)
{
}
}
/* Cleanup */
if (pvMfBuf)
if (pShaIo)
if (pvCertBuf)
return rc;
}
#ifdef VBOX_WITH_S3
/**
* Worker code for importing OVF from the cloud. This is called from Appliance::taskThreadImportOrExport()
* in S3 mode and therefore runs on the OVF import worker thread. This then starts a second worker
* thread to import from temporary files (see Appliance::i_importFS()).
* @param pTask
* @return
*/
{
LogFlowFunc(("Appliance %p\n", this));
int vrc = VINF_SUCCESS;
char szOSTmpDir[RTPATH_MAX];
/* The template for the temporary directory created below */
try
{
/* Extract the bucket */
/* We need a temporary directory which we can put the all disk images
* in */
if (RT_FAILURE(vrc))
throw setError(VBOX_E_FILE_ERROR,
/* Add every disks of every virtual system to an internal list */
++it)
{
std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskImage);
++itH)
{
if (!strTargetFile.isEmpty())
{
/* The temporary name of the target disk file */
}
}
}
/* Next we have to download the disk images */
"virtualbox-agent/" VBOX_VERSION_STRING);
if (RT_FAILURE(vrc))
throw setError(VBOX_E_IPRT_ERROR,
tr("Cannot create S3 service handler"));
/* Download all files */
for (list< pair<Utf8Str, ULONG> >::const_iterator it1 = filesList.begin(); it1 != filesList.end(); ++it1)
{
/* Construct the source file name */
/* Advance to the next operation */
pTask->pProgress->SetNextOperation(BstrFmt(tr("Downloading file '%s'"), pszFilename).raw(), s.second);
if (RT_FAILURE(vrc))
{
if (vrc == VERR_S3_CANCELED)
throw S_OK; /* todo: !!!!!!!!!!!!! */
else if (vrc == VERR_S3_ACCESS_DENIED)
throw setError(E_ACCESSDENIED,
tr("Cannot download file '%s' from S3 storage server (Access denied). "
"Make sure that your credentials are right. Also check that your host clock is "
"properly synced"),
else if (vrc == VERR_S3_NOT_FOUND)
throw setError(VBOX_E_FILE_ERROR,
tr("Cannot download file '%s' from S3 storage server (File not found)"),
else
throw setError(VBOX_E_IPRT_ERROR,
tr("Cannot download file '%s' from S3 storage server (%Rrc)"),
pszFilename, vrc);
}
}
/* Provide a OVF file (haven't to exist) so the import routine can
/* Now check if there is an manifest file. This is optional. */
// Utf8Str strManifestFile = queryManifestFileName(strTmpOvf);
/* Try to download it. If the error is VERR_S3_NOT_FOUND, it isn't fatal. */
if (RT_SUCCESS(vrc))
else if (RT_FAILURE(vrc))
{
if (vrc == VERR_S3_CANCELED)
throw S_OK; /* todo: !!!!!!!!!!!!! */
else if (vrc == VERR_S3_NOT_FOUND)
else if (vrc == VERR_S3_ACCESS_DENIED)
throw setError(E_ACCESSDENIED,
tr("Cannot download file '%s' from S3 storage server (Access denied)."
"Make sure that your credentials are right. "
"Also check that your host clock is properly synced"),
else
throw setError(VBOX_E_IPRT_ERROR,
tr("Cannot download file '%s' from S3 storage server (%Rrc)"),
pszFilename, vrc);
}
/* Close the connection early */
pTask->pProgress->SetNextOperation(BstrFmt(tr("Importing appliance")).raw(), m->ulWeightForXmlOperation);
/* Import the whole temporary OVF & the disk images */
/* Unlock the appliance for the fs import thread */
/* Wait until the import is done, but report the progress back to the
caller */
/* Again lock the appliance for the next steps */
}
{
}
/* Cleanup */
/* Delete all files which where temporary created */
for (list< pair<Utf8Str, ULONG> >::const_iterator it1 = filesList.begin(); it1 != filesList.end(); ++it1)
{
if (RTPathExists(pszFilePath))
{
if (RT_FAILURE(vrc))
}
}
/* Delete the temporary directory */
if (RTPathExists(pszTmpDir))
{
if (RT_FAILURE(vrc))
}
if (pszTmpDir)
return rc;
}
#endif /* VBOX_WITH_S3 */
void **ppvBuf,
bool fCreateDigest,
{
if ( RT_FAILURE(vrc)
&& vrc != VERR_FILE_NOT_FOUND)
tr("Could not read file '%s' (%Rrc)"),
return rc;
}
void **ppvBuf,
bool fCreateDigest,
{
const char *pszCurFile;
if (RT_SUCCESS(vrc))
{
if (vrc != VINF_TAR_DIR_PATH)
{
}
else
tr("Empty directory folder (%s) isn't allowed in the OVA package (%Rrc)"),
pszCurFile, vrc);
}
return rc;
}
HRESULT Appliance::i_verifyManifestFile(const Utf8Str &strFile, ImportStack &stack, void *pvBuf, size_t cbSize)
{
LogFlowFunc(("Appliance %p\n", this));
PRTMANIFESTTEST paTests = (PRTMANIFESTTEST)RTMemAlloc(sizeof(RTMANIFESTTEST) * stack.llSrcDisksDigest.size());
if (!paTests)
return E_OUTOFMEMORY;
size_t i = 0;
++it1, ++i)
{
}
int vrc = RTManifestVerifyFilesBuf(pvBuf, cbSize, paTests, stack.llSrcDisksDigest.size(), &iFailed);
tr("The SHA digest of '%s' does not match the one in '%s' (%Rrc)"),
else if (RT_FAILURE(vrc))
tr("Could not verify the content of '%s' against the available files (%Rrc)"),
return rc;
}
{
LogFlowFunc(("Appliance %p\n", this));
int vrc = 0;
if (RT_FAILURE(vrc))
{
}
else
{
if (RT_SUCCESS(vrc))
{
/*
* possible step in the future. Not obligatory due to OVF2.0 standard
* OVF2.0:"A consumer of the OVF package shall verify the signature and should validate the certificate"
*/
}
/* After first unsuccessful operation */
if (RT_FAILURE(vrc))
{
{
/* first stage for getting possible error code and it's description using native openssl method */
char* errStrDesc = NULL;
if(errValue != 0)
{
LogFlowFunc(("Error during verifying X509 certificate(internal openssl description): %s\n", errStrDesc));
}
}
{
/* second stage for getting possible error code using our defined errors codes. The original error description
will be replaced by our description */
switch(vrc)
{
errStrDesc = "Error during reading a certificate in PEM format from BIO ";
break;
errStrDesc = "Error during extraction a public key from the certificate ";
break;
errStrDesc = "Error during extraction RSA from the public key ";
break;
errStrDesc = "RSA verification failure ";
break;
errStrDesc = "Basic constraints were not found ";
break;
errStrDesc = "Error during getting extensions from the certificate ";
break;
errStrDesc = "Error during extraction data from the extension ";
break;
errStrDesc = "Error during print out an extension to BIO ";
break;
errStrDesc = "X509 certificate verification failure ";
break;
default:
errStrDesc = "Unknown error during X509 certificate verification";
}
}
}
else
{
{
tr("Signature from the X509 certificate has been verified. "
"But VirtualBox can't validate the given X509 certificate. "
"Only self signed X509 certificates are supported at moment. \n"));
}
}
}
return rc;
}
/**
* Helper that converts VirtualSystem attachment values into VirtualBox attachment values.
* Throws HRESULT values on errors!
*
* @param hdc in: the HardDiskController structure to attach to.
* @param ulAddressOnParent in: the AddressOnParent parameter from OVF.
* @param controllerType out: the name of the hard disk controller to attach to (e.g. "IDE Controller").
* @param lControllerPort out: the channel (controller port) of the controller to attach to.
* @param lDevice out: the device number to attach to.
*/
{
Log(("Appliance::i_convertDiskAttachmentValues: hdc.system=%d, hdc.fPrimary=%d, ulAddressOnParent=%d\n",
{
// For the IDE bus, the port parameter can be either 0 or 1, to specify the primary
// or secondary IDE controller, respectively. For the primary controller of the IDE bus,
// the device number can be either 0 or 1, to specify the master or the slave device,
// respectively. For the secondary IDE controller, the device number is always 1 because
// the master device is reserved for the CD-ROM drive.
switch (ulAddressOnParent)
{
case 0: // master
{
// secondary master
lControllerPort = (long)1;
lDevice = (long)0;
}
else // primary master
{
lControllerPort = (long)0;
lDevice = (long)0;
}
break;
case 1: // slave
{
// secondary slave
lControllerPort = (long)1;
lDevice = (long)1;
}
else // primary slave
{
lControllerPort = (long)0;
lDevice = (long)1;
}
break;
// used by older VBox exports
case 2: // interpret this as secondary master
lControllerPort = (long)1;
lDevice = (long)0;
break;
// used by older VBox exports
case 3: // interpret this as secondary slave
lControllerPort = (long)1;
lDevice = (long)1;
break;
default:
throw setError(VBOX_E_NOT_SUPPORTED,
tr("Invalid channel %RI16 specified; IDE controllers support only 0, 1 or 2"),
break;
}
break;
lControllerPort = (long)ulAddressOnParent;
lDevice = (long)0;
break;
{
else
lControllerPort = (long)ulAddressOnParent;
lDevice = (long)0;
}
break;
default: break;
}
}
/**
* Imports one disk image. This is common code shared between
* -- i_importMachineGeneric() for the OVF case; in that case the information comes from
* the OVF virtual systems;
* -- i_importVBoxMachine(); in that case, the information comes from the <vbox:Machine>
* tag.
*
* Both ways of describing machines use the OVF disk references section, so in both cases
* the caller needs to pass in the ovf::DiskImage structure from ovfreader.cpp.
*
* As a result, in both cases, if di.strHref is empty, we create a new disk as per the OVF
* spec, even though this cannot really happen in the vbox:Machine case since such data
* would never have been exported.
*
* This advances stack.pProgress by one operation with the disk's weight.
*
* @param di ovfreader.cpp structure describing the disk image from the OVF that is to be imported
* @param strTargetPath Where to create the target image.
* @param pTargetHD out: The newly created target disk. This also gets pushed on stack.llHardDisksCreated for cleanup.
* @param stack
*/
{
static_cast<IAppliance*>(this),
TRUE);
/* Get the system properties. */
/*
* we put strSourceOVF into the stack.llSrcDisksDigest in the end of this
* function like a key for a later validation of the SHA digests
*/
/* Construct source file path */
else
{
}
/* First of all check if the path is an UUID. If so, the user like to
* import the disk into an existing path. This is useful for iSCSI for
* example. */
if (vrc == VINF_SUCCESS)
{
}
else
{
/* check read file to GZIP compression */
try
{
if (fGzipUsed == true)
{
/*
* Create the necessary file access interfaces.
* For the next step:
* We need to replace the previously created chain of SHA-TAR or SHA-FILE interfaces
* with simple FILE interface because we don't need SHA or TAR interfaces here anymore.
* But we mustn't delete the chain of SHA-TAR or SHA-FILE interfaces.
*/
/* Decompress the GZIP file and save a new file in the target path */
if (RT_FAILURE(vrc))
throw setError(VBOX_E_FILE_ERROR,
tr("Could not read the file '%s' (%Rrc)"),
/* Create the necessary file access interfaces. */
if (!pFileIo)
throw setError(E_OUTOFMEMORY);
if (RT_FAILURE(vrc))
throw setError(VBOX_E_IPRT_ERROR,
/* Correct the source and the target with the actual values */
}
{
/* Figure out which format the user like to have. Default is VMDK. */
throw setError(VBOX_E_NOT_SUPPORTED,
tr("Could not find a valid medium format for the target disk '%s'"),
strTargetPath->c_str());
/* Check the capabilities. We need create capabilities. */
lCabs = 0;
throw rc;
else
{
lCabs |= mediumFormatCap[j];
}
throw setError(VBOX_E_NOT_SUPPORTED,
tr("Could not find a valid medium format for the target disk '%s'"),
strTargetPath->c_str());
}
else
{
throw setError(VBOX_E_FILE_ERROR,
tr("The target disk '%s' has no extension "),
}
/* Create an IMedium object. */
{
try
{
if (fGzipUsed == true)
{
/*
* The source and target pathes are the same.
* It means that we have the needed file already.
* For example, in GZIP case, we decompress the file and save it in the target path,
* but with some prefix like "temp_". See part "check read file to GZIP compression" earlier
* in this function.
* Just rename the file by deleting "temp_" from it's name
*/
if (RT_FAILURE(vrc))
throw setError(VBOX_E_FILE_ERROR,
tr("Could not rename the file '%s' (%Rrc)"),
}
else
{
/* Calculating SHA digest for ISO file while copying one */
strTargetPath->c_str(),
if (RT_FAILURE(vrc))
throw setError(VBOX_E_FILE_ERROR,
tr("Could not copy ISO file '%s' listed in the OVF file (%Rrc)"),
}
}
catch (HRESULT /*arc*/)
{
throw;
}
/* Advance to the next operation. */
/* operation's weight, as set up with the IProgress originally */
}
else/* HDD case*/
{
/* Now create an empty hard disk. */
/* If strHref is empty we have to create a new file. */
if (strSourceOVF.isEmpty())
{
/* Create a dynamic growing disk image with the given capacity. */
/* Advance to the next operation. */
/* operation's weight, as set up with the IProgress originally */
}
else
{
/* We need a proper source format description */
/* Which format to use? */
throw setError(VBOX_E_NOT_SUPPORTED,
tr("Could not find a valid medium format for the source disk '%s' "
"Check correctness of the image format URL in the OVF description file "
"or extension of the image"),
/* Clone the source disk image */
/* Advance to the next operation. */
/* operation's weight, as set up with the IProgress originally */
}
/* Now wait for the background disk operation to complete; this throws
* HRESULTs on error. */
if (fGzipUsed == true)
{
/*
* Just delete the temporary file
*/
if (RT_FAILURE(vrc))
tr("Could not delete the file '%s' (%Rrc)"),
}
}
}
catch (...)
{
if (pFileIo)
throw;
}
}
if (pFileIo)
/* Add the newly create disk path + a corresponding digest the our list for
* later manifest verification. */
}
/**
* Imports one OVF virtual system (described by the given ovf::VirtualSystem and VirtualSystemDescription)
* into VirtualBox by creating an IMachine instance, which is returned.
*
* This throws HRESULT error codes for anything that goes wrong, in which case the caller must clean
* up any leftovers from this function. For this, the given ImportStack instance has received information
* about what needs cleaning up (to support rollback).
*
* @param vsysThis OVF virtual system (machine) to import.
* @param vsdescThis Matching virtual system description (machine) to import.
* @param pNewMachine out: Newly created machine.
* @param stack Cleanup stack for when this throws.
*/
{
// Get the instance of IGuestOSType which matches our string guest OS type so we
// can use recommended defaults for the new machine where OVF doesn't provide any
/* Create the machine */
NULL, /* aCreateFlags */
// set the description
{
}
// CPU count
if (stack.fForceHWVirt)
{
}
// RAM
/* VRAM */
/* Get the recommended VRAM for this guest OS type */
/* Set the VRAM */
// I/O APIC: Generic OVF has no setting for this. Enable it if we
// import a Windows VM because if if Windows was installed without IOAPIC,
// it will not mind finding an one later on, but if Windows was installed
// _with_ an IOAPIC, it will bluescreen if it's not found
if (!stack.fForceIOAPIC)
{
if (bstrFamilyId == "Windows")
stack.fForceIOAPIC = true;
}
if (stack.fForceIOAPIC)
{
}
{
}
#ifdef VBOX_WITH_USB
/* USB Controller */
if (stack.fUSBEnabled)
{
rc = pNewMachine->AddUSBController(Bstr("OHCI").raw(), USBControllerType_OHCI, usbController.asOutParam());
}
#endif /* VBOX_WITH_USB */
/* Change the network adapters */
std::list<VirtualSystemDescriptionEntry*> vsdeNW = vsdescThis->i_findByType(VirtualSystemDescriptionType_NetworkAdapter);
{
/* No network adapters, so we have to disable our default one */
}
throw setError(VBOX_E_FILE_ERROR,
tr("Too many network adapters: OVF requests %d network adapters, "
"but VirtualBox only supports %d"),
else
{
size_t a = 0;
++nwIt, ++a)
{
/* Enable the network card & set the adapter type */
// default is NAT; change to "bridged" if extra conf says so
{
/* Attach to the right interface */
// We search for the first host network interface which
// is usable for bridged networking
for (size_t j = 0;
j < nwInterfaces.size();
++j)
{
{
/* Set the interface name to attach to */
break;
}
}
}
/* Next test for host only interfaces */
{
/* Attach to the right interface */
// We search for the first host network interface which
// is usable for host only networking
for (size_t j = 0;
j < nwInterfaces.size();
++j)
{
{
/* Set the interface name to attach to */
break;
}
}
}
/* Next test for internal interfaces */
{
/* Attach to the right interface */
}
/* Next test for Generic interfaces */
{
/* Attach to the right interface */
}
/* Next test for NAT network interfaces */
{
/* Attach to the right interface */
// Pick the first NAT network (if there is any)
if (nwNATNetworks.size())
{
/* Set the NAT network name to attach to */
break;
}
}
}
}
// IDE Hard disk controller
std::list<VirtualSystemDescriptionEntry*> vsdeHDCIDE = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskControllerIDE);
/*
* In OVF (at least VMware's version of it), an IDE controller has two ports,
* so VirtualBox's single IDE controller with two channels and two ports each counts as
* two OVF IDE controllers -- so we accept one or two such IDE controllers
*/
if (cIDEControllers > 2)
throw setError(VBOX_E_FILE_ERROR,
tr("Too many IDE controllers in OVF; import facility only supports two"));
if (vsdeHDCIDE.size() > 0)
{
// one or two IDE controllers present in OVF: add one VirtualBox controller
rc = pNewMachine->AddStorageController(Bstr("IDE Controller").raw(), StorageBus_IDE, pController.asOutParam());
else
throw setError(VBOX_E_FILE_ERROR,
tr("Invalid IDE controller type \"%s\""),
}
/* Hard disk controller SATA */
std::list<VirtualSystemDescriptionEntry*> vsdeHDCSATA = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskControllerSATA);
throw setError(VBOX_E_FILE_ERROR,
tr("Too many SATA controllers in OVF; import facility only supports one"));
if (vsdeHDCSATA.size() > 0)
{
if (hdcVBox == "AHCI")
{
}
else
throw setError(VBOX_E_FILE_ERROR,
tr("Invalid SATA controller type \"%s\""),
}
/* Hard disk controller SCSI */
std::list<VirtualSystemDescriptionEntry*> vsdeHDCSCSI = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskControllerSCSI);
throw setError(VBOX_E_FILE_ERROR,
tr("Too many SCSI controllers in OVF; import facility only supports one"));
if (vsdeHDCSCSI.size() > 0)
{
if (hdcVBox == "LsiLogic")
else if (hdcVBox == "LsiLogicSas")
{
// OVF treats LsiLogicSas as a SCSI controller but VBox considers it a class of its own
bstrName = L"SAS Controller";
}
else if (hdcVBox == "BusLogic")
else
throw setError(VBOX_E_FILE_ERROR,
tr("Invalid SCSI controller type \"%s\""),
}
/* Hard disk controller SAS */
std::list<VirtualSystemDescriptionEntry*> vsdeHDCSAS = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskControllerSAS);
throw setError(VBOX_E_FILE_ERROR,
tr("Too many SAS controllers in OVF; import facility only supports one"));
if (vsdeHDCSAS.size() > 0)
{
}
/* Now its time to register the machine before we add any hard disks */
// store new machine for roll-back in case of errors
// Add floppies and CD-ROMs to the appropriate controllers.
std::list<VirtualSystemDescriptionEntry*> vsdeFloppy = vsdescThis->i_findByType(VirtualSystemDescriptionType_Floppy);
throw setError(VBOX_E_FILE_ERROR,
tr("Too many floppy controllers in OVF; import facility only supports one"));
std::list<VirtualSystemDescriptionEntry*> vsdeCDROM = vsdescThis->i_findByType(VirtualSystemDescriptionType_CDROM);
if ( (vsdeFloppy.size() > 0)
)
{
// If there's an error here we need to close the session, so
try
{
// to attach things we need to open a session for the new machine
stack.fSessionOpen = true;
// floppy first
{
// this is for rollback later
mhda.lControllerPort = 0;
Log(("Attaching floppy\n"));
NULL);
}
// only now that we're done with all disks, close the session
stack.fSessionOpen = false;
}
{
if (stack.fSessionOpen)
if (info.isFullAvailable())
else
}
}
// create the hard disks & connect them to the appropriate controllers
std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskImage);
{
// If there's an error here we need to close the session, so
try
{
#ifdef LOG_ENABLED
if (LogIsEnabled())
{
size_t i = 0;
for (list<VirtualSystemDescriptionEntry*>::const_iterator itHD = avsdeHDs.begin(); itHD != avsdeHDs.end(); ++itHD, i++)
i = 0;
for (ovf::DiskImagesMap::const_iterator itDisk = stack.mapDisks.begin(); itDisk != stack.mapDisks.end(); ++itDisk)
Log(("mapDisks[%zu]: strDiskId=%s strHref=%s\n", i, itDisk->second.strDiskId.c_str(), itDisk->second.strHref.c_str()));
}
#endif
// to attach things we need to open a session for the new machine
stack.fSessionOpen = true;
/* get VM name from virtual system description. Only one record is possible (size of list is equal 1). */
std::list<VirtualSystemDescriptionEntry*> vmName = vsdescThis->i_findByType(VirtualSystemDescriptionType_Name);
uint32_t cImportedDisks = 0;
{
Log(("diCurrent.strDiskId=%s diCurrent.strHref=%s\n", diCurrent.strDiskId.c_str(), diCurrent.strHref.c_str()));
/*
*
* Iterate over all given disk images of the virtual system
* disks description. We need to find the target disk path,
* which could be changed by the user.
*
*/
{
++itHD)
{
{
break;
}
}
if (!vsdeTargetHD)
{
/* possible case if a disk image belongs to other virtual system (OVF package with multiple VMs inside) */
LogWarning(("OVA/OVF import: Disk image %s was missed during import of VM %s\n",
++oit;
continue;
}
//diCurrent.strDiskId contains the disk identifier (e.g. "vmdisk1"), which should exist
//in the virtual system's disks map under that ID and also in the global images map
tr("Internal inconsistency looking up disk image '%s'"),
}
/*
* preliminary check availability of the image
* This step is useful if image is placed in the OVA (TAR) package
*/
{
/* It means that we possibly have imported the storage earlier on the previous loop steps*/
if (h != disksResolvedNames.end())
{
/* Yes, disk name was found, we can skip it*/
++oit;
continue;
}
{
/* current opened file isn't the same as passed one */
{
/*
* availableImage contains the disk file reference (e.g. "disk1.vmdk"), which should exist
* in the global images map.
* And find the disk from the OVF's disk list
*
*/
{
{
break;
}
{
tr("Internal inconsistency looking up disk image '%s'. "
"Check compliance OVA package structure and file names "
"references in the section <References> in the OVF file."),
availableImage.c_str());
}
/* replace with a new found disk image */
}
/*
* Again iterate over all given disk images of the virtual system
* disks description using the found disk image
*/
{
++itHD)
{
{
break;
}
}
if (!vsdeTargetHD)
{
/*
* in this case it's an error because something wrong with OVF description file.
* May be VBox imports OVA package with wrong file sequence inside the archive.
*/
tr("Internal inconsistency looking up disk image '%s'"),
}
tr("Internal inconsistency looking up disk image '%s'"),
}
}
else
{
++oit;
}
}
else
{
++oit;
continue;
}
}
else
{
/* just continue with normal files*/
++oit;
}
/* very important to store disk name for the next checks */
pStorage);
// now use the new uuid to attach the disk image to our new machine
throw rc;
// find the hard disk controller to which we should attach
// this is for rollback later
Log(("Attaching disk %s to port %d on device %d\n",
throw rc;
throw rc;
{
false,
dvdImage.asOutParam());
throw rc;
DeviceType_DVD, // DeviceType_T type
dvdImage);
throw rc;
}
else
{
DeviceType_HardDisk, // DeviceType_T type
throw rc;
}
throw rc;
/* restore */
} // end while(oit != stack.mapDisks.end())
/*
* quantity of the imported disks isn't equal to the size of the avsdeHDs list.
*/
{
LogWarning(("Not all disk images were imported for VM %s. Check OVF description file.",
}
// only now that we're done with all disks, close the session
throw rc;
stack.fSessionOpen = false;
}
{
if (stack.fSessionOpen)
if (info.isFullAvailable())
else
}
}
}
/**
* Imports one OVF virtual system (described by a vbox:Machine tag represented by the given config
* structure) into VirtualBox by creating an IMachine instance, which is returned.
*
* This throws HRESULT error codes for anything that goes wrong, in which case the caller must clean
* up any leftovers from this function. For this, the given ImportStack instance has received information
* about what needs cleaning up (to support rollback).
*
* The machine config stored in the settings::MachineConfigFile structure contains the UUIDs of
* the disk attachments used by the machine when it was exported. We also add vbox:uuid attributes
* to the OVF disks sections so we can look them up. While importing these UUIDs into a second host
* will most probably work, reimporting them into the same host will cause conflicts, so we always
* generate new ones on import. This involves the following:
*
* 1) Scan the machine config for disk attachments.
*
* 2) For each disk attachment found, look up the OVF disk image from the disk references section
* and import the disk into VirtualBox, which creates a new UUID for it. In the machine config,
* replace the old UUID with the new one.
*
* 3) Change the machine config according to the OVF virtual system descriptions, in case the
* caller has modified them using setFinalValues().
*
* 4) Create the VirtualBox machine with the modfified machine config.
*
* @param config
* @param pNewMachine
* @param stack
*/
{
/*
* step 1): modify machine config according to OVF config, in case the user
* has modified them using setFinalValues()
*/
/* OS Type */
/* Description */
/* CPU count & extented attributes */
if (stack.fForceIOAPIC)
if (stack.fForceIOAPIC)
/* RAM size */
/*
<const name="HardDiskControllerIDE" value="14" />
<const name="HardDiskControllerSATA" value="15" />
<const name="HardDiskControllerSCSI" value="16" />
<const name="HardDiskControllerSAS" value="17" />
*/
#ifdef VBOX_WITH_USB
/* USB controller */
if (stack.fUSBEnabled)
{
/** @todo r=klaus add support for arbitrary USB controller types, this can't handle multiple controllers due to its design anyway */
/* usually the OHCI controller is enabled already, need to check */
bool fOHCIEnabled = false;
settings::USBControllerList &llUSBControllers = config.hardwareMachine.usbSettings.llUSBControllers;
{
{
fOHCIEnabled = true;
break;
}
}
if (!fOHCIEnabled)
{
}
}
else
#endif
/* Audio adapter */
{
config.hardwareMachine.audioAdapter.controllerType = (AudioControllerType_T)stack.strAudioAdapter.toUInt32();
}
else
/* Network adapter */
/* First disable all network cards, they will be enabled below again. */
{
if (!( fKeepAllMACs
}
/* Now iterate over all network entries. */
std::list<VirtualSystemDescriptionEntry*> avsdeNWs = vsdescThis->i_findByType(VirtualSystemDescriptionType_NetworkAdapter);
{
/* Iterate through all network adapter entries and search for the
* corresponding one in the machine config. If one is found, configure
* it based on the user settings. */
++itNW)
{
{
/* Iterate through all network adapters in the machine config. */
++it1)
{
/* Compare the slots. */
{
break;
}
}
}
}
}
/* Floppy controller */
/* DVD controller */
/* Iterate over all storage controller check the attachments and remove
* them when necessary. Also detect broken configs with more than one
* attachment. Old VirtualBox versions (prior to 3.2.10) had all disk
* attachments pointing to the last hard disk image, which causes import
* failures. A long fixed bug, however the OVF files are long lived. */
bool fInconsistent = false;
bool fRepairDuplicate = false;
++it3)
{
{
if ( ( !fDVD
||
( !fFloppy
{
continue;
}
{
cDisks++;
if (cDisks == 1)
{
else
fInconsistent = true;
}
else
{
fInconsistent = true;
fRepairDuplicate = true;
}
}
++it4;
}
}
/* paranoia... */
fRepairDuplicate = false;
/*
* step 2: scan the machine config for media attachments
*/
/* get VM name from virtual system description. Only one record is possible (size of list is equal 1). */
std::list<VirtualSystemDescriptionEntry*> vmName = vsdescThis->i_findByType(VirtualSystemDescriptionType_Name);
/* Get all hard disk descriptions. */
std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskImage);
/* paranoia - if there is no 1:1 match do not try to repair. */
fRepairDuplicate = false;
// there must be an image in the OVF disk structs with the same UUID
uint32_t cImportedDisks = 0;
{
{
/* Iterate over all given disk images of the virtual system
* disks description. We need to find the target disk path,
* which could be changed by the user. */
++itHD)
{
{
break;
}
}
if (!vsdeTargetHD)
{
/* possible case if a disk image belongs to other virtual system (OVF package with multiple VMs inside) */
LogWarning(("OVA/OVF import: Disk image %s was missed during import of VM %s\n",
++oit;
continue;
}
}
/*
* preliminary check availability of the image
* This step is useful if image is placed in the OVA (TAR) package
*/
{
/* It means that we possibly have imported the storage earlier on the previous loop steps*/
if (h != disksResolvedNames.end())
{
/* Yes, disk name was found, we can skip it*/
++oit;
continue;
}
{
/* current opened file isn't the same as passed one */
{
// availableImage contains the disk identifier (e.g. "vmdisk1"), which should exist
// in the virtual system's disks map under that ID and also in the global images map
// and find the disk from the OVF's disk list
{
break;
}
{
tr("Internal inconsistency looking up disk image '%s'. "
"Check compliance OVA package structure and file names "
"references in the section <References> in the OVF file."),
availableImage.c_str());
}
/* replace with a new found disk image */
/*
* Again iterate over all given disk images of the virtual system
* disks description using the found disk image
*/
++itHD)
{
{
break;
}
}
if (!vsdeTargetHD)
/*
* in this case it's an error because something wrong with OVF description file.
* May be VBox imports OVA package with wrong file sequence inside the archive.
*/
tr("Internal inconsistency looking up disk image '%s'"),
}
else
{
++oit;
}
}
else
{
++oit;
continue;
}
}
else
{
/* just continue with normal files*/
++oit;
}
/* Important! to store disk name for the next checks */
// there must be an image in the OVF disk structs with the same UUID
bool fFound = false;
// for each storage controller...
for (settings::StorageControllersList::iterator sit = config.storageMachine.llStorageControllers.begin();
++sit)
{
// find the OVF virtual system description entry for this storage controller
switch (sc.storageBus)
{
case StorageBus_SATA:
break;
case StorageBus_SCSI:
break;
case StorageBus_IDE:
break;
case StorageBus_SAS:
break;
}
// for each medium attachment to this controller...
++dit)
{
// empty DVD and floppy media
continue;
// When repairing a broken VirtualBox xml config section (written
// by VirtualBox versions earlier than 3.2.10) assume the disks
// show up in the same order as in the OVF description.
if (fRepairDuplicate)
{
{
}
++avsdeHDsIt;
}
// convert the Guid to string
{
continue;
}
/*
* step 3: import disk
*/
pStorage);
throw rc;
throw rc;
{
false,
dvdImage.asOutParam());
// ... and replace the old UUID in the machine config with the one of
// the imported disk that was just created
}
else
{
// ... and replace the old UUID in the machine config with the one of
// the imported disk that was just created
}
/* restore */
/*
* 1. saving original UUID for restoring in case of failure.
* 2. replacement of original UUID by new UUID in the current VM config (settings::MachineConfigFile).
*/
{
}
fFound = true;
break;
} // for (settings::AttachedDevicesList::const_iterator dit = sc.llAttachedDevices.begin();
} // for (settings::StorageControllersList::const_iterator sit = config.storageMachine.llStorageControllers.begin();
// no disk with such a UUID found:
if (!fFound)
tr("<vbox:Machine> element in OVF contains a medium attachment for the disk image %s "
"but the OVF describes no such image"),
}// while(oit != stack.mapDisks.end())
/*
* quantity of the imported disks isn't equal to the size of the avsdeHDs list.
*/
{
LogWarning(("Not all disk images were imported for VM %s. Check OVF description file.",
}
/*
* step 4): create the machine and have it import the config
*/
// this magic constructor fills the new machine object with the MachineConfig
// instance that we created from the vbox:Machine
config); // the whole machine config
// and register it
// store new machine for roll-back in case of errors
}
{
// this is safe to access because this thread only gets started
/*
* get the SHA digest version that was set in accordance with the value of attribute "xmlns:ovf"
* of the element <Envelope> in the OVF file during reading operation. See readFSImpl().
*/
// create a session for the machine + disks we manipulate below
/* Iterate through all virtual systems of that appliance */
size_t i = 0;
{
// there are two ways in which we can create a vbox machine from OVF:
// -- either this OVF was written by vbox 3.2 or later, in which case there is a <vbox:Machine> element
// in the <VirtualSystem>; then the VirtualSystemDescription::Data has a settings::MachineConfigFile
// with all the machine config pretty-parsed;
// -- or this is an OVF from an older vbox or an external source, and then we need to translate the
// VirtualSystemDescriptionEntry and do import work
// Even for the vbox:Machine case, there are a number of configuration items that will be taken from
// the OVF because otherwise the "override import parameters" mechanism in the GUI won't work.
// VM name
std::list<VirtualSystemDescriptionEntry*> vsdeName = vsdescThis->i_findByType(VirtualSystemDescriptionType_Name);
throw setError(VBOX_E_FILE_ERROR,
tr("Missing VM name"));
// have VirtualBox suggest where the filename would be placed so we can
// put the disk images in the same directory
NULL /* aGroup */,
NULL /* aCreateFlags */,
NULL /* aBaseFolder */,
// and determine the machine folder from that
LogFunc(("i=%zu strName=%s bstrMachineFilename=%ls\n", i, stack.strNameVBox.c_str(), bstrMachineFilename.raw()));
// guest OS type
throw setError(VBOX_E_FILE_ERROR,
tr("Missing guest OS type"));
// CPU count
std::list<VirtualSystemDescriptionEntry*> vsdeCPU = vsdescThis->i_findByType(VirtualSystemDescriptionType_CPU);
// We need HWVirt & IO-APIC if more than one CPU is requested
{
stack.fForceHWVirt = true;
stack.fForceIOAPIC = true;
}
// RAM
std::list<VirtualSystemDescriptionEntry*> vsdeRAM = vsdescThis->i_findByType(VirtualSystemDescriptionType_Memory);
#ifdef VBOX_WITH_USB
// USB controller
std::list<VirtualSystemDescriptionEntry*> vsdeUSBController = vsdescThis->i_findByType(VirtualSystemDescriptionType_USBController);
// USB support is enabled if there's at least one such entry; to disable USB support,
// the type of the USB item would have been changed to "ignore"
#endif
// audio adapter
std::list<VirtualSystemDescriptionEntry*> vsdeAudioAdapter = vsdescThis->i_findByType(VirtualSystemDescriptionType_SoundCard);
/* @todo: we support one audio adapter only */
if (vsdeAudioAdapter.size() > 0)
// for the description of the new machine, always use the OVF entry, the user may have changed it in the import config
std::list<VirtualSystemDescriptionEntry*> vsdeDescription = vsdescThis->i_findByType(VirtualSystemDescriptionType_Description);
if (vsdeDescription.size())
// import vbox:machine or OVF now
if (vsdescThis->m->pConfig)
// vbox:Machine config
else
// generic OVF config
} // for (it = pAppliance->m->llVirtualSystems.begin() ...
}
{
/* save for restoring */
return rc;
}
HRESULT Appliance::ImportStack::restoreOriginalUUIDOfAttachedDevice(settings::MachineConfigFile *config)
{
++itscl)
{
{
{
}
++itadl;
}
}
return rc;
}