VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageImport.cpp@ 18775

Last change on this file since 18775 was 18775, checked in by vboxsync, 16 years ago

FE/VBoxManage-OVF: use absolute paths, cause the disk handling is based on the ovf file path

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.4 KB
Line 
1/* $Id: VBoxManageImport.cpp 18775 2009-04-06 15:19:22Z vboxsync $ */
2/** @file
3 * VBoxManage - The appliance-related commands.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#ifndef VBOX_ONLY_DOCS
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#ifndef VBOX_ONLY_DOCS
28#include <VBox/com/com.h>
29#include <VBox/com/string.h>
30#include <VBox/com/Guid.h>
31#include <VBox/com/array.h>
32#include <VBox/com/ErrorInfo.h>
33#include <VBox/com/errorprint2.h>
34#include <VBox/com/EventQueue.h>
35
36#include <VBox/com/VirtualBox.h>
37
38#include <list>
39#include <map>
40#endif /* !VBOX_ONLY_DOCS */
41
42#include <iprt/stream.h>
43#include <iprt/getopt.h>
44#include <iprt/ctype.h>
45#include <iprt/path.h>
46
47#include <VBox/log.h>
48
49#include "VBoxManage.h"
50using namespace com;
51
52
53// funcs
54///////////////////////////////////////////////////////////////////////////////
55
56typedef std::map<Utf8Str, Utf8Str> ArgsMap; // pairs of strings like "vmname" => "newvmname"
57typedef std::map<uint32_t, ArgsMap> ArgsMapsMap; // map of maps, one for each virtual system, sorted by index
58
59typedef std::map<uint32_t, bool> IgnoresMap; // pairs of numeric description entry indices
60typedef std::map<uint32_t, IgnoresMap> IgnoresMapsMap; // map of maps, one for each virtual system, sorted by index
61
62static bool findArgValue(Utf8Str &strOut,
63 ArgsMap *pmapArgs,
64 const Utf8Str &strKey)
65{
66 if (pmapArgs)
67 {
68 ArgsMap::iterator it;
69 it = pmapArgs->find(strKey);
70 if (it != pmapArgs->end())
71 {
72 strOut = it->second;
73 pmapArgs->erase(it);
74 return true;
75 }
76 }
77
78 return false;
79}
80
81static const RTGETOPTDEF g_aImportApplianceOptions[] =
82{
83 { "--dry-run", 'n', RTGETOPT_REQ_NOTHING },
84 { "-dry-run", 'n', RTGETOPT_REQ_NOTHING }, // deprecated
85 { "--dryrun", 'n', RTGETOPT_REQ_NOTHING },
86 { "-dryrun", 'n', RTGETOPT_REQ_NOTHING }, // deprecated
87 { "--detailed-progress", 'P', RTGETOPT_REQ_NOTHING },
88 { "-detailed-progress", 'P', RTGETOPT_REQ_NOTHING }, // deprecated
89 { "--vsys", 's', RTGETOPT_REQ_UINT32 },
90 { "-vsys", 's', RTGETOPT_REQ_UINT32 }, // deprecated
91 { "--ostype", 'o', RTGETOPT_REQ_STRING },
92 { "-ostype", 'o', RTGETOPT_REQ_STRING }, // deprecated
93 { "--vmname", 'V', RTGETOPT_REQ_STRING },
94 { "-vmname", 'V', RTGETOPT_REQ_STRING }, // deprecated
95 { "--eula", 'L', RTGETOPT_REQ_STRING },
96 { "-eula", 'L', RTGETOPT_REQ_STRING }, // deprecated
97 { "--unit", 'u', RTGETOPT_REQ_UINT32 },
98 { "-unit", 'u', RTGETOPT_REQ_UINT32 }, // deprecated
99 { "--ignore", 'x', RTGETOPT_REQ_NOTHING },
100 { "-ignore", 'x', RTGETOPT_REQ_NOTHING }, // deprecated
101 { "--scsitype", 'T', RTGETOPT_REQ_UINT32 },
102 { "-scsitype", 'T', RTGETOPT_REQ_UINT32 }, // deprecated
103 { "--type", 'T', RTGETOPT_REQ_UINT32 }, // deprecated
104 { "-type", 'T', RTGETOPT_REQ_UINT32 }, // deprecated
105};
106
107int handleImportAppliance(HandlerArg *a)
108{
109 HRESULT rc = S_OK;
110
111 Utf8Str strOvfFilename;
112 bool fExecute = true; // if true, then we actually do the import
113 uint32_t ulCurVsys = (uint32_t)-1;
114 uint32_t ulCurUnit = (uint32_t)-1;
115 // for each --vsys X command, maintain a map of command line items
116 // (we'll parse them later after interpreting the OVF, when we can
117 // actually check whether they make sense semantically)
118 ArgsMapsMap mapArgsMapsPerVsys;
119 IgnoresMapsMap mapIgnoresMapsPerVsys;
120
121 int c;
122 RTGETOPTUNION ValueUnion;
123 RTGETOPTSTATE GetState;
124 // start at 0 because main() has hacked both the argc and argv given to us
125 RTGetOptInit(&GetState, a->argc, a->argv, g_aImportApplianceOptions, RT_ELEMENTS(g_aImportApplianceOptions), 0, 0 /* fFlags */);
126 while ((c = RTGetOpt(&GetState, &ValueUnion)))
127 {
128 switch (c)
129 {
130 case 'n': // --dry-run
131 fExecute = false;
132 break;
133
134 case 'P': // --detailed-progress
135 g_fDetailedProgress = true;
136 break;
137
138 case 's': // --vsys
139 ulCurVsys = ValueUnion.u32;
140 ulCurUnit = (uint32_t)-1;
141 break;
142
143 case 'o': // --ostype
144 if (ulCurVsys == (uint32_t)-1)
145 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
146 mapArgsMapsPerVsys[ulCurVsys]["ostype"] = ValueUnion.psz;
147 break;
148
149 case 'V': // --vmname
150 if (ulCurVsys == (uint32_t)-1)
151 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
152 mapArgsMapsPerVsys[ulCurVsys]["vmname"] = ValueUnion.psz;
153 break;
154
155 case 'm': // --memory
156 if (ulCurVsys == (uint32_t)-1)
157 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
158 mapArgsMapsPerVsys[ulCurVsys]["memory"] = ValueUnion.psz;
159 break;
160
161 case 'u': // --unit
162 ulCurUnit = ValueUnion.u32;
163 break;
164
165 case 'x': // --ignore
166 if (ulCurVsys == (uint32_t)-1)
167 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
168 if (ulCurUnit == (uint32_t)-1)
169 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
170 mapIgnoresMapsPerVsys[ulCurVsys][ulCurUnit] = true;
171 break;
172
173 case 'T': // --scsitype
174 if (ulCurVsys == (uint32_t)-1)
175 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
176 if (ulCurUnit == (uint32_t)-1)
177 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
178 mapArgsMapsPerVsys[ulCurVsys][Utf8StrFmt("scsitype%u", ulCurUnit)] = ValueUnion.psz;
179 break;
180
181 case 'C': // --controller
182 if (ulCurVsys == (uint32_t)-1)
183 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
184 if (ulCurUnit == (uint32_t)-1)
185 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
186 mapArgsMapsPerVsys[ulCurVsys][Utf8StrFmt("controller%u", ulCurUnit)] = ValueUnion.psz;
187 break;
188
189 case VINF_GETOPT_NOT_OPTION:
190 if (!strOvfFilename)
191 strOvfFilename = ValueUnion.psz;
192 else
193 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Invalid parameter '%s'", ValueUnion.psz);
194 break;
195
196 default:
197 if (c > 0)
198 {
199 if (RT_C_IS_PRINT(c))
200 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Invalid option -%c", c);
201 else
202 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Invalid option case %i", c);
203 }
204 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
205 return errorSyntax(USAGE_IMPORTAPPLIANCE, "unknown option: %s\n", ValueUnion.psz);
206 else if (ValueUnion.pDef)
207 return errorSyntax(USAGE_IMPORTAPPLIANCE, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
208 else
209 return errorSyntax(USAGE_IMPORTAPPLIANCE, "error: %Rrs", c);
210 }
211 }
212
213 if (!strOvfFilename)
214 return errorSyntax(USAGE_IMPORTAPPLIANCE, "Not enough arguments for \"import\" command.");
215
216 do
217 {
218 ComPtr<IAppliance> pAppliance;
219 CHECK_ERROR_BREAK(a->virtualBox, CreateAppliance(pAppliance.asOutParam()));
220
221 char *pszAbsFilePath = RTPathAbsDup(strOvfFilename.c_str());
222 CHECK_ERROR_BREAK(pAppliance, Read(Bstr(pszAbsFilePath)));
223 RTStrFree(pszAbsFilePath);
224
225 // call interpret(); this can yield both warnings and errors, so we need
226 // to tinker with the error info a bit
227 RTPrintf("Interpreting %s...\n", strOvfFilename.c_str());
228 rc = pAppliance->Interpret();
229 com::ErrorInfo info0(pAppliance);
230
231 com::SafeArray<BSTR> aWarnings;
232 if (SUCCEEDED(pAppliance->GetWarnings(ComSafeArrayAsOutParam(aWarnings))))
233 {
234 size_t cWarnings = aWarnings.size();
235 for (unsigned i = 0; i < cWarnings; ++i)
236 {
237 Bstr bstrWarning(aWarnings[i]);
238 RTPrintf("WARNING: %ls.\n", bstrWarning.raw());
239 }
240 }
241
242 if (FAILED(rc)) // during interpret, after printing warnings
243 {
244 com::GluePrintErrorInfo(info0);
245 com::GluePrintErrorContext("Interpret", __FILE__, __LINE__);
246 break;
247 }
248
249 RTPrintf("OK.\n");
250
251 // fetch all disks
252 com::SafeArray<BSTR> retDisks;
253 CHECK_ERROR_BREAK(pAppliance,
254 COMGETTER(Disks)(ComSafeArrayAsOutParam(retDisks)));
255 if (retDisks.size() > 0)
256 {
257 RTPrintf("Disks:");
258 for (unsigned i = 0; i < retDisks.size(); i++)
259 RTPrintf(" %ls", retDisks[i]);
260 RTPrintf("\n");
261 }
262
263 // fetch virtual system descriptions
264 com::SafeIfaceArray<IVirtualSystemDescription> aVirtualSystemDescriptions;
265 CHECK_ERROR_BREAK(pAppliance,
266 COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aVirtualSystemDescriptions)));
267
268 size_t cVirtualSystemDescriptions = aVirtualSystemDescriptions.size();
269
270 // match command line arguments with virtual system descriptions;
271 // this is only to sort out invalid indices at this time
272 ArgsMapsMap::const_iterator it;
273 for (it = mapArgsMapsPerVsys.begin();
274 it != mapArgsMapsPerVsys.end();
275 ++it)
276 {
277 uint32_t ulVsys = it->first;
278 if (ulVsys >= cVirtualSystemDescriptions)
279 return errorSyntax(USAGE_IMPORTAPPLIANCE,
280 "Invalid index %RI32 with -vsys option; the OVF contains only %zu virtual system(s).",
281 ulVsys, cVirtualSystemDescriptions);
282 }
283
284 uint32_t cLicensesInTheWay = 0;
285
286 // dump virtual system descriptions and match command-line arguments
287 if (cVirtualSystemDescriptions > 0)
288 {
289 for (unsigned i = 0; i < cVirtualSystemDescriptions; ++i)
290 {
291 com::SafeArray<VirtualSystemDescriptionType_T> retTypes;
292 com::SafeArray<BSTR> aRefs;
293 com::SafeArray<BSTR> aOvfValues;
294 com::SafeArray<BSTR> aVboxValues;
295 com::SafeArray<BSTR> aExtraConfigValues;
296 CHECK_ERROR_BREAK(aVirtualSystemDescriptions[i],
297 GetDescription(ComSafeArrayAsOutParam(retTypes),
298 ComSafeArrayAsOutParam(aRefs),
299 ComSafeArrayAsOutParam(aOvfValues),
300 ComSafeArrayAsOutParam(aVboxValues),
301 ComSafeArrayAsOutParam(aExtraConfigValues)));
302
303 RTPrintf("Virtual system %u:\n", i);
304
305 // look up the corresponding command line options, if any
306 ArgsMap *pmapArgs = NULL;
307 ArgsMapsMap::iterator itm = mapArgsMapsPerVsys.find(i);
308 if (itm != mapArgsMapsPerVsys.end())
309 pmapArgs = &itm->second;
310
311 // this collects the final values for setFinalValues()
312 com::SafeArray<BOOL> aEnabled(retTypes.size());
313 com::SafeArray<BSTR> aFinalValues(retTypes.size());
314
315 for (unsigned a = 0; a < retTypes.size(); ++a)
316 {
317 VirtualSystemDescriptionType_T t = retTypes[a];
318
319 Utf8Str strOverride;
320
321 Bstr bstrFinalValue = aVboxValues[a];
322
323 bool fIgnoreThis = mapIgnoresMapsPerVsys[i][a];
324
325 aEnabled[a] = true;
326
327 switch (t)
328 {
329 case VirtualSystemDescriptionType_Name:
330 if (findArgValue(strOverride, pmapArgs, "vmname"))
331 {
332 bstrFinalValue = strOverride;
333 RTPrintf("%2u: VM name specified with --vmname: \"%ls\"\n",
334 a, bstrFinalValue.raw());
335 }
336 else
337 RTPrintf("%2u: Suggested VM name \"%ls\""
338 "\n (change with \"--vsys %u --vmname <name>\")\n",
339 a, bstrFinalValue.raw(), i);
340 break;
341
342 case VirtualSystemDescriptionType_OS:
343 if (findArgValue(strOverride, pmapArgs, "ostype"))
344 {
345 bstrFinalValue = strOverride;
346 RTPrintf("%2u: OS type specified with --ostype: \"%ls\"\n",
347 a, bstrFinalValue.raw());
348 }
349 else
350 RTPrintf("%2u: Suggested OS type: \"%ls\""
351 "\n (change with \"--vsys %u --ostype <type>\"; use \"list ostypes\" to list all)\n",
352 a, bstrFinalValue.raw(), i);
353 break;
354
355 case VirtualSystemDescriptionType_Description:
356 RTPrintf("%2u: Description: \"%ls\"\n",
357 a, bstrFinalValue.raw());
358 break;
359
360 case VirtualSystemDescriptionType_License:
361 ++cLicensesInTheWay;
362 if (findArgValue(strOverride, pmapArgs, "eula"))
363 {
364 if (strOverride == "show")
365 {
366 RTPrintf("%2u: End-user license agreement"
367 "\n (accept with \"--vsys %u --eula accept\"):"
368 "\n\n%ls\n\n",
369 a, i, bstrFinalValue.raw());
370 }
371 else if (strOverride == "accept")
372 {
373 RTPrintf("%2u: End-user license agreement (accepted)\n",
374 a);
375 --cLicensesInTheWay;
376 }
377 else
378 return errorSyntax(USAGE_IMPORTAPPLIANCE,
379 "Argument to --eula must be either \"show\" or \"accept\".");
380 }
381 else
382 RTPrintf("%2u: End-user license agreement"
383 "\n (display with \"--vsys %u -eula show\";"
384 "\n accept with \"--vsys %u -eula accept\")\n",
385 a, i, i);
386 break;
387
388 case VirtualSystemDescriptionType_CPU:
389 RTPrintf("%2u: Number of CPUs (ignored): %ls\n",
390 a, aVboxValues[a]);
391 break;
392
393 case VirtualSystemDescriptionType_Memory:
394 {
395 if (findArgValue(strOverride, pmapArgs, "memory"))
396 {
397 uint32_t ulMemMB;
398 if (VINF_SUCCESS == strOverride.toInt(ulMemMB))
399 {
400 bstrFinalValue = strOverride;
401 RTPrintf("%2u: Guest memory specified with --memory: %ls MB\n",
402 a, bstrFinalValue.raw());
403 }
404 else
405 return errorSyntax(USAGE_IMPORTAPPLIANCE,
406 "Argument to --memory option must be a non-negative number.");
407 }
408 else
409 RTPrintf("%2u: Guest memory: %ls MB\n (change with \"--vsys %u --memory <MB>\")\n",
410 a, bstrFinalValue.raw(), i);
411 }
412 break;
413
414 case VirtualSystemDescriptionType_HardDiskControllerIDE:
415 if (fIgnoreThis)
416 {
417 RTPrintf("%2u: IDE controller, type %ls -- disabled\n",
418 a,
419 aVboxValues[a]);
420 aEnabled[a] = false;
421 }
422 else
423 RTPrintf("%2u: IDE controller, type %ls"
424 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
425 a,
426 aVboxValues[a],
427 i, a);
428 break;
429
430 case VirtualSystemDescriptionType_HardDiskControllerSATA:
431 if (fIgnoreThis)
432 {
433 RTPrintf("%2u: SATA controller, type %ls -- disabled\n",
434 a,
435 aVboxValues[a]);
436 aEnabled[a] = false;
437 }
438 else
439 RTPrintf("%2u: SATA controller, type %ls"
440 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
441 a,
442 aVboxValues[a],
443 i, a);
444 break;
445
446 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
447 if (fIgnoreThis)
448 {
449 RTPrintf("%2u: SCSI controller, type %ls -- disabled\n",
450 a,
451 aVboxValues[a]);
452 aEnabled[a] = false;
453 }
454 else
455 {
456 Utf8StrFmt strTypeArg("scsitype%u", a);
457 if (findArgValue(strOverride, pmapArgs, strTypeArg))
458 {
459 bstrFinalValue = strOverride;
460 RTPrintf("%2u: SCSI controller, type set with --unit %u --scsitype: \"%ls\"\n",
461 a,
462 a,
463 bstrFinalValue.raw());
464 }
465 else
466 RTPrintf("%2u: SCSI controller, type %ls"
467 "\n (change with \"--vsys %u --unit %u --scsitype {BusLogic|LsiLogic}\";"
468 "\n disable with \"--vsys %u --unit %u --ignore\")\n",
469 a,
470 aVboxValues[a],
471 i, a, i, a);
472 }
473 break;
474
475 case VirtualSystemDescriptionType_HardDiskImage:
476 if (fIgnoreThis)
477 {
478 RTPrintf("%2u: Hard disk image: source image=%ls -- disabled\n",
479 a,
480 aOvfValues[a]);
481 aEnabled[a] = false;
482 }
483 else
484 {
485 Utf8StrFmt strTypeArg("controller%u", a);
486 if (findArgValue(strOverride, pmapArgs, strTypeArg))
487 {
488 // strOverride now has the controller index as a number, but we
489 // need a "controller=X" format string
490 strOverride = Utf8StrFmt("controller=%s", strOverride.c_str());
491 Bstr bstrExtraConfigValue = strOverride;
492 bstrExtraConfigValue.detachTo(&aExtraConfigValues[a]);
493 RTPrintf("%2u: Hard disk image: source image=%ls, target path=%ls, %ls\n",
494 a,
495 aOvfValues[a],
496 aVboxValues[a],
497 aExtraConfigValues[a]);
498 }
499 else
500 RTPrintf("%2u: Hard disk image: source image=%ls, target path=%ls, %ls"
501 "\n (change controller with \"--vsys %u --unit %u --controller <id>\";"
502 "\n disable with \"--vsys %u --unit %u --ignore\")\n",
503 a,
504 aOvfValues[a],
505 aVboxValues[a],
506 aExtraConfigValues[a],
507 i, a, i, a);
508 }
509 break;
510
511 case VirtualSystemDescriptionType_CDROM:
512 if (fIgnoreThis)
513 {
514 RTPrintf("%2u: CD-ROM -- disabled\n",
515 a);
516 aEnabled[a] = false;
517 }
518 else
519 RTPrintf("%2u: CD-ROM"
520 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
521 a, i, a);
522 break;
523
524 case VirtualSystemDescriptionType_Floppy:
525 if (fIgnoreThis)
526 {
527 RTPrintf("%2u: Floppy -- disabled\n",
528 a);
529 aEnabled[a] = false;
530 }
531 else
532 RTPrintf("%2u: Floppy"
533 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
534 a, i, a);
535 break;
536
537 case VirtualSystemDescriptionType_NetworkAdapter:
538 RTPrintf("%2u: Network adapter: orig %ls, config %ls, extra %ls\n", // @todo implement once we have a plan for the back-end
539 a,
540 aOvfValues[a],
541 aVboxValues[a],
542 aExtraConfigValues[a]);
543 break;
544
545 case VirtualSystemDescriptionType_USBController:
546 if (fIgnoreThis)
547 {
548 RTPrintf("%2u: USB controller -- disabled\n",
549 a);
550 aEnabled[a] = false;
551 }
552 else
553 RTPrintf("%2u: USB controller"
554 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
555 a, i, a);
556 break;
557
558 case VirtualSystemDescriptionType_SoundCard:
559 if (fIgnoreThis)
560 {
561 RTPrintf("%2u: Sound card \"%ls\" -- disabled\n",
562 a,
563 aOvfValues[a]);
564 aEnabled[a] = false;
565 }
566 else
567 RTPrintf("%2u: Sound card (appliance expects \"%ls\", can change on import)"
568 "\n (disable with \"--vsys %u --unit %u --ignore\")\n",
569 a,
570 aOvfValues[a],
571 i,
572 a);
573 break;
574 }
575
576 bstrFinalValue.detachTo(&aFinalValues[a]);
577 }
578
579 if (fExecute)
580 CHECK_ERROR_BREAK(aVirtualSystemDescriptions[i],
581 SetFinalValues(ComSafeArrayAsInParam(aEnabled),
582 ComSafeArrayAsInParam(aFinalValues),
583 ComSafeArrayAsInParam(aExtraConfigValues)));
584
585 } // for (unsigned i = 0; i < cVirtualSystemDescriptions; ++i)
586
587 if (cLicensesInTheWay == 1)
588 RTPrintf("ERROR: Cannot import until the license agreement listed above is accepted.\n");
589 else if (cLicensesInTheWay > 1)
590 RTPrintf("ERROR: Cannot import until the %c license agreements listed above are accepted.\n", cLicensesInTheWay);
591
592 if (!cLicensesInTheWay && fExecute)
593 {
594 // go!
595 ComPtr<IProgress> progress;
596 CHECK_ERROR_BREAK(pAppliance,
597 ImportMachines(progress.asOutParam()));
598
599 showProgress(progress);
600
601 if (SUCCEEDED(rc))
602 progress->COMGETTER(ResultCode)(&rc);
603
604 if (FAILED(rc))
605 {
606 com::ProgressErrorInfo info(progress);
607 com::GluePrintErrorInfo(info);
608 com::GluePrintErrorContext("ImportAppliance", __FILE__, __LINE__);
609 }
610 else
611 RTPrintf("Successfully imported the appliance.\n");
612 }
613 } // end if (aVirtualSystemDescriptions.size() > 0)
614 } while (0);
615
616 return SUCCEEDED(rc) ? 0 : 1;
617}
618
619static const RTGETOPTDEF g_aExportOptions[]
620 = {
621 { "--output", 'o', RTGETOPT_REQ_STRING },
622 };
623
624int handleExportAppliance(HandlerArg *a)
625{
626 HRESULT rc = S_OK;
627
628 Utf8Str strOutputFile;
629 std::list< ComPtr<IMachine> > llMachines;
630
631 do
632 {
633 int c;
634
635 RTGETOPTUNION ValueUnion;
636 RTGETOPTSTATE GetState;
637 // start at 0 because main() has hacked both the argc and argv given to us
638 RTGetOptInit(&GetState, a->argc, a->argv, g_aExportOptions,
639 RT_ELEMENTS(g_aExportOptions), 0, 0 /* fFlags */);
640 while ((c = RTGetOpt(&GetState, &ValueUnion)))
641 {
642 switch (c)
643 {
644 case 'o': // --output
645 if (strOutputFile.length())
646 return errorSyntax(USAGE_EXPORTAPPLIANCE, "You can only specify --output once.");
647 else
648 strOutputFile = ValueUnion.psz;
649 break;
650
651 case VINF_GETOPT_NOT_OPTION:
652 {
653 Utf8Str strMachine(ValueUnion.psz);
654 // must be machine: try UUID or name
655 ComPtr<IMachine> machine;
656 /* assume it's a UUID */
657 rc = a->virtualBox->GetMachine(Guid(strMachine), machine.asOutParam());
658 if (FAILED(rc) || !machine)
659 {
660 /* must be a name */
661 CHECK_ERROR_BREAK(a->virtualBox, FindMachine(Bstr(strMachine), machine.asOutParam()));
662 }
663
664 if (machine)
665 llMachines.push_back(machine);
666 }
667 break;
668
669 default:
670 if (c > 0)
671 {
672 if (RT_C_IS_GRAPH(c))
673 return errorSyntax(USAGE_EXPORTAPPLIANCE, "unhandled option: -%c", c);
674 else
675 return errorSyntax(USAGE_EXPORTAPPLIANCE, "unhandled option: %i", c);
676 }
677 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
678 return errorSyntax(USAGE_EXPORTAPPLIANCE, "unknown option: %s", ValueUnion.psz);
679 else if (ValueUnion.pDef)
680 return errorSyntax(USAGE_EXPORTAPPLIANCE, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
681 else
682 return errorSyntax(USAGE_EXPORTAPPLIANCE, "%Rrs", c);
683 }
684
685 if (FAILED(rc))
686 break;
687 }
688
689 if (FAILED(rc))
690 break;
691
692 if (llMachines.size() == 0)
693 return errorSyntax(USAGE_EXPORTAPPLIANCE, "At least one machine must be specified with the export command.");
694 if (!strOutputFile.length())
695 return errorSyntax(USAGE_EXPORTAPPLIANCE, "Missing --output argument with export command.");
696
697 ComPtr<IAppliance> pAppliance;
698 CHECK_ERROR_BREAK(a->virtualBox, CreateAppliance(pAppliance.asOutParam()));
699
700 std::list< ComPtr<IMachine> >::iterator itM;
701 for (itM = llMachines.begin();
702 itM != llMachines.end();
703 ++itM)
704 {
705 ComPtr<IMachine> pMachine = *itM;
706 ComPtr<IVirtualSystemDescription> pVSD;
707 CHECK_ERROR_BREAK(pMachine, Export(pAppliance, pVSD.asOutParam()));
708 }
709
710 if (FAILED(rc))
711 break;
712
713 ComPtr<IProgress> progress;
714 char *pszAbsFilePath = RTPathAbsDup(strOutputFile.c_str());
715 CHECK_ERROR_BREAK(pAppliance, Write(Bstr("ovf-0.9"), Bstr(pszAbsFilePath), progress.asOutParam()));
716 RTStrFree(pszAbsFilePath);
717
718 showProgress(progress);
719
720 if (SUCCEEDED(rc))
721 progress->COMGETTER(ResultCode)(&rc);
722
723 if (FAILED(rc))
724 {
725 com::ProgressErrorInfo info(progress);
726 com::GluePrintErrorInfo(info);
727 com::GluePrintErrorContext("Write", __FILE__, __LINE__);
728 }
729 else
730 RTPrintf("Successfully exported %d machine(s).\n", llMachines.size());
731
732 } while (0);
733
734 return SUCCEEDED(rc) ? 0 : 1;
735}
736
737#endif /* !VBOX_ONLY_DOCS */
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