VirtualBox

source: vbox/trunk/src/VBox/Main/xml/Settings.cpp@ 28098

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

Main: don't export snapshots in OVF, separate OVF import from vbox:Machine import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 161.4 KB
Line 
1/** @file
2 * Settings File Manipulation API.
3 *
4 * Two classes, MainConfigFile and MachineConfigFile, represent the VirtualBox.xml and
5 * machine XML files. They share a common ancestor class, ConfigFileBase, which shares
6 * functionality such as talking to the XML back-end classes and settings version management.
7 *
8 * The code can read all VirtualBox settings files version 1.3 and higher. That version was
9 * written by VirtualBox 2.0. It can write settings version 1.7 (used by VirtualBox 2.2 and
10 * 3.0) and 1.9 (used by VirtualBox 3.1).
11 *
12 * Rules for introducing new settings: If an element or attribute is introduced that was not
13 * present before VirtualBox 3.1, then settings version checks need to be introduced. The
14 * settings version for VirtualBox 3.1 is 1.9; see the SettingsVersion enumeration in
15 * src/VBox/Main/idl/VirtualBox.xidl for details about which version was used when.
16 *
17 * The settings versions checks are necessary because VirtualBox 3.1 no longer automatically
18 * converts XML settings files but only if necessary, that is, if settings are present that
19 * the old format does not support. If we write an element or attribute to a settings file
20 * of an older version, then an old VirtualBox (before 3.1) will attempt to validate it
21 * with XML schema, and that will certainly fail.
22 *
23 * So, to introduce a new setting:
24 *
25 * 1) Make sure the constructor of corresponding settings structure has a proper default.
26 *
27 * 2) In the settings reader method, try to read the setting; if it's there, great, if not,
28 * the default value will have been set by the constructor.
29 *
30 * 3) In the settings writer method, write the setting _only_ if the current settings
31 * version (stored in m->sv) is high enough. That is, for VirtualBox 3.2, write it
32 * only if (m->sv >= SettingsVersion_v1_10).
33 *
34 * 4) In MachineConfigFile::bumpSettingsVersionIfNeeded(), check if the new setting has
35 * a non-default value (i.e. that differs from the constructor). If so, bump the
36 * settings version to the current version so the settings writer (3) can write out
37 * the non-default value properly.
38 *
39 * So far a corresponding method for MainConfigFile has not been necessary since there
40 * have been no incompatible changes yet.
41 */
42
43/*
44 * Copyright (C) 2007-2010 Sun Microsystems, Inc.
45 *
46 * This file is part of VirtualBox Open Source Edition (OSE), as
47 * available from http://www.virtualbox.org. This file is free software;
48 * you can redistribute it and/or modify it under the terms of the GNU
49 * General Public License (GPL) as published by the Free Software
50 * Foundation, in version 2 as it comes in the "COPYING" file of the
51 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
52 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
53 *
54 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
55 * Clara, CA 95054 USA or visit http://www.sun.com if you need
56 * additional information or have any questions.
57 */
58
59#include "VBox/com/string.h"
60#include "VBox/settings.h"
61#include <iprt/cpp/xml.h>
62#include <iprt/stream.h>
63#include <iprt/ctype.h>
64#include <iprt/file.h>
65
66// generated header
67#include "SchemaDefs.h"
68
69#include "Logging.h"
70
71using namespace com;
72using namespace settings;
73
74////////////////////////////////////////////////////////////////////////////////
75//
76// Defines
77//
78////////////////////////////////////////////////////////////////////////////////
79
80/** VirtualBox XML settings namespace */
81#define VBOX_XML_NAMESPACE "http://www.innotek.de/VirtualBox-settings"
82
83/** VirtualBox XML settings version number substring ("x.y") */
84#define VBOX_XML_VERSION "1.10"
85
86/** VirtualBox XML settings version platform substring */
87#if defined (RT_OS_DARWIN)
88# define VBOX_XML_PLATFORM "macosx"
89#elif defined (RT_OS_FREEBSD)
90# define VBOX_XML_PLATFORM "freebsd"
91#elif defined (RT_OS_LINUX)
92# define VBOX_XML_PLATFORM "linux"
93#elif defined (RT_OS_NETBSD)
94# define VBOX_XML_PLATFORM "netbsd"
95#elif defined (RT_OS_OPENBSD)
96# define VBOX_XML_PLATFORM "openbsd"
97#elif defined (RT_OS_OS2)
98# define VBOX_XML_PLATFORM "os2"
99#elif defined (RT_OS_SOLARIS)
100# define VBOX_XML_PLATFORM "solaris"
101#elif defined (RT_OS_WINDOWS)
102# define VBOX_XML_PLATFORM "windows"
103#else
104# error Unsupported platform!
105#endif
106
107/** VirtualBox XML settings full version string ("x.y-platform") */
108#define VBOX_XML_VERSION_FULL VBOX_XML_VERSION "-" VBOX_XML_PLATFORM
109
110////////////////////////////////////////////////////////////////////////////////
111//
112// Internal data
113//
114////////////////////////////////////////////////////////////////////////////////
115
116/**
117 * Opaque data structore for ConfigFileBase (only declared
118 * in header, defined only here).
119 */
120
121struct ConfigFileBase::Data
122{
123 Data()
124 : pParser(NULL),
125 pDoc(NULL),
126 pelmRoot(NULL),
127 sv(SettingsVersion_Null),
128 svRead(SettingsVersion_Null)
129 {}
130
131 ~Data()
132 {
133 cleanup();
134 }
135
136 iprt::MiniString strFilename;
137 bool fFileExists;
138
139 xml::XmlFileParser *pParser;
140 xml::Document *pDoc;
141 xml::ElementNode *pelmRoot;
142
143 com::Utf8Str strSettingsVersionFull; // e.g. "1.7-linux"
144 SettingsVersion_T sv; // e.g. SettingsVersion_v1_7
145
146 SettingsVersion_T svRead; // settings version that the original file had when it was read,
147 // or SettingsVersion_Null if none
148
149 void copyFrom(const Data &d)
150 {
151 strFilename = d.strFilename;
152 fFileExists = d.fFileExists;
153 strSettingsVersionFull = d.strSettingsVersionFull;
154 sv = d.sv;
155 svRead = d.svRead;
156 }
157
158 void cleanup()
159 {
160 if (pDoc)
161 {
162 delete pDoc;
163 pDoc = NULL;
164 pelmRoot = NULL;
165 }
166
167 if (pParser)
168 {
169 delete pParser;
170 pParser = NULL;
171 }
172 }
173};
174
175/**
176 * Private exception class (not in the header file) that makes
177 * throwing xml::LogicError instances easier. That class is public
178 * and should be caught by client code.
179 */
180class settings::ConfigFileError : public xml::LogicError
181{
182public:
183 ConfigFileError(const ConfigFileBase *file,
184 const xml::Node *pNode,
185 const char *pcszFormat, ...)
186 : xml::LogicError()
187 {
188 va_list args;
189 va_start(args, pcszFormat);
190 Utf8StrFmtVA strWhat(pcszFormat, args);
191 va_end(args);
192
193 Utf8Str strLine;
194 if (pNode)
195 strLine = Utf8StrFmt(" (line %RU32)", pNode->getLineNumber());
196
197 const char *pcsz = strLine.c_str();
198 Utf8StrFmt str(N_("Error in %s%s -- %s"),
199 file->m->strFilename.c_str(),
200 (pcsz) ? pcsz : "",
201 strWhat.c_str());
202
203 setWhat(str.c_str());
204 }
205};
206
207////////////////////////////////////////////////////////////////////////////////
208//
209// ConfigFileBase
210//
211////////////////////////////////////////////////////////////////////////////////
212
213/**
214 * Constructor. Allocates the XML internals.
215 * @param strFilename
216 */
217ConfigFileBase::ConfigFileBase(const com::Utf8Str *pstrFilename)
218 : m(new Data)
219{
220 Utf8Str strMajor;
221 Utf8Str strMinor;
222
223 m->fFileExists = false;
224
225 if (pstrFilename)
226 {
227 // reading existing settings file:
228 m->strFilename = *pstrFilename;
229
230 m->pParser = new xml::XmlFileParser;
231 m->pDoc = new xml::Document;
232 m->pParser->read(*pstrFilename,
233 *m->pDoc);
234
235 m->fFileExists = true;
236
237 m->pelmRoot = m->pDoc->getRootElement();
238 if (!m->pelmRoot || !m->pelmRoot->nameEquals("VirtualBox"))
239 throw ConfigFileError(this, NULL, N_("Root element in VirtualBox settings files must be \"VirtualBox\"."));
240
241 if (!(m->pelmRoot->getAttributeValue("version", m->strSettingsVersionFull)))
242 throw ConfigFileError(this, m->pelmRoot, N_("Required VirtualBox/@version attribute is missing"));
243
244 LogRel(("Loading settings file \"%s\" with version \"%s\"\n", m->strFilename.c_str(), m->strSettingsVersionFull.c_str()));
245
246 // parse settings version; allow future versions but fail if file is older than 1.6
247 m->sv = SettingsVersion_Null;
248 if (m->strSettingsVersionFull.length() > 3)
249 {
250 const char *pcsz = m->strSettingsVersionFull.c_str();
251 char c;
252
253 while ( (c = *pcsz)
254 && RT_C_IS_DIGIT(c)
255 )
256 {
257 strMajor.append(c);
258 ++pcsz;
259 }
260
261 if (*pcsz++ == '.')
262 {
263 while ( (c = *pcsz)
264 && RT_C_IS_DIGIT(c)
265 )
266 {
267 strMinor.append(c);
268 ++pcsz;
269 }
270 }
271
272 uint32_t ulMajor = RTStrToUInt32(strMajor.c_str());
273 uint32_t ulMinor = RTStrToUInt32(strMinor.c_str());
274
275 if (ulMajor == 1)
276 {
277 if (ulMinor == 3)
278 m->sv = SettingsVersion_v1_3;
279 else if (ulMinor == 4)
280 m->sv = SettingsVersion_v1_4;
281 else if (ulMinor == 5)
282 m->sv = SettingsVersion_v1_5;
283 else if (ulMinor == 6)
284 m->sv = SettingsVersion_v1_6;
285 else if (ulMinor == 7)
286 m->sv = SettingsVersion_v1_7;
287 else if (ulMinor == 8)
288 m->sv = SettingsVersion_v1_8;
289 else if (ulMinor == 9)
290 m->sv = SettingsVersion_v1_9;
291 else if (ulMinor == 10)
292 m->sv = SettingsVersion_v1_10;
293 else if (ulMinor > 10)
294 m->sv = SettingsVersion_Future;
295 }
296 else if (ulMajor > 1)
297 m->sv = SettingsVersion_Future;
298
299 LogRel(("Parsed settings version %d.%d to enum value %d\n", ulMajor, ulMinor, m->sv));
300 }
301
302 if (m->sv == SettingsVersion_Null)
303 throw ConfigFileError(this, m->pelmRoot, N_("Cannot handle settings version '%s'"), m->strSettingsVersionFull.c_str());
304
305 // remember the settings version we read in case it gets upgraded later,
306 // so we know when to make backups
307 m->svRead = m->sv;
308 }
309 else
310 {
311 // creating new settings file:
312 m->strSettingsVersionFull = VBOX_XML_VERSION_FULL;
313 m->sv = SettingsVersion_v1_10;
314 }
315}
316
317/**
318 * Clean up.
319 */
320ConfigFileBase::~ConfigFileBase()
321{
322 if (m)
323 {
324 delete m;
325 m = NULL;
326 }
327}
328
329/**
330 * Helper function that parses a UUID in string form into
331 * a com::Guid item. Since that uses an IPRT function which
332 * does not accept "{}" characters around the UUID string,
333 * we handle that here. Throws on errors.
334 * @param guid
335 * @param strUUID
336 */
337void ConfigFileBase::parseUUID(Guid &guid,
338 const Utf8Str &strUUID) const
339{
340 // {5f102a55-a51b-48e3-b45a-b28d33469488}
341 // 01234567890123456789012345678901234567
342 // 1 2 3
343 if ( (strUUID[0] == '{')
344 && (strUUID[37] == '}')
345 )
346 guid = strUUID.substr(1, 36).c_str();
347 else
348 guid = strUUID.c_str();
349
350 if (guid.isEmpty())
351 throw ConfigFileError(this, NULL, N_("UUID \"%s\" has invalid format"), strUUID.c_str());
352}
353
354/**
355 * Parses the given string in str and attempts to treat it as an ISO
356 * date/time stamp to put into timestamp. Throws on errors.
357 * @param timestamp
358 * @param str
359 */
360void ConfigFileBase::parseTimestamp(RTTIMESPEC &timestamp,
361 const com::Utf8Str &str) const
362{
363 const char *pcsz = str.c_str();
364 // yyyy-mm-ddThh:mm:ss
365 // "2009-07-10T11:54:03Z"
366 // 01234567890123456789
367 // 1
368 if (str.length() > 19)
369 {
370 // timezone must either be unspecified or 'Z' for UTC
371 if ( (pcsz[19])
372 && (pcsz[19] != 'Z')
373 )
374 throw ConfigFileError(this, NULL, N_("Cannot handle ISO timestamp '%s': is not UTC date"), str.c_str());
375
376 int32_t yyyy;
377 uint32_t mm, dd, hh, min, secs;
378 if ( (pcsz[4] == '-')
379 && (pcsz[7] == '-')
380 && (pcsz[10] == 'T')
381 && (pcsz[13] == ':')
382 && (pcsz[16] == ':')
383 )
384 {
385 int rc;
386 if ( (RT_SUCCESS(rc = RTStrToInt32Ex(pcsz, NULL, 0, &yyyy)))
387 // could theoretically be negative but let's assume that nobody
388 // created virtual machines before the Christian era
389 && (RT_SUCCESS(rc = RTStrToUInt32Ex(pcsz + 5, NULL, 0, &mm)))
390 && (RT_SUCCESS(rc = RTStrToUInt32Ex(pcsz + 8, NULL, 0, &dd)))
391 && (RT_SUCCESS(rc = RTStrToUInt32Ex(pcsz + 11, NULL, 0, &hh)))
392 && (RT_SUCCESS(rc = RTStrToUInt32Ex(pcsz + 14, NULL, 0, &min)))
393 && (RT_SUCCESS(rc = RTStrToUInt32Ex(pcsz + 17, NULL, 0, &secs)))
394 )
395 {
396 RTTIME time =
397 {
398 yyyy,
399 (uint8_t)mm,
400 0,
401 0,
402 (uint8_t)dd,
403 (uint8_t)hh,
404 (uint8_t)min,
405 (uint8_t)secs,
406 0,
407 RTTIME_FLAGS_TYPE_UTC,
408 0
409 };
410 if (RTTimeNormalize(&time))
411 if (RTTimeImplode(&timestamp, &time))
412 return;
413 }
414
415 throw ConfigFileError(this, NULL, N_("Cannot parse ISO timestamp '%s': runtime error, %Rra"), str.c_str(), rc);
416 }
417
418 throw ConfigFileError(this, NULL, N_("Cannot parse ISO timestamp '%s': invalid format"), str.c_str());
419 }
420}
421
422/**
423 * Helper to create a string for a RTTIMESPEC for writing out ISO timestamps.
424 * @param stamp
425 * @return
426 */
427com::Utf8Str ConfigFileBase::makeString(const RTTIMESPEC &stamp)
428{
429 RTTIME time;
430 if (!RTTimeExplode(&time, &stamp))
431 throw ConfigFileError(this, NULL, N_("Timespec %lld ms is invalid"), RTTimeSpecGetMilli(&stamp));
432
433 return Utf8StrFmt("%04ld-%02hd-%02hdT%02hd:%02hd:%02hdZ",
434 time.i32Year,
435 (uint16_t)time.u8Month,
436 (uint16_t)time.u8MonthDay,
437 (uint16_t)time.u8Hour,
438 (uint16_t)time.u8Minute,
439 (uint16_t)time.u8Second);
440}
441
442/**
443 * Helper to create a string for a GUID.
444 * @param guid
445 * @return
446 */
447com::Utf8Str ConfigFileBase::makeString(const Guid &guid)
448{
449 Utf8Str str("{");
450 str.append(guid.toString());
451 str.append("}");
452 return str;
453}
454
455/**
456 * Helper method to read in an ExtraData subtree and stores its contents
457 * in the given map of extradata items. Used for both main and machine
458 * extradata (MainConfigFile and MachineConfigFile).
459 * @param elmExtraData
460 * @param map
461 */
462void ConfigFileBase::readExtraData(const xml::ElementNode &elmExtraData,
463 ExtraDataItemsMap &map)
464{
465 xml::NodesLoop nlLevel4(elmExtraData);
466 const xml::ElementNode *pelmExtraDataItem;
467 while ((pelmExtraDataItem = nlLevel4.forAllNodes()))
468 {
469 if (pelmExtraDataItem->nameEquals("ExtraDataItem"))
470 {
471 // <ExtraDataItem name="GUI/LastWindowPostion" value="97,88,981,858"/>
472 Utf8Str strName, strValue;
473 if ( ((pelmExtraDataItem->getAttributeValue("name", strName)))
474 && ((pelmExtraDataItem->getAttributeValue("value", strValue)))
475 )
476 map[strName] = strValue;
477 else
478 throw ConfigFileError(this, pelmExtraDataItem, N_("Required ExtraDataItem/@name or @value attribute is missing"));
479 }
480 }
481}
482
483/**
484 * Reads <USBDeviceFilter> entries from under the given elmDeviceFilters node and
485 * stores them in the given linklist. This is in ConfigFileBase because it's used
486 * from both MainConfigFile (for host filters) and MachineConfigFile (for machine
487 * filters).
488 * @param elmDeviceFilters
489 * @param ll
490 */
491void ConfigFileBase::readUSBDeviceFilters(const xml::ElementNode &elmDeviceFilters,
492 USBDeviceFiltersList &ll)
493{
494 xml::NodesLoop nl1(elmDeviceFilters, "DeviceFilter");
495 const xml::ElementNode *pelmLevel4Child;
496 while ((pelmLevel4Child = nl1.forAllNodes()))
497 {
498 USBDeviceFilter flt;
499 flt.action = USBDeviceFilterAction_Ignore;
500 Utf8Str strAction;
501 if ( (pelmLevel4Child->getAttributeValue("name", flt.strName))
502 && (pelmLevel4Child->getAttributeValue("active", flt.fActive))
503 )
504 {
505 if (!pelmLevel4Child->getAttributeValue("vendorId", flt.strVendorId))
506 pelmLevel4Child->getAttributeValue("vendorid", flt.strVendorId); // used before 1.3
507 if (!pelmLevel4Child->getAttributeValue("productId", flt.strProductId))
508 pelmLevel4Child->getAttributeValue("productid", flt.strProductId); // used before 1.3
509 pelmLevel4Child->getAttributeValue("revision", flt.strRevision);
510 pelmLevel4Child->getAttributeValue("manufacturer", flt.strManufacturer);
511 pelmLevel4Child->getAttributeValue("product", flt.strProduct);
512 if (!pelmLevel4Child->getAttributeValue("serialNumber", flt.strSerialNumber))
513 pelmLevel4Child->getAttributeValue("serialnumber", flt.strSerialNumber); // used before 1.3
514 pelmLevel4Child->getAttributeValue("port", flt.strPort);
515
516 // the next 2 are irrelevant for host USB objects
517 pelmLevel4Child->getAttributeValue("remote", flt.strRemote);
518 pelmLevel4Child->getAttributeValue("maskedInterfaces", flt.ulMaskedInterfaces);
519
520 // action is only used with host USB objects
521 if (pelmLevel4Child->getAttributeValue("action", strAction))
522 {
523 if (strAction == "Ignore")
524 flt.action = USBDeviceFilterAction_Ignore;
525 else if (strAction == "Hold")
526 flt.action = USBDeviceFilterAction_Hold;
527 else
528 throw ConfigFileError(this, pelmLevel4Child, N_("Invalid value '%s' in DeviceFilter/@action attribute"), strAction.c_str());
529 }
530
531 ll.push_back(flt);
532 }
533 }
534}
535
536/**
537 * Creates a new stub xml::Document in the m->pDoc member with the
538 * root "VirtualBox" element set up. This is used by both
539 * MainConfigFile and MachineConfigFile at the beginning of writing
540 * out their XML.
541 *
542 * Before calling this, it is the responsibility of the caller to
543 * set the "sv" member to the required settings version that is to
544 * be written. For newly created files, the settings version will be
545 * the latest (1.9); for files read in from disk earlier, it will be
546 * the settings version indicated in the file. However, this method
547 * will silently make sure that the settings version is always
548 * at least 1.7 and change it if necessary, since there is no write
549 * support for earlier settings versions.
550 */
551void ConfigFileBase::createStubDocument()
552{
553 Assert(m->pDoc == NULL);
554 m->pDoc = new xml::Document;
555
556 m->pelmRoot = m->pDoc->createRootElement("VirtualBox");
557 m->pelmRoot->setAttribute("xmlns", VBOX_XML_NAMESPACE);
558
559 const char *pcszVersion = NULL;
560 switch (m->sv)
561 {
562 case SettingsVersion_v1_8:
563 pcszVersion = "1.8";
564 break;
565
566 case SettingsVersion_v1_9:
567 pcszVersion = "1.9";
568 break;
569
570 case SettingsVersion_v1_10:
571 case SettingsVersion_Future: // can be set if this code runs on XML files that were created by a future version of VBox;
572 // in that case, downgrade to current version when writing since we can't write future versions...
573 pcszVersion = "1.10";
574 m->sv = SettingsVersion_v1_10;
575 break;
576
577 default:
578 // silently upgrade if this is less than 1.7 because that's the oldest we can write
579 pcszVersion = "1.7";
580 m->sv = SettingsVersion_v1_7;
581 break;
582 }
583
584 m->pelmRoot->setAttribute("version", Utf8StrFmt("%s-%s",
585 pcszVersion,
586 VBOX_XML_PLATFORM)); // e.g. "linux"
587
588 // since this gets called before the XML document is actually written out
589 // do this, this is where we must check whether we're upgrading the settings
590 // version and need to make a backup, so the user can go back to an earlier
591 // VirtualBox version and recover his old settings files.
592 if ( (m->svRead != SettingsVersion_Null) // old file exists?
593 && (m->svRead < m->sv) // we're upgrading?
594 )
595 {
596 // compose new filename: strip off trailing ".xml"
597 Utf8Str strFilenameNew = m->strFilename.substr(0, m->strFilename.length() - 4);
598 // and append something likd "-1.3-linux.xml"
599 strFilenameNew.append("-");
600 strFilenameNew.append(m->strSettingsVersionFull); // e.g. "1.3-linux"
601 strFilenameNew.append(".xml");
602
603 RTFileMove(m->strFilename.c_str(),
604 strFilenameNew.c_str(),
605 0); // no RTFILEMOVE_FLAGS_REPLACE
606
607 // do this only once
608 m->svRead = SettingsVersion_Null;
609 }
610}
611
612/**
613 * Creates an <ExtraData> node under the given parent element with
614 * <ExtraDataItem> childern according to the contents of the given
615 * map.
616 * This is in ConfigFileBase because it's used in both MainConfigFile
617 * MachineConfigFile, which both can have extradata.
618 *
619 * @param elmParent
620 * @param me
621 */
622void ConfigFileBase::writeExtraData(xml::ElementNode &elmParent,
623 const ExtraDataItemsMap &me)
624{
625 if (me.size())
626 {
627 xml::ElementNode *pelmExtraData = elmParent.createChild("ExtraData");
628 for (ExtraDataItemsMap::const_iterator it = me.begin();
629 it != me.end();
630 ++it)
631 {
632 const Utf8Str &strName = it->first;
633 const Utf8Str &strValue = it->second;
634 xml::ElementNode *pelmThis = pelmExtraData->createChild("ExtraDataItem");
635 pelmThis->setAttribute("name", strName);
636 pelmThis->setAttribute("value", strValue);
637 }
638 }
639}
640
641/**
642 * Creates <DeviceFilter> nodes under the given parent element according to
643 * the contents of the given USBDeviceFiltersList. This is in ConfigFileBase
644 * because it's used in both MainConfigFile (for host filters) and
645 * MachineConfigFile (for machine filters).
646 *
647 * If fHostMode is true, this means that we're supposed to write filters
648 * for the IHost interface (respect "action", omit "strRemote" and
649 * "ulMaskedInterfaces" in struct USBDeviceFilter).
650 *
651 * @param elmParent
652 * @param ll
653 * @param fHostMode
654 */
655void ConfigFileBase::writeUSBDeviceFilters(xml::ElementNode &elmParent,
656 const USBDeviceFiltersList &ll,
657 bool fHostMode)
658{
659 for (USBDeviceFiltersList::const_iterator it = ll.begin();
660 it != ll.end();
661 ++it)
662 {
663 const USBDeviceFilter &flt = *it;
664 xml::ElementNode *pelmFilter = elmParent.createChild("DeviceFilter");
665 pelmFilter->setAttribute("name", flt.strName);
666 pelmFilter->setAttribute("active", flt.fActive);
667 if (flt.strVendorId.length())
668 pelmFilter->setAttribute("vendorId", flt.strVendorId);
669 if (flt.strProductId.length())
670 pelmFilter->setAttribute("productId", flt.strProductId);
671 if (flt.strRevision.length())
672 pelmFilter->setAttribute("revision", flt.strRevision);
673 if (flt.strManufacturer.length())
674 pelmFilter->setAttribute("manufacturer", flt.strManufacturer);
675 if (flt.strProduct.length())
676 pelmFilter->setAttribute("product", flt.strProduct);
677 if (flt.strSerialNumber.length())
678 pelmFilter->setAttribute("serialNumber", flt.strSerialNumber);
679 if (flt.strPort.length())
680 pelmFilter->setAttribute("port", flt.strPort);
681
682 if (fHostMode)
683 {
684 const char *pcsz =
685 (flt.action == USBDeviceFilterAction_Ignore) ? "Ignore"
686 : /*(flt.action == USBDeviceFilterAction_Hold) ?*/ "Hold";
687 pelmFilter->setAttribute("action", pcsz);
688 }
689 else
690 {
691 if (flt.strRemote.length())
692 pelmFilter->setAttribute("remote", flt.strRemote);
693 if (flt.ulMaskedInterfaces)
694 pelmFilter->setAttribute("maskedInterfaces", flt.ulMaskedInterfaces);
695 }
696 }
697}
698
699/**
700 * Cleans up memory allocated by the internal XML parser. To be called by
701 * descendant classes when they're done analyzing the DOM tree to discard it.
702 */
703void ConfigFileBase::clearDocument()
704{
705 m->cleanup();
706}
707
708/**
709 * Returns true only if the underlying config file exists on disk;
710 * either because the file has been loaded from disk, or it's been written
711 * to disk, or both.
712 * @return
713 */
714bool ConfigFileBase::fileExists()
715{
716 return m->fFileExists;
717}
718
719/**
720 * Copies the base variables from another instance. Used by Machine::saveSettings
721 * so that the settings version does not get lost when a copy of the Machine settings
722 * file is made to see if settings have actually changed.
723 * @param b
724 */
725void ConfigFileBase::copyBaseFrom(const ConfigFileBase &b)
726{
727 m->copyFrom(*b.m);
728}
729
730////////////////////////////////////////////////////////////////////////////////
731//
732// Structures shared between Machine XML and VirtualBox.xml
733//
734////////////////////////////////////////////////////////////////////////////////
735
736/**
737 * Comparison operator. This gets called from MachineConfigFile::operator==,
738 * which in turn gets called from Machine::saveSettings to figure out whether
739 * machine settings have really changed and thus need to be written out to disk.
740 */
741bool USBDeviceFilter::operator==(const USBDeviceFilter &u) const
742{
743 return ( (this == &u)
744 || ( (strName == u.strName)
745 && (fActive == u.fActive)
746 && (strVendorId == u.strVendorId)
747 && (strProductId == u.strProductId)
748 && (strRevision == u.strRevision)
749 && (strManufacturer == u.strManufacturer)
750 && (strProduct == u.strProduct)
751 && (strSerialNumber == u.strSerialNumber)
752 && (strPort == u.strPort)
753 && (action == u.action)
754 && (strRemote == u.strRemote)
755 && (ulMaskedInterfaces == u.ulMaskedInterfaces)
756 )
757 );
758}
759
760////////////////////////////////////////////////////////////////////////////////
761//
762// MainConfigFile
763//
764////////////////////////////////////////////////////////////////////////////////
765
766/**
767 * Reads one <MachineEntry> from the main VirtualBox.xml file.
768 * @param elmMachineRegistry
769 */
770void MainConfigFile::readMachineRegistry(const xml::ElementNode &elmMachineRegistry)
771{
772 // <MachineEntry uuid="{ xxx }" src=" xxx "/>
773 xml::NodesLoop nl1(elmMachineRegistry);
774 const xml::ElementNode *pelmChild1;
775 while ((pelmChild1 = nl1.forAllNodes()))
776 {
777 if (pelmChild1->nameEquals("MachineEntry"))
778 {
779 MachineRegistryEntry mre;
780 Utf8Str strUUID;
781 if ( ((pelmChild1->getAttributeValue("uuid", strUUID)))
782 && ((pelmChild1->getAttributeValue("src", mre.strSettingsFile)))
783 )
784 {
785 parseUUID(mre.uuid, strUUID);
786 llMachines.push_back(mre);
787 }
788 else
789 throw ConfigFileError(this, pelmChild1, N_("Required MachineEntry/@uuid or @src attribute is missing"));
790 }
791 }
792}
793
794/**
795 * Reads a media registry entry from the main VirtualBox.xml file.
796 *
797 * Whereas the current media registry code is fairly straightforward, it was quite a mess
798 * with settings format before 1.4 (VirtualBox 2.0 used settings format 1.3). The elements
799 * in the media registry were much more inconsistent, and different elements were used
800 * depending on the type of device and image.
801 *
802 * @param t
803 * @param elmMedium
804 * @param llMedia
805 */
806void MainConfigFile::readMedium(MediaType t,
807 const xml::ElementNode &elmMedium, // HardDisk node if root; if recursing,
808 // child HardDisk node or DiffHardDisk node for pre-1.4
809 MediaList &llMedia) // list to append medium to (root disk or child list)
810{
811 // <HardDisk uuid="{5471ecdb-1ddb-4012-a801-6d98e226868b}" location="/mnt/innotek-unix/vdis/Windows XP.vdi" format="VDI" type="Normal">
812 settings::Medium med;
813 Utf8Str strUUID;
814 if (!(elmMedium.getAttributeValue("uuid", strUUID)))
815 throw ConfigFileError(this, &elmMedium, N_("Required %s/@uuid attribute is missing"), elmMedium.getName());
816
817 parseUUID(med.uuid, strUUID);
818
819 bool fNeedsLocation = true;
820
821 if (t == HardDisk)
822 {
823 if (m->sv < SettingsVersion_v1_4)
824 {
825 // here the system is:
826 // <HardDisk uuid="{....}" type="normal">
827 // <VirtualDiskImage filePath="/path/to/xxx.vdi"/>
828 // </HardDisk>
829
830 fNeedsLocation = false;
831 bool fNeedsFilePath = true;
832 const xml::ElementNode *pelmImage;
833 if ((pelmImage = elmMedium.findChildElement("VirtualDiskImage")))
834 med.strFormat = "VDI";
835 else if ((pelmImage = elmMedium.findChildElement("VMDKImage")))
836 med.strFormat = "VMDK";
837 else if ((pelmImage = elmMedium.findChildElement("VHDImage")))
838 med.strFormat = "VHD";
839 else if ((pelmImage = elmMedium.findChildElement("ISCSIHardDisk")))
840 {
841 med.strFormat = "iSCSI";
842
843 fNeedsFilePath = false;
844 // location is special here: current settings specify an "iscsi://user@server:port/target/lun"
845 // string for the location and also have several disk properties for these, whereas this used
846 // to be hidden in several sub-elements before 1.4, so compose a location string and set up
847 // the properties:
848 med.strLocation = "iscsi://";
849 Utf8Str strUser, strServer, strPort, strTarget, strLun;
850 if (pelmImage->getAttributeValue("userName", strUser))
851 {
852 med.strLocation.append(strUser);
853 med.strLocation.append("@");
854 }
855 Utf8Str strServerAndPort;
856 if (pelmImage->getAttributeValue("server", strServer))
857 {
858 strServerAndPort = strServer;
859 }
860 if (pelmImage->getAttributeValue("port", strPort))
861 {
862 if (strServerAndPort.length())
863 strServerAndPort.append(":");
864 strServerAndPort.append(strPort);
865 }
866 med.strLocation.append(strServerAndPort);
867 if (pelmImage->getAttributeValue("target", strTarget))
868 {
869 med.strLocation.append("/");
870 med.strLocation.append(strTarget);
871 }
872 if (pelmImage->getAttributeValue("lun", strLun))
873 {
874 med.strLocation.append("/");
875 med.strLocation.append(strLun);
876 }
877
878 if (strServer.length() && strPort.length())
879 med.properties["TargetAddress"] = strServerAndPort;
880 if (strTarget.length())
881 med.properties["TargetName"] = strTarget;
882 if (strUser.length())
883 med.properties["InitiatorUsername"] = strUser;
884 Utf8Str strPassword;
885 if (pelmImage->getAttributeValue("password", strPassword))
886 med.properties["InitiatorSecret"] = strPassword;
887 if (strLun.length())
888 med.properties["LUN"] = strLun;
889 }
890 else if ((pelmImage = elmMedium.findChildElement("CustomHardDisk")))
891 {
892 fNeedsFilePath = false;
893 fNeedsLocation = true;
894 // also requires @format attribute, which will be queried below
895 }
896 else
897 throw ConfigFileError(this, &elmMedium, N_("Required %s/VirtualDiskImage element is missing"), elmMedium.getName());
898
899 if (fNeedsFilePath)
900 if (!(pelmImage->getAttributeValue("filePath", med.strLocation)))
901 throw ConfigFileError(this, &elmMedium, N_("Required %s/@filePath attribute is missing"), elmMedium.getName());
902 }
903
904 if (med.strFormat.isEmpty()) // not set with 1.4 format above, or 1.4 Custom format?
905 if (!(elmMedium.getAttributeValue("format", med.strFormat)))
906 throw ConfigFileError(this, &elmMedium, N_("Required %s/@format attribute is missing"), elmMedium.getName());
907
908 if (!(elmMedium.getAttributeValue("autoReset", med.fAutoReset)))
909 med.fAutoReset = false;
910
911 Utf8Str strType;
912 if ((elmMedium.getAttributeValue("type", strType)))
913 {
914 // pre-1.4 used lower case, so make this case-insensitive
915 strType.toUpper();
916 if (strType == "NORMAL")
917 med.hdType = MediumType_Normal;
918 else if (strType == "IMMUTABLE")
919 med.hdType = MediumType_Immutable;
920 else if (strType == "WRITETHROUGH")
921 med.hdType = MediumType_Writethrough;
922 else
923 throw ConfigFileError(this, &elmMedium, N_("HardDisk/@type attribute must be one of Normal, Immutable or Writethrough"));
924 }
925 }
926 else if (m->sv < SettingsVersion_v1_4)
927 {
928 // DVD and floppy images before 1.4 had "src" attribute instead of "location"
929 if (!(elmMedium.getAttributeValue("src", med.strLocation)))
930 throw ConfigFileError(this, &elmMedium, N_("Required %s/@src attribute is missing"), elmMedium.getName());
931
932 fNeedsLocation = false;
933 }
934
935 if (fNeedsLocation)
936 // current files and 1.4 CustomHardDisk elements must have a location attribute
937 if (!(elmMedium.getAttributeValue("location", med.strLocation)))
938 throw ConfigFileError(this, &elmMedium, N_("Required %s/@location attribute is missing"), elmMedium.getName());
939
940 elmMedium.getAttributeValue("Description", med.strDescription); // optional
941
942 // recurse to handle children
943 xml::NodesLoop nl2(elmMedium);
944 const xml::ElementNode *pelmHDChild;
945 while ((pelmHDChild = nl2.forAllNodes()))
946 {
947 if ( t == HardDisk
948 && ( pelmHDChild->nameEquals("HardDisk")
949 || ( (m->sv < SettingsVersion_v1_4)
950 && (pelmHDChild->nameEquals("DiffHardDisk"))
951 )
952 )
953 )
954 // recurse with this element and push the child onto our current children list
955 readMedium(t,
956 *pelmHDChild,
957 med.llChildren);
958 else if (pelmHDChild->nameEquals("Property"))
959 {
960 Utf8Str strPropName, strPropValue;
961 if ( (pelmHDChild->getAttributeValue("name", strPropName))
962 && (pelmHDChild->getAttributeValue("value", strPropValue))
963 )
964 med.properties[strPropName] = strPropValue;
965 else
966 throw ConfigFileError(this, pelmHDChild, N_("Required HardDisk/Property/@name or @value attribute is missing"));
967 }
968 }
969
970 llMedia.push_back(med);
971}
972
973/**
974 * Reads in the entire <MediaRegistry> chunk. For pre-1.4 files, this gets called
975 * with the <DiskRegistry> chunk instead.
976 * @param elmMediaRegistry
977 */
978void MainConfigFile::readMediaRegistry(const xml::ElementNode &elmMediaRegistry)
979{
980 xml::NodesLoop nl1(elmMediaRegistry);
981 const xml::ElementNode *pelmChild1;
982 while ((pelmChild1 = nl1.forAllNodes()))
983 {
984 MediaType t = Error;
985 if (pelmChild1->nameEquals("HardDisks"))
986 t = HardDisk;
987 else if (pelmChild1->nameEquals("DVDImages"))
988 t = DVDImage;
989 else if (pelmChild1->nameEquals("FloppyImages"))
990 t = FloppyImage;
991 else
992 continue;
993
994 xml::NodesLoop nl2(*pelmChild1);
995 const xml::ElementNode *pelmMedium;
996 while ((pelmMedium = nl2.forAllNodes()))
997 {
998 if ( t == HardDisk
999 && (pelmMedium->nameEquals("HardDisk"))
1000 )
1001 readMedium(t,
1002 *pelmMedium,
1003 llHardDisks); // list to append hard disk data to: the root list
1004 else if ( t == DVDImage
1005 && (pelmMedium->nameEquals("Image"))
1006 )
1007 readMedium(t,
1008 *pelmMedium,
1009 llDvdImages); // list to append dvd images to: the root list
1010 else if ( t == FloppyImage
1011 && (pelmMedium->nameEquals("Image"))
1012 )
1013 readMedium(t,
1014 *pelmMedium,
1015 llFloppyImages); // list to append floppy images to: the root list
1016 }
1017 }
1018}
1019
1020/**
1021 * Reads in the <DHCPServers> chunk.
1022 * @param elmDHCPServers
1023 */
1024void MainConfigFile::readDHCPServers(const xml::ElementNode &elmDHCPServers)
1025{
1026 xml::NodesLoop nl1(elmDHCPServers);
1027 const xml::ElementNode *pelmServer;
1028 while ((pelmServer = nl1.forAllNodes()))
1029 {
1030 if (pelmServer->nameEquals("DHCPServer"))
1031 {
1032 DHCPServer srv;
1033 if ( (pelmServer->getAttributeValue("networkName", srv.strNetworkName))
1034 && (pelmServer->getAttributeValue("IPAddress", srv.strIPAddress))
1035 && (pelmServer->getAttributeValue("networkMask", srv.strIPNetworkMask))
1036 && (pelmServer->getAttributeValue("lowerIP", srv.strIPLower))
1037 && (pelmServer->getAttributeValue("upperIP", srv.strIPUpper))
1038 && (pelmServer->getAttributeValue("enabled", srv.fEnabled))
1039 )
1040 llDhcpServers.push_back(srv);
1041 else
1042 throw ConfigFileError(this, pelmServer, N_("Required DHCPServer/@networkName, @IPAddress, @networkMask, @lowerIP, @upperIP or @enabled attribute is missing"));
1043 }
1044 }
1045}
1046
1047/**
1048 * Constructor.
1049 *
1050 * If pstrFilename is != NULL, this reads the given settings file into the member
1051 * variables and various substructures and lists. Otherwise, the member variables
1052 * are initialized with default values.
1053 *
1054 * Throws variants of xml::Error for I/O, XML and logical content errors, which
1055 * the caller should catch; if this constructor does not throw, then the member
1056 * variables contain meaningful values (either from the file or defaults).
1057 *
1058 * @param strFilename
1059 */
1060MainConfigFile::MainConfigFile(const Utf8Str *pstrFilename)
1061 : ConfigFileBase(pstrFilename)
1062{
1063 if (pstrFilename)
1064 {
1065 // the ConfigFileBase constructor has loaded the XML file, so now
1066 // we need only analyze what is in there
1067 xml::NodesLoop nlRootChildren(*m->pelmRoot);
1068 const xml::ElementNode *pelmRootChild;
1069 while ((pelmRootChild = nlRootChildren.forAllNodes()))
1070 {
1071 if (pelmRootChild->nameEquals("Global"))
1072 {
1073 xml::NodesLoop nlGlobalChildren(*pelmRootChild);
1074 const xml::ElementNode *pelmGlobalChild;
1075 while ((pelmGlobalChild = nlGlobalChildren.forAllNodes()))
1076 {
1077 if (pelmGlobalChild->nameEquals("SystemProperties"))
1078 {
1079 pelmGlobalChild->getAttributeValue("defaultMachineFolder", systemProperties.strDefaultMachineFolder);
1080 if (!pelmGlobalChild->getAttributeValue("defaultHardDiskFolder", systemProperties.strDefaultHardDiskFolder))
1081 // pre-1.4 used @defaultVDIFolder instead
1082 pelmGlobalChild->getAttributeValue("defaultVDIFolder", systemProperties.strDefaultHardDiskFolder);
1083 pelmGlobalChild->getAttributeValue("defaultHardDiskFormat", systemProperties.strDefaultHardDiskFormat);
1084 pelmGlobalChild->getAttributeValue("remoteDisplayAuthLibrary", systemProperties.strRemoteDisplayAuthLibrary);
1085 pelmGlobalChild->getAttributeValue("webServiceAuthLibrary", systemProperties.strWebServiceAuthLibrary);
1086 pelmGlobalChild->getAttributeValue("LogHistoryCount", systemProperties.ulLogHistoryCount);
1087 }
1088 else if (pelmGlobalChild->nameEquals("ExtraData"))
1089 readExtraData(*pelmGlobalChild, mapExtraDataItems);
1090 else if (pelmGlobalChild->nameEquals("MachineRegistry"))
1091 readMachineRegistry(*pelmGlobalChild);
1092 else if ( (pelmGlobalChild->nameEquals("MediaRegistry"))
1093 || ( (m->sv < SettingsVersion_v1_4)
1094 && (pelmGlobalChild->nameEquals("DiskRegistry"))
1095 )
1096 )
1097 readMediaRegistry(*pelmGlobalChild);
1098 else if (pelmGlobalChild->nameEquals("NetserviceRegistry"))
1099 {
1100 xml::NodesLoop nlLevel4(*pelmGlobalChild);
1101 const xml::ElementNode *pelmLevel4Child;
1102 while ((pelmLevel4Child = nlLevel4.forAllNodes()))
1103 {
1104 if (pelmLevel4Child->nameEquals("DHCPServers"))
1105 readDHCPServers(*pelmLevel4Child);
1106 }
1107 }
1108 else if (pelmGlobalChild->nameEquals("USBDeviceFilters"))
1109 readUSBDeviceFilters(*pelmGlobalChild, host.llUSBDeviceFilters);
1110 }
1111 } // end if (pelmRootChild->nameEquals("Global"))
1112 }
1113
1114 clearDocument();
1115 }
1116
1117 // DHCP servers were introduced with settings version 1.7; if we're loading
1118 // from an older version OR this is a fresh install, then add one DHCP server
1119 // with default settings
1120 if ( (!llDhcpServers.size())
1121 && ( (!pstrFilename) // empty VirtualBox.xml file
1122 || (m->sv < SettingsVersion_v1_7) // upgrading from before 1.7
1123 )
1124 )
1125 {
1126 DHCPServer srv;
1127 srv.strNetworkName =
1128#ifdef RT_OS_WINDOWS
1129 "HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter";
1130#else
1131 "HostInterfaceNetworking-vboxnet0";
1132#endif
1133 srv.strIPAddress = "192.168.56.100";
1134 srv.strIPNetworkMask = "255.255.255.0";
1135 srv.strIPLower = "192.168.56.101";
1136 srv.strIPUpper = "192.168.56.254";
1137 srv.fEnabled = true;
1138 llDhcpServers.push_back(srv);
1139 }
1140}
1141
1142/**
1143 * Creates a single <HardDisk> element for the given Medium structure
1144 * and recurses to write the child hard disks underneath. Called from
1145 * MainConfigFile::write().
1146 *
1147 * @param elmMedium
1148 * @param m
1149 * @param level
1150 */
1151void MainConfigFile::writeHardDisk(xml::ElementNode &elmMedium,
1152 const Medium &mdm,
1153 uint32_t level) // 0 for "root" call, incremented with each recursion
1154{
1155 xml::ElementNode *pelmHardDisk = elmMedium.createChild("HardDisk");
1156 pelmHardDisk->setAttribute("uuid", makeString(mdm.uuid));
1157 pelmHardDisk->setAttribute("location", mdm.strLocation);
1158 pelmHardDisk->setAttribute("format", mdm.strFormat);
1159 if (mdm.fAutoReset)
1160 pelmHardDisk->setAttribute("autoReset", mdm.fAutoReset);
1161 if (mdm.strDescription.length())
1162 pelmHardDisk->setAttribute("Description", mdm.strDescription);
1163
1164 for (PropertiesMap::const_iterator it = mdm.properties.begin();
1165 it != mdm.properties.end();
1166 ++it)
1167 {
1168 xml::ElementNode *pelmProp = pelmHardDisk->createChild("Property");
1169 pelmProp->setAttribute("name", it->first);
1170 pelmProp->setAttribute("value", it->second);
1171 }
1172
1173 // only for base hard disks, save the type
1174 if (level == 0)
1175 {
1176 const char *pcszType =
1177 mdm.hdType == MediumType_Normal ? "Normal" :
1178 mdm.hdType == MediumType_Immutable ? "Immutable" :
1179 /*mdm.hdType == MediumType_Writethrough ?*/ "Writethrough";
1180 pelmHardDisk->setAttribute("type", pcszType);
1181 }
1182
1183 for (MediaList::const_iterator it = mdm.llChildren.begin();
1184 it != mdm.llChildren.end();
1185 ++it)
1186 {
1187 // recurse for children
1188 writeHardDisk(*pelmHardDisk, // parent
1189 *it, // settings::Medium
1190 ++level); // recursion level
1191 }
1192}
1193
1194/**
1195 * Called from the IVirtualBox interface to write out VirtualBox.xml. This
1196 * builds an XML DOM tree and writes it out to disk.
1197 */
1198void MainConfigFile::write(const com::Utf8Str strFilename)
1199{
1200 m->strFilename = strFilename;
1201 createStubDocument();
1202
1203 xml::ElementNode *pelmGlobal = m->pelmRoot->createChild("Global");
1204
1205 writeExtraData(*pelmGlobal, mapExtraDataItems);
1206
1207 xml::ElementNode *pelmMachineRegistry = pelmGlobal->createChild("MachineRegistry");
1208 for (MachinesRegistry::const_iterator it = llMachines.begin();
1209 it != llMachines.end();
1210 ++it)
1211 {
1212 // <MachineEntry uuid="{5f102a55-a51b-48e3-b45a-b28d33469488}" src="/mnt/innotek-unix/vbox-machines/Windows 5.1 XP 1 (Office 2003)/Windows 5.1 XP 1 (Office 2003).xml"/>
1213 const MachineRegistryEntry &mre = *it;
1214 xml::ElementNode *pelmMachineEntry = pelmMachineRegistry->createChild("MachineEntry");
1215 pelmMachineEntry->setAttribute("uuid", makeString(mre.uuid));
1216 pelmMachineEntry->setAttribute("src", mre.strSettingsFile);
1217 }
1218
1219 xml::ElementNode *pelmMediaRegistry = pelmGlobal->createChild("MediaRegistry");
1220
1221 xml::ElementNode *pelmHardDisks = pelmMediaRegistry->createChild("HardDisks");
1222 for (MediaList::const_iterator it = llHardDisks.begin();
1223 it != llHardDisks.end();
1224 ++it)
1225 {
1226 writeHardDisk(*pelmHardDisks, *it, 0);
1227 }
1228
1229 xml::ElementNode *pelmDVDImages = pelmMediaRegistry->createChild("DVDImages");
1230 for (MediaList::const_iterator it = llDvdImages.begin();
1231 it != llDvdImages.end();
1232 ++it)
1233 {
1234 const Medium &mdm = *it;
1235 xml::ElementNode *pelmMedium = pelmDVDImages->createChild("Image");
1236 pelmMedium->setAttribute("uuid", makeString(mdm.uuid));
1237 pelmMedium->setAttribute("location", mdm.strLocation);
1238 if (mdm.strDescription.length())
1239 pelmMedium->setAttribute("Description", mdm.strDescription);
1240 }
1241
1242 xml::ElementNode *pelmFloppyImages = pelmMediaRegistry->createChild("FloppyImages");
1243 for (MediaList::const_iterator it = llFloppyImages.begin();
1244 it != llFloppyImages.end();
1245 ++it)
1246 {
1247 const Medium &mdm = *it;
1248 xml::ElementNode *pelmMedium = pelmFloppyImages->createChild("Image");
1249 pelmMedium->setAttribute("uuid", makeString(mdm.uuid));
1250 pelmMedium->setAttribute("location", mdm.strLocation);
1251 if (mdm.strDescription.length())
1252 pelmMedium->setAttribute("Description", mdm.strDescription);
1253 }
1254
1255 xml::ElementNode *pelmNetserviceRegistry = pelmGlobal->createChild("NetserviceRegistry");
1256 xml::ElementNode *pelmDHCPServers = pelmNetserviceRegistry->createChild("DHCPServers");
1257 for (DHCPServersList::const_iterator it = llDhcpServers.begin();
1258 it != llDhcpServers.end();
1259 ++it)
1260 {
1261 const DHCPServer &d = *it;
1262 xml::ElementNode *pelmThis = pelmDHCPServers->createChild("DHCPServer");
1263 pelmThis->setAttribute("networkName", d.strNetworkName);
1264 pelmThis->setAttribute("IPAddress", d.strIPAddress);
1265 pelmThis->setAttribute("networkMask", d.strIPNetworkMask);
1266 pelmThis->setAttribute("lowerIP", d.strIPLower);
1267 pelmThis->setAttribute("upperIP", d.strIPUpper);
1268 pelmThis->setAttribute("enabled", (d.fEnabled) ? 1 : 0); // too bad we chose 1 vs. 0 here
1269 }
1270
1271 xml::ElementNode *pelmSysProps = pelmGlobal->createChild("SystemProperties");
1272 if (systemProperties.strDefaultMachineFolder.length())
1273 pelmSysProps->setAttribute("defaultMachineFolder", systemProperties.strDefaultMachineFolder);
1274 if (systemProperties.strDefaultHardDiskFolder.length())
1275 pelmSysProps->setAttribute("defaultHardDiskFolder", systemProperties.strDefaultHardDiskFolder);
1276 if (systemProperties.strDefaultHardDiskFormat.length())
1277 pelmSysProps->setAttribute("defaultHardDiskFormat", systemProperties.strDefaultHardDiskFormat);
1278 if (systemProperties.strRemoteDisplayAuthLibrary.length())
1279 pelmSysProps->setAttribute("remoteDisplayAuthLibrary", systemProperties.strRemoteDisplayAuthLibrary);
1280 if (systemProperties.strWebServiceAuthLibrary.length())
1281 pelmSysProps->setAttribute("webServiceAuthLibrary", systemProperties.strWebServiceAuthLibrary);
1282 pelmSysProps->setAttribute("LogHistoryCount", systemProperties.ulLogHistoryCount);
1283
1284 writeUSBDeviceFilters(*pelmGlobal->createChild("USBDeviceFilters"),
1285 host.llUSBDeviceFilters,
1286 true); // fHostMode
1287
1288 // now go write the XML
1289 xml::XmlFileWriter writer(*m->pDoc);
1290 writer.write(m->strFilename.c_str());
1291
1292 m->fFileExists = true;
1293
1294 clearDocument();
1295}
1296
1297////////////////////////////////////////////////////////////////////////////////
1298//
1299// Machine XML structures
1300//
1301////////////////////////////////////////////////////////////////////////////////
1302
1303/**
1304 * Comparison operator. This gets called from MachineConfigFile::operator==,
1305 * which in turn gets called from Machine::saveSettings to figure out whether
1306 * machine settings have really changed and thus need to be written out to disk.
1307 */
1308bool VRDPSettings::operator==(const VRDPSettings& v) const
1309{
1310 return ( (this == &v)
1311 || ( (fEnabled == v.fEnabled)
1312 && (strPort == v.strPort)
1313 && (strNetAddress == v.strNetAddress)
1314 && (authType == v.authType)
1315 && (ulAuthTimeout == v.ulAuthTimeout)
1316 && (fAllowMultiConnection == v.fAllowMultiConnection)
1317 && (fReuseSingleConnection == v.fReuseSingleConnection)
1318 )
1319 );
1320}
1321
1322/**
1323 * Comparison operator. This gets called from MachineConfigFile::operator==,
1324 * which in turn gets called from Machine::saveSettings to figure out whether
1325 * machine settings have really changed and thus need to be written out to disk.
1326 */
1327bool BIOSSettings::operator==(const BIOSSettings &d) const
1328{
1329 return ( (this == &d)
1330 || ( fACPIEnabled == d.fACPIEnabled
1331 && fIOAPICEnabled == d.fIOAPICEnabled
1332 && fLogoFadeIn == d.fLogoFadeIn
1333 && fLogoFadeOut == d.fLogoFadeOut
1334 && ulLogoDisplayTime == d.ulLogoDisplayTime
1335 && strLogoImagePath == d.strLogoImagePath
1336 && biosBootMenuMode == d.biosBootMenuMode
1337 && fPXEDebugEnabled == d.fPXEDebugEnabled
1338 && llTimeOffset == d.llTimeOffset)
1339 );
1340}
1341
1342/**
1343 * Comparison operator. This gets called from MachineConfigFile::operator==,
1344 * which in turn gets called from Machine::saveSettings to figure out whether
1345 * machine settings have really changed and thus need to be written out to disk.
1346 */
1347bool USBController::operator==(const USBController &u) const
1348{
1349 return ( (this == &u)
1350 || ( (fEnabled == u.fEnabled)
1351 && (fEnabledEHCI == u.fEnabledEHCI)
1352 && (llDeviceFilters == u.llDeviceFilters)
1353 )
1354 );
1355}
1356
1357/**
1358 * Comparison operator. This gets called from MachineConfigFile::operator==,
1359 * which in turn gets called from Machine::saveSettings to figure out whether
1360 * machine settings have really changed and thus need to be written out to disk.
1361 */
1362bool NetworkAdapter::operator==(const NetworkAdapter &n) const
1363{
1364 return ( (this == &n)
1365 || ( (ulSlot == n.ulSlot)
1366 && (type == n.type)
1367 && (fEnabled == n.fEnabled)
1368 && (strMACAddress == n.strMACAddress)
1369 && (fCableConnected == n.fCableConnected)
1370 && (ulLineSpeed == n.ulLineSpeed)
1371 && (fTraceEnabled == n.fTraceEnabled)
1372 && (strTraceFile == n.strTraceFile)
1373 && (mode == n.mode)
1374 && (strName == n.strName)
1375 )
1376 );
1377}
1378
1379/**
1380 * Comparison operator. This gets called from MachineConfigFile::operator==,
1381 * which in turn gets called from Machine::saveSettings to figure out whether
1382 * machine settings have really changed and thus need to be written out to disk.
1383 */
1384bool SerialPort::operator==(const SerialPort &s) const
1385{
1386 return ( (this == &s)
1387 || ( (ulSlot == s.ulSlot)
1388 && (fEnabled == s.fEnabled)
1389 && (ulIOBase == s.ulIOBase)
1390 && (ulIRQ == s.ulIRQ)
1391 && (portMode == s.portMode)
1392 && (strPath == s.strPath)
1393 && (fServer == s.fServer)
1394 )
1395 );
1396}
1397
1398/**
1399 * Comparison operator. This gets called from MachineConfigFile::operator==,
1400 * which in turn gets called from Machine::saveSettings to figure out whether
1401 * machine settings have really changed and thus need to be written out to disk.
1402 */
1403bool ParallelPort::operator==(const ParallelPort &s) const
1404{
1405 return ( (this == &s)
1406 || ( (ulSlot == s.ulSlot)
1407 && (fEnabled == s.fEnabled)
1408 && (ulIOBase == s.ulIOBase)
1409 && (ulIRQ == s.ulIRQ)
1410 && (strPath == s.strPath)
1411 )
1412 );
1413}
1414
1415/**
1416 * Comparison operator. This gets called from MachineConfigFile::operator==,
1417 * which in turn gets called from Machine::saveSettings to figure out whether
1418 * machine settings have really changed and thus need to be written out to disk.
1419 */
1420bool SharedFolder::operator==(const SharedFolder &g) const
1421{
1422 return ( (this == &g)
1423 || ( (strName == g.strName)
1424 && (strHostPath == g.strHostPath)
1425 && (fWritable == g.fWritable)
1426 )
1427 );
1428}
1429
1430/**
1431 * Comparison operator. This gets called from MachineConfigFile::operator==,
1432 * which in turn gets called from Machine::saveSettings to figure out whether
1433 * machine settings have really changed and thus need to be written out to disk.
1434 */
1435bool GuestProperty::operator==(const GuestProperty &g) const
1436{
1437 return ( (this == &g)
1438 || ( (strName == g.strName)
1439 && (strValue == g.strValue)
1440 && (timestamp == g.timestamp)
1441 && (strFlags == g.strFlags)
1442 )
1443 );
1444}
1445
1446// use a define for the platform-dependent default value of
1447// hwvirt exclusivity, since we'll need to check that value
1448// in bumpSettingsVersionIfNeeded()
1449#if defined(RT_OS_DARWIN) || defined(RT_OS_WINDOWS)
1450 #define HWVIRTEXCLUSIVEDEFAULT false
1451#else
1452 #define HWVIRTEXCLUSIVEDEFAULT true
1453#endif
1454
1455/**
1456 * Hardware struct constructor.
1457 */
1458Hardware::Hardware()
1459 : strVersion("1"),
1460 fHardwareVirt(true),
1461 fHardwareVirtExclusive(HWVIRTEXCLUSIVEDEFAULT),
1462 fNestedPaging(true),
1463 fLargePages(false),
1464 fVPID(true),
1465 fSyntheticCpu(false),
1466 fPAE(false),
1467 cCPUs(1),
1468 fCpuHotPlug(false),
1469 fHpetEnabled(false),
1470 ulMemorySizeMB((uint32_t)-1),
1471 ulVRAMSizeMB(8),
1472 cMonitors(1),
1473 fAccelerate3D(false),
1474 fAccelerate2DVideo(false),
1475 firmwareType(FirmwareType_BIOS),
1476 pointingHidType(PointingHidType_PS2Mouse),
1477 keyboardHidType(KeyboardHidType_PS2Keyboard),
1478 clipboardMode(ClipboardMode_Bidirectional),
1479 ulMemoryBalloonSize(0)
1480{
1481 mapBootOrder[0] = DeviceType_Floppy;
1482 mapBootOrder[1] = DeviceType_DVD;
1483 mapBootOrder[2] = DeviceType_HardDisk;
1484
1485 /* The default value for PAE depends on the host:
1486 * - 64 bits host -> always true
1487 * - 32 bits host -> true for Windows & Darwin (masked off if the host cpu doesn't support it anyway)
1488 */
1489#if HC_ARCH_BITS == 64 || defined(RT_OS_WINDOWS) || defined(RT_OS_DARWIN)
1490 fPAE = true;
1491#endif
1492}
1493
1494/**
1495 * Comparison operator. This gets called from MachineConfigFile::operator==,
1496 * which in turn gets called from Machine::saveSettings to figure out whether
1497 * machine settings have really changed and thus need to be written out to disk.
1498 */
1499bool Hardware::operator==(const Hardware& h) const
1500{
1501 return ( (this == &h)
1502 || ( (strVersion == h.strVersion)
1503 && (uuid == h.uuid)
1504 && (fHardwareVirt == h.fHardwareVirt)
1505 && (fHardwareVirtExclusive == h.fHardwareVirtExclusive)
1506 && (fNestedPaging == h.fNestedPaging)
1507 && (fLargePages == h.fLargePages)
1508 && (fVPID == h.fVPID)
1509 && (fSyntheticCpu == h.fSyntheticCpu)
1510 && (fPAE == h.fPAE)
1511 && (cCPUs == h.cCPUs)
1512 && (fCpuHotPlug == h.fCpuHotPlug)
1513 && (fHpetEnabled == h.fHpetEnabled)
1514 && (llCpus == h.llCpus)
1515 && (llCpuIdLeafs == h.llCpuIdLeafs)
1516 && (ulMemorySizeMB == h.ulMemorySizeMB)
1517 && (mapBootOrder == h.mapBootOrder)
1518 && (ulVRAMSizeMB == h.ulVRAMSizeMB)
1519 && (cMonitors == h.cMonitors)
1520 && (fAccelerate3D == h.fAccelerate3D)
1521 && (fAccelerate2DVideo == h.fAccelerate2DVideo)
1522 && (firmwareType == h.firmwareType)
1523 && (pointingHidType == h.pointingHidType)
1524 && (keyboardHidType == h.keyboardHidType)
1525 && (vrdpSettings == h.vrdpSettings)
1526 && (biosSettings == h.biosSettings)
1527 && (usbController == h.usbController)
1528 && (llNetworkAdapters == h.llNetworkAdapters)
1529 && (llSerialPorts == h.llSerialPorts)
1530 && (llParallelPorts == h.llParallelPorts)
1531 && (audioAdapter == h.audioAdapter)
1532 && (llSharedFolders == h.llSharedFolders)
1533 && (clipboardMode == h.clipboardMode)
1534 && (ulMemoryBalloonSize == h.ulMemoryBalloonSize)
1535 && (llGuestProperties == h.llGuestProperties)
1536 && (strNotificationPatterns == h.strNotificationPatterns)
1537 )
1538 );
1539}
1540
1541/**
1542 * Comparison operator. This gets called from MachineConfigFile::operator==,
1543 * which in turn gets called from Machine::saveSettings to figure out whether
1544 * machine settings have really changed and thus need to be written out to disk.
1545 */
1546bool AttachedDevice::operator==(const AttachedDevice &a) const
1547{
1548 return ( (this == &a)
1549 || ( (deviceType == a.deviceType)
1550 && (fPassThrough == a.fPassThrough)
1551 && (lPort == a.lPort)
1552 && (lDevice == a.lDevice)
1553 && (uuid == a.uuid)
1554 && (strHostDriveSrc == a.strHostDriveSrc)
1555 )
1556 );
1557}
1558
1559/**
1560 * Comparison operator. This gets called from MachineConfigFile::operator==,
1561 * which in turn gets called from Machine::saveSettings to figure out whether
1562 * machine settings have really changed and thus need to be written out to disk.
1563 */
1564bool StorageController::operator==(const StorageController &s) const
1565{
1566 return ( (this == &s)
1567 || ( (strName == s.strName)
1568 && (storageBus == s.storageBus)
1569 && (controllerType == s.controllerType)
1570 && (ulPortCount == s.ulPortCount)
1571 && (ulInstance == s.ulInstance)
1572 && (lIDE0MasterEmulationPort == s.lIDE0MasterEmulationPort)
1573 && (lIDE0SlaveEmulationPort == s.lIDE0SlaveEmulationPort)
1574 && (lIDE1MasterEmulationPort == s.lIDE1MasterEmulationPort)
1575 && (lIDE1SlaveEmulationPort == s.lIDE1SlaveEmulationPort)
1576 && (llAttachedDevices == s.llAttachedDevices)
1577 )
1578 );
1579}
1580
1581/**
1582 * Comparison operator. This gets called from MachineConfigFile::operator==,
1583 * which in turn gets called from Machine::saveSettings to figure out whether
1584 * machine settings have really changed and thus need to be written out to disk.
1585 */
1586bool Storage::operator==(const Storage &s) const
1587{
1588 return ( (this == &s)
1589 || (llStorageControllers == s.llStorageControllers) // deep compare
1590 );
1591}
1592
1593/**
1594 * Comparison operator. This gets called from MachineConfigFile::operator==,
1595 * which in turn gets called from Machine::saveSettings to figure out whether
1596 * machine settings have really changed and thus need to be written out to disk.
1597 */
1598bool Snapshot::operator==(const Snapshot &s) const
1599{
1600 return ( (this == &s)
1601 || ( (uuid == s.uuid)
1602 && (strName == s.strName)
1603 && (strDescription == s.strDescription)
1604 && (RTTimeSpecIsEqual(&timestamp, &s.timestamp))
1605 && (strStateFile == s.strStateFile)
1606 && (hardware == s.hardware) // deep compare
1607 && (storage == s.storage) // deep compare
1608 && (llChildSnapshots == s.llChildSnapshots) // deep compare
1609 )
1610 );
1611}
1612
1613/**
1614 * IoSettings constructor.
1615 */
1616IoSettings::IoSettings()
1617{
1618 ioMgrType = IoMgrType_Async;
1619#if defined(RT_OS_LINUX)
1620 ioBackendType = IoBackendType_Unbuffered;
1621#else
1622 ioBackendType = IoBackendType_Buffered;
1623#endif
1624 fIoCacheEnabled = true;
1625 ulIoCacheSize = 5;
1626 ulIoBandwidthMax = 0;
1627};
1628
1629////////////////////////////////////////////////////////////////////////////////
1630//
1631// MachineConfigFile
1632//
1633////////////////////////////////////////////////////////////////////////////////
1634
1635/**
1636 * Constructor.
1637 *
1638 * If pstrFilename is != NULL, this reads the given settings file into the member
1639 * variables and various substructures and lists. Otherwise, the member variables
1640 * are initialized with default values.
1641 *
1642 * Throws variants of xml::Error for I/O, XML and logical content errors, which
1643 * the caller should catch; if this constructor does not throw, then the member
1644 * variables contain meaningful values (either from the file or defaults).
1645 *
1646 * @param strFilename
1647 */
1648MachineConfigFile::MachineConfigFile(const Utf8Str *pstrFilename)
1649 : ConfigFileBase(pstrFilename),
1650 fNameSync(true),
1651 fTeleporterEnabled(false),
1652 uTeleporterPort(0),
1653 fRTCUseUTC(false),
1654 fCurrentStateModified(true),
1655 fAborted(false)
1656{
1657 RTTimeNow(&timeLastStateChange);
1658
1659 if (pstrFilename)
1660 {
1661 // the ConfigFileBase constructor has loaded the XML file, so now
1662 // we need only analyze what is in there
1663
1664 xml::NodesLoop nlRootChildren(*m->pelmRoot);
1665 const xml::ElementNode *pelmRootChild;
1666 while ((pelmRootChild = nlRootChildren.forAllNodes()))
1667 {
1668 if (pelmRootChild->nameEquals("Machine"))
1669 readMachine(*pelmRootChild);
1670 }
1671
1672 // clean up memory allocated by XML engine
1673 clearDocument();
1674 }
1675}
1676
1677/**
1678 * Public routine which allows for importing machine XML from an external DOM tree.
1679 * Use this after having called the constructor with a NULL argument.
1680 *
1681 * This is used by the OVF code if a <vbox:Machine> element has been encountered
1682 * in an OVF VirtualSystem element.
1683 *
1684 * @param elmMachine
1685 */
1686void MachineConfigFile::importMachineXML(const xml::ElementNode &elmMachine)
1687{
1688 readMachine(elmMachine);
1689}
1690
1691/**
1692 * Comparison operator. This gets called from Machine::saveSettings to figure out
1693 * whether machine settings have really changed and thus need to be written out to disk.
1694 *
1695 * Even though this is called operator==, this does NOT compare all fields; the "equals"
1696 * should be understood as "has the same machine config as". The following fields are
1697 * NOT compared:
1698 * -- settings versions and file names inherited from ConfigFileBase;
1699 * -- fCurrentStateModified because that is considered separately in Machine::saveSettings!!
1700 *
1701 * The "deep" comparisons marked below will invoke the operator== functions of the
1702 * structs defined in this file, which may in turn go into comparing lists of
1703 * other structures. As a result, invoking this can be expensive, but it's
1704 * less expensive than writing out XML to disk.
1705 */
1706bool MachineConfigFile::operator==(const MachineConfigFile &c) const
1707{
1708 return ( (this == &c)
1709 || ( (uuid == c.uuid)
1710 && (strName == c.strName)
1711 && (fNameSync == c.fNameSync)
1712 && (strDescription == c.strDescription)
1713 && (strOsType == c.strOsType)
1714 && (strStateFile == c.strStateFile)
1715 && (uuidCurrentSnapshot == c.uuidCurrentSnapshot)
1716 && (strSnapshotFolder == c.strSnapshotFolder)
1717 && (fTeleporterEnabled == c.fTeleporterEnabled)
1718 && (uTeleporterPort == c.uTeleporterPort)
1719 && (strTeleporterAddress == c.strTeleporterAddress)
1720 && (strTeleporterPassword == c.strTeleporterPassword)
1721 && (fRTCUseUTC == c.fRTCUseUTC)
1722 // skip fCurrentStateModified!
1723 && (RTTimeSpecIsEqual(&timeLastStateChange, &c.timeLastStateChange))
1724 && (fAborted == c.fAborted)
1725 && (hardwareMachine == c.hardwareMachine) // this one's deep
1726 && (storageMachine == c.storageMachine) // this one's deep
1727 && (mapExtraDataItems == c.mapExtraDataItems) // this one's deep
1728 && (llFirstSnapshot == c.llFirstSnapshot) // this one's deep
1729 )
1730 );
1731}
1732
1733/**
1734 * Called from MachineConfigFile::readHardware() to read cpu information.
1735 * @param elmCpuid
1736 * @param ll
1737 */
1738void MachineConfigFile::readCpuTree(const xml::ElementNode &elmCpu,
1739 CpuList &ll)
1740{
1741 xml::NodesLoop nl1(elmCpu, "Cpu");
1742 const xml::ElementNode *pelmCpu;
1743 while ((pelmCpu = nl1.forAllNodes()))
1744 {
1745 Cpu cpu;
1746
1747 if (!pelmCpu->getAttributeValue("id", cpu.ulId))
1748 throw ConfigFileError(this, pelmCpu, N_("Required Cpu/@id attribute is missing"));
1749
1750 ll.push_back(cpu);
1751 }
1752}
1753
1754/**
1755 * Called from MachineConfigFile::readHardware() to cpuid information.
1756 * @param elmCpuid
1757 * @param ll
1758 */
1759void MachineConfigFile::readCpuIdTree(const xml::ElementNode &elmCpuid,
1760 CpuIdLeafsList &ll)
1761{
1762 xml::NodesLoop nl1(elmCpuid, "CpuIdLeaf");
1763 const xml::ElementNode *pelmCpuIdLeaf;
1764 while ((pelmCpuIdLeaf = nl1.forAllNodes()))
1765 {
1766 CpuIdLeaf leaf;
1767
1768 if (!pelmCpuIdLeaf->getAttributeValue("id", leaf.ulId))
1769 throw ConfigFileError(this, pelmCpuIdLeaf, N_("Required CpuId/@id attribute is missing"));
1770
1771 pelmCpuIdLeaf->getAttributeValue("eax", leaf.ulEax);
1772 pelmCpuIdLeaf->getAttributeValue("ebx", leaf.ulEbx);
1773 pelmCpuIdLeaf->getAttributeValue("ecx", leaf.ulEcx);
1774 pelmCpuIdLeaf->getAttributeValue("edx", leaf.ulEdx);
1775
1776 ll.push_back(leaf);
1777 }
1778}
1779
1780/**
1781 * Called from MachineConfigFile::readHardware() to network information.
1782 * @param elmNetwork
1783 * @param ll
1784 */
1785void MachineConfigFile::readNetworkAdapters(const xml::ElementNode &elmNetwork,
1786 NetworkAdaptersList &ll)
1787{
1788 xml::NodesLoop nl1(elmNetwork, "Adapter");
1789 const xml::ElementNode *pelmAdapter;
1790 while ((pelmAdapter = nl1.forAllNodes()))
1791 {
1792 NetworkAdapter nic;
1793
1794 if (!pelmAdapter->getAttributeValue("slot", nic.ulSlot))
1795 throw ConfigFileError(this, pelmAdapter, N_("Required Adapter/@slot attribute is missing"));
1796
1797 Utf8Str strTemp;
1798 if (pelmAdapter->getAttributeValue("type", strTemp))
1799 {
1800 if (strTemp == "Am79C970A")
1801 nic.type = NetworkAdapterType_Am79C970A;
1802 else if (strTemp == "Am79C973")
1803 nic.type = NetworkAdapterType_Am79C973;
1804 else if (strTemp == "82540EM")
1805 nic.type = NetworkAdapterType_I82540EM;
1806 else if (strTemp == "82543GC")
1807 nic.type = NetworkAdapterType_I82543GC;
1808 else if (strTemp == "82545EM")
1809 nic.type = NetworkAdapterType_I82545EM;
1810 else if (strTemp == "virtio")
1811 nic.type = NetworkAdapterType_Virtio;
1812 else
1813 throw ConfigFileError(this, pelmAdapter, N_("Invalid value '%s' in Adapter/@type attribute"), strTemp.c_str());
1814 }
1815
1816 pelmAdapter->getAttributeValue("enabled", nic.fEnabled);
1817 pelmAdapter->getAttributeValue("MACAddress", nic.strMACAddress);
1818 pelmAdapter->getAttributeValue("cable", nic.fCableConnected);
1819 pelmAdapter->getAttributeValue("speed", nic.ulLineSpeed);
1820 pelmAdapter->getAttributeValue("trace", nic.fTraceEnabled);
1821 pelmAdapter->getAttributeValue("tracefile", nic.strTraceFile);
1822
1823 const xml::ElementNode *pelmAdapterChild;
1824 if ((pelmAdapterChild = pelmAdapter->findChildElement("NAT")))
1825 {
1826 nic.mode = NetworkAttachmentType_NAT;
1827 pelmAdapterChild->getAttributeValue("network", nic.nat.strNetwork); // optional network name
1828 pelmAdapterChild->getAttributeValue("hostip", nic.nat.strBindIP);
1829 pelmAdapterChild->getAttributeValue("mtu", nic.nat.u32Mtu);
1830 pelmAdapterChild->getAttributeValue("sockrcv", nic.nat.u32SockRcv);
1831 pelmAdapterChild->getAttributeValue("socksnd", nic.nat.u32SockSnd);
1832 pelmAdapterChild->getAttributeValue("tcprcv", nic.nat.u32TcpRcv);
1833 pelmAdapterChild->getAttributeValue("tcpsnd", nic.nat.u32TcpSnd);
1834 const xml::ElementNode *pelmDNS;
1835 if ((pelmDNS = pelmAdapterChild->findChildElement("DNS")))
1836 {
1837 pelmDNS->getAttributeValue("pass-domain", nic.nat.fDnsPassDomain);
1838 pelmDNS->getAttributeValue("use-proxy", nic.nat.fDnsProxy);
1839 pelmDNS->getAttributeValue("use-host-resolver", nic.nat.fDnsUseHostResolver);
1840 }
1841 const xml::ElementNode *pelmTFTP;
1842 if ((pelmTFTP = pelmAdapterChild->findChildElement("TFTP")))
1843 {
1844 pelmTFTP->getAttributeValue("prefix", nic.nat.strTftpPrefix);
1845 pelmTFTP->getAttributeValue("boot-file", nic.nat.strTftpBootFile);
1846 pelmTFTP->getAttributeValue("next-server", nic.nat.strTftpNextServer);
1847 }
1848 xml::ElementNodesList plstNatPF;
1849 pelmAdapterChild->getChildElements(plstNatPF, "Forwarding");
1850 for(xml::ElementNodesList::iterator pf = plstNatPF.begin(); pf != plstNatPF.end(); ++pf)
1851 {
1852 NATRule rule;
1853 uint32_t port = 0;
1854 (*pf)->getAttributeValue("name", rule.strName);
1855 (*pf)->getAttributeValue("proto", rule.u32Proto);
1856 (*pf)->getAttributeValue("hostip", rule.strHostIP);
1857 (*pf)->getAttributeValue("hostport", port);
1858 rule.u16HostPort = port;
1859 (*pf)->getAttributeValue("guestip", rule.strGuestIP);
1860 (*pf)->getAttributeValue("guestport", port);
1861 rule.u16GuestPort = port;
1862 nic.nat.llRules.push_back(rule);
1863 }
1864 }
1865 else if ( ((pelmAdapterChild = pelmAdapter->findChildElement("HostInterface")))
1866 || ((pelmAdapterChild = pelmAdapter->findChildElement("BridgedInterface")))
1867 )
1868 {
1869 nic.mode = NetworkAttachmentType_Bridged;
1870 pelmAdapterChild->getAttributeValue("name", nic.strName); // optional host interface name
1871 }
1872 else if ((pelmAdapterChild = pelmAdapter->findChildElement("InternalNetwork")))
1873 {
1874 nic.mode = NetworkAttachmentType_Internal;
1875 if (!pelmAdapterChild->getAttributeValue("name", nic.strName)) // required network name
1876 throw ConfigFileError(this, pelmAdapterChild, N_("Required InternalNetwork/@name element is missing"));
1877 }
1878 else if ((pelmAdapterChild = pelmAdapter->findChildElement("HostOnlyInterface")))
1879 {
1880 nic.mode = NetworkAttachmentType_HostOnly;
1881 if (!pelmAdapterChild->getAttributeValue("name", nic.strName)) // required network name
1882 throw ConfigFileError(this, pelmAdapterChild, N_("Required HostOnlyInterface/@name element is missing"));
1883 }
1884 // else: default is NetworkAttachmentType_Null
1885
1886 ll.push_back(nic);
1887 }
1888}
1889
1890/**
1891 * Called from MachineConfigFile::readHardware() to read serial port information.
1892 * @param elmUART
1893 * @param ll
1894 */
1895void MachineConfigFile::readSerialPorts(const xml::ElementNode &elmUART,
1896 SerialPortsList &ll)
1897{
1898 xml::NodesLoop nl1(elmUART, "Port");
1899 const xml::ElementNode *pelmPort;
1900 while ((pelmPort = nl1.forAllNodes()))
1901 {
1902 SerialPort port;
1903 if (!pelmPort->getAttributeValue("slot", port.ulSlot))
1904 throw ConfigFileError(this, pelmPort, N_("Required UART/Port/@slot attribute is missing"));
1905
1906 // slot must be unique
1907 for (SerialPortsList::const_iterator it = ll.begin();
1908 it != ll.end();
1909 ++it)
1910 if ((*it).ulSlot == port.ulSlot)
1911 throw ConfigFileError(this, pelmPort, N_("Invalid value %RU32 in UART/Port/@slot attribute: value is not unique"), port.ulSlot);
1912
1913 if (!pelmPort->getAttributeValue("enabled", port.fEnabled))
1914 throw ConfigFileError(this, pelmPort, N_("Required UART/Port/@enabled attribute is missing"));
1915 if (!pelmPort->getAttributeValue("IOBase", port.ulIOBase))
1916 throw ConfigFileError(this, pelmPort, N_("Required UART/Port/@IOBase attribute is missing"));
1917 if (!pelmPort->getAttributeValue("IRQ", port.ulIRQ))
1918 throw ConfigFileError(this, pelmPort, N_("Required UART/Port/@IRQ attribute is missing"));
1919
1920 Utf8Str strPortMode;
1921 if (!pelmPort->getAttributeValue("hostMode", strPortMode))
1922 throw ConfigFileError(this, pelmPort, N_("Required UART/Port/@hostMode attribute is missing"));
1923 if (strPortMode == "RawFile")
1924 port.portMode = PortMode_RawFile;
1925 else if (strPortMode == "HostPipe")
1926 port.portMode = PortMode_HostPipe;
1927 else if (strPortMode == "HostDevice")
1928 port.portMode = PortMode_HostDevice;
1929 else if (strPortMode == "Disconnected")
1930 port.portMode = PortMode_Disconnected;
1931 else
1932 throw ConfigFileError(this, pelmPort, N_("Invalid value '%s' in UART/Port/@hostMode attribute"), strPortMode.c_str());
1933
1934 pelmPort->getAttributeValue("path", port.strPath);
1935 pelmPort->getAttributeValue("server", port.fServer);
1936
1937 ll.push_back(port);
1938 }
1939}
1940
1941/**
1942 * Called from MachineConfigFile::readHardware() to read parallel port information.
1943 * @param elmLPT
1944 * @param ll
1945 */
1946void MachineConfigFile::readParallelPorts(const xml::ElementNode &elmLPT,
1947 ParallelPortsList &ll)
1948{
1949 xml::NodesLoop nl1(elmLPT, "Port");
1950 const xml::ElementNode *pelmPort;
1951 while ((pelmPort = nl1.forAllNodes()))
1952 {
1953 ParallelPort port;
1954 if (!pelmPort->getAttributeValue("slot", port.ulSlot))
1955 throw ConfigFileError(this, pelmPort, N_("Required LPT/Port/@slot attribute is missing"));
1956
1957 // slot must be unique
1958 for (ParallelPortsList::const_iterator it = ll.begin();
1959 it != ll.end();
1960 ++it)
1961 if ((*it).ulSlot == port.ulSlot)
1962 throw ConfigFileError(this, pelmPort, N_("Invalid value %RU32 in LPT/Port/@slot attribute: value is not unique"), port.ulSlot);
1963
1964 if (!pelmPort->getAttributeValue("enabled", port.fEnabled))
1965 throw ConfigFileError(this, pelmPort, N_("Required LPT/Port/@enabled attribute is missing"));
1966 if (!pelmPort->getAttributeValue("IOBase", port.ulIOBase))
1967 throw ConfigFileError(this, pelmPort, N_("Required LPT/Port/@IOBase attribute is missing"));
1968 if (!pelmPort->getAttributeValue("IRQ", port.ulIRQ))
1969 throw ConfigFileError(this, pelmPort, N_("Required LPT/Port/@IRQ attribute is missing"));
1970
1971 pelmPort->getAttributeValue("path", port.strPath);
1972
1973 ll.push_back(port);
1974 }
1975}
1976
1977/**
1978 * Called from MachineConfigFile::readHardware() to read guest property information.
1979 * @param elmGuestProperties
1980 * @param hw
1981 */
1982void MachineConfigFile::readGuestProperties(const xml::ElementNode &elmGuestProperties,
1983 Hardware &hw)
1984{
1985 xml::NodesLoop nl1(elmGuestProperties, "GuestProperty");
1986 const xml::ElementNode *pelmProp;
1987 while ((pelmProp = nl1.forAllNodes()))
1988 {
1989 GuestProperty prop;
1990 pelmProp->getAttributeValue("name", prop.strName);
1991 pelmProp->getAttributeValue("value", prop.strValue);
1992
1993 pelmProp->getAttributeValue("timestamp", prop.timestamp);
1994 pelmProp->getAttributeValue("flags", prop.strFlags);
1995 hw.llGuestProperties.push_back(prop);
1996 }
1997
1998 elmGuestProperties.getAttributeValue("notificationPatterns", hw.strNotificationPatterns);
1999}
2000
2001/**
2002 * Helper function to read attributes that are common to <SATAController> (pre-1.7)
2003 * and <StorageController>.
2004 * @param elmStorageController
2005 * @param strg
2006 */
2007void MachineConfigFile::readStorageControllerAttributes(const xml::ElementNode &elmStorageController,
2008 StorageController &sctl)
2009{
2010 elmStorageController.getAttributeValue("PortCount", sctl.ulPortCount);
2011 elmStorageController.getAttributeValue("IDE0MasterEmulationPort", sctl.lIDE0MasterEmulationPort);
2012 elmStorageController.getAttributeValue("IDE0SlaveEmulationPort", sctl.lIDE0SlaveEmulationPort);
2013 elmStorageController.getAttributeValue("IDE1MasterEmulationPort", sctl.lIDE1MasterEmulationPort);
2014 elmStorageController.getAttributeValue("IDE1SlaveEmulationPort", sctl.lIDE1SlaveEmulationPort);
2015}
2016
2017/**
2018 * Reads in a <Hardware> block and stores it in the given structure. Used
2019 * both directly from readMachine and from readSnapshot, since snapshots
2020 * have their own hardware sections.
2021 *
2022 * For legacy pre-1.7 settings we also need a storage structure because
2023 * the IDE and SATA controllers used to be defined under <Hardware>.
2024 *
2025 * @param elmHardware
2026 * @param hw
2027 */
2028void MachineConfigFile::readHardware(const xml::ElementNode &elmHardware,
2029 Hardware &hw,
2030 Storage &strg)
2031{
2032 if (!elmHardware.getAttributeValue("version", hw.strVersion))
2033 {
2034 /* KLUDGE ALERT! For a while during the 3.1 development this was not
2035 written because it was thought to have a default value of "2". For
2036 sv <= 1.3 it defaults to "1" because the attribute didn't exist,
2037 while for 1.4+ it is sort of mandatory. Now, the buggy XML writer
2038 code only wrote 1.7 and later. So, if it's a 1.7+ XML file and it's
2039 missing the hardware version, then it probably should be "2" instead
2040 of "1". */
2041 if (m->sv < SettingsVersion_v1_7)
2042 hw.strVersion = "1";
2043 else
2044 hw.strVersion = "2";
2045 }
2046 Utf8Str strUUID;
2047 if (elmHardware.getAttributeValue("uuid", strUUID))
2048 parseUUID(hw.uuid, strUUID);
2049
2050 xml::NodesLoop nl1(elmHardware);
2051 const xml::ElementNode *pelmHwChild;
2052 while ((pelmHwChild = nl1.forAllNodes()))
2053 {
2054 if (pelmHwChild->nameEquals("CPU"))
2055 {
2056 if (!pelmHwChild->getAttributeValue("count", hw.cCPUs))
2057 {
2058 // pre-1.5 variant; not sure if this actually exists in the wild anywhere
2059 const xml::ElementNode *pelmCPUChild;
2060 if ((pelmCPUChild = pelmHwChild->findChildElement("CPUCount")))
2061 pelmCPUChild->getAttributeValue("count", hw.cCPUs);
2062 }
2063
2064 pelmHwChild->getAttributeValue("hotplug", hw.fCpuHotPlug);
2065
2066 const xml::ElementNode *pelmCPUChild;
2067 if (hw.fCpuHotPlug)
2068 {
2069 if ((pelmCPUChild = pelmHwChild->findChildElement("CpuTree")))
2070 readCpuTree(*pelmCPUChild, hw.llCpus);
2071 }
2072
2073 if ((pelmCPUChild = pelmHwChild->findChildElement("HardwareVirtEx")))
2074 {
2075 pelmCPUChild->getAttributeValue("enabled", hw.fHardwareVirt);
2076 pelmCPUChild->getAttributeValue("exclusive", hw.fHardwareVirtExclusive); // settings version 1.9
2077 }
2078 if ((pelmCPUChild = pelmHwChild->findChildElement("HardwareVirtExNestedPaging")))
2079 pelmCPUChild->getAttributeValue("enabled", hw.fNestedPaging);
2080 if ((pelmCPUChild = pelmHwChild->findChildElement("HardwareVirtExLargePages")))
2081 pelmCPUChild->getAttributeValue("enabled", hw.fLargePages);
2082 if ((pelmCPUChild = pelmHwChild->findChildElement("HardwareVirtExVPID")))
2083 pelmCPUChild->getAttributeValue("enabled", hw.fVPID);
2084
2085 if (!(pelmCPUChild = pelmHwChild->findChildElement("PAE")))
2086 {
2087 /* The default for pre 3.1 was false, so we must respect that. */
2088 if (m->sv < SettingsVersion_v1_9)
2089 hw.fPAE = false;
2090 }
2091 else
2092 pelmCPUChild->getAttributeValue("enabled", hw.fPAE);
2093
2094 if ((pelmCPUChild = pelmHwChild->findChildElement("SyntheticCpu")))
2095 pelmCPUChild->getAttributeValue("enabled", hw.fSyntheticCpu);
2096 if ((pelmCPUChild = pelmHwChild->findChildElement("CpuIdTree")))
2097 readCpuIdTree(*pelmCPUChild, hw.llCpuIdLeafs);
2098 }
2099 else if (pelmHwChild->nameEquals("Memory"))
2100 pelmHwChild->getAttributeValue("RAMSize", hw.ulMemorySizeMB);
2101 else if (pelmHwChild->nameEquals("Firmware"))
2102 {
2103 Utf8Str strFirmwareType;
2104 if (pelmHwChild->getAttributeValue("type", strFirmwareType))
2105 {
2106 if ( (strFirmwareType == "BIOS")
2107 || (strFirmwareType == "1") // some trunk builds used the number here
2108 )
2109 hw.firmwareType = FirmwareType_BIOS;
2110 else if ( (strFirmwareType == "EFI")
2111 || (strFirmwareType == "2") // some trunk builds used the number here
2112 )
2113 hw.firmwareType = FirmwareType_EFI;
2114 else if ( strFirmwareType == "EFI32")
2115 hw.firmwareType = FirmwareType_EFI32;
2116 else if ( strFirmwareType == "EFI64")
2117 hw.firmwareType = FirmwareType_EFI64;
2118 else if ( strFirmwareType == "EFIDUAL")
2119 hw.firmwareType = FirmwareType_EFIDUAL;
2120 else
2121 throw ConfigFileError(this,
2122 pelmHwChild,
2123 N_("Invalid value '%s' in Firmware/@type"),
2124 strFirmwareType.c_str());
2125 }
2126 }
2127 else if (pelmHwChild->nameEquals("HID"))
2128 {
2129 Utf8Str strHidType;
2130 if (pelmHwChild->getAttributeValue("Keyboard", strHidType))
2131 {
2132 if (strHidType == "None")
2133 hw.keyboardHidType = KeyboardHidType_None;
2134 else if (strHidType == "USBKeyboard")
2135 hw.keyboardHidType = KeyboardHidType_USBKeyboard;
2136 else if (strHidType == "PS2Keyboard")
2137 hw.keyboardHidType = KeyboardHidType_PS2Keyboard;
2138 else if (strHidType == "ComboKeyboard")
2139 hw.keyboardHidType = KeyboardHidType_ComboKeyboard;
2140 else
2141 throw ConfigFileError(this,
2142 pelmHwChild,
2143 N_("Invalid value '%s' in HID/Keyboard/@type"),
2144 strHidType.c_str());
2145 }
2146 if (pelmHwChild->getAttributeValue("Pointing", strHidType))
2147 {
2148 if (strHidType == "None")
2149 hw.pointingHidType = PointingHidType_None;
2150 else if (strHidType == "USBMouse")
2151 hw.pointingHidType = PointingHidType_USBMouse;
2152 else if (strHidType == "USBTablet")
2153 hw.pointingHidType = PointingHidType_USBTablet;
2154 else if (strHidType == "PS2Mouse")
2155 hw.pointingHidType = PointingHidType_PS2Mouse;
2156 else if (strHidType == "ComboMouse")
2157 hw.pointingHidType = PointingHidType_ComboMouse;
2158 else
2159 throw ConfigFileError(this,
2160 pelmHwChild,
2161 N_("Invalid value '%s' in HID/Pointing/@type"),
2162 strHidType.c_str());
2163 }
2164 }
2165 else if (pelmHwChild->nameEquals("HPET"))
2166 {
2167 pelmHwChild->getAttributeValue("enabled", hw.fHpetEnabled);
2168 }
2169 else if (pelmHwChild->nameEquals("Boot"))
2170 {
2171 hw.mapBootOrder.clear();
2172
2173 xml::NodesLoop nl2(*pelmHwChild, "Order");
2174 const xml::ElementNode *pelmOrder;
2175 while ((pelmOrder = nl2.forAllNodes()))
2176 {
2177 uint32_t ulPos;
2178 Utf8Str strDevice;
2179 if (!pelmOrder->getAttributeValue("position", ulPos))
2180 throw ConfigFileError(this, pelmOrder, N_("Required Boot/Order/@position attribute is missing"));
2181
2182 if ( ulPos < 1
2183 || ulPos > SchemaDefs::MaxBootPosition
2184 )
2185 throw ConfigFileError(this,
2186 pelmOrder,
2187 N_("Invalid value '%RU32' in Boot/Order/@position: must be greater than 0 and less than %RU32"),
2188 ulPos,
2189 SchemaDefs::MaxBootPosition + 1);
2190 // XML is 1-based but internal data is 0-based
2191 --ulPos;
2192
2193 if (hw.mapBootOrder.find(ulPos) != hw.mapBootOrder.end())
2194 throw ConfigFileError(this, pelmOrder, N_("Invalid value '%RU32' in Boot/Order/@position: value is not unique"), ulPos);
2195
2196 if (!pelmOrder->getAttributeValue("device", strDevice))
2197 throw ConfigFileError(this, pelmOrder, N_("Required Boot/Order/@device attribute is missing"));
2198
2199 DeviceType_T type;
2200 if (strDevice == "None")
2201 type = DeviceType_Null;
2202 else if (strDevice == "Floppy")
2203 type = DeviceType_Floppy;
2204 else if (strDevice == "DVD")
2205 type = DeviceType_DVD;
2206 else if (strDevice == "HardDisk")
2207 type = DeviceType_HardDisk;
2208 else if (strDevice == "Network")
2209 type = DeviceType_Network;
2210 else
2211 throw ConfigFileError(this, pelmOrder, N_("Invalid value '%s' in Boot/Order/@device attribute"), strDevice.c_str());
2212 hw.mapBootOrder[ulPos] = type;
2213 }
2214 }
2215 else if (pelmHwChild->nameEquals("Display"))
2216 {
2217 pelmHwChild->getAttributeValue("VRAMSize", hw.ulVRAMSizeMB);
2218 if (!pelmHwChild->getAttributeValue("monitorCount", hw.cMonitors))
2219 pelmHwChild->getAttributeValue("MonitorCount", hw.cMonitors); // pre-v1.5 variant
2220 if (!pelmHwChild->getAttributeValue("accelerate3D", hw.fAccelerate3D))
2221 pelmHwChild->getAttributeValue("Accelerate3D", hw.fAccelerate3D); // pre-v1.5 variant
2222 pelmHwChild->getAttributeValue("accelerate2DVideo", hw.fAccelerate2DVideo);
2223 }
2224 else if (pelmHwChild->nameEquals("RemoteDisplay"))
2225 {
2226 pelmHwChild->getAttributeValue("enabled", hw.vrdpSettings.fEnabled);
2227 pelmHwChild->getAttributeValue("port", hw.vrdpSettings.strPort);
2228 pelmHwChild->getAttributeValue("netAddress", hw.vrdpSettings.strNetAddress);
2229
2230 Utf8Str strAuthType;
2231 if (pelmHwChild->getAttributeValue("authType", strAuthType))
2232 {
2233 // settings before 1.3 used lower case so make sure this is case-insensitive
2234 strAuthType.toUpper();
2235 if (strAuthType == "NULL")
2236 hw.vrdpSettings.authType = VRDPAuthType_Null;
2237 else if (strAuthType == "GUEST")
2238 hw.vrdpSettings.authType = VRDPAuthType_Guest;
2239 else if (strAuthType == "EXTERNAL")
2240 hw.vrdpSettings.authType = VRDPAuthType_External;
2241 else
2242 throw ConfigFileError(this, pelmHwChild, N_("Invalid value '%s' in RemoteDisplay/@authType attribute"), strAuthType.c_str());
2243 }
2244
2245 pelmHwChild->getAttributeValue("authTimeout", hw.vrdpSettings.ulAuthTimeout);
2246 pelmHwChild->getAttributeValue("allowMultiConnection", hw.vrdpSettings.fAllowMultiConnection);
2247 pelmHwChild->getAttributeValue("reuseSingleConnection", hw.vrdpSettings.fReuseSingleConnection);
2248 }
2249 else if (pelmHwChild->nameEquals("BIOS"))
2250 {
2251 const xml::ElementNode *pelmBIOSChild;
2252 if ((pelmBIOSChild = pelmHwChild->findChildElement("ACPI")))
2253 pelmBIOSChild->getAttributeValue("enabled", hw.biosSettings.fACPIEnabled);
2254 if ((pelmBIOSChild = pelmHwChild->findChildElement("IOAPIC")))
2255 pelmBIOSChild->getAttributeValue("enabled", hw.biosSettings.fIOAPICEnabled);
2256 if ((pelmBIOSChild = pelmHwChild->findChildElement("Logo")))
2257 {
2258 pelmBIOSChild->getAttributeValue("fadeIn", hw.biosSettings.fLogoFadeIn);
2259 pelmBIOSChild->getAttributeValue("fadeOut", hw.biosSettings.fLogoFadeOut);
2260 pelmBIOSChild->getAttributeValue("displayTime", hw.biosSettings.ulLogoDisplayTime);
2261 pelmBIOSChild->getAttributeValue("imagePath", hw.biosSettings.strLogoImagePath);
2262 }
2263 if ((pelmBIOSChild = pelmHwChild->findChildElement("BootMenu")))
2264 {
2265 Utf8Str strBootMenuMode;
2266 if (pelmBIOSChild->getAttributeValue("mode", strBootMenuMode))
2267 {
2268 // settings before 1.3 used lower case so make sure this is case-insensitive
2269 strBootMenuMode.toUpper();
2270 if (strBootMenuMode == "DISABLED")
2271 hw.biosSettings.biosBootMenuMode = BIOSBootMenuMode_Disabled;
2272 else if (strBootMenuMode == "MENUONLY")
2273 hw.biosSettings.biosBootMenuMode = BIOSBootMenuMode_MenuOnly;
2274 else if (strBootMenuMode == "MESSAGEANDMENU")
2275 hw.biosSettings.biosBootMenuMode = BIOSBootMenuMode_MessageAndMenu;
2276 else
2277 throw ConfigFileError(this, pelmBIOSChild, N_("Invalid value '%s' in BootMenu/@mode attribute"), strBootMenuMode.c_str());
2278 }
2279 }
2280 if ((pelmBIOSChild = pelmHwChild->findChildElement("PXEDebug")))
2281 pelmBIOSChild->getAttributeValue("enabled", hw.biosSettings.fPXEDebugEnabled);
2282 if ((pelmBIOSChild = pelmHwChild->findChildElement("TimeOffset")))
2283 pelmBIOSChild->getAttributeValue("value", hw.biosSettings.llTimeOffset);
2284
2285 // legacy BIOS/IDEController (pre 1.7)
2286 if ( (m->sv < SettingsVersion_v1_7)
2287 && ((pelmBIOSChild = pelmHwChild->findChildElement("IDEController")))
2288 )
2289 {
2290 StorageController sctl;
2291 sctl.strName = "IDE Controller";
2292 sctl.storageBus = StorageBus_IDE;
2293
2294 Utf8Str strType;
2295 if (pelmBIOSChild->getAttributeValue("type", strType))
2296 {
2297 if (strType == "PIIX3")
2298 sctl.controllerType = StorageControllerType_PIIX3;
2299 else if (strType == "PIIX4")
2300 sctl.controllerType = StorageControllerType_PIIX4;
2301 else if (strType == "ICH6")
2302 sctl.controllerType = StorageControllerType_ICH6;
2303 else
2304 throw ConfigFileError(this, pelmBIOSChild, N_("Invalid value '%s' for IDEController/@type attribute"), strType.c_str());
2305 }
2306 sctl.ulPortCount = 2;
2307 strg.llStorageControllers.push_back(sctl);
2308 }
2309 }
2310 else if (pelmHwChild->nameEquals("USBController"))
2311 {
2312 pelmHwChild->getAttributeValue("enabled", hw.usbController.fEnabled);
2313 pelmHwChild->getAttributeValue("enabledEhci", hw.usbController.fEnabledEHCI);
2314
2315 readUSBDeviceFilters(*pelmHwChild,
2316 hw.usbController.llDeviceFilters);
2317 }
2318 else if ( (m->sv < SettingsVersion_v1_7)
2319 && (pelmHwChild->nameEquals("SATAController"))
2320 )
2321 {
2322 bool f;
2323 if ( (pelmHwChild->getAttributeValue("enabled", f))
2324 && (f)
2325 )
2326 {
2327 StorageController sctl;
2328 sctl.strName = "SATA Controller";
2329 sctl.storageBus = StorageBus_SATA;
2330 sctl.controllerType = StorageControllerType_IntelAhci;
2331
2332 readStorageControllerAttributes(*pelmHwChild, sctl);
2333
2334 strg.llStorageControllers.push_back(sctl);
2335 }
2336 }
2337 else if (pelmHwChild->nameEquals("Network"))
2338 readNetworkAdapters(*pelmHwChild, hw.llNetworkAdapters);
2339 else if (pelmHwChild->nameEquals("RTC"))
2340 {
2341 Utf8Str strLocalOrUTC;
2342 fRTCUseUTC = pelmHwChild->getAttributeValue("localOrUTC", strLocalOrUTC)
2343 && strLocalOrUTC == "UTC";
2344 }
2345 else if ( (pelmHwChild->nameEquals("UART"))
2346 || (pelmHwChild->nameEquals("Uart")) // used before 1.3
2347 )
2348 readSerialPorts(*pelmHwChild, hw.llSerialPorts);
2349 else if ( (pelmHwChild->nameEquals("LPT"))
2350 || (pelmHwChild->nameEquals("Lpt")) // used before 1.3
2351 )
2352 readParallelPorts(*pelmHwChild, hw.llParallelPorts);
2353 else if (pelmHwChild->nameEquals("AudioAdapter"))
2354 {
2355 pelmHwChild->getAttributeValue("enabled", hw.audioAdapter.fEnabled);
2356
2357 Utf8Str strTemp;
2358 if (pelmHwChild->getAttributeValue("controller", strTemp))
2359 {
2360 if (strTemp == "SB16")
2361 hw.audioAdapter.controllerType = AudioControllerType_SB16;
2362 else if (strTemp == "AC97")
2363 hw.audioAdapter.controllerType = AudioControllerType_AC97;
2364 else
2365 throw ConfigFileError(this, pelmHwChild, N_("Invalid value '%s' in AudioAdapter/@controller attribute"), strTemp.c_str());
2366 }
2367 if (pelmHwChild->getAttributeValue("driver", strTemp))
2368 {
2369 // settings before 1.3 used lower case so make sure this is case-insensitive
2370 strTemp.toUpper();
2371 if (strTemp == "NULL")
2372 hw.audioAdapter.driverType = AudioDriverType_Null;
2373 else if (strTemp == "WINMM")
2374 hw.audioAdapter.driverType = AudioDriverType_WinMM;
2375 else if ( (strTemp == "DIRECTSOUND") || (strTemp == "DSOUND") )
2376 hw.audioAdapter.driverType = AudioDriverType_DirectSound;
2377 else if (strTemp == "SOLAUDIO")
2378 hw.audioAdapter.driverType = AudioDriverType_SolAudio;
2379 else if (strTemp == "ALSA")
2380 hw.audioAdapter.driverType = AudioDriverType_ALSA;
2381 else if (strTemp == "PULSE")
2382 hw.audioAdapter.driverType = AudioDriverType_Pulse;
2383 else if (strTemp == "OSS")
2384 hw.audioAdapter.driverType = AudioDriverType_OSS;
2385 else if (strTemp == "COREAUDIO")
2386 hw.audioAdapter.driverType = AudioDriverType_CoreAudio;
2387 else if (strTemp == "MMPM")
2388 hw.audioAdapter.driverType = AudioDriverType_MMPM;
2389 else
2390 throw ConfigFileError(this, pelmHwChild, N_("Invalid value '%s' in AudioAdapter/@driver attribute"), strTemp.c_str());
2391 }
2392 }
2393 else if (pelmHwChild->nameEquals("SharedFolders"))
2394 {
2395 xml::NodesLoop nl2(*pelmHwChild, "SharedFolder");
2396 const xml::ElementNode *pelmFolder;
2397 while ((pelmFolder = nl2.forAllNodes()))
2398 {
2399 SharedFolder sf;
2400 pelmFolder->getAttributeValue("name", sf.strName);
2401 pelmFolder->getAttributeValue("hostPath", sf.strHostPath);
2402 pelmFolder->getAttributeValue("writable", sf.fWritable);
2403 hw.llSharedFolders.push_back(sf);
2404 }
2405 }
2406 else if (pelmHwChild->nameEquals("Clipboard"))
2407 {
2408 Utf8Str strTemp;
2409 if (pelmHwChild->getAttributeValue("mode", strTemp))
2410 {
2411 if (strTemp == "Disabled")
2412 hw.clipboardMode = ClipboardMode_Disabled;
2413 else if (strTemp == "HostToGuest")
2414 hw.clipboardMode = ClipboardMode_HostToGuest;
2415 else if (strTemp == "GuestToHost")
2416 hw.clipboardMode = ClipboardMode_GuestToHost;
2417 else if (strTemp == "Bidirectional")
2418 hw.clipboardMode = ClipboardMode_Bidirectional;
2419 else
2420 throw ConfigFileError(this, pelmHwChild, N_("Invalid value '%s' in Clipboard/@mode attribute"), strTemp.c_str());
2421 }
2422 }
2423 else if (pelmHwChild->nameEquals("Guest"))
2424 {
2425 if (!pelmHwChild->getAttributeValue("memoryBalloonSize", hw.ulMemoryBalloonSize))
2426 pelmHwChild->getAttributeValue("MemoryBalloonSize", hw.ulMemoryBalloonSize); // used before 1.3
2427 }
2428 else if (pelmHwChild->nameEquals("GuestProperties"))
2429 readGuestProperties(*pelmHwChild, hw);
2430 else if (pelmHwChild->nameEquals("IO"))
2431 {
2432 Utf8Str strTemp;
2433 const xml::ElementNode *pelmIoChild;
2434
2435 if ((pelmIoChild = pelmHwChild->findChildElement("IoMgr")))
2436 {
2437 if (pelmIoChild->getAttributeValue("type", strTemp))
2438 {
2439 if (strTemp == "Async")
2440 hw.ioSettings.ioMgrType = IoMgrType_Async;
2441 else if (strTemp == "Simple")
2442 hw.ioSettings.ioMgrType = IoMgrType_Simple;
2443 else
2444 throw ConfigFileError(this, pelmIoChild, N_("Invalid value '%s' in IoMgr/@type attribute"), strTemp.c_str());
2445 }
2446 }
2447
2448 if ((pelmIoChild = pelmHwChild->findChildElement("IoBackend")))
2449 {
2450 if (pelmIoChild->getAttributeValue("type", strTemp))
2451 {
2452 if (strTemp == "Unbuffered")
2453 hw.ioSettings.ioBackendType = IoBackendType_Unbuffered;
2454 else if (strTemp == "Buffered")
2455 hw.ioSettings.ioBackendType = IoBackendType_Buffered;
2456 else
2457 throw ConfigFileError(this, pelmIoChild, N_("Invalid value '%s' in IoBackend/@type attribute"), strTemp.c_str());
2458 }
2459 }
2460 if ((pelmIoChild = pelmHwChild->findChildElement("IoCache")))
2461 {
2462 pelmIoChild->getAttributeValue("enabled", hw.ioSettings.fIoCacheEnabled);
2463 pelmIoChild->getAttributeValue("size", hw.ioSettings.ulIoCacheSize);
2464 }
2465 if ((pelmIoChild = pelmHwChild->findChildElement("IoBandwidth")))
2466 {
2467 pelmIoChild->getAttributeValue("max", hw.ioSettings.ulIoBandwidthMax);
2468 }
2469 }
2470 }
2471
2472 if (hw.ulMemorySizeMB == (uint32_t)-1)
2473 throw ConfigFileError(this, &elmHardware, N_("Required Memory/@RAMSize element/attribute is missing"));
2474}
2475
2476/**
2477 * This gets called instead of readStorageControllers() for legacy pre-1.7 settings
2478 * files which have a <HardDiskAttachments> node and storage controller settings
2479 * hidden in the <Hardware> settings. We set the StorageControllers fields just the
2480 * same, just from different sources.
2481 * @param elmHardware <Hardware> XML node.
2482 * @param elmHardDiskAttachments <HardDiskAttachments> XML node.
2483 * @param strg
2484 */
2485void MachineConfigFile::readHardDiskAttachments_pre1_7(const xml::ElementNode &elmHardDiskAttachments,
2486 Storage &strg)
2487{
2488 StorageController *pIDEController = NULL;
2489 StorageController *pSATAController = NULL;
2490
2491 for (StorageControllersList::iterator it = strg.llStorageControllers.begin();
2492 it != strg.llStorageControllers.end();
2493 ++it)
2494 {
2495 StorageController &s = *it;
2496 if (s.storageBus == StorageBus_IDE)
2497 pIDEController = &s;
2498 else if (s.storageBus == StorageBus_SATA)
2499 pSATAController = &s;
2500 }
2501
2502 xml::NodesLoop nl1(elmHardDiskAttachments, "HardDiskAttachment");
2503 const xml::ElementNode *pelmAttachment;
2504 while ((pelmAttachment = nl1.forAllNodes()))
2505 {
2506 AttachedDevice att;
2507 Utf8Str strUUID, strBus;
2508
2509 if (!pelmAttachment->getAttributeValue("hardDisk", strUUID))
2510 throw ConfigFileError(this, pelmAttachment, N_("Required HardDiskAttachment/@hardDisk attribute is missing"));
2511 parseUUID(att.uuid, strUUID);
2512
2513 if (!pelmAttachment->getAttributeValue("bus", strBus))
2514 throw ConfigFileError(this, pelmAttachment, N_("Required HardDiskAttachment/@bus attribute is missing"));
2515 // pre-1.7 'channel' is now port
2516 if (!pelmAttachment->getAttributeValue("channel", att.lPort))
2517 throw ConfigFileError(this, pelmAttachment, N_("Required HardDiskAttachment/@channel attribute is missing"));
2518 // pre-1.7 'device' is still device
2519 if (!pelmAttachment->getAttributeValue("device", att.lDevice))
2520 throw ConfigFileError(this, pelmAttachment, N_("Required HardDiskAttachment/@device attribute is missing"));
2521
2522 att.deviceType = DeviceType_HardDisk;
2523
2524 if (strBus == "IDE")
2525 {
2526 if (!pIDEController)
2527 throw ConfigFileError(this, pelmAttachment, N_("HardDiskAttachment/@bus is 'IDE' but cannot find IDE controller"));
2528 pIDEController->llAttachedDevices.push_back(att);
2529 }
2530 else if (strBus == "SATA")
2531 {
2532 if (!pSATAController)
2533 throw ConfigFileError(this, pelmAttachment, N_("HardDiskAttachment/@bus is 'SATA' but cannot find SATA controller"));
2534 pSATAController->llAttachedDevices.push_back(att);
2535 }
2536 else
2537 throw ConfigFileError(this, pelmAttachment, N_("HardDiskAttachment/@bus attribute has illegal value '%s'"), strBus.c_str());
2538 }
2539}
2540
2541/**
2542 * Reads in a <StorageControllers> block and stores it in the given Storage structure.
2543 * Used both directly from readMachine and from readSnapshot, since snapshots
2544 * have their own storage controllers sections.
2545 *
2546 * This is only called for settings version 1.7 and above; see readHardDiskAttachments_pre1_7()
2547 * for earlier versions.
2548 *
2549 * @param elmStorageControllers
2550 */
2551void MachineConfigFile::readStorageControllers(const xml::ElementNode &elmStorageControllers,
2552 Storage &strg)
2553{
2554 xml::NodesLoop nlStorageControllers(elmStorageControllers, "StorageController");
2555 const xml::ElementNode *pelmController;
2556 while ((pelmController = nlStorageControllers.forAllNodes()))
2557 {
2558 StorageController sctl;
2559
2560 if (!pelmController->getAttributeValue("name", sctl.strName))
2561 throw ConfigFileError(this, pelmController, N_("Required StorageController/@name attribute is missing"));
2562 // canonicalize storage controller names for configs in the switchover
2563 // period.
2564 if (m->sv < SettingsVersion_v1_9)
2565 {
2566 if (sctl.strName == "IDE")
2567 sctl.strName = "IDE Controller";
2568 else if (sctl.strName == "SATA")
2569 sctl.strName = "SATA Controller";
2570 else if (sctl.strName == "SCSI")
2571 sctl.strName = "SCSI Controller";
2572 }
2573
2574 pelmController->getAttributeValue("Instance", sctl.ulInstance);
2575 // default from constructor is 0
2576
2577 Utf8Str strType;
2578 if (!pelmController->getAttributeValue("type", strType))
2579 throw ConfigFileError(this, pelmController, N_("Required StorageController/@type attribute is missing"));
2580
2581 if (strType == "AHCI")
2582 {
2583 sctl.storageBus = StorageBus_SATA;
2584 sctl.controllerType = StorageControllerType_IntelAhci;
2585 }
2586 else if (strType == "LsiLogic")
2587 {
2588 sctl.storageBus = StorageBus_SCSI;
2589 sctl.controllerType = StorageControllerType_LsiLogic;
2590 }
2591 else if (strType == "BusLogic")
2592 {
2593 sctl.storageBus = StorageBus_SCSI;
2594 sctl.controllerType = StorageControllerType_BusLogic;
2595 }
2596 else if (strType == "PIIX3")
2597 {
2598 sctl.storageBus = StorageBus_IDE;
2599 sctl.controllerType = StorageControllerType_PIIX3;
2600 }
2601 else if (strType == "PIIX4")
2602 {
2603 sctl.storageBus = StorageBus_IDE;
2604 sctl.controllerType = StorageControllerType_PIIX4;
2605 }
2606 else if (strType == "ICH6")
2607 {
2608 sctl.storageBus = StorageBus_IDE;
2609 sctl.controllerType = StorageControllerType_ICH6;
2610 }
2611 else if ( (m->sv >= SettingsVersion_v1_9)
2612 && (strType == "I82078")
2613 )
2614 {
2615 sctl.storageBus = StorageBus_Floppy;
2616 sctl.controllerType = StorageControllerType_I82078;
2617 }
2618 else if (strType == "LsiLogicSas")
2619 {
2620 sctl.storageBus = StorageBus_SAS;
2621 sctl.controllerType = StorageControllerType_LsiLogicSas;
2622 }
2623 else
2624 throw ConfigFileError(this, pelmController, N_("Invalid value '%s' for StorageController/@type attribute"), strType.c_str());
2625
2626 readStorageControllerAttributes(*pelmController, sctl);
2627
2628 xml::NodesLoop nlAttached(*pelmController, "AttachedDevice");
2629 const xml::ElementNode *pelmAttached;
2630 while ((pelmAttached = nlAttached.forAllNodes()))
2631 {
2632 AttachedDevice att;
2633 Utf8Str strTemp;
2634 pelmAttached->getAttributeValue("type", strTemp);
2635
2636 if (strTemp == "HardDisk")
2637 att.deviceType = DeviceType_HardDisk;
2638 else if (m->sv >= SettingsVersion_v1_9)
2639 {
2640 // starting with 1.9 we list DVD and floppy drive info + attachments under <StorageControllers>
2641 if (strTemp == "DVD")
2642 {
2643 att.deviceType = DeviceType_DVD;
2644 pelmAttached->getAttributeValue("passthrough", att.fPassThrough);
2645 }
2646 else if (strTemp == "Floppy")
2647 att.deviceType = DeviceType_Floppy;
2648 }
2649
2650 if (att.deviceType != DeviceType_Null)
2651 {
2652 const xml::ElementNode *pelmImage;
2653 // all types can have images attached, but for HardDisk it's required
2654 if (!(pelmImage = pelmAttached->findChildElement("Image")))
2655 {
2656 if (att.deviceType == DeviceType_HardDisk)
2657 throw ConfigFileError(this, pelmImage, N_("Required AttachedDevice/Image element is missing"));
2658 else
2659 {
2660 // DVDs and floppies can also have <HostDrive> instead of <Image>
2661 const xml::ElementNode *pelmHostDrive;
2662 if ((pelmHostDrive = pelmAttached->findChildElement("HostDrive")))
2663 if (!pelmHostDrive->getAttributeValue("src", att.strHostDriveSrc))
2664 throw ConfigFileError(this, pelmHostDrive, N_("Required AttachedDevice/HostDrive/@src attribute is missing"));
2665 }
2666 }
2667 else
2668 {
2669 if (!pelmImage->getAttributeValue("uuid", strTemp))
2670 throw ConfigFileError(this, pelmImage, N_("Required AttachedDevice/Image/@uuid attribute is missing"));
2671 parseUUID(att.uuid, strTemp);
2672 }
2673
2674 if (!pelmAttached->getAttributeValue("port", att.lPort))
2675 throw ConfigFileError(this, pelmImage, N_("Required AttachedDevice/@port attribute is missing"));
2676 if (!pelmAttached->getAttributeValue("device", att.lDevice))
2677 throw ConfigFileError(this, pelmImage, N_("Required AttachedDevice/@device attribute is missing"));
2678
2679 sctl.llAttachedDevices.push_back(att);
2680 }
2681 }
2682
2683 strg.llStorageControllers.push_back(sctl);
2684 }
2685}
2686
2687/**
2688 * This gets called for legacy pre-1.9 settings files after having parsed the
2689 * <Hardware> and <StorageControllers> sections to parse <Hardware> once more
2690 * for the <DVDDrive> and <FloppyDrive> sections.
2691 *
2692 * Before settings version 1.9, DVD and floppy drives were specified separately
2693 * under <Hardware>; we then need this extra loop to make sure the storage
2694 * controller structs are already set up so we can add stuff to them.
2695 *
2696 * @param elmHardware
2697 * @param strg
2698 */
2699void MachineConfigFile::readDVDAndFloppies_pre1_9(const xml::ElementNode &elmHardware,
2700 Storage &strg)
2701{
2702 xml::NodesLoop nl1(elmHardware);
2703 const xml::ElementNode *pelmHwChild;
2704 while ((pelmHwChild = nl1.forAllNodes()))
2705 {
2706 if (pelmHwChild->nameEquals("DVDDrive"))
2707 {
2708 // create a DVD "attached device" and attach it to the existing IDE controller
2709 AttachedDevice att;
2710 att.deviceType = DeviceType_DVD;
2711 // legacy DVD drive is always secondary master (port 1, device 0)
2712 att.lPort = 1;
2713 att.lDevice = 0;
2714 pelmHwChild->getAttributeValue("passthrough", att.fPassThrough);
2715
2716 const xml::ElementNode *pDriveChild;
2717 Utf8Str strTmp;
2718 if ( ((pDriveChild = pelmHwChild->findChildElement("Image")))
2719 && (pDriveChild->getAttributeValue("uuid", strTmp))
2720 )
2721 parseUUID(att.uuid, strTmp);
2722 else if ((pDriveChild = pelmHwChild->findChildElement("HostDrive")))
2723 pDriveChild->getAttributeValue("src", att.strHostDriveSrc);
2724
2725 // find the IDE controller and attach the DVD drive
2726 bool fFound = false;
2727 for (StorageControllersList::iterator it = strg.llStorageControllers.begin();
2728 it != strg.llStorageControllers.end();
2729 ++it)
2730 {
2731 StorageController &sctl = *it;
2732 if (sctl.storageBus == StorageBus_IDE)
2733 {
2734 sctl.llAttachedDevices.push_back(att);
2735 fFound = true;
2736 break;
2737 }
2738 }
2739
2740 if (!fFound)
2741 throw ConfigFileError(this, pelmHwChild, N_("Internal error: found DVD drive but IDE controller does not exist"));
2742 // shouldn't happen because pre-1.9 settings files always had at least one IDE controller in the settings
2743 // which should have gotten parsed in <StorageControllers> before this got called
2744 }
2745 else if (pelmHwChild->nameEquals("FloppyDrive"))
2746 {
2747 bool fEnabled;
2748 if ( (pelmHwChild->getAttributeValue("enabled", fEnabled))
2749 && (fEnabled)
2750 )
2751 {
2752 // create a new floppy controller and attach a floppy "attached device"
2753 StorageController sctl;
2754 sctl.strName = "Floppy Controller";
2755 sctl.storageBus = StorageBus_Floppy;
2756 sctl.controllerType = StorageControllerType_I82078;
2757 sctl.ulPortCount = 1;
2758
2759 AttachedDevice att;
2760 att.deviceType = DeviceType_Floppy;
2761 att.lPort = 0;
2762 att.lDevice = 0;
2763
2764 const xml::ElementNode *pDriveChild;
2765 Utf8Str strTmp;
2766 if ( ((pDriveChild = pelmHwChild->findChildElement("Image")))
2767 && (pDriveChild->getAttributeValue("uuid", strTmp))
2768 )
2769 parseUUID(att.uuid, strTmp);
2770 else if ((pDriveChild = pelmHwChild->findChildElement("HostDrive")))
2771 pDriveChild->getAttributeValue("src", att.strHostDriveSrc);
2772
2773 // store attachment with controller
2774 sctl.llAttachedDevices.push_back(att);
2775 // store controller with storage
2776 strg.llStorageControllers.push_back(sctl);
2777 }
2778 }
2779 }
2780}
2781
2782/**
2783 * Called initially for the <Snapshot> element under <Machine>, if present,
2784 * to store the snapshot's data into the given Snapshot structure (which is
2785 * then the one in the Machine struct). This might then recurse if
2786 * a <Snapshots> (plural) element is found in the snapshot, which should
2787 * contain a list of child snapshots; such lists are maintained in the
2788 * Snapshot structure.
2789 *
2790 * @param elmSnapshot
2791 * @param snap
2792 */
2793void MachineConfigFile::readSnapshot(const xml::ElementNode &elmSnapshot,
2794 Snapshot &snap)
2795{
2796 Utf8Str strTemp;
2797
2798 if (!elmSnapshot.getAttributeValue("uuid", strTemp))
2799 throw ConfigFileError(this, &elmSnapshot, N_("Required Snapshot/@uuid attribute is missing"));
2800 parseUUID(snap.uuid, strTemp);
2801
2802 if (!elmSnapshot.getAttributeValue("name", snap.strName))
2803 throw ConfigFileError(this, &elmSnapshot, N_("Required Snapshot/@name attribute is missing"));
2804
2805 // earlier 3.1 trunk builds had a bug and added Description as an attribute, read it silently and write it back as an element
2806 elmSnapshot.getAttributeValue("Description", snap.strDescription);
2807
2808 if (!elmSnapshot.getAttributeValue("timeStamp", strTemp))
2809 throw ConfigFileError(this, &elmSnapshot, N_("Required Snapshot/@timeStamp attribute is missing"));
2810 parseTimestamp(snap.timestamp, strTemp);
2811
2812 elmSnapshot.getAttributeValue("stateFile", snap.strStateFile); // online snapshots only
2813
2814 // parse Hardware before the other elements because other things depend on it
2815 const xml::ElementNode *pelmHardware;
2816 if (!(pelmHardware = elmSnapshot.findChildElement("Hardware")))
2817 throw ConfigFileError(this, &elmSnapshot, N_("Required Snapshot/@Hardware element is missing"));
2818 readHardware(*pelmHardware, snap.hardware, snap.storage);
2819
2820 xml::NodesLoop nlSnapshotChildren(elmSnapshot);
2821 const xml::ElementNode *pelmSnapshotChild;
2822 while ((pelmSnapshotChild = nlSnapshotChildren.forAllNodes()))
2823 {
2824 if (pelmSnapshotChild->nameEquals("Description"))
2825 snap.strDescription = pelmSnapshotChild->getValue();
2826 else if ( (m->sv < SettingsVersion_v1_7)
2827 && (pelmSnapshotChild->nameEquals("HardDiskAttachments"))
2828 )
2829 readHardDiskAttachments_pre1_7(*pelmSnapshotChild, snap.storage);
2830 else if ( (m->sv >= SettingsVersion_v1_7)
2831 && (pelmSnapshotChild->nameEquals("StorageControllers"))
2832 )
2833 readStorageControllers(*pelmSnapshotChild, snap.storage);
2834 else if (pelmSnapshotChild->nameEquals("Snapshots"))
2835 {
2836 xml::NodesLoop nlChildSnapshots(*pelmSnapshotChild);
2837 const xml::ElementNode *pelmChildSnapshot;
2838 while ((pelmChildSnapshot = nlChildSnapshots.forAllNodes()))
2839 {
2840 if (pelmChildSnapshot->nameEquals("Snapshot"))
2841 {
2842 Snapshot child;
2843 readSnapshot(*pelmChildSnapshot, child);
2844 snap.llChildSnapshots.push_back(child);
2845 }
2846 }
2847 }
2848 }
2849
2850 if (m->sv < SettingsVersion_v1_9)
2851 // go through Hardware once more to repair the settings controller structures
2852 // with data from old DVDDrive and FloppyDrive elements
2853 readDVDAndFloppies_pre1_9(*pelmHardware, snap.storage);
2854}
2855
2856const struct {
2857 const char *pcszOld;
2858 const char *pcszNew;
2859} aConvertOSTypes[] =
2860{
2861 { "unknown", "Other" },
2862 { "dos", "DOS" },
2863 { "win31", "Windows31" },
2864 { "win95", "Windows95" },
2865 { "win98", "Windows98" },
2866 { "winme", "WindowsMe" },
2867 { "winnt4", "WindowsNT4" },
2868 { "win2k", "Windows2000" },
2869 { "winxp", "WindowsXP" },
2870 { "win2k3", "Windows2003" },
2871 { "winvista", "WindowsVista" },
2872 { "win2k8", "Windows2008" },
2873 { "os2warp3", "OS2Warp3" },
2874 { "os2warp4", "OS2Warp4" },
2875 { "os2warp45", "OS2Warp45" },
2876 { "ecs", "OS2eCS" },
2877 { "linux22", "Linux22" },
2878 { "linux24", "Linux24" },
2879 { "linux26", "Linux26" },
2880 { "archlinux", "ArchLinux" },
2881 { "debian", "Debian" },
2882 { "opensuse", "OpenSUSE" },
2883 { "fedoracore", "Fedora" },
2884 { "gentoo", "Gentoo" },
2885 { "mandriva", "Mandriva" },
2886 { "redhat", "RedHat" },
2887 { "ubuntu", "Ubuntu" },
2888 { "xandros", "Xandros" },
2889 { "freebsd", "FreeBSD" },
2890 { "openbsd", "OpenBSD" },
2891 { "netbsd", "NetBSD" },
2892 { "netware", "Netware" },
2893 { "solaris", "Solaris" },
2894 { "opensolaris", "OpenSolaris" },
2895 { "l4", "L4" }
2896};
2897
2898void MachineConfigFile::convertOldOSType_pre1_5(Utf8Str &str)
2899{
2900 for (unsigned u = 0;
2901 u < RT_ELEMENTS(aConvertOSTypes);
2902 ++u)
2903 {
2904 if (str == aConvertOSTypes[u].pcszOld)
2905 {
2906 str = aConvertOSTypes[u].pcszNew;
2907 break;
2908 }
2909 }
2910}
2911
2912/**
2913 * Called from the constructor to actually read in the <Machine> element
2914 * of a machine config file.
2915 * @param elmMachine
2916 */
2917void MachineConfigFile::readMachine(const xml::ElementNode &elmMachine)
2918{
2919 Utf8Str strUUID;
2920 if ( (elmMachine.getAttributeValue("uuid", strUUID))
2921 && (elmMachine.getAttributeValue("name", strName))
2922 )
2923 {
2924 parseUUID(uuid, strUUID);
2925
2926 if (!elmMachine.getAttributeValue("nameSync", fNameSync))
2927 fNameSync = true;
2928
2929 Utf8Str str;
2930 elmMachine.getAttributeValue("Description", strDescription);
2931
2932 elmMachine.getAttributeValue("OSType", strOsType);
2933 if (m->sv < SettingsVersion_v1_5)
2934 convertOldOSType_pre1_5(strOsType);
2935
2936 elmMachine.getAttributeValue("stateFile", strStateFile);
2937 if (elmMachine.getAttributeValue("currentSnapshot", str))
2938 parseUUID(uuidCurrentSnapshot, str);
2939 elmMachine.getAttributeValue("snapshotFolder", strSnapshotFolder);
2940 if (!elmMachine.getAttributeValue("currentStateModified", fCurrentStateModified))
2941 fCurrentStateModified = true;
2942 if (elmMachine.getAttributeValue("lastStateChange", str))
2943 parseTimestamp(timeLastStateChange, str);
2944 // constructor has called RTTimeNow(&timeLastStateChange) before
2945
2946 // parse Hardware before the other elements because other things depend on it
2947 const xml::ElementNode *pelmHardware;
2948 if (!(pelmHardware = elmMachine.findChildElement("Hardware")))
2949 throw ConfigFileError(this, &elmMachine, N_("Required Machine/Hardware element is missing"));
2950 readHardware(*pelmHardware, hardwareMachine, storageMachine);
2951
2952 xml::NodesLoop nlRootChildren(elmMachine);
2953 const xml::ElementNode *pelmMachineChild;
2954 while ((pelmMachineChild = nlRootChildren.forAllNodes()))
2955 {
2956 if (pelmMachineChild->nameEquals("ExtraData"))
2957 readExtraData(*pelmMachineChild,
2958 mapExtraDataItems);
2959 else if ( (m->sv < SettingsVersion_v1_7)
2960 && (pelmMachineChild->nameEquals("HardDiskAttachments"))
2961 )
2962 readHardDiskAttachments_pre1_7(*pelmMachineChild, storageMachine);
2963 else if ( (m->sv >= SettingsVersion_v1_7)
2964 && (pelmMachineChild->nameEquals("StorageControllers"))
2965 )
2966 readStorageControllers(*pelmMachineChild, storageMachine);
2967 else if (pelmMachineChild->nameEquals("Snapshot"))
2968 {
2969 Snapshot snap;
2970 // this will recurse into child snapshots, if necessary
2971 readSnapshot(*pelmMachineChild, snap);
2972 llFirstSnapshot.push_back(snap);
2973 }
2974 else if (pelmMachineChild->nameEquals("Description"))
2975 strDescription = pelmMachineChild->getValue();
2976 else if (pelmMachineChild->nameEquals("Teleporter"))
2977 {
2978 if (!pelmMachineChild->getAttributeValue("enabled", fTeleporterEnabled))
2979 fTeleporterEnabled = false;
2980 if (!pelmMachineChild->getAttributeValue("port", uTeleporterPort))
2981 uTeleporterPort = 0;
2982 if (!pelmMachineChild->getAttributeValue("address", strTeleporterAddress))
2983 strTeleporterAddress = "";
2984 if (!pelmMachineChild->getAttributeValue("password", strTeleporterPassword))
2985 strTeleporterPassword = "";
2986 }
2987 }
2988
2989 if (m->sv < SettingsVersion_v1_9)
2990 // go through Hardware once more to repair the settings controller structures
2991 // with data from old DVDDrive and FloppyDrive elements
2992 readDVDAndFloppies_pre1_9(*pelmHardware, storageMachine);
2993 }
2994 else
2995 throw ConfigFileError(this, &elmMachine, N_("Required Machine/@uuid or @name attributes is missing"));
2996}
2997
2998/**
2999 * Creates a <Hardware> node under elmParent and then writes out the XML
3000 * keys under that. Called for both the <Machine> node and for snapshots.
3001 * @param elmParent
3002 * @param st
3003 */
3004void MachineConfigFile::buildHardwareXML(xml::ElementNode &elmParent,
3005 const Hardware &hw,
3006 const Storage &strg)
3007{
3008 xml::ElementNode *pelmHardware = elmParent.createChild("Hardware");
3009
3010 if (m->sv >= SettingsVersion_v1_4)
3011 pelmHardware->setAttribute("version", hw.strVersion);
3012 if ( (m->sv >= SettingsVersion_v1_9)
3013 && (!hw.uuid.isEmpty())
3014 )
3015 pelmHardware->setAttribute("uuid", makeString(hw.uuid));
3016
3017 xml::ElementNode *pelmCPU = pelmHardware->createChild("CPU");
3018
3019 xml::ElementNode *pelmHwVirtEx = pelmCPU->createChild("HardwareVirtEx");
3020 pelmHwVirtEx->setAttribute("enabled", hw.fHardwareVirt);
3021 if (m->sv >= SettingsVersion_v1_9)
3022 pelmHwVirtEx->setAttribute("exclusive", hw.fHardwareVirtExclusive);
3023
3024 pelmCPU->createChild("HardwareVirtExNestedPaging")->setAttribute("enabled", hw.fNestedPaging);
3025 pelmCPU->createChild("HardwareVirtExVPID")->setAttribute("enabled", hw.fVPID);
3026 pelmCPU->createChild("PAE")->setAttribute("enabled", hw.fPAE);
3027
3028 if (hw.fSyntheticCpu)
3029 pelmCPU->createChild("SyntheticCpu")->setAttribute("enabled", hw.fSyntheticCpu);
3030 pelmCPU->setAttribute("count", hw.cCPUs);
3031
3032 if (hw.fLargePages)
3033 pelmCPU->createChild("HardwareVirtExLargePages")->setAttribute("enabled", hw.fLargePages);
3034
3035 if (m->sv >= SettingsVersion_v1_10)
3036 {
3037 pelmCPU->setAttribute("hotplug", hw.fCpuHotPlug);
3038
3039 xml::ElementNode *pelmCpuTree = NULL;
3040 for (CpuList::const_iterator it = hw.llCpus.begin();
3041 it != hw.llCpus.end();
3042 ++it)
3043 {
3044 const Cpu &cpu = *it;
3045
3046 if (pelmCpuTree == NULL)
3047 pelmCpuTree = pelmCPU->createChild("CpuTree");
3048
3049 xml::ElementNode *pelmCpu = pelmCpuTree->createChild("Cpu");
3050 pelmCpu->setAttribute("id", cpu.ulId);
3051 }
3052 }
3053
3054 xml::ElementNode *pelmCpuIdTree = NULL;
3055 for (CpuIdLeafsList::const_iterator it = hw.llCpuIdLeafs.begin();
3056 it != hw.llCpuIdLeafs.end();
3057 ++it)
3058 {
3059 const CpuIdLeaf &leaf = *it;
3060
3061 if (pelmCpuIdTree == NULL)
3062 pelmCpuIdTree = pelmCPU->createChild("CpuIdTree");
3063
3064 xml::ElementNode *pelmCpuIdLeaf = pelmCpuIdTree->createChild("CpuIdLeaf");
3065 pelmCpuIdLeaf->setAttribute("id", leaf.ulId);
3066 pelmCpuIdLeaf->setAttribute("eax", leaf.ulEax);
3067 pelmCpuIdLeaf->setAttribute("ebx", leaf.ulEbx);
3068 pelmCpuIdLeaf->setAttribute("ecx", leaf.ulEcx);
3069 pelmCpuIdLeaf->setAttribute("edx", leaf.ulEdx);
3070 }
3071
3072 xml::ElementNode *pelmMemory = pelmHardware->createChild("Memory");
3073 pelmMemory->setAttribute("RAMSize", hw.ulMemorySizeMB);
3074
3075 if ( (m->sv >= SettingsVersion_v1_9)
3076 && (hw.firmwareType >= FirmwareType_EFI)
3077 )
3078 {
3079 xml::ElementNode *pelmFirmware = pelmHardware->createChild("Firmware");
3080 const char *pcszFirmware;
3081
3082 switch (hw.firmwareType)
3083 {
3084 case FirmwareType_EFI: pcszFirmware = "EFI"; break;
3085 case FirmwareType_EFI32: pcszFirmware = "EFI32"; break;
3086 case FirmwareType_EFI64: pcszFirmware = "EFI64"; break;
3087 case FirmwareType_EFIDUAL: pcszFirmware = "EFIDUAL"; break;
3088 default: pcszFirmware = "None"; break;
3089 }
3090 pelmFirmware->setAttribute("type", pcszFirmware);
3091 }
3092
3093 if ( (m->sv >= SettingsVersion_v1_10)
3094 )
3095 {
3096 xml::ElementNode *pelmHid = pelmHardware->createChild("HID");
3097 const char *pcszHid;
3098
3099 switch (hw.pointingHidType)
3100 {
3101 case PointingHidType_USBMouse: pcszHid = "USBMouse"; break;
3102 case PointingHidType_USBTablet: pcszHid = "USBTablet"; break;
3103 case PointingHidType_PS2Mouse: pcszHid = "PS2Mouse"; break;
3104 case PointingHidType_ComboMouse: pcszHid = "ComboMouse"; break;
3105 case PointingHidType_None: pcszHid = "None"; break;
3106 default: Assert(false); pcszHid = "PS2Mouse"; break;
3107 }
3108 pelmHid->setAttribute("Pointing", pcszHid);
3109
3110 switch (hw.keyboardHidType)
3111 {
3112 case KeyboardHidType_USBKeyboard: pcszHid = "USBKeyboard"; break;
3113 case KeyboardHidType_PS2Keyboard: pcszHid = "PS2Keyboard"; break;
3114 case KeyboardHidType_ComboKeyboard: pcszHid = "ComboKeyboard"; break;
3115 case KeyboardHidType_None: pcszHid = "None"; break;
3116 default: Assert(false); pcszHid = "PS2Keyboard"; break;
3117 }
3118 pelmHid->setAttribute("Keyboard", pcszHid);
3119 }
3120
3121 if ( (m->sv >= SettingsVersion_v1_10)
3122 )
3123 {
3124 xml::ElementNode *pelmHpet = pelmHardware->createChild("HPET");
3125 pelmHpet->setAttribute("enabled", hw.fHpetEnabled);
3126 }
3127
3128 xml::ElementNode *pelmBoot = pelmHardware->createChild("Boot");
3129 for (BootOrderMap::const_iterator it = hw.mapBootOrder.begin();
3130 it != hw.mapBootOrder.end();
3131 ++it)
3132 {
3133 uint32_t i = it->first;
3134 DeviceType_T type = it->second;
3135 const char *pcszDevice;
3136
3137 switch (type)
3138 {
3139 case DeviceType_Floppy: pcszDevice = "Floppy"; break;
3140 case DeviceType_DVD: pcszDevice = "DVD"; break;
3141 case DeviceType_HardDisk: pcszDevice = "HardDisk"; break;
3142 case DeviceType_Network: pcszDevice = "Network"; break;
3143 default: /*case DeviceType_Null:*/ pcszDevice = "None"; break;
3144 }
3145
3146 xml::ElementNode *pelmOrder = pelmBoot->createChild("Order");
3147 pelmOrder->setAttribute("position",
3148 i + 1); // XML is 1-based but internal data is 0-based
3149 pelmOrder->setAttribute("device", pcszDevice);
3150 }
3151
3152 xml::ElementNode *pelmDisplay = pelmHardware->createChild("Display");
3153 pelmDisplay->setAttribute("VRAMSize", hw.ulVRAMSizeMB);
3154 pelmDisplay->setAttribute("monitorCount", hw.cMonitors);
3155 pelmDisplay->setAttribute("accelerate3D", hw.fAccelerate3D);
3156
3157 if (m->sv >= SettingsVersion_v1_8)
3158 pelmDisplay->setAttribute("accelerate2DVideo", hw.fAccelerate2DVideo);
3159
3160 xml::ElementNode *pelmVRDP = pelmHardware->createChild("RemoteDisplay");
3161 pelmVRDP->setAttribute("enabled", hw.vrdpSettings.fEnabled);
3162 Utf8Str strPort = hw.vrdpSettings.strPort;
3163 if (!strPort.length())
3164 strPort = "3389";
3165 pelmVRDP->setAttribute("port", strPort);
3166 if (hw.vrdpSettings.strNetAddress.length())
3167 pelmVRDP->setAttribute("netAddress", hw.vrdpSettings.strNetAddress);
3168 const char *pcszAuthType;
3169 switch (hw.vrdpSettings.authType)
3170 {
3171 case VRDPAuthType_Guest: pcszAuthType = "Guest"; break;
3172 case VRDPAuthType_External: pcszAuthType = "External"; break;
3173 default: /*case VRDPAuthType_Null:*/ pcszAuthType = "Null"; break;
3174 }
3175 pelmVRDP->setAttribute("authType", pcszAuthType);
3176
3177 if (hw.vrdpSettings.ulAuthTimeout != 0)
3178 pelmVRDP->setAttribute("authTimeout", hw.vrdpSettings.ulAuthTimeout);
3179 if (hw.vrdpSettings.fAllowMultiConnection)
3180 pelmVRDP->setAttribute("allowMultiConnection", hw.vrdpSettings.fAllowMultiConnection);
3181 if (hw.vrdpSettings.fReuseSingleConnection)
3182 pelmVRDP->setAttribute("reuseSingleConnection", hw.vrdpSettings.fReuseSingleConnection);
3183
3184 xml::ElementNode *pelmBIOS = pelmHardware->createChild("BIOS");
3185 pelmBIOS->createChild("ACPI")->setAttribute("enabled", hw.biosSettings.fACPIEnabled);
3186 pelmBIOS->createChild("IOAPIC")->setAttribute("enabled", hw.biosSettings.fIOAPICEnabled);
3187
3188 xml::ElementNode *pelmLogo = pelmBIOS->createChild("Logo");
3189 pelmLogo->setAttribute("fadeIn", hw.biosSettings.fLogoFadeIn);
3190 pelmLogo->setAttribute("fadeOut", hw.biosSettings.fLogoFadeOut);
3191 pelmLogo->setAttribute("displayTime", hw.biosSettings.ulLogoDisplayTime);
3192 if (hw.biosSettings.strLogoImagePath.length())
3193 pelmLogo->setAttribute("imagePath", hw.biosSettings.strLogoImagePath);
3194
3195 const char *pcszBootMenu;
3196 switch (hw.biosSettings.biosBootMenuMode)
3197 {
3198 case BIOSBootMenuMode_Disabled: pcszBootMenu = "Disabled"; break;
3199 case BIOSBootMenuMode_MenuOnly: pcszBootMenu = "MenuOnly"; break;
3200 default: /*BIOSBootMenuMode_MessageAndMenu*/ pcszBootMenu = "MessageAndMenu"; break;
3201 }
3202 pelmBIOS->createChild("BootMenu")->setAttribute("mode", pcszBootMenu);
3203 pelmBIOS->createChild("TimeOffset")->setAttribute("value", hw.biosSettings.llTimeOffset);
3204 pelmBIOS->createChild("PXEDebug")->setAttribute("enabled", hw.biosSettings.fPXEDebugEnabled);
3205
3206 if (m->sv < SettingsVersion_v1_9)
3207 {
3208 // settings formats before 1.9 had separate DVDDrive and FloppyDrive items under Hardware;
3209 // run thru the storage controllers to see if we have a DVD or floppy drives
3210 size_t cDVDs = 0;
3211 size_t cFloppies = 0;
3212
3213 xml::ElementNode *pelmDVD = pelmHardware->createChild("DVDDrive");
3214 xml::ElementNode *pelmFloppy = pelmHardware->createChild("FloppyDrive");
3215
3216 for (StorageControllersList::const_iterator it = strg.llStorageControllers.begin();
3217 it != strg.llStorageControllers.end();
3218 ++it)
3219 {
3220 const StorageController &sctl = *it;
3221 // in old settings format, the DVD drive could only have been under the IDE controller
3222 if (sctl.storageBus == StorageBus_IDE)
3223 {
3224 for (AttachedDevicesList::const_iterator it2 = sctl.llAttachedDevices.begin();
3225 it2 != sctl.llAttachedDevices.end();
3226 ++it2)
3227 {
3228 const AttachedDevice &att = *it2;
3229 if (att.deviceType == DeviceType_DVD)
3230 {
3231 if (cDVDs > 0)
3232 throw ConfigFileError(this, NULL, N_("Internal error: cannot save more than one DVD drive with old settings format"));
3233
3234 ++cDVDs;
3235
3236 pelmDVD->setAttribute("passthrough", att.fPassThrough);
3237 if (!att.uuid.isEmpty())
3238 pelmDVD->createChild("Image")->setAttribute("uuid", makeString(att.uuid));
3239 else if (att.strHostDriveSrc.length())
3240 pelmDVD->createChild("HostDrive")->setAttribute("src", att.strHostDriveSrc);
3241 }
3242 }
3243 }
3244 else if (sctl.storageBus == StorageBus_Floppy)
3245 {
3246 size_t cFloppiesHere = sctl.llAttachedDevices.size();
3247 if (cFloppiesHere > 1)
3248 throw ConfigFileError(this, NULL, N_("Internal error: floppy controller cannot have more than one device attachment"));
3249 if (cFloppiesHere)
3250 {
3251 const AttachedDevice &att = sctl.llAttachedDevices.front();
3252 pelmFloppy->setAttribute("enabled", true);
3253 if (!att.uuid.isEmpty())
3254 pelmFloppy->createChild("Image")->setAttribute("uuid", makeString(att.uuid));
3255 else if (att.strHostDriveSrc.length())
3256 pelmFloppy->createChild("HostDrive")->setAttribute("src", att.strHostDriveSrc);
3257 }
3258
3259 cFloppies += cFloppiesHere;
3260 }
3261 }
3262
3263 if (cFloppies == 0)
3264 pelmFloppy->setAttribute("enabled", false);
3265 else if (cFloppies > 1)
3266 throw ConfigFileError(this, NULL, N_("Internal error: cannot save more than one floppy drive with old settings format"));
3267 }
3268
3269 xml::ElementNode *pelmUSB = pelmHardware->createChild("USBController");
3270 pelmUSB->setAttribute("enabled", hw.usbController.fEnabled);
3271 pelmUSB->setAttribute("enabledEhci", hw.usbController.fEnabledEHCI);
3272
3273 writeUSBDeviceFilters(*pelmUSB,
3274 hw.usbController.llDeviceFilters,
3275 false); // fHostMode
3276
3277 xml::ElementNode *pelmNetwork = pelmHardware->createChild("Network");
3278 for (NetworkAdaptersList::const_iterator it = hw.llNetworkAdapters.begin();
3279 it != hw.llNetworkAdapters.end();
3280 ++it)
3281 {
3282 const NetworkAdapter &nic = *it;
3283
3284 xml::ElementNode *pelmAdapter = pelmNetwork->createChild("Adapter");
3285 pelmAdapter->setAttribute("slot", nic.ulSlot);
3286 pelmAdapter->setAttribute("enabled", nic.fEnabled);
3287 pelmAdapter->setAttribute("MACAddress", nic.strMACAddress);
3288 pelmAdapter->setAttribute("cable", nic.fCableConnected);
3289 pelmAdapter->setAttribute("speed", nic.ulLineSpeed);
3290 if (nic.fTraceEnabled)
3291 {
3292 pelmAdapter->setAttribute("trace", nic.fTraceEnabled);
3293 pelmAdapter->setAttribute("tracefile", nic.strTraceFile);
3294 }
3295
3296 const char *pcszType;
3297 switch (nic.type)
3298 {
3299 case NetworkAdapterType_Am79C973: pcszType = "Am79C973"; break;
3300 case NetworkAdapterType_I82540EM: pcszType = "82540EM"; break;
3301 case NetworkAdapterType_I82543GC: pcszType = "82543GC"; break;
3302 case NetworkAdapterType_I82545EM: pcszType = "82545EM"; break;
3303 case NetworkAdapterType_Virtio: pcszType = "virtio"; break;
3304 default: /*case NetworkAdapterType_Am79C970A:*/ pcszType = "Am79C970A"; break;
3305 }
3306 pelmAdapter->setAttribute("type", pcszType);
3307
3308 xml::ElementNode *pelmNAT;
3309 switch (nic.mode)
3310 {
3311 case NetworkAttachmentType_NAT:
3312 pelmNAT = pelmAdapter->createChild("NAT");
3313 if (nic.nat.strNetwork.length())
3314 pelmNAT->setAttribute("network", nic.nat.strNetwork);
3315 if (m->sv >= SettingsVersion_v1_10)
3316 {
3317 if (nic.nat.strBindIP.length())
3318 pelmNAT->setAttribute("hostip", nic.nat.strBindIP);
3319 if (nic.nat.u32Mtu)
3320 pelmNAT->setAttribute("mtu", nic.nat.u32Mtu);
3321 if (nic.nat.u32SockRcv)
3322 pelmNAT->setAttribute("sockrcv", nic.nat.u32SockRcv);
3323 if (nic.nat.u32SockSnd)
3324 pelmNAT->setAttribute("socksnd", nic.nat.u32SockSnd);
3325 if (nic.nat.u32TcpRcv)
3326 pelmNAT->setAttribute("tcprcv", nic.nat.u32TcpRcv);
3327 if (nic.nat.u32TcpSnd)
3328 pelmNAT->setAttribute("tcpsnd", nic.nat.u32TcpSnd);
3329 xml::ElementNode *pelmDNS;
3330 pelmDNS = pelmNAT->createChild("DNS");
3331 pelmDNS->setAttribute("pass-domain", nic.nat.fDnsPassDomain);
3332 pelmDNS->setAttribute("use-proxy", nic.nat.fDnsProxy);
3333 pelmDNS->setAttribute("use-host-resolver", nic.nat.fDnsUseHostResolver);
3334 if ( nic.nat.strTftpPrefix.length()
3335 || nic.nat.strTftpBootFile.length()
3336 || nic.nat.strTftpNextServer.length())
3337 {
3338 xml::ElementNode *pelmTFTP;
3339 pelmTFTP = pelmNAT->createChild("TFTP");
3340 if (nic.nat.strTftpPrefix.length())
3341 pelmTFTP->setAttribute("prefix", nic.nat.strTftpPrefix);
3342 if (nic.nat.strTftpBootFile.length())
3343 pelmTFTP->setAttribute("boot-file", nic.nat.strTftpBootFile);
3344 if (nic.nat.strTftpNextServer.length())
3345 pelmTFTP->setAttribute("next-server", nic.nat.strTftpNextServer);
3346 }
3347 for(NATRuleList::const_iterator rule = nic.nat.llRules.begin();
3348 rule != nic.nat.llRules.end(); ++rule)
3349 {
3350 xml::ElementNode *pelmPF;
3351 pelmPF = pelmNAT->createChild("Forwarding");
3352 if ((*rule).strName.length())
3353 pelmPF->setAttribute("name", (*rule).strName);
3354 pelmPF->setAttribute("proto", (*rule).u32Proto);
3355 if ((*rule).strHostIP.length())
3356 pelmPF->setAttribute("hostip", (*rule).strHostIP);
3357 if ((*rule).u16HostPort)
3358 pelmPF->setAttribute("hostport", (*rule).u16HostPort);
3359 if ((*rule).strGuestIP.length())
3360 pelmPF->setAttribute("guestip", (*rule).strGuestIP);
3361 if ((*rule).u16GuestPort)
3362 pelmPF->setAttribute("guestport", (*rule).u16GuestPort);
3363 }
3364 }
3365 break;
3366
3367 case NetworkAttachmentType_Bridged:
3368 pelmAdapter->createChild("BridgedInterface")->setAttribute("name", nic.strName);
3369 break;
3370
3371 case NetworkAttachmentType_Internal:
3372 pelmAdapter->createChild("InternalNetwork")->setAttribute("name", nic.strName);
3373 break;
3374
3375 case NetworkAttachmentType_HostOnly:
3376 pelmAdapter->createChild("HostOnlyInterface")->setAttribute("name", nic.strName);
3377 break;
3378
3379 default: /*case NetworkAttachmentType_Null:*/
3380 break;
3381 }
3382 }
3383
3384 xml::ElementNode *pelmPorts = pelmHardware->createChild("UART");
3385 for (SerialPortsList::const_iterator it = hw.llSerialPorts.begin();
3386 it != hw.llSerialPorts.end();
3387 ++it)
3388 {
3389 const SerialPort &port = *it;
3390 xml::ElementNode *pelmPort = pelmPorts->createChild("Port");
3391 pelmPort->setAttribute("slot", port.ulSlot);
3392 pelmPort->setAttribute("enabled", port.fEnabled);
3393 pelmPort->setAttributeHex("IOBase", port.ulIOBase);
3394 pelmPort->setAttribute("IRQ", port.ulIRQ);
3395
3396 const char *pcszHostMode;
3397 switch (port.portMode)
3398 {
3399 case PortMode_HostPipe: pcszHostMode = "HostPipe"; break;
3400 case PortMode_HostDevice: pcszHostMode = "HostDevice"; break;
3401 case PortMode_RawFile: pcszHostMode = "RawFile"; break;
3402 default: /*case PortMode_Disconnected:*/ pcszHostMode = "Disconnected"; break;
3403 }
3404 switch (port.portMode)
3405 {
3406 case PortMode_HostPipe:
3407 pelmPort->setAttribute("server", port.fServer);
3408 /* no break */
3409 case PortMode_HostDevice:
3410 case PortMode_RawFile:
3411 pelmPort->setAttribute("path", port.strPath);
3412 break;
3413
3414 default:
3415 break;
3416 }
3417 pelmPort->setAttribute("hostMode", pcszHostMode);
3418 }
3419
3420 pelmPorts = pelmHardware->createChild("LPT");
3421 for (ParallelPortsList::const_iterator it = hw.llParallelPorts.begin();
3422 it != hw.llParallelPorts.end();
3423 ++it)
3424 {
3425 const ParallelPort &port = *it;
3426 xml::ElementNode *pelmPort = pelmPorts->createChild("Port");
3427 pelmPort->setAttribute("slot", port.ulSlot);
3428 pelmPort->setAttribute("enabled", port.fEnabled);
3429 pelmPort->setAttributeHex("IOBase", port.ulIOBase);
3430 pelmPort->setAttribute("IRQ", port.ulIRQ);
3431 if (port.strPath.length())
3432 pelmPort->setAttribute("path", port.strPath);
3433 }
3434
3435 xml::ElementNode *pelmAudio = pelmHardware->createChild("AudioAdapter");
3436 pelmAudio->setAttribute("controller", (hw.audioAdapter.controllerType == AudioControllerType_SB16) ? "SB16" : "AC97");
3437
3438 if ( m->sv >= SettingsVersion_v1_10)
3439 {
3440 xml::ElementNode *pelmRTC = pelmHardware->createChild("RTC");
3441 pelmRTC->setAttribute("localOrUTC", fRTCUseUTC ? "UTC" : "local");
3442 }
3443
3444 const char *pcszDriver;
3445 switch (hw.audioAdapter.driverType)
3446 {
3447 case AudioDriverType_WinMM: pcszDriver = "WinMM"; break;
3448 case AudioDriverType_DirectSound: pcszDriver = "DirectSound"; break;
3449 case AudioDriverType_SolAudio: pcszDriver = "SolAudio"; break;
3450 case AudioDriverType_ALSA: pcszDriver = "ALSA"; break;
3451 case AudioDriverType_Pulse: pcszDriver = "Pulse"; break;
3452 case AudioDriverType_OSS: pcszDriver = "OSS"; break;
3453 case AudioDriverType_CoreAudio: pcszDriver = "CoreAudio"; break;
3454 case AudioDriverType_MMPM: pcszDriver = "MMPM"; break;
3455 default: /*case AudioDriverType_Null:*/ pcszDriver = "Null"; break;
3456 }
3457 pelmAudio->setAttribute("driver", pcszDriver);
3458
3459 pelmAudio->setAttribute("enabled", hw.audioAdapter.fEnabled);
3460
3461 xml::ElementNode *pelmSharedFolders = pelmHardware->createChild("SharedFolders");
3462 for (SharedFoldersList::const_iterator it = hw.llSharedFolders.begin();
3463 it != hw.llSharedFolders.end();
3464 ++it)
3465 {
3466 const SharedFolder &sf = *it;
3467 xml::ElementNode *pelmThis = pelmSharedFolders->createChild("SharedFolder");
3468 pelmThis->setAttribute("name", sf.strName);
3469 pelmThis->setAttribute("hostPath", sf.strHostPath);
3470 pelmThis->setAttribute("writable", sf.fWritable);
3471 }
3472
3473 xml::ElementNode *pelmClip = pelmHardware->createChild("Clipboard");
3474 const char *pcszClip;
3475 switch (hw.clipboardMode)
3476 {
3477 case ClipboardMode_Disabled: pcszClip = "Disabled"; break;
3478 case ClipboardMode_HostToGuest: pcszClip = "HostToGuest"; break;
3479 case ClipboardMode_GuestToHost: pcszClip = "GuestToHost"; break;
3480 default: /*case ClipboardMode_Bidirectional:*/ pcszClip = "Bidirectional"; break;
3481 }
3482 pelmClip->setAttribute("mode", pcszClip);
3483
3484 if (m->sv >= SettingsVersion_v1_10)
3485 {
3486 xml::ElementNode *pelmIo = pelmHardware->createChild("IO");
3487 xml::ElementNode *pelmIoCache;
3488 xml::ElementNode *pelmIoBandwidth;
3489 const char *pcszTemp;
3490
3491 switch (hw.ioSettings.ioMgrType)
3492 {
3493 case IoMgrType_Simple: pcszTemp = "Simple"; break;
3494 case IoMgrType_Async:
3495 default:
3496 pcszTemp = "Async"; break;
3497 }
3498
3499 pelmIo->createChild("IoMgr")->setAttribute("type", pcszTemp);
3500
3501 switch (hw.ioSettings.ioBackendType)
3502 {
3503 case IoBackendType_Buffered: pcszTemp = "Buffered"; break;
3504 case IoBackendType_Unbuffered:
3505 default:
3506 pcszTemp = "Unbuffered"; break;
3507 }
3508
3509 pelmIo->createChild("IoBackend")->setAttribute("type", pcszTemp);
3510
3511 pelmIoCache = pelmIo->createChild("IoCache");
3512 pelmIoCache->setAttribute("enabled", hw.ioSettings.fIoCacheEnabled);
3513 pelmIoCache->setAttribute("size", hw.ioSettings.ulIoCacheSize);
3514 pelmIoBandwidth = pelmIo->createChild("IoBandwidth");
3515 pelmIoBandwidth->setAttribute("max", hw.ioSettings.ulIoBandwidthMax);
3516 }
3517
3518 xml::ElementNode *pelmGuest = pelmHardware->createChild("Guest");
3519 pelmGuest->setAttribute("memoryBalloonSize", hw.ulMemoryBalloonSize);
3520
3521 xml::ElementNode *pelmGuestProps = pelmHardware->createChild("GuestProperties");
3522 for (GuestPropertiesList::const_iterator it = hw.llGuestProperties.begin();
3523 it != hw.llGuestProperties.end();
3524 ++it)
3525 {
3526 const GuestProperty &prop = *it;
3527 xml::ElementNode *pelmProp = pelmGuestProps->createChild("GuestProperty");
3528 pelmProp->setAttribute("name", prop.strName);
3529 pelmProp->setAttribute("value", prop.strValue);
3530 pelmProp->setAttribute("timestamp", prop.timestamp);
3531 pelmProp->setAttribute("flags", prop.strFlags);
3532 }
3533
3534 if (hw.strNotificationPatterns.length())
3535 pelmGuestProps->setAttribute("notificationPatterns", hw.strNotificationPatterns);
3536}
3537
3538/**
3539 * Creates a <StorageControllers> node under elmParent and then writes out the XML
3540 * keys under that. Called for both the <Machine> node and for snapshots.
3541 * @param elmParent
3542 * @param st
3543 */
3544void MachineConfigFile::buildStorageControllersXML(xml::ElementNode &elmParent,
3545 const Storage &st)
3546{
3547 xml::ElementNode *pelmStorageControllers = elmParent.createChild("StorageControllers");
3548
3549 for (StorageControllersList::const_iterator it = st.llStorageControllers.begin();
3550 it != st.llStorageControllers.end();
3551 ++it)
3552 {
3553 const StorageController &sc = *it;
3554
3555 if ( (m->sv < SettingsVersion_v1_9)
3556 && (sc.controllerType == StorageControllerType_I82078)
3557 )
3558 // floppy controller already got written into <Hardware>/<FloppyController> in writeHardware()
3559 // for pre-1.9 settings
3560 continue;
3561
3562 xml::ElementNode *pelmController = pelmStorageControllers->createChild("StorageController");
3563 com::Utf8Str name = sc.strName.raw();
3564 //
3565 if (m->sv < SettingsVersion_v1_8)
3566 {
3567 // pre-1.8 settings use shorter controller names, they are
3568 // expanded when reading the settings
3569 if (name == "IDE Controller")
3570 name = "IDE";
3571 else if (name == "SATA Controller")
3572 name = "SATA";
3573 else if (name == "SCSI Controller")
3574 name = "SCSI";
3575 }
3576 pelmController->setAttribute("name", sc.strName);
3577
3578 const char *pcszType;
3579 switch (sc.controllerType)
3580 {
3581 case StorageControllerType_IntelAhci: pcszType = "AHCI"; break;
3582 case StorageControllerType_LsiLogic: pcszType = "LsiLogic"; break;
3583 case StorageControllerType_BusLogic: pcszType = "BusLogic"; break;
3584 case StorageControllerType_PIIX4: pcszType = "PIIX4"; break;
3585 case StorageControllerType_ICH6: pcszType = "ICH6"; break;
3586 case StorageControllerType_I82078: pcszType = "I82078"; break;
3587 case StorageControllerType_LsiLogicSas: pcszType = "LsiLogicSas"; break;
3588 default: /*case StorageControllerType_PIIX3:*/ pcszType = "PIIX3"; break;
3589 }
3590 pelmController->setAttribute("type", pcszType);
3591
3592 pelmController->setAttribute("PortCount", sc.ulPortCount);
3593
3594 if (m->sv >= SettingsVersion_v1_9)
3595 if (sc.ulInstance)
3596 pelmController->setAttribute("Instance", sc.ulInstance);
3597
3598 if (sc.controllerType == StorageControllerType_IntelAhci)
3599 {
3600 pelmController->setAttribute("IDE0MasterEmulationPort", sc.lIDE0MasterEmulationPort);
3601 pelmController->setAttribute("IDE0SlaveEmulationPort", sc.lIDE0SlaveEmulationPort);
3602 pelmController->setAttribute("IDE1MasterEmulationPort", sc.lIDE1MasterEmulationPort);
3603 pelmController->setAttribute("IDE1SlaveEmulationPort", sc.lIDE1SlaveEmulationPort);
3604 }
3605
3606 for (AttachedDevicesList::const_iterator it2 = sc.llAttachedDevices.begin();
3607 it2 != sc.llAttachedDevices.end();
3608 ++it2)
3609 {
3610 const AttachedDevice &att = *it2;
3611
3612 // For settings version before 1.9, DVDs and floppies are in hardware, not storage controllers,
3613 // so we shouldn't write them here; we only get here for DVDs though because we ruled out
3614 // the floppy controller at the top of the loop
3615 if ( att.deviceType == DeviceType_DVD
3616 && m->sv < SettingsVersion_v1_9
3617 )
3618 continue;
3619
3620 xml::ElementNode *pelmDevice = pelmController->createChild("AttachedDevice");
3621
3622 pcszType = NULL;
3623
3624 switch (att.deviceType)
3625 {
3626 case DeviceType_HardDisk:
3627 pcszType = "HardDisk";
3628 break;
3629
3630 case DeviceType_DVD:
3631 pcszType = "DVD";
3632 pelmDevice->setAttribute("passthrough", att.fPassThrough);
3633 break;
3634
3635 case DeviceType_Floppy:
3636 pcszType = "Floppy";
3637 break;
3638 }
3639
3640 pelmDevice->setAttribute("type", pcszType);
3641
3642 pelmDevice->setAttribute("port", att.lPort);
3643 pelmDevice->setAttribute("device", att.lDevice);
3644
3645 if (!att.uuid.isEmpty())
3646 pelmDevice->createChild("Image")->setAttribute("uuid", makeString(att.uuid));
3647 else if ( (m->sv >= SettingsVersion_v1_9)
3648 && (att.strHostDriveSrc.length())
3649 )
3650 pelmDevice->createChild("HostDrive")->setAttribute("src", att.strHostDriveSrc);
3651 }
3652 }
3653}
3654
3655/**
3656 * Writes a single snapshot into the DOM tree. Initially this gets called from MachineConfigFile::write()
3657 * for the root snapshot of a machine, if present; elmParent then points to the <Snapshots> node under the
3658 * <Machine> node to which <Snapshot> must be added. This may then recurse for child snapshots.
3659 * @param elmParent
3660 * @param snap
3661 */
3662void MachineConfigFile::buildSnapshotXML(xml::ElementNode &elmParent,
3663 const Snapshot &snap)
3664{
3665 xml::ElementNode *pelmSnapshot = elmParent.createChild("Snapshot");
3666
3667 pelmSnapshot->setAttribute("uuid", makeString(snap.uuid));
3668 pelmSnapshot->setAttribute("name", snap.strName);
3669 pelmSnapshot->setAttribute("timeStamp", makeString(snap.timestamp));
3670
3671 if (snap.strStateFile.length())
3672 pelmSnapshot->setAttribute("stateFile", snap.strStateFile);
3673
3674 if (snap.strDescription.length())
3675 pelmSnapshot->createChild("Description")->addContent(snap.strDescription);
3676
3677 buildHardwareXML(*pelmSnapshot, snap.hardware, snap.storage);
3678 buildStorageControllersXML(*pelmSnapshot, snap.storage);
3679
3680 if (snap.llChildSnapshots.size())
3681 {
3682 xml::ElementNode *pelmChildren = pelmSnapshot->createChild("Snapshots");
3683 for (SnapshotsList::const_iterator it = snap.llChildSnapshots.begin();
3684 it != snap.llChildSnapshots.end();
3685 ++it)
3686 {
3687 const Snapshot &child = *it;
3688 buildSnapshotXML(*pelmChildren, child);
3689 }
3690 }
3691}
3692
3693/**
3694 * Builds the XML DOM tree for the machine config under the given XML element.
3695 *
3696 * This has been separated out from write() so it can be called from elsewhere,
3697 * such as the OVF code, to build machine XML in an existing XML tree.
3698 *
3699 * As a result, this gets called from two locations:
3700 *
3701 * -- MachineConfigFile::write();
3702 *
3703 * -- Appliance::buildXMLForOneVirtualSystem()
3704 *
3705 * @param elmMachine XML <Machine> element to add attributes and elements to.
3706 * @param fWriteSnapshots If false, we omit snapshots entirely (we don't recurse then).
3707 */
3708void MachineConfigFile::buildMachineXML(xml::ElementNode &elmMachine,
3709 bool fIncludeSnapshots)
3710{
3711 elmMachine.setAttribute("uuid", makeString(uuid));
3712 elmMachine.setAttribute("name", strName);
3713 if (!fNameSync)
3714 elmMachine.setAttribute("nameSync", fNameSync);
3715 if (strDescription.length())
3716 elmMachine.createChild("Description")->addContent(strDescription);
3717 elmMachine.setAttribute("OSType", strOsType);
3718 if (strStateFile.length())
3719 elmMachine.setAttribute("stateFile", strStateFile);
3720 if (fIncludeSnapshots && !uuidCurrentSnapshot.isEmpty())
3721 elmMachine.setAttribute("currentSnapshot", makeString(uuidCurrentSnapshot));
3722 if (strSnapshotFolder.length())
3723 elmMachine.setAttribute("snapshotFolder", strSnapshotFolder);
3724 if (!fCurrentStateModified)
3725 elmMachine.setAttribute("currentStateModified", fCurrentStateModified);
3726 elmMachine.setAttribute("lastStateChange", makeString(timeLastStateChange));
3727 if (fAborted)
3728 elmMachine.setAttribute("aborted", fAborted);
3729 if ( m->sv >= SettingsVersion_v1_9
3730 && ( fTeleporterEnabled
3731 || uTeleporterPort
3732 || !strTeleporterAddress.isEmpty()
3733 || !strTeleporterPassword.isEmpty()
3734 )
3735 )
3736 {
3737 xml::ElementNode *pelmTeleporter = elmMachine.createChild("Teleporter");
3738 pelmTeleporter->setAttribute("enabled", fTeleporterEnabled);
3739 pelmTeleporter->setAttribute("port", uTeleporterPort);
3740 pelmTeleporter->setAttribute("address", strTeleporterAddress);
3741 pelmTeleporter->setAttribute("password", strTeleporterPassword);
3742 }
3743
3744 writeExtraData(elmMachine, mapExtraDataItems);
3745
3746 if (fIncludeSnapshots && llFirstSnapshot.size())
3747 buildSnapshotXML(elmMachine, llFirstSnapshot.front());
3748
3749 buildHardwareXML(elmMachine, hardwareMachine, storageMachine);
3750 buildStorageControllersXML(elmMachine, storageMachine);
3751}
3752
3753/**
3754 * Called from write() before calling ConfigFileBase::createStubDocument().
3755 * This adjusts the settings version in m->sv if incompatible settings require
3756 * a settings bump, whereas otherwise we try to preserve the settings version
3757 * to avoid breaking compatibility with older versions.
3758 */
3759void MachineConfigFile::bumpSettingsVersionIfNeeded()
3760{
3761 // The hardware versions other than "1" requires settings version 1.4 (2.1+).
3762 if ( m->sv < SettingsVersion_v1_4
3763 && hardwareMachine.strVersion != "1"
3764 )
3765 m->sv = SettingsVersion_v1_4;
3766
3767 // "accelerate 2d video" requires settings version 1.8
3768 if ( (m->sv < SettingsVersion_v1_8)
3769 && (hardwareMachine.fAccelerate2DVideo)
3770 )
3771 m->sv = SettingsVersion_v1_8;
3772
3773 // all the following require settings version 1.9
3774 if ( (m->sv < SettingsVersion_v1_9)
3775 && ( (hardwareMachine.firmwareType >= FirmwareType_EFI)
3776 || (hardwareMachine.fHardwareVirtExclusive != HWVIRTEXCLUSIVEDEFAULT)
3777 || fTeleporterEnabled
3778 || uTeleporterPort
3779 || !strTeleporterAddress.isEmpty()
3780 || !strTeleporterPassword.isEmpty()
3781 || !hardwareMachine.uuid.isEmpty()
3782 )
3783 )
3784 m->sv = SettingsVersion_v1_9;
3785
3786 // settings version 1.9 is also required if there is not exactly one DVD
3787 // or more than one floppy drive present or the DVD is not at the secondary
3788 // master; this check is a bit more complicated
3789 if (m->sv < SettingsVersion_v1_9)
3790 {
3791 size_t cDVDs = 0;
3792 size_t cFloppies = 0;
3793
3794 // need to run thru all the storage controllers to figure this out
3795 for (StorageControllersList::const_iterator it = storageMachine.llStorageControllers.begin();
3796 it != storageMachine.llStorageControllers.end()
3797 && m->sv < SettingsVersion_v1_9;
3798 ++it)
3799 {
3800 const StorageController &sctl = *it;
3801 for (AttachedDevicesList::const_iterator it2 = sctl.llAttachedDevices.begin();
3802 it2 != sctl.llAttachedDevices.end();
3803 ++it2)
3804 {
3805 if (sctl.ulInstance != 0) // we can only write the StorageController/@Instance attribute with v1.9
3806 {
3807 m->sv = SettingsVersion_v1_9;
3808 break;
3809 }
3810
3811 const AttachedDevice &att = *it2;
3812 if (att.deviceType == DeviceType_DVD)
3813 {
3814 if ( (sctl.storageBus != StorageBus_IDE) // DVD at bus other than DVD?
3815 || (att.lPort != 1) // DVDs not at secondary master?
3816 || (att.lDevice != 0)
3817 )
3818 {
3819 m->sv = SettingsVersion_v1_9;
3820 break;
3821 }
3822
3823 ++cDVDs;
3824 }
3825 else if (att.deviceType == DeviceType_Floppy)
3826 ++cFloppies;
3827 }
3828 }
3829
3830 // VirtualBox before 3.1 had zero or one floppy and exactly one DVD,
3831 // so any deviation from that will require settings version 1.9
3832 if ( (m->sv < SettingsVersion_v1_9)
3833 && ( (cDVDs != 1)
3834 || (cFloppies > 1)
3835 )
3836 )
3837 m->sv = SettingsVersion_v1_9;
3838 }
3839
3840 // VirtualBox 3.2 adds support for CPU hotplug, RTC timezone control, HID type and HPET
3841 if ( m->sv < SettingsVersion_v1_10
3842 && ( fRTCUseUTC
3843 || hardwareMachine.fCpuHotPlug
3844 || hardwareMachine.pointingHidType != PointingHidType_PS2Mouse
3845 || hardwareMachine.keyboardHidType != KeyboardHidType_PS2Keyboard
3846 || hardwareMachine.fHpetEnabled
3847 )
3848 )
3849 m->sv = SettingsVersion_v1_10;
3850 // VirtualBox 3.2 adds NAT Main
3851 if (m->sv < SettingsVersion_v1_10)
3852 {
3853 NetworkAdaptersList::const_iterator netit;
3854 for (netit = hardwareMachine.llNetworkAdapters.begin();
3855 netit != hardwareMachine.llNetworkAdapters.end(); ++netit)
3856 if ( netit->fEnabled
3857 && netit->mode == NetworkAttachmentType_NAT
3858 && ( netit->nat.u32Mtu != 0
3859 || netit->nat.u32SockRcv != 0
3860 || netit->nat.u32SockSnd != 0
3861 || netit->nat.u32TcpRcv != 0
3862 || netit->nat.u32TcpSnd != 0
3863 || !netit->nat.fDnsPassDomain
3864 || netit->nat.fDnsProxy
3865 || netit->nat.fDnsUseHostResolver
3866 || netit->nat.strTftpPrefix.length()
3867 || netit->nat.strTftpBootFile.length()
3868 || netit->nat.strTftpNextServer.length()
3869 || netit->nat.llRules.size())
3870 )
3871 {
3872 m->sv = SettingsVersion_v1_10;
3873 break;
3874 }
3875 }
3876 // Check for non default I/O settings and bump the settings version.
3877 if (m->sv < SettingsVersion_v1_10)
3878 {
3879 if ( hardwareMachine.ioSettings.fIoCacheEnabled != true
3880 || hardwareMachine.ioSettings.ulIoCacheSize != 5
3881 || hardwareMachine.ioSettings.ulIoBandwidthMax != 0
3882 || hardwareMachine.ioSettings.ioMgrType != IoMgrType_Async)
3883 m->sv = SettingsVersion_v1_10;
3884
3885#if defined(RT_OS_LINUX)
3886 if (hardwareMachine.ioSettings.ioBackendType != IoBackendType_Unbuffered)
3887 m->sv = SettingsVersion_v1_10;
3888#else
3889 if (hardwareMachine.ioSettings.ioBackendType != IoBackendType_Buffered)
3890 m->sv = SettingsVersion_v1_10;
3891#endif
3892 }
3893}
3894
3895/**
3896 * Called from Main code to write a machine config file to disk. This builds a DOM tree from
3897 * the member variables and then writes the XML file; it throws xml::Error instances on errors,
3898 * in particular if the file cannot be written.
3899 */
3900void MachineConfigFile::write(const com::Utf8Str &strFilename)
3901{
3902 try
3903 {
3904 // createStubDocument() sets the settings version to at least 1.7; however,
3905 // we might need to enfore a later settings version if incompatible settings
3906 // are present:
3907 bumpSettingsVersionIfNeeded();
3908
3909 m->strFilename = strFilename;
3910 createStubDocument();
3911
3912 xml::ElementNode *pelmMachine = m->pelmRoot->createChild("Machine");
3913 buildMachineXML(*pelmMachine,
3914 true /* fIncludeSnapshots */);
3915
3916 // now go write the XML
3917 xml::XmlFileWriter writer(*m->pDoc);
3918 writer.write(m->strFilename.c_str());
3919
3920 m->fFileExists = true;
3921 clearDocument();
3922 }
3923 catch (...)
3924 {
3925 clearDocument();
3926 throw;
3927 }
3928}
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