VirtualBox

source: vbox/trunk/src/VBox/Main/xml/ovfreader.cpp@ 28999

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.0 KB
Line 
1/* $Id: ovfreader.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 *
4 * OVF reader declarations. Depends only on IPRT, including the iprt::MiniString
5 * and IPRT XML classes.
6 */
7
8/*
9 * Copyright (C) 2008-2009 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#include "ovfreader.h"
21
22using namespace std;
23using namespace iprt;
24using namespace ovf;
25
26////////////////////////////////////////////////////////////////////////////////
27//
28// OVF reader implemenation
29//
30////////////////////////////////////////////////////////////////////////////////
31
32/**
33 * Constructor. This opens the given XML file and parses it. Throws lots of exceptions
34 * on XML or OVF invalidity.
35 * @param path
36 */
37OVFReader::OVFReader(const MiniString &path)
38 : m_strPath(path)
39{
40 xml::XmlFileParser parser;
41 parser.read(m_strPath,
42 m_doc);
43
44 const xml::ElementNode *pRootElem = m_doc.getRootElement();
45 if ( !pRootElem
46 || strcmp(pRootElem->getName(), "Envelope")
47 )
48 throw OVFLogicError(N_("Root element in OVF file must be \"Envelope\"."));
49
50 // OVF has the following rough layout:
51 /*
52 -- <References> .... files referenced from other parts of the file, such as VMDK images
53 -- Metadata, comprised of several section commands
54 -- virtual machines, either a single <VirtualSystem>, or a <VirtualSystemCollection>
55 -- optionally <Strings> for localization
56 */
57
58 // get all "File" child elements of "References" section so we can look up files easily;
59 // first find the "References" sections so we can look up files
60 xml::ElementNodesList listFileElements; // receives all /Envelope/References/File nodes
61 const xml::ElementNode *pReferencesElem;
62 if ((pReferencesElem = pRootElem->findChildElement("References")))
63 pReferencesElem->getChildElements(listFileElements, "File");
64
65 // now go though the sections
66 LoopThruSections(pReferencesElem, pRootElem);
67}
68
69/**
70 * Private helper method that goes thru the elements of the given "current" element in the OVF XML
71 * and handles the contained child elements (which can be "Section" or "Content" elements).
72 *
73 * @param pcszPath Path spec of the XML file, for error messages.
74 * @param pReferencesElement "References" element from OVF, for looking up file specifications; can be NULL if no such element is present.
75 * @param pCurElem Element whose children are to be analyzed here.
76 * @return
77 */
78void OVFReader::LoopThruSections(const xml::ElementNode *pReferencesElem,
79 const xml::ElementNode *pCurElem)
80{
81 xml::NodesLoop loopChildren(*pCurElem);
82 const xml::ElementNode *pElem;
83 while ((pElem = loopChildren.forAllNodes()))
84 {
85 const char *pcszElemName = pElem->getName();
86 const char *pcszTypeAttr = "";
87 const xml::AttributeNode *pTypeAttr;
88 if ((pTypeAttr = pElem->findAttribute("type")))
89 pcszTypeAttr = pTypeAttr->getValue();
90
91 if ( (!strcmp(pcszElemName, "DiskSection"))
92 || ( (!strcmp(pcszElemName, "Section"))
93 && (!strcmp(pcszTypeAttr, "ovf:DiskSection_Type"))
94 )
95 )
96 {
97 HandleDiskSection(pReferencesElem, pElem);
98 }
99 else if ( (!strcmp(pcszElemName, "NetworkSection"))
100 || ( (!strcmp(pcszElemName, "Section"))
101 && (!strcmp(pcszTypeAttr, "ovf:NetworkSection_Type"))
102 )
103 )
104 {
105 HandleNetworkSection(pElem);
106 }
107 else if ( (!strcmp(pcszElemName, "DeploymentOptionSection")))
108 {
109 // TODO
110 }
111 else if ( (!strcmp(pcszElemName, "Info")))
112 {
113 // child of VirtualSystemCollection -- TODO
114 }
115 else if ( (!strcmp(pcszElemName, "ResourceAllocationSection")))
116 {
117 // child of VirtualSystemCollection -- TODO
118 }
119 else if ( (!strcmp(pcszElemName, "StartupSection")))
120 {
121 // child of VirtualSystemCollection -- TODO
122 }
123 else if ( (!strcmp(pcszElemName, "VirtualSystem"))
124 || ( (!strcmp(pcszElemName, "Content"))
125 && (!strcmp(pcszTypeAttr, "ovf:VirtualSystem_Type"))
126 )
127 )
128 {
129 HandleVirtualSystemContent(pElem);
130 }
131 else if ( (!strcmp(pcszElemName, "VirtualSystemCollection"))
132 || ( (!strcmp(pcszElemName, "Content"))
133 && (!strcmp(pcszTypeAttr, "ovf:VirtualSystemCollection_Type"))
134 )
135 )
136 {
137 // TODO ResourceAllocationSection
138
139 // recurse for this, since it has VirtualSystem elements as children
140 LoopThruSections(pReferencesElem, pElem);
141 }
142 }
143}
144
145/**
146 * Private helper method that handles disk sections in the OVF XML.
147 * Gets called indirectly from IAppliance::read().
148 *
149 * @param pcszPath Path spec of the XML file, for error messages.
150 * @param pReferencesElement "References" element from OVF, for looking up file specifications; can be NULL if no such element is present.
151 * @param pSectionElem Section element for which this helper is getting called.
152 * @return
153 */
154void OVFReader::HandleDiskSection(const xml::ElementNode *pReferencesElem,
155 const xml::ElementNode *pSectionElem)
156{
157 // contains "Disk" child elements
158 xml::NodesLoop loopDisks(*pSectionElem, "Disk");
159 const xml::ElementNode *pelmDisk;
160 while ((pelmDisk = loopDisks.forAllNodes()))
161 {
162 DiskImage d;
163 const char *pcszBad = NULL;
164 const char *pcszDiskId;
165 const char *pcszFormat;
166 if (!(pelmDisk->getAttributeValue("diskId", pcszDiskId)))
167 pcszBad = "diskId";
168 else if (!(pelmDisk->getAttributeValue("format", pcszFormat)))
169 pcszBad = "format";
170 else if (!(pelmDisk->getAttributeValue("capacity", d.iCapacity)))
171 pcszBad = "capacity";
172 else
173 {
174 d.strDiskId = pcszDiskId;
175 d.strFormat = pcszFormat;
176
177 if (!(pelmDisk->getAttributeValue("populatedSize", d.iPopulatedSize)))
178 // optional
179 d.iPopulatedSize = -1;
180
181 // optional vbox:uuid attribute (if OVF was exported by VirtualBox != 3.2)
182 pelmDisk->getAttributeValue("vbox:uuid", d.uuidVbox);
183
184 const char *pcszFileRef;
185 if (pelmDisk->getAttributeValue("fileRef", pcszFileRef)) // optional
186 {
187 // look up corresponding /References/File nodes (list built above)
188 const xml::ElementNode *pFileElem;
189 if ( pReferencesElem
190 && ((pFileElem = pReferencesElem->findChildElementFromId(pcszFileRef)))
191 )
192 {
193 // copy remaining values from file node then
194 const char *pcszBadInFile = NULL;
195 const char *pcszHref;
196 if (!(pFileElem->getAttributeValue("href", pcszHref)))
197 pcszBadInFile = "href";
198 else if (!(pFileElem->getAttributeValue("size", d.iSize)))
199 d.iSize = -1; // optional
200
201 d.strHref = pcszHref;
202
203 // if (!(pFileElem->getAttributeValue("size", d.iChunkSize))) TODO
204 d.iChunkSize = -1; // optional
205 const char *pcszCompression;
206 if (pFileElem->getAttributeValue("compression", pcszCompression))
207 d.strCompression = pcszCompression;
208
209 if (pcszBadInFile)
210 throw OVFLogicError(N_("Error reading \"%s\": missing or invalid attribute '%s' in 'File' element, line %d"),
211 m_strPath.c_str(),
212 pcszBadInFile,
213 pFileElem->getLineNumber());
214 }
215 else
216 throw OVFLogicError(N_("Error reading \"%s\": cannot find References/File element for ID '%s' referenced by 'Disk' element, line %d"),
217 m_strPath.c_str(),
218 pcszFileRef,
219 pelmDisk->getLineNumber());
220 }
221 }
222
223 if (pcszBad)
224 throw OVFLogicError(N_("Error reading \"%s\": missing or invalid attribute '%s' in 'DiskSection' element, line %d"),
225 m_strPath.c_str(),
226 pcszBad,
227 pelmDisk->getLineNumber());
228
229 // suggest a size in megabytes to help callers with progress reports
230 d.ulSuggestedSizeMB = 0;
231 if (d.iCapacity != -1)
232 d.ulSuggestedSizeMB = d.iCapacity / _1M;
233 else if (d.iPopulatedSize != -1)
234 d.ulSuggestedSizeMB = d.iPopulatedSize / _1M;
235 else if (d.iSize != -1)
236 d.ulSuggestedSizeMB = d.iSize / _1M;
237 if (d.ulSuggestedSizeMB == 0)
238 d.ulSuggestedSizeMB = 10000; // assume 10 GB, this is for the progress bar only anyway
239
240 m_mapDisks[d.strDiskId] = d;
241 }
242}
243
244/**
245 * Private helper method that handles network sections in the OVF XML.
246 * Gets called indirectly from IAppliance::read().
247 *
248 * @param pcszPath Path spec of the XML file, for error messages.
249 * @param pSectionElem Section element for which this helper is getting called.
250 * @return
251 */
252void OVFReader::HandleNetworkSection(const xml::ElementNode * /* pSectionElem */)
253{
254 // we ignore network sections for now
255
256// xml::NodesLoop loopNetworks(*pSectionElem, "Network");
257// const xml::Node *pelmNetwork;
258// while ((pelmNetwork = loopNetworks.forAllNodes()))
259// {
260// Network n;
261// if (!(pelmNetwork->getAttributeValue("name", n.strNetworkName)))
262// return setError(VBOX_E_FILE_ERROR,
263// tr("Error reading \"%s\": missing 'name' attribute in 'Network', line %d"),
264// pcszPath,
265// pelmNetwork->getLineNumber());
266//
267// m->mapNetworks[n.strNetworkName] = n;
268// }
269}
270
271/**
272 * Private helper method that handles a "VirtualSystem" element in the OVF XML.
273 * Gets called indirectly from IAppliance::read().
274 *
275 * @param pcszPath
276 * @param pContentElem
277 * @return
278 */
279void OVFReader::HandleVirtualSystemContent(const xml::ElementNode *pelmVirtualSystem)
280{
281 VirtualSystem vsys;
282
283 // peek under the <VirtualSystem> node whether we have a <vbox:Machine> node;
284 // that case case, the caller can completely ignore the OVF but only load the VBox machine XML
285 vsys.pelmVboxMachine = pelmVirtualSystem->findChildElement("vbox", "Machine");
286
287 // now look for real OVF
288 const xml::AttributeNode *pIdAttr = pelmVirtualSystem->findAttribute("id");
289 if (pIdAttr)
290 vsys.strName = pIdAttr->getValue();
291
292 xml::NodesLoop loop(*pelmVirtualSystem); // all child elements
293 const xml::ElementNode *pelmThis;
294 while ((pelmThis = loop.forAllNodes()))
295 {
296 const char *pcszElemName = pelmThis->getName();
297 const xml::AttributeNode *pTypeAttr = pelmThis->findAttribute("type");
298 const char *pcszTypeAttr = (pTypeAttr) ? pTypeAttr->getValue() : "";
299
300 if ( (!strcmp(pcszElemName, "EulaSection"))
301 || (!strcmp(pcszTypeAttr, "ovf:EulaSection_Type"))
302 )
303 {
304 /* <EulaSection>
305 <Info ovf:msgid="6">License agreement for the Virtual System.</Info>
306 <License ovf:msgid="1">License terms can go in here.</License>
307 </EulaSection> */
308
309 const xml::ElementNode *pelmLicense;
310 if ((pelmLicense = pelmThis->findChildElement("License")))
311 vsys.strLicenseText = pelmLicense->getValue();
312 }
313 if ( (!strcmp(pcszElemName, "ProductSection"))
314 || (!strcmp(pcszTypeAttr, "ovf:ProductSection_Type"))
315 )
316 {
317 /* <Section ovf:required="false" xsi:type="ovf:ProductSection_Type">
318 <Info>Meta-information about the installed software</Info>
319 <Product>VAtest</Product>
320 <Vendor>SUN Microsystems</Vendor>
321 <Version>10.0</Version>
322 <ProductUrl>http://blogs.sun.com/VirtualGuru</ProductUrl>
323 <VendorUrl>http://www.sun.com</VendorUrl>
324 </Section> */
325 const xml::ElementNode *pelmProduct;
326 if ((pelmProduct = pelmThis->findChildElement("Product")))
327 vsys.strProduct = pelmProduct->getValue();
328 const xml::ElementNode *pelmVendor;
329 if ((pelmVendor = pelmThis->findChildElement("Vendor")))
330 vsys.strVendor = pelmVendor->getValue();
331 const xml::ElementNode *pelmVersion;
332 if ((pelmVersion = pelmThis->findChildElement("Version")))
333 vsys.strVersion = pelmVersion->getValue();
334 const xml::ElementNode *pelmProductUrl;
335 if ((pelmProductUrl = pelmThis->findChildElement("ProductUrl")))
336 vsys.strProductUrl = pelmProductUrl->getValue();
337 const xml::ElementNode *pelmVendorUrl;
338 if ((pelmVendorUrl = pelmThis->findChildElement("VendorUrl")))
339 vsys.strVendorUrl = pelmVendorUrl->getValue();
340 }
341 else if ( (!strcmp(pcszElemName, "VirtualHardwareSection"))
342 || (!strcmp(pcszTypeAttr, "ovf:VirtualHardwareSection_Type"))
343 )
344 {
345 const xml::ElementNode *pelmSystem, *pelmVirtualSystemType;
346 if ((pelmSystem = pelmThis->findChildElement("System")))
347 {
348 /* <System>
349 <vssd:Description>Description of the virtual hardware section.</vssd:Description>
350 <vssd:ElementName>vmware</vssd:ElementName>
351 <vssd:InstanceID>1</vssd:InstanceID>
352 <vssd:VirtualSystemIdentifier>MyLampService</vssd:VirtualSystemIdentifier>
353 <vssd:VirtualSystemType>vmx-4</vssd:VirtualSystemType>
354 </System>*/
355 if ((pelmVirtualSystemType = pelmSystem->findChildElement("VirtualSystemType")))
356 vsys.strVirtualSystemType = pelmVirtualSystemType->getValue();
357 }
358
359 xml::NodesLoop loopVirtualHardwareItems(*pelmThis, "Item"); // all "Item" child elements
360 const xml::ElementNode *pelmItem;
361 while ((pelmItem = loopVirtualHardwareItems.forAllNodes()))
362 {
363 VirtualHardwareItem i;
364
365 i.ulLineNumber = pelmItem->getLineNumber();
366
367 xml::NodesLoop loopItemChildren(*pelmItem); // all child elements
368 const xml::ElementNode *pelmItemChild;
369 while ((pelmItemChild = loopItemChildren.forAllNodes()))
370 {
371 const char *pcszItemChildName = pelmItemChild->getName();
372 if (!strcmp(pcszItemChildName, "Description"))
373 i.strDescription = pelmItemChild->getValue();
374 else if (!strcmp(pcszItemChildName, "Caption"))
375 i.strCaption = pelmItemChild->getValue();
376 else if (!strcmp(pcszItemChildName, "ElementName"))
377 i.strElementName = pelmItemChild->getValue();
378 else if ( (!strcmp(pcszItemChildName, "InstanceID"))
379 || (!strcmp(pcszItemChildName, "InstanceId"))
380 )
381 pelmItemChild->copyValue(i.ulInstanceID);
382 else if (!strcmp(pcszItemChildName, "HostResource"))
383 i.strHostResource = pelmItemChild->getValue();
384 else if (!strcmp(pcszItemChildName, "ResourceType"))
385 {
386 uint32_t ulType;
387 pelmItemChild->copyValue(ulType);
388 i.resourceType = (ResourceType_T)ulType;
389 }
390 else if (!strcmp(pcszItemChildName, "OtherResourceType"))
391 i.strOtherResourceType = pelmItemChild->getValue();
392 else if (!strcmp(pcszItemChildName, "ResourceSubType"))
393 i.strResourceSubType = pelmItemChild->getValue();
394 else if (!strcmp(pcszItemChildName, "AutomaticAllocation"))
395 i.fAutomaticAllocation = (!strcmp(pelmItemChild->getValue(), "true")) ? true : false;
396 else if (!strcmp(pcszItemChildName, "AutomaticDeallocation"))
397 i.fAutomaticDeallocation = (!strcmp(pelmItemChild->getValue(), "true")) ? true : false;
398 else if (!strcmp(pcszItemChildName, "Parent"))
399 pelmItemChild->copyValue(i.ulParent);
400 else if (!strcmp(pcszItemChildName, "Connection"))
401 i.strConnection = pelmItemChild->getValue();
402 else if (!strcmp(pcszItemChildName, "Address"))
403 i.strAddress = pelmItemChild->getValue();
404 else if (!strcmp(pcszItemChildName, "AddressOnParent"))
405 i.strAddressOnParent = pelmItemChild->getValue();
406 else if (!strcmp(pcszItemChildName, "AllocationUnits"))
407 i.strAllocationUnits = pelmItemChild->getValue();
408 else if (!strcmp(pcszItemChildName, "VirtualQuantity"))
409 pelmItemChild->copyValue(i.ullVirtualQuantity);
410 else if (!strcmp(pcszItemChildName, "Reservation"))
411 pelmItemChild->copyValue(i.ullReservation);
412 else if (!strcmp(pcszItemChildName, "Limit"))
413 pelmItemChild->copyValue(i.ullLimit);
414 else if (!strcmp(pcszItemChildName, "Weight"))
415 pelmItemChild->copyValue(i.ullWeight);
416 else if (!strcmp(pcszItemChildName, "ConsumerVisibility"))
417 i.strConsumerVisibility = pelmItemChild->getValue();
418 else if (!strcmp(pcszItemChildName, "MappingBehavior"))
419 i.strMappingBehavior = pelmItemChild->getValue();
420 else if (!strcmp(pcszItemChildName, "PoolID"))
421 i.strPoolID = pelmItemChild->getValue();
422 else if (!strcmp(pcszItemChildName, "BusNumber")) // seen in some old OVF, but it's not listed in the OVF specs
423 pelmItemChild->copyValue(i.ulBusNumber);
424 else
425 throw OVFLogicError(N_("Error reading \"%s\": unknown element \"%s\" under Item element, line %d"),
426 m_strPath.c_str(),
427 pcszItemChildName,
428 i.ulLineNumber);
429 }
430
431 // store!
432 vsys.mapHardwareItems[i.ulInstanceID] = i;
433 }
434
435 // now go thru all hardware items and handle them according to their type;
436 // in this first loop we handle all items _except_ hard disk images,
437 // which we'll handle in a second loop below
438 HardwareItemsMap::const_iterator itH;
439 for (itH = vsys.mapHardwareItems.begin();
440 itH != vsys.mapHardwareItems.end();
441 ++itH)
442 {
443 const VirtualHardwareItem &i = itH->second;
444
445 // do some analysis
446 switch (i.resourceType)
447 {
448 case ResourceType_Processor: // 3
449 /* <rasd:Caption>1 virtual CPU</rasd:Caption>
450 <rasd:Description>Number of virtual CPUs</rasd:Description>
451 <rasd:ElementName>virtual CPU</rasd:ElementName>
452 <rasd:InstanceID>1</rasd:InstanceID>
453 <rasd:ResourceType>3</rasd:ResourceType>
454 <rasd:VirtualQuantity>1</rasd:VirtualQuantity>*/
455 if (i.ullVirtualQuantity < UINT16_MAX)
456 vsys.cCPUs = (uint16_t)i.ullVirtualQuantity;
457 else
458 throw OVFLogicError(N_("Error reading \"%s\": CPU count %RI64 is larger than %d, line %d"),
459 m_strPath.c_str(),
460 i.ullVirtualQuantity,
461 UINT16_MAX,
462 i.ulLineNumber);
463 break;
464
465 case ResourceType_Memory: // 4
466 if ( (i.strAllocationUnits == "MegaBytes") // found in OVF created by OVF toolkit
467 || (i.strAllocationUnits == "MB") // found in MS docs
468 || (i.strAllocationUnits == "byte * 2^20") // suggested by OVF spec DSP0243 page 21
469 )
470 vsys.ullMemorySize = i.ullVirtualQuantity * 1024 * 1024;
471 else
472 throw OVFLogicError(N_("Error reading \"%s\": Invalid allocation unit \"%s\" specified with memory size item, line %d"),
473 m_strPath.c_str(),
474 i.strAllocationUnits.c_str(),
475 i.ulLineNumber);
476 break;
477
478 case ResourceType_IDEController: // 5
479 {
480 /* <Item>
481 <rasd:Caption>ideController0</rasd:Caption>
482 <rasd:Description>IDE Controller</rasd:Description>
483 <rasd:InstanceId>5</rasd:InstanceId>
484 <rasd:ResourceType>5</rasd:ResourceType>
485 <rasd:Address>0</rasd:Address>
486 <rasd:BusNumber>0</rasd:BusNumber>
487 </Item> */
488 HardDiskController hdc;
489 hdc.system = HardDiskController::IDE;
490 hdc.idController = i.ulInstanceID;
491 hdc.strControllerType = i.strResourceSubType;
492
493 // if there is a numeric address tag for the IDE controller, use that;
494 // VMware uses "0" and "1" to keep the two OVF IDE controllers apart;
495 // otherwise use the "bus number" field which was specified in some old
496 // OVF files (but not the standard)
497 if (i.strAddress == "0")
498 hdc.ulAddress = 0;
499 else if (i.strAddress == "1")
500 hdc.ulAddress = 1;
501 else if (i.strAddress == "2") // just to be sure, this doesn't seem to be used by VMware
502 hdc.ulAddress = 2;
503 else if (i.strAddress == "3")
504 hdc.ulAddress = 3;
505 else
506 hdc.ulAddress = i.ulBusNumber;
507
508 vsys.mapControllers[i.ulInstanceID] = hdc;
509 }
510 break;
511
512 case ResourceType_ParallelSCSIHBA: // 6 SCSI controller
513 {
514 /* <Item>
515 <rasd:Caption>SCSI Controller 0 - LSI Logic</rasd:Caption>
516 <rasd:Description>SCI Controller</rasd:Description>
517 <rasd:ElementName>SCSI controller</rasd:ElementName>
518 <rasd:InstanceID>4</rasd:InstanceID>
519 <rasd:ResourceSubType>LsiLogic</rasd:ResourceSubType>
520 <rasd:ResourceType>6</rasd:ResourceType>
521 </Item> */
522 HardDiskController hdc;
523 hdc.system = HardDiskController::SCSI;
524 hdc.idController = i.ulInstanceID;
525 hdc.strControllerType = i.strResourceSubType;
526
527 vsys.mapControllers[i.ulInstanceID] = hdc;
528 }
529 break;
530
531 case ResourceType_EthernetAdapter: // 10
532 {
533 /* <Item>
534 <rasd:Caption>Ethernet adapter on 'Bridged'</rasd:Caption>
535 <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
536 <rasd:Connection>Bridged</rasd:Connection>
537 <rasd:InstanceID>6</rasd:InstanceID>
538 <rasd:ResourceType>10</rasd:ResourceType>
539 <rasd:ResourceSubType>E1000</rasd:ResourceSubType>
540 </Item>
541
542 OVF spec DSP 0243 page 21:
543 "For an Ethernet adapter, this specifies the abstract network connection name
544 for the virtual machine. All Ethernet adapters that specify the same abstract
545 network connection name within an OVF package shall be deployed on the same
546 network. The abstract network connection name shall be listed in the NetworkSection
547 at the outermost envelope level." */
548
549 // only store the name
550 EthernetAdapter ea;
551 ea.strAdapterType = i.strResourceSubType;
552 ea.strNetworkName = i.strConnection;
553 vsys.llEthernetAdapters.push_back(ea);
554 }
555 break;
556
557 case ResourceType_FloppyDrive: // 14
558 vsys.fHasFloppyDrive = true; // we have no additional information
559 break;
560
561 case ResourceType_CDDrive: // 15
562 /* <Item ovf:required="false">
563 <rasd:Caption>cdrom1</rasd:Caption>
564 <rasd:InstanceId>7</rasd:InstanceId>
565 <rasd:ResourceType>15</rasd:ResourceType>
566 <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
567 <rasd:Parent>5</rasd:Parent>
568 <rasd:AddressOnParent>0</rasd:AddressOnParent>
569 </Item> */
570 // I tried to see what happens if I set an ISO for the CD-ROM in VMware Workstation,
571 // but then the ovftool dies with "Device backing not supported". So I guess if
572 // VMware can't export ISOs, then we don't need to be able to import them right now.
573 vsys.fHasCdromDrive = true; // we have no additional information
574 break;
575
576 case ResourceType_HardDisk: // 17
577 // handled separately in second loop below
578 break;
579
580 case ResourceType_OtherStorageDevice: // 20 SATA controller
581 {
582 /* <Item>
583 <rasd:Description>SATA Controller</rasd:Description>
584 <rasd:Caption>sataController0</rasd:Caption>
585 <rasd:InstanceID>4</rasd:InstanceID>
586 <rasd:ResourceType>20</rasd:ResourceType>
587 <rasd:ResourceSubType>AHCI</rasd:ResourceSubType>
588 <rasd:Address>0</rasd:Address>
589 <rasd:BusNumber>0</rasd:BusNumber>
590 </Item> */
591 if ( i.strCaption.startsWith("sataController", MiniString::CaseInsensitive)
592 && !i.strResourceSubType.compare("AHCI", MiniString::CaseInsensitive)
593 )
594 {
595 HardDiskController hdc;
596 hdc.system = HardDiskController::SATA;
597 hdc.idController = i.ulInstanceID;
598 hdc.strControllerType = i.strResourceSubType;
599
600 vsys.mapControllers[i.ulInstanceID] = hdc;
601 }
602 else
603 throw OVFLogicError(N_("Error reading \"%s\": Host resource of type \"Other Storage Device (%d)\" is supported with SATA AHCI controllers only, line %d"),
604 m_strPath.c_str(),
605 ResourceType_OtherStorageDevice,
606 i.ulLineNumber);
607 }
608 break;
609
610 case ResourceType_USBController: // 23
611 /* <Item ovf:required="false">
612 <rasd:Caption>usb</rasd:Caption>
613 <rasd:Description>USB Controller</rasd:Description>
614 <rasd:InstanceId>3</rasd:InstanceId>
615 <rasd:ResourceType>23</rasd:ResourceType>
616 <rasd:Address>0</rasd:Address>
617 <rasd:BusNumber>0</rasd:BusNumber>
618 </Item> */
619 vsys.fHasUsbController = true; // we have no additional information
620 break;
621
622 case ResourceType_SoundCard: // 35
623 /* <Item ovf:required="false">
624 <rasd:Caption>sound</rasd:Caption>
625 <rasd:Description>Sound Card</rasd:Description>
626 <rasd:InstanceId>10</rasd:InstanceId>
627 <rasd:ResourceType>35</rasd:ResourceType>
628 <rasd:ResourceSubType>ensoniq1371</rasd:ResourceSubType>
629 <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
630 <rasd:AddressOnParent>3</rasd:AddressOnParent>
631 </Item> */
632 vsys.strSoundCardType = i.strResourceSubType;
633 break;
634
635 default:
636 throw OVFLogicError(N_("Error reading \"%s\": Unknown resource type %d in hardware item, line %d"),
637 m_strPath.c_str(),
638 i.resourceType,
639 i.ulLineNumber);
640 } // end switch
641 }
642
643 // now run through the items for a second time, but handle only
644 // hard disk images; otherwise the code would fail if a hard
645 // disk image appears in the OVF before its hard disk controller
646 for (itH = vsys.mapHardwareItems.begin();
647 itH != vsys.mapHardwareItems.end();
648 ++itH)
649 {
650 const VirtualHardwareItem &i = itH->second;
651
652 // do some analysis
653 switch (i.resourceType)
654 {
655 case ResourceType_HardDisk: // 17
656 {
657 /* <Item>
658 <rasd:Caption>Harddisk 1</rasd:Caption>
659 <rasd:Description>HD</rasd:Description>
660 <rasd:ElementName>Hard Disk</rasd:ElementName>
661 <rasd:HostResource>ovf://disk/lamp</rasd:HostResource>
662 <rasd:InstanceID>5</rasd:InstanceID>
663 <rasd:Parent>4</rasd:Parent>
664 <rasd:ResourceType>17</rasd:ResourceType>
665 </Item> */
666
667 // look up the hard disk controller element whose InstanceID equals our Parent;
668 // this is how the connection is specified in OVF
669 ControllersMap::const_iterator it = vsys.mapControllers.find(i.ulParent);
670 if (it == vsys.mapControllers.end())
671 throw OVFLogicError(N_("Error reading \"%s\": Hard disk item with instance ID %d specifies invalid parent %d, line %d"),
672 m_strPath.c_str(),
673 i.ulInstanceID,
674 i.ulParent,
675 i.ulLineNumber);
676 //const HardDiskController &hdc = it->second;
677
678 VirtualDisk vd;
679 vd.idController = i.ulParent;
680 i.strAddressOnParent.toInt(vd.ulAddressOnParent);
681 // ovf://disk/lamp
682 // 123456789012345
683 if (i.strHostResource.substr(0, 11) == "ovf://disk/")
684 vd.strDiskId = i.strHostResource.substr(11);
685 else if (i.strHostResource.substr(0, 10) == "ovf:/disk/")
686 vd.strDiskId = i.strHostResource.substr(10);
687 else if (i.strHostResource.substr(0, 6) == "/disk/")
688 vd.strDiskId = i.strHostResource.substr(6);
689
690 if ( !(vd.strDiskId.length())
691 || (m_mapDisks.find(vd.strDiskId) == m_mapDisks.end())
692 )
693 throw OVFLogicError(N_("Error reading \"%s\": Hard disk item with instance ID %d specifies invalid host resource \"%s\", line %d"),
694 m_strPath.c_str(),
695 i.ulInstanceID,
696 i.strHostResource.c_str(),
697 i.ulLineNumber);
698
699 vsys.mapVirtualDisks[vd.strDiskId] = vd;
700 }
701 break;
702 default: break;
703 }
704 }
705 }
706 else if ( (!strcmp(pcszElemName, "OperatingSystemSection"))
707 || (!strcmp(pcszTypeAttr, "ovf:OperatingSystemSection_Type"))
708 )
709 {
710 uint64_t cimos64;
711 if (!(pelmThis->getAttributeValue("id", cimos64)))
712 throw OVFLogicError(N_("Error reading \"%s\": missing or invalid 'ovf:id' attribute in operating system section element, line %d"),
713 m_strPath.c_str(),
714 pelmThis->getLineNumber());
715
716 vsys.cimos = (CIMOSType_T)cimos64;
717 const xml::ElementNode *pelmCIMOSDescription;
718 if ((pelmCIMOSDescription = pelmThis->findChildElement("Description")))
719 vsys.strCimosDesc = pelmCIMOSDescription->getValue();
720 }
721 else if ( (!strcmp(pcszElemName, "AnnotationSection"))
722 || (!strcmp(pcszTypeAttr, "ovf:AnnotationSection_Type"))
723 )
724 {
725 const xml::ElementNode *pelmAnnotation;
726 if ((pelmAnnotation = pelmThis->findChildElement("Annotation")))
727 vsys.strDescription = pelmAnnotation->getValue();
728 }
729 }
730
731 // now create the virtual system
732 m_llVirtualSystems.push_back(vsys);
733}
734
735////////////////////////////////////////////////////////////////////////////////
736//
737// Errors
738//
739////////////////////////////////////////////////////////////////////////////////
740
741OVFLogicError::OVFLogicError(const char *aFormat, ...)
742{
743 char *pszNewMsg;
744 va_list args;
745 va_start(args, aFormat);
746 RTStrAPrintfV(&pszNewMsg, aFormat, args);
747 setWhat(pszNewMsg);
748 RTStrFree(pszNewMsg);
749 va_end(args);
750}
Note: See TracBrowser for help on using the repository browser.

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