VirtualBox

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

Last change on this file since 98288 was 98288, checked in by vboxsync, 23 months ago

Main/src-server: rc -> hrc/vrc (partial). bugref:10223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 274.7 KB
Line 
1/* $Id: ApplianceImplImport.cpp 98288 2023-01-24 15:32:43Z vboxsync $ */
2/** @file
3 * IAppliance and IVirtualSystem COM class implementations.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#define LOG_GROUP LOG_GROUP_MAIN_APPLIANCE
29#include <iprt/alloca.h>
30#include <iprt/path.h>
31#include <iprt/cpp/path.h>
32#include <iprt/dir.h>
33#include <iprt/file.h>
34#include <iprt/s3.h>
35#include <iprt/sha.h>
36#include <iprt/manifest.h>
37#include <iprt/tar.h>
38#include <iprt/zip.h>
39#include <iprt/stream.h>
40#include <iprt/crypto/digest.h>
41#include <iprt/crypto/pkix.h>
42#include <iprt/crypto/store.h>
43#include <iprt/crypto/x509.h>
44#include <iprt/rand.h>
45
46#include <VBox/vd.h>
47#include <VBox/com/array.h>
48
49#include "ApplianceImpl.h"
50#include "VirtualBoxImpl.h"
51#include "GuestOSTypeImpl.h"
52#include "ProgressImpl.h"
53#include "MachineImpl.h"
54#include "MediumImpl.h"
55#include "MediumFormatImpl.h"
56#include "SystemPropertiesImpl.h"
57#include "HostImpl.h"
58
59#include "AutoCaller.h"
60#include "LoggingNew.h"
61
62#include "ApplianceImplPrivate.h"
63#include "CertificateImpl.h"
64#include "ovfreader.h"
65
66#include <VBox/param.h>
67#include <VBox/version.h>
68#include <VBox/settings.h>
69
70#include <set>
71
72using namespace std;
73
74////////////////////////////////////////////////////////////////////////////////
75//
76// IAppliance public methods
77//
78////////////////////////////////////////////////////////////////////////////////
79
80/**
81 * Public method implementation. This opens the OVF with ovfreader.cpp.
82 * Thread implementation is in Appliance::readImpl().
83 *
84 * @param aFile File to read the appliance from.
85 * @param aProgress Progress object.
86 * @return
87 */
88HRESULT Appliance::read(const com::Utf8Str &aFile,
89 ComPtr<IProgress> &aProgress)
90{
91 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
92
93 if (!i_isApplianceIdle())
94 return E_ACCESSDENIED;
95
96 if (m->pReader)
97 {
98 delete m->pReader;
99 m->pReader = NULL;
100 }
101
102 /* Parse all necessary info out of the URI (please not how stupid utterly wasteful
103 this status & allocation error throwing is): */
104 try
105 {
106 i_parseURI(aFile, m->locInfo); /* may throw hrc. */
107 }
108 catch (HRESULT aRC)
109 {
110 return aRC;
111 }
112 catch (std::bad_alloc &)
113 {
114 return E_OUTOFMEMORY;
115 }
116
117 // see if we can handle this file; for now we insist it has an ovf/ova extension
118 if ( m->locInfo.storageType == VFSType_File
119 && !aFile.endsWith(".ovf", Utf8Str::CaseInsensitive)
120 && !aFile.endsWith(".ova", Utf8Str::CaseInsensitive))
121 return setError(VBOX_E_FILE_ERROR, tr("Appliance file must have .ovf or .ova extension"));
122
123 ComObjPtr<Progress> progress;
124 HRESULT hrc = i_readImpl(m->locInfo, progress);
125 if (SUCCEEDED(hrc))
126 progress.queryInterfaceTo(aProgress.asOutParam());
127 return hrc;
128}
129
130/**
131 * Public method implementation. This looks at the output of ovfreader.cpp and creates
132 * VirtualSystemDescription instances.
133 * @return
134 */
135HRESULT Appliance::interpret()
136{
137 /// @todo
138 // - don't use COM methods but the methods directly (faster, but needs appropriate
139 // locking of that objects itself (s. HardDisk))
140 // - Appropriate handle errors like not supported file formats
141 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
142
143 if (!i_isApplianceIdle())
144 return E_ACCESSDENIED;
145
146 HRESULT hrc = S_OK;
147
148 /* Clear any previous virtual system descriptions */
149 m->virtualSystemDescriptions.clear();
150
151 if (m->locInfo.storageType == VFSType_File && !m->pReader)
152 return setError(E_FAIL,
153 tr("Cannot interpret appliance without reading it first (call read() before interpret())"));
154
155 // Change the appliance state so we can safely leave the lock while doing time-consuming
156 // medium imports; also the below method calls do all kinds of locking which conflicts with
157 // the appliance object lock
158 m->state = ApplianceImporting;
159 alock.release();
160
161 /* Try/catch so we can clean up on error */
162 try
163 {
164 list<ovf::VirtualSystem>::const_iterator it;
165 /* Iterate through all virtual systems */
166 for (it = m->pReader->m_llVirtualSystems.begin();
167 it != m->pReader->m_llVirtualSystems.end();
168 ++it)
169 {
170 const ovf::VirtualSystem &vsysThis = *it;
171
172 ComObjPtr<VirtualSystemDescription> pNewDesc;
173 hrc = pNewDesc.createObject();
174 if (FAILED(hrc)) throw hrc;
175 hrc = pNewDesc->init();
176 if (FAILED(hrc)) throw hrc;
177
178 // if the virtual system in OVF had a <vbox:Machine> element, have the
179 // VirtualBox settings code parse that XML now
180 if (vsysThis.pelmVBoxMachine)
181 pNewDesc->i_importVBoxMachineXML(*vsysThis.pelmVBoxMachine);
182
183 // Guest OS type
184 // This is taken from one of three places, in this order:
185 Utf8Str strOsTypeVBox;
186 Utf8StrFmt strCIMOSType("%RU32", (uint32_t)vsysThis.cimos);
187 // 1) If there is a <vbox:Machine>, then use the type from there.
188 if ( vsysThis.pelmVBoxMachine
189 && pNewDesc->m->pConfig->machineUserData.strOsType.isNotEmpty()
190 )
191 strOsTypeVBox = pNewDesc->m->pConfig->machineUserData.strOsType;
192 // 2) Otherwise, if there is OperatingSystemSection/vbox:OSType, use that one.
193 else if (vsysThis.strTypeVBox.isNotEmpty()) // OVFReader has found vbox:OSType
194 strOsTypeVBox = vsysThis.strTypeVBox;
195 // 3) Otherwise, make a best guess what the vbox type is from the OVF (CIM) OS type.
196 else
197 convertCIMOSType2VBoxOSType(strOsTypeVBox, vsysThis.cimos, vsysThis.strCimosDesc);
198 pNewDesc->i_addEntry(VirtualSystemDescriptionType_OS,
199 "",
200 strCIMOSType,
201 strOsTypeVBox);
202
203 /* VM name */
204 Utf8Str nameVBox;
205 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
206 if ( vsysThis.pelmVBoxMachine
207 && pNewDesc->m->pConfig->machineUserData.strName.isNotEmpty())
208 nameVBox = pNewDesc->m->pConfig->machineUserData.strName;
209 else
210 nameVBox = vsysThis.strName;
211 /* If there isn't any name specified create a default one out
212 * of the OS type */
213 if (nameVBox.isEmpty())
214 nameVBox = strOsTypeVBox;
215 i_searchUniqueVMName(nameVBox);
216 pNewDesc->i_addEntry(VirtualSystemDescriptionType_Name,
217 "",
218 vsysThis.strName,
219 nameVBox);
220
221 /* VM Primary Group */
222 Utf8Str strPrimaryGroup;
223 if ( vsysThis.pelmVBoxMachine
224 && pNewDesc->m->pConfig->machineUserData.llGroups.size())
225 strPrimaryGroup = pNewDesc->m->pConfig->machineUserData.llGroups.front();
226 if (strPrimaryGroup.isEmpty())
227 strPrimaryGroup = "/";
228 pNewDesc->i_addEntry(VirtualSystemDescriptionType_PrimaryGroup,
229 "",
230 "" /* no direct OVF correspondence */,
231 strPrimaryGroup);
232
233 /* Based on the VM name, create a target machine path. */
234 Bstr bstrSettingsFilename;
235 hrc = mVirtualBox->ComposeMachineFilename(Bstr(nameVBox).raw(),
236 Bstr(strPrimaryGroup).raw(),
237 NULL /* aCreateFlags */,
238 NULL /* aBaseFolder */,
239 bstrSettingsFilename.asOutParam());
240 if (FAILED(hrc)) throw hrc;
241 Utf8Str strMachineFolder(bstrSettingsFilename);
242 strMachineFolder.stripFilename();
243
244#if 1
245 /* The import logic should work exactly the same whether the
246 * following 2 items are present or not, but of course it may have
247 * an influence on the exact presentation of the import settings
248 * of an API client. */
249 Utf8Str strSettingsFilename(bstrSettingsFilename);
250 pNewDesc->i_addEntry(VirtualSystemDescriptionType_SettingsFile,
251 "",
252 "" /* no direct OVF correspondence */,
253 strSettingsFilename);
254 Utf8Str strBaseFolder;
255 mVirtualBox->i_getDefaultMachineFolder(strBaseFolder);
256 pNewDesc->i_addEntry(VirtualSystemDescriptionType_BaseFolder,
257 "",
258 "" /* no direct OVF correspondence */,
259 strBaseFolder);
260#endif
261
262 /* VM Product */
263 if (!vsysThis.strProduct.isEmpty())
264 pNewDesc->i_addEntry(VirtualSystemDescriptionType_Product,
265 "",
266 vsysThis.strProduct,
267 vsysThis.strProduct);
268
269 /* VM Vendor */
270 if (!vsysThis.strVendor.isEmpty())
271 pNewDesc->i_addEntry(VirtualSystemDescriptionType_Vendor,
272 "",
273 vsysThis.strVendor,
274 vsysThis.strVendor);
275
276 /* VM Version */
277 if (!vsysThis.strVersion.isEmpty())
278 pNewDesc->i_addEntry(VirtualSystemDescriptionType_Version,
279 "",
280 vsysThis.strVersion,
281 vsysThis.strVersion);
282
283 /* VM ProductUrl */
284 if (!vsysThis.strProductUrl.isEmpty())
285 pNewDesc->i_addEntry(VirtualSystemDescriptionType_ProductUrl,
286 "",
287 vsysThis.strProductUrl,
288 vsysThis.strProductUrl);
289
290 /* VM VendorUrl */
291 if (!vsysThis.strVendorUrl.isEmpty())
292 pNewDesc->i_addEntry(VirtualSystemDescriptionType_VendorUrl,
293 "",
294 vsysThis.strVendorUrl,
295 vsysThis.strVendorUrl);
296
297 /* VM description */
298 if (!vsysThis.strDescription.isEmpty())
299 pNewDesc->i_addEntry(VirtualSystemDescriptionType_Description,
300 "",
301 vsysThis.strDescription,
302 vsysThis.strDescription);
303
304 /* VM license */
305 if (!vsysThis.strLicenseText.isEmpty())
306 pNewDesc->i_addEntry(VirtualSystemDescriptionType_License,
307 "",
308 vsysThis.strLicenseText,
309 vsysThis.strLicenseText);
310
311 /* Now that we know the OS type, get our internal defaults based on
312 * that, if it is known (otherwise pGuestOSType will be NULL). */
313 ComPtr<IGuestOSType> pGuestOSType;
314 mVirtualBox->GetGuestOSType(Bstr(strOsTypeVBox).raw(), pGuestOSType.asOutParam());
315
316 /* CPU count */
317 ULONG cpuCountVBox;
318 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
319 if ( vsysThis.pelmVBoxMachine
320 && pNewDesc->m->pConfig->hardwareMachine.cCPUs)
321 cpuCountVBox = pNewDesc->m->pConfig->hardwareMachine.cCPUs;
322 else
323 cpuCountVBox = vsysThis.cCPUs;
324 /* Check for the constraints */
325 if (cpuCountVBox > SchemaDefs::MaxCPUCount)
326 {
327 i_addWarning(tr("Virtual appliance \"%s\" was configured with %u CPUs however VirtualBox "
328 "supports a maximum of %u CPUs. Setting the CPU count to %u."),
329 vsysThis.strName.c_str(), cpuCountVBox, SchemaDefs::MaxCPUCount, SchemaDefs::MaxCPUCount);
330 cpuCountVBox = SchemaDefs::MaxCPUCount;
331 }
332 if (vsysThis.cCPUs == 0)
333 cpuCountVBox = 1;
334 pNewDesc->i_addEntry(VirtualSystemDescriptionType_CPU,
335 "",
336 Utf8StrFmt("%RU32", (uint32_t)vsysThis.cCPUs),
337 Utf8StrFmt("%RU32", (uint32_t)cpuCountVBox));
338
339 /* RAM (in bytes) */
340 uint64_t ullMemSizeVBox;
341 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
342 if ( vsysThis.pelmVBoxMachine
343 && pNewDesc->m->pConfig->hardwareMachine.ulMemorySizeMB)
344 ullMemSizeVBox = (uint64_t)pNewDesc->m->pConfig->hardwareMachine.ulMemorySizeMB * _1M;
345 else
346 ullMemSizeVBox = vsysThis.ullMemorySize; /* already in bytes via OVFReader::HandleVirtualSystemContent() */
347 /* Check for the constraints */
348 if ( ullMemSizeVBox != 0
349 && ( ullMemSizeVBox < MM_RAM_MIN
350 || ullMemSizeVBox > MM_RAM_MAX
351 )
352 )
353 {
354 i_addWarning(tr("Virtual appliance \"%s\" was configured with %RU64 MB of memory (RAM) "
355 "however VirtualBox supports a minimum of %u MB and a maximum of %u MB "
356 "of memory."),
357 vsysThis.strName.c_str(), ullMemSizeVBox / _1M, MM_RAM_MIN_IN_MB, MM_RAM_MAX_IN_MB);
358 ullMemSizeVBox = RT_MIN(RT_MAX(ullMemSizeVBox, MM_RAM_MIN), MM_RAM_MAX);
359 }
360 if (vsysThis.ullMemorySize == 0)
361 {
362 /* If the RAM of the OVF is zero, use our predefined values */
363 ULONG memSizeVBox2;
364 if (!pGuestOSType.isNull())
365 {
366 hrc = pGuestOSType->COMGETTER(RecommendedRAM)(&memSizeVBox2);
367 if (FAILED(hrc)) throw hrc;
368 }
369 else
370 memSizeVBox2 = 1024;
371 /* IGuestOSType::recommendedRAM() returns the size in MB so convert to bytes */
372 ullMemSizeVBox = (uint64_t)memSizeVBox2 * _1M;
373 }
374 pNewDesc->i_addEntry(VirtualSystemDescriptionType_Memory,
375 "",
376 Utf8StrFmt("%RU64", vsysThis.ullMemorySize),
377 Utf8StrFmt("%RU64", ullMemSizeVBox));
378
379 /* Audio */
380 Utf8Str strSoundCard;
381 Utf8Str strSoundCardOrig;
382 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
383 if ( vsysThis.pelmVBoxMachine
384 && pNewDesc->m->pConfig->hardwareMachine.audioAdapter.fEnabled)
385 {
386 strSoundCard = Utf8StrFmt("%RU32",
387 (uint32_t)pNewDesc->m->pConfig->hardwareMachine.audioAdapter.controllerType);
388 }
389 else if (vsysThis.strSoundCardType.isNotEmpty())
390 {
391 /* Set the AC97 always for the simple OVF case.
392 * @todo: figure out the hardware which could be possible */
393 strSoundCard = Utf8StrFmt("%RU32", (uint32_t)AudioControllerType_AC97);
394 strSoundCardOrig = vsysThis.strSoundCardType;
395 }
396 if (strSoundCard.isNotEmpty())
397 pNewDesc->i_addEntry(VirtualSystemDescriptionType_SoundCard,
398 "",
399 strSoundCardOrig,
400 strSoundCard);
401
402#ifdef VBOX_WITH_USB
403 /* USB Controller */
404 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
405 if ( ( vsysThis.pelmVBoxMachine
406 && pNewDesc->m->pConfig->hardwareMachine.usbSettings.llUSBControllers.size() > 0)
407 || vsysThis.fHasUsbController)
408 pNewDesc->i_addEntry(VirtualSystemDescriptionType_USBController, "", "", "");
409#endif /* VBOX_WITH_USB */
410
411 /* Network Controller */
412 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
413 if (vsysThis.pelmVBoxMachine)
414 {
415 uint32_t maxNetworkAdapters = Global::getMaxNetworkAdapters(pNewDesc->m->pConfig->hardwareMachine.chipsetType);
416
417 const settings::NetworkAdaptersList &llNetworkAdapters = pNewDesc->m->pConfig->hardwareMachine.llNetworkAdapters;
418 /* Check for the constrains */
419 if (llNetworkAdapters.size() > maxNetworkAdapters)
420 i_addWarning(tr("Virtual appliance \"%s\" was configured with %zu network adapters however "
421 "VirtualBox supports a maximum of %u network adapters.", "", llNetworkAdapters.size()),
422 vsysThis.strName.c_str(), llNetworkAdapters.size(), maxNetworkAdapters);
423 /* Iterate through all network adapters. */
424 settings::NetworkAdaptersList::const_iterator it1;
425 size_t a = 0;
426 for (it1 = llNetworkAdapters.begin();
427 it1 != llNetworkAdapters.end() && a < maxNetworkAdapters;
428 ++it1, ++a)
429 {
430 if (it1->fEnabled)
431 {
432 Utf8Str strMode = convertNetworkAttachmentTypeToString(it1->mode);
433 pNewDesc->i_addEntry(VirtualSystemDescriptionType_NetworkAdapter,
434 "", // ref
435 strMode, // orig
436 Utf8StrFmt("%RU32", (uint32_t)it1->type), // conf
437 0,
438 Utf8StrFmt("slot=%RU32;type=%s", it1->ulSlot, strMode.c_str())); // extra conf
439 }
440 }
441 }
442 /* else we use the ovf configuration. */
443 else if (vsysThis.llEthernetAdapters.size() > 0)
444 {
445 size_t cEthernetAdapters = vsysThis.llEthernetAdapters.size();
446 uint32_t maxNetworkAdapters = Global::getMaxNetworkAdapters(ChipsetType_PIIX3);
447
448 /* Check for the constrains */
449 if (cEthernetAdapters > maxNetworkAdapters)
450 i_addWarning(tr("Virtual appliance \"%s\" was configured with %zu network adapters however "
451 "VirtualBox supports a maximum of %u network adapters.", "", cEthernetAdapters),
452 vsysThis.strName.c_str(), cEthernetAdapters, maxNetworkAdapters);
453
454 /* Get the default network adapter type for the selected guest OS */
455 NetworkAdapterType_T defaultAdapterVBox = NetworkAdapterType_Am79C970A;
456 if (!pGuestOSType.isNull())
457 {
458 hrc = pGuestOSType->COMGETTER(AdapterType)(&defaultAdapterVBox);
459 if (FAILED(hrc)) throw hrc;
460 }
461 else
462 {
463#ifdef VBOX_WITH_E1000
464 defaultAdapterVBox = NetworkAdapterType_I82540EM;
465#else
466 defaultAdapterVBox = NetworkAdapterType_Am79C973A;
467#endif
468 }
469
470 ovf::EthernetAdaptersList::const_iterator itEA;
471 /* Iterate through all abstract networks. Ignore network cards
472 * which exceed the limit of VirtualBox. */
473 size_t a = 0;
474 for (itEA = vsysThis.llEthernetAdapters.begin();
475 itEA != vsysThis.llEthernetAdapters.end() && a < maxNetworkAdapters;
476 ++itEA, ++a)
477 {
478 const ovf::EthernetAdapter &ea = *itEA; // logical network to connect to
479 Utf8Str strNetwork = ea.strNetworkName;
480 // make sure it's one of these two
481 if ( (strNetwork.compare("Null", Utf8Str::CaseInsensitive))
482 && (strNetwork.compare("NAT", Utf8Str::CaseInsensitive))
483 && (strNetwork.compare("Bridged", Utf8Str::CaseInsensitive))
484 && (strNetwork.compare("Internal", Utf8Str::CaseInsensitive))
485 && (strNetwork.compare("HostOnly", Utf8Str::CaseInsensitive))
486 && (strNetwork.compare("Generic", Utf8Str::CaseInsensitive))
487 )
488 strNetwork = "Bridged"; // VMware assumes this is the default apparently
489
490 /* Figure out the hardware type */
491 NetworkAdapterType_T nwAdapterVBox = defaultAdapterVBox;
492 if (!ea.strAdapterType.compare("PCNet32", Utf8Str::CaseInsensitive))
493 {
494 /* If the default adapter is already one of the two
495 * PCNet adapters use the default one. If not use the
496 * Am79C970A as fallback. */
497 if (!(defaultAdapterVBox == NetworkAdapterType_Am79C970A ||
498 defaultAdapterVBox == NetworkAdapterType_Am79C973))
499 nwAdapterVBox = NetworkAdapterType_Am79C970A;
500 }
501#ifdef VBOX_WITH_E1000
502 /* VMWare accidentally write this with VirtualCenter 3.5,
503 so make sure in this case always to use the VMWare one */
504 else if (!ea.strAdapterType.compare("E10000", Utf8Str::CaseInsensitive))
505 nwAdapterVBox = NetworkAdapterType_I82545EM;
506 else if (!ea.strAdapterType.compare("E1000", Utf8Str::CaseInsensitive))
507 {
508 /* Check if this OVF was written by VirtualBox */
509 if (Utf8Str(vsysThis.strVirtualSystemType).contains("virtualbox", Utf8Str::CaseInsensitive))
510 {
511 /* If the default adapter is already one of the three
512 * E1000 adapters use the default one. If not use the
513 * I82545EM as fallback. */
514 if (!(defaultAdapterVBox == NetworkAdapterType_I82540EM ||
515 defaultAdapterVBox == NetworkAdapterType_I82543GC ||
516 defaultAdapterVBox == NetworkAdapterType_I82545EM))
517 nwAdapterVBox = NetworkAdapterType_I82540EM;
518 }
519 else
520 /* Always use this one since it's what VMware uses */
521 nwAdapterVBox = NetworkAdapterType_I82545EM;
522 }
523#endif /* VBOX_WITH_E1000 */
524 else if ( !ea.strAdapterType.compare("VirtioNet", Utf8Str::CaseInsensitive)
525 || !ea.strAdapterType.compare("virtio-net", Utf8Str::CaseInsensitive)
526 || !ea.strAdapterType.compare("3", Utf8Str::CaseInsensitive))
527 nwAdapterVBox = NetworkAdapterType_Virtio;
528
529 pNewDesc->i_addEntry(VirtualSystemDescriptionType_NetworkAdapter,
530 "", // ref
531 ea.strNetworkName, // orig
532 Utf8StrFmt("%RU32", (uint32_t)nwAdapterVBox), // conf
533 0,
534 Utf8StrFmt("type=%s", strNetwork.c_str())); // extra conf
535 }
536 }
537
538 /* If there is a <vbox:Machine>, we always prefer the setting from there. */
539 bool fFloppy = false;
540 bool fDVD = false;
541 if (vsysThis.pelmVBoxMachine)
542 {
543 settings::StorageControllersList &llControllers = pNewDesc->m->pConfig->hardwareMachine.storage.llStorageControllers;
544 settings::StorageControllersList::iterator it3;
545 for (it3 = llControllers.begin();
546 it3 != llControllers.end();
547 ++it3)
548 {
549 settings::AttachedDevicesList &llAttachments = it3->llAttachedDevices;
550 settings::AttachedDevicesList::iterator it4;
551 for (it4 = llAttachments.begin();
552 it4 != llAttachments.end();
553 ++it4)
554 {
555 fDVD |= it4->deviceType == DeviceType_DVD;
556 fFloppy |= it4->deviceType == DeviceType_Floppy;
557 if (fFloppy && fDVD)
558 break;
559 }
560 if (fFloppy && fDVD)
561 break;
562 }
563 }
564 else
565 {
566 fFloppy = vsysThis.fHasFloppyDrive;
567 fDVD = vsysThis.fHasCdromDrive;
568 }
569 /* Floppy Drive */
570 if (fFloppy)
571 pNewDesc->i_addEntry(VirtualSystemDescriptionType_Floppy, "", "", "");
572 /* CD Drive */
573 if (fDVD)
574 pNewDesc->i_addEntry(VirtualSystemDescriptionType_CDROM, "", "", "");
575
576 /* Storage Controller */
577 uint16_t cIDEused = 0;
578 uint16_t cSATAused = 0; NOREF(cSATAused);
579 uint16_t cSCSIused = 0; NOREF(cSCSIused);
580 uint16_t cVIRTIOSCSIused = 0; NOREF(cVIRTIOSCSIused);
581
582 ovf::ControllersMap::const_iterator hdcIt;
583 /* Iterate through all storage controllers */
584 for (hdcIt = vsysThis.mapControllers.begin();
585 hdcIt != vsysThis.mapControllers.end();
586 ++hdcIt)
587 {
588 const ovf::HardDiskController &hdc = hdcIt->second;
589
590 switch (hdc.system)
591 {
592 case ovf::HardDiskController::IDE:
593 /* Check for the constrains */
594 if (cIDEused < 4)
595 {
596 /// @todo figure out the IDE types
597 /* Use PIIX4 as default */
598 Utf8Str strType = "PIIX4";
599 if (!hdc.strControllerType.compare("PIIX3", Utf8Str::CaseInsensitive))
600 strType = "PIIX3";
601 else if (!hdc.strControllerType.compare("ICH6", Utf8Str::CaseInsensitive))
602 strType = "ICH6";
603 pNewDesc->i_addEntry(VirtualSystemDescriptionType_HardDiskControllerIDE,
604 hdc.strIdController, // strRef
605 hdc.strControllerType, // aOvfValue
606 strType); // aVBoxValue
607 }
608 else
609 /* Warn only once */
610 if (cIDEused == 2)
611 i_addWarning(tr("Virtual appliance \"%s\" was configured with more than two "
612 "IDE controllers however VirtualBox supports a maximum of two "
613 "IDE controllers."),
614 vsysThis.strName.c_str());
615
616 ++cIDEused;
617 break;
618
619 case ovf::HardDiskController::SATA:
620 /* Check for the constrains */
621 if (cSATAused < 1)
622 {
623 /// @todo figure out the SATA types
624 /* We only support a plain AHCI controller, so use them always */
625 pNewDesc->i_addEntry(VirtualSystemDescriptionType_HardDiskControllerSATA,
626 hdc.strIdController,
627 hdc.strControllerType,
628 "AHCI");
629 }
630 else
631 {
632 /* Warn only once */
633 if (cSATAused == 1)
634 i_addWarning(tr("Virtual appliance \"%s\" was configured with more than one "
635 "SATA controller however VirtualBox supports a maximum of one "
636 "SATA controller."),
637 vsysThis.strName.c_str());
638
639 }
640 ++cSATAused;
641 break;
642
643 case ovf::HardDiskController::SCSI:
644 /* Check for the constrains */
645 if (cSCSIused < 1)
646 {
647 VirtualSystemDescriptionType_T vsdet = VirtualSystemDescriptionType_HardDiskControllerSCSI;
648 Utf8Str hdcController = "LsiLogic";
649 if (!hdc.strControllerType.compare("lsilogicsas", Utf8Str::CaseInsensitive))
650 {
651 // OVF considers SAS a variant of SCSI but VirtualBox considers it a class of its own
652 vsdet = VirtualSystemDescriptionType_HardDiskControllerSAS;
653 hdcController = "LsiLogicSas";
654 }
655 else if (!hdc.strControllerType.compare("BusLogic", Utf8Str::CaseInsensitive))
656 hdcController = "BusLogic";
657 pNewDesc->i_addEntry(vsdet,
658 hdc.strIdController,
659 hdc.strControllerType,
660 hdcController);
661 }
662 else
663 i_addWarning(tr("Virtual appliance \"%s\" was configured with more than one SCSI "
664 "controller of type \"%s\" with ID %s however VirtualBox supports "
665 "a maximum of one SCSI controller for each type."),
666 vsysThis.strName.c_str(),
667 hdc.strControllerType.c_str(),
668 hdc.strIdController.c_str());
669 ++cSCSIused;
670 break;
671
672 case ovf::HardDiskController::VIRTIOSCSI:
673 /* Check for the constrains */
674 if (cVIRTIOSCSIused < 1)
675 {
676 pNewDesc->i_addEntry(VirtualSystemDescriptionType_HardDiskControllerVirtioSCSI,
677 hdc.strIdController,
678 hdc.strControllerType,
679 "VirtioSCSI");
680 }
681 else
682 {
683 /* Warn only once */
684 if (cVIRTIOSCSIused == 1)
685 i_addWarning(tr("Virtual appliance \"%s\" was configured with more than one "
686 "VirtioSCSI controller however VirtualBox supports a maximum "
687 "of one VirtioSCSI controller."),
688 vsysThis.strName.c_str());
689
690 }
691 ++cVIRTIOSCSIused;
692 break;
693
694 }
695 }
696
697 /* Storage devices (hard disks/DVDs/...) */
698 if (vsysThis.mapVirtualDisks.size() > 0)
699 {
700 ovf::VirtualDisksMap::const_iterator itVD;
701 /* Iterate through all storage devices */
702 for (itVD = vsysThis.mapVirtualDisks.begin();
703 itVD != vsysThis.mapVirtualDisks.end();
704 ++itVD)
705 {
706 const ovf::VirtualDisk &hd = itVD->second;
707 /* Get the associated image */
708 ovf::DiskImage di;
709 std::map<RTCString, ovf::DiskImage>::iterator foundDisk;
710
711 foundDisk = m->pReader->m_mapDisks.find(hd.strDiskId);
712 if (foundDisk == m->pReader->m_mapDisks.end())
713 continue;
714 else
715 {
716 di = foundDisk->second;
717 }
718
719 /*
720 * Figure out from URI which format the image has.
721 * There is no strict mapping of image URI to image format.
722 * It's possible we aren't able to recognize some URIs.
723 */
724
725 ComObjPtr<MediumFormat> mediumFormat;
726 hrc = i_findMediumFormatFromDiskImage(di, mediumFormat);
727 if (FAILED(hrc))
728 throw hrc;
729
730 Bstr bstrFormatName;
731 hrc = mediumFormat->COMGETTER(Name)(bstrFormatName.asOutParam());
732 if (FAILED(hrc))
733 throw hrc;
734 Utf8Str vdf = Utf8Str(bstrFormatName);
735
736 /// @todo
737 // - figure out all possible vmdk formats we also support
738 // - figure out if there is a url specifier for vhd already
739 // - we need a url specifier for the vdi format
740
741 Utf8Str strFilename = di.strHref;
742 DeviceType_T devType = DeviceType_Null;
743 if (vdf.compare("VMDK", Utf8Str::CaseInsensitive) == 0)
744 {
745 /* If the href is empty use the VM name as filename */
746 if (!strFilename.length())
747 strFilename = Utf8StrFmt("%s.vmdk", hd.strDiskId.c_str());
748 devType = DeviceType_HardDisk;
749 }
750 else if (vdf.compare("RAW", Utf8Str::CaseInsensitive) == 0)
751 {
752 /* If the href is empty use the VM name as filename */
753 if (!strFilename.length())
754 strFilename = Utf8StrFmt("%s.iso", hd.strDiskId.c_str());
755 devType = DeviceType_DVD;
756 }
757 else
758 throw setError(VBOX_E_FILE_ERROR,
759 tr("Unsupported format for virtual disk image %s in OVF: \"%s\""),
760 di.strHref.c_str(),
761 di.strFormat.c_str());
762
763 /*
764 * Remove last extension from the file name if the file is compressed
765 */
766 if (di.strCompression.compare("gzip", Utf8Str::CaseInsensitive)==0)
767 strFilename.stripSuffix();
768
769 i_ensureUniqueImageFilePath(strMachineFolder, devType, strFilename); /** @todo check the return code! */
770
771 /* find the description for the storage controller
772 * that has the same ID as hd.strIdController */
773 const VirtualSystemDescriptionEntry *pController;
774 if (!(pController = pNewDesc->i_findControllerFromID(hd.strIdController)))
775 throw setError(E_FAIL,
776 tr("Cannot find storage controller with OVF instance ID \"%s\" "
777 "to which medium \"%s\" should be attached"),
778 hd.strIdController.c_str(),
779 di.strHref.c_str());
780
781 /* controller to attach to, and the bus within that controller */
782 Utf8StrFmt strExtraConfig("controller=%RI16;channel=%RI16",
783 pController->ulIndex,
784 hd.ulAddressOnParent);
785 pNewDesc->i_addEntry(VirtualSystemDescriptionType_HardDiskImage,
786 hd.strDiskId,
787 di.strHref,
788 strFilename,
789 di.ulSuggestedSizeMB,
790 strExtraConfig);
791 }
792 }
793
794 m->virtualSystemDescriptions.push_back(pNewDesc);
795 }
796 }
797 catch (HRESULT aRC)
798 {
799 /* On error we clear the list & return */
800 m->virtualSystemDescriptions.clear();
801 hrc = aRC;
802 }
803
804 // reset the appliance state
805 alock.acquire();
806 m->state = ApplianceIdle;
807
808 return hrc;
809}
810
811/**
812 * Public method implementation. This creates one or more new machines according to the
813 * VirtualSystemScription instances created by Appliance::Interpret().
814 * Thread implementation is in Appliance::i_importImpl().
815 * @param aOptions Import options.
816 * @param aProgress Progress object.
817 * @return
818 */
819HRESULT Appliance::importMachines(const std::vector<ImportOptions_T> &aOptions,
820 ComPtr<IProgress> &aProgress)
821{
822 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
823
824 if (aOptions.size())
825 {
826 try
827 {
828 m->optListImport.setCapacity(aOptions.size());
829 for (size_t i = 0; i < aOptions.size(); ++i)
830 m->optListImport.insert(i, aOptions[i]);
831 }
832 catch (std::bad_alloc &)
833 {
834 return E_OUTOFMEMORY;
835 }
836 }
837
838 AssertReturn(!( m->optListImport.contains(ImportOptions_KeepAllMACs)
839 && m->optListImport.contains(ImportOptions_KeepNATMACs) )
840 , E_INVALIDARG);
841
842 // do not allow entering this method if the appliance is busy reading or writing
843 if (!i_isApplianceIdle())
844 return E_ACCESSDENIED;
845
846 //check for the local import only. For import from the Cloud m->pReader is always NULL.
847 if (m->locInfo.storageType == VFSType_File && !m->pReader)
848 return setError(E_FAIL,
849 tr("Cannot import machines without reading it first (call read() before i_importMachines())"));
850
851 ComObjPtr<Progress> progress;
852 HRESULT hrc = i_importImpl(m->locInfo, progress);
853 if (SUCCEEDED(hrc))
854 progress.queryInterfaceTo(aProgress.asOutParam());
855
856 return hrc;
857}
858
859////////////////////////////////////////////////////////////////////////////////
860//
861// Appliance private methods
862//
863////////////////////////////////////////////////////////////////////////////////
864
865/**
866 * Ensures that there is a look-ahead object ready.
867 *
868 * @returns true if there's an object handy, false if end-of-stream.
869 * @throws HRESULT if the next object isn't a regular file. Sets error info
870 * (which is why it's a method on Appliance and not the
871 * ImportStack).
872 */
873bool Appliance::i_importEnsureOvaLookAhead(ImportStack &stack)
874{
875 Assert(stack.hVfsFssOva != NULL);
876 if (stack.hVfsIosOvaLookAhead == NIL_RTVFSIOSTREAM)
877 {
878 RTStrFree(stack.pszOvaLookAheadName);
879 stack.pszOvaLookAheadName = NULL;
880
881 RTVFSOBJTYPE enmType;
882 RTVFSOBJ hVfsObj;
883 int vrc = RTVfsFsStrmNext(stack.hVfsFssOva, &stack.pszOvaLookAheadName, &enmType, &hVfsObj);
884 if (RT_SUCCESS(vrc))
885 {
886 stack.hVfsIosOvaLookAhead = RTVfsObjToIoStream(hVfsObj);
887 RTVfsObjRelease(hVfsObj);
888 if ( ( enmType != RTVFSOBJTYPE_FILE
889 && enmType != RTVFSOBJTYPE_IO_STREAM)
890 || stack.hVfsIosOvaLookAhead == NIL_RTVFSIOSTREAM)
891 throw setError(VBOX_E_FILE_ERROR,
892 tr("Malformed OVA. '%s' is not a regular file (%d)."), stack.pszOvaLookAheadName, enmType);
893 }
894 else if (vrc == VERR_EOF)
895 return false;
896 else
897 throw setErrorVrc(vrc, tr("RTVfsFsStrmNext failed (%Rrc)"), vrc);
898 }
899 return true;
900}
901
902HRESULT Appliance::i_preCheckImageAvailability(ImportStack &stack)
903{
904 if (i_importEnsureOvaLookAhead(stack))
905 return S_OK;
906 throw setError(VBOX_E_FILE_ERROR, tr("Unexpected end of OVA package"));
907 /** @todo r=bird: dunno why this bother returning a value and the caller
908 * having a special 'continue' case for it. It always threw all non-OK
909 * status codes. It's possibly to handle out of order stuff, so that
910 * needs adding to the testcase! */
911}
912
913/**
914 * Opens a source file (for reading obviously).
915 *
916 * @param stack
917 * @param rstrSrcPath The source file to open.
918 * @param pszManifestEntry The manifest entry of the source file. This is
919 * used when constructing our manifest using a pass
920 * thru.
921 * @returns I/O stream handle to the source file.
922 * @throws HRESULT error status, error info set.
923 */
924RTVFSIOSTREAM Appliance::i_importOpenSourceFile(ImportStack &stack, Utf8Str const &rstrSrcPath, const char *pszManifestEntry)
925{
926 /*
927 * Open the source file. Special considerations for OVAs.
928 */
929 RTVFSIOSTREAM hVfsIosSrc;
930 if (stack.hVfsFssOva != NIL_RTVFSFSSTREAM)
931 {
932 for (uint32_t i = 0;; i++)
933 {
934 if (!i_importEnsureOvaLookAhead(stack))
935 throw setErrorBoth(VBOX_E_FILE_ERROR, VERR_EOF,
936 tr("Unexpected end of OVA / internal error - missing '%s' (skipped %u)"),
937 rstrSrcPath.c_str(), i);
938 if (RTStrICmp(stack.pszOvaLookAheadName, rstrSrcPath.c_str()) == 0)
939 break;
940
941 /* release the current object, loop to get the next. */
942 RTVfsIoStrmRelease(stack.claimOvaLookAHead());
943 }
944 hVfsIosSrc = stack.claimOvaLookAHead();
945 }
946 else
947 {
948 int vrc = RTVfsIoStrmOpenNormal(rstrSrcPath.c_str(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &hVfsIosSrc);
949 if (RT_FAILURE(vrc))
950 throw setErrorVrc(vrc, tr("Error opening '%s' for reading (%Rrc)"), rstrSrcPath.c_str(), vrc);
951 }
952
953 /*
954 * Digest calculation filtering.
955 */
956 hVfsIosSrc = i_manifestSetupDigestCalculationForGivenIoStream(hVfsIosSrc, pszManifestEntry);
957 if (hVfsIosSrc == NIL_RTVFSIOSTREAM)
958 throw E_FAIL;
959
960 return hVfsIosSrc;
961}
962
963/**
964 * Creates the destination file and fills it with bytes from the source stream.
965 *
966 * This assumes that we digest the source when fDigestTypes is non-zero, and
967 * thus calls RTManifestPtIosAddEntryNow when done.
968 *
969 * @param rstrDstPath The path to the destination file. Missing path
970 * components will be created.
971 * @param hVfsIosSrc The source I/O stream.
972 * @param rstrSrcLogNm The name of the source for logging and error
973 * messages.
974 * @returns COM status code.
975 * @throws Nothing (as the caller has VFS handles to release).
976 */
977HRESULT Appliance::i_importCreateAndWriteDestinationFile(Utf8Str const &rstrDstPath, RTVFSIOSTREAM hVfsIosSrc,
978 Utf8Str const &rstrSrcLogNm)
979{
980 int vrc;
981
982 /*
983 * Create the output file, including necessary paths.
984 * Any existing file will be overwritten.
985 */
986 HRESULT hrc = VirtualBox::i_ensureFilePathExists(rstrDstPath, true /*fCreate*/);
987 if (SUCCEEDED(hrc))
988 {
989 RTVFSIOSTREAM hVfsIosDst;
990 vrc = RTVfsIoStrmOpenNormal(rstrDstPath.c_str(),
991 RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_ALL,
992 &hVfsIosDst);
993 if (RT_SUCCESS(vrc))
994 {
995 /*
996 * Pump the bytes thru. If we fail, delete the output file.
997 */
998 vrc = RTVfsUtilPumpIoStreams(hVfsIosSrc, hVfsIosDst, 0);
999 if (RT_SUCCESS(vrc))
1000 hrc = S_OK;
1001 else
1002 hrc = setErrorVrc(vrc, tr("Error occured decompressing '%s' to '%s' (%Rrc)"),
1003 rstrSrcLogNm.c_str(), rstrDstPath.c_str(), vrc);
1004 uint32_t cRefs = RTVfsIoStrmRelease(hVfsIosDst);
1005 AssertMsg(cRefs == 0, ("cRefs=%u\n", cRefs)); NOREF(cRefs);
1006 if (RT_FAILURE(vrc))
1007 RTFileDelete(rstrDstPath.c_str());
1008 }
1009 else
1010 hrc = setErrorVrc(vrc, tr("Error opening destionation image '%s' for writing (%Rrc)"), rstrDstPath.c_str(), vrc);
1011 }
1012 return hrc;
1013}
1014
1015
1016/**
1017 *
1018 * @param stack Import stack.
1019 * @param rstrSrcPath Source path.
1020 * @param rstrDstPath Destination path.
1021 * @param pszManifestEntry The manifest entry of the source file. This is
1022 * used when constructing our manifest using a pass
1023 * thru.
1024 * @throws HRESULT error status, error info set.
1025 */
1026void Appliance::i_importCopyFile(ImportStack &stack, Utf8Str const &rstrSrcPath, Utf8Str const &rstrDstPath,
1027 const char *pszManifestEntry)
1028{
1029 /*
1030 * Open the file (throws error) and add a read ahead thread so we can do
1031 * concurrent reads (+digest) and writes.
1032 */
1033 RTVFSIOSTREAM hVfsIosSrc = i_importOpenSourceFile(stack, rstrSrcPath, pszManifestEntry);
1034 RTVFSIOSTREAM hVfsIosReadAhead;
1035 int vrc = RTVfsCreateReadAheadForIoStream(hVfsIosSrc, 0 /*fFlags*/, 0 /*cBuffers=default*/, 0 /*cbBuffers=default*/,
1036 &hVfsIosReadAhead);
1037 if (RT_FAILURE(vrc))
1038 {
1039 RTVfsIoStrmRelease(hVfsIosSrc);
1040 throw setErrorVrc(vrc, tr("Error initializing read ahead thread for '%s' (%Rrc)"), rstrSrcPath.c_str(), vrc);
1041 }
1042
1043 /*
1044 * Write the destination file (nothrow).
1045 */
1046 HRESULT hrc = i_importCreateAndWriteDestinationFile(rstrDstPath, hVfsIosReadAhead, rstrSrcPath);
1047 RTVfsIoStrmRelease(hVfsIosReadAhead);
1048
1049 /*
1050 * Before releasing the source stream, make sure we've successfully added
1051 * the digest to our manifest.
1052 */
1053 if (SUCCEEDED(hrc) && m->fDigestTypes)
1054 {
1055 vrc = RTManifestPtIosAddEntryNow(hVfsIosSrc);
1056 if (RT_FAILURE(vrc))
1057 hrc = setErrorVrc(vrc, tr("RTManifestPtIosAddEntryNow failed with %Rrc"), vrc);
1058 }
1059
1060 uint32_t cRefs = RTVfsIoStrmRelease(hVfsIosSrc);
1061 AssertMsg(cRefs == 0, ("cRefs=%u\n", cRefs)); NOREF(cRefs);
1062 if (SUCCEEDED(hrc))
1063 return;
1064 throw hrc;
1065}
1066
1067/**
1068 *
1069 * @param stack
1070 * @param rstrSrcPath
1071 * @param rstrDstPath
1072 * @param pszManifestEntry The manifest entry of the source file. This is
1073 * used when constructing our manifest using a pass
1074 * thru.
1075 * @throws HRESULT error status, error info set.
1076 */
1077void Appliance::i_importDecompressFile(ImportStack &stack, Utf8Str const &rstrSrcPath, Utf8Str const &rstrDstPath,
1078 const char *pszManifestEntry)
1079{
1080 RTVFSIOSTREAM hVfsIosSrcCompressed = i_importOpenSourceFile(stack, rstrSrcPath, pszManifestEntry);
1081
1082 /*
1083 * Add a read ahead thread here. This means reading and digest calculation
1084 * is done on one thread, while unpacking and writing is one on this thread.
1085 */
1086 RTVFSIOSTREAM hVfsIosReadAhead;
1087 int vrc = RTVfsCreateReadAheadForIoStream(hVfsIosSrcCompressed, 0 /*fFlags*/, 0 /*cBuffers=default*/,
1088 0 /*cbBuffers=default*/, &hVfsIosReadAhead);
1089 if (RT_FAILURE(vrc))
1090 {
1091 RTVfsIoStrmRelease(hVfsIosSrcCompressed);
1092 throw setErrorVrc(vrc, tr("Error initializing read ahead thread for '%s' (%Rrc)"), rstrSrcPath.c_str(), vrc);
1093 }
1094
1095 /*
1096 * Add decompression step.
1097 */
1098 RTVFSIOSTREAM hVfsIosSrc;
1099 vrc = RTZipGzipDecompressIoStream(hVfsIosReadAhead, 0, &hVfsIosSrc);
1100 RTVfsIoStrmRelease(hVfsIosReadAhead);
1101 if (RT_FAILURE(vrc))
1102 {
1103 RTVfsIoStrmRelease(hVfsIosSrcCompressed);
1104 throw setErrorVrc(vrc, tr("Error initializing gzip decompression for '%s' (%Rrc)"), rstrSrcPath.c_str(), vrc);
1105 }
1106
1107 /*
1108 * Write the stream to the destination file (nothrow).
1109 */
1110 HRESULT hrc = i_importCreateAndWriteDestinationFile(rstrDstPath, hVfsIosSrc, rstrSrcPath);
1111
1112 /*
1113 * Before releasing the source stream, make sure we've successfully added
1114 * the digest to our manifest.
1115 */
1116 if (SUCCEEDED(hrc) && m->fDigestTypes)
1117 {
1118 vrc = RTManifestPtIosAddEntryNow(hVfsIosSrcCompressed);
1119 if (RT_FAILURE(vrc))
1120 hrc = setErrorVrc(vrc, tr("RTManifestPtIosAddEntryNow failed with %Rrc"), vrc);
1121 }
1122
1123 uint32_t cRefs = RTVfsIoStrmRelease(hVfsIosSrc);
1124 AssertMsg(cRefs == 0, ("cRefs=%u\n", cRefs)); NOREF(cRefs);
1125
1126 cRefs = RTVfsIoStrmRelease(hVfsIosSrcCompressed);
1127 AssertMsg(cRefs == 0, ("cRefs=%u\n", cRefs)); NOREF(cRefs);
1128
1129 if (SUCCEEDED(hrc))
1130 return;
1131 throw hrc;
1132}
1133
1134/*******************************************************************************
1135 * Read stuff
1136 ******************************************************************************/
1137
1138/**
1139 * Implementation for reading an OVF (via task).
1140 *
1141 * This starts a new thread which will call
1142 * Appliance::taskThreadImportOrExport() which will then call readFS(). This
1143 * will then open the OVF with ovfreader.cpp.
1144 *
1145 * This is in a separate private method because it is used from two locations:
1146 *
1147 * 1) from the public Appliance::Read().
1148 *
1149 * 2) in a second worker thread; in that case, Appliance::ImportMachines() called Appliance::i_importImpl(), which
1150 * called Appliance::readFSOVA(), which called Appliance::i_importImpl(), which then called this again.
1151 *
1152 * @returns COM status with error info set.
1153 * @param aLocInfo The OVF location.
1154 * @param aProgress Where to return the progress object.
1155 */
1156HRESULT Appliance::i_readImpl(const LocationInfo &aLocInfo, ComObjPtr<Progress> &aProgress)
1157{
1158 /*
1159 * Create the progress object.
1160 */
1161 HRESULT hrc;
1162 aProgress.createObject();
1163 try
1164 {
1165 if (aLocInfo.storageType == VFSType_Cloud)
1166 {
1167 /* 1 operation only */
1168 hrc = aProgress->init(mVirtualBox, static_cast<IAppliance*>(this),
1169 Utf8Str(tr("Getting cloud instance information")), TRUE /* aCancelable */);
1170
1171 /* Create an empty ovf::OVFReader for manual filling it.
1172 * It's not a normal usage case, but we try to re-use some OVF stuff to friend
1173 * the cloud import with OVF import.
1174 * In the standard case the ovf::OVFReader is created in the Appliance::i_readOVFFile().
1175 * We need the existing m->pReader for Appliance::i_importCloudImpl() where we re-use OVF logic. */
1176 m->pReader = new ovf::OVFReader();
1177 }
1178 else
1179 {
1180 Utf8StrFmt strDesc(tr("Reading appliance '%s'"), aLocInfo.strPath.c_str());
1181 if (aLocInfo.storageType == VFSType_File)
1182 /* 1 operation only */
1183 hrc = aProgress->init(mVirtualBox, static_cast<IAppliance*>(this), strDesc, TRUE /* aCancelable */);
1184 else
1185 /* 4/5 is downloading, 1/5 is reading */
1186 hrc = aProgress->init(mVirtualBox, static_cast<IAppliance*>(this), strDesc, TRUE /* aCancelable */,
1187 2, // ULONG cOperations,
1188 5, // ULONG ulTotalOperationsWeight,
1189 Utf8StrFmt(tr("Download appliance '%s'"),
1190 aLocInfo.strPath.c_str()), // CBSTR bstrFirstOperationDescription,
1191 4); // ULONG ulFirstOperationWeight,
1192 }
1193 }
1194 catch (std::bad_alloc &) /* Utf8Str/Utf8StrFmt */
1195 {
1196 return E_OUTOFMEMORY;
1197 }
1198 if (FAILED(hrc))
1199 return hrc;
1200
1201 /*
1202 * Initialize the worker task.
1203 */
1204 ThreadTask *pTask;
1205 try
1206 {
1207 if (aLocInfo.storageType == VFSType_Cloud)
1208 pTask = new TaskCloud(this, TaskCloud::ReadData, aLocInfo, aProgress);
1209 else
1210 pTask = new TaskOVF(this, TaskOVF::Read, aLocInfo, aProgress);
1211 }
1212 catch (std::bad_alloc &)
1213 {
1214 return E_OUTOFMEMORY;
1215 }
1216
1217 /*
1218 * Kick off the worker thread.
1219 */
1220 hrc = pTask->createThread();
1221 pTask = NULL; /* Note! createThread has consumed the task.*/
1222 if (SUCCEEDED(hrc))
1223 return hrc;
1224 return setError(hrc, tr("Failed to create thread for reading appliance data"));
1225}
1226
1227HRESULT Appliance::i_gettingCloudData(TaskCloud *pTask)
1228{
1229 LogFlowFuncEnter();
1230 LogFlowFunc(("Appliance %p\n", this));
1231
1232 AutoCaller autoCaller(this);
1233 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
1234
1235 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
1236
1237 HRESULT hrc = S_OK;
1238
1239 try
1240 {
1241 Utf8Str strBasename(pTask->locInfo.strPath);
1242 RTCList<RTCString, RTCString *> parts = strBasename.split("/");
1243 if (parts.size() != 2)//profile + instance id
1244 return setErrorVrc(VERR_MISMATCH,
1245 tr("%s: The profile name or instance id are absent or contain unsupported characters: %s"),
1246 __FUNCTION__, strBasename.c_str());
1247
1248 //Get information about the passed cloud instance
1249 ComPtr<ICloudProviderManager> cpm;
1250 hrc = mVirtualBox->COMGETTER(CloudProviderManager)(cpm.asOutParam());
1251 if (FAILED(hrc))
1252 return setError(VBOX_E_OBJECT_NOT_FOUND, tr("%s: Cloud provider manager object wasn't found (%Rhrc)"), __FUNCTION__, hrc);
1253
1254 Utf8Str strProviderName = pTask->locInfo.strProvider;
1255 ComPtr<ICloudProvider> cloudProvider;
1256 ComPtr<ICloudProfile> cloudProfile;
1257 hrc = cpm->GetProviderByShortName(Bstr(strProviderName.c_str()).raw(), cloudProvider.asOutParam());
1258
1259 if (FAILED(hrc))
1260 return setError(VBOX_E_OBJECT_NOT_FOUND, tr("%s: Cloud provider object wasn't found (%Rhrc)"), __FUNCTION__, hrc);
1261
1262 Utf8Str profileName(parts.at(0));//profile
1263 if (profileName.isEmpty())
1264 return setError(VBOX_E_OBJECT_NOT_FOUND, tr("%s: Cloud user profile name wasn't found (%Rhrc)"), __FUNCTION__, hrc);
1265
1266 hrc = cloudProvider->GetProfileByName(Bstr(parts.at(0)).raw(), cloudProfile.asOutParam());
1267 if (FAILED(hrc))
1268 return setError(VBOX_E_OBJECT_NOT_FOUND, tr("%s: Cloud profile object wasn't found (%Rhrc)"), __FUNCTION__, hrc);
1269
1270 ComObjPtr<ICloudClient> cloudClient;
1271 hrc = cloudProfile->CreateCloudClient(cloudClient.asOutParam());
1272 if (FAILED(hrc))
1273 return setError(VBOX_E_OBJECT_NOT_FOUND, tr("%s: Cloud client object wasn't found (%Rhrc)"), __FUNCTION__, hrc);
1274
1275 m->virtualSystemDescriptions.clear();//clear all for assurance before creating new
1276 std::vector<ComPtr<IVirtualSystemDescription> > vsdArray;
1277 ULONG requestedVSDnums = 1;
1278 ULONG newVSDnums = 0;
1279 hrc = createVirtualSystemDescriptions(requestedVSDnums, &newVSDnums);
1280 if (FAILED(hrc)) throw hrc;
1281 if (requestedVSDnums != newVSDnums)
1282 throw setErrorVrc(VERR_MISMATCH, tr("%s: Requested (%d) and created (%d) numbers of VSD are differ ."),
1283 __FUNCTION__, requestedVSDnums, newVSDnums);
1284
1285 hrc = getVirtualSystemDescriptions(vsdArray);
1286 if (FAILED(hrc)) throw hrc;
1287 ComPtr<IVirtualSystemDescription> instanceDescription = vsdArray[0];
1288
1289 LogRel(("%s: calling CloudClient::GetInstanceInfo()\n", __FUNCTION__));
1290
1291 ComPtr<IProgress> pProgress;
1292 hrc = cloudClient->GetInstanceInfo(Bstr(parts.at(1)).raw(), instanceDescription, pProgress.asOutParam());
1293 if (FAILED(hrc)) throw hrc;
1294 hrc = pTask->pProgress->WaitForOtherProgressCompletion(pProgress, 60000);//timeout 1 min = 60000 millisec
1295 if (FAILED(hrc)) throw hrc;
1296
1297 // set cloud profile
1298 instanceDescription->AddDescription(VirtualSystemDescriptionType_CloudProfileName, Bstr(profileName).raw(), NULL);
1299
1300 Utf8StrFmt strSetting("VM with id %s imported from the cloud provider %s",
1301 parts.at(1).c_str(), strProviderName.c_str());
1302 // set description
1303 instanceDescription->AddDescription(VirtualSystemDescriptionType_Description, Bstr(strSetting).raw(), NULL);
1304 }
1305 catch (HRESULT arc)
1306 {
1307 LogFlowFunc(("arc=%Rhrc\n", arc));
1308 hrc = arc;
1309 }
1310
1311 LogFlowFunc(("hrc=%Rhrc\n", hrc));
1312 LogFlowFuncLeave();
1313
1314 return hrc;
1315}
1316
1317void Appliance::i_setApplianceState(const ApplianceState &state)
1318{
1319 AutoWriteLock writeLock(this COMMA_LOCKVAL_SRC_POS);
1320 m->state = state;
1321 writeLock.release();
1322}
1323
1324/**
1325 * Actual worker code for import from the Cloud
1326 *
1327 * @param pTask
1328 * @return
1329 */
1330HRESULT Appliance::i_importCloudImpl(TaskCloud *pTask)
1331{
1332 LogFlowFuncEnter();
1333 LogFlowFunc(("Appliance %p\n", this));
1334
1335 int vrc = VINF_SUCCESS;
1336 /** @todo r=klaus This should be a MultiResult, because this can cause
1337 * multiple errors and warnings which should be relevant for the caller.
1338 * Needs some work, because there might be errors which need to be
1339 * excluded if they happen in error recovery code paths. */
1340 HRESULT hrc = S_OK;
1341 bool fKeepDownloadedObject = false;//in the future should be passed from the caller
1342
1343 /* Clear the list of imported machines, if any */
1344 m->llGuidsMachinesCreated.clear();
1345
1346 ComPtr<ICloudProviderManager> cpm;
1347 hrc = mVirtualBox->COMGETTER(CloudProviderManager)(cpm.asOutParam());
1348 if (FAILED(hrc))
1349 return setErrorVrc(VERR_COM_OBJECT_NOT_FOUND, tr("%s: Cloud provider manager object wasn't found"), __FUNCTION__);
1350
1351 Utf8Str strProviderName = pTask->locInfo.strProvider;
1352 ComPtr<ICloudProvider> cloudProvider;
1353 ComPtr<ICloudProfile> cloudProfile;
1354 hrc = cpm->GetProviderByShortName(Bstr(strProviderName.c_str()).raw(), cloudProvider.asOutParam());
1355
1356 if (FAILED(hrc))
1357 return setErrorVrc(VERR_COM_OBJECT_NOT_FOUND, tr("%s: Cloud provider object wasn't found"), __FUNCTION__);
1358
1359 /* Get the actual VSD, only one VSD object can be there for now so just call the function front() */
1360 ComPtr<IVirtualSystemDescription> vsd = m->virtualSystemDescriptions.front();
1361
1362 Utf8Str vsdData;
1363 com::SafeArray<VirtualSystemDescriptionType_T> retTypes;
1364 com::SafeArray<BSTR> aRefs;
1365 com::SafeArray<BSTR> aOvfValues;
1366 com::SafeArray<BSTR> aVBoxValues;
1367 com::SafeArray<BSTR> aExtraConfigValues;
1368
1369/*
1370 * local #define for better reading the code
1371 * uses only the previously locally declared variable names
1372 * set hrc as the result of operation
1373 *
1374 * What the above description fail to say is that this returns:
1375 * - retTypes
1376 * - aRefs
1377 * - aOvfValues
1378 * - aVBoxValues
1379 * - aExtraConfigValues
1380 */
1381/** @todo r=bird: The setNull calls here are implicit in ComSafeArraySasOutParam,
1382 * so we're doing twice here for no good reason! Btw. very untidy to not wrap
1383 * this in do { } while (0) and require ';' when used. */
1384#define GET_VSD_DESCRIPTION_BY_TYPE(aParamType) do { \
1385 retTypes.setNull(); \
1386 aRefs.setNull(); \
1387 aOvfValues.setNull(); \
1388 aVBoxValues.setNull(); \
1389 aExtraConfigValues.setNull(); \
1390 vsd->GetDescriptionByType(aParamType, \
1391 ComSafeArrayAsOutParam(retTypes), \
1392 ComSafeArrayAsOutParam(aRefs), \
1393 ComSafeArrayAsOutParam(aOvfValues), \
1394 ComSafeArrayAsOutParam(aVBoxValues), \
1395 ComSafeArrayAsOutParam(aExtraConfigValues)); \
1396 } while (0)
1397
1398 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_CloudProfileName);
1399 if (aVBoxValues.size() == 0)
1400 return setErrorVrc(VERR_NOT_FOUND, tr("%s: Cloud user profile name wasn't found"), __FUNCTION__);
1401
1402 Utf8Str profileName(aVBoxValues[0]);
1403 if (profileName.isEmpty())
1404 return setErrorVrc(VERR_INVALID_STATE, tr("%s: Cloud user profile name is empty"), __FUNCTION__);
1405
1406 hrc = cloudProvider->GetProfileByName(aVBoxValues[0], cloudProfile.asOutParam());
1407 if (FAILED(hrc))
1408 return setErrorVrc(VERR_COM_OBJECT_NOT_FOUND, tr("%s: Cloud profile object wasn't found"), __FUNCTION__);
1409
1410 ComObjPtr<ICloudClient> cloudClient;
1411 hrc = cloudProfile->CreateCloudClient(cloudClient.asOutParam());
1412 if (FAILED(hrc))
1413 return setErrorVrc(VERR_COM_OBJECT_NOT_FOUND, tr("%s: Cloud client object wasn't found"), __FUNCTION__);
1414
1415 ComPtr<IProgress> pProgress;
1416 hrc = pTask->pProgress.queryInterfaceTo(pProgress.asOutParam());
1417 if (FAILED(hrc))
1418 return hrc;
1419
1420 Utf8Str strOsType;
1421 ComPtr<IGuestOSType> pGuestOSType;
1422 {
1423 VBOXOSTYPE guestOsType = VBOXOSTYPE_Unknown;
1424 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_OS); //aVBoxValues is set in this #define
1425 if (aVBoxValues.size() != 0)
1426 {
1427 strOsType = aVBoxValues[0];
1428 /* Check the OS type */
1429 uint32_t const idxOSType = Global::getOSTypeIndexFromId(strOsType.c_str());
1430 guestOsType = idxOSType < Global::cOSTypes ? Global::sOSTypes[idxOSType].osType : VBOXOSTYPE_Unknown;
1431
1432 /* Case when some invalid OS type or garbage was passed. Set to VBOXOSTYPE_Unknown. */
1433 if (idxOSType > Global::cOSTypes)
1434 {
1435 strOsType = Global::OSTypeId(guestOsType);
1436 vsd->RemoveDescriptionByType(VirtualSystemDescriptionType_OS);
1437 vsd->AddDescription(VirtualSystemDescriptionType_OS,
1438 Bstr(strOsType).raw(),
1439 NULL);
1440 }
1441 }
1442 /* Case when no OS type was passed. Set to VBOXOSTYPE_Unknown. */
1443 else
1444 {
1445 strOsType = Global::OSTypeId(guestOsType);
1446 vsd->AddDescription(VirtualSystemDescriptionType_OS,
1447 Bstr(strOsType).raw(),
1448 NULL);
1449 }
1450
1451 LogRel(("%s: OS type is %s\n", __FUNCTION__, strOsType.c_str()));
1452
1453 /* We can get some default settings from GuestOSType when it's needed */
1454 hrc = mVirtualBox->GetGuestOSType(Bstr(strOsType).raw(), pGuestOSType.asOutParam());
1455 if (FAILED(hrc))
1456 return hrc;
1457 }
1458
1459 /* Should be defined here because it's used later, at least when ComposeMachineFilename() is called */
1460 Utf8Str strVMName("VM_exported_from_cloud");
1461
1462 if (m->virtualSystemDescriptions.size() == 1)
1463 {
1464 do
1465 {
1466 ComPtr<IVirtualBox> VBox(mVirtualBox);
1467
1468 {
1469 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_Name); //aVBoxValues is set in this #define
1470 if (aVBoxValues.size() != 0)//paranoia but anyway...
1471 strVMName = aVBoxValues[0];
1472 LogRel(("%s: VM name is %s\n", __FUNCTION__, strVMName.c_str()));
1473 }
1474
1475// i_searchUniqueVMName(strVMName);//internally calls setError() in the case of absent the registered VM with such name
1476
1477 ComPtr<IMachine> machine;
1478 hrc = mVirtualBox->FindMachine(Bstr(strVMName.c_str()).raw(), machine.asOutParam());
1479 if (SUCCEEDED(hrc))
1480 {
1481 /* what to do? create a new name from the old one with some suffix? */
1482 uint64_t uRndSuff = RTRandU64();
1483 vrc = strVMName.appendPrintfNoThrow("__%RU64", uRndSuff);
1484 AssertRCBreakStmt(vrc, hrc = E_OUTOFMEMORY);
1485
1486 vsd->RemoveDescriptionByType(VirtualSystemDescriptionType_Name);
1487 vsd->AddDescription(VirtualSystemDescriptionType_Name,
1488 Bstr(strVMName).raw(),
1489 NULL);
1490 /* No check again because it would be weird if a VM with such unique name exists */
1491 }
1492
1493 /* Check the target path. If the path exists and folder isn't empty return an error */
1494 {
1495 Bstr bstrSettingsFilename;
1496 /* Based on the VM name, create a target machine path. */
1497 hrc = mVirtualBox->ComposeMachineFilename(Bstr(strVMName).raw(),
1498 Bstr("/").raw(),
1499 NULL /* aCreateFlags */,
1500 NULL /* aBaseFolder */,
1501 bstrSettingsFilename.asOutParam());
1502 if (FAILED(hrc))
1503 break;
1504
1505 Utf8Str strMachineFolder(bstrSettingsFilename);
1506 strMachineFolder.stripFilename();
1507
1508 RTFSOBJINFO dirInfo;
1509 vrc = RTPathQueryInfo(strMachineFolder.c_str(), &dirInfo, RTFSOBJATTRADD_NOTHING);
1510 if (RT_SUCCESS(vrc))
1511 {
1512 size_t counter = 0;
1513 RTDIR hDir;
1514 vrc = RTDirOpen(&hDir, strMachineFolder.c_str());
1515 if (RT_SUCCESS(vrc))
1516 {
1517 RTDIRENTRY DirEntry;
1518 while (RT_SUCCESS(RTDirRead(hDir, &DirEntry, NULL)))
1519 {
1520 if (RTDirEntryIsStdDotLink(&DirEntry))
1521 continue;
1522 ++counter;
1523 }
1524
1525 if ( hDir != NULL)
1526 vrc = RTDirClose(hDir);
1527 }
1528 else
1529 return setErrorVrc(vrc, tr("Can't open folder %s"), strMachineFolder.c_str());
1530
1531 if (counter > 0)
1532 return setErrorVrc(VERR_ALREADY_EXISTS,
1533 tr("The target folder %s has already contained some files (%d items). Clear the folder from the files or choose another folder"),
1534 strMachineFolder.c_str(), counter);
1535 }
1536 }
1537
1538 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_CloudInstanceId); //aVBoxValues is set in this #define
1539 if (aVBoxValues.size() == 0)
1540 return setErrorVrc(VERR_NOT_FOUND, "%s: Cloud Instance Id wasn't found", __FUNCTION__);
1541
1542 Utf8Str strInsId = aVBoxValues[0];
1543
1544 LogRelFunc(("calling CloudClient::ImportInstance\n"));
1545
1546 /* Here it's strongly supposed that cloud import produces ONE object on the disk.
1547 * Because it much easier to manage one object in any case.
1548 * In the case when cloud import creates several object on the disk all of them
1549 * must be combined together into one object by cloud client.
1550 * The most simple way is to create a TAR archive. */
1551 hrc = cloudClient->ImportInstance(m->virtualSystemDescriptions.front(), pProgress);
1552 if (FAILED(hrc))
1553 {
1554 LogRelFunc(("Cloud import (cloud phase) failed. Used cloud instance is \'%s\'\n", strInsId.c_str()));
1555 hrc = setError(hrc, tr("%s: Cloud import (cloud phase) failed. Used cloud instance is \'%s\'\n"),
1556 __FUNCTION__, strInsId.c_str());
1557 break;
1558 }
1559
1560 } while (0);
1561 }
1562 else
1563 {
1564 hrc = setErrorVrc(VERR_NOT_SUPPORTED, tr("Import from Cloud isn't supported for more than one VM instance."));
1565 return hrc;
1566 }
1567
1568
1569 /* In any case we delete the cloud leavings which may exist after the first phase (cloud phase).
1570 * Should they be deleted in the OCICloudClient::importInstance()?
1571 * Because deleting them here is not easy as it in the importInstance(). */
1572 {
1573 ErrorInfoKeeper eik; /* save the error info */
1574 HRESULT const hrcSaved = hrc;
1575
1576 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_CloudInstanceId); //aVBoxValues is set in this #define
1577 if (aVBoxValues.size() == 0)
1578 hrc = setErrorVrc(VERR_NOT_FOUND, tr("%s: Cloud cleanup action - the instance wasn't found"), __FUNCTION__);
1579 else
1580 {
1581 vsdData = aVBoxValues[0];
1582
1583 /** @todo
1584 * future function which will eliminate the temporary objects created during the first phase.
1585 * hrc = cloud.EliminateImportLeavings(aVBoxValues[0], pProgress); */
1586/*
1587 if (FAILED(hrc))
1588 {
1589 hrc = setError(hrc, tr("Some leavings may exist in the Cloud."));
1590 LogRel(("%s: Cleanup action - the leavings in the %s after import the "
1591 "instance %s may not have been deleted\n",
1592 __FUNCTION__, strProviderName.c_str(), vsdData.c_str()));
1593 }
1594 else
1595 LogRel(("%s: Cleanup action - the leavings in the %s after import the "
1596 "instance %s have been deleted\n",
1597 __FUNCTION__, strProviderName.c_str(), vsdData.c_str()));
1598*/
1599 }
1600
1601 /* Because during the cleanup phase the hrc may have the good result
1602 * Thus we restore the original error in the case when the cleanup phase was successful
1603 * Otherwise we return not the original error but the last error in the cleanup phase */
1604 /** @todo r=bird: do this conditionally perhaps?
1605 * if (FAILED(hrcSaved))
1606 * hrc = hrcSaved;
1607 * else
1608 * eik.forget();
1609 */
1610 hrc = hrcSaved;
1611 }
1612
1613 if (FAILED(hrc))
1614 {
1615 const char *pszGeneralRollBackErrorMessage = tr("Rollback action for Import Cloud operation failed. "
1616 "Some leavings may exist on the local disk or in the Cloud.");
1617 /*
1618 * Roll-back actions.
1619 * we finish here if:
1620 * 1. Getting the object from the Cloud has been failed.
1621 * 2. Something is wrong with getting data from ComPtr<IVirtualSystemDescription> vsd.
1622 * 3. More than 1 VirtualSystemDescription is presented in the list m->virtualSystemDescriptions.
1623 * Maximum what we have there are:
1624 * 1. The downloaded object, so just check the presence and delete it if one exists
1625 */
1626
1627 { /** @todo r=bird: Pointless {}. */
1628 if (!fKeepDownloadedObject)
1629 {
1630 ErrorInfoKeeper eik; /* save the error info */
1631 HRESULT const hrcSaved = hrc;
1632
1633 /* small explanation here, the image here points out to the whole downloaded object (not to the image only)
1634 * filled during the first cloud import stage (in the ICloudClient::importInstance()) */
1635 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_HardDiskImage); //aVBoxValues is set in this #define
1636 if (aVBoxValues.size() == 0)
1637 hrc = setErrorVrc(VERR_NOT_FOUND, pszGeneralRollBackErrorMessage);
1638 else
1639 {
1640 vsdData = aVBoxValues[0];
1641 //try to delete the downloaded object
1642 bool fExist = RTPathExists(vsdData.c_str());
1643 if (fExist)
1644 {
1645 vrc = RTFileDelete(vsdData.c_str());
1646 if (RT_FAILURE(vrc))
1647 {
1648 hrc = setErrorVrc(vrc, pszGeneralRollBackErrorMessage);
1649 LogRel(("%s: Rollback action - the object %s hasn't been deleted\n", __FUNCTION__, vsdData.c_str()));
1650 }
1651 else
1652 LogRel(("%s: Rollback action - the object %s has been deleted\n", __FUNCTION__, vsdData.c_str()));
1653 }
1654 }
1655
1656 /* Because during the rollback phase the hrc may have the good result
1657 * Thus we restore the original error in the case when the rollback phase was successful
1658 * Otherwise we return not the original error but the last error in the rollback phase */
1659 hrc = hrcSaved;
1660 }
1661 }
1662 }
1663 else
1664 {
1665 Utf8Str strMachineFolder;
1666 Utf8Str strAbsSrcPath;
1667 Utf8Str strGroup("/");//default VM group
1668 Utf8Str strTargetFormat("VMDK");//default image format
1669 Bstr bstrSettingsFilename;
1670 SystemProperties *pSysProps = NULL;
1671 RTCList<Utf8Str> extraCreatedFiles;/* All extra created files, it's used during cleanup */
1672
1673 /* Put all VFS* declaration here because they are needed to be release by the corresponding
1674 RTVfs***Release functions in the case of exception */
1675 RTVFSOBJ hVfsObj = NIL_RTVFSOBJ;
1676 RTVFSFSSTREAM hVfsFssObject = NIL_RTVFSFSSTREAM;
1677 RTVFSIOSTREAM hVfsIosCurr = NIL_RTVFSIOSTREAM;
1678
1679 try
1680 {
1681 /* Small explanation here, the image here points out to the whole downloaded object (not to the image only)
1682 * filled during the first cloud import stage (in the ICloudClient::importInstance()) */
1683 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_HardDiskImage); //aVBoxValues is set in this #define
1684 if (aVBoxValues.size() == 0)
1685 throw setErrorVrc(VERR_NOT_FOUND, "%s: The description of the downloaded object wasn't found", __FUNCTION__);
1686
1687 strAbsSrcPath = aVBoxValues[0];
1688
1689 /* Based on the VM name, create a target machine path. */
1690 hrc = mVirtualBox->ComposeMachineFilename(Bstr(strVMName).raw(),
1691 Bstr(strGroup).raw(),
1692 NULL /* aCreateFlags */,
1693 NULL /* aBaseFolder */,
1694 bstrSettingsFilename.asOutParam());
1695 if (FAILED(hrc)) throw hrc;
1696
1697 strMachineFolder = bstrSettingsFilename;
1698 strMachineFolder.stripFilename();
1699
1700 /* Get the system properties. */
1701 pSysProps = mVirtualBox->i_getSystemProperties();
1702 if (pSysProps == NULL)
1703 throw VBOX_E_OBJECT_NOT_FOUND;
1704
1705 ComObjPtr<MediumFormat> trgFormat;
1706 trgFormat = pSysProps->i_mediumFormatFromExtension(strTargetFormat);
1707 if (trgFormat.isNull())
1708 throw VBOX_E_OBJECT_NOT_FOUND;
1709
1710 /* Continue and create new VM using data from VSD and downloaded object.
1711 * The downloaded images should be converted to VDI/VMDK if they have another format */
1712 Utf8Str strInstId("default cloud instance id");
1713 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_CloudInstanceId); //aVBoxValues is set in this #define
1714 if (aVBoxValues.size() != 0)//paranoia but anyway...
1715 strInstId = aVBoxValues[0];
1716 LogRel(("%s: Importing cloud instance %s\n", __FUNCTION__, strInstId.c_str()));
1717
1718 /* Processing the downloaded object (prepare for the local import) */
1719 RTVFSIOSTREAM hVfsIosSrc;
1720 vrc = RTVfsIoStrmOpenNormal(strAbsSrcPath.c_str(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &hVfsIosSrc);
1721 if (RT_FAILURE(vrc))
1722 throw setErrorVrc(vrc, tr("Error opening '%s' for reading (%Rrc)\n"), strAbsSrcPath.c_str(), vrc);
1723
1724 vrc = RTZipTarFsStreamFromIoStream(hVfsIosSrc, 0 /*fFlags*/, &hVfsFssObject);
1725 RTVfsIoStrmRelease(hVfsIosSrc);
1726 if (RT_FAILURE(vrc))
1727 throw setErrorVrc(vrc, tr("Error reading the downloaded file '%s' (%Rrc)"), strAbsSrcPath.c_str(), vrc);
1728
1729 /* Create a new virtual system and work directly on the list copy. */
1730 m->pReader->m_llVirtualSystems.push_back(ovf::VirtualSystem());
1731 ovf::VirtualSystem &vsys = m->pReader->m_llVirtualSystems.back();
1732
1733 /* Try to re-use some OVF stuff here */
1734 {
1735 vsys.strName = strVMName;
1736 uint32_t cpus = 1;
1737 {
1738 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_CPU); //aVBoxValues is set in this #define
1739 if (aVBoxValues.size() != 0)
1740 {
1741 vsdData = aVBoxValues[0];
1742 cpus = vsdData.toUInt32();
1743 }
1744 vsys.cCPUs = (uint16_t)cpus;
1745 LogRel(("%s: Number of CPUs is %s\n", __FUNCTION__, vsdData.c_str()));
1746 }
1747
1748 ULONG memory;//Mb
1749 pGuestOSType->COMGETTER(RecommendedRAM)(&memory);
1750 {
1751 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_Memory); //aVBoxValues is set in this #define
1752 if (aVBoxValues.size() != 0)
1753 {
1754 vsdData = aVBoxValues[0];
1755 if (memory > vsdData.toUInt32())
1756 memory = vsdData.toUInt32();
1757 }
1758 vsys.ullMemorySize = memory;
1759 LogRel(("%s: Size of RAM is %d MB\n", __FUNCTION__, vsys.ullMemorySize));
1760 }
1761
1762 {
1763 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_Description); //aVBoxValues is set in this #define
1764 if (aVBoxValues.size() != 0)
1765 {
1766 vsdData = aVBoxValues[0];
1767 vsys.strDescription = vsdData;
1768 }
1769 LogRel(("%s: VM description \'%s\'\n", __FUNCTION__, vsdData.c_str()));
1770 }
1771
1772 {
1773 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_OS); //aVBoxValues is set in this #define
1774 if (aVBoxValues.size() != 0)
1775 strOsType = aVBoxValues[0];
1776 vsys.strTypeVBox = strOsType;
1777 LogRel(("%s: OS type is %s\n", __FUNCTION__, strOsType.c_str()));
1778 }
1779
1780 ovf::EthernetAdapter ea;
1781 {
1782 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_NetworkAdapter); //aVBoxValues is set in this #define
1783 if (aVBoxValues.size() != 0)
1784 {
1785 ea.strAdapterType = (Utf8Str)(aVBoxValues[0]);
1786 ea.strNetworkName = "NAT";//default
1787 vsys.llEthernetAdapters.push_back(ea);
1788 LogRel(("%s: Network adapter type is %s\n", __FUNCTION__, ea.strAdapterType.c_str()));
1789 }
1790 else
1791 {
1792 NetworkAdapterType_T defaultAdapterType = NetworkAdapterType_Am79C970A;
1793 pGuestOSType->COMGETTER(AdapterType)(&defaultAdapterType);
1794 Utf8StrFmt dat("%RU32", (uint32_t)defaultAdapterType);
1795 vsd->AddDescription(VirtualSystemDescriptionType_NetworkAdapter,
1796 Bstr(dat).raw(),
1797 Bstr(Utf8Str("NAT")).raw());
1798 }
1799 }
1800
1801 ovf::HardDiskController hdc;
1802 {
1803 //It's thought that SATA is supported by any OS types
1804 hdc.system = ovf::HardDiskController::SATA;
1805 hdc.strIdController = "0";
1806
1807 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_HardDiskControllerSATA); //aVBoxValues is set in this #define
1808 if (aVBoxValues.size() != 0)
1809 hdc.strControllerType = (Utf8Str)(aVBoxValues[0]);
1810 else
1811 hdc.strControllerType = "AHCI";
1812
1813 LogRel(("%s: Hard disk controller type is %s\n", __FUNCTION__, hdc.strControllerType.c_str()));
1814 vsys.mapControllers[hdc.strIdController] = hdc;
1815
1816 if (aVBoxValues.size() == 0)
1817 {
1818 /* we should do it here because it'll be used later in the OVF logic (inside i_importMachines()) */
1819 vsd->AddDescription(VirtualSystemDescriptionType_HardDiskControllerSATA,
1820 Bstr(hdc.strControllerType).raw(),
1821 NULL);
1822 }
1823 }
1824
1825 {
1826 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_SoundCard); //aVBoxValues is set in this #define
1827 if (aVBoxValues.size() != 0)
1828 vsys.strSoundCardType = (Utf8Str)(aVBoxValues[0]);
1829 else
1830 {
1831 AudioControllerType_T defaultAudioController;
1832 pGuestOSType->COMGETTER(RecommendedAudioController)(&defaultAudioController);
1833 vsys.strSoundCardType = Utf8StrFmt("%RU32", (uint32_t)defaultAudioController);//"ensoniq1371";//"AC97";
1834 vsd->AddDescription(VirtualSystemDescriptionType_SoundCard,
1835 Bstr(vsys.strSoundCardType).raw(),
1836 NULL);
1837 }
1838
1839 LogRel(("%s: Sound card is %s\n", __FUNCTION__, vsys.strSoundCardType.c_str()));
1840 }
1841
1842 vsys.fHasFloppyDrive = false;
1843 vsys.fHasCdromDrive = false;
1844 vsys.fHasUsbController = true;
1845 }
1846
1847 unsigned currImageObjectNum = 0;
1848 hrc = S_OK;
1849 do
1850 {
1851 char *pszName = NULL;
1852 RTVFSOBJTYPE enmType;
1853 vrc = RTVfsFsStrmNext(hVfsFssObject, &pszName, &enmType, &hVfsObj);
1854 if (RT_FAILURE(vrc))
1855 {
1856 if (vrc != VERR_EOF)
1857 {
1858 hrc = setErrorVrc(vrc, tr("%s: Error reading '%s' (%Rrc)"), __FUNCTION__, strAbsSrcPath.c_str(), vrc);
1859 throw hrc;
1860 }
1861 break;
1862 }
1863
1864 /* We only care about entries that are files. Get the I/O stream handle for them. */
1865 if ( enmType == RTVFSOBJTYPE_IO_STREAM
1866 || enmType == RTVFSOBJTYPE_FILE)
1867 {
1868 /* Find the suffix and check if this is a possibly interesting file. */
1869 char *pszSuffix = RTStrToLower(strrchr(pszName, '.'));
1870
1871 /* Get the I/O stream. */
1872 hVfsIosCurr = RTVfsObjToIoStream(hVfsObj);
1873 Assert(hVfsIosCurr != NIL_RTVFSIOSTREAM);
1874
1875 /* Get the source medium format */
1876 ComObjPtr<MediumFormat> srcFormat;
1877 srcFormat = pSysProps->i_mediumFormatFromExtension(pszSuffix + 1);
1878
1879 /* unknown image format so just extract a file without any processing */
1880 if (srcFormat == NULL)
1881 {
1882 /* Read the file into a memory buffer */
1883 void *pvBuffered;
1884 size_t cbBuffered;
1885 RTVFSFILE hVfsDstFile = NIL_RTVFSFILE;
1886 try
1887 {
1888 vrc = RTVfsIoStrmReadAll(hVfsIosCurr, &pvBuffered, &cbBuffered);
1889 RTVfsIoStrmRelease(hVfsIosCurr);
1890 hVfsIosCurr = NIL_RTVFSIOSTREAM;
1891 if (RT_FAILURE(vrc))
1892 throw setErrorVrc(vrc, tr("Could not read the file '%s' (%Rrc)"), strAbsSrcPath.c_str(), vrc);
1893
1894 Utf8StrFmt strAbsDstPath("%s%s%s", strMachineFolder.c_str(), RTPATH_SLASH_STR, pszName);
1895
1896 /* Simple logic - just try to get dir info, in case of absent try to create one.
1897 No deep errors analysis */
1898 RTFSOBJINFO dirInfo;
1899 vrc = RTPathQueryInfo(strMachineFolder.c_str(), &dirInfo, RTFSOBJATTRADD_NOTHING);
1900 if (RT_FAILURE(vrc))
1901 {
1902 if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
1903 {
1904 vrc = RTDirCreateFullPath(strMachineFolder.c_str(), 0755);
1905 if (RT_FAILURE(vrc))
1906 throw setErrorVrc(vrc, tr("Could not create the directory '%s' (%Rrc)"),
1907 strMachineFolder.c_str(), vrc);
1908 }
1909 else
1910 throw setErrorVrc(vrc, tr("Error during getting info about the directory '%s' (%Rrc)"),
1911 strMachineFolder.c_str(), vrc);
1912 }
1913
1914 /* Write the file on the disk */
1915 vrc = RTVfsFileOpenNormal(strAbsDstPath.c_str(),
1916 RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE,
1917 &hVfsDstFile);
1918 if (RT_FAILURE(vrc))
1919 throw setErrorVrc(vrc, tr("Could not create the file '%s' (%Rrc)"), strAbsDstPath.c_str(), vrc);
1920
1921 size_t cbWritten;
1922 vrc = RTVfsFileWrite(hVfsDstFile, pvBuffered, cbBuffered, &cbWritten);
1923 if (RT_FAILURE(vrc))
1924 throw setErrorVrc(vrc, tr("Could not write into the file '%s' (%Rrc)"), strAbsDstPath.c_str(), vrc);
1925
1926 /* Remember this file */
1927 extraCreatedFiles.append(strAbsDstPath);
1928 }
1929 catch (HRESULT aRc)
1930 {
1931 hrc = aRc;
1932 LogRel(("%s: Processing the downloaded object was failed. The exception (%Rhrc)\n",
1933 __FUNCTION__, hrc));
1934 }
1935 catch (int aRc)
1936 {
1937 hrc = setErrorVrc(aRc);
1938 LogRel(("%s: Processing the downloaded object was failed. The exception (%Rrc/%Rhrc)\n",
1939 __FUNCTION__, aRc, hrc));
1940 }
1941 catch (...)
1942 {
1943 hrc = setErrorVrc(VERR_UNEXPECTED_EXCEPTION);
1944 LogRel(("%s: Processing the downloaded object was failed. The exception (VERR_UNEXPECTED_EXCEPTION/%Rhrc)\n",
1945 __FUNCTION__, hrc));
1946 }
1947 }
1948 else
1949 {
1950 /* Just skip the rest images if they exist. Only the first image is used as the base image. */
1951 if (currImageObjectNum >= 1)
1952 continue;
1953
1954 /* Image format is supported by VBox so extract the file and try to convert
1955 * one to the default format (which is VMDK for now) */
1956 Utf8Str z(bstrSettingsFilename);
1957 Utf8StrFmt strAbsDstPath("%s_%d.%s",
1958 z.stripSuffix().c_str(),
1959 currImageObjectNum,
1960 strTargetFormat.c_str());
1961
1962 hrc = mVirtualBox->i_findHardDiskByLocation(strAbsDstPath, false, NULL);
1963 if (SUCCEEDED(hrc))
1964 throw setErrorVrc(VERR_ALREADY_EXISTS, tr("The hard disk '%s' already exists."), strAbsDstPath.c_str());
1965
1966 /* Create an IMedium object. */
1967 ComObjPtr<Medium> pTargetMedium;
1968 pTargetMedium.createObject();
1969 hrc = pTargetMedium->init(mVirtualBox,
1970 strTargetFormat,
1971 strAbsDstPath,
1972 Guid::Empty /* media registry: none yet */,
1973 DeviceType_HardDisk);
1974 if (FAILED(hrc))
1975 throw hrc;
1976
1977 pTask->pProgress->SetNextOperation(BstrFmt(tr("Importing virtual disk image '%s'"), pszName).raw(),
1978 200);
1979 ComObjPtr<Medium> nullParent;
1980 ComPtr<IProgress> pProgressImport;
1981 ComObjPtr<Progress> pProgressImportTmp;
1982 hrc = pProgressImportTmp.createObject();
1983 if (FAILED(hrc))
1984 throw hrc;
1985
1986 hrc = pProgressImportTmp->init(mVirtualBox,
1987 static_cast<IAppliance*>(this),
1988 Utf8StrFmt(tr("Importing medium '%s'"), pszName),
1989 TRUE);
1990 if (FAILED(hrc))
1991 throw hrc;
1992
1993 pProgressImportTmp.queryInterfaceTo(pProgressImport.asOutParam());
1994
1995 hrc = pTargetMedium->i_importFile(pszName,
1996 srcFormat,
1997 MediumVariant_Standard,
1998 hVfsIosCurr,
1999 nullParent,
2000 pProgressImportTmp,
2001 true /* aNotify */);
2002 RTVfsIoStrmRelease(hVfsIosCurr);
2003 hVfsIosCurr = NIL_RTVFSIOSTREAM;
2004 /* Now wait for the background import operation to complete;
2005 * this throws HRESULTs on error. */
2006 hrc = pTask->pProgress->WaitForOtherProgressCompletion(pProgressImport, 0 /* indefinite wait */);
2007
2008 /* Try to re-use some OVF stuff here */
2009 if (SUCCEEDED(hrc))
2010 {
2011 /* Small trick here.
2012 * We add new item into the actual VSD after successful conversion.
2013 * There is no need to delete any previous records describing the images in the VSD
2014 * because later in the code the search of the images in the VSD will use such records
2015 * with the actual image id (d.strDiskId = pTargetMedium->i_getId().toString()) which is
2016 * used as a key for m->pReader->m_mapDisks, vsys.mapVirtualDisks.
2017 * So all 3 objects are tied via the image id.
2018 * In the OVF case we already have all such records in the VSD after reading OVF
2019 * description file (read() and interpret() functions).*/
2020 ovf::DiskImage d;
2021 d.strDiskId = pTargetMedium->i_getId().toString();
2022 d.strHref = pTargetMedium->i_getLocationFull();
2023 d.strFormat = pTargetMedium->i_getFormat();
2024 d.iSize = (int64_t)pTargetMedium->i_getSize();
2025 d.ulSuggestedSizeMB = (uint32_t)(d.iSize/_1M);
2026
2027 m->pReader->m_mapDisks[d.strDiskId] = d;
2028
2029 ComObjPtr<VirtualSystemDescription> vsdescThis = m->virtualSystemDescriptions.front();
2030
2031 /* It's needed here to use the internal function i_addEntry() instead of the API function
2032 * addDescription() because we should pass the d.strDiskId for the proper handling this
2033 * disk later in the i_importMachineGeneric():
2034 * - find the line like this "if (vsdeHD->strRef == diCurrent.strDiskId)".
2035 * if those code can be eliminated then addDescription() will be used. */
2036 vsdescThis->i_addEntry(VirtualSystemDescriptionType_HardDiskImage,
2037 d.strDiskId,
2038 d.strHref,
2039 d.strHref,
2040 d.ulSuggestedSizeMB);
2041
2042 ovf::VirtualDisk vd;
2043 //next line may generates std::out_of_range exception in case of failure
2044 vd.strIdController = vsys.mapControllers.at("0").strIdController;
2045 vd.ulAddressOnParent = 0;
2046 vd.strDiskId = d.strDiskId;
2047 vsys.mapVirtualDisks[vd.strDiskId] = vd;
2048
2049 ++currImageObjectNum;
2050 }
2051 }
2052
2053 RTVfsIoStrmRelease(hVfsIosCurr);
2054 hVfsIosCurr = NIL_RTVFSIOSTREAM;
2055 }
2056
2057 RTVfsObjRelease(hVfsObj);
2058 hVfsObj = NIL_RTVFSOBJ;
2059
2060 RTStrFree(pszName);
2061
2062 } while (SUCCEEDED(hrc));
2063
2064 RTVfsFsStrmRelease(hVfsFssObject);
2065 hVfsFssObject = NIL_RTVFSFSSTREAM;
2066
2067 if (SUCCEEDED(hrc))
2068 {
2069 pTask->pProgress->SetNextOperation(BstrFmt(tr("Creating new VM '%s'"), strVMName.c_str()).raw(), 50);
2070 /* Create the import stack to comply OVF logic.
2071 * Before we filled some other data structures which are needed by OVF logic too.*/
2072 ImportStack stack(pTask->locInfo, m->pReader->m_mapDisks, pTask->pProgress, NIL_RTVFSFSSTREAM);
2073 i_importMachines(stack);
2074 }
2075
2076 }
2077 catch (HRESULT aRc)
2078 {
2079 hrc = aRc;
2080 LogRel(("%s: Cloud import (local phase) failed. The exception (%Rhrc)\n",
2081 __FUNCTION__, hrc));
2082 }
2083 catch (int aRc)
2084 {
2085 hrc = setErrorVrc(aRc);
2086 LogRel(("%s: Cloud import (local phase) failed. The exception (%Rrc/%Rhrc)\n",
2087 __FUNCTION__, aRc, hrc));
2088 }
2089 catch (...)
2090 {
2091 hrc = setErrorVrc(VERR_UNRESOLVED_ERROR);
2092 LogRel(("%s: Cloud import (local phase) failed. The exception (VERR_UNRESOLVED_ERROR/%Rhrc)\n",
2093 __FUNCTION__, hrc));
2094 }
2095
2096 LogRel(("%s: Cloud import (local phase) final result (%Rrc).\n", __FUNCTION__, hrc));
2097
2098 /* Try to free VFS stuff because some of them might not be released due to the exception */
2099 if (hVfsIosCurr != NIL_RTVFSIOSTREAM)
2100 RTVfsIoStrmRelease(hVfsIosCurr);
2101 if (hVfsObj != NIL_RTVFSOBJ)
2102 RTVfsObjRelease(hVfsObj);
2103 if (hVfsFssObject != NIL_RTVFSFSSTREAM)
2104 RTVfsFsStrmRelease(hVfsFssObject);
2105
2106 /* Small explanation here.
2107 * After adding extracted files into the actual VSD the returned list will contain not only the
2108 * record about the downloaded object but also the records about the extracted files from this object.
2109 * It's needed to go through this list to find the record about the downloaded object.
2110 * But it was the first record added into the list, so aVBoxValues[0] should be correct here.
2111 */
2112 GET_VSD_DESCRIPTION_BY_TYPE(VirtualSystemDescriptionType_HardDiskImage); //aVBoxValues is set in this #define
2113 if (!fKeepDownloadedObject)
2114 {
2115 if (aVBoxValues.size() != 0)
2116 {
2117 vsdData = aVBoxValues[0];
2118 //try to delete the downloaded object
2119 bool fExist = RTPathExists(vsdData.c_str());
2120 if (fExist)
2121 {
2122 vrc = RTFileDelete(vsdData.c_str());
2123 if (RT_FAILURE(vrc))
2124 LogRel(("%s: Cleanup action - the downloaded object %s hasn't been deleted\n", __FUNCTION__, vsdData.c_str()));
2125 else
2126 LogRel(("%s: Cleanup action - the downloaded object %s has been deleted\n", __FUNCTION__, vsdData.c_str()));
2127 }
2128 }
2129 }
2130
2131 if (FAILED(hrc))
2132 {
2133 /* What to do here?
2134 * For now:
2135 * - check the registration of created VM and delete one.
2136 * - check the list of imported images, detach them and next delete if they have still registered in the VBox.
2137 * - check some other leavings and delete them if they exist.
2138 */
2139
2140 /* It's not needed to call "pTask->pProgress->SetNextOperation(BstrFmt("The cleanup phase").raw(), 50)" here
2141 * because, WaitForOtherProgressCompletion() calls the SetNextOperation() iternally.
2142 * At least, it's strange that the operation description is set to the previous value. */
2143
2144 ComPtr<IMachine> pMachine;
2145 Utf8Str machineNameOrId = strVMName;
2146
2147 /* m->llGuidsMachinesCreated is filled in the i_importMachineGeneric()/i_importVBoxMachine()
2148 * after successful registration of new VM */
2149 if (!m->llGuidsMachinesCreated.empty())
2150 machineNameOrId = m->llGuidsMachinesCreated.front().toString();
2151
2152 hrc = mVirtualBox->FindMachine(Bstr(machineNameOrId).raw(), pMachine.asOutParam());
2153
2154 if (SUCCEEDED(hrc))
2155 {
2156 LogRel(("%s: Cleanup action - the VM with the name(or id) %s was found\n", __FUNCTION__, machineNameOrId.c_str()));
2157 SafeIfaceArray<IMedium> aMedia;
2158 hrc = pMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly, ComSafeArrayAsOutParam(aMedia));
2159 if (SUCCEEDED(hrc))
2160 {
2161 LogRel(("%s: Cleanup action - the VM %s has been unregistered\n", __FUNCTION__, machineNameOrId.c_str()));
2162 ComPtr<IProgress> pProgress1;
2163 hrc = pMachine->DeleteConfig(ComSafeArrayAsInParam(aMedia), pProgress1.asOutParam());
2164 pTask->pProgress->WaitForOtherProgressCompletion(pProgress1, 0 /* indefinite wait */);
2165
2166 LogRel(("%s: Cleanup action - the VM config file and the attached images have been deleted\n",
2167 __FUNCTION__));
2168 }
2169 }
2170 else
2171 {
2172 /* Re-check the items in the array with the images names (paths).
2173 * if the import fails before creation VM, then VM won't be found
2174 * -> VM can't be unregistered and the images can't be deleted.
2175 * The rest items in the array aVBoxValues are the images which might
2176 * have still been registered in the VBox.
2177 * So go through the array and detach-unregister-delete those images */
2178
2179 /* have to get write lock as the whole find/update sequence must be done
2180 * in one critical section, otherwise there are races which can lead to
2181 * multiple Medium objects with the same content */
2182
2183 AutoWriteLock treeLock(mVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
2184
2185 for (size_t i = 1; i < aVBoxValues.size(); ++i)
2186 {
2187 vsdData = aVBoxValues[i];
2188 ComObjPtr<Medium> poHardDisk;
2189 hrc = mVirtualBox->i_findHardDiskByLocation(vsdData, false, &poHardDisk);
2190 if (SUCCEEDED(hrc))
2191 {
2192 hrc = mVirtualBox->i_unregisterMedium((Medium*)(poHardDisk));
2193 if (SUCCEEDED(hrc))
2194 {
2195 ComPtr<IProgress> pProgress1;
2196 hrc = poHardDisk->DeleteStorage(pProgress1.asOutParam());
2197 pTask->pProgress->WaitForOtherProgressCompletion(pProgress1, 0 /* indefinite wait */);
2198 }
2199 if (SUCCEEDED(hrc))
2200 LogRel(("%s: Cleanup action - the image %s has been deleted\n", __FUNCTION__, vsdData.c_str()));
2201 }
2202 else if (hrc == VBOX_E_OBJECT_NOT_FOUND)
2203 {
2204 LogRel(("%s: Cleanup action - the image %s wasn't found. Nothing to delete.\n", __FUNCTION__, vsdData.c_str()));
2205 hrc = S_OK;
2206 }
2207
2208 }
2209 }
2210
2211 /* Deletion of all additional files which were created during unpacking the downloaded object */
2212 for (size_t i = 0; i < extraCreatedFiles.size(); ++i)
2213 {
2214 vrc = RTFileDelete(extraCreatedFiles.at(i).c_str());
2215 if (RT_FAILURE(vrc))
2216 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc);
2217 else
2218 LogRel(("%s: Cleanup action - file %s has been deleted\n", __FUNCTION__, extraCreatedFiles.at(i).c_str()));
2219 }
2220
2221 /* Deletion of the other files in the VM folder and the folder itself */
2222 {
2223 RTDIR hDir;
2224 vrc = RTDirOpen(&hDir, strMachineFolder.c_str());
2225 if (RT_SUCCESS(vrc))
2226 {
2227 for (;;)
2228 {
2229 RTDIRENTRYEX Entry;
2230 vrc = RTDirReadEx(hDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2231 if (RT_FAILURE(vrc))
2232 {
2233 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
2234 break;
2235 }
2236 if (RTFS_IS_FILE(Entry.Info.Attr.fMode))
2237 {
2238 vrc = RTFileDelete(Entry.szName);
2239 if (RT_FAILURE(vrc))
2240 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc);
2241 else
2242 LogRel(("%s: Cleanup action - file %s has been deleted\n", __FUNCTION__, Entry.szName));
2243 }
2244 }
2245 RTDirClose(hDir);
2246 }
2247
2248 vrc = RTDirRemove(strMachineFolder.c_str());
2249 if (RT_FAILURE(vrc))
2250 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc);
2251 }
2252
2253 if (FAILED(hrc))
2254 LogRel(("%s: Cleanup action - some leavings still may exist in the folder %s\n",
2255 __FUNCTION__, strMachineFolder.c_str()));
2256 }
2257 else
2258 {
2259 /* See explanation in the Appliance::i_importImpl() where Progress was setup */
2260 ULONG operationCount;
2261 ULONG currOperation;
2262 pTask->pProgress->COMGETTER(OperationCount)(&operationCount);
2263 pTask->pProgress->COMGETTER(Operation)(&currOperation);
2264 while (++currOperation < operationCount)
2265 {
2266 pTask->pProgress->SetNextOperation(BstrFmt("Skipping the cleanup phase. All right.").raw(), 1);
2267 LogRel(("%s: Skipping the cleanup step %d\n", __FUNCTION__, currOperation));
2268 }
2269 }
2270 }
2271
2272 LogFlowFunc(("hrc=%Rhrc\n", hrc));
2273 LogFlowFuncLeave();
2274 return hrc;
2275}
2276
2277/**
2278 * Actual worker code for reading an OVF from disk. This is called from Appliance::taskThreadImportOrExport()
2279 * and therefore runs on the OVF read worker thread. This opens the OVF with ovfreader.cpp.
2280 *
2281 * This runs in one context:
2282 *
2283 * 1) in a first worker thread; in that case, Appliance::Read() called Appliance::readImpl();
2284 *
2285 * @param pTask
2286 * @return
2287 */
2288HRESULT Appliance::i_readFS(TaskOVF *pTask)
2289{
2290 LogFlowFuncEnter();
2291 LogFlowFunc(("Appliance %p\n", this));
2292
2293 AutoCaller autoCaller(this);
2294 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
2295
2296 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
2297
2298 HRESULT hrc;
2299 if (pTask->locInfo.strPath.endsWith(".ovf", Utf8Str::CaseInsensitive))
2300 hrc = i_readFSOVF(pTask);
2301 else
2302 hrc = i_readFSOVA(pTask);
2303
2304 LogFlowFunc(("hrc=%Rhrc\n", hrc));
2305 LogFlowFuncLeave();
2306
2307 return hrc;
2308}
2309
2310HRESULT Appliance::i_readFSOVF(TaskOVF *pTask)
2311{
2312 LogFlowFunc(("'%s'\n", pTask->locInfo.strPath.c_str()));
2313
2314 /*
2315 * Allocate a buffer for filenames and prep it for suffix appending.
2316 */
2317 char *pszNameBuf = (char *)alloca(pTask->locInfo.strPath.length() + 16);
2318 AssertReturn(pszNameBuf, E_OUTOFMEMORY);
2319 memcpy(pszNameBuf, pTask->locInfo.strPath.c_str(), pTask->locInfo.strPath.length() + 1);
2320 RTPathStripSuffix(pszNameBuf);
2321 size_t const cchBaseName = strlen(pszNameBuf);
2322
2323 /*
2324 * Open the OVF file first since that is what this is all about.
2325 */
2326 RTVFSIOSTREAM hIosOvf;
2327 int vrc = RTVfsIoStrmOpenNormal(pTask->locInfo.strPath.c_str(),
2328 RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &hIosOvf);
2329 if (RT_FAILURE(vrc))
2330 return setErrorVrc(vrc, tr("Failed to open OVF file '%s' (%Rrc)"), pTask->locInfo.strPath.c_str(), vrc);
2331
2332 HRESULT hrc = i_readOVFFile(pTask, hIosOvf, RTPathFilename(pTask->locInfo.strPath.c_str())); /* consumes hIosOvf */
2333 if (FAILED(hrc))
2334 return hrc;
2335
2336 /*
2337 * Try open the manifest file (for signature purposes and to determine digest type(s)).
2338 */
2339 RTVFSIOSTREAM hIosMf;
2340 strcpy(&pszNameBuf[cchBaseName], ".mf");
2341 vrc = RTVfsIoStrmOpenNormal(pszNameBuf, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &hIosMf);
2342 if (RT_SUCCESS(vrc))
2343 {
2344 const char * const pszFilenamePart = RTPathFilename(pszNameBuf);
2345 hrc = i_readManifestFile(pTask, hIosMf /*consumed*/, pszFilenamePart);
2346 if (FAILED(hrc))
2347 return hrc;
2348
2349 /*
2350 * Check for the signature file.
2351 */
2352 RTVFSIOSTREAM hIosCert;
2353 strcpy(&pszNameBuf[cchBaseName], ".cert");
2354 vrc = RTVfsIoStrmOpenNormal(pszNameBuf, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &hIosCert);
2355 if (RT_SUCCESS(vrc))
2356 {
2357 hrc = i_readSignatureFile(pTask, hIosCert /*consumed*/, pszFilenamePart);
2358 if (FAILED(hrc))
2359 return hrc;
2360 }
2361 else if (vrc != VERR_FILE_NOT_FOUND && vrc != VERR_PATH_NOT_FOUND)
2362 return setErrorVrc(vrc, tr("Failed to open the signature file '%s' (%Rrc)"), pszNameBuf, vrc);
2363
2364 }
2365 else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
2366 {
2367 m->fDeterminedDigestTypes = true;
2368 m->fDigestTypes = 0;
2369 }
2370 else
2371 return setErrorVrc(vrc, tr("Failed to open the manifest file '%s' (%Rrc)"), pszNameBuf, vrc);
2372
2373 /*
2374 * Do tail processing (check the signature).
2375 */
2376 hrc = i_readTailProcessing(pTask);
2377
2378 LogFlowFunc(("returns %Rhrc\n", hrc));
2379 return hrc;
2380}
2381
2382HRESULT Appliance::i_readFSOVA(TaskOVF *pTask)
2383{
2384 LogFlowFunc(("'%s'\n", pTask->locInfo.strPath.c_str()));
2385
2386 /*
2387 * Open the tar file as file stream.
2388 */
2389 RTVFSIOSTREAM hVfsIosOva;
2390 int vrc = RTVfsIoStrmOpenNormal(pTask->locInfo.strPath.c_str(),
2391 RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN, &hVfsIosOva);
2392 if (RT_FAILURE(vrc))
2393 return setErrorVrc(vrc, tr("Error opening the OVA file '%s' (%Rrc)"), pTask->locInfo.strPath.c_str(), vrc);
2394
2395 RTVFSFSSTREAM hVfsFssOva;
2396 vrc = RTZipTarFsStreamFromIoStream(hVfsIosOva, 0 /*fFlags*/, &hVfsFssOva);
2397 RTVfsIoStrmRelease(hVfsIosOva);
2398 if (RT_FAILURE(vrc))
2399 return setErrorVrc(vrc, tr("Error reading the OVA file '%s' (%Rrc)"), pTask->locInfo.strPath.c_str(), vrc);
2400
2401 /*
2402 * Since jumping thru an OVA file with seekable disk backing is rather
2403 * efficient, we can process .ovf, .mf and .cert files here without any
2404 * strict ordering restrictions.
2405 *
2406 * (Technically, the .ovf-file comes first, while the manifest and its
2407 * optional signature file either follows immediately or at the very end of
2408 * the OVA. The manifest is optional.)
2409 */
2410 char *pszOvfNameBase = NULL;
2411 size_t cchOvfNameBase = 0; NOREF(cchOvfNameBase);
2412 unsigned cLeftToFind = 3;
2413 HRESULT hrc = S_OK;
2414 do
2415 {
2416 char *pszName = NULL;
2417 RTVFSOBJTYPE enmType;
2418 RTVFSOBJ hVfsObj;
2419 vrc = RTVfsFsStrmNext(hVfsFssOva, &pszName, &enmType, &hVfsObj);
2420 if (RT_FAILURE(vrc))
2421 {
2422 if (vrc != VERR_EOF)
2423 hrc = setErrorVrc(vrc, tr("Error reading OVA '%s' (%Rrc)"), pTask->locInfo.strPath.c_str(), vrc);
2424 break;
2425 }
2426
2427 /* We only care about entries that are files. Get the I/O stream handle for them. */
2428 if ( enmType == RTVFSOBJTYPE_IO_STREAM
2429 || enmType == RTVFSOBJTYPE_FILE)
2430 {
2431 /* Find the suffix and check if this is a possibly interesting file. */
2432 char *pszSuffix = strrchr(pszName, '.');
2433 if ( pszSuffix
2434 && ( RTStrICmp(pszSuffix + 1, "ovf") == 0
2435 || RTStrICmp(pszSuffix + 1, "mf") == 0
2436 || RTStrICmp(pszSuffix + 1, "cert") == 0) )
2437 {
2438 /* Match the OVF base name. */
2439 *pszSuffix = '\0';
2440 if ( pszOvfNameBase == NULL
2441 || RTStrICmp(pszName, pszOvfNameBase) == 0)
2442 {
2443 *pszSuffix = '.';
2444
2445 /* Since we're pretty sure we'll be processing this file, get the I/O stream. */
2446 RTVFSIOSTREAM hVfsIos = RTVfsObjToIoStream(hVfsObj);
2447 Assert(hVfsIos != NIL_RTVFSIOSTREAM);
2448
2449 /* Check for the OVF (should come first). */
2450 if (RTStrICmp(pszSuffix + 1, "ovf") == 0)
2451 {
2452 if (pszOvfNameBase == NULL)
2453 {
2454 hrc = i_readOVFFile(pTask, hVfsIos, pszName);
2455 hVfsIos = NIL_RTVFSIOSTREAM;
2456
2457 /* Set the base name. */
2458 *pszSuffix = '\0';
2459 pszOvfNameBase = pszName;
2460 cchOvfNameBase = strlen(pszName);
2461 pszName = NULL;
2462 cLeftToFind--;
2463 }
2464 else
2465 LogRel(("i_readFSOVA: '%s' contains more than one OVF file ('%s'), picking the first one\n",
2466 pTask->locInfo.strPath.c_str(), pszName));
2467 }
2468 /* Check for manifest. */
2469 else if (RTStrICmp(pszSuffix + 1, "mf") == 0)
2470 {
2471 if (m->hMemFileTheirManifest == NIL_RTVFSFILE)
2472 {
2473 hrc = i_readManifestFile(pTask, hVfsIos, pszName);
2474 hVfsIos = NIL_RTVFSIOSTREAM; /*consumed*/
2475 cLeftToFind--;
2476 }
2477 else
2478 LogRel(("i_readFSOVA: '%s' contains more than one manifest file ('%s'), picking the first one\n",
2479 pTask->locInfo.strPath.c_str(), pszName));
2480 }
2481 /* Check for signature. */
2482 else if (RTStrICmp(pszSuffix + 1, "cert") == 0)
2483 {
2484 if (!m->fSignerCertLoaded)
2485 {
2486 hrc = i_readSignatureFile(pTask, hVfsIos, pszName);
2487 hVfsIos = NIL_RTVFSIOSTREAM; /*consumed*/
2488 cLeftToFind--;
2489 }
2490 else
2491 LogRel(("i_readFSOVA: '%s' contains more than one signature file ('%s'), picking the first one\n",
2492 pTask->locInfo.strPath.c_str(), pszName));
2493 }
2494 else
2495 AssertFailed();
2496 if (hVfsIos != NIL_RTVFSIOSTREAM)
2497 RTVfsIoStrmRelease(hVfsIos);
2498 }
2499 }
2500 }
2501 RTVfsObjRelease(hVfsObj);
2502 RTStrFree(pszName);
2503 } while (cLeftToFind > 0 && SUCCEEDED(hrc));
2504
2505 RTVfsFsStrmRelease(hVfsFssOva);
2506 RTStrFree(pszOvfNameBase);
2507
2508 /*
2509 * Check that we found and OVF file.
2510 */
2511 if (SUCCEEDED(hrc) && !pszOvfNameBase)
2512 hrc = setError(VBOX_E_FILE_ERROR, tr("OVA '%s' does not contain an .ovf-file"), pTask->locInfo.strPath.c_str());
2513 if (SUCCEEDED(hrc))
2514 {
2515 /*
2516 * Do tail processing (check the signature).
2517 */
2518 hrc = i_readTailProcessing(pTask);
2519 }
2520 LogFlowFunc(("returns %Rhrc\n", hrc));
2521 return hrc;
2522}
2523
2524/**
2525 * Reads & parses the OVF file.
2526 *
2527 * @param pTask The read task.
2528 * @param hVfsIosOvf The I/O stream for the OVF. The reference is
2529 * always consumed.
2530 * @param pszManifestEntry The manifest entry name.
2531 * @returns COM status code, error info set.
2532 * @throws Nothing
2533 */
2534HRESULT Appliance::i_readOVFFile(TaskOVF *pTask, RTVFSIOSTREAM hVfsIosOvf, const char *pszManifestEntry)
2535{
2536 LogFlowFunc(("%s[%s]\n", pTask->locInfo.strPath.c_str(), pszManifestEntry));
2537
2538 /*
2539 * Set the OVF manifest entry name (needed for tweaking the manifest
2540 * validation during import).
2541 */
2542 try { m->strOvfManifestEntry = pszManifestEntry; }
2543 catch (...) { return E_OUTOFMEMORY; }
2544
2545 /*
2546 * Set up digest calculation.
2547 */
2548 hVfsIosOvf = i_manifestSetupDigestCalculationForGivenIoStream(hVfsIosOvf, pszManifestEntry);
2549 if (hVfsIosOvf == NIL_RTVFSIOSTREAM)
2550 return VBOX_E_FILE_ERROR;
2551
2552 /*
2553 * Read the OVF into a memory buffer and parse it.
2554 */
2555 void *pvBufferedOvf;
2556 size_t cbBufferedOvf;
2557 int vrc = RTVfsIoStrmReadAll(hVfsIosOvf, &pvBufferedOvf, &cbBufferedOvf);
2558 uint32_t cRefs = RTVfsIoStrmRelease(hVfsIosOvf); /* consumes stream handle. */
2559 NOREF(cRefs);
2560 Assert(cRefs == 0);
2561 if (RT_FAILURE(vrc))
2562 return setErrorVrc(vrc, tr("Could not read the OVF file for '%s' (%Rrc)"), pTask->locInfo.strPath.c_str(), vrc);
2563
2564 HRESULT hrc;
2565 try
2566 {
2567 m->pReader = new ovf::OVFReader(pvBufferedOvf, cbBufferedOvf, pTask->locInfo.strPath);
2568 hrc = S_OK;
2569 }
2570 catch (RTCError &rXcpt) // includes all XML exceptions
2571 {
2572 hrc = setError(VBOX_E_FILE_ERROR, rXcpt.what());
2573 }
2574 catch (HRESULT aRC)
2575 {
2576 hrc = aRC;
2577 }
2578 catch (...)
2579 {
2580 hrc = E_FAIL;
2581 }
2582 LogFlowFunc(("OVFReader(%s) -> hrc=%Rhrc\n", pTask->locInfo.strPath.c_str(), hrc));
2583
2584 RTVfsIoStrmReadAllFree(pvBufferedOvf, cbBufferedOvf);
2585 if (SUCCEEDED(hrc))
2586 {
2587 /*
2588 * If we see an OVF v2.0 envelope, select only the SHA-256 digest.
2589 */
2590 if ( !m->fDeterminedDigestTypes
2591 && m->pReader->m_envelopeData.getOVFVersion() == ovf::OVFVersion_2_0)
2592 m->fDigestTypes &= ~RTMANIFEST_ATTR_SHA256;
2593 }
2594
2595 return hrc;
2596}
2597
2598/**
2599 * Reads & parses the manifest file.
2600 *
2601 * @param pTask The read task.
2602 * @param hVfsIosMf The I/O stream for the manifest file. The
2603 * reference is always consumed.
2604 * @param pszSubFileNm The manifest filename (no path) for error
2605 * messages and logging.
2606 * @returns COM status code, error info set.
2607 * @throws Nothing
2608 */
2609HRESULT Appliance::i_readManifestFile(TaskOVF *pTask, RTVFSIOSTREAM hVfsIosMf, const char *pszSubFileNm)
2610{
2611 LogFlowFunc(("%s[%s]\n", pTask->locInfo.strPath.c_str(), pszSubFileNm));
2612
2613 /*
2614 * Copy the manifest into a memory backed file so we can later do signature
2615 * validation independent of the algorithms used by the signature.
2616 */
2617 int vrc = RTVfsMemorizeIoStreamAsFile(hVfsIosMf, RTFILE_O_READ, &m->hMemFileTheirManifest);
2618 RTVfsIoStrmRelease(hVfsIosMf); /* consumes stream handle. */
2619 if (RT_FAILURE(vrc))
2620 return setErrorVrc(vrc, tr("Error reading the manifest file '%s' for '%s' (%Rrc)"),
2621 pszSubFileNm, pTask->locInfo.strPath.c_str(), vrc);
2622
2623 /*
2624 * Parse the manifest.
2625 */
2626 Assert(m->hTheirManifest == NIL_RTMANIFEST);
2627 vrc = RTManifestCreate(0 /*fFlags*/, &m->hTheirManifest);
2628 AssertStmt(RT_SUCCESS(vrc), Global::vboxStatusCodeToCOM(vrc));
2629
2630 char szErr[256];
2631 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(m->hMemFileTheirManifest);
2632 vrc = RTManifestReadStandardEx(m->hTheirManifest, hVfsIos, szErr, sizeof(szErr));
2633 RTVfsIoStrmRelease(hVfsIos);
2634 if (RT_FAILURE(vrc))
2635 return setErrorVrc(vrc, tr("Failed to parse manifest file '%s' for '%s' (%Rrc): %s"),
2636 pszSubFileNm, pTask->locInfo.strPath.c_str(), vrc, szErr);
2637
2638 /*
2639 * Check which digest files are used.
2640 * Note! the file could be empty, in which case fDigestTypes is set to 0.
2641 */
2642 vrc = RTManifestQueryAllAttrTypes(m->hTheirManifest, true /*fEntriesOnly*/, &m->fDigestTypes);
2643 AssertRCReturn(vrc, Global::vboxStatusCodeToCOM(vrc));
2644 m->fDeterminedDigestTypes = true;
2645
2646 return S_OK;
2647}
2648
2649/**
2650 * Reads the signature & certificate file.
2651 *
2652 * @param pTask The read task.
2653 * @param hVfsIosCert The I/O stream for the signature file. The
2654 * reference is always consumed.
2655 * @param pszSubFileNm The signature filename (no path) for error
2656 * messages and logging. Used to construct
2657 * .mf-file name.
2658 * @returns COM status code, error info set.
2659 * @throws Nothing
2660 */
2661HRESULT Appliance::i_readSignatureFile(TaskOVF *pTask, RTVFSIOSTREAM hVfsIosCert, const char *pszSubFileNm)
2662{
2663 LogFlowFunc(("%s[%s]\n", pTask->locInfo.strPath.c_str(), pszSubFileNm));
2664
2665 /*
2666 * Construct the manifest filename from pszSubFileNm.
2667 */
2668 Utf8Str strManifestName;
2669 try
2670 {
2671 const char *pszSuffix = strrchr(pszSubFileNm, '.');
2672 AssertReturn(pszSuffix, E_FAIL);
2673 strManifestName = Utf8Str(pszSubFileNm, (size_t)(pszSuffix - pszSubFileNm));
2674 strManifestName.append(".mf");
2675 }
2676 catch (...)
2677 {
2678 return E_OUTOFMEMORY;
2679 }
2680
2681 /*
2682 * Copy the manifest into a memory buffer. We'll do the signature processing
2683 * later to not force any specific order in the OVAs or any other archive we
2684 * may be accessing later.
2685 */
2686 void *pvSignature;
2687 size_t cbSignature;
2688 int vrc = RTVfsIoStrmReadAll(hVfsIosCert, &pvSignature, &cbSignature);
2689 RTVfsIoStrmRelease(hVfsIosCert); /* consumes stream handle. */
2690 if (RT_FAILURE(vrc))
2691 return setErrorVrc(vrc, tr("Error reading the signature file '%s' for '%s' (%Rrc)"),
2692 pszSubFileNm, pTask->locInfo.strPath.c_str(), vrc);
2693
2694 /*
2695 * Parse the signing certificate. Unlike the manifest parser we use below,
2696 * this API ignores parts of the file that aren't relevant.
2697 */
2698 RTERRINFOSTATIC StaticErrInfo;
2699 vrc = RTCrX509Certificate_ReadFromBuffer(&m->SignerCert, pvSignature, cbSignature,
2700 RTCRX509CERT_READ_F_PEM_ONLY,
2701 &g_RTAsn1DefaultAllocator, RTErrInfoInitStatic(&StaticErrInfo), pszSubFileNm);
2702 HRESULT hrc;
2703 if (RT_SUCCESS(vrc))
2704 {
2705 m->fSignerCertLoaded = true;
2706 m->fCertificateIsSelfSigned = RTCrX509Certificate_IsSelfSigned(&m->SignerCert);
2707
2708 /*
2709 * Find the start of the certificate part of the file, so we can avoid
2710 * upsetting the manifest parser with it.
2711 */
2712 char *pszSplit = (char *)RTCrPemFindFirstSectionInContent(pvSignature, cbSignature,
2713 g_aRTCrX509CertificateMarkers, g_cRTCrX509CertificateMarkers);
2714 if (pszSplit)
2715 while ( pszSplit != (char *)pvSignature
2716 && pszSplit[-1] != '\n'
2717 && pszSplit[-1] != '\r')
2718 pszSplit--;
2719 else
2720 {
2721 AssertLogRelMsgFailed(("Failed to find BEGIN CERTIFICATE markers in '%s'::'%s' - impossible unless it's a DER encoded certificate!",
2722 pTask->locInfo.strPath.c_str(), pszSubFileNm));
2723 pszSplit = (char *)pvSignature + cbSignature;
2724 }
2725 char const chSaved = *pszSplit;
2726 *pszSplit = '\0';
2727
2728 /*
2729 * Now, read the manifest part. We use the IPRT manifest reader here
2730 * to avoid duplicating code and be somewhat flexible wrt the digest
2731 * type choosen by the signer.
2732 */
2733 RTMANIFEST hSignedDigestManifest;
2734 vrc = RTManifestCreate(0 /*fFlags*/, &hSignedDigestManifest);
2735 if (RT_SUCCESS(vrc))
2736 {
2737 RTVFSIOSTREAM hVfsIosTmp;
2738 vrc = RTVfsIoStrmFromBuffer(RTFILE_O_READ, pvSignature, (size_t)(pszSplit - (char *)pvSignature), &hVfsIosTmp);
2739 if (RT_SUCCESS(vrc))
2740 {
2741 vrc = RTManifestReadStandardEx(hSignedDigestManifest, hVfsIosTmp, StaticErrInfo.szMsg, sizeof(StaticErrInfo.szMsg));
2742 RTVfsIoStrmRelease(hVfsIosTmp);
2743 if (RT_SUCCESS(vrc))
2744 {
2745 /*
2746 * Get signed digest, we prefer SHA-2, so explicitly query those first.
2747 */
2748 uint32_t fDigestType;
2749 char szSignedDigest[_8K + 1];
2750 vrc = RTManifestEntryQueryAttr(hSignedDigestManifest, strManifestName.c_str(), NULL,
2751 RTMANIFEST_ATTR_SHA512 | RTMANIFEST_ATTR_SHA256,
2752 szSignedDigest, sizeof(szSignedDigest), &fDigestType);
2753 if (vrc == VERR_MANIFEST_ATTR_TYPE_NOT_FOUND)
2754 vrc = RTManifestEntryQueryAttr(hSignedDigestManifest, strManifestName.c_str(), NULL,
2755 RTMANIFEST_ATTR_ANY, szSignedDigest, sizeof(szSignedDigest), &fDigestType);
2756 if (RT_SUCCESS(vrc))
2757 {
2758 const char *pszSignedDigest = RTStrStrip(szSignedDigest);
2759 size_t cbSignedDigest = strlen(pszSignedDigest) / 2;
2760 uint8_t abSignedDigest[sizeof(szSignedDigest) / 2];
2761 vrc = RTStrConvertHexBytes(szSignedDigest, abSignedDigest, cbSignedDigest, 0 /*fFlags*/);
2762 if (RT_SUCCESS(vrc))
2763 {
2764 /*
2765 * Convert it to RTDIGESTTYPE_XXX and save the binary value for later use.
2766 */
2767 switch (fDigestType)
2768 {
2769 case RTMANIFEST_ATTR_SHA1: m->enmSignedDigestType = RTDIGESTTYPE_SHA1; break;
2770 case RTMANIFEST_ATTR_SHA256: m->enmSignedDigestType = RTDIGESTTYPE_SHA256; break;
2771 case RTMANIFEST_ATTR_SHA512: m->enmSignedDigestType = RTDIGESTTYPE_SHA512; break;
2772 case RTMANIFEST_ATTR_MD5: m->enmSignedDigestType = RTDIGESTTYPE_MD5; break;
2773 default: AssertFailed(); m->enmSignedDigestType = RTDIGESTTYPE_INVALID; break;
2774 }
2775 if (m->enmSignedDigestType != RTDIGESTTYPE_INVALID)
2776 {
2777 m->pbSignedDigest = (uint8_t *)RTMemDup(abSignedDigest, cbSignedDigest);
2778 m->cbSignedDigest = cbSignedDigest;
2779 hrc = S_OK;
2780 }
2781 else
2782 hrc = setError(E_FAIL, tr("Unsupported signed digest type (%#x)"), fDigestType);
2783 }
2784 else
2785 hrc = setErrorVrc(vrc, tr("Error reading signed manifest digest: %Rrc"), vrc);
2786 }
2787 else if (vrc == VERR_NOT_FOUND)
2788 hrc = setErrorVrc(vrc, tr("Could not locate signed digest for '%s' in the cert-file for '%s'"),
2789 strManifestName.c_str(), pTask->locInfo.strPath.c_str());
2790 else
2791 hrc = setErrorVrc(vrc, tr("RTManifestEntryQueryAttr failed unexpectedly: %Rrc"), vrc);
2792 }
2793 else
2794 hrc = setErrorVrc(vrc, tr("Error parsing the .cert-file for '%s': %s"),
2795 pTask->locInfo.strPath.c_str(), StaticErrInfo.szMsg);
2796 }
2797 else
2798 hrc = E_OUTOFMEMORY;
2799 RTManifestRelease(hSignedDigestManifest);
2800 }
2801 else
2802 hrc = E_OUTOFMEMORY;
2803
2804 /*
2805 * Look for the additional for PKCS#7/CMS signature we produce when we sign stuff.
2806 */
2807 if (SUCCEEDED(hrc))
2808 {
2809 *pszSplit = chSaved;
2810 vrc = RTCrPkcs7_ReadFromBuffer(&m->ContentInfo, pvSignature, cbSignature, RTCRPKCS7_READ_F_PEM_ONLY,
2811 &g_RTAsn1DefaultAllocator, NULL /*pfCmsLabeled*/,
2812 RTErrInfoInitStatic(&StaticErrInfo), pszSubFileNm);
2813 if (RT_SUCCESS(vrc))
2814 m->fContentInfoLoaded = true;
2815 else if (vrc != VERR_NOT_FOUND)
2816 hrc = setErrorVrc(vrc, tr("Error reading the PKCS#7/CMS signature from '%s' for '%s' (%Rrc): %s"),
2817 pszSubFileNm, pTask->locInfo.strPath.c_str(), vrc, StaticErrInfo.Core.pszMsg);
2818 }
2819 }
2820 else if (vrc == VERR_NOT_FOUND || vrc == VERR_EOF)
2821 hrc = setErrorBoth(E_FAIL, vrc, tr("Malformed .cert-file for '%s': Signer's certificate not found (%Rrc)"),
2822 pTask->locInfo.strPath.c_str(), vrc);
2823 else
2824 hrc = setErrorVrc(vrc, tr("Error reading the signer's certificate from '%s' for '%s' (%Rrc): %s"),
2825 pszSubFileNm, pTask->locInfo.strPath.c_str(), vrc, StaticErrInfo.Core.pszMsg);
2826
2827 RTVfsIoStrmReadAllFree(pvSignature, cbSignature);
2828 LogFlowFunc(("returns %Rhrc (%Rrc)\n", hrc, vrc));
2829 return hrc;
2830}
2831
2832
2833/**
2834 * Does tail processing after the files have been read in.
2835 *
2836 * @param pTask The read task.
2837 * @returns COM status.
2838 * @throws Nothing!
2839 */
2840HRESULT Appliance::i_readTailProcessing(TaskOVF *pTask)
2841{
2842 /*
2843 * Parse and validate the signature file.
2844 *
2845 * The signature file nominally has two parts, manifest part and a PEM
2846 * encoded certificate. The former contains an entry for the manifest file
2847 * with a digest that is encrypted with the certificate in the latter part.
2848 *
2849 * When an appliance is signed by VirtualBox, a PKCS#7/CMS signedData part
2850 * is added by default, supplying more info than the bits mandated by the
2851 * OVF specs. We will validate both the signedData and the standard OVF
2852 * signature. Another requirement is that the first signedData signer
2853 * uses the same certificate as the regular OVF signature, allowing us to
2854 * only do path building for the signedData with the additional info it
2855 * ships with.
2856 */
2857 if (m->pbSignedDigest)
2858 {
2859 /* Since we're validating the digest of the manifest, there have to be
2860 a manifest. We cannot allow a the manifest to be missing. */
2861 if (m->hMemFileTheirManifest == NIL_RTVFSFILE)
2862 return setError(VBOX_E_FILE_ERROR, tr("Found .cert-file but no .mf-file for '%s'"), pTask->locInfo.strPath.c_str());
2863
2864 /*
2865 * Validate the signed digest.
2866 *
2867 * It's possible we should allow the user to ignore signature
2868 * mismatches, but for now it is a solid show stopper.
2869 */
2870 HRESULT hrc;
2871 RTERRINFOSTATIC StaticErrInfo;
2872
2873 /* Calc the digest of the manifest using the algorithm found above. */
2874 RTCRDIGEST hDigest;
2875 int vrc = RTCrDigestCreateByType(&hDigest, m->enmSignedDigestType);
2876 if (RT_SUCCESS(vrc))
2877 {
2878 vrc = RTCrDigestUpdateFromVfsFile(hDigest, m->hMemFileTheirManifest, true /*fRewindFile*/);
2879 if (RT_SUCCESS(vrc))
2880 {
2881 /* Compare the signed digest with the one we just calculated. (This
2882 API will do the verification twice, once using IPRT's own crypto
2883 and once using OpenSSL. Both must OK it for success.) */
2884 vrc = RTCrPkixPubKeyVerifySignedDigestByCertPubKeyInfo(&m->SignerCert.TbsCertificate.SubjectPublicKeyInfo,
2885 m->pbSignedDigest, m->cbSignedDigest, hDigest,
2886 RTErrInfoInitStatic(&StaticErrInfo));
2887 if (RT_SUCCESS(vrc))
2888 {
2889 m->fSignatureValid = true;
2890 hrc = S_OK;
2891 }
2892 else if (vrc == VERR_CR_PKIX_SIGNATURE_MISMATCH)
2893 hrc = setErrorVrc(vrc, tr("The manifest signature does not match"));
2894 else
2895 hrc = setErrorVrc(vrc,
2896 tr("Error validating the manifest signature (%Rrc, %s)"), vrc, StaticErrInfo.Core.pszMsg);
2897 }
2898 else
2899 hrc = setErrorVrc(vrc, tr("RTCrDigestUpdateFromVfsFile failed: %Rrc"), vrc);
2900 RTCrDigestRelease(hDigest);
2901 }
2902 else
2903 hrc = setErrorVrc(vrc, tr("RTCrDigestCreateByType failed: %Rrc"), vrc);
2904
2905 /*
2906 * If we have a PKCS#7/CMS signature, validate it and check that the
2907 * certificate matches the first signerInfo entry.
2908 */
2909 HRESULT hrc2 = i_readTailProcessingSignedData(&StaticErrInfo);
2910 if (FAILED(hrc2) && SUCCEEDED(hrc))
2911 hrc = hrc2;
2912
2913 /*
2914 * Validate the certificate.
2915 *
2916 * We don't fail here if we cannot validate the certificate, we postpone
2917 * that till the import stage, so that we can allow the user to ignore it.
2918 *
2919 * The certificate validity time is deliberately left as warnings as the
2920 * OVF specification does not provision for any timestamping of the
2921 * signature. This is course a security concern, but the whole signing
2922 * of OVFs is currently weirdly trusting (self signed * certs), so this
2923 * is the least of our current problems.
2924 *
2925 * While we try build and verify certificate paths properly, the
2926 * "neighbours" quietly ignores this and seems only to check the signature
2927 * and not whether the certificate is trusted. Also, we don't currently
2928 * complain about self-signed certificates either (ditto "neighbours").
2929 * The OVF creator is also a bit restricted wrt to helping us build the
2930 * path as he cannot supply intermediate certificates. Anyway, we issue
2931 * warnings (goes to /dev/null, am I right?) for self-signed certificates
2932 * and certificates we cannot build and verify a root path for.
2933 *
2934 * (The OVF sillibuggers should've used PKCS#7, CMS or something else
2935 * that's already been standardized instead of combining manifests with
2936 * certificate PEM files in some very restrictive manner! I wonder if
2937 * we could add a PKCS#7 section to the .cert file in addition to the CERT
2938 * and manifest stuff dictated by the standard. Would depend on how others
2939 * deal with it.)
2940 */
2941 Assert(!m->fCertificateValid);
2942 Assert(m->fCertificateMissingPath);
2943 Assert(!m->fCertificateValidTime);
2944 Assert(m->strCertError.isEmpty());
2945 Assert(m->fCertificateIsSelfSigned == RTCrX509Certificate_IsSelfSigned(&m->SignerCert));
2946
2947 /* We'll always needs the trusted cert store. */
2948 hrc2 = S_OK;
2949 RTCRSTORE hTrustedCerts;
2950 vrc = RTCrStoreCreateSnapshotOfUserAndSystemTrustedCAsAndCerts(&hTrustedCerts, RTErrInfoInitStatic(&StaticErrInfo));
2951 if (RT_SUCCESS(vrc))
2952 {
2953 /* If we don't have a PKCS7/CMS signature or if it uses a different
2954 certificate, we try our best to validate the OVF certificate. */
2955 if (!m->fContentInfoOkay || !m->fContentInfoSameCert)
2956 {
2957 if (m->fCertificateIsSelfSigned)
2958 hrc2 = i_readTailProcessingVerifySelfSignedOvfCert(pTask, hTrustedCerts, &StaticErrInfo);
2959 else
2960 hrc2 = i_readTailProcessingVerifyIssuedOvfCert(pTask, hTrustedCerts, &StaticErrInfo);
2961 }
2962
2963 /* If there is a PKCS7/CMS signature, we always verify its certificates. */
2964 if (m->fContentInfoOkay)
2965 {
2966 void *pvData = NULL;
2967 size_t cbData = 0;
2968 HRESULT hrc3 = i_readTailProcessingGetManifestData(&pvData, &cbData);
2969 if (SUCCEEDED(hrc3))
2970 {
2971 hrc3 = i_readTailProcessingVerifyContentInfoCerts(pvData, cbData, hTrustedCerts, &StaticErrInfo);
2972 RTMemTmpFree(pvData);
2973 }
2974 if (FAILED(hrc3) && SUCCEEDED(hrc2))
2975 hrc2 = hrc3;
2976 }
2977 RTCrStoreRelease(hTrustedCerts);
2978 }
2979 else
2980 hrc2 = setErrorBoth(E_FAIL, vrc,
2981 tr("Failed to query trusted CAs and Certificates from the system and for the current user (%Rrc%RTeim)"),
2982 vrc, &StaticErrInfo.Core);
2983
2984 /* Merge statuses from signature and certificate validation, prefering the signature one. */
2985 if (SUCCEEDED(hrc) && FAILED(hrc2))
2986 hrc = hrc2;
2987 if (FAILED(hrc))
2988 return hrc;
2989 }
2990
2991 /** @todo provide details about the signatory, signature, etc. */
2992 if (m->fSignerCertLoaded)
2993 {
2994 /** @todo PKCS7/CMS certs too */
2995 m->ptrCertificateInfo.createObject();
2996 m->ptrCertificateInfo->initCertificate(&m->SignerCert,
2997 m->fCertificateValid && !m->fCertificateMissingPath,
2998 !m->fCertificateValidTime);
2999 }
3000
3001 /*
3002 * If there is a manifest, check that the OVF digest matches up (if present).
3003 */
3004
3005 NOREF(pTask);
3006 return S_OK;
3007}
3008
3009/**
3010 * Reads hMemFileTheirManifest into a memory buffer so it can be passed to
3011 * RTCrPkcs7VerifySignedDataWithExternalData.
3012 *
3013 * Use RTMemTmpFree to free the memory.
3014 */
3015HRESULT Appliance::i_readTailProcessingGetManifestData(void **ppvData, size_t *pcbData)
3016{
3017 uint64_t cbData;
3018 int vrc = RTVfsFileQuerySize(m->hMemFileTheirManifest, &cbData);
3019 AssertRCReturn(vrc, setErrorVrc(vrc, "RTVfsFileQuerySize"));
3020
3021 void *pvData = RTMemTmpAllocZ((size_t)cbData);
3022 AssertPtrReturn(pvData, E_OUTOFMEMORY);
3023
3024 vrc = RTVfsFileReadAt(m->hMemFileTheirManifest, 0, pvData, (size_t)cbData, NULL);
3025 AssertRCReturnStmt(vrc, RTMemTmpFree(pvData), setErrorVrc(vrc, "RTVfsFileReadAt"));
3026
3027 *pcbData = (size_t)cbData;
3028 *ppvData = pvData;
3029 return S_OK;
3030}
3031
3032/**
3033 * Worker for i_readTailProcessing that validates the signedData.
3034 *
3035 * If we have a PKCS#7/CMS signature:
3036 * - validate it
3037 * - check that the OVF certificate matches the first signerInfo entry
3038 * - verify the signature, but leave the certificate path validation for
3039 * later.
3040 *
3041 * @param pErrInfo Static error info buffer (not for returning, just for
3042 * avoiding wasting stack).
3043 * @returns COM status.
3044 * @throws Nothing!
3045 */
3046HRESULT Appliance::i_readTailProcessingSignedData(PRTERRINFOSTATIC pErrInfo)
3047{
3048 m->fContentInfoOkay = false;
3049 m->fContentInfoSameCert = false;
3050 m->fContentInfoValidSignature = false;
3051
3052 if (!m->fContentInfoLoaded)
3053 return S_OK;
3054
3055 /*
3056 * Validate it.
3057 */
3058 HRESULT hrc = S_OK;
3059 PCRTCRPKCS7SIGNEDDATA pSignedData = m->ContentInfo.u.pSignedData;
3060 if (!RTCrPkcs7ContentInfo_IsSignedData(&m->ContentInfo))
3061 i_addWarning(tr("Invalid PKCS#7/CMS type: %s, expected %s (signedData)"),
3062 m->ContentInfo.ContentType.szObjId, RTCRPKCS7SIGNEDDATA_OID);
3063 else if (RTAsn1ObjId_CompareWithString(&pSignedData->ContentInfo.ContentType, RTCR_PKCS7_DATA_OID) != 0)
3064 i_addWarning(tr("Invalid PKCS#7/CMS inner type: %s, expected %s (data)"),
3065 pSignedData->ContentInfo.ContentType.szObjId, RTCR_PKCS7_DATA_OID);
3066 else if (RTAsn1OctetString_IsPresent(&pSignedData->ContentInfo.Content))
3067 i_addWarning(tr("Invalid PKCS#7/CMS data: embedded (%u bytes), expected external","",
3068 pSignedData->ContentInfo.Content.Asn1Core.cb),
3069 pSignedData->ContentInfo.Content.Asn1Core.cb);
3070 else if (pSignedData->SignerInfos.cItems == 0)
3071 i_addWarning(tr("Invalid PKCS#7/CMS: No signers"));
3072 else
3073 {
3074 m->fContentInfoOkay = true;
3075
3076 /*
3077 * Same certificate as the OVF signature?
3078 */
3079 PCRTCRPKCS7SIGNERINFO pSignerInfo = pSignedData->SignerInfos.papItems[0];
3080 if ( RTCrX509Name_Compare(&pSignerInfo->IssuerAndSerialNumber.Name, &m->SignerCert.TbsCertificate.Issuer) == 0
3081 && RTAsn1Integer_Compare(&pSignerInfo->IssuerAndSerialNumber.SerialNumber,
3082 &m->SignerCert.TbsCertificate.SerialNumber) == 0)
3083 m->fContentInfoSameCert = true;
3084 else
3085 i_addWarning(tr("Invalid PKCS#7/CMS: Using a different certificate"));
3086
3087 /*
3088 * Then perform a validation of the signatures, but first without
3089 * validating the certificate trust paths yet.
3090 */
3091 RTCRSTORE hTrustedCerts = NIL_RTCRSTORE;
3092 int vrc = RTCrStoreCreateInMem(&hTrustedCerts, 1);
3093 AssertRCReturn(vrc, setErrorVrc(vrc, tr("RTCrStoreCreateInMem failed: %Rrc"), vrc));
3094
3095 vrc = RTCrStoreCertAddX509(hTrustedCerts, 0, &m->SignerCert, RTErrInfoInitStatic(pErrInfo));
3096 if (RT_SUCCESS(vrc))
3097 {
3098 void *pvData = NULL;
3099 size_t cbData = 0;
3100 hrc = i_readTailProcessingGetManifestData(&pvData, &cbData);
3101 if (SUCCEEDED(hrc))
3102 {
3103 RTTIMESPEC Now;
3104 vrc = RTCrPkcs7VerifySignedDataWithExternalData(&m->ContentInfo, RTCRPKCS7VERIFY_SD_F_TRUST_ALL_CERTS,
3105 NIL_RTCRSTORE /*hAdditionalCerts*/, hTrustedCerts,
3106 RTTimeNow(&Now), NULL /*pfnVerifyCert*/, NULL /*pvUser*/,
3107 pvData, cbData, RTErrInfoInitStatic(pErrInfo));
3108 if (RT_SUCCESS(vrc))
3109 m->fContentInfoValidSignature = true;
3110 else
3111 i_addWarning(tr("Failed to validate PKCS#7/CMS signature: %Rrc%RTeim"), vrc, &pErrInfo->Core);
3112 RTMemTmpFree(pvData);
3113 }
3114 }
3115 else
3116 hrc = setErrorVrc(vrc, tr("RTCrStoreCertAddX509 failed: %Rrc%RTeim"), vrc, &pErrInfo->Core);
3117 RTCrStoreRelease(hTrustedCerts);
3118 }
3119
3120 return hrc;
3121}
3122
3123
3124/**
3125 * Worker for i_readTailProcessing that verifies a self signed certificate when
3126 * no PKCS\#7/CMS signature using the same certificate is present.
3127 */
3128HRESULT Appliance::i_readTailProcessingVerifySelfSignedOvfCert(TaskOVF *pTask, RTCRSTORE hTrustedStore, PRTERRINFOSTATIC pErrInfo)
3129{
3130 /*
3131 * It's a self signed certificate. We assume the frontend will
3132 * present this fact to the user and give a choice whether this
3133 * is acceptable. But, first make sure it makes internal sense.
3134 */
3135 m->fCertificateMissingPath = true;
3136 PCRTCRCERTCTX pCertCtx = RTCrStoreCertByIssuerAndSerialNo(hTrustedStore, &m->SignerCert.TbsCertificate.Issuer,
3137 &m->SignerCert.TbsCertificate.SerialNumber);
3138 if (pCertCtx)
3139 {
3140 if (pCertCtx->pCert && RTCrX509Certificate_Compare(pCertCtx->pCert, &m->SignerCert) == 0)
3141 m->fCertificateMissingPath = true;
3142 RTCrCertCtxRelease(pCertCtx);
3143 }
3144
3145 int vrc = RTCrX509Certificate_VerifySignatureSelfSigned(&m->SignerCert, RTErrInfoInitStatic(pErrInfo));
3146 if (RT_SUCCESS(vrc))
3147 {
3148 m->fCertificateValid = true;
3149
3150 /* Check whether the certificate is currently valid, just warn if not. */
3151 RTTIMESPEC Now;
3152 m->fCertificateValidTime = RTCrX509Validity_IsValidAtTimeSpec(&m->SignerCert.TbsCertificate.Validity, RTTimeNow(&Now));
3153 if (m->fCertificateValidTime)
3154 {
3155 m->fCertificateValidTime = true;
3156 i_addWarning(tr("A self signed certificate was used to sign '%s'"), pTask->locInfo.strPath.c_str());
3157 }
3158 else
3159 i_addWarning(tr("Self signed certificate used to sign '%s' is not currently valid"),
3160 pTask->locInfo.strPath.c_str());
3161 }
3162 else
3163 {
3164 m->strCertError.printfNoThrow(tr("Verification of the self signed certificate failed (%Rrc%#RTeim)"),
3165 vrc, &pErrInfo->Core);
3166 i_addWarning(tr("Verification of the self signed certificate used to sign '%s' failed (%Rrc)%RTeim"),
3167 pTask->locInfo.strPath.c_str(), vrc, &pErrInfo->Core);
3168 }
3169
3170 /* Just warn if it's not a CA. Self-signed certificates are
3171 hardly trustworthy to start with without the user's consent. */
3172 if ( !m->SignerCert.TbsCertificate.T3.pBasicConstraints
3173 || !m->SignerCert.TbsCertificate.T3.pBasicConstraints->CA.fValue)
3174 i_addWarning(tr("Self signed certificate used to sign '%s' is not marked as certificate authority (CA)"),
3175 pTask->locInfo.strPath.c_str());
3176
3177 return S_OK;
3178}
3179
3180/**
3181 * Worker for i_readTailProcessing that verfies a non-self-issued OVF
3182 * certificate when no PKCS\#7/CMS signature using the same certificate is
3183 * present.
3184 */
3185HRESULT Appliance::i_readTailProcessingVerifyIssuedOvfCert(TaskOVF *pTask, RTCRSTORE hTrustedStore, PRTERRINFOSTATIC pErrInfo)
3186{
3187 /*
3188 * The certificate is not self-signed. Use the system certificate
3189 * stores to try build a path that validates successfully.
3190 */
3191 HRESULT hrc = S_OK;
3192 RTCRX509CERTPATHS hCertPaths;
3193 int vrc = RTCrX509CertPathsCreate(&hCertPaths, &m->SignerCert);
3194 if (RT_SUCCESS(vrc))
3195 {
3196 /* Get trusted certificates from the system and add them to the path finding mission. */
3197 vrc = RTCrX509CertPathsSetTrustedStore(hCertPaths, hTrustedStore);
3198 if (RT_FAILURE(vrc))
3199 hrc = setErrorBoth(E_FAIL, vrc, tr("RTCrX509CertPathsSetTrustedStore failed (%Rrc)"), vrc);
3200
3201 /* Add untrusted intermediate certificates. */
3202 if (RT_SUCCESS(vrc))
3203 {
3204 /// @todo RTCrX509CertPathsSetUntrustedStore(hCertPaths, hAdditionalCerts);
3205 /// We should look for intermediate certificates on the system, at least.
3206 }
3207 if (RT_SUCCESS(vrc))
3208 {
3209 /*
3210 * Do the building and verification of certificate paths.
3211 */
3212 vrc = RTCrX509CertPathsBuild(hCertPaths, RTErrInfoInitStatic(pErrInfo));
3213 if (RT_SUCCESS(vrc))
3214 {
3215 vrc = RTCrX509CertPathsValidateAll(hCertPaths, NULL, RTErrInfoInitStatic(pErrInfo));
3216 if (RT_SUCCESS(vrc))
3217 {
3218 /*
3219 * Mark the certificate as good.
3220 */
3221 /** @todo check the certificate purpose? If so, share with self-signed. */
3222 m->fCertificateValid = true;
3223 m->fCertificateMissingPath = false;
3224
3225 /*
3226 * We add a warning if the certificate path isn't valid at the current
3227 * time. Since the time is only considered during path validation and we
3228 * can repeat the validation process (but not building), it's easy to check.
3229 */
3230 RTTIMESPEC Now;
3231 vrc = RTCrX509CertPathsSetValidTimeSpec(hCertPaths, RTTimeNow(&Now));
3232 if (RT_SUCCESS(vrc))
3233 {
3234 vrc = RTCrX509CertPathsValidateAll(hCertPaths, NULL, RTErrInfoInitStatic(pErrInfo));
3235 if (RT_SUCCESS(vrc))
3236 m->fCertificateValidTime = true;
3237 else
3238 i_addWarning(tr("The certificate used to sign '%s' (or a certificate in the path) is not currently valid (%Rrc)"),
3239 pTask->locInfo.strPath.c_str(), vrc);
3240 }
3241 else
3242 hrc = setErrorVrc(vrc, tr("RTCrX509CertPathsSetValidTimeSpec failed: %Rrc"), vrc);
3243 }
3244 else if (vrc == VERR_CR_X509_CPV_NO_TRUSTED_PATHS)
3245 {
3246 m->fCertificateValid = true;
3247 i_addWarning(tr("No trusted certificate paths"));
3248
3249 /* Add another warning if the pathless certificate is not valid at present. */
3250 RTTIMESPEC Now;
3251 if (RTCrX509Validity_IsValidAtTimeSpec(&m->SignerCert.TbsCertificate.Validity, RTTimeNow(&Now)))
3252 m->fCertificateValidTime = true;
3253 else
3254 i_addWarning(tr("The certificate used to sign '%s' is not currently valid"),
3255 pTask->locInfo.strPath.c_str());
3256 }
3257 else
3258 hrc = setErrorBoth(E_FAIL, vrc, tr("Certificate path validation failed (%Rrc%RTeim)"), vrc, &pErrInfo->Core);
3259 }
3260 else
3261 hrc = setErrorBoth(E_FAIL, vrc, tr("Certificate path building failed (%Rrc%RTeim)"), vrc, &pErrInfo->Core);
3262 }
3263 RTCrX509CertPathsRelease(hCertPaths);
3264 }
3265 else
3266 hrc = setErrorVrc(vrc, tr("RTCrX509CertPathsCreate failed: %Rrc"), vrc);
3267 return hrc;
3268}
3269
3270/**
3271 * Helper for i_readTailProcessingVerifySignerInfo that reports a verfication
3272 * failure.
3273 *
3274 * @returns S_OK
3275 */
3276HRESULT Appliance::i_readTailProcessingVerifyContentInfoFailOne(const char *pszSignature, int vrc, PRTERRINFOSTATIC pErrInfo)
3277{
3278 i_addWarning(tr("%s verification failed: %Rrc%RTeim"), pszSignature, vrc, &pErrInfo->Core);
3279 if (m->strCertError.isEmpty())
3280 m->strCertError.printfNoThrow(tr("%s verification failed: %Rrc%RTeim"), pszSignature, vrc, &pErrInfo->Core);
3281 return S_OK;
3282}
3283
3284/**
3285 * Worker for i_readTailProcessingVerifyContentInfoCerts that analyzes why the
3286 * standard verification of a signer info entry failed (@a vrc & @a pErrInfo).
3287 *
3288 * There are a couple of things we might want try to investigate deeper here:
3289 * 1. Untrusted signing certificate, often self-signed.
3290 * 2. Untrusted timstamp signing certificate.
3291 * 3. Certificate not valid at the current time and there isn't a
3292 * timestamp counter signature.
3293 *
3294 * That said, it is difficult to get an accurate fix and report on the
3295 * issues here since there are a number of error sources, so just try identify
3296 * the more typical cases.
3297 *
3298 * @note Caller cleans up *phTrustedStore2 if not NIL.
3299 */
3300HRESULT Appliance::i_readTailProcessingVerifyAnalyzeSignerInfo(void const *pvData, size_t cbData, RTCRSTORE hTrustedStore,
3301 uint32_t iSigner, PRTTIMESPEC pNow, int vrc,
3302 PRTERRINFOSTATIC pErrInfo, PRTCRSTORE phTrustedStore2)
3303{
3304 PRTCRPKCS7SIGNEDDATA const pSignedData = m->ContentInfo.u.pSignedData;
3305 PRTCRPKCS7SIGNERINFO const pSigner = pSignedData->SignerInfos.papItems[iSigner];
3306
3307 /*
3308 * Error/warning message prefix:
3309 */
3310 const char *pszSignature;
3311 if (iSigner == 0 && m->fContentInfoSameCert)
3312 pszSignature = tr("OVF & PKCS#7/CMS signature");
3313 else
3314 pszSignature = tr("PKCS#7/CMS signature");
3315 char szSignatureBuf[64];
3316 if (pSignedData->SignerInfos.cItems > 1)
3317 {
3318 RTStrPrintf(szSignatureBuf, sizeof(szSignatureBuf), "%s #%u", pszSignature, iSigner + 1);
3319 pszSignature = szSignatureBuf;
3320 }
3321
3322 /*
3323 * Don't try handle weird stuff:
3324 */
3325 /** @todo Are there more statuses we can deal with here? */
3326 if ( vrc != VERR_CR_X509_CPV_NOT_VALID_AT_TIME
3327 && vrc != VERR_CR_X509_NO_TRUST_ANCHOR)
3328 return i_readTailProcessingVerifyContentInfoFailOne(pszSignature, vrc, pErrInfo);
3329
3330 /*
3331 * Find the signing certificate.
3332 * We require the certificate to be included in the signed data here.
3333 */
3334 PCRTCRX509CERTIFICATE pSigningCert;
3335 pSigningCert = RTCrPkcs7SetOfCerts_FindX509ByIssuerAndSerialNumber(&pSignedData->Certificates,
3336 &pSigner->IssuerAndSerialNumber.Name,
3337 &pSigner->IssuerAndSerialNumber.SerialNumber);
3338 if (!pSigningCert)
3339 {
3340 i_addWarning(tr("PKCS#7/CMS signature #%u does not include the signing certificate"), iSigner + 1);
3341 if (m->strCertError.isEmpty())
3342 m->strCertError.printfNoThrow(tr("PKCS#7/CMS signature #%u does not include the signing certificate"), iSigner + 1);
3343 return S_OK;
3344 }
3345
3346 PCRTCRCERTCTX const pCertCtxTrusted = RTCrStoreCertByIssuerAndSerialNo(hTrustedStore, &pSigner->IssuerAndSerialNumber.Name,
3347 &pSigner->IssuerAndSerialNumber.SerialNumber);
3348 bool const fSelfSigned = RTCrX509Certificate_IsSelfSigned(pSigningCert);
3349
3350 /*
3351 * Add warning about untrusted self-signed certificate:
3352 */
3353 if (fSelfSigned && !pCertCtxTrusted)
3354 i_addWarning(tr("%s: Untrusted self-signed certificate"), pszSignature);
3355
3356 /*
3357 * Start by eliminating signing time issues (2 + 3) first as primary problem.
3358 * Keep the error info and status for later failures.
3359 */
3360 char szTime[RTTIME_STR_LEN];
3361 RTTIMESPEC Now2 = *pNow;
3362 vrc = RTCrPkcs7VerifySignedDataWithExternalData(&m->ContentInfo, RTCRPKCS7VERIFY_SD_F_USE_SIGNING_TIME_UNVERIFIED
3363 | RTCRPKCS7VERIFY_SD_F_UPDATE_VALIDATION_TIME
3364 | RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX(iSigner)
3365 | RTCRPKCS7VERIFY_SD_F_CHECK_TRUST_ANCHORS, NIL_RTCRSTORE,
3366 hTrustedStore, &Now2, NULL, NULL,
3367 pvData, cbData, RTErrInfoInitStatic(pErrInfo));
3368 if (RT_SUCCESS(vrc))
3369 {
3370 /* Okay, is it an untrusted time signing certificate or just signing time in general? */
3371 RTTIMESPEC Now3 = *pNow;
3372 vrc = RTCrPkcs7VerifySignedDataWithExternalData(&m->ContentInfo, RTCRPKCS7VERIFY_SD_F_USE_SIGNING_TIME_UNVERIFIED
3373 | RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE_SIGNING_TIME_ONLY
3374 | RTCRPKCS7VERIFY_SD_F_UPDATE_VALIDATION_TIME
3375 | RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX(iSigner)
3376 | RTCRPKCS7VERIFY_SD_F_CHECK_TRUST_ANCHORS, NIL_RTCRSTORE,
3377 hTrustedStore, &Now3, NULL, NULL, pvData, cbData, NULL);
3378 if (RT_SUCCESS(vrc))
3379 i_addWarning(tr("%s: Untrusted timestamp (%s)"), pszSignature, RTTimeSpecToString(&Now3, szTime, sizeof(szTime)));
3380 else
3381 i_addWarning(tr("%s: Not valid at current time, but validates fine for untrusted signing time (%s)"),
3382 pszSignature, RTTimeSpecToString(&Now2, szTime, sizeof(szTime)));
3383 return S_OK;
3384 }
3385
3386 /* If we've got a trusted signing certificate (unlikely, but whatever), we can stop already.
3387 If we haven't got a self-signed certificate, stop too as messaging becomes complicated otherwise. */
3388 if (pCertCtxTrusted || !fSelfSigned)
3389 return i_readTailProcessingVerifyContentInfoFailOne(pszSignature, vrc, pErrInfo);
3390
3391 int const vrcErrInfo = vrc;
3392
3393 /*
3394 * Create a new trust store that includes the signing certificate
3395 * to see what that changes.
3396 */
3397 vrc = RTCrStoreCreateInMemEx(phTrustedStore2, 1, hTrustedStore);
3398 AssertRCReturn(vrc, setErrorVrc(vrc, "RTCrStoreCreateInMemEx"));
3399 vrc = RTCrStoreCertAddX509(*phTrustedStore2, 0, (PRTCRX509CERTIFICATE)pSigningCert, NULL);
3400 AssertRCReturn(vrc, setErrorVrc(vrc, "RTCrStoreCertAddX509/%u", iSigner));
3401
3402 vrc = RTCrPkcs7VerifySignedDataWithExternalData(&m->ContentInfo,
3403 RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE_SIGNING_TIME_ONLY
3404 | RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX(iSigner)
3405 | RTCRPKCS7VERIFY_SD_F_CHECK_TRUST_ANCHORS, NIL_RTCRSTORE,
3406 *phTrustedStore2, pNow, NULL, NULL, pvData, cbData, NULL);
3407 if (RT_SUCCESS(vrc))
3408 {
3409 if (!fSelfSigned)
3410 i_readTailProcessingVerifyContentInfoFailOne(pszSignature, vrcErrInfo, pErrInfo);
3411 return S_OK;
3412 }
3413
3414 /*
3415 * Time problems too? Repeat what we did above, but with the modified trust store.
3416 */
3417 Now2 = *pNow;
3418 vrc = RTCrPkcs7VerifySignedDataWithExternalData(&m->ContentInfo, RTCRPKCS7VERIFY_SD_F_USE_SIGNING_TIME_UNVERIFIED
3419 | RTCRPKCS7VERIFY_SD_F_UPDATE_VALIDATION_TIME
3420 | RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX(iSigner)
3421 | RTCRPKCS7VERIFY_SD_F_CHECK_TRUST_ANCHORS, NIL_RTCRSTORE,
3422 *phTrustedStore2, pNow, NULL, NULL, pvData, cbData, NULL);
3423 if (RT_SUCCESS(vrc))
3424 {
3425 /* Okay, is it an untrusted time signing certificate or just signing time in general? */
3426 RTTIMESPEC Now3 = *pNow;
3427 vrc = RTCrPkcs7VerifySignedDataWithExternalData(&m->ContentInfo, RTCRPKCS7VERIFY_SD_F_USE_SIGNING_TIME_UNVERIFIED
3428 | RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE_SIGNING_TIME_ONLY
3429 | RTCRPKCS7VERIFY_SD_F_UPDATE_VALIDATION_TIME
3430 | RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX(iSigner)
3431 | RTCRPKCS7VERIFY_SD_F_CHECK_TRUST_ANCHORS, NIL_RTCRSTORE,
3432 *phTrustedStore2, &Now3, NULL, NULL, pvData, cbData, NULL);
3433 if (RT_SUCCESS(vrc))
3434 i_addWarning(tr("%s: Untrusted timestamp (%s)"), pszSignature, RTTimeSpecToString(&Now3, szTime, sizeof(szTime)));
3435 else
3436 i_addWarning(tr("%s: Not valid at current time, but validates fine for untrusted signing time (%s)"),
3437 pszSignature, RTTimeSpecToString(&Now2, szTime, sizeof(szTime)));
3438 }
3439 else
3440 i_readTailProcessingVerifyContentInfoFailOne(pszSignature, vrcErrInfo, pErrInfo);
3441
3442 return S_OK;
3443}
3444
3445/**
3446 * Verify the signing certificates used to sign the PKCS\#7/CMS signature.
3447 *
3448 * ASSUMES that we've previously verified the PKCS\#7/CMS stuff in
3449 * trust-all-certs-without-question mode and it's just the certificate
3450 * validation that can fail now.
3451 */
3452HRESULT Appliance::i_readTailProcessingVerifyContentInfoCerts(void const *pvData, size_t cbData,
3453 RTCRSTORE hTrustedStore, PRTERRINFOSTATIC pErrInfo)
3454{
3455 /*
3456 * Just do a run and see what happens (note we've already verified
3457 * the data signatures, which just leaves certificates and paths).
3458 */
3459 RTTIMESPEC Now;
3460 int vrc = RTCrPkcs7VerifySignedDataWithExternalData(&m->ContentInfo,
3461 RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE_SIGNING_TIME_ONLY
3462 | RTCRPKCS7VERIFY_SD_F_CHECK_TRUST_ANCHORS,
3463 NIL_RTCRSTORE /*hAdditionalCerts*/, hTrustedStore,
3464 RTTimeNow(&Now), NULL /*pfnVerifyCert*/, NULL /*pvUser*/,
3465 pvData, cbData, RTErrInfoInitStatic(pErrInfo));
3466 if (RT_SUCCESS(vrc))
3467 m->fContentInfoVerifiedOkay = true;
3468 else
3469 {
3470 /*
3471 * Deal with each of the signatures separately to try figure out
3472 * more exactly what's going wrong.
3473 */
3474 uint32_t cVerifiedOkay = 0;
3475 PRTCRPKCS7SIGNEDDATA pSignedData = m->ContentInfo.u.pSignedData;
3476 for (uint32_t iSigner = 0; iSigner < pSignedData->SignerInfos.cItems; iSigner++)
3477 {
3478 vrc = RTCrPkcs7VerifySignedDataWithExternalData(&m->ContentInfo,
3479 RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE_SIGNING_TIME_ONLY
3480 | RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX(iSigner)
3481 | RTCRPKCS7VERIFY_SD_F_CHECK_TRUST_ANCHORS,
3482 NIL_RTCRSTORE /*hAdditionalCerts*/, hTrustedStore,
3483 &Now, NULL /*pfnVerifyCert*/, NULL /*pvUser*/,
3484 pvData, cbData, RTErrInfoInitStatic(pErrInfo));
3485 if (RT_SUCCESS(vrc))
3486 cVerifiedOkay++;
3487 else
3488 {
3489 RTCRSTORE hTrustedStore2 = NIL_RTCRSTORE;
3490 HRESULT hrc = i_readTailProcessingVerifyAnalyzeSignerInfo(pvData, cbData, hTrustedStore, iSigner, &Now,
3491 vrc, pErrInfo, &hTrustedStore2);
3492 RTCrStoreRelease(hTrustedStore2);
3493 if (FAILED(hrc))
3494 return hrc;
3495 }
3496 }
3497
3498 if ( pSignedData->SignerInfos.cItems > 1
3499 && pSignedData->SignerInfos.cItems != cVerifiedOkay)
3500 i_addWarning(tr("%u out of %u PKCS#7/CMS signatures verfified okay", "",
3501 pSignedData->SignerInfos.cItems),
3502 cVerifiedOkay, pSignedData->SignerInfos.cItems);
3503 }
3504
3505 return S_OK;
3506}
3507
3508
3509
3510/*******************************************************************************
3511 * Import stuff
3512 ******************************************************************************/
3513
3514/**
3515 * Implementation for importing OVF data into VirtualBox. This starts a new thread which will call
3516 * Appliance::taskThreadImportOrExport().
3517 *
3518 * This creates one or more new machines according to the VirtualSystemScription instances created by
3519 * Appliance::Interpret().
3520 *
3521 * This is in a separate private method because it is used from one location:
3522 *
3523 * 1) from the public Appliance::ImportMachines().
3524 *
3525 * @param locInfo
3526 * @param progress
3527 * @return
3528 */
3529HRESULT Appliance::i_importImpl(const LocationInfo &locInfo,
3530 ComObjPtr<Progress> &progress)
3531{
3532 HRESULT hrc;
3533
3534 /* Initialize our worker task */
3535 ThreadTask *pTask;
3536 if (locInfo.storageType != VFSType_Cloud)
3537 {
3538 hrc = i_setUpProgress(progress, Utf8StrFmt(tr("Importing appliance '%s'"), locInfo.strPath.c_str()),
3539 locInfo.storageType == VFSType_File ? ImportFile : ImportS3);
3540 if (FAILED(hrc))
3541 return setError(hrc, tr("Failed to create task for importing appliance into VirtualBox"));
3542 try
3543 {
3544 pTask = new TaskOVF(this, TaskOVF::Import, locInfo, progress);
3545 }
3546 catch (std::bad_alloc &)
3547 {
3548 return E_OUTOFMEMORY;
3549 }
3550 }
3551 else
3552 {
3553 if (locInfo.strProvider.equals("OCI"))
3554 {
3555 /*
3556 * 1. Create a custom image from the instance:
3557 * - 2 operations (starting and waiting)
3558 * 2. Import the custom image into the Object Storage (OCI format - TAR file with QCOW2 image and JSON file):
3559 * - 2 operations (starting and waiting)
3560 * 3. Download the object from the Object Storage:
3561 * - 1 operation (starting and downloadind is one operation)
3562 * 4. Open the object, extract an image and convert one to VDI:
3563 * - 1 operation (extracting and conversion are piped) because only 1 base bootable image is imported for now
3564 * 5. Create VM with user settings and attach the converted image to VM:
3565 * - 1 operation.
3566 * 6. Cleanup phase.
3567 * - 1 to N operations.
3568 * The number of the correct Progress operations are much tricky here.
3569 * Whether Machine::deleteConfig() is called or Medium::deleteStorage() is called in the loop.
3570 * Both require a new Progress object. To work with these functions the original Progress object uses
3571 * the function Progress::waitForOtherProgressCompletion().
3572 *
3573 * Some speculation here...
3574 * Total: 2+2+1(cloud) + 1+1(local) + 1+1+1(cleanup) = 10 operations
3575 * or
3576 * Total: 2+2+1(cloud) + 1+1(local) + 1(cleanup) = 8 operations
3577 * if VM wasn't created we would have only 1 registered image for cleanup.
3578 *
3579 * Weight "#define"s for the Cloud operations are located in the file OCICloudClient.h.
3580 * Weight of cloud import operations (1-3 items from above):
3581 * Total = 750 = 25+75(start and wait)+25+375(start and wait)+250(download)
3582 *
3583 * Weight of local import operations (4-5 items from above):
3584 * Total = 150 = 100 (extract and convert) + 50 (create VM, attach disks)
3585 *
3586 * Weight of local cleanup operations (6 item from above):
3587 * Some speculation here...
3588 * Total = 3 = 1 (1 image) + 1 (1 setting file)+ 1 (1 prev setting file) - quick operations
3589 * or
3590 * Total = 1 (1 image) if VM wasn't created we would have only 1 registered image for now.
3591 */
3592 try
3593 {
3594 hrc = progress.createObject();
3595 if (SUCCEEDED(hrc))
3596 hrc = progress->init(mVirtualBox, static_cast<IAppliance *>(this),
3597 Utf8Str(tr("Importing VM from Cloud...")),
3598 TRUE /* aCancelable */,
3599 10, // ULONG cOperations,
3600 1000, // ULONG ulTotalOperationsWeight,
3601 Utf8Str(tr("Start import VM from the Cloud...")), // aFirstOperationDescription
3602 25); // ULONG ulFirstOperationWeight
3603 if (SUCCEEDED(hrc))
3604 pTask = new TaskCloud(this, TaskCloud::Import, locInfo, progress);
3605 else
3606 pTask = NULL; /* shut up vcc */
3607 }
3608 catch (std::bad_alloc &)
3609 {
3610 return E_OUTOFMEMORY;
3611 }
3612 if (FAILED(hrc))
3613 return setError(hrc, tr("Failed to create task for importing appliance into VirtualBox"));
3614 }
3615 else
3616 return setError(E_NOTIMPL, tr("Only \"OCI\" cloud provider is supported for now. \"%s\" isn't supported."),
3617 locInfo.strProvider.c_str());
3618 }
3619
3620 /*
3621 * Start the task thread.
3622 */
3623 hrc = pTask->createThread();
3624 pTask = NULL;
3625 if (SUCCEEDED(hrc))
3626 return hrc;
3627 return setError(hrc, tr("Failed to start thread for importing appliance into VirtualBox"));
3628}
3629
3630/**
3631 * Actual worker code for importing OVF data into VirtualBox.
3632 *
3633 * This is called from Appliance::taskThreadImportOrExport() and therefore runs
3634 * on the OVF import worker thread. This creates one or more new machines
3635 * according to the VirtualSystemScription instances created by
3636 * Appliance::Interpret().
3637 *
3638 * This runs in two contexts:
3639 *
3640 * 1) in a first worker thread; in that case, Appliance::ImportMachines() called
3641 * Appliance::i_importImpl();
3642 *
3643 * 2) in a second worker thread; in that case, Appliance::ImportMachines()
3644 * called Appliance::i_importImpl(), which called Appliance::i_importFSOVA(),
3645 * which called Appliance::i_importImpl(), which then called this again.
3646 *
3647 * @param pTask The OVF task data.
3648 * @return COM status code.
3649 */
3650HRESULT Appliance::i_importFS(TaskOVF *pTask)
3651{
3652 LogFlowFuncEnter();
3653 LogFlowFunc(("Appliance %p\n", this));
3654
3655 /* Change the appliance state so we can safely leave the lock while doing
3656 * time-consuming image imports; also the below method calls do all kinds of
3657 * locking which conflicts with the appliance object lock. */
3658 AutoWriteLock writeLock(this COMMA_LOCKVAL_SRC_POS);
3659 /* Check if the appliance is currently busy. */
3660 if (!i_isApplianceIdle())
3661 return E_ACCESSDENIED;
3662 /* Set the internal state to importing. */
3663 m->state = ApplianceImporting;
3664
3665 HRESULT hrc = S_OK;
3666
3667 /* Clear the list of imported machines, if any */
3668 m->llGuidsMachinesCreated.clear();
3669
3670 if (pTask->locInfo.strPath.endsWith(".ovf", Utf8Str::CaseInsensitive))
3671 hrc = i_importFSOVF(pTask, writeLock);
3672 else
3673 hrc = i_importFSOVA(pTask, writeLock);
3674 if (FAILED(hrc))
3675 {
3676 /* With _whatever_ error we've had, do a complete roll-back of
3677 * machines and images we've created */
3678 writeLock.release();
3679 ErrorInfoKeeper eik;
3680 for (list<Guid>::iterator itID = m->llGuidsMachinesCreated.begin();
3681 itID != m->llGuidsMachinesCreated.end();
3682 ++itID)
3683 {
3684 Guid guid = *itID;
3685 Bstr bstrGuid = guid.toUtf16();
3686 ComPtr<IMachine> failedMachine;
3687 HRESULT rc2 = mVirtualBox->FindMachine(bstrGuid.raw(), failedMachine.asOutParam());
3688 if (SUCCEEDED(rc2))
3689 {
3690 SafeIfaceArray<IMedium> aMedia;
3691 rc2 = failedMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly, ComSafeArrayAsOutParam(aMedia));
3692 ComPtr<IProgress> pProgress2;
3693 rc2 = failedMachine->DeleteConfig(ComSafeArrayAsInParam(aMedia), pProgress2.asOutParam());
3694 pProgress2->WaitForCompletion(-1);
3695 }
3696 }
3697 writeLock.acquire();
3698 }
3699
3700 /* Reset the state so others can call methods again */
3701 m->state = ApplianceIdle;
3702
3703 LogFlowFunc(("hrc=%Rhrc\n", hrc));
3704 LogFlowFuncLeave();
3705 return hrc;
3706}
3707
3708HRESULT Appliance::i_importFSOVF(TaskOVF *pTask, AutoWriteLockBase &rWriteLock)
3709{
3710 return i_importDoIt(pTask, rWriteLock);
3711}
3712
3713HRESULT Appliance::i_importFSOVA(TaskOVF *pTask, AutoWriteLockBase &rWriteLock)
3714{
3715 LogFlowFuncEnter();
3716
3717 /*
3718 * Open the tar file as file stream.
3719 */
3720 RTVFSIOSTREAM hVfsIosOva;
3721 int vrc = RTVfsIoStrmOpenNormal(pTask->locInfo.strPath.c_str(),
3722 RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN, &hVfsIosOva);
3723 if (RT_FAILURE(vrc))
3724 return setErrorVrc(vrc, tr("Error opening the OVA file '%s' (%Rrc)"), pTask->locInfo.strPath.c_str(), vrc);
3725
3726 RTVFSFSSTREAM hVfsFssOva;
3727 vrc = RTZipTarFsStreamFromIoStream(hVfsIosOva, 0 /*fFlags*/, &hVfsFssOva);
3728 RTVfsIoStrmRelease(hVfsIosOva);
3729 if (RT_FAILURE(vrc))
3730 return setErrorVrc(vrc, tr("Error reading the OVA file '%s' (%Rrc)"), pTask->locInfo.strPath.c_str(), vrc);
3731
3732 /*
3733 * Join paths with the i_importFSOVF code.
3734 *
3735 * Note! We don't need to skip the OVF, manifest or signature files, as the
3736 * i_importMachineGeneric, i_importVBoxMachine and i_importOpenSourceFile
3737 * code will deal with this (as there could be other files in the OVA
3738 * that we don't process, like 'de-DE-resources.xml' in EXAMPLE 1,
3739 * Appendix D.1, OVF v2.1.0).
3740 */
3741 HRESULT hrc = i_importDoIt(pTask, rWriteLock, hVfsFssOva);
3742
3743 RTVfsFsStrmRelease(hVfsFssOva);
3744
3745 LogFlowFunc(("returns %Rhrc\n", hrc));
3746 return hrc;
3747}
3748
3749/**
3750 * Does the actual importing after the caller has made the source accessible.
3751 *
3752 * @param pTask The import task.
3753 * @param rWriteLock The write lock the caller's caller is holding,
3754 * will be released for some reason.
3755 * @param hVfsFssOva The file system stream if OVA, NIL if not.
3756 * @returns COM status code.
3757 * @throws Nothing.
3758 */
3759HRESULT Appliance::i_importDoIt(TaskOVF *pTask, AutoWriteLockBase &rWriteLock, RTVFSFSSTREAM hVfsFssOva /*= NIL_RTVFSFSSTREAM*/)
3760{
3761 rWriteLock.release();
3762
3763 HRESULT hrc = E_FAIL;
3764 try
3765 {
3766 /*
3767 * Create the import stack for the rollback on errors.
3768 */
3769 ImportStack stack(pTask->locInfo, m->pReader->m_mapDisks, pTask->pProgress, hVfsFssOva);
3770
3771 try
3772 {
3773 /* Do the importing. */
3774 i_importMachines(stack);
3775
3776 /* We should've processed all the files now, so compare. */
3777 hrc = i_verifyManifestFile(stack);
3778
3779 /* If everything was successful so far check if some extension
3780 * pack wants to do file sanity checking. */
3781 if (SUCCEEDED(hrc))
3782 {
3783 /** @todo */;
3784 }
3785 }
3786 catch (HRESULT hrcXcpt)
3787 {
3788 hrc = hrcXcpt;
3789 }
3790 catch (...)
3791 {
3792 AssertFailed();
3793 hrc = E_FAIL;
3794 }
3795 if (FAILED(hrc))
3796 {
3797 /*
3798 * Restoring original UUID from OVF description file.
3799 * During import VBox creates new UUIDs for imported images and
3800 * assigns them to the images. In case of failure we have to restore
3801 * the original UUIDs because those new UUIDs are obsolete now and
3802 * won't be used anymore.
3803 */
3804 ErrorInfoKeeper eik; /* paranoia */
3805 list< ComObjPtr<VirtualSystemDescription> >::const_iterator itvsd;
3806 /* Iterate through all virtual systems of that appliance */
3807 for (itvsd = m->virtualSystemDescriptions.begin();
3808 itvsd != m->virtualSystemDescriptions.end();
3809 ++itvsd)
3810 {
3811 ComObjPtr<VirtualSystemDescription> vsdescThis = (*itvsd);
3812 settings::MachineConfigFile *pConfig = vsdescThis->m->pConfig;
3813 if(vsdescThis->m->pConfig!=NULL)
3814 stack.restoreOriginalUUIDOfAttachedDevice(pConfig);
3815 }
3816 }
3817 }
3818 catch (...)
3819 {
3820 hrc = E_FAIL;
3821 AssertFailed();
3822 }
3823
3824 rWriteLock.acquire();
3825 return hrc;
3826}
3827
3828/**
3829 * Undocumented, you figure it from the name.
3830 *
3831 * @returns Undocumented
3832 * @param stack Undocumented.
3833 */
3834HRESULT Appliance::i_verifyManifestFile(ImportStack &stack)
3835{
3836 LogFlowThisFuncEnter();
3837 HRESULT hrc;
3838 int vrc;
3839
3840 /*
3841 * No manifest is fine, it always matches.
3842 */
3843 if (m->hTheirManifest == NIL_RTMANIFEST)
3844 hrc = S_OK;
3845 else
3846 {
3847 /*
3848 * Hack: If the manifest we just read doesn't have a digest for the OVF, copy
3849 * it from the manifest we got from the caller.
3850 * @bugref{6022#c119}
3851 */
3852 if ( !RTManifestEntryExists(m->hTheirManifest, m->strOvfManifestEntry.c_str())
3853 && RTManifestEntryExists(m->hOurManifest, m->strOvfManifestEntry.c_str()) )
3854 {
3855 uint32_t fType = 0;
3856 char szDigest[512 + 1];
3857 vrc = RTManifestEntryQueryAttr(m->hOurManifest, m->strOvfManifestEntry.c_str(), NULL, RTMANIFEST_ATTR_ANY,
3858 szDigest, sizeof(szDigest), &fType);
3859 if (RT_SUCCESS(vrc))
3860 vrc = RTManifestEntrySetAttr(m->hTheirManifest, m->strOvfManifestEntry.c_str(),
3861 NULL /*pszAttr*/, szDigest, fType);
3862 if (RT_FAILURE(vrc))
3863 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Error fudging missing OVF digest in manifest: %Rrc"), vrc);
3864 }
3865
3866 /*
3867 * Compare with the digests we've created while read/processing the import.
3868 *
3869 * We specify the RTMANIFEST_EQUALS_IGN_MISSING_ATTRS to ignore attributes
3870 * (SHA1, SHA256, etc) that are only present in one of the manifests, as long
3871 * as each entry has at least one common attribute that we can check. This
3872 * is important for the OVF in OVAs, for which we generates several digests
3873 * since we don't know which are actually used in the manifest (OVF comes
3874 * first in an OVA, then manifest).
3875 */
3876 char szErr[256];
3877 vrc = RTManifestEqualsEx(m->hTheirManifest, m->hOurManifest, NULL /*papszIgnoreEntries*/,
3878 NULL /*papszIgnoreAttrs*/,
3879 RTMANIFEST_EQUALS_IGN_MISSING_ATTRS | RTMANIFEST_EQUALS_IGN_MISSING_ENTRIES_2ND,
3880 szErr, sizeof(szErr));
3881 if (RT_SUCCESS(vrc))
3882 hrc = S_OK;
3883 else
3884 hrc = setErrorVrc(vrc, tr("Digest mismatch (%Rrc): %s"), vrc, szErr);
3885 }
3886
3887 NOREF(stack);
3888 LogFlowThisFunc(("returns %Rhrc\n", hrc));
3889 return hrc;
3890}
3891
3892/**
3893 * Helper that converts VirtualSystem attachment values into VirtualBox attachment values.
3894 * Throws HRESULT values on errors!
3895 *
3896 * @param hdc in: the HardDiskController structure to attach to.
3897 * @param ulAddressOnParent in: the AddressOnParent parameter from OVF.
3898 * @param controllerName out: the name of the storage controller to attach to (e.g. "IDE").
3899 * @param lControllerPort out: the channel (controller port) of the controller to attach to.
3900 * @param lDevice out: the device number to attach to.
3901 */
3902void Appliance::i_convertDiskAttachmentValues(const ovf::HardDiskController &hdc,
3903 uint32_t ulAddressOnParent,
3904 Utf8Str &controllerName,
3905 int32_t &lControllerPort,
3906 int32_t &lDevice)
3907{
3908 Log(("Appliance::i_convertDiskAttachmentValues: hdc.system=%d, hdc.fPrimary=%d, ulAddressOnParent=%d\n",
3909 hdc.system,
3910 hdc.fPrimary,
3911 ulAddressOnParent));
3912
3913 switch (hdc.system)
3914 {
3915 case ovf::HardDiskController::IDE:
3916 // For the IDE bus, the port parameter can be either 0 or 1, to specify the primary
3917 // or secondary IDE controller, respectively. For the primary controller of the IDE bus,
3918 // the device number can be either 0 or 1, to specify the master or the slave device,
3919 // respectively. For the secondary IDE controller, the device number is always 1 because
3920 // the master device is reserved for the CD-ROM drive.
3921 controllerName = "IDE";
3922 switch (ulAddressOnParent)
3923 {
3924 case 0: // master
3925 if (!hdc.fPrimary)
3926 {
3927 // secondary master
3928 lControllerPort = 1;
3929 lDevice = 0;
3930 }
3931 else // primary master
3932 {
3933 lControllerPort = 0;
3934 lDevice = 0;
3935 }
3936 break;
3937
3938 case 1: // slave
3939 if (!hdc.fPrimary)
3940 {
3941 // secondary slave
3942 lControllerPort = 1;
3943 lDevice = 1;
3944 }
3945 else // primary slave
3946 {
3947 lControllerPort = 0;
3948 lDevice = 1;
3949 }
3950 break;
3951
3952 // used by older VBox exports
3953 case 2: // interpret this as secondary master
3954 lControllerPort = 1;
3955 lDevice = 0;
3956 break;
3957
3958 // used by older VBox exports
3959 case 3: // interpret this as secondary slave
3960 lControllerPort = 1;
3961 lDevice = 1;
3962 break;
3963
3964 default:
3965 throw setError(VBOX_E_NOT_SUPPORTED,
3966 tr("Invalid channel %RU32 specified; IDE controllers support only 0, 1 or 2"),
3967 ulAddressOnParent);
3968 break;
3969 }
3970 break;
3971
3972 case ovf::HardDiskController::SATA:
3973 controllerName = "SATA";
3974 lControllerPort = (int32_t)ulAddressOnParent;
3975 lDevice = 0;
3976 break;
3977
3978 case ovf::HardDiskController::SCSI:
3979 {
3980 if (hdc.strControllerType.compare("lsilogicsas")==0)
3981 controllerName = "SAS";
3982 else
3983 controllerName = "SCSI";
3984 lControllerPort = (int32_t)ulAddressOnParent;
3985 lDevice = 0;
3986 break;
3987 }
3988
3989 case ovf::HardDiskController::VIRTIOSCSI:
3990 controllerName = "VirtioSCSI";
3991 lControllerPort = (int32_t)ulAddressOnParent;
3992 lDevice = 0;
3993 break;
3994
3995 default: break;
3996 }
3997
3998 Log(("=> lControllerPort=%d, lDevice=%d\n", lControllerPort, lDevice));
3999}
4000
4001/**
4002 * Imports one image.
4003 *
4004 * This is common code shared between
4005 * -- i_importMachineGeneric() for the OVF case; in that case the information comes from
4006 * the OVF virtual systems;
4007 * -- i_importVBoxMachine(); in that case, the information comes from the <vbox:Machine>
4008 * tag.
4009 *
4010 * Both ways of describing machines use the OVF disk references section, so in both cases
4011 * the caller needs to pass in the ovf::DiskImage structure from ovfreader.cpp.
4012 *
4013 * As a result, in both cases, if di.strHref is empty, we create a new image as per the OVF
4014 * spec, even though this cannot really happen in the vbox:Machine case since such data
4015 * would never have been exported.
4016 *
4017 * This advances stack.pProgress by one operation with the image's weight.
4018 *
4019 * @param di ovfreader.cpp structure describing the image from the OVF that is to be imported
4020 * @param strDstPath Where to create the target image.
4021 * @param pTargetMedium out: The newly created target medium. This also gets pushed on stack.llHardDisksCreated for cleanup.
4022 * @param stack
4023 *
4024 * @throws HRESULT
4025 */
4026void Appliance::i_importOneDiskImage(const ovf::DiskImage &di,
4027 const Utf8Str &strDstPath,
4028 ComObjPtr<Medium> &pTargetMedium,
4029 ImportStack &stack)
4030{
4031 HRESULT hrc;
4032
4033 Utf8Str strAbsDstPath;
4034 int vrc = RTPathAbsExCxx(strAbsDstPath, stack.strMachineFolder, strDstPath);
4035 AssertRCStmt(vrc, throw Global::vboxStatusCodeToCOM(vrc));
4036
4037 /* Get the system properties. */
4038 SystemProperties *pSysProps = mVirtualBox->i_getSystemProperties();
4039
4040 /* Keep the source file ref handy for later. */
4041 const Utf8Str &strSourceOVF = di.strHref;
4042
4043 /* Construct source file path */
4044 Utf8Str strSrcFilePath;
4045 if (stack.hVfsFssOva != NIL_RTVFSFSSTREAM)
4046 strSrcFilePath = strSourceOVF;
4047 else
4048 {
4049 strSrcFilePath = stack.strSourceDir;
4050 strSrcFilePath.append(RTPATH_SLASH_STR);
4051 strSrcFilePath.append(strSourceOVF);
4052 }
4053
4054 /* First of all check if the original (non-absolute) destination path is
4055 * a valid medium UUID. If so, the user wants to import the image into
4056 * an existing path. This is useful for iSCSI for example. */
4057 /** @todo r=klaus the code structure after this point is totally wrong,
4058 * full of unnecessary code duplication and other issues. 4.2 still had
4059 * the right structure for importing into existing medium objects, which
4060 * the current code can't possibly handle. */
4061 RTUUID uuid;
4062 vrc = RTUuidFromStr(&uuid, strDstPath.c_str());
4063 if (vrc == VINF_SUCCESS)
4064 {
4065 hrc = mVirtualBox->i_findHardDiskById(Guid(uuid), true, &pTargetMedium);
4066 if (FAILED(hrc)) throw hrc;
4067 }
4068 else
4069 {
4070 RTVFSIOSTREAM hVfsIosSrc = NIL_RTVFSIOSTREAM;
4071
4072 /* check read file to GZIP compression */
4073 bool const fGzipped = di.strCompression.compare("gzip", Utf8Str::CaseInsensitive) == 0;
4074 Utf8Str strDeleteTemp;
4075 try
4076 {
4077 Utf8Str strTrgFormat = "VMDK";
4078 ComObjPtr<MediumFormat> trgFormat;
4079 Bstr bstrFormatName;
4080 ULONG lCabs = 0;
4081
4082 char *pszSuff = RTPathSuffix(strAbsDstPath.c_str());
4083 if (pszSuff != NULL)
4084 {
4085 /*
4086 * Figure out which format the user like to have. Default is VMDK
4087 * or it can be VDI if according command-line option is set
4088 */
4089
4090 /*
4091 * We need a proper target format
4092 * if target format has been changed by user via GUI import wizard
4093 * or via VBoxManage import command (option --importtovdi)
4094 * then we need properly process such format like ISO
4095 * Because there is no conversion ISO to VDI
4096 */
4097 trgFormat = pSysProps->i_mediumFormatFromExtension(++pszSuff);
4098 if (trgFormat.isNull())
4099 throw setError(E_FAIL, tr("Unsupported medium format for disk image '%s'"), di.strHref.c_str());
4100
4101 hrc = trgFormat->COMGETTER(Name)(bstrFormatName.asOutParam());
4102 if (FAILED(hrc)) throw hrc;
4103
4104 strTrgFormat = Utf8Str(bstrFormatName);
4105
4106 if ( m->optListImport.contains(ImportOptions_ImportToVDI)
4107 && strTrgFormat.compare("RAW", Utf8Str::CaseInsensitive) != 0)
4108 {
4109 /* change the target extension */
4110 strTrgFormat = "vdi";
4111 trgFormat = pSysProps->i_mediumFormatFromExtension(strTrgFormat);
4112 strAbsDstPath.stripSuffix();
4113 strAbsDstPath.append(".");
4114 strAbsDstPath.append(strTrgFormat.c_str());
4115 }
4116
4117 /* Check the capabilities. We need create capabilities. */
4118 lCabs = 0;
4119 com::SafeArray <MediumFormatCapabilities_T> mediumFormatCap;
4120 hrc = trgFormat->COMGETTER(Capabilities)(ComSafeArrayAsOutParam(mediumFormatCap));
4121
4122 if (FAILED(hrc))
4123 throw hrc;
4124
4125 for (ULONG j = 0; j < mediumFormatCap.size(); j++)
4126 lCabs |= mediumFormatCap[j];
4127
4128 if ( !(lCabs & MediumFormatCapabilities_CreateFixed)
4129 && !(lCabs & MediumFormatCapabilities_CreateDynamic) )
4130 throw setError(VBOX_E_NOT_SUPPORTED,
4131 tr("Could not find a valid medium format for the target disk '%s'"),
4132 strAbsDstPath.c_str());
4133 }
4134 else
4135 {
4136 throw setError(VBOX_E_FILE_ERROR,
4137 tr("The target disk '%s' has no extension "),
4138 strAbsDstPath.c_str(), VERR_INVALID_NAME);
4139 }
4140
4141 /*CD/DVD case*/
4142 if (strTrgFormat.compare("RAW", Utf8Str::CaseInsensitive) == 0)
4143 {
4144 try
4145 {
4146 if (fGzipped)
4147 i_importDecompressFile(stack, strSrcFilePath, strAbsDstPath, strSourceOVF.c_str());
4148 else
4149 i_importCopyFile(stack, strSrcFilePath, strAbsDstPath, strSourceOVF.c_str());
4150
4151 ComPtr<IMedium> pTmp;
4152 hrc = mVirtualBox->OpenMedium(Bstr(strAbsDstPath).raw(),
4153 DeviceType_DVD,
4154 AccessMode_ReadWrite,
4155 false,
4156 pTmp.asOutParam());
4157 if (FAILED(hrc))
4158 throw hrc;
4159
4160 IMedium *iM = pTmp;
4161 pTargetMedium = static_cast<Medium*>(iM);
4162 }
4163 catch (HRESULT /*arc*/)
4164 {
4165 throw;
4166 }
4167
4168 /* Advance to the next operation. */
4169 /* operation's weight, as set up with the IProgress originally */
4170 stack.pProgress->SetNextOperation(BstrFmt(tr("Importing virtual disk image '%s'"),
4171 RTPathFilename(strSourceOVF.c_str())).raw(),
4172 di.ulSuggestedSizeMB);
4173 }
4174 else/* HDD case*/
4175 {
4176 /* Create an IMedium object. */
4177 pTargetMedium.createObject();
4178
4179 hrc = pTargetMedium->init(mVirtualBox,
4180 strTrgFormat,
4181 strAbsDstPath,
4182 Guid::Empty /* media registry: none yet */,
4183 DeviceType_HardDisk);
4184 if (FAILED(hrc)) throw hrc;
4185
4186 ComPtr<IProgress> pProgressImport;
4187 /* If strHref is empty we have to create a new file. */
4188 if (strSourceOVF.isEmpty())
4189 {
4190 com::SafeArray<MediumVariant_T> mediumVariant;
4191 mediumVariant.push_back(MediumVariant_Standard);
4192
4193 /* Kick off the creation of a dynamic growing disk image with the given capacity. */
4194 hrc = pTargetMedium->CreateBaseStorage(di.iCapacity / _1M,
4195 ComSafeArrayAsInParam(mediumVariant),
4196 pProgressImport.asOutParam());
4197 if (FAILED(hrc)) throw hrc;
4198
4199 /* Advance to the next operation. */
4200 /* operation's weight, as set up with the IProgress originally */
4201 stack.pProgress->SetNextOperation(BstrFmt(tr("Creating disk image '%s'"),
4202 strAbsDstPath.c_str()).raw(),
4203 di.ulSuggestedSizeMB);
4204 }
4205 else
4206 {
4207 /* We need a proper source format description */
4208 /* Which format to use? */
4209 ComObjPtr<MediumFormat> srcFormat;
4210 hrc = i_findMediumFormatFromDiskImage(di, srcFormat);
4211 if (FAILED(hrc))
4212 throw setError(VBOX_E_NOT_SUPPORTED,
4213 tr("Could not find a valid medium format for the source disk '%s' "
4214 "Check correctness of the image format URL in the OVF description file "
4215 "or extension of the image"),
4216 RTPathFilename(strSourceOVF.c_str()));
4217
4218 /* If gzipped, decompress the GZIP file and save a new file in the target path */
4219 if (fGzipped)
4220 {
4221 Utf8Str strTargetFilePath(strAbsDstPath);
4222 strTargetFilePath.stripFilename();
4223 strTargetFilePath.append(RTPATH_SLASH_STR);
4224 strTargetFilePath.append("temp_");
4225 strTargetFilePath.append(RTPathFilename(strSrcFilePath.c_str()));
4226 strDeleteTemp = strTargetFilePath;
4227
4228 i_importDecompressFile(stack, strSrcFilePath, strTargetFilePath, strSourceOVF.c_str());
4229
4230 /* Correct the source and the target with the actual values */
4231 strSrcFilePath = strTargetFilePath;
4232
4233 /* Open the new source file. */
4234 vrc = RTVfsIoStrmOpenNormal(strSrcFilePath.c_str(), RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN,
4235 &hVfsIosSrc);
4236 if (RT_FAILURE(vrc))
4237 throw setErrorVrc(vrc, tr("Error opening decompressed image file '%s' (%Rrc)"),
4238 strSrcFilePath.c_str(), vrc);
4239 }
4240 else
4241 hVfsIosSrc = i_importOpenSourceFile(stack, strSrcFilePath, strSourceOVF.c_str());
4242
4243 /* Add a read ahead thread to try speed things up with concurrent reads and
4244 writes going on in different threads. */
4245 RTVFSIOSTREAM hVfsIosReadAhead;
4246 vrc = RTVfsCreateReadAheadForIoStream(hVfsIosSrc, 0 /*fFlags*/, 0 /*cBuffers=default*/,
4247 0 /*cbBuffers=default*/, &hVfsIosReadAhead);
4248 RTVfsIoStrmRelease(hVfsIosSrc);
4249 if (RT_FAILURE(vrc))
4250 throw setErrorVrc(vrc, tr("Error initializing read ahead thread for '%s' (%Rrc)"),
4251 strSrcFilePath.c_str(), vrc);
4252
4253 /* Start the source image cloning operation. */
4254 ComObjPtr<Medium> nullParent;
4255 ComObjPtr<Progress> pProgressImportTmp;
4256 hrc = pProgressImportTmp.createObject();
4257 if (FAILED(hrc)) throw hrc;
4258 hrc = pProgressImportTmp->init(mVirtualBox,
4259 static_cast<IAppliance*>(this),
4260 Utf8StrFmt(tr("Importing medium '%s'"), strAbsDstPath.c_str()),
4261 TRUE);
4262 if (FAILED(hrc)) throw hrc;
4263 pProgressImportTmp.queryInterfaceTo(pProgressImport.asOutParam());
4264 /* pProgressImportTmp is in parameter for Medium::i_importFile,
4265 * which is somewhat unusual and might be changed later. */
4266 hrc = pTargetMedium->i_importFile(strSrcFilePath.c_str(),
4267 srcFormat,
4268 MediumVariant_Standard,
4269 hVfsIosReadAhead,
4270 nullParent,
4271 pProgressImportTmp,
4272 true /* aNotify */);
4273 RTVfsIoStrmRelease(hVfsIosReadAhead);
4274 hVfsIosSrc = NIL_RTVFSIOSTREAM;
4275 if (FAILED(hrc))
4276 throw hrc;
4277
4278 /* Advance to the next operation. */
4279 /* operation's weight, as set up with the IProgress originally */
4280 stack.pProgress->SetNextOperation(BstrFmt(tr("Importing virtual disk image '%s'"),
4281 RTPathFilename(strSourceOVF.c_str())).raw(),
4282 di.ulSuggestedSizeMB);
4283 }
4284
4285 /* Now wait for the background import operation to complete; this throws
4286 * HRESULTs on error. */
4287 stack.pProgress->WaitForOtherProgressCompletion(pProgressImport, 0 /* indefinite wait */);
4288
4289 /* The creating/importing has placed the medium in the global
4290 * media registry since the VM isn't created yet. Remove it
4291 * again to let it added to the right registry when the VM
4292 * has been created below. */
4293 pTargetMedium->i_removeRegistry(mVirtualBox->i_getGlobalRegistryId());
4294 }
4295 }
4296 catch (...)
4297 {
4298 if (strDeleteTemp.isNotEmpty())
4299 RTFileDelete(strDeleteTemp.c_str());
4300 throw;
4301 }
4302
4303 /* Make sure the source file is closed. */
4304 if (hVfsIosSrc != NIL_RTVFSIOSTREAM)
4305 RTVfsIoStrmRelease(hVfsIosSrc);
4306
4307 /*
4308 * Delete the temp gunzip result, if any.
4309 */
4310 if (strDeleteTemp.isNotEmpty())
4311 {
4312 vrc = RTFileDelete(strSrcFilePath.c_str());
4313 if (RT_FAILURE(vrc))
4314 setWarning(VBOX_E_FILE_ERROR,
4315 tr("Failed to delete the temporary file '%s' (%Rrc)"), strSrcFilePath.c_str(), vrc);
4316 }
4317 }
4318}
4319
4320/**
4321 * Helper routine to parse the ExtraData Utf8Str for a storage controller's
4322 * value or channel value.
4323 *
4324 * @param aExtraData The ExtraData string with a format of
4325 * 'controller=13;channel=3'.
4326 * @param pszKey The string being looked up, either 'controller' or
4327 * 'channel'.
4328 * @param puVal The integer value of the 'controller=' or 'channel='
4329 * key in the ExtraData string.
4330 * @returns COM status code.
4331 * @throws Nothing.
4332 */
4333static int getStorageControllerDetailsFromStr(const com::Utf8Str &aExtraData, const char *pszKey, uint32_t *puVal)
4334{
4335 size_t posKey = aExtraData.find(pszKey);
4336 if (posKey == Utf8Str::npos)
4337 return VERR_INVALID_PARAMETER;
4338
4339 int vrc = RTStrToUInt32Ex(aExtraData.c_str() + posKey + strlen(pszKey), NULL, 0, puVal);
4340 if (vrc == VWRN_NUMBER_TOO_BIG || vrc == VWRN_NEGATIVE_UNSIGNED)
4341 return VERR_INVALID_PARAMETER;
4342
4343 return vrc;
4344}
4345
4346/**
4347 * Verifies the validity of a storage controller's channel (aka controller port).
4348 *
4349 * @param aStorageControllerType The type of storage controller as idenfitied
4350 * by the enum of type StorageControllerType_T.
4351 * @param uControllerPort The controller port value.
4352 * @param aMaxPortCount The maximum number of ports allowed for this
4353 * storage controller type.
4354 * @returns COM status code.
4355 * @throws Nothing.
4356 */
4357HRESULT Appliance::i_verifyStorageControllerPortValid(const StorageControllerType_T aStorageControllerType,
4358 const uint32_t uControllerPort,
4359 ULONG *aMaxPortCount)
4360{
4361 SystemProperties *pSysProps;
4362 pSysProps = mVirtualBox->i_getSystemProperties();
4363 if (pSysProps == NULL)
4364 return VBOX_E_OBJECT_NOT_FOUND;
4365
4366 StorageBus_T enmStorageBus = StorageBus_Null;
4367 HRESULT vrc = pSysProps->GetStorageBusForStorageControllerType(aStorageControllerType, &enmStorageBus);
4368 if (FAILED(vrc))
4369 return vrc;
4370
4371 vrc = pSysProps->GetMaxPortCountForStorageBus(enmStorageBus, aMaxPortCount);
4372 if (FAILED(vrc))
4373 return vrc;
4374
4375 if (uControllerPort >= *aMaxPortCount)
4376 return E_INVALIDARG;
4377
4378 return S_OK;
4379}
4380
4381/**
4382 * Imports one OVF virtual system (described by the given ovf::VirtualSystem and VirtualSystemDescription)
4383 * into VirtualBox by creating an IMachine instance, which is returned.
4384 *
4385 * This throws HRESULT error codes for anything that goes wrong, in which case the caller must clean
4386 * up any leftovers from this function. For this, the given ImportStack instance has received information
4387 * about what needs cleaning up (to support rollback).
4388 *
4389 * @param vsysThis OVF virtual system (machine) to import.
4390 * @param vsdescThis Matching virtual system description (machine) to import.
4391 * @param[out] pNewMachineRet Newly created machine.
4392 * @param stack Cleanup stack for when this throws.
4393 *
4394 * @throws HRESULT
4395 */
4396void Appliance::i_importMachineGeneric(const ovf::VirtualSystem &vsysThis,
4397 ComObjPtr<VirtualSystemDescription> &vsdescThis,
4398 ComPtr<IMachine> &pNewMachineRet,
4399 ImportStack &stack)
4400{
4401 LogFlowFuncEnter();
4402 HRESULT hrc;
4403
4404 // Get the instance of IGuestOSType which matches our string guest OS type so we
4405 // can use recommended defaults for the new machine where OVF doesn't provide any
4406 ComPtr<IGuestOSType> osType;
4407 hrc = mVirtualBox->GetGuestOSType(Bstr(stack.strOsTypeVBox).raw(), osType.asOutParam());
4408 if (FAILED(hrc)) throw hrc;
4409
4410 /* Create the machine */
4411 SafeArray<BSTR> groups; /* no groups, or maybe one group... */
4412 if (!stack.strPrimaryGroup.isEmpty() && stack.strPrimaryGroup != "/")
4413 Bstr(stack.strPrimaryGroup).detachTo(groups.appendedRaw());
4414 ComPtr<IMachine> pNewMachine;
4415 hrc = mVirtualBox->CreateMachine(Bstr(stack.strSettingsFilename).raw(),
4416 Bstr(stack.strNameVBox).raw(),
4417 ComSafeArrayAsInParam(groups),
4418 Bstr(stack.strOsTypeVBox).raw(),
4419 NULL, /* aCreateFlags */
4420 NULL, /* aCipher */
4421 NULL, /* aPasswordId */
4422 NULL, /* aPassword */
4423 pNewMachine.asOutParam());
4424 if (FAILED(hrc)) throw hrc;
4425 pNewMachineRet = pNewMachine;
4426
4427 // set the description
4428 if (!stack.strDescription.isEmpty())
4429 {
4430 hrc = pNewMachine->COMSETTER(Description)(Bstr(stack.strDescription).raw());
4431 if (FAILED(hrc)) throw hrc;
4432 }
4433
4434 // CPU count
4435 hrc = pNewMachine->COMSETTER(CPUCount)(stack.cCPUs);
4436 if (FAILED(hrc)) throw hrc;
4437
4438 if (stack.fForceHWVirt)
4439 {
4440 hrc = pNewMachine->SetHWVirtExProperty(HWVirtExPropertyType_Enabled, TRUE);
4441 if (FAILED(hrc)) throw hrc;
4442 }
4443
4444 // RAM
4445 hrc = pNewMachine->COMSETTER(MemorySize)(stack.ulMemorySizeMB);
4446 if (FAILED(hrc)) throw hrc;
4447
4448 /* VRAM */
4449 /* Get the recommended VRAM for this guest OS type */
4450 ULONG vramVBox;
4451 hrc = osType->COMGETTER(RecommendedVRAM)(&vramVBox);
4452 if (FAILED(hrc)) throw hrc;
4453
4454 /* Set the VRAM */
4455 ComPtr<IGraphicsAdapter> pGraphicsAdapter;
4456 hrc = pNewMachine->COMGETTER(GraphicsAdapter)(pGraphicsAdapter.asOutParam());
4457 if (FAILED(hrc)) throw hrc;
4458 hrc = pGraphicsAdapter->COMSETTER(VRAMSize)(vramVBox);
4459 if (FAILED(hrc)) throw hrc;
4460
4461 // I/O APIC: Generic OVF has no setting for this. Enable it if we
4462 // import a Windows VM because if if Windows was installed without IOAPIC,
4463 // it will not mind finding an one later on, but if Windows was installed
4464 // _with_ an IOAPIC, it will bluescreen if it's not found
4465 if (!stack.fForceIOAPIC)
4466 {
4467 Bstr bstrFamilyId;
4468 hrc = osType->COMGETTER(FamilyId)(bstrFamilyId.asOutParam());
4469 if (FAILED(hrc)) throw hrc;
4470 if (bstrFamilyId == "Windows")
4471 stack.fForceIOAPIC = true;
4472 }
4473
4474 if (stack.fForceIOAPIC)
4475 {
4476 ComPtr<IBIOSSettings> pBIOSSettings;
4477 hrc = pNewMachine->COMGETTER(BIOSSettings)(pBIOSSettings.asOutParam());
4478 if (FAILED(hrc)) throw hrc;
4479
4480 hrc = pBIOSSettings->COMSETTER(IOAPICEnabled)(TRUE);
4481 if (FAILED(hrc)) throw hrc;
4482 }
4483
4484 if (stack.strFirmwareType.isNotEmpty())
4485 {
4486 FirmwareType_T firmwareType = FirmwareType_BIOS;
4487 if (stack.strFirmwareType.contains("EFI"))
4488 {
4489 if (stack.strFirmwareType.contains("32"))
4490 firmwareType = FirmwareType_EFI32;
4491 if (stack.strFirmwareType.contains("64"))
4492 firmwareType = FirmwareType_EFI64;
4493 else
4494 firmwareType = FirmwareType_EFI;
4495 }
4496 hrc = pNewMachine->COMSETTER(FirmwareType)(firmwareType);
4497 if (FAILED(hrc)) throw hrc;
4498 }
4499
4500 if (!stack.strAudioAdapter.isEmpty())
4501 if (stack.strAudioAdapter.compare("null", Utf8Str::CaseInsensitive) != 0)
4502 {
4503 ComPtr<IAudioSettings> audioSettings;
4504 hrc = pNewMachine->COMGETTER(AudioSettings)(audioSettings.asOutParam());
4505 if (FAILED(hrc)) throw hrc;
4506 uint32_t audio = RTStrToUInt32(stack.strAudioAdapter.c_str()); // should be 0 for AC97
4507 ComPtr<IAudioAdapter> audioAdapter;
4508 hrc = audioSettings->COMGETTER(Adapter)(audioAdapter.asOutParam());
4509 if (FAILED(hrc)) throw hrc;
4510 hrc = audioAdapter->COMSETTER(Enabled)(true);
4511 if (FAILED(hrc)) throw hrc;
4512 hrc = audioAdapter->COMSETTER(AudioController)(static_cast<AudioControllerType_T>(audio));
4513 if (FAILED(hrc)) throw hrc;
4514 }
4515
4516#ifdef VBOX_WITH_USB
4517 /* USB Controller */
4518 if (stack.fUSBEnabled)
4519 {
4520 ComPtr<IUSBController> usbController;
4521 hrc = pNewMachine->AddUSBController(Bstr("OHCI").raw(), USBControllerType_OHCI, usbController.asOutParam());
4522 if (FAILED(hrc)) throw hrc;
4523 }
4524#endif /* VBOX_WITH_USB */
4525
4526 /* Change the network adapters */
4527 uint32_t maxNetworkAdapters = Global::getMaxNetworkAdapters(ChipsetType_PIIX3);
4528
4529 std::list<VirtualSystemDescriptionEntry*> vsdeNW = vsdescThis->i_findByType(VirtualSystemDescriptionType_NetworkAdapter);
4530 if (vsdeNW.empty())
4531 {
4532 /* No network adapters, so we have to disable our default one */
4533 ComPtr<INetworkAdapter> nwVBox;
4534 hrc = pNewMachine->GetNetworkAdapter(0, nwVBox.asOutParam());
4535 if (FAILED(hrc)) throw hrc;
4536 hrc = nwVBox->COMSETTER(Enabled)(false);
4537 if (FAILED(hrc)) throw hrc;
4538 }
4539 else if (vsdeNW.size() > maxNetworkAdapters)
4540 throw setError(VBOX_E_FILE_ERROR,
4541 tr("Too many network adapters: OVF requests %d network adapters, "
4542 "but VirtualBox only supports %d", "", vsdeNW.size()),
4543 vsdeNW.size(), maxNetworkAdapters);
4544 else
4545 {
4546 list<VirtualSystemDescriptionEntry*>::const_iterator nwIt;
4547 size_t a = 0;
4548 for (nwIt = vsdeNW.begin();
4549 nwIt != vsdeNW.end();
4550 ++nwIt, ++a)
4551 {
4552 const VirtualSystemDescriptionEntry* pvsys = *nwIt;
4553
4554 const Utf8Str &nwTypeVBox = pvsys->strVBoxCurrent;
4555 uint32_t tt1 = RTStrToUInt32(nwTypeVBox.c_str());
4556 ComPtr<INetworkAdapter> pNetworkAdapter;
4557 hrc = pNewMachine->GetNetworkAdapter((ULONG)a, pNetworkAdapter.asOutParam());
4558 if (FAILED(hrc)) throw hrc;
4559 /* Enable the network card & set the adapter type */
4560 hrc = pNetworkAdapter->COMSETTER(Enabled)(true);
4561 if (FAILED(hrc)) throw hrc;
4562 hrc = pNetworkAdapter->COMSETTER(AdapterType)(static_cast<NetworkAdapterType_T>(tt1));
4563 if (FAILED(hrc)) throw hrc;
4564
4565 // default is NAT; change to "bridged" if extra conf says so
4566 if (pvsys->strExtraConfigCurrent.endsWith("type=Bridged", Utf8Str::CaseInsensitive))
4567 {
4568 /* Attach to the right interface */
4569 hrc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_Bridged);
4570 if (FAILED(hrc)) throw hrc;
4571 ComPtr<IHost> host;
4572 hrc = mVirtualBox->COMGETTER(Host)(host.asOutParam());
4573 if (FAILED(hrc)) throw hrc;
4574 com::SafeIfaceArray<IHostNetworkInterface> nwInterfaces;
4575 hrc = host->COMGETTER(NetworkInterfaces)(ComSafeArrayAsOutParam(nwInterfaces));
4576 if (FAILED(hrc)) throw hrc;
4577 // We search for the first host network interface which
4578 // is usable for bridged networking
4579 for (size_t j = 0;
4580 j < nwInterfaces.size();
4581 ++j)
4582 {
4583 HostNetworkInterfaceType_T itype;
4584 hrc = nwInterfaces[j]->COMGETTER(InterfaceType)(&itype);
4585 if (FAILED(hrc)) throw hrc;
4586 if (itype == HostNetworkInterfaceType_Bridged)
4587 {
4588 Bstr name;
4589 hrc = nwInterfaces[j]->COMGETTER(Name)(name.asOutParam());
4590 if (FAILED(hrc)) throw hrc;
4591 /* Set the interface name to attach to */
4592 hrc = pNetworkAdapter->COMSETTER(BridgedInterface)(name.raw());
4593 if (FAILED(hrc)) throw hrc;
4594 break;
4595 }
4596 }
4597 }
4598 /* Next test for host only interfaces */
4599 else if (pvsys->strExtraConfigCurrent.endsWith("type=HostOnly", Utf8Str::CaseInsensitive))
4600 {
4601 /* Attach to the right interface */
4602 hrc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_HostOnly);
4603 if (FAILED(hrc)) throw hrc;
4604 ComPtr<IHost> host;
4605 hrc = mVirtualBox->COMGETTER(Host)(host.asOutParam());
4606 if (FAILED(hrc)) throw hrc;
4607 com::SafeIfaceArray<IHostNetworkInterface> nwInterfaces;
4608 hrc = host->COMGETTER(NetworkInterfaces)(ComSafeArrayAsOutParam(nwInterfaces));
4609 if (FAILED(hrc)) throw hrc;
4610 // We search for the first host network interface which
4611 // is usable for host only networking
4612 for (size_t j = 0;
4613 j < nwInterfaces.size();
4614 ++j)
4615 {
4616 HostNetworkInterfaceType_T itype;
4617 hrc = nwInterfaces[j]->COMGETTER(InterfaceType)(&itype);
4618 if (FAILED(hrc)) throw hrc;
4619 if (itype == HostNetworkInterfaceType_HostOnly)
4620 {
4621 Bstr name;
4622 hrc = nwInterfaces[j]->COMGETTER(Name)(name.asOutParam());
4623 if (FAILED(hrc)) throw hrc;
4624 /* Set the interface name to attach to */
4625 hrc = pNetworkAdapter->COMSETTER(HostOnlyInterface)(name.raw());
4626 if (FAILED(hrc)) throw hrc;
4627 break;
4628 }
4629 }
4630 }
4631 /* Next test for internal interfaces */
4632 else if (pvsys->strExtraConfigCurrent.endsWith("type=Internal", Utf8Str::CaseInsensitive))
4633 {
4634 /* Attach to the right interface */
4635 hrc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_Internal);
4636 if (FAILED(hrc)) throw hrc;
4637 }
4638 /* Next test for Generic interfaces */
4639 else if (pvsys->strExtraConfigCurrent.endsWith("type=Generic", Utf8Str::CaseInsensitive))
4640 {
4641 /* Attach to the right interface */
4642 hrc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_Generic);
4643 if (FAILED(hrc)) throw hrc;
4644 }
4645
4646 /* Next test for NAT network interfaces */
4647 else if (pvsys->strExtraConfigCurrent.endsWith("type=NATNetwork", Utf8Str::CaseInsensitive))
4648 {
4649 /* Attach to the right interface */
4650 hrc = pNetworkAdapter->COMSETTER(AttachmentType)(NetworkAttachmentType_NATNetwork);
4651 if (FAILED(hrc)) throw hrc;
4652 com::SafeIfaceArray<INATNetwork> nwNATNetworks;
4653 hrc = mVirtualBox->COMGETTER(NATNetworks)(ComSafeArrayAsOutParam(nwNATNetworks));
4654 if (FAILED(hrc)) throw hrc;
4655 // Pick the first NAT network (if there is any)
4656 if (nwNATNetworks.size())
4657 {
4658 Bstr name;
4659 hrc = nwNATNetworks[0]->COMGETTER(NetworkName)(name.asOutParam());
4660 if (FAILED(hrc)) throw hrc;
4661 /* Set the NAT network name to attach to */
4662 hrc = pNetworkAdapter->COMSETTER(NATNetwork)(name.raw());
4663 if (FAILED(hrc)) throw hrc;
4664 break;
4665 }
4666 }
4667 }
4668 }
4669
4670 // Storage controller IDE
4671 std::list<VirtualSystemDescriptionEntry*> vsdeHDCIDE =
4672 vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskControllerIDE);
4673 /*
4674 * In OVF (at least VMware's version of it), an IDE controller has two ports,
4675 * so VirtualBox's single IDE controller with two channels and two ports each counts as
4676 * two OVF IDE controllers -- so we accept one or two such IDE controllers
4677 */
4678 size_t cIDEControllers = vsdeHDCIDE.size();
4679 if (cIDEControllers > 2)
4680 throw setError(VBOX_E_FILE_ERROR,
4681 tr("Too many IDE controllers in OVF; import facility only supports two"));
4682 if (!vsdeHDCIDE.empty())
4683 {
4684 // one or two IDE controllers present in OVF: add one VirtualBox controller
4685 ComPtr<IStorageController> pController;
4686 hrc = pNewMachine->AddStorageController(Bstr("IDE").raw(), StorageBus_IDE, pController.asOutParam());
4687 if (FAILED(hrc)) throw hrc;
4688
4689 const char *pcszIDEType = vsdeHDCIDE.front()->strVBoxCurrent.c_str();
4690 if (!strcmp(pcszIDEType, "PIIX3"))
4691 hrc = pController->COMSETTER(ControllerType)(StorageControllerType_PIIX3);
4692 else if (!strcmp(pcszIDEType, "PIIX4"))
4693 hrc = pController->COMSETTER(ControllerType)(StorageControllerType_PIIX4);
4694 else if (!strcmp(pcszIDEType, "ICH6"))
4695 hrc = pController->COMSETTER(ControllerType)(StorageControllerType_ICH6);
4696 else
4697 throw setError(VBOX_E_FILE_ERROR,
4698 tr("Invalid IDE controller type \"%s\""),
4699 pcszIDEType);
4700 if (FAILED(hrc)) throw hrc;
4701 }
4702
4703 /* Storage controller SATA */
4704 std::list<VirtualSystemDescriptionEntry*> vsdeHDCSATA =
4705 vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskControllerSATA);
4706 if (vsdeHDCSATA.size() > 1)
4707 throw setError(VBOX_E_FILE_ERROR,
4708 tr("Too many SATA controllers in OVF; import facility only supports one"));
4709 if (!vsdeHDCSATA.empty())
4710 {
4711 ComPtr<IStorageController> pController;
4712 const Utf8Str &hdcVBox = vsdeHDCSATA.front()->strVBoxCurrent;
4713 if (hdcVBox == "AHCI")
4714 {
4715 hrc = pNewMachine->AddStorageController(Bstr("SATA").raw(), StorageBus_SATA, pController.asOutParam());
4716 if (FAILED(hrc)) throw hrc;
4717 }
4718 else
4719 throw setError(VBOX_E_FILE_ERROR, tr("Invalid SATA controller type \"%s\""), hdcVBox.c_str());
4720 }
4721
4722 /* Storage controller SCSI */
4723 std::list<VirtualSystemDescriptionEntry*> vsdeHDCSCSI =
4724 vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskControllerSCSI);
4725 if (vsdeHDCSCSI.size() > 1)
4726 throw setError(VBOX_E_FILE_ERROR,
4727 tr("Too many SCSI controllers in OVF; import facility only supports one"));
4728 if (!vsdeHDCSCSI.empty())
4729 {
4730 ComPtr<IStorageController> pController;
4731 Utf8Str strName("SCSI");
4732 StorageBus_T busType = StorageBus_SCSI;
4733 StorageControllerType_T controllerType;
4734 const Utf8Str &hdcVBox = vsdeHDCSCSI.front()->strVBoxCurrent;
4735 if (hdcVBox == "LsiLogic")
4736 controllerType = StorageControllerType_LsiLogic;
4737 else if (hdcVBox == "LsiLogicSas")
4738 {
4739 // OVF treats LsiLogicSas as a SCSI controller but VBox considers it a class of its own
4740 strName = "SAS";
4741 busType = StorageBus_SAS;
4742 controllerType = StorageControllerType_LsiLogicSas;
4743 }
4744 else if (hdcVBox == "BusLogic")
4745 controllerType = StorageControllerType_BusLogic;
4746 else
4747 throw setError(VBOX_E_FILE_ERROR, tr("Invalid SCSI controller type \"%s\""), hdcVBox.c_str());
4748
4749 hrc = pNewMachine->AddStorageController(Bstr(strName).raw(), busType, pController.asOutParam());
4750 if (FAILED(hrc)) throw hrc;
4751 hrc = pController->COMSETTER(ControllerType)(controllerType);
4752 if (FAILED(hrc)) throw hrc;
4753 }
4754
4755 /* Storage controller SAS */
4756 std::list<VirtualSystemDescriptionEntry*> vsdeHDCSAS =
4757 vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskControllerSAS);
4758 if (vsdeHDCSAS.size() > 1)
4759 throw setError(VBOX_E_FILE_ERROR,
4760 tr("Too many SAS controllers in OVF; import facility only supports one"));
4761 if (!vsdeHDCSAS.empty())
4762 {
4763 ComPtr<IStorageController> pController;
4764 hrc = pNewMachine->AddStorageController(Bstr(L"SAS").raw(), StorageBus_SAS, pController.asOutParam());
4765 if (FAILED(hrc)) throw hrc;
4766 hrc = pController->COMSETTER(ControllerType)(StorageControllerType_LsiLogicSas);
4767 if (FAILED(hrc)) throw hrc;
4768 }
4769
4770
4771 /* Storage controller VirtioSCSI */
4772 std::list<VirtualSystemDescriptionEntry*> vsdeHDCVirtioSCSI =
4773 vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskControllerVirtioSCSI);
4774 if (vsdeHDCVirtioSCSI.size() > 1)
4775 throw setError(VBOX_E_FILE_ERROR,
4776 tr("Too many VirtioSCSI controllers in OVF; import facility only supports one"));
4777 if (!vsdeHDCVirtioSCSI.empty())
4778 {
4779 ComPtr<IStorageController> pController;
4780 Utf8Str strName("VirtioSCSI");
4781 const Utf8Str &hdcVBox = vsdeHDCVirtioSCSI.front()->strVBoxCurrent;
4782 if (hdcVBox == "VirtioSCSI")
4783 {
4784 hrc = pNewMachine->AddStorageController(Bstr(strName).raw(), StorageBus_VirtioSCSI, pController.asOutParam());
4785 if (FAILED(hrc)) throw hrc;
4786
4787 hrc = pController->COMSETTER(ControllerType)(StorageControllerType_VirtioSCSI);
4788 if (FAILED(hrc)) throw hrc;
4789 }
4790 else
4791 throw setError(VBOX_E_FILE_ERROR, tr("Invalid VirtioSCSI controller type \"%s\""), hdcVBox.c_str());
4792 }
4793
4794 /* Now its time to register the machine before we add any storage devices */
4795 hrc = mVirtualBox->RegisterMachine(pNewMachine);
4796 if (FAILED(hrc)) throw hrc;
4797
4798 // store new machine for roll-back in case of errors
4799 Bstr bstrNewMachineId;
4800 hrc = pNewMachine->COMGETTER(Id)(bstrNewMachineId.asOutParam());
4801 if (FAILED(hrc)) throw hrc;
4802 Guid uuidNewMachine(bstrNewMachineId);
4803 m->llGuidsMachinesCreated.push_back(uuidNewMachine);
4804
4805 // Add floppies and CD-ROMs to the appropriate controllers.
4806 std::list<VirtualSystemDescriptionEntry*> vsdeFloppy = vsdescThis->i_findByType(VirtualSystemDescriptionType_Floppy);
4807 if (vsdeFloppy.size() > 1)
4808 throw setError(VBOX_E_FILE_ERROR,
4809 tr("Too many floppy controllers in OVF; import facility only supports one"));
4810 std::list<VirtualSystemDescriptionEntry*> vsdeCDROM = vsdescThis->i_findByType(VirtualSystemDescriptionType_CDROM);
4811 if ( !vsdeFloppy.empty()
4812 || !vsdeCDROM.empty()
4813 )
4814 {
4815 // If there's an error here we need to close the session, so
4816 // we need another try/catch block.
4817
4818 try
4819 {
4820 // to attach things we need to open a session for the new machine
4821 hrc = pNewMachine->LockMachine(stack.pSession, LockType_Write);
4822 if (FAILED(hrc)) throw hrc;
4823 stack.fSessionOpen = true;
4824
4825 ComPtr<IMachine> sMachine;
4826 hrc = stack.pSession->COMGETTER(Machine)(sMachine.asOutParam());
4827 if (FAILED(hrc)) throw hrc;
4828
4829 // floppy first
4830 if (vsdeFloppy.size() == 1)
4831 {
4832 ComPtr<IStorageController> pController;
4833 hrc = sMachine->AddStorageController(Bstr("Floppy").raw(), StorageBus_Floppy, pController.asOutParam());
4834 if (FAILED(hrc)) throw hrc;
4835
4836 Bstr bstrName;
4837 hrc = pController->COMGETTER(Name)(bstrName.asOutParam());
4838 if (FAILED(hrc)) throw hrc;
4839
4840 // this is for rollback later
4841 MyHardDiskAttachment mhda;
4842 mhda.pMachine = pNewMachine;
4843 mhda.controllerName = bstrName;
4844 mhda.lControllerPort = 0;
4845 mhda.lDevice = 0;
4846
4847 Log(("Attaching floppy\n"));
4848
4849 hrc = sMachine->AttachDevice(Bstr(mhda.controllerName).raw(),
4850 mhda.lControllerPort,
4851 mhda.lDevice,
4852 DeviceType_Floppy,
4853 NULL);
4854 if (FAILED(hrc)) throw hrc;
4855
4856 stack.llHardDiskAttachments.push_back(mhda);
4857 }
4858
4859 hrc = sMachine->SaveSettings();
4860 if (FAILED(hrc)) throw hrc;
4861
4862 // only now that we're done with all storage devices, close the session
4863 hrc = stack.pSession->UnlockMachine();
4864 if (FAILED(hrc)) throw hrc;
4865 stack.fSessionOpen = false;
4866 }
4867 catch(HRESULT aRC)
4868 {
4869 com::ErrorInfo info;
4870
4871 if (stack.fSessionOpen)
4872 stack.pSession->UnlockMachine();
4873
4874 if (info.isFullAvailable())
4875 throw setError(aRC, Utf8Str(info.getText()).c_str());
4876 else
4877 throw setError(aRC, tr("Unknown error during OVF import"));
4878 }
4879 }
4880
4881 // create the storage devices & connect them to the appropriate controllers
4882 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskImage);
4883 if (!avsdeHDs.empty())
4884 {
4885 // If there's an error here we need to close the session, so
4886 // we need another try/catch block.
4887 try
4888 {
4889#ifdef LOG_ENABLED
4890 if (LogIsEnabled())
4891 {
4892 size_t i = 0;
4893 for (list<VirtualSystemDescriptionEntry*>::const_iterator itHD = avsdeHDs.begin();
4894 itHD != avsdeHDs.end(); ++itHD, i++)
4895 Log(("avsdeHDs[%zu]: strRef=%s strOvf=%s\n", i, (*itHD)->strRef.c_str(), (*itHD)->strOvf.c_str()));
4896 i = 0;
4897 for (ovf::DiskImagesMap::const_iterator itDisk = stack.mapDisks.begin(); itDisk != stack.mapDisks.end(); ++itDisk)
4898 Log(("mapDisks[%zu]: strDiskId=%s strHref=%s\n",
4899 i, itDisk->second.strDiskId.c_str(), itDisk->second.strHref.c_str()));
4900
4901 }
4902#endif
4903
4904 // to attach things we need to open a session for the new machine
4905 hrc = pNewMachine->LockMachine(stack.pSession, LockType_Write);
4906 if (FAILED(hrc)) throw hrc;
4907 stack.fSessionOpen = true;
4908
4909 /* get VM name from virtual system description. Only one record is possible (size of list is equal 1). */
4910 std::list<VirtualSystemDescriptionEntry*> vmName = vsdescThis->i_findByType(VirtualSystemDescriptionType_Name);
4911 std::list<VirtualSystemDescriptionEntry*>::iterator vmNameIt = vmName.begin();
4912 VirtualSystemDescriptionEntry* vmNameEntry = *vmNameIt;
4913
4914
4915 ovf::DiskImagesMap::const_iterator oit = stack.mapDisks.begin();
4916 std::set<RTCString> disksResolvedNames;
4917
4918 uint32_t cImportedDisks = 0;
4919
4920 while (oit != stack.mapDisks.end() && cImportedDisks != avsdeHDs.size())
4921 {
4922/** @todo r=bird: Most of the code here is duplicated in the other machine
4923 * import method, factor out. */
4924 ovf::DiskImage diCurrent = oit->second;
4925
4926 Log(("diCurrent.strDiskId=%s diCurrent.strHref=%s\n", diCurrent.strDiskId.c_str(), diCurrent.strHref.c_str()));
4927 /* Iterate over all given images of the virtual system
4928 * description. We need to find the target image path,
4929 * which could be changed by the user. */
4930 VirtualSystemDescriptionEntry *vsdeTargetHD = NULL;
4931 for (list<VirtualSystemDescriptionEntry*>::const_iterator itHD = avsdeHDs.begin();
4932 itHD != avsdeHDs.end();
4933 ++itHD)
4934 {
4935 VirtualSystemDescriptionEntry *vsdeHD = *itHD;
4936 if (vsdeHD->strRef == diCurrent.strDiskId)
4937 {
4938 vsdeTargetHD = vsdeHD;
4939 break;
4940 }
4941 }
4942 if (!vsdeTargetHD)
4943 {
4944 /* possible case if an image belongs to other virtual system (OVF package with multiple VMs inside) */
4945 Log1Warning(("OVA/OVF import: Disk image %s was missed during import of VM %s\n",
4946 oit->first.c_str(), vmNameEntry->strOvf.c_str()));
4947 NOREF(vmNameEntry);
4948 ++oit;
4949 continue;
4950 }
4951
4952 //diCurrent.strDiskId contains the image identifier (e.g. "vmdisk1"), which should exist
4953 //in the virtual system's images map under that ID and also in the global images map
4954 ovf::VirtualDisksMap::const_iterator itVDisk = vsysThis.mapVirtualDisks.find(diCurrent.strDiskId);
4955 if (itVDisk == vsysThis.mapVirtualDisks.end())
4956 throw setError(E_FAIL,
4957 tr("Internal inconsistency looking up disk image '%s'"),
4958 diCurrent.strHref.c_str());
4959
4960 /*
4961 * preliminary check availability of the image
4962 * This step is useful if image is placed in the OVA (TAR) package
4963 */
4964 if (stack.hVfsFssOva != NIL_RTVFSFSSTREAM)
4965 {
4966 /* It means that we possibly have imported the storage earlier on the previous loop steps*/
4967 std::set<RTCString>::const_iterator h = disksResolvedNames.find(diCurrent.strHref);
4968 if (h != disksResolvedNames.end())
4969 {
4970 /* Yes, image name was found, we can skip it*/
4971 ++oit;
4972 continue;
4973 }
4974l_skipped:
4975 hrc = i_preCheckImageAvailability(stack);
4976 if (SUCCEEDED(hrc))
4977 {
4978 /* current opened file isn't the same as passed one */
4979 if (RTStrICmp(diCurrent.strHref.c_str(), stack.pszOvaLookAheadName) != 0)
4980 {
4981 /* availableImage contains the image file reference (e.g. "disk1.vmdk"), which should
4982 * exist in the global images map.
4983 * And find the image from the OVF's disk list */
4984 ovf::DiskImagesMap::const_iterator itDiskImage;
4985 for (itDiskImage = stack.mapDisks.begin();
4986 itDiskImage != stack.mapDisks.end();
4987 itDiskImage++)
4988 if (itDiskImage->second.strHref.compare(stack.pszOvaLookAheadName,
4989 Utf8Str::CaseInsensitive) == 0)
4990 break;
4991 if (itDiskImage == stack.mapDisks.end())
4992 {
4993 LogFunc(("Skipping '%s'\n", stack.pszOvaLookAheadName));
4994 RTVfsIoStrmRelease(stack.claimOvaLookAHead());
4995 goto l_skipped;
4996 }
4997
4998 /* replace with a new found image */
4999 diCurrent = *(&itDiskImage->second);
5000
5001 /*
5002 * Again iterate over all given images of the virtual system
5003 * description using the found image
5004 */
5005 for (list<VirtualSystemDescriptionEntry*>::const_iterator itHD = avsdeHDs.begin();
5006 itHD != avsdeHDs.end();
5007 ++itHD)
5008 {
5009 VirtualSystemDescriptionEntry *vsdeHD = *itHD;
5010 if (vsdeHD->strRef == diCurrent.strDiskId)
5011 {
5012 vsdeTargetHD = vsdeHD;
5013 break;
5014 }
5015 }
5016
5017 /*
5018 * in this case it's an error because something is wrong with the OVF description file.
5019 * May be VBox imports OVA package with wrong file sequence inside the archive.
5020 */
5021 if (!vsdeTargetHD)
5022 throw setError(E_FAIL,
5023 tr("Internal inconsistency looking up disk image '%s'"),
5024 diCurrent.strHref.c_str());
5025
5026 itVDisk = vsysThis.mapVirtualDisks.find(diCurrent.strDiskId);
5027 if (itVDisk == vsysThis.mapVirtualDisks.end())
5028 throw setError(E_FAIL,
5029 tr("Internal inconsistency looking up disk image '%s'"),
5030 diCurrent.strHref.c_str());
5031 }
5032 else
5033 {
5034 ++oit;
5035 }
5036 }
5037 else
5038 {
5039 ++oit;
5040 continue;
5041 }
5042 }
5043 else
5044 {
5045 /* just continue with normal files */
5046 ++oit;
5047 }
5048
5049 /* very important to store image name for the next checks */
5050 disksResolvedNames.insert(diCurrent.strHref);
5051////// end of duplicated code.
5052 const ovf::VirtualDisk &ovfVdisk = itVDisk->second;
5053
5054 ComObjPtr<Medium> pTargetMedium;
5055 if (stack.locInfo.storageType == VFSType_Cloud)
5056 {
5057 /* We have already all disks prepared (converted and registered in the VBox)
5058 * and in the correct place (VM machine folder).
5059 * so what is needed is to get the disk uuid from VirtualDisk::strDiskId
5060 * and find the Medium object with this uuid.
5061 * next just attach the Medium object to new VM.
5062 * VirtualDisk::strDiskId is filled in the */
5063
5064 Guid id(ovfVdisk.strDiskId);
5065 hrc = mVirtualBox->i_findHardDiskById(id, false, &pTargetMedium);
5066 if (FAILED(hrc))
5067 throw hrc;
5068 }
5069 else
5070 {
5071 i_importOneDiskImage(diCurrent,
5072 vsdeTargetHD->strVBoxCurrent,
5073 pTargetMedium,
5074 stack);
5075 }
5076
5077 // now use the new uuid to attach the medium to our new machine
5078 ComPtr<IMachine> sMachine;
5079 hrc = stack.pSession->COMGETTER(Machine)(sMachine.asOutParam());
5080 if (FAILED(hrc))
5081 throw hrc;
5082
5083 // this is for rollback later
5084 MyHardDiskAttachment mhda;
5085 mhda.pMachine = pNewMachine;
5086
5087 // find the hard disk controller to which we should attach
5088 ovf::HardDiskController hdc;
5089
5090 /*
5091 * Before importing the virtual hard disk found above (diCurrent/vsdeTargetHD) first
5092 * check if the user requested to change either the controller it is to be attached
5093 * to and/or the controller port (aka 'channel') on the controller.
5094 */
5095 if ( !vsdeTargetHD->strExtraConfigCurrent.isEmpty()
5096 && vsdeTargetHD->strExtraConfigSuggested != vsdeTargetHD->strExtraConfigCurrent)
5097 {
5098 int vrc;
5099 uint32_t uTargetControllerIndex;
5100 vrc = getStorageControllerDetailsFromStr(vsdeTargetHD->strExtraConfigCurrent, "controller=",
5101 &uTargetControllerIndex);
5102 if (RT_FAILURE(vrc))
5103 throw setError(E_FAIL,
5104 tr("Target controller value invalid or missing: '%s'"),
5105 vsdeTargetHD->strExtraConfigCurrent.c_str());
5106
5107 uint32_t uNewControllerPortValue;
5108 vrc = getStorageControllerDetailsFromStr(vsdeTargetHD->strExtraConfigCurrent, "channel=",
5109 &uNewControllerPortValue);
5110 if (RT_FAILURE(vrc))
5111 throw setError(E_FAIL,
5112 tr("Target controller port ('channel=') invalid or missing: '%s'"),
5113 vsdeTargetHD->strExtraConfigCurrent.c_str());
5114
5115 const VirtualSystemDescriptionEntry *vsdeTargetController;
5116 vsdeTargetController = vsdescThis->i_findByIndex(uTargetControllerIndex);
5117 if (!vsdeTargetController)
5118 throw setError(E_FAIL,
5119 tr("Failed to find storage controller '%u' in the System Description list"),
5120 uTargetControllerIndex);
5121
5122 hdc = (*vsysThis.mapControllers.find(vsdeTargetController->strRef.c_str())).second;
5123
5124 StorageControllerType_T hdStorageControllerType = StorageControllerType_Null;
5125 switch (hdc.system)
5126 {
5127 case ovf::HardDiskController::IDE:
5128 hdStorageControllerType = StorageControllerType_PIIX3;
5129 break;
5130 case ovf::HardDiskController::SATA:
5131 hdStorageControllerType = StorageControllerType_IntelAhci;
5132 break;
5133 case ovf::HardDiskController::SCSI:
5134 {
5135 if (hdc.strControllerType.compare("lsilogicsas")==0)
5136 hdStorageControllerType = StorageControllerType_LsiLogicSas;
5137 else
5138 hdStorageControllerType = StorageControllerType_LsiLogic;
5139 break;
5140 }
5141 case ovf::HardDiskController::VIRTIOSCSI:
5142 hdStorageControllerType = StorageControllerType_VirtioSCSI;
5143 break;
5144 default:
5145 throw setError(E_FAIL,
5146 tr("Invalid hard disk contoller type: '%d'"),
5147 hdc.system);
5148 break;
5149 }
5150
5151 ULONG ulMaxPorts;
5152 hrc = i_verifyStorageControllerPortValid(hdStorageControllerType, uNewControllerPortValue, &ulMaxPorts);
5153 if (FAILED(hrc))
5154 {
5155 if (hrc == E_INVALIDARG)
5156 {
5157 const char *pcszSCType = Global::stringifyStorageControllerType(hdStorageControllerType);
5158 throw setError(E_INVALIDARG,
5159 tr("Illegal channel: '%u'. For %s controllers the valid values are "
5160 "0 to %lu (inclusive).\n"), uNewControllerPortValue, pcszSCType, ulMaxPorts-1);
5161 }
5162 else
5163 throw hrc;
5164 }
5165
5166 unconst(ovfVdisk.ulAddressOnParent) = uNewControllerPortValue;
5167 }
5168 else
5169 hdc = (*vsysThis.mapControllers.find(ovfVdisk.strIdController)).second;
5170
5171
5172 i_convertDiskAttachmentValues(hdc,
5173 ovfVdisk.ulAddressOnParent,
5174 mhda.controllerName,
5175 mhda.lControllerPort,
5176 mhda.lDevice);
5177
5178 Log(("Attaching disk %s to port %d on device %d\n",
5179 vsdeTargetHD->strVBoxCurrent.c_str(), mhda.lControllerPort, mhda.lDevice));
5180
5181 DeviceType_T devType = DeviceType_Null;
5182 hrc = pTargetMedium->COMGETTER(DeviceType)(&devType);
5183 if (FAILED(hrc))
5184 throw hrc;
5185
5186 hrc = sMachine->AttachDevice(Bstr(mhda.controllerName).raw(),// name
5187 mhda.lControllerPort, // long controllerPort
5188 mhda.lDevice, // long device
5189 devType, // DeviceType_T type
5190 pTargetMedium);
5191 if (FAILED(hrc))
5192 throw hrc;
5193
5194 stack.llHardDiskAttachments.push_back(mhda);
5195
5196 hrc = sMachine->SaveSettings();
5197 if (FAILED(hrc))
5198 throw hrc;
5199
5200 ++cImportedDisks;
5201
5202 } // end while(oit != stack.mapDisks.end())
5203
5204 /*
5205 * quantity of the imported disks isn't equal to the size of the avsdeHDs list.
5206 */
5207 if(cImportedDisks < avsdeHDs.size())
5208 {
5209 Log1Warning(("Not all disk images were imported for VM %s. Check OVF description file.",
5210 vmNameEntry->strOvf.c_str()));
5211 }
5212
5213 // only now that we're done with all disks, close the session
5214 hrc = stack.pSession->UnlockMachine();
5215 if (FAILED(hrc))
5216 throw hrc;
5217 stack.fSessionOpen = false;
5218 }
5219 catch(HRESULT aRC)
5220 {
5221 com::ErrorInfo info;
5222 if (stack.fSessionOpen)
5223 stack.pSession->UnlockMachine();
5224
5225 if (info.isFullAvailable())
5226 throw setError(aRC, Utf8Str(info.getText()).c_str());
5227 else
5228 throw setError(aRC, tr("Unknown error during OVF import"));
5229 }
5230 }
5231 LogFlowFuncLeave();
5232}
5233
5234/**
5235 * Imports one OVF virtual system (described by a vbox:Machine tag represented by the given config
5236 * structure) into VirtualBox by creating an IMachine instance, which is returned.
5237 *
5238 * This throws HRESULT error codes for anything that goes wrong, in which case the caller must clean
5239 * up any leftovers from this function. For this, the given ImportStack instance has received information
5240 * about what needs cleaning up (to support rollback).
5241 *
5242 * The machine config stored in the settings::MachineConfigFile structure contains the UUIDs of
5243 * the disk attachments used by the machine when it was exported. We also add vbox:uuid attributes
5244 * to the OVF disks sections so we can look them up. While importing these UUIDs into a second host
5245 * will most probably work, reimporting them into the same host will cause conflicts, so we always
5246 * generate new ones on import. This involves the following:
5247 *
5248 * 1) Scan the machine config for disk attachments.
5249 *
5250 * 2) For each disk attachment found, look up the OVF disk image from the disk references section
5251 * and import the disk into VirtualBox, which creates a new UUID for it. In the machine config,
5252 * replace the old UUID with the new one.
5253 *
5254 * 3) Change the machine config according to the OVF virtual system descriptions, in case the
5255 * caller has modified them using setFinalValues().
5256 *
5257 * 4) Create the VirtualBox machine with the modfified machine config.
5258 *
5259 * @param vsdescThis
5260 * @param pReturnNewMachine
5261 * @param stack
5262 */
5263void Appliance::i_importVBoxMachine(ComObjPtr<VirtualSystemDescription> &vsdescThis,
5264 ComPtr<IMachine> &pReturnNewMachine,
5265 ImportStack &stack)
5266{
5267 LogFlowFuncEnter();
5268 Assert(vsdescThis->m->pConfig);
5269
5270 HRESULT hrc = S_OK;
5271
5272 settings::MachineConfigFile &config = *vsdescThis->m->pConfig;
5273
5274 /*
5275 * step 1): modify machine config according to OVF config, in case the user
5276 * has modified them using setFinalValues()
5277 */
5278
5279 /* OS Type */
5280 config.machineUserData.strOsType = stack.strOsTypeVBox;
5281 /* Groups */
5282 if (stack.strPrimaryGroup.isEmpty() || stack.strPrimaryGroup == "/")
5283 {
5284 config.machineUserData.llGroups.clear();
5285 config.machineUserData.llGroups.push_back("/");
5286 }
5287 else
5288 {
5289 /* Replace the primary group if there is one, otherwise add it. */
5290 if (config.machineUserData.llGroups.size())
5291 config.machineUserData.llGroups.pop_front();
5292 config.machineUserData.llGroups.push_front(stack.strPrimaryGroup);
5293 }
5294 /* Description */
5295 config.machineUserData.strDescription = stack.strDescription;
5296 /* CPU count & extented attributes */
5297 config.hardwareMachine.cCPUs = stack.cCPUs;
5298 if (stack.fForceIOAPIC)
5299 config.hardwareMachine.fHardwareVirt = true;
5300 if (stack.fForceIOAPIC)
5301 config.hardwareMachine.biosSettings.fIOAPICEnabled = true;
5302 /* RAM size */
5303 config.hardwareMachine.ulMemorySizeMB = stack.ulMemorySizeMB;
5304
5305/*
5306 <const name="HardDiskControllerIDE" value="14" />
5307 <const name="HardDiskControllerSATA" value="15" />
5308 <const name="HardDiskControllerSCSI" value="16" />
5309 <const name="HardDiskControllerSAS" value="17" />
5310 <const name="HardDiskControllerVirtioSCSI" value="60" />
5311*/
5312
5313#ifdef VBOX_WITH_USB
5314 /* USB controller */
5315 if (stack.fUSBEnabled)
5316 {
5317 /** @todo r=klaus add support for arbitrary USB controller types, this can't handle
5318 * multiple controllers due to its design anyway */
5319 /* Usually the OHCI controller is enabled already, need to check. But
5320 * do this only if there is no xHCI controller. */
5321 bool fOHCIEnabled = false;
5322 bool fXHCIEnabled = false;
5323 settings::USBControllerList &llUSBControllers = config.hardwareMachine.usbSettings.llUSBControllers;
5324 settings::USBControllerList::iterator it;
5325 for (it = llUSBControllers.begin(); it != llUSBControllers.end(); ++it)
5326 {
5327 if (it->enmType == USBControllerType_OHCI)
5328 fOHCIEnabled = true;
5329 if (it->enmType == USBControllerType_XHCI)
5330 fXHCIEnabled = true;
5331 }
5332
5333 if (!fXHCIEnabled && !fOHCIEnabled)
5334 {
5335 settings::USBController ctrl;
5336 ctrl.strName = "OHCI";
5337 ctrl.enmType = USBControllerType_OHCI;
5338
5339 llUSBControllers.push_back(ctrl);
5340 }
5341 }
5342 else
5343 config.hardwareMachine.usbSettings.llUSBControllers.clear();
5344#endif
5345 /* Audio adapter */
5346 if (stack.strAudioAdapter.isNotEmpty())
5347 {
5348 config.hardwareMachine.audioAdapter.fEnabled = true;
5349 config.hardwareMachine.audioAdapter.controllerType = (AudioControllerType_T)stack.strAudioAdapter.toUInt32();
5350 }
5351 else
5352 config.hardwareMachine.audioAdapter.fEnabled = false;
5353 /* Network adapter */
5354 settings::NetworkAdaptersList &llNetworkAdapters = config.hardwareMachine.llNetworkAdapters;
5355 /* First disable all network cards, they will be enabled below again. */
5356 settings::NetworkAdaptersList::iterator it1;
5357 bool fKeepAllMACs = m->optListImport.contains(ImportOptions_KeepAllMACs);
5358 bool fKeepNATMACs = m->optListImport.contains(ImportOptions_KeepNATMACs);
5359 for (it1 = llNetworkAdapters.begin(); it1 != llNetworkAdapters.end(); ++it1)
5360 {
5361 it1->fEnabled = false;
5362 if (!( fKeepAllMACs
5363 || (fKeepNATMACs && it1->mode == NetworkAttachmentType_NAT)
5364 || (fKeepNATMACs && it1->mode == NetworkAttachmentType_NATNetwork)))
5365 /* Force generation of new MAC address below. */
5366 it1->strMACAddress.setNull();
5367 }
5368 /* Now iterate over all network entries. */
5369 std::list<VirtualSystemDescriptionEntry*> avsdeNWs = vsdescThis->i_findByType(VirtualSystemDescriptionType_NetworkAdapter);
5370 if (!avsdeNWs.empty())
5371 {
5372 /* Iterate through all network adapter entries and search for the
5373 * corresponding one in the machine config. If one is found, configure
5374 * it based on the user settings. */
5375 list<VirtualSystemDescriptionEntry*>::const_iterator itNW;
5376 for (itNW = avsdeNWs.begin();
5377 itNW != avsdeNWs.end();
5378 ++itNW)
5379 {
5380 VirtualSystemDescriptionEntry *vsdeNW = *itNW;
5381 if ( vsdeNW->strExtraConfigCurrent.startsWith("slot=", Utf8Str::CaseInsensitive)
5382 && vsdeNW->strExtraConfigCurrent.length() > 6)
5383 {
5384 uint32_t iSlot = vsdeNW->strExtraConfigCurrent.substr(5).toUInt32();
5385 /* Iterate through all network adapters in the machine config. */
5386 for (it1 = llNetworkAdapters.begin();
5387 it1 != llNetworkAdapters.end();
5388 ++it1)
5389 {
5390 /* Compare the slots. */
5391 if (it1->ulSlot == iSlot)
5392 {
5393 it1->fEnabled = true;
5394 if (it1->strMACAddress.isEmpty())
5395 Host::i_generateMACAddress(it1->strMACAddress);
5396 it1->type = (NetworkAdapterType_T)vsdeNW->strVBoxCurrent.toUInt32();
5397 break;
5398 }
5399 }
5400 }
5401 }
5402 }
5403
5404 /* Floppy controller */
5405 bool fFloppy = vsdescThis->i_findByType(VirtualSystemDescriptionType_Floppy).size() > 0;
5406 /* DVD controller */
5407 bool fDVD = vsdescThis->i_findByType(VirtualSystemDescriptionType_CDROM).size() > 0;
5408 /* Iterate over all storage controller check the attachments and remove
5409 * them when necessary. Also detect broken configs with more than one
5410 * attachment. Old VirtualBox versions (prior to 3.2.10) had all disk
5411 * attachments pointing to the last hard disk image, which causes import
5412 * failures. A long fixed bug, however the OVF files are long lived. */
5413 settings::StorageControllersList &llControllers = config.hardwareMachine.storage.llStorageControllers;
5414 uint32_t cDisks = 0;
5415 bool fInconsistent = false;
5416 bool fRepairDuplicate = false;
5417 settings::StorageControllersList::iterator it3;
5418 for (it3 = llControllers.begin();
5419 it3 != llControllers.end();
5420 ++it3)
5421 {
5422 Guid hdUuid;
5423 settings::AttachedDevicesList &llAttachments = it3->llAttachedDevices;
5424 settings::AttachedDevicesList::iterator it4 = llAttachments.begin();
5425 while (it4 != llAttachments.end())
5426 {
5427 if ( ( !fDVD
5428 && it4->deviceType == DeviceType_DVD)
5429 ||
5430 ( !fFloppy
5431 && it4->deviceType == DeviceType_Floppy))
5432 {
5433 it4 = llAttachments.erase(it4);
5434 continue;
5435 }
5436 else if (it4->deviceType == DeviceType_HardDisk)
5437 {
5438 const Guid &thisUuid = it4->uuid;
5439 cDisks++;
5440 if (cDisks == 1)
5441 {
5442 if (hdUuid.isZero())
5443 hdUuid = thisUuid;
5444 else
5445 fInconsistent = true;
5446 }
5447 else
5448 {
5449 if (thisUuid.isZero())
5450 fInconsistent = true;
5451 else if (thisUuid == hdUuid)
5452 fRepairDuplicate = true;
5453 }
5454 }
5455 ++it4;
5456 }
5457 }
5458 /* paranoia... */
5459 if (fInconsistent || cDisks == 1)
5460 fRepairDuplicate = false;
5461
5462 /*
5463 * step 2: scan the machine config for media attachments
5464 */
5465 /* get VM name from virtual system description. Only one record is possible (size of list is equal 1). */
5466 std::list<VirtualSystemDescriptionEntry*> vmName = vsdescThis->i_findByType(VirtualSystemDescriptionType_Name);
5467 std::list<VirtualSystemDescriptionEntry*>::iterator vmNameIt = vmName.begin();
5468 VirtualSystemDescriptionEntry* vmNameEntry = *vmNameIt;
5469
5470 /* Get all hard disk descriptions. */
5471 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskImage);
5472 std::list<VirtualSystemDescriptionEntry*>::iterator avsdeHDsIt = avsdeHDs.begin();
5473 /* paranoia - if there is no 1:1 match do not try to repair. */
5474 if (cDisks != avsdeHDs.size())
5475 fRepairDuplicate = false;
5476
5477 // there must be an image in the OVF disk structs with the same UUID
5478
5479 ovf::DiskImagesMap::const_iterator oit = stack.mapDisks.begin();
5480 std::set<RTCString> disksResolvedNames;
5481
5482 uint32_t cImportedDisks = 0;
5483
5484 while (oit != stack.mapDisks.end() && cImportedDisks != avsdeHDs.size())
5485 {
5486/** @todo r=bird: Most of the code here is duplicated in the other machine
5487 * import method, factor out. */
5488 ovf::DiskImage diCurrent = oit->second;
5489
5490 Log(("diCurrent.strDiskId=%s diCurrent.strHref=%s\n", diCurrent.strDiskId.c_str(), diCurrent.strHref.c_str()));
5491
5492 /* Iterate over all given disk images of the virtual system
5493 * disks description. We need to find the target disk path,
5494 * which could be changed by the user. */
5495 VirtualSystemDescriptionEntry *vsdeTargetHD = NULL;
5496 for (list<VirtualSystemDescriptionEntry*>::const_iterator itHD = avsdeHDs.begin();
5497 itHD != avsdeHDs.end();
5498 ++itHD)
5499 {
5500 VirtualSystemDescriptionEntry *vsdeHD = *itHD;
5501 if (vsdeHD->strRef == oit->first)
5502 {
5503 vsdeTargetHD = vsdeHD;
5504 break;
5505 }
5506 }
5507 if (!vsdeTargetHD)
5508 {
5509 /* possible case if a disk image belongs to other virtual system (OVF package with multiple VMs inside) */
5510 Log1Warning(("OVA/OVF import: Disk image %s was missed during import of VM %s\n",
5511 oit->first.c_str(), vmNameEntry->strOvf.c_str()));
5512 NOREF(vmNameEntry);
5513 ++oit;
5514 continue;
5515 }
5516
5517 /*
5518 * preliminary check availability of the image
5519 * This step is useful if image is placed in the OVA (TAR) package
5520 */
5521 if (stack.hVfsFssOva != NIL_RTVFSFSSTREAM)
5522 {
5523 /* It means that we possibly have imported the storage earlier on a previous loop step. */
5524 std::set<RTCString>::const_iterator h = disksResolvedNames.find(diCurrent.strHref);
5525 if (h != disksResolvedNames.end())
5526 {
5527 /* Yes, disk name was found, we can skip it*/
5528 ++oit;
5529 continue;
5530 }
5531l_skipped:
5532 hrc = i_preCheckImageAvailability(stack);
5533 if (SUCCEEDED(hrc))
5534 {
5535 /* current opened file isn't the same as passed one */
5536 if (RTStrICmp(diCurrent.strHref.c_str(), stack.pszOvaLookAheadName) != 0)
5537 {
5538 // availableImage contains the disk identifier (e.g. "vmdisk1"), which should exist
5539 // in the virtual system's disks map under that ID and also in the global images map
5540 // and find the disk from the OVF's disk list
5541 ovf::DiskImagesMap::const_iterator itDiskImage;
5542 for (itDiskImage = stack.mapDisks.begin();
5543 itDiskImage != stack.mapDisks.end();
5544 itDiskImage++)
5545 if (itDiskImage->second.strHref.compare(stack.pszOvaLookAheadName,
5546 Utf8Str::CaseInsensitive) == 0)
5547 break;
5548 if (itDiskImage == stack.mapDisks.end())
5549 {
5550 LogFunc(("Skipping '%s'\n", stack.pszOvaLookAheadName));
5551 RTVfsIoStrmRelease(stack.claimOvaLookAHead());
5552 goto l_skipped;
5553 }
5554 //throw setError(E_FAIL,
5555 // tr("Internal inconsistency looking up disk image '%s'. "
5556 // "Check compliance OVA package structure and file names "
5557 // "references in the section <References> in the OVF file."),
5558 // stack.pszOvaLookAheadName);
5559
5560 /* replace with a new found disk image */
5561 diCurrent = *(&itDiskImage->second);
5562
5563 /*
5564 * Again iterate over all given disk images of the virtual system
5565 * disks description using the found disk image
5566 */
5567 vsdeTargetHD = NULL;
5568 for (list<VirtualSystemDescriptionEntry*>::const_iterator itHD = avsdeHDs.begin();
5569 itHD != avsdeHDs.end();
5570 ++itHD)
5571 {
5572 VirtualSystemDescriptionEntry *vsdeHD = *itHD;
5573 if (vsdeHD->strRef == diCurrent.strDiskId)
5574 {
5575 vsdeTargetHD = vsdeHD;
5576 break;
5577 }
5578 }
5579
5580 /*
5581 * in this case it's an error because something is wrong with the OVF description file.
5582 * May be VBox imports OVA package with wrong file sequence inside the archive.
5583 */
5584 if (!vsdeTargetHD)
5585 throw setError(E_FAIL,
5586 tr("Internal inconsistency looking up disk image '%s'"),
5587 diCurrent.strHref.c_str());
5588 }
5589 else
5590 {
5591 ++oit;
5592 }
5593 }
5594 else
5595 {
5596 ++oit;
5597 continue;
5598 }
5599 }
5600 else
5601 {
5602 /* just continue with normal files*/
5603 ++oit;
5604 }
5605
5606 /* Important! to store disk name for the next checks */
5607 disksResolvedNames.insert(diCurrent.strHref);
5608////// end of duplicated code.
5609 // there must be an image in the OVF disk structs with the same UUID
5610 bool fFound = false;
5611 Utf8Str strUuid;
5612
5613 /*
5614 * Before importing the virtual hard disk found above (diCurrent/vsdeTargetHD) first
5615 * check if the user requested to change either the controller it is to be attached
5616 * to and/or the controller port (aka 'channel') on the controller.
5617 */
5618 if ( !vsdeTargetHD->strExtraConfigCurrent.isEmpty()
5619 && vsdeTargetHD->strExtraConfigSuggested != vsdeTargetHD->strExtraConfigCurrent)
5620 {
5621 /*
5622 * First, we examine the extra configuration values for this vdisk:
5623 * vsdeTargetHD->strExtraConfigSuggested
5624 * vsdeTargetHD->strExtraConfigCurrent
5625 * in order to extract both the "before" and "after" storage controller and port
5626 * details. The strExtraConfigSuggested string contains the current controller
5627 * and port the vdisk is attached to and is populated by Appliance::interpret()
5628 * when processing the OVF data; it is in the following format:
5629 * 'controller=12;channel=0' (the 'channel=' label for the controller port is
5630 * historical and is documented as such in the SDK so can't be changed). The
5631 * strExtraConfigSuggested string contains the target controller and port specified
5632 * by the user and it has the same format. The 'controller=' value is not a
5633 * controller-ID but rather it is the index for the corresponding storage controller
5634 * in the array of VirtualSystemDescriptionEntry entries.
5635 */
5636 int vrc;
5637 uint32_t uOrigControllerIndex;
5638 vrc = getStorageControllerDetailsFromStr(vsdeTargetHD->strExtraConfigSuggested, "controller=", &uOrigControllerIndex);
5639 if (RT_FAILURE(vrc))
5640 throw setError(E_FAIL,
5641 tr("Original controller value invalid or missing: '%s'"),
5642 vsdeTargetHD->strExtraConfigSuggested.c_str());
5643
5644 uint32_t uTargetControllerIndex;
5645 vrc = getStorageControllerDetailsFromStr(vsdeTargetHD->strExtraConfigCurrent, "controller=", &uTargetControllerIndex);
5646 if (RT_FAILURE(vrc))
5647 throw setError(E_FAIL,
5648 tr("Target controller value invalid or missing: '%s'"),
5649 vsdeTargetHD->strExtraConfigCurrent.c_str());
5650
5651 uint32_t uOrigControllerPortValue;
5652 vrc = getStorageControllerDetailsFromStr(vsdeTargetHD->strExtraConfigSuggested, "channel=",
5653 &uOrigControllerPortValue);
5654 if (RT_FAILURE(vrc))
5655 throw setError(E_FAIL,
5656 tr("Original controller port ('channel=') invalid or missing: '%s'"),
5657 vsdeTargetHD->strExtraConfigSuggested.c_str());
5658
5659 uint32_t uNewControllerPortValue;
5660 vrc = getStorageControllerDetailsFromStr(vsdeTargetHD->strExtraConfigCurrent, "channel=", &uNewControllerPortValue);
5661 if (RT_FAILURE(vrc))
5662 throw setError(E_FAIL,
5663 tr("Target controller port ('channel=') invalid or missing: '%s'"),
5664 vsdeTargetHD->strExtraConfigCurrent.c_str());
5665
5666 /*
5667 * Second, now that we have the storage controller indexes we locate the corresponding
5668 * VirtualSystemDescriptionEntry (VSDE) for both storage controllers which contain
5669 * identifying details which will be needed later when walking the list of storage
5670 * controllers.
5671 */
5672 const VirtualSystemDescriptionEntry *vsdeOrigController;
5673 vsdeOrigController = vsdescThis->i_findByIndex(uOrigControllerIndex);
5674 if (!vsdeOrigController)
5675 throw setError(E_FAIL,
5676 tr("Failed to find storage controller '%u' in the System Description list"),
5677 uOrigControllerIndex);
5678
5679 const VirtualSystemDescriptionEntry *vsdeTargetController;
5680 vsdeTargetController = vsdescThis->i_findByIndex(uTargetControllerIndex);
5681 if (!vsdeTargetController)
5682 throw setError(E_FAIL,
5683 tr("Failed to find storage controller '%u' in the System Description list"),
5684 uTargetControllerIndex);
5685
5686 /*
5687 * Third, grab the UUID of the current vdisk so we can identify which device
5688 * attached to the original storage controller needs to be updated (channel) and/or
5689 * removed.
5690 */
5691 ovf::DiskImagesMap::const_iterator itDiskImageMap = stack.mapDisks.find(vsdeTargetHD->strRef);
5692 if (itDiskImageMap == stack.mapDisks.end())
5693 throw setError(E_FAIL,
5694 tr("Failed to find virtual disk '%s' in DiskImagesMap"),
5695 vsdeTargetHD->strVBoxCurrent.c_str());
5696 const ovf::DiskImage &targetDiskImage = itDiskImageMap->second;
5697 Utf8Str strTargetDiskUuid = targetDiskImage.uuidVBox;;
5698
5699 /*
5700 * Fourth, walk the attached devices of the original storage controller to find the
5701 * current vdisk and update the controller port (aka channel) value if necessary and
5702 * also remove the vdisk from this controller if needed.
5703 *
5704 * A short note on the choice of which items to compare when determining the type of
5705 * storage controller here and below in the vdisk addition scenario:
5706 * + The VirtualSystemDescriptionEntry 'strOvf' field is populated from the OVF
5707 * data which can contain a value like 'vmware.sata.ahci' if created by VMWare so
5708 * it isn't a reliable choice.
5709 * + The settings::StorageController 'strName' field can have varying content based
5710 * on the version of the settings file, e.g. 'IDE Controller' vs. 'IDE' so it
5711 * isn't a reliable choice. Further, this field can contain 'SATA' whereas
5712 * 'AHCI' is used in 'strOvf' and 'strVBoxSuggested'.
5713 * + The VirtualSystemDescriptionEntry 'strVBoxSuggested' field is populated by
5714 * Appliance::interpret()->VirtualSystemDescription::i_addEntry() and is thus
5715 * under VBox's control and has a fixed format and predictable content.
5716 */
5717 bool fDiskRemoved = false;
5718 settings::AttachedDevice originalAttachedDevice;
5719 settings::StorageControllersList::iterator itSCL;
5720 for (itSCL = config.hardwareMachine.storage.llStorageControllers.begin();
5721 itSCL != config.hardwareMachine.storage.llStorageControllers.end();
5722 ++itSCL)
5723 {
5724 settings::StorageController &SC = *itSCL;
5725 const char *pcszSCType = Global::stringifyStorageControllerType(SC.controllerType);
5726
5727 /* There can only be one storage controller of each type in the OVF data. */
5728 if (!vsdeOrigController->strVBoxSuggested.compare(pcszSCType, Utf8Str::CaseInsensitive))
5729 {
5730 settings::AttachedDevicesList::iterator itAD;
5731 for (itAD = SC.llAttachedDevices.begin();
5732 itAD != SC.llAttachedDevices.end();
5733 ++itAD)
5734 {
5735 settings::AttachedDevice &AD = *itAD;
5736
5737 if (AD.uuid.toString() == strTargetDiskUuid)
5738 {
5739 ULONG ulMaxPorts;
5740 hrc = i_verifyStorageControllerPortValid(SC.controllerType, uNewControllerPortValue, &ulMaxPorts);
5741 if (FAILED(hrc))
5742 {
5743 if (hrc == E_INVALIDARG)
5744 throw setError(E_INVALIDARG,
5745 tr("Illegal channel: '%u'. For %s controllers the valid values are "
5746 "0 to %lu (inclusive).\n"), uNewControllerPortValue, pcszSCType, ulMaxPorts-1);
5747 else
5748 throw hrc;
5749 }
5750
5751 if (uOrigControllerPortValue != uNewControllerPortValue)
5752 {
5753 AD.lPort = (int32_t)uNewControllerPortValue;
5754 }
5755 if (uOrigControllerIndex != uTargetControllerIndex)
5756 {
5757 LogFunc(("Removing vdisk '%s' (uuid = %RTuuid) from the %s storage controller.\n",
5758 vsdeTargetHD->strVBoxCurrent.c_str(),
5759 itAD->uuid.raw(),
5760 SC.strName.c_str()));
5761 originalAttachedDevice = AD;
5762 SC.llAttachedDevices.erase(itAD);
5763 fDiskRemoved = true;
5764 }
5765 }
5766 }
5767 }
5768 }
5769
5770 /*
5771 * Fifth, if we are moving the vdisk to a different controller and not just changing
5772 * the channel then we walk the attached devices of the target controller and check
5773 * for conflicts before adding the vdisk detached/removed above.
5774 */
5775 bool fDiskAdded = false;
5776 if (fDiskRemoved)
5777 {
5778 for (itSCL = config.hardwareMachine.storage.llStorageControllers.begin();
5779 itSCL != config.hardwareMachine.storage.llStorageControllers.end();
5780 ++itSCL)
5781 {
5782 settings::StorageController &SC = *itSCL;
5783 const char *pcszSCType = Global::stringifyStorageControllerType(SC.controllerType);
5784
5785 /* There can only be one storage controller of each type in the OVF data. */
5786 if (!vsdeTargetController->strVBoxSuggested.compare(pcszSCType, Utf8Str::CaseInsensitive))
5787 {
5788 settings::AttachedDevicesList::iterator itAD;
5789 for (itAD = SC.llAttachedDevices.begin();
5790 itAD != SC.llAttachedDevices.end();
5791 ++itAD)
5792 {
5793 settings::AttachedDevice &AD = *itAD;
5794 if ( AD.lDevice == originalAttachedDevice.lDevice
5795 && AD.lPort == originalAttachedDevice.lPort)
5796 throw setError(E_FAIL,
5797 tr("Device of type '%s' already attached to the %s controller at this "
5798 "port/channel (%d)."),
5799 Global::stringifyDeviceType(AD.deviceType), pcszSCType, AD.lPort);
5800 }
5801
5802 LogFunc(("Adding vdisk '%s' (uuid = %RTuuid) to the %s storage controller\n",
5803 vsdeTargetHD->strVBoxCurrent.c_str(),
5804 originalAttachedDevice.uuid.raw(),
5805 SC.strName.c_str()));
5806 SC.llAttachedDevices.push_back(originalAttachedDevice);
5807 fDiskAdded = true;
5808 }
5809 }
5810
5811 if (!fDiskAdded)
5812 throw setError(E_FAIL,
5813 tr("Failed to add disk '%s' (uuid=%RTuuid) to the %s storage controller."),
5814 vsdeTargetHD->strVBoxCurrent.c_str(),
5815 originalAttachedDevice.uuid.raw(),
5816 vsdeTargetController->strVBoxSuggested.c_str());
5817 }
5818
5819 /*
5820 * Sixth, update the machine settings since we've changed the storage controller
5821 * and/or controller port for this vdisk.
5822 */
5823 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
5824 mVirtualBox->i_saveSettings();
5825 vboxLock.release();
5826 }
5827
5828 // for each storage controller...
5829 for (settings::StorageControllersList::iterator sit = config.hardwareMachine.storage.llStorageControllers.begin();
5830 sit != config.hardwareMachine.storage.llStorageControllers.end();
5831 ++sit)
5832 {
5833 settings::StorageController &sc = *sit;
5834
5835 // for each medium attachment to this controller...
5836 for (settings::AttachedDevicesList::iterator dit = sc.llAttachedDevices.begin();
5837 dit != sc.llAttachedDevices.end();
5838 ++dit)
5839 {
5840 settings::AttachedDevice &d = *dit;
5841
5842 if (d.uuid.isZero())
5843 // empty DVD and floppy media
5844 continue;
5845
5846 // When repairing a broken VirtualBox xml config section (written
5847 // by VirtualBox versions earlier than 3.2.10) assume the disks
5848 // show up in the same order as in the OVF description.
5849 if (fRepairDuplicate)
5850 {
5851 VirtualSystemDescriptionEntry *vsdeHD = *avsdeHDsIt;
5852 ovf::DiskImagesMap::const_iterator itDiskImage = stack.mapDisks.find(vsdeHD->strRef);
5853 if (itDiskImage != stack.mapDisks.end())
5854 {
5855 const ovf::DiskImage &di = itDiskImage->second;
5856 d.uuid = Guid(di.uuidVBox);
5857 }
5858 ++avsdeHDsIt;
5859 }
5860
5861 // convert the Guid to string
5862 strUuid = d.uuid.toString();
5863
5864 if (diCurrent.uuidVBox != strUuid)
5865 {
5866 continue;
5867 }
5868
5869 /*
5870 * step 3: import disk
5871 */
5872 ComObjPtr<Medium> pTargetMedium;
5873 i_importOneDiskImage(diCurrent,
5874 vsdeTargetHD->strVBoxCurrent,
5875 pTargetMedium,
5876 stack);
5877
5878 // ... and replace the old UUID in the machine config with the one of
5879 // the imported disk that was just created
5880 Bstr hdId;
5881 hrc = pTargetMedium->COMGETTER(Id)(hdId.asOutParam());
5882 if (FAILED(hrc)) throw hrc;
5883
5884 /*
5885 * 1. saving original UUID for restoring in case of failure.
5886 * 2. replacement of original UUID by new UUID in the current VM config (settings::MachineConfigFile).
5887 */
5888 {
5889 hrc = stack.saveOriginalUUIDOfAttachedDevice(d, Utf8Str(hdId));
5890 d.uuid = hdId;
5891 }
5892
5893 fFound = true;
5894 break;
5895 } // for (settings::AttachedDevicesList::const_iterator dit = sc.llAttachedDevices.begin();
5896 } // for (settings::StorageControllersList::const_iterator sit = config.hardwareMachine.storage.llStorageControllers.begin();
5897
5898 // no disk with such a UUID found:
5899 if (!fFound)
5900 throw setError(E_FAIL,
5901 tr("<vbox:Machine> element in OVF contains a medium attachment for the disk image %s "
5902 "but the OVF describes no such image"),
5903 strUuid.c_str());
5904
5905 ++cImportedDisks;
5906
5907 }// while(oit != stack.mapDisks.end())
5908
5909
5910 /*
5911 * quantity of the imported disks isn't equal to the size of the avsdeHDs list.
5912 */
5913 if(cImportedDisks < avsdeHDs.size())
5914 {
5915 Log1Warning(("Not all disk images were imported for VM %s. Check OVF description file.",
5916 vmNameEntry->strOvf.c_str()));
5917 }
5918
5919 /*
5920 * step 4): create the machine and have it import the config
5921 */
5922
5923 ComObjPtr<Machine> pNewMachine;
5924 hrc = pNewMachine.createObject();
5925 if (FAILED(hrc)) throw hrc;
5926
5927 // this magic constructor fills the new machine object with the MachineConfig
5928 // instance that we created from the vbox:Machine
5929 hrc = pNewMachine->init(mVirtualBox,
5930 stack.strNameVBox,// name from OVF preparations; can be suffixed to avoid duplicates
5931 stack.strSettingsFilename,
5932 config); // the whole machine config
5933 if (FAILED(hrc)) throw hrc;
5934
5935 pReturnNewMachine = ComPtr<IMachine>(pNewMachine);
5936
5937 // and register it
5938 hrc = mVirtualBox->RegisterMachine(pNewMachine);
5939 if (FAILED(hrc)) throw hrc;
5940
5941 // store new machine for roll-back in case of errors
5942 Bstr bstrNewMachineId;
5943 hrc = pNewMachine->COMGETTER(Id)(bstrNewMachineId.asOutParam());
5944 if (FAILED(hrc)) throw hrc;
5945 m->llGuidsMachinesCreated.push_back(Guid(bstrNewMachineId));
5946
5947 LogFlowFuncLeave();
5948}
5949
5950/**
5951 * @throws HRESULT errors.
5952 */
5953void Appliance::i_importMachines(ImportStack &stack)
5954{
5955 // this is safe to access because this thread only gets started
5956 const ovf::OVFReader &reader = *m->pReader;
5957
5958 // create a session for the machine + disks we manipulate below
5959 HRESULT hrc = stack.pSession.createInprocObject(CLSID_Session);
5960 ComAssertComRCThrowRC(hrc);
5961
5962 list<ovf::VirtualSystem>::const_iterator it;
5963 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it1;
5964 /* Iterate through all virtual systems of that appliance */
5965 size_t i = 0;
5966 for (it = reader.m_llVirtualSystems.begin(), it1 = m->virtualSystemDescriptions.begin();
5967 it != reader.m_llVirtualSystems.end() && it1 != m->virtualSystemDescriptions.end();
5968 ++it, ++it1, ++i)
5969 {
5970 const ovf::VirtualSystem &vsysThis = *it;
5971 ComObjPtr<VirtualSystemDescription> vsdescThis = (*it1);
5972
5973 // there are two ways in which we can create a vbox machine from OVF:
5974 // -- either this OVF was written by vbox 3.2 or later, in which case there is a <vbox:Machine> element
5975 // in the <VirtualSystem>; then the VirtualSystemDescription::Data has a settings::MachineConfigFile
5976 // with all the machine config pretty-parsed;
5977 // -- or this is an OVF from an older vbox or an external source, and then we need to translate the
5978 // VirtualSystemDescriptionEntry and do import work
5979
5980 // Even for the vbox:Machine case, there are a number of configuration items that will be taken from
5981 // the OVF because otherwise the "override import parameters" mechanism in the GUI won't work.
5982
5983 // VM name
5984 std::list<VirtualSystemDescriptionEntry*> vsdeName = vsdescThis->i_findByType(VirtualSystemDescriptionType_Name);
5985 if (vsdeName.size() < 1)
5986 throw setError(VBOX_E_FILE_ERROR,
5987 tr("Missing VM name"));
5988 stack.strNameVBox = vsdeName.front()->strVBoxCurrent;
5989
5990 // Primary group, which is entirely optional.
5991 stack.strPrimaryGroup.setNull();
5992 std::list<VirtualSystemDescriptionEntry*> vsdePrimaryGroup = vsdescThis->i_findByType(VirtualSystemDescriptionType_PrimaryGroup);
5993 if (vsdePrimaryGroup.size() >= 1)
5994 {
5995 stack.strPrimaryGroup = vsdePrimaryGroup.front()->strVBoxCurrent;
5996 if (stack.strPrimaryGroup.isEmpty())
5997 stack.strPrimaryGroup = "/";
5998 }
5999
6000 // Draw the right conclusions from the (possibly modified) VM settings
6001 // file name and base folder. If the VM settings file name is modified,
6002 // it takes precedence, otherwise it is recreated from the base folder
6003 // and the primary group.
6004 stack.strSettingsFilename.setNull();
6005 std::list<VirtualSystemDescriptionEntry*> vsdeSettingsFile = vsdescThis->i_findByType(VirtualSystemDescriptionType_SettingsFile);
6006 if (vsdeSettingsFile.size() >= 1)
6007 {
6008 VirtualSystemDescriptionEntry *vsdeSF1 = vsdeSettingsFile.front();
6009 if (vsdeSF1->strVBoxCurrent != vsdeSF1->strVBoxSuggested)
6010 stack.strSettingsFilename = vsdeSF1->strVBoxCurrent;
6011 }
6012 if (stack.strSettingsFilename.isEmpty())
6013 {
6014 Utf8Str strBaseFolder;
6015 std::list<VirtualSystemDescriptionEntry*> vsdeBaseFolder = vsdescThis->i_findByType(VirtualSystemDescriptionType_BaseFolder);
6016 if (vsdeBaseFolder.size() >= 1)
6017 strBaseFolder = vsdeBaseFolder.front()->strVBoxCurrent;
6018 Bstr bstrSettingsFilename;
6019 hrc = mVirtualBox->ComposeMachineFilename(Bstr(stack.strNameVBox).raw(),
6020 Bstr(stack.strPrimaryGroup).raw(),
6021 NULL /* aCreateFlags */,
6022 Bstr(strBaseFolder).raw(),
6023 bstrSettingsFilename.asOutParam());
6024 if (FAILED(hrc)) throw hrc;
6025 stack.strSettingsFilename = bstrSettingsFilename;
6026 }
6027
6028 // Determine the machine folder from the settings file.
6029 LogFunc(("i=%zu strName=%s strSettingsFilename=%s\n", i, stack.strNameVBox.c_str(), stack.strSettingsFilename.c_str()));
6030 stack.strMachineFolder = stack.strSettingsFilename;
6031 stack.strMachineFolder.stripFilename();
6032
6033 // guest OS type
6034 std::list<VirtualSystemDescriptionEntry*> vsdeOS;
6035 vsdeOS = vsdescThis->i_findByType(VirtualSystemDescriptionType_OS);
6036 if (vsdeOS.size() < 1)
6037 throw setError(VBOX_E_FILE_ERROR,
6038 tr("Missing guest OS type"));
6039 stack.strOsTypeVBox = vsdeOS.front()->strVBoxCurrent;
6040
6041 // Firmware
6042 std::list<VirtualSystemDescriptionEntry*> firmware = vsdescThis->i_findByType(VirtualSystemDescriptionType_BootingFirmware);
6043 if (firmware.size() != 1)
6044 stack.strFirmwareType = "BIOS";//try default BIOS type
6045 else
6046 stack.strFirmwareType = firmware.front()->strVBoxCurrent;
6047
6048 // CPU count
6049 std::list<VirtualSystemDescriptionEntry*> vsdeCPU = vsdescThis->i_findByType(VirtualSystemDescriptionType_CPU);
6050 if (vsdeCPU.size() != 1)
6051 throw setError(VBOX_E_FILE_ERROR, tr("CPU count missing"));
6052
6053 stack.cCPUs = vsdeCPU.front()->strVBoxCurrent.toUInt32();
6054 // We need HWVirt & IO-APIC if more than one CPU is requested
6055 if (stack.cCPUs > 1)
6056 {
6057 stack.fForceHWVirt = true;
6058 stack.fForceIOAPIC = true;
6059 }
6060
6061 // RAM
6062 std::list<VirtualSystemDescriptionEntry*> vsdeRAM = vsdescThis->i_findByType(VirtualSystemDescriptionType_Memory);
6063 if (vsdeRAM.size() != 1)
6064 throw setError(VBOX_E_FILE_ERROR, tr("RAM size missing"));
6065 uint64_t ullMemorySizeMB = vsdeRAM.front()->strVBoxCurrent.toUInt64() / _1M;
6066 stack.ulMemorySizeMB = (uint32_t)ullMemorySizeMB;
6067
6068#ifdef VBOX_WITH_USB
6069 // USB controller
6070 std::list<VirtualSystemDescriptionEntry*> vsdeUSBController =
6071 vsdescThis->i_findByType(VirtualSystemDescriptionType_USBController);
6072 // USB support is enabled if there's at least one such entry; to disable USB support,
6073 // the type of the USB item would have been changed to "ignore"
6074 stack.fUSBEnabled = !vsdeUSBController.empty();
6075#endif
6076 // audio adapter
6077 std::list<VirtualSystemDescriptionEntry*> vsdeAudioAdapter =
6078 vsdescThis->i_findByType(VirtualSystemDescriptionType_SoundCard);
6079 /** @todo we support one audio adapter only */
6080 if (!vsdeAudioAdapter.empty())
6081 stack.strAudioAdapter = vsdeAudioAdapter.front()->strVBoxCurrent;
6082
6083 // for the description of the new machine, always use the OVF entry, the user may have changed it in the import config
6084 std::list<VirtualSystemDescriptionEntry*> vsdeDescription =
6085 vsdescThis->i_findByType(VirtualSystemDescriptionType_Description);
6086 if (!vsdeDescription.empty())
6087 stack.strDescription = vsdeDescription.front()->strVBoxCurrent;
6088
6089 // import vbox:machine or OVF now
6090 ComPtr<IMachine> pNewMachine; /** @todo pointless */
6091 if (vsdescThis->m->pConfig)
6092 // vbox:Machine config
6093 i_importVBoxMachine(vsdescThis, pNewMachine, stack);
6094 else
6095 // generic OVF config
6096 i_importMachineGeneric(vsysThis, vsdescThis, pNewMachine, stack);
6097
6098 } // for (it = pAppliance->m->llVirtualSystems.begin() ...
6099}
6100
6101HRESULT Appliance::ImportStack::saveOriginalUUIDOfAttachedDevice(settings::AttachedDevice &device,
6102 const Utf8Str &newlyUuid)
6103{
6104 HRESULT hrc = S_OK;
6105
6106 /* save for restoring */
6107 mapNewUUIDsToOriginalUUIDs.insert(std::make_pair(newlyUuid, device.uuid.toString()));
6108
6109 return hrc;
6110}
6111
6112HRESULT Appliance::ImportStack::restoreOriginalUUIDOfAttachedDevice(settings::MachineConfigFile *config)
6113{
6114 settings::StorageControllersList &llControllers = config->hardwareMachine.storage.llStorageControllers;
6115 settings::StorageControllersList::iterator itscl;
6116 for (itscl = llControllers.begin();
6117 itscl != llControllers.end();
6118 ++itscl)
6119 {
6120 settings::AttachedDevicesList &llAttachments = itscl->llAttachedDevices;
6121 settings::AttachedDevicesList::iterator itadl = llAttachments.begin();
6122 while (itadl != llAttachments.end())
6123 {
6124 std::map<Utf8Str , Utf8Str>::iterator it =
6125 mapNewUUIDsToOriginalUUIDs.find(itadl->uuid.toString());
6126 if(it!=mapNewUUIDsToOriginalUUIDs.end())
6127 {
6128 Utf8Str uuidOriginal = it->second;
6129 itadl->uuid = Guid(uuidOriginal);
6130 mapNewUUIDsToOriginalUUIDs.erase(it->first);
6131 }
6132 ++itadl;
6133 }
6134 }
6135
6136 return S_OK;
6137}
6138
6139/**
6140 * @throws Nothing
6141 */
6142RTVFSIOSTREAM Appliance::ImportStack::claimOvaLookAHead(void)
6143{
6144 RTVFSIOSTREAM hVfsIos = this->hVfsIosOvaLookAhead;
6145 this->hVfsIosOvaLookAhead = NIL_RTVFSIOSTREAM;
6146 /* We don't free the name since it may be referenced in error messages and such. */
6147 return hVfsIos;
6148}
6149
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