VirtualBox

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

Last change on this file since 34913 was 34787, checked in by vboxsync, 14 years ago

Main,VBoxManage,FE/Qt: Implemented IExtPackFile and dropped IExtPackManager::Install.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.1 KB
Line 
1/* $Id: VBoxManageMisc.cpp 34787 2010-12-07 14:51:18Z vboxsync $ */
2/** @file
3 * VBoxManage - VirtualBox's command-line interface.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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/EventQueue.h>
30
31# include <VBox/com/VirtualBox.h>
32#endif /* !VBOX_ONLY_DOCS */
33
34#include <iprt/asm.h>
35#include <iprt/buildconfig.h>
36#include <iprt/cidr.h>
37#include <iprt/ctype.h>
38#include <iprt/dir.h>
39#include <iprt/env.h>
40#include <VBox/err.h>
41#include <iprt/file.h>
42#include <iprt/initterm.h>
43#include <iprt/param.h>
44#include <iprt/path.h>
45#include <iprt/stream.h>
46#include <iprt/string.h>
47#include <iprt/stdarg.h>
48#include <iprt/thread.h>
49#include <iprt/uuid.h>
50#include <iprt/getopt.h>
51#include <iprt/ctype.h>
52#include <VBox/version.h>
53#include <VBox/log.h>
54
55#include "VBoxManage.h"
56
57using namespace com;
58
59
60
61int 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 {
80 RTMsgError("Cannot convert filename \"%s\" to absolute path", a->argv[0]);
81 return 1;
82 }
83 CHECK_ERROR(a->virtualBox, OpenMachine(Bstr(szVMFileAbs).raw(),
84 machine.asOutParam()));
85 }
86 else if (FAILED(rc))
87 CHECK_ERROR(a->virtualBox, OpenMachine(Bstr(a->argv[0]).raw(),
88 machine.asOutParam()));
89 if (SUCCEEDED(rc))
90 {
91 ASSERT(machine);
92 CHECK_ERROR(a->virtualBox, RegisterMachine(machine));
93 }
94 return SUCCEEDED(rc) ? 0 : 1;
95}
96
97static const RTGETOPTDEF g_aUnregisterVMOptions[] =
98{
99 { "--delete", 'd', RTGETOPT_REQ_NOTHING },
100 { "-delete", 'd', RTGETOPT_REQ_NOTHING }, // deprecated
101};
102
103int handleUnregisterVM(HandlerArg *a)
104{
105 HRESULT rc;
106 const char *VMName = NULL;
107 bool fDelete = false;
108
109 int c;
110 RTGETOPTUNION ValueUnion;
111 RTGETOPTSTATE GetState;
112 // start at 0 because main() has hacked both the argc and argv given to us
113 RTGetOptInit(&GetState, a->argc, a->argv, g_aUnregisterVMOptions, RT_ELEMENTS(g_aUnregisterVMOptions),
114 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
115 while ((c = RTGetOpt(&GetState, &ValueUnion)))
116 {
117 switch (c)
118 {
119 case 'd': // --delete
120 fDelete = true;
121 break;
122
123 case VINF_GETOPT_NOT_OPTION:
124 if (!VMName)
125 VMName = ValueUnion.psz;
126 else
127 return errorSyntax(USAGE_UNREGISTERVM, "Invalid parameter '%s'", ValueUnion.psz);
128 break;
129
130 default:
131 if (c > 0)
132 {
133 if (RT_C_IS_PRINT(c))
134 return errorSyntax(USAGE_UNREGISTERVM, "Invalid option -%c", c);
135 else
136 return errorSyntax(USAGE_UNREGISTERVM, "Invalid option case %i", c);
137 }
138 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
139 return errorSyntax(USAGE_UNREGISTERVM, "unknown option: %s\n", ValueUnion.psz);
140 else if (ValueUnion.pDef)
141 return errorSyntax(USAGE_UNREGISTERVM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
142 else
143 return errorSyntax(USAGE_UNREGISTERVM, "error: %Rrs", c);
144 }
145 }
146
147 /* check for required options */
148 if (!VMName)
149 return errorSyntax(USAGE_UNREGISTERVM, "VM name required");
150
151 ComPtr<IMachine> machine;
152 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(VMName).raw(),
153 machine.asOutParam()));
154 if (machine)
155 {
156 SafeIfaceArray<IMedium> aMedia;
157 CleanupMode_T cleanupMode = CleanupMode_DetachAllReturnNone;
158 if (fDelete)
159 cleanupMode = CleanupMode_DetachAllReturnHardDisksOnly;
160 CHECK_ERROR(machine, Unregister(cleanupMode,
161 ComSafeArrayAsOutParam(aMedia)));
162 if (SUCCEEDED(rc))
163 {
164 if (fDelete)
165 {
166 ComPtr<IProgress> pProgress;
167 CHECK_ERROR(machine, Delete(ComSafeArrayAsInParam(aMedia), pProgress.asOutParam()));
168 CHECK_ERROR(pProgress, WaitForCompletion(-1));
169 }
170 }
171 }
172 return SUCCEEDED(rc) ? 0 : 1;
173}
174
175int handleCreateVM(HandlerArg *a)
176{
177 HRESULT rc;
178 Bstr baseFolder;
179 Bstr name;
180 Bstr osTypeId;
181 RTUUID id;
182 bool fRegister = false;
183
184 RTUuidClear(&id);
185 for (int i = 0; i < a->argc; i++)
186 {
187 if ( !strcmp(a->argv[i], "--basefolder")
188 || !strcmp(a->argv[i], "-basefolder"))
189 {
190 if (a->argc <= i + 1)
191 return errorArgument("Missing argument to '%s'", a->argv[i]);
192 i++;
193 baseFolder = a->argv[i];
194 }
195 else if ( !strcmp(a->argv[i], "--name")
196 || !strcmp(a->argv[i], "-name"))
197 {
198 if (a->argc <= i + 1)
199 return errorArgument("Missing argument to '%s'", a->argv[i]);
200 i++;
201 name = a->argv[i];
202 }
203 else if ( !strcmp(a->argv[i], "--ostype")
204 || !strcmp(a->argv[i], "-ostype"))
205 {
206 if (a->argc <= i + 1)
207 return errorArgument("Missing argument to '%s'", a->argv[i]);
208 i++;
209 osTypeId = a->argv[i];
210 }
211 else if ( !strcmp(a->argv[i], "--uuid")
212 || !strcmp(a->argv[i], "-uuid"))
213 {
214 if (a->argc <= i + 1)
215 return errorArgument("Missing argument to '%s'", a->argv[i]);
216 i++;
217 if (RT_FAILURE(RTUuidFromStr(&id, a->argv[i])))
218 return errorArgument("Invalid UUID format %s\n", a->argv[i]);
219 }
220 else if ( !strcmp(a->argv[i], "--register")
221 || !strcmp(a->argv[i], "-register"))
222 {
223 fRegister = true;
224 }
225 else
226 return errorSyntax(USAGE_CREATEVM, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
227 }
228
229 /* check for required options */
230 if (name.isEmpty())
231 return errorSyntax(USAGE_CREATEVM, "Parameter --name is required");
232
233 do
234 {
235 Bstr bstrSettingsFile;
236 CHECK_ERROR_BREAK(a->virtualBox,
237 ComposeMachineFilename(name.raw(),
238 baseFolder.raw(),
239 bstrSettingsFile.asOutParam()));
240 ComPtr<IMachine> machine;
241 CHECK_ERROR_BREAK(a->virtualBox,
242 CreateMachine(bstrSettingsFile.raw(),
243 name.raw(),
244 osTypeId.raw(),
245 Guid(id).toUtf16().raw(),
246 FALSE /* forceOverwrite */,
247 machine.asOutParam()));
248
249 CHECK_ERROR_BREAK(machine, SaveSettings());
250 if (fRegister)
251 {
252 CHECK_ERROR_BREAK(a->virtualBox, RegisterMachine(machine));
253 }
254 Bstr uuid;
255 CHECK_ERROR_BREAK(machine, COMGETTER(Id)(uuid.asOutParam()));
256 Bstr settingsFile;
257 CHECK_ERROR_BREAK(machine, COMGETTER(SettingsFilePath)(settingsFile.asOutParam()));
258 RTPrintf("Virtual machine '%ls' is created%s.\n"
259 "UUID: %s\n"
260 "Settings file: '%ls'\n",
261 name.raw(), fRegister ? " and registered" : "",
262 Utf8Str(uuid).c_str(), settingsFile.raw());
263 }
264 while (0);
265
266 return SUCCEEDED(rc) ? 0 : 1;
267}
268
269int handleStartVM(HandlerArg *a)
270{
271 HRESULT rc;
272 const char *VMName = NULL;
273 Bstr sessionType = "gui";
274
275 static const RTGETOPTDEF s_aStartVMOptions[] =
276 {
277 { "--type", 't', RTGETOPT_REQ_STRING },
278 { "-type", 't', RTGETOPT_REQ_STRING }, // deprecated
279 };
280 int c;
281 RTGETOPTUNION ValueUnion;
282 RTGETOPTSTATE GetState;
283 // start at 0 because main() has hacked both the argc and argv given to us
284 RTGetOptInit(&GetState, a->argc, a->argv, s_aStartVMOptions, RT_ELEMENTS(s_aStartVMOptions),
285 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
286 while ((c = RTGetOpt(&GetState, &ValueUnion)))
287 {
288 switch (c)
289 {
290 case 't': // --type
291 if (!RTStrICmp(ValueUnion.psz, "gui"))
292 {
293 sessionType = "gui";
294 }
295#ifdef VBOX_WITH_VBOXSDL
296 else if (!RTStrICmp(ValueUnion.psz, "sdl"))
297 {
298 sessionType = "sdl";
299 }
300#endif
301#ifdef VBOX_WITH_HEADLESS
302 else if (!RTStrICmp(ValueUnion.psz, "capture"))
303 {
304 sessionType = "capture";
305 }
306 else if (!RTStrICmp(ValueUnion.psz, "headless"))
307 {
308 sessionType = "headless";
309 }
310#endif
311 else
312 return errorArgument("Invalid session type '%s'", ValueUnion.psz);
313 break;
314
315 case VINF_GETOPT_NOT_OPTION:
316 if (!VMName)
317 VMName = ValueUnion.psz;
318 else
319 return errorSyntax(USAGE_STARTVM, "Invalid parameter '%s'", ValueUnion.psz);
320 break;
321
322 default:
323 if (c > 0)
324 {
325 if (RT_C_IS_PRINT(c))
326 return errorSyntax(USAGE_STARTVM, "Invalid option -%c", c);
327 else
328 return errorSyntax(USAGE_STARTVM, "Invalid option case %i", c);
329 }
330 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
331 return errorSyntax(USAGE_STARTVM, "unknown option: %s\n", ValueUnion.psz);
332 else if (ValueUnion.pDef)
333 return errorSyntax(USAGE_STARTVM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
334 else
335 return errorSyntax(USAGE_STARTVM, "error: %Rrs", c);
336 }
337 }
338
339 /* check for required options */
340 if (!VMName)
341 return errorSyntax(USAGE_STARTVM, "VM name required");
342
343 ComPtr<IMachine> machine;
344 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(VMName).raw(),
345 machine.asOutParam()));
346 if (machine)
347 {
348 Bstr env;
349#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
350 /* make sure the VM process will start on the same display as VBoxManage */
351 Utf8Str str;
352 const char *pszDisplay = RTEnvGet("DISPLAY");
353 if (pszDisplay)
354 str = Utf8StrFmt("DISPLAY=%s\n", pszDisplay);
355 const char *pszXAuth = RTEnvGet("XAUTHORITY");
356 if (pszXAuth)
357 str.append(Utf8StrFmt("XAUTHORITY=%s\n", pszXAuth));
358 env = str;
359#endif
360 ComPtr<IProgress> progress;
361 CHECK_ERROR_RET(machine, LaunchVMProcess(a->session, sessionType.raw(),
362 env.raw(), progress.asOutParam()), rc);
363 RTPrintf("Waiting for the VM to power on...\n");
364 CHECK_ERROR_RET(progress, WaitForCompletion(-1), 1);
365
366 BOOL completed;
367 CHECK_ERROR_RET(progress, COMGETTER(Completed)(&completed), rc);
368 ASSERT(completed);
369
370 LONG iRc;
371 CHECK_ERROR_RET(progress, COMGETTER(ResultCode)(&iRc), rc);
372 if (FAILED(iRc))
373 {
374 ComPtr<IVirtualBoxErrorInfo> errorInfo;
375 CHECK_ERROR_RET(progress, COMGETTER(ErrorInfo)(errorInfo.asOutParam()), 1);
376 ErrorInfo info(errorInfo, COM_IIDOF(IVirtualBoxErrorInfo));
377 com::GluePrintErrorInfo(info);
378 }
379 else
380 {
381 RTPrintf("VM has been successfully started.\n");
382 }
383 }
384
385 /* it's important to always close sessions */
386 a->session->UnlockMachine();
387
388 return SUCCEEDED(rc) ? 0 : 1;
389}
390
391int handleDiscardState(HandlerArg *a)
392{
393 HRESULT rc;
394
395 if (a->argc != 1)
396 return errorSyntax(USAGE_DISCARDSTATE, "Incorrect number of parameters");
397
398 ComPtr<IMachine> machine;
399 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
400 machine.asOutParam()));
401 if (machine)
402 {
403 do
404 {
405 /* we have to open a session for this task */
406 CHECK_ERROR_BREAK(machine, LockMachine(a->session, LockType_Write));
407 do
408 {
409 ComPtr<IConsole> console;
410 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
411 CHECK_ERROR_BREAK(console, DiscardSavedState(true /* fDeleteFile */));
412 } while (0);
413 CHECK_ERROR_BREAK(a->session, UnlockMachine());
414 } while (0);
415 }
416
417 return SUCCEEDED(rc) ? 0 : 1;
418}
419
420int handleAdoptState(HandlerArg *a)
421{
422 HRESULT rc;
423
424 if (a->argc != 2)
425 return errorSyntax(USAGE_ADOPTSTATE, "Incorrect number of parameters");
426
427 ComPtr<IMachine> machine;
428 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
429 machine.asOutParam()));
430 if (machine)
431 {
432 do
433 {
434 /* we have to open a session for this task */
435 CHECK_ERROR_BREAK(machine, LockMachine(a->session, LockType_Write));
436 do
437 {
438 ComPtr<IConsole> console;
439 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
440 CHECK_ERROR_BREAK(console, AdoptSavedState(Bstr(a->argv[1]).raw()));
441 } while (0);
442 CHECK_ERROR_BREAK(a->session, UnlockMachine());
443 } while (0);
444 }
445
446 return SUCCEEDED(rc) ? 0 : 1;
447}
448
449int handleGetExtraData(HandlerArg *a)
450{
451 HRESULT rc = S_OK;
452
453 if (a->argc != 2)
454 return errorSyntax(USAGE_GETEXTRADATA, "Incorrect number of parameters");
455
456 /* global data? */
457 if (!strcmp(a->argv[0], "global"))
458 {
459 /* enumeration? */
460 if (!strcmp(a->argv[1], "enumerate"))
461 {
462 SafeArray<BSTR> aKeys;
463 CHECK_ERROR(a->virtualBox, GetExtraDataKeys(ComSafeArrayAsOutParam(aKeys)));
464
465 for (size_t i = 0;
466 i < aKeys.size();
467 ++i)
468 {
469 Bstr bstrKey(aKeys[i]);
470 Bstr bstrValue;
471 CHECK_ERROR(a->virtualBox, GetExtraData(bstrKey.raw(),
472 bstrValue.asOutParam()));
473
474 RTPrintf("Key: %lS, Value: %lS\n", bstrKey.raw(), bstrValue.raw());
475 }
476 }
477 else
478 {
479 Bstr value;
480 CHECK_ERROR(a->virtualBox, GetExtraData(Bstr(a->argv[1]).raw(),
481 value.asOutParam()));
482 if (!value.isEmpty())
483 RTPrintf("Value: %lS\n", value.raw());
484 else
485 RTPrintf("No value set!\n");
486 }
487 }
488 else
489 {
490 ComPtr<IMachine> machine;
491 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
492 machine.asOutParam()));
493 if (machine)
494 {
495 /* enumeration? */
496 if (!strcmp(a->argv[1], "enumerate"))
497 {
498 SafeArray<BSTR> aKeys;
499 CHECK_ERROR(machine, GetExtraDataKeys(ComSafeArrayAsOutParam(aKeys)));
500
501 for (size_t i = 0;
502 i < aKeys.size();
503 ++i)
504 {
505 Bstr bstrKey(aKeys[i]);
506 Bstr bstrValue;
507 CHECK_ERROR(machine, GetExtraData(bstrKey.raw(),
508 bstrValue.asOutParam()));
509
510 RTPrintf("Key: %lS, Value: %lS\n", bstrKey.raw(), bstrValue.raw());
511 }
512 }
513 else
514 {
515 Bstr value;
516 CHECK_ERROR(machine, GetExtraData(Bstr(a->argv[1]).raw(),
517 value.asOutParam()));
518 if (!value.isEmpty())
519 RTPrintf("Value: %lS\n", value.raw());
520 else
521 RTPrintf("No value set!\n");
522 }
523 }
524 }
525 return SUCCEEDED(rc) ? 0 : 1;
526}
527
528int handleSetExtraData(HandlerArg *a)
529{
530 HRESULT rc = S_OK;
531
532 if (a->argc < 2)
533 return errorSyntax(USAGE_SETEXTRADATA, "Not enough parameters");
534
535 /* global data? */
536 if (!strcmp(a->argv[0], "global"))
537 {
538 /** @todo passing NULL is deprecated */
539 if (a->argc < 3)
540 CHECK_ERROR(a->virtualBox, SetExtraData(Bstr(a->argv[1]).raw(),
541 NULL));
542 else if (a->argc == 3)
543 CHECK_ERROR(a->virtualBox, SetExtraData(Bstr(a->argv[1]).raw(),
544 Bstr(a->argv[2]).raw()));
545 else
546 return errorSyntax(USAGE_SETEXTRADATA, "Too many parameters");
547 }
548 else
549 {
550 ComPtr<IMachine> machine;
551 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
552 machine.asOutParam()));
553 if (machine)
554 {
555 /** @todo passing NULL is deprecated */
556 if (a->argc < 3)
557 CHECK_ERROR(machine, SetExtraData(Bstr(a->argv[1]).raw(),
558 NULL));
559 else if (a->argc == 3)
560 CHECK_ERROR(machine, SetExtraData(Bstr(a->argv[1]).raw(),
561 Bstr(a->argv[2]).raw()));
562 else
563 return errorSyntax(USAGE_SETEXTRADATA, "Too many parameters");
564 }
565 }
566 return SUCCEEDED(rc) ? 0 : 1;
567}
568
569int handleSetProperty(HandlerArg *a)
570{
571 HRESULT rc;
572
573 /* there must be two arguments: property name and value */
574 if (a->argc != 2)
575 return errorSyntax(USAGE_SETPROPERTY, "Incorrect number of parameters");
576
577 ComPtr<ISystemProperties> systemProperties;
578 a->virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
579
580 if (!strcmp(a->argv[0], "machinefolder"))
581 {
582 /* reset to default? */
583 if (!strcmp(a->argv[1], "default"))
584 CHECK_ERROR(systemProperties, COMSETTER(DefaultMachineFolder)(NULL));
585 else
586 CHECK_ERROR(systemProperties, COMSETTER(DefaultMachineFolder)(Bstr(a->argv[1]).raw()));
587 }
588 else if ( !strcmp(a->argv[0], "vrdeauthlibrary")
589 || !strcmp(a->argv[0], "vrdpauthlibrary"))
590 {
591 if (!strcmp(a->argv[0], "vrdpauthlibrary"))
592 RTStrmPrintf(g_pStdErr, "Warning: 'vrdpauthlibrary' is deprecated. Use 'vrdeauthlibrary'.\n");
593
594 /* reset to default? */
595 if (!strcmp(a->argv[1], "default"))
596 CHECK_ERROR(systemProperties, COMSETTER(VRDEAuthLibrary)(NULL));
597 else
598 CHECK_ERROR(systemProperties, COMSETTER(VRDEAuthLibrary)(Bstr(a->argv[1]).raw()));
599 }
600 else if (!strcmp(a->argv[0], "websrvauthlibrary"))
601 {
602 /* reset to default? */
603 if (!strcmp(a->argv[1], "default"))
604 CHECK_ERROR(systemProperties, COMSETTER(WebServiceAuthLibrary)(NULL));
605 else
606 CHECK_ERROR(systemProperties, COMSETTER(WebServiceAuthLibrary)(Bstr(a->argv[1]).raw()));
607 }
608 else if (!strcmp(a->argv[0], "vrdeextpack"))
609 {
610 /* disable? */
611 if (!strcmp(a->argv[1], "null"))
612 CHECK_ERROR(systemProperties, COMSETTER(DefaultVRDEExtPack)(NULL));
613 else
614 CHECK_ERROR(systemProperties, COMSETTER(DefaultVRDEExtPack)(Bstr(a->argv[1]).raw()));
615 }
616 else if (!strcmp(a->argv[0], "loghistorycount"))
617 {
618 uint32_t uVal;
619 int vrc;
620 vrc = RTStrToUInt32Ex(a->argv[1], NULL, 0, &uVal);
621 if (vrc != VINF_SUCCESS)
622 return errorArgument("Error parsing Log history count '%s'", a->argv[1]);
623 CHECK_ERROR(systemProperties, COMSETTER(LogHistoryCount)(uVal));
624 }
625 else
626 return errorSyntax(USAGE_SETPROPERTY, "Invalid parameter '%s'", a->argv[0]);
627
628 return SUCCEEDED(rc) ? 0 : 1;
629}
630
631int handleSharedFolder(HandlerArg *a)
632{
633 HRESULT rc;
634
635 /* we need at least a command and target */
636 if (a->argc < 2)
637 return errorSyntax(USAGE_SHAREDFOLDER, "Not enough parameters");
638
639 ComPtr<IMachine> machine;
640 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[1]).raw(),
641 machine.asOutParam()));
642 if (!machine)
643 return 1;
644
645 if (!strcmp(a->argv[0], "add"))
646 {
647 /* we need at least four more parameters */
648 if (a->argc < 5)
649 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Not enough parameters");
650
651 char *name = NULL;
652 char *hostpath = NULL;
653 bool fTransient = false;
654 bool fWritable = true;
655 bool fAutoMount = false;
656
657 for (int i = 2; i < a->argc; i++)
658 {
659 if ( !strcmp(a->argv[i], "--name")
660 || !strcmp(a->argv[i], "-name"))
661 {
662 if (a->argc <= i + 1 || !*a->argv[i+1])
663 return errorArgument("Missing argument to '%s'", a->argv[i]);
664 i++;
665 name = a->argv[i];
666 }
667 else if ( !strcmp(a->argv[i], "--hostpath")
668 || !strcmp(a->argv[i], "-hostpath"))
669 {
670 if (a->argc <= i + 1 || !*a->argv[i+1])
671 return errorArgument("Missing argument to '%s'", a->argv[i]);
672 i++;
673 hostpath = a->argv[i];
674 }
675 else if ( !strcmp(a->argv[i], "--readonly")
676 || !strcmp(a->argv[i], "-readonly"))
677 {
678 fWritable = false;
679 }
680 else if ( !strcmp(a->argv[i], "--transient")
681 || !strcmp(a->argv[i], "-transient"))
682 {
683 fTransient = true;
684 }
685 else if ( !strcmp(a->argv[i], "--automount")
686 || !strcmp(a->argv[i], "-automount"))
687 {
688 fAutoMount = true;
689 }
690 else
691 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
692 }
693
694 if (NULL != strstr(name, " "))
695 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "No spaces allowed in parameter '-name'!");
696
697 /* required arguments */
698 if (!name || !hostpath)
699 {
700 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Parameters --name and --hostpath are required");
701 }
702
703 if (fTransient)
704 {
705 ComPtr <IConsole> console;
706
707 /* open an existing session for the VM */
708 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
709 /* get the session machine */
710 CHECK_ERROR_RET(a->session, COMGETTER(Machine)(machine.asOutParam()), 1);
711 /* get the session console */
712 CHECK_ERROR_RET(a->session, COMGETTER(Console)(console.asOutParam()), 1);
713
714 CHECK_ERROR(console, CreateSharedFolder(Bstr(name).raw(),
715 Bstr(hostpath).raw(),
716 fWritable, fAutoMount));
717 if (console)
718 a->session->UnlockMachine();
719 }
720 else
721 {
722 /* open a session for the VM */
723 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Write), 1);
724
725 /* get the mutable session machine */
726 a->session->COMGETTER(Machine)(machine.asOutParam());
727
728 CHECK_ERROR(machine, CreateSharedFolder(Bstr(name).raw(),
729 Bstr(hostpath).raw(),
730 fWritable, fAutoMount));
731 if (SUCCEEDED(rc))
732 CHECK_ERROR(machine, SaveSettings());
733
734 a->session->UnlockMachine();
735 }
736 }
737 else if (!strcmp(a->argv[0], "remove"))
738 {
739 /* we need at least two more parameters */
740 if (a->argc < 3)
741 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Not enough parameters");
742
743 char *name = NULL;
744 bool fTransient = false;
745
746 for (int i = 2; i < a->argc; i++)
747 {
748 if ( !strcmp(a->argv[i], "--name")
749 || !strcmp(a->argv[i], "-name"))
750 {
751 if (a->argc <= i + 1 || !*a->argv[i+1])
752 return errorArgument("Missing argument to '%s'", a->argv[i]);
753 i++;
754 name = a->argv[i];
755 }
756 else if ( !strcmp(a->argv[i], "--transient")
757 || !strcmp(a->argv[i], "-transient"))
758 {
759 fTransient = true;
760 }
761 else
762 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
763 }
764
765 /* required arguments */
766 if (!name)
767 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Parameter --name is required");
768
769 if (fTransient)
770 {
771 ComPtr <IConsole> console;
772
773 /* open an existing session for the VM */
774 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
775 /* get the session machine */
776 CHECK_ERROR_RET(a->session, COMGETTER(Machine)(machine.asOutParam()), 1);
777 /* get the session console */
778 CHECK_ERROR_RET(a->session, COMGETTER(Console)(console.asOutParam()), 1);
779
780 CHECK_ERROR(console, RemoveSharedFolder(Bstr(name).raw()));
781
782 if (console)
783 a->session->UnlockMachine();
784 }
785 else
786 {
787 /* open a session for the VM */
788 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Write), 1);
789
790 /* get the mutable session machine */
791 a->session->COMGETTER(Machine)(machine.asOutParam());
792
793 CHECK_ERROR(machine, RemoveSharedFolder(Bstr(name).raw()));
794
795 /* commit and close the session */
796 CHECK_ERROR(machine, SaveSettings());
797 a->session->UnlockMachine();
798 }
799 }
800 else
801 return errorSyntax(USAGE_SETPROPERTY, "Invalid parameter '%s'", Utf8Str(a->argv[0]).c_str());
802
803 return 0;
804}
805
806int handleVMStatistics(HandlerArg *a)
807{
808 HRESULT rc;
809
810 /* at least one option: the UUID or name of the VM */
811 if (a->argc < 1)
812 return errorSyntax(USAGE_VM_STATISTICS, "Incorrect number of parameters");
813
814 /* try to find the given machine */
815 ComPtr<IMachine> machine;
816 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
817 machine.asOutParam()));
818 if (FAILED(rc))
819 return 1;
820
821 /* parse arguments. */
822 bool fReset = false;
823 bool fWithDescriptions = false;
824 const char *pszPattern = NULL; /* all */
825 for (int i = 1; i < a->argc; i++)
826 {
827 if ( !strcmp(a->argv[i], "--pattern")
828 || !strcmp(a->argv[i], "-pattern"))
829 {
830 if (pszPattern)
831 return errorSyntax(USAGE_VM_STATISTICS, "Multiple --patterns options is not permitted");
832 if (i + 1 >= a->argc)
833 return errorArgument("Missing argument to '%s'", a->argv[i]);
834 pszPattern = a->argv[++i];
835 }
836 else if ( !strcmp(a->argv[i], "--descriptions")
837 || !strcmp(a->argv[i], "-descriptions"))
838 fWithDescriptions = true;
839 /* add: --file <filename> and --formatted */
840 else if ( !strcmp(a->argv[i], "--reset")
841 || !strcmp(a->argv[i], "-reset"))
842 fReset = true;
843 else
844 return errorSyntax(USAGE_VM_STATISTICS, "Unknown option '%s'", a->argv[i]);
845 }
846 if (fReset && fWithDescriptions)
847 return errorSyntax(USAGE_VM_STATISTICS, "The --reset and --descriptions options does not mix");
848
849
850 /* open an existing session for the VM. */
851 CHECK_ERROR(machine, LockMachine(a->session, LockType_Shared));
852 if (SUCCEEDED(rc))
853 {
854 /* get the session console. */
855 ComPtr <IConsole> console;
856 CHECK_ERROR(a->session, COMGETTER(Console)(console.asOutParam()));
857 if (SUCCEEDED(rc))
858 {
859 /* get the machine debugger. */
860 ComPtr <IMachineDebugger> debugger;
861 CHECK_ERROR(console, COMGETTER(Debugger)(debugger.asOutParam()));
862 if (SUCCEEDED(rc))
863 {
864 if (fReset)
865 CHECK_ERROR(debugger, ResetStats(Bstr(pszPattern).raw()));
866 else
867 {
868 Bstr stats;
869 CHECK_ERROR(debugger, GetStats(Bstr(pszPattern).raw(),
870 fWithDescriptions,
871 stats.asOutParam()));
872 if (SUCCEEDED(rc))
873 {
874 /* if (fFormatted)
875 { big mess }
876 else
877 */
878 RTPrintf("%ls\n", stats.raw());
879 }
880 }
881 }
882 a->session->UnlockMachine();
883 }
884 }
885
886 return SUCCEEDED(rc) ? 0 : 1;
887}
888
889int handleExtPack(HandlerArg *a)
890{
891 if (a->argc < 1)
892 return errorSyntax(USAGE_EXTPACK, "Incorrect number of parameters");
893
894 ComObjPtr<IExtPackManager> ptrExtPackMgr;
895 CHECK_ERROR2_RET(a->virtualBox, COMGETTER(ExtensionPackManager)(ptrExtPackMgr.asOutParam()), RTEXITCODE_FAILURE);
896
897 RTGETOPTSTATE GetState;
898 RTGETOPTUNION ValueUnion;
899 int ch;
900 HRESULT hrc = S_OK;
901
902 if (!strcmp(a->argv[0], "install"))
903 {
904 if (a->argc > 2)
905 return errorSyntax(USAGE_EXTPACK, "Too many parameters given to \"extpack install\"");
906
907 char szPath[RTPATH_MAX];
908 int vrc = RTPathAbs(a->argv[1], szPath, sizeof(szPath));
909 if (RT_FAILURE(vrc))
910 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPathAbs(%s,,) failed with rc=%Rrc", a->argv[1], vrc);
911
912 Bstr bstrTarball(szPath);
913 Bstr bstrName;
914 ComPtr<IExtPackFile> ptrExtPackFile;
915 CHECK_ERROR2_RET(ptrExtPackMgr, OpenExtPackFile(bstrTarball.raw(), ptrExtPackFile.asOutParam()), RTEXITCODE_FAILURE);
916 CHECK_ERROR2_RET(ptrExtPackFile, COMGETTER(Name)(bstrName.asOutParam()), RTEXITCODE_FAILURE);
917 CHECK_ERROR2_RET(ptrExtPackFile, Install(), RTEXITCODE_FAILURE);
918 RTPrintf("Successfully installed \"%lS\".\n", bstrName.raw());
919 }
920 else if (!strcmp(a->argv[0], "uninstall"))
921 {
922 const char *pszName = NULL;
923 bool fForced = false;
924
925 static const RTGETOPTDEF s_aUninstallOptions[] =
926 {
927 { "--forced", 'f', RTGETOPT_REQ_NOTHING },
928 };
929
930 RTGetOptInit(&GetState, a->argc, a->argv, s_aUninstallOptions, RT_ELEMENTS(s_aUninstallOptions),
931 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
932 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
933 {
934 switch (ch)
935 {
936 case 'f':
937 fForced = true;
938 break;
939
940 case VINF_GETOPT_NOT_OPTION:
941 if (pszName)
942 return errorSyntax(USAGE_EXTPACK, "Too many extension pack names given to \"extpack uninstall\"");
943 pszName = ValueUnion.psz;
944 break;
945
946 default:
947 return errorGetOpt(USAGE_EXTPACK, ch, &ValueUnion);
948 }
949 }
950 if (!pszName)
951 return errorSyntax(USAGE_EXTPACK, "Not extension pack name was given to \"extpack uninstall\"");
952
953 Bstr bstrName(pszName);
954 CHECK_ERROR2_RET(ptrExtPackMgr, Uninstall(bstrName.raw(), fForced), RTEXITCODE_FAILURE);
955 RTPrintf("Successfully uninstalled \"%s\".\n", pszName);
956 }
957 else if (!strcmp(a->argv[0], "cleanup"))
958 {
959 if (a->argc > 1)
960 return errorSyntax(USAGE_EXTPACK, "Too many parameters given to \"extpack cleanup\"");
961
962 CHECK_ERROR2_RET(ptrExtPackMgr, Cleanup(), RTEXITCODE_FAILURE);
963 RTPrintf("Successfully performed extension pack cleanup\n");
964 }
965 else
966 return errorSyntax(USAGE_EXTPACK, "Unknown command \"%s\"", a->argv[0]);
967
968 return RTEXITCODE_SUCCESS;
969}
970
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