VirtualBox

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

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

Main/OVF: use readable disk names instead of UUIDs on export, better progress message

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 72.6 KB
Line 
1/* $Id: ApplianceImplExport.cpp 28150 2010-04-09 15:58:10Z 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 /* todo: This progress init stuff should be done a little bit more generic */
613 if (aLocInfo.storageType == VFSType_File)
614 rc = setUpProgressFS(aProgress, progressDesc);
615 else
616 rc = setUpProgressWriteS3(aProgress, progressDesc);
617
618 /* Initialize our worker task */
619 std::auto_ptr<TaskOVF> task(new TaskOVF(this, TaskOVF::Write, aLocInfo, aProgress));
620 /* The OVF version to write */
621 task->enFormat = aFormat;
622
623 rc = task->startThread();
624 if (FAILED(rc)) throw rc;
625
626 /* Don't destruct on success */
627 task.release();
628 }
629 catch (HRESULT aRC)
630 {
631 rc = aRC;
632 }
633
634 return rc;
635}
636
637/**
638 * Called from Appliance::writeFS() for each virtual system (machine) that needs XML written out.
639 *
640 * @param elmToAddVirtualSystemsTo XML element to append elements to.
641 * @param vsdescThis The IVirtualSystemDescription instance for which to write XML.
642 * @param enFormat OVF format (0.9 or 1.0).
643 * @param stack Structure for temporary private data shared with caller.
644 */
645void Appliance::buildXMLForOneVirtualSystem(xml::ElementNode &elmToAddVirtualSystemsTo,
646 ComObjPtr<VirtualSystemDescription> &vsdescThis,
647 OVFFormat enFormat,
648 XMLStack &stack)
649{
650 xml::ElementNode *pelmVirtualSystem;
651 if (enFormat == OVF_0_9)
652 {
653 // <Section xsi:type="ovf:NetworkSection_Type">
654 pelmVirtualSystem = elmToAddVirtualSystemsTo.createChild("Content");
655 pelmVirtualSystem->setAttribute("xsi:type", "ovf:VirtualSystem_Type");
656 }
657 else
658 pelmVirtualSystem = elmToAddVirtualSystemsTo.createChild("VirtualSystem");
659
660 /*xml::ElementNode *pelmVirtualSystemInfo =*/ pelmVirtualSystem->createChild("Info")->addContent("A virtual machine");
661
662 std::list<VirtualSystemDescriptionEntry*> llName = vsdescThis->findByType(VirtualSystemDescriptionType_Name);
663 if (llName.size() != 1)
664 throw setError(VBOX_E_NOT_SUPPORTED,
665 tr("Missing VM name"));
666 Utf8Str &strVMName = llName.front()->strVbox;
667 pelmVirtualSystem->setAttribute("ovf:id", strVMName);
668
669 // product info
670 std::list<VirtualSystemDescriptionEntry*> llProduct = vsdescThis->findByType(VirtualSystemDescriptionType_Product);
671 std::list<VirtualSystemDescriptionEntry*> llProductUrl = vsdescThis->findByType(VirtualSystemDescriptionType_ProductUrl);
672 std::list<VirtualSystemDescriptionEntry*> llVendor = vsdescThis->findByType(VirtualSystemDescriptionType_Vendor);
673 std::list<VirtualSystemDescriptionEntry*> llVendorUrl = vsdescThis->findByType(VirtualSystemDescriptionType_VendorUrl);
674 std::list<VirtualSystemDescriptionEntry*> llVersion = vsdescThis->findByType(VirtualSystemDescriptionType_Version);
675 bool fProduct = llProduct.size() && !llProduct.front()->strVbox.isEmpty();
676 bool fProductUrl = llProductUrl.size() && !llProductUrl.front()->strVbox.isEmpty();
677 bool fVendor = llVendor.size() && !llVendor.front()->strVbox.isEmpty();
678 bool fVendorUrl = llVendorUrl.size() && !llVendorUrl.front()->strVbox.isEmpty();
679 bool fVersion = llVersion.size() && !llVersion.front()->strVbox.isEmpty();
680 if (fProduct ||
681 fProductUrl ||
682 fVersion ||
683 fVendorUrl ||
684 fVersion)
685 {
686 /* <Section ovf:required="false" xsi:type="ovf:ProductSection_Type">
687 <Info>Meta-information about the installed software</Info>
688 <Product>VAtest</Product>
689 <Vendor>SUN Microsystems</Vendor>
690 <Version>10.0</Version>
691 <ProductUrl>http://blogs.sun.com/VirtualGuru</ProductUrl>
692 <VendorUrl>http://www.sun.com</VendorUrl>
693 </Section> */
694 xml::ElementNode *pelmAnnotationSection;
695 if (enFormat == OVF_0_9)
696 {
697 // <Section ovf:required="false" xsi:type="ovf:ProductSection_Type">
698 pelmAnnotationSection = pelmVirtualSystem->createChild("Section");
699 pelmAnnotationSection->setAttribute("xsi:type", "ovf:ProductSection_Type");
700 }
701 else
702 pelmAnnotationSection = pelmVirtualSystem->createChild("ProductSection");
703
704 pelmAnnotationSection->createChild("Info")->addContent("Meta-information about the installed software");
705 if (fProduct)
706 pelmAnnotationSection->createChild("Product")->addContent(llProduct.front()->strVbox);
707 if (fVendor)
708 pelmAnnotationSection->createChild("Vendor")->addContent(llVendor.front()->strVbox);
709 if (fVersion)
710 pelmAnnotationSection->createChild("Version")->addContent(llVersion.front()->strVbox);
711 if (fProductUrl)
712 pelmAnnotationSection->createChild("ProductUrl")->addContent(llProductUrl.front()->strVbox);
713 if (fVendorUrl)
714 pelmAnnotationSection->createChild("VendorUrl")->addContent(llVendorUrl.front()->strVbox);
715 }
716
717 // description
718 std::list<VirtualSystemDescriptionEntry*> llDescription = vsdescThis->findByType(VirtualSystemDescriptionType_Description);
719 if (llDescription.size() &&
720 !llDescription.front()->strVbox.isEmpty())
721 {
722 /* <Section ovf:required="false" xsi:type="ovf:AnnotationSection_Type">
723 <Info>A human-readable annotation</Info>
724 <Annotation>Plan 9</Annotation>
725 </Section> */
726 xml::ElementNode *pelmAnnotationSection;
727 if (enFormat == OVF_0_9)
728 {
729 // <Section ovf:required="false" xsi:type="ovf:AnnotationSection_Type">
730 pelmAnnotationSection = pelmVirtualSystem->createChild("Section");
731 pelmAnnotationSection->setAttribute("xsi:type", "ovf:AnnotationSection_Type");
732 }
733 else
734 pelmAnnotationSection = pelmVirtualSystem->createChild("AnnotationSection");
735
736 pelmAnnotationSection->createChild("Info")->addContent("A human-readable annotation");
737 pelmAnnotationSection->createChild("Annotation")->addContent(llDescription.front()->strVbox);
738 }
739
740 // license
741 std::list<VirtualSystemDescriptionEntry*> llLicense = vsdescThis->findByType(VirtualSystemDescriptionType_License);
742 if (llLicense.size() &&
743 !llLicense.front()->strVbox.isEmpty())
744 {
745 /* <EulaSection>
746 <Info ovf:msgid="6">License agreement for the Virtual System.</Info>
747 <License ovf:msgid="1">License terms can go in here.</License>
748 </EulaSection> */
749 xml::ElementNode *pelmEulaSection;
750 if (enFormat == OVF_0_9)
751 {
752 pelmEulaSection = pelmVirtualSystem->createChild("Section");
753 pelmEulaSection->setAttribute("xsi:type", "ovf:EulaSection_Type");
754 }
755 else
756 pelmEulaSection = pelmVirtualSystem->createChild("EulaSection");
757
758 pelmEulaSection->createChild("Info")->addContent("License agreement for the virtual system");
759 pelmEulaSection->createChild("License")->addContent(llLicense.front()->strVbox);
760 }
761
762 // operating system
763 std::list<VirtualSystemDescriptionEntry*> llOS = vsdescThis->findByType(VirtualSystemDescriptionType_OS);
764 if (llOS.size() != 1)
765 throw setError(VBOX_E_NOT_SUPPORTED,
766 tr("Missing OS type"));
767 /* <OperatingSystemSection ovf:id="82">
768 <Info>Guest Operating System</Info>
769 <Description>Linux 2.6.x</Description>
770 </OperatingSystemSection> */
771 xml::ElementNode *pelmOperatingSystemSection;
772 if (enFormat == OVF_0_9)
773 {
774 pelmOperatingSystemSection = pelmVirtualSystem->createChild("Section");
775 pelmOperatingSystemSection->setAttribute("xsi:type", "ovf:OperatingSystemSection_Type");
776 }
777 else
778 pelmOperatingSystemSection = pelmVirtualSystem->createChild("OperatingSystemSection");
779
780 pelmOperatingSystemSection->setAttribute("ovf:id", llOS.front()->strOvf);
781 pelmOperatingSystemSection->createChild("Info")->addContent("The kind of installed guest operating system");
782 Utf8Str strOSDesc;
783 convertCIMOSType2VBoxOSType(strOSDesc, (ovf::CIMOSType_T)llOS.front()->strOvf.toInt32(), "");
784 pelmOperatingSystemSection->createChild("Description")->addContent(strOSDesc);
785
786 // <VirtualHardwareSection ovf:id="hw1" ovf:transport="iso">
787 xml::ElementNode *pelmVirtualHardwareSection;
788 if (enFormat == OVF_0_9)
789 {
790 // <Section xsi:type="ovf:VirtualHardwareSection_Type">
791 pelmVirtualHardwareSection = pelmVirtualSystem->createChild("Section");
792 pelmVirtualHardwareSection->setAttribute("xsi:type", "ovf:VirtualHardwareSection_Type");
793 }
794 else
795 pelmVirtualHardwareSection = pelmVirtualSystem->createChild("VirtualHardwareSection");
796
797 pelmVirtualHardwareSection->createChild("Info")->addContent("Virtual hardware requirements for a virtual machine");
798
799 /* <System>
800 <vssd:Description>Description of the virtual hardware section.</vssd:Description>
801 <vssd:ElementName>vmware</vssd:ElementName>
802 <vssd:InstanceID>1</vssd:InstanceID>
803 <vssd:VirtualSystemIdentifier>MyLampService</vssd:VirtualSystemIdentifier>
804 <vssd:VirtualSystemType>vmx-4</vssd:VirtualSystemType>
805 </System> */
806 xml::ElementNode *pelmSystem = pelmVirtualHardwareSection->createChild("System");
807
808 pelmSystem->createChild("vssd:ElementName")->addContent("Virtual Hardware Family"); // required OVF 1.0
809
810 // <vssd:InstanceId>0</vssd:InstanceId>
811 if (enFormat == OVF_0_9)
812 pelmSystem->createChild("vssd:InstanceId")->addContent("0");
813 else // capitalization changed...
814 pelmSystem->createChild("vssd:InstanceID")->addContent("0");
815
816 // <vssd:VirtualSystemIdentifier>VAtest</vssd:VirtualSystemIdentifier>
817 pelmSystem->createChild("vssd:VirtualSystemIdentifier")->addContent(strVMName);
818 // <vssd:VirtualSystemType>vmx-4</vssd:VirtualSystemType>
819 const char *pcszHardware = "virtualbox-2.2";
820 if (enFormat == OVF_0_9)
821 // pretend to be vmware compatible then
822 pcszHardware = "vmx-6";
823 pelmSystem->createChild("vssd:VirtualSystemType")->addContent(pcszHardware);
824
825 // loop thru all description entries twice; once to write out all
826 // devices _except_ disk images, and a second time to assign the
827 // disk images; this is because disk images need to reference
828 // IDE controllers, and we can't know their instance IDs without
829 // assigning them first
830
831 uint32_t idIDEController = 0;
832 int32_t lIDEControllerIndex = 0;
833 uint32_t idSATAController = 0;
834 int32_t lSATAControllerIndex = 0;
835 uint32_t idSCSIController = 0;
836 int32_t lSCSIControllerIndex = 0;
837
838 uint32_t ulInstanceID = 1;
839
840 for (size_t uLoop = 1; uLoop <= 2; ++uLoop)
841 {
842 int32_t lIndexThis = 0;
843 list<VirtualSystemDescriptionEntry>::const_iterator itD;
844 for (itD = vsdescThis->m->llDescriptions.begin();
845 itD != vsdescThis->m->llDescriptions.end();
846 ++itD, ++lIndexThis)
847 {
848 const VirtualSystemDescriptionEntry &desc = *itD;
849
850 ovf::ResourceType_T type = (ovf::ResourceType_T)0; // if this becomes != 0 then we do stuff
851 Utf8Str strResourceSubType;
852
853 Utf8Str strDescription; // results in <rasd:Description>...</rasd:Description> block
854 Utf8Str strCaption; // results in <rasd:Caption>...</rasd:Caption> block
855
856 uint32_t ulParent = 0;
857
858 int32_t lVirtualQuantity = -1;
859 Utf8Str strAllocationUnits;
860
861 int32_t lAddress = -1;
862 int32_t lBusNumber = -1;
863 int32_t lAddressOnParent = -1;
864
865 int32_t lAutomaticAllocation = -1; // 0 means "false", 1 means "true"
866 Utf8Str strConnection; // results in <rasd:Connection>...</rasd:Connection> block
867 Utf8Str strHostResource;
868
869 uint64_t uTemp;
870
871 switch (desc.type)
872 {
873 case VirtualSystemDescriptionType_CPU:
874 /* <Item>
875 <rasd:Caption>1 virtual CPU</rasd:Caption>
876 <rasd:Description>Number of virtual CPUs</rasd:Description>
877 <rasd:ElementName>virtual CPU</rasd:ElementName>
878 <rasd:InstanceID>1</rasd:InstanceID>
879 <rasd:ResourceType>3</rasd:ResourceType>
880 <rasd:VirtualQuantity>1</rasd:VirtualQuantity>
881 </Item> */
882 if (uLoop == 1)
883 {
884 strDescription = "Number of virtual CPUs";
885 type = ovf::ResourceType_Processor; // 3
886 desc.strVbox.toInt(uTemp);
887 lVirtualQuantity = (int32_t)uTemp;
888 strCaption = Utf8StrFmt("%d virtual CPU", lVirtualQuantity); // without this ovftool won't eat the item
889 }
890 break;
891
892 case VirtualSystemDescriptionType_Memory:
893 /* <Item>
894 <rasd:AllocationUnits>MegaBytes</rasd:AllocationUnits>
895 <rasd:Caption>256 MB of memory</rasd:Caption>
896 <rasd:Description>Memory Size</rasd:Description>
897 <rasd:ElementName>Memory</rasd:ElementName>
898 <rasd:InstanceID>2</rasd:InstanceID>
899 <rasd:ResourceType>4</rasd:ResourceType>
900 <rasd:VirtualQuantity>256</rasd:VirtualQuantity>
901 </Item> */
902 if (uLoop == 1)
903 {
904 strDescription = "Memory Size";
905 type = ovf::ResourceType_Memory; // 4
906 desc.strVbox.toInt(uTemp);
907 lVirtualQuantity = (int32_t)(uTemp / _1M);
908 strAllocationUnits = "MegaBytes";
909 strCaption = Utf8StrFmt("%d MB of memory", lVirtualQuantity); // without this ovftool won't eat the item
910 }
911 break;
912
913 case VirtualSystemDescriptionType_HardDiskControllerIDE:
914 /* <Item>
915 <rasd:Caption>ideController1</rasd:Caption>
916 <rasd:Description>IDE Controller</rasd:Description>
917 <rasd:InstanceId>5</rasd:InstanceId>
918 <rasd:ResourceType>5</rasd:ResourceType>
919 <rasd:Address>1</rasd:Address>
920 <rasd:BusNumber>1</rasd:BusNumber>
921 </Item> */
922 if (uLoop == 1)
923 {
924 strDescription = "IDE Controller";
925 strCaption = "ideController0";
926 type = ovf::ResourceType_IDEController; // 5
927 strResourceSubType = desc.strVbox;
928 // it seems that OVFTool always writes these two, and since we can only
929 // have one IDE controller, we'll use this as well
930 lAddress = 1;
931 lBusNumber = 1;
932
933 // remember this ID
934 idIDEController = ulInstanceID;
935 lIDEControllerIndex = lIndexThis;
936 }
937 break;
938
939 case VirtualSystemDescriptionType_HardDiskControllerSATA:
940 /* <Item>
941 <rasd:Caption>sataController0</rasd:Caption>
942 <rasd:Description>SATA Controller</rasd:Description>
943 <rasd:InstanceId>4</rasd:InstanceId>
944 <rasd:ResourceType>20</rasd:ResourceType>
945 <rasd:ResourceSubType>ahci</rasd:ResourceSubType>
946 <rasd:Address>0</rasd:Address>
947 <rasd:BusNumber>0</rasd:BusNumber>
948 </Item>
949 */
950 if (uLoop == 1)
951 {
952 strDescription = "SATA Controller";
953 strCaption = "sataController0";
954 type = ovf::ResourceType_OtherStorageDevice; // 20
955 // it seems that OVFTool always writes these two, and since we can only
956 // have one SATA controller, we'll use this as well
957 lAddress = 0;
958 lBusNumber = 0;
959
960 if ( desc.strVbox.isEmpty() // AHCI is the default in VirtualBox
961 || (!desc.strVbox.compare("ahci", Utf8Str::CaseInsensitive))
962 )
963 strResourceSubType = "AHCI";
964 else
965 throw setError(VBOX_E_NOT_SUPPORTED,
966 tr("Invalid config string \"%s\" in SATA controller"), desc.strVbox.c_str());
967
968 // remember this ID
969 idSATAController = ulInstanceID;
970 lSATAControllerIndex = lIndexThis;
971 }
972 break;
973
974 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
975 /* <Item>
976 <rasd:Caption>scsiController0</rasd:Caption>
977 <rasd:Description>SCSI Controller</rasd:Description>
978 <rasd:InstanceId>4</rasd:InstanceId>
979 <rasd:ResourceType>6</rasd:ResourceType>
980 <rasd:ResourceSubType>buslogic</rasd:ResourceSubType>
981 <rasd:Address>0</rasd:Address>
982 <rasd:BusNumber>0</rasd:BusNumber>
983 </Item>
984 */
985 if (uLoop == 1)
986 {
987 strDescription = "SCSI Controller";
988 strCaption = "scsiController0";
989 type = ovf::ResourceType_ParallelSCSIHBA; // 6
990 // it seems that OVFTool always writes these two, and since we can only
991 // have one SATA controller, we'll use this as well
992 lAddress = 0;
993 lBusNumber = 0;
994
995 if ( desc.strVbox.isEmpty() // LsiLogic is the default in VirtualBox
996 || (!desc.strVbox.compare("lsilogic", Utf8Str::CaseInsensitive))
997 )
998 strResourceSubType = "lsilogic";
999 else if (!desc.strVbox.compare("buslogic", Utf8Str::CaseInsensitive))
1000 strResourceSubType = "buslogic";
1001 else
1002 throw setError(VBOX_E_NOT_SUPPORTED,
1003 tr("Invalid config string \"%s\" in SCSI controller"), desc.strVbox.c_str());
1004
1005 // remember this ID
1006 idSCSIController = ulInstanceID;
1007 lSCSIControllerIndex = lIndexThis;
1008 }
1009 break;
1010
1011 case VirtualSystemDescriptionType_HardDiskImage:
1012 /* <Item>
1013 <rasd:Caption>disk1</rasd:Caption>
1014 <rasd:InstanceId>8</rasd:InstanceId>
1015 <rasd:ResourceType>17</rasd:ResourceType>
1016 <rasd:HostResource>/disk/vmdisk1</rasd:HostResource>
1017 <rasd:Parent>4</rasd:Parent>
1018 <rasd:AddressOnParent>0</rasd:AddressOnParent>
1019 </Item> */
1020 if (uLoop == 2)
1021 {
1022 uint32_t cDisks = stack.mapDisks.size();
1023 Utf8Str strDiskID = Utf8StrFmt("vmdisk%RI32", ++cDisks);
1024
1025 strDescription = "Disk Image";
1026 strCaption = Utf8StrFmt("disk%RI32", cDisks); // this is not used for anything else
1027 type = ovf::ResourceType_HardDisk; // 17
1028
1029 // the following references the "<Disks>" XML block
1030 strHostResource = Utf8StrFmt("/disk/%s", strDiskID.c_str());
1031
1032 // controller=<index>;channel=<c>
1033 size_t pos1 = desc.strExtraConfig.find("controller=");
1034 size_t pos2 = desc.strExtraConfig.find("channel=");
1035 if (pos1 != Utf8Str::npos)
1036 {
1037 int32_t lControllerIndex = -1;
1038 RTStrToInt32Ex(desc.strExtraConfig.c_str() + pos1 + 11, NULL, 0, &lControllerIndex);
1039 if (lControllerIndex == lIDEControllerIndex)
1040 ulParent = idIDEController;
1041 else if (lControllerIndex == lSCSIControllerIndex)
1042 ulParent = idSCSIController;
1043 else if (lControllerIndex == lSATAControllerIndex)
1044 ulParent = idSATAController;
1045 }
1046 if (pos2 != Utf8Str::npos)
1047 RTStrToInt32Ex(desc.strExtraConfig.c_str() + pos2 + 8, NULL, 0, &lAddressOnParent);
1048
1049 if ( !ulParent
1050 || lAddressOnParent == -1
1051 )
1052 throw setError(VBOX_E_NOT_SUPPORTED,
1053 tr("Missing or bad extra config string in hard disk image: \"%s\""), desc.strExtraConfig.c_str());
1054
1055 stack.mapDisks[strDiskID] = &desc;
1056 }
1057 break;
1058
1059 case VirtualSystemDescriptionType_Floppy:
1060 if (uLoop == 1)
1061 {
1062 strDescription = "Floppy Drive";
1063 strCaption = "floppy0"; // this is what OVFTool writes
1064 type = ovf::ResourceType_FloppyDrive; // 14
1065 lAutomaticAllocation = 0;
1066 lAddressOnParent = 0; // this is what OVFTool writes
1067 }
1068 break;
1069
1070 case VirtualSystemDescriptionType_CDROM:
1071 if (uLoop == 2)
1072 {
1073 // we can't have a CD without an IDE controller
1074 if (!idIDEController)
1075 throw setError(VBOX_E_NOT_SUPPORTED,
1076 tr("Can't have CD-ROM without IDE controller"));
1077
1078 strDescription = "CD-ROM Drive";
1079 strCaption = "cdrom1"; // this is what OVFTool writes
1080 type = ovf::ResourceType_CDDrive; // 15
1081 lAutomaticAllocation = 1;
1082 ulParent = idIDEController;
1083 lAddressOnParent = 0; // this is what OVFTool writes
1084 }
1085 break;
1086
1087 case VirtualSystemDescriptionType_NetworkAdapter:
1088 /* <Item>
1089 <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
1090 <rasd:Caption>Ethernet adapter on 'VM Network'</rasd:Caption>
1091 <rasd:Connection>VM Network</rasd:Connection>
1092 <rasd:ElementName>VM network</rasd:ElementName>
1093 <rasd:InstanceID>3</rasd:InstanceID>
1094 <rasd:ResourceType>10</rasd:ResourceType>
1095 </Item> */
1096 if (uLoop == 1)
1097 {
1098 lAutomaticAllocation = 1;
1099 strCaption = Utf8StrFmt("Ethernet adapter on '%s'", desc.strOvf.c_str());
1100 type = ovf::ResourceType_EthernetAdapter; // 10
1101 /* Set the hardware type to something useful.
1102 * To be compatible with vmware & others we set
1103 * PCNet32 for our PCNet types & E1000 for the
1104 * E1000 cards. */
1105 switch (desc.strVbox.toInt32())
1106 {
1107 case NetworkAdapterType_Am79C970A:
1108 case NetworkAdapterType_Am79C973: strResourceSubType = "PCNet32"; break;
1109#ifdef VBOX_WITH_E1000
1110 case NetworkAdapterType_I82540EM:
1111 case NetworkAdapterType_I82545EM:
1112 case NetworkAdapterType_I82543GC: strResourceSubType = "E1000"; break;
1113#endif /* VBOX_WITH_E1000 */
1114 }
1115 strConnection = desc.strOvf;
1116
1117 stack.mapNetworks[desc.strOvf] = true;
1118 }
1119 break;
1120
1121 case VirtualSystemDescriptionType_USBController:
1122 /* <Item ovf:required="false">
1123 <rasd:Caption>usb</rasd:Caption>
1124 <rasd:Description>USB Controller</rasd:Description>
1125 <rasd:InstanceId>3</rasd:InstanceId>
1126 <rasd:ResourceType>23</rasd:ResourceType>
1127 <rasd:Address>0</rasd:Address>
1128 <rasd:BusNumber>0</rasd:BusNumber>
1129 </Item> */
1130 if (uLoop == 1)
1131 {
1132 strDescription = "USB Controller";
1133 strCaption = "usb";
1134 type = ovf::ResourceType_USBController; // 23
1135 lAddress = 0; // this is what OVFTool writes
1136 lBusNumber = 0; // this is what OVFTool writes
1137 }
1138 break;
1139
1140 case VirtualSystemDescriptionType_SoundCard:
1141 /* <Item ovf:required="false">
1142 <rasd:Caption>sound</rasd:Caption>
1143 <rasd:Description>Sound Card</rasd:Description>
1144 <rasd:InstanceId>10</rasd:InstanceId>
1145 <rasd:ResourceType>35</rasd:ResourceType>
1146 <rasd:ResourceSubType>ensoniq1371</rasd:ResourceSubType>
1147 <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
1148 <rasd:AddressOnParent>3</rasd:AddressOnParent>
1149 </Item> */
1150 if (uLoop == 1)
1151 {
1152 strDescription = "Sound Card";
1153 strCaption = "sound";
1154 type = ovf::ResourceType_SoundCard; // 35
1155 strResourceSubType = desc.strOvf; // e.g. ensoniq1371
1156 lAutomaticAllocation = 0;
1157 lAddressOnParent = 3; // what gives? this is what OVFTool writes
1158 }
1159 break;
1160 }
1161
1162 if (type)
1163 {
1164 xml::ElementNode *pItem;
1165
1166 pItem = pelmVirtualHardwareSection->createChild("Item");
1167
1168 // NOTE: do not change the order of these items without good reason! While we don't care
1169 // about ordering, VMware's ovftool does and fails if the items are not written in
1170 // exactly this order, as stupid as it seems.
1171
1172 if (!strCaption.isEmpty())
1173 {
1174 pItem->createChild("rasd:Caption")->addContent(strCaption);
1175 if (enFormat == OVF_1_0)
1176 pItem->createChild("rasd:ElementName")->addContent(strCaption);
1177 }
1178
1179 if (!strDescription.isEmpty())
1180 pItem->createChild("rasd:Description")->addContent(strDescription);
1181
1182 // <rasd:InstanceID>1</rasd:InstanceID>
1183 xml::ElementNode *pelmInstanceID;
1184 if (enFormat == OVF_0_9)
1185 pelmInstanceID = pItem->createChild("rasd:InstanceId");
1186 else
1187 pelmInstanceID = pItem->createChild("rasd:InstanceID"); // capitalization changed...
1188 pelmInstanceID->addContent(Utf8StrFmt("%d", ulInstanceID++));
1189
1190 // <rasd:ResourceType>3</rasd:ResourceType>
1191 pItem->createChild("rasd:ResourceType")->addContent(Utf8StrFmt("%d", type));
1192 if (!strResourceSubType.isEmpty())
1193 pItem->createChild("rasd:ResourceSubType")->addContent(strResourceSubType);
1194
1195 if (!strHostResource.isEmpty())
1196 pItem->createChild("rasd:HostResource")->addContent(strHostResource);
1197
1198 if (!strAllocationUnits.isEmpty())
1199 pItem->createChild("rasd:AllocationUnits")->addContent(strAllocationUnits);
1200
1201 // <rasd:VirtualQuantity>1</rasd:VirtualQuantity>
1202 if (lVirtualQuantity != -1)
1203 pItem->createChild("rasd:VirtualQuantity")->addContent(Utf8StrFmt("%d", lVirtualQuantity));
1204
1205 if (lAutomaticAllocation != -1)
1206 pItem->createChild("rasd:AutomaticAllocation")->addContent( (lAutomaticAllocation) ? "true" : "false" );
1207
1208 if (!strConnection.isEmpty())
1209 pItem->createChild("rasd:Connection")->addContent(strConnection);
1210
1211 if (lAddress != -1)
1212 pItem->createChild("rasd:Address")->addContent(Utf8StrFmt("%d", lAddress));
1213
1214 if (lBusNumber != -1)
1215 if (enFormat == OVF_0_9) // BusNumber is invalid OVF 1.0 so only write it in 0.9 mode for OVFTool compatibility
1216 pItem->createChild("rasd:BusNumber")->addContent(Utf8StrFmt("%d", lBusNumber));
1217
1218 if (ulParent)
1219 pItem->createChild("rasd:Parent")->addContent(Utf8StrFmt("%d", ulParent));
1220 if (lAddressOnParent != -1)
1221 pItem->createChild("rasd:AddressOnParent")->addContent(Utf8StrFmt("%d", lAddressOnParent));
1222 }
1223 }
1224 } // for (size_t uLoop = 1; uLoop <= 2; ++uLoop)
1225
1226 // now that we're done with the official OVF <Item> tags under <VirtualSystem>, write out VirtualBox XML
1227 // under the vbox: namespace
1228 xml::ElementNode *pelmVBoxMachine = pelmVirtualSystem->createChild("vbox:Machine");
1229 settings::MachineConfigFile *pConfig = new settings::MachineConfigFile(NULL);
1230
1231 try
1232 {
1233 AutoWriteLock machineLock(vsdescThis->m->pMachine COMMA_LOCKVAL_SRC_POS);
1234 vsdescThis->m->pMachine->copyMachineDataToSettings(*pConfig);
1235 pConfig->buildMachineXML(*pelmVBoxMachine,
1236 false /* fIncludeSnapshots */);
1237 delete pConfig;
1238 }
1239 catch (...)
1240 {
1241 delete pConfig;
1242 throw;
1243 }
1244}
1245
1246/**
1247 * Actual worker code for writing out OVF to disk. This is called from Appliance::taskThreadWriteOVF()
1248 * and therefore runs on the OVF write worker thread. This runs in two contexts:
1249 *
1250 * 1) in a first worker thread; in that case, Appliance::Write() called Appliance::writeImpl();
1251 *
1252 * 2) in a second worker thread; in that case, Appliance::Write() called Appliance::writeImpl(), which
1253 * called Appliance::writeS3(), which called Appliance::writeImpl(), which then called this. In other
1254 * words, to write to the cloud, the first worker thread first starts a second worker thread to create
1255 * temporary files and then uploads them to the S3 cloud server.
1256 *
1257 * @param pTask
1258 * @return
1259 */
1260HRESULT Appliance::writeFS(const LocationInfo &locInfo, const OVFFormat enFormat, ComObjPtr<Progress> &pProgress)
1261{
1262 LogFlowFuncEnter();
1263 LogFlowFunc(("Appliance %p\n", this));
1264
1265 AutoCaller autoCaller(this);
1266 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1267
1268 HRESULT rc = S_OK;
1269
1270 try
1271 {
1272 AutoMultiWriteLock2 multiLock(&mVirtualBox->getMediaTreeLockHandle(), this->lockHandle() COMMA_LOCKVAL_SRC_POS);
1273
1274 xml::Document doc;
1275 xml::ElementNode *pelmRoot = doc.createRootElement("Envelope");
1276
1277 pelmRoot->setAttribute("ovf:version", (enFormat == OVF_1_0) ? "1.0" : "0.9");
1278 pelmRoot->setAttribute("xml:lang", "en-US");
1279
1280 Utf8Str strNamespace = (enFormat == OVF_0_9)
1281 ? "http://www.vmware.com/schema/ovf/1/envelope" // 0.9
1282 : "http://schemas.dmtf.org/ovf/envelope/1"; // 1.0
1283 pelmRoot->setAttribute("xmlns", strNamespace);
1284 pelmRoot->setAttribute("xmlns:ovf", strNamespace);
1285
1286// pelmRoot->setAttribute("xmlns:ovfstr", "http://schema.dmtf.org/ovf/strings/1");
1287 pelmRoot->setAttribute("xmlns:rasd", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData");
1288 pelmRoot->setAttribute("xmlns:vssd", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData");
1289 pelmRoot->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
1290 pelmRoot->setAttribute("xmlns:vbox", "http://www.virtualbox.org/ovf/machine");
1291// pelmRoot->setAttribute("xsi:schemaLocation", "http://schemas.dmtf.org/ovf/envelope/1 ../ovf-envelope.xsd");
1292
1293 // <Envelope>/<References>
1294 xml::ElementNode *pelmReferences = pelmRoot->createChild("References"); // 0.9 and 1.0
1295
1296 /* <Envelope>/<DiskSection>:
1297 <DiskSection>
1298 <Info>List of the virtual disks used in the package</Info>
1299 <Disk ovf:capacity="4294967296" ovf:diskId="lamp" ovf:format="http://www.vmware.com/specifications/vmdk.html#compressed" ovf:populatedSize="1924967692"/>
1300 </DiskSection> */
1301 xml::ElementNode *pelmDiskSection;
1302 if (enFormat == OVF_0_9)
1303 {
1304 // <Section xsi:type="ovf:DiskSection_Type">
1305 pelmDiskSection = pelmRoot->createChild("Section");
1306 pelmDiskSection->setAttribute("xsi:type", "ovf:DiskSection_Type");
1307 }
1308 else
1309 pelmDiskSection = pelmRoot->createChild("DiskSection");
1310
1311 xml::ElementNode *pelmDiskSectionInfo = pelmDiskSection->createChild("Info");
1312 pelmDiskSectionInfo->addContent("List of the virtual disks used in the package");
1313
1314 // the XML stack contains two maps for disks and networks, which allows us to
1315 // a) have a list of unique disk names (to make sure the same disk name is only added once)
1316 // and b) keep a list of all networks
1317 XMLStack stack;
1318
1319 /* <Envelope>/<NetworkSection>:
1320 <NetworkSection>
1321 <Info>Logical networks used in the package</Info>
1322 <Network ovf:name="VM Network">
1323 <Description>The network that the LAMP Service will be available on</Description>
1324 </Network>
1325 </NetworkSection> */
1326 xml::ElementNode *pelmNetworkSection;
1327 if (enFormat == OVF_0_9)
1328 {
1329 // <Section xsi:type="ovf:NetworkSection_Type">
1330 pelmNetworkSection = pelmRoot->createChild("Section");
1331 pelmNetworkSection->setAttribute("xsi:type", "ovf:NetworkSection_Type");
1332 }
1333 else
1334 pelmNetworkSection = pelmRoot->createChild("NetworkSection");
1335
1336 xml::ElementNode *pelmNetworkSectionInfo = pelmNetworkSection->createChild("Info");
1337 pelmNetworkSectionInfo->addContent("Logical networks used in the package");
1338
1339 // and here come the virtual systems:
1340
1341 // This can take a very long time so leave the locks; in particular, we have the media tree
1342 // lock which Medium::CloneTo() will request, and that would deadlock. Instead, protect
1343 // the appliance by resetting its state so we can safely leave the lock
1344 m->state = Data::ApplianceExporting;
1345 multiLock.release();
1346
1347 // write a collection if we have more than one virtual system _and_ we're
1348 // writing OVF 1.0; otherwise fail since ovftool can't import more than
1349 // one machine, it seems
1350 xml::ElementNode *pelmToAddVirtualSystemsTo;
1351 if (m->virtualSystemDescriptions.size() > 1)
1352 {
1353 if (enFormat == OVF_0_9)
1354 throw setError(VBOX_E_FILE_ERROR,
1355 tr("Cannot export more than one virtual system with OVF 0.9, use OVF 1.0"));
1356
1357 pelmToAddVirtualSystemsTo = pelmRoot->createChild("VirtualSystemCollection");
1358 pelmToAddVirtualSystemsTo->setAttribute("ovf:name", "ExportedVirtualBoxMachines"); // whatever
1359 }
1360 else
1361 pelmToAddVirtualSystemsTo = pelmRoot; // add virtual system directly under root element
1362
1363 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it;
1364 /* Iterate throughs all virtual systems of that appliance */
1365 for (it = m->virtualSystemDescriptions.begin();
1366 it != m->virtualSystemDescriptions.end();
1367 ++it)
1368 {
1369 ComObjPtr<VirtualSystemDescription> vsdescThis = *it;
1370 buildXMLForOneVirtualSystem(*pelmToAddVirtualSystemsTo,
1371 vsdescThis,
1372 enFormat,
1373 stack); // disks and networks stack
1374 }
1375
1376 // now, fill in the network section we set up empty above according
1377 // to the networks we found with the hardware items
1378 map<Utf8Str, bool>::const_iterator itN;
1379 for (itN = stack.mapNetworks.begin();
1380 itN != stack.mapNetworks.end();
1381 ++itN)
1382 {
1383 const Utf8Str &strNetwork = itN->first;
1384 xml::ElementNode *pelmNetwork = pelmNetworkSection->createChild("Network");
1385 pelmNetwork->setAttribute("ovf:name", strNetwork.c_str());
1386 pelmNetwork->createChild("Description")->addContent("Logical network used by this appliance.");
1387 }
1388
1389 // Finally, write out the disks!
1390
1391 list<Utf8Str> diskList;
1392 map<Utf8Str, const VirtualSystemDescriptionEntry*>::const_iterator itS;
1393 uint32_t ulFile = 1;
1394 for (itS = stack.mapDisks.begin();
1395 itS != stack.mapDisks.end();
1396 ++itS)
1397 {
1398 const Utf8Str &strDiskID = itS->first;
1399 const VirtualSystemDescriptionEntry *pDiskEntry = itS->second;
1400
1401 // source path: where the VBox image is
1402 const Utf8Str &strSrcFilePath = pDiskEntry->strVbox;
1403 Bstr bstrSrcFilePath(strSrcFilePath);
1404 if (!RTPathExists(strSrcFilePath.c_str()))
1405 /* This isn't allowed */
1406 throw setError(VBOX_E_FILE_ERROR,
1407 tr("Source virtual disk image file '%s' doesn't exist"),
1408 strSrcFilePath.c_str());
1409
1410 // clone the disk:
1411 ComPtr<IMedium> pSourceDisk;
1412 ComPtr<IMedium> pTargetDisk;
1413 ComPtr<IProgress> pProgress2;
1414
1415 Log(("Finding source disk \"%ls\"\n", bstrSrcFilePath.raw()));
1416 rc = mVirtualBox->FindHardDisk(bstrSrcFilePath, pSourceDisk.asOutParam());
1417 if (FAILED(rc)) throw rc;
1418
1419 Bstr uuidSource;
1420 rc = pSourceDisk->COMGETTER(Id)(uuidSource.asOutParam());
1421 if (FAILED(rc)) throw rc;
1422 Guid guidSource(uuidSource);
1423
1424 // output filename
1425 const Utf8Str &strTargetFileNameOnly = pDiskEntry->strOvf;
1426 // target path needs to be composed from where the output OVF is
1427 Utf8Str strTargetFilePath(locInfo.strPath);
1428 strTargetFilePath.stripFilename();
1429 strTargetFilePath.append("/");
1430 strTargetFilePath.append(strTargetFileNameOnly);
1431
1432 // We are always exporting to VMDK stream optimized for now
1433 Bstr bstrSrcFormat = L"VMDK";
1434
1435 // create a new hard disk interface for the destination disk image
1436 Log(("Creating target disk \"%s\"\n", strTargetFilePath.raw()));
1437 rc = mVirtualBox->CreateHardDisk(bstrSrcFormat, Bstr(strTargetFilePath), pTargetDisk.asOutParam());
1438 if (FAILED(rc)) throw rc;
1439
1440 // the target disk is now registered and needs to be removed again,
1441 // both after successful cloning or if anything goes bad!
1442 try
1443 {
1444 // create a flat copy of the source disk image
1445 rc = pSourceDisk->CloneTo(pTargetDisk, MediumVariant_VmdkStreamOptimized, NULL, pProgress2.asOutParam());
1446 if (FAILED(rc)) throw rc;
1447
1448 // advance to the next operation
1449 if (!pProgress.isNull())
1450 pProgress->SetNextOperation(BstrFmt(tr("Exporting to disk image '%s'"), strTargetFilePath.c_str()),
1451 pDiskEntry->ulSizeMB); // operation's weight, as set up with the IProgress originally);
1452
1453 // now wait for the background disk operation to complete; this throws HRESULTs on error
1454 waitForAsyncProgress(pProgress, pProgress2);
1455 }
1456 catch (HRESULT rc3)
1457 {
1458 // upon error after registering, close the disk or
1459 // it'll stick in the registry forever
1460 pTargetDisk->Close();
1461 throw rc3;
1462 }
1463 diskList.push_back(strTargetFilePath);
1464
1465 // we need the following for the XML
1466 uint64_t cbFile = 0; // actual file size
1467 rc = pTargetDisk->COMGETTER(Size)(&cbFile);
1468 if (FAILED(rc)) throw rc;
1469
1470 ULONG64 cbCapacity = 0; // size reported to guest
1471 rc = pTargetDisk->COMGETTER(LogicalSize)(&cbCapacity);
1472 if (FAILED(rc)) throw rc;
1473 // capacity is reported in megabytes, so...
1474 cbCapacity *= _1M;
1475
1476 // upon success, close the disk as well
1477 rc = pTargetDisk->Close();
1478 if (FAILED(rc)) throw rc;
1479
1480 // now handle the XML for the disk:
1481 Utf8StrFmt strFileRef("file%RI32", ulFile++);
1482 // <File ovf:href="WindowsXpProfessional-disk1.vmdk" ovf:id="file1" ovf:size="1710381056"/>
1483 xml::ElementNode *pelmFile = pelmReferences->createChild("File");
1484 pelmFile->setAttribute("ovf:href", strTargetFileNameOnly);
1485 pelmFile->setAttribute("ovf:id", strFileRef);
1486 pelmFile->setAttribute("ovf:size", Utf8StrFmt("%RI64", cbFile).c_str());
1487
1488 // add disk to XML Disks section
1489 // <Disk ovf:capacity="8589934592" ovf:diskId="vmdisk1" ovf:fileRef="file1" ovf:format="http://www.vmware.com/specifications/vmdk.html#sparse"/>
1490 xml::ElementNode *pelmDisk = pelmDiskSection->createChild("Disk");
1491 pelmDisk->setAttribute("ovf:capacity", Utf8StrFmt("%RI64", cbCapacity).c_str());
1492 pelmDisk->setAttribute("ovf:diskId", strDiskID);
1493 pelmDisk->setAttribute("ovf:fileRef", strFileRef);
1494 pelmDisk->setAttribute("ovf:format", "http://www.vmware.com/specifications/vmdk.html#sparse"); // must be sparse or ovftool chokes
1495 pelmDisk->setAttribute("vbox:uuid", Utf8StrFmt("%RTuuid", guidSource.raw()).c_str());
1496 }
1497
1498 // now go write the XML
1499 xml::XmlFileWriter writer(doc);
1500 writer.write(locInfo.strPath.c_str());
1501
1502 /* Create & write the manifest file */
1503 const char** ppManifestFiles = (const char**)RTMemAlloc(sizeof(char*)*diskList.size() + 1);
1504 ppManifestFiles[0] = locInfo.strPath.c_str();
1505 list<Utf8Str>::const_iterator it1;
1506 size_t i = 1;
1507 for (it1 = diskList.begin();
1508 it1 != diskList.end();
1509 ++it1, ++i)
1510 ppManifestFiles[i] = (*it1).c_str();
1511 Utf8Str strMfFile = manifestFileName(locInfo.strPath.c_str());
1512 int vrc = RTManifestWriteFiles(strMfFile.c_str(), ppManifestFiles, diskList.size()+1);
1513 RTMemFree(ppManifestFiles);
1514 if (RT_FAILURE(vrc))
1515 throw setError(VBOX_E_FILE_ERROR,
1516 tr("Couldn't create manifest file '%s' (%Rrc)"),
1517 RTPathFilename(strMfFile.c_str()), vrc);
1518 }
1519 catch(xml::Error &x)
1520 {
1521 rc = setError(VBOX_E_FILE_ERROR,
1522 x.what());
1523 }
1524 catch(HRESULT aRC)
1525 {
1526 rc = aRC;
1527 }
1528
1529 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
1530 // reset the state so others can call methods again
1531 m->state = Data::ApplianceIdle;
1532
1533 LogFlowFunc(("rc=%Rhrc\n", rc));
1534 LogFlowFuncLeave();
1535
1536 return rc;
1537}
1538
1539/**
1540 * Worker code for writing out OVF to the cloud. This is called from Appliance::taskThreadWriteOVF()
1541 * in S3 mode and therefore runs on the OVF write worker thread. This then starts a second worker
1542 * thread to create temporary files (see Appliance::writeFS()).
1543 *
1544 * @param pTask
1545 * @return
1546 */
1547HRESULT Appliance::writeS3(TaskOVF *pTask)
1548{
1549 LogFlowFuncEnter();
1550 LogFlowFunc(("Appliance %p\n", this));
1551
1552 AutoCaller autoCaller(this);
1553 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1554
1555 HRESULT rc = S_OK;
1556
1557 AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
1558
1559 int vrc = VINF_SUCCESS;
1560 RTS3 hS3 = NIL_RTS3;
1561 char szOSTmpDir[RTPATH_MAX];
1562 RTPathTemp(szOSTmpDir, sizeof(szOSTmpDir));
1563 /* The template for the temporary directory created below */
1564 char *pszTmpDir;
1565 RTStrAPrintf(&pszTmpDir, "%s"RTPATH_SLASH_STR"vbox-ovf-XXXXXX", szOSTmpDir);
1566 list< pair<Utf8Str, ULONG> > filesList;
1567
1568 // todo:
1569 // - usable error codes
1570 // - seems snapshot filenames are problematic {uuid}.vdi
1571 try
1572 {
1573 /* Extract the bucket */
1574 Utf8Str tmpPath = pTask->locInfo.strPath;
1575 Utf8Str bucket;
1576 parseBucket(tmpPath, bucket);
1577
1578 /* We need a temporary directory which we can put the OVF file & all
1579 * disk images in */
1580 vrc = RTDirCreateTemp(pszTmpDir);
1581 if (RT_FAILURE(vrc))
1582 throw setError(VBOX_E_FILE_ERROR,
1583 tr("Cannot create temporary directory '%s'"), pszTmpDir);
1584
1585 /* The temporary name of the target OVF file */
1586 Utf8StrFmt strTmpOvf("%s/%s", pszTmpDir, RTPathFilename(tmpPath.c_str()));
1587
1588 /* Prepare the temporary writing of the OVF */
1589 ComObjPtr<Progress> progress;
1590 /* Create a temporary file based location info for the sub task */
1591 LocationInfo li;
1592 li.strPath = strTmpOvf;
1593 rc = writeImpl(pTask->enFormat, li, progress);
1594 if (FAILED(rc)) throw rc;
1595
1596 /* Unlock the appliance for the writing thread */
1597 appLock.release();
1598 /* Wait until the writing is done, but report the progress back to the
1599 caller */
1600 ComPtr<IProgress> progressInt(progress);
1601 waitForAsyncProgress(pTask->pProgress, progressInt); /* Any errors will be thrown */
1602
1603 /* Again lock the appliance for the next steps */
1604 appLock.acquire();
1605
1606 vrc = RTPathExists(strTmpOvf.c_str()); /* Paranoid check */
1607 if (RT_FAILURE(vrc))
1608 throw setError(VBOX_E_FILE_ERROR,
1609 tr("Cannot find source file '%s'"), strTmpOvf.c_str());
1610 /* Add the OVF file */
1611 filesList.push_back(pair<Utf8Str, ULONG>(strTmpOvf, m->ulWeightPerOperation)); /* Use 1% of the total for the OVF file upload */
1612 Utf8Str strMfFile = manifestFileName(strTmpOvf);
1613 filesList.push_back(pair<Utf8Str, ULONG>(strMfFile , m->ulWeightPerOperation)); /* Use 1% of the total for the manifest file upload */
1614
1615 /* Now add every disks of every virtual system */
1616 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it;
1617 for (it = m->virtualSystemDescriptions.begin();
1618 it != m->virtualSystemDescriptions.end();
1619 ++it)
1620 {
1621 ComObjPtr<VirtualSystemDescription> vsdescThis = (*it);
1622 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
1623 std::list<VirtualSystemDescriptionEntry*>::const_iterator itH;
1624 for (itH = avsdeHDs.begin();
1625 itH != avsdeHDs.end();
1626 ++itH)
1627 {
1628 const Utf8Str &strTargetFileNameOnly = (*itH)->strOvf;
1629 /* Target path needs to be composed from where the output OVF is */
1630 Utf8Str strTargetFilePath(strTmpOvf);
1631 strTargetFilePath.stripFilename();
1632 strTargetFilePath.append("/");
1633 strTargetFilePath.append(strTargetFileNameOnly);
1634 vrc = RTPathExists(strTargetFilePath.c_str()); /* Paranoid check */
1635 if (RT_FAILURE(vrc))
1636 throw setError(VBOX_E_FILE_ERROR,
1637 tr("Cannot find source file '%s'"), strTargetFilePath.c_str());
1638 filesList.push_back(pair<Utf8Str, ULONG>(strTargetFilePath, (*itH)->ulSizeMB));
1639 }
1640 }
1641 /* Next we have to upload the OVF & all disk images */
1642 vrc = RTS3Create(&hS3, pTask->locInfo.strUsername.c_str(), pTask->locInfo.strPassword.c_str(), pTask->locInfo.strHostname.c_str(), "virtualbox-agent/"VBOX_VERSION_STRING);
1643 if (RT_FAILURE(vrc))
1644 throw setError(VBOX_E_IPRT_ERROR,
1645 tr("Cannot create S3 service handler"));
1646 RTS3SetProgressCallback(hS3, pTask->updateProgress, &pTask);
1647
1648 /* Upload all files */
1649 for (list< pair<Utf8Str, ULONG> >::const_iterator it1 = filesList.begin(); it1 != filesList.end(); ++it1)
1650 {
1651 const pair<Utf8Str, ULONG> &s = (*it1);
1652 char *pszFilename = RTPathFilename(s.first.c_str());
1653 /* Advance to the next operation */
1654 if (!pTask->pProgress.isNull())
1655 pTask->pProgress->SetNextOperation(BstrFmt(tr("Uploading file '%s'"), pszFilename), s.second);
1656 vrc = RTS3PutKey(hS3, bucket.c_str(), pszFilename, s.first.c_str());
1657 if (RT_FAILURE(vrc))
1658 {
1659 if (vrc == VERR_S3_CANCELED)
1660 break;
1661 else if (vrc == VERR_S3_ACCESS_DENIED)
1662 throw setError(E_ACCESSDENIED,
1663 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);
1664 else if (vrc == VERR_S3_NOT_FOUND)
1665 throw setError(VBOX_E_FILE_ERROR,
1666 tr("Cannot upload file '%s' to S3 storage server (File not found)"), pszFilename);
1667 else
1668 throw setError(VBOX_E_IPRT_ERROR,
1669 tr("Cannot upload file '%s' to S3 storage server (%Rrc)"), pszFilename, vrc);
1670 }
1671 }
1672 }
1673 catch(HRESULT aRC)
1674 {
1675 rc = aRC;
1676 }
1677 /* Cleanup */
1678 RTS3Destroy(hS3);
1679 /* Delete all files which where temporary created */
1680 for (list< pair<Utf8Str, ULONG> >::const_iterator it1 = filesList.begin(); it1 != filesList.end(); ++it1)
1681 {
1682 const char *pszFilePath = (*it1).first.c_str();
1683 if (RTPathExists(pszFilePath))
1684 {
1685 vrc = RTFileDelete(pszFilePath);
1686 if (RT_FAILURE(vrc))
1687 rc = setError(VBOX_E_FILE_ERROR,
1688 tr("Cannot delete file '%s' (%Rrc)"), pszFilePath, vrc);
1689 }
1690 }
1691 /* Delete the temporary directory */
1692 if (RTPathExists(pszTmpDir))
1693 {
1694 vrc = RTDirRemove(pszTmpDir);
1695 if (RT_FAILURE(vrc))
1696 rc = setError(VBOX_E_FILE_ERROR,
1697 tr("Cannot delete temporary directory '%s' (%Rrc)"), pszTmpDir, vrc);
1698 }
1699 if (pszTmpDir)
1700 RTStrFree(pszTmpDir);
1701
1702 LogFlowFunc(("rc=%Rhrc\n", rc));
1703 LogFlowFuncLeave();
1704
1705 return rc;
1706}
1707
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