1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
|
/* $Id: VBoxManageImport.cpp 29994 2010-06-02 13:04:36Z vboxsync $ */
/** @file
* VBoxManage - The appliance-related commands.
*/
/*
* Copyright (C) 2009 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#ifndef VBOX_ONLY_DOCS
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifndef VBOX_ONLY_DOCS
#include <VBox/com/com.h>
#include <VBox/com/string.h>
#include <VBox/com/Guid.h>
#include <VBox/com/array.h>
#include <VBox/com/ErrorInfo.h>
#include <VBox/com/errorprint.h>
#include <VBox/com/EventQueue.h>
#include <VBox/com/VirtualBox.h>
#include <list>
#include <map>
#endif /* !VBOX_ONLY_DOCS */
#include <iprt/stream.h>
#include <iprt/getopt.h>
#include <iprt/ctype.h>
#include <iprt/path.h>
#include <iprt/file.h>
#include <VBox/log.h>
#include <VBox/param.h>
#include "VBoxManage.h"
using namespace com;
// funcs
///////////////////////////////////////////////////////////////////////////////
typedef std::map<Utf8Str, Utf8Str> ArgsMap; // pairs of strings like "vmname" => "newvmname"
typedef std::map<uint32_t, ArgsMap> ArgsMapsMap; // map of maps, one for each virtual system, sorted by index
typedef std::map<uint32_t, bool> IgnoresMap; // pairs of numeric description entry indices
typedef std::map<uint32_t, IgnoresMap> IgnoresMapsMap; // map of maps, one for each virtual system, sorted by index
static bool findArgValue(Utf8Str &strOut,
ArgsMap *pmapArgs,
const Utf8Str &strKey)
{
if (pmapArgs)
{
ArgsMap::iterator it;
it = pmapArgs->find(strKey);
if (it != pmapArgs->end())
{
strOut = it->second;
pmapArgs->erase(it);
return true;
}
}
return false;
}
static const RTGETOPTDEF g_aImportApplianceOptions[] =
{
{ "--dry-run", 'n', RTGETOPT_REQ_NOTHING },
{ "-dry-run", 'n', RTGETOPT_REQ_NOTHING }, // deprecated
{ "--dryrun", 'n', RTGETOPT_REQ_NOTHING },
{ "-dryrun", 'n', RTGETOPT_REQ_NOTHING }, // deprecated
{ "--detailed-progress", 'P', RTGETOPT_REQ_NOTHING },
{ "-detailed-progress", 'P', RTGETOPT_REQ_NOTHING }, // deprecated
{ "--vsys", 's', RTGETOPT_REQ_UINT32 },
{ "-vsys", 's', RTGETOPT_REQ_UINT32 }, // deprecated
{ "--ostype", 'o', RTGETOPT_REQ_STRING },
{ "-ostype", 'o', RTGETOPT_REQ_STRING }, // deprecated
{ "--vmname", 'V', RTGETOPT_REQ_STRING },
{ "-vmname", 'V', RTGETOPT_REQ_STRING }, // deprecated
{ "--memory", 'm', RTGETOPT_REQ_STRING },
{ "-memory", 'm', RTGETOPT_REQ_STRING }, // deprecated
{ "--cpus", 'c', RTGETOPT_REQ_STRING },
{ "--description", 'd', RTGETOPT_REQ_STRING },
{ "--eula", 'L', RTGETOPT_REQ_STRING },
{ "-eula", 'L', RTGETOPT_REQ_STRING }, // deprecated
{ "--unit", 'u', RTGETOPT_REQ_UINT32 },
{ "-unit", 'u', RTGETOPT_REQ_UINT32 }, // deprecated
{ "--ignore", 'x', RTGETOPT_REQ_NOTHING },
{ "-ignore", 'x', RTGETOPT_REQ_NOTHING }, // deprecated
{ "--scsitype", 'T', RTGETOPT_REQ_UINT32 },
{ "-scsitype", 'T', RTGETOPT_REQ_UINT32 }, // deprecated
{ "--type", 'T', RTGETOPT_REQ_UINT32 }, // deprecated
{ "-type", 'T', RTGETOPT_REQ_UINT32 }, // deprecated
};
int handleImportAppliance(HandlerArg *arg)
{
HRESULT rc = S_OK;
Utf8Str strOvfFilename;
bool fExecute = true; // if true, then we actually do the import
uint32_t ulCurVsys = (uint32_t)-1;
uint32_t ulCurUnit = (uint32_t)-1;
// for each --vsys X command, maintain a map of command line items
// (we'll parse them later after interpreting the OVF, when we can
// actually check whether they make sense semantically)
ArgsMapsMap mapArgsMapsPerVsys;
IgnoresMapsMap mapIgnoresMapsPerVsys;
int c;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
// start at 0 because main() has hacked both the argc and argv given to us
RTGetOptInit(&GetState, arg->argc, arg->argv, g_aImportApplianceOptions, RT_ELEMENTS(g_aImportApplianceOptions),
0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
while ((c = RTGetOpt(&GetState, &ValueUnion)))
{
switch (c)
{
case 'n': // --dry-run
fExecute = false;
break;
case 'P': // --detailed-progress
g_fDetailedProgress = true;
break;
case 's': // --vsys
ulCurVsys = ValueUnion.u32;
ulCurUnit = (uint32_t)-1;
break;
case 'o': // --ostype
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["ostype"] = ValueUnion.psz;
break;
case 'V': // --vmname
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["vmname"] = ValueUnion.psz;
break;
case 'd': // --description
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["description"] = ValueUnion.psz;
break;
case 'L': // --eula
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["eula"] = ValueUnion.psz;
break;
case 'm': // --memory
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["memory"] = ValueUnion.psz;
break;
case 'c': // --cpus
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["cpus"] = ValueUnion.psz;
break;
case 'u': // --unit
ulCurUnit = ValueUnion.u32;
break;
case 'x': // --ignore
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
if (ulCurUnit == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
mapIgnoresMapsPerVsys[ulCurVsys][ulCurUnit] = true;
break;
case 'T': // --scsitype
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
if (ulCurUnit == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys][Utf8StrFmt("scsitype%u", ulCurUnit)] = ValueUnion.psz;
break;
case 'C': // --controller
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
if (ulCurUnit == (uint32_t)-1)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Option \"%s\" requires preceding --unit argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys][Utf8StrFmt("controller%u", ulCurUnit)] = ValueUnion.psz;
break;
case VINF_GETOPT_NOT_OPTION:
if (strOvfFilename.isEmpty())
strOvfFilename = ValueUnion.psz;
else
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Invalid parameter '%s'", ValueUnion.psz);
break;
default:
if (c > 0)
{
if (RT_C_IS_PRINT(c))
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Invalid option -%c", c);
else
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Invalid option case %i", c);
}
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "unknown option: %s\n", ValueUnion.psz);
else if (ValueUnion.pDef)
return errorSyntax(USAGE_IMPORTAPPLIANCE, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
else
return errorSyntax(USAGE_IMPORTAPPLIANCE, "error: %Rrs", c);
}
}
if (strOvfFilename.isEmpty())
return errorSyntax(USAGE_IMPORTAPPLIANCE, "Not enough arguments for \"import\" command.");
do
{
ComPtr<IAppliance> pAppliance;
CHECK_ERROR_BREAK(arg->virtualBox, CreateAppliance(pAppliance.asOutParam()));
char *pszAbsFilePath;
if (strOvfFilename.startsWith("S3://", iprt::MiniString::CaseInsensitive) ||
strOvfFilename.startsWith("SunCloud://", iprt::MiniString::CaseInsensitive) ||
strOvfFilename.startsWith("webdav://", iprt::MiniString::CaseInsensitive))
pszAbsFilePath = RTStrDup(strOvfFilename.c_str());
else
pszAbsFilePath = RTPathAbsDup(strOvfFilename.c_str());
ComPtr<IProgress> progressRead;
CHECK_ERROR_BREAK(pAppliance, Read(Bstr(pszAbsFilePath), progressRead.asOutParam()));
RTStrFree(pszAbsFilePath);
rc = showProgress(progressRead);
if (FAILED(rc))
{
com::ProgressErrorInfo info(progressRead);
com::GluePrintErrorInfo(info);
com::GluePrintErrorContext("ImportAppliance", __FILE__, __LINE__);
return 1;
}
Bstr path; /* fetch the path, there is stuff like username/password removed if any */
CHECK_ERROR_BREAK(pAppliance, COMGETTER(Path)(path.asOutParam()));
// call interpret(); this can yield both warnings and errors, so we need
// to tinker with the error info a bit
RTPrintf("Interpreting %ls...\n", path.raw());
rc = pAppliance->Interpret();
com::ErrorInfo info0(pAppliance);
com::SafeArray<BSTR> aWarnings;
if (SUCCEEDED(pAppliance->GetWarnings(ComSafeArrayAsOutParam(aWarnings))))
{
size_t cWarnings = aWarnings.size();
for (unsigned i = 0; i < cWarnings; ++i)
{
Bstr bstrWarning(aWarnings[i]);
RTPrintf("WARNING: %ls.\n", bstrWarning.raw());
}
}
if (FAILED(rc)) // during interpret, after printing warnings
{
com::GluePrintErrorInfo(info0);
com::GluePrintErrorContext("Interpret", __FILE__, __LINE__);
break;
}
RTPrintf("OK.\n");
// fetch all disks
com::SafeArray<BSTR> retDisks;
CHECK_ERROR_BREAK(pAppliance,
COMGETTER(Disks)(ComSafeArrayAsOutParam(retDisks)));
if (retDisks.size() > 0)
{
RTPrintf("Disks:");
for (unsigned i = 0; i < retDisks.size(); i++)
RTPrintf(" %ls", retDisks[i]);
RTPrintf("\n");
}
// fetch virtual system descriptions
com::SafeIfaceArray<IVirtualSystemDescription> aVirtualSystemDescriptions;
CHECK_ERROR_BREAK(pAppliance,
COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aVirtualSystemDescriptions)));
size_t cVirtualSystemDescriptions = aVirtualSystemDescriptions.size();
// match command line arguments with virtual system descriptions;
// this is only to sort out invalid indices at this time
ArgsMapsMap::const_iterator it;
for (it = mapArgsMapsPerVsys.begin();
it != mapArgsMapsPerVsys.end();
++it)
{
uint32_t ulVsys = it->first;
if (ulVsys >= cVirtualSystemDescriptions)
return errorSyntax(USAGE_IMPORTAPPLIANCE,
"Invalid index %RI32 with -vsys option; the OVF contains only %zu virtual system(s).",
ulVsys, cVirtualSystemDescriptions);
}
uint32_t cLicensesInTheWay = 0;
// dump virtual system descriptions and match command-line arguments
if (cVirtualSystemDescriptions > 0)
{
for (unsigned i = 0; i < cVirtualSystemDescriptions; ++i)
{
com::SafeArray<VirtualSystemDescriptionType_T> retTypes;
com::SafeArray<BSTR> aRefs;
com::SafeArray<BSTR> aOvfValues;
com::SafeArray<BSTR> aVboxValues;
com::SafeArray<BSTR> aExtraConfigValues;
CHECK_ERROR_BREAK(aVirtualSystemDescriptions[i],
GetDescription(ComSafeArrayAsOutParam(retTypes),
ComSafeArrayAsOutParam(aRefs),
ComSafeArrayAsOutParam(aOvfValues),
ComSafeArrayAsOutParam(aVboxValues),
ComSafeArrayAsOutParam(aExtraConfigValues)));
RTPrintf("Virtual system %u:\n", i);
// look up the corresponding command line options, if any
ArgsMap *pmapArgs = NULL;
ArgsMapsMap::iterator itm = mapArgsMapsPerVsys.find(i);
if (itm != mapArgsMapsPerVsys.end())
pmapArgs = &itm->second;
// this collects the final values for setFinalValues()
com::SafeArray<BOOL> aEnabled(retTypes.size());
com::SafeArray<BSTR> aFinalValues(retTypes.size());
for (unsigned a = 0; a < retTypes.size(); ++a)
{
VirtualSystemDescriptionType_T t = retTypes[a];
Utf8Str strOverride;
Bstr bstrFinalValue = aVboxValues[a];
bool fIgnoreThis = mapIgnoresMapsPerVsys[i][a];
aEnabled[a] = true;
switch (t)
{
case VirtualSystemDescriptionType_OS:
if (findArgValue(strOverride, pmapArgs, "ostype"))
{
bstrFinalValue = strOverride;
RTPrintf("%2u: OS type specified with --ostype: \"%ls\"\n",
a, bstrFinalValue.raw());
}
else
RTPrintf("%2u: Suggested OS type: \"%ls\""
"\n (change with \"--vsys %u --ostype <type>\"; use \"list ostypes\" to list all possible values)\n",
a, bstrFinalValue.raw(), i);
break;
case VirtualSystemDescriptionType_Name:
if (findArgValue(strOverride, pmapArgs, "vmname"))
{
bstrFinalValue = strOverride;
RTPrintf("%2u: VM name specified with --vmname: \"%ls\"\n",
a, bstrFinalValue.raw());
}
else
RTPrintf("%2u: Suggested VM name \"%ls\""
"\n (change with \"--vsys %u --vmname <name>\")\n",
a, bstrFinalValue.raw(), i);
break;
case VirtualSystemDescriptionType_Product:
RTPrintf("%2u: Product (ignored): %ls\n",
a, aVboxValues[a]);
break;
case VirtualSystemDescriptionType_ProductUrl:
RTPrintf("%2u: ProductUrl (ignored): %ls\n",
a, aVboxValues[a]);
break;
case VirtualSystemDescriptionType_Vendor:
RTPrintf("%2u: Vendor (ignored): %ls\n",
a, aVboxValues[a]);
break;
case VirtualSystemDescriptionType_VendorUrl:
RTPrintf("%2u: VendorUrl (ignored): %ls\n",
a, aVboxValues[a]);
break;
case VirtualSystemDescriptionType_Version:
RTPrintf("%2u: Version (ignored): %ls\n",
a, aVboxValues[a]);
break;
case VirtualSystemDescriptionType_Description:
if (findArgValue(strOverride, pmapArgs, "description"))
{
bstrFinalValue = strOverride;
RTPrintf("%2u: Description specified with --description: \"%ls\"\n",
a, bstrFinalValue.raw());
}
else
RTPrintf("%2u: Description \"%ls\""
"\n (change with \"--vsys %u --description <desc>\")\n",
a, bstrFinalValue.raw(), i);
break;
case VirtualSystemDescriptionType_License:
++cLicensesInTheWay;
if (findArgValue(strOverride, pmapArgs, "eula"))
{
if (strOverride == "show")
{
RTPrintf("%2u: End-user license agreement"
"\n (accept with \"--vsys %u --eula accept\"):"
"\n\n%ls\n\n",
a, i, bstrFinalValue.raw());
}
else if (strOverride == "accept")
{
RTPrintf("%2u: End-user license agreement (accepted)\n",
a);
--cLicensesInTheWay;
}
else
return errorSyntax(USAGE_IMPORTAPPLIANCE,
"Argument to --eula must be either \"show\" or \"accept\".");
}
else
RTPrintf("%2u: End-user license agreement"
"\n (display with \"--vsys %u --eula show\";"
"\n accept with \"--vsys %u --eula accept\")\n",
a, i, i);
break;
case VirtualSystemDescriptionType_CPU:
if (findArgValue(strOverride, pmapArgs, "cpus"))
{
uint32_t cCPUs;
if ( strOverride.toInt(cCPUs) == VINF_SUCCESS
&& cCPUs >= VMM_MIN_CPU_COUNT
&& cCPUs <= VMM_MAX_CPU_COUNT
)
{
bstrFinalValue = strOverride;
RTPrintf("%2u: No. of CPUs specified with --cpus: %ls\n",
a, bstrFinalValue.raw());
}
else
return errorSyntax(USAGE_IMPORTAPPLIANCE,
"Argument to --cpus option must be a number greater than %d and less than %d.",
VMM_MIN_CPU_COUNT - 1, VMM_MAX_CPU_COUNT + 1);
}
else
RTPrintf("%2u: Number of CPUs: %ls\n (change with \"--vsys %u --cpus <n>\")\n",
a, bstrFinalValue.raw(), i);
break;
case VirtualSystemDescriptionType_Memory:
{
if (findArgValue(strOverride, pmapArgs, "memory"))
{
uint32_t ulMemMB;
if (VINF_SUCCESS == strOverride.toInt(ulMemMB))
{
bstrFinalValue = strOverride;
RTPrintf("%2u: Guest memory specified with --memory: %ls MB\n",
a, bstrFinalValue.raw());
}
else
return errorSyntax(USAGE_IMPORTAPPLIANCE,
"Argument to --memory option must be a non-negative number.");
}
else
RTPrintf("%2u: Guest memory: %ls MB\n (change with \"--vsys %u --memory <MB>\")\n",
a, bstrFinalValue.raw(), i);
}
break;
case VirtualSystemDescriptionType_HardDiskControllerIDE:
if (fIgnoreThis)
{
RTPrintf("%2u: IDE controller, type %ls -- disabled\n",
a,
aVboxValues[a]);
aEnabled[a] = false;
}
else
RTPrintf("%2u: IDE controller, type %ls"
"\n (disable with \"--vsys %u --unit %u --ignore\")\n",
a,
aVboxValues[a],
i, a);
break;
case VirtualSystemDescriptionType_HardDiskControllerSATA:
if (fIgnoreThis)
{
RTPrintf("%2u: SATA controller, type %ls -- disabled\n",
a,
aVboxValues[a]);
aEnabled[a] = false;
}
else
RTPrintf("%2u: SATA controller, type %ls"
"\n (disable with \"--vsys %u --unit %u --ignore\")\n",
a,
aVboxValues[a],
i, a);
break;
case VirtualSystemDescriptionType_HardDiskControllerSAS:
if (fIgnoreThis)
{
RTPrintf("%2u: SAS controller, type %ls -- disabled\n",
a,
aVboxValues[a]);
aEnabled[a] = false;
}
else
RTPrintf("%2u: SAS controller, type %ls"
"\n (disable with \"--vsys %u --unit %u --ignore\")\n",
a,
aVboxValues[a],
i, a);
break;
case VirtualSystemDescriptionType_HardDiskControllerSCSI:
if (fIgnoreThis)
{
RTPrintf("%2u: SCSI controller, type %ls -- disabled\n",
a,
aVboxValues[a]);
aEnabled[a] = false;
}
else
{
Utf8StrFmt strTypeArg("scsitype%u", a);
if (findArgValue(strOverride, pmapArgs, strTypeArg))
{
bstrFinalValue = strOverride;
RTPrintf("%2u: SCSI controller, type set with --unit %u --scsitype: \"%ls\"\n",
a,
a,
bstrFinalValue.raw());
}
else
RTPrintf("%2u: SCSI controller, type %ls"
"\n (change with \"--vsys %u --unit %u --scsitype {BusLogic|LsiLogic}\";"
"\n disable with \"--vsys %u --unit %u --ignore\")\n",
a,
aVboxValues[a],
i, a, i, a);
}
break;
case VirtualSystemDescriptionType_HardDiskImage:
if (fIgnoreThis)
{
RTPrintf("%2u: Hard disk image: source image=%ls -- disabled\n",
a,
aOvfValues[a]);
aEnabled[a] = false;
}
else
{
Utf8StrFmt strTypeArg("controller%u", a);
if (findArgValue(strOverride, pmapArgs, strTypeArg))
{
// strOverride now has the controller index as a number, but we
// need a "controller=X" format string
strOverride = Utf8StrFmt("controller=%s", strOverride.c_str());
Bstr bstrExtraConfigValue = strOverride;
bstrExtraConfigValue.detachTo(&aExtraConfigValues[a]);
RTPrintf("%2u: Hard disk image: source image=%ls, target path=%ls, %ls\n",
a,
aOvfValues[a],
aVboxValues[a],
aExtraConfigValues[a]);
}
else
RTPrintf("%2u: Hard disk image: source image=%ls, target path=%ls, %ls"
"\n (change controller with \"--vsys %u --unit %u --controller <id>\";"
"\n disable with \"--vsys %u --unit %u --ignore\")\n",
a,
aOvfValues[a],
aVboxValues[a],
aExtraConfigValues[a],
i, a, i, a);
}
break;
case VirtualSystemDescriptionType_CDROM:
if (fIgnoreThis)
{
RTPrintf("%2u: CD-ROM -- disabled\n",
a);
aEnabled[a] = false;
}
else
RTPrintf("%2u: CD-ROM"
"\n (disable with \"--vsys %u --unit %u --ignore\")\n",
a, i, a);
break;
case VirtualSystemDescriptionType_Floppy:
if (fIgnoreThis)
{
RTPrintf("%2u: Floppy -- disabled\n",
a);
aEnabled[a] = false;
}
else
RTPrintf("%2u: Floppy"
"\n (disable with \"--vsys %u --unit %u --ignore\")\n",
a, i, a);
break;
case VirtualSystemDescriptionType_NetworkAdapter:
RTPrintf("%2u: Network adapter: orig %ls, config %ls, extra %ls\n", // @todo implement once we have a plan for the back-end
a,
aOvfValues[a],
aVboxValues[a],
aExtraConfigValues[a]);
break;
case VirtualSystemDescriptionType_USBController:
if (fIgnoreThis)
{
RTPrintf("%2u: USB controller -- disabled\n",
a);
aEnabled[a] = false;
}
else
RTPrintf("%2u: USB controller"
"\n (disable with \"--vsys %u --unit %u --ignore\")\n",
a, i, a);
break;
case VirtualSystemDescriptionType_SoundCard:
if (fIgnoreThis)
{
RTPrintf("%2u: Sound card \"%ls\" -- disabled\n",
a,
aOvfValues[a]);
aEnabled[a] = false;
}
else
RTPrintf("%2u: Sound card (appliance expects \"%ls\", can change on import)"
"\n (disable with \"--vsys %u --unit %u --ignore\")\n",
a,
aOvfValues[a],
i,
a);
break;
}
bstrFinalValue.detachTo(&aFinalValues[a]);
}
if (fExecute)
CHECK_ERROR_BREAK(aVirtualSystemDescriptions[i],
SetFinalValues(ComSafeArrayAsInParam(aEnabled),
ComSafeArrayAsInParam(aFinalValues),
ComSafeArrayAsInParam(aExtraConfigValues)));
} // for (unsigned i = 0; i < cVirtualSystemDescriptions; ++i)
if (cLicensesInTheWay == 1)
RTPrintf("ERROR: Cannot import until the license agreement listed above is accepted.\n");
else if (cLicensesInTheWay > 1)
RTPrintf("ERROR: Cannot import until the %c license agreements listed above are accepted.\n", cLicensesInTheWay);
if (!cLicensesInTheWay && fExecute)
{
// go!
ComPtr<IProgress> progress;
CHECK_ERROR_BREAK(pAppliance,
ImportMachines(progress.asOutParam()));
rc = showProgress(progress);
if (FAILED(rc))
{
com::ProgressErrorInfo info(progress);
com::GluePrintErrorInfo(info);
com::GluePrintErrorContext("ImportAppliance", __FILE__, __LINE__);
return 1;
}
else
RTPrintf("Successfully imported the appliance.\n");
}
} // end if (aVirtualSystemDescriptions.size() > 0)
} while (0);
return SUCCEEDED(rc) ? 0 : 1;
}
static const RTGETOPTDEF g_aExportOptions[]
= {
{ "--output", 'o', RTGETOPT_REQ_STRING },
{ "--legacy09", 'l', RTGETOPT_REQ_NOTHING },
{ "--vsys", 's', RTGETOPT_REQ_UINT32 },
{ "--product", 'p', RTGETOPT_REQ_STRING },
{ "--producturl", 'P', RTGETOPT_REQ_STRING },
{ "--vendor", 'd', RTGETOPT_REQ_STRING },
{ "--vendorurl", 'D', RTGETOPT_REQ_STRING },
{ "--version", 'v', RTGETOPT_REQ_STRING },
{ "--eula", 'e', RTGETOPT_REQ_STRING },
{ "--eulafile", 'E', RTGETOPT_REQ_STRING },
};
int handleExportAppliance(HandlerArg *a)
{
HRESULT rc = S_OK;
Utf8Str strOutputFile;
Utf8Str strOvfFormat("ovf-1.0"); // the default export version
std::list< ComPtr<IMachine> > llMachines;
uint32_t ulCurVsys = (uint32_t)-1;
// for each --vsys X command, maintain a map of command line items
ArgsMapsMap mapArgsMapsPerVsys;
do
{
int c;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
// start at 0 because main() has hacked both the argc and argv given to us
RTGetOptInit(&GetState, a->argc, a->argv, g_aExportOptions,
RT_ELEMENTS(g_aExportOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
Utf8Str strProductUrl;
while ((c = RTGetOpt(&GetState, &ValueUnion)))
{
switch (c)
{
case 'o': // --output
if (strOutputFile.length())
return errorSyntax(USAGE_EXPORTAPPLIANCE, "You can only specify --output once.");
else
strOutputFile = ValueUnion.psz;
break;
case 'l': // --legacy09
strOvfFormat = "ovf-0.9";
break;
case 's': // --vsys
ulCurVsys = ValueUnion.u32;
break;
case 'p': // --product
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["product"] = ValueUnion.psz;
break;
case 'P': // --producturl
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["producturl"] = ValueUnion.psz;
break;
case 'd': // --vendor
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["vendor"] = ValueUnion.psz;
break;
case 'D': // --vendorurl
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["vendorurl"] = ValueUnion.psz;
break;
case 'v': // --version
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["version"] = ValueUnion.psz;
break;
case 'e': // --eula
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["eula"] = ValueUnion.psz;
break;
case 'E': // --eulafile
if (ulCurVsys == (uint32_t)-1)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "Option \"%s\" requires preceding --vsys argument.", GetState.pDef->pszLong);
mapArgsMapsPerVsys[ulCurVsys]["eulafile"] = ValueUnion.psz;
break;
case VINF_GETOPT_NOT_OPTION:
{
Utf8Str strMachine(ValueUnion.psz);
// must be machine: try UUID or name
ComPtr<IMachine> machine;
/* assume it's a UUID */
rc = a->virtualBox->GetMachine(Bstr(strMachine), machine.asOutParam());
if (FAILED(rc) || !machine)
{
/* must be a name */
CHECK_ERROR_BREAK(a->virtualBox, FindMachine(Bstr(strMachine), machine.asOutParam()));
}
if (machine)
llMachines.push_back(machine);
}
break;
default:
if (c > 0)
{
if (RT_C_IS_GRAPH(c))
return errorSyntax(USAGE_EXPORTAPPLIANCE, "unhandled option: -%c", c);
else
return errorSyntax(USAGE_EXPORTAPPLIANCE, "unhandled option: %i", c);
}
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "unknown option: %s", ValueUnion.psz);
else if (ValueUnion.pDef)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
else
return errorSyntax(USAGE_EXPORTAPPLIANCE, "%Rrs", c);
}
if (FAILED(rc))
break;
}
if (FAILED(rc))
break;
if (llMachines.size() == 0)
return errorSyntax(USAGE_EXPORTAPPLIANCE, "At least one machine must be specified with the export command.");
if (!strOutputFile.length())
return errorSyntax(USAGE_EXPORTAPPLIANCE, "Missing --output argument with export command.");
// match command line arguments with the machines count
// this is only to sort out invalid indices at this time
ArgsMapsMap::const_iterator it;
for (it = mapArgsMapsPerVsys.begin();
it != mapArgsMapsPerVsys.end();
++it)
{
uint32_t ulVsys = it->first;
if (ulVsys >= llMachines.size())
return errorSyntax(USAGE_EXPORTAPPLIANCE,
"Invalid index %RI32 with -vsys option; you specified only %zu virtual system(s).",
ulVsys, llMachines.size());
}
ComPtr<IAppliance> pAppliance;
CHECK_ERROR_BREAK(a->virtualBox, CreateAppliance(pAppliance.asOutParam()));
std::list< ComPtr<IMachine> >::iterator itM;
uint32_t i=0;
for (itM = llMachines.begin();
itM != llMachines.end();
++itM, ++i)
{
ComPtr<IMachine> pMachine = *itM;
ComPtr<IVirtualSystemDescription> pVSD;
CHECK_ERROR_BREAK(pMachine, Export(pAppliance, pVSD.asOutParam()));
// Add additional info to the virtal system description if the user wants so
ArgsMap *pmapArgs = NULL;
ArgsMapsMap::iterator itm = mapArgsMapsPerVsys.find(i);
if (itm != mapArgsMapsPerVsys.end())
pmapArgs = &itm->second;
if (pmapArgs)
{
ArgsMap::iterator itD;
for (itD = pmapArgs->begin();
itD != pmapArgs->end();
++itD)
{
if (itD->first == "product")
pVSD->AddDescription (VirtualSystemDescriptionType_Product, Bstr(itD->second), Bstr(itD->second));
else if (itD->first == "producturl")
pVSD->AddDescription (VirtualSystemDescriptionType_ProductUrl, Bstr(itD->second), Bstr(itD->second));
else if (itD->first == "vendor")
pVSD->AddDescription (VirtualSystemDescriptionType_Vendor, Bstr(itD->second), Bstr(itD->second));
else if (itD->first == "vendorurl")
pVSD->AddDescription (VirtualSystemDescriptionType_VendorUrl, Bstr(itD->second), Bstr(itD->second));
else if (itD->first == "version")
pVSD->AddDescription (VirtualSystemDescriptionType_Version, Bstr(itD->second), Bstr(itD->second));
else if (itD->first == "eula")
pVSD->AddDescription (VirtualSystemDescriptionType_License, Bstr(itD->second), Bstr(itD->second));
else if (itD->first == "eulafile")
{
Utf8Str strContent;
void *pvFile;
size_t cbFile;
int irc = RTFileReadAll(itD->second.c_str(), &pvFile, &cbFile);
if (RT_SUCCESS(irc))
{
Bstr bstrContent((char*)pvFile);
pVSD->AddDescription(VirtualSystemDescriptionType_License, bstrContent, bstrContent);
RTFileReadAllFree(pvFile, cbFile);
}
else
{
RTPrintf("ERROR: Cannot read license file \"%s\" which should be included in the virtual system %u.\n",
itD->second.c_str(),
i);
return 1;
}
}
}
}
}
if (FAILED(rc))
break;
ComPtr<IProgress> progress;
char *pszAbsFilePath;
if (strOutputFile.startsWith("S3://", iprt::MiniString::CaseInsensitive) ||
strOutputFile.startsWith("SunCloud://", iprt::MiniString::CaseInsensitive) ||
strOutputFile.startsWith("webdav://", iprt::MiniString::CaseInsensitive))
pszAbsFilePath = RTStrDup(strOutputFile.c_str());
else
pszAbsFilePath = RTPathAbsDup(strOutputFile.c_str());
CHECK_ERROR_BREAK(pAppliance, Write(Bstr(strOvfFormat), Bstr(pszAbsFilePath), progress.asOutParam()));
RTStrFree(pszAbsFilePath);
rc = showProgress(progress);
if (FAILED(rc))
{
com::ProgressErrorInfo info(progress);
com::GluePrintErrorInfo(info);
com::GluePrintErrorContext("Write", __FILE__, __LINE__);
return 1;
}
else
RTPrintf("Successfully exported %d machine(s).\n", llMachines.size());
} while (0);
return SUCCEEDED(rc) ? 0 : 1;
}
#endif /* !VBOX_ONLY_DOCS */
|