VirtualBox

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

Last change on this file since 35951 was 35828, checked in by vboxsync, 14 years ago

this looks better

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