VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageAppliance.cpp@ 81156

Last change on this file since 81156 was 81086, checked in by vboxsync, 5 years ago

bugref:9555. Fixed error: enumeration value 'VirtualSystemDescriptionType_CloudBootVolumeId' not handled in switch.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 87.6 KB
Line 
1/* $Id: VBoxManageAppliance.cpp 81086 2019-09-30 18:33:56Z vboxsync $ */
2/** @file
3 * VBoxManage - The appliance-related commands.
4 */
5
6/*
7 * Copyright (C) 2009-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef VBOX_ONLY_DOCS
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#ifndef VBOX_ONLY_DOCS
25#include <VBox/com/com.h>
26#include <VBox/com/string.h>
27#include <VBox/com/Guid.h>
28#include <VBox/com/array.h>
29#include <VBox/com/ErrorInfo.h>
30#include <VBox/com/errorprint.h>
31#include <VBox/com/VirtualBox.h>
32
33#include <list>
34#include <map>
35#endif /* !VBOX_ONLY_DOCS */
36
37#include <iprt/stream.h>
38#include <iprt/getopt.h>
39#include <iprt/ctype.h>
40#include <iprt/path.h>
41#include <iprt/file.h>
42
43#include <VBox/log.h>
44#include <VBox/param.h>
45
46#include "VBoxManage.h"
47using namespace com;
48
49
50// funcs
51///////////////////////////////////////////////////////////////////////////////
52
53typedef std::map<Utf8Str, Utf8Str> ArgsMap; // pairs of strings like "vmname" => "newvmname"
54typedef std::map<uint32_t, ArgsMap> ArgsMapsMap; // map of maps, one for each virtual system, sorted by index
55
56typedef std::map<uint32_t, bool> IgnoresMap; // pairs of numeric description entry indices
57typedef std::map<uint32_t, IgnoresMap> IgnoresMapsMap; // map of maps, one for each virtual system, sorted by index
58
59static bool findArgValue(Utf8Str &strOut,
60 ArgsMap *pmapArgs,
61 const Utf8Str &strKey)
62{
63 if (pmapArgs)
64 {
65 ArgsMap::iterator it;
66 it = pmapArgs->find(strKey);
67 if (it != pmapArgs->end())
68 {
69 strOut = it->second;
70 pmapArgs->erase(it);
71 return true;
72 }
73 }
74
75 return false;
76}
77
78static int parseImportOptions(const char *psz, com::SafeArray<ImportOptions_T> *options)
79{
80 int rc = VINF_SUCCESS;
81 while (psz && *psz && RT_SUCCESS(rc))
82 {
83 size_t len;
84 const char *pszComma = strchr(psz, ',');
85 if (pszComma)
86 len = pszComma - psz;
87 else
88 len = strlen(psz);
89 if (len > 0)
90 {
91 if (!RTStrNICmp(psz, "KeepAllMACs", len))
92 options->push_back(ImportOptions_KeepAllMACs);
93 else if (!RTStrNICmp(psz, "KeepNATMACs", len))
94 options->push_back(ImportOptions_KeepNATMACs);
95 else if (!RTStrNICmp(psz, "ImportToVDI", len))
96 options->push_back(ImportOptions_ImportToVDI);
97 else
98 rc = VERR_PARSE_ERROR;
99 }
100 if (pszComma)
101 psz += len + 1;
102 else
103 psz += len;
104 }
105
106 return rc;
107}
108
109static const RTGETOPTDEF g_aImportApplianceOptions[] =
110{
111 { "--dry-run", 'n', RTGETOPT_REQ_NOTHING },
112 { "-dry-run", 'n', RTGETOPT_REQ_NOTHING }, // deprecated
113 { "--dryrun", 'n', RTGETOPT_REQ_NOTHING },
114 { "-dryrun", 'n', RTGETOPT_REQ_NOTHING }, // deprecated
115 { "--detailed-progress", 'P', RTGETOPT_REQ_NOTHING },
116 { "-detailed-progress", 'P', RTGETOPT_REQ_NOTHING }, // deprecated
117 { "--vsys", 's', RTGETOPT_REQ_UINT32 },
118 { "-vsys", 's', RTGETOPT_REQ_UINT32 }, // deprecated
119 { "--ostype", 'o', RTGETOPT_REQ_STRING },
120 { "-ostype", 'o', RTGETOPT_REQ_STRING }, // deprecated
121 { "--vmname", 'V', RTGETOPT_REQ_STRING },
122 { "-vmname", 'V', RTGETOPT_REQ_STRING }, // deprecated
123 { "--settingsfile", 'S', RTGETOPT_REQ_STRING },
124 { "--basefolder", 'p', RTGETOPT_REQ_STRING },
125 { "--group", 'g', RTGETOPT_REQ_STRING },
126 { "--memory", 'm', RTGETOPT_REQ_STRING },
127 { "-memory", 'm', RTGETOPT_REQ_STRING }, // deprecated
128 { "--cpus", 'c', RTGETOPT_REQ_STRING },
129 { "--description", 'd', RTGETOPT_REQ_STRING },
130 { "--eula", 'L', RTGETOPT_REQ_STRING },
131 { "-eula", 'L', RTGETOPT_REQ_STRING }, // deprecated
132 { "--unit", 'u', RTGETOPT_REQ_UINT32 },
133 { "-unit", 'u', RTGETOPT_REQ_UINT32 }, // deprecated
134 { "--ignore", 'x', RTGETOPT_REQ_NOTHING },
135 { "-ignore", 'x', RTGETOPT_REQ_NOTHING }, // deprecated
136 { "--scsitype", 'T', RTGETOPT_REQ_UINT32 },
137 { "-scsitype", 'T', RTGETOPT_REQ_UINT32 }, // deprecated
138 { "--type", 'T', RTGETOPT_REQ_UINT32 }, // deprecated
139 { "-type", 'T', RTGETOPT_REQ_UINT32 }, // deprecated
140#if 0 /* Changing the controller is fully valid, but the current design on how
141 the params are evaluated here doesn't allow two parameter for one
142 unit. The target disk path is more important. I leave it for future
143 improvments. */
144 { "--controller", 'C', RTGETOPT_REQ_STRING },
145#endif
146 { "--disk", 'D', RTGETOPT_REQ_STRING },
147 { "--options", 'O', RTGETOPT_REQ_STRING },
148
149 { "--cloud", 'j', RTGETOPT_REQ_NOTHING},
150 { "--cloudprofile", 'k', RTGETOPT_REQ_STRING },
151 { "--cloudinstanceid", 'l', RTGETOPT_REQ_STRING },
152 { "--cloudbucket", 'B', RTGETOPT_REQ_STRING }
153};
154
155enum actionType
156{
157 NOT_SET, LOCAL, CLOUD
158} actionType;
159
160RTEXITCODE handleImportAppliance(HandlerArg *arg)
161{
162 HRESULT rc = S_OK;
163 bool fCloud = false; // the default
164 actionType = NOT_SET;
165 Utf8Str strOvfFilename;
166 bool fExecute = true; // if true, then we actually do the import
167 com::SafeArray<ImportOptions_T> options;
168 uint32_t ulCurVsys = (uint32_t)-1;
169 uint32_t ulCurUnit = (uint32_t)-1;
170 // for each --vsys X command, maintain a map of command line items
171 // (we'll parse them later after interpreting the OVF, when we can
172 // actually check whether they make sense semantically)
173 ArgsMapsMap mapArgsMapsPerVsys;
174 IgnoresMapsMap mapIgnoresMapsPerVsys;
175
176 int c;
177 RTGETOPTUNION ValueUnion;
178 RTGETOPTSTATE GetState;
179 // start at 0 because main() has hacked both the argc and argv given to us
180 RTGetOptInit(&GetState, arg->argc, arg->argv, g_aImportApplianceOptions, RT_ELEMENTS(g_aImportApplianceOptions),
181 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
182 while ((c = RTGetOpt(&GetState, &ValueUnion)))
183 {
184 switch (c)
185 {
186 case 'n': // --dry-run
187 fExecute = false;
188 break;
189
190 case 'P': // --detailed-progress
191 g_fDetailedProgress = true;
192 break;
193
194 case 's': // --vsys
195 if (fCloud == false && actionType == NOT_SET)
196 actionType = LOCAL;
197
198 if (actionType != LOCAL)
199 return errorSyntax(USAGE_EXPORTAPPLIANCE,
200 "Option \"%s\" can't be used together with \"--cloud\" argument.",
201 GetState.pDef->pszLong);
202
203 ulCurVsys = ValueUnion.u32;
204 ulCurUnit = (uint32_t)-1;
205 break;
206
207 case 'o': // --ostype
208 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
209 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
210 mapArgsMapsPerVsys[ulCurVsys]["ostype"] = ValueUnion.psz;
211 break;
212
213 case 'V': // --vmname
214 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
215 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
216 mapArgsMapsPerVsys[ulCurVsys]["vmname"] = ValueUnion.psz;
217 break;
218
219 case 'S': // --settingsfile
220 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
221 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
222 mapArgsMapsPerVsys[ulCurVsys]["settingsfile"] = ValueUnion.psz;
223 break;
224
225 case 'p': // --basefolder
226 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
227 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
228 mapArgsMapsPerVsys[ulCurVsys]["basefolder"] = ValueUnion.psz;
229 break;
230
231 case 'g': // --group
232 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
233 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
234 mapArgsMapsPerVsys[ulCurVsys]["group"] = ValueUnion.psz;
235 break;
236
237 case 'd': // --description
238 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
239 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
240 mapArgsMapsPerVsys[ulCurVsys]["description"] = ValueUnion.psz;
241 break;
242
243 case 'L': // --eula
244 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
245 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
246 mapArgsMapsPerVsys[ulCurVsys]["eula"] = ValueUnion.psz;
247 break;
248
249 case 'm': // --memory
250 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
251 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
252 mapArgsMapsPerVsys[ulCurVsys]["memory"] = ValueUnion.psz;
253 break;
254
255 case 'c': // --cpus
256 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
257 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
258 mapArgsMapsPerVsys[ulCurVsys]["cpus"] = ValueUnion.psz;
259 break;
260
261 case 'u': // --unit
262 ulCurUnit = ValueUnion.u32;
263 break;
264
265 case 'x': // --ignore
266 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
267 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
268 if (ulCurUnit == (uint32_t)-1)
269 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
270 mapIgnoresMapsPerVsys[ulCurVsys][ulCurUnit] = true;
271 break;
272
273 case 'T': // --scsitype
274 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
275 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
276 if (ulCurUnit == (uint32_t)-1)
277 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
278 mapArgsMapsPerVsys[ulCurVsys][Utf8StrFmt("scsitype%u", ulCurUnit)] = ValueUnion.psz;
279 break;
280
281 case 'C': // --controller
282 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
283 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
284 if (ulCurUnit == (uint32_t)-1)
285 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
286 mapArgsMapsPerVsys[ulCurVsys][Utf8StrFmt("controller%u", ulCurUnit)] = ValueUnion.psz;
287 break;
288
289 case 'D': // --disk
290 if (actionType == LOCAL && ulCurVsys == (uint32_t)-1)
291 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
292 if (ulCurUnit == (uint32_t)-1)
293 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
294 mapArgsMapsPerVsys[ulCurVsys][Utf8StrFmt("disk%u", ulCurUnit)] = ValueUnion.psz;
295 break;
296
297 case 'O': // --options
298 if (RT_FAILURE(parseImportOptions(ValueUnion.psz, &options)))
299 return errorArgument("Invalid import options '%s'\n", ValueUnion.psz);
300 break;
301
302 /*--cloud and --vsys are orthogonal, only one must be presented*/
303 case 'j': // --cloud
304 if (fCloud == false && actionType == NOT_SET)
305 {
306 fCloud = true;
307 actionType = CLOUD;
308 }
309
310 if (actionType != CLOUD)
311 return errorSyntax(USAGE_IMPORTAPPLIANCE,
312 "Option \"%s\" can't be used together with \"--vsys\" argument.",
313 GetState.pDef->pszLong);
314
315 ulCurVsys = 0;
316 break;
317
318 /* Cloud export settings */
319 case 'k': // --cloudprofile
320 if (actionType != CLOUD)
321 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
322 GetState.pDef->pszLong);
323 mapArgsMapsPerVsys[ulCurVsys]["cloudprofile"] = ValueUnion.psz;
324 break;
325
326 case 'l': // --cloudinstanceid
327 if (actionType != CLOUD)
328 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
329 GetState.pDef->pszLong);
330 mapArgsMapsPerVsys[ulCurVsys]["cloudinstanceid"] = ValueUnion.psz;
331 break;
332
333 case 'B': // --cloudbucket
334 if (actionType != CLOUD)
335 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
336 GetState.pDef->pszLong);
337 mapArgsMapsPerVsys[ulCurVsys]["cloudbucket"] = ValueUnion.psz;
338 break;
339
340 case VINF_GETOPT_NOT_OPTION:
341 if (strOvfFilename.isEmpty())
342 strOvfFilename = ValueUnion.psz;
343 else
344 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Invalid parameter '%s'", ValueUnion.psz);
345 break;
346
347 default:
348 if (c > 0)
349 {
350 if (RT_C_IS_PRINT(c))
351 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Invalid option -%c", c);
352 else
353 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Invalid option case %i", c);
354 }
355 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
356 return errorSyntax(USAGE_IMPORTAPPLIANCE, "unknown option: %s\n", ValueUnion.psz);
357 else if (ValueUnion.pDef)
358 return errorSyntax(USAGE_IMPORTAPPLIANCE, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
359 else
360 return errorSyntax(USAGE_IMPORTAPPLIANCE, "error: %Rrs", c);
361 }
362 }
363
364 if (actionType == LOCAL && strOvfFilename.isEmpty())
365 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Not enough arguments for \"import\" command.");
366
367 do
368 {
369 ComPtr<IAppliance> pAppliance;
370 CHECK_ERROR_BREAK(arg->virtualBox, CreateAppliance(pAppliance.asOutParam()));
371 //in the case of Cloud, append the instance id here because later it's harder to do
372 if (actionType == CLOUD)
373 {
374 try
375 {
376 /* Check presence of cloudprofile and cloudinstanceid in the map.
377 * If there isn't the exception is triggered. It's standard std:map logic.*/
378 ArgsMap a = mapArgsMapsPerVsys[ulCurVsys];
379 a.at("cloudprofile");
380 a.at("cloudinstanceid");
381 } catch (...)
382 {
383 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Not enough arguments for import from the Cloud.");
384 }
385
386 strOvfFilename.append(mapArgsMapsPerVsys[ulCurVsys]["cloudprofile"]);
387 strOvfFilename.append("/");
388 strOvfFilename.append(mapArgsMapsPerVsys[ulCurVsys]["cloudinstanceid"]);
389 }
390
391 char *pszAbsFilePath;
392 if (strOvfFilename.startsWith("S3://", RTCString::CaseInsensitive) ||
393 strOvfFilename.startsWith("SunCloud://", RTCString::CaseInsensitive) ||
394 strOvfFilename.startsWith("webdav://", RTCString::CaseInsensitive) ||
395 strOvfFilename.startsWith("OCI://", RTCString::CaseInsensitive))
396 pszAbsFilePath = RTStrDup(strOvfFilename.c_str());
397 else
398 pszAbsFilePath = RTPathAbsDup(strOvfFilename.c_str());
399
400 ComPtr<IProgress> progressRead;
401 CHECK_ERROR_BREAK(pAppliance, Read(Bstr(pszAbsFilePath).raw(),
402 progressRead.asOutParam()));
403 RTStrFree(pszAbsFilePath);
404
405 rc = showProgress(progressRead);
406 CHECK_PROGRESS_ERROR_RET(progressRead, ("Appliance read failed"), RTEXITCODE_FAILURE);
407
408 Bstr path; /* fetch the path, there is stuff like username/password removed if any */
409 CHECK_ERROR_BREAK(pAppliance, COMGETTER(Path)(path.asOutParam()));
410
411 size_t cVirtualSystemDescriptions = 0;
412 com::SafeIfaceArray<IVirtualSystemDescription> aVirtualSystemDescriptions;
413
414 if (actionType == LOCAL)
415 {
416 // call interpret(); this can yield both warnings and errors, so we need
417 // to tinker with the error info a bit
418 RTStrmPrintf(g_pStdErr, "Interpreting %ls...\n", path.raw());
419 rc = pAppliance->Interpret();
420 com::ErrorInfo info0(pAppliance, COM_IIDOF(IAppliance));
421
422 com::SafeArray<BSTR> aWarnings;
423 if (SUCCEEDED(pAppliance->GetWarnings(ComSafeArrayAsOutParam(aWarnings))))
424 {
425 size_t cWarnings = aWarnings.size();
426 for (unsigned i = 0; i < cWarnings; ++i)
427 {
428 Bstr bstrWarning(aWarnings[i]);
429 RTMsgWarning("%ls.", bstrWarning.raw());
430 }
431 }
432
433 if (FAILED(rc)) // during interpret, after printing warnings
434 {
435 com::GluePrintErrorInfo(info0);
436 com::GluePrintErrorContext("Interpret", __FILE__, __LINE__);
437 break;
438 }
439
440 RTStrmPrintf(g_pStdErr, "OK.\n");
441
442 // fetch all disks
443 com::SafeArray<BSTR> retDisks;
444 CHECK_ERROR_BREAK(pAppliance,
445 COMGETTER(Disks)(ComSafeArrayAsOutParam(retDisks)));
446 if (retDisks.size() > 0)
447 {
448 RTPrintf("Disks:\n");
449 for (unsigned i = 0; i < retDisks.size(); i++)
450 RTPrintf(" %ls\n", retDisks[i]);
451 RTPrintf("\n");
452 }
453
454 // fetch virtual system descriptions
455 CHECK_ERROR_BREAK(pAppliance,
456 COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aVirtualSystemDescriptions)));
457
458 cVirtualSystemDescriptions = aVirtualSystemDescriptions.size();
459
460 // match command line arguments with virtual system descriptions;
461 // this is only to sort out invalid indices at this time
462 ArgsMapsMap::const_iterator it;
463 for (it = mapArgsMapsPerVsys.begin();
464 it != mapArgsMapsPerVsys.end();
465 ++it)
466 {
467 uint32_t ulVsys = it->first;
468 if (ulVsys >= cVirtualSystemDescriptions)
469 return errorSyntax(USAGE_IMPORTAPPLIANCE,
470 "Invalid index %RI32 with -vsys option; the OVF contains only %zu virtual system(s).",
471 ulVsys, cVirtualSystemDescriptions);
472 }
473 }
474 else if (actionType == CLOUD)
475 {
476 /* In the Cloud case the call of interpret() isn't needed because there isn't any OVF XML file.
477 * All info is got from the Cloud and VSD is filled inside IAppliance::read(). */
478 // fetch virtual system descriptions
479 CHECK_ERROR_BREAK(pAppliance,
480 COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aVirtualSystemDescriptions)));
481
482 cVirtualSystemDescriptions = aVirtualSystemDescriptions.size();
483 }
484
485 uint32_t cLicensesInTheWay = 0;
486
487 // dump virtual system descriptions and match command-line arguments
488 if (cVirtualSystemDescriptions > 0)
489 {
490 for (unsigned i = 0; i < cVirtualSystemDescriptions; ++i)
491 {
492 com::SafeArray<VirtualSystemDescriptionType_T> retTypes;
493 com::SafeArray<BSTR> aRefs;
494 com::SafeArray<BSTR> aOvfValues;
495 com::SafeArray<BSTR> aVBoxValues;
496 com::SafeArray<BSTR> aExtraConfigValues;
497 CHECK_ERROR_BREAK(aVirtualSystemDescriptions[i],
498 GetDescription(ComSafeArrayAsOutParam(retTypes),
499 ComSafeArrayAsOutParam(aRefs),
500 ComSafeArrayAsOutParam(aOvfValues),
501 ComSafeArrayAsOutParam(aVBoxValues),
502 ComSafeArrayAsOutParam(aExtraConfigValues)));
503
504 RTPrintf("Virtual system %u:\n", i);
505
506 // look up the corresponding command line options, if any
507 ArgsMap *pmapArgs = NULL;
508 ArgsMapsMap::iterator itm = mapArgsMapsPerVsys.find(i);
509 if (itm != mapArgsMapsPerVsys.end())
510 pmapArgs = &itm->second;
511
512 // this collects the final values for setFinalValues()
513 com::SafeArray<BOOL> aEnabled(retTypes.size());
514 com::SafeArray<BSTR> aFinalValues(retTypes.size());
515
516 for (unsigned a = 0; a < retTypes.size(); ++a)
517 {
518 VirtualSystemDescriptionType_T t = retTypes[a];
519
520 Utf8Str strOverride;
521
522 Bstr bstrFinalValue = aVBoxValues[a];
523
524 bool fIgnoreThis = mapIgnoresMapsPerVsys[i][a];
525
526 aEnabled[a] = true;
527
528 switch (t)
529 {
530 case VirtualSystemDescriptionType_OS:
531 if (findArgValue(strOverride, pmapArgs, "ostype"))
532 {
533 bstrFinalValue = strOverride;
534 RTPrintf("%2u: OS type specified with --ostype: \"%ls\"\n",
535 a, bstrFinalValue.raw());
536 }
537 else
538 RTPrintf("%2u: Suggested OS type: \"%ls\""
539 "\n (change with \"--vsys %u --ostype <type>\"; use \"list ostypes\" to list all possible values)\n",
540 a, bstrFinalValue.raw(), i);
541 break;
542
543 case VirtualSystemDescriptionType_Name:
544 if (findArgValue(strOverride, pmapArgs, "vmname"))
545 {
546 bstrFinalValue = strOverride;
547 RTPrintf("%2u: VM name specified with --vmname: \"%ls\"\n",
548 a, bstrFinalValue.raw());
549 }
550 else
551 RTPrintf("%2u: Suggested VM name \"%ls\""
552 "\n (change with \"--vsys %u --vmname <name>\")\n",
553 a, bstrFinalValue.raw(), i);
554 break;
555
556 case VirtualSystemDescriptionType_Product:
557 RTPrintf("%2u: Product (ignored): %ls\n",
558 a, aVBoxValues[a]);
559 break;
560
561 case VirtualSystemDescriptionType_ProductUrl:
562 RTPrintf("%2u: ProductUrl (ignored): %ls\n",
563 a, aVBoxValues[a]);
564 break;
565
566 case VirtualSystemDescriptionType_Vendor:
567 RTPrintf("%2u: Vendor (ignored): %ls\n",
568 a, aVBoxValues[a]);
569 break;
570
571 case VirtualSystemDescriptionType_VendorUrl:
572 RTPrintf("%2u: VendorUrl (ignored): %ls\n",
573 a, aVBoxValues[a]);
574 break;
575
576 case VirtualSystemDescriptionType_Version:
577 RTPrintf("%2u: Version (ignored): %ls\n",
578 a, aVBoxValues[a]);
579 break;
580
581 case VirtualSystemDescriptionType_Description:
582 if (findArgValue(strOverride, pmapArgs, "description"))
583 {
584 bstrFinalValue = strOverride;
585 RTPrintf("%2u: Description specified with --description: \"%ls\"\n",
586 a, bstrFinalValue.raw());
587 }
588 else
589 RTPrintf("%2u: Description \"%ls\""
590 "\n (change with \"--vsys %u --description <desc>\")\n",
591 a, bstrFinalValue.raw(), i);
592 break;
593
594 case VirtualSystemDescriptionType_License:
595 ++cLicensesInTheWay;
596 if (findArgValue(strOverride, pmapArgs, "eula"))
597 {
598 if (strOverride == "show")
599 {
600 RTPrintf("%2u: End-user license agreement"
601 "\n (accept with \"--vsys %u --eula accept\"):"
602 "\n\n%ls\n\n",
603 a, i, bstrFinalValue.raw());
604 }
605 else if (strOverride == "accept")
606 {
607 RTPrintf("%2u: End-user license agreement (accepted)\n",
608 a);
609 --cLicensesInTheWay;
610 }
611 else
612 return errorSyntax(USAGE_IMPORTAPPLIANCE,
613 "Argument to --eula must be either \"show\" or \"accept\".");
614 }
615 else
616 RTPrintf("%2u: End-user license agreement"
617 "\n (display with \"--vsys %u --eula show\";"
618 "\n accept with \"--vsys %u --eula accept\")\n",
619 a, i, i);
620 break;
621
622 case VirtualSystemDescriptionType_CPU:
623 if (findArgValue(strOverride, pmapArgs, "cpus"))
624 {
625 uint32_t cCPUs;
626 if ( strOverride.toInt(cCPUs) == VINF_SUCCESS
627 && cCPUs >= VMM_MIN_CPU_COUNT
628 && cCPUs <= VMM_MAX_CPU_COUNT
629 )
630 {
631 bstrFinalValue = strOverride;
632 RTPrintf("%2u: No. of CPUs specified with --cpus: %ls\n",
633 a, bstrFinalValue.raw());
634 }
635 else
636 return errorSyntax(USAGE_IMPORTAPPLIANCE,
637 "Argument to --cpus option must be a number greater than %d and less than %d.",
638 VMM_MIN_CPU_COUNT - 1, VMM_MAX_CPU_COUNT + 1);
639 }
640 else
641 RTPrintf("%2u: Number of CPUs: %ls\n (change with \"--vsys %u --cpus <n>\")\n",
642 a, bstrFinalValue.raw(), i);
643 break;
644
645 case VirtualSystemDescriptionType_Memory:
646 {
647 if (findArgValue(strOverride, pmapArgs, "memory"))
648 {
649 uint32_t ulMemMB;
650 if (VINF_SUCCESS == strOverride.toInt(ulMemMB))
651 {
652 bstrFinalValue = strOverride;
653 RTPrintf("%2u: Guest memory specified with --memory: %ls MB\n",
654 a, bstrFinalValue.raw());
655 }
656 else
657 return errorSyntax(USAGE_IMPORTAPPLIANCE,
658 "Argument to --memory option must be a non-negative number.");
659 }
660 else
661 RTPrintf("%2u: Guest memory: %ls MB\n (change with \"--vsys %u --memory <MB>\")\n",
662 a, bstrFinalValue.raw(), i);
663 break;
664 }
665
666 case VirtualSystemDescriptionType_HardDiskControllerIDE:
667 if (fIgnoreThis)
668 {
669 RTPrintf("%2u: IDE controller, type %ls -- disabled\n",
670 a,
671 aVBoxValues[a]);
672 aEnabled[a] = false;
673 }
674 else
675 RTPrintf("%2u: IDE controller, type %ls"
676 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
677 a,
678 aVBoxValues[a],
679 i, a);
680 break;
681
682 case VirtualSystemDescriptionType_HardDiskControllerSATA:
683 if (fIgnoreThis)
684 {
685 RTPrintf("%2u: SATA controller, type %ls -- disabled\n",
686 a,
687 aVBoxValues[a]);
688 aEnabled[a] = false;
689 }
690 else
691 RTPrintf("%2u: SATA controller, type %ls"
692 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
693 a,
694 aVBoxValues[a],
695 i, a);
696 break;
697
698 case VirtualSystemDescriptionType_HardDiskControllerSAS:
699 if (fIgnoreThis)
700 {
701 RTPrintf("%2u: SAS controller, type %ls -- disabled\n",
702 a,
703 aVBoxValues[a]);
704 aEnabled[a] = false;
705 }
706 else
707 RTPrintf("%2u: SAS controller, type %ls"
708 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
709 a,
710 aVBoxValues[a],
711 i, a);
712 break;
713
714 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
715 if (fIgnoreThis)
716 {
717 RTPrintf("%2u: SCSI controller, type %ls -- disabled\n",
718 a,
719 aVBoxValues[a]);
720 aEnabled[a] = false;
721 }
722 else
723 {
724 Utf8StrFmt strTypeArg("scsitype%u", a);
725 if (findArgValue(strOverride, pmapArgs, strTypeArg))
726 {
727 bstrFinalValue = strOverride;
728 RTPrintf("%2u: SCSI controller, type set with --unit %u --scsitype: \"%ls\"\n",
729 a,
730 a,
731 bstrFinalValue.raw());
732 }
733 else
734 RTPrintf("%2u: SCSI controller, type %ls"
735 "\n (change with \"--vsys %u --unit %u --scsitype {BusLogic|LsiLogic}\";"
736 "\n disable with \"--vsys %u --unit %u --ignore\")\n",
737 a,
738 aVBoxValues[a],
739 i, a, i, a);
740 }
741 break;
742
743 case VirtualSystemDescriptionType_HardDiskImage:
744 if (fIgnoreThis)
745 {
746 RTPrintf("%2u: Hard disk image: source image=%ls -- disabled\n",
747 a,
748 aOvfValues[a]);
749 aEnabled[a] = false;
750 }
751 else
752 {
753 Utf8StrFmt strTypeArg("disk%u", a);
754 RTCList<ImportOptions_T> optionsList = options.toList();
755
756 bstrFinalValue = aVBoxValues[a];
757
758 if (findArgValue(strOverride, pmapArgs, strTypeArg))
759 {
760 if (!optionsList.contains(ImportOptions_ImportToVDI))
761 {
762 RTUUID uuid;
763 /* Check if this is a uuid. If so, don't touch. */
764 int vrc = RTUuidFromStr(&uuid, strOverride.c_str());
765 if (vrc != VINF_SUCCESS)
766 {
767 /* Make the path absolute. */
768 if (!RTPathStartsWithRoot(strOverride.c_str()))
769 {
770 char pszPwd[RTPATH_MAX];
771 vrc = RTPathGetCurrent(pszPwd, RTPATH_MAX);
772 if (RT_SUCCESS(vrc))
773 strOverride = Utf8Str(pszPwd).append(RTPATH_SLASH).append(strOverride);
774 }
775 }
776 bstrFinalValue = strOverride;
777 }
778 else
779 {
780 //print some error about incompatible command-line arguments
781 return errorSyntax(USAGE_IMPORTAPPLIANCE,
782 "Option --ImportToVDI shall not be used together with "
783 "manually set target path.");
784
785 }
786
787 RTPrintf("%2u: Hard disk image: source image=%ls, target path=%ls, %ls\n",
788 a,
789 aOvfValues[a],
790 bstrFinalValue.raw(),
791 aExtraConfigValues[a]);
792 }
793#if 0 /* Changing the controller is fully valid, but the current design on how
794 the params are evaluated here doesn't allow two parameter for one
795 unit. The target disk path is more important I leave it for future
796 improvments. */
797 Utf8StrFmt strTypeArg("controller%u", a);
798 if (findArgValue(strOverride, pmapArgs, strTypeArg))
799 {
800 // strOverride now has the controller index as a number, but we
801 // need a "controller=X" format string
802 strOverride = Utf8StrFmt("controller=%s", strOverride.c_str());
803 Bstr bstrExtraConfigValue = strOverride;
804 bstrExtraConfigValue.detachTo(&aExtraConfigValues[a]);
805 RTPrintf("%2u: Hard disk image: source image=%ls, target path=%ls, %ls\n",
806 a,
807 aOvfValues[a],
808 aVBoxValues[a],
809 aExtraConfigValues[a]);
810 }
811#endif
812 else
813 {
814 strOverride = aVBoxValues[a];
815
816 /*
817 * Current solution isn't optimal.
818 * Better way is to provide API call for function
819 * Appliance::i_findMediumFormatFromDiskImage()
820 * and creating one new function which returns
821 * struct ovf::DiskImage for currently processed disk.
822 */
823
824 /*
825 * if user wants to convert all imported disks to VDI format
826 * we need to replace files extensions to "vdi"
827 * except CD/DVD disks
828 */
829 if (optionsList.contains(ImportOptions_ImportToVDI))
830 {
831 ComPtr<IVirtualBox> pVirtualBox = arg->virtualBox;
832 ComPtr<ISystemProperties> systemProperties;
833 com::SafeIfaceArray<IMediumFormat> mediumFormats;
834 Bstr bstrFormatName;
835
836 CHECK_ERROR(pVirtualBox,
837 COMGETTER(SystemProperties)(systemProperties.asOutParam()));
838
839 CHECK_ERROR(systemProperties,
840 COMGETTER(MediumFormats)(ComSafeArrayAsOutParam(mediumFormats)));
841
842 /* go through all supported media formats and store files extensions only for RAW */
843 com::SafeArray<BSTR> extensions;
844
845 for (unsigned j = 0; j < mediumFormats.size(); ++j)
846 {
847 com::SafeArray<DeviceType_T> deviceType;
848 ComPtr<IMediumFormat> mediumFormat = mediumFormats[j];
849 CHECK_ERROR(mediumFormat, COMGETTER(Name)(bstrFormatName.asOutParam()));
850 Utf8Str strFormatName = Utf8Str(bstrFormatName);
851
852 if (strFormatName.compare("RAW", Utf8Str::CaseInsensitive) == 0)
853 {
854 /* getting files extensions for "RAW" format */
855 CHECK_ERROR(mediumFormat,
856 DescribeFileExtensions(ComSafeArrayAsOutParam(extensions),
857 ComSafeArrayAsOutParam(deviceType)));
858 break;
859 }
860 }
861
862 /* go through files extensions for RAW format and compare them with
863 * extension of current file
864 */
865 bool fReplace = true;
866
867 const char *pszExtension = RTPathSuffix(strOverride.c_str());
868 if (pszExtension)
869 pszExtension++;
870
871 for (unsigned j = 0; j < extensions.size(); ++j)
872 {
873 Bstr bstrExt(extensions[j]);
874 Utf8Str strExtension(bstrExt);
875 if(strExtension.compare(pszExtension, Utf8Str::CaseInsensitive) == 0)
876 {
877 fReplace = false;
878 break;
879 }
880 }
881
882 if (fReplace)
883 {
884 strOverride = strOverride.stripSuffix();
885 strOverride = strOverride.append(".").append("vdi");
886 }
887 }
888
889 bstrFinalValue = strOverride;
890
891 RTPrintf("%2u: Hard disk image: source image=%ls, target path=%ls, %ls"
892 "\n (change target path with \"--vsys %u --unit %u --disk path\";"
893 "\n disable with \"--vsys %u --unit %u --ignore\")\n",
894 a,
895 aOvfValues[a],
896 bstrFinalValue.raw(),
897 aExtraConfigValues[a],
898 i, a, i, a);
899 }
900 }
901 break;
902
903 case VirtualSystemDescriptionType_CDROM:
904 if (fIgnoreThis)
905 {
906 RTPrintf("%2u: CD-ROM -- disabled\n",
907 a);
908 aEnabled[a] = false;
909 }
910 else
911 RTPrintf("%2u: CD-ROM"
912 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
913 a, i, a);
914 break;
915
916 case VirtualSystemDescriptionType_Floppy:
917 if (fIgnoreThis)
918 {
919 RTPrintf("%2u: Floppy -- disabled\n",
920 a);
921 aEnabled[a] = false;
922 }
923 else
924 RTPrintf("%2u: Floppy"
925 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
926 a, i, a);
927 break;
928
929 case VirtualSystemDescriptionType_NetworkAdapter:
930 RTPrintf("%2u: Network adapter: orig %ls, config %ls, extra %ls\n", /// @todo implement once we have a plan for the back-end
931 a,
932 aOvfValues[a],
933 aVBoxValues[a],
934 aExtraConfigValues[a]);
935 break;
936
937 case VirtualSystemDescriptionType_USBController:
938 if (fIgnoreThis)
939 {
940 RTPrintf("%2u: USB controller -- disabled\n",
941 a);
942 aEnabled[a] = false;
943 }
944 else
945 RTPrintf("%2u: USB controller"
946 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
947 a, i, a);
948 break;
949
950 case VirtualSystemDescriptionType_SoundCard:
951 if (fIgnoreThis)
952 {
953 RTPrintf("%2u: Sound card \"%ls\" -- disabled\n",
954 a,
955 aOvfValues[a]);
956 aEnabled[a] = false;
957 }
958 else
959 RTPrintf("%2u: Sound card (appliance expects \"%ls\", can change on import)"
960 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
961 a,
962 aOvfValues[a],
963 i,
964 a);
965 break;
966
967 case VirtualSystemDescriptionType_SettingsFile:
968 if (findArgValue(strOverride, pmapArgs, "settingsfile"))
969 {
970 bstrFinalValue = strOverride;
971 RTPrintf("%2u: VM settings file name specified with --settingsfile: \"%ls\"\n",
972 a, bstrFinalValue.raw());
973 }
974 else
975 RTPrintf("%2u: Suggested VM settings file name \"%ls\""
976 "\n (change with \"--vsys %u --settingsfile <filename>\")\n",
977 a, bstrFinalValue.raw(), i);
978 break;
979
980 case VirtualSystemDescriptionType_BaseFolder:
981 if (findArgValue(strOverride, pmapArgs, "basefolder"))
982 {
983 bstrFinalValue = strOverride;
984 RTPrintf("%2u: VM base folder specified with --basefolder: \"%ls\"\n",
985 a, bstrFinalValue.raw());
986 }
987 else
988 RTPrintf("%2u: Suggested VM base folder \"%ls\""
989 "\n (change with \"--vsys %u --basefolder <path>\")\n",
990 a, bstrFinalValue.raw(), i);
991 break;
992
993 case VirtualSystemDescriptionType_PrimaryGroup:
994 if (findArgValue(strOverride, pmapArgs, "group"))
995 {
996 bstrFinalValue = strOverride;
997 RTPrintf("%2u: VM group specified with --group: \"%ls\"\n",
998 a, bstrFinalValue.raw());
999 }
1000 else
1001 RTPrintf("%2u: Suggested VM group \"%ls\""
1002 "\n (change with \"--vsys %u --group <group>\")\n",
1003 a, bstrFinalValue.raw(), i);
1004 break;
1005
1006 case VirtualSystemDescriptionType_CloudInstanceShape:
1007 RTPrintf("%2u: Suggested cloud shape \"%ls\"\n",
1008 a, bstrFinalValue.raw());
1009 break;
1010
1011 case VirtualSystemDescriptionType_CloudBucket:
1012 if (findArgValue(strOverride, pmapArgs, "cloudbucket"))
1013 {
1014 bstrFinalValue = strOverride;
1015 RTPrintf("%2u: Cloud bucket id specified with --cloudbucket: \"%ls\"\n",
1016 a, bstrFinalValue.raw());
1017 }
1018 else
1019 RTPrintf("%2u: Suggested cloud bucket id \"%ls\""
1020 "\n (change with \"--cloud %u --cloudbucket <id>\")\n",
1021 a, bstrFinalValue.raw(), i);
1022 break;
1023
1024 case VirtualSystemDescriptionType_CloudProfileName:
1025 if (findArgValue(strOverride, pmapArgs, "cloudprofile"))
1026 {
1027 bstrFinalValue = strOverride;
1028 RTPrintf("%2u: Cloud profile name specified with --cloudprofile: \"%ls\"\n",
1029 a, bstrFinalValue.raw());
1030 }
1031 else
1032 RTPrintf("%2u: Suggested cloud profile name \"%ls\""
1033 "\n (change with \"--cloud %u --cloudprofile <id>\")\n",
1034 a, bstrFinalValue.raw(), i);
1035 break;
1036
1037 case VirtualSystemDescriptionType_CloudInstanceId:
1038 if (findArgValue(strOverride, pmapArgs, "cloudinstanceid"))
1039 {
1040 bstrFinalValue = strOverride;
1041 RTPrintf("%2u: Cloud instance id specified with --cloudinstanceid: \"%ls\"\n",
1042 a, bstrFinalValue.raw());
1043 }
1044 else
1045 RTPrintf("%2u: Suggested cloud instance id \"%ls\""
1046 "\n (change with \"--cloud %u --cloudinstanceid <id>\")\n",
1047 a, bstrFinalValue.raw(), i);
1048 break;
1049
1050 case VirtualSystemDescriptionType_CloudImageId:
1051 RTPrintf("%2u: Suggested cloud base image id \"%ls\"\n",
1052 a, bstrFinalValue.raw());
1053 break;
1054 case VirtualSystemDescriptionType_CloudDomain:
1055 case VirtualSystemDescriptionType_CloudBootDiskSize:
1056 case VirtualSystemDescriptionType_CloudOCIVCN:
1057 case VirtualSystemDescriptionType_CloudPublicIP:
1058 case VirtualSystemDescriptionType_CloudOCISubnet:
1059 case VirtualSystemDescriptionType_CloudKeepObject:
1060 case VirtualSystemDescriptionType_CloudLaunchInstance:
1061 case VirtualSystemDescriptionType_CloudInstanceState:
1062 case VirtualSystemDescriptionType_CloudImageState:
1063 case VirtualSystemDescriptionType_Miscellaneous:
1064 case VirtualSystemDescriptionType_CloudInstanceDisplayName:
1065 case VirtualSystemDescriptionType_CloudImageDisplayName:
1066 case VirtualSystemDescriptionType_CloudOCILaunchMode:
1067 case VirtualSystemDescriptionType_CloudPrivateIP:
1068 case VirtualSystemDescriptionType_CloudBootVolumeId:
1069 /** @todo VirtualSystemDescriptionType_Miscellaneous? */
1070 break;
1071
1072 case VirtualSystemDescriptionType_Ignore:
1073#ifdef VBOX_WITH_XPCOM_CPP_ENUM_HACK
1074 case VirtualSystemDescriptionType_32BitHack:
1075#endif
1076 break;
1077 }
1078
1079 bstrFinalValue.detachTo(&aFinalValues[a]);
1080 }
1081
1082 if (fExecute)
1083 CHECK_ERROR_BREAK(aVirtualSystemDescriptions[i],
1084 SetFinalValues(ComSafeArrayAsInParam(aEnabled),
1085 ComSafeArrayAsInParam(aFinalValues),
1086 ComSafeArrayAsInParam(aExtraConfigValues)));
1087
1088 } // for (unsigned i = 0; i < cVirtualSystemDescriptions; ++i)
1089
1090 if (cLicensesInTheWay == 1)
1091 RTMsgError("Cannot import until the license agreement listed above is accepted.");
1092 else if (cLicensesInTheWay > 1)
1093 RTMsgError("Cannot import until the %c license agreements listed above are accepted.", cLicensesInTheWay);
1094
1095 if (!cLicensesInTheWay && fExecute)
1096 {
1097 // go!
1098 ComPtr<IProgress> progress;
1099 CHECK_ERROR_BREAK(pAppliance,
1100 ImportMachines(ComSafeArrayAsInParam(options), progress.asOutParam()));
1101
1102 rc = showProgress(progress);
1103 CHECK_PROGRESS_ERROR_RET(progress, ("Appliance import failed"), RTEXITCODE_FAILURE);
1104
1105 if (SUCCEEDED(rc))
1106 RTPrintf("Successfully imported the appliance.\n");
1107 }
1108 } // end if (aVirtualSystemDescriptions.size() > 0)
1109 } while (0);
1110
1111 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1112}
1113
1114static int parseExportOptions(const char *psz, com::SafeArray<ExportOptions_T> *options)
1115{
1116 int rc = VINF_SUCCESS;
1117 while (psz && *psz && RT_SUCCESS(rc))
1118 {
1119 size_t len;
1120 const char *pszComma = strchr(psz, ',');
1121 if (pszComma)
1122 len = pszComma - psz;
1123 else
1124 len = strlen(psz);
1125 if (len > 0)
1126 {
1127 if (!RTStrNICmp(psz, "CreateManifest", len))
1128 options->push_back(ExportOptions_CreateManifest);
1129 else if (!RTStrNICmp(psz, "manifest", len))
1130 options->push_back(ExportOptions_CreateManifest);
1131 else if (!RTStrNICmp(psz, "ExportDVDImages", len))
1132 options->push_back(ExportOptions_ExportDVDImages);
1133 else if (!RTStrNICmp(psz, "iso", len))
1134 options->push_back(ExportOptions_ExportDVDImages);
1135 else if (!RTStrNICmp(psz, "StripAllMACs", len))
1136 options->push_back(ExportOptions_StripAllMACs);
1137 else if (!RTStrNICmp(psz, "nomacs", len))
1138 options->push_back(ExportOptions_StripAllMACs);
1139 else if (!RTStrNICmp(psz, "StripAllNonNATMACs", len))
1140 options->push_back(ExportOptions_StripAllNonNATMACs);
1141 else if (!RTStrNICmp(psz, "nomacsbutnat", len))
1142 options->push_back(ExportOptions_StripAllNonNATMACs);
1143 else
1144 rc = VERR_PARSE_ERROR;
1145 }
1146 if (pszComma)
1147 psz += len + 1;
1148 else
1149 psz += len;
1150 }
1151
1152 return rc;
1153}
1154
1155static const RTGETOPTDEF g_aExportOptions[] =
1156{
1157 { "--output", 'o', RTGETOPT_REQ_STRING },
1158 { "--legacy09", 'l', RTGETOPT_REQ_NOTHING },
1159 { "--ovf09", 'l', RTGETOPT_REQ_NOTHING },
1160 { "--ovf10", '1', RTGETOPT_REQ_NOTHING },
1161 { "--ovf20", '2', RTGETOPT_REQ_NOTHING },
1162 { "--opc10", 'c', RTGETOPT_REQ_NOTHING },
1163 { "--manifest", 'm', RTGETOPT_REQ_NOTHING }, // obsoleted by --options
1164 { "--iso", 'I', RTGETOPT_REQ_NOTHING }, // obsoleted by --options
1165 { "--vsys", 's', RTGETOPT_REQ_UINT32 },
1166 { "--vmname", 'V', RTGETOPT_REQ_STRING },
1167 { "--product", 'p', RTGETOPT_REQ_STRING },
1168 { "--producturl", 'P', RTGETOPT_REQ_STRING },
1169 { "--vendor", 'n', RTGETOPT_REQ_STRING },
1170 { "--vendorurl", 'N', RTGETOPT_REQ_STRING },
1171 { "--version", 'v', RTGETOPT_REQ_STRING },
1172 { "--description", 'd', RTGETOPT_REQ_STRING },
1173 { "--eula", 'e', RTGETOPT_REQ_STRING },
1174 { "--eulafile", 'E', RTGETOPT_REQ_STRING },
1175 { "--options", 'O', RTGETOPT_REQ_STRING },
1176 { "--cloud", 'C', RTGETOPT_REQ_UINT32 },
1177 { "--cloudshape", 'S', RTGETOPT_REQ_STRING },
1178 { "--clouddomain", 'D', RTGETOPT_REQ_STRING },
1179 { "--clouddisksize", 'R', RTGETOPT_REQ_STRING },
1180 { "--cloudbucket", 'B', RTGETOPT_REQ_STRING },
1181 { "--cloudocivcn", 'Q', RTGETOPT_REQ_STRING },
1182 { "--cloudpublicip", 'A', RTGETOPT_REQ_STRING },
1183 { "--cloudprofile", 'F', RTGETOPT_REQ_STRING },
1184 { "--cloudocisubnet", 'T', RTGETOPT_REQ_STRING },
1185 { "--cloudkeepobject", 'K', RTGETOPT_REQ_STRING },
1186 { "--cloudlaunchinstance", 'L', RTGETOPT_REQ_STRING },
1187 { "--cloudlaunchmode", 'M', RTGETOPT_REQ_STRING },
1188 { "--cloudprivateip", 'i', RTGETOPT_REQ_STRING },
1189};
1190
1191RTEXITCODE handleExportAppliance(HandlerArg *a)
1192{
1193 HRESULT rc = S_OK;
1194
1195 Utf8Str strOutputFile;
1196 Utf8Str strOvfFormat("ovf-1.0"); // the default export version
1197 bool fManifest = false; // the default
1198 bool fCloud = false; // the default
1199 actionType = NOT_SET;
1200 bool fExportISOImages = false; // the default
1201 com::SafeArray<ExportOptions_T> options;
1202 std::list< ComPtr<IMachine> > llMachines;
1203
1204 uint32_t ulCurVsys = (uint32_t)-1;
1205 // for each --vsys X command, maintain a map of command line items
1206 ArgsMapsMap mapArgsMapsPerVsys;
1207 do
1208 {
1209 int c;
1210
1211 RTGETOPTUNION ValueUnion;
1212 RTGETOPTSTATE GetState;
1213 // start at 0 because main() has hacked both the argc and argv given to us
1214 RTGetOptInit(&GetState, a->argc, a->argv, g_aExportOptions,
1215 RT_ELEMENTS(g_aExportOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1216
1217 Utf8Str strProductUrl;
1218 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1219 {
1220 switch (c)
1221 {
1222 case 'o': // --output
1223 if (strOutputFile.length())
1224 return errorSyntax(USAGE_EXPORTAPPLIANCE, "You can only specify --output once.");
1225 else
1226 strOutputFile = ValueUnion.psz;
1227 break;
1228
1229 case 'l': // --legacy09/--ovf09
1230 strOvfFormat = "ovf-0.9";
1231 break;
1232
1233 case '1': // --ovf10
1234 strOvfFormat = "ovf-1.0";
1235 break;
1236
1237 case '2': // --ovf20
1238 strOvfFormat = "ovf-2.0";
1239 break;
1240
1241 case 'c': // --opc
1242 strOvfFormat = "opc-1.0";
1243 break;
1244
1245 case 'I': // --iso
1246 fExportISOImages = true;
1247 break;
1248
1249 case 'm': // --manifest
1250 fManifest = true;
1251 break;
1252
1253 case 's': // --vsys
1254 if (fCloud == false && actionType == NOT_SET)
1255 actionType = LOCAL;
1256
1257 if (actionType != LOCAL)
1258 return errorSyntax(USAGE_EXPORTAPPLIANCE,
1259 "Option \"%s\" can't be used together with \"--cloud\" argument.",
1260 GetState.pDef->pszLong);
1261
1262 ulCurVsys = ValueUnion.u32;
1263 break;
1264
1265 case 'V': // --vmname
1266 if (actionType == NOT_SET || ulCurVsys == (uint32_t)-1)
1267 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys or --cloud argument.",
1268 GetState.pDef->pszLong);
1269 mapArgsMapsPerVsys[ulCurVsys]["vmname"] = ValueUnion.psz;
1270 break;
1271
1272 case 'p': // --product
1273 if (actionType != LOCAL)
1274 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
1275 mapArgsMapsPerVsys[ulCurVsys]["product"] = ValueUnion.psz;
1276 break;
1277
1278 case 'P': // --producturl
1279 if (actionType != LOCAL)
1280 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
1281 mapArgsMapsPerVsys[ulCurVsys]["producturl"] = ValueUnion.psz;
1282 break;
1283
1284 case 'n': // --vendor
1285 if (actionType != LOCAL)
1286 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
1287 mapArgsMapsPerVsys[ulCurVsys]["vendor"] = ValueUnion.psz;
1288 break;
1289
1290 case 'N': // --vendorurl
1291 if (actionType != LOCAL)
1292 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
1293 mapArgsMapsPerVsys[ulCurVsys]["vendorurl"] = ValueUnion.psz;
1294 break;
1295
1296 case 'v': // --version
1297 if (actionType != LOCAL)
1298 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
1299 mapArgsMapsPerVsys[ulCurVsys]["version"] = ValueUnion.psz;
1300 break;
1301
1302 case 'd': // --description
1303 if (actionType != LOCAL)
1304 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
1305 mapArgsMapsPerVsys[ulCurVsys]["description"] = ValueUnion.psz;
1306 break;
1307
1308 case 'e': // --eula
1309 if (actionType != LOCAL)
1310 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
1311 mapArgsMapsPerVsys[ulCurVsys]["eula"] = ValueUnion.psz;
1312 break;
1313
1314 case 'E': // --eulafile
1315 if (actionType != LOCAL)
1316 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
1317 mapArgsMapsPerVsys[ulCurVsys]["eulafile"] = ValueUnion.psz;
1318 break;
1319
1320 case 'O': // --options
1321 if (RT_FAILURE(parseExportOptions(ValueUnion.psz, &options)))
1322 return errorArgument("Invalid export options '%s'\n", ValueUnion.psz);
1323 break;
1324
1325 /*--cloud and --vsys are orthogonal, only one must be presented*/
1326 case 'C': // --cloud
1327 if (fCloud == false && actionType == NOT_SET)
1328 {
1329 fCloud = true;
1330 actionType = CLOUD;
1331 }
1332
1333 if (actionType != CLOUD)
1334 return errorSyntax(USAGE_EXPORTAPPLIANCE,
1335 "Option \"%s\" can't be used together with \"--vsys\" argument.",
1336 GetState.pDef->pszLong);
1337
1338 ulCurVsys = ValueUnion.u32;
1339 break;
1340
1341 /* Cloud export settings */
1342 case 'S': // --cloudshape
1343 if (actionType != CLOUD)
1344 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1345 GetState.pDef->pszLong);
1346 mapArgsMapsPerVsys[ulCurVsys]["cloudshape"] = ValueUnion.psz;
1347 break;
1348
1349 case 'D': // --clouddomain
1350 if (actionType != CLOUD)
1351 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1352 GetState.pDef->pszLong);
1353 mapArgsMapsPerVsys[ulCurVsys]["clouddomain"] = ValueUnion.psz;
1354 break;
1355
1356 case 'R': // --clouddisksize
1357 if (actionType != CLOUD)
1358 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1359 GetState.pDef->pszLong);
1360 mapArgsMapsPerVsys[ulCurVsys]["clouddisksize"] = ValueUnion.psz;
1361 break;
1362
1363 case 'B': // --cloudbucket
1364 if (actionType != CLOUD)
1365 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1366 GetState.pDef->pszLong);
1367 mapArgsMapsPerVsys[ulCurVsys]["cloudbucket"] = ValueUnion.psz;
1368 break;
1369
1370 case 'Q': // --cloudocivcn
1371 if (actionType != CLOUD)
1372 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1373 GetState.pDef->pszLong);
1374 mapArgsMapsPerVsys[ulCurVsys]["cloudocivcn"] = ValueUnion.psz;
1375 break;
1376
1377 case 'A': // --cloudpublicip
1378 if (actionType != CLOUD)
1379 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1380 GetState.pDef->pszLong);
1381 mapArgsMapsPerVsys[ulCurVsys]["cloudpublicip"] = ValueUnion.psz;
1382 break;
1383
1384 case 'i': /* --cloudprivateip */
1385 if (actionType != CLOUD)
1386 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1387 GetState.pDef->pszLong);
1388 mapArgsMapsPerVsys[ulCurVsys]["cloudprivateip"] = ValueUnion.psz;
1389 break;
1390
1391 case 'F': // --cloudprofile
1392 if (actionType != CLOUD)
1393 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1394 GetState.pDef->pszLong);
1395 mapArgsMapsPerVsys[ulCurVsys]["cloudprofile"] = ValueUnion.psz;
1396 break;
1397
1398 case 'T': // --cloudocisubnet
1399 if (actionType != CLOUD)
1400 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1401 GetState.pDef->pszLong);
1402 mapArgsMapsPerVsys[ulCurVsys]["cloudocisubnet"] = ValueUnion.psz;
1403 break;
1404
1405 case 'K': // --cloudkeepobject
1406 if (actionType != CLOUD)
1407 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1408 GetState.pDef->pszLong);
1409 mapArgsMapsPerVsys[ulCurVsys]["cloudkeepobject"] = ValueUnion.psz;
1410 break;
1411
1412 case 'L': // --cloudlaunchinstance
1413 if (actionType != CLOUD)
1414 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1415 GetState.pDef->pszLong);
1416 mapArgsMapsPerVsys[ulCurVsys]["cloudlaunchinstance"] = ValueUnion.psz;
1417 break;
1418
1419 case 'M': /* --cloudlaunchmode */
1420 if (actionType != CLOUD)
1421 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --cloud argument.",
1422 GetState.pDef->pszLong);
1423 mapArgsMapsPerVsys[ulCurVsys]["cloudlaunchmode"] = ValueUnion.psz;
1424 break;
1425
1426 case VINF_GETOPT_NOT_OPTION:
1427 {
1428 Utf8Str strMachine(ValueUnion.psz);
1429 // must be machine: try UUID or name
1430 ComPtr<IMachine> machine;
1431 CHECK_ERROR_BREAK(a->virtualBox, FindMachine(Bstr(strMachine).raw(),
1432 machine.asOutParam()));
1433 if (machine)
1434 llMachines.push_back(machine);
1435 break;
1436 }
1437
1438 default:
1439 if (c > 0)
1440 {
1441 if (RT_C_IS_GRAPH(c))
1442 return errorSyntax(USAGE_EXPORTAPPLIANCE, "unhandled option: -%c", c);
1443 else
1444 return errorSyntax(USAGE_EXPORTAPPLIANCE, "unhandled option: %i", c);
1445 }
1446 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
1447 return errorSyntax(USAGE_EXPORTAPPLIANCE, "unknown option: %s", ValueUnion.psz);
1448 else if (ValueUnion.pDef)
1449 return errorSyntax(USAGE_EXPORTAPPLIANCE, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
1450 else
1451 return errorSyntax(USAGE_EXPORTAPPLIANCE, "%Rrs", c);
1452 }
1453
1454 if (FAILED(rc))
1455 break;
1456 }
1457
1458 if (FAILED(rc))
1459 break;
1460
1461 if (llMachines.empty())
1462 return errorSyntax(USAGE_EXPORTAPPLIANCE, "At least one machine must be specified with the export command.");
1463 if (!strOutputFile.length())
1464 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Missing --output argument with export command.");
1465
1466 // match command line arguments with the machines count
1467 // this is only to sort out invalid indices at this time
1468 ArgsMapsMap::const_iterator it;
1469 for (it = mapArgsMapsPerVsys.begin();
1470 it != mapArgsMapsPerVsys.end();
1471 ++it)
1472 {
1473 uint32_t ulVsys = it->first;
1474 if (ulVsys >= llMachines.size())
1475 return errorSyntax(USAGE_EXPORTAPPLIANCE,
1476 "Invalid index %RI32 with -vsys option; you specified only %zu virtual system(s).",
1477 ulVsys, llMachines.size());
1478 }
1479
1480 ComPtr<IAppliance> pAppliance;
1481 CHECK_ERROR_BREAK(a->virtualBox, CreateAppliance(pAppliance.asOutParam()));
1482
1483 char *pszAbsFilePath = 0;
1484 if (strOutputFile.startsWith("S3://", RTCString::CaseInsensitive) ||
1485 strOutputFile.startsWith("SunCloud://", RTCString::CaseInsensitive) ||
1486 strOutputFile.startsWith("webdav://", RTCString::CaseInsensitive) ||
1487 strOutputFile.startsWith("OCI://", RTCString::CaseInsensitive))
1488 pszAbsFilePath = RTStrDup(strOutputFile.c_str());
1489 else
1490 pszAbsFilePath = RTPathAbsDup(strOutputFile.c_str());
1491
1492 /*
1493 * The first stage - export machine/s to the Cloud or into the
1494 * OVA/OVF format on the local host.
1495 */
1496
1497 /* VSDList is needed for the second stage where we launch the cloud instances if it was requested by user */
1498 std::list< ComPtr<IVirtualSystemDescription> > VSDList;
1499 std::list< ComPtr<IMachine> >::iterator itM;
1500 uint32_t i=0;
1501 for (itM = llMachines.begin();
1502 itM != llMachines.end();
1503 ++itM, ++i)
1504 {
1505 ComPtr<IMachine> pMachine = *itM;
1506 ComPtr<IVirtualSystemDescription> pVSD;
1507 CHECK_ERROR_BREAK(pMachine, ExportTo(pAppliance, Bstr(pszAbsFilePath).raw(), pVSD.asOutParam()));
1508
1509 // Add additional info to the virtual system description if the user wants so
1510 ArgsMap *pmapArgs = NULL;
1511 ArgsMapsMap::iterator itm = mapArgsMapsPerVsys.find(i);
1512 if (itm != mapArgsMapsPerVsys.end())
1513 pmapArgs = &itm->second;
1514 if (pmapArgs)
1515 {
1516 ArgsMap::iterator itD;
1517 for (itD = pmapArgs->begin();
1518 itD != pmapArgs->end();
1519 ++itD)
1520 {
1521 if (itD->first == "vmname")
1522 {
1523 //remove default value if user has specified new name (default value is set in the ExportTo())
1524// pVSD->RemoveDescriptionByType(VirtualSystemDescriptionType_Name);
1525 pVSD->AddDescription(VirtualSystemDescriptionType_Name,
1526 Bstr(itD->second).raw(),
1527 Bstr(itD->second).raw());
1528 }
1529 else if (itD->first == "product")
1530 pVSD->AddDescription(VirtualSystemDescriptionType_Product,
1531 Bstr(itD->second).raw(),
1532 Bstr(itD->second).raw());
1533 else if (itD->first == "producturl")
1534 pVSD->AddDescription(VirtualSystemDescriptionType_ProductUrl,
1535 Bstr(itD->second).raw(),
1536 Bstr(itD->second).raw());
1537 else if (itD->first == "vendor")
1538 pVSD->AddDescription(VirtualSystemDescriptionType_Vendor,
1539 Bstr(itD->second).raw(),
1540 Bstr(itD->second).raw());
1541 else if (itD->first == "vendorurl")
1542 pVSD->AddDescription(VirtualSystemDescriptionType_VendorUrl,
1543 Bstr(itD->second).raw(),
1544 Bstr(itD->second).raw());
1545 else if (itD->first == "version")
1546 pVSD->AddDescription(VirtualSystemDescriptionType_Version,
1547 Bstr(itD->second).raw(),
1548 Bstr(itD->second).raw());
1549 else if (itD->first == "description")
1550 pVSD->AddDescription(VirtualSystemDescriptionType_Description,
1551 Bstr(itD->second).raw(),
1552 Bstr(itD->second).raw());
1553 else if (itD->first == "eula")
1554 pVSD->AddDescription(VirtualSystemDescriptionType_License,
1555 Bstr(itD->second).raw(),
1556 Bstr(itD->second).raw());
1557 else if (itD->first == "eulafile")
1558 {
1559 Utf8Str strContent;
1560 void *pvFile;
1561 size_t cbFile;
1562 int irc = RTFileReadAll(itD->second.c_str(), &pvFile, &cbFile);
1563 if (RT_SUCCESS(irc))
1564 {
1565 Bstr bstrContent((char*)pvFile, cbFile);
1566 pVSD->AddDescription(VirtualSystemDescriptionType_License,
1567 bstrContent.raw(),
1568 bstrContent.raw());
1569 RTFileReadAllFree(pvFile, cbFile);
1570 }
1571 else
1572 {
1573 RTMsgError("Cannot read license file \"%s\" which should be included in the virtual system %u.",
1574 itD->second.c_str(), i);
1575 return RTEXITCODE_FAILURE;
1576 }
1577 }
1578 /* add cloud export settings */
1579 else if (itD->first == "cloudshape")
1580 pVSD->AddDescription(VirtualSystemDescriptionType_CloudInstanceShape,
1581 Bstr(itD->second).raw(),
1582 Bstr(itD->second).raw());
1583 else if (itD->first == "clouddomain")
1584 pVSD->AddDescription(VirtualSystemDescriptionType_CloudDomain,
1585 Bstr(itD->second).raw(),
1586 Bstr(itD->second).raw());
1587 else if (itD->first == "clouddisksize")
1588 pVSD->AddDescription(VirtualSystemDescriptionType_CloudBootDiskSize,
1589 Bstr(itD->second).raw(),
1590 Bstr(itD->second).raw());
1591 else if (itD->first == "cloudbucket")
1592 pVSD->AddDescription(VirtualSystemDescriptionType_CloudBucket,
1593 Bstr(itD->second).raw(),
1594 Bstr(itD->second).raw());
1595 else if (itD->first == "cloudocivcn")
1596 pVSD->AddDescription(VirtualSystemDescriptionType_CloudOCIVCN,
1597 Bstr(itD->second).raw(),
1598 Bstr(itD->second).raw());
1599 else if (itD->first == "cloudpublicip")
1600 pVSD->AddDescription(VirtualSystemDescriptionType_CloudPublicIP,
1601 Bstr(itD->second).raw(),
1602 Bstr(itD->second).raw());
1603 else if (itD->first == "cloudprivateip")
1604 pVSD->AddDescription(VirtualSystemDescriptionType_CloudPrivateIP,
1605 Bstr(itD->second).raw(), NULL);
1606 else if (itD->first == "cloudprofile")
1607 pVSD->AddDescription(VirtualSystemDescriptionType_CloudProfileName,
1608 Bstr(itD->second).raw(),
1609 Bstr(itD->second).raw());
1610 else if (itD->first == "cloudocisubnet")
1611 pVSD->AddDescription(VirtualSystemDescriptionType_CloudOCISubnet,
1612 Bstr(itD->second).raw(),
1613 Bstr(itD->second).raw());
1614 else if (itD->first == "cloudkeepobject")
1615 pVSD->AddDescription(VirtualSystemDescriptionType_CloudKeepObject,
1616 Bstr(itD->second).raw(),
1617 Bstr(itD->second).raw());
1618 else if (itD->first == "cloudlaunchmode")
1619 pVSD->AddDescription(VirtualSystemDescriptionType_CloudOCILaunchMode,
1620 Bstr(itD->second).raw(), NULL);
1621 else if (itD->first == "cloudlaunchinstance")
1622 pVSD->AddDescription(VirtualSystemDescriptionType_CloudLaunchInstance,
1623 Bstr(itD->second).raw(),
1624 Bstr(itD->second).raw());
1625 }
1626 }
1627
1628 VSDList.push_back(pVSD);//store vsd for the possible second stage
1629 }
1630
1631 if (FAILED(rc))
1632 break;
1633
1634 /* Query required passwords and supply them to the appliance. */
1635 com::SafeArray<BSTR> aIdentifiers;
1636
1637 CHECK_ERROR_BREAK(pAppliance, GetPasswordIds(ComSafeArrayAsOutParam(aIdentifiers)));
1638
1639 if (aIdentifiers.size() > 0)
1640 {
1641 com::SafeArray<BSTR> aPasswords(aIdentifiers.size());
1642 RTPrintf("Enter the passwords for the following identifiers to export the apppliance:\n");
1643 for (unsigned idxId = 0; idxId < aIdentifiers.size(); idxId++)
1644 {
1645 com::Utf8Str strPassword;
1646 Bstr bstrPassword;
1647 Bstr bstrId = aIdentifiers[idxId];
1648
1649 RTEXITCODE rcExit = readPasswordFromConsole(&strPassword, "Password ID %s:", Utf8Str(bstrId).c_str());
1650 if (rcExit == RTEXITCODE_FAILURE)
1651 {
1652 RTStrFree(pszAbsFilePath);
1653 return rcExit;
1654 }
1655
1656 bstrPassword = strPassword;
1657 bstrPassword.detachTo(&aPasswords[idxId]);
1658 }
1659
1660 CHECK_ERROR_BREAK(pAppliance, AddPasswords(ComSafeArrayAsInParam(aIdentifiers),
1661 ComSafeArrayAsInParam(aPasswords)));
1662 }
1663
1664 if (fManifest)
1665 options.push_back(ExportOptions_CreateManifest);
1666
1667 if (fExportISOImages)
1668 options.push_back(ExportOptions_ExportDVDImages);
1669
1670 ComPtr<IProgress> progress;
1671 CHECK_ERROR_BREAK(pAppliance, Write(Bstr(strOvfFormat).raw(),
1672 ComSafeArrayAsInParam(options),
1673 Bstr(pszAbsFilePath).raw(),
1674 progress.asOutParam()));
1675 RTStrFree(pszAbsFilePath);
1676
1677 rc = showProgress(progress);
1678 CHECK_PROGRESS_ERROR_RET(progress, ("Appliance write failed"), RTEXITCODE_FAILURE);
1679
1680 if (SUCCEEDED(rc))
1681 RTPrintf("Successfully exported %d machine(s).\n", llMachines.size());
1682
1683 /*
1684 * The second stage for the cloud case
1685 */
1686 if (actionType == CLOUD)
1687 {
1688 /* Launch the exported VM if the appropriate flag had been set on the first stage */
1689 for (std::list< ComPtr<IVirtualSystemDescription> >::iterator itVSD = VSDList.begin();
1690 itVSD != VSDList.end();
1691 ++itVSD)
1692 {
1693 ComPtr<IVirtualSystemDescription> pVSD = *itVSD;
1694
1695 com::SafeArray<VirtualSystemDescriptionType_T> retTypes;
1696 com::SafeArray<BSTR> aRefs;
1697 com::SafeArray<BSTR> aOvfValues;
1698 com::SafeArray<BSTR> aVBoxValues;
1699 com::SafeArray<BSTR> aExtraConfigValues;
1700
1701 CHECK_ERROR_BREAK(pVSD, GetDescriptionByType(VirtualSystemDescriptionType_CloudLaunchInstance,
1702 ComSafeArrayAsOutParam(retTypes),
1703 ComSafeArrayAsOutParam(aRefs),
1704 ComSafeArrayAsOutParam(aOvfValues),
1705 ComSafeArrayAsOutParam(aVBoxValues),
1706 ComSafeArrayAsOutParam(aExtraConfigValues)));
1707
1708 Utf8Str flagCloudLaunchInstance(Bstr(aVBoxValues[0]).raw());
1709 retTypes.setNull(); aRefs.setNull(); aOvfValues.setNull(); aVBoxValues.setNull(); aExtraConfigValues.setNull();
1710
1711 if (flagCloudLaunchInstance.equals("true"))
1712 {
1713 /* Getting the short provider name */
1714 Bstr bstrCloudProviderShortName(strOutputFile.c_str(), strOutputFile.find("://"));
1715
1716 ComPtr<IVirtualBox> pVirtualBox = a->virtualBox;
1717 ComPtr<ICloudProviderManager> pCloudProviderManager;
1718 CHECK_ERROR_BREAK(pVirtualBox, COMGETTER(CloudProviderManager)(pCloudProviderManager.asOutParam()));
1719
1720 ComPtr<ICloudProvider> pCloudProvider;
1721 CHECK_ERROR_BREAK(pCloudProviderManager,
1722 GetProviderByShortName(bstrCloudProviderShortName.raw(), pCloudProvider.asOutParam()));
1723
1724 CHECK_ERROR_BREAK(pVSD, GetDescriptionByType(VirtualSystemDescriptionType_CloudProfileName,
1725 ComSafeArrayAsOutParam(retTypes),
1726 ComSafeArrayAsOutParam(aRefs),
1727 ComSafeArrayAsOutParam(aOvfValues),
1728 ComSafeArrayAsOutParam(aVBoxValues),
1729 ComSafeArrayAsOutParam(aExtraConfigValues)));
1730
1731 ComPtr<ICloudProfile> pCloudProfile;
1732 CHECK_ERROR_BREAK(pCloudProvider, GetProfileByName(Bstr(aVBoxValues[0]).raw(), pCloudProfile.asOutParam()));
1733 retTypes.setNull(); aRefs.setNull(); aOvfValues.setNull(); aVBoxValues.setNull(); aExtraConfigValues.setNull();
1734
1735 ComObjPtr<ICloudClient> oCloudClient;
1736 CHECK_ERROR_BREAK(pCloudProfile, CreateCloudClient(oCloudClient.asOutParam()));
1737 RTPrintf("Creating a cloud instance...\n");
1738
1739 ComPtr<IProgress> progress1;
1740 CHECK_ERROR_BREAK(oCloudClient, LaunchVM(pVSD, progress1.asOutParam()));
1741 rc = showProgress(progress1);
1742 CHECK_PROGRESS_ERROR_RET(progress1, ("Creating the cloud instance failed"), RTEXITCODE_FAILURE);
1743
1744 if (SUCCEEDED(rc))
1745 {
1746 CHECK_ERROR_BREAK(pVSD, GetDescriptionByType(VirtualSystemDescriptionType_CloudInstanceId,
1747 ComSafeArrayAsOutParam(retTypes),
1748 ComSafeArrayAsOutParam(aRefs),
1749 ComSafeArrayAsOutParam(aOvfValues),
1750 ComSafeArrayAsOutParam(aVBoxValues),
1751 ComSafeArrayAsOutParam(aExtraConfigValues)));
1752
1753 RTPrintf("A cloud instance with id '%s' (provider '%s') was created\n",
1754 Utf8Str(Bstr(aVBoxValues[0]).raw()).c_str(),
1755 Utf8Str(bstrCloudProviderShortName.raw()).c_str());
1756 retTypes.setNull(); aRefs.setNull(); aOvfValues.setNull(); aVBoxValues.setNull(); aExtraConfigValues.setNull();
1757 }
1758 }
1759 }
1760 }
1761 } while (0);
1762
1763 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1764}
1765
1766#endif /* !VBOX_ONLY_DOCS */
1767
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