VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/ApplianceImpl.cpp@ 82968

Last change on this file since 82968 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 64.6 KB
Line 
1/* $Id: ApplianceImpl.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IAppliance and IVirtualSystem COM class implementations.
4 */
5
6/*
7 * Copyright (C) 2008-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_MAIN_APPLIANCE
23#include <iprt/path.h>
24#include <iprt/cpp/path.h>
25#include <iprt/cpp/utils.h>
26#include <VBox/com/array.h>
27#include <map>
28
29#include "ApplianceImpl.h"
30#include "VFSExplorerImpl.h"
31#include "VirtualBoxImpl.h"
32#include "GuestOSTypeImpl.h"
33#include "Global.h"
34#include "ProgressImpl.h"
35#include "MachineImpl.h"
36#include "SystemPropertiesImpl.h"
37#include "AutoCaller.h"
38#include "LoggingNew.h"
39#include "CertificateImpl.h"
40
41#include "ApplianceImplPrivate.h"
42
43using namespace std;
44
45
46/*********************************************************************************************************************************
47* Global Variables *
48*********************************************************************************************************************************/
49static const char * const g_pszISOURI = "http://www.ecma-international.org/publications/standards/Ecma-119.htm";
50static const char * const g_pszVMDKStreamURI = "http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized";
51static const char * const g_pszVMDKSparseURI = "http://www.vmware.com/specifications/vmdk.html#sparse";
52static const char * const g_pszVMDKCompressedURI = "http://www.vmware.com/specifications/vmdk.html#compressed";
53static const char * const g_pszVMDKCompressedURI2 = "http://www.vmware.com/interfaces/specifications/vmdk.html#compressed";
54static const char * const g_pszrVHDURI = "http://go.microsoft.com/fwlink/?LinkId=137171";
55static char g_szIsoBackend[128];
56static char g_szVmdkBackend[128];
57static char g_szVhdBackend[128];
58/** Set after the g_szXxxxBackend variables has been initialized. */
59static bool volatile g_fInitializedBackendNames = false;
60
61static struct
62{
63 const char *pszUri, *pszBackend;
64} const g_aUriToBackend[] =
65{
66 { g_pszISOURI, g_szIsoBackend },
67 { g_pszVMDKStreamURI, g_szVmdkBackend },
68 { g_pszVMDKSparseURI, g_szVmdkBackend },
69 { g_pszVMDKCompressedURI, g_szVmdkBackend },
70 { g_pszVMDKCompressedURI2, g_szVmdkBackend },
71 { g_pszrVHDURI, g_szVhdBackend },
72};
73
74static std::map<Utf8Str, Utf8Str> supportedStandardsURI;
75
76static struct
77{
78 ovf::CIMOSType_T cim;
79 VBOXOSTYPE osType;
80} const g_aOsTypes[] =
81{
82 { ovf::CIMOSType_CIMOS_Unknown, VBOXOSTYPE_Unknown },
83 { ovf::CIMOSType_CIMOS_OS2, VBOXOSTYPE_OS2 },
84 { ovf::CIMOSType_CIMOS_OS2, VBOXOSTYPE_OS2Warp3 },
85 { ovf::CIMOSType_CIMOS_OS2, VBOXOSTYPE_OS2Warp4 },
86 { ovf::CIMOSType_CIMOS_OS2, VBOXOSTYPE_OS2Warp45 },
87 { ovf::CIMOSType_CIMOS_MSDOS, VBOXOSTYPE_DOS },
88 { ovf::CIMOSType_CIMOS_WIN3x, VBOXOSTYPE_Win31 },
89 { ovf::CIMOSType_CIMOS_WIN95, VBOXOSTYPE_Win95 },
90 { ovf::CIMOSType_CIMOS_WIN98, VBOXOSTYPE_Win98 },
91 { ovf::CIMOSType_CIMOS_WINNT, VBOXOSTYPE_WinNT },
92 { ovf::CIMOSType_CIMOS_WINNT, VBOXOSTYPE_WinNT4 },
93 { ovf::CIMOSType_CIMOS_WINNT, VBOXOSTYPE_WinNT3x },
94 { ovf::CIMOSType_CIMOS_NetWare, VBOXOSTYPE_Netware },
95 { ovf::CIMOSType_CIMOS_NovellOES, VBOXOSTYPE_Netware },
96 { ovf::CIMOSType_CIMOS_Solaris, VBOXOSTYPE_Solaris },
97 { ovf::CIMOSType_CIMOS_SunOS, VBOXOSTYPE_Solaris },
98 { ovf::CIMOSType_CIMOS_FreeBSD, VBOXOSTYPE_FreeBSD },
99 { ovf::CIMOSType_CIMOS_NetBSD, VBOXOSTYPE_NetBSD },
100 { ovf::CIMOSType_CIMOS_QNX, VBOXOSTYPE_QNX },
101 { ovf::CIMOSType_CIMOS_Windows2000, VBOXOSTYPE_Win2k },
102 { ovf::CIMOSType_CIMOS_WindowsMe, VBOXOSTYPE_WinMe },
103 { ovf::CIMOSType_CIMOS_OpenBSD, VBOXOSTYPE_OpenBSD },
104 { ovf::CIMOSType_CIMOS_WindowsXP, VBOXOSTYPE_WinXP },
105 { ovf::CIMOSType_CIMOS_WindowsXPEmbedded, VBOXOSTYPE_WinXP },
106 { ovf::CIMOSType_CIMOS_WindowsEmbeddedforPointofService, VBOXOSTYPE_WinXP },
107 { ovf::CIMOSType_CIMOS_MicrosoftWindowsServer2003, VBOXOSTYPE_Win2k3 },
108 { ovf::CIMOSType_CIMOS_MicrosoftWindowsServer2003_64, VBOXOSTYPE_Win2k3_x64 },
109 { ovf::CIMOSType_CIMOS_WindowsXP_64, VBOXOSTYPE_WinXP_x64 },
110 { ovf::CIMOSType_CIMOS_WindowsVista, VBOXOSTYPE_WinVista },
111 { ovf::CIMOSType_CIMOS_WindowsVista_64, VBOXOSTYPE_WinVista_x64 },
112 { ovf::CIMOSType_CIMOS_MicrosoftWindowsServer2008, VBOXOSTYPE_Win2k8 },
113 { ovf::CIMOSType_CIMOS_MicrosoftWindowsServer2008_64, VBOXOSTYPE_Win2k8_x64 },
114 { ovf::CIMOSType_CIMOS_FreeBSD_64, VBOXOSTYPE_FreeBSD_x64 },
115 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS },
116 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS_x64 }, // there is no CIM 64-bit type for this
117 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS106 },
118 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS106_x64 },
119 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS107_x64 },
120 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS108_x64 },
121 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS109_x64 },
122 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS1010_x64 },
123 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS1011_x64 },
124 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS1012_x64 },
125 { ovf::CIMOSType_CIMOS_MACOS, VBOXOSTYPE_MacOS1013_x64 },
126
127 // Linuxes
128 { ovf::CIMOSType_CIMOS_RedHatEnterpriseLinux, VBOXOSTYPE_RedHat },
129 { ovf::CIMOSType_CIMOS_RedHatEnterpriseLinux_64, VBOXOSTYPE_RedHat_x64 },
130 { ovf::CIMOSType_CIMOS_Solaris_64, VBOXOSTYPE_Solaris_x64 },
131 { ovf::CIMOSType_CIMOS_SUSE, VBOXOSTYPE_OpenSUSE },
132 { ovf::CIMOSType_CIMOS_SLES, VBOXOSTYPE_OpenSUSE },
133 { ovf::CIMOSType_CIMOS_NovellLinuxDesktop, VBOXOSTYPE_OpenSUSE },
134 { ovf::CIMOSType_CIMOS_SUSE_64, VBOXOSTYPE_OpenSUSE_x64 },
135 { ovf::CIMOSType_CIMOS_SLES_64, VBOXOSTYPE_OpenSUSE_x64 },
136 { ovf::CIMOSType_CIMOS_LINUX, VBOXOSTYPE_Linux },
137 { ovf::CIMOSType_CIMOS_LINUX, VBOXOSTYPE_Linux22 },
138 { ovf::CIMOSType_CIMOS_SunJavaDesktopSystem, VBOXOSTYPE_Linux },
139 { ovf::CIMOSType_CIMOS_TurboLinux, VBOXOSTYPE_Turbolinux },
140 { ovf::CIMOSType_CIMOS_TurboLinux_64, VBOXOSTYPE_Turbolinux_x64 },
141 { ovf::CIMOSType_CIMOS_Mandriva, VBOXOSTYPE_Mandriva },
142 { ovf::CIMOSType_CIMOS_Mandriva_64, VBOXOSTYPE_Mandriva_x64 },
143 { ovf::CIMOSType_CIMOS_Ubuntu, VBOXOSTYPE_Ubuntu },
144 { ovf::CIMOSType_CIMOS_Ubuntu_64, VBOXOSTYPE_Ubuntu_x64 },
145 { ovf::CIMOSType_CIMOS_Debian, VBOXOSTYPE_Debian },
146 { ovf::CIMOSType_CIMOS_Debian_64, VBOXOSTYPE_Debian_x64 },
147 { ovf::CIMOSType_CIMOS_Linux_2_4_x, VBOXOSTYPE_Linux24 },
148 { ovf::CIMOSType_CIMOS_Linux_2_4_x_64, VBOXOSTYPE_Linux24_x64 },
149 { ovf::CIMOSType_CIMOS_Linux_2_6_x, VBOXOSTYPE_Linux26 },
150 { ovf::CIMOSType_CIMOS_Linux_2_6_x_64, VBOXOSTYPE_Linux26_x64 },
151 { ovf::CIMOSType_CIMOS_Linux_64, VBOXOSTYPE_Linux26_x64 },
152
153 // types that we have support for but CIM doesn't
154 { ovf::CIMOSType_CIMOS_Linux_2_6_x, VBOXOSTYPE_ArchLinux },
155 { ovf::CIMOSType_CIMOS_Linux_2_6_x_64, VBOXOSTYPE_ArchLinux_x64 },
156 { ovf::CIMOSType_CIMOS_Linux_2_6_x, VBOXOSTYPE_FedoraCore },
157 { ovf::CIMOSType_CIMOS_Linux_2_6_x_64, VBOXOSTYPE_FedoraCore_x64 },
158 { ovf::CIMOSType_CIMOS_Linux_2_6_x, VBOXOSTYPE_Gentoo },
159 { ovf::CIMOSType_CIMOS_Linux_2_6_x_64, VBOXOSTYPE_Gentoo_x64 },
160 { ovf::CIMOSType_CIMOS_Linux_2_6_x, VBOXOSTYPE_Xandros },
161 { ovf::CIMOSType_CIMOS_Linux_2_6_x_64, VBOXOSTYPE_Xandros_x64 },
162 { ovf::CIMOSType_CIMOS_Solaris, VBOXOSTYPE_OpenSolaris },
163 { ovf::CIMOSType_CIMOS_Solaris_64, VBOXOSTYPE_OpenSolaris_x64 },
164
165 // types added with CIM 2.25.0 follow:
166 { ovf::CIMOSType_CIMOS_WindowsServer2008R2, VBOXOSTYPE_Win2k8 }, // duplicate, see above
167// { ovf::CIMOSType_CIMOS_VMwareESXi = 104, // we can't run ESX in a VM
168 { ovf::CIMOSType_CIMOS_Windows7, VBOXOSTYPE_Win7 },
169 { ovf::CIMOSType_CIMOS_Windows7, VBOXOSTYPE_Win7_x64 }, // there is no
170 // CIM 64-bit type for this
171 { ovf::CIMOSType_CIMOS_CentOS, VBOXOSTYPE_RedHat },
172 { ovf::CIMOSType_CIMOS_CentOS_64, VBOXOSTYPE_RedHat_x64 },
173 { ovf::CIMOSType_CIMOS_OracleLinux, VBOXOSTYPE_Oracle },
174 { ovf::CIMOSType_CIMOS_OracleLinux_64, VBOXOSTYPE_Oracle_x64 },
175 { ovf::CIMOSType_CIMOS_eComStation, VBOXOSTYPE_ECS },
176
177 { ovf::CIMOSType_CIMOS_WindowsServer2011, VBOXOSTYPE_Win2k8_x64 }, // no 1:1 match on the VBox side
178 { ovf::CIMOSType_CIMOS_WindowsServer2012, VBOXOSTYPE_Win2k12_x64 },
179 { ovf::CIMOSType_CIMOS_Windows8, VBOXOSTYPE_Win8 },
180 { ovf::CIMOSType_CIMOS_Windows8_64, VBOXOSTYPE_Win8_x64 },
181 { ovf::CIMOSType_CIMOS_WindowsServer2012R2, VBOXOSTYPE_Win2k12_x64 },
182 { ovf::CIMOSType_CIMOS_Windows8_1, VBOXOSTYPE_Win81 },
183 { ovf::CIMOSType_CIMOS_Windows8_1_64, VBOXOSTYPE_Win81_x64 },
184 { ovf::CIMOSType_CIMOS_WindowsServer2016, VBOXOSTYPE_Win2k16_x64 },
185 { ovf::CIMOSType_CIMOS_Windows10, VBOXOSTYPE_Win10 },
186 { ovf::CIMOSType_CIMOS_Windows10_64, VBOXOSTYPE_Win10_x64 },
187 { ovf::CIMOSType_CIMOS_Windows10_64, VBOXOSTYPE_Win10_x64 },
188 { ovf::CIMOSType_CIMOS_WindowsServer2016, VBOXOSTYPE_Win2k19_x64 }, // no CIM type for this yet
189
190 // there are no CIM types for these, so these turn to "other" on export:
191 // VBOXOSTYPE_OpenBSD
192 // VBOXOSTYPE_OpenBSD_x64
193 // VBOXOSTYPE_NetBSD
194 // VBOXOSTYPE_NetBSD_x64
195
196};
197
198/* Pattern structure for matching the OS type description field */
199struct osTypePattern
200{
201 const char *pcszPattern;
202 VBOXOSTYPE osType;
203};
204
205/* These are the 32-Bit ones. They are sorted by priority. */
206static const osTypePattern g_aOsTypesPattern[] =
207{
208 {"Windows NT", VBOXOSTYPE_WinNT4},
209 {"Windows XP", VBOXOSTYPE_WinXP},
210 {"Windows 2000", VBOXOSTYPE_Win2k},
211 {"Windows 2003", VBOXOSTYPE_Win2k3},
212 {"Windows Vista", VBOXOSTYPE_WinVista},
213 {"Windows 2008", VBOXOSTYPE_Win2k8},
214 {"Windows 7", VBOXOSTYPE_Win7},
215 {"Windows 8.1", VBOXOSTYPE_Win81},
216 {"Windows 8", VBOXOSTYPE_Win8},
217 {"Windows 10", VBOXOSTYPE_Win10},
218 {"SUSE", VBOXOSTYPE_OpenSUSE},
219 {"Novell", VBOXOSTYPE_OpenSUSE},
220 {"Red Hat", VBOXOSTYPE_RedHat},
221 {"Mandriva", VBOXOSTYPE_Mandriva},
222 {"Ubuntu", VBOXOSTYPE_Ubuntu},
223 {"Debian", VBOXOSTYPE_Debian},
224 {"QNX", VBOXOSTYPE_QNX},
225 {"Linux 2.4", VBOXOSTYPE_Linux24},
226 {"Linux 2.6", VBOXOSTYPE_Linux26},
227 {"Linux", VBOXOSTYPE_Linux},
228 {"OpenSolaris", VBOXOSTYPE_OpenSolaris},
229 {"Solaris", VBOXOSTYPE_OpenSolaris},
230 {"FreeBSD", VBOXOSTYPE_FreeBSD},
231 {"NetBSD", VBOXOSTYPE_NetBSD},
232 {"Windows 95", VBOXOSTYPE_Win95},
233 {"Windows 98", VBOXOSTYPE_Win98},
234 {"Windows Me", VBOXOSTYPE_WinMe},
235 {"Windows 3.", VBOXOSTYPE_Win31},
236 {"DOS", VBOXOSTYPE_DOS},
237 {"OS2", VBOXOSTYPE_OS2}
238};
239
240/* These are the 64-Bit ones. They are sorted by priority. */
241static const osTypePattern g_aOsTypesPattern64[] =
242{
243 {"Windows XP", VBOXOSTYPE_WinXP_x64},
244 {"Windows 2003", VBOXOSTYPE_Win2k3_x64},
245 {"Windows Vista", VBOXOSTYPE_WinVista_x64},
246 {"Windows 2008", VBOXOSTYPE_Win2k8_x64},
247 {"Windows 7", VBOXOSTYPE_Win7_x64},
248 {"Windows 8.1", VBOXOSTYPE_Win81_x64},
249 {"Windows 8", VBOXOSTYPE_Win8_x64},
250 {"Windows 2012", VBOXOSTYPE_Win2k12_x64},
251 {"Windows 10", VBOXOSTYPE_Win10_x64},
252 {"Windows 2016", VBOXOSTYPE_Win2k16_x64},
253 {"Windows 2019", VBOXOSTYPE_Win2k19_x64},
254 {"SUSE", VBOXOSTYPE_OpenSUSE_x64},
255 {"Novell", VBOXOSTYPE_OpenSUSE_x64},
256 {"Red Hat", VBOXOSTYPE_RedHat_x64},
257 {"Mandriva", VBOXOSTYPE_Mandriva_x64},
258 {"Ubuntu", VBOXOSTYPE_Ubuntu_x64},
259 {"Debian", VBOXOSTYPE_Debian_x64},
260 {"Linux 2.4", VBOXOSTYPE_Linux24_x64},
261 {"Linux 2.6", VBOXOSTYPE_Linux26_x64},
262 {"Linux", VBOXOSTYPE_Linux26_x64},
263 {"OpenSolaris", VBOXOSTYPE_OpenSolaris_x64},
264 {"Solaris", VBOXOSTYPE_OpenSolaris_x64},
265 {"FreeBSD", VBOXOSTYPE_FreeBSD_x64},
266};
267
268/**
269 * Private helper func that suggests a VirtualBox guest OS type
270 * for the given OVF operating system type.
271 */
272void convertCIMOSType2VBoxOSType(Utf8Str &strType, ovf::CIMOSType_T c, const Utf8Str &cStr)
273{
274 /* First check if the type is other/other_64 */
275 if (c == ovf::CIMOSType_CIMOS_Other)
276 {
277 for (size_t i = 0; i < RT_ELEMENTS(g_aOsTypesPattern); ++i)
278 if (cStr.contains(g_aOsTypesPattern[i].pcszPattern, Utf8Str::CaseInsensitive))
279 {
280 strType = Global::OSTypeId(g_aOsTypesPattern[i].osType);
281 return;
282 }
283 }
284 else if (c == ovf::CIMOSType_CIMOS_Other_64)
285 {
286 for (size_t i = 0; i < RT_ELEMENTS(g_aOsTypesPattern64); ++i)
287 if (cStr.contains(g_aOsTypesPattern64[i].pcszPattern, Utf8Str::CaseInsensitive))
288 {
289 strType = Global::OSTypeId(g_aOsTypesPattern64[i].osType);
290 return;
291 }
292 }
293
294 for (size_t i = 0; i < RT_ELEMENTS(g_aOsTypes); ++i)
295 {
296 if (c == g_aOsTypes[i].cim)
297 {
298 strType = Global::OSTypeId(g_aOsTypes[i].osType);
299 return;
300 }
301 }
302
303 if (c == ovf::CIMOSType_CIMOS_Other_64)
304 strType = Global::OSTypeId(VBOXOSTYPE_Unknown_x64);
305 else
306 strType = Global::OSTypeId(VBOXOSTYPE_Unknown);
307}
308
309/**
310 * Private helper func that suggests a VirtualBox guest OS type
311 * for the given OVF operating system type.
312 * @returns CIM OS type.
313 * @param pcszVBox Our guest OS type identifier string.
314 * @param fLongMode Whether long mode is enabled and a 64-bit CIM type is
315 * preferred even if the VBox guest type isn't 64-bit.
316 */
317ovf::CIMOSType_T convertVBoxOSType2CIMOSType(const char *pcszVBox, BOOL fLongMode)
318{
319 for (size_t i = 0; i < RT_ELEMENTS(g_aOsTypes); ++i)
320 {
321 if (!RTStrICmp(pcszVBox, Global::OSTypeId(g_aOsTypes[i].osType)))
322 {
323 if (fLongMode && !(g_aOsTypes[i].osType & VBOXOSTYPE_x64))
324 {
325 VBOXOSTYPE enmDesiredOsType = (VBOXOSTYPE)((int)g_aOsTypes[i].osType | (int)VBOXOSTYPE_x64);
326 size_t j = i;
327 while (++j < RT_ELEMENTS(g_aOsTypes))
328 if (g_aOsTypes[j].osType == enmDesiredOsType)
329 return g_aOsTypes[j].cim;
330 j = i;
331 while (--j > 0)
332 if (g_aOsTypes[j].osType == enmDesiredOsType)
333 return g_aOsTypes[j].cim;
334 /* Not all OSes have 64-bit versions, so just return the 32-bit variant. */
335 }
336 return g_aOsTypes[i].cim;
337 }
338 }
339
340 return fLongMode ? ovf::CIMOSType_CIMOS_Other_64 : ovf::CIMOSType_CIMOS_Other;
341}
342
343Utf8Str convertNetworkAttachmentTypeToString(NetworkAttachmentType_T type)
344{
345 Utf8Str strType;
346 switch (type)
347 {
348 case NetworkAttachmentType_NAT: strType = "NAT"; break;
349 case NetworkAttachmentType_Bridged: strType = "Bridged"; break;
350 case NetworkAttachmentType_Internal: strType = "Internal"; break;
351 case NetworkAttachmentType_HostOnly: strType = "HostOnly"; break;
352 case NetworkAttachmentType_Generic: strType = "Generic"; break;
353 case NetworkAttachmentType_NATNetwork: strType = "NATNetwork"; break;
354 case NetworkAttachmentType_Null: strType = "Null"; break;
355 case NetworkAttachmentType_Cloud: strType = "Cloud"; break;
356#ifdef VBOX_WITH_XPCOM_CPP_ENUM_HACK
357 case NetworkAttachmentType_32BitHack: AssertFailedBreak(); /* (compiler warnings) */
358#endif
359 }
360 return strType;
361}
362
363
364////////////////////////////////////////////////////////////////////////////////
365//
366// Appliance constructor / destructor
367//
368// ////////////////////////////////////////////////////////////////////////////////
369
370DEFINE_EMPTY_CTOR_DTOR(VirtualSystemDescription)
371
372HRESULT VirtualSystemDescription::FinalConstruct()
373{
374 return BaseFinalConstruct();
375}
376
377void VirtualSystemDescription::FinalRelease()
378{
379 uninit();
380
381 BaseFinalRelease();
382}
383
384Appliance::Appliance()
385 : mVirtualBox(NULL)
386{
387}
388
389Appliance::~Appliance()
390{
391}
392
393
394HRESULT Appliance::FinalConstruct()
395{
396 return BaseFinalConstruct();
397}
398
399void Appliance::FinalRelease()
400{
401 uninit();
402
403 BaseFinalRelease();
404}
405
406
407////////////////////////////////////////////////////////////////////////////////
408//
409// Internal helpers
410//
411////////////////////////////////////////////////////////////////////////////////
412
413
414////////////////////////////////////////////////////////////////////////////////
415//
416// IVirtualBox public methods
417//
418////////////////////////////////////////////////////////////////////////////////
419
420// This code is here so we won't have to include the appliance headers in the
421// IVirtualBox implementation.
422
423/**
424 * Implementation for IVirtualBox::createAppliance.
425 *
426 * @param aAppliance IAppliance object created if S_OK is returned.
427 * @return S_OK or error.
428 */
429HRESULT VirtualBox::createAppliance(ComPtr<IAppliance> &aAppliance)
430{
431 ComObjPtr<Appliance> appliance;
432 HRESULT hrc = appliance.createObject();
433 if (SUCCEEDED(hrc))
434 {
435 hrc = appliance->init(this);
436 if (SUCCEEDED(hrc))
437 hrc = appliance.queryInterfaceTo(aAppliance.asOutParam());
438 }
439 return hrc;
440}
441
442/**
443 * Appliance COM initializer.
444 * @param aVirtualBox The VirtualBox object.
445 */
446HRESULT Appliance::init(VirtualBox *aVirtualBox)
447{
448 HRESULT rc = S_OK;
449 /* Enclose the state transition NotReady->InInit->Ready */
450 AutoInitSpan autoInitSpan(this);
451 AssertReturn(autoInitSpan.isOk(), E_FAIL);
452
453 /* Weak reference to a VirtualBox object */
454 unconst(mVirtualBox) = aVirtualBox;
455
456 // initialize data
457 m = new Data;
458 m->m_pSecretKeyStore = new SecretKeyStore(false /* fRequireNonPageable*/);
459 AssertReturn(m->m_pSecretKeyStore, E_FAIL);
460
461 rc = i_initBackendNames();
462
463 /* Confirm a successful initialization */
464 autoInitSpan.setSucceeded();
465
466 return rc;
467}
468
469/**
470 * Appliance COM uninitializer.
471 */
472void Appliance::uninit()
473{
474 /* Enclose the state transition Ready->InUninit->NotReady */
475 AutoUninitSpan autoUninitSpan(this);
476 if (autoUninitSpan.uninitDone())
477 return;
478
479 if (m->m_pSecretKeyStore)
480 delete m->m_pSecretKeyStore;
481
482 delete m;
483 m = NULL;
484}
485
486////////////////////////////////////////////////////////////////////////////////
487//
488// IAppliance public methods
489//
490////////////////////////////////////////////////////////////////////////////////
491
492/**
493 * Public method implementation.
494 */
495HRESULT Appliance::getPath(com::Utf8Str &aPath)
496{
497 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
498
499 aPath = m->locInfo.strPath;
500
501 return S_OK;
502}
503
504/**
505 * Public method implementation.
506 */
507HRESULT Appliance::getDisks(std::vector<com::Utf8Str> &aDisks)
508{
509 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
510
511 aDisks.resize(0);
512
513 if (m->pReader) // OVFReader instantiated?
514 {
515 aDisks.resize(m->pReader->m_mapDisks.size());
516
517 ovf::DiskImagesMap::const_iterator it;
518 size_t i = 0;
519 for (it = m->pReader->m_mapDisks.begin();
520 it != m->pReader->m_mapDisks.end();
521 ++it, ++i)
522 {
523 // create a string representing this disk
524 const ovf::DiskImage &d = it->second;
525 char *psz = NULL;
526 RTStrAPrintf(&psz,
527 "%s\t"
528 "%RI64\t"
529 "%RI64\t"
530 "%s\t"
531 "%s\t"
532 "%RI64\t"
533 "%RI64\t"
534 "%s",
535 d.strDiskId.c_str(),
536 d.iCapacity,
537 d.iPopulatedSize,
538 d.strFormat.c_str(),
539 d.strHref.c_str(),
540 d.iSize,
541 d.iChunkSize,
542 d.strCompression.c_str());
543 Utf8Str utf(psz);
544 aDisks[i] = utf;
545 RTStrFree(psz);
546 }
547 }
548
549 return S_OK;
550}
551
552/**
553 * Public method implementation.
554 */
555HRESULT Appliance::getCertificate(ComPtr<ICertificate> &aCertificateInfo)
556{
557 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
558
559 /* Can be NULL at this point, queryInterfaceto handles that. */
560 m->ptrCertificateInfo.queryInterfaceTo(aCertificateInfo.asOutParam());
561 return S_OK;
562}
563
564/**
565 * Public method implementation.
566 */
567HRESULT Appliance::getVirtualSystemDescriptions(std::vector<ComPtr<IVirtualSystemDescription> > &aVirtualSystemDescriptions)
568{
569 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
570
571 aVirtualSystemDescriptions.resize(m->virtualSystemDescriptions.size());
572 std::list< ComObjPtr<VirtualSystemDescription> > vsds(m->virtualSystemDescriptions);
573 size_t i = 0;
574 for (std::list< ComObjPtr<VirtualSystemDescription> >::iterator it = vsds.begin(); it != vsds.end(); ++it, ++i)
575 {
576 (*it).queryInterfaceTo(aVirtualSystemDescriptions[i].asOutParam());
577 }
578 return S_OK;
579}
580
581/**
582 * Public method implementation.
583 */
584HRESULT Appliance::getMachines(std::vector<com::Utf8Str> &aMachines)
585{
586 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
587
588 aMachines.resize(m->llGuidsMachinesCreated.size());
589 size_t i = 0;
590 for (std::list<Guid>::const_iterator it = m->llGuidsMachinesCreated.begin();
591 it != m->llGuidsMachinesCreated.end();
592 ++it, ++i)
593 {
594 const Guid &uuid = *it;
595 aMachines[i] = uuid.toUtf16();
596 }
597 return S_OK;
598}
599
600HRESULT Appliance::createVFSExplorer(const com::Utf8Str &aURI, ComPtr<IVFSExplorer> &aExplorer)
601{
602 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
603
604 ComObjPtr<VFSExplorer> explorer;
605 HRESULT rc = S_OK;
606 try
607 {
608 Utf8Str uri(aURI);
609 /* Check which kind of export the user has requested */
610 LocationInfo li;
611 i_parseURI(aURI, li);
612 /* Create the explorer object */
613 explorer.createObject();
614 rc = explorer->init(li.storageType, li.strPath, li.strHostname, li.strUsername, li.strPassword, mVirtualBox);
615 }
616 catch (HRESULT aRC)
617 {
618 rc = aRC;
619 }
620
621 if (SUCCEEDED(rc))
622 /* Return explorer to the caller */
623 explorer.queryInterfaceTo(aExplorer.asOutParam());
624
625 return rc;
626}
627
628
629/**
630 * Public method implementation.
631 * Add the "aRequested" numbers of new empty objects of VSD into the list
632 * "virtualSystemDescriptions".
633 * The parameter "aCreated" keeps the actual number of the added objects.
634 * In case of exception all added objects are removed from the list.
635 */
636HRESULT Appliance::createVirtualSystemDescriptions(ULONG aRequested, ULONG *aCreated)
637{
638 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
639
640 HRESULT rc = S_OK;
641 uint32_t lQuantity = aRequested;
642 uint32_t i=0;
643
644 if (lQuantity < 1)
645 return setError(E_FAIL, tr("The number of VirtualSystemDescription objects must be at least 1 or more."));
646 try
647 {
648 for (; i<lQuantity; ++i)
649 {
650 ComObjPtr<VirtualSystemDescription> opVSD;
651 rc = opVSD.createObject();
652 if (SUCCEEDED(rc))
653 {
654 rc = opVSD->init();
655 if (SUCCEEDED(rc))
656 m->virtualSystemDescriptions.push_back(opVSD);
657 else
658 break;
659 }
660 else
661 break;
662 }
663
664 if (i<lQuantity)
665 LogRel(("Number of created VirtualSystemDescription objects is less than requested"
666 "(Requested %d, Created %d)",lQuantity, i));
667
668 *aCreated = i;
669 }
670 catch (HRESULT aRC)
671 {
672 for (; i>0; --i)
673 {
674 if (!m->virtualSystemDescriptions.empty())
675 m->virtualSystemDescriptions.pop_back();
676 else
677 break;
678 }
679 rc = aRC;
680 }
681
682 return rc;
683}
684
685/**
686 * Public method implementation.
687 */
688HRESULT Appliance::getWarnings(std::vector<com::Utf8Str> &aWarnings)
689{
690 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
691
692 aWarnings.resize(m->llWarnings.size());
693
694 list<Utf8Str>::const_iterator it;
695 size_t i = 0;
696 for (it = m->llWarnings.begin();
697 it != m->llWarnings.end();
698 ++it, ++i)
699 {
700 aWarnings[i] = *it;
701 }
702
703 return S_OK;
704}
705
706HRESULT Appliance::getPasswordIds(std::vector<com::Utf8Str> &aIdentifiers)
707{
708 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
709
710 aIdentifiers = m->m_vecPasswordIdentifiers;
711 return S_OK;
712}
713
714HRESULT Appliance::getMediumIdsForPasswordId(const com::Utf8Str &aPasswordId, std::vector<com::Guid> &aIdentifiers)
715{
716 HRESULT hrc = S_OK;
717 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
718
719 std::map<com::Utf8Str, GUIDVEC>::const_iterator it = m->m_mapPwIdToMediumIds.find(aPasswordId);
720 if (it != m->m_mapPwIdToMediumIds.end())
721 aIdentifiers = it->second;
722 else
723 hrc = setError(E_FAIL, tr("The given password identifier is not associated with any medium"));
724
725 return hrc;
726}
727
728HRESULT Appliance::addPasswords(const std::vector<com::Utf8Str> &aIdentifiers,
729 const std::vector<com::Utf8Str> &aPasswords)
730{
731 HRESULT hrc = S_OK;
732
733 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
734
735 /* Check that the IDs do not exist already before changing anything. */
736 for (unsigned i = 0; i < aIdentifiers.size(); i++)
737 {
738 SecretKey *pKey = NULL;
739 int rc = m->m_pSecretKeyStore->retainSecretKey(aIdentifiers[i], &pKey);
740 if (rc != VERR_NOT_FOUND)
741 {
742 AssertPtr(pKey);
743 if (pKey)
744 pKey->release();
745 return setError(VBOX_E_OBJECT_IN_USE, tr("A password with the given ID already exists"));
746 }
747 }
748
749 for (unsigned i = 0; i < aIdentifiers.size() && SUCCEEDED(hrc); i++)
750 {
751 size_t cbKey = aPasswords[i].length() + 1; /* Include terminator */
752 const uint8_t *pbKey = (const uint8_t *)aPasswords[i].c_str();
753
754 int rc = m->m_pSecretKeyStore->addSecretKey(aIdentifiers[i], pbKey, cbKey);
755 if (RT_SUCCESS(rc))
756 m->m_cPwProvided++;
757 else if (rc == VERR_NO_MEMORY)
758 hrc = setError(E_OUTOFMEMORY, tr("Failed to allocate enough secure memory for the key"));
759 else
760 hrc = setError(E_FAIL, tr("Unknown error happened while adding a password (%Rrc)"), rc);
761 }
762
763 return hrc;
764}
765
766////////////////////////////////////////////////////////////////////////////////
767//
768// Appliance private methods
769//
770////////////////////////////////////////////////////////////////////////////////
771
772HRESULT Appliance::i_initBackendNames()
773{
774 HRESULT hrc = S_OK;
775 if (!g_fInitializedBackendNames)
776 {
777 /*
778 * Use the system properties to translate file extensions into
779 * storage backend names.
780 */
781 static struct
782 {
783 const char *pszExt; /**< extension */
784 char *pszBackendName;
785 size_t cbBackendName;
786 } const s_aFormats[] =
787 {
788 { "iso", g_szIsoBackend, sizeof(g_szIsoBackend) },
789 { "vmdk", g_szVmdkBackend, sizeof(g_szVmdkBackend) },
790 { "vhd", g_szVhdBackend, sizeof(g_szVhdBackend) },
791 };
792 SystemProperties *pSysProps = mVirtualBox->i_getSystemProperties();
793 for (unsigned i = 0; i < RT_ELEMENTS(s_aFormats); i++)
794 {
795 ComObjPtr<MediumFormat> trgFormat = pSysProps->i_mediumFormatFromExtension(s_aFormats[i].pszExt);
796 if (trgFormat.isNotNull())
797 {
798 const char *pszName = trgFormat->i_getName().c_str();
799 int vrc = RTStrCopy(s_aFormats[i].pszBackendName, s_aFormats[i].cbBackendName, pszName);
800 AssertRCStmt(vrc, hrc = setError(E_FAIL, "Unexpected storage backend name copy error %Rrc for %s.", vrc, pszName));
801 }
802 else
803 hrc = setError(E_FAIL, tr("Can't find appropriate medium format for ISO type of a virtual disk."));
804 }
805
806 if (SUCCEEDED(hrc))
807 g_fInitializedBackendNames = true;
808 }
809
810 return hrc;
811}
812
813Utf8Str Appliance::i_typeOfVirtualDiskFormatFromURI(Utf8Str uri) const
814{
815 Assert(g_fInitializedBackendNames);
816
817 unsigned i = RT_ELEMENTS(g_aUriToBackend);
818 while (i-- > 0)
819 if (RTStrICmp(g_aUriToBackend[i].pszUri, uri.c_str()) == 0)
820 return Utf8Str(g_aUriToBackend[i].pszBackend);
821 return Utf8Str();
822}
823
824#if 0 /* unused */
825std::set<Utf8Str> Appliance::i_URIFromTypeOfVirtualDiskFormat(Utf8Str type)
826{
827 Assert(g_fInitializedBackendNames);
828
829 std::set<Utf8Str> UriSet;
830 unsigned i = RT_ELEMENTS(g_aUriToBackend);
831 while (i-- > 0)
832 if (RTStrICmp(g_aUriToBackend[i].pszBackend, type.c_str()) == 0)
833 UriSet.insert(g_aUriToBackend[i].pszUri);
834 return UriSet;
835}
836#endif
837
838/**
839 * Returns a medium format object corresponding to the given
840 * disk image or null if no such format.
841 *
842 * @param di Disk Image
843 * @param mf Medium Format
844 *
845 * @return ComObjPtr<MediumFormat>
846 */
847HRESULT Appliance::i_findMediumFormatFromDiskImage(const ovf::DiskImage &di, ComObjPtr<MediumFormat>& mf)
848{
849 HRESULT rc = S_OK;
850
851 /* Get the system properties. */
852 SystemProperties *pSysProps = mVirtualBox->i_getSystemProperties();
853
854 /* We need a proper source format description */
855 /* Which format to use? */
856 Utf8Str strSrcFormat = i_typeOfVirtualDiskFormatFromURI(di.strFormat);
857
858 /*
859 * fallback, if we can't determine virtual disk format using URI from the attribute ovf:format
860 * in the corresponding section <Disk> in the OVF file.
861 */
862 if (strSrcFormat.isEmpty())
863 {
864 strSrcFormat = di.strHref;
865
866 /* check either file gzipped or not
867 * if "yes" then remove last extension,
868 * i.e. "image.vmdk.gz"->"image.vmdk"
869 */
870 if (di.strCompression == "gzip")
871 {
872 if (RTPathHasSuffix(strSrcFormat.c_str()))
873 {
874 strSrcFormat.stripSuffix();
875 }
876 else
877 {
878 mf.setNull();
879 rc = setError(E_FAIL,
880 tr("Internal inconsistency looking up medium format for the disk image '%s'"),
881 di.strHref.c_str());
882 return rc;
883 }
884 }
885 /* Figure out from extension which format the image of disk has. */
886 if (RTPathHasSuffix(strSrcFormat.c_str()))
887 {
888 const char *pszExt = RTPathSuffix(strSrcFormat.c_str());
889 if (pszExt)
890 pszExt++;
891 mf = pSysProps->i_mediumFormatFromExtension(pszExt);
892 }
893 else
894 mf.setNull();
895 }
896 else
897 mf = pSysProps->i_mediumFormat(strSrcFormat);
898
899 if (mf.isNull())
900 rc = setError(E_FAIL, tr("Internal inconsistency looking up medium format for the disk image '%s'"),
901 di.strHref.c_str());
902
903 return rc;
904}
905
906/**
907 * Setup automatic I/O stream digest calculation, adding it to hOurManifest.
908 *
909 * @returns Passthru I/O stream, of @a hVfsIos if no digest calc needed.
910 * @param hVfsIos The stream to wrap. Always consumed.
911 * @param pszManifestEntry The manifest entry.
912 * @param fRead Set if read stream, clear if write.
913 * @throws Nothing.
914 */
915RTVFSIOSTREAM Appliance::i_manifestSetupDigestCalculationForGivenIoStream(RTVFSIOSTREAM hVfsIos, const char *pszManifestEntry,
916 bool fRead /*= true */)
917{
918 int vrc;
919 Assert(!RTManifestPtIosIsInstanceOf(hVfsIos));
920
921 if (m->fDigestTypes == 0)
922 return hVfsIos;
923
924 /* Create the manifest if necessary. */
925 if (m->hOurManifest == NIL_RTMANIFEST)
926 {
927 vrc = RTManifestCreate(0 /*fFlags*/, &m->hOurManifest);
928 AssertRCReturnStmt(vrc, RTVfsIoStrmRelease(hVfsIos), NIL_RTVFSIOSTREAM);
929 }
930
931 /* Setup the stream. */
932 RTVFSIOSTREAM hVfsIosPt;
933 vrc = RTManifestEntryAddPassthruIoStream(m->hOurManifest, hVfsIos, pszManifestEntry, m->fDigestTypes, fRead, &hVfsIosPt);
934
935 RTVfsIoStrmRelease(hVfsIos); /* always consumed! */
936 if (RT_SUCCESS(vrc))
937 return hVfsIosPt;
938
939 setErrorVrc(vrc, "RTManifestEntryAddPassthruIoStream failed with rc=%Rrc", vrc);
940 return NIL_RTVFSIOSTREAM;
941}
942
943/**
944 * Returns true if the appliance is in "idle" state. This should always be the
945 * case unless an import or export is currently in progress. Similar to machine
946 * states, this permits the Appliance implementation code to let go of the
947 * Appliance object lock while a time-consuming disk conversion is in progress
948 * without exposing the appliance to conflicting calls.
949 *
950 * This sets an error on "this" (the appliance) and returns false if the appliance
951 * is busy. The caller should then return E_ACCESSDENIED.
952 *
953 * Must be called from under the object lock!
954 */
955bool Appliance::i_isApplianceIdle()
956{
957 if (m->state == ApplianceImporting)
958 setError(VBOX_E_INVALID_OBJECT_STATE, tr("The appliance is busy importing files"));
959 else if (m->state == ApplianceExporting)
960 setError(VBOX_E_INVALID_OBJECT_STATE, tr("The appliance is busy exporting files"));
961 else
962 return true;
963
964 return false;
965}
966
967HRESULT Appliance::i_searchUniqueVMName(Utf8Str& aName) const
968{
969 IMachine *machine = NULL;
970 char *tmpName = RTStrDup(aName.c_str());
971 int i = 1;
972 while (mVirtualBox->FindMachine(Bstr(tmpName).raw(), &machine) != VBOX_E_OBJECT_NOT_FOUND)
973 {
974 RTStrFree(tmpName);
975 RTStrAPrintf(&tmpName, "%s %d", aName.c_str(), i);
976 ++i;
977 }
978 aName = tmpName;
979 RTStrFree(tmpName);
980
981 return S_OK;
982}
983
984HRESULT Appliance::i_searchUniqueImageFilePath(const Utf8Str &aMachineFolder, DeviceType_T aDeviceType, Utf8Str &aName) const
985{
986 /*
987 * Check if the file exists or if a medium with this path is registered already
988 */
989 Utf8Str strAbsName;
990 ssize_t offDashNum = -1;
991 ssize_t cchDashNum = 0;
992 for (unsigned i = 1;; i++)
993 {
994 /* Complete the path (could be relative to machine folder). */
995 int rc = RTPathAbsExCxx(strAbsName, aMachineFolder, aName);
996 AssertRCReturn(rc, Global::vboxStatusCodeToCOM(rc)); /** @todo stupid caller ignores this */
997
998 /* Check that the file does not exist and that there is no media somehow matching the name. */
999 if (!RTPathExists(strAbsName.c_str()))
1000 {
1001 ComPtr<IMedium> ptrMedium;
1002 HRESULT hrc = mVirtualBox->OpenMedium(Bstr(strAbsName).raw(), aDeviceType, AccessMode_ReadWrite,
1003 FALSE /* fForceNewUuid */, ptrMedium.asOutParam());
1004 if (hrc == VBOX_E_OBJECT_NOT_FOUND)
1005 return S_OK;
1006 }
1007
1008 /* Insert '_%i' before the suffix and try again. */
1009 if (offDashNum < 0)
1010 {
1011 const char *pszSuffix = RTPathSuffix(aName.c_str());
1012 offDashNum = pszSuffix ? pszSuffix - aName.c_str() : aName.length();
1013 }
1014 char szTmp[32];
1015 size_t cchTmp = RTStrPrintf(szTmp, sizeof(szTmp), "_%u", i);
1016 aName.replace(offDashNum, cchDashNum, szTmp, cchTmp);
1017 cchDashNum = cchTmp;
1018 }
1019}
1020
1021/**
1022 * Called from Appliance::importImpl() and Appliance::writeImpl() to set up a
1023 * progress object with the proper weights and maximum progress values.
1024 */
1025HRESULT Appliance::i_setUpProgress(ComObjPtr<Progress> &pProgress,
1026 const Utf8Str &strDescription,
1027 SetUpProgressMode mode)
1028{
1029 HRESULT rc;
1030
1031 /* Create the progress object */
1032 try
1033 {
1034 rc = pProgress.createObject();
1035 if (FAILED(rc))
1036 return rc;
1037 }
1038 catch (std::bad_alloc &)
1039 {
1040 return E_OUTOFMEMORY;
1041 }
1042
1043 // compute the disks weight (this sets ulTotalDisksMB and cDisks in the instance data)
1044 i_disksWeight();
1045
1046 m->ulWeightForManifestOperation = 0;
1047
1048 ULONG cOperations = 1 // one for XML setup
1049 + m->cDisks; // plus one per disk
1050 ULONG ulTotalOperationsWeight;
1051 if (m->ulTotalDisksMB)
1052 {
1053 m->ulWeightForXmlOperation = (ULONG)((double)m->ulTotalDisksMB * 1 / 100); // use 1% of the progress for the XML
1054 ulTotalOperationsWeight = m->ulTotalDisksMB + m->ulWeightForXmlOperation;
1055 }
1056 else
1057 {
1058 // no disks to export:
1059 m->ulWeightForXmlOperation = 1;
1060 ulTotalOperationsWeight = 1;
1061 }
1062
1063 switch (mode)
1064 {
1065 case ImportFile:
1066 {
1067 break;
1068 }
1069 case WriteFile:
1070 {
1071 // assume that creating the manifest will take .1% of the time it takes to export the disks
1072 if (m->fManifest)
1073 {
1074 ++cOperations; // another one for creating the manifest
1075
1076 m->ulWeightForManifestOperation = (ULONG)((double)m->ulTotalDisksMB * .1 / 100); // use .5% of the
1077 // progress for the manifest
1078 ulTotalOperationsWeight += m->ulWeightForManifestOperation;
1079 }
1080 break;
1081 }
1082 case ImportS3:
1083 {
1084 cOperations += 1 + 1; // another one for the manifest file & another one for the import
1085 ulTotalOperationsWeight = m->ulTotalDisksMB;
1086 if (!m->ulTotalDisksMB)
1087 // no disks to export:
1088 ulTotalOperationsWeight = 1;
1089
1090 ULONG ulImportWeight = (ULONG)((double)ulTotalOperationsWeight * 50 / 100); // use 50% for import
1091 ulTotalOperationsWeight += ulImportWeight;
1092
1093 m->ulWeightForXmlOperation = ulImportWeight; /* save for using later */
1094
1095 ULONG ulInitWeight = (ULONG)((double)ulTotalOperationsWeight * 0.1 / 100); // use 0.1% for init
1096 ulTotalOperationsWeight += ulInitWeight;
1097 break;
1098 }
1099 case WriteS3:
1100 {
1101 cOperations += 1 + 1; // another one for the mf & another one for temporary creation
1102
1103 if (m->ulTotalDisksMB)
1104 {
1105 m->ulWeightForXmlOperation = (ULONG)((double)m->ulTotalDisksMB * 1 / 100); // use 1% of the progress
1106 // for OVF file upload
1107 // (we didn't know the
1108 // size at this point)
1109 ulTotalOperationsWeight = m->ulTotalDisksMB + m->ulWeightForXmlOperation;
1110 }
1111 else
1112 {
1113 // no disks to export:
1114 ulTotalOperationsWeight = 1;
1115 m->ulWeightForXmlOperation = 1;
1116 }
1117 ULONG ulOVFCreationWeight = (ULONG)((double)ulTotalOperationsWeight * 50.0 / 100.0); /* Use 50% for the
1118 creation of the OVF
1119 & the disks */
1120 ulTotalOperationsWeight += ulOVFCreationWeight;
1121 break;
1122 }
1123 case ExportCloud:
1124 case ImportCloud:
1125 break;
1126 }
1127 Log(("Setting up progress object: ulTotalMB = %d, cDisks = %d, => cOperations = %d, ulTotalOperationsWeight = %d, m->ulWeightForXmlOperation = %d\n",
1128 m->ulTotalDisksMB, m->cDisks, cOperations, ulTotalOperationsWeight, m->ulWeightForXmlOperation));
1129
1130 return pProgress->init(mVirtualBox, static_cast<IAppliance*>(this),
1131 strDescription,
1132 TRUE /* aCancelable */,
1133 cOperations, // ULONG cOperations,
1134 ulTotalOperationsWeight, // ULONG ulTotalOperationsWeight,
1135 strDescription, // CBSTR bstrFirstOperationDescription,
1136 m->ulWeightForXmlOperation); // ULONG ulFirstOperationWeight,
1137}
1138
1139void Appliance::i_addWarning(const char* aWarning, ...)
1140{
1141 try
1142 {
1143 va_list args;
1144 va_start(args, aWarning);
1145 Utf8Str str(aWarning, args);
1146 va_end(args);
1147 m->llWarnings.push_back(str);
1148 }
1149 catch (...)
1150 {
1151 AssertFailed();
1152 }
1153}
1154
1155/**
1156 * Refreshes the cDisks and ulTotalDisksMB members in the instance data.
1157 * Requires that virtual system descriptions are present.
1158 */
1159void Appliance::i_disksWeight()
1160{
1161 m->ulTotalDisksMB = 0;
1162 m->cDisks = 0;
1163 // weigh the disk images according to their sizes
1164 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it;
1165 for (it = m->virtualSystemDescriptions.begin();
1166 it != m->virtualSystemDescriptions.end();
1167 ++it)
1168 {
1169 ComObjPtr<VirtualSystemDescription> vsdescThis = (*it);
1170 /* One for every medium of the Virtual System */
1171 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->i_findByType(VirtualSystemDescriptionType_HardDiskImage);
1172 std::list<VirtualSystemDescriptionEntry*>::const_iterator itH;
1173 for (itH = avsdeHDs.begin();
1174 itH != avsdeHDs.end();
1175 ++itH)
1176 {
1177 const VirtualSystemDescriptionEntry *pHD = *itH;
1178 m->ulTotalDisksMB += pHD->ulSizeMB;
1179 ++m->cDisks;
1180 }
1181
1182 avsdeHDs = vsdescThis->i_findByType(VirtualSystemDescriptionType_CDROM);
1183 for (itH = avsdeHDs.begin();
1184 itH != avsdeHDs.end();
1185 ++itH)
1186 {
1187 const VirtualSystemDescriptionEntry *pHD = *itH;
1188 m->ulTotalDisksMB += pHD->ulSizeMB;
1189 ++m->cDisks;
1190 }
1191 }
1192
1193}
1194
1195void Appliance::i_parseBucket(Utf8Str &aPath, Utf8Str &aBucket)
1196{
1197 /* Buckets are S3 specific. So parse the bucket out of the file path */
1198 if (!aPath.startsWith("/"))
1199 throw setError(E_INVALIDARG,
1200 tr("The path '%s' must start with /"), aPath.c_str());
1201 size_t bpos = aPath.find("/", 1);
1202 if (bpos != Utf8Str::npos)
1203 {
1204 aBucket = aPath.substr(1, bpos - 1); /* The bucket without any slashes */
1205 aPath = aPath.substr(bpos); /* The rest of the file path */
1206 }
1207 /* If there is no bucket name provided reject it */
1208 if (aBucket.isEmpty())
1209 throw setError(E_INVALIDARG,
1210 tr("You doesn't provide a bucket name in the URI '%s'"), aPath.c_str());
1211}
1212
1213/**
1214 * Worker for TaskOVF::handler.
1215 *
1216 * The TaskOVF is started in Appliance::readImpl() and Appliance::importImpl()
1217 * and Appliance::writeImpl().
1218 *
1219 * This will in turn call Appliance::readFS() or Appliance::importFS() or
1220 * Appliance::writeFS().
1221 *
1222 * @thread pTask The task.
1223 */
1224/* static */ void Appliance::i_importOrExportThreadTask(TaskOVF *pTask)
1225{
1226 LogFlowFuncEnter();
1227 AssertReturnVoid(pTask);
1228
1229 Appliance *pAppliance = pTask->pAppliance;
1230 LogFlowFunc(("Appliance %p taskType=%d\n", pAppliance, pTask->taskType));
1231
1232 switch (pTask->taskType)
1233 {
1234 case TaskOVF::Read:
1235 pAppliance->m->resetReadData();
1236 if (pTask->locInfo.storageType == VFSType_File)
1237 pTask->rc = pAppliance->i_readFS(pTask);
1238 else
1239 pTask->rc = E_NOTIMPL;
1240 break;
1241
1242 case TaskOVF::Import:
1243 /** @todo allow overriding these? */
1244 if (!pAppliance->m->fSignatureValid && pAppliance->m->pbSignedDigest)
1245 pTask->rc = pAppliance->setError(E_FAIL, tr("The manifest signature for '%s' is not valid"),
1246 pTask->locInfo.strPath.c_str());
1247 else if (!pAppliance->m->fCertificateValid && pAppliance->m->pbSignedDigest)
1248 {
1249 if (pAppliance->m->strCertError.isNotEmpty())
1250 pTask->rc = pAppliance->setError(E_FAIL, tr("The certificate used to signed '%s' is not valid: %s"),
1251 pTask->locInfo.strPath.c_str(), pAppliance->m->strCertError.c_str());
1252 else
1253 pTask->rc = pAppliance->setError(E_FAIL, tr("The certificate used to signed '%s' is not valid"),
1254 pTask->locInfo.strPath.c_str());
1255 }
1256 // fusion does not consider this a show stopper (we've filed a warning during read).
1257 //else if (pAppliance->m->fCertificateMissingPath && pAppliance->m->pbSignedDigest)
1258 // pTask->rc = pAppliance->setError(E_FAIL, tr("The certificate used to signed '%s' is does not have a valid CA path"),
1259 // pTask->locInfo.strPath.c_str());
1260 else
1261 {
1262 if (pTask->locInfo.storageType == VFSType_File)
1263 pTask->rc = pAppliance->i_importFS(pTask);
1264 else
1265 pTask->rc = E_NOTIMPL;
1266 }
1267 break;
1268
1269 case TaskOVF::Write:
1270 if (pTask->locInfo.storageType == VFSType_File)
1271 pTask->rc = pAppliance->i_writeFS(pTask);
1272 else
1273 pTask->rc = E_NOTIMPL;
1274 break;
1275
1276 default:
1277 AssertFailed();
1278 pTask->rc = E_FAIL;
1279 break;
1280 }
1281
1282 if (!pTask->pProgress.isNull())
1283 pTask->pProgress->i_notifyComplete(pTask->rc);
1284
1285 LogFlowFuncLeave();
1286}
1287
1288/* static */ DECLCALLBACK(int) Appliance::TaskOVF::updateProgress(unsigned uPercent, void *pvUser)
1289{
1290 Appliance::TaskOVF* pTask = *(Appliance::TaskOVF**)pvUser;
1291
1292 if ( pTask
1293 && !pTask->pProgress.isNull())
1294 {
1295 BOOL fCanceled;
1296 pTask->pProgress->COMGETTER(Canceled)(&fCanceled);
1297 if (fCanceled)
1298 return -1;
1299 pTask->pProgress->SetCurrentOperationProgress(uPercent);
1300 }
1301 return VINF_SUCCESS;
1302}
1303
1304/**
1305 * Worker for TaskOPC::handler.
1306 * @thread pTask The task.
1307 */
1308/* static */
1309void Appliance::i_exportOPCThreadTask(TaskOPC *pTask)
1310{
1311 LogFlowFuncEnter();
1312 AssertReturnVoid(pTask);
1313
1314 Appliance *pAppliance = pTask->pAppliance;
1315 LogFlowFunc(("Appliance %p taskType=%d\n", pAppliance, pTask->taskType));
1316
1317 switch (pTask->taskType)
1318 {
1319 case TaskOPC::Export:
1320 pTask->rc = pAppliance->i_writeFSOPC(pTask);
1321 break;
1322
1323 default:
1324 AssertFailed();
1325 pTask->rc = E_FAIL;
1326 break;
1327 }
1328
1329 if (!pTask->pProgress.isNull())
1330 pTask->pProgress->i_notifyComplete(pTask->rc);
1331
1332 LogFlowFuncLeave();
1333}
1334
1335/* static */
1336DECLCALLBACK(int) Appliance::TaskOPC::updateProgress(unsigned uPercent, void *pvUser)
1337{
1338 Appliance::TaskOPC* pTask = *(Appliance::TaskOPC**)pvUser;
1339
1340 if ( pTask
1341 && !pTask->pProgress.isNull())
1342 {
1343 BOOL fCanceled;
1344 pTask->pProgress->COMGETTER(Canceled)(&fCanceled);
1345 if (fCanceled)
1346 return -1;
1347 pTask->pProgress->SetCurrentOperationProgress(uPercent);
1348 }
1349 return VINF_SUCCESS;
1350}
1351
1352/**
1353 * Worker for TaskCloud::handler.
1354 * @thread pTask The task.
1355 */
1356/* static */
1357void Appliance::i_importOrExportCloudThreadTask(TaskCloud *pTask)
1358{
1359 LogFlowFuncEnter();
1360 AssertReturnVoid(pTask);
1361
1362 Appliance *pAppliance = pTask->pAppliance;
1363 LogFlowFunc(("Appliance %p taskType=%d\n", pAppliance, pTask->taskType));
1364
1365 switch (pTask->taskType)
1366 {
1367 case TaskCloud::Export:
1368 pAppliance->i_setApplianceState(ApplianceExporting);
1369 pTask->rc = pAppliance->i_exportCloudImpl(pTask);
1370 break;
1371 case TaskCloud::Import:
1372 pAppliance->i_setApplianceState(ApplianceImporting);
1373 pTask->rc = pAppliance->i_importCloudImpl(pTask);
1374 break;
1375 case TaskCloud::ReadData:
1376 pAppliance->i_setApplianceState(ApplianceImporting);
1377 pTask->rc = pAppliance->i_gettingCloudData(pTask);
1378 break;
1379 default:
1380 AssertFailed();
1381 pTask->rc = E_FAIL;
1382 break;
1383 }
1384
1385 pAppliance->i_setApplianceState(ApplianceIdle);
1386
1387 if (!pTask->pProgress.isNull())
1388 pTask->pProgress->i_notifyComplete(pTask->rc);
1389
1390 LogFlowFuncLeave();
1391}
1392
1393/* static */
1394DECLCALLBACK(int) Appliance::TaskCloud::updateProgress(unsigned uPercent, void *pvUser)
1395{
1396 Appliance::TaskCloud* pTask = *(Appliance::TaskCloud**)pvUser;
1397
1398 if ( pTask
1399 && !pTask->pProgress.isNull())
1400 {
1401 BOOL fCanceled;
1402 pTask->pProgress->COMGETTER(Canceled)(&fCanceled);
1403 if (fCanceled)
1404 return -1;
1405 pTask->pProgress->SetCurrentOperationProgress(uPercent);
1406 }
1407 return VINF_SUCCESS;
1408}
1409
1410void i_parseURI(Utf8Str strUri, LocationInfo &locInfo)
1411{
1412 /* Check the URI for the protocol */
1413 if (strUri.startsWith("file://", Utf8Str::CaseInsensitive)) /* File based */
1414 {
1415 locInfo.storageType = VFSType_File;
1416 strUri = strUri.substr(sizeof("file://") - 1);
1417 }
1418 else if (strUri.startsWith("SunCloud://", Utf8Str::CaseInsensitive)) /* Sun Cloud service */
1419 {
1420 locInfo.storageType = VFSType_S3;
1421 strUri = strUri.substr(sizeof("SunCloud://") - 1);
1422 }
1423 else if (strUri.startsWith("S3://", Utf8Str::CaseInsensitive)) /* S3 service */
1424 {
1425 locInfo.storageType = VFSType_S3;
1426 strUri = strUri.substr(sizeof("S3://") - 1);
1427 }
1428 else if (strUri.startsWith("OCI://", Utf8Str::CaseInsensitive)) /* OCI service (storage or compute) */
1429 {
1430 locInfo.storageType = VFSType_Cloud;
1431 locInfo.strProvider = "OCI";
1432 strUri = strUri.substr(sizeof("OCI://") - 1);
1433 }
1434 else if (strUri.startsWith("webdav://", Utf8Str::CaseInsensitive)) /* webdav service */
1435 throw E_NOTIMPL;
1436
1437 /* Not necessary on a file based URI */
1438// if (locInfo.storageType != VFSType_File)
1439// {
1440// size_t uppos = strUri.find("@"); /* username:password combo */
1441// if (uppos != Utf8Str::npos)
1442// {
1443// locInfo.strUsername = strUri.substr(0, uppos);
1444// strUri = strUri.substr(uppos + 1);
1445// size_t upos = locInfo.strUsername.find(":");
1446// if (upos != Utf8Str::npos)
1447// {
1448// locInfo.strPassword = locInfo.strUsername.substr(upos + 1);
1449// locInfo.strUsername = locInfo.strUsername.substr(0, upos);
1450// }
1451// }
1452// size_t hpos = strUri.find("/"); /* hostname part */
1453// if (hpos != Utf8Str::npos)
1454// {
1455// locInfo.strHostname = strUri.substr(0, hpos);
1456// strUri = strUri.substr(hpos);
1457// }
1458// }
1459
1460 locInfo.strPath = strUri;
1461}
1462
1463
1464////////////////////////////////////////////////////////////////////////////////
1465//
1466// IVirtualSystemDescription constructor / destructor
1467//
1468////////////////////////////////////////////////////////////////////////////////
1469
1470/**
1471 * COM initializer.
1472 * @return
1473 */
1474HRESULT VirtualSystemDescription::init()
1475{
1476 /* Enclose the state transition NotReady->InInit->Ready */
1477 AutoInitSpan autoInitSpan(this);
1478 AssertReturn(autoInitSpan.isOk(), E_FAIL);
1479
1480 /* Initialize data */
1481 m = new Data();
1482 m->pConfig = NULL;
1483
1484 /* Confirm a successful initialization */
1485 autoInitSpan.setSucceeded();
1486 return S_OK;
1487}
1488
1489/**
1490* COM uninitializer.
1491*/
1492
1493void VirtualSystemDescription::uninit()
1494{
1495 if (m->pConfig)
1496 delete m->pConfig;
1497 delete m;
1498 m = NULL;
1499}
1500
1501
1502////////////////////////////////////////////////////////////////////////////////
1503//
1504// IVirtualSystemDescription public methods
1505//
1506////////////////////////////////////////////////////////////////////////////////
1507
1508/**
1509 * Public method implementation.
1510 */
1511HRESULT VirtualSystemDescription::getCount(ULONG *aCount)
1512{
1513 if (!aCount)
1514 return E_POINTER;
1515
1516 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1517
1518 *aCount = (ULONG)m->maDescriptions.size();
1519 return S_OK;
1520}
1521
1522/**
1523 * Public method implementation.
1524 */
1525HRESULT VirtualSystemDescription::getDescription(std::vector<VirtualSystemDescriptionType_T> &aTypes,
1526 std::vector<com::Utf8Str> &aRefs,
1527 std::vector<com::Utf8Str> &aOVFValues,
1528 std::vector<com::Utf8Str> &aVBoxValues,
1529 std::vector<com::Utf8Str> &aExtraConfigValues)
1530
1531{
1532 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1533 size_t c = m->maDescriptions.size();
1534 aTypes.resize(c);
1535 aRefs.resize(c);
1536 aOVFValues.resize(c);
1537 aVBoxValues.resize(c);
1538 aExtraConfigValues.resize(c);
1539
1540 for (size_t i = 0; i < c; i++)
1541 {
1542 const VirtualSystemDescriptionEntry &vsde = m->maDescriptions[i];
1543 aTypes[i] = vsde.type;
1544 aRefs[i] = vsde.strRef;
1545 aOVFValues[i] = vsde.strOvf;
1546 aVBoxValues[i] = vsde.strVBoxCurrent;
1547 aExtraConfigValues[i] = vsde.strExtraConfigCurrent;
1548 }
1549 return S_OK;
1550}
1551
1552/**
1553 * Public method implementation.
1554 */
1555HRESULT VirtualSystemDescription::getDescriptionByType(VirtualSystemDescriptionType_T aType,
1556 std::vector<VirtualSystemDescriptionType_T> &aTypes,
1557 std::vector<com::Utf8Str> &aRefs,
1558 std::vector<com::Utf8Str> &aOVFValues,
1559 std::vector<com::Utf8Str> &aVBoxValues,
1560 std::vector<com::Utf8Str> &aExtraConfigValues)
1561{
1562 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1563 std::list<VirtualSystemDescriptionEntry*> vsd = i_findByType(aType);
1564
1565 size_t c = vsd.size();
1566 aTypes.resize(c);
1567 aRefs.resize(c);
1568 aOVFValues.resize(c);
1569 aVBoxValues.resize(c);
1570 aExtraConfigValues.resize(c);
1571
1572 size_t i = 0;
1573 for (list<VirtualSystemDescriptionEntry*>::const_iterator it = vsd.begin(); it != vsd.end(); ++it, ++i)
1574 {
1575 const VirtualSystemDescriptionEntry *vsde = (*it);
1576 aTypes[i] = vsde->type;
1577 aRefs[i] = vsde->strRef;
1578 aOVFValues[i] = vsde->strOvf;
1579 aVBoxValues[i] = vsde->strVBoxCurrent;
1580 aExtraConfigValues[i] = vsde->strExtraConfigCurrent;
1581 }
1582
1583 return S_OK;
1584}
1585
1586/**
1587 * Public method implementation.
1588 */
1589HRESULT VirtualSystemDescription::getValuesByType(VirtualSystemDescriptionType_T aType,
1590 VirtualSystemDescriptionValueType_T aWhich,
1591 std::vector<com::Utf8Str> &aValues)
1592{
1593 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1594
1595 std::list<VirtualSystemDescriptionEntry*> vsd = i_findByType (aType);
1596 aValues.resize((ULONG)vsd.size());
1597
1598 list<VirtualSystemDescriptionEntry*>::const_iterator it;
1599 size_t i = 0;
1600 for (it = vsd.begin();
1601 it != vsd.end();
1602 ++it, ++i)
1603 {
1604 const VirtualSystemDescriptionEntry *vsde = (*it);
1605
1606 Bstr bstr;
1607 switch (aWhich)
1608 {
1609 case VirtualSystemDescriptionValueType_Reference: aValues[i] = vsde->strRef; break;
1610 case VirtualSystemDescriptionValueType_Original: aValues[i] = vsde->strOvf; break;
1611 case VirtualSystemDescriptionValueType_Auto: aValues[i] = vsde->strVBoxCurrent; break;
1612 case VirtualSystemDescriptionValueType_ExtraConfig: aValues[i] = vsde->strExtraConfigCurrent; break;
1613#ifdef VBOX_WITH_XPCOM_CPP_ENUM_HACK
1614 case VirtualSystemDescriptionValueType_32BitHack: AssertFailedBreak(); /* (compiler warnings) */
1615#endif
1616 }
1617 }
1618
1619 return S_OK;
1620}
1621
1622/**
1623 * Public method implementation.
1624 */
1625HRESULT VirtualSystemDescription::setFinalValues(const std::vector<BOOL> &aEnabled,
1626 const std::vector<com::Utf8Str> &aVBoxValues,
1627 const std::vector<com::Utf8Str> &aExtraConfigValues)
1628{
1629#ifndef RT_OS_WINDOWS
1630 // NOREF(aEnabledSize);
1631#endif /* RT_OS_WINDOWS */
1632 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1633
1634 if ( (aEnabled.size() != m->maDescriptions.size())
1635 || (aVBoxValues.size() != m->maDescriptions.size())
1636 || (aExtraConfigValues.size() != m->maDescriptions.size())
1637 )
1638 return E_INVALIDARG;
1639
1640 size_t i = 0;
1641 for (vector<VirtualSystemDescriptionEntry>::iterator it = m->maDescriptions.begin();
1642 it != m->maDescriptions.end();
1643 ++it, ++i)
1644 {
1645 VirtualSystemDescriptionEntry& vsde = *it;
1646
1647 if (aEnabled[i])
1648 {
1649 vsde.strVBoxCurrent = aVBoxValues[i];
1650 vsde.strExtraConfigCurrent = aExtraConfigValues[i];
1651 }
1652 else
1653 vsde.type = VirtualSystemDescriptionType_Ignore;
1654 }
1655
1656 return S_OK;
1657}
1658
1659/**
1660 * Public method implementation.
1661 */
1662HRESULT VirtualSystemDescription::addDescription(VirtualSystemDescriptionType_T aType,
1663 const com::Utf8Str &aVBoxValue,
1664 const com::Utf8Str &aExtraConfigValue)
1665
1666{
1667 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1668 i_addEntry(aType, "", aVBoxValue, aVBoxValue, 0, aExtraConfigValue);
1669 return S_OK;
1670}
1671
1672/**
1673 * Internal method; adds a new description item to the member list.
1674 * @param aType Type of description for the new item.
1675 * @param strRef Reference item; only used with storage controllers.
1676 * @param aOvfValue Corresponding original value from OVF.
1677 * @param aVBoxValue Initial configuration value (can be overridden by caller with setFinalValues).
1678 * @param ulSizeMB Weight for IProgress
1679 * @param strExtraConfig Extra configuration; meaning dependent on type.
1680 */
1681void VirtualSystemDescription::i_addEntry(VirtualSystemDescriptionType_T aType,
1682 const Utf8Str &strRef,
1683 const Utf8Str &aOvfValue,
1684 const Utf8Str &aVBoxValue,
1685 uint32_t ulSizeMB,
1686 const Utf8Str &strExtraConfig /*= ""*/)
1687{
1688 VirtualSystemDescriptionEntry vsde;
1689 vsde.ulIndex = (uint32_t)m->maDescriptions.size(); // each entry gets an index so the client side can reference them
1690 vsde.type = aType;
1691 vsde.strRef = strRef;
1692 vsde.strOvf = aOvfValue;
1693 vsde.strVBoxSuggested /* remember original value */
1694 = vsde.strVBoxCurrent /* and set current value which can be overridden by setFinalValues() */
1695 = aVBoxValue;
1696 vsde.strExtraConfigSuggested
1697 = vsde.strExtraConfigCurrent
1698 = strExtraConfig;
1699 vsde.ulSizeMB = ulSizeMB;
1700
1701 vsde.skipIt = false;
1702
1703 m->maDescriptions.push_back(vsde);
1704}
1705
1706/**
1707 * Private method; returns a list of description items containing all the items from the member
1708 * description items of this virtual system that match the given type.
1709 */
1710std::list<VirtualSystemDescriptionEntry*> VirtualSystemDescription::i_findByType(VirtualSystemDescriptionType_T aType)
1711{
1712 std::list<VirtualSystemDescriptionEntry*> vsd;
1713 for (vector<VirtualSystemDescriptionEntry>::iterator it = m->maDescriptions.begin();
1714 it != m->maDescriptions.end();
1715 ++it)
1716 {
1717 if (it->type == aType)
1718 vsd.push_back(&(*it));
1719 }
1720
1721 return vsd;
1722}
1723
1724HRESULT VirtualSystemDescription::removeDescriptionByType(VirtualSystemDescriptionType_T aType)
1725{
1726 std::vector<VirtualSystemDescriptionEntry>::iterator it = m->maDescriptions.begin();
1727 while (it != m->maDescriptions.end())
1728 {
1729 if (it->type == aType)
1730 it = m->maDescriptions.erase(it);
1731 else
1732 ++it;
1733 }
1734
1735 return S_OK;
1736}
1737
1738/* Private method; delete all records from the list
1739 * m->llDescriptions that match the given type.
1740 */
1741void VirtualSystemDescription::i_removeByType(VirtualSystemDescriptionType_T aType)
1742{
1743 std::vector<VirtualSystemDescriptionEntry>::iterator it = m->maDescriptions.begin();
1744 while (it != m->maDescriptions.end())
1745 {
1746 if (it->type == aType)
1747 it = m->maDescriptions.erase(it);
1748 else
1749 ++it;
1750 }
1751}
1752
1753/**
1754 * Private method; looks thru the member hardware items for the IDE, SATA, or SCSI controller with
1755 * the given reference ID. Useful when needing the controller for a particular
1756 * virtual disk.
1757 */
1758const VirtualSystemDescriptionEntry* VirtualSystemDescription::i_findControllerFromID(uint32_t id)
1759{
1760 Utf8Str strRef = Utf8StrFmt("%RI32", id);
1761 vector<VirtualSystemDescriptionEntry>::const_iterator it;
1762 for (it = m->maDescriptions.begin();
1763 it != m->maDescriptions.end();
1764 ++it)
1765 {
1766 const VirtualSystemDescriptionEntry &d = *it;
1767 switch (d.type)
1768 {
1769 case VirtualSystemDescriptionType_HardDiskControllerIDE:
1770 case VirtualSystemDescriptionType_HardDiskControllerSATA:
1771 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
1772 case VirtualSystemDescriptionType_HardDiskControllerSAS:
1773 if (d.strRef == strRef)
1774 return &d;
1775 break;
1776 default: break; /* Shut up MSC. */
1777 }
1778 }
1779
1780 return NULL;
1781}
1782
1783/**
1784 * Method called from Appliance::Interpret() if the source OVF for a virtual system
1785 * contains a <vbox:Machine> element. This method then attempts to parse that and
1786 * create a MachineConfigFile instance from it which is stored in this instance data
1787 * and can then be used to create a machine.
1788 *
1789 * This must only be called once per instance.
1790 *
1791 * This rethrows all XML and logic errors from MachineConfigFile.
1792 *
1793 * @param elmMachine <vbox:Machine> element with attributes and subelements from some
1794 * DOM tree.
1795 */
1796void VirtualSystemDescription::i_importVBoxMachineXML(const xml::ElementNode &elmMachine)
1797{
1798 settings::MachineConfigFile *pConfig = NULL;
1799
1800 Assert(m->pConfig == NULL);
1801
1802 try
1803 {
1804 pConfig = new settings::MachineConfigFile(NULL);
1805 pConfig->importMachineXML(elmMachine);
1806
1807 m->pConfig = pConfig;
1808 }
1809 catch (...)
1810 {
1811 if (pConfig)
1812 delete pConfig;
1813 throw;
1814 }
1815}
1816
1817/**
1818 * Returns the machine config created by importVBoxMachineXML() or NULL if there's none.
1819 */
1820const settings::MachineConfigFile* VirtualSystemDescription::i_getMachineConfig() const
1821{
1822 return m->pConfig;
1823}
1824
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