VirtualBox

source: vbox/trunk/src/VBox/Main/ApplianceImplExport.cpp@ 28195

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

Main/OVF: add vbox settings version to vbox:Machine element

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 72.5 KB
Line 
1/* $Id: ApplianceImplExport.cpp 28195 2010-04-12 10:38:51Z vboxsync $ */
2/** @file
3 *
4 * IAppliance and IVirtualSystem COM class implementations.
5 */
6
7/*
8 * Copyright (C) 2008-2010 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#include <iprt/path.h>
24#include <iprt/dir.h>
25#include <iprt/param.h>
26#include <iprt/s3.h>
27#include <iprt/manifest.h>
28
29#include <VBox/version.h>
30
31#include "ApplianceImpl.h"
32#include "VirtualBoxImpl.h"
33
34#include "ProgressImpl.h"
35#include "MachineImpl.h"
36
37#include "AutoCaller.h"
38#include "Logging.h"
39
40#include "ApplianceImplPrivate.h"
41
42using namespace std;
43
44////////////////////////////////////////////////////////////////////////////////
45//
46// IMachine public methods
47//
48////////////////////////////////////////////////////////////////////////////////
49
50// This code is here so we won't have to include the appliance headers in the
51// IMachine implementation, and we also need to access private appliance data.
52
53/**
54* Public method implementation.
55* @param appliance
56* @return
57*/
58
59STDMETHODIMP Machine::Export(IAppliance *aAppliance, IVirtualSystemDescription **aDescription)
60{
61 HRESULT rc = S_OK;
62
63 if (!aAppliance)
64 return E_POINTER;
65
66 AutoCaller autoCaller(this);
67 if (FAILED(autoCaller.rc())) return autoCaller.rc();
68
69 ComObjPtr<VirtualSystemDescription> pNewDesc;
70
71 try
72 {
73 // create a new virtual system to store in the appliance
74 rc = pNewDesc.createObject();
75 if (FAILED(rc)) throw rc;
76 rc = pNewDesc->init();
77 if (FAILED(rc)) throw rc;
78
79 // store the machine object so we can dump the XML in Appliance::Write()
80 pNewDesc->m->pMachine = this;
81
82 // now fill it with description items
83 Bstr bstrName1;
84 Bstr bstrDescription;
85 Bstr bstrGuestOSType;
86 uint32_t cCPUs;
87 uint32_t ulMemSizeMB;
88 BOOL fUSBEnabled;
89 BOOL fAudioEnabled;
90 AudioControllerType_T audioController;
91
92 ComPtr<IUSBController> pUsbController;
93 ComPtr<IAudioAdapter> pAudioAdapter;
94
95 // first, call the COM methods, as they request locks
96 rc = COMGETTER(USBController)(pUsbController.asOutParam());
97 if (FAILED(rc))
98 fUSBEnabled = false;
99 else
100 rc = pUsbController->COMGETTER(Enabled)(&fUSBEnabled);
101
102 // request the machine lock while acessing internal members
103 AutoReadLock alock1(this COMMA_LOCKVAL_SRC_POS);
104
105 pAudioAdapter = mAudioAdapter;
106 rc = pAudioAdapter->COMGETTER(Enabled)(&fAudioEnabled);
107 if (FAILED(rc)) throw rc;
108 rc = pAudioAdapter->COMGETTER(AudioController)(&audioController);
109 if (FAILED(rc)) throw rc;
110
111 // get name
112 bstrName1 = mUserData->mName;
113 // get description
114 bstrDescription = mUserData->mDescription;
115 // get guest OS
116 bstrGuestOSType = mUserData->mOSTypeId;
117 // CPU count
118 cCPUs = mHWData->mCPUCount;
119 // memory size in MB
120 ulMemSizeMB = mHWData->mMemorySize;
121 // VRAM size?
122 // BIOS settings?
123 // 3D acceleration enabled?
124 // hardware virtualization enabled?
125 // nested paging enabled?
126 // HWVirtExVPIDEnabled?
127 // PAEEnabled?
128 // snapshotFolder?
129 // VRDPServer?
130
131 /* Guest OS type */
132 Utf8Str strOsTypeVBox(bstrGuestOSType);
133 ovf::CIMOSType_T cim = convertVBoxOSType2CIMOSType(strOsTypeVBox.c_str());
134 pNewDesc->addEntry(VirtualSystemDescriptionType_OS,
135 "",
136 Utf8StrFmt("%RI32", cim),
137 strOsTypeVBox);
138
139 /* VM name */
140 Utf8Str strVMName(bstrName1);
141 pNewDesc->addEntry(VirtualSystemDescriptionType_Name,
142 "",
143 strVMName,
144 strVMName);
145
146 // description
147 Utf8Str strDescription(bstrDescription);
148 pNewDesc->addEntry(VirtualSystemDescriptionType_Description,
149 "",
150 strDescription,
151 strDescription);
152
153 /* CPU count*/
154 Utf8Str strCpuCount = Utf8StrFmt("%RI32", cCPUs);
155 pNewDesc->addEntry(VirtualSystemDescriptionType_CPU,
156 "",
157 strCpuCount,
158 strCpuCount);
159
160 /* Memory */
161 Utf8Str strMemory = Utf8StrFmt("%RI64", (uint64_t)ulMemSizeMB * _1M);
162 pNewDesc->addEntry(VirtualSystemDescriptionType_Memory,
163 "",
164 strMemory,
165 strMemory);
166
167 int32_t lIDEControllerIndex = 0;
168 int32_t lSATAControllerIndex = 0;
169 int32_t lSCSIControllerIndex = 0;
170
171 /* Fetch all available storage controllers */
172 com::SafeIfaceArray<IStorageController> nwControllers;
173 rc = COMGETTER(StorageControllers)(ComSafeArrayAsOutParam(nwControllers));
174 if (FAILED(rc)) throw rc;
175
176 ComPtr<IStorageController> pIDEController;
177#ifdef VBOX_WITH_AHCI
178 ComPtr<IStorageController> pSATAController;
179#endif /* VBOX_WITH_AHCI */
180#ifdef VBOX_WITH_LSILOGIC
181 ComPtr<IStorageController> pSCSIController;
182#endif /* VBOX_WITH_LSILOGIC */
183 for (size_t j = 0; j < nwControllers.size(); ++j)
184 {
185 StorageBus_T eType;
186 rc = nwControllers[j]->COMGETTER(Bus)(&eType);
187 if (FAILED(rc)) throw rc;
188 if ( eType == StorageBus_IDE
189 && pIDEController.isNull())
190 pIDEController = nwControllers[j];
191#ifdef VBOX_WITH_AHCI
192 else if ( eType == StorageBus_SATA
193 && pSATAController.isNull())
194 pSATAController = nwControllers[j];
195#endif /* VBOX_WITH_AHCI */
196#ifdef VBOX_WITH_LSILOGIC
197 else if ( eType == StorageBus_SCSI
198 && pSATAController.isNull())
199 pSCSIController = nwControllers[j];
200#endif /* VBOX_WITH_LSILOGIC */
201 }
202
203// <const name="HardDiskControllerIDE" value="6" />
204 if (!pIDEController.isNull())
205 {
206 Utf8Str strVbox;
207 StorageControllerType_T ctlr;
208 rc = pIDEController->COMGETTER(ControllerType)(&ctlr);
209 if (FAILED(rc)) throw rc;
210 switch(ctlr)
211 {
212 case StorageControllerType_PIIX3: strVbox = "PIIX3"; break;
213 case StorageControllerType_PIIX4: strVbox = "PIIX4"; break;
214 case StorageControllerType_ICH6: strVbox = "ICH6"; break;
215 }
216
217 if (strVbox.length())
218 {
219 lIDEControllerIndex = (int32_t)pNewDesc->m->llDescriptions.size();
220 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskControllerIDE,
221 Utf8StrFmt("%d", lIDEControllerIndex),
222 strVbox,
223 strVbox);
224 }
225 }
226
227#ifdef VBOX_WITH_AHCI
228// <const name="HardDiskControllerSATA" value="7" />
229 if (!pSATAController.isNull())
230 {
231 Utf8Str strVbox = "AHCI";
232 lSATAControllerIndex = (int32_t)pNewDesc->m->llDescriptions.size();
233 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskControllerSATA,
234 Utf8StrFmt("%d", lSATAControllerIndex),
235 strVbox,
236 strVbox);
237 }
238#endif // VBOX_WITH_AHCI
239
240#ifdef VBOX_WITH_LSILOGIC
241// <const name="HardDiskControllerSCSI" value="8" />
242 if (!pSCSIController.isNull())
243 {
244 StorageControllerType_T ctlr;
245 rc = pSCSIController->COMGETTER(ControllerType)(&ctlr);
246 if (SUCCEEDED(rc))
247 {
248 Utf8Str strVbox = "LsiLogic"; // the default in VBox
249 switch(ctlr)
250 {
251 case StorageControllerType_LsiLogic: strVbox = "LsiLogic"; break;
252 case StorageControllerType_BusLogic: strVbox = "BusLogic"; break;
253 }
254 lSCSIControllerIndex = (int32_t)pNewDesc->m->llDescriptions.size();
255 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskControllerSCSI,
256 Utf8StrFmt("%d", lSCSIControllerIndex),
257 strVbox,
258 strVbox);
259 }
260 else
261 throw rc;
262 }
263#endif // VBOX_WITH_LSILOGIC
264
265// <const name="HardDiskImage" value="9" />
266// <const name="Floppy" value="18" />
267// <const name="CDROM" value="19" />
268
269 MediaData::AttachmentList::iterator itA;
270 for (itA = mMediaData->mAttachments.begin();
271 itA != mMediaData->mAttachments.end();
272 ++itA)
273 {
274 ComObjPtr<MediumAttachment> pHDA = *itA;
275
276 // the attachment's data
277 ComPtr<IMedium> pMedium;
278 ComPtr<IStorageController> ctl;
279 Bstr controllerName;
280
281 rc = pHDA->COMGETTER(Controller)(controllerName.asOutParam());
282 if (FAILED(rc)) throw rc;
283
284 rc = GetStorageControllerByName(controllerName, ctl.asOutParam());
285 if (FAILED(rc)) throw rc;
286
287 StorageBus_T storageBus;
288 DeviceType_T deviceType;
289 LONG lChannel;
290 LONG lDevice;
291
292 rc = ctl->COMGETTER(Bus)(&storageBus);
293 if (FAILED(rc)) throw rc;
294
295 rc = pHDA->COMGETTER(Type)(&deviceType);
296 if (FAILED(rc)) throw rc;
297
298 rc = pHDA->COMGETTER(Medium)(pMedium.asOutParam());
299 if (FAILED(rc)) throw rc;
300
301 rc = pHDA->COMGETTER(Port)(&lChannel);
302 if (FAILED(rc)) throw rc;
303
304 rc = pHDA->COMGETTER(Device)(&lDevice);
305 if (FAILED(rc)) throw rc;
306
307 Utf8Str strTargetVmdkName;
308 Utf8Str strLocation;
309 ULONG64 ullSize = 0;
310
311 if ( deviceType == DeviceType_HardDisk
312 && pMedium
313 )
314 {
315 Bstr bstrLocation;
316 rc = pMedium->COMGETTER(Location)(bstrLocation.asOutParam());
317 if (FAILED(rc)) throw rc;
318 strLocation = bstrLocation;
319
320 // find the source's base medium for two things:
321 // 1) we'll use its name to determine the name of the target disk, which is readable,
322 // as opposed to the UUID filename of a differencing image, if pMedium is one
323 // 2) we need the size of the base image so we can give it to addEntry(), and later
324 // on export, the progress will be based on that (and not the diff image)
325 ComPtr<IMedium> pBaseMedium;
326 rc = pMedium->COMGETTER(Base)(pBaseMedium.asOutParam());
327 // returns pMedium if there are no diff images
328 if (FAILED(rc)) throw rc;
329
330 Bstr bstrBaseName;
331 rc = pBaseMedium->COMGETTER(Name)(bstrBaseName.asOutParam());
332 if (FAILED(rc)) throw rc;
333
334 strTargetVmdkName = bstrBaseName;
335 strTargetVmdkName.stripExt();
336 strTargetVmdkName.append(".vmdk");
337
338 // force reading state, or else size will be returned as 0
339 MediumState_T ms;
340 rc = pBaseMedium->RefreshState(&ms);
341 if (FAILED(rc)) throw rc;
342
343 rc = pBaseMedium->COMGETTER(Size)(&ullSize);
344 if (FAILED(rc)) throw rc;
345 }
346
347 // and how this translates to the virtual system
348 int32_t lControllerVsys = 0;
349 LONG lChannelVsys;
350
351 switch (storageBus)
352 {
353 case StorageBus_IDE:
354 // this is the exact reverse to what we're doing in Appliance::taskThreadImportMachines,
355 // and it must be updated when that is changed!
356
357 if (lChannel == 0 && lDevice == 0) // primary master
358 lChannelVsys = 0;
359 else if (lChannel == 0 && lDevice == 1) // primary slave
360 lChannelVsys = 1;
361 else if (lChannel == 1 && lDevice == 0) // secondary master; by default this is the CD-ROM but as of VirtualBox 3.1 that can change
362 lChannelVsys = 2;
363 else if (lChannel == 1 && lDevice == 1) // secondary slave
364 lChannelVsys = 3;
365 else
366 throw setError(VBOX_E_NOT_SUPPORTED,
367 tr("Cannot handle medium attachment: channel is %d, device is %d"), lChannel, lDevice);
368
369 lControllerVsys = lIDEControllerIndex;
370 break;
371
372 case StorageBus_SATA:
373 lChannelVsys = lChannel; // should be between 0 and 29
374 lControllerVsys = lSATAControllerIndex;
375 break;
376
377 case StorageBus_SCSI:
378 lChannelVsys = lChannel; // should be between 0 and 15
379 lControllerVsys = lSCSIControllerIndex;
380 break;
381
382 case StorageBus_Floppy:
383 lChannelVsys = 0;
384 lControllerVsys = 0;
385 break;
386
387 default:
388 throw setError(VBOX_E_NOT_SUPPORTED,
389 tr("Cannot handle medium attachment: storageBus is %d, channel is %d, device is %d"), storageBus, lChannel, lDevice);
390 break;
391 }
392
393 Utf8StrFmt strExtra("controller=%RI32;channel=%RI32", lControllerVsys, lChannelVsys);
394 Utf8Str strEmpty;
395
396 switch (deviceType)
397 {
398 case DeviceType_HardDisk:
399 Log(("Adding VirtualSystemDescriptionType_HardDiskImage, disk size: %RI64\n", ullSize));
400 pNewDesc->addEntry(VirtualSystemDescriptionType_HardDiskImage,
401 strTargetVmdkName, // disk ID: let's use the name
402 strTargetVmdkName, // OVF value:
403 strLocation, // vbox value: media path
404 (uint32_t)(ullSize / _1M),
405 strExtra);
406 break;
407
408 case DeviceType_DVD:
409 pNewDesc->addEntry(VirtualSystemDescriptionType_CDROM,
410 strEmpty, // disk ID
411 strEmpty, // OVF value
412 strEmpty, // vbox value
413 1, // ulSize
414 strExtra);
415 break;
416
417 case DeviceType_Floppy:
418 pNewDesc->addEntry(VirtualSystemDescriptionType_Floppy,
419 strEmpty, // disk ID
420 strEmpty, // OVF value
421 strEmpty, // vbox value
422 1, // ulSize
423 strExtra);
424 break;
425 }
426 }
427
428// <const name="NetworkAdapter" />
429 size_t a;
430 for (a = 0;
431 a < SchemaDefs::NetworkAdapterCount;
432 ++a)
433 {
434 ComPtr<INetworkAdapter> pNetworkAdapter;
435 BOOL fEnabled;
436 NetworkAdapterType_T adapterType;
437 NetworkAttachmentType_T attachmentType;
438
439 rc = GetNetworkAdapter((ULONG)a, pNetworkAdapter.asOutParam());
440 if (FAILED(rc)) throw rc;
441 /* Enable the network card & set the adapter type */
442 rc = pNetworkAdapter->COMGETTER(Enabled)(&fEnabled);
443 if (FAILED(rc)) throw rc;
444
445 if (fEnabled)
446 {
447 Utf8Str strAttachmentType;
448
449 rc = pNetworkAdapter->COMGETTER(AdapterType)(&adapterType);
450 if (FAILED(rc)) throw rc;
451
452 rc = pNetworkAdapter->COMGETTER(AttachmentType)(&attachmentType);
453 if (FAILED(rc)) throw rc;
454
455 switch (attachmentType)
456 {
457 case NetworkAttachmentType_Null:
458 strAttachmentType = "Null";
459 break;
460
461 case NetworkAttachmentType_NAT:
462 strAttachmentType = "NAT";
463 break;
464
465 case NetworkAttachmentType_Bridged:
466 strAttachmentType = "Bridged";
467 break;
468
469 case NetworkAttachmentType_Internal:
470 strAttachmentType = "Internal";
471 break;
472
473 case NetworkAttachmentType_HostOnly:
474 strAttachmentType = "HostOnly";
475 break;
476 }
477
478 pNewDesc->addEntry(VirtualSystemDescriptionType_NetworkAdapter,
479 "", // ref
480 strAttachmentType, // orig
481 Utf8StrFmt("%RI32", (uint32_t)adapterType), // conf
482 0,
483 Utf8StrFmt("type=%s", strAttachmentType.c_str())); // extra conf
484 }
485 }
486
487// <const name="USBController" />
488#ifdef VBOX_WITH_USB
489 if (fUSBEnabled)
490 pNewDesc->addEntry(VirtualSystemDescriptionType_USBController, "", "", "");
491#endif /* VBOX_WITH_USB */
492
493// <const name="SoundCard" />
494 if (fAudioEnabled)
495 pNewDesc->addEntry(VirtualSystemDescriptionType_SoundCard,
496 "",
497 "ensoniq1371", // this is what OVFTool writes and VMware supports
498 Utf8StrFmt("%RI32", audioController));
499
500 // finally, add the virtual system to the appliance
501 Appliance *pAppliance = static_cast<Appliance*>(aAppliance);
502 AutoCaller autoCaller1(pAppliance);
503 if (FAILED(autoCaller1.rc())) return autoCaller1.rc();
504
505 /* We return the new description to the caller */
506 ComPtr<IVirtualSystemDescription> copy(pNewDesc);
507 copy.queryInterfaceTo(aDescription);
508
509 AutoWriteLock alock(pAppliance COMMA_LOCKVAL_SRC_POS);
510
511 pAppliance->m->virtualSystemDescriptions.push_back(pNewDesc);
512 }
513 catch(HRESULT arc)
514 {
515 rc = arc;
516 }
517
518 return rc;
519}
520
521////////////////////////////////////////////////////////////////////////////////
522//
523// IAppliance public methods
524//
525////////////////////////////////////////////////////////////////////////////////
526
527/**
528 * Public method implementation.
529 * @param format
530 * @param path
531 * @param aProgress
532 * @return
533 */
534STDMETHODIMP Appliance::Write(IN_BSTR format, IN_BSTR path, IProgress **aProgress)
535{
536 if (!path) return E_POINTER;
537 CheckComArgOutPointerValid(aProgress);
538
539 AutoCaller autoCaller(this);
540 if (FAILED(autoCaller.rc())) return autoCaller.rc();
541
542 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
543
544 // do not allow entering this method if the appliance is busy reading or writing
545 if (!isApplianceIdle())
546 return E_ACCESSDENIED;
547
548 // see if we can handle this file; for now we insist it has an ".ovf" extension
549 Utf8Str strPath = path;
550 if (!strPath.endsWith(".ovf", Utf8Str::CaseInsensitive))
551 return setError(VBOX_E_FILE_ERROR,
552 tr("Appliance file must have .ovf extension"));
553
554 Utf8Str strFormat(format);
555 OVFFormat ovfF;
556 if (strFormat == "ovf-0.9")
557 ovfF = OVF_0_9;
558 else if (strFormat == "ovf-1.0")
559 ovfF = OVF_1_0;
560 else
561 return setError(VBOX_E_FILE_ERROR,
562 tr("Invalid format \"%s\" specified"), strFormat.c_str());
563
564 ComObjPtr<Progress> progress;
565 HRESULT rc = S_OK;
566 try
567 {
568 /* Parse all necessary info out of the URI */
569 parseURI(strPath, m->locInfo);
570 rc = writeImpl(ovfF, m->locInfo, progress);
571 }
572 catch (HRESULT aRC)
573 {
574 rc = aRC;
575 }
576
577 if (SUCCEEDED(rc))
578 /* Return progress to the caller */
579 progress.queryInterfaceTo(aProgress);
580
581 return rc;
582}
583
584////////////////////////////////////////////////////////////////////////////////
585//
586// Appliance private methods
587//
588////////////////////////////////////////////////////////////////////////////////
589
590/**
591 * Implementation for writing out the OVF to disk. This starts a new thread which will call
592 * Appliance::taskThreadWriteOVF().
593 *
594 * This is in a separate private method because it is used from two locations:
595 *
596 * 1) from the public Appliance::Write().
597 * 2) from Appliance::writeS3(), which got called from a previous instance of Appliance::taskThreadWriteOVF().
598 *
599 * @param aFormat
600 * @param aLocInfo
601 * @param aProgress
602 * @return
603 */
604HRESULT Appliance::writeImpl(OVFFormat aFormat, const LocationInfo &aLocInfo, ComObjPtr<Progress> &aProgress)
605{
606 HRESULT rc = S_OK;
607 try
608 {
609 Bstr progressDesc = BstrFmt(tr("Export appliance '%s'"),
610 aLocInfo.strPath.c_str());
611
612 rc = setUpProgress(aProgress, progressDesc, (aLocInfo.storageType == VFSType_File) ? Regular : WriteS3);
613
614 /* Initialize our worker task */
615 std::auto_ptr<TaskOVF> task(new TaskOVF(this, TaskOVF::Write, aLocInfo, aProgress));
616 /* The OVF version to write */
617 task->enFormat = aFormat;
618
619 rc = task->startThread();
620 if (FAILED(rc)) throw rc;
621
622 /* Don't destruct on success */
623 task.release();
624 }
625 catch (HRESULT aRC)
626 {
627 rc = aRC;
628 }
629
630 return rc;
631}
632
633/**
634 * Called from Appliance::writeFS() for each virtual system (machine) that needs XML written out.
635 *
636 * @param elmToAddVirtualSystemsTo XML element to append elements to.
637 * @param vsdescThis The IVirtualSystemDescription instance for which to write XML.
638 * @param enFormat OVF format (0.9 or 1.0).
639 * @param stack Structure for temporary private data shared with caller.
640 */
641void Appliance::buildXMLForOneVirtualSystem(xml::ElementNode &elmToAddVirtualSystemsTo,
642 ComObjPtr<VirtualSystemDescription> &vsdescThis,
643 OVFFormat enFormat,
644 XMLStack &stack)
645{
646 xml::ElementNode *pelmVirtualSystem;
647 if (enFormat == OVF_0_9)
648 {
649 // <Section xsi:type="ovf:NetworkSection_Type">
650 pelmVirtualSystem = elmToAddVirtualSystemsTo.createChild("Content");
651 pelmVirtualSystem->setAttribute("xsi:type", "ovf:VirtualSystem_Type");
652 }
653 else
654 pelmVirtualSystem = elmToAddVirtualSystemsTo.createChild("VirtualSystem");
655
656 /*xml::ElementNode *pelmVirtualSystemInfo =*/ pelmVirtualSystem->createChild("Info")->addContent("A virtual machine");
657
658 std::list<VirtualSystemDescriptionEntry*> llName = vsdescThis->findByType(VirtualSystemDescriptionType_Name);
659 if (llName.size() != 1)
660 throw setError(VBOX_E_NOT_SUPPORTED,
661 tr("Missing VM name"));
662 Utf8Str &strVMName = llName.front()->strVbox;
663 pelmVirtualSystem->setAttribute("ovf:id", strVMName);
664
665 // product info
666 std::list<VirtualSystemDescriptionEntry*> llProduct = vsdescThis->findByType(VirtualSystemDescriptionType_Product);
667 std::list<VirtualSystemDescriptionEntry*> llProductUrl = vsdescThis->findByType(VirtualSystemDescriptionType_ProductUrl);
668 std::list<VirtualSystemDescriptionEntry*> llVendor = vsdescThis->findByType(VirtualSystemDescriptionType_Vendor);
669 std::list<VirtualSystemDescriptionEntry*> llVendorUrl = vsdescThis->findByType(VirtualSystemDescriptionType_VendorUrl);
670 std::list<VirtualSystemDescriptionEntry*> llVersion = vsdescThis->findByType(VirtualSystemDescriptionType_Version);
671 bool fProduct = llProduct.size() && !llProduct.front()->strVbox.isEmpty();
672 bool fProductUrl = llProductUrl.size() && !llProductUrl.front()->strVbox.isEmpty();
673 bool fVendor = llVendor.size() && !llVendor.front()->strVbox.isEmpty();
674 bool fVendorUrl = llVendorUrl.size() && !llVendorUrl.front()->strVbox.isEmpty();
675 bool fVersion = llVersion.size() && !llVersion.front()->strVbox.isEmpty();
676 if (fProduct ||
677 fProductUrl ||
678 fVersion ||
679 fVendorUrl ||
680 fVersion)
681 {
682 /* <Section ovf:required="false" xsi:type="ovf:ProductSection_Type">
683 <Info>Meta-information about the installed software</Info>
684 <Product>VAtest</Product>
685 <Vendor>SUN Microsystems</Vendor>
686 <Version>10.0</Version>
687 <ProductUrl>http://blogs.sun.com/VirtualGuru</ProductUrl>
688 <VendorUrl>http://www.sun.com</VendorUrl>
689 </Section> */
690 xml::ElementNode *pelmAnnotationSection;
691 if (enFormat == OVF_0_9)
692 {
693 // <Section ovf:required="false" xsi:type="ovf:ProductSection_Type">
694 pelmAnnotationSection = pelmVirtualSystem->createChild("Section");
695 pelmAnnotationSection->setAttribute("xsi:type", "ovf:ProductSection_Type");
696 }
697 else
698 pelmAnnotationSection = pelmVirtualSystem->createChild("ProductSection");
699
700 pelmAnnotationSection->createChild("Info")->addContent("Meta-information about the installed software");
701 if (fProduct)
702 pelmAnnotationSection->createChild("Product")->addContent(llProduct.front()->strVbox);
703 if (fVendor)
704 pelmAnnotationSection->createChild("Vendor")->addContent(llVendor.front()->strVbox);
705 if (fVersion)
706 pelmAnnotationSection->createChild("Version")->addContent(llVersion.front()->strVbox);
707 if (fProductUrl)
708 pelmAnnotationSection->createChild("ProductUrl")->addContent(llProductUrl.front()->strVbox);
709 if (fVendorUrl)
710 pelmAnnotationSection->createChild("VendorUrl")->addContent(llVendorUrl.front()->strVbox);
711 }
712
713 // description
714 std::list<VirtualSystemDescriptionEntry*> llDescription = vsdescThis->findByType(VirtualSystemDescriptionType_Description);
715 if (llDescription.size() &&
716 !llDescription.front()->strVbox.isEmpty())
717 {
718 /* <Section ovf:required="false" xsi:type="ovf:AnnotationSection_Type">
719 <Info>A human-readable annotation</Info>
720 <Annotation>Plan 9</Annotation>
721 </Section> */
722 xml::ElementNode *pelmAnnotationSection;
723 if (enFormat == OVF_0_9)
724 {
725 // <Section ovf:required="false" xsi:type="ovf:AnnotationSection_Type">
726 pelmAnnotationSection = pelmVirtualSystem->createChild("Section");
727 pelmAnnotationSection->setAttribute("xsi:type", "ovf:AnnotationSection_Type");
728 }
729 else
730 pelmAnnotationSection = pelmVirtualSystem->createChild("AnnotationSection");
731
732 pelmAnnotationSection->createChild("Info")->addContent("A human-readable annotation");
733 pelmAnnotationSection->createChild("Annotation")->addContent(llDescription.front()->strVbox);
734 }
735
736 // license
737 std::list<VirtualSystemDescriptionEntry*> llLicense = vsdescThis->findByType(VirtualSystemDescriptionType_License);
738 if (llLicense.size() &&
739 !llLicense.front()->strVbox.isEmpty())
740 {
741 /* <EulaSection>
742 <Info ovf:msgid="6">License agreement for the Virtual System.</Info>
743 <License ovf:msgid="1">License terms can go in here.</License>
744 </EulaSection> */
745 xml::ElementNode *pelmEulaSection;
746 if (enFormat == OVF_0_9)
747 {
748 pelmEulaSection = pelmVirtualSystem->createChild("Section");
749 pelmEulaSection->setAttribute("xsi:type", "ovf:EulaSection_Type");
750 }
751 else
752 pelmEulaSection = pelmVirtualSystem->createChild("EulaSection");
753
754 pelmEulaSection->createChild("Info")->addContent("License agreement for the virtual system");
755 pelmEulaSection->createChild("License")->addContent(llLicense.front()->strVbox);
756 }
757
758 // operating system
759 std::list<VirtualSystemDescriptionEntry*> llOS = vsdescThis->findByType(VirtualSystemDescriptionType_OS);
760 if (llOS.size() != 1)
761 throw setError(VBOX_E_NOT_SUPPORTED,
762 tr("Missing OS type"));
763 /* <OperatingSystemSection ovf:id="82">
764 <Info>Guest Operating System</Info>
765 <Description>Linux 2.6.x</Description>
766 </OperatingSystemSection> */
767 xml::ElementNode *pelmOperatingSystemSection;
768 if (enFormat == OVF_0_9)
769 {
770 pelmOperatingSystemSection = pelmVirtualSystem->createChild("Section");
771 pelmOperatingSystemSection->setAttribute("xsi:type", "ovf:OperatingSystemSection_Type");
772 }
773 else
774 pelmOperatingSystemSection = pelmVirtualSystem->createChild("OperatingSystemSection");
775
776 pelmOperatingSystemSection->setAttribute("ovf:id", llOS.front()->strOvf);
777 pelmOperatingSystemSection->createChild("Info")->addContent("The kind of installed guest operating system");
778 Utf8Str strOSDesc;
779 convertCIMOSType2VBoxOSType(strOSDesc, (ovf::CIMOSType_T)llOS.front()->strOvf.toInt32(), "");
780 pelmOperatingSystemSection->createChild("Description")->addContent(strOSDesc);
781
782 // <VirtualHardwareSection ovf:id="hw1" ovf:transport="iso">
783 xml::ElementNode *pelmVirtualHardwareSection;
784 if (enFormat == OVF_0_9)
785 {
786 // <Section xsi:type="ovf:VirtualHardwareSection_Type">
787 pelmVirtualHardwareSection = pelmVirtualSystem->createChild("Section");
788 pelmVirtualHardwareSection->setAttribute("xsi:type", "ovf:VirtualHardwareSection_Type");
789 }
790 else
791 pelmVirtualHardwareSection = pelmVirtualSystem->createChild("VirtualHardwareSection");
792
793 pelmVirtualHardwareSection->createChild("Info")->addContent("Virtual hardware requirements for a virtual machine");
794
795 /* <System>
796 <vssd:Description>Description of the virtual hardware section.</vssd:Description>
797 <vssd:ElementName>vmware</vssd:ElementName>
798 <vssd:InstanceID>1</vssd:InstanceID>
799 <vssd:VirtualSystemIdentifier>MyLampService</vssd:VirtualSystemIdentifier>
800 <vssd:VirtualSystemType>vmx-4</vssd:VirtualSystemType>
801 </System> */
802 xml::ElementNode *pelmSystem = pelmVirtualHardwareSection->createChild("System");
803
804 pelmSystem->createChild("vssd:ElementName")->addContent("Virtual Hardware Family"); // required OVF 1.0
805
806 // <vssd:InstanceId>0</vssd:InstanceId>
807 if (enFormat == OVF_0_9)
808 pelmSystem->createChild("vssd:InstanceId")->addContent("0");
809 else // capitalization changed...
810 pelmSystem->createChild("vssd:InstanceID")->addContent("0");
811
812 // <vssd:VirtualSystemIdentifier>VAtest</vssd:VirtualSystemIdentifier>
813 pelmSystem->createChild("vssd:VirtualSystemIdentifier")->addContent(strVMName);
814 // <vssd:VirtualSystemType>vmx-4</vssd:VirtualSystemType>
815 const char *pcszHardware = "virtualbox-2.2";
816 if (enFormat == OVF_0_9)
817 // pretend to be vmware compatible then
818 pcszHardware = "vmx-6";
819 pelmSystem->createChild("vssd:VirtualSystemType")->addContent(pcszHardware);
820
821 // loop thru all description entries twice; once to write out all
822 // devices _except_ disk images, and a second time to assign the
823 // disk images; this is because disk images need to reference
824 // IDE controllers, and we can't know their instance IDs without
825 // assigning them first
826
827 uint32_t idIDEController = 0;
828 int32_t lIDEControllerIndex = 0;
829 uint32_t idSATAController = 0;
830 int32_t lSATAControllerIndex = 0;
831 uint32_t idSCSIController = 0;
832 int32_t lSCSIControllerIndex = 0;
833
834 uint32_t ulInstanceID = 1;
835
836 for (size_t uLoop = 1; uLoop <= 2; ++uLoop)
837 {
838 int32_t lIndexThis = 0;
839 list<VirtualSystemDescriptionEntry>::const_iterator itD;
840 for (itD = vsdescThis->m->llDescriptions.begin();
841 itD != vsdescThis->m->llDescriptions.end();
842 ++itD, ++lIndexThis)
843 {
844 const VirtualSystemDescriptionEntry &desc = *itD;
845
846 ovf::ResourceType_T type = (ovf::ResourceType_T)0; // if this becomes != 0 then we do stuff
847 Utf8Str strResourceSubType;
848
849 Utf8Str strDescription; // results in <rasd:Description>...</rasd:Description> block
850 Utf8Str strCaption; // results in <rasd:Caption>...</rasd:Caption> block
851
852 uint32_t ulParent = 0;
853
854 int32_t lVirtualQuantity = -1;
855 Utf8Str strAllocationUnits;
856
857 int32_t lAddress = -1;
858 int32_t lBusNumber = -1;
859 int32_t lAddressOnParent = -1;
860
861 int32_t lAutomaticAllocation = -1; // 0 means "false", 1 means "true"
862 Utf8Str strConnection; // results in <rasd:Connection>...</rasd:Connection> block
863 Utf8Str strHostResource;
864
865 uint64_t uTemp;
866
867 switch (desc.type)
868 {
869 case VirtualSystemDescriptionType_CPU:
870 /* <Item>
871 <rasd:Caption>1 virtual CPU</rasd:Caption>
872 <rasd:Description>Number of virtual CPUs</rasd:Description>
873 <rasd:ElementName>virtual CPU</rasd:ElementName>
874 <rasd:InstanceID>1</rasd:InstanceID>
875 <rasd:ResourceType>3</rasd:ResourceType>
876 <rasd:VirtualQuantity>1</rasd:VirtualQuantity>
877 </Item> */
878 if (uLoop == 1)
879 {
880 strDescription = "Number of virtual CPUs";
881 type = ovf::ResourceType_Processor; // 3
882 desc.strVbox.toInt(uTemp);
883 lVirtualQuantity = (int32_t)uTemp;
884 strCaption = Utf8StrFmt("%d virtual CPU", lVirtualQuantity); // without this ovftool won't eat the item
885 }
886 break;
887
888 case VirtualSystemDescriptionType_Memory:
889 /* <Item>
890 <rasd:AllocationUnits>MegaBytes</rasd:AllocationUnits>
891 <rasd:Caption>256 MB of memory</rasd:Caption>
892 <rasd:Description>Memory Size</rasd:Description>
893 <rasd:ElementName>Memory</rasd:ElementName>
894 <rasd:InstanceID>2</rasd:InstanceID>
895 <rasd:ResourceType>4</rasd:ResourceType>
896 <rasd:VirtualQuantity>256</rasd:VirtualQuantity>
897 </Item> */
898 if (uLoop == 1)
899 {
900 strDescription = "Memory Size";
901 type = ovf::ResourceType_Memory; // 4
902 desc.strVbox.toInt(uTemp);
903 lVirtualQuantity = (int32_t)(uTemp / _1M);
904 strAllocationUnits = "MegaBytes";
905 strCaption = Utf8StrFmt("%d MB of memory", lVirtualQuantity); // without this ovftool won't eat the item
906 }
907 break;
908
909 case VirtualSystemDescriptionType_HardDiskControllerIDE:
910 /* <Item>
911 <rasd:Caption>ideController1</rasd:Caption>
912 <rasd:Description>IDE Controller</rasd:Description>
913 <rasd:InstanceId>5</rasd:InstanceId>
914 <rasd:ResourceType>5</rasd:ResourceType>
915 <rasd:Address>1</rasd:Address>
916 <rasd:BusNumber>1</rasd:BusNumber>
917 </Item> */
918 if (uLoop == 1)
919 {
920 strDescription = "IDE Controller";
921 strCaption = "ideController0";
922 type = ovf::ResourceType_IDEController; // 5
923 strResourceSubType = desc.strVbox;
924 // it seems that OVFTool always writes these two, and since we can only
925 // have one IDE controller, we'll use this as well
926 lAddress = 1;
927 lBusNumber = 1;
928
929 // remember this ID
930 idIDEController = ulInstanceID;
931 lIDEControllerIndex = lIndexThis;
932 }
933 break;
934
935 case VirtualSystemDescriptionType_HardDiskControllerSATA:
936 /* <Item>
937 <rasd:Caption>sataController0</rasd:Caption>
938 <rasd:Description>SATA Controller</rasd:Description>
939 <rasd:InstanceId>4</rasd:InstanceId>
940 <rasd:ResourceType>20</rasd:ResourceType>
941 <rasd:ResourceSubType>ahci</rasd:ResourceSubType>
942 <rasd:Address>0</rasd:Address>
943 <rasd:BusNumber>0</rasd:BusNumber>
944 </Item>
945 */
946 if (uLoop == 1)
947 {
948 strDescription = "SATA Controller";
949 strCaption = "sataController0";
950 type = ovf::ResourceType_OtherStorageDevice; // 20
951 // it seems that OVFTool always writes these two, and since we can only
952 // have one SATA controller, we'll use this as well
953 lAddress = 0;
954 lBusNumber = 0;
955
956 if ( desc.strVbox.isEmpty() // AHCI is the default in VirtualBox
957 || (!desc.strVbox.compare("ahci", Utf8Str::CaseInsensitive))
958 )
959 strResourceSubType = "AHCI";
960 else
961 throw setError(VBOX_E_NOT_SUPPORTED,
962 tr("Invalid config string \"%s\" in SATA controller"), desc.strVbox.c_str());
963
964 // remember this ID
965 idSATAController = ulInstanceID;
966 lSATAControllerIndex = lIndexThis;
967 }
968 break;
969
970 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
971 /* <Item>
972 <rasd:Caption>scsiController0</rasd:Caption>
973 <rasd:Description>SCSI Controller</rasd:Description>
974 <rasd:InstanceId>4</rasd:InstanceId>
975 <rasd:ResourceType>6</rasd:ResourceType>
976 <rasd:ResourceSubType>buslogic</rasd:ResourceSubType>
977 <rasd:Address>0</rasd:Address>
978 <rasd:BusNumber>0</rasd:BusNumber>
979 </Item>
980 */
981 if (uLoop == 1)
982 {
983 strDescription = "SCSI Controller";
984 strCaption = "scsiController0";
985 type = ovf::ResourceType_ParallelSCSIHBA; // 6
986 // it seems that OVFTool always writes these two, and since we can only
987 // have one SATA controller, we'll use this as well
988 lAddress = 0;
989 lBusNumber = 0;
990
991 if ( desc.strVbox.isEmpty() // LsiLogic is the default in VirtualBox
992 || (!desc.strVbox.compare("lsilogic", Utf8Str::CaseInsensitive))
993 )
994 strResourceSubType = "lsilogic";
995 else if (!desc.strVbox.compare("buslogic", Utf8Str::CaseInsensitive))
996 strResourceSubType = "buslogic";
997 else
998 throw setError(VBOX_E_NOT_SUPPORTED,
999 tr("Invalid config string \"%s\" in SCSI controller"), desc.strVbox.c_str());
1000
1001 // remember this ID
1002 idSCSIController = ulInstanceID;
1003 lSCSIControllerIndex = lIndexThis;
1004 }
1005 break;
1006
1007 case VirtualSystemDescriptionType_HardDiskImage:
1008 /* <Item>
1009 <rasd:Caption>disk1</rasd:Caption>
1010 <rasd:InstanceId>8</rasd:InstanceId>
1011 <rasd:ResourceType>17</rasd:ResourceType>
1012 <rasd:HostResource>/disk/vmdisk1</rasd:HostResource>
1013 <rasd:Parent>4</rasd:Parent>
1014 <rasd:AddressOnParent>0</rasd:AddressOnParent>
1015 </Item> */
1016 if (uLoop == 2)
1017 {
1018 uint32_t cDisks = stack.mapDisks.size();
1019 Utf8Str strDiskID = Utf8StrFmt("vmdisk%RI32", ++cDisks);
1020
1021 strDescription = "Disk Image";
1022 strCaption = Utf8StrFmt("disk%RI32", cDisks); // this is not used for anything else
1023 type = ovf::ResourceType_HardDisk; // 17
1024
1025 // the following references the "<Disks>" XML block
1026 strHostResource = Utf8StrFmt("/disk/%s", strDiskID.c_str());
1027
1028 // controller=<index>;channel=<c>
1029 size_t pos1 = desc.strExtraConfig.find("controller=");
1030 size_t pos2 = desc.strExtraConfig.find("channel=");
1031 if (pos1 != Utf8Str::npos)
1032 {
1033 int32_t lControllerIndex = -1;
1034 RTStrToInt32Ex(desc.strExtraConfig.c_str() + pos1 + 11, NULL, 0, &lControllerIndex);
1035 if (lControllerIndex == lIDEControllerIndex)
1036 ulParent = idIDEController;
1037 else if (lControllerIndex == lSCSIControllerIndex)
1038 ulParent = idSCSIController;
1039 else if (lControllerIndex == lSATAControllerIndex)
1040 ulParent = idSATAController;
1041 }
1042 if (pos2 != Utf8Str::npos)
1043 RTStrToInt32Ex(desc.strExtraConfig.c_str() + pos2 + 8, NULL, 0, &lAddressOnParent);
1044
1045 if ( !ulParent
1046 || lAddressOnParent == -1
1047 )
1048 throw setError(VBOX_E_NOT_SUPPORTED,
1049 tr("Missing or bad extra config string in hard disk image: \"%s\""), desc.strExtraConfig.c_str());
1050
1051 stack.mapDisks[strDiskID] = &desc;
1052 }
1053 break;
1054
1055 case VirtualSystemDescriptionType_Floppy:
1056 if (uLoop == 1)
1057 {
1058 strDescription = "Floppy Drive";
1059 strCaption = "floppy0"; // this is what OVFTool writes
1060 type = ovf::ResourceType_FloppyDrive; // 14
1061 lAutomaticAllocation = 0;
1062 lAddressOnParent = 0; // this is what OVFTool writes
1063 }
1064 break;
1065
1066 case VirtualSystemDescriptionType_CDROM:
1067 if (uLoop == 2)
1068 {
1069 // we can't have a CD without an IDE controller
1070 if (!idIDEController)
1071 throw setError(VBOX_E_NOT_SUPPORTED,
1072 tr("Can't have CD-ROM without IDE controller"));
1073
1074 strDescription = "CD-ROM Drive";
1075 strCaption = "cdrom1"; // this is what OVFTool writes
1076 type = ovf::ResourceType_CDDrive; // 15
1077 lAutomaticAllocation = 1;
1078 ulParent = idIDEController;
1079 lAddressOnParent = 0; // this is what OVFTool writes
1080 }
1081 break;
1082
1083 case VirtualSystemDescriptionType_NetworkAdapter:
1084 /* <Item>
1085 <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
1086 <rasd:Caption>Ethernet adapter on 'VM Network'</rasd:Caption>
1087 <rasd:Connection>VM Network</rasd:Connection>
1088 <rasd:ElementName>VM network</rasd:ElementName>
1089 <rasd:InstanceID>3</rasd:InstanceID>
1090 <rasd:ResourceType>10</rasd:ResourceType>
1091 </Item> */
1092 if (uLoop == 1)
1093 {
1094 lAutomaticAllocation = 1;
1095 strCaption = Utf8StrFmt("Ethernet adapter on '%s'", desc.strOvf.c_str());
1096 type = ovf::ResourceType_EthernetAdapter; // 10
1097 /* Set the hardware type to something useful.
1098 * To be compatible with vmware & others we set
1099 * PCNet32 for our PCNet types & E1000 for the
1100 * E1000 cards. */
1101 switch (desc.strVbox.toInt32())
1102 {
1103 case NetworkAdapterType_Am79C970A:
1104 case NetworkAdapterType_Am79C973: strResourceSubType = "PCNet32"; break;
1105#ifdef VBOX_WITH_E1000
1106 case NetworkAdapterType_I82540EM:
1107 case NetworkAdapterType_I82545EM:
1108 case NetworkAdapterType_I82543GC: strResourceSubType = "E1000"; break;
1109#endif /* VBOX_WITH_E1000 */
1110 }
1111 strConnection = desc.strOvf;
1112
1113 stack.mapNetworks[desc.strOvf] = true;
1114 }
1115 break;
1116
1117 case VirtualSystemDescriptionType_USBController:
1118 /* <Item ovf:required="false">
1119 <rasd:Caption>usb</rasd:Caption>
1120 <rasd:Description>USB Controller</rasd:Description>
1121 <rasd:InstanceId>3</rasd:InstanceId>
1122 <rasd:ResourceType>23</rasd:ResourceType>
1123 <rasd:Address>0</rasd:Address>
1124 <rasd:BusNumber>0</rasd:BusNumber>
1125 </Item> */
1126 if (uLoop == 1)
1127 {
1128 strDescription = "USB Controller";
1129 strCaption = "usb";
1130 type = ovf::ResourceType_USBController; // 23
1131 lAddress = 0; // this is what OVFTool writes
1132 lBusNumber = 0; // this is what OVFTool writes
1133 }
1134 break;
1135
1136 case VirtualSystemDescriptionType_SoundCard:
1137 /* <Item ovf:required="false">
1138 <rasd:Caption>sound</rasd:Caption>
1139 <rasd:Description>Sound Card</rasd:Description>
1140 <rasd:InstanceId>10</rasd:InstanceId>
1141 <rasd:ResourceType>35</rasd:ResourceType>
1142 <rasd:ResourceSubType>ensoniq1371</rasd:ResourceSubType>
1143 <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
1144 <rasd:AddressOnParent>3</rasd:AddressOnParent>
1145 </Item> */
1146 if (uLoop == 1)
1147 {
1148 strDescription = "Sound Card";
1149 strCaption = "sound";
1150 type = ovf::ResourceType_SoundCard; // 35
1151 strResourceSubType = desc.strOvf; // e.g. ensoniq1371
1152 lAutomaticAllocation = 0;
1153 lAddressOnParent = 3; // what gives? this is what OVFTool writes
1154 }
1155 break;
1156 }
1157
1158 if (type)
1159 {
1160 xml::ElementNode *pItem;
1161
1162 pItem = pelmVirtualHardwareSection->createChild("Item");
1163
1164 // NOTE: do not change the order of these items without good reason! While we don't care
1165 // about ordering, VMware's ovftool does and fails if the items are not written in
1166 // exactly this order, as stupid as it seems.
1167
1168 if (!strCaption.isEmpty())
1169 {
1170 pItem->createChild("rasd:Caption")->addContent(strCaption);
1171 if (enFormat == OVF_1_0)
1172 pItem->createChild("rasd:ElementName")->addContent(strCaption);
1173 }
1174
1175 if (!strDescription.isEmpty())
1176 pItem->createChild("rasd:Description")->addContent(strDescription);
1177
1178 // <rasd:InstanceID>1</rasd:InstanceID>
1179 xml::ElementNode *pelmInstanceID;
1180 if (enFormat == OVF_0_9)
1181 pelmInstanceID = pItem->createChild("rasd:InstanceId");
1182 else
1183 pelmInstanceID = pItem->createChild("rasd:InstanceID"); // capitalization changed...
1184 pelmInstanceID->addContent(Utf8StrFmt("%d", ulInstanceID++));
1185
1186 // <rasd:ResourceType>3</rasd:ResourceType>
1187 pItem->createChild("rasd:ResourceType")->addContent(Utf8StrFmt("%d", type));
1188 if (!strResourceSubType.isEmpty())
1189 pItem->createChild("rasd:ResourceSubType")->addContent(strResourceSubType);
1190
1191 if (!strHostResource.isEmpty())
1192 pItem->createChild("rasd:HostResource")->addContent(strHostResource);
1193
1194 if (!strAllocationUnits.isEmpty())
1195 pItem->createChild("rasd:AllocationUnits")->addContent(strAllocationUnits);
1196
1197 // <rasd:VirtualQuantity>1</rasd:VirtualQuantity>
1198 if (lVirtualQuantity != -1)
1199 pItem->createChild("rasd:VirtualQuantity")->addContent(Utf8StrFmt("%d", lVirtualQuantity));
1200
1201 if (lAutomaticAllocation != -1)
1202 pItem->createChild("rasd:AutomaticAllocation")->addContent( (lAutomaticAllocation) ? "true" : "false" );
1203
1204 if (!strConnection.isEmpty())
1205 pItem->createChild("rasd:Connection")->addContent(strConnection);
1206
1207 if (lAddress != -1)
1208 pItem->createChild("rasd:Address")->addContent(Utf8StrFmt("%d", lAddress));
1209
1210 if (lBusNumber != -1)
1211 if (enFormat == OVF_0_9) // BusNumber is invalid OVF 1.0 so only write it in 0.9 mode for OVFTool compatibility
1212 pItem->createChild("rasd:BusNumber")->addContent(Utf8StrFmt("%d", lBusNumber));
1213
1214 if (ulParent)
1215 pItem->createChild("rasd:Parent")->addContent(Utf8StrFmt("%d", ulParent));
1216 if (lAddressOnParent != -1)
1217 pItem->createChild("rasd:AddressOnParent")->addContent(Utf8StrFmt("%d", lAddressOnParent));
1218 }
1219 }
1220 } // for (size_t uLoop = 1; uLoop <= 2; ++uLoop)
1221
1222 // now that we're done with the official OVF <Item> tags under <VirtualSystem>, write out VirtualBox XML
1223 // under the vbox: namespace
1224 xml::ElementNode *pelmVBoxMachine = pelmVirtualSystem->createChild("vbox:Machine");
1225 settings::MachineConfigFile *pConfig = new settings::MachineConfigFile(NULL);
1226
1227 try
1228 {
1229 AutoWriteLock machineLock(vsdescThis->m->pMachine COMMA_LOCKVAL_SRC_POS);
1230 vsdescThis->m->pMachine->copyMachineDataToSettings(*pConfig);
1231 pConfig->buildMachineXML(*pelmVBoxMachine,
1232 settings::MachineConfigFile::BuildMachineXML_WriteVboxVersionAttribute);
1233 // but not BuildMachineXML_IncludeSnapshots
1234 delete pConfig;
1235 }
1236 catch (...)
1237 {
1238 delete pConfig;
1239 throw;
1240 }
1241}
1242
1243/**
1244 * Actual worker code for writing out OVF to disk. This is called from Appliance::taskThreadWriteOVF()
1245 * and therefore runs on the OVF write worker thread. This runs in two contexts:
1246 *
1247 * 1) in a first worker thread; in that case, Appliance::Write() called Appliance::writeImpl();
1248 *
1249 * 2) in a second worker thread; in that case, Appliance::Write() called Appliance::writeImpl(), which
1250 * called Appliance::writeS3(), which called Appliance::writeImpl(), which then called this. In other
1251 * words, to write to the cloud, the first worker thread first starts a second worker thread to create
1252 * temporary files and then uploads them to the S3 cloud server.
1253 *
1254 * @param pTask
1255 * @return
1256 */
1257HRESULT Appliance::writeFS(const LocationInfo &locInfo, const OVFFormat enFormat, ComObjPtr<Progress> &pProgress)
1258{
1259 LogFlowFuncEnter();
1260 LogFlowFunc(("Appliance %p\n", this));
1261
1262 AutoCaller autoCaller(this);
1263 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1264
1265 HRESULT rc = S_OK;
1266
1267 try
1268 {
1269 AutoMultiWriteLock2 multiLock(&mVirtualBox->getMediaTreeLockHandle(), this->lockHandle() COMMA_LOCKVAL_SRC_POS);
1270
1271 xml::Document doc;
1272 xml::ElementNode *pelmRoot = doc.createRootElement("Envelope");
1273
1274 pelmRoot->setAttribute("ovf:version", (enFormat == OVF_1_0) ? "1.0" : "0.9");
1275 pelmRoot->setAttribute("xml:lang", "en-US");
1276
1277 Utf8Str strNamespace = (enFormat == OVF_0_9)
1278 ? "http://www.vmware.com/schema/ovf/1/envelope" // 0.9
1279 : "http://schemas.dmtf.org/ovf/envelope/1"; // 1.0
1280 pelmRoot->setAttribute("xmlns", strNamespace);
1281 pelmRoot->setAttribute("xmlns:ovf", strNamespace);
1282
1283// pelmRoot->setAttribute("xmlns:ovfstr", "http://schema.dmtf.org/ovf/strings/1");
1284 pelmRoot->setAttribute("xmlns:rasd", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData");
1285 pelmRoot->setAttribute("xmlns:vssd", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData");
1286 pelmRoot->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
1287 pelmRoot->setAttribute("xmlns:vbox", "http://www.virtualbox.org/ovf/machine");
1288// pelmRoot->setAttribute("xsi:schemaLocation", "http://schemas.dmtf.org/ovf/envelope/1 ../ovf-envelope.xsd");
1289
1290 // <Envelope>/<References>
1291 xml::ElementNode *pelmReferences = pelmRoot->createChild("References"); // 0.9 and 1.0
1292
1293 /* <Envelope>/<DiskSection>:
1294 <DiskSection>
1295 <Info>List of the virtual disks used in the package</Info>
1296 <Disk ovf:capacity="4294967296" ovf:diskId="lamp" ovf:format="http://www.vmware.com/specifications/vmdk.html#compressed" ovf:populatedSize="1924967692"/>
1297 </DiskSection> */
1298 xml::ElementNode *pelmDiskSection;
1299 if (enFormat == OVF_0_9)
1300 {
1301 // <Section xsi:type="ovf:DiskSection_Type">
1302 pelmDiskSection = pelmRoot->createChild("Section");
1303 pelmDiskSection->setAttribute("xsi:type", "ovf:DiskSection_Type");
1304 }
1305 else
1306 pelmDiskSection = pelmRoot->createChild("DiskSection");
1307
1308 xml::ElementNode *pelmDiskSectionInfo = pelmDiskSection->createChild("Info");
1309 pelmDiskSectionInfo->addContent("List of the virtual disks used in the package");
1310
1311 // the XML stack contains two maps for disks and networks, which allows us to
1312 // a) have a list of unique disk names (to make sure the same disk name is only added once)
1313 // and b) keep a list of all networks
1314 XMLStack stack;
1315
1316 /* <Envelope>/<NetworkSection>:
1317 <NetworkSection>
1318 <Info>Logical networks used in the package</Info>
1319 <Network ovf:name="VM Network">
1320 <Description>The network that the LAMP Service will be available on</Description>
1321 </Network>
1322 </NetworkSection> */
1323 xml::ElementNode *pelmNetworkSection;
1324 if (enFormat == OVF_0_9)
1325 {
1326 // <Section xsi:type="ovf:NetworkSection_Type">
1327 pelmNetworkSection = pelmRoot->createChild("Section");
1328 pelmNetworkSection->setAttribute("xsi:type", "ovf:NetworkSection_Type");
1329 }
1330 else
1331 pelmNetworkSection = pelmRoot->createChild("NetworkSection");
1332
1333 xml::ElementNode *pelmNetworkSectionInfo = pelmNetworkSection->createChild("Info");
1334 pelmNetworkSectionInfo->addContent("Logical networks used in the package");
1335
1336 // and here come the virtual systems:
1337
1338 // This can take a very long time so leave the locks; in particular, we have the media tree
1339 // lock which Medium::CloneTo() will request, and that would deadlock. Instead, protect
1340 // the appliance by resetting its state so we can safely leave the lock
1341 m->state = Data::ApplianceExporting;
1342 multiLock.release();
1343
1344 // write a collection if we have more than one virtual system _and_ we're
1345 // writing OVF 1.0; otherwise fail since ovftool can't import more than
1346 // one machine, it seems
1347 xml::ElementNode *pelmToAddVirtualSystemsTo;
1348 if (m->virtualSystemDescriptions.size() > 1)
1349 {
1350 if (enFormat == OVF_0_9)
1351 throw setError(VBOX_E_FILE_ERROR,
1352 tr("Cannot export more than one virtual system with OVF 0.9, use OVF 1.0"));
1353
1354 pelmToAddVirtualSystemsTo = pelmRoot->createChild("VirtualSystemCollection");
1355 pelmToAddVirtualSystemsTo->setAttribute("ovf:name", "ExportedVirtualBoxMachines"); // whatever
1356 }
1357 else
1358 pelmToAddVirtualSystemsTo = pelmRoot; // add virtual system directly under root element
1359
1360 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it;
1361 /* Iterate throughs all virtual systems of that appliance */
1362 for (it = m->virtualSystemDescriptions.begin();
1363 it != m->virtualSystemDescriptions.end();
1364 ++it)
1365 {
1366 ComObjPtr<VirtualSystemDescription> vsdescThis = *it;
1367 buildXMLForOneVirtualSystem(*pelmToAddVirtualSystemsTo,
1368 vsdescThis,
1369 enFormat,
1370 stack); // disks and networks stack
1371 }
1372
1373 // now, fill in the network section we set up empty above according
1374 // to the networks we found with the hardware items
1375 map<Utf8Str, bool>::const_iterator itN;
1376 for (itN = stack.mapNetworks.begin();
1377 itN != stack.mapNetworks.end();
1378 ++itN)
1379 {
1380 const Utf8Str &strNetwork = itN->first;
1381 xml::ElementNode *pelmNetwork = pelmNetworkSection->createChild("Network");
1382 pelmNetwork->setAttribute("ovf:name", strNetwork.c_str());
1383 pelmNetwork->createChild("Description")->addContent("Logical network used by this appliance.");
1384 }
1385
1386 // Finally, write out the disks!
1387
1388 list<Utf8Str> diskList;
1389 map<Utf8Str, const VirtualSystemDescriptionEntry*>::const_iterator itS;
1390 uint32_t ulFile = 1;
1391 for (itS = stack.mapDisks.begin();
1392 itS != stack.mapDisks.end();
1393 ++itS)
1394 {
1395 const Utf8Str &strDiskID = itS->first;
1396 const VirtualSystemDescriptionEntry *pDiskEntry = itS->second;
1397
1398 // source path: where the VBox image is
1399 const Utf8Str &strSrcFilePath = pDiskEntry->strVbox;
1400 Bstr bstrSrcFilePath(strSrcFilePath);
1401 if (!RTPathExists(strSrcFilePath.c_str()))
1402 /* This isn't allowed */
1403 throw setError(VBOX_E_FILE_ERROR,
1404 tr("Source virtual disk image file '%s' doesn't exist"),
1405 strSrcFilePath.c_str());
1406
1407 // clone the disk:
1408 ComPtr<IMedium> pSourceDisk;
1409 ComPtr<IMedium> pTargetDisk;
1410 ComPtr<IProgress> pProgress2;
1411
1412 Log(("Finding source disk \"%ls\"\n", bstrSrcFilePath.raw()));
1413 rc = mVirtualBox->FindHardDisk(bstrSrcFilePath, pSourceDisk.asOutParam());
1414 if (FAILED(rc)) throw rc;
1415
1416 Bstr uuidSource;
1417 rc = pSourceDisk->COMGETTER(Id)(uuidSource.asOutParam());
1418 if (FAILED(rc)) throw rc;
1419 Guid guidSource(uuidSource);
1420
1421 // output filename
1422 const Utf8Str &strTargetFileNameOnly = pDiskEntry->strOvf;
1423 // target path needs to be composed from where the output OVF is
1424 Utf8Str strTargetFilePath(locInfo.strPath);
1425 strTargetFilePath.stripFilename();
1426 strTargetFilePath.append("/");
1427 strTargetFilePath.append(strTargetFileNameOnly);
1428
1429 // We are always exporting to VMDK stream optimized for now
1430 Bstr bstrSrcFormat = L"VMDK";
1431
1432 // create a new hard disk interface for the destination disk image
1433 Log(("Creating target disk \"%s\"\n", strTargetFilePath.raw()));
1434 rc = mVirtualBox->CreateHardDisk(bstrSrcFormat, Bstr(strTargetFilePath), pTargetDisk.asOutParam());
1435 if (FAILED(rc)) throw rc;
1436
1437 // the target disk is now registered and needs to be removed again,
1438 // both after successful cloning or if anything goes bad!
1439 try
1440 {
1441 // create a flat copy of the source disk image
1442 rc = pSourceDisk->CloneTo(pTargetDisk, MediumVariant_VmdkStreamOptimized, NULL, pProgress2.asOutParam());
1443 if (FAILED(rc)) throw rc;
1444
1445 // advance to the next operation
1446 if (!pProgress.isNull())
1447 pProgress->SetNextOperation(BstrFmt(tr("Exporting to disk image '%s'"), strTargetFilePath.c_str()),
1448 pDiskEntry->ulSizeMB); // operation's weight, as set up with the IProgress originally);
1449
1450 // now wait for the background disk operation to complete; this throws HRESULTs on error
1451 waitForAsyncProgress(pProgress, pProgress2);
1452 }
1453 catch (HRESULT rc3)
1454 {
1455 // upon error after registering, close the disk or
1456 // it'll stick in the registry forever
1457 pTargetDisk->Close();
1458 throw rc3;
1459 }
1460 diskList.push_back(strTargetFilePath);
1461
1462 // we need the following for the XML
1463 uint64_t cbFile = 0; // actual file size
1464 rc = pTargetDisk->COMGETTER(Size)(&cbFile);
1465 if (FAILED(rc)) throw rc;
1466
1467 ULONG64 cbCapacity = 0; // size reported to guest
1468 rc = pTargetDisk->COMGETTER(LogicalSize)(&cbCapacity);
1469 if (FAILED(rc)) throw rc;
1470 // capacity is reported in megabytes, so...
1471 cbCapacity *= _1M;
1472
1473 // upon success, close the disk as well
1474 rc = pTargetDisk->Close();
1475 if (FAILED(rc)) throw rc;
1476
1477 // now handle the XML for the disk:
1478 Utf8StrFmt strFileRef("file%RI32", ulFile++);
1479 // <File ovf:href="WindowsXpProfessional-disk1.vmdk" ovf:id="file1" ovf:size="1710381056"/>
1480 xml::ElementNode *pelmFile = pelmReferences->createChild("File");
1481 pelmFile->setAttribute("ovf:href", strTargetFileNameOnly);
1482 pelmFile->setAttribute("ovf:id", strFileRef);
1483 pelmFile->setAttribute("ovf:size", Utf8StrFmt("%RI64", cbFile).c_str());
1484
1485 // add disk to XML Disks section
1486 // <Disk ovf:capacity="8589934592" ovf:diskId="vmdisk1" ovf:fileRef="file1" ovf:format="http://www.vmware.com/specifications/vmdk.html#sparse"/>
1487 xml::ElementNode *pelmDisk = pelmDiskSection->createChild("Disk");
1488 pelmDisk->setAttribute("ovf:capacity", Utf8StrFmt("%RI64", cbCapacity).c_str());
1489 pelmDisk->setAttribute("ovf:diskId", strDiskID);
1490 pelmDisk->setAttribute("ovf:fileRef", strFileRef);
1491 pelmDisk->setAttribute("ovf:format", "http://www.vmware.com/specifications/vmdk.html#sparse"); // must be sparse or ovftool chokes
1492 pelmDisk->setAttribute("vbox:uuid", Utf8StrFmt("%RTuuid", guidSource.raw()).c_str());
1493 }
1494
1495 // now go write the XML
1496 xml::XmlFileWriter writer(doc);
1497 writer.write(locInfo.strPath.c_str());
1498
1499 /* Create & write the manifest file */
1500 const char** ppManifestFiles = (const char**)RTMemAlloc(sizeof(char*)*diskList.size() + 1);
1501 ppManifestFiles[0] = locInfo.strPath.c_str();
1502 list<Utf8Str>::const_iterator it1;
1503 size_t i = 1;
1504 for (it1 = diskList.begin();
1505 it1 != diskList.end();
1506 ++it1, ++i)
1507 ppManifestFiles[i] = (*it1).c_str();
1508 Utf8Str strMfFile = manifestFileName(locInfo.strPath.c_str());
1509 int vrc = RTManifestWriteFiles(strMfFile.c_str(), ppManifestFiles, diskList.size()+1);
1510 RTMemFree(ppManifestFiles);
1511 if (RT_FAILURE(vrc))
1512 throw setError(VBOX_E_FILE_ERROR,
1513 tr("Couldn't create manifest file '%s' (%Rrc)"),
1514 RTPathFilename(strMfFile.c_str()), vrc);
1515 }
1516 catch(xml::Error &x)
1517 {
1518 rc = setError(VBOX_E_FILE_ERROR,
1519 x.what());
1520 }
1521 catch(HRESULT aRC)
1522 {
1523 rc = aRC;
1524 }
1525
1526 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
1527 // reset the state so others can call methods again
1528 m->state = Data::ApplianceIdle;
1529
1530 LogFlowFunc(("rc=%Rhrc\n", rc));
1531 LogFlowFuncLeave();
1532
1533 return rc;
1534}
1535
1536/**
1537 * Worker code for writing out OVF to the cloud. This is called from Appliance::taskThreadWriteOVF()
1538 * in S3 mode and therefore runs on the OVF write worker thread. This then starts a second worker
1539 * thread to create temporary files (see Appliance::writeFS()).
1540 *
1541 * @param pTask
1542 * @return
1543 */
1544HRESULT Appliance::writeS3(TaskOVF *pTask)
1545{
1546 LogFlowFuncEnter();
1547 LogFlowFunc(("Appliance %p\n", this));
1548
1549 AutoCaller autoCaller(this);
1550 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1551
1552 HRESULT rc = S_OK;
1553
1554 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
1555
1556 int vrc = VINF_SUCCESS;
1557 RTS3 hS3 = NIL_RTS3;
1558 char szOSTmpDir[RTPATH_MAX];
1559 RTPathTemp(szOSTmpDir, sizeof(szOSTmpDir));
1560 /* The template for the temporary directory created below */
1561 char *pszTmpDir;
1562 RTStrAPrintf(&pszTmpDir, "%s"RTPATH_SLASH_STR"vbox-ovf-XXXXXX", szOSTmpDir);
1563 list< pair<Utf8Str, ULONG> > filesList;
1564
1565 // todo:
1566 // - usable error codes
1567 // - seems snapshot filenames are problematic {uuid}.vdi
1568 try
1569 {
1570 /* Extract the bucket */
1571 Utf8Str tmpPath = pTask->locInfo.strPath;
1572 Utf8Str bucket;
1573 parseBucket(tmpPath, bucket);
1574
1575 /* We need a temporary directory which we can put the OVF file & all
1576 * disk images in */
1577 vrc = RTDirCreateTemp(pszTmpDir);
1578 if (RT_FAILURE(vrc))
1579 throw setError(VBOX_E_FILE_ERROR,
1580 tr("Cannot create temporary directory '%s'"), pszTmpDir);
1581
1582 /* The temporary name of the target OVF file */
1583 Utf8StrFmt strTmpOvf("%s/%s", pszTmpDir, RTPathFilename(tmpPath.c_str()));
1584
1585 /* Prepare the temporary writing of the OVF */
1586 ComObjPtr<Progress> progress;
1587 /* Create a temporary file based location info for the sub task */
1588 LocationInfo li;
1589 li.strPath = strTmpOvf;
1590 rc = writeImpl(pTask->enFormat, li, progress);
1591 if (FAILED(rc)) throw rc;
1592
1593 /* Unlock the appliance for the writing thread */
1594 appLock.release();
1595 /* Wait until the writing is done, but report the progress back to the
1596 caller */
1597 ComPtr<IProgress> progressInt(progress);
1598 waitForAsyncProgress(pTask->pProgress, progressInt); /* Any errors will be thrown */
1599
1600 /* Again lock the appliance for the next steps */
1601 appLock.acquire();
1602
1603 vrc = RTPathExists(strTmpOvf.c_str()); /* Paranoid check */
1604 if (RT_FAILURE(vrc))
1605 throw setError(VBOX_E_FILE_ERROR,
1606 tr("Cannot find source file '%s'"), strTmpOvf.c_str());
1607 /* Add the OVF file */
1608 filesList.push_back(pair<Utf8Str, ULONG>(strTmpOvf, m->ulWeightPerOperation)); /* Use 1% of the total for the OVF file upload */
1609 Utf8Str strMfFile = manifestFileName(strTmpOvf);
1610 filesList.push_back(pair<Utf8Str, ULONG>(strMfFile , m->ulWeightPerOperation)); /* Use 1% of the total for the manifest file upload */
1611
1612 /* Now add every disks of every virtual system */
1613 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it;
1614 for (it = m->virtualSystemDescriptions.begin();
1615 it != m->virtualSystemDescriptions.end();
1616 ++it)
1617 {
1618 ComObjPtr<VirtualSystemDescription> vsdescThis = (*it);
1619 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
1620 std::list<VirtualSystemDescriptionEntry*>::const_iterator itH;
1621 for (itH = avsdeHDs.begin();
1622 itH != avsdeHDs.end();
1623 ++itH)
1624 {
1625 const Utf8Str &strTargetFileNameOnly = (*itH)->strOvf;
1626 /* Target path needs to be composed from where the output OVF is */
1627 Utf8Str strTargetFilePath(strTmpOvf);
1628 strTargetFilePath.stripFilename();
1629 strTargetFilePath.append("/");
1630 strTargetFilePath.append(strTargetFileNameOnly);
1631 vrc = RTPathExists(strTargetFilePath.c_str()); /* Paranoid check */
1632 if (RT_FAILURE(vrc))
1633 throw setError(VBOX_E_FILE_ERROR,
1634 tr("Cannot find source file '%s'"), strTargetFilePath.c_str());
1635 filesList.push_back(pair<Utf8Str, ULONG>(strTargetFilePath, (*itH)->ulSizeMB));
1636 }
1637 }
1638 /* Next we have to upload the OVF & all disk images */
1639 vrc = RTS3Create(&hS3, pTask->locInfo.strUsername.c_str(), pTask->locInfo.strPassword.c_str(), pTask->locInfo.strHostname.c_str(), "virtualbox-agent/"VBOX_VERSION_STRING);
1640 if (RT_FAILURE(vrc))
1641 throw setError(VBOX_E_IPRT_ERROR,
1642 tr("Cannot create S3 service handler"));
1643 RTS3SetProgressCallback(hS3, pTask->updateProgress, &pTask);
1644
1645 /* Upload all files */
1646 for (list< pair<Utf8Str, ULONG> >::const_iterator it1 = filesList.begin(); it1 != filesList.end(); ++it1)
1647 {
1648 const pair<Utf8Str, ULONG> &s = (*it1);
1649 char *pszFilename = RTPathFilename(s.first.c_str());
1650 /* Advance to the next operation */
1651 if (!pTask->pProgress.isNull())
1652 pTask->pProgress->SetNextOperation(BstrFmt(tr("Uploading file '%s'"), pszFilename), s.second);
1653 vrc = RTS3PutKey(hS3, bucket.c_str(), pszFilename, s.first.c_str());
1654 if (RT_FAILURE(vrc))
1655 {
1656 if (vrc == VERR_S3_CANCELED)
1657 break;
1658 else if (vrc == VERR_S3_ACCESS_DENIED)
1659 throw setError(E_ACCESSDENIED,
1660 tr("Cannot upload file '%s' to S3 storage server (Access denied). Make sure that your credentials are right. Also check that your host clock is properly synced"), pszFilename);
1661 else if (vrc == VERR_S3_NOT_FOUND)
1662 throw setError(VBOX_E_FILE_ERROR,
1663 tr("Cannot upload file '%s' to S3 storage server (File not found)"), pszFilename);
1664 else
1665 throw setError(VBOX_E_IPRT_ERROR,
1666 tr("Cannot upload file '%s' to S3 storage server (%Rrc)"), pszFilename, vrc);
1667 }
1668 }
1669 }
1670 catch(HRESULT aRC)
1671 {
1672 rc = aRC;
1673 }
1674 /* Cleanup */
1675 RTS3Destroy(hS3);
1676 /* Delete all files which where temporary created */
1677 for (list< pair<Utf8Str, ULONG> >::const_iterator it1 = filesList.begin(); it1 != filesList.end(); ++it1)
1678 {
1679 const char *pszFilePath = (*it1).first.c_str();
1680 if (RTPathExists(pszFilePath))
1681 {
1682 vrc = RTFileDelete(pszFilePath);
1683 if (RT_FAILURE(vrc))
1684 rc = setError(VBOX_E_FILE_ERROR,
1685 tr("Cannot delete file '%s' (%Rrc)"), pszFilePath, vrc);
1686 }
1687 }
1688 /* Delete the temporary directory */
1689 if (RTPathExists(pszTmpDir))
1690 {
1691 vrc = RTDirRemove(pszTmpDir);
1692 if (RT_FAILURE(vrc))
1693 rc = setError(VBOX_E_FILE_ERROR,
1694 tr("Cannot delete temporary directory '%s' (%Rrc)"), pszTmpDir, vrc);
1695 }
1696 if (pszTmpDir)
1697 RTStrFree(pszTmpDir);
1698
1699 LogFlowFunc(("rc=%Rhrc\n", rc));
1700 LogFlowFuncLeave();
1701
1702 return rc;
1703}
1704
Note: See TracBrowser for help on using the repository browser.

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