ApplianceImplImport.cpp revision ceba829c00a67e9dcf7b27a986735a6764bb785e
/* $Id$ */
/** @file
*
* IAppliance and IVirtualSystem COM class implementations.
*/
/*
* Copyright (C) 2008-2010 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 "AutoCaller.h"
#include "Logging.h"
#include "ApplianceImplPrivate.h"
#include <VBox/settings.h>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
//
// IAppliance public methods
//
////////////////////////////////////////////////////////////////////////////////
/**
* Public method implementation. This opens the OVF with ovfreader.cpp.
* Thread implementation is in Appliance::readImpl().
*
* @param path
* @return
*/
{
AutoCaller autoCaller(this);
if (!isApplianceIdle())
return E_ACCESSDENIED;
if (m->pReader)
{
delete m->pReader;
}
// see if we can handle this file; for now we insist it has an ".ovf" extension
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
AutoCaller autoCaller(this);
if (!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 */
"",
/* VM name */
/* If the there isn't any name specified create a default one out of
* the OS type */
"",
nameVBox);
/* 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 */
/* Check for the constrains */
{
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 */
/* Check for the constrains */
if ( ullMemSizeVBox != 0
&& ( ullMemSizeVBox < MM_RAM_MIN_IN_MB
)
)
{
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 */
/* Currently we set the AC97 always.
@todo: figure out the hardware which could be possible */
"",
#ifdef VBOX_WITH_USB
/* USB Controller */
#endif /* VBOX_WITH_USB */
/* Network Controller */
if (cEthernetAdapters > 0)
{
/* Check for the constrains */
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. We support 8 network
* adapters at the maximum, so the first 8 will be added only. */
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,
}
}
/* Floppy Drive */
if (vsysThis.fHasFloppyDrive)
/* CD Drive */
if (vsysThis.fHasCdromDrive)
/* 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)
addWarning(tr("The virtual \"%s\" system requests support for more than two 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)
addWarning(tr("The virtual system \"%s\" requests support for more than one 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
addWarning(tr("The virtual system \"%s\" requests support for an additional 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 */
// @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 ( di.strFormat.compare("http://www.vmware.com/specifications/vmdk.html#sparse", Utf8Str::CaseInsensitive)
|| di.strFormat.compare("http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized", Utf8Str::CaseInsensitive)
|| di.strFormat.compare("http://www.vmware.com/specifications/vmdk.html#compressed", Utf8Str::CaseInsensitive)
|| di.strFormat.compare("http://www.vmware.com/interfaces/specifications/vmdk.html#compressed", Utf8Str::CaseInsensitive)
)
{
/* If the href is empty use the VM name as filename */
if (!strFilename.length())
/* Construct a unique target path */
strFilename.c_str());
/* 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 */
}
else
}
}
}
}
{
/* 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::importImpl().
* @param aProgress
* @return
*/
{
AutoCaller autoCaller(this);
// do not allow entering this method if the appliance is busy reading or writing
if (!isApplianceIdle())
return E_ACCESSDENIED;
if (!m->pReader)
tr("Cannot import machines without reading it first (call read() before importMachines())"));
try
{
}
{
}
/* Return progress to the caller */
return rc;
}
////////////////////////////////////////////////////////////////////////////////
//
// Appliance private methods
//
////////////////////////////////////////////////////////////////////////////////
/**
* Implementation for reading an OVF. 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 two locations:
*
* 1) from the public Appliance::Read().
* 2) from Appliance::readS3(), which got called from a previous instance of Appliance::taskThreadImportOrExport().
*
* @param aLocInfo
* @param aProgress
* @return
*/
{
/* 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);
try
{
/* Read & parse the XML structure of the OVF file */
/* Create the SHA1 sum of the OVF file for later validation */
char *pszDigest;
if (RT_FAILURE(vrc))
tr("Couldn't calculate SHA1 digest for file '%s' (%Rrc)"),
m->strOVFSHA1Digest = pszDigest;
}
{
x.what());
}
{
}
return rc;
}
/**
* 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 */
char *pszTmpDir;
try
{
/* Extract the bucket */
/* We need a temporary directory which we can put the OVF file & all
* disk images in */
if (RT_FAILURE(vrc))
/* The temporary name of the target OVF file */
/* Next we have to download the OVF */
vrc = RTS3Create(&hS3, pTask->locInfo.strUsername.c_str(), pTask->locInfo.strPassword.c_str(), pTask->locInfo.strHostname.c_str(), "virtualbox-agent/"VBOX_VERSION_STRING);
if (RT_FAILURE(vrc))
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)
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"),
pszFilename));
else if (vrc == VERR_S3_NOT_FOUND)
else
}
/* 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;
}
/**
* 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::convertDiskAttachmentValues: hdc.system=%d, hdc.fPrimary=%d, ulAddressOnParent=%d\n", hdc.system, hdc.fPrimary, ulAddressOnParent));
{
// 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:
tr("Invalid channel %RI16 specified; IDE controllers support only 0, 1 or 2"),
break;
}
break;
lControllerPort = (long)ulAddressOnParent;
lDevice = (long)0;
break;
lControllerPort = (long)ulAddressOnParent;
lDevice = (long)0;
break;
default: break;
}
}
/**
* 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::importS3(), which got called from a previous instance of Appliance::taskThreadImportOrExport().
*
* @param aLocInfo
* @param aProgress
* @return
*/
{
m->strManifestFile.setNull();
{
{
m->strManifestFile = strMfFile;
}
else
}
else
mode);
/* Initialize our worker task */
/* Don't destruct on success */
return rc;
}
/**
* Checks if a manifest file exists in the given location and, if so, verifies
* that the relevant files (the OVF XML and the disks referenced by it, as
* represented by the VirtualSystemDescription instances contained in this appliance)
* match it. Requires a previous read() and interpret().
*
* @param locInfo
* @param reader
* @return
*/
{
if (!m->strManifestFile.isEmpty())
{
m->ulWeightForManifestOperation); // operation's weight, as set up with the IProgress originally
// add every disks of every virtual system to an internal list
++it)
{
std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
++itH)
{
// find the disk from the OVF's disk list
}
}
// create the test list
PRTMANIFESTTEST pTestList = (PRTMANIFESTTEST)RTMemAllocZ(sizeof(RTMANIFESTTEST) * (filesList.size() + 1));
int vrc = VINF_SUCCESS;
size_t i = 1;
++it1, ++i)
{
char* pszDigest;
}
// this call can take a very long time
if (vrc == VERR_MANIFEST_DIGEST_MISMATCH)
tr("The SHA1 digest of '%s' does not match the one in '%s'"),
else if (RT_FAILURE(vrc))
tr("Could not verify the content of '%s' against the available files (%Rrc)"),
vrc);
// clean up
for (size_t j = 1;
++j)
}
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 two contexts:
*
* 1) in a first worker thread; in that case, Appliance::ImportMachines() called Appliance::importImpl();
*
* 2) in a second worker thread; in that case, Appliance::ImportMachines() called Appliance::importImpl(), which
* called Appliance::importS3(), which called Appliance::importImpl(), which then called this.
*
* @param pTask
* @return
*/
{
LogFlowFunc(("Appliance %p\n", this));
AutoCaller autoCaller(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
if (!isApplianceIdle())
return E_ACCESSDENIED;
// this is safe to access because this thread only gets started
// if pReader != NULL
// rollback for errors:
// clear the list of imported machines, if any
m->llGuidsMachinesCreated.clear();
try
{
// if a manifest file exists, verify the content; we then need all files which are referenced by the OVF & the OVF itself
// 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->findByType(VirtualSystemDescriptionType_Name);
tr("Missing VM name")));
// guest OS type
tr("Missing guest OS type")));
// CPU count
std::list<VirtualSystemDescriptionEntry*> vsdeCPU = vsdescThis->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->findByType(VirtualSystemDescriptionType_Memory);
#ifdef VBOX_WITH_USB
// USB controller
std::list<VirtualSystemDescriptionEntry*> vsdeUSBController = vsdescThis->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->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->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() ...
}
{
}
{
// with _whatever_ error we've had, do a complete roll-back of
// machines and disks we've created; unfortunately this is
// not so trivially done...
// detach all hard disks from all machines we created
++itM)
{
{
{
}
}
}
// now clean up all hard disks we created
++itHD)
{
}
// finally, deregister and remove all machines
++itID)
{
rc2 = mVirtualBox->UnregisterMachine(bstrGuid, false, ComSafeArrayAsOutParam(abstrPaths), failedMachine.asOutParam());
}
}
// restore the appliance state
return rc;
}
/**
* Imports one disk image. This is common code shared between
* -- importMachineGeneric() for the OVF case; in that case the information comes from
* the OVF virtual systems;
* -- 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 ulSizeMB Size of the disk image (for progress reporting)
* @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
*/
const Utf8Str &strTargetPath,
{
bool fSourceHdNeedsClosing = false;
try
{
// destination file must not exist
if ( strTargetPath.isEmpty()
)
tr("Destination file '%s' exists"),
strTargetPath.c_str()));
// Make sure target directory exists
// subprogress object for hard disk
/* If strHref is empty we have to create a new file */
if (strSourceOVF.isEmpty())
{
// which format to use?
if ( di.strFormat.compare("http://www.vmware.com/specifications/vmdk.html#sparse", Utf8Str::CaseInsensitive)
|| di.strFormat.compare("http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized", Utf8Str::CaseInsensitive)
|| di.strFormat.compare("http://www.vmware.com/specifications/vmdk.html#compressed", Utf8Str::CaseInsensitive)
|| di.strFormat.compare("http://www.vmware.com/interfaces/specifications/vmdk.html#compressed", Utf8Str::CaseInsensitive)
)
srcFormat = L"VMDK";
// create an empty hard disk
// create a dynamic growing disk image with the given capacity
rc = pTargetHD->CreateBaseStorage(di.iCapacity / _1M, MediumVariant_Standard, pProgress2.asOutParam());
// advance to the next operation
}
else
{
// construct source file path
Utf8StrFmt strSrcFilePath("%s%c%s", stack.strSourceDir.c_str(), RTPATH_DELIMITER, strSourceOVF.c_str());
// source path must exist
tr("Source virtual disk image file '%s' doesn't exist"),
strSrcFilePath.c_str()));
// Clone the disk image (this is necessary cause the id has
// to be recreated for the case the same hard disk is
// attached already from a previous import)
// First open the existing disk image
false,
NULL,
false,
NULL,
pSourceHD.asOutParam());
fSourceHdNeedsClosing = true;
/* We need the format description of the source disk image */
/* Create a new hard disk interface for the destination disk image */
/* Clone the source disk image */
/* Advance to the next operation */
stack.pProgress->SetNextOperation(BstrFmt(tr("Importing virtual disk image '%s'"), strSrcFilePath.c_str()),
}
// now wait for the background disk operation to complete; this throws HRESULTs on error
{
fSourceHdNeedsClosing = false;
}
}
catch (...)
{
throw;
}
}
/**
* 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 doesen't provice any
/* Create the machine */
NULL,
NULL,
// 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 */
#endif /* VBOX_WITH_USB */
/* Change the network adapters */
std::list<VirtualSystemDescriptionEntry*> vsdeNW = vsdescThis->findByType(VirtualSystemDescriptionType_NetworkAdapter);
{
/* No network adapters, so we have to disable our default one */
}
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;
}
}
}
}
}
// IDE Hard disk controller
std::list<VirtualSystemDescriptionEntry*> vsdeHDCIDE = vsdescThis->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)
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"), StorageBus_IDE, pController.asOutParam());
else
tr("Invalid IDE controller type \"%s\""),
pcszIDEType));
}
/* Hard disk controller SATA */
std::list<VirtualSystemDescriptionEntry*> vsdeHDCSATA = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerSATA);
tr("Too many SATA controllers in OVF; import facility only supports one")));
if (vsdeHDCSATA.size() > 0)
{
if (hdcVBox == "AHCI")
{
rc = pNewMachine->AddStorageController(Bstr("SATA Controller"), StorageBus_SATA, pController.asOutParam());
}
else
tr("Invalid SATA controller type \"%s\""),
}
/* Hard disk controller SCSI */
std::list<VirtualSystemDescriptionEntry*> vsdeHDCSCSI = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerSCSI);
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
tr("Invalid SCSI controller type \"%s\""),
}
/* Hard disk controller SAS */
std::list<VirtualSystemDescriptionEntry*> vsdeHDCSAS = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerSAS);
tr("Too many SAS controllers in OVF; import facility only supports one")));
if (vsdeHDCSAS.size() > 0)
{
rc = pNewMachine->AddStorageController(Bstr(L"SAS Controller"), StorageBus_SAS, pController.asOutParam());
}
/* 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->findByType(VirtualSystemDescriptionType_Floppy);
tr("Too many floppy controllers in OVF; import facility only supports one")));
std::list<VirtualSystemDescriptionEntry*> vsdeCDROM = vsdescThis->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
{
rc = sMachine->AddStorageController(Bstr("Floppy Controller"), StorageBus_Floppy, pController.asOutParam());
// this is for rollback later
mhda.lControllerPort = 0;
Log(("Attaching floppy\n"));
NULL);
}
// CD-ROMs next
++jt)
{
// for now always attach to secondary master on IDE controller;
// there seems to be no useful information in OVF where else to
// attach it (@todo test with latest versions of OVF software)
// find the IDE controller
++kt)
{
{
break;
}
}
if (!pController)
tr("OVF wants a CD-ROM drive but cannot find IDE controller, which is required in this version of VirtualBox")));
// this is for rollback later
2, // interpreted as secondary master
NULL);
} // end for (itHD = avsdeHDs.begin();
// only now that we're done with all disks, close the session
stack.fSessionOpen = false;
}
catch(HRESULT /* aRC */)
{
if (stack.fSessionOpen)
throw;
}
}
// create the hard disks & connect them to the appropriate controllers
std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
{
// 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;
/* Iterate over all given disk images */
++itHD)
{
// vsdeHD->strRef 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
)
tr("Internal inconsistency looking up disk image '%s'"),
stack);
// now use the new uuid to attach the disk image to our new machine
// 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", vsdeHD->strVboxCurrent.c_str(), mhda.lControllerPort, mhda.lDevice));
DeviceType_HardDisk, // DeviceType_T type
hdId); // uuid id
} // end for (itHD = avsdeHDs.begin();
// only now that we're done with all disks, close the session
stack.fSessionOpen = false;
}
catch(HRESULT /* aRC */)
{
if (stack.fSessionOpen)
throw;
}
}
}
/**
* 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()
*
*/
if (stack.fForceIOAPIC)
if (stack.fForceIOAPIC)
/*
<const name="HardDiskControllerIDE" value="14" />
<const name="HardDiskControllerSATA" value="15" />
<const name="HardDiskControllerSCSI" value="16" />
<const name="HardDiskControllerSAS" value="17" />
<const name="HardDiskImage" value="18" />
<const name="Floppy" value="19" />
<const name="CDROM" value="20" />
<const name="NetworkAdapter" value="21" />
*/
#ifdef VBOX_WITH_USB
// disable USB if user disabled USB
#endif
// audio adapter: only config is turning it off presently
/*
*
* step 2: scan the machine config for media attachments
*
*/
// 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;
// convert the Guid to string
// there must be an image in the OVF disk structs with the same UUID
bool fFound = false;
++oit)
{
{
/*
*
* step 3: import disk
*
*/
stack);
// ... and replace the old UUID in the machine config with the one of
// the imported disk that was just created
fFound = true;
break;
}
}
// 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"),
} // for (settings::AttachedDevicesList::const_iterator dit = sc.llAttachedDevices.begin();
} // for (settings::StorageControllersList::const_iterator sit = config.storageMachine.llStorageControllers.begin();
/*
*
* 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
stack.strNameVBox, // name from OVF preparations; can be suffixed to avoid duplicates, or changed by user
config); // the whole machine config
// return the new machine as an IMachine
IMachine *p;
pReturnNewMachine = p;
// and register it
// store new machine for roll-back in case of errors
}
/**
* 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::importFS()).
* @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 */
char *pszTmpDir;
try
{
/* Extract the bucket */
/* We need a temporary directory which we can put the all disk images
* in */
if (RT_FAILURE(vrc))
/* Add every disks of every virtual system to an internal list */
++it)
{
std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
++itH)
{
if (!strTargetFile.isEmpty())
{
/* The temporary name of the target disk file */
}
}
}
/* Next we have to download the disk images */
vrc = RTS3Create(&hS3, pTask->locInfo.strUsername.c_str(), pTask->locInfo.strPassword.c_str(), pTask->locInfo.strHostname.c_str(), "virtualbox-agent/"VBOX_VERSION_STRING);
if (RT_FAILURE(vrc))
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 */
if (RT_FAILURE(vrc))
{
if (vrc == VERR_S3_CANCELED)
throw S_OK; /* todo: !!!!!!!!!!!!! */
else if (vrc == VERR_S3_ACCESS_DENIED)
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"),
pszFilename));
else if (vrc == VERR_S3_NOT_FOUND)
tr("Cannot download file '%s' from S3 storage server (File not found)"),
pszFilename));
else
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. */
/* 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)
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"),
pszFilename));
else
tr("Cannot download file '%s' from S3 storage server (%Rrc)"),
pszFilename, vrc));
}
/* Close the connection early */
/* 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;
}