VirtualBox

source: vbox/trunk/src/VBox/Main/include/ovfreader.h@ 33334

Last change on this file since 33334 was 32565, checked in by vboxsync, 14 years ago

Runtime/Main: add xml/ovf parsing in memory

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.6 KB
Line 
1/* $Id: ovfreader.h 32565 2010-09-16 14:39:24Z vboxsync $ */
2/** @file
3 * OVF reader declarations.
4 *
5 * Depends only on IPRT, including the iprt::MiniString 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#ifndef ____H_OVFREADER
21#define ____H_OVFREADER
22
23#include "iprt/cpp/xml.h"
24#include <map>
25
26namespace ovf
27{
28
29////////////////////////////////////////////////////////////////////////////////
30//
31// Enumerations
32//
33////////////////////////////////////////////////////////////////////////////////
34
35enum CIMOSType_T
36{
37 CIMOSType_CIMOS_Unknown = 0,
38 CIMOSType_CIMOS_Other = 1,
39 CIMOSType_CIMOS_MACOS = 2,
40 CIMOSType_CIMOS_ATTUNIX = 3,
41 CIMOSType_CIMOS_DGUX = 4,
42 CIMOSType_CIMOS_DECNT = 5,
43 CIMOSType_CIMOS_Tru64UNIX = 6,
44 CIMOSType_CIMOS_OpenVMS = 7,
45 CIMOSType_CIMOS_HPUX = 8,
46 CIMOSType_CIMOS_AIX = 9,
47 CIMOSType_CIMOS_MVS = 10,
48 CIMOSType_CIMOS_OS400 = 11,
49 CIMOSType_CIMOS_OS2 = 12,
50 CIMOSType_CIMOS_JavaVM = 13,
51 CIMOSType_CIMOS_MSDOS = 14,
52 CIMOSType_CIMOS_WIN3x = 15,
53 CIMOSType_CIMOS_WIN95 = 16,
54 CIMOSType_CIMOS_WIN98 = 17,
55 CIMOSType_CIMOS_WINNT = 18,
56 CIMOSType_CIMOS_WINCE = 19,
57 CIMOSType_CIMOS_NCR3000 = 20,
58 CIMOSType_CIMOS_NetWare = 21,
59 CIMOSType_CIMOS_OSF = 22,
60 CIMOSType_CIMOS_DCOS = 23,
61 CIMOSType_CIMOS_ReliantUNIX = 24,
62 CIMOSType_CIMOS_SCOUnixWare = 25,
63 CIMOSType_CIMOS_SCOOpenServer = 26,
64 CIMOSType_CIMOS_Sequent = 27,
65 CIMOSType_CIMOS_IRIX = 28,
66 CIMOSType_CIMOS_Solaris = 29,
67 CIMOSType_CIMOS_SunOS = 30,
68 CIMOSType_CIMOS_U6000 = 31,
69 CIMOSType_CIMOS_ASERIES = 32,
70 CIMOSType_CIMOS_HPNonStopOS = 33,
71 CIMOSType_CIMOS_HPNonStopOSS = 34,
72 CIMOSType_CIMOS_BS2000 = 35,
73 CIMOSType_CIMOS_LINUX = 36,
74 CIMOSType_CIMOS_Lynx = 37,
75 CIMOSType_CIMOS_XENIX = 38,
76 CIMOSType_CIMOS_VM = 39,
77 CIMOSType_CIMOS_InteractiveUNIX = 40,
78 CIMOSType_CIMOS_BSDUNIX = 41,
79 CIMOSType_CIMOS_FreeBSD = 42,
80 CIMOSType_CIMOS_NetBSD = 43,
81 CIMOSType_CIMOS_GNUHurd = 44,
82 CIMOSType_CIMOS_OS9 = 45,
83 CIMOSType_CIMOS_MACHKernel = 46,
84 CIMOSType_CIMOS_Inferno = 47,
85 CIMOSType_CIMOS_QNX = 48,
86 CIMOSType_CIMOS_EPOC = 49,
87 CIMOSType_CIMOS_IxWorks = 50,
88 CIMOSType_CIMOS_VxWorks = 51,
89 CIMOSType_CIMOS_MiNT = 52,
90 CIMOSType_CIMOS_BeOS = 53,
91 CIMOSType_CIMOS_HPMPE = 54,
92 CIMOSType_CIMOS_NextStep = 55,
93 CIMOSType_CIMOS_PalmPilot = 56,
94 CIMOSType_CIMOS_Rhapsody = 57,
95 CIMOSType_CIMOS_Windows2000 = 58,
96 CIMOSType_CIMOS_Dedicated = 59,
97 CIMOSType_CIMOS_OS390 = 60,
98 CIMOSType_CIMOS_VSE = 61,
99 CIMOSType_CIMOS_TPF = 62,
100 CIMOSType_CIMOS_WindowsMe = 63,
101 CIMOSType_CIMOS_CalderaOpenUNIX = 64,
102 CIMOSType_CIMOS_OpenBSD = 65,
103 CIMOSType_CIMOS_NotApplicable = 66,
104 CIMOSType_CIMOS_WindowsXP = 67,
105 CIMOSType_CIMOS_zOS = 68,
106 CIMOSType_CIMOS_MicrosoftWindowsServer2003 = 69,
107 CIMOSType_CIMOS_MicrosoftWindowsServer2003_64 = 70,
108 CIMOSType_CIMOS_WindowsXP_64 = 71,
109 CIMOSType_CIMOS_WindowsXPEmbedded = 72,
110 CIMOSType_CIMOS_WindowsVista = 73,
111 CIMOSType_CIMOS_WindowsVista_64 = 74,
112 CIMOSType_CIMOS_WindowsEmbeddedforPointofService = 75,
113 CIMOSType_CIMOS_MicrosoftWindowsServer2008 = 76,
114 CIMOSType_CIMOS_MicrosoftWindowsServer2008_64 = 77,
115 CIMOSType_CIMOS_FreeBSD_64 = 78,
116 CIMOSType_CIMOS_RedHatEnterpriseLinux = 79,
117 CIMOSType_CIMOS_RedHatEnterpriseLinux_64 = 80,
118 CIMOSType_CIMOS_Solaris_64 = 81,
119 CIMOSType_CIMOS_SUSE = 82,
120 CIMOSType_CIMOS_SUSE_64 = 83,
121 CIMOSType_CIMOS_SLES = 84,
122 CIMOSType_CIMOS_SLES_64 = 85,
123 CIMOSType_CIMOS_NovellOES = 86,
124 CIMOSType_CIMOS_NovellLinuxDesktop = 87,
125 CIMOSType_CIMOS_SunJavaDesktopSystem = 88,
126 CIMOSType_CIMOS_Mandriva = 89,
127 CIMOSType_CIMOS_Mandriva_64 = 90,
128 CIMOSType_CIMOS_TurboLinux = 91,
129 CIMOSType_CIMOS_TurboLinux_64 = 92,
130 CIMOSType_CIMOS_Ubuntu = 93,
131 CIMOSType_CIMOS_Ubuntu_64 = 94,
132 CIMOSType_CIMOS_Debian = 95,
133 CIMOSType_CIMOS_Debian_64 = 96,
134 CIMOSType_CIMOS_Linux_2_4_x = 97,
135 CIMOSType_CIMOS_Linux_2_4_x_64 = 98,
136 CIMOSType_CIMOS_Linux_2_6_x = 99,
137 CIMOSType_CIMOS_Linux_2_6_x_64 = 100,
138 CIMOSType_CIMOS_Linux_64 = 101,
139 CIMOSType_CIMOS_Other_64 = 102,
140 // types added with CIM 2.25.0 follow:
141 CIMOSType_CIMOS_WindowsServer2008R2 = 103,
142 CIMOSType_CIMOS_VMwareESXi = 104,
143 CIMOSType_CIMOS_Windows7 = 105,
144 CIMOSType_CIMOS_CentOS = 106,
145 CIMOSType_CIMOS_CentOS_64 = 107,
146 CIMOSType_CIMOS_OracleEnterpriseLinux = 108,
147 CIMOSType_CIMOS_OracleEnterpriseLinux_64 = 109,
148 CIMOSType_CIMOS_eComStation = 110
149};
150
151
152////////////////////////////////////////////////////////////////////////////////
153//
154// Hardware definition structs
155//
156////////////////////////////////////////////////////////////////////////////////
157
158struct DiskImage
159{
160 // fields from /DiskSection/Disk
161 iprt::MiniString strDiskId; // value from DiskSection/Disk/@diskId
162 int64_t iCapacity; // value from DiskSection/Disk/@capacity;
163 // (maximum size for dynamic images, I guess; we always translate this to bytes)
164 int64_t iPopulatedSize; // optional value from DiskSection/Disk/@populatedSize
165 // (actual used size of disk, always in bytes; can be an estimate of used disk
166 // space, but cannot be larger than iCapacity; -1 if not set)
167 iprt::MiniString strFormat; // value from DiskSection/Disk/@format
168 // typically http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized
169 iprt::MiniString uuidVbox; // optional; if the file was exported by VirtualBox >= 3.2,
170 // then this has the UUID with which the disk was registered
171
172 // fields from /References/File; the spec says the file reference from disk can be empty,
173 // so in that case, strFilename will be empty, then a new disk should be created
174 iprt::MiniString strHref; // value from /References/File/@href (filename); if empty, then the remaining fields are ignored
175 int64_t iSize; // value from /References/File/@size (optional according to spec; then we set -1 here)
176 int64_t iChunkSize; // value from /References/File/@chunkSize (optional, unsupported)
177 iprt::MiniString strCompression; // value from /References/File/@compression (optional, can be "gzip" according to spec)
178
179 // additional field which has a descriptive size in megabytes derived from the above; this can be used for progress reports
180 uint32_t ulSuggestedSizeMB;
181};
182
183enum ResourceType_T
184{ ResourceType_Other = 1,
185 ResourceType_ComputerSystem = 2,
186 ResourceType_Processor = 3,
187 ResourceType_Memory = 4,
188 ResourceType_IDEController = 5,
189 ResourceType_ParallelSCSIHBA = 6,
190 ResourceType_FCHBA = 7,
191 ResourceType_iSCSIHBA = 8,
192 ResourceType_IBHCA = 9,
193 ResourceType_EthernetAdapter = 10,
194 ResourceType_OtherNetworkAdapter = 11,
195 ResourceType_IOSlot = 12,
196 ResourceType_IODevice = 13,
197 ResourceType_FloppyDrive = 14,
198 ResourceType_CDDrive = 15,
199 ResourceType_DVDDrive = 16,
200 ResourceType_HardDisk = 17,
201 ResourceType_OtherStorageDevice = 20,
202 ResourceType_USBController = 23,
203 ResourceType_SoundCard = 35
204};
205
206struct VirtualHardwareItem
207{
208 iprt::MiniString strDescription;
209 iprt::MiniString strCaption;
210 iprt::MiniString strElementName;
211
212 uint32_t ulInstanceID;
213 uint32_t ulParent;
214
215 ResourceType_T resourceType;
216 iprt::MiniString strOtherResourceType;
217 iprt::MiniString strResourceSubType;
218
219 iprt::MiniString strHostResource; // "Abstractly specifies how a device shall connect to a resource on the deployment platform.
220 // Not all devices need a backing." Used with disk items, for which this references a virtual
221 // disk from the Disks section.
222 bool fAutomaticAllocation;
223 bool fAutomaticDeallocation;
224 iprt::MiniString strConnection; // "All Ethernet adapters that specify the same abstract network connection name within an OVF
225 // package shall be deployed on the same network. The abstract network connection name shall be
226 // listed in the NetworkSection at the outermost envelope level." We ignore this and only set up
227 // a network adapter depending on the network name.
228 iprt::MiniString strAddress; // "Device-specific. For an Ethernet adapter, this specifies the MAC address."
229 int32_t lAddress; // strAddress as an integer, if applicable.
230 iprt::MiniString strAddressOnParent; // "For a device, this specifies its location on the controller."
231 iprt::MiniString strAllocationUnits; // "Specifies the units of allocation used. For example, “byte * 2^20”."
232 uint64_t ullVirtualQuantity; // "Specifies the quantity of resources presented. For example, “256”."
233 uint64_t ullReservation; // "Specifies the minimum quantity of resources guaranteed to be available."
234 uint64_t ullLimit; // "Specifies the maximum quantity of resources that will be granted."
235 uint64_t ullWeight; // "Specifies a relative priority for this allocation in relation to other allocations."
236
237 iprt::MiniString strConsumerVisibility;
238 iprt::MiniString strMappingBehavior;
239 iprt::MiniString strPoolID;
240 uint32_t ulBusNumber; // seen with IDE controllers, but not listed in OVF spec
241
242 uint32_t ulLineNumber; // line number of <Item> element in XML source; cached for error messages
243
244 VirtualHardwareItem()
245 : ulInstanceID(0),
246 fAutomaticAllocation(false),
247 fAutomaticDeallocation(false),
248 ullVirtualQuantity(0),
249 ullReservation(0),
250 ullLimit(0),
251 ullWeight(0),
252 ulBusNumber(0),
253 ulLineNumber(0)
254 {};
255};
256
257typedef std::map<iprt::MiniString, DiskImage> DiskImagesMap;
258
259struct VirtualSystem;
260
261typedef std::map<uint32_t, VirtualHardwareItem> HardwareItemsMap;
262
263struct HardDiskController
264{
265 uint32_t idController; // instance ID (Item/InstanceId); this gets referenced from VirtualDisk
266
267 enum ControllerSystemType { IDE, SATA, SCSI };
268 ControllerSystemType system; // one of IDE, SATA, SCSI
269
270 iprt::MiniString strControllerType;
271 // controller subtype (Item/ResourceSubType); e.g. "LsiLogic"; can be empty (esp. for IDE)
272 // note that we treat LsiLogicSAS as a SCSI controller (system == SCSI) even though VirtualBox
273 // treats it as a fourth class besides IDE, SATA, SCSI
274
275 int32_t lAddress; // value from OVF "Address" element
276 bool fPrimary; // controller index; this is determined heuristically by the OVF reader and will
277 // be true for the first controller of this type (e.g. IDE primary ctler) or
278 // false for the next (e.g. IDE secondary ctler)
279
280 HardDiskController()
281 : idController(0),
282 lAddress(0),
283 fPrimary(true)
284 { }
285};
286
287typedef std::map<uint32_t, HardDiskController> ControllersMap;
288
289struct VirtualDisk
290{
291 uint32_t idController; // SCSI (or IDE) controller this disk is connected to;
292 // this must match HardDiskController.idController and
293 // points into VirtualSystem.mapControllers
294 uint32_t ulAddressOnParent; // parsed strAddressOnParent of hardware item; will be 0 or 1 for IDE
295 // and possibly higher for disks attached to SCSI controllers (untested)
296 iprt::MiniString strDiskId; // if the hard disk has an ovf:/disk/<id> reference,
297 // this receives the <id> component; points to one of the
298 // references in Appliance::Data.mapDisks
299};
300
301typedef std::map<iprt::MiniString, VirtualDisk> VirtualDisksMap;
302
303/**
304 * A list of EthernetAdapters is contained in VirtualSystem, representing the
305 * ethernet adapters in the virtual system.
306 */
307struct EthernetAdapter
308{
309 iprt::MiniString strAdapterType; // "PCNet32" or "E1000" or whatever; from <rasd:ResourceSubType>
310 iprt::MiniString strNetworkName; // from <rasd:Connection>
311};
312
313typedef std::list<EthernetAdapter> EthernetAdaptersList;
314
315/**
316 * A list of VirtualSystem structs is created by OVFReader::read(). Each refers to
317 * a <VirtualSystem> block in the OVF file.
318 */
319struct VirtualSystem
320{
321 iprt::MiniString strName; // copy of VirtualSystem/@id
322
323 iprt::MiniString strDescription; // copy of VirtualSystem/AnnotationSection content, if any
324
325 CIMOSType_T cimos;
326 iprt::MiniString strCimosDesc; // readable description of the cimos type in the case of cimos = 0/1/102
327 iprt::MiniString strVirtualSystemType; // generic hardware description; OVF says this can be something like "vmx-4" or "xen";
328 // VMware Workstation 6.5 is "vmx-07"
329
330 HardwareItemsMap mapHardwareItems; // map of virtual hardware items, sorted by unique instance ID
331
332 uint64_t ullMemorySize; // always in bytes, copied from llHardwareItems; default = 0 (unspecified)
333 uint16_t cCPUs; // no. of CPUs, copied from llHardwareItems; default = 1
334
335 EthernetAdaptersList llEthernetAdapters; // (one for each VirtualSystem/Item[@ResourceType=10]element)
336
337 ControllersMap mapControllers;
338 // list of hard disk controllers
339 // (one for each VirtualSystem/Item[@ResourceType=6] element with accumulated data from children)
340
341 VirtualDisksMap mapVirtualDisks;
342 // (one for each VirtualSystem/Item[@ResourceType=17] element with accumulated data from children)
343
344 bool fHasFloppyDrive; // true if there's a floppy item in mapHardwareItems
345 bool fHasCdromDrive; // true if there's a CD-ROM item in mapHardwareItems; ISO images are not yet supported by OVFtool
346 bool fHasUsbController; // true if there's a USB controller item in mapHardwareItems
347
348 iprt::MiniString strSoundCardType; // if not empty, then the system wants a soundcard; this then specifies the hardware;
349 // VMware Workstation 6.5 uses "ensoniq1371" for example
350
351 iprt::MiniString strLicenseText; // license info if any; receives contents of VirtualSystem/EulaSection/License
352
353 iprt::MiniString strProduct; // product info if any; receives contents of VirtualSystem/ProductSection/Product
354 iprt::MiniString strVendor; // product info if any; receives contents of VirtualSystem/ProductSection/Vendor
355 iprt::MiniString strVersion; // product info if any; receives contents of VirtualSystem/ProductSection/Version
356 iprt::MiniString strProductUrl; // product info if any; receives contents of VirtualSystem/ProductSection/ProductUrl
357 iprt::MiniString strVendorUrl; // product info if any; receives contents of VirtualSystem/ProductSection/VendorUrl
358
359 const xml::ElementNode // pointer to <vbox:Machine> element under <VirtualSystem> element or NULL if not present
360 *pelmVboxMachine;
361
362 VirtualSystem()
363 : ullMemorySize(0),
364 cCPUs(1),
365 fHasFloppyDrive(false),
366 fHasCdromDrive(false),
367 fHasUsbController(false),
368 pelmVboxMachine(NULL)
369 {
370 }
371};
372
373////////////////////////////////////////////////////////////////////////////////
374//
375// Class OVFReader
376//
377////////////////////////////////////////////////////////////////////////////////
378
379/**
380 * OVFReader attempts to open, read in and parse an OVF XML file. This is all done
381 * in the constructor; if there is any kind of error in the file -- filesystem error
382 * from IPRT, XML parsing errors from libxml, or OVF logical errors --, exceptions
383 * are thrown. These are all based on xml::Error.
384 *
385 * Hence, use this class as follows:
386<code>
387 OVFReader *pReader = NULL;
388 try
389 {
390 pReader = new("/path/to/file.ovf");
391 }
392 catch (xml::Error &e)
393 {
394 printf("A terrible thing happened: %s", e.what());
395 }
396 // now go look at pReader->m_llVirtualSystem and what's in there
397 if (pReader)
398 delete pReader;
399</code>
400 */
401
402class OVFReader
403{
404public:
405 OVFReader(const void *pvBuf, int cbSize, const iprt::MiniString &path);
406 OVFReader(const iprt::MiniString &path);
407
408 // Data fields
409 iprt::MiniString m_strPath; // file name given to constructor
410 DiskImagesMap m_mapDisks; // map of DiskImage structs, sorted by DiskImage.strDiskId
411 std::list<VirtualSystem> m_llVirtualSystems; // list of virtual systems, created by and valid after read()
412
413private:
414 xml::Document m_doc;
415
416 void parse();
417 void LoopThruSections(const xml::ElementNode *pReferencesElem, const xml::ElementNode *pCurElem);
418 void HandleDiskSection(const xml::ElementNode *pReferencesElem, const xml::ElementNode *pSectionElem);
419 void HandleNetworkSection(const xml::ElementNode *pSectionElem);
420 void HandleVirtualSystemContent(const xml::ElementNode *pContentElem);
421};
422
423////////////////////////////////////////////////////////////////////////////////
424//
425// Errors
426//
427////////////////////////////////////////////////////////////////////////////////
428
429/**
430 * Thrown by OVFReader for any kind of error that is not an XML error but
431 * still makes the OVF impossible to parse. Based on xml::LogicError so
432 * that one catch() for all xml::LogicError can handle all possible errors.
433 */
434
435class OVFLogicError : public xml::LogicError
436{
437public:
438 OVFLogicError(const char *aFormat, ...);
439};
440
441} // end namespace ovf
442
443#endif // ____H_OVFREADER
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