VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/ApplianceImplImport.cpp@ 48654

Last change on this file since 48654 was 48538, checked in by vboxsync, 11 years ago

Main/NetworkAdapter+Machine+Appliance+SystemProperties+Medium+Console+Settings+IDL: make NAT networking a separate network attachment type which improves the user experience, store the necessary settings, plus changing the design of the methods which will move images and entire VMs, they lacked a progress object
Frontends/VirtualBox: adapted fully, can configure NAT networks with proper drop down list support
Frontends/VBoxManage: also supports NAT networks completely, and adds the long missing code to list intnets

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 156.7 KB
Line 
1/* $Id: ApplianceImplImport.cpp 48538 2013-09-19 15:17:43Z vboxsync $ */
2/** @file
3 *
4 * IAppliance and IVirtualSystem COM class implementations.
5 */
6
7/*
8 * Copyright (C) 2008-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include <iprt/path.h>
20#include <iprt/dir.h>
21#include <iprt/file.h>
22#include <iprt/s3.h>
23#include <iprt/sha.h>
24#include <iprt/manifest.h>
25#include <iprt/tar.h>
26#include <iprt/stream.h>
27
28#include <VBox/vd.h>
29#include <VBox/com/array.h>
30
31#include "ApplianceImpl.h"
32#include "VirtualBoxImpl.h"
33#include "GuestOSTypeImpl.h"
34#include "ProgressImpl.h"
35#include "MachineImpl.h"
36#include "MediumImpl.h"
37#include "MediumFormatImpl.h"
38#include "SystemPropertiesImpl.h"
39#include "HostImpl.h"
40
41#include "AutoCaller.h"
42#include "Logging.h"
43
44#include "ApplianceImplPrivate.h"
45
46#include <VBox/param.h>
47#include <VBox/version.h>
48#include <VBox/settings.h>
49
50#include <set>
51
52using namespace std;
53
54////////////////////////////////////////////////////////////////////////////////
55//
56// IAppliance public methods
57//
58////////////////////////////////////////////////////////////////////////////////
59
60/**
61 * Public method implementation. This opens the OVF with ovfreader.cpp.
62 * Thread implementation is in Appliance::readImpl().
63 *
64 * @param path
65 * @return
66 */
67STDMETHODIMP Appliance::Read(IN_BSTR path, IProgress **aProgress)
68{
69 if (!path) return E_POINTER;
70 CheckComArgOutPointerValid(aProgress);
71
72 AutoCaller autoCaller(this);
73 if (FAILED(autoCaller.rc())) return autoCaller.rc();
74
75 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
76
77 if (!isApplianceIdle())
78 return E_ACCESSDENIED;
79
80 if (m->pReader)
81 {
82 delete m->pReader;
83 m->pReader = NULL;
84 }
85
86 // see if we can handle this file; for now we insist it has an ovf/ova extension
87 Utf8Str strPath (path);
88 if (!( strPath.endsWith(".ovf", Utf8Str::CaseInsensitive)
89 || strPath.endsWith(".ova", Utf8Str::CaseInsensitive)))
90 return setError(VBOX_E_FILE_ERROR,
91 tr("Appliance file must have .ovf extension"));
92
93 ComObjPtr<Progress> progress;
94 HRESULT rc = S_OK;
95 try
96 {
97 /* Parse all necessary info out of the URI */
98 parseURI(strPath, m->locInfo);
99 rc = readImpl(m->locInfo, progress);
100 }
101 catch (HRESULT aRC)
102 {
103 rc = aRC;
104 }
105
106 if (SUCCEEDED(rc))
107 /* Return progress to the caller */
108 progress.queryInterfaceTo(aProgress);
109
110 return S_OK;
111}
112
113/**
114 * Public method implementation. This looks at the output of ovfreader.cpp and creates
115 * VirtualSystemDescription instances.
116 * @return
117 */
118STDMETHODIMP Appliance::Interpret()
119{
120 // @todo:
121 // - don't use COM methods but the methods directly (faster, but needs appropriate
122 // locking of that objects itself (s. HardDisk))
123 // - Appropriate handle errors like not supported file formats
124 AutoCaller autoCaller(this);
125 if (FAILED(autoCaller.rc())) return autoCaller.rc();
126
127 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
128
129 if (!isApplianceIdle())
130 return E_ACCESSDENIED;
131
132 HRESULT rc = S_OK;
133
134 /* Clear any previous virtual system descriptions */
135 m->virtualSystemDescriptions.clear();
136
137 if (!m->pReader)
138 return setError(E_FAIL,
139 tr("Cannot interpret appliance without reading it first (call read() before interpret())"));
140
141 // Change the appliance state so we can safely leave the lock while doing time-consuming
142 // disk imports; also the below method calls do all kinds of locking which conflicts with
143 // the appliance object lock
144 m->state = Data::ApplianceImporting;
145 alock.release();
146
147 /* Try/catch so we can clean up on error */
148 try
149 {
150 list<ovf::VirtualSystem>::const_iterator it;
151 /* Iterate through all virtual systems */
152 for (it = m->pReader->m_llVirtualSystems.begin();
153 it != m->pReader->m_llVirtualSystems.end();
154 ++it)
155 {
156 const ovf::VirtualSystem &vsysThis = *it;
157
158 ComObjPtr<VirtualSystemDescription> pNewDesc;
159 rc = pNewDesc.createObject();
160 if (FAILED(rc)) throw rc;
161 rc = pNewDesc->init();
162 if (FAILED(rc)) throw rc;
163
164 // if the virtual system in OVF had a <vbox:Machine> element, have the
165 // VirtualBox settings code parse that XML now
166 if (vsysThis.pelmVboxMachine)
167 pNewDesc->importVboxMachineXML(*vsysThis.pelmVboxMachine);
168
169 // Guest OS type
170 // This is taken from one of three places, in this order:
171 Utf8Str strOsTypeVBox;
172 Utf8StrFmt strCIMOSType("%RU32", (uint32_t)vsysThis.cimos);
173 // 1) If there is a <vbox:Machine>, then use the type from there.
174 if ( vsysThis.pelmVboxMachine
175 && pNewDesc->m->pConfig->machineUserData.strOsType.isNotEmpty()
176 )
177 strOsTypeVBox = pNewDesc->m->pConfig->machineUserData.strOsType;
178 // 2) Otherwise, if there is OperatingSystemSection/vbox:OSType, use that one.
179 else if (vsysThis.strTypeVbox.isNotEmpty()) // OVFReader has found vbox:OSType
180 strOsTypeVBox = vsysThis.strTypeVbox;
181 // 3) Otherwise, make a best guess what the vbox type is from the OVF (CIM) OS type.
182 else
183 convertCIMOSType2VBoxOSType(strOsTypeVBox, vsysThis.cimos, vsysThis.strCimosDesc);
184 pNewDesc->addEntry(VirtualSystemDescriptionType_OS,
185 "",
186 strCIMOSType,
187 strOsTypeVBox);
188
189 /* VM name */
190 Utf8Str nameVBox;
191 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
192 if ( vsysThis.pelmVboxMachine
193 && pNewDesc->m->pConfig->machineUserData.strName.isNotEmpty())
194 nameVBox = pNewDesc->m->pConfig->machineUserData.strName;
195 else
196 nameVBox = vsysThis.strName;
197 /* If there isn't any name specified create a default one out
198 * of the OS type */
199 if (nameVBox.isEmpty())
200 nameVBox = strOsTypeVBox;
201 searchUniqueVMName(nameVBox);
202 pNewDesc->addEntry(VirtualSystemDescriptionType_Name,
203 "",
204 vsysThis.strName,
205 nameVBox);
206
207 /* Based on the VM name, create a target machine path. */
208 Bstr bstrMachineFilename;
209 rc = mVirtualBox->ComposeMachineFilename(Bstr(nameVBox).raw(),
210 NULL /* aGroup */,
211 NULL /* aCreateFlags */,
212 NULL /* aBaseFolder */,
213 bstrMachineFilename.asOutParam());
214 if (FAILED(rc)) throw rc;
215 /* Determine the machine folder from that */
216 Utf8Str strMachineFolder = Utf8Str(bstrMachineFilename).stripFilename();
217
218 /* VM Product */
219 if (!vsysThis.strProduct.isEmpty())
220 pNewDesc->addEntry(VirtualSystemDescriptionType_Product,
221 "",
222 vsysThis.strProduct,
223 vsysThis.strProduct);
224
225 /* VM Vendor */
226 if (!vsysThis.strVendor.isEmpty())
227 pNewDesc->addEntry(VirtualSystemDescriptionType_Vendor,
228 "",
229 vsysThis.strVendor,
230 vsysThis.strVendor);
231
232 /* VM Version */
233 if (!vsysThis.strVersion.isEmpty())
234 pNewDesc->addEntry(VirtualSystemDescriptionType_Version,
235 "",
236 vsysThis.strVersion,
237 vsysThis.strVersion);
238
239 /* VM ProductUrl */
240 if (!vsysThis.strProductUrl.isEmpty())
241 pNewDesc->addEntry(VirtualSystemDescriptionType_ProductUrl,
242 "",
243 vsysThis.strProductUrl,
244 vsysThis.strProductUrl);
245
246 /* VM VendorUrl */
247 if (!vsysThis.strVendorUrl.isEmpty())
248 pNewDesc->addEntry(VirtualSystemDescriptionType_VendorUrl,
249 "",
250 vsysThis.strVendorUrl,
251 vsysThis.strVendorUrl);
252
253 /* VM description */
254 if (!vsysThis.strDescription.isEmpty())
255 pNewDesc->addEntry(VirtualSystemDescriptionType_Description,
256 "",
257 vsysThis.strDescription,
258 vsysThis.strDescription);
259
260 /* VM license */
261 if (!vsysThis.strLicenseText.isEmpty())
262 pNewDesc->addEntry(VirtualSystemDescriptionType_License,
263 "",
264 vsysThis.strLicenseText,
265 vsysThis.strLicenseText);
266
267 /* Now that we know the OS type, get our internal defaults based on that. */
268 ComPtr<IGuestOSType> pGuestOSType;
269 rc = mVirtualBox->GetGuestOSType(Bstr(strOsTypeVBox).raw(), pGuestOSType.asOutParam());
270 if (FAILED(rc)) throw rc;
271
272 /* CPU count */
273 ULONG cpuCountVBox;
274 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
275 if ( vsysThis.pelmVboxMachine
276 && pNewDesc->m->pConfig->hardwareMachine.cCPUs)
277 cpuCountVBox = pNewDesc->m->pConfig->hardwareMachine.cCPUs;
278 else
279 cpuCountVBox = vsysThis.cCPUs;
280 /* Check for the constraints */
281 if (cpuCountVBox > SchemaDefs::MaxCPUCount)
282 {
283 addWarning(tr("The virtual system \"%s\" claims support for %u CPU's, but VirtualBox has support for "
284 "max %u CPU's only."),
285 vsysThis.strName.c_str(), cpuCountVBox, SchemaDefs::MaxCPUCount);
286 cpuCountVBox = SchemaDefs::MaxCPUCount;
287 }
288 if (vsysThis.cCPUs == 0)
289 cpuCountVBox = 1;
290 pNewDesc->addEntry(VirtualSystemDescriptionType_CPU,
291 "",
292 Utf8StrFmt("%RU32", (uint32_t)vsysThis.cCPUs),
293 Utf8StrFmt("%RU32", (uint32_t)cpuCountVBox));
294
295 /* RAM */
296 uint64_t ullMemSizeVBox;
297 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
298 if ( vsysThis.pelmVboxMachine
299 && pNewDesc->m->pConfig->hardwareMachine.ulMemorySizeMB)
300 ullMemSizeVBox = pNewDesc->m->pConfig->hardwareMachine.ulMemorySizeMB;
301 else
302 ullMemSizeVBox = vsysThis.ullMemorySize / _1M;
303 /* Check for the constraints */
304 if ( ullMemSizeVBox != 0
305 && ( ullMemSizeVBox < MM_RAM_MIN_IN_MB
306 || ullMemSizeVBox > MM_RAM_MAX_IN_MB
307 )
308 )
309 {
310 addWarning(tr("The virtual system \"%s\" claims support for %llu MB RAM size, but VirtualBox has "
311 "support for min %u & max %u MB RAM size only."),
312 vsysThis.strName.c_str(), ullMemSizeVBox, MM_RAM_MIN_IN_MB, MM_RAM_MAX_IN_MB);
313 ullMemSizeVBox = RT_MIN(RT_MAX(ullMemSizeVBox, MM_RAM_MIN_IN_MB), MM_RAM_MAX_IN_MB);
314 }
315 if (vsysThis.ullMemorySize == 0)
316 {
317 /* If the RAM of the OVF is zero, use our predefined values */
318 ULONG memSizeVBox2;
319 rc = pGuestOSType->COMGETTER(RecommendedRAM)(&memSizeVBox2);
320 if (FAILED(rc)) throw rc;
321 /* VBox stores that in MByte */
322 ullMemSizeVBox = (uint64_t)memSizeVBox2;
323 }
324 pNewDesc->addEntry(VirtualSystemDescriptionType_Memory,
325 "",
326 Utf8StrFmt("%RU64", (uint64_t)vsysThis.ullMemorySize),
327 Utf8StrFmt("%RU64", (uint64_t)ullMemSizeVBox));
328
329 /* Audio */
330 Utf8Str strSoundCard;
331 Utf8Str strSoundCardOrig;
332 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
333 if ( vsysThis.pelmVboxMachine
334 && pNewDesc->m->pConfig->hardwareMachine.audioAdapter.fEnabled)
335 {
336 strSoundCard = Utf8StrFmt("%RU32",
337 (uint32_t)pNewDesc->m->pConfig->hardwareMachine.audioAdapter.controllerType);
338 }
339 else if (vsysThis.strSoundCardType.isNotEmpty())
340 {
341 /* Set the AC97 always for the simple OVF case.
342 * @todo: figure out the hardware which could be possible */
343 strSoundCard = Utf8StrFmt("%RU32", (uint32_t)AudioControllerType_AC97);
344 strSoundCardOrig = vsysThis.strSoundCardType;
345 }
346 if (strSoundCard.isNotEmpty())
347 pNewDesc->addEntry(VirtualSystemDescriptionType_SoundCard,
348 "",
349 strSoundCardOrig,
350 strSoundCard);
351
352#ifdef VBOX_WITH_USB
353 /* USB Controller */
354 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
355 if ( ( vsysThis.pelmVboxMachine
356 && pNewDesc->m->pConfig->hardwareMachine.usbSettings.llUSBControllers.size() > 0)
357 || vsysThis.fHasUsbController)
358 pNewDesc->addEntry(VirtualSystemDescriptionType_USBController, "", "", "");
359#endif /* VBOX_WITH_USB */
360
361 /* Network Controller */
362 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
363 if (vsysThis.pelmVboxMachine)
364 {
365 uint32_t maxNetworkAdapters = Global::getMaxNetworkAdapters(pNewDesc->m->pConfig->hardwareMachine.chipsetType);
366
367 const settings::NetworkAdaptersList &llNetworkAdapters = pNewDesc->m->pConfig->hardwareMachine.llNetworkAdapters;
368 /* Check for the constrains */
369 if (llNetworkAdapters.size() > maxNetworkAdapters)
370 addWarning(tr("The virtual system \"%s\" claims support for %zu network adapters, but VirtualBox "
371 "has support for max %u network adapter only."),
372 vsysThis.strName.c_str(), llNetworkAdapters.size(), maxNetworkAdapters);
373 /* Iterate through all network adapters. */
374 settings::NetworkAdaptersList::const_iterator it1;
375 size_t a = 0;
376 for (it1 = llNetworkAdapters.begin();
377 it1 != llNetworkAdapters.end() && a < maxNetworkAdapters;
378 ++it1, ++a)
379 {
380 if (it1->fEnabled)
381 {
382 Utf8Str strMode = convertNetworkAttachmentTypeToString(it1->mode);
383 pNewDesc->addEntry(VirtualSystemDescriptionType_NetworkAdapter,
384 "", // ref
385 strMode, // orig
386 Utf8StrFmt("%RU32", (uint32_t)it1->type), // conf
387 0,
388 Utf8StrFmt("slot=%RU32;type=%s", it1->ulSlot, strMode.c_str())); // extra conf
389 }
390 }
391 }
392 /* else we use the ovf configuration. */
393 else if (size_t cEthernetAdapters = vsysThis.llEthernetAdapters.size() > 0)
394 {
395 uint32_t maxNetworkAdapters = Global::getMaxNetworkAdapters(ChipsetType_PIIX3);
396
397 /* Check for the constrains */
398 if (cEthernetAdapters > maxNetworkAdapters)
399 addWarning(tr("The virtual system \"%s\" claims support for %zu network adapters, but VirtualBox "
400 "has support for max %u network adapter only."),
401 vsysThis.strName.c_str(), cEthernetAdapters, maxNetworkAdapters);
402
403 /* Get the default network adapter type for the selected guest OS */
404 NetworkAdapterType_T defaultAdapterVBox = NetworkAdapterType_Am79C970A;
405 rc = pGuestOSType->COMGETTER(AdapterType)(&defaultAdapterVBox);
406 if (FAILED(rc)) throw rc;
407
408 ovf::EthernetAdaptersList::const_iterator itEA;
409 /* Iterate through all abstract networks. Ignore network cards
410 * which exceed the limit of VirtualBox. */
411 size_t a = 0;
412 for (itEA = vsysThis.llEthernetAdapters.begin();
413 itEA != vsysThis.llEthernetAdapters.end() && a < maxNetworkAdapters;
414 ++itEA, ++a)
415 {
416 const ovf::EthernetAdapter &ea = *itEA; // logical network to connect to
417 Utf8Str strNetwork = ea.strNetworkName;
418 // make sure it's one of these two
419 if ( (strNetwork.compare("Null", Utf8Str::CaseInsensitive))
420 && (strNetwork.compare("NAT", Utf8Str::CaseInsensitive))
421 && (strNetwork.compare("Bridged", Utf8Str::CaseInsensitive))
422 && (strNetwork.compare("Internal", Utf8Str::CaseInsensitive))
423 && (strNetwork.compare("HostOnly", Utf8Str::CaseInsensitive))
424 && (strNetwork.compare("Generic", Utf8Str::CaseInsensitive))
425 )
426 strNetwork = "Bridged"; // VMware assumes this is the default apparently
427
428 /* Figure out the hardware type */
429 NetworkAdapterType_T nwAdapterVBox = defaultAdapterVBox;
430 if (!ea.strAdapterType.compare("PCNet32", Utf8Str::CaseInsensitive))
431 {
432 /* If the default adapter is already one of the two
433 * PCNet adapters use the default one. If not use the
434 * Am79C970A as fallback. */
435 if (!(defaultAdapterVBox == NetworkAdapterType_Am79C970A ||
436 defaultAdapterVBox == NetworkAdapterType_Am79C973))
437 nwAdapterVBox = NetworkAdapterType_Am79C970A;
438 }
439#ifdef VBOX_WITH_E1000
440 /* VMWare accidentally write this with VirtualCenter 3.5,
441 so make sure in this case always to use the VMWare one */
442 else if (!ea.strAdapterType.compare("E10000", Utf8Str::CaseInsensitive))
443 nwAdapterVBox = NetworkAdapterType_I82545EM;
444 else if (!ea.strAdapterType.compare("E1000", Utf8Str::CaseInsensitive))
445 {
446 /* Check if this OVF was written by VirtualBox */
447 if (Utf8Str(vsysThis.strVirtualSystemType).contains("virtualbox", Utf8Str::CaseInsensitive))
448 {
449 /* If the default adapter is already one of the three
450 * E1000 adapters use the default one. If not use the
451 * I82545EM as fallback. */
452 if (!(defaultAdapterVBox == NetworkAdapterType_I82540EM ||
453 defaultAdapterVBox == NetworkAdapterType_I82543GC ||
454 defaultAdapterVBox == NetworkAdapterType_I82545EM))
455 nwAdapterVBox = NetworkAdapterType_I82540EM;
456 }
457 else
458 /* Always use this one since it's what VMware uses */
459 nwAdapterVBox = NetworkAdapterType_I82545EM;
460 }
461#endif /* VBOX_WITH_E1000 */
462
463 pNewDesc->addEntry(VirtualSystemDescriptionType_NetworkAdapter,
464 "", // ref
465 ea.strNetworkName, // orig
466 Utf8StrFmt("%RU32", (uint32_t)nwAdapterVBox), // conf
467 0,
468 Utf8StrFmt("type=%s", strNetwork.c_str())); // extra conf
469 }
470 }
471
472 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
473 bool fFloppy = false;
474 bool fDVD = false;
475 if (vsysThis.pelmVboxMachine)
476 {
477 settings::StorageControllersList &llControllers = pNewDesc->m->pConfig->storageMachine.llStorageControllers;
478 settings::StorageControllersList::iterator it3;
479 for (it3 = llControllers.begin();
480 it3 != llControllers.end();
481 ++it3)
482 {
483 settings::AttachedDevicesList &llAttachments = it3->llAttachedDevices;
484 settings::AttachedDevicesList::iterator it4;
485 for (it4 = llAttachments.begin();
486 it4 != llAttachments.end();
487 ++it4)
488 {
489 fDVD |= it4->deviceType == DeviceType_DVD;
490 fFloppy |= it4->deviceType == DeviceType_Floppy;
491 if (fFloppy && fDVD)
492 break;
493 }
494 if (fFloppy && fDVD)
495 break;
496 }
497 }
498 else
499 {
500 fFloppy = vsysThis.fHasFloppyDrive;
501 fDVD = vsysThis.fHasCdromDrive;
502 }
503 /* Floppy Drive */
504 if (fFloppy)
505 pNewDesc->addEntry(VirtualSystemDescriptionType_Floppy, "", "", "");
506 /* CD Drive */
507 if (fDVD)
508 pNewDesc->addEntry(VirtualSystemDescriptionType_CDROM, "", "", "");
509
510 /* Hard disk Controller */
511 uint16_t cIDEused = 0;
512 uint16_t cSATAused = 0; NOREF(cSATAused);
513 uint16_t cSCSIused = 0; NOREF(cSCSIused);
514 ovf::ControllersMap::const_iterator hdcIt;
515 /* Iterate through all hard disk controllers */
516 for (hdcIt = vsysThis.mapControllers.begin();
517 hdcIt != vsysThis.mapControllers.end();
518 ++hdcIt)
519 {
520 const ovf::HardDiskController &hdc = hdcIt->second;
521 Utf8Str strControllerID = Utf8StrFmt("%RI32", (uint32_t)hdc.idController);
522
523 switch (hdc.system)
524 {
525 case ovf::HardDiskController::IDE:
526 /* Check for the constrains */
527 if (cIDEused < 4)
528 {
529 // @todo: figure out the IDE types
530 /* Use PIIX4 as default */
531 Utf8Str strType = "PIIX4";
532 if (!hdc.strControllerType.compare("PIIX3", Utf8Str::CaseInsensitive))
533 strType = "PIIX3";
534 else if (!hdc.strControllerType.compare("ICH6", Utf8Str::CaseInsensitive))
535 strType = "ICH6";
536 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskControllerIDE,
537 strControllerID, // strRef
538 hdc.strControllerType, // aOvfValue
539 strType); // aVboxValue
540 }
541 else
542 /* Warn only once */
543 if (cIDEused == 2)
544 addWarning(tr("The virtual \"%s\" system requests support for more than two "
545 "IDE controller channels, but VirtualBox supports only two."),
546 vsysThis.strName.c_str());
547
548 ++cIDEused;
549 break;
550
551 case ovf::HardDiskController::SATA:
552 /* Check for the constrains */
553 if (cSATAused < 1)
554 {
555 // @todo: figure out the SATA types
556 /* We only support a plain AHCI controller, so use them always */
557 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskControllerSATA,
558 strControllerID,
559 hdc.strControllerType,
560 "AHCI");
561 }
562 else
563 {
564 /* Warn only once */
565 if (cSATAused == 1)
566 addWarning(tr("The virtual system \"%s\" requests support for more than one "
567 "SATA controller, but VirtualBox has support for only one"),
568 vsysThis.strName.c_str());
569
570 }
571 ++cSATAused;
572 break;
573
574 case ovf::HardDiskController::SCSI:
575 /* Check for the constrains */
576 if (cSCSIused < 1)
577 {
578 VirtualSystemDescriptionType_T vsdet = VirtualSystemDescriptionType_HardDiskControllerSCSI;
579 Utf8Str hdcController = "LsiLogic";
580 if (!hdc.strControllerType.compare("lsilogicsas", Utf8Str::CaseInsensitive))
581 {
582 // OVF considers SAS a variant of SCSI but VirtualBox considers it a class of its own
583 vsdet = VirtualSystemDescriptionType_HardDiskControllerSAS;
584 hdcController = "LsiLogicSas";
585 }
586 else if (!hdc.strControllerType.compare("BusLogic", Utf8Str::CaseInsensitive))
587 hdcController = "BusLogic";
588 pNewDesc->addEntry(vsdet,
589 strControllerID,
590 hdc.strControllerType,
591 hdcController);
592 }
593 else
594 addWarning(tr("The virtual system \"%s\" requests support for an additional "
595 "SCSI controller of type \"%s\" with ID %s, but VirtualBox presently "
596 "supports only one SCSI controller."),
597 vsysThis.strName.c_str(),
598 hdc.strControllerType.c_str(),
599 strControllerID.c_str());
600 ++cSCSIused;
601 break;
602 }
603 }
604
605 /* Hard disks */
606 if (vsysThis.mapVirtualDisks.size() > 0)
607 {
608 ovf::VirtualDisksMap::const_iterator itVD;
609 /* Iterate through all hard disks ()*/
610 for (itVD = vsysThis.mapVirtualDisks.begin();
611 itVD != vsysThis.mapVirtualDisks.end();
612 ++itVD)
613 {
614 const ovf::VirtualDisk &hd = itVD->second;
615 /* Get the associated disk image */
616 ovf::DiskImage di;
617 std::map<RTCString, ovf::DiskImage>::iterator foundDisk;
618
619 foundDisk = m->pReader->m_mapDisks.find(hd.strDiskId);
620 if (foundDisk == m->pReader->m_mapDisks.end())
621 continue;
622 else
623 {
624 di = foundDisk->second;
625 }
626
627 /*
628 * Figure out from URI which format the image of disk has.
629 * URI must have inside section <Disk> .
630 * But there aren't strong requirements about correspondence one URI for one disk virtual format.
631 * So possibly, we aren't able to recognize some URIs.
632 */
633 Utf8Str vdf = typeOfVirtualDiskFormatFromURI(di.strFormat);
634
635 /*
636 * fallback, if we can't determine virtual disk format using URI from the attribute ovf:format
637 * in the corresponding section <Disk> in the OVF file.
638 */
639 if (vdf.isEmpty())
640 {
641 /* Figure out from extension which format the image of disk has. */
642 {
643 char *pszExt = RTPathExt(di.strHref.c_str());
644 /* Get the system properties. */
645 SystemProperties *pSysProps = mVirtualBox->getSystemProperties();
646 ComObjPtr<MediumFormat> trgFormat = pSysProps->mediumFormatFromExtension(&pszExt[1]);
647 if (trgFormat.isNull())
648 {
649 throw setError(E_FAIL,
650 tr("Internal inconsistency looking up medium format for the disk image '%s'"),
651 di.strHref.c_str());
652 }
653
654 Bstr bstrFormatName;
655 rc = trgFormat->COMGETTER(Name)(bstrFormatName.asOutParam());
656 if (FAILED(rc))
657 throw rc;
658
659 vdf = Utf8Str(bstrFormatName);
660 }
661 }
662
663 // @todo:
664 // - figure out all possible vmdk formats we also support
665 // - figure out if there is a url specifier for vhd already
666 // - we need a url specifier for the vdi format
667
668 if (vdf.compare("VMDK", Utf8Str::CaseInsensitive) == 0)
669 {
670 /* If the href is empty use the VM name as filename */
671 Utf8Str strFilename = di.strHref;
672 if (!strFilename.length())
673 strFilename = Utf8StrFmt("%s.vmdk", hd.strDiskId.c_str());
674
675 Utf8Str strTargetPath = Utf8Str(strMachineFolder);
676 strTargetPath.append(RTPATH_DELIMITER).append(di.strHref);
677 searchUniqueDiskImageFilePath(strTargetPath);
678
679 /* find the description for the hard disk controller
680 * that has the same ID as hd.idController */
681 const VirtualSystemDescriptionEntry *pController;
682 if (!(pController = pNewDesc->findControllerFromID(hd.idController)))
683 throw setError(E_FAIL,
684 tr("Cannot find hard disk controller with OVF instance ID %RI32 "
685 "to which disk \"%s\" should be attached"),
686 hd.idController,
687 di.strHref.c_str());
688
689 /* controller to attach to, and the bus within that controller */
690 Utf8StrFmt strExtraConfig("controller=%RI16;channel=%RI16",
691 pController->ulIndex,
692 hd.ulAddressOnParent);
693 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskImage,
694 hd.strDiskId,
695 di.strHref,
696 strTargetPath,
697 di.ulSuggestedSizeMB,
698 strExtraConfig);
699 }
700 else if (vdf.compare("RAW", Utf8Str::CaseInsensitive) == 0)
701 {
702 /* If the href is empty use the VM name as filename */
703 Utf8Str strFilename = di.strHref;
704 if (!strFilename.length())
705 strFilename = Utf8StrFmt("%s.iso", hd.strDiskId.c_str());
706
707 Utf8Str strTargetPath = Utf8Str(strMachineFolder)
708 .append(RTPATH_DELIMITER)
709 .append(di.strHref);
710 searchUniqueDiskImageFilePath(strTargetPath);
711
712 /* find the description for the hard disk controller
713 * that has the same ID as hd.idController */
714 const VirtualSystemDescriptionEntry *pController;
715 if (!(pController = pNewDesc->findControllerFromID(hd.idController)))
716 throw setError(E_FAIL,
717 tr("Cannot find disk controller with OVF instance ID %RI32 "
718 "to which disk \"%s\" should be attached"),
719 hd.idController,
720 di.strHref.c_str());
721
722 /* controller to attach to, and the bus within that controller */
723 Utf8StrFmt strExtraConfig("controller=%RI16;channel=%RI16",
724 pController->ulIndex,
725 hd.ulAddressOnParent);
726 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskImage,
727 hd.strDiskId,
728 di.strHref,
729 strTargetPath,
730 di.ulSuggestedSizeMB,
731 strExtraConfig);
732 }
733 else
734 throw setError(VBOX_E_FILE_ERROR,
735 tr("Unsupported format for virtual disk image %s in OVF: \"%s\""),
736 di.strHref.c_str(),
737 di.strFormat.c_str());
738 }
739 }
740
741 m->virtualSystemDescriptions.push_back(pNewDesc);
742 }
743 }
744 catch (HRESULT aRC)
745 {
746 /* On error we clear the list & return */
747 m->virtualSystemDescriptions.clear();
748 rc = aRC;
749 }
750
751 // reset the appliance state
752 alock.acquire();
753 m->state = Data::ApplianceIdle;
754
755 return rc;
756}
757
758/**
759 * Public method implementation. This creates one or more new machines according to the
760 * VirtualSystemScription instances created by Appliance::Interpret().
761 * Thread implementation is in Appliance::importImpl().
762 * @param aProgress
763 * @return
764 */
765STDMETHODIMP Appliance::ImportMachines(ComSafeArrayIn(ImportOptions_T, options), IProgress **aProgress)
766{
767 CheckComArgOutPointerValid(aProgress);
768
769 AutoCaller autoCaller(this);
770 if (FAILED(autoCaller.rc())) return autoCaller.rc();
771
772 if (options != NULL)
773 m->optList = com::SafeArray<ImportOptions_T>(ComSafeArrayInArg(options)).toList();
774
775 AssertReturn(!(m->optList.contains(ImportOptions_KeepAllMACs) && m->optList.contains(ImportOptions_KeepNATMACs)), E_INVALIDARG);
776
777 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
778
779 // do not allow entering this method if the appliance is busy reading or writing
780 if (!isApplianceIdle())
781 return E_ACCESSDENIED;
782
783 if (!m->pReader)
784 return setError(E_FAIL,
785 tr("Cannot import machines without reading it first (call read() before importMachines())"));
786
787 ComObjPtr<Progress> progress;
788 HRESULT rc = S_OK;
789 try
790 {
791 rc = importImpl(m->locInfo, progress);
792 }
793 catch (HRESULT aRC)
794 {
795 rc = aRC;
796 }
797
798 if (SUCCEEDED(rc))
799 /* Return progress to the caller */
800 progress.queryInterfaceTo(aProgress);
801
802 return rc;
803}
804
805////////////////////////////////////////////////////////////////////////////////
806//
807// Appliance private methods
808//
809////////////////////////////////////////////////////////////////////////////////
810
811HRESULT Appliance::preCheckImageAvailability(PSHASTORAGE pSHAStorage,
812 RTCString &availableImage)
813{
814 HRESULT rc = S_OK;
815 RTTAR tar = (RTTAR)pSHAStorage->pVDImageIfaces->pvUser;
816 char *pszFilename = 0;
817
818 int vrc = RTTarCurrentFile(tar, &pszFilename);
819
820 if (RT_FAILURE(vrc))
821 {
822 throw setError(VBOX_E_FILE_ERROR,
823 tr("Could not open the current file in the OVA package (%Rrc)"), vrc);
824 }
825 else
826 {
827 if (vrc == VINF_TAR_DIR_PATH)
828 {
829 throw setError(VBOX_E_FILE_ERROR,
830 tr("Empty directory folder (%s) isn't allowed in the OVA package (%Rrc)"),
831 pszFilename,
832 vrc);
833 }
834 }
835
836 availableImage = pszFilename;
837
838 return rc;
839}
840
841/*******************************************************************************
842 * Read stuff
843 ******************************************************************************/
844
845/**
846 * Implementation for reading an OVF. This starts a new thread which will call
847 * Appliance::taskThreadImportOrExport() which will then call readFS() or readS3().
848 * This will then open the OVF with ovfreader.cpp.
849 *
850 * This is in a separate private method because it is used from three locations:
851 *
852 * 1) from the public Appliance::Read().
853 *
854 * 2) in a second worker thread; in that case, Appliance::ImportMachines() called Appliance::importImpl(), which
855 * called Appliance::readFSOVA(), which called Appliance::importImpl(), which then called this again.
856 *
857 * 3) from Appliance::readS3(), which got called from a previous instance of Appliance::taskThreadImportOrExport().
858 *
859 * @param aLocInfo
860 * @param aProgress
861 * @return
862 */
863HRESULT Appliance::readImpl(const LocationInfo &aLocInfo, ComObjPtr<Progress> &aProgress)
864{
865 BstrFmt bstrDesc = BstrFmt(tr("Reading appliance '%s'"),
866 aLocInfo.strPath.c_str());
867 HRESULT rc;
868 /* Create the progress object */
869 aProgress.createObject();
870 if (aLocInfo.storageType == VFSType_File)
871 /* 1 operation only */
872 rc = aProgress->init(mVirtualBox, static_cast<IAppliance*>(this),
873 bstrDesc.raw(),
874 TRUE /* aCancelable */);
875 else
876 /* 4/5 is downloading, 1/5 is reading */
877 rc = aProgress->init(mVirtualBox, static_cast<IAppliance*>(this),
878 bstrDesc.raw(),
879 TRUE /* aCancelable */,
880 2, // ULONG cOperations,
881 5, // ULONG ulTotalOperationsWeight,
882 BstrFmt(tr("Download appliance '%s'"),
883 aLocInfo.strPath.c_str()).raw(), // CBSTR bstrFirstOperationDescription,
884 4); // ULONG ulFirstOperationWeight,
885 if (FAILED(rc)) throw rc;
886
887 /* Initialize our worker task */
888 std::auto_ptr<TaskOVF> task(new TaskOVF(this, TaskOVF::Read, aLocInfo, aProgress));
889
890 rc = task->startThread();
891 if (FAILED(rc)) throw rc;
892
893 /* Don't destruct on success */
894 task.release();
895
896 return rc;
897}
898
899/**
900 * Actual worker code for reading an OVF from disk. This is called from Appliance::taskThreadImportOrExport()
901 * and therefore runs on the OVF read worker thread. This opens the OVF with ovfreader.cpp.
902 *
903 * This runs in two contexts:
904 *
905 * 1) in a first worker thread; in that case, Appliance::Read() called Appliance::readImpl();
906 *
907 * 2) in a second worker thread; in that case, Appliance::Read() called Appliance::readImpl(), which
908 * called Appliance::readS3(), which called Appliance::readImpl(), which then called this.
909 *
910 * @param pTask
911 * @return
912 */
913HRESULT Appliance::readFS(TaskOVF *pTask)
914{
915 LogFlowFuncEnter();
916 LogFlowFunc(("Appliance %p\n", this));
917
918 AutoCaller autoCaller(this);
919 if (FAILED(autoCaller.rc())) return autoCaller.rc();
920
921 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
922
923 HRESULT rc = S_OK;
924
925 if (pTask->locInfo.strPath.endsWith(".ovf", Utf8Str::CaseInsensitive))
926 rc = readFSOVF(pTask);
927 else
928 rc = readFSOVA(pTask);
929
930 LogFlowFunc(("rc=%Rhrc\n", rc));
931 LogFlowFuncLeave();
932
933 return rc;
934}
935
936HRESULT Appliance::readFSOVF(TaskOVF *pTask)
937{
938 LogFlowFuncEnter();
939
940 HRESULT rc = S_OK;
941 int vrc = VINF_SUCCESS;
942
943 PVDINTERFACEIO pShaIo = 0;
944 PVDINTERFACEIO pFileIo = 0;
945 do
946 {
947 try
948 {
949 /* Create the necessary file access interfaces. */
950 pFileIo = FileCreateInterface();
951 if (!pFileIo)
952 {
953 rc = E_OUTOFMEMORY;
954 break;
955 }
956
957 Utf8Str strMfFile = Utf8Str(pTask->locInfo.strPath).stripExt().append(".mf");
958
959 SHASTORAGE storage;
960 RT_ZERO(storage);
961
962 if (RTFileExists(strMfFile.c_str()))
963 {
964 pShaIo = ShaCreateInterface();
965 if (!pShaIo)
966 {
967 rc = E_OUTOFMEMORY;
968 break;
969 }
970
971 //read the manifest file and find a type of used digest
972 RTFILE pFile = NULL;
973 vrc = RTFileOpen(&pFile, strMfFile.c_str(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
974 if (RT_SUCCESS(vrc) && pFile != NULL)
975 {
976 uint64_t cbFile = 0;
977 uint64_t maxFileSize = _1M;
978 size_t cbRead = 0;
979 void *pBuf; /** @todo r=bird: You leak this buffer! throwing stuff is evil. */
980
981 vrc = RTFileGetSize(pFile, &cbFile);
982 if (cbFile > maxFileSize)
983 throw setError(VBOX_E_FILE_ERROR,
984 tr("Size of the manifest file '%s' is bigger than 1Mb. Check it, please."),
985 RTPathFilename(strMfFile.c_str()));
986
987 if (RT_SUCCESS(vrc))
988 pBuf = RTMemAllocZ(cbFile);
989 else
990 throw setError(VBOX_E_FILE_ERROR,
991 tr("Could not get size of the manifest file '%s' "),
992 RTPathFilename(strMfFile.c_str()));
993
994 vrc = RTFileRead(pFile, pBuf, cbFile, &cbRead);
995
996 if (RT_FAILURE(vrc))
997 {
998 if (pBuf)
999 RTMemFree(pBuf);
1000 throw setError(VBOX_E_FILE_ERROR,
1001 tr("Could not read the manifest file '%s' (%Rrc)"),
1002 RTPathFilename(strMfFile.c_str()), vrc);
1003 }
1004
1005 RTFileClose(pFile);
1006
1007 RTDIGESTTYPE digestType;
1008 vrc = RTManifestVerifyDigestType(pBuf, cbRead, &digestType);
1009
1010 if (pBuf)
1011 RTMemFree(pBuf);
1012
1013 if (RT_FAILURE(vrc))
1014 {
1015 throw setError(VBOX_E_FILE_ERROR,
1016 tr("Could not verify supported digest types in the manifest file '%s' (%Rrc)"),
1017 RTPathFilename(strMfFile.c_str()), vrc);
1018 }
1019
1020 storage.fCreateDigest = true;
1021
1022 if (digestType == RTDIGESTTYPE_SHA256)
1023 {
1024 storage.fSha256 = true;
1025 }
1026
1027 Utf8Str name = applianceIOName(applianceIOFile);
1028
1029 vrc = VDInterfaceAdd(&pFileIo->Core, name.c_str(),
1030 VDINTERFACETYPE_IO, 0, sizeof(VDINTERFACEIO),
1031 &storage.pVDImageIfaces);
1032 if (RT_FAILURE(vrc))
1033 throw setError(VBOX_E_IPRT_ERROR, "Creation of the VD interface failed (%Rrc)", vrc);
1034
1035 rc = readFSImpl(pTask, pTask->locInfo.strPath, pShaIo, &storage);
1036 if (FAILED(rc))
1037 break;
1038 }
1039 else
1040 {
1041 throw setError(VBOX_E_FILE_ERROR,
1042 tr("Could not open the manifest file '%s' (%Rrc)"),
1043 RTPathFilename(strMfFile.c_str()), vrc);
1044 }
1045 }
1046 else
1047 {
1048 storage.fCreateDigest = false;
1049 rc = readFSImpl(pTask, pTask->locInfo.strPath, pFileIo, &storage);
1050 if (FAILED(rc))
1051 break;
1052 }
1053 }
1054 catch (HRESULT rc2)
1055 {
1056 rc = rc2;
1057 }
1058
1059 }while (0);
1060
1061 /* Cleanup */
1062 if (pShaIo)
1063 RTMemFree(pShaIo);
1064 if (pFileIo)
1065 RTMemFree(pFileIo);
1066
1067 LogFlowFunc(("rc=%Rhrc\n", rc));
1068 LogFlowFuncLeave();
1069
1070 return rc;
1071}
1072
1073HRESULT Appliance::readFSOVA(TaskOVF *pTask)
1074{
1075 LogFlowFuncEnter();
1076
1077 RTTAR tar;
1078 HRESULT rc = S_OK;
1079 int vrc = 0;
1080 PVDINTERFACEIO pShaIo = 0;
1081 PVDINTERFACEIO pTarIo = 0;
1082 char *pszFilename = 0;
1083 SHASTORAGE storage;
1084
1085 RT_ZERO(storage);
1086
1087 vrc = RTTarOpen(&tar, pTask->locInfo.strPath.c_str(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, true);
1088 if (RT_FAILURE(vrc))
1089 rc = setError(VBOX_E_FILE_ERROR,
1090 tr("Could not open the OVA file '%s' (%Rrc)"),
1091 pTask->locInfo.strPath.c_str(), vrc);
1092 else
1093 {
1094 do
1095 {
1096 vrc = RTTarCurrentFile(tar, &pszFilename);
1097 if (RT_FAILURE(vrc))
1098 {
1099 rc = VBOX_E_FILE_ERROR;
1100 break;
1101 }
1102
1103 Utf8Str extension(RTPathExt(pszFilename));
1104
1105 if (!extension.endsWith(".ovf",Utf8Str::CaseInsensitive))
1106 {
1107 vrc = VERR_FILE_NOT_FOUND;
1108 rc = setError(VBOX_E_FILE_ERROR,
1109 tr("First file in the OVA package must have the extension 'ovf'. "
1110 "But the file '%s' has the different extension (%Rrc)"),
1111 pszFilename,
1112 vrc);
1113 break;
1114 }
1115
1116 pTarIo = TarCreateInterface();
1117 if (!pTarIo)
1118 {
1119 rc = E_OUTOFMEMORY;
1120 break;
1121 }
1122
1123 pShaIo = ShaCreateInterface();
1124 if (!pShaIo)
1125 {
1126 rc = E_OUTOFMEMORY;
1127 break ;
1128 }
1129
1130 Utf8Str name = applianceIOName(applianceIOTar);
1131
1132 vrc = VDInterfaceAdd(&pTarIo->Core, name.c_str(),
1133 VDINTERFACETYPE_IO, tar, sizeof(VDINTERFACEIO),
1134 &storage.pVDImageIfaces);
1135 if (RT_FAILURE(vrc))
1136 {
1137 rc = setError(VBOX_E_IPRT_ERROR, "Creation of the VD interface failed (%Rrc)", vrc);
1138 break;
1139 }
1140
1141 rc = readFSImpl(pTask, pszFilename, pShaIo, &storage);
1142 if (FAILED(rc))
1143 break;
1144
1145 } while (0);
1146
1147 RTTarClose(tar);
1148 }
1149
1150
1151
1152 /* Cleanup */
1153 if (pszFilename)
1154 RTMemFree(pszFilename);
1155 if (pShaIo)
1156 RTMemFree(pShaIo);
1157 if (pTarIo)
1158 RTMemFree(pTarIo);
1159
1160 LogFlowFunc(("rc=%Rhrc\n", rc));
1161 LogFlowFuncLeave();
1162
1163 return rc;
1164}
1165
1166HRESULT Appliance::readFSImpl(TaskOVF *pTask, const RTCString &strFilename, PVDINTERFACEIO pIfIo, PSHASTORAGE pStorage)
1167{
1168 LogFlowFuncEnter();
1169
1170 HRESULT rc = S_OK;
1171
1172 pStorage->fCreateDigest = true;
1173
1174 void *pvTmpBuf = 0;
1175 try
1176 {
1177 /* Read the OVF into a memory buffer */
1178 size_t cbSize = 0;
1179 int vrc = ShaReadBuf(strFilename.c_str(), &pvTmpBuf, &cbSize, pIfIo, pStorage);
1180 if (RT_FAILURE(vrc)
1181 || !pvTmpBuf)
1182 throw setError(VBOX_E_FILE_ERROR,
1183 tr("Could not read OVF file '%s' (%Rrc)"),
1184 RTPathFilename(strFilename.c_str()), vrc);
1185
1186 /* Read & parse the XML structure of the OVF file */
1187 m->pReader = new ovf::OVFReader(pvTmpBuf, cbSize, pTask->locInfo.strPath);
1188
1189 if (m->pReader->m_envelopeData.getOVFVersion() == ovf::OVFVersion_2_0)
1190 {
1191 m->fSha256 = true;
1192
1193 uint8_t digest[RTSHA256_HASH_SIZE];
1194 size_t cbDigest = RTSHA256_DIGEST_LEN;
1195 char *pszDigest;
1196
1197 RTSha256(pvTmpBuf, cbSize, &digest[0]);
1198
1199 vrc = RTStrAllocEx(&pszDigest, cbDigest + 1);
1200 if (RT_SUCCESS(vrc))
1201 vrc = RTSha256ToString(digest, pszDigest, cbDigest + 1);
1202 else
1203 throw setError(VBOX_E_FILE_ERROR,
1204 tr("Could not allocate string for SHA256 digest (%Rrc)"), vrc);
1205
1206 if (RT_SUCCESS(vrc))
1207 /* Copy the SHA256 sum of the OVF file for later validation */
1208 m->strOVFSHADigest = pszDigest;
1209 else
1210 throw setError(VBOX_E_FILE_ERROR,
1211 tr("Converting SHA256 digest to a string was failed (%Rrc)"), vrc);
1212
1213 RTStrFree(pszDigest);
1214
1215 }
1216 else
1217 {
1218 m->fSha256 = false;
1219 /* Copy the SHA1 sum of the OVF file for later validation */
1220 m->strOVFSHADigest = pStorage->strDigest;
1221 }
1222
1223 }
1224 catch (RTCError &x) // includes all XML exceptions
1225 {
1226 rc = setError(VBOX_E_FILE_ERROR,
1227 x.what());
1228 }
1229 catch (HRESULT aRC)
1230 {
1231 rc = aRC;
1232 }
1233
1234 /* Cleanup */
1235 if (pvTmpBuf)
1236 RTMemFree(pvTmpBuf);
1237
1238 LogFlowFunc(("rc=%Rhrc\n", rc));
1239 LogFlowFuncLeave();
1240
1241 return rc;
1242}
1243
1244#ifdef VBOX_WITH_S3
1245/**
1246 * Worker code for reading OVF from the cloud. This is called from Appliance::taskThreadImportOrExport()
1247 * in S3 mode and therefore runs on the OVF read worker thread. This then starts a second worker
1248 * thread to create temporary files (see Appliance::readFS()).
1249 *
1250 * @param pTask
1251 * @return
1252 */
1253HRESULT Appliance::readS3(TaskOVF *pTask)
1254{
1255 LogFlowFuncEnter();
1256 LogFlowFunc(("Appliance %p\n", this));
1257
1258 AutoCaller autoCaller(this);
1259 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1260
1261 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
1262
1263 HRESULT rc = S_OK;
1264 int vrc = VINF_SUCCESS;
1265 RTS3 hS3 = NIL_RTS3;
1266 char szOSTmpDir[RTPATH_MAX];
1267 RTPathTemp(szOSTmpDir, sizeof(szOSTmpDir));
1268 /* The template for the temporary directory created below */
1269 char *pszTmpDir = RTPathJoinA(szOSTmpDir, "vbox-ovf-XXXXXX");
1270 list< pair<Utf8Str, ULONG> > filesList;
1271 Utf8Str strTmpOvf;
1272
1273 try
1274 {
1275 /* Extract the bucket */
1276 Utf8Str tmpPath = pTask->locInfo.strPath;
1277 Utf8Str bucket;
1278 parseBucket(tmpPath, bucket);
1279
1280 /* We need a temporary directory which we can put the OVF file & all
1281 * disk images in */
1282 vrc = RTDirCreateTemp(pszTmpDir, 0700);
1283 if (RT_FAILURE(vrc))
1284 throw setError(VBOX_E_FILE_ERROR,
1285 tr("Cannot create temporary directory '%s'"), pszTmpDir);
1286
1287 /* The temporary name of the target OVF file */
1288 strTmpOvf = Utf8StrFmt("%s/%s", pszTmpDir, RTPathFilename(tmpPath.c_str()));
1289
1290 /* Next we have to download the OVF */
1291 vrc = RTS3Create(&hS3,
1292 pTask->locInfo.strUsername.c_str(),
1293 pTask->locInfo.strPassword.c_str(),
1294 pTask->locInfo.strHostname.c_str(),
1295 "virtualbox-agent/"VBOX_VERSION_STRING);
1296 if (RT_FAILURE(vrc))
1297 throw setError(VBOX_E_IPRT_ERROR,
1298 tr("Cannot create S3 service handler"));
1299 RTS3SetProgressCallback(hS3, pTask->updateProgress, &pTask);
1300
1301 /* Get it */
1302 char *pszFilename = RTPathFilename(strTmpOvf.c_str());
1303 vrc = RTS3GetKey(hS3, bucket.c_str(), pszFilename, strTmpOvf.c_str());
1304 if (RT_FAILURE(vrc))
1305 {
1306 if (vrc == VERR_S3_CANCELED)
1307 throw S_OK; /* todo: !!!!!!!!!!!!! */
1308 else if (vrc == VERR_S3_ACCESS_DENIED)
1309 throw setError(E_ACCESSDENIED,
1310 tr("Cannot download file '%s' from S3 storage server (Access denied). Make sure that "
1311 "your credentials are right. "
1312 "Also check that your host clock is properly synced"),
1313 pszFilename);
1314 else if (vrc == VERR_S3_NOT_FOUND)
1315 throw setError(VBOX_E_FILE_ERROR,
1316 tr("Cannot download file '%s' from S3 storage server (File not found)"), pszFilename);
1317 else
1318 throw setError(VBOX_E_IPRT_ERROR,
1319 tr("Cannot download file '%s' from S3 storage server (%Rrc)"), pszFilename, vrc);
1320 }
1321
1322 /* Close the connection early */
1323 RTS3Destroy(hS3);
1324 hS3 = NIL_RTS3;
1325
1326 pTask->pProgress->SetNextOperation(Bstr(tr("Reading")).raw(), 1);
1327
1328 /* Prepare the temporary reading of the OVF */
1329 ComObjPtr<Progress> progress;
1330 LocationInfo li;
1331 li.strPath = strTmpOvf;
1332 /* Start the reading from the fs */
1333 rc = readImpl(li, progress);
1334 if (FAILED(rc)) throw rc;
1335
1336 /* Unlock the appliance for the reading thread */
1337 appLock.release();
1338 /* Wait until the reading is done, but report the progress back to the
1339 caller */
1340 ComPtr<IProgress> progressInt(progress);
1341 waitForAsyncProgress(pTask->pProgress, progressInt); /* Any errors will be thrown */
1342
1343 /* Again lock the appliance for the next steps */
1344 appLock.acquire();
1345 }
1346 catch(HRESULT aRC)
1347 {
1348 rc = aRC;
1349 }
1350 /* Cleanup */
1351 RTS3Destroy(hS3);
1352 /* Delete all files which where temporary created */
1353 if (RTPathExists(strTmpOvf.c_str()))
1354 {
1355 vrc = RTFileDelete(strTmpOvf.c_str());
1356 if (RT_FAILURE(vrc))
1357 rc = setError(VBOX_E_FILE_ERROR,
1358 tr("Cannot delete file '%s' (%Rrc)"), strTmpOvf.c_str(), vrc);
1359 }
1360 /* Delete the temporary directory */
1361 if (RTPathExists(pszTmpDir))
1362 {
1363 vrc = RTDirRemove(pszTmpDir);
1364 if (RT_FAILURE(vrc))
1365 rc = setError(VBOX_E_FILE_ERROR,
1366 tr("Cannot delete temporary directory '%s' (%Rrc)"), pszTmpDir, vrc);
1367 }
1368 if (pszTmpDir)
1369 RTStrFree(pszTmpDir);
1370
1371 LogFlowFunc(("rc=%Rhrc\n", rc));
1372 LogFlowFuncLeave();
1373
1374 return rc;
1375}
1376#endif /* VBOX_WITH_S3 */
1377
1378/*******************************************************************************
1379 * Import stuff
1380 ******************************************************************************/
1381
1382/**
1383 * Implementation for importing OVF data into VirtualBox. This starts a new thread which will call
1384 * Appliance::taskThreadImportOrExport().
1385 *
1386 * This creates one or more new machines according to the VirtualSystemScription instances created by
1387 * Appliance::Interpret().
1388 *
1389 * This is in a separate private method because it is used from two locations:
1390 *
1391 * 1) from the public Appliance::ImportMachines().
1392 * 2) from Appliance::importS3(), which got called from a previous instance of Appliance::taskThreadImportOrExport().
1393 *
1394 * @param aLocInfo
1395 * @param aProgress
1396 * @return
1397 */
1398HRESULT Appliance::importImpl(const LocationInfo &locInfo,
1399 ComObjPtr<Progress> &progress)
1400{
1401 HRESULT rc = S_OK;
1402
1403 SetUpProgressMode mode;
1404 if (locInfo.storageType == VFSType_File)
1405 mode = ImportFile;
1406 else
1407 mode = ImportS3;
1408
1409 rc = setUpProgress(progress,
1410 BstrFmt(tr("Importing appliance '%s'"), locInfo.strPath.c_str()),
1411 mode);
1412 if (FAILED(rc)) throw rc;
1413
1414 /* Initialize our worker task */
1415 std::auto_ptr<TaskOVF> task(new TaskOVF(this, TaskOVF::Import, locInfo, progress));
1416
1417 rc = task->startThread();
1418 if (FAILED(rc)) throw rc;
1419
1420 /* Don't destruct on success */
1421 task.release();
1422
1423 return rc;
1424}
1425
1426/**
1427 * Actual worker code for importing OVF data into VirtualBox. This is called from Appliance::taskThreadImportOrExport()
1428 * and therefore runs on the OVF import worker thread. This creates one or more new machines according to the
1429 * VirtualSystemScription instances created by Appliance::Interpret().
1430 *
1431 * This runs in three contexts:
1432 *
1433 * 1) in a first worker thread; in that case, Appliance::ImportMachines() called Appliance::importImpl();
1434 *
1435 * 2) in a second worker thread; in that case, Appliance::ImportMachines() called Appliance::importImpl(), which
1436 * called Appliance::importFSOVA(), which called Appliance::importImpl(), which then called this again.
1437 *
1438 * 3) in a second worker thread; in that case, Appliance::ImportMachines() called Appliance::importImpl(), which
1439 * called Appliance::importS3(), which called Appliance::importImpl(), which then called this again.
1440 *
1441 * @param pTask
1442 * @return
1443 */
1444HRESULT Appliance::importFS(TaskOVF *pTask)
1445{
1446
1447 LogFlowFuncEnter();
1448 LogFlowFunc(("Appliance %p\n", this));
1449
1450 AutoCaller autoCaller(this);
1451 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1452
1453 /* Change the appliance state so we can safely leave the lock while doing
1454 * time-consuming disk imports; also the below method calls do all kinds of
1455 * locking which conflicts with the appliance object lock. */
1456 AutoWriteLock writeLock(this COMMA_LOCKVAL_SRC_POS);
1457 /* Check if the appliance is currently busy. */
1458 if (!isApplianceIdle())
1459 return E_ACCESSDENIED;
1460 /* Set the internal state to importing. */
1461 m->state = Data::ApplianceImporting;
1462
1463 HRESULT rc = S_OK;
1464
1465 /* Clear the list of imported machines, if any */
1466 m->llGuidsMachinesCreated.clear();
1467
1468 if (pTask->locInfo.strPath.endsWith(".ovf", Utf8Str::CaseInsensitive))
1469 rc = importFSOVF(pTask, writeLock);
1470 else
1471 rc = importFSOVA(pTask, writeLock);
1472
1473 if (FAILED(rc))
1474 {
1475 /* With _whatever_ error we've had, do a complete roll-back of
1476 * machines and disks we've created */
1477 writeLock.release();
1478 for (list<Guid>::iterator itID = m->llGuidsMachinesCreated.begin();
1479 itID != m->llGuidsMachinesCreated.end();
1480 ++itID)
1481 {
1482 Guid guid = *itID;
1483 Bstr bstrGuid = guid.toUtf16();
1484 ComPtr<IMachine> failedMachine;
1485 HRESULT rc2 = mVirtualBox->FindMachine(bstrGuid.raw(), failedMachine.asOutParam());
1486 if (SUCCEEDED(rc2))
1487 {
1488 SafeIfaceArray<IMedium> aMedia;
1489 rc2 = failedMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly, ComSafeArrayAsOutParam(aMedia));
1490 ComPtr<IProgress> pProgress2;
1491 rc2 = failedMachine->DeleteConfig(ComSafeArrayAsInParam(aMedia), pProgress2.asOutParam());
1492 pProgress2->WaitForCompletion(-1);
1493 }
1494 }
1495 writeLock.acquire();
1496 }
1497
1498 /* Reset the state so others can call methods again */
1499 m->state = Data::ApplianceIdle;
1500
1501 LogFlowFunc(("rc=%Rhrc\n", rc));
1502 LogFlowFuncLeave();
1503
1504 return rc;
1505}
1506
1507HRESULT Appliance::importFSOVF(TaskOVF *pTask, AutoWriteLockBase& writeLock)
1508{
1509 LogFlowFuncEnter();
1510
1511 HRESULT rc = S_OK;
1512
1513 PVDINTERFACEIO pShaIo = NULL;
1514 PVDINTERFACEIO pFileIo = NULL;
1515 void *pvMfBuf = NULL;
1516 void *pvCertBuf = NULL;
1517 writeLock.release();
1518 try
1519 {
1520 /* Create the necessary file access interfaces. */
1521 pFileIo = FileCreateInterface();
1522 if (!pFileIo)
1523 throw setError(E_OUTOFMEMORY);
1524
1525 Utf8Str strMfFile = Utf8Str(pTask->locInfo.strPath).stripExt().append(".mf");
1526
1527 SHASTORAGE storage;
1528 RT_ZERO(storage);
1529
1530 Utf8Str name = applianceIOName(applianceIOFile);
1531
1532 int vrc = VDInterfaceAdd(&pFileIo->Core, name.c_str(),
1533 VDINTERFACETYPE_IO, 0, sizeof(VDINTERFACEIO),
1534 &storage.pVDImageIfaces);
1535 if (RT_FAILURE(vrc))
1536 throw setError(VBOX_E_IPRT_ERROR, "Creation of the VD interface failed (%Rrc)", vrc);
1537
1538 /* Create the import stack for the rollback on errors. */
1539 ImportStack stack(pTask->locInfo, m->pReader->m_mapDisks, pTask->pProgress);
1540
1541 if (RTFileExists(strMfFile.c_str()))
1542 {
1543 pShaIo = ShaCreateInterface();
1544 if (!pShaIo)
1545 throw setError(E_OUTOFMEMORY);
1546
1547 Utf8Str nameSha = applianceIOName(applianceIOSha);
1548 /* Fill out interface descriptor. */
1549 pShaIo->Core.u32Magic = VDINTERFACE_MAGIC;
1550 pShaIo->Core.cbSize = sizeof(VDINTERFACEIO);
1551 pShaIo->Core.pszInterfaceName = nameSha.c_str();
1552 pShaIo->Core.enmInterface = VDINTERFACETYPE_IO;
1553 pShaIo->Core.pvUser = &storage;
1554 pShaIo->Core.pNext = NULL;
1555
1556 storage.fCreateDigest = true;
1557
1558 size_t cbMfSize = 0;
1559
1560 /* Now import the appliance. */
1561 importMachines(stack, pShaIo, &storage);
1562 /* Read & verify the manifest file. */
1563 /* Add the ovf file to the digest list. */
1564 stack.llSrcDisksDigest.push_front(STRPAIR(pTask->locInfo.strPath, m->strOVFSHADigest));
1565 rc = readFileToBuf(strMfFile, &pvMfBuf, &cbMfSize, true, pShaIo, &storage);
1566 if (FAILED(rc)) throw rc;
1567 rc = verifyManifestFile(strMfFile, stack, pvMfBuf, cbMfSize);
1568 if (FAILED(rc)) throw rc;
1569
1570 size_t cbCertSize = 0;
1571
1572 /* Save the SHA digest of the manifest file for the next validation */
1573 Utf8Str manifestShaDigest = storage.strDigest;
1574
1575 Utf8Str strCertFile = Utf8Str(pTask->locInfo.strPath).stripExt().append(".cert");
1576 if (RTFileExists(strCertFile.c_str()))
1577 {
1578 rc = readFileToBuf(strCertFile, &pvCertBuf, &cbCertSize, false, pShaIo, &storage);
1579 if (FAILED(rc)) throw rc;
1580
1581 /* verify Certificate */
1582 }
1583 }
1584 else
1585 {
1586 storage.fCreateDigest = false;
1587 importMachines(stack, pFileIo, &storage);
1588 }
1589 }
1590 catch (HRESULT rc2)
1591 {
1592 rc = rc2;
1593 }
1594 writeLock.acquire();
1595
1596 /* Cleanup */
1597 if (pvMfBuf)
1598 RTMemFree(pvMfBuf);
1599 if (pvCertBuf)
1600 RTMemFree(pvCertBuf);
1601 if (pShaIo)
1602 RTMemFree(pShaIo);
1603 if (pFileIo)
1604 RTMemFree(pFileIo);
1605
1606 LogFlowFunc(("rc=%Rhrc\n", rc));
1607 LogFlowFuncLeave();
1608
1609 return rc;
1610}
1611
1612HRESULT Appliance::importFSOVA(TaskOVF *pTask, AutoWriteLockBase& writeLock)
1613{
1614 LogFlowFuncEnter();
1615
1616 RTTAR tar;
1617 int vrc = RTTarOpen(&tar,
1618 pTask->locInfo.strPath.c_str(),
1619 RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, true);
1620 if (RT_FAILURE(vrc))
1621 return setError(VBOX_E_FILE_ERROR,
1622 tr("Could not open OVA file '%s' (%Rrc)"),
1623 pTask->locInfo.strPath.c_str(), vrc);
1624
1625 HRESULT rc = S_OK;
1626
1627 PVDINTERFACEIO pShaIo = 0;
1628 PVDINTERFACEIO pTarIo = 0;
1629 char *pszFilename = 0;
1630 void *pvMfBuf = 0;
1631 void *pvCertBuf = 0;
1632 Utf8Str OVFfilename;
1633
1634 writeLock.release();
1635 try
1636 {
1637 /* Create the necessary file access interfaces. */
1638 pShaIo = ShaCreateInterface();
1639 if (!pShaIo)
1640 throw setError(E_OUTOFMEMORY);
1641 pTarIo = TarCreateInterface();
1642 if (!pTarIo)
1643 throw setError(E_OUTOFMEMORY);
1644
1645 SHASTORAGE storage;
1646 RT_ZERO(storage);
1647
1648 Utf8Str nameTar = applianceIOName(applianceIOTar);
1649
1650 vrc = VDInterfaceAdd(&pTarIo->Core, nameTar.c_str(),
1651 VDINTERFACETYPE_IO, tar, sizeof(VDINTERFACEIO),
1652 &storage.pVDImageIfaces);
1653 if (RT_FAILURE(vrc))
1654 throw setError(VBOX_E_IPRT_ERROR,
1655 tr("Creation of the VD interface failed (%Rrc)"), vrc);
1656
1657 Utf8Str nameSha = applianceIOName(applianceIOSha);
1658 /* Fill out interface descriptor. */
1659 pShaIo->Core.u32Magic = VDINTERFACE_MAGIC;
1660 pShaIo->Core.cbSize = sizeof(VDINTERFACEIO);
1661 pShaIo->Core.pszInterfaceName = nameSha.c_str();
1662 pShaIo->Core.enmInterface = VDINTERFACETYPE_IO;
1663 pShaIo->Core.pvUser = &storage;
1664 pShaIo->Core.pNext = NULL;
1665
1666 /* Read the file name of the first file (need to be the ovf file). This
1667 * is how all internal files are named. */
1668 vrc = RTTarCurrentFile(tar, &pszFilename);
1669 if (RT_FAILURE(vrc))
1670 throw setError(VBOX_E_IPRT_ERROR,
1671 tr("Getting the OVF file within the archive failed (%Rrc)"), vrc);
1672 else
1673 {
1674 if (vrc == VINF_TAR_DIR_PATH)
1675 {
1676 throw setError(VBOX_E_FILE_ERROR,
1677 tr("Empty directory folder (%s) isn't allowed in the OVA package (%Rrc)"),
1678 pszFilename,
1679 vrc);
1680 }
1681 }
1682
1683 /* save original OVF filename */
1684 OVFfilename = pszFilename;
1685 size_t cbMfSize = 0;
1686 size_t cbCertSize = 0;
1687 Utf8Str strMfFile = (Utf8Str(pszFilename)).stripExt().append(".mf");
1688 Utf8Str strCertFile = (Utf8Str(pszFilename)).stripExt().append(".cert");
1689
1690 /* Skip the OVF file, cause this was read in IAppliance::Read already. */
1691 vrc = RTTarSeekNextFile(tar);
1692 if ( RT_FAILURE(vrc)
1693 && vrc != VERR_TAR_END_OF_FILE)
1694 throw setError(VBOX_E_IPRT_ERROR,
1695 tr("Seeking within the archive failed (%Rrc)"), vrc);
1696 else
1697 {
1698 RTTarCurrentFile(tar, &pszFilename);
1699 if (vrc == VINF_TAR_DIR_PATH)
1700 {
1701 throw setError(VBOX_E_FILE_ERROR,
1702 tr("Empty directory folder (%s) isn't allowed in the OVA package (%Rrc)"),
1703 pszFilename,
1704 vrc);
1705 }
1706 }
1707
1708 PVDINTERFACEIO pCallbacks = pShaIo;
1709 PSHASTORAGE pStorage = &storage;
1710
1711 /* We always need to create the digest, cause we didn't know if there
1712 * is a manifest file in the stream. */
1713 pStorage->fCreateDigest = true;
1714
1715 /* Create the import stack for the rollback on errors. */
1716 ImportStack stack(pTask->locInfo, m->pReader->m_mapDisks, pTask->pProgress);
1717 /*
1718 * Try to read the manifest file. First try.
1719 *
1720 * Note: This isn't fatal if the file is not found. The standard
1721 * defines 3 cases.
1722 * 1. no manifest file
1723 * 2. manifest file after the OVF file
1724 * 3. manifest file after all disk files
1725 * If we want streaming capabilities, we can't check if it is there by
1726 * searching for it. We have to try to open it on all possible places.
1727 * If it fails here, we will try it again after all disks where read.
1728 */
1729 rc = readTarFileToBuf(tar, strMfFile, &pvMfBuf, &cbMfSize, true, pCallbacks, pStorage);
1730 if (FAILED(rc)) throw rc;
1731
1732 /*
1733 * Try to read the certificate file. First try.
1734 * Logic is the same as with manifest file
1735 * Only if the manifest file had been read successfully before
1736 */
1737 vrc = RTTarCurrentFile(tar, &pszFilename);
1738 if (RT_SUCCESS(vrc))
1739 {
1740 if (pvMfBuf)
1741 {
1742 if (strCertFile.compare(pszFilename) == 0)
1743 {
1744 rc = readTarFileToBuf(tar, strCertFile, &pvCertBuf, &cbCertSize, false, pCallbacks, pStorage);
1745 if (FAILED(rc)) throw rc;
1746
1747 if (pvCertBuf)
1748 {
1749 /* verify the certificate */
1750 }
1751 }
1752 }
1753 }
1754
1755 /* Now import the appliance. */
1756 importMachines(stack, pCallbacks, pStorage);
1757 /* Try to read the manifest file. Second try. */
1758 if (!pvMfBuf)
1759 {
1760 rc = readTarFileToBuf(tar, strMfFile, &pvMfBuf, &cbMfSize, true, pCallbacks, pStorage);
1761 if (FAILED(rc)) throw rc;
1762
1763 /* If we were able to read a manifest file we can check it now. */
1764 if (pvMfBuf)
1765 {
1766 /* Add the ovf file to the digest list. */
1767 stack.llSrcDisksDigest.push_front(STRPAIR(OVFfilename, m->strOVFSHADigest));
1768 rc = verifyManifestFile(strMfFile, stack, pvMfBuf, cbMfSize);
1769 if (FAILED(rc)) throw rc;
1770
1771 /*
1772 * Try to read the certificate file. Second try.
1773 * Only if the manifest file had been read successfully before
1774 */
1775
1776 vrc = RTTarCurrentFile(tar, &pszFilename);
1777 if (RT_SUCCESS(vrc))
1778 {
1779 if (strCertFile.compare(pszFilename) == 0)
1780 {
1781 rc = readTarFileToBuf(tar, strCertFile, &pvCertBuf, &cbCertSize, false, pCallbacks, pStorage);
1782 if (FAILED(rc)) throw rc;
1783
1784 if (pvCertBuf)
1785 {
1786 /* verify the certificate */
1787 }
1788 }
1789 }
1790 }
1791 }
1792 }
1793 catch (HRESULT rc2)
1794 {
1795 rc = rc2;
1796 }
1797 writeLock.acquire();
1798
1799 RTTarClose(tar);
1800
1801 /* Cleanup */
1802 if (pszFilename)
1803 RTMemFree(pszFilename);
1804 if (pvMfBuf)
1805 RTMemFree(pvMfBuf);
1806 if (pShaIo)
1807 RTMemFree(pShaIo);
1808 if (pTarIo)
1809 RTMemFree(pTarIo);
1810 if (pvCertBuf)
1811 RTMemFree(pvCertBuf);
1812
1813 LogFlowFunc(("rc=%Rhrc\n", rc));
1814 LogFlowFuncLeave();
1815
1816 return rc;
1817}
1818
1819#ifdef VBOX_WITH_S3
1820/**
1821 * Worker code for importing OVF from the cloud. This is called from Appliance::taskThreadImportOrExport()
1822 * in S3 mode and therefore runs on the OVF import worker thread. This then starts a second worker
1823 * thread to import from temporary files (see Appliance::importFS()).
1824 * @param pTask
1825 * @return
1826 */
1827HRESULT Appliance::importS3(TaskOVF *pTask)
1828{
1829 LogFlowFuncEnter();
1830 LogFlowFunc(("Appliance %p\n", this));
1831
1832 AutoCaller autoCaller(this);
1833 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1834
1835 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
1836
1837 int vrc = VINF_SUCCESS;
1838 RTS3 hS3 = NIL_RTS3;
1839 char szOSTmpDir[RTPATH_MAX];
1840 RTPathTemp(szOSTmpDir, sizeof(szOSTmpDir));
1841 /* The template for the temporary directory created below */
1842 char *pszTmpDir = RTPathJoinA(szOSTmpDir, "vbox-ovf-XXXXXX");
1843 list< pair<Utf8Str, ULONG> > filesList;
1844
1845 HRESULT rc = S_OK;
1846 try
1847 {
1848 /* Extract the bucket */
1849 Utf8Str tmpPath = pTask->locInfo.strPath;
1850 Utf8Str bucket;
1851 parseBucket(tmpPath, bucket);
1852
1853 /* We need a temporary directory which we can put the all disk images
1854 * in */
1855 vrc = RTDirCreateTemp(pszTmpDir, 0700);
1856 if (RT_FAILURE(vrc))
1857 throw setError(VBOX_E_FILE_ERROR,
1858 tr("Cannot create temporary directory '%s' (%Rrc)"), pszTmpDir, vrc);
1859
1860 /* Add every disks of every virtual system to an internal list */
1861 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it;
1862 for (it = m->virtualSystemDescriptions.begin();
1863 it != m->virtualSystemDescriptions.end();
1864 ++it)
1865 {
1866 ComObjPtr<VirtualSystemDescription> vsdescThis = (*it);
1867 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
1868 std::list<VirtualSystemDescriptionEntry*>::const_iterator itH;
1869 for (itH = avsdeHDs.begin();
1870 itH != avsdeHDs.end();
1871 ++itH)
1872 {
1873 const Utf8Str &strTargetFile = (*itH)->strOvf;
1874 if (!strTargetFile.isEmpty())
1875 {
1876 /* The temporary name of the target disk file */
1877 Utf8StrFmt strTmpDisk("%s/%s", pszTmpDir, RTPathFilename(strTargetFile.c_str()));
1878 filesList.push_back(pair<Utf8Str, ULONG>(strTmpDisk, (*itH)->ulSizeMB));
1879 }
1880 }
1881 }
1882
1883 /* Next we have to download the disk images */
1884 vrc = RTS3Create(&hS3,
1885 pTask->locInfo.strUsername.c_str(),
1886 pTask->locInfo.strPassword.c_str(),
1887 pTask->locInfo.strHostname.c_str(),
1888 "virtualbox-agent/"VBOX_VERSION_STRING);
1889 if (RT_FAILURE(vrc))
1890 throw setError(VBOX_E_IPRT_ERROR,
1891 tr("Cannot create S3 service handler"));
1892 RTS3SetProgressCallback(hS3, pTask->updateProgress, &pTask);
1893
1894 /* Download all files */
1895 for (list< pair<Utf8Str, ULONG> >::const_iterator it1 = filesList.begin(); it1 != filesList.end(); ++it1)
1896 {
1897 const pair<Utf8Str, ULONG> &s = (*it1);
1898 const Utf8Str &strSrcFile = s.first;
1899 /* Construct the source file name */
1900 char *pszFilename = RTPathFilename(strSrcFile.c_str());
1901 /* Advance to the next operation */
1902 if (!pTask->pProgress.isNull())
1903 pTask->pProgress->SetNextOperation(BstrFmt(tr("Downloading file '%s'"), pszFilename).raw(), s.second);
1904
1905 vrc = RTS3GetKey(hS3, bucket.c_str(), pszFilename, strSrcFile.c_str());
1906 if (RT_FAILURE(vrc))
1907 {
1908 if (vrc == VERR_S3_CANCELED)
1909 throw S_OK; /* todo: !!!!!!!!!!!!! */
1910 else if (vrc == VERR_S3_ACCESS_DENIED)
1911 throw setError(E_ACCESSDENIED,
1912 tr("Cannot download file '%s' from S3 storage server (Access denied). "
1913 "Make sure that your credentials are right. Also check that your host clock is "
1914 "properly synced"),
1915 pszFilename);
1916 else if (vrc == VERR_S3_NOT_FOUND)
1917 throw setError(VBOX_E_FILE_ERROR,
1918 tr("Cannot download file '%s' from S3 storage server (File not found)"),
1919 pszFilename);
1920 else
1921 throw setError(VBOX_E_IPRT_ERROR,
1922 tr("Cannot download file '%s' from S3 storage server (%Rrc)"),
1923 pszFilename, vrc);
1924 }
1925 }
1926
1927 /* Provide a OVF file (haven't to exist) so the import routine can
1928 * figure out where the disk images/manifest file are located. */
1929 Utf8StrFmt strTmpOvf("%s/%s", pszTmpDir, RTPathFilename(tmpPath.c_str()));
1930 /* Now check if there is an manifest file. This is optional. */
1931 Utf8Str strManifestFile; //= queryManifestFileName(strTmpOvf);
1932// Utf8Str strManifestFile = queryManifestFileName(strTmpOvf);
1933 char *pszFilename = RTPathFilename(strManifestFile.c_str());
1934 if (!pTask->pProgress.isNull())
1935 pTask->pProgress->SetNextOperation(BstrFmt(tr("Downloading file '%s'"), pszFilename).raw(), 1);
1936
1937 /* Try to download it. If the error is VERR_S3_NOT_FOUND, it isn't fatal. */
1938 vrc = RTS3GetKey(hS3, bucket.c_str(), pszFilename, strManifestFile.c_str());
1939 if (RT_SUCCESS(vrc))
1940 filesList.push_back(pair<Utf8Str, ULONG>(strManifestFile, 0));
1941 else if (RT_FAILURE(vrc))
1942 {
1943 if (vrc == VERR_S3_CANCELED)
1944 throw S_OK; /* todo: !!!!!!!!!!!!! */
1945 else if (vrc == VERR_S3_NOT_FOUND)
1946 vrc = VINF_SUCCESS; /* Not found is ok */
1947 else if (vrc == VERR_S3_ACCESS_DENIED)
1948 throw setError(E_ACCESSDENIED,
1949 tr("Cannot download file '%s' from S3 storage server (Access denied)."
1950 "Make sure that your credentials are right. "
1951 "Also check that your host clock is properly synced"),
1952 pszFilename);
1953 else
1954 throw setError(VBOX_E_IPRT_ERROR,
1955 tr("Cannot download file '%s' from S3 storage server (%Rrc)"),
1956 pszFilename, vrc);
1957 }
1958
1959 /* Close the connection early */
1960 RTS3Destroy(hS3);
1961 hS3 = NIL_RTS3;
1962
1963 pTask->pProgress->SetNextOperation(BstrFmt(tr("Importing appliance")).raw(), m->ulWeightForXmlOperation);
1964
1965 ComObjPtr<Progress> progress;
1966 /* Import the whole temporary OVF & the disk images */
1967 LocationInfo li;
1968 li.strPath = strTmpOvf;
1969 rc = importImpl(li, progress);
1970 if (FAILED(rc)) throw rc;
1971
1972 /* Unlock the appliance for the fs import thread */
1973 appLock.release();
1974 /* Wait until the import is done, but report the progress back to the
1975 caller */
1976 ComPtr<IProgress> progressInt(progress);
1977 waitForAsyncProgress(pTask->pProgress, progressInt); /* Any errors will be thrown */
1978
1979 /* Again lock the appliance for the next steps */
1980 appLock.acquire();
1981 }
1982 catch(HRESULT aRC)
1983 {
1984 rc = aRC;
1985 }
1986 /* Cleanup */
1987 RTS3Destroy(hS3);
1988 /* Delete all files which where temporary created */
1989 for (list< pair<Utf8Str, ULONG> >::const_iterator it1 = filesList.begin(); it1 != filesList.end(); ++it1)
1990 {
1991 const char *pszFilePath = (*it1).first.c_str();
1992 if (RTPathExists(pszFilePath))
1993 {
1994 vrc = RTFileDelete(pszFilePath);
1995 if (RT_FAILURE(vrc))
1996 rc = setError(VBOX_E_FILE_ERROR,
1997 tr("Cannot delete file '%s' (%Rrc)"), pszFilePath, vrc);
1998 }
1999 }
2000 /* Delete the temporary directory */
2001 if (RTPathExists(pszTmpDir))
2002 {
2003 vrc = RTDirRemove(pszTmpDir);
2004 if (RT_FAILURE(vrc))
2005 rc = setError(VBOX_E_FILE_ERROR,
2006 tr("Cannot delete temporary directory '%s' (%Rrc)"), pszTmpDir, vrc);
2007 }
2008 if (pszTmpDir)
2009 RTStrFree(pszTmpDir);
2010
2011 LogFlowFunc(("rc=%Rhrc\n", rc));
2012 LogFlowFuncLeave();
2013
2014 return rc;
2015}
2016#endif /* VBOX_WITH_S3 */
2017
2018HRESULT Appliance::readFileToBuf(const Utf8Str &strFile,
2019 void **ppvBuf,
2020 size_t *pcbSize,
2021 bool fCreateDigest,
2022 PVDINTERFACEIO pCallbacks,
2023 PSHASTORAGE pStorage)
2024{
2025 HRESULT rc = S_OK;
2026
2027 bool fOldDigest = pStorage->fCreateDigest;/* Save the old digest property */
2028 pStorage->fCreateDigest = fCreateDigest;
2029 int vrc = ShaReadBuf(strFile.c_str(), ppvBuf, pcbSize, pCallbacks, pStorage);
2030 if ( RT_FAILURE(vrc)
2031 && vrc != VERR_FILE_NOT_FOUND)
2032 rc = setError(VBOX_E_FILE_ERROR,
2033 tr("Could not read file '%s' (%Rrc)"),
2034 RTPathFilename(strFile.c_str()), vrc);
2035 pStorage->fCreateDigest = fOldDigest; /* Restore the old digest creation behavior again. */
2036
2037 return rc;
2038}
2039
2040HRESULT Appliance::readTarFileToBuf(RTTAR tar,
2041 const Utf8Str &strFile,
2042 void **ppvBuf,
2043 size_t *pcbSize,
2044 bool fCreateDigest,
2045 PVDINTERFACEIO pCallbacks,
2046 PSHASTORAGE pStorage)
2047{
2048 HRESULT rc = S_OK;
2049
2050 char *pszCurFile;
2051 int vrc = RTTarCurrentFile(tar, &pszCurFile);
2052 if (RT_SUCCESS(vrc))
2053 {
2054 if (vrc == VINF_TAR_DIR_PATH)
2055 {
2056 rc = setError(VBOX_E_FILE_ERROR,
2057 tr("Empty directory folder (%s) isn't allowed in the OVA package (%Rrc)"),
2058 pszCurFile,
2059 vrc);
2060 }
2061 else
2062 {
2063 if (!strcmp(pszCurFile, RTPathFilename(strFile.c_str())))
2064 rc = readFileToBuf(strFile, ppvBuf, pcbSize, fCreateDigest, pCallbacks, pStorage);
2065 RTStrFree(pszCurFile);
2066 }
2067 }
2068 else if (vrc != VERR_TAR_END_OF_FILE)
2069 rc = setError(VBOX_E_IPRT_ERROR, "Seeking within the archive failed (%Rrc)", vrc);
2070
2071 return rc;
2072}
2073
2074HRESULT Appliance::verifyManifestFile(const Utf8Str &strFile, ImportStack &stack, void *pvBuf, size_t cbSize)
2075{
2076 HRESULT rc = S_OK;
2077
2078 PRTMANIFESTTEST paTests = (PRTMANIFESTTEST)RTMemAlloc(sizeof(RTMANIFESTTEST) * stack.llSrcDisksDigest.size());
2079 if (!paTests)
2080 return E_OUTOFMEMORY;
2081
2082 size_t i = 0;
2083 list<STRPAIR>::const_iterator it1;
2084 for (it1 = stack.llSrcDisksDigest.begin();
2085 it1 != stack.llSrcDisksDigest.end();
2086 ++it1, ++i)
2087 {
2088 paTests[i].pszTestFile = (*it1).first.c_str();
2089 paTests[i].pszTestDigest = (*it1).second.c_str();
2090 }
2091 size_t iFailed;
2092 int vrc = RTManifestVerifyFilesBuf(pvBuf, cbSize, paTests, stack.llSrcDisksDigest.size(), &iFailed);
2093 if (RT_UNLIKELY(vrc == VERR_MANIFEST_DIGEST_MISMATCH))
2094 rc = setError(VBOX_E_FILE_ERROR,
2095 tr("The SHA digest of '%s' does not match the one in '%s' (%Rrc)"),
2096 RTPathFilename(paTests[iFailed].pszTestFile), RTPathFilename(strFile.c_str()), vrc);
2097 else if (RT_FAILURE(vrc))
2098 rc = setError(VBOX_E_FILE_ERROR,
2099 tr("Could not verify the content of '%s' against the available files (%Rrc)"),
2100 RTPathFilename(strFile.c_str()), vrc);
2101
2102 RTMemFree(paTests);
2103
2104 return rc;
2105}
2106
2107
2108/**
2109 * Helper that converts VirtualSystem attachment values into VirtualBox attachment values.
2110 * Throws HRESULT values on errors!
2111 *
2112 * @param hdc in: the HardDiskController structure to attach to.
2113 * @param ulAddressOnParent in: the AddressOnParent parameter from OVF.
2114 * @param controllerType out: the name of the hard disk controller to attach to (e.g. "IDE Controller").
2115 * @param lControllerPort out: the channel (controller port) of the controller to attach to.
2116 * @param lDevice out: the device number to attach to.
2117 */
2118void Appliance::convertDiskAttachmentValues(const ovf::HardDiskController &hdc,
2119 uint32_t ulAddressOnParent,
2120 Bstr &controllerType,
2121 int32_t &lControllerPort,
2122 int32_t &lDevice)
2123{
2124 Log(("Appliance::convertDiskAttachmentValues: hdc.system=%d, hdc.fPrimary=%d, ulAddressOnParent=%d\n",
2125 hdc.system,
2126 hdc.fPrimary,
2127 ulAddressOnParent));
2128
2129 switch (hdc.system)
2130 {
2131 case ovf::HardDiskController::IDE:
2132 // For the IDE bus, the port parameter can be either 0 or 1, to specify the primary
2133 // or secondary IDE controller, respectively. For the primary controller of the IDE bus,
2134 // the device number can be either 0 or 1, to specify the master or the slave device,
2135 // respectively. For the secondary IDE controller, the device number is always 1 because
2136 // the master device is reserved for the CD-ROM drive.
2137 controllerType = Bstr("IDE Controller");
2138 switch (ulAddressOnParent)
2139 {
2140 case 0: // master
2141 if (!hdc.fPrimary)
2142 {
2143 // secondary master
2144 lControllerPort = (long)1;
2145 lDevice = (long)0;
2146 }
2147 else // primary master
2148 {
2149 lControllerPort = (long)0;
2150 lDevice = (long)0;
2151 }
2152 break;
2153
2154 case 1: // slave
2155 if (!hdc.fPrimary)
2156 {
2157 // secondary slave
2158 lControllerPort = (long)1;
2159 lDevice = (long)1;
2160 }
2161 else // primary slave
2162 {
2163 lControllerPort = (long)0;
2164 lDevice = (long)1;
2165 }
2166 break;
2167
2168 // used by older VBox exports
2169 case 2: // interpret this as secondary master
2170 lControllerPort = (long)1;
2171 lDevice = (long)0;
2172 break;
2173
2174 // used by older VBox exports
2175 case 3: // interpret this as secondary slave
2176 lControllerPort = (long)1;
2177 lDevice = (long)1;
2178 break;
2179
2180 default:
2181 throw setError(VBOX_E_NOT_SUPPORTED,
2182 tr("Invalid channel %RI16 specified; IDE controllers support only 0, 1 or 2"),
2183 ulAddressOnParent);
2184 break;
2185 }
2186 break;
2187
2188 case ovf::HardDiskController::SATA:
2189 controllerType = Bstr("SATA Controller");
2190 lControllerPort = (long)ulAddressOnParent;
2191 lDevice = (long)0;
2192 break;
2193
2194 case ovf::HardDiskController::SCSI:
2195 controllerType = Bstr("SCSI Controller");
2196 lControllerPort = (long)ulAddressOnParent;
2197 lDevice = (long)0;
2198 break;
2199
2200 default: break;
2201 }
2202
2203 Log(("=> lControllerPort=%d, lDevice=%d\n", lControllerPort, lDevice));
2204}
2205
2206/**
2207 * Imports one disk image. This is common code shared between
2208 * -- importMachineGeneric() for the OVF case; in that case the information comes from
2209 * the OVF virtual systems;
2210 * -- importVBoxMachine(); in that case, the information comes from the <vbox:Machine>
2211 * tag.
2212 *
2213 * Both ways of describing machines use the OVF disk references section, so in both cases
2214 * the caller needs to pass in the ovf::DiskImage structure from ovfreader.cpp.
2215 *
2216 * As a result, in both cases, if di.strHref is empty, we create a new disk as per the OVF
2217 * spec, even though this cannot really happen in the vbox:Machine case since such data
2218 * would never have been exported.
2219 *
2220 * This advances stack.pProgress by one operation with the disk's weight.
2221 *
2222 * @param di ovfreader.cpp structure describing the disk image from the OVF that is to be imported
2223 * @param strTargetPath Where to create the target image.
2224 * @param pTargetHD out: The newly created target disk. This also gets pushed on stack.llHardDisksCreated for cleanup.
2225 * @param stack
2226 */
2227void Appliance::importOneDiskImage(const ovf::DiskImage &di,
2228 Utf8Str *strTargetPath,
2229 ComObjPtr<Medium> &pTargetHD,
2230 ImportStack &stack,
2231 PVDINTERFACEIO pCallbacks,
2232 PSHASTORAGE pStorage)
2233{
2234 SHASTORAGE finalStorage;
2235 PSHASTORAGE pRealUsedStorage = pStorage;/* may be changed later to finalStorage */
2236 PVDINTERFACEIO pFileIo = NULL;/* used in GZIP case*/
2237 ComObjPtr<Progress> pProgress;
2238 pProgress.createObject();
2239 HRESULT rc = pProgress->init(mVirtualBox,
2240 static_cast<IAppliance*>(this),
2241 BstrFmt(tr("Creating medium '%s'"),
2242 strTargetPath->c_str()).raw(),
2243 TRUE);
2244 if (FAILED(rc)) throw rc;
2245
2246 /* Get the system properties. */
2247 SystemProperties *pSysProps = mVirtualBox->getSystemProperties();
2248
2249 /*
2250 * we put strSourceOVF into the stack.llSrcDisksDigest in the end of this
2251 * function like a key for a later validation of the SHA digests
2252 */
2253 const Utf8Str &strSourceOVF = di.strHref;
2254
2255 Utf8Str strSrcFilePath(stack.strSourceDir);
2256 Utf8Str strTargetDir(*strTargetPath);
2257
2258 /* Construct source file path */
2259 Utf8Str name = applianceIOName(applianceIOTar);
2260
2261 if (RTStrNICmp(pStorage->pVDImageIfaces->pszInterfaceName, name.c_str(), name.length()) == 0)
2262 strSrcFilePath = strSourceOVF;
2263 else
2264 {
2265 strSrcFilePath.append(RTPATH_SLASH_STR);
2266 strSrcFilePath.append(strSourceOVF);
2267 }
2268
2269 /* First of all check if the path is an UUID. If so, the user like to
2270 * import the disk into an existing path. This is useful for iSCSI for
2271 * example. */
2272 RTUUID uuid;
2273 int vrc = RTUuidFromStr(&uuid, strTargetPath->c_str());
2274 if (vrc == VINF_SUCCESS)
2275 {
2276 rc = mVirtualBox->findHardDiskById(Guid(uuid), true, &pTargetHD);
2277 if (FAILED(rc)) throw rc;
2278 }
2279 else
2280 {
2281 bool fGzipUsed = !(di.strCompression.compare("gzip",Utf8Str::CaseInsensitive));
2282 /* check read file to GZIP compression */
2283 try
2284 {
2285 if (fGzipUsed == true)
2286 {
2287 /*
2288 * Create the necessary file access interfaces.
2289 * For the next step:
2290 * We need to replace the previously created chain of SHA-TAR or SHA-FILE interfaces
2291 * with simple FILE interface because we don't need SHA or TAR interfaces here anymore.
2292 * But we mustn't delete the chain of SHA-TAR or SHA-FILE interfaces.
2293 */
2294
2295 /* Decompress the GZIP file and save a new file in the target path */
2296 strTargetDir = strTargetDir.stripFilename();
2297 strTargetDir.append("/temp_");
2298
2299 Utf8Str strTempTargetFilename(*strTargetPath);
2300 strTempTargetFilename = strTempTargetFilename.stripPath();
2301 strTempTargetFilename = strTempTargetFilename.stripExt();
2302 Utf8Str vdf = typeOfVirtualDiskFormatFromURI(di.strFormat);
2303
2304 strTargetDir.append(strTempTargetFilename);
2305
2306 vrc = decompressImageAndSave(strSrcFilePath.c_str(), strTargetDir.c_str(), pCallbacks, pStorage);
2307
2308 if (RT_FAILURE(vrc))
2309 throw setError(VBOX_E_FILE_ERROR,
2310 tr("Could not read the file '%s' (%Rrc)"),
2311 RTPathFilename(strSrcFilePath.c_str()), vrc);
2312
2313 /* Create the necessary file access interfaces. */
2314 pFileIo = FileCreateInterface();
2315 if (!pFileIo)
2316 throw setError(E_OUTOFMEMORY);
2317
2318 name = applianceIOName(applianceIOFile);
2319
2320 vrc = VDInterfaceAdd(&pFileIo->Core, name.c_str(),
2321 VDINTERFACETYPE_IO, NULL, sizeof(VDINTERFACEIO),
2322 &finalStorage.pVDImageIfaces);
2323 if (RT_FAILURE(vrc))
2324 throw setError(VBOX_E_IPRT_ERROR,
2325 tr("Creation of the VD interface failed (%Rrc)"), vrc);
2326
2327 /* Correct the source and the target with the actual values */
2328 strSrcFilePath = strTargetDir;
2329 strTargetDir = strTargetDir.stripFilename();
2330 strTargetDir.append(RTPATH_SLASH_STR);
2331 strTargetDir.append(strTempTargetFilename.c_str());
2332 *strTargetPath = strTargetDir.c_str();
2333
2334 pRealUsedStorage = &finalStorage;
2335 }
2336
2337 Utf8Str strTrgFormat = "VMDK";
2338 ULONG lCabs = 0;
2339 char *pszExt = NULL;
2340
2341 if (RTPathHaveExt(strTargetPath->c_str()))
2342 {
2343 pszExt = RTPathExt(strTargetPath->c_str());
2344 /* Figure out which format the user like to have. Default is VMDK. */
2345 ComObjPtr<MediumFormat> trgFormat = pSysProps->mediumFormatFromExtension(&pszExt[1]);
2346 if (trgFormat.isNull())
2347 throw setError(VBOX_E_NOT_SUPPORTED,
2348 tr("Could not find a valid medium format for the target disk '%s'"),
2349 strTargetPath->c_str());
2350 /* Check the capabilities. We need create capabilities. */
2351 lCabs = 0;
2352 com::SafeArray <MediumFormatCapabilities_T> mediumFormatCap;
2353 rc = trgFormat->COMGETTER(Capabilities)(ComSafeArrayAsOutParam(mediumFormatCap));
2354
2355 if (FAILED(rc))
2356 throw rc;
2357 else
2358 {
2359 for (ULONG j = 0; j < mediumFormatCap.size(); j++)
2360 lCabs |= mediumFormatCap[j];
2361 }
2362
2363 if (!( ((lCabs & MediumFormatCapabilities_CreateFixed) == MediumFormatCapabilities_CreateFixed)
2364 || ((lCabs & MediumFormatCapabilities_CreateDynamic) == MediumFormatCapabilities_CreateDynamic)))
2365 throw setError(VBOX_E_NOT_SUPPORTED,
2366 tr("Could not find a valid medium format for the target disk '%s'"),
2367 strTargetPath->c_str());
2368 Bstr bstrFormatName;
2369 rc = trgFormat->COMGETTER(Name)(bstrFormatName.asOutParam());
2370 if (FAILED(rc)) throw rc;
2371 strTrgFormat = Utf8Str(bstrFormatName);
2372 }
2373 else
2374 {
2375 throw setError(VBOX_E_FILE_ERROR,
2376 tr("The target disk '%s' has no extension "),
2377 strTargetPath->c_str(), VERR_INVALID_NAME);
2378 }
2379
2380 /* Create an IMedium object. */
2381 pTargetHD.createObject();
2382
2383 /*CD/DVD case*/
2384 if (strTrgFormat.compare("RAW", Utf8Str::CaseInsensitive) == 0)
2385 {
2386 try
2387 {
2388 if (fGzipUsed == true)
2389 {
2390 /*
2391 * The source and target pathes are the same.
2392 * It means that we have the needed file already.
2393 * For example, in GZIP case, we decompress the file and save it in the target path,
2394 * but with some prefix like "temp_". See part "check read file to GZIP compression" earlier
2395 * in this function.
2396 * Just rename the file by deleting "temp_" from it's name
2397 */
2398 vrc = RTFileRename(strSrcFilePath.c_str(), strTargetPath->c_str(), RTPATHRENAME_FLAGS_NO_REPLACE);
2399 if (RT_FAILURE(vrc))
2400 throw setError(VBOX_E_FILE_ERROR,
2401 tr("Could not rename the file '%s' (%Rrc)"),
2402 RTPathFilename(strSourceOVF.c_str()), vrc);
2403 }
2404 else
2405 {
2406 /* Calculating SHA digest for ISO file while copying one */
2407 vrc = copyFileAndCalcShaDigest(strSrcFilePath.c_str(),
2408 strTargetPath->c_str(),
2409 pCallbacks,
2410 pRealUsedStorage);
2411
2412 if (RT_FAILURE(vrc))
2413 throw setError(VBOX_E_FILE_ERROR,
2414 tr("Could not copy ISO file '%s' listed in the OVF file (%Rrc)"),
2415 RTPathFilename(strSourceOVF.c_str()), vrc);
2416 }
2417 }
2418 catch (HRESULT arc)
2419 {
2420 throw;
2421 }
2422
2423 /* Advance to the next operation. */
2424 /* operation's weight, as set up with the IProgress originally */
2425 stack.pProgress->SetNextOperation(BstrFmt(tr("Importing virtual disk image '%s'"),
2426 RTPathFilename(strSourceOVF.c_str())).raw(),
2427 di.ulSuggestedSizeMB);
2428 }
2429 else/* HDD case*/
2430 {
2431 rc = pTargetHD->init(mVirtualBox,
2432 strTrgFormat,
2433 *strTargetPath,
2434 Guid::Empty /* media registry: none yet */);
2435 if (FAILED(rc)) throw rc;
2436
2437 /* Now create an empty hard disk. */
2438 rc = mVirtualBox->CreateHardDisk(Bstr(strTrgFormat).raw(),
2439 Bstr(*strTargetPath).raw(),
2440 ComPtr<IMedium>(pTargetHD).asOutParam());
2441 if (FAILED(rc)) throw rc;
2442
2443 /* If strHref is empty we have to create a new file. */
2444 if (strSourceOVF.isEmpty())
2445 {
2446 com::SafeArray<MediumVariant_T> mediumVariant;
2447 mediumVariant.push_back(MediumVariant_Standard);
2448 /* Create a dynamic growing disk image with the given capacity. */
2449 rc = pTargetHD->CreateBaseStorage(di.iCapacity / _1M,
2450 ComSafeArrayAsInParam(mediumVariant),
2451 ComPtr<IProgress>(pProgress).asOutParam());
2452 if (FAILED(rc)) throw rc;
2453
2454 /* Advance to the next operation. */
2455 /* operation's weight, as set up with the IProgress originally */
2456 stack.pProgress->SetNextOperation(BstrFmt(tr("Creating disk image '%s'"),
2457 strTargetPath->c_str()).raw(),
2458 di.ulSuggestedSizeMB);
2459 }
2460 else
2461 {
2462 /* We need a proper source format description */
2463 ComObjPtr<MediumFormat> srcFormat;
2464 /* Which format to use? */
2465 Utf8Str strSrcFormat = "VDI";
2466
2467 std::set<Utf8Str> listURIs = Appliance::URIFromTypeOfVirtualDiskFormat("VMDK");
2468 std::set<Utf8Str>::const_iterator itr = listURIs.find(di.strFormat);
2469
2470 if (itr != listURIs.end())
2471 {
2472 strSrcFormat = "VMDK";
2473 }
2474
2475 srcFormat = pSysProps->mediumFormat(strSrcFormat);
2476 if (srcFormat.isNull())
2477 throw setError(VBOX_E_NOT_SUPPORTED,
2478 tr("Could not find a valid medium format for the source disk '%s'"),
2479 RTPathFilename(strSourceOVF.c_str()));
2480
2481 /* Clone the source disk image */
2482 ComObjPtr<Medium> nullParent;
2483 rc = pTargetHD->importFile(strSrcFilePath.c_str(),
2484 srcFormat,
2485 MediumVariant_Standard,
2486 pCallbacks, pRealUsedStorage,
2487 nullParent,
2488 pProgress);
2489 if (FAILED(rc)) throw rc;
2490
2491
2492
2493 /* Advance to the next operation. */
2494 /* operation's weight, as set up with the IProgress originally */
2495 stack.pProgress->SetNextOperation(BstrFmt(tr("Importing virtual disk image '%s'"),
2496 RTPathFilename(strSourceOVF.c_str())).raw(),
2497 di.ulSuggestedSizeMB);
2498 }
2499
2500 /* Now wait for the background disk operation to complete; this throws
2501 * HRESULTs on error. */
2502 ComPtr<IProgress> pp(pProgress);
2503 waitForAsyncProgress(stack.pProgress, pp);
2504
2505 if (fGzipUsed == true)
2506 {
2507 /*
2508 * Just delete the temporary file
2509 */
2510 vrc = RTFileDelete(strSrcFilePath.c_str());
2511 if (RT_FAILURE(vrc))
2512 setWarning(VBOX_E_FILE_ERROR,
2513 tr("Could not delete the file '%s' (%Rrc)"),
2514 RTPathFilename(strSrcFilePath.c_str()), vrc);
2515 }
2516 }
2517 }
2518 catch (...)
2519 {
2520 if (pFileIo)
2521 RTMemFree(pFileIo);
2522
2523 throw;
2524 }
2525 }
2526
2527 if (pFileIo)
2528 RTMemFree(pFileIo);
2529
2530 /* Add the newly create disk path + a corresponding digest the our list for
2531 * later manifest verification. */
2532 stack.llSrcDisksDigest.push_back(STRPAIR(strSourceOVF, pStorage ? pStorage->strDigest : ""));
2533}
2534
2535/**
2536 * Imports one OVF virtual system (described by the given ovf::VirtualSystem and VirtualSystemDescription)
2537 * into VirtualBox by creating an IMachine instance, which is returned.
2538 *
2539 * This throws HRESULT error codes for anything that goes wrong, in which case the caller must clean
2540 * up any leftovers from this function. For this, the given ImportStack instance has received information
2541 * about what needs cleaning up (to support rollback).
2542 *
2543 * @param vsysThis OVF virtual system (machine) to import.
2544 * @param vsdescThis Matching virtual system description (machine) to import.
2545 * @param pNewMachine out: Newly created machine.
2546 * @param stack Cleanup stack for when this throws.
2547 */
2548void Appliance::importMachineGeneric(const ovf::VirtualSystem &vsysThis,
2549 ComObjPtr<VirtualSystemDescription> &vsdescThis,
2550 ComPtr<IMachine> &pNewMachine,
2551 ImportStack &stack,
2552 PVDINTERFACEIO pCallbacks,
2553 PSHASTORAGE pStorage)
2554{
2555 HRESULT rc;
2556
2557 // Get the instance of IGuestOSType which matches our string guest OS type so we
2558 // can use recommended defaults for the new machine where OVF doesn't provide any
2559 ComPtr<IGuestOSType> osType;
2560 rc = mVirtualBox->GetGuestOSType(Bstr(stack.strOsTypeVBox).raw(), osType.asOutParam());
2561 if (FAILED(rc)) throw rc;
2562
2563 /* Create the machine */
2564 SafeArray<BSTR> groups; /* no groups */
2565 rc = mVirtualBox->CreateMachine(NULL, /* machine name: use default */
2566 Bstr(stack.strNameVBox).raw(),
2567 ComSafeArrayAsInParam(groups),
2568 Bstr(stack.strOsTypeVBox).raw(),
2569 NULL, /* aCreateFlags */
2570 pNewMachine.asOutParam());
2571 if (FAILED(rc)) throw rc;
2572
2573 // set the description
2574 if (!stack.strDescription.isEmpty())
2575 {
2576 rc = pNewMachine->COMSETTER(Description)(Bstr(stack.strDescription).raw());
2577 if (FAILED(rc)) throw rc;
2578 }
2579
2580 // CPU count
2581 rc = pNewMachine->COMSETTER(CPUCount)(stack.cCPUs);
2582 if (FAILED(rc)) throw rc;
2583
2584 if (stack.fForceHWVirt)
2585 {
2586 rc = pNewMachine->SetHWVirtExProperty(HWVirtExPropertyType_Enabled, TRUE);
2587 if (FAILED(rc)) throw rc;
2588 }
2589
2590 // RAM
2591 rc = pNewMachine->COMSETTER(MemorySize)(stack.ulMemorySizeMB);
2592 if (FAILED(rc)) throw rc;
2593
2594 /* VRAM */
2595 /* Get the recommended VRAM for this guest OS type */
2596 ULONG vramVBox;
2597 rc = osType->COMGETTER(RecommendedVRAM)(&vramVBox);
2598 if (FAILED(rc)) throw rc;
2599
2600 /* Set the VRAM */
2601 rc = pNewMachine->COMSETTER(VRAMSize)(vramVBox);
2602 if (FAILED(rc)) throw rc;
2603
2604 // I/O APIC: Generic OVF has no setting for this. Enable it if we
2605 // import a Windows VM because if if Windows was installed without IOAPIC,
2606 // it will not mind finding an one later on, but if Windows was installed
2607 // _with_ an IOAPIC, it will bluescreen if it's not found
2608 if (!stack.fForceIOAPIC)
2609 {
2610 Bstr bstrFamilyId;
2611 rc = osType->COMGETTER(FamilyId)(bstrFamilyId.asOutParam());
2612 if (FAILED(rc)) throw rc;
2613 if (bstrFamilyId == "Windows")
2614 stack.fForceIOAPIC = true;
2615 }
2616
2617 if (stack.fForceIOAPIC)
2618 {
2619 ComPtr<IBIOSSettings> pBIOSSettings;
2620 rc = pNewMachine->COMGETTER(BIOSSettings)(pBIOSSettings.asOutParam());
2621 if (FAILED(rc)) throw rc;
2622
2623 rc = pBIOSSettings->COMSETTER(IOAPICEnabled)(TRUE);
2624 if (FAILED(rc)) throw rc;
2625 }
2626
2627 if (!stack.strAudioAdapter.isEmpty())
2628 if (stack.strAudioAdapter.compare("null", Utf8Str::CaseInsensitive) != 0)
2629 {
2630 uint32_t audio = RTStrToUInt32(stack.strAudioAdapter.c_str()); // should be 0 for AC97
2631 ComPtr<IAudioAdapter> audioAdapter;
2632 rc = pNewMachine->COMGETTER(AudioAdapter)(audioAdapter.asOutParam());
2633 if (FAILED(rc)) throw rc;
2634 rc = audioAdapter->COMSETTER(Enabled)(true);
2635 if (FAILED(rc)) throw rc;
2636 rc = audioAdapter->COMSETTER(AudioController)(static_cast<AudioControllerType_T>(audio));
2637 if (FAILED(rc)) throw rc;
2638 }
2639
2640#ifdef VBOX_WITH_USB
2641 /* USB Controller */
2642 if (stack.fUSBEnabled)
2643 {
2644 ComPtr<IUSBController> usbController;
2645 rc = pNewMachine->AddUSBController(Bstr("OHCI").raw(), USBControllerType_OHCI, usbController.asOutParam());
2646 if (FAILED(rc)) throw rc;
2647 }
2648#endif /* VBOX_WITH_USB */
2649
2650 /* Change the network adapters */
2651 uint32_t maxNetworkAdapters = Global::getMaxNetworkAdapters(ChipsetType_PIIX3);
2652
2653 std::list<VirtualSystemDescriptionEntry*> vsdeNW = vsdescThis->findByType(VirtualSystemDescriptionType_NetworkAdapter);
2654 if (vsdeNW.size() == 0)
2655 {
2656 /* No network adapters, so we have to disable our default one */
2657 ComPtr<INetworkAdapter> nwVBox;
2658 rc = pNewMachine->GetNetworkAdapter(0, nwVBox.asOutParam());
2659 if (FAILED(rc)) throw rc;
2660 rc = nwVBox->COMSETTER(Enabled)(false);
2661 if (FAILED(rc)) throw rc;
2662 }
2663 else if (vsdeNW.size() > maxNetworkAdapters)
2664 throw setError(VBOX_E_FILE_ERROR,
2665 tr("Too many network adapters: OVF requests %d network adapters, "
2666 "but VirtualBox only supports %d"),
2667 vsdeNW.size(), maxNetworkAdapters);
2668 else
2669 {
2670 list<VirtualSystemDescriptionEntry*>::const_iterator nwIt;
2671 size_t a = 0;
2672 for (nwIt = vsdeNW.begin();
2673 nwIt != vsdeNW.end();
2674 ++nwIt, ++a)
2675 {
2676 const VirtualSystemDescriptionEntry* pvsys = *nwIt;
2677
2678 const Utf8Str &nwTypeVBox = pvsys->strVboxCurrent;
2679 uint32_t tt1 = RTStrToUInt32(nwTypeVBox.c_str());
2680 ComPtr<INetworkAdapter> pNetworkAdapter;
2681 rc = pNewMachine->GetNetworkAdapter((ULONG)a, pNetworkAdapter.asOutParam());
2682 if (FAILED(rc)) throw rc;
2683 /* Enable the network card & set the adapter type */
2684 rc = pNetworkAdapter->COMSETTER(Enabled)(true);
2685 if (FAILED(rc)) throw rc;
2686 rc = pNetworkAdapter->COMSETTER(AdapterType)(static_cast<NetworkAdapterType_T>(tt1));
2687 if (FAILED(rc)) throw rc;
2688
2689 // default is NAT; change to "bridged" if extra conf says so
2690 if (pvsys->strExtraConfigCurrent.endsWith("type=Bridged", Utf8Str::CaseInsensitive))
2691 {
2692 /* Attach to the right interface */
2693 rc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_Bridged);
2694 if (FAILED(rc)) throw rc;
2695 ComPtr<IHost> host;
2696 rc = mVirtualBox->COMGETTER(Host)(host.asOutParam());
2697 if (FAILED(rc)) throw rc;
2698 com::SafeIfaceArray<IHostNetworkInterface> nwInterfaces;
2699 rc = host->COMGETTER(NetworkInterfaces)(ComSafeArrayAsOutParam(nwInterfaces));
2700 if (FAILED(rc)) throw rc;
2701 // We search for the first host network interface which
2702 // is usable for bridged networking
2703 for (size_t j = 0;
2704 j < nwInterfaces.size();
2705 ++j)
2706 {
2707 HostNetworkInterfaceType_T itype;
2708 rc = nwInterfaces[j]->COMGETTER(InterfaceType)(&itype);
2709 if (FAILED(rc)) throw rc;
2710 if (itype == HostNetworkInterfaceType_Bridged)
2711 {
2712 Bstr name;
2713 rc = nwInterfaces[j]->COMGETTER(Name)(name.asOutParam());
2714 if (FAILED(rc)) throw rc;
2715 /* Set the interface name to attach to */
2716 rc = pNetworkAdapter->COMSETTER(BridgedInterface)(name.raw());
2717 if (FAILED(rc)) throw rc;
2718 break;
2719 }
2720 }
2721 }
2722 /* Next test for host only interfaces */
2723 else if (pvsys->strExtraConfigCurrent.endsWith("type=HostOnly", Utf8Str::CaseInsensitive))
2724 {
2725 /* Attach to the right interface */
2726 rc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_HostOnly);
2727 if (FAILED(rc)) throw rc;
2728 ComPtr<IHost> host;
2729 rc = mVirtualBox->COMGETTER(Host)(host.asOutParam());
2730 if (FAILED(rc)) throw rc;
2731 com::SafeIfaceArray<IHostNetworkInterface> nwInterfaces;
2732 rc = host->COMGETTER(NetworkInterfaces)(ComSafeArrayAsOutParam(nwInterfaces));
2733 if (FAILED(rc)) throw rc;
2734 // We search for the first host network interface which
2735 // is usable for host only networking
2736 for (size_t j = 0;
2737 j < nwInterfaces.size();
2738 ++j)
2739 {
2740 HostNetworkInterfaceType_T itype;
2741 rc = nwInterfaces[j]->COMGETTER(InterfaceType)(&itype);
2742 if (FAILED(rc)) throw rc;
2743 if (itype == HostNetworkInterfaceType_HostOnly)
2744 {
2745 Bstr name;
2746 rc = nwInterfaces[j]->COMGETTER(Name)(name.asOutParam());
2747 if (FAILED(rc)) throw rc;
2748 /* Set the interface name to attach to */
2749 rc = pNetworkAdapter->COMSETTER(HostOnlyInterface)(name.raw());
2750 if (FAILED(rc)) throw rc;
2751 break;
2752 }
2753 }
2754 }
2755 /* Next test for internal interfaces */
2756 else if (pvsys->strExtraConfigCurrent.endsWith("type=Internal", Utf8Str::CaseInsensitive))
2757 {
2758 /* Attach to the right interface */
2759 rc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_Internal);
2760 if (FAILED(rc)) throw rc;
2761 }
2762 /* Next test for Generic interfaces */
2763 else if (pvsys->strExtraConfigCurrent.endsWith("type=Generic", Utf8Str::CaseInsensitive))
2764 {
2765 /* Attach to the right interface */
2766 rc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_Generic);
2767 if (FAILED(rc)) throw rc;
2768 }
2769 /* Next test for NAT network interfaces */
2770 else if (pvsys->strExtraConfigCurrent.endsWith("type=NATNetwork", Utf8Str::CaseInsensitive))
2771 {
2772 /* Attach to the right interface */
2773 rc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_NATNetwork);
2774 if (FAILED(rc)) throw rc;
2775 com::SafeIfaceArray<INATNetwork> nwNATNetworks;
2776 rc = mVirtualBox->COMGETTER(NATNetworks)(ComSafeArrayAsOutParam(nwNATNetworks));
2777 if (FAILED(rc)) throw rc;
2778 // Pick the first NAT network (if there is any)
2779 if (nwNATNetworks.size())
2780 {
2781 Bstr name;
2782 rc = nwNATNetworks[0]->COMGETTER(NetworkName)(name.asOutParam());
2783 if (FAILED(rc)) throw rc;
2784 /* Set the NAT network name to attach to */
2785 rc = pNetworkAdapter->COMSETTER(NATNetwork)(name.raw());
2786 if (FAILED(rc)) throw rc;
2787 break;
2788 }
2789 }
2790 }
2791 }
2792
2793 // IDE Hard disk controller
2794 std::list<VirtualSystemDescriptionEntry*> vsdeHDCIDE = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerIDE);
2795 /*
2796 * In OVF (at least VMware's version of it), an IDE controller has two ports,
2797 * so VirtualBox's single IDE controller with two channels and two ports each counts as
2798 * two OVF IDE controllers -- so we accept one or two such IDE controllers
2799 */
2800 size_t cIDEControllers = vsdeHDCIDE.size();
2801 if (cIDEControllers > 2)
2802 throw setError(VBOX_E_FILE_ERROR,
2803 tr("Too many IDE controllers in OVF; import facility only supports two"));
2804 if (vsdeHDCIDE.size() > 0)
2805 {
2806 // one or two IDE controllers present in OVF: add one VirtualBox controller
2807 ComPtr<IStorageController> pController;
2808 rc = pNewMachine->AddStorageController(Bstr("IDE Controller").raw(), StorageBus_IDE, pController.asOutParam());
2809 if (FAILED(rc)) throw rc;
2810
2811 const char *pcszIDEType = vsdeHDCIDE.front()->strVboxCurrent.c_str();
2812 if (!strcmp(pcszIDEType, "PIIX3"))
2813 rc = pController->COMSETTER(ControllerType)(StorageControllerType_PIIX3);
2814 else if (!strcmp(pcszIDEType, "PIIX4"))
2815 rc = pController->COMSETTER(ControllerType)(StorageControllerType_PIIX4);
2816 else if (!strcmp(pcszIDEType, "ICH6"))
2817 rc = pController->COMSETTER(ControllerType)(StorageControllerType_ICH6);
2818 else
2819 throw setError(VBOX_E_FILE_ERROR,
2820 tr("Invalid IDE controller type \"%s\""),
2821 pcszIDEType);
2822 if (FAILED(rc)) throw rc;
2823 }
2824
2825 /* Hard disk controller SATA */
2826 std::list<VirtualSystemDescriptionEntry*> vsdeHDCSATA = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerSATA);
2827 if (vsdeHDCSATA.size() > 1)
2828 throw setError(VBOX_E_FILE_ERROR,
2829 tr("Too many SATA controllers in OVF; import facility only supports one"));
2830 if (vsdeHDCSATA.size() > 0)
2831 {
2832 ComPtr<IStorageController> pController;
2833 const Utf8Str &hdcVBox = vsdeHDCSATA.front()->strVboxCurrent;
2834 if (hdcVBox == "AHCI")
2835 {
2836 rc = pNewMachine->AddStorageController(Bstr("SATA Controller").raw(),
2837 StorageBus_SATA,
2838 pController.asOutParam());
2839 if (FAILED(rc)) throw rc;
2840 }
2841 else
2842 throw setError(VBOX_E_FILE_ERROR,
2843 tr("Invalid SATA controller type \"%s\""),
2844 hdcVBox.c_str());
2845 }
2846
2847 /* Hard disk controller SCSI */
2848 std::list<VirtualSystemDescriptionEntry*> vsdeHDCSCSI = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerSCSI);
2849 if (vsdeHDCSCSI.size() > 1)
2850 throw setError(VBOX_E_FILE_ERROR,
2851 tr("Too many SCSI controllers in OVF; import facility only supports one"));
2852 if (vsdeHDCSCSI.size() > 0)
2853 {
2854 ComPtr<IStorageController> pController;
2855 Bstr bstrName(L"SCSI Controller");
2856 StorageBus_T busType = StorageBus_SCSI;
2857 StorageControllerType_T controllerType;
2858 const Utf8Str &hdcVBox = vsdeHDCSCSI.front()->strVboxCurrent;
2859 if (hdcVBox == "LsiLogic")
2860 controllerType = StorageControllerType_LsiLogic;
2861 else if (hdcVBox == "LsiLogicSas")
2862 {
2863 // OVF treats LsiLogicSas as a SCSI controller but VBox considers it a class of its own
2864 bstrName = L"SAS Controller";
2865 busType = StorageBus_SAS;
2866 controllerType = StorageControllerType_LsiLogicSas;
2867 }
2868 else if (hdcVBox == "BusLogic")
2869 controllerType = StorageControllerType_BusLogic;
2870 else
2871 throw setError(VBOX_E_FILE_ERROR,
2872 tr("Invalid SCSI controller type \"%s\""),
2873 hdcVBox.c_str());
2874
2875 rc = pNewMachine->AddStorageController(bstrName.raw(), busType, pController.asOutParam());
2876 if (FAILED(rc)) throw rc;
2877 rc = pController->COMSETTER(ControllerType)(controllerType);
2878 if (FAILED(rc)) throw rc;
2879 }
2880
2881 /* Hard disk controller SAS */
2882 std::list<VirtualSystemDescriptionEntry*> vsdeHDCSAS = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerSAS);
2883 if (vsdeHDCSAS.size() > 1)
2884 throw setError(VBOX_E_FILE_ERROR,
2885 tr("Too many SAS controllers in OVF; import facility only supports one"));
2886 if (vsdeHDCSAS.size() > 0)
2887 {
2888 ComPtr<IStorageController> pController;
2889 rc = pNewMachine->AddStorageController(Bstr(L"SAS Controller").raw(),
2890 StorageBus_SAS,
2891 pController.asOutParam());
2892 if (FAILED(rc)) throw rc;
2893 rc = pController->COMSETTER(ControllerType)(StorageControllerType_LsiLogicSas);
2894 if (FAILED(rc)) throw rc;
2895 }
2896
2897 /* Now its time to register the machine before we add any hard disks */
2898 rc = mVirtualBox->RegisterMachine(pNewMachine);
2899 if (FAILED(rc)) throw rc;
2900
2901 // store new machine for roll-back in case of errors
2902 Bstr bstrNewMachineId;
2903 rc = pNewMachine->COMGETTER(Id)(bstrNewMachineId.asOutParam());
2904 if (FAILED(rc)) throw rc;
2905 Guid uuidNewMachine(bstrNewMachineId);
2906 m->llGuidsMachinesCreated.push_back(uuidNewMachine);
2907
2908 // Add floppies and CD-ROMs to the appropriate controllers.
2909 std::list<VirtualSystemDescriptionEntry*> vsdeFloppy = vsdescThis->findByType(VirtualSystemDescriptionType_Floppy);
2910 if (vsdeFloppy.size() > 1)
2911 throw setError(VBOX_E_FILE_ERROR,
2912 tr("Too many floppy controllers in OVF; import facility only supports one"));
2913 std::list<VirtualSystemDescriptionEntry*> vsdeCDROM = vsdescThis->findByType(VirtualSystemDescriptionType_CDROM);
2914 if ( (vsdeFloppy.size() > 0)
2915 || (vsdeCDROM.size() > 0)
2916 )
2917 {
2918 // If there's an error here we need to close the session, so
2919 // we need another try/catch block.
2920
2921 try
2922 {
2923 // to attach things we need to open a session for the new machine
2924 rc = pNewMachine->LockMachine(stack.pSession, LockType_Write);
2925 if (FAILED(rc)) throw rc;
2926 stack.fSessionOpen = true;
2927
2928 ComPtr<IMachine> sMachine;
2929 rc = stack.pSession->COMGETTER(Machine)(sMachine.asOutParam());
2930 if (FAILED(rc)) throw rc;
2931
2932 // floppy first
2933 if (vsdeFloppy.size() == 1)
2934 {
2935 ComPtr<IStorageController> pController;
2936 rc = sMachine->AddStorageController(Bstr("Floppy Controller").raw(),
2937 StorageBus_Floppy,
2938 pController.asOutParam());
2939 if (FAILED(rc)) throw rc;
2940
2941 Bstr bstrName;
2942 rc = pController->COMGETTER(Name)(bstrName.asOutParam());
2943 if (FAILED(rc)) throw rc;
2944
2945 // this is for rollback later
2946 MyHardDiskAttachment mhda;
2947 mhda.pMachine = pNewMachine;
2948 mhda.controllerType = bstrName;
2949 mhda.lControllerPort = 0;
2950 mhda.lDevice = 0;
2951
2952 Log(("Attaching floppy\n"));
2953
2954 rc = sMachine->AttachDevice(mhda.controllerType.raw(),
2955 mhda.lControllerPort,
2956 mhda.lDevice,
2957 DeviceType_Floppy,
2958 NULL);
2959 if (FAILED(rc)) throw rc;
2960
2961 stack.llHardDiskAttachments.push_back(mhda);
2962 }
2963
2964 rc = sMachine->SaveSettings();
2965 if (FAILED(rc)) throw rc;
2966
2967 // only now that we're done with all disks, close the session
2968 rc = stack.pSession->UnlockMachine();
2969 if (FAILED(rc)) throw rc;
2970 stack.fSessionOpen = false;
2971 }
2972 catch(HRESULT /* aRC */)
2973 {
2974 if (stack.fSessionOpen)
2975 stack.pSession->UnlockMachine();
2976
2977 throw;
2978 }
2979 }
2980
2981 // create the hard disks & connect them to the appropriate controllers
2982 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
2983 if (avsdeHDs.size() > 0)
2984 {
2985 // If there's an error here we need to close the session, so
2986 // we need another try/catch block.
2987 try
2988 {
2989 // to attach things we need to open a session for the new machine
2990 rc = pNewMachine->LockMachine(stack.pSession, LockType_Write);
2991 if (FAILED(rc)) throw rc;
2992 stack.fSessionOpen = true;
2993
2994 ovf::DiskImagesMap::const_iterator oit = stack.mapDisks.begin();
2995 std::set<RTCString> disksResolvedNames;
2996
2997 while(oit != stack.mapDisks.end())
2998 {
2999 ovf::DiskImage diCurrent = oit->second;
3000 ovf::VirtualDisksMap::const_iterator itVDisk = vsysThis.mapVirtualDisks.begin();
3001
3002 VirtualSystemDescriptionEntry *vsdeTargetHD = 0;
3003
3004 /*
3005 *
3006 * Iterate over all given disk images of the virtual system
3007 * disks description. We need to find the target disk path,
3008 * which could be changed by the user.
3009 *
3010 */
3011 {
3012 list<VirtualSystemDescriptionEntry*>::const_iterator itHD;
3013 for (itHD = avsdeHDs.begin();
3014 itHD != avsdeHDs.end();
3015 ++itHD)
3016 {
3017 VirtualSystemDescriptionEntry *vsdeHD = *itHD;
3018 if (vsdeHD->strRef == diCurrent.strDiskId)
3019 {
3020 vsdeTargetHD = vsdeHD;
3021 break;
3022 }
3023 }
3024 if (!vsdeTargetHD)
3025 throw setError(E_FAIL,
3026 tr("Internal inconsistency looking up disk image '%s'"),
3027 diCurrent.strHref.c_str());
3028
3029 //diCurrent.strDiskId contains the disk identifier (e.g. "vmdisk1"), which should exist
3030 //in the virtual system's disks map under that ID and also in the global images map
3031 itVDisk = vsysThis.mapVirtualDisks.find(diCurrent.strDiskId);
3032 if (itVDisk == vsysThis.mapVirtualDisks.end())
3033 throw setError(E_FAIL,
3034 tr("Internal inconsistency looking up disk image '%s'"),
3035 diCurrent.strHref.c_str());
3036 }
3037
3038 /*
3039 * preliminary check availability of the image
3040 * This step is useful if image is placed in the OVA (TAR) package
3041 */
3042
3043 Utf8Str name = applianceIOName(applianceIOTar);
3044
3045 if (strncmp(pStorage->pVDImageIfaces->pszInterfaceName, name.c_str(), name.length()) == 0)
3046 {
3047 /* It means that we possibly have imported the storage earlier on the previous loop steps*/
3048 std::set<RTCString>::const_iterator h = disksResolvedNames.find(diCurrent.strHref);
3049 if (h != disksResolvedNames.end())
3050 {
3051 /* Yes, disk name was found, we can skip it*/
3052 ++oit;
3053 continue;
3054 }
3055
3056 RTCString availableImage(diCurrent.strHref);
3057
3058 rc = preCheckImageAvailability(pStorage,
3059 availableImage
3060 );
3061
3062 if (SUCCEEDED(rc))
3063 {
3064 /* current opened file isn't the same as passed one */
3065 if(availableImage.compare(diCurrent.strHref, Utf8Str::CaseInsensitive) != 0)
3066 {
3067 /*
3068 * availableImage contains the disk file reference (e.g. "disk1.vmdk"), which should exist
3069 * in the global images map.
3070 * And find the disk from the OVF's disk list
3071 *
3072 */
3073 {
3074 ovf::DiskImagesMap::const_iterator itDiskImage = stack.mapDisks.begin();
3075 while (++itDiskImage != stack.mapDisks.end())
3076 {
3077 if (itDiskImage->second.strHref.compare(availableImage, Utf8Str::CaseInsensitive) == 0)
3078 break;
3079 }
3080 if (itDiskImage == stack.mapDisks.end())
3081 {
3082 throw setError(E_FAIL,
3083 tr("Internal inconsistency looking up disk image '%s'. "
3084 "Check compliance OVA package structure and file names "
3085 "references in the section <References> in the OVF file."),
3086 availableImage.c_str());
3087 }
3088
3089 /* replace with a new found disk image */
3090 diCurrent = *(&itDiskImage->second);
3091 }
3092
3093 /*
3094 * Again iterate over all given disk images of the virtual system
3095 * disks description using the found disk image
3096 */
3097 {
3098 list<VirtualSystemDescriptionEntry*>::const_iterator itHD;
3099 for (itHD = avsdeHDs.begin();
3100 itHD != avsdeHDs.end();
3101 ++itHD)
3102 {
3103 VirtualSystemDescriptionEntry *vsdeHD = *itHD;
3104 if (vsdeHD->strRef == diCurrent.strDiskId)
3105 {
3106 vsdeTargetHD = vsdeHD;
3107 break;
3108 }
3109 }
3110 if (!vsdeTargetHD)
3111 throw setError(E_FAIL,
3112 tr("Internal inconsistency looking up disk image '%s'"),
3113 diCurrent.strHref.c_str());
3114
3115 itVDisk = vsysThis.mapVirtualDisks.find(diCurrent.strDiskId);
3116 if (itVDisk == vsysThis.mapVirtualDisks.end())
3117 throw setError(E_FAIL,
3118 tr("Internal inconsistency looking up disk image '%s'"),
3119 diCurrent.strHref.c_str());
3120 }
3121 }
3122 else
3123 {
3124 ++oit;
3125 }
3126 }
3127 else
3128 {
3129 ++oit;
3130 continue;
3131 }
3132 }
3133 else
3134 {
3135 /* just continue with normal files*/
3136 ++oit;
3137 }
3138
3139 const ovf::VirtualDisk &ovfVdisk = itVDisk->second;
3140
3141 /* very important to store disk name for the next checks */
3142 disksResolvedNames.insert(diCurrent.strHref);
3143
3144 ComObjPtr<Medium> pTargetHD;
3145
3146 Utf8Str savedVboxCurrent = vsdeTargetHD->strVboxCurrent;
3147
3148 importOneDiskImage(diCurrent,
3149 &vsdeTargetHD->strVboxCurrent,
3150 pTargetHD,
3151 stack,
3152 pCallbacks,
3153 pStorage);
3154
3155 // now use the new uuid to attach the disk image to our new machine
3156 ComPtr<IMachine> sMachine;
3157 rc = stack.pSession->COMGETTER(Machine)(sMachine.asOutParam());
3158 if (FAILED(rc)) throw rc;
3159
3160 // find the hard disk controller to which we should attach
3161 ovf::HardDiskController hdc = (*vsysThis.mapControllers.find(ovfVdisk.idController)).second;
3162
3163 // this is for rollback later
3164 MyHardDiskAttachment mhda;
3165 mhda.pMachine = pNewMachine;
3166
3167 convertDiskAttachmentValues(hdc,
3168 ovfVdisk.ulAddressOnParent,
3169 mhda.controllerType, // Bstr
3170 mhda.lControllerPort,
3171 mhda.lDevice);
3172
3173 Log(("Attaching disk %s to port %d on device %d\n",
3174 vsdeTargetHD->strVboxCurrent.c_str(), mhda.lControllerPort, mhda.lDevice));
3175
3176 Utf8Str vdf = typeOfVirtualDiskFormatFromURI(diCurrent.strFormat);
3177
3178 if (vdf.compare("RAW", Utf8Str::CaseInsensitive) == 0)
3179 {
3180 ComPtr<IMedium> dvdImage(pTargetHD);
3181
3182 rc = mVirtualBox->OpenMedium(Bstr(vsdeTargetHD->strVboxCurrent).raw(),
3183 DeviceType_DVD,
3184 AccessMode_ReadWrite,
3185 false,
3186 dvdImage.asOutParam());
3187
3188 if (FAILED(rc)) throw rc;
3189
3190 rc = sMachine->AttachDevice(mhda.controllerType.raw(),// wstring name
3191 mhda.lControllerPort, // long controllerPort
3192 mhda.lDevice, // long device
3193 DeviceType_DVD, // DeviceType_T type
3194 dvdImage);
3195 if (FAILED(rc)) throw rc;
3196 }
3197 else
3198 {
3199 rc = sMachine->AttachDevice(mhda.controllerType.raw(),// wstring name
3200 mhda.lControllerPort, // long controllerPort
3201 mhda.lDevice, // long device
3202 DeviceType_HardDisk, // DeviceType_T type
3203 pTargetHD);
3204
3205 if (FAILED(rc)) throw rc;
3206 }
3207
3208 stack.llHardDiskAttachments.push_back(mhda);
3209
3210 rc = sMachine->SaveSettings();
3211 if (FAILED(rc)) throw rc;
3212
3213 /* restore */
3214 vsdeTargetHD->strVboxCurrent = savedVboxCurrent;
3215
3216 } // end while(oit != stack.mapDisks.end())
3217
3218 // only now that we're done with all disks, close the session
3219 rc = stack.pSession->UnlockMachine();
3220 if (FAILED(rc)) throw rc;
3221 stack.fSessionOpen = false;
3222 }
3223 catch(HRESULT /* aRC */)
3224 {
3225 if (stack.fSessionOpen)
3226 stack.pSession->UnlockMachine();
3227
3228 throw;
3229 }
3230 }
3231}
3232
3233/**
3234 * Imports one OVF virtual system (described by a vbox:Machine tag represented by the given config
3235 * structure) into VirtualBox by creating an IMachine instance, which is returned.
3236 *
3237 * This throws HRESULT error codes for anything that goes wrong, in which case the caller must clean
3238 * up any leftovers from this function. For this, the given ImportStack instance has received information
3239 * about what needs cleaning up (to support rollback).
3240 *
3241 * The machine config stored in the settings::MachineConfigFile structure contains the UUIDs of
3242 * the disk attachments used by the machine when it was exported. We also add vbox:uuid attributes
3243 * to the OVF disks sections so we can look them up. While importing these UUIDs into a second host
3244 * will most probably work, reimporting them into the same host will cause conflicts, so we always
3245 * generate new ones on import. This involves the following:
3246 *
3247 * 1) Scan the machine config for disk attachments.
3248 *
3249 * 2) For each disk attachment found, look up the OVF disk image from the disk references section
3250 * and import the disk into VirtualBox, which creates a new UUID for it. In the machine config,
3251 * replace the old UUID with the new one.
3252 *
3253 * 3) Change the machine config according to the OVF virtual system descriptions, in case the
3254 * caller has modified them using setFinalValues().
3255 *
3256 * 4) Create the VirtualBox machine with the modfified machine config.
3257 *
3258 * @param config
3259 * @param pNewMachine
3260 * @param stack
3261 */
3262void Appliance::importVBoxMachine(ComObjPtr<VirtualSystemDescription> &vsdescThis,
3263 ComPtr<IMachine> &pReturnNewMachine,
3264 ImportStack &stack,
3265 PVDINTERFACEIO pCallbacks,
3266 PSHASTORAGE pStorage)
3267{
3268 Assert(vsdescThis->m->pConfig);
3269
3270 HRESULT rc = S_OK;
3271
3272 settings::MachineConfigFile &config = *vsdescThis->m->pConfig;
3273
3274 /*
3275 * step 1): modify machine config according to OVF config, in case the user
3276 * has modified them using setFinalValues()
3277 */
3278
3279 /* OS Type */
3280 config.machineUserData.strOsType = stack.strOsTypeVBox;
3281 /* Description */
3282 config.machineUserData.strDescription = stack.strDescription;
3283 /* CPU count & extented attributes */
3284 config.hardwareMachine.cCPUs = stack.cCPUs;
3285 if (stack.fForceIOAPIC)
3286 config.hardwareMachine.fHardwareVirt = true;
3287 if (stack.fForceIOAPIC)
3288 config.hardwareMachine.biosSettings.fIOAPICEnabled = true;
3289 /* RAM size */
3290 config.hardwareMachine.ulMemorySizeMB = stack.ulMemorySizeMB;
3291
3292/*
3293 <const name="HardDiskControllerIDE" value="14" />
3294 <const name="HardDiskControllerSATA" value="15" />
3295 <const name="HardDiskControllerSCSI" value="16" />
3296 <const name="HardDiskControllerSAS" value="17" />
3297*/
3298
3299#ifdef VBOX_WITH_USB
3300 /* USB controller */
3301 if (stack.fUSBEnabled)
3302 {
3303 /** @todo r=klaus add support for arbitrary USB controller types, this can't handle multiple controllers due to its design anyway */
3304
3305 /* usually the OHCI controller is enabled already, need to check */
3306 bool fOHCIEnabled = false;
3307 settings::USBControllerList &llUSBControllers = config.hardwareMachine.usbSettings.llUSBControllers;
3308 settings::USBControllerList::iterator it;
3309 for (it = llUSBControllers.begin(); it != llUSBControllers.end(); ++it)
3310 {
3311 if (it->enmType == USBControllerType_OHCI)
3312 {
3313 fOHCIEnabled = true;
3314 break;
3315 }
3316 }
3317
3318 if (!fOHCIEnabled)
3319 {
3320 settings::USBController ctrl;
3321 ctrl.strName = "OHCI";
3322 ctrl.enmType = USBControllerType_OHCI;
3323
3324 llUSBControllers.push_back(ctrl);
3325 }
3326 }
3327 else
3328 config.hardwareMachine.usbSettings.llUSBControllers.clear();
3329#endif
3330 /* Audio adapter */
3331 if (stack.strAudioAdapter.isNotEmpty())
3332 {
3333 config.hardwareMachine.audioAdapter.fEnabled = true;
3334 config.hardwareMachine.audioAdapter.controllerType = (AudioControllerType_T)stack.strAudioAdapter.toUInt32();
3335 }
3336 else
3337 config.hardwareMachine.audioAdapter.fEnabled = false;
3338 /* Network adapter */
3339 settings::NetworkAdaptersList &llNetworkAdapters = config.hardwareMachine.llNetworkAdapters;
3340 /* First disable all network cards, they will be enabled below again. */
3341 settings::NetworkAdaptersList::iterator it1;
3342 bool fKeepAllMACs = m->optList.contains(ImportOptions_KeepAllMACs);
3343 bool fKeepNATMACs = m->optList.contains(ImportOptions_KeepNATMACs);
3344 for (it1 = llNetworkAdapters.begin(); it1 != llNetworkAdapters.end(); ++it1)
3345 {
3346 it1->fEnabled = false;
3347 if (!( fKeepAllMACs
3348 || (fKeepNATMACs && it1->mode == NetworkAttachmentType_NAT)
3349 || (fKeepNATMACs && it1->mode == NetworkAttachmentType_NATNetwork)))
3350 Host::generateMACAddress(it1->strMACAddress);
3351 }
3352 /* Now iterate over all network entries. */
3353 std::list<VirtualSystemDescriptionEntry*> avsdeNWs = vsdescThis->findByType(VirtualSystemDescriptionType_NetworkAdapter);
3354 if (avsdeNWs.size() > 0)
3355 {
3356 /* Iterate through all network adapter entries and search for the
3357 * corresponding one in the machine config. If one is found, configure
3358 * it based on the user settings. */
3359 list<VirtualSystemDescriptionEntry*>::const_iterator itNW;
3360 for (itNW = avsdeNWs.begin();
3361 itNW != avsdeNWs.end();
3362 ++itNW)
3363 {
3364 VirtualSystemDescriptionEntry *vsdeNW = *itNW;
3365 if ( vsdeNW->strExtraConfigCurrent.startsWith("slot=", Utf8Str::CaseInsensitive)
3366 && vsdeNW->strExtraConfigCurrent.length() > 6)
3367 {
3368 uint32_t iSlot = vsdeNW->strExtraConfigCurrent.substr(5, 1).toUInt32();
3369 /* Iterate through all network adapters in the machine config. */
3370 for (it1 = llNetworkAdapters.begin();
3371 it1 != llNetworkAdapters.end();
3372 ++it1)
3373 {
3374 /* Compare the slots. */
3375 if (it1->ulSlot == iSlot)
3376 {
3377 it1->fEnabled = true;
3378 it1->type = (NetworkAdapterType_T)vsdeNW->strVboxCurrent.toUInt32();
3379 break;
3380 }
3381 }
3382 }
3383 }
3384 }
3385
3386 /* Floppy controller */
3387 bool fFloppy = vsdescThis->findByType(VirtualSystemDescriptionType_Floppy).size() > 0;
3388 /* DVD controller */
3389 bool fDVD = vsdescThis->findByType(VirtualSystemDescriptionType_CDROM).size() > 0;
3390 /* Iterate over all storage controller check the attachments and remove
3391 * them when necessary. Also detect broken configs with more than one
3392 * attachment. Old VirtualBox versions (prior to 3.2.10) had all disk
3393 * attachments pointing to the last hard disk image, which causes import
3394 * failures. A long fixed bug, however the OVF files are long lived. */
3395 settings::StorageControllersList &llControllers = config.storageMachine.llStorageControllers;
3396 Guid hdUuid;
3397 uint32_t cHardDisks = 0;
3398 bool fInconsistent = false;
3399 bool fRepairDuplicate = false;
3400 settings::StorageControllersList::iterator it3;
3401 for (it3 = llControllers.begin();
3402 it3 != llControllers.end();
3403 ++it3)
3404 {
3405 settings::AttachedDevicesList &llAttachments = it3->llAttachedDevices;
3406 settings::AttachedDevicesList::iterator it4 = llAttachments.begin();
3407 while (it4 != llAttachments.end())
3408 {
3409 if ( ( !fDVD
3410 && it4->deviceType == DeviceType_DVD)
3411 ||
3412 ( !fFloppy
3413 && it4->deviceType == DeviceType_Floppy))
3414 {
3415 it4 = llAttachments.erase(it4);
3416 continue;
3417 }
3418 else if (it4->deviceType == DeviceType_HardDisk)
3419 {
3420 const Guid &thisUuid = it4->uuid;
3421 cHardDisks++;
3422 if (cHardDisks == 1)
3423 {
3424 if (hdUuid.isZero())
3425 hdUuid = thisUuid;
3426 else
3427 fInconsistent = true;
3428 }
3429 else
3430 {
3431 if (thisUuid.isZero())
3432 fInconsistent = true;
3433 else if (thisUuid == hdUuid)
3434 fRepairDuplicate = true;
3435 }
3436 }
3437 ++it4;
3438 }
3439 }
3440 /* paranoia... */
3441 if (fInconsistent || cHardDisks == 1)
3442 fRepairDuplicate = false;
3443
3444 /*
3445 * step 2: scan the machine config for media attachments
3446 */
3447
3448 /* Get all hard disk descriptions. */
3449 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
3450 std::list<VirtualSystemDescriptionEntry*>::iterator avsdeHDsIt = avsdeHDs.begin();
3451 /* paranoia - if there is no 1:1 match do not try to repair. */
3452 if (cHardDisks != avsdeHDs.size())
3453 fRepairDuplicate = false;
3454
3455 // there must be an image in the OVF disk structs with the same UUID
3456
3457 ovf::DiskImagesMap::const_iterator oit = stack.mapDisks.begin();
3458 std::set<RTCString> disksResolvedNames;
3459
3460 while(oit != stack.mapDisks.end())
3461 {
3462 ovf::DiskImage diCurrent = oit->second;
3463
3464 VirtualSystemDescriptionEntry *vsdeTargetHD = 0;
3465
3466 {
3467 /* Iterate over all given disk images of the virtual system
3468 * disks description. We need to find the target disk path,
3469 * which could be changed by the user. */
3470 list<VirtualSystemDescriptionEntry*>::const_iterator itHD;
3471 for (itHD = avsdeHDs.begin();
3472 itHD != avsdeHDs.end();
3473 ++itHD)
3474 {
3475 VirtualSystemDescriptionEntry *vsdeHD = *itHD;
3476 if (vsdeHD->strRef == oit->first)
3477 {
3478 vsdeTargetHD = vsdeHD;
3479 break;
3480 }
3481 }
3482 if (!vsdeTargetHD)
3483 throw setError(E_FAIL,
3484 tr("Internal inconsistency looking up disk image '%s'"),
3485 oit->first.c_str());
3486 }
3487
3488 /*
3489 * preliminary check availability of the image
3490 * This step is useful if image is placed in the OVA (TAR) package
3491 */
3492
3493 Utf8Str name = applianceIOName(applianceIOTar);
3494
3495 if (strncmp(pStorage->pVDImageIfaces->pszInterfaceName, name.c_str(), name.length()) == 0)
3496 {
3497 /* It means that we possibly have imported the storage earlier on the previous loop steps*/
3498 std::set<RTCString>::const_iterator h = disksResolvedNames.find(diCurrent.strHref);
3499 if (h != disksResolvedNames.end())
3500 {
3501 /* Yes, disk name was found, we can skip it*/
3502 ++oit;
3503 continue;
3504 }
3505
3506 RTCString availableImage(diCurrent.strHref);
3507
3508 rc = preCheckImageAvailability(pStorage,
3509 availableImage
3510 );
3511
3512 if (SUCCEEDED(rc))
3513 {
3514 /* current opened file isn't the same as passed one */
3515 if(availableImage.compare(diCurrent.strHref, Utf8Str::CaseInsensitive) != 0)
3516 {
3517 // availableImage contains the disk identifier (e.g. "vmdisk1"), which should exist
3518 // in the virtual system's disks map under that ID and also in the global images map
3519 // and find the disk from the OVF's disk list
3520 ovf::DiskImagesMap::const_iterator itDiskImage = stack.mapDisks.begin();
3521 while (++itDiskImage != stack.mapDisks.end())
3522 {
3523 if(itDiskImage->second.strHref.compare(availableImage, Utf8Str::CaseInsensitive) == 0 )
3524 break;
3525 }
3526 if (itDiskImage == stack.mapDisks.end())
3527 {
3528 throw setError(E_FAIL,
3529 tr("Internal inconsistency looking up disk image '%s'. "
3530 "Check compliance OVA package structure and file names "
3531 "references in the section <References> in the OVF file."),
3532 availableImage.c_str());
3533 }
3534
3535 /* replace with a new found disk image */
3536 diCurrent = *(&itDiskImage->second);
3537
3538 /*
3539 * Again iterate over all given disk images of the virtual system
3540 * disks description using the found disk image
3541 */
3542 list<VirtualSystemDescriptionEntry*>::const_iterator itHD;
3543 for (itHD = avsdeHDs.begin();
3544 itHD != avsdeHDs.end();
3545 ++itHD)
3546 {
3547 VirtualSystemDescriptionEntry *vsdeHD = *itHD;
3548 if (vsdeHD->strRef == diCurrent.strDiskId)
3549 {
3550 vsdeTargetHD = vsdeHD;
3551 break;
3552 }
3553 }
3554 if (!vsdeTargetHD)
3555 throw setError(E_FAIL,
3556 tr("Internal inconsistency looking up disk image '%s'"),
3557 diCurrent.strHref.c_str());
3558 }
3559 else
3560 {
3561 ++oit;
3562 }
3563 }
3564 else
3565 {
3566 ++oit;
3567 continue;
3568 }
3569 }
3570 else
3571 {
3572 /* just continue with normal files*/
3573 ++oit;
3574 }
3575
3576 /* Important! to store disk name for the next checks */
3577 disksResolvedNames.insert(diCurrent.strHref);
3578
3579 // there must be an image in the OVF disk structs with the same UUID
3580 bool fFound = false;
3581 Utf8Str strUuid;
3582
3583 // for each storage controller...
3584 for (settings::StorageControllersList::iterator sit = config.storageMachine.llStorageControllers.begin();
3585 sit != config.storageMachine.llStorageControllers.end();
3586 ++sit)
3587 {
3588 settings::StorageController &sc = *sit;
3589
3590 // find the OVF virtual system description entry for this storage controller
3591 switch (sc.storageBus)
3592 {
3593 case StorageBus_SATA:
3594 break;
3595 case StorageBus_SCSI:
3596 break;
3597 case StorageBus_IDE:
3598 break;
3599 case StorageBus_SAS:
3600 break;
3601 }
3602
3603 // for each medium attachment to this controller...
3604 for (settings::AttachedDevicesList::iterator dit = sc.llAttachedDevices.begin();
3605 dit != sc.llAttachedDevices.end();
3606 ++dit)
3607 {
3608 settings::AttachedDevice &d = *dit;
3609
3610 if (d.uuid.isZero())
3611 // empty DVD and floppy media
3612 continue;
3613
3614 // When repairing a broken VirtualBox xml config section (written
3615 // by VirtualBox versions earlier than 3.2.10) assume the disks
3616 // show up in the same order as in the OVF description.
3617 if (fRepairDuplicate)
3618 {
3619 VirtualSystemDescriptionEntry *vsdeHD = *avsdeHDsIt;
3620 ovf::DiskImagesMap::const_iterator itDiskImage = stack.mapDisks.find(vsdeHD->strRef);
3621 if (itDiskImage != stack.mapDisks.end())
3622 {
3623 const ovf::DiskImage &di = itDiskImage->second;
3624 d.uuid = Guid(di.uuidVbox);
3625 }
3626 ++avsdeHDsIt;
3627 }
3628
3629 // convert the Guid to string
3630 strUuid = d.uuid.toString();
3631
3632 if (diCurrent.uuidVbox != strUuid)
3633 {
3634 continue;
3635 }
3636
3637 /*
3638 * step 3: import disk
3639 */
3640 Utf8Str savedVboxCurrent = vsdeTargetHD->strVboxCurrent;
3641 ComObjPtr<Medium> pTargetHD;
3642 importOneDiskImage(diCurrent,
3643 &vsdeTargetHD->strVboxCurrent,
3644 pTargetHD,
3645 stack,
3646 pCallbacks,
3647 pStorage);
3648
3649 Bstr hdId;
3650
3651 Utf8Str vdf = typeOfVirtualDiskFormatFromURI(diCurrent.strFormat);
3652
3653 if (vdf.compare("RAW", Utf8Str::CaseInsensitive) == 0)
3654 {
3655 ComPtr<IMedium> dvdImage(pTargetHD);
3656
3657 rc = mVirtualBox->OpenMedium(Bstr(vsdeTargetHD->strVboxCurrent).raw(),
3658 DeviceType_DVD,
3659 AccessMode_ReadWrite,
3660 false,
3661 dvdImage.asOutParam());
3662
3663 if (FAILED(rc)) throw rc;
3664
3665 // ... and replace the old UUID in the machine config with the one of
3666 // the imported disk that was just created
3667 rc = dvdImage->COMGETTER(Id)(hdId.asOutParam());
3668 if (FAILED(rc)) throw rc;
3669 }
3670 else
3671 {
3672 // ... and replace the old UUID in the machine config with the one of
3673 // the imported disk that was just created
3674 rc = pTargetHD->COMGETTER(Id)(hdId.asOutParam());
3675 if (FAILED(rc)) throw rc;
3676 }
3677
3678 /* restore */
3679 vsdeTargetHD->strVboxCurrent = savedVboxCurrent;
3680
3681 d.uuid = hdId;
3682 fFound = true;
3683 break;
3684 } // for (settings::AttachedDevicesList::const_iterator dit = sc.llAttachedDevices.begin();
3685 } // for (settings::StorageControllersList::const_iterator sit = config.storageMachine.llStorageControllers.begin();
3686
3687 // no disk with such a UUID found:
3688 if (!fFound)
3689 throw setError(E_FAIL,
3690 tr("<vbox:Machine> element in OVF contains a medium attachment for the disk image %s "
3691 "but the OVF describes no such image"),
3692 strUuid.c_str());
3693
3694 }// while(oit != stack.mapDisks.end())
3695
3696 /*
3697 * step 4): create the machine and have it import the config
3698 */
3699
3700 ComObjPtr<Machine> pNewMachine;
3701 rc = pNewMachine.createObject();
3702 if (FAILED(rc)) throw rc;
3703
3704 // this magic constructor fills the new machine object with the MachineConfig
3705 // instance that we created from the vbox:Machine
3706 rc = pNewMachine->init(mVirtualBox,
3707 stack.strNameVBox,// name from OVF preparations; can be suffixed to avoid duplicates
3708 config); // the whole machine config
3709 if (FAILED(rc)) throw rc;
3710
3711 pReturnNewMachine = ComPtr<IMachine>(pNewMachine);
3712
3713 // and register it
3714 rc = mVirtualBox->RegisterMachine(pNewMachine);
3715 if (FAILED(rc)) throw rc;
3716
3717 // store new machine for roll-back in case of errors
3718 Bstr bstrNewMachineId;
3719 rc = pNewMachine->COMGETTER(Id)(bstrNewMachineId.asOutParam());
3720 if (FAILED(rc)) throw rc;
3721 m->llGuidsMachinesCreated.push_back(Guid(bstrNewMachineId));
3722}
3723
3724void Appliance::importMachines(ImportStack &stack,
3725 PVDINTERFACEIO pCallbacks,
3726 PSHASTORAGE pStorage)
3727{
3728 HRESULT rc = S_OK;
3729
3730 // this is safe to access because this thread only gets started
3731 const ovf::OVFReader &reader = *m->pReader;
3732
3733 /*
3734 * get the SHA digest version that was set in accordance with the value of attribute "xmlns:ovf"
3735 * of the element <Envelope> in the OVF file during reading operation. See readFSImpl().
3736 */
3737 pStorage->fSha256 = m->fSha256;
3738
3739 // create a session for the machine + disks we manipulate below
3740 rc = stack.pSession.createInprocObject(CLSID_Session);
3741 if (FAILED(rc)) throw rc;
3742
3743 list<ovf::VirtualSystem>::const_iterator it;
3744 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it1;
3745 /* Iterate through all virtual systems of that appliance */
3746 size_t i = 0;
3747 for (it = reader.m_llVirtualSystems.begin(),
3748 it1 = m->virtualSystemDescriptions.begin();
3749 it != reader.m_llVirtualSystems.end(),
3750 it1 != m->virtualSystemDescriptions.end();
3751 ++it, ++it1, ++i)
3752 {
3753 const ovf::VirtualSystem &vsysThis = *it;
3754 ComObjPtr<VirtualSystemDescription> vsdescThis = (*it1);
3755
3756 ComPtr<IMachine> pNewMachine;
3757
3758 // there are two ways in which we can create a vbox machine from OVF:
3759 // -- either this OVF was written by vbox 3.2 or later, in which case there is a <vbox:Machine> element
3760 // in the <VirtualSystem>; then the VirtualSystemDescription::Data has a settings::MachineConfigFile
3761 // with all the machine config pretty-parsed;
3762 // -- or this is an OVF from an older vbox or an external source, and then we need to translate the
3763 // VirtualSystemDescriptionEntry and do import work
3764
3765 // Even for the vbox:Machine case, there are a number of configuration items that will be taken from
3766 // the OVF because otherwise the "override import parameters" mechanism in the GUI won't work.
3767
3768 // VM name
3769 std::list<VirtualSystemDescriptionEntry*> vsdeName = vsdescThis->findByType(VirtualSystemDescriptionType_Name);
3770 if (vsdeName.size() < 1)
3771 throw setError(VBOX_E_FILE_ERROR,
3772 tr("Missing VM name"));
3773 stack.strNameVBox = vsdeName.front()->strVboxCurrent;
3774
3775 // have VirtualBox suggest where the filename would be placed so we can
3776 // put the disk images in the same directory
3777 Bstr bstrMachineFilename;
3778 rc = mVirtualBox->ComposeMachineFilename(Bstr(stack.strNameVBox).raw(),
3779 NULL /* aGroup */,
3780 NULL /* aCreateFlags */,
3781 NULL /* aBaseFolder */,
3782 bstrMachineFilename.asOutParam());
3783 if (FAILED(rc)) throw rc;
3784 // and determine the machine folder from that
3785 stack.strMachineFolder = bstrMachineFilename;
3786 stack.strMachineFolder.stripFilename();
3787
3788 // guest OS type
3789 std::list<VirtualSystemDescriptionEntry*> vsdeOS;
3790 vsdeOS = vsdescThis->findByType(VirtualSystemDescriptionType_OS);
3791 if (vsdeOS.size() < 1)
3792 throw setError(VBOX_E_FILE_ERROR,
3793 tr("Missing guest OS type"));
3794 stack.strOsTypeVBox = vsdeOS.front()->strVboxCurrent;
3795
3796 // CPU count
3797 std::list<VirtualSystemDescriptionEntry*> vsdeCPU = vsdescThis->findByType(VirtualSystemDescriptionType_CPU);
3798 if (vsdeCPU.size() != 1)
3799 throw setError(VBOX_E_FILE_ERROR, tr("CPU count missing"));
3800
3801 stack.cCPUs = vsdeCPU.front()->strVboxCurrent.toUInt32();
3802 // We need HWVirt & IO-APIC if more than one CPU is requested
3803 if (stack.cCPUs > 1)
3804 {
3805 stack.fForceHWVirt = true;
3806 stack.fForceIOAPIC = true;
3807 }
3808
3809 // RAM
3810 std::list<VirtualSystemDescriptionEntry*> vsdeRAM = vsdescThis->findByType(VirtualSystemDescriptionType_Memory);
3811 if (vsdeRAM.size() != 1)
3812 throw setError(VBOX_E_FILE_ERROR, tr("RAM size missing"));
3813 stack.ulMemorySizeMB = (ULONG)vsdeRAM.front()->strVboxCurrent.toUInt64();
3814
3815#ifdef VBOX_WITH_USB
3816 // USB controller
3817 std::list<VirtualSystemDescriptionEntry*> vsdeUSBController = vsdescThis->findByType(VirtualSystemDescriptionType_USBController);
3818 // USB support is enabled if there's at least one such entry; to disable USB support,
3819 // the type of the USB item would have been changed to "ignore"
3820 stack.fUSBEnabled = vsdeUSBController.size() > 0;
3821#endif
3822 // audio adapter
3823 std::list<VirtualSystemDescriptionEntry*> vsdeAudioAdapter = vsdescThis->findByType(VirtualSystemDescriptionType_SoundCard);
3824 /* @todo: we support one audio adapter only */
3825 if (vsdeAudioAdapter.size() > 0)
3826 stack.strAudioAdapter = vsdeAudioAdapter.front()->strVboxCurrent;
3827
3828 // for the description of the new machine, always use the OVF entry, the user may have changed it in the import config
3829 std::list<VirtualSystemDescriptionEntry*> vsdeDescription = vsdescThis->findByType(VirtualSystemDescriptionType_Description);
3830 if (vsdeDescription.size())
3831 stack.strDescription = vsdeDescription.front()->strVboxCurrent;
3832
3833 // import vbox:machine or OVF now
3834 if (vsdescThis->m->pConfig)
3835 // vbox:Machine config
3836 importVBoxMachine(vsdescThis, pNewMachine, stack, pCallbacks, pStorage);
3837 else
3838 // generic OVF config
3839 importMachineGeneric(vsysThis, vsdescThis, pNewMachine, stack, pCallbacks, pStorage);
3840
3841 } // for (it = pAppliance->m->llVirtualSystems.begin() ...
3842}
3843
3844
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette