VirtualBox

source: vbox/trunk/src/VBox/Main/ApplianceImpl.cpp@ 16560

Last change on this file since 16560 was 16560, checked in by vboxsync, 15 years ago

Main: do not include include/VBox/settings.h from other header files but only from implementations that need it (save compile time)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 90.3 KB
Line 
1/* $Id: ApplianceImpl.cpp 16560 2009-02-06 18:06:04Z vboxsync $ */
2/** @file
3 *
4 * IAppliance and IVirtualSystem COM class implementations
5 */
6
7/*
8 * Copyright (C) 2008-2009 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#include <iprt/stream.h>
24#include <iprt/path.h>
25#include <iprt/dir.h>
26#include <iprt/file.h>
27
28#include "ApplianceImpl.h"
29#include "VirtualBoxImpl.h"
30#include "GuestOSTypeImpl.h"
31#include "ProgressImpl.h"
32
33#include "Logging.h"
34
35#include "VBox/xml.h"
36
37#include <iostream>
38#include <sstream>
39
40using namespace std;
41
42// defines
43////////////////////////////////////////////////////////////////////////////////
44
45struct DiskImage
46{
47 Utf8Str strDiskId; // value from DiskSection/Disk/@diskId
48 int64_t iCapacity; // value from DiskSection/Disk/@capacity;
49 // (maximum size for dynamic images, I guess; we always translate this to bytes)
50 int64_t iPopulatedSize; // value from DiskSection/Disk/@populatedSize
51 // (actual used size of disk, always in bytes; can be an estimate of used disk
52 // space, but cannot be larger than iCapacity)
53 Utf8Str strFormat; // value from DiskSection/Disk/@format
54 // typically http://www.vmware.com/specifications/vmdk.html#sparse
55
56 // fields from /References/File; the spec says the file reference from disk can be empty,
57 // so in that case, strFilename will be empty, then a new disk should be created
58 Utf8Str strHref; // value from /References/File/@href (filename); if empty, then the remaining fields are ignored
59 int64_t iSize; // value from /References/File/@size (optional according to spec; then we set -1 here)
60 int64_t iChunkSize; // value from /References/File/@chunkSize (optional, unsupported)
61 Utf8Str strCompression; // value from /References/File/@compression (optional, can be "gzip" according to spec)
62};
63
64struct Network
65{
66 Utf8Str strNetworkName; // value from NetworkSection/Network/@name
67 // unfortunately the OVF spec is unspecific about how networks should be specified further
68};
69
70struct VirtualHardwareItem
71{
72 Utf8Str strDescription;
73 Utf8Str strCaption;
74 Utf8Str strElementName;
75
76 uint32_t ulInstanceID;
77 uint32_t ulParent;
78
79 OVFResourceType_T resourceType;
80 Utf8Str strOtherResourceType;
81 Utf8Str strResourceSubType;
82
83 Utf8Str strHostResource; // "Abstractly specifies how a device shall connect to a resource on the deployment platform.
84 // Not all devices need a backing." Used with disk items, for which this references a virtual
85 // disk from the Disks section.
86 bool fAutomaticAllocation;
87 bool fAutomaticDeallocation;
88 Utf8Str strConnection; // "All Ethernet adapters that specify the same abstract network connection name within an OVF
89 // package shall be deployed on the same network. The abstract network connection name shall be
90 // listed in the NetworkSection at the outermost envelope level."
91 Utf8Str strAddress; // "Device-specific. For an Ethernet adapter, this specifies the MAC address."
92 Utf8Str strAddressOnParent; // "For a device, this specifies its location on the controller."
93 Utf8Str strAllocationUnits; // "Specifies the units of allocation used. For example, “byte * 2^20”."
94 uint64_t ullVirtualQuantity; // "Specifies the quantity of resources presented. For example, “256”."
95 uint64_t ullReservation; // "Specifies the minimum quantity of resources guaranteed to be available."
96 uint64_t ullLimit; // "Specifies the maximum quantity of resources that will be granted."
97 uint64_t ullWeight; // "Specifies a relative priority for this allocation in relation to other allocations."
98
99 Utf8Str strConsumerVisibility;
100 Utf8Str strMappingBehavior;
101 Utf8Str strPoolID;
102 uint32_t ulBusNumber; // seen with IDE controllers, but not listed in OVF spec
103
104 uint32_t ulLineNumber; // line number of <Item> element in XML source; cached for error messages
105
106 VirtualHardwareItem()
107 : ulInstanceID(0), fAutomaticAllocation(false), fAutomaticDeallocation(false), ullVirtualQuantity(0), ullReservation(0), ullLimit(0), ullWeight(0), ulBusNumber(0), ulLineNumber(0)
108 {};
109};
110
111typedef map<Utf8Str, DiskImage> DiskImagesMap;
112typedef map<Utf8Str, Network> NetworksMap;
113
114struct VirtualSystem;
115
116// opaque private instance data of Appliance class
117struct Appliance::Data
118{
119 Bstr bstrPath;
120
121 DiskImagesMap mapDisks; // map of DiskImage structs, sorted by DiskImage.strDiskId
122
123 NetworksMap mapNetworks; // map of Network structs, sorted by Network.strNetworkName
124
125 list<VirtualSystem> llVirtualSystems;
126
127 list< ComObjPtr<VirtualSystemDescription> > virtualSystemDescriptions;
128};
129
130typedef map<uint32_t, VirtualHardwareItem> HardwareItemsMap;
131
132struct HardDiskController
133{
134 uint32_t idController; // instance ID (Item/InstanceId); this gets referenced from HardDisk
135 enum ControllerSystemType { IDE, SATA, SCSI };
136 ControllerSystemType system; // one of IDE, SATA, SCSI
137 Utf8Str strControllerType; // controller subtype (Item/ResourceSubType); e.g. "LsiLogic"; can be empty (esp. for IDE)
138 Utf8Str strAddress; // for IDE
139 uint32_t ulBusNumber; // for IDE
140
141 HardDiskController()
142 : idController(0),
143 ulBusNumber(0)
144 {
145 }
146};
147
148typedef map<uint32_t, HardDiskController> ControllersMap;
149
150struct VirtualDisk
151{
152 uint32_t idController; // SCSI (or IDE) controller this disk is connected to;
153 // points into VirtualSystem.mapControllers
154 Utf8Str strDiskId; // if the hard disk has an ovf:/disk/<id> reference,
155 // this receives the <id> component; points to one of the
156 // references in Appliance::Data.mapDisks
157};
158
159typedef map<Utf8Str, VirtualDisk> VirtualDisksMap;
160
161struct VirtualSystem
162{
163 Utf8Str strName; // copy of VirtualSystem/@id
164
165 CIMOSType_T cimos;
166 Utf8Str strVirtualSystemType; // generic hardware description; OVF says this can be something like "vmx-4" or "xen";
167 // VMware Workstation 6.5 is "vmx-07"
168
169 HardwareItemsMap mapHardwareItems; // map of virtual hardware items, sorted by unique instance ID
170
171 uint64_t ullMemorySize; // always in bytes, copied from llHardwareItems; default = 0 (unspecified)
172 uint16_t cCPUs; // no. of CPUs, copied from llHardwareItems; default = 1
173
174 list<Utf8Str> llNetworkNames;
175 // list of strings referring to network names
176 // (one for each VirtualSystem/Item[@ResourceType=10]/Connection element)
177
178 ControllersMap mapControllers;
179 // list of hard disk controllers
180 // (one for each VirtualSystem/Item[@ResourceType=6] element with accumulated data from children)
181
182 VirtualDisksMap mapVirtualDisks;
183 // (one for each VirtualSystem/Item[@ResourceType=17] element with accumulated data from children)
184
185 bool fHasFloppyDrive; // true if there's a floppy item in mapHardwareItems
186 bool fHasCdromDrive; // true if there's a CD-ROM item in mapHardwareItems; ISO images are not yet supported by OVFtool
187 bool fHasUsbController; // true if there's a USB controller item in mapHardwareItems
188
189 Utf8Str strSoundCardType; // if not empty, then the system wants a soundcard; this then specifies the hardware;
190 // VMware Workstation 6.5 uses "ensoniq1371" for example
191
192 Utf8Str strLicenceInfo; // license info if any; receives contents of VirtualSystem/EulaSection/Info
193 Utf8Str strLicenceText; // license info if any; receives contents of VirtualSystem/EulaSection/License
194
195 VirtualSystem()
196 : ullMemorySize(0), cCPUs(1), fHasFloppyDrive(false), fHasCdromDrive(false), fHasUsbController(false)
197 {
198 }
199};
200
201struct Appliance::Task
202{
203 Task(Appliance *aThat, Progress *aProgress)
204 : that(aThat)
205 , progress(aProgress)
206 , rc(S_OK)
207 {}
208 ~Task() {}
209
210 HRESULT startThread();
211
212 Appliance *that;
213 ComObjPtr<Progress> progress;
214 HRESULT rc;
215};
216
217// globals
218////////////////////////////////////////////////////////////////////////////////
219
220template <class T>
221inline
222com::Utf8Str toString(const T& val)
223{
224 // @todo optimize
225 std::ostringstream ss;
226 ss << val;
227 return Utf8Str(ss.str().c_str());
228}
229
230static Utf8Str stripFilename(const Utf8Str &strFile)
231{
232 Utf8Str str2(strFile);
233 RTPathStripFilename(str2.mutableRaw());
234 return str2;
235}
236
237// IVirtualBox public methods
238////////////////////////////////////////////////////////////////////////////////
239
240/**
241 * Implementation for IVirtualBox::openAppliance. Loads the given appliance (see API reference).
242 *
243 * @param bstrPath Appliance to open (either .ovf or .ova file, see API reference)
244 * @param anAppliance IAppliance object created if S_OK is returned.
245 * @return S_OK or error.
246 */
247STDMETHODIMP VirtualBox::OpenAppliance(IN_BSTR bstrPath, IAppliance** anAppliance)
248{
249 HRESULT rc;
250
251 ComObjPtr<Appliance> appliance;
252 appliance.createObject();
253 rc = appliance->init(this, bstrPath);
254// ComAssertComRCThrowRC(rc);
255
256 if (SUCCEEDED(rc))
257 appliance.queryInterfaceTo(anAppliance);
258
259 return rc;
260}
261
262// Appliance::task methods
263////////////////////////////////////////////////////////////////////////////////
264
265HRESULT Appliance::Task::startThread()
266{
267 int vrc = RTThreadCreate(NULL, Appliance::taskThread, this,
268 0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
269 "Appliance::Task");
270 ComAssertMsgRCRet(vrc,
271 ("Could not create Appliance::Task thread (%Rrc)\n", vrc), E_FAIL);
272
273 return S_OK;
274}
275
276// IAppliance constructor / destructor
277////////////////////////////////////////////////////////////////////////////////
278
279DEFINE_EMPTY_CTOR_DTOR(Appliance)
280struct shutup {};
281
282// IAppliance private methods
283////////////////////////////////////////////////////////////////////////////////
284
285/**
286 * Private helper method that goes thru the elements of the given "current" element in the OVF XML
287 * and handles the contained child elements (which can be "Section" or "Content" elements).
288 *
289 * @param pcszPath Path spec of the XML file, for error messages.
290 * @param pReferencesElement "References" element from OVF, for looking up file specifications; can be NULL if no such element is present.
291 * @param pCurElem Element whose children are to be analyzed here.
292 * @return
293 */
294HRESULT Appliance::LoopThruSections(const char *pcszPath,
295 const xml::Node *pReferencesElem,
296 const xml::Node *pCurElem)
297{
298 HRESULT rc;
299
300 xml::NodesLoop loopChildren(*pCurElem);
301 const xml::Node *pElem;
302 while ((pElem = loopChildren.forAllNodes()))
303 {
304 const char *pcszElemName = pElem->getName();
305 const char *pcszTypeAttr = "";
306 const xml::Node *pTypeAttr;
307 if ((pTypeAttr = pElem->findAttribute("type")))
308 pcszTypeAttr = pTypeAttr->getValue();
309
310 if ( (!strcmp(pcszElemName, "DiskSection"))
311 || ( (!strcmp(pcszElemName, "Section"))
312 && (!strcmp(pcszTypeAttr, "ovf:DiskSection_Type"))
313 )
314 )
315 {
316 if (!(SUCCEEDED((rc = HandleDiskSection(pcszPath, pReferencesElem, pElem)))))
317 return rc;
318 }
319 else if ( (!strcmp(pcszElemName, "NetworkSection"))
320 || ( (!strcmp(pcszElemName, "Section"))
321 && (!strcmp(pcszTypeAttr, "ovf:NetworkSection_Type"))
322 )
323 )
324 {
325 if (!(SUCCEEDED((rc = HandleNetworkSection(pcszPath, pElem)))))
326 return rc;
327 }
328 else if ( (!strcmp(pcszElemName, "DeploymentOptionSection>")))
329 {
330 // TODO
331 }
332 else if ( (!strcmp(pcszElemName, "Info")))
333 {
334 // child of VirtualSystemCollection -- TODO
335 }
336 else if ( (!strcmp(pcszElemName, "ResourceAllocationSection")))
337 {
338 // child of VirtualSystemCollection -- TODO
339 }
340 else if ( (!strcmp(pcszElemName, "StartupSection")))
341 {
342 // child of VirtualSystemCollection -- TODO
343 }
344 else if ( (!strcmp(pcszElemName, "VirtualSystem"))
345 || ( (!strcmp(pcszElemName, "Content"))
346 && (!strcmp(pcszTypeAttr, "ovf:VirtualSystem_Type"))
347 )
348 )
349 {
350 if (!(SUCCEEDED((rc = HandleVirtualSystemContent(pcszPath, pElem)))))
351 return rc;
352 }
353 else if ( (!strcmp(pcszElemName, "VirtualSystemCollection"))
354 || ( (!strcmp(pcszElemName, "Content"))
355 && (!strcmp(pcszTypeAttr, "ovf:VirtualSystemCollection_Type"))
356 )
357 )
358 {
359 // TODO ResourceAllocationSection
360
361 // recurse for this, since it has VirtualSystem elements as children
362 if (!(SUCCEEDED((rc = LoopThruSections(pcszPath, pReferencesElem, pElem)))))
363 return rc;
364 }
365 }
366
367 return S_OK;
368}
369
370/**
371 * Private helper method that handles disk sections in the OVF XML.
372 * @param pcszPath Path spec of the XML file, for error messages.
373 * @param pReferencesElement "References" element from OVF, for looking up file specifications; can be NULL if no such element is present.
374 * @param pSectionElem Section element for which this helper is getting called.
375 * @return
376 */
377HRESULT Appliance::HandleDiskSection(const char *pcszPath,
378 const xml::Node *pReferencesElem,
379 const xml::Node *pSectionElem)
380{
381 // contains "Disk" child elements
382 xml::NodesLoop loopDisks(*pSectionElem, "Disk");
383 const xml::Node *pelmDisk;
384 while ((pelmDisk = loopDisks.forAllNodes()))
385 {
386 DiskImage d;
387 const char *pcszBad = NULL;
388 if (!(pelmDisk->getAttributeValue("diskId", d.strDiskId)))
389 pcszBad = "diskId";
390 else if (!(pelmDisk->getAttributeValue("format", d.strFormat)))
391 pcszBad = "format";
392 else if (!(pelmDisk->getAttributeValue("capacity", d.iCapacity)))
393 pcszBad = "capacity";
394 else
395 {
396 if (!(pelmDisk->getAttributeValue("populatedSize", d.iPopulatedSize)))
397 // optional
398 d.iPopulatedSize = -1;
399
400 Utf8Str strFileRef;
401 if (pelmDisk->getAttributeValue("fileRef", strFileRef)) // optional
402 {
403 // look up corresponding /References/File nodes (list built above)
404 const xml::Node *pFileElem;
405 if ( pReferencesElem
406 && ((pFileElem = pReferencesElem->findChildElementFromId(strFileRef.c_str())))
407 )
408 {
409 // copy remaining values from file node then
410 const char *pcszBadInFile = NULL;
411 if (!(pFileElem->getAttributeValue("href", d.strHref)))
412 pcszBadInFile = "href";
413 else if (!(pFileElem->getAttributeValue("size", d.iSize)))
414 d.iSize = -1; // optional
415 // if (!(pFileElem->getAttributeValue("size", d.iChunkSize))) TODO
416 d.iChunkSize = -1; // optional
417 pFileElem->getAttributeValue("compression", d.strCompression);
418
419 if (pcszBadInFile)
420 return setError(VBOX_E_FILE_ERROR,
421 tr("Error reading \"%s\": missing or invalid attribute '%s' in 'File' element, line %d"),
422 pcszPath,
423 pcszBadInFile,
424 pFileElem->getLineNumber());
425 }
426 else
427 return setError(VBOX_E_FILE_ERROR,
428 tr("Error reading \"%s\": cannot find References/File element for ID '%s' referenced by 'Disk' element, line %d"),
429 pcszPath,
430 strFileRef.c_str(),
431 pelmDisk->getLineNumber());
432 }
433 }
434
435 if (pcszBad)
436 return setError(VBOX_E_FILE_ERROR,
437 tr("Error reading \"%s\": missing or invalid attribute '%s' in 'DiskSection' element, line %d"),
438 pcszPath,
439 pcszBad,
440 pelmDisk->getLineNumber());
441
442 m->mapDisks[d.strDiskId] = d;
443 }
444
445 return S_OK;
446}
447
448/**
449 * Private helper method that handles network sections in the OVF XML.
450 * @param pcszPath Path spec of the XML file, for error messages.
451 * @param pSectionElem Section element for which this helper is getting called.
452 * @return
453 */
454HRESULT Appliance::HandleNetworkSection(const char *pcszPath,
455 const xml::Node *pSectionElem)
456{
457 // contains "Disk" child elements
458 xml::NodesLoop loopNetworks(*pSectionElem, "Network");
459 const xml::Node *pelmNetwork;
460 while ((pelmNetwork = loopNetworks.forAllNodes()))
461 {
462 Network n;
463 if (!(pelmNetwork->getAttributeValue("name", n.strNetworkName)))
464 return setError(VBOX_E_FILE_ERROR,
465 tr("Error reading \"%s\": missing 'name' attribute in 'Network', line %d"),
466 pcszPath,
467 pelmNetwork->getLineNumber());
468
469 m->mapNetworks[n.strNetworkName] = n;
470 }
471
472 return S_OK;
473}
474
475/**
476 * Private helper method that handles a "VirtualSystem" element in the OVF XML.
477 *
478 * @param pcszPath
479 * @param pContentElem
480 * @return
481 */
482HRESULT Appliance::HandleVirtualSystemContent(const char *pcszPath,
483 const xml::Node *pelmVirtualSystem)
484{
485 VirtualSystem vsys;
486
487 const xml::Node *pIdAttr = pelmVirtualSystem->findAttribute("id");
488 if (pIdAttr)
489 vsys.strName = pIdAttr->getValue();
490
491 xml::NodesLoop loop(*pelmVirtualSystem); // all child elements
492 const xml::Node *pelmThis;
493 while ((pelmThis = loop.forAllNodes()))
494 {
495 const char *pcszElemName = pelmThis->getName();
496 const xml::Node *pTypeAttr = pelmThis->findAttribute("type");
497 const char *pcszTypeAttr = (pTypeAttr) ? pTypeAttr->getValue() : "";
498
499 if (!strcmp(pcszElemName, "EulaSection"))
500 {
501 /* <EulaSection>
502 <Info ovf:msgid="6">License agreement for the Virtual System.</Info>
503 <License ovf:msgid="1">License terms can go in here.</License>
504 </EulaSection> */
505
506 const xml::Node *pelmInfo, *pelmLicense;
507 if ( ((pelmInfo = pelmThis->findChildElement("Info")))
508 && ((pelmLicense = pelmThis->findChildElement("License")))
509 )
510 {
511 vsys.strLicenceInfo = pelmInfo->getValue();
512 vsys.strLicenceText = pelmLicense->getValue();
513 }
514 }
515 else if ( (!strcmp(pcszElemName, "VirtualHardwareSection"))
516 || (!strcmp(pcszTypeAttr, "ovf:VirtualHardwareSection_Type"))
517 )
518 {
519 const xml::Node *pelmSystem, *pelmVirtualSystemType;
520 if ((pelmSystem = pelmThis->findChildElement("System")))
521 {
522 /* <System>
523 <vssd:Description>Description of the virtual hardware section.</vssd:Description>
524 <vssd:ElementName>vmware</vssd:ElementName>
525 <vssd:InstanceID>1</vssd:InstanceID>
526 <vssd:VirtualSystemIdentifier>MyLampService</vssd:VirtualSystemIdentifier>
527 <vssd:VirtualSystemType>vmx-4</vssd:VirtualSystemType>
528 </System>*/
529 if ((pelmVirtualSystemType = pelmSystem->findChildElement("VirtualSystemType")))
530 vsys.strVirtualSystemType = pelmVirtualSystemType->getValue();
531 }
532
533 xml::NodesLoop loopVirtualHardwareItems(*pelmThis, "Item"); // all "Item" child elements
534 const xml::Node *pelmItem;
535 while ((pelmItem = loopVirtualHardwareItems.forAllNodes()))
536 {
537 VirtualHardwareItem i;
538
539 i.ulLineNumber = pelmItem->getLineNumber();
540
541 xml::NodesLoop loopItemChildren(*pelmItem); // all child elements
542 const xml::Node *pelmItemChild;
543 while ((pelmItemChild = loopItemChildren.forAllNodes()))
544 {
545 const char *pcszItemChildName = pelmItemChild->getName();
546 if (!strcmp(pcszItemChildName, "Description"))
547 i.strDescription = pelmItemChild->getValue();
548 else if (!strcmp(pcszItemChildName, "Caption"))
549 i.strCaption = pelmItemChild->getValue();
550 else if (!strcmp(pcszItemChildName, "ElementName"))
551 i.strElementName = pelmItemChild->getValue();
552 else if ( (!strcmp(pcszItemChildName, "InstanceID"))
553 || (!strcmp(pcszItemChildName, "InstanceId"))
554 )
555 pelmItemChild->copyValue(i.ulInstanceID);
556 else if (!strcmp(pcszItemChildName, "HostResource"))
557 i.strHostResource = pelmItemChild->getValue();
558 else if (!strcmp(pcszItemChildName, "ResourceType"))
559 {
560 int32_t iType; /** @todo how to fix correctly? (enum fun.) */
561 pelmItemChild->copyValue(iType);
562 i.resourceType = (OVFResourceType_T)iType;
563 }
564 else if (!strcmp(pcszItemChildName, "OtherResourceType"))
565 i.strOtherResourceType = pelmItemChild->getValue();
566 else if (!strcmp(pcszItemChildName, "ResourceSubType"))
567 i.strResourceSubType = pelmItemChild->getValue();
568 else if (!strcmp(pcszItemChildName, "AutomaticAllocation"))
569 i.fAutomaticAllocation = (!strcmp(pelmItemChild->getValue(), "true")) ? true : false;
570 else if (!strcmp(pcszItemChildName, "AutomaticDeallocation"))
571 i.fAutomaticDeallocation = (!strcmp(pelmItemChild->getValue(), "true")) ? true : false;
572 else if (!strcmp(pcszItemChildName, "Parent"))
573 pelmItemChild->copyValue(i.ulParent);
574 else if (!strcmp(pcszItemChildName, "Connection"))
575 i.strConnection = pelmItemChild->getValue();
576 else if (!strcmp(pcszItemChildName, "Address"))
577 i.strAddress = pelmItemChild->getValue();
578 else if (!strcmp(pcszItemChildName, "AddressOnParent"))
579 i.strAddressOnParent = pelmItemChild->getValue();
580 else if (!strcmp(pcszItemChildName, "AllocationUnits"))
581 i.strAllocationUnits = pelmItemChild->getValue();
582 else if (!strcmp(pcszItemChildName, "VirtualQuantity"))
583 pelmItemChild->copyValue(i.ullVirtualQuantity);
584 else if (!strcmp(pcszItemChildName, "Reservation"))
585 pelmItemChild->copyValue(i.ullReservation);
586 else if (!strcmp(pcszItemChildName, "Limit"))
587 pelmItemChild->copyValue(i.ullLimit);
588 else if (!strcmp(pcszItemChildName, "Weight"))
589 pelmItemChild->copyValue(i.ullWeight);
590 else if (!strcmp(pcszItemChildName, "ConsumerVisibility"))
591 i.strConsumerVisibility = pelmItemChild->getValue();
592 else if (!strcmp(pcszItemChildName, "MappingBehavior"))
593 i.strMappingBehavior = pelmItemChild->getValue();
594 else if (!strcmp(pcszItemChildName, "PoolID"))
595 i.strPoolID = pelmItemChild->getValue();
596 else if (!strcmp(pcszItemChildName, "BusNumber"))
597 pelmItemChild->copyValue(i.ulBusNumber);
598 else
599 return setError(VBOX_E_FILE_ERROR,
600 tr("Error reading \"%s\": unknown element \"%s\" under Item element, line %d"),
601 pcszPath,
602 pcszItemChildName,
603 i.ulLineNumber);
604 }
605
606 // store!
607 vsys.mapHardwareItems[i.ulInstanceID] = i;
608 }
609
610 HardwareItemsMap::const_iterator itH;
611
612 for (itH = vsys.mapHardwareItems.begin();
613 itH != vsys.mapHardwareItems.end();
614 ++itH)
615 {
616 const VirtualHardwareItem &i = itH->second;
617
618 // do some analysis
619 switch (i.resourceType)
620 {
621 case OVFResourceType_Processor: // 3
622 /* <rasd:Caption>1 virtual CPU</rasd:Caption>
623 <rasd:Description>Number of virtual CPUs</rasd:Description>
624 <rasd:ElementName>virtual CPU</rasd:ElementName>
625 <rasd:InstanceID>1</rasd:InstanceID>
626 <rasd:ResourceType>3</rasd:ResourceType>
627 <rasd:VirtualQuantity>1</rasd:VirtualQuantity>*/
628 if (i.ullVirtualQuantity < UINT16_MAX)
629 vsys.cCPUs = (uint16_t)i.ullVirtualQuantity;
630 else
631 return setError(VBOX_E_FILE_ERROR,
632 tr("Error reading \"%s\": CPU count %RI64 is larger than %d, line %d"),
633 pcszPath,
634 i.ullVirtualQuantity,
635 UINT16_MAX,
636 i.ulLineNumber);
637 break;
638
639 case OVFResourceType_Memory: // 4
640 if ( (i.strAllocationUnits == "MegaBytes") // found in OVF created by OVF toolkit
641 || (i.strAllocationUnits == "MB") // found in MS docs
642 || (i.strAllocationUnits == "byte * 2^20") // suggested by OVF spec DSP0243 page 21
643 )
644 vsys.ullMemorySize = i.ullVirtualQuantity * 1024 * 1024;
645 else
646 return setError(VBOX_E_FILE_ERROR,
647 tr("Error reading \"%s\": Invalid allocation unit \"%s\" specified with memory size item, line %d"),
648 pcszPath,
649 i.strAllocationUnits.c_str(),
650 i.ulLineNumber);
651 break;
652
653 case OVFResourceType_IdeController: // 5 IdeController
654 {
655 /* <Item>
656 <rasd:Caption>ideController0</rasd:Caption>
657 <rasd:Description>IDE Controller</rasd:Description>
658 <rasd:InstanceId>5</rasd:InstanceId>
659 <rasd:ResourceType>5</rasd:ResourceType>
660 <rasd:Address>0</rasd:Address>
661 <rasd:BusNumber>0</rasd:BusNumber>
662 </Item> */
663 HardDiskController hdc;
664 hdc.system = HardDiskController::IDE;
665 hdc.idController = i.ulInstanceID;
666 hdc.strAddress = i.strAddress;
667 hdc.ulBusNumber = i.ulBusNumber;
668
669 vsys.mapControllers[i.ulInstanceID] = hdc;
670 }
671 break;
672
673 case OVFResourceType_ParallelScsiHba: // 6 SCSI controller
674 {
675 /* <Item>
676 <rasd:Caption>SCSI Controller 0 - LSI Logic</rasd:Caption>
677 <rasd:Description>SCI Controller</rasd:Description>
678 <rasd:ElementName>SCSI controller</rasd:ElementName>
679 <rasd:InstanceID>4</rasd:InstanceID>
680 <rasd:ResourceSubType>LsiLogic</rasd:ResourceSubType>
681 <rasd:ResourceType>6</rasd:ResourceType>
682 </Item> */
683 HardDiskController hdc;
684 hdc.system = HardDiskController::SCSI;
685 hdc.idController = i.ulInstanceID;
686 hdc.strControllerType = i.strResourceSubType;
687
688 vsys.mapControllers[i.ulInstanceID] = hdc;
689 }
690 break;
691
692 case OVFResourceType_EthernetAdapter: // 10
693 {
694 /* <Item>
695 <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
696 <rasd:Caption>Ethernet adapter on 'VM Network'</rasd:Caption>
697 <rasd:Connection>VM Network</rasd:Connection>
698 <rasd:Description>VM Network?</rasd:Description>
699 <rasd:ElementName>Ethernet adapter</rasd:ElementName>
700 <rasd:InstanceID>3</rasd:InstanceID>
701 <rasd:ResourceType>10</rasd:ResourceType>
702 </Item>
703
704 OVF spec DSP 0243 page 21:
705 "For an Ethernet adapter, this specifies the abstract network connection name
706 for the virtual machine. All Ethernet adapters that specify the same abstract
707 network connection name within an OVF package shall be deployed on the same
708 network. The abstract network connection name shall be listed in the NetworkSection
709 at the outermost envelope level." */
710
711 // make sure we have a matching NetworkSection/Network
712 NetworksMap::iterator it = m->mapNetworks.find(i.strConnection);
713 if (it == m->mapNetworks.end())
714 return setError(VBOX_E_FILE_ERROR,
715 tr("Error reading \"%s\": Invalid connection \"%s\"; cannot find matching NetworkSection/Network element, line %d"),
716 pcszPath,
717 i.strConnection.c_str(),
718 i.ulLineNumber);
719
720 vsys.llNetworkNames.push_back(i.strConnection);
721 }
722 break;
723
724 case OVFResourceType_FloppyDrive: // 14
725 vsys.fHasFloppyDrive = true; // we have no additional information
726 break;
727
728 case OVFResourceType_CdDrive: // 15
729 /* <Item ovf:required="false">
730 <rasd:Caption>cdrom1</rasd:Caption>
731 <rasd:InstanceId>7</rasd:InstanceId>
732 <rasd:ResourceType>15</rasd:ResourceType>
733 <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
734 <rasd:Parent>5</rasd:Parent>
735 <rasd:AddressOnParent>0</rasd:AddressOnParent>
736 </Item> */
737 // I tried to see what happens if I set an ISO for the CD-ROM in VMware Workstation,
738 // but then the ovftool dies with "Device backing not supported". So I guess if
739 // VMware can't export ISOs, then we don't need to be able to import them right now.
740 vsys.fHasCdromDrive = true; // we have no additional information
741 break;
742
743 case OVFResourceType_HardDisk: // 17
744 {
745 /* <Item>
746 <rasd:Caption>Harddisk 1</rasd:Caption>
747 <rasd:Description>HD</rasd:Description>
748 <rasd:ElementName>Hard Disk</rasd:ElementName>
749 <rasd:HostResource>ovf://disk/lamp</rasd:HostResource>
750 <rasd:InstanceID>5</rasd:InstanceID>
751 <rasd:Parent>4</rasd:Parent>
752 <rasd:ResourceType>17</rasd:ResourceType>
753 </Item> */
754
755 // look up the hard disk controller element whose InstanceID equals our Parent;
756 // this is how the connection is specified in OVF
757 ControllersMap::const_iterator it = vsys.mapControllers.find(i.ulParent);
758 if (it == vsys.mapControllers.end())
759 return setError(VBOX_E_FILE_ERROR,
760 tr("Error reading \"%s\": Hard disk item with instance ID %d specifies invalid parent %d, line %d"),
761 pcszPath,
762 i.ulInstanceID,
763 i.ulParent,
764 i.ulLineNumber);
765 const HardDiskController &hdc = it->second;
766
767 VirtualDisk vd;
768 vd.idController = i.ulParent;
769 bool fFound = false;
770 // ovf://disk/lamp
771 // 12345678901234
772 if (i.strHostResource.substr(0, 11) == "ovf://disk/")
773 vd.strDiskId = i.strHostResource.substr(11);
774 else if (i.strHostResource.substr(0, 6) == "/disk/")
775 vd.strDiskId = i.strHostResource.substr(6);
776
777 if ( !(vd.strDiskId.length())
778 || (m->mapDisks.find(vd.strDiskId) == m->mapDisks.end())
779 )
780 return setError(VBOX_E_FILE_ERROR,
781 tr("Error reading \"%s\": Hard disk item with instance ID %d specifies invalid host resource \"%s\", line %d"),
782 pcszPath,
783 i.ulInstanceID,
784 i.strHostResource.c_str(),
785 i.ulLineNumber);
786
787 vsys.mapVirtualDisks[vd.strDiskId] = vd;
788 }
789 break;
790
791 case OVFResourceType_UsbController: // 23
792 /* <Item ovf:required="false">
793 <rasd:Caption>usb</rasd:Caption>
794 <rasd:Description>USB Controller</rasd:Description>
795 <rasd:InstanceId>3</rasd:InstanceId>
796 <rasd:ResourceType>23</rasd:ResourceType>
797 <rasd:Address>0</rasd:Address>
798 <rasd:BusNumber>0</rasd:BusNumber>
799 </Item> */
800 vsys.fHasUsbController = true; // we have no additional information
801 break;
802
803 case OVFResourceType_SoundCard: // 35
804 /* <Item ovf:required="false">
805 <rasd:Caption>sound</rasd:Caption>
806 <rasd:Description>Sound Card</rasd:Description>
807 <rasd:InstanceId>10</rasd:InstanceId>
808 <rasd:ResourceType>35</rasd:ResourceType>
809 <rasd:ResourceSubType>ensoniq1371</rasd:ResourceSubType>
810 <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
811 <rasd:AddressOnParent>3</rasd:AddressOnParent>
812 </Item> */
813 vsys.strSoundCardType = i.strResourceSubType;
814 break;
815
816 default:
817 return setError(VBOX_E_FILE_ERROR,
818 tr("Error reading \"%s\": Unknown resource type %d in hardware item, line %d"),
819 pcszPath,
820 i.resourceType,
821 i.ulLineNumber);
822 }
823 }
824 }
825 else if ( (!strcmp(pcszElemName, "OperatingSystemSection"))
826 || (!strcmp(pcszTypeAttr, "ovf:OperatingSystemSection_Type"))
827 )
828 {
829 uint64_t cimos64;
830 if (!(pelmThis->getAttributeValue("id", cimos64)))
831 return setError(VBOX_E_FILE_ERROR,
832 tr("Error reading \"%s\": missing or invalid 'ovf:id' attribute in operating system section element, line %d"),
833 pcszPath,
834 pelmThis->getLineNumber());
835
836 vsys.cimos = (CIMOSType_T)cimos64;
837 }
838 }
839
840 // now create the virtual system
841 m->llVirtualSystems.push_back(vsys);
842
843 return S_OK;
844}
845
846// IAppliance public methods
847////////////////////////////////////////////////////////////////////////////////
848
849/**
850 * Appliance initializer.
851 *
852 * This loads the given appliance.
853 * @param
854 * @return
855 */
856
857HRESULT Appliance::init(VirtualBox *aVirtualBox, IN_BSTR &path)
858{
859 HRESULT rc;
860
861 /* Enclose the state transition NotReady->InInit->Ready */
862 AutoInitSpan autoInitSpan(this);
863 AssertReturn(autoInitSpan.isOk(), E_FAIL);
864
865 /* Weakly reference to a VirtualBox object */
866 unconst(mVirtualBox) = aVirtualBox;
867
868 // initialize data
869 m = new Data;
870 m->bstrPath = path;
871
872 // see if we can handle this file; for now we insist it has an ".ovf" extension
873 Utf8Str utf8Path(path);
874 const char *pcszLastDot = strrchr(utf8Path, '.');
875 if ( (!pcszLastDot)
876 || ( strcmp(pcszLastDot, ".ovf")
877 && strcmp(pcszLastDot, ".OVF")
878 )
879 )
880 return setError(VBOX_E_FILE_ERROR,
881 tr("Appliance file must have .ovf extension"));
882
883 try
884 {
885 xml::XmlFileParser parser;
886 xml::Document doc;
887 parser.read(utf8Path.raw(),
888 doc);
889
890 const xml::Node *pRootElem = doc.getRootElement();
891 if (strcmp(pRootElem->getName(), "Envelope"))
892 return setError(VBOX_E_FILE_ERROR,
893 tr("Root element in OVF file must be \"Envelope\"."));
894
895 // OVF has the following rough layout:
896 /*
897 -- <References> .... files referenced from other parts of the file, such as VMDK images
898 -- Metadata, comprised of several section commands
899 -- virtual machines, either a single <VirtualSystem>, or a <VirtualSystemCollection>
900 -- optionally <Strings> for localization
901 */
902
903 // get all "File" child elements of "References" section so we can look up files easily;
904 // first find the "References" sections so we can look up files
905 xml::NodesList listFileElements; // receives all /Envelope/References/File nodes
906 const xml::Node *pReferencesElem;
907 if ((pReferencesElem = pRootElem->findChildElement("References")))
908 pReferencesElem->getChildElements(listFileElements, "File");
909
910 // now go though the sections
911 if (!(SUCCEEDED(rc = LoopThruSections(utf8Path.raw(), pReferencesElem, pRootElem))))
912 return rc;
913 }
914 catch(xml::Error &x)
915 {
916 return setError(VBOX_E_FILE_ERROR,
917 x.what());
918 }
919
920 /* Confirm a successful initialization */
921 autoInitSpan.setSucceeded();
922
923 return S_OK;
924}
925
926void Appliance::uninit()
927{
928 delete m;
929 m = NULL;
930}
931
932STDMETHODIMP Appliance::COMGETTER(Path)(BSTR *aPath)
933{
934 if (!aPath)
935 return E_POINTER;
936
937 AutoCaller autoCaller(this);
938 CheckComRCReturnRC(autoCaller.rc());
939
940 AutoReadLock alock(this);
941
942 m->bstrPath.cloneTo(aPath);
943
944 return S_OK;
945}
946
947STDMETHODIMP Appliance::COMGETTER(Disks)(ComSafeArrayOut(BSTR, aDisks))
948{
949 CheckComArgOutSafeArrayPointerValid(aDisks);
950
951 AutoCaller autoCaller(this);
952 CheckComRCReturnRC(autoCaller.rc());
953
954 AutoReadLock alock(this);
955
956 size_t c = m->mapDisks.size();
957 com::SafeArray<BSTR> sfaDisks(c);
958
959 DiskImagesMap::const_iterator it;
960 size_t i = 0;
961 for (it = m->mapDisks.begin();
962 it != m->mapDisks.end();
963 ++it, ++i)
964 {
965 // create a string representing this disk
966 const DiskImage &d = it->second;
967 char *psz = NULL;
968 RTStrAPrintf(&psz,
969 "%s\t"
970 "%RI64\t"
971 "%RI64\t"
972 "%s\t"
973 "%s\t"
974 "%RI64\t"
975 "%RI64\t"
976 "%s",
977 d.strDiskId.c_str(),
978 d.iCapacity,
979 d.iPopulatedSize,
980 d.strFormat.c_str(),
981 d.strHref.c_str(),
982 d.iSize,
983 d.iChunkSize,
984 d.strCompression.c_str());
985 Utf8Str utf(psz);
986 Bstr bstr(utf);
987 // push to safearray
988 bstr.cloneTo(&sfaDisks[i]);
989 RTStrFree(psz);
990 }
991
992 sfaDisks.detachTo(ComSafeArrayOutArg(aDisks));
993
994 return S_OK;
995}
996
997STDMETHODIMP Appliance::COMGETTER(VirtualSystemDescriptions)(ComSafeArrayOut(IVirtualSystemDescription*, aVirtualSystemDescriptions))
998{
999 CheckComArgOutSafeArrayPointerValid(aVirtualSystemDescriptions);
1000
1001 AutoCaller autoCaller(this);
1002 CheckComRCReturnRC(autoCaller.rc());
1003
1004 AutoReadLock alock(this);
1005
1006 SafeIfaceArray<IVirtualSystemDescription> sfaVSD(m->virtualSystemDescriptions);
1007 sfaVSD.detachTo(ComSafeArrayOutArg(aVirtualSystemDescriptions));
1008
1009 return S_OK;
1010}
1011
1012void convertCIMOSType2VBoxOSType(Utf8Str &osTypeVBox, CIMOSType_T c)
1013{
1014 switch (c)
1015 {
1016 case CIMOSType_CIMOS_Unknown: // 0 - Unknown
1017 osTypeVBox = SchemaDefs_OSTypeId_Other;
1018 break;
1019
1020 case CIMOSType_CIMOS_OS2: // 12 - OS/2
1021 osTypeVBox = SchemaDefs_OSTypeId_OS2;
1022 break;
1023
1024 case CIMOSType_CIMOS_MSDOS: // 14 - MSDOS
1025 osTypeVBox = SchemaDefs_OSTypeId_DOS;
1026 break;
1027
1028 case CIMOSType_CIMOS_WIN3x: // 15 - WIN3x
1029 osTypeVBox = SchemaDefs_OSTypeId_Windows31;
1030 break;
1031
1032 case CIMOSType_CIMOS_WIN95: // 16 - WIN95
1033 osTypeVBox = SchemaDefs_OSTypeId_Windows95;
1034 break;
1035
1036 case CIMOSType_CIMOS_WIN98: // 17 - WIN98
1037 osTypeVBox = SchemaDefs_OSTypeId_Windows98;
1038 break;
1039
1040 case CIMOSType_CIMOS_WINNT: // 18 - WINNT
1041 osTypeVBox = SchemaDefs_OSTypeId_WindowsNT4;
1042 break;
1043
1044 case CIMOSType_CIMOS_NetWare: // 21 - NetWare
1045 case CIMOSType_CIMOS_NovellOES: // 86 - Novell OES
1046 osTypeVBox = SchemaDefs_OSTypeId_Netware;
1047 break;
1048
1049 case CIMOSType_CIMOS_Solaris: // 29 - Solaris
1050 case CIMOSType_CIMOS_SunOS: // 30 - SunOS
1051 osTypeVBox = SchemaDefs_OSTypeId_Solaris;
1052 break;
1053
1054 case CIMOSType_CIMOS_FreeBSD: // 42 - FreeBSD
1055 osTypeVBox = SchemaDefs_OSTypeId_FreeBSD;
1056 break;
1057
1058 case CIMOSType_CIMOS_NetBSD: // 43 - NetBSD
1059 osTypeVBox = SchemaDefs_OSTypeId_NetBSD;
1060 break;
1061
1062 case CIMOSType_CIMOS_QNX: // 48 - QNX
1063 osTypeVBox = SchemaDefs_OSTypeId_QNX;
1064 break;
1065
1066 case CIMOSType_CIMOS_Windows2000: // 58 - Windows 2000
1067 osTypeVBox = SchemaDefs_OSTypeId_Windows2000;
1068 break;
1069
1070 case CIMOSType_CIMOS_WindowsMe: // 63 - Windows (R) Me
1071 osTypeVBox = SchemaDefs_OSTypeId_WindowsMe;
1072 break;
1073
1074 case CIMOSType_CIMOS_OpenBSD: // 65 - OpenBSD
1075 osTypeVBox = SchemaDefs_OSTypeId_OpenBSD;
1076 break;
1077
1078 case CIMOSType_CIMOS_WindowsXP: // 67 - Windows XP
1079 case CIMOSType_CIMOS_WindowsXPEmbedded: // 72 - Windows XP Embedded
1080 case CIMOSType_CIMOS_WindowsEmbeddedforPointofService: // 75 - Windows Embedded for Point of Service
1081 osTypeVBox = SchemaDefs_OSTypeId_WindowsXP;
1082 break;
1083
1084 case CIMOSType_CIMOS_MicrosoftWindowsServer2003: // 69 - Microsoft Windows Server 2003
1085 osTypeVBox = SchemaDefs_OSTypeId_Windows2003;
1086 break;
1087
1088 case CIMOSType_CIMOS_MicrosoftWindowsServer2003_64: // 70 - Microsoft Windows Server 2003 64-Bit
1089 osTypeVBox = SchemaDefs_OSTypeId_Windows2003_64;
1090 break;
1091
1092 case CIMOSType_CIMOS_WindowsXP_64: // 71 - Windows XP 64-Bit
1093 osTypeVBox = SchemaDefs_OSTypeId_WindowsXP_64;
1094 break;
1095
1096 case CIMOSType_CIMOS_WindowsVista: // 73 - Windows Vista
1097 osTypeVBox = SchemaDefs_OSTypeId_WindowsVista;
1098 break;
1099
1100 case CIMOSType_CIMOS_WindowsVista_64: // 74 - Windows Vista 64-Bit
1101 osTypeVBox = SchemaDefs_OSTypeId_WindowsVista_64;
1102 break;
1103
1104 case CIMOSType_CIMOS_MicrosoftWindowsServer2008: // 76 - Microsoft Windows Server 2008
1105 osTypeVBox = SchemaDefs_OSTypeId_Windows2008;
1106 break;
1107
1108 case CIMOSType_CIMOS_MicrosoftWindowsServer2008_64: // 77 - Microsoft Windows Server 2008 64-Bit
1109 osTypeVBox = SchemaDefs_OSTypeId_Windows2008_64;
1110 break;
1111
1112 case CIMOSType_CIMOS_FreeBSD_64: // 78 - FreeBSD 64-Bit
1113 osTypeVBox = SchemaDefs_OSTypeId_FreeBSD_64;
1114 break;
1115
1116 case CIMOSType_CIMOS_RedHatEnterpriseLinux: // 79 - RedHat Enterprise Linux
1117 osTypeVBox = SchemaDefs_OSTypeId_RedHat;
1118 break;
1119
1120 case CIMOSType_CIMOS_RedHatEnterpriseLinux_64: // 80 - RedHat Enterprise Linux 64-Bit
1121 osTypeVBox = SchemaDefs_OSTypeId_RedHat_64;
1122 break;
1123
1124 case CIMOSType_CIMOS_Solaris_64: // 81 - Solaris 64-Bit
1125 osTypeVBox = SchemaDefs_OSTypeId_Solaris_64;
1126 break;
1127
1128 case CIMOSType_CIMOS_SUSE: // 82 - SUSE
1129 case CIMOSType_CIMOS_SLES: // 84 - SLES
1130 case CIMOSType_CIMOS_NovellLinuxDesktop: // 87 - Novell Linux Desktop
1131 osTypeVBox = SchemaDefs_OSTypeId_OpenSUSE;
1132 break;
1133
1134 case CIMOSType_CIMOS_SUSE_64: // 83 - SUSE 64-Bit
1135 case CIMOSType_CIMOS_SLES_64: // 85 - SLES 64-Bit
1136 osTypeVBox = SchemaDefs_OSTypeId_OpenSUSE_64;
1137 break;
1138
1139 case CIMOSType_CIMOS_LINUX: // 36 - LINUX
1140 case CIMOSType_CIMOS_SunJavaDesktopSystem: // 88 - Sun Java Desktop System
1141 case CIMOSType_CIMOS_TurboLinux: // 91 - TurboLinux
1142 osTypeVBox = SchemaDefs_OSTypeId_Linux;
1143 break;
1144
1145 // case CIMOSType_CIMOS_TurboLinux_64: // 92 - TurboLinux 64-Bit
1146 // case CIMOSType_CIMOS_Linux_64: // 101 - Linux 64-Bit
1147 // osTypeVBox = VBOXOSTYPE_Linux_x64;
1148 // break;
1149
1150 case CIMOSType_CIMOS_Mandriva: // 89 - Mandriva
1151 osTypeVBox = SchemaDefs_OSTypeId_Mandriva;
1152 break;
1153
1154 case CIMOSType_CIMOS_Mandriva_64: // 90 - Mandriva 64-Bit
1155 osTypeVBox = SchemaDefs_OSTypeId_Mandriva_64;
1156 break;
1157
1158 case CIMOSType_CIMOS_Ubuntu: // 93 - Ubuntu
1159 osTypeVBox = SchemaDefs_OSTypeId_Ubuntu;
1160 break;
1161
1162 case CIMOSType_CIMOS_Ubuntu_64: // 94 - Ubuntu 64-Bit
1163 osTypeVBox = SchemaDefs_OSTypeId_Ubuntu_64;
1164 break;
1165
1166 case CIMOSType_CIMOS_Debian: // 95 - Debian
1167 osTypeVBox = SchemaDefs_OSTypeId_Debian;
1168 break;
1169
1170 case CIMOSType_CIMOS_Debian_64: // 96 - Debian 64-Bit
1171 osTypeVBox = SchemaDefs_OSTypeId_Debian_64;
1172 break;
1173
1174 case CIMOSType_CIMOS_Linux_2_4_x: // 97 - Linux 2.4.x
1175 osTypeVBox = SchemaDefs_OSTypeId_Linux24;
1176 break;
1177
1178 case CIMOSType_CIMOS_Linux_2_4_x_64: // 98 - Linux 2.4.x 64-Bit
1179 osTypeVBox = SchemaDefs_OSTypeId_Linux24_64;
1180 break;
1181
1182 case CIMOSType_CIMOS_Linux_2_6_x: // 99 - Linux 2.6.x
1183 osTypeVBox = SchemaDefs_OSTypeId_Linux26;
1184 break;
1185
1186 case CIMOSType_CIMOS_Linux_2_6_x_64: // 100 - Linux 2.6.x 64-Bit
1187 osTypeVBox = SchemaDefs_OSTypeId_Linux26_64;
1188 break;
1189 default:
1190 {
1191 /* If we are here we have no clue what OS this should be. Set
1192 to other type as default. */
1193 osTypeVBox = SchemaDefs_OSTypeId_Other;
1194 }
1195 }
1196}
1197
1198STDMETHODIMP Appliance::Interpret()
1199{
1200 // @todo:
1201 // - Locking
1202 // - COM error handling
1203 // - don't use COM methods but the methods directly (faster, but needs appropriate locking of that objects itself (s. HardDisk2))
1204 // - Appropriate handle errors like not supported file formats
1205 AutoCaller autoCaller(this);
1206 CheckComRCReturnRC(autoCaller.rc());
1207
1208 AutoWriteLock(this);
1209
1210 HRESULT rc = S_OK;
1211
1212 /* Clear any previous virtual system descriptions */
1213 // @todo: have the entries deleted also?
1214 m->virtualSystemDescriptions.clear();
1215
1216 /* We need the default path for storing disk images */
1217 ComPtr<ISystemProperties> systemProps;
1218 rc = mVirtualBox->COMGETTER(SystemProperties)(systemProps.asOutParam());
1219 CheckComRCReturnRC(rc);
1220 Bstr bstrDefaultHardDiskLocation;
1221 rc = systemProps->COMGETTER(DefaultHardDiskFolder)(bstrDefaultHardDiskLocation.asOutParam());
1222 CheckComRCReturnRC(rc);
1223
1224 try
1225 {
1226 list<VirtualSystem>::const_iterator it;
1227 /* Iterate through all appliances */
1228 for (it = m->llVirtualSystems.begin();
1229 it != m->llVirtualSystems.end();
1230 ++it)
1231 {
1232 const VirtualSystem &vsysThis = *it;
1233
1234 ComObjPtr<VirtualSystemDescription> pNewDesc;
1235 rc = pNewDesc.createObject();
1236 CheckComRCThrowRC(rc);
1237 rc = pNewDesc->init();
1238 CheckComRCThrowRC(rc);
1239
1240 /* Guest OS type */
1241 Utf8Str strOsTypeVBox,
1242 strCIMOSType = toString<ULONG>(vsysThis.cimos);
1243 convertCIMOSType2VBoxOSType(strOsTypeVBox, vsysThis.cimos);
1244 pNewDesc->addEntry(VirtualSystemDescriptionType_OS,
1245 "",
1246 strCIMOSType,
1247 strOsTypeVBox);
1248
1249 /* VM name */
1250 /* If the there isn't any name specified create a default one out of
1251 * the OS type */
1252 Utf8Str nameVBox = vsysThis.strName;
1253 if (nameVBox == "")
1254 nameVBox = strOsTypeVBox;
1255 searchUniqueVMName(nameVBox);
1256 pNewDesc->addEntry(VirtualSystemDescriptionType_Name,
1257 "",
1258 vsysThis.strName,
1259 nameVBox);
1260
1261 /* Now that we know the OS type, get our internal defaults based on that. */
1262 ComPtr<IGuestOSType> pGuestOSType;
1263 rc = mVirtualBox->GetGuestOSType(Bstr(strOsTypeVBox), pGuestOSType.asOutParam());
1264 ComAssertComRCThrowRC(rc);
1265
1266 /* CPU count */
1267 /* @todo: check min/max requirements of VBox (SchemaDefs::Min/MaxCPUCount) */
1268 ULONG cpuCountVBox = vsysThis.cCPUs;
1269 if (vsysThis.cCPUs == 0)
1270 cpuCountVBox = 1;
1271 pNewDesc->addEntry(VirtualSystemDescriptionType_CPU,
1272 "",
1273 toString<ULONG>(vsysThis.cCPUs),
1274 toString<ULONG>(cpuCountVBox));
1275
1276 /* RAM */
1277 /* @todo: check min/max requirements of VBox (SchemaDefs::Min/MaxGuestRAM) */
1278 uint64_t ullMemSizeVBox = vsysThis.ullMemorySize;
1279 if (vsysThis.ullMemorySize == 0)
1280 {
1281 /* If the RAM of the OVF is zero, use our predefined values */
1282 ULONG memSizeVBox2;
1283 rc = pGuestOSType->COMGETTER(RecommendedRAM)(&memSizeVBox2);
1284 ComAssertComRCThrowRC(rc);
1285 /* VBox stores that in MByte */
1286 ullMemSizeVBox = (uint64_t)memSizeVBox2 * _1M;
1287 }
1288 pNewDesc->addEntry(VirtualSystemDescriptionType_Memory,
1289 "",
1290 toString<uint64_t>(vsysThis.ullMemorySize),
1291 toString<uint64_t>(ullMemSizeVBox));
1292
1293 /* Audio */
1294 if (!vsysThis.strSoundCardType.isNull())
1295 /* Currently we set the AC97 always.
1296 @todo: figure out the hardware which could be possible */
1297 pNewDesc->addEntry(VirtualSystemDescriptionType_SoundCard,
1298 "",
1299 vsysThis.strSoundCardType,
1300 "");
1301
1302 /* USB Controller */
1303 if (vsysThis.fHasUsbController)
1304 pNewDesc->addEntry(VirtualSystemDescriptionType_USBController, "", "", "");
1305
1306 /* Network Controller */
1307 // @todo: there is no hardware specification in the OVF file; supposedly the
1308 // hardware will then be determined by the VirtualSystemType element (e.g. "vmx-07")
1309 if (vsysThis.llNetworkNames.size() > 0)
1310 {
1311 /* Get the default network adapter type for the selected guest OS */
1312 NetworkAdapterType_T nwAdapterVBox = NetworkAdapterType_Am79C970A;
1313 rc = pGuestOSType->COMGETTER(AdapterType)(&nwAdapterVBox);
1314 ComAssertComRCThrowRC(rc);
1315 list<Utf8Str>::const_iterator nwIt;
1316 /* Iterate through all abstract networks. We support 8 network
1317 * adapters at the maximum. (@todo: warn if it are more!) */
1318 size_t a = 0;
1319 for (nwIt = vsysThis.llNetworkNames.begin();
1320 nwIt != vsysThis.llNetworkNames.end() && a < SchemaDefs::NetworkAdapterCount;
1321 ++nwIt, ++a)
1322 {
1323 Utf8Str nwController = *nwIt; // @todo: not used yet
1324 pNewDesc->addEntry(VirtualSystemDescriptionType_NetworkAdapter, "", "", toString<ULONG>(nwAdapterVBox));
1325 }
1326 }
1327
1328 /* Floppy Drive */
1329 if (vsysThis.fHasFloppyDrive)
1330 pNewDesc->addEntry(VirtualSystemDescriptionType_Floppy, "", "", "");
1331
1332 /* CD Drive */
1333 if (vsysThis.fHasCdromDrive)
1334 pNewDesc->addEntry(VirtualSystemDescriptionType_CDROM, "", "", "");
1335
1336 /* Hard disk Controller */
1337 ControllersMap::const_iterator hdcIt;
1338 /* Iterate through all hard disk controllers */
1339 for (hdcIt = vsysThis.mapControllers.begin();
1340 hdcIt != vsysThis.mapControllers.end();
1341 ++hdcIt)
1342 {
1343 const HardDiskController &hdc = hdcIt->second;
1344 Utf8Str strControllerID = toString<uint32_t>(hdc.idController);
1345
1346 switch (hdc.system)
1347 {
1348 case HardDiskController::IDE:
1349 {
1350 // @todo: figure out the IDE types
1351 /* Use PIIX4 as default */
1352// IDEControllerType_T hdcController = IDEControllerType_PIIX4;
1353 Utf8Str strType = "PIIX4";
1354 if (!RTStrICmp(hdc.strControllerType.c_str(), "PIIX3"))
1355 strType = "PIIX3";
1356// else // if (!RTStrICmp(hdc.strControllerType.c_str(), "PIIX4"))
1357// hdcController = IDEControllerType_PIIX4;
1358 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskControllerIDE,
1359 strControllerID,
1360 hdc.strControllerType,
1361 strType);
1362 break;
1363 }
1364
1365 case HardDiskController::SATA:
1366 {
1367 // @todo: figure out the SATA types
1368 /* We only support a plain AHCI controller, so use them always */
1369 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskControllerSATA,
1370 strControllerID,
1371 hdc.strControllerType,
1372 "AHCI");
1373 break;
1374 }
1375
1376 case HardDiskController::SCSI:
1377 {
1378 // @todo: figure out the SCSI types
1379 Utf8Str hdcController = "LsiLogic";
1380 /* if (!RTStrICmp(hdc.strControllerType.c_str(), "LsiLogic"))
1381 hdcController = "LsiLogic";
1382 else*/
1383 if (!RTStrICmp(hdc.strControllerType.c_str(), "BusLogic"))
1384 hdcController = "BusLogic";
1385 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskControllerSCSI,
1386 strControllerID,
1387 hdc.strControllerType,
1388 hdcController);
1389 break;
1390 }
1391 }
1392 }
1393
1394 /* Hard disks */
1395 if (vsysThis.mapVirtualDisks.size() > 0)
1396 {
1397 // @todo:
1398 // - strHref could be empty (construct a new default file name)
1399 // - check that the filename is unique to vbox in any case
1400 VirtualDisksMap::const_iterator itVD;
1401 /* Iterate through all hard disks ()*/
1402 for (itVD = vsysThis.mapVirtualDisks.begin();
1403 itVD != vsysThis.mapVirtualDisks.end();
1404 ++itVD)
1405 {
1406 const VirtualDisk &hd = itVD->second;
1407 /* Get the associated disk image */
1408 const DiskImage &di = m->mapDisks[hd.strDiskId];
1409
1410 // @todo:
1411 // - figure out all possible vmdk formats we also support
1412 // - figure out if there is a url specifier for vhd already
1413 // - we need a url specifier for the vdi format
1414 if ( (!RTStrICmp(di.strFormat.c_str(), "http://www.vmware.com/specifications/vmdk.html#sparse"))
1415 || (!RTStrICmp(di.strFormat.c_str(), "http://www.vmware.com/specifications/vmdk.html#compressed"))
1416 )
1417 {
1418 // construct a unique target path
1419 Utf8StrFmt strPath("%ls%c%s",
1420 bstrDefaultHardDiskLocation.raw(),
1421 RTPATH_DELIMITER,
1422 di.strHref.c_str());
1423 searchUniqueDiskImageFilePath(strPath);
1424
1425 // find the description for the hard disk controller that has the
1426 // same ID as hd.idController
1427 const VirtualSystemDescriptionEntry *pController;
1428 if (!(pController = pNewDesc->findControllerFromID(hd.idController)))
1429 throw setError(E_FAIL,
1430 tr("Internal inconsistency looking up hard disk controller."));
1431
1432 // controller to attach to @todo bus?
1433 Utf8StrFmt strExtraConfig("controller=%RI16",
1434 pController->ulIndex);
1435 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskImage,
1436 hd.strDiskId,
1437 di.strHref,
1438 strPath,
1439 strExtraConfig);
1440 }
1441 }
1442 }
1443
1444 m->virtualSystemDescriptions.push_back(pNewDesc);
1445 }
1446 }
1447 catch (HRESULT aRC)
1448 {
1449 /* On error we clear the list & return */
1450 //m->virtualSystemDescriptions.clear();
1451 rc = aRC;
1452 }
1453
1454 return rc;
1455}
1456
1457STDMETHODIMP Appliance::ImportAppliance(IProgress **aProgress)
1458{
1459 CheckComArgOutPointerValid(aProgress);
1460
1461 AutoCaller autoCaller(this);
1462 CheckComRCReturnRC(autoCaller.rc());
1463
1464 AutoReadLock(this);
1465
1466 HRESULT rc = S_OK;
1467
1468 ComObjPtr<Progress> progress;
1469
1470 try
1471 {
1472 /* Create the progress object */
1473 progress.createObject();
1474 rc = progress->init(mVirtualBox, static_cast<IAppliance *>(this),
1475 BstrFmt(tr("Import appliance '%ls'"),
1476 m->bstrPath.raw()),
1477 FALSE /* aCancelable */);
1478 CheckComRCThrowRC(rc);
1479
1480 /* Initialize our worker task */
1481 std::auto_ptr<Task> task(new Task(this, progress));
1482 //AssertComRCThrowRC (task->autoCaller.rc());
1483
1484 rc = task->startThread();
1485 CheckComRCThrowRC(rc);
1486
1487 task.release();
1488 }
1489 catch (HRESULT aRC)
1490 {
1491 rc = aRC;
1492 }
1493
1494 if (SUCCEEDED(rc))
1495 /* Return progress to the caller */
1496 progress.queryInterfaceTo(aProgress);
1497
1498 return rc;
1499}
1500
1501HRESULT Appliance::searchUniqueVMName(Utf8Str& aName) const
1502{
1503 IMachine *machine = NULL;
1504 char *tmpName = RTStrDup(aName.c_str());
1505 int i = 1;
1506 /* @todo: Maybe too cost-intensive; try to find a lighter way */
1507 while (mVirtualBox->FindMachine(Bstr(tmpName), &machine) != VBOX_E_OBJECT_NOT_FOUND)
1508 {
1509 RTStrFree(tmpName);
1510 RTStrAPrintf(&tmpName, "%s_%d", aName.c_str(), i);
1511 ++i;
1512 }
1513 aName = tmpName;
1514 RTStrFree(tmpName);
1515
1516 return S_OK;
1517}
1518
1519HRESULT Appliance::searchUniqueDiskImageFilePath(Utf8Str& aName) const
1520{
1521 IHardDisk2 *harddisk = NULL;
1522 char *tmpName = RTStrDup(aName.c_str());
1523 int i = 1;
1524 /* Check if the file exists or if a file with this path is registered
1525 * already */
1526 /* @todo: Maybe too cost-intensive; try to find a lighter way */
1527 while (RTPathExists(tmpName) ||
1528 mVirtualBox->FindHardDisk2(Bstr(tmpName), &harddisk) != VBOX_E_OBJECT_NOT_FOUND)
1529 {
1530 RTStrFree(tmpName);
1531 char *tmpDir = RTStrDup(aName.c_str());
1532 RTPathStripFilename(tmpDir);;
1533 char *tmpFile = RTStrDup(RTPathFilename(aName.c_str()));
1534 RTPathStripExt(tmpFile);
1535 const char *tmpExt = RTPathExt(aName.c_str());
1536 RTStrAPrintf(&tmpName, "%s%c%s_%d%s", tmpDir, RTPATH_DELIMITER, tmpFile, i, tmpExt);
1537 RTStrFree(tmpFile);
1538 RTStrFree(tmpDir);
1539 ++i;
1540 }
1541 aName = tmpName;
1542 RTStrFree(tmpName);
1543
1544 return S_OK;
1545}
1546
1547/* static */
1548DECLCALLBACK(int) Appliance::taskThread(RTTHREAD aThread, void *pvUser)
1549{
1550 std::auto_ptr <Task> task(static_cast<Task *>(pvUser));
1551 AssertReturn(task.get(), VERR_GENERAL_FAILURE);
1552
1553 Appliance *app = task->that;
1554
1555 /// @todo ugly hack, fix ComAssert... (same as in HardDisk2::taskThread)
1556 #define setError app->setError
1557
1558 AutoCaller autoCaller(app);
1559 CheckComRCReturnRC(autoCaller.rc());
1560
1561 AutoWriteLock appLock(app);
1562
1563 HRESULT rc = S_OK;
1564
1565 /* For now we report 2 steps for every virtual system. Later we may add the
1566 progress of the image cloning. */
1567 float opCountMax = (float)100.0 / (app->m->llVirtualSystems.size() * 2);
1568 uint32_t opCount = 0;
1569
1570 list<VirtualSystem>::const_iterator it;
1571 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it1;
1572 /* Iterate through all virtual systems of that appliance */
1573 size_t i = 0;
1574 for (it = app->m->llVirtualSystems.begin(),
1575 it1 = app->m->virtualSystemDescriptions.begin();
1576 it != app->m->llVirtualSystems.end();
1577 ++it, ++it1, ++i)
1578 {
1579 const VirtualSystem &vsysThis = *it;
1580 ComObjPtr<VirtualSystemDescription> vsdescThis = (*it1);
1581
1582 /* Catch possible errors */
1583 try
1584 {
1585 /* Guest OS type */
1586 std::list<VirtualSystemDescriptionEntry*> vsdeOS = vsdescThis->findByType(VirtualSystemDescriptionType_OS);
1587 Assert(vsdeOS.size() == 1);
1588 const Utf8Str &strOsTypeVBox = vsdeOS.front()->strConfig;
1589
1590 /* Now that we know the base system get our internal defaults based on that. */
1591 ComPtr<IGuestOSType> osType;
1592 rc = app->mVirtualBox->GetGuestOSType(Bstr(strOsTypeVBox), osType.asOutParam());
1593 CheckComRCThrowRC(rc);
1594
1595 /* Create the machine */
1596 /* First get the name */
1597 std::list<VirtualSystemDescriptionEntry*> vsdeName = vsdescThis->findByType(VirtualSystemDescriptionType_Name);
1598 Assert(vsdeName.size() == 1);
1599 const Utf8Str &strNameVBox = vsdeName.front()->strConfig;
1600 ComPtr<IMachine> pNewMachine;
1601 rc = app->mVirtualBox->CreateMachine(Bstr(strNameVBox), Bstr(strOsTypeVBox),
1602 Bstr(), Guid(),
1603 pNewMachine.asOutParam());
1604 CheckComRCThrowRC(rc);
1605
1606 /* CPU count (ignored for now) */
1607 /* @todo: check min/max requirements of VBox (SchemaDefs::Min/MaxCPUCount) */
1608 // EntriesList vsdeCPU = vsd->findByType (VirtualSystemDescriptionType_CPU);
1609
1610 /* RAM */
1611 /* @todo: check min/max requirements of VBox (SchemaDefs::Min/MaxGuestRAM) */
1612 std::list<VirtualSystemDescriptionEntry*> vsdeRAM = vsdescThis->findByType(VirtualSystemDescriptionType_Memory);
1613 Assert(vsdeRAM.size() == 1);
1614 const Utf8Str &memoryVBox = vsdeRAM.front()->strConfig;
1615 ULONG tt = (ULONG)RTStrToUInt64(memoryVBox.c_str()) / _1M;
1616
1617 rc = pNewMachine->COMSETTER(MemorySize)(tt);
1618 CheckComRCThrowRC(rc);
1619
1620 /* VRAM */
1621 /* Get the recommended VRAM for this guest OS type */
1622 /* @todo: check min/max requirements of VBox (SchemaDefs::Min/MaxGuestVRAM) */
1623 ULONG vramVBox;
1624 rc = osType->COMGETTER(RecommendedVRAM)(&vramVBox);
1625 CheckComRCThrowRC(rc);
1626 /* Set the VRAM */
1627 rc = pNewMachine->COMSETTER(VRAMSize)(vramVBox);
1628 CheckComRCThrowRC(rc);
1629
1630 /* Audio Adapter */
1631 std::list<VirtualSystemDescriptionEntry*> vsdeAudioAdapter = vsdescThis->findByType(VirtualSystemDescriptionType_SoundCard);
1632 /* @todo: we support one audio adapter only */
1633 if (vsdeAudioAdapter.size() > 0)
1634 {
1635 const Utf8Str& audioAdapterVBox = vsdeAudioAdapter.front()->strConfig;
1636 if (RTStrICmp(audioAdapterVBox, "null") != 0)
1637 {
1638 uint32_t audio = RTStrToUInt32(audioAdapterVBox.c_str());
1639 ComPtr<IAudioAdapter> audioAdapter;
1640 rc = pNewMachine->COMGETTER(AudioAdapter)(audioAdapter.asOutParam());
1641 CheckComRCThrowRC(rc);
1642 rc = audioAdapter->COMSETTER(Enabled)(true);
1643 CheckComRCThrowRC(rc);
1644 /* @todo: For now this is preselected, but on Linux for example
1645 more drivers are possible. The user should be able to change
1646 this also. */
1647 AudioDriverType_T adt = AudioDriverType_Null;
1648#if defined(RT_OS_WINDOWS)
1649# ifdef VBOX_WITH_WINMM
1650 adt = AudioDriverType_WinMM;
1651# else
1652 adt = AudioDriverType_DirectSound;
1653# endif
1654#elif defined(RT_OS_LINUX)
1655# ifdef VBOX_WITH_ALSA
1656 adt = AudioDriverType_ALSA;
1657# elif defined(VBOX_WITH_PULSE)
1658 adt = AudioDriverType_Pulse;
1659# else
1660 adt = AudioDriverType_OSS;
1661# endif
1662#elif defined(RT_OS_DARWIN)
1663 adt = AudioDriverType_CoreAudio;
1664#elif defined(RT_OS_SOLARIS)
1665 adt = AudioDriverType_SolAudio;
1666#elif defined(RT_OS_OS2)
1667 adt = AudioDriverType_MMPM;
1668#endif
1669 rc = audioAdapter->COMSETTER(AudioDriver)(adt);
1670 CheckComRCThrowRC(rc);
1671 rc = audioAdapter->COMSETTER(AudioController)(static_cast<AudioControllerType_T>(audio));
1672 CheckComRCThrowRC(rc);
1673 }
1674 }
1675
1676 /* USB Controller */
1677 std::list<VirtualSystemDescriptionEntry*> vsdeUSBController = vsdescThis->findByType(VirtualSystemDescriptionType_USBController);
1678 // USB support is enabled if there's at least one such entry; to disable USB support,
1679 // the type of the USB item would have been changed to "ignore"
1680 bool fUSBEnabled = vsdeUSBController.size() > 0;
1681
1682 ComPtr<IUSBController> usbController;
1683 rc = pNewMachine->COMGETTER(USBController)(usbController.asOutParam());
1684 CheckComRCThrowRC(rc);
1685 rc = usbController->COMSETTER(Enabled)(fUSBEnabled);
1686 CheckComRCThrowRC(rc);
1687
1688 /* Change the network adapters */
1689 std::list<VirtualSystemDescriptionEntry*> vsdeNW = vsdescThis->findByType(VirtualSystemDescriptionType_NetworkAdapter);
1690 if (vsdeNW.size() == 0)
1691 {
1692 /* No network adapters, so we have to disable our default one */
1693 ComPtr<INetworkAdapter> nwVBox;
1694 rc = pNewMachine->GetNetworkAdapter(0, nwVBox.asOutParam());
1695 CheckComRCThrowRC(rc);
1696 rc = nwVBox->COMSETTER(Enabled)(false);
1697 CheckComRCThrowRC(rc);
1698 }
1699 else
1700 {
1701 list<VirtualSystemDescriptionEntry*>::const_iterator nwIt;
1702 /* Iterate through all network cards. We support 8 network adapters
1703 * at the maximum. (@todo: warn if there are more!) */
1704 size_t a = 0;
1705 for (nwIt = vsdeNW.begin();
1706 (nwIt != vsdeNW.end() && a < SchemaDefs::NetworkAdapterCount);
1707 ++nwIt, ++a)
1708 {
1709 const Utf8Str &nwTypeVBox = (*nwIt)->strConfig;
1710 uint32_t tt1 = RTStrToUInt32(nwTypeVBox.c_str());
1711 ComPtr<INetworkAdapter> nwVBox;
1712 rc = pNewMachine->GetNetworkAdapter((ULONG)a, nwVBox.asOutParam());
1713 CheckComRCThrowRC(rc);
1714 /* Enable the network card & set the adapter type */
1715 /* NAT is set as default */
1716 rc = nwVBox->COMSETTER(Enabled)(true);
1717 CheckComRCThrowRC(rc);
1718 rc = nwVBox->COMSETTER(AdapterType)(static_cast<NetworkAdapterType_T>(tt1));
1719 CheckComRCThrowRC(rc);
1720 }
1721 }
1722
1723 /* Floppy drive */
1724 std::list<VirtualSystemDescriptionEntry*> vsdeFloppy = vsdescThis->findByType(VirtualSystemDescriptionType_Floppy);
1725 // Floppy support is enabled if there's at least one such entry; to disable floppy support,
1726 // the type of the floppy item would have been changed to "ignore"
1727 bool fFloppyEnabled = vsdeFloppy.size() > 0;
1728 ComPtr<IFloppyDrive> floppyDrive;
1729 rc = pNewMachine->COMGETTER(FloppyDrive)(floppyDrive.asOutParam());
1730 CheckComRCThrowRC(rc);
1731 rc = floppyDrive->COMSETTER(Enabled)(fFloppyEnabled);
1732 CheckComRCThrowRC(rc);
1733
1734 /* CDROM drive */
1735 /* @todo: I can't disable the CDROM. So nothing to do for now */
1736 // std::list<VirtualSystemDescriptionEntry*> vsdeFloppy = vsd->findByType(VirtualSystemDescriptionType_CDROM);
1737
1738 /* Hard disk controller IDE */
1739 std::list<VirtualSystemDescriptionEntry*> vsdeHDCIDE = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerIDE);
1740 /* @todo: we support one IDE controller only */
1741 if (vsdeHDCIDE.size() > 0)
1742 {
1743 // set the appropriate IDE controller in the virtual BIOS of the VM
1744 ComPtr<IBIOSSettings> biosSettings;
1745 rc = pNewMachine->COMGETTER(BIOSSettings)(biosSettings.asOutParam());
1746 CheckComRCThrowRC(rc);
1747 const char *pcszIDEType = vsdeHDCIDE.front()->strConfig.c_str();
1748 if (!strcmp(pcszIDEType, "PIIX3"))
1749 rc = biosSettings->COMSETTER(IDEControllerType)(IDEControllerType_PIIX3);
1750 else if (!strcmp(pcszIDEType, "PIIX4"))
1751 rc = biosSettings->COMSETTER(IDEControllerType)(IDEControllerType_PIIX4);
1752 else
1753 throw setError(VBOX_E_FILE_ERROR,
1754 tr("Invalid IDE controller type \"%s\""),
1755 pcszIDEType);
1756 CheckComRCThrowRC(rc);
1757 }
1758#ifdef VBOX_WITH_AHCI
1759 /* Hard disk controller SATA */
1760 std::list<VirtualSystemDescriptionEntry*> vsdeHDCSATA = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerSATA);
1761 /* @todo: we support one SATA controller only */
1762 if (vsdeHDCSATA.size() > 0)
1763 {
1764 const Utf8Str &hdcVBox = vsdeHDCIDE.front()->strConfig;
1765 if (hdcVBox == "AHCI")
1766 {
1767 /* For now we have just to enable the AHCI controller. */
1768 ComPtr<ISATAController> hdcSATAVBox;
1769 rc = pNewMachine->COMGETTER(SATAController)(hdcSATAVBox.asOutParam());
1770 CheckComRCThrowRC(rc);
1771 rc = hdcSATAVBox->COMSETTER(Enabled)(true);
1772 CheckComRCThrowRC(rc);
1773 }
1774 else
1775 {
1776 /* @todo: set an error if this is other than AHCI */
1777 }
1778 }
1779#endif /* VBOX_WITH_AHCI */
1780
1781 /* Hard disk controller SCSI */
1782 std::list<VirtualSystemDescriptionEntry*> vsdeHDCSCSI = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskControllerSCSI);
1783 /* @todo: do we support more than one SCSI controller? */
1784 if (vsdeHDCSCSI.size() > 0)
1785 {
1786 /* @todo: revisit when Main support for SCSI is ready */
1787 }
1788
1789 /* Now its time to register the machine before we add any hard disks */
1790 rc = app->mVirtualBox->RegisterMachine(pNewMachine);
1791 CheckComRCThrowRC(rc);
1792
1793 if (!task->progress.isNull())
1794 task->progress->notifyProgress(static_cast<ULONG>(opCountMax * opCount++));
1795
1796 /* Create the hard disks & connect them to the appropriate controllers. */
1797 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
1798 if (avsdeHDs.size() > 0)
1799 {
1800 Guid newMachineId;
1801 rc = pNewMachine->COMGETTER(Id)(newMachineId.asOutParam());
1802 CheckComRCThrowRC(rc);
1803 /* If in the next block an error occur we have to deregister
1804 the machine, so make an extra try/catch block. */
1805 ComPtr<ISession> session;
1806 try
1807 {
1808 // in order to attach hard disks we need to open a session for the new machine
1809 rc = session.createInprocObject(CLSID_Session);
1810 CheckComRCThrowRC(rc);
1811 rc = app->mVirtualBox->OpenSession(session, newMachineId);
1812 CheckComRCThrowRC(rc);
1813
1814 int result;
1815 /* The disk image has to be on the same place as the OVF file. So
1816 * strip the filename out of the full file path. */
1817 Utf8Str strSrcDir = stripFilename(Utf8Str(app->m->bstrPath).raw());
1818
1819 /* Iterate over all given disk images */
1820 list<VirtualSystemDescriptionEntry*>::const_iterator itHD;
1821 for (itHD = avsdeHDs.begin();
1822 itHD != avsdeHDs.end();
1823 ++itHD)
1824 {
1825 VirtualSystemDescriptionEntry *vsdeHD = *itHD;
1826
1827 const char *pcszDstFilePath = vsdeHD->strConfig.c_str();
1828 /* Check if the destination file exists already or the
1829 * destination path is empty. */
1830 if ( !(*pcszDstFilePath)
1831 || RTPathExists(pcszDstFilePath)
1832 )
1833 /* This isn't allowed */
1834 throw setError(VBOX_E_FILE_ERROR,
1835 tr("Destination file '%s' exists",
1836 pcszDstFilePath));
1837
1838 // find the disk from the OVF's disk list
1839 DiskImagesMap::const_iterator itDiskImage = app->m->mapDisks.find(vsdeHD->strRef);
1840 // vsdeHD->strRef contains the disk identifier (e.g. "vmdisk1", which should exist
1841 // in the virtual system's disks map under that ID and also in the global images map
1842 VirtualDisksMap::const_iterator itVirtualDisk = vsysThis.mapVirtualDisks.find(vsdeHD->strRef);
1843
1844 if ( itDiskImage == app->m->mapDisks.end()
1845 || itVirtualDisk == vsysThis.mapVirtualDisks.end()
1846 )
1847 throw setError(E_FAIL,
1848 tr("Internal inconsistency looking up disk images."));
1849
1850 const DiskImage &di = itDiskImage->second;
1851 const VirtualDisk &vd = itVirtualDisk->second;
1852
1853 /* Construct the source file path */
1854 Utf8StrFmt strSrcFilePath("%s%c%s", strSrcDir.c_str(), RTPATH_DELIMITER, di.strHref.c_str());
1855 /* Check if the source file exists */
1856 if (!RTPathExists(strSrcFilePath.c_str()))
1857 {
1858 /* @todo: we have to create a new one */
1859 }
1860 else
1861 {
1862 /* Make sure all target directories exists */
1863 rc = VirtualBox::ensureFilePathExists(pcszDstFilePath);
1864 CheckComRCThrowRC(rc);
1865 /* Clone the disk image (this is necessary cause the id has
1866 * to be recreated for the case the same hard disk is
1867 * attached already from a previous import) */
1868 /* First open the existing disk image */
1869 ComPtr<IHardDisk2> srcHdVBox;
1870 rc = app->mVirtualBox->OpenHardDisk2(Bstr(strSrcFilePath), srcHdVBox.asOutParam());
1871 CheckComRCThrowRC(rc);
1872 /* We need the format description of the source disk image */
1873 Bstr srcFormat;
1874 rc = srcHdVBox->COMGETTER(Format)(srcFormat.asOutParam());
1875 CheckComRCThrowRC(rc);
1876 /* Create a new hard disk interface for the destination disk image */
1877 ComPtr<IHardDisk2> dstHdVBox;
1878 rc = app->mVirtualBox->CreateHardDisk2(srcFormat, Bstr(pcszDstFilePath), dstHdVBox.asOutParam());
1879 CheckComRCThrowRC(rc);
1880 /* Clone the source disk image */
1881 ComPtr<IProgress> progress;
1882 rc = srcHdVBox->CloneTo(dstHdVBox, progress.asOutParam());
1883 CheckComRCThrowRC(rc);
1884 rc = progress->WaitForCompletion(-1);
1885 CheckComRCThrowRC(rc);
1886 /* We *must* close the source disk image in order to deregister it */
1887 rc = srcHdVBox->Close();
1888 CheckComRCThrowRC(rc);
1889 /* Now use the new uuid to attach the disk image to our new machine */
1890 ComPtr<IMachine> sMachine;
1891 rc = session->COMGETTER(Machine)(sMachine.asOutParam());
1892 Guid hdId;
1893 rc = dstHdVBox->COMGETTER(Id)(hdId.asOutParam());;
1894 CheckComRCThrowRC(rc);
1895 /* For now we assume we have one controller of every type only */
1896 HardDiskController hdc = (*vsysThis.mapControllers.find(vd.idController)).second;
1897 StorageBus_T sbt = StorageBus_IDE;
1898 switch (hdc.system)
1899 {
1900 case HardDiskController::IDE: sbt = StorageBus_IDE; break;
1901 case HardDiskController::SATA: sbt = StorageBus_SATA; break;
1902 //case HardDiskController::SCSI: sbt = StorageBus_SCSI; break; // @todo: not available yet
1903 default: break;
1904 }
1905 rc = sMachine->AttachHardDisk2(hdId, sbt, hdc.ulBusNumber, 0);
1906 CheckComRCThrowRC(rc);
1907 rc = sMachine->SaveSettings();
1908 CheckComRCThrowRC(rc);
1909 rc = session->Close();
1910 CheckComRCThrowRC(rc);
1911 }
1912 }
1913 }
1914 catch(HRESULT aRC)
1915 {
1916 /* Unregister/Delete the failed machine */
1917 /* @todo: Not sure what to do when there are succesfully
1918 added disk images. Delete them also? For now we leave
1919 them. */
1920 if (!session.isNull())
1921 {
1922 /* If there is an open session, close them before doing
1923 anything further. */
1924 rc = session->Close();
1925 CheckComRCThrowRC(rc);
1926 }
1927 ComPtr<IMachine> failedMachine;
1928 rc = app->mVirtualBox->UnregisterMachine(newMachineId, failedMachine.asOutParam());
1929 CheckComRCThrowRC(rc);
1930 rc = failedMachine->DeleteSettings();
1931 CheckComRCThrowRC(rc);
1932 /* Throw the original error number */
1933 throw aRC;
1934 }
1935 }
1936 if (!task->progress.isNull())
1937 task->progress->notifyProgress(static_cast<ULONG>(opCountMax * opCount++));
1938 }
1939 catch(HRESULT aRC)
1940 {
1941 /* @todo: If we are here an error on importing of *one* virtual
1942 system has occured. We didn't break now, but try to import the
1943 other virtual systems. This needs some further discussion. */
1944 rc = aRC;
1945 }
1946 }
1947
1948 task->rc = rc;
1949
1950 if (!task->progress.isNull())
1951 task->progress->notifyComplete (rc);
1952
1953 /// @todo ugly hack, fix ComAssert... (same as in HardDisk2::taskThread)
1954 #undef setError
1955
1956 return VINF_SUCCESS;
1957}
1958
1959// IVirtualSystemDescription constructor / destructor
1960////////////////////////////////////////////////////////////////////////////////
1961
1962DEFINE_EMPTY_CTOR_DTOR(VirtualSystemDescription)
1963struct shutup3 {};
1964
1965struct VirtualSystemDescription::Data
1966{
1967 list<VirtualSystemDescriptionEntry> descriptions;
1968};
1969
1970HRESULT VirtualSystemDescription::init()
1971{
1972 /* Enclose the state transition NotReady->InInit->Ready */
1973 AutoInitSpan autoInitSpan(this);
1974 AssertReturn(autoInitSpan.isOk(), E_FAIL);
1975
1976 /* Initialize data */
1977 m = new Data();
1978
1979 /* Confirm a successful initialization */
1980 autoInitSpan.setSucceeded();
1981
1982 return S_OK;
1983}
1984
1985void VirtualSystemDescription::uninit()
1986{
1987 delete m;
1988 m = NULL;
1989}
1990
1991STDMETHODIMP VirtualSystemDescription::GetDescription(ComSafeArrayOut(VirtualSystemDescriptionType_T, aTypes),
1992 ComSafeArrayOut(BSTR, aRefs),
1993 ComSafeArrayOut(BSTR, aOrigValues),
1994 ComSafeArrayOut(BSTR, aConfigValues),
1995 ComSafeArrayOut(BSTR, aExtraConfigValues))
1996{
1997 if (ComSafeArrayOutIsNull(aTypes) ||
1998 ComSafeArrayOutIsNull(aRefs) ||
1999 ComSafeArrayOutIsNull(aOrigValues) ||
2000 ComSafeArrayOutIsNull(aConfigValues) ||
2001 ComSafeArrayOutIsNull(aExtraConfigValues))
2002 return E_POINTER;
2003
2004 AutoCaller autoCaller(this);
2005 CheckComRCReturnRC(autoCaller.rc());
2006
2007 AutoReadLock alock(this);
2008
2009 ULONG c = (ULONG)m->descriptions.size();
2010 com::SafeArray<VirtualSystemDescriptionType_T> sfaTypes(c);
2011 com::SafeArray<BSTR> sfaRefs(c);
2012 com::SafeArray<BSTR> sfaOrigValues(c);
2013 com::SafeArray<BSTR> sfaConfigValues(c);
2014 com::SafeArray<BSTR> sfaExtraConfigValues(c);
2015
2016 list<VirtualSystemDescriptionEntry>::const_iterator it;
2017 size_t i = 0;
2018 for (it = m->descriptions.begin();
2019 it != m->descriptions.end();
2020 ++it, ++i)
2021 {
2022 const VirtualSystemDescriptionEntry &vsde = (*it);
2023
2024 sfaTypes[i] = vsde.type;
2025
2026 Bstr bstr = vsde.strRef;
2027 bstr.cloneTo(&sfaRefs[i]);
2028
2029 bstr = vsde.strOrig;
2030 bstr.cloneTo(&sfaOrigValues[i]);
2031
2032 bstr = vsde.strConfig;
2033 bstr.cloneTo(&sfaConfigValues[i]);
2034
2035 bstr = vsde.strExtraConfig;
2036 bstr.cloneTo(&sfaExtraConfigValues[i]);
2037 }
2038
2039 sfaTypes.detachTo(ComSafeArrayOutArg(aTypes));
2040 sfaRefs.detachTo(ComSafeArrayOutArg(aRefs));
2041 sfaOrigValues.detachTo(ComSafeArrayOutArg(aOrigValues));
2042 sfaConfigValues.detachTo(ComSafeArrayOutArg(aConfigValues));
2043 sfaExtraConfigValues.detachTo(ComSafeArrayOutArg(aExtraConfigValues));
2044
2045 return S_OK;
2046}
2047
2048STDMETHODIMP VirtualSystemDescription::DisableItem(ULONG index)
2049{
2050 AutoCaller autoCaller(this);
2051 CheckComRCReturnRC(autoCaller.rc());
2052
2053 AutoWriteLock alock(this);
2054
2055 list<VirtualSystemDescriptionEntry>::iterator it;
2056 for (it = m->descriptions.begin();
2057 it != m->descriptions.end();
2058 ++it)
2059 {
2060 VirtualSystemDescriptionEntry &e = *it;
2061 if (e.ulIndex == index)
2062 {
2063 e.type = VirtualSystemDescriptionType_Ignore;
2064 return S_OK;
2065 }
2066 }
2067
2068 return setError(VBOX_E_OBJECT_NOT_FOUND,
2069 tr("Array item index %d not found"),
2070 index);
2071}
2072
2073STDMETHODIMP VirtualSystemDescription::SetFinalValues(ComSafeArrayIn(IN_BSTR, aFinalValues))
2074{
2075 CheckComArgSafeArrayNotNull(aFinalValues);
2076
2077 AutoCaller autoCaller(this);
2078 CheckComRCReturnRC(autoCaller.rc());
2079
2080 AutoWriteLock alock(this);
2081
2082 com::SafeArray <IN_BSTR> values(ComSafeArrayInArg(aFinalValues));
2083 if (values.size() != m->descriptions.size())
2084 return E_INVALIDARG;
2085
2086 list<VirtualSystemDescriptionEntry>::const_iterator it;
2087 size_t i = 0;
2088 for (it = m->descriptions.begin();
2089 it != m->descriptions.end();
2090 ++it, ++i)
2091 {
2092 VirtualSystemDescriptionEntry vsde = (*it);
2093 vsde.strConfig = values[i];
2094 }
2095
2096 return S_OK;
2097}
2098
2099void VirtualSystemDescription::addEntry(VirtualSystemDescriptionType_T aType,
2100 const Utf8Str &strRef,
2101 const Utf8Str &aOrigValue,
2102 const Utf8Str &aAutoValue,
2103 const Utf8Str &strExtraConfig /*= ""*/)
2104{
2105 VirtualSystemDescriptionEntry vsde;
2106 vsde.ulIndex = m->descriptions.size(); // each entry gets an index so the client side can reference them
2107 vsde.type = aType;
2108 vsde.strRef = strRef;
2109 vsde.strOrig = aOrigValue;
2110 vsde.strConfig = aAutoValue;
2111 vsde.strExtraConfig = strExtraConfig;
2112
2113 m->descriptions.push_back(vsde);
2114}
2115
2116std::list<VirtualSystemDescriptionEntry*> VirtualSystemDescription::findByType(VirtualSystemDescriptionType_T aType)
2117{
2118 std::list<VirtualSystemDescriptionEntry*> vsd;
2119 list<VirtualSystemDescriptionEntry>::iterator it;
2120 for (it = m->descriptions.begin();
2121 it != m->descriptions.end();
2122 ++it)
2123 if (it->type == aType)
2124 vsd.push_back(&(*it));
2125
2126 return vsd;
2127}
2128
2129const VirtualSystemDescriptionEntry* VirtualSystemDescription::findControllerFromID(uint32_t id)
2130{
2131 Utf8Str strRef = toString<uint32_t>(id);
2132 list<VirtualSystemDescriptionEntry>::const_iterator it;
2133 for (it = m->descriptions.begin();
2134 it != m->descriptions.end();
2135 ++it)
2136 {
2137 switch (it->type)
2138 {
2139 case VirtualSystemDescriptionType_HardDiskControllerIDE:
2140 case VirtualSystemDescriptionType_HardDiskControllerSATA:
2141 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
2142 if (it->strRef == strRef)
2143 return &(*it);
2144 break;
2145 }
2146 }
2147
2148 return NULL;
2149}
2150
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use