VirtualBox

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

Last change on this file since 36236 was 35566, checked in by vboxsync, 14 years ago

Main/OVF: optimization

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

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