VirtualBox

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

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

Main/OVF: plug memory leak

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