VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageMisc.cpp@ 64997

Last change on this file since 64997 was 64997, checked in by vboxsync, 8 years ago

bugref:8527. VBoxManage: added new command "unattended"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 52.5 KB
Line 
1/* $Id: VBoxManageMisc.cpp 64997 2016-12-22 07:41:39Z vboxsync $ */
2/** @file
3 * VBoxManage - VirtualBox's command-line interface.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#ifndef VBOX_ONLY_DOCS
23# include <VBox/com/com.h>
24# include <VBox/com/string.h>
25# include <VBox/com/Guid.h>
26# include <VBox/com/array.h>
27# include <VBox/com/ErrorInfo.h>
28# include <VBox/com/errorprint.h>
29# include <VBox/com/VirtualBox.h>
30#endif /* !VBOX_ONLY_DOCS */
31
32#include <iprt/asm.h>
33#include <iprt/buildconfig.h>
34#include <iprt/cidr.h>
35#include <iprt/ctype.h>
36#include <iprt/dir.h>
37#include <iprt/env.h>
38#include <VBox/err.h>
39#include <iprt/file.h>
40#include <iprt/initterm.h>
41#include <iprt/param.h>
42#include <iprt/path.h>
43#include <iprt/stream.h>
44#include <iprt/string.h>
45#include <iprt/stdarg.h>
46#include <iprt/thread.h>
47#include <iprt/uuid.h>
48#include <iprt/getopt.h>
49#include <iprt/ctype.h>
50#include <VBox/version.h>
51#include <VBox/log.h>
52
53#include "VBoxManage.h"
54
55#include <list>
56
57using namespace com;
58
59
60
61RTEXITCODE handleRegisterVM(HandlerArg *a)
62{
63 HRESULT rc;
64
65 if (a->argc != 1)
66 return errorSyntax(USAGE_REGISTERVM, "Incorrect number of parameters");
67
68 ComPtr<IMachine> machine;
69 /** @todo Ugly hack to get both the API interpretation of relative paths
70 * and the client's interpretation of relative paths. Remove after the API
71 * has been redesigned. */
72 rc = a->virtualBox->OpenMachine(Bstr(a->argv[0]).raw(),
73 machine.asOutParam());
74 if (rc == VBOX_E_FILE_ERROR)
75 {
76 char szVMFileAbs[RTPATH_MAX] = "";
77 int vrc = RTPathAbs(a->argv[0], szVMFileAbs, sizeof(szVMFileAbs));
78 if (RT_FAILURE(vrc))
79 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot convert filename \"%s\" to absolute path: %Rrc", a->argv[0], vrc);
80 CHECK_ERROR(a->virtualBox, OpenMachine(Bstr(szVMFileAbs).raw(),
81 machine.asOutParam()));
82 }
83 else if (FAILED(rc))
84 CHECK_ERROR(a->virtualBox, OpenMachine(Bstr(a->argv[0]).raw(),
85 machine.asOutParam()));
86 if (SUCCEEDED(rc))
87 {
88 ASSERT(machine);
89 CHECK_ERROR(a->virtualBox, RegisterMachine(machine));
90 }
91 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
92}
93
94static const RTGETOPTDEF g_aUnregisterVMOptions[] =
95{
96 { "--delete", 'd', RTGETOPT_REQ_NOTHING },
97 { "-delete", 'd', RTGETOPT_REQ_NOTHING }, // deprecated
98};
99
100RTEXITCODE handleUnregisterVM(HandlerArg *a)
101{
102 HRESULT rc;
103 const char *VMName = NULL;
104 bool fDelete = false;
105
106 int c;
107 RTGETOPTUNION ValueUnion;
108 RTGETOPTSTATE GetState;
109 // start at 0 because main() has hacked both the argc and argv given to us
110 RTGetOptInit(&GetState, a->argc, a->argv, g_aUnregisterVMOptions, RT_ELEMENTS(g_aUnregisterVMOptions),
111 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
112 while ((c = RTGetOpt(&GetState, &ValueUnion)))
113 {
114 switch (c)
115 {
116 case 'd': // --delete
117 fDelete = true;
118 break;
119
120 case VINF_GETOPT_NOT_OPTION:
121 if (!VMName)
122 VMName = ValueUnion.psz;
123 else
124 return errorSyntax(USAGE_UNREGISTERVM, "Invalid parameter '%s'", ValueUnion.psz);
125 break;
126
127 default:
128 if (c > 0)
129 {
130 if (RT_C_IS_PRINT(c))
131 return errorSyntax(USAGE_UNREGISTERVM, "Invalid option -%c", c);
132 else
133 return errorSyntax(USAGE_UNREGISTERVM, "Invalid option case %i", c);
134 }
135 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
136 return errorSyntax(USAGE_UNREGISTERVM, "unknown option: %s\n", ValueUnion.psz);
137 else if (ValueUnion.pDef)
138 return errorSyntax(USAGE_UNREGISTERVM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
139 else
140 return errorSyntax(USAGE_UNREGISTERVM, "error: %Rrs", c);
141 }
142 }
143
144 /* check for required options */
145 if (!VMName)
146 return errorSyntax(USAGE_UNREGISTERVM, "VM name required");
147
148 ComPtr<IMachine> machine;
149 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(VMName).raw(),
150 machine.asOutParam()),
151 RTEXITCODE_FAILURE);
152 SafeIfaceArray<IMedium> aMedia;
153 CHECK_ERROR_RET(machine, Unregister(CleanupMode_DetachAllReturnHardDisksOnly,
154 ComSafeArrayAsOutParam(aMedia)),
155 RTEXITCODE_FAILURE);
156 if (fDelete)
157 {
158 ComPtr<IProgress> pProgress;
159 CHECK_ERROR_RET(machine, DeleteConfig(ComSafeArrayAsInParam(aMedia), pProgress.asOutParam()),
160 RTEXITCODE_FAILURE);
161
162 rc = showProgress(pProgress);
163 CHECK_PROGRESS_ERROR_RET(pProgress, ("Machine delete failed"), RTEXITCODE_FAILURE);
164 }
165 else
166 {
167 /* Note that the IMachine::Unregister method will return the medium
168 * reference in a sane order, which means that closing will normally
169 * succeed, unless there is still another machine which uses the
170 * medium. No harm done if we ignore the error. */
171 for (size_t i = 0; i < aMedia.size(); i++)
172 {
173 IMedium *pMedium = aMedia[i];
174 if (pMedium)
175 rc = pMedium->Close();
176 }
177 rc = S_OK;
178 }
179 return RTEXITCODE_SUCCESS;
180}
181
182static const RTGETOPTDEF g_aCreateVMOptions[] =
183{
184 { "--name", 'n', RTGETOPT_REQ_STRING },
185 { "-name", 'n', RTGETOPT_REQ_STRING },
186 { "--groups", 'g', RTGETOPT_REQ_STRING },
187 { "--basefolder", 'p', RTGETOPT_REQ_STRING },
188 { "-basefolder", 'p', RTGETOPT_REQ_STRING },
189 { "--ostype", 'o', RTGETOPT_REQ_STRING },
190 { "-ostype", 'o', RTGETOPT_REQ_STRING },
191 { "--uuid", 'u', RTGETOPT_REQ_UUID },
192 { "-uuid", 'u', RTGETOPT_REQ_UUID },
193 { "--register", 'r', RTGETOPT_REQ_NOTHING },
194 { "-register", 'r', RTGETOPT_REQ_NOTHING },
195};
196
197RTEXITCODE handleCreateVM(HandlerArg *a)
198{
199 HRESULT rc;
200 Bstr bstrBaseFolder;
201 Bstr bstrName;
202 Bstr bstrOsTypeId;
203 Bstr bstrUuid;
204 bool fRegister = false;
205 com::SafeArray<BSTR> groups;
206
207 int c;
208 RTGETOPTUNION ValueUnion;
209 RTGETOPTSTATE GetState;
210 // start at 0 because main() has hacked both the argc and argv given to us
211 RTGetOptInit(&GetState, a->argc, a->argv, g_aCreateVMOptions, RT_ELEMENTS(g_aCreateVMOptions),
212 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
213 while ((c = RTGetOpt(&GetState, &ValueUnion)))
214 {
215 switch (c)
216 {
217 case 'n': // --name
218 bstrName = ValueUnion.psz;
219 break;
220
221 case 'g': // --groups
222 parseGroups(ValueUnion.psz, &groups);
223 break;
224
225 case 'p': // --basefolder
226 bstrBaseFolder = ValueUnion.psz;
227 break;
228
229 case 'o': // --ostype
230 bstrOsTypeId = ValueUnion.psz;
231 break;
232
233 case 'u': // --uuid
234 bstrUuid = Guid(ValueUnion.Uuid).toUtf16().raw();
235 break;
236
237 case 'r': // --register
238 fRegister = true;
239 break;
240
241 default:
242 return errorGetOpt(USAGE_CREATEVM, c, &ValueUnion);
243 }
244 }
245
246 /* check for required options */
247 if (bstrName.isEmpty())
248 return errorSyntax(USAGE_CREATEVM, "Parameter --name is required");
249
250 do
251 {
252 Bstr createFlags;
253 if (!bstrUuid.isEmpty())
254 createFlags = BstrFmt("UUID=%ls", bstrUuid.raw());
255 Bstr bstrPrimaryGroup;
256 if (groups.size())
257 bstrPrimaryGroup = groups[0];
258 Bstr bstrSettingsFile;
259 CHECK_ERROR_BREAK(a->virtualBox,
260 ComposeMachineFilename(bstrName.raw(),
261 bstrPrimaryGroup.raw(),
262 createFlags.raw(),
263 bstrBaseFolder.raw(),
264 bstrSettingsFile.asOutParam()));
265 ComPtr<IMachine> machine;
266 CHECK_ERROR_BREAK(a->virtualBox,
267 CreateMachine(bstrSettingsFile.raw(),
268 bstrName.raw(),
269 ComSafeArrayAsInParam(groups),
270 bstrOsTypeId.raw(),
271 createFlags.raw(),
272 machine.asOutParam()));
273
274 CHECK_ERROR_BREAK(machine, SaveSettings());
275 if (fRegister)
276 {
277 CHECK_ERROR_BREAK(a->virtualBox, RegisterMachine(machine));
278 }
279 Bstr uuid;
280 CHECK_ERROR_BREAK(machine, COMGETTER(Id)(uuid.asOutParam()));
281 Bstr settingsFile;
282 CHECK_ERROR_BREAK(machine, COMGETTER(SettingsFilePath)(settingsFile.asOutParam()));
283 RTPrintf("Virtual machine '%ls' is created%s.\n"
284 "UUID: %s\n"
285 "Settings file: '%ls'\n",
286 bstrName.raw(), fRegister ? " and registered" : "",
287 Utf8Str(uuid).c_str(), settingsFile.raw());
288 }
289 while (0);
290
291 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
292}
293
294static const RTGETOPTDEF g_aCloneVMOptions[] =
295{
296 { "--snapshot", 's', RTGETOPT_REQ_STRING },
297 { "--name", 'n', RTGETOPT_REQ_STRING },
298 { "--groups", 'g', RTGETOPT_REQ_STRING },
299 { "--mode", 'm', RTGETOPT_REQ_STRING },
300 { "--options", 'o', RTGETOPT_REQ_STRING },
301 { "--register", 'r', RTGETOPT_REQ_NOTHING },
302 { "--basefolder", 'p', RTGETOPT_REQ_STRING },
303 { "--uuid", 'u', RTGETOPT_REQ_UUID },
304};
305
306static int parseCloneMode(const char *psz, CloneMode_T *pMode)
307{
308 if (!RTStrICmp(psz, "machine"))
309 *pMode = CloneMode_MachineState;
310 else if (!RTStrICmp(psz, "machineandchildren"))
311 *pMode = CloneMode_MachineAndChildStates;
312 else if (!RTStrICmp(psz, "all"))
313 *pMode = CloneMode_AllStates;
314 else
315 return VERR_PARSE_ERROR;
316
317 return VINF_SUCCESS;
318}
319
320static int parseCloneOptions(const char *psz, com::SafeArray<CloneOptions_T> *options)
321{
322 int rc = VINF_SUCCESS;
323 while (psz && *psz && RT_SUCCESS(rc))
324 {
325 size_t len;
326 const char *pszComma = strchr(psz, ',');
327 if (pszComma)
328 len = pszComma - psz;
329 else
330 len = strlen(psz);
331 if (len > 0)
332 {
333 if (!RTStrNICmp(psz, "KeepAllMACs", len))
334 options->push_back(CloneOptions_KeepAllMACs);
335 else if (!RTStrNICmp(psz, "KeepNATMACs", len))
336 options->push_back(CloneOptions_KeepNATMACs);
337 else if (!RTStrNICmp(psz, "KeepDiskNames", len))
338 options->push_back(CloneOptions_KeepDiskNames);
339 else if ( !RTStrNICmp(psz, "Link", len)
340 || !RTStrNICmp(psz, "Linked", len))
341 options->push_back(CloneOptions_Link);
342 else
343 rc = VERR_PARSE_ERROR;
344 }
345 if (pszComma)
346 psz += len + 1;
347 else
348 psz += len;
349 }
350
351 return rc;
352}
353
354RTEXITCODE handleCloneVM(HandlerArg *a)
355{
356 HRESULT rc;
357 const char *pszSrcName = NULL;
358 const char *pszSnapshotName = NULL;
359 CloneMode_T mode = CloneMode_MachineState;
360 com::SafeArray<CloneOptions_T> options;
361 const char *pszTrgName = NULL;
362 const char *pszTrgBaseFolder = NULL;
363 bool fRegister = false;
364 Bstr bstrUuid;
365 com::SafeArray<BSTR> groups;
366
367 int c;
368 RTGETOPTUNION ValueUnion;
369 RTGETOPTSTATE GetState;
370 // start at 0 because main() has hacked both the argc and argv given to us
371 RTGetOptInit(&GetState, a->argc, a->argv, g_aCloneVMOptions, RT_ELEMENTS(g_aCloneVMOptions),
372 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
373 while ((c = RTGetOpt(&GetState, &ValueUnion)))
374 {
375 switch (c)
376 {
377 case 's': // --snapshot
378 pszSnapshotName = ValueUnion.psz;
379 break;
380
381 case 'n': // --name
382 pszTrgName = ValueUnion.psz;
383 break;
384
385 case 'g': // --groups
386 parseGroups(ValueUnion.psz, &groups);
387 break;
388
389 case 'p': // --basefolder
390 pszTrgBaseFolder = ValueUnion.psz;
391 break;
392
393 case 'm': // --mode
394 if (RT_FAILURE(parseCloneMode(ValueUnion.psz, &mode)))
395 return errorArgument("Invalid clone mode '%s'\n", ValueUnion.psz);
396 break;
397
398 case 'o': // --options
399 if (RT_FAILURE(parseCloneOptions(ValueUnion.psz, &options)))
400 return errorArgument("Invalid clone options '%s'\n", ValueUnion.psz);
401 break;
402
403 case 'u': // --uuid
404 bstrUuid = Guid(ValueUnion.Uuid).toUtf16().raw();
405 break;
406
407 case 'r': // --register
408 fRegister = true;
409 break;
410
411 case VINF_GETOPT_NOT_OPTION:
412 if (!pszSrcName)
413 pszSrcName = ValueUnion.psz;
414 else
415 return errorSyntax(USAGE_CLONEVM, "Invalid parameter '%s'", ValueUnion.psz);
416 break;
417
418 default:
419 return errorGetOpt(USAGE_CLONEVM, c, &ValueUnion);
420 }
421 }
422
423 /* Check for required options */
424 if (!pszSrcName)
425 return errorSyntax(USAGE_CLONEVM, "VM name required");
426
427 /* Get the machine object */
428 ComPtr<IMachine> srcMachine;
429 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(pszSrcName).raw(),
430 srcMachine.asOutParam()),
431 RTEXITCODE_FAILURE);
432
433 /* If a snapshot name/uuid was given, get the particular machine of this
434 * snapshot. */
435 if (pszSnapshotName)
436 {
437 ComPtr<ISnapshot> srcSnapshot;
438 CHECK_ERROR_RET(srcMachine, FindSnapshot(Bstr(pszSnapshotName).raw(),
439 srcSnapshot.asOutParam()),
440 RTEXITCODE_FAILURE);
441 CHECK_ERROR_RET(srcSnapshot, COMGETTER(Machine)(srcMachine.asOutParam()),
442 RTEXITCODE_FAILURE);
443 }
444
445 /* Default name necessary? */
446 if (!pszTrgName)
447 pszTrgName = RTStrAPrintf2("%s Clone", pszSrcName);
448
449 Bstr createFlags;
450 if (!bstrUuid.isEmpty())
451 createFlags = BstrFmt("UUID=%ls", bstrUuid.raw());
452 Bstr bstrPrimaryGroup;
453 if (groups.size())
454 bstrPrimaryGroup = groups[0];
455 Bstr bstrSettingsFile;
456 CHECK_ERROR_RET(a->virtualBox,
457 ComposeMachineFilename(Bstr(pszTrgName).raw(),
458 bstrPrimaryGroup.raw(),
459 createFlags.raw(),
460 Bstr(pszTrgBaseFolder).raw(),
461 bstrSettingsFile.asOutParam()),
462 RTEXITCODE_FAILURE);
463
464 ComPtr<IMachine> trgMachine;
465 CHECK_ERROR_RET(a->virtualBox, CreateMachine(bstrSettingsFile.raw(),
466 Bstr(pszTrgName).raw(),
467 ComSafeArrayAsInParam(groups),
468 NULL,
469 createFlags.raw(),
470 trgMachine.asOutParam()),
471 RTEXITCODE_FAILURE);
472
473 /* Start the cloning */
474 ComPtr<IProgress> progress;
475 CHECK_ERROR_RET(srcMachine, CloneTo(trgMachine,
476 mode,
477 ComSafeArrayAsInParam(options),
478 progress.asOutParam()),
479 RTEXITCODE_FAILURE);
480 rc = showProgress(progress);
481 CHECK_PROGRESS_ERROR_RET(progress, ("Clone VM failed"), RTEXITCODE_FAILURE);
482
483 if (fRegister)
484 CHECK_ERROR_RET(a->virtualBox, RegisterMachine(trgMachine), RTEXITCODE_FAILURE);
485
486 Bstr bstrNewName;
487 CHECK_ERROR_RET(trgMachine, COMGETTER(Name)(bstrNewName.asOutParam()), RTEXITCODE_FAILURE);
488 RTPrintf("Machine has been successfully cloned as \"%ls\"\n", bstrNewName.raw());
489
490 return RTEXITCODE_SUCCESS;
491}
492
493RTEXITCODE handleStartVM(HandlerArg *a)
494{
495 HRESULT rc = S_OK;
496 std::list<const char *> VMs;
497 Bstr sessionType;
498 Utf8Str strEnv;
499
500#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
501 /* make sure the VM process will by default start on the same display as VBoxManage */
502 {
503 const char *pszDisplay = RTEnvGet("DISPLAY");
504 if (pszDisplay)
505 strEnv = Utf8StrFmt("DISPLAY=%s\n", pszDisplay);
506 const char *pszXAuth = RTEnvGet("XAUTHORITY");
507 if (pszXAuth)
508 strEnv.append(Utf8StrFmt("XAUTHORITY=%s\n", pszXAuth));
509 }
510#endif
511
512 static const RTGETOPTDEF s_aStartVMOptions[] =
513 {
514 { "--type", 't', RTGETOPT_REQ_STRING },
515 { "-type", 't', RTGETOPT_REQ_STRING }, // deprecated
516 { "--putenv", 'E', RTGETOPT_REQ_STRING },
517 };
518 int c;
519 RTGETOPTUNION ValueUnion;
520 RTGETOPTSTATE GetState;
521 // start at 0 because main() has hacked both the argc and argv given to us
522 RTGetOptInit(&GetState, a->argc, a->argv, s_aStartVMOptions, RT_ELEMENTS(s_aStartVMOptions),
523 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
524 while ((c = RTGetOpt(&GetState, &ValueUnion)))
525 {
526 switch (c)
527 {
528 case 't': // --type
529 if (!RTStrICmp(ValueUnion.psz, "gui"))
530 {
531 sessionType = "gui";
532 }
533#ifdef VBOX_WITH_VBOXSDL
534 else if (!RTStrICmp(ValueUnion.psz, "sdl"))
535 {
536 sessionType = "sdl";
537 }
538#endif
539#ifdef VBOX_WITH_HEADLESS
540 else if (!RTStrICmp(ValueUnion.psz, "capture"))
541 {
542 sessionType = "capture";
543 }
544 else if (!RTStrICmp(ValueUnion.psz, "headless"))
545 {
546 sessionType = "headless";
547 }
548#endif
549 else
550 sessionType = ValueUnion.psz;
551 break;
552
553 case 'E': // --putenv
554 if (!RTStrStr(ValueUnion.psz, "\n"))
555 strEnv.append(Utf8StrFmt("%s\n", ValueUnion.psz));
556 else
557 return errorSyntax(USAGE_STARTVM, "Parameter to option --putenv must not contain any newline character");
558 break;
559
560 case VINF_GETOPT_NOT_OPTION:
561 VMs.push_back(ValueUnion.psz);
562 break;
563
564 default:
565 if (c > 0)
566 {
567 if (RT_C_IS_PRINT(c))
568 return errorSyntax(USAGE_STARTVM, "Invalid option -%c", c);
569 else
570 return errorSyntax(USAGE_STARTVM, "Invalid option case %i", c);
571 }
572 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
573 return errorSyntax(USAGE_STARTVM, "unknown option: %s\n", ValueUnion.psz);
574 else if (ValueUnion.pDef)
575 return errorSyntax(USAGE_STARTVM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
576 else
577 return errorSyntax(USAGE_STARTVM, "error: %Rrs", c);
578 }
579 }
580
581 /* check for required options */
582 if (VMs.empty())
583 return errorSyntax(USAGE_STARTVM, "at least one VM name or uuid required");
584
585 for (std::list<const char *>::const_iterator it = VMs.begin();
586 it != VMs.end();
587 ++it)
588 {
589 HRESULT rc2 = rc;
590 const char *pszVM = *it;
591 ComPtr<IMachine> machine;
592 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(pszVM).raw(),
593 machine.asOutParam()));
594 if (machine)
595 {
596 ComPtr<IProgress> progress;
597 CHECK_ERROR(machine, LaunchVMProcess(a->session, sessionType.raw(),
598 Bstr(strEnv).raw(), progress.asOutParam()));
599 if (SUCCEEDED(rc) && !progress.isNull())
600 {
601 RTPrintf("Waiting for VM \"%s\" to power on...\n", pszVM);
602 CHECK_ERROR(progress, WaitForCompletion(-1));
603 if (SUCCEEDED(rc))
604 {
605 BOOL completed = true;
606 CHECK_ERROR(progress, COMGETTER(Completed)(&completed));
607 if (SUCCEEDED(rc))
608 {
609 ASSERT(completed);
610
611 LONG iRc;
612 CHECK_ERROR(progress, COMGETTER(ResultCode)(&iRc));
613 if (SUCCEEDED(rc))
614 {
615 if (SUCCEEDED(iRc))
616 RTPrintf("VM \"%s\" has been successfully started.\n", pszVM);
617 else
618 {
619 ProgressErrorInfo info(progress);
620 com::GluePrintErrorInfo(info);
621 }
622 rc = iRc;
623 }
624 }
625 }
626 }
627 }
628
629 /* it's important to always close sessions */
630 a->session->UnlockMachine();
631
632 /* make sure that we remember the failed state */
633 if (FAILED(rc2))
634 rc = rc2;
635 }
636
637 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
638}
639
640RTEXITCODE handleDiscardState(HandlerArg *a)
641{
642 HRESULT rc;
643
644 if (a->argc != 1)
645 return errorSyntax(USAGE_DISCARDSTATE, "Incorrect number of parameters");
646
647 ComPtr<IMachine> machine;
648 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
649 machine.asOutParam()));
650 if (machine)
651 {
652 do
653 {
654 /* we have to open a session for this task */
655 CHECK_ERROR_BREAK(machine, LockMachine(a->session, LockType_Write));
656 do
657 {
658 ComPtr<IMachine> sessionMachine;
659 CHECK_ERROR_BREAK(a->session, COMGETTER(Machine)(sessionMachine.asOutParam()));
660 CHECK_ERROR_BREAK(sessionMachine, DiscardSavedState(true /* fDeleteFile */));
661 } while (0);
662 CHECK_ERROR_BREAK(a->session, UnlockMachine());
663 } while (0);
664 }
665
666 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
667}
668
669RTEXITCODE handleAdoptState(HandlerArg *a)
670{
671 HRESULT rc;
672
673 if (a->argc != 2)
674 return errorSyntax(USAGE_ADOPTSTATE, "Incorrect number of parameters");
675
676 ComPtr<IMachine> machine;
677 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
678 machine.asOutParam()));
679 if (machine)
680 {
681 char szStateFileAbs[RTPATH_MAX] = "";
682 int vrc = RTPathAbs(a->argv[1], szStateFileAbs, sizeof(szStateFileAbs));
683 if (RT_FAILURE(vrc))
684 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot convert filename \"%s\" to absolute path: %Rrc", a->argv[0], vrc);
685
686 do
687 {
688 /* we have to open a session for this task */
689 CHECK_ERROR_BREAK(machine, LockMachine(a->session, LockType_Write));
690 do
691 {
692 ComPtr<IMachine> sessionMachine;
693 CHECK_ERROR_BREAK(a->session, COMGETTER(Machine)(sessionMachine.asOutParam()));
694 CHECK_ERROR_BREAK(sessionMachine, AdoptSavedState(Bstr(szStateFileAbs).raw()));
695 } while (0);
696 CHECK_ERROR_BREAK(a->session, UnlockMachine());
697 } while (0);
698 }
699
700 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
701}
702
703RTEXITCODE handleGetExtraData(HandlerArg *a)
704{
705 HRESULT rc = S_OK;
706
707 if (a->argc != 2)
708 return errorSyntax(USAGE_GETEXTRADATA, "Incorrect number of parameters");
709
710 /* global data? */
711 if (!strcmp(a->argv[0], "global"))
712 {
713 /* enumeration? */
714 if (!strcmp(a->argv[1], "enumerate"))
715 {
716 SafeArray<BSTR> aKeys;
717 CHECK_ERROR(a->virtualBox, GetExtraDataKeys(ComSafeArrayAsOutParam(aKeys)));
718
719 for (size_t i = 0;
720 i < aKeys.size();
721 ++i)
722 {
723 Bstr bstrKey(aKeys[i]);
724 Bstr bstrValue;
725 CHECK_ERROR(a->virtualBox, GetExtraData(bstrKey.raw(),
726 bstrValue.asOutParam()));
727
728 RTPrintf("Key: %ls, Value: %ls\n", bstrKey.raw(), bstrValue.raw());
729 }
730 }
731 else
732 {
733 Bstr value;
734 CHECK_ERROR(a->virtualBox, GetExtraData(Bstr(a->argv[1]).raw(),
735 value.asOutParam()));
736 if (!value.isEmpty())
737 RTPrintf("Value: %ls\n", value.raw());
738 else
739 RTPrintf("No value set!\n");
740 }
741 }
742 else
743 {
744 ComPtr<IMachine> machine;
745 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
746 machine.asOutParam()));
747 if (machine)
748 {
749 /* enumeration? */
750 if (!strcmp(a->argv[1], "enumerate"))
751 {
752 SafeArray<BSTR> aKeys;
753 CHECK_ERROR(machine, GetExtraDataKeys(ComSafeArrayAsOutParam(aKeys)));
754
755 for (size_t i = 0;
756 i < aKeys.size();
757 ++i)
758 {
759 Bstr bstrKey(aKeys[i]);
760 Bstr bstrValue;
761 CHECK_ERROR(machine, GetExtraData(bstrKey.raw(),
762 bstrValue.asOutParam()));
763
764 RTPrintf("Key: %ls, Value: %ls\n", bstrKey.raw(), bstrValue.raw());
765 }
766 }
767 else
768 {
769 Bstr value;
770 CHECK_ERROR(machine, GetExtraData(Bstr(a->argv[1]).raw(),
771 value.asOutParam()));
772 if (!value.isEmpty())
773 RTPrintf("Value: %ls\n", value.raw());
774 else
775 RTPrintf("No value set!\n");
776 }
777 }
778 }
779 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
780}
781
782RTEXITCODE handleSetExtraData(HandlerArg *a)
783{
784 HRESULT rc = S_OK;
785
786 if (a->argc < 2)
787 return errorSyntax(USAGE_SETEXTRADATA, "Not enough parameters");
788
789 /* global data? */
790 if (!strcmp(a->argv[0], "global"))
791 {
792 /** @todo passing NULL is deprecated */
793 if (a->argc < 3)
794 CHECK_ERROR(a->virtualBox, SetExtraData(Bstr(a->argv[1]).raw(),
795 NULL));
796 else if (a->argc == 3)
797 CHECK_ERROR(a->virtualBox, SetExtraData(Bstr(a->argv[1]).raw(),
798 Bstr(a->argv[2]).raw()));
799 else
800 return errorSyntax(USAGE_SETEXTRADATA, "Too many parameters");
801 }
802 else
803 {
804 ComPtr<IMachine> machine;
805 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
806 machine.asOutParam()));
807 if (machine)
808 {
809 /* open an existing session for the VM */
810 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
811 /* get the session machine */
812 ComPtr<IMachine> sessionMachine;
813 CHECK_ERROR_RET(a->session, COMGETTER(Machine)(sessionMachine.asOutParam()), RTEXITCODE_FAILURE);
814 /** @todo passing NULL is deprecated */
815 if (a->argc < 3)
816 CHECK_ERROR(sessionMachine, SetExtraData(Bstr(a->argv[1]).raw(),
817 NULL));
818 else if (a->argc == 3)
819 CHECK_ERROR(sessionMachine, SetExtraData(Bstr(a->argv[1]).raw(),
820 Bstr(a->argv[2]).raw()));
821 else
822 return errorSyntax(USAGE_SETEXTRADATA, "Too many parameters");
823 }
824 }
825 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
826}
827
828RTEXITCODE handleSetProperty(HandlerArg *a)
829{
830 HRESULT rc;
831
832 /* there must be two arguments: property name and value */
833 if (a->argc != 2)
834 return errorSyntax(USAGE_SETPROPERTY, "Incorrect number of parameters");
835
836 ComPtr<ISystemProperties> systemProperties;
837 a->virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
838
839 if (!strcmp(a->argv[0], "machinefolder"))
840 {
841 /* reset to default? */
842 if (!strcmp(a->argv[1], "default"))
843 CHECK_ERROR(systemProperties, COMSETTER(DefaultMachineFolder)(NULL));
844 else
845 CHECK_ERROR(systemProperties, COMSETTER(DefaultMachineFolder)(Bstr(a->argv[1]).raw()));
846 }
847 else if (!strcmp(a->argv[0], "hwvirtexclusive"))
848 {
849 bool fHwVirtExclusive;
850
851 if (!strcmp(a->argv[1], "on"))
852 fHwVirtExclusive = true;
853 else if (!strcmp(a->argv[1], "off"))
854 fHwVirtExclusive = false;
855 else
856 return errorArgument("Invalid hwvirtexclusive argument '%s'", a->argv[1]);
857 CHECK_ERROR(systemProperties, COMSETTER(ExclusiveHwVirt)(fHwVirtExclusive));
858 }
859 else if ( !strcmp(a->argv[0], "vrdeauthlibrary")
860 || !strcmp(a->argv[0], "vrdpauthlibrary"))
861 {
862 if (!strcmp(a->argv[0], "vrdpauthlibrary"))
863 RTStrmPrintf(g_pStdErr, "Warning: 'vrdpauthlibrary' is deprecated. Use 'vrdeauthlibrary'.\n");
864
865 /* reset to default? */
866 if (!strcmp(a->argv[1], "default"))
867 CHECK_ERROR(systemProperties, COMSETTER(VRDEAuthLibrary)(NULL));
868 else
869 CHECK_ERROR(systemProperties, COMSETTER(VRDEAuthLibrary)(Bstr(a->argv[1]).raw()));
870 }
871 else if (!strcmp(a->argv[0], "websrvauthlibrary"))
872 {
873 /* reset to default? */
874 if (!strcmp(a->argv[1], "default"))
875 CHECK_ERROR(systemProperties, COMSETTER(WebServiceAuthLibrary)(NULL));
876 else
877 CHECK_ERROR(systemProperties, COMSETTER(WebServiceAuthLibrary)(Bstr(a->argv[1]).raw()));
878 }
879 else if (!strcmp(a->argv[0], "vrdeextpack"))
880 {
881 /* disable? */
882 if (!strcmp(a->argv[1], "null"))
883 CHECK_ERROR(systemProperties, COMSETTER(DefaultVRDEExtPack)(NULL));
884 else
885 CHECK_ERROR(systemProperties, COMSETTER(DefaultVRDEExtPack)(Bstr(a->argv[1]).raw()));
886 }
887 else if (!strcmp(a->argv[0], "loghistorycount"))
888 {
889 uint32_t uVal;
890 int vrc;
891 vrc = RTStrToUInt32Ex(a->argv[1], NULL, 0, &uVal);
892 if (vrc != VINF_SUCCESS)
893 return errorArgument("Error parsing Log history count '%s'", a->argv[1]);
894 CHECK_ERROR(systemProperties, COMSETTER(LogHistoryCount)(uVal));
895 }
896 else if (!strcmp(a->argv[0], "autostartdbpath"))
897 {
898 /* disable? */
899 if (!strcmp(a->argv[1], "null"))
900 CHECK_ERROR(systemProperties, COMSETTER(AutostartDatabasePath)(NULL));
901 else
902 CHECK_ERROR(systemProperties, COMSETTER(AutostartDatabasePath)(Bstr(a->argv[1]).raw()));
903 }
904 else if (!strcmp(a->argv[0], "defaultfrontend"))
905 {
906 Bstr bstrDefaultFrontend(a->argv[1]);
907 if (!strcmp(a->argv[1], "default"))
908 bstrDefaultFrontend.setNull();
909 CHECK_ERROR(systemProperties, COMSETTER(DefaultFrontend)(bstrDefaultFrontend.raw()));
910 }
911 else if (!strcmp(a->argv[0], "logginglevel"))
912 {
913 Bstr bstrLoggingLevel(a->argv[1]);
914 if (!strcmp(a->argv[1], "default"))
915 bstrLoggingLevel.setNull();
916 CHECK_ERROR(systemProperties, COMSETTER(LoggingLevel)(bstrLoggingLevel.raw()));
917 }
918 else
919 return errorSyntax(USAGE_SETPROPERTY, "Invalid parameter '%s'", a->argv[0]);
920
921 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
922}
923
924RTEXITCODE handleSharedFolder(HandlerArg *a)
925{
926 HRESULT rc;
927
928 /* we need at least a command and target */
929 if (a->argc < 2)
930 return errorSyntax(USAGE_SHAREDFOLDER, "Not enough parameters");
931
932 const char *pszMachineName = a->argv[1];
933 ComPtr<IMachine> machine;
934 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(pszMachineName).raw(), machine.asOutParam()));
935 if (!machine)
936 return RTEXITCODE_FAILURE;
937
938 if (!strcmp(a->argv[0], "add"))
939 {
940 /* we need at least four more parameters */
941 if (a->argc < 5)
942 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Not enough parameters");
943
944 char *name = NULL;
945 char *hostpath = NULL;
946 bool fTransient = false;
947 bool fWritable = true;
948 bool fAutoMount = false;
949
950 for (int i = 2; i < a->argc; i++)
951 {
952 if ( !strcmp(a->argv[i], "--name")
953 || !strcmp(a->argv[i], "-name"))
954 {
955 if (a->argc <= i + 1 || !*a->argv[i+1])
956 return errorArgument("Missing argument to '%s'", a->argv[i]);
957 i++;
958 name = a->argv[i];
959 }
960 else if ( !strcmp(a->argv[i], "--hostpath")
961 || !strcmp(a->argv[i], "-hostpath"))
962 {
963 if (a->argc <= i + 1 || !*a->argv[i+1])
964 return errorArgument("Missing argument to '%s'", a->argv[i]);
965 i++;
966 hostpath = a->argv[i];
967 }
968 else if ( !strcmp(a->argv[i], "--readonly")
969 || !strcmp(a->argv[i], "-readonly"))
970 {
971 fWritable = false;
972 }
973 else if ( !strcmp(a->argv[i], "--transient")
974 || !strcmp(a->argv[i], "-transient"))
975 {
976 fTransient = true;
977 }
978 else if ( !strcmp(a->argv[i], "--automount")
979 || !strcmp(a->argv[i], "-automount"))
980 {
981 fAutoMount = true;
982 }
983 else
984 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
985 }
986
987 if (NULL != strstr(name, " "))
988 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "No spaces allowed in parameter '-name'!");
989
990 /* required arguments */
991 if (!name || !hostpath)
992 {
993 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Parameters --name and --hostpath are required");
994 }
995
996 if (fTransient)
997 {
998 ComPtr<IConsole> console;
999
1000 /* open an existing session for the VM */
1001 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
1002
1003 /* get the session machine */
1004 ComPtr<IMachine> sessionMachine;
1005 CHECK_ERROR_RET(a->session, COMGETTER(Machine)(sessionMachine.asOutParam()), RTEXITCODE_FAILURE);
1006
1007 /* get the session console */
1008 CHECK_ERROR_RET(a->session, COMGETTER(Console)(console.asOutParam()), RTEXITCODE_FAILURE);
1009 if (console.isNull())
1010 return RTMsgErrorExit(RTEXITCODE_FAILURE,
1011 "Machine '%s' is not currently running.\n", pszMachineName);
1012
1013 CHECK_ERROR(console, CreateSharedFolder(Bstr(name).raw(),
1014 Bstr(hostpath).raw(),
1015 fWritable, fAutoMount));
1016 a->session->UnlockMachine();
1017 }
1018 else
1019 {
1020 /* open a session for the VM */
1021 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Write), RTEXITCODE_FAILURE);
1022
1023 /* get the mutable session machine */
1024 ComPtr<IMachine> sessionMachine;
1025 a->session->COMGETTER(Machine)(sessionMachine.asOutParam());
1026
1027 CHECK_ERROR(sessionMachine, CreateSharedFolder(Bstr(name).raw(),
1028 Bstr(hostpath).raw(),
1029 fWritable, fAutoMount));
1030 if (SUCCEEDED(rc))
1031 CHECK_ERROR(sessionMachine, SaveSettings());
1032
1033 a->session->UnlockMachine();
1034 }
1035 }
1036 else if (!strcmp(a->argv[0], "remove"))
1037 {
1038 /* we need at least two more parameters */
1039 if (a->argc < 3)
1040 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Not enough parameters");
1041
1042 char *name = NULL;
1043 bool fTransient = false;
1044
1045 for (int i = 2; i < a->argc; i++)
1046 {
1047 if ( !strcmp(a->argv[i], "--name")
1048 || !strcmp(a->argv[i], "-name"))
1049 {
1050 if (a->argc <= i + 1 || !*a->argv[i+1])
1051 return errorArgument("Missing argument to '%s'", a->argv[i]);
1052 i++;
1053 name = a->argv[i];
1054 }
1055 else if ( !strcmp(a->argv[i], "--transient")
1056 || !strcmp(a->argv[i], "-transient"))
1057 {
1058 fTransient = true;
1059 }
1060 else
1061 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
1062 }
1063
1064 /* required arguments */
1065 if (!name)
1066 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Parameter --name is required");
1067
1068 if (fTransient)
1069 {
1070 /* open an existing session for the VM */
1071 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
1072 /* get the session machine */
1073 ComPtr<IMachine> sessionMachine;
1074 CHECK_ERROR_RET(a->session, COMGETTER(Machine)(sessionMachine.asOutParam()), RTEXITCODE_FAILURE);
1075 /* get the session console */
1076 ComPtr<IConsole> console;
1077 CHECK_ERROR_RET(a->session, COMGETTER(Console)(console.asOutParam()), RTEXITCODE_FAILURE);
1078 if (console.isNull())
1079 return RTMsgErrorExit(RTEXITCODE_FAILURE,
1080 "Machine '%s' is not currently running.\n", pszMachineName);
1081
1082 CHECK_ERROR(console, RemoveSharedFolder(Bstr(name).raw()));
1083
1084 a->session->UnlockMachine();
1085 }
1086 else
1087 {
1088 /* open a session for the VM */
1089 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Write), RTEXITCODE_FAILURE);
1090
1091 /* get the mutable session machine */
1092 ComPtr<IMachine> sessionMachine;
1093 a->session->COMGETTER(Machine)(sessionMachine.asOutParam());
1094
1095 CHECK_ERROR(sessionMachine, RemoveSharedFolder(Bstr(name).raw()));
1096
1097 /* commit and close the session */
1098 CHECK_ERROR(sessionMachine, SaveSettings());
1099 a->session->UnlockMachine();
1100 }
1101 }
1102 else
1103 return errorSyntax(USAGE_SHAREDFOLDER, "Invalid parameter '%s'", Utf8Str(a->argv[0]).c_str());
1104
1105 return RTEXITCODE_SUCCESS;
1106}
1107
1108RTEXITCODE handleExtPack(HandlerArg *a)
1109{
1110 if (a->argc < 1)
1111 return errorNoSubcommand();
1112
1113 ComObjPtr<IExtPackManager> ptrExtPackMgr;
1114 CHECK_ERROR2I_RET(a->virtualBox, COMGETTER(ExtensionPackManager)(ptrExtPackMgr.asOutParam()), RTEXITCODE_FAILURE);
1115
1116 RTGETOPTSTATE GetState;
1117 RTGETOPTUNION ValueUnion;
1118 int ch;
1119 HRESULT hrc = S_OK;
1120
1121 if (!strcmp(a->argv[0], "install"))
1122 {
1123 setCurrentSubcommand(HELP_SCOPE_EXTPACK_INSTALL);
1124 const char *pszName = NULL;
1125 bool fReplace = false;
1126
1127 static const RTGETOPTDEF s_aInstallOptions[] =
1128 {
1129 { "--replace", 'r', RTGETOPT_REQ_NOTHING },
1130 };
1131
1132 RTGetOptInit(&GetState, a->argc, a->argv, s_aInstallOptions, RT_ELEMENTS(s_aInstallOptions), 1, 0 /*fFlags*/);
1133 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
1134 {
1135 switch (ch)
1136 {
1137 case 'r':
1138 fReplace = true;
1139 break;
1140
1141 case VINF_GETOPT_NOT_OPTION:
1142 if (pszName)
1143 return errorSyntax("Too many extension pack names given to \"extpack uninstall\"");
1144 pszName = ValueUnion.psz;
1145 break;
1146
1147 default:
1148 return errorGetOpt(ch, &ValueUnion);
1149 }
1150 }
1151 if (!pszName)
1152 return errorSyntax("No extension pack name was given to \"extpack install\"");
1153
1154 char szPath[RTPATH_MAX];
1155 int vrc = RTPathAbs(pszName, szPath, sizeof(szPath));
1156 if (RT_FAILURE(vrc))
1157 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPathAbs(%s,,) failed with rc=%Rrc", pszName, vrc);
1158
1159 Bstr bstrTarball(szPath);
1160 Bstr bstrName;
1161 ComPtr<IExtPackFile> ptrExtPackFile;
1162 CHECK_ERROR2I_RET(ptrExtPackMgr, OpenExtPackFile(bstrTarball.raw(), ptrExtPackFile.asOutParam()), RTEXITCODE_FAILURE);
1163 CHECK_ERROR2I_RET(ptrExtPackFile, COMGETTER(Name)(bstrName.asOutParam()), RTEXITCODE_FAILURE);
1164 ComPtr<IProgress> ptrProgress;
1165 CHECK_ERROR2I_RET(ptrExtPackFile, Install(fReplace, NULL, ptrProgress.asOutParam()), RTEXITCODE_FAILURE);
1166 hrc = showProgress(ptrProgress);
1167 CHECK_PROGRESS_ERROR_RET(ptrProgress, ("Failed to install \"%s\"", szPath), RTEXITCODE_FAILURE);
1168
1169 RTPrintf("Successfully installed \"%ls\".\n", bstrName.raw());
1170 }
1171 else if (!strcmp(a->argv[0], "uninstall"))
1172 {
1173 setCurrentSubcommand(HELP_SCOPE_EXTPACK_UNINSTALL);
1174 const char *pszName = NULL;
1175 bool fForced = false;
1176
1177 static const RTGETOPTDEF s_aUninstallOptions[] =
1178 {
1179 { "--force", 'f', RTGETOPT_REQ_NOTHING },
1180 };
1181
1182 RTGetOptInit(&GetState, a->argc, a->argv, s_aUninstallOptions, RT_ELEMENTS(s_aUninstallOptions), 1, 0);
1183 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
1184 {
1185 switch (ch)
1186 {
1187 case 'f':
1188 fForced = true;
1189 break;
1190
1191 case VINF_GETOPT_NOT_OPTION:
1192 if (pszName)
1193 return errorSyntax("Too many extension pack names given to \"extpack uninstall\"");
1194 pszName = ValueUnion.psz;
1195 break;
1196
1197 default:
1198 return errorGetOpt(ch, &ValueUnion);
1199 }
1200 }
1201 if (!pszName)
1202 return errorSyntax("No extension pack name was given to \"extpack uninstall\"");
1203
1204 Bstr bstrName(pszName);
1205 ComPtr<IProgress> ptrProgress;
1206 CHECK_ERROR2I_RET(ptrExtPackMgr, Uninstall(bstrName.raw(), fForced, NULL, ptrProgress.asOutParam()), RTEXITCODE_FAILURE);
1207 hrc = showProgress(ptrProgress);
1208 CHECK_PROGRESS_ERROR_RET(ptrProgress, ("Failed to uninstall \"%s\"", pszName), RTEXITCODE_FAILURE);
1209
1210 RTPrintf("Successfully uninstalled \"%s\".\n", pszName);
1211 }
1212 else if (!strcmp(a->argv[0], "cleanup"))
1213 {
1214 setCurrentSubcommand(HELP_SCOPE_EXTPACK_CLEANUP);
1215 if (a->argc > 1)
1216 return errorTooManyParameters(&a->argv[1]);
1217 CHECK_ERROR2I_RET(ptrExtPackMgr, Cleanup(), RTEXITCODE_FAILURE);
1218 RTPrintf("Successfully performed extension pack cleanup\n");
1219 }
1220 else
1221 return errorUnknownSubcommand(a->argv[0]);
1222
1223 return RTEXITCODE_SUCCESS;
1224}
1225
1226static const RTGETOPTDEF g_aUnattendedInstallOptions[] =
1227{
1228 { "--user", 'u', RTGETOPT_REQ_STRING },
1229 { "-user", 'u', RTGETOPT_REQ_STRING },
1230 { "--password", 'p', RTGETOPT_REQ_STRING },
1231 { "-password", 'p', RTGETOPT_REQ_STRING },
1232 { "--key", 'k', RTGETOPT_REQ_STRING },
1233 { "-key", 'k', RTGETOPT_REQ_STRING },
1234 { "--isopath", 'i', RTGETOPT_REQ_STRING },
1235 { "-isopath", 'i', RTGETOPT_REQ_STRING },
1236 { "--addisopath", 'a', RTGETOPT_REQ_STRING },
1237 { "-addisopath", 'a', RTGETOPT_REQ_STRING },
1238 { "-imageindex", 'm', RTGETOPT_REQ_UINT16 },
1239 { "--imageindex", 'm', RTGETOPT_REQ_UINT16 },
1240};
1241
1242RTEXITCODE handleUnattendedInstall(HandlerArg *a)
1243{
1244 HRESULT rc;
1245 Bstr vboxAdditionsIsoPath;
1246 Bstr isoPath;
1247 Bstr user;
1248 Bstr password;
1249 Bstr productKey;
1250 Bstr group("group");
1251 Bstr uuid = Guid(a->argv[0]).toUtf16().raw();
1252 Bstr machineName;
1253 BOOL bInstallGuestAdditions;
1254 unsigned short imageIndex = 1;//applied only to Windows installation
1255
1256 int c;
1257 RTGETOPTUNION ValueUnion;
1258 RTGETOPTSTATE GetState;
1259
1260 // start at 0 because main() has hacked both the argc and argv given to us
1261 RTGetOptInit(&GetState, a->argc, a->argv, g_aUnattendedInstallOptions, RT_ELEMENTS(g_aUnattendedInstallOptions),
1262 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1263 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1264 {
1265 switch (c)
1266 {
1267 case 'u': // --user
1268 user = ValueUnion.psz;
1269 break;
1270
1271 case 'p': // --password
1272 password = ValueUnion.psz;
1273 break;
1274
1275 case 'k': // --key
1276 productKey = ValueUnion.psz;
1277 break;
1278
1279 case 'a': // --addisopath
1280 vboxAdditionsIsoPath = ValueUnion.psz;
1281 break;
1282
1283 case 'i': // --isopath
1284 isoPath = ValueUnion.psz;
1285 break;
1286 case 'm': // --image index
1287 imageIndex = ValueUnion.u8;
1288 break;
1289 default:
1290 return errorGetOpt(USAGE_UNATTENDEDINSTALL, c, &ValueUnion);
1291 }
1292 }
1293
1294 /* check for required options */
1295 if (user.isEmpty() ||
1296 password.isEmpty() ||
1297 isoPath.isEmpty()
1298 )
1299 return errorSyntax(USAGE_UNATTENDEDINSTALL, "One of needed parameters has been missed");
1300
1301 /* try to find the given machine */
1302 ComPtr<IMachine> machine;
1303 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam()));
1304 if (FAILED(rc))
1305 return RTEXITCODE_FAILURE;
1306
1307 CHECK_ERROR_RET(machine, COMGETTER(Name)(machineName.asOutParam()), RTEXITCODE_FAILURE);
1308 /* open a session for the VM */
1309 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
1310
1311 ComPtr<IConsole> console;
1312 ComPtr<IMachine> sessionMachine;
1313 ComPtr<IUnattended> unAttended;
1314
1315 /* get the associated console */
1316 CHECK_ERROR(a->session, COMGETTER(Console)(console.asOutParam()));
1317 if (console)
1318 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Machine '%s' is currently running", Utf8Str(uuid).c_str());
1319
1320 /* ... and session machine */
1321 CHECK_ERROR_RET(a->session, COMGETTER(Machine)(sessionMachine.asOutParam()), RTEXITCODE_FAILURE);
1322
1323 BSTR installedOSBSTR;
1324
1325 CHECK_ERROR_RET(sessionMachine, COMGETTER(OSTypeId)(&installedOSBSTR), RTEXITCODE_FAILURE);
1326
1327 Utf8Str installedOSStr(installedOSBSTR);
1328
1329 do{
1330 RTPrintf("Start unattended installation OS %s on virtual machine '%ls'.\n"
1331 "UUID: %s\n",
1332 installedOSStr.c_str(),
1333 machineName.raw(),
1334 Utf8Str(uuid).c_str());
1335
1336 CHECK_ERROR_BREAK(machine, COMGETTER(Unattended)(unAttended.asOutParam()));
1337
1338 CHECK_ERROR_BREAK(unAttended, COMSETTER(Group)(group.raw()));
1339
1340 if(installedOSStr.contains("Windows") && productKey.isEmpty())
1341 {
1342 return errorSyntax(USAGE_UNATTENDEDINSTALL, "Product key has been missed.");
1343 }
1344
1345 CHECK_ERROR_BREAK(unAttended, COMSETTER(VboxAdditionsIsoPath)(vboxAdditionsIsoPath.raw()));
1346
1347 CHECK_ERROR_BREAK(unAttended, COMSETTER(IsoPath)(isoPath.raw()));
1348
1349 CHECK_ERROR_BREAK(unAttended, COMSETTER(User)(user.raw()));
1350
1351 CHECK_ERROR_BREAK(unAttended, COMSETTER(Password)(password.raw()));
1352
1353 CHECK_ERROR_BREAK(unAttended, COMSETTER(ProductKey)(productKey.raw()));
1354
1355 bool fInstallGuestAdditions = (vboxAdditionsIsoPath.isEmpty()) ? false: true;
1356 CHECK_ERROR_BREAK(unAttended, COMSETTER(InstallGuestAdditions)(fInstallGuestAdditions));
1357
1358 CHECK_ERROR_BREAK(unAttended, COMSETTER(ImageIndex)(imageIndex));
1359
1360 CHECK_ERROR_BREAK(unAttended,Preparation());
1361 CHECK_ERROR_BREAK(unAttended,ConstructScript());
1362 CHECK_ERROR_BREAK(unAttended,ConstructMedia());
1363 CHECK_ERROR_BREAK(unAttended,ReconstructVM());
1364 CHECK_ERROR_BREAK(unAttended,RunInstallation());
1365 a->session->UnlockMachine();
1366// CHECK_ERROR_RET(sessionMachine, UnlockMachine(), RTEXITCODE_FAILURE);
1367 {
1368 Bstr env;
1369 Bstr sessionType = "headless";//"gui";
1370
1371#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
1372 /* make sure the VM process will start on the same display as VBoxManage */
1373 Utf8Str str;
1374 const char *pszDisplay = RTEnvGet("DISPLAY");
1375 if (pszDisplay)
1376 str = Utf8StrFmt("DISPLAY=%s\n", pszDisplay);
1377 const char *pszXAuth = RTEnvGet("XAUTHORITY");
1378 if (pszXAuth)
1379 str.append(Utf8StrFmt("XAUTHORITY=%s\n", pszXAuth));
1380 env = str;
1381#endif
1382 ComPtr<IProgress> progress;
1383 CHECK_ERROR(machine, LaunchVMProcess(a->session, sessionType.raw(),
1384 env.raw(), progress.asOutParam()));
1385 if (SUCCEEDED(rc) && !progress.isNull())
1386 {
1387 RTPrintf("Waiting for VM \"%s\" to power on...\n", Utf8Str(uuid).c_str());
1388 CHECK_ERROR(progress, WaitForCompletion(-1));
1389 if (SUCCEEDED(rc))
1390 {
1391 BOOL completed = true;
1392 CHECK_ERROR(progress, COMGETTER(Completed)(&completed));
1393 if (SUCCEEDED(rc))
1394 {
1395 ASSERT(completed);
1396
1397 LONG iRc;
1398 CHECK_ERROR(progress, COMGETTER(ResultCode)(&iRc));
1399 if (SUCCEEDED(rc))
1400 {
1401 if (SUCCEEDED(iRc))
1402 RTPrintf("VM \"%s\" has been successfully started.\n", Utf8Str(uuid).c_str());
1403 else
1404 {
1405 ProgressErrorInfo info(progress);
1406 com::GluePrintErrorInfo(info);
1407 }
1408 rc = iRc;
1409 }
1410 }
1411 }
1412 }
1413 }
1414
1415 vboxAdditionsIsoPath.setNull();
1416 isoPath.setNull();
1417 user.setNull();
1418 password.setNull();
1419 productKey.setNull();
1420 group.setNull();
1421 bInstallGuestAdditions = false;
1422 imageIndex = 0;
1423
1424 CHECK_ERROR_BREAK(unAttended, COMGETTER(Group)(group.asOutParam()));
1425 CHECK_ERROR_BREAK(unAttended, COMGETTER(VboxAdditionsIsoPath)(vboxAdditionsIsoPath.asOutParam()));
1426 CHECK_ERROR_BREAK(unAttended, COMGETTER(IsoPath)(isoPath.asOutParam()));
1427 CHECK_ERROR_BREAK(unAttended, COMGETTER(User)(user.asOutParam()));
1428 CHECK_ERROR_BREAK(unAttended, COMGETTER(Password)(password.asOutParam()));
1429 CHECK_ERROR_BREAK(unAttended, COMGETTER(ProductKey)(productKey.asOutParam()));
1430 CHECK_ERROR_BREAK(unAttended, COMGETTER(ImageIndex)(&imageIndex));
1431 RTPrintf("Got values:\n user: %s\n password: %s\n productKey: %s\n image index: %d\n "
1432 "isoPath: %s\n vboxAdditionsIsoPath: %s\n",
1433 Utf8Str(user).c_str(),
1434 Utf8Str(password).c_str(),
1435 Utf8Str(productKey).c_str(),
1436 imageIndex,
1437 Utf8Str(isoPath).c_str(),
1438 Utf8Str(vboxAdditionsIsoPath).c_str()
1439 );
1440 }
1441 while (0);
1442
1443 a->session->UnlockMachine();
1444// CHECK_ERROR_RET(sessionMachine, UnlockMachine(), RTEXITCODE_FAILURE);
1445
1446 RTPrintf("Unattended installation OS %s has been running on virtual machine '%ls'.\n"
1447 "UUID: %s\n",
1448 installedOSStr.c_str(),
1449 machineName.raw(),
1450 Utf8Str(uuid).c_str());
1451
1452 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1453}
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