1 | /* $Id: VBoxManageMisc.cpp 33556 2010-10-28 13:16:42Z 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 |
|
---|
57 | using namespace com;
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | int 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 |
|
---|
97 | static const RTGETOPTDEF g_aUnregisterVMOptions[] =
|
---|
98 | {
|
---|
99 | { "--delete", 'd', RTGETOPT_REQ_NOTHING },
|
---|
100 | { "-delete", 'd', RTGETOPT_REQ_NOTHING }, // deprecated
|
---|
101 | };
|
---|
102 |
|
---|
103 | int 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 |
|
---|
175 | int 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 |
|
---|
269 | int 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 |
|
---|
391 | int 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 |
|
---|
420 | int 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 |
|
---|
449 | int 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 |
|
---|
528 | int 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 |
|
---|
569 | int 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], "vrdelibrary"))
|
---|
609 | {
|
---|
610 | /* disable? */
|
---|
611 | if (!strcmp(a->argv[1], "null"))
|
---|
612 | CHECK_ERROR(systemProperties, COMSETTER(DefaultVRDELibrary)(NULL));
|
---|
613 | else
|
---|
614 | CHECK_ERROR(systemProperties, COMSETTER(DefaultVRDELibrary)(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 |
|
---|
631 | int 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 | SessionType_T st;
|
---|
724 | CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Write), 1);
|
---|
725 |
|
---|
726 | /* get the mutable session machine */
|
---|
727 | a->session->COMGETTER(Machine)(machine.asOutParam());
|
---|
728 |
|
---|
729 | CHECK_ERROR(machine, CreateSharedFolder(Bstr(name).raw(),
|
---|
730 | Bstr(hostpath).raw(),
|
---|
731 | fWritable, fAutoMount));
|
---|
732 | if (SUCCEEDED(rc))
|
---|
733 | CHECK_ERROR(machine, SaveSettings());
|
---|
734 |
|
---|
735 | a->session->UnlockMachine();
|
---|
736 | }
|
---|
737 | }
|
---|
738 | else if (!strcmp(a->argv[0], "remove"))
|
---|
739 | {
|
---|
740 | /* we need at least two more parameters */
|
---|
741 | if (a->argc < 3)
|
---|
742 | return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Not enough parameters");
|
---|
743 |
|
---|
744 | char *name = NULL;
|
---|
745 | bool fTransient = false;
|
---|
746 |
|
---|
747 | for (int i = 2; i < a->argc; i++)
|
---|
748 | {
|
---|
749 | if ( !strcmp(a->argv[i], "--name")
|
---|
750 | || !strcmp(a->argv[i], "-name"))
|
---|
751 | {
|
---|
752 | if (a->argc <= i + 1 || !*a->argv[i+1])
|
---|
753 | return errorArgument("Missing argument to '%s'", a->argv[i]);
|
---|
754 | i++;
|
---|
755 | name = a->argv[i];
|
---|
756 | }
|
---|
757 | else if ( !strcmp(a->argv[i], "--transient")
|
---|
758 | || !strcmp(a->argv[i], "-transient"))
|
---|
759 | {
|
---|
760 | fTransient = true;
|
---|
761 | }
|
---|
762 | else
|
---|
763 | return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
|
---|
764 | }
|
---|
765 |
|
---|
766 | /* required arguments */
|
---|
767 | if (!name)
|
---|
768 | return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Parameter --name is required");
|
---|
769 |
|
---|
770 | if (fTransient)
|
---|
771 | {
|
---|
772 | ComPtr <IConsole> console;
|
---|
773 |
|
---|
774 | /* open an existing session for the VM */
|
---|
775 | CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
|
---|
776 | /* get the session machine */
|
---|
777 | CHECK_ERROR_RET(a->session, COMGETTER(Machine)(machine.asOutParam()), 1);
|
---|
778 | /* get the session console */
|
---|
779 | CHECK_ERROR_RET(a->session, COMGETTER(Console)(console.asOutParam()), 1);
|
---|
780 |
|
---|
781 | CHECK_ERROR(console, RemoveSharedFolder(Bstr(name).raw()));
|
---|
782 |
|
---|
783 | if (console)
|
---|
784 | a->session->UnlockMachine();
|
---|
785 | }
|
---|
786 | else
|
---|
787 | {
|
---|
788 | /* open a session for the VM */
|
---|
789 | CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Write), 1);
|
---|
790 |
|
---|
791 | /* get the mutable session machine */
|
---|
792 | a->session->COMGETTER(Machine)(machine.asOutParam());
|
---|
793 |
|
---|
794 | CHECK_ERROR(machine, RemoveSharedFolder(Bstr(name).raw()));
|
---|
795 |
|
---|
796 | /* commit and close the session */
|
---|
797 | CHECK_ERROR(machine, SaveSettings());
|
---|
798 | a->session->UnlockMachine();
|
---|
799 | }
|
---|
800 | }
|
---|
801 | else
|
---|
802 | return errorSyntax(USAGE_SETPROPERTY, "Invalid parameter '%s'", Utf8Str(a->argv[0]).c_str());
|
---|
803 |
|
---|
804 | return 0;
|
---|
805 | }
|
---|
806 |
|
---|
807 | int handleVMStatistics(HandlerArg *a)
|
---|
808 | {
|
---|
809 | HRESULT rc;
|
---|
810 |
|
---|
811 | /* at least one option: the UUID or name of the VM */
|
---|
812 | if (a->argc < 1)
|
---|
813 | return errorSyntax(USAGE_VM_STATISTICS, "Incorrect number of parameters");
|
---|
814 |
|
---|
815 | /* try to find the given machine */
|
---|
816 | ComPtr<IMachine> machine;
|
---|
817 | CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
|
---|
818 | machine.asOutParam()));
|
---|
819 | if (FAILED(rc))
|
---|
820 | return 1;
|
---|
821 |
|
---|
822 | /* parse arguments. */
|
---|
823 | bool fReset = false;
|
---|
824 | bool fWithDescriptions = false;
|
---|
825 | const char *pszPattern = NULL; /* all */
|
---|
826 | for (int i = 1; i < a->argc; i++)
|
---|
827 | {
|
---|
828 | if ( !strcmp(a->argv[i], "--pattern")
|
---|
829 | || !strcmp(a->argv[i], "-pattern"))
|
---|
830 | {
|
---|
831 | if (pszPattern)
|
---|
832 | return errorSyntax(USAGE_VM_STATISTICS, "Multiple --patterns options is not permitted");
|
---|
833 | if (i + 1 >= a->argc)
|
---|
834 | return errorArgument("Missing argument to '%s'", a->argv[i]);
|
---|
835 | pszPattern = a->argv[++i];
|
---|
836 | }
|
---|
837 | else if ( !strcmp(a->argv[i], "--descriptions")
|
---|
838 | || !strcmp(a->argv[i], "-descriptions"))
|
---|
839 | fWithDescriptions = true;
|
---|
840 | /* add: --file <filename> and --formatted */
|
---|
841 | else if ( !strcmp(a->argv[i], "--reset")
|
---|
842 | || !strcmp(a->argv[i], "-reset"))
|
---|
843 | fReset = true;
|
---|
844 | else
|
---|
845 | return errorSyntax(USAGE_VM_STATISTICS, "Unknown option '%s'", a->argv[i]);
|
---|
846 | }
|
---|
847 | if (fReset && fWithDescriptions)
|
---|
848 | return errorSyntax(USAGE_VM_STATISTICS, "The --reset and --descriptions options does not mix");
|
---|
849 |
|
---|
850 |
|
---|
851 | /* open an existing session for the VM. */
|
---|
852 | CHECK_ERROR(machine, LockMachine(a->session, LockType_Shared));
|
---|
853 | if (SUCCEEDED(rc))
|
---|
854 | {
|
---|
855 | /* get the session console. */
|
---|
856 | ComPtr <IConsole> console;
|
---|
857 | CHECK_ERROR(a->session, COMGETTER(Console)(console.asOutParam()));
|
---|
858 | if (SUCCEEDED(rc))
|
---|
859 | {
|
---|
860 | /* get the machine debugger. */
|
---|
861 | ComPtr <IMachineDebugger> debugger;
|
---|
862 | CHECK_ERROR(console, COMGETTER(Debugger)(debugger.asOutParam()));
|
---|
863 | if (SUCCEEDED(rc))
|
---|
864 | {
|
---|
865 | if (fReset)
|
---|
866 | CHECK_ERROR(debugger, ResetStats(Bstr(pszPattern).raw()));
|
---|
867 | else
|
---|
868 | {
|
---|
869 | Bstr stats;
|
---|
870 | CHECK_ERROR(debugger, GetStats(Bstr(pszPattern).raw(),
|
---|
871 | fWithDescriptions,
|
---|
872 | stats.asOutParam()));
|
---|
873 | if (SUCCEEDED(rc))
|
---|
874 | {
|
---|
875 | /* if (fFormatted)
|
---|
876 | { big mess }
|
---|
877 | else
|
---|
878 | */
|
---|
879 | RTPrintf("%ls\n", stats.raw());
|
---|
880 | }
|
---|
881 | }
|
---|
882 | }
|
---|
883 | a->session->UnlockMachine();
|
---|
884 | }
|
---|
885 | }
|
---|
886 |
|
---|
887 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
888 | }
|
---|
889 |
|
---|
890 | int handleVRDE(HandlerArg *a)
|
---|
891 | {
|
---|
892 | HRESULT rc = S_OK;
|
---|
893 |
|
---|
894 | if (a->argc != 2)
|
---|
895 | return errorSyntax(USAGE_VRDE, "Incorrect number of parameters");
|
---|
896 |
|
---|
897 | if (!strcmp(a->argv[0], "register"))
|
---|
898 | {
|
---|
899 | Bstr name = a->argv[1];
|
---|
900 | CHECK_ERROR(a->virtualBox, VRDERegisterLibrary(name.raw()));
|
---|
901 | }
|
---|
902 | else if (!strcmp(a->argv[0], "unregister"))
|
---|
903 | {
|
---|
904 | Bstr name = a->argv[1];
|
---|
905 | CHECK_ERROR(a->virtualBox, VRDEUnregisterLibrary(name.raw()));
|
---|
906 | }
|
---|
907 | else
|
---|
908 | return errorSyntax(USAGE_VRDE, "Invalid parameter");
|
---|
909 |
|
---|
910 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
911 | }
|
---|
912 |
|
---|