1 | /* $Id: VBoxManageControlVM.cpp 31008 2010-07-22 15:24:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - VirtualBox's command-line interface.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 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 | #include <VBox/com/com.h>
|
---|
23 | #include <VBox/com/string.h>
|
---|
24 | #include <VBox/com/Guid.h>
|
---|
25 | #include <VBox/com/array.h>
|
---|
26 | #include <VBox/com/ErrorInfo.h>
|
---|
27 | #include <VBox/com/errorprint.h>
|
---|
28 | #include <VBox/com/EventQueue.h>
|
---|
29 |
|
---|
30 | #include <VBox/com/VirtualBox.h>
|
---|
31 |
|
---|
32 | #include <iprt/ctype.h>
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #include <iprt/getopt.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/uuid.h>
|
---|
38 | #include <VBox/log.h>
|
---|
39 |
|
---|
40 | #include "VBoxManage.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Parses a number.
|
---|
45 | *
|
---|
46 | * @returns Valid number on success.
|
---|
47 | * @returns 0 if invalid number. All necesary bitching has been done.
|
---|
48 | * @param psz Pointer to the nic number.
|
---|
49 | */
|
---|
50 | static unsigned parseNum(const char *psz, unsigned cMaxNum, const char *name)
|
---|
51 | {
|
---|
52 | uint32_t u32;
|
---|
53 | char *pszNext;
|
---|
54 | int rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u32);
|
---|
55 | if ( RT_SUCCESS(rc)
|
---|
56 | && *pszNext == '\0'
|
---|
57 | && u32 >= 1
|
---|
58 | && u32 <= cMaxNum)
|
---|
59 | return (unsigned)u32;
|
---|
60 | errorArgument("Invalid %s number '%s'", name, psz);
|
---|
61 | return 0;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | int handleControlVM(HandlerArg *a)
|
---|
66 | {
|
---|
67 | using namespace com;
|
---|
68 | HRESULT rc;
|
---|
69 |
|
---|
70 | if (a->argc < 2)
|
---|
71 | return errorSyntax(USAGE_CONTROLVM, "Not enough parameters");
|
---|
72 |
|
---|
73 | /* try to find the given machine */
|
---|
74 | ComPtr <IMachine> machine;
|
---|
75 | Bstr machineuuid(a->argv[0]);
|
---|
76 | if (!Guid(machineuuid).isEmpty())
|
---|
77 | {
|
---|
78 | CHECK_ERROR(a->virtualBox, GetMachine(machineuuid, machine.asOutParam()));
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | CHECK_ERROR(a->virtualBox, FindMachine(machineuuid, machine.asOutParam()));
|
---|
83 | if (SUCCEEDED (rc))
|
---|
84 | machine->COMGETTER(Id)(machineuuid.asOutParam());
|
---|
85 | }
|
---|
86 | if (FAILED (rc))
|
---|
87 | return 1;
|
---|
88 |
|
---|
89 | /* open a session for the VM */
|
---|
90 | CHECK_ERROR_RET(machine, LockForSession(a->session, true /* fPermitShared */, NULL), 1);
|
---|
91 |
|
---|
92 | do
|
---|
93 | {
|
---|
94 | /* get the associated console */
|
---|
95 | ComPtr<IConsole> console;
|
---|
96 | CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
|
---|
97 | /* ... and session machine */
|
---|
98 | ComPtr<IMachine> sessionMachine;
|
---|
99 | CHECK_ERROR_BREAK(a->session, COMGETTER(Machine)(sessionMachine.asOutParam()));
|
---|
100 |
|
---|
101 | /* which command? */
|
---|
102 | if (!strcmp(a->argv[1], "pause"))
|
---|
103 | {
|
---|
104 | CHECK_ERROR_BREAK(console, Pause());
|
---|
105 | }
|
---|
106 | else if (!strcmp(a->argv[1], "resume"))
|
---|
107 | {
|
---|
108 | CHECK_ERROR_BREAK(console, Resume());
|
---|
109 | }
|
---|
110 | else if (!strcmp(a->argv[1], "reset"))
|
---|
111 | {
|
---|
112 | CHECK_ERROR_BREAK(console, Reset());
|
---|
113 | }
|
---|
114 | else if (!strcmp(a->argv[1], "unplugcpu"))
|
---|
115 | {
|
---|
116 | if (a->argc <= 1 + 1)
|
---|
117 | {
|
---|
118 | errorArgument("Missing argument to '%s'. Expected CPU number.", a->argv[1]);
|
---|
119 | rc = E_FAIL;
|
---|
120 | break;
|
---|
121 | }
|
---|
122 |
|
---|
123 | unsigned n = parseNum(a->argv[2], 32, "CPU");
|
---|
124 |
|
---|
125 | CHECK_ERROR_BREAK(sessionMachine, HotUnplugCPU(n));
|
---|
126 | }
|
---|
127 | else if (!strcmp(a->argv[1], "plugcpu"))
|
---|
128 | {
|
---|
129 | if (a->argc <= 1 + 1)
|
---|
130 | {
|
---|
131 | errorArgument("Missing argument to '%s'. Expected CPU number.", a->argv[1]);
|
---|
132 | rc = E_FAIL;
|
---|
133 | break;
|
---|
134 | }
|
---|
135 |
|
---|
136 | unsigned n = parseNum(a->argv[2], 32, "CPU");
|
---|
137 |
|
---|
138 | CHECK_ERROR_BREAK(sessionMachine, HotPlugCPU(n));
|
---|
139 | }
|
---|
140 | else if (!strcmp(a->argv[1], "poweroff"))
|
---|
141 | {
|
---|
142 | ComPtr<IProgress> progress;
|
---|
143 | CHECK_ERROR_BREAK(console, PowerDown(progress.asOutParam()));
|
---|
144 |
|
---|
145 | rc = showProgress(progress);
|
---|
146 | if (FAILED(rc))
|
---|
147 | {
|
---|
148 | com::ProgressErrorInfo info(progress);
|
---|
149 | if (info.isBasicAvailable())
|
---|
150 | {
|
---|
151 | RTPrintf("Error: failed to power off machine. Error message: %lS\n", info.getText().raw());
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | RTPrintf("Error: failed to power off machine. No error message available!\n");
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | else if (!strcmp(a->argv[1], "savestate"))
|
---|
160 | {
|
---|
161 | /* first pause so we don't trigger a live save which needs more time/resources */
|
---|
162 | CHECK_ERROR_BREAK(console, Pause());
|
---|
163 |
|
---|
164 | ComPtr<IProgress> progress;
|
---|
165 | CHECK_ERROR(console, SaveState(progress.asOutParam()));
|
---|
166 | if (FAILED(rc))
|
---|
167 | {
|
---|
168 | console->Resume();
|
---|
169 | break;
|
---|
170 | }
|
---|
171 |
|
---|
172 | rc = showProgress(progress);
|
---|
173 | if (FAILED(rc))
|
---|
174 | {
|
---|
175 | com::ProgressErrorInfo info(progress);
|
---|
176 | if (info.isBasicAvailable())
|
---|
177 | {
|
---|
178 | RTPrintf("Error: failed to save machine state. Error message: %lS\n", info.getText().raw());
|
---|
179 | }
|
---|
180 | else
|
---|
181 | {
|
---|
182 | RTPrintf("Error: failed to save machine state. No error message available!\n");
|
---|
183 | }
|
---|
184 | console->Resume();
|
---|
185 | }
|
---|
186 | }
|
---|
187 | else if (!strcmp(a->argv[1], "acpipowerbutton"))
|
---|
188 | {
|
---|
189 | CHECK_ERROR_BREAK(console, PowerButton());
|
---|
190 | }
|
---|
191 | else if (!strcmp(a->argv[1], "acpisleepbutton"))
|
---|
192 | {
|
---|
193 | CHECK_ERROR_BREAK(console, SleepButton());
|
---|
194 | }
|
---|
195 | else if (!strcmp(a->argv[1], "injectnmi"))
|
---|
196 | {
|
---|
197 | /* get the machine debugger. */
|
---|
198 | ComPtr <IMachineDebugger> debugger;
|
---|
199 | CHECK_ERROR_BREAK(console, COMGETTER(Debugger)(debugger.asOutParam()));
|
---|
200 | CHECK_ERROR_BREAK(debugger, InjectNMI());
|
---|
201 | }
|
---|
202 | else if (!strcmp(a->argv[1], "keyboardputscancode"))
|
---|
203 | {
|
---|
204 | ComPtr<IKeyboard> keyboard;
|
---|
205 | CHECK_ERROR_BREAK(console, COMGETTER(Keyboard)(keyboard.asOutParam()));
|
---|
206 |
|
---|
207 | if (a->argc <= 1 + 1)
|
---|
208 | {
|
---|
209 | errorArgument("Missing argument to '%s'. Expected IBM PC AT set 2 keyboard scancode(s) as hex byte(s).", a->argv[1]);
|
---|
210 | rc = E_FAIL;
|
---|
211 | break;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* Arbitrary restrict the length of a sequence of scancodes to 1024. */
|
---|
215 | LONG alScancodes[1024];
|
---|
216 | int cScancodes = 0;
|
---|
217 |
|
---|
218 | /* Process the command line. */
|
---|
219 | int i;
|
---|
220 | for (i = 1 + 1; i < a->argc && cScancodes < (int)RT_ELEMENTS(alScancodes); i++, cScancodes++)
|
---|
221 | {
|
---|
222 | if ( RT_C_IS_XDIGIT (a->argv[i][0])
|
---|
223 | && RT_C_IS_XDIGIT (a->argv[i][1])
|
---|
224 | && a->argv[i][2] == 0)
|
---|
225 | {
|
---|
226 | uint8_t u8Scancode;
|
---|
227 | int irc = RTStrToUInt8Ex(a->argv[i], NULL, 16, &u8Scancode);
|
---|
228 | if (RT_FAILURE (irc))
|
---|
229 | {
|
---|
230 | RTPrintf("Error: converting '%s' returned %Rrc!\n", a->argv[i], rc);
|
---|
231 | rc = E_FAIL;
|
---|
232 | break;
|
---|
233 | }
|
---|
234 |
|
---|
235 | alScancodes[cScancodes] = u8Scancode;
|
---|
236 | }
|
---|
237 | else
|
---|
238 | {
|
---|
239 | RTPrintf("Error: '%s' is not a hex byte!\n", a->argv[i]);
|
---|
240 | rc = E_FAIL;
|
---|
241 | break;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (FAILED(rc))
|
---|
246 | break;
|
---|
247 |
|
---|
248 | if ( cScancodes == RT_ELEMENTS(alScancodes)
|
---|
249 | && i < a->argc)
|
---|
250 | {
|
---|
251 | RTPrintf("Error: too many scancodes, maximum %d allowed!\n", RT_ELEMENTS(alScancodes));
|
---|
252 | rc = E_FAIL;
|
---|
253 | break;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /* Send scancodes to the VM.
|
---|
257 | * Note: 'PutScancodes' did not work here. Only the first scancode was transmitted.
|
---|
258 | */
|
---|
259 | for (i = 0; i < cScancodes; i++)
|
---|
260 | {
|
---|
261 | CHECK_ERROR_BREAK(keyboard, PutScancode(alScancodes[i]));
|
---|
262 | RTPrintf("Scancode[%d]: 0x%02X\n", i, alScancodes[i]);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | else if (!strncmp(a->argv[1], "setlinkstate", 12))
|
---|
266 | {
|
---|
267 | /* Get the number of network adapters */
|
---|
268 | ULONG NetworkAdapterCount = 0;
|
---|
269 | ComPtr <ISystemProperties> info;
|
---|
270 | CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
|
---|
271 | CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
|
---|
272 |
|
---|
273 | unsigned n = parseNum(&a->argv[1][12], NetworkAdapterCount, "NIC");
|
---|
274 | if (!n)
|
---|
275 | {
|
---|
276 | rc = E_FAIL;
|
---|
277 | break;
|
---|
278 | }
|
---|
279 | if (a->argc <= 1 + 1)
|
---|
280 | {
|
---|
281 | errorArgument("Missing argument to '%s'", a->argv[1]);
|
---|
282 | rc = E_FAIL;
|
---|
283 | break;
|
---|
284 | }
|
---|
285 | /* get the corresponding network adapter */
|
---|
286 | ComPtr<INetworkAdapter> adapter;
|
---|
287 | CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
|
---|
288 | if (adapter)
|
---|
289 | {
|
---|
290 | if (!strcmp(a->argv[2], "on"))
|
---|
291 | {
|
---|
292 | CHECK_ERROR_BREAK(adapter, COMSETTER(CableConnected)(TRUE));
|
---|
293 | }
|
---|
294 | else if (!strcmp(a->argv[2], "off"))
|
---|
295 | {
|
---|
296 | CHECK_ERROR_BREAK(adapter, COMSETTER(CableConnected)(FALSE));
|
---|
297 | }
|
---|
298 | else
|
---|
299 | {
|
---|
300 | errorArgument("Invalid link state '%s'", Utf8Str(a->argv[2]).raw());
|
---|
301 | rc = E_FAIL;
|
---|
302 | break;
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|
306 | #ifdef VBOX_DYNAMIC_NET_ATTACH
|
---|
307 | /* here the order in which strncmp is called is important
|
---|
308 | * cause nictracefile can be very well compared with
|
---|
309 | * nictrace and nic and thus everything will always fail
|
---|
310 | * if the order is changed
|
---|
311 | */
|
---|
312 | else if (!strncmp(a->argv[1], "nictracefile", 12))
|
---|
313 | {
|
---|
314 | /* Get the number of network adapters */
|
---|
315 | ULONG NetworkAdapterCount = 0;
|
---|
316 | ComPtr <ISystemProperties> info;
|
---|
317 | CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
|
---|
318 | CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
|
---|
319 |
|
---|
320 | unsigned n = parseNum(&a->argv[1][12], NetworkAdapterCount, "NIC");
|
---|
321 | if (!n)
|
---|
322 | {
|
---|
323 | rc = E_FAIL;
|
---|
324 | break;
|
---|
325 | }
|
---|
326 | if (a->argc <= 2)
|
---|
327 | {
|
---|
328 | errorArgument("Missing argument to '%s'", a->argv[1]);
|
---|
329 | rc = E_FAIL;
|
---|
330 | break;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /* get the corresponding network adapter */
|
---|
334 | ComPtr<INetworkAdapter> adapter;
|
---|
335 | CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
|
---|
336 | if (adapter)
|
---|
337 | {
|
---|
338 | BOOL fEnabled;
|
---|
339 | adapter->COMGETTER(Enabled)(&fEnabled);
|
---|
340 | if (fEnabled)
|
---|
341 | {
|
---|
342 | if (a->argv[2])
|
---|
343 | {
|
---|
344 | CHECK_ERROR_RET(adapter, COMSETTER(TraceFile)(Bstr(a->argv[2])), 1);
|
---|
345 | }
|
---|
346 | else
|
---|
347 | {
|
---|
348 | errorArgument("Invalid filename or filename not specified for NIC %lu", n);
|
---|
349 | rc = E_FAIL;
|
---|
350 | break;
|
---|
351 | }
|
---|
352 | }
|
---|
353 | else
|
---|
354 | {
|
---|
355 | RTPrintf("The NIC %d is currently disabled and thus can't change its tracefile\n", n);
|
---|
356 | }
|
---|
357 | }
|
---|
358 | }
|
---|
359 | else if (!strncmp(a->argv[1], "nictrace", 8))
|
---|
360 | {
|
---|
361 | /* Get the number of network adapters */
|
---|
362 | ULONG NetworkAdapterCount = 0;
|
---|
363 | ComPtr <ISystemProperties> info;
|
---|
364 | CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
|
---|
365 | CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
|
---|
366 |
|
---|
367 | unsigned n = parseNum(&a->argv[1][8], NetworkAdapterCount, "NIC");
|
---|
368 | if (!n)
|
---|
369 | {
|
---|
370 | rc = E_FAIL;
|
---|
371 | break;
|
---|
372 | }
|
---|
373 | if (a->argc <= 2)
|
---|
374 | {
|
---|
375 | errorArgument("Missing argument to '%s'", a->argv[1]);
|
---|
376 | rc = E_FAIL;
|
---|
377 | break;
|
---|
378 | }
|
---|
379 |
|
---|
380 | /* get the corresponding network adapter */
|
---|
381 | ComPtr<INetworkAdapter> adapter;
|
---|
382 | CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
|
---|
383 | if (adapter)
|
---|
384 | {
|
---|
385 | BOOL fEnabled;
|
---|
386 | adapter->COMGETTER(Enabled)(&fEnabled);
|
---|
387 | if (fEnabled)
|
---|
388 | {
|
---|
389 | if (!strcmp(a->argv[2], "on"))
|
---|
390 | {
|
---|
391 | CHECK_ERROR_RET(adapter, COMSETTER(TraceEnabled)(TRUE), 1);
|
---|
392 | }
|
---|
393 | else if (!strcmp(a->argv[2], "off"))
|
---|
394 | {
|
---|
395 | CHECK_ERROR_RET(adapter, COMSETTER(TraceEnabled)(FALSE), 1);
|
---|
396 | }
|
---|
397 | else
|
---|
398 | {
|
---|
399 | errorArgument("Invalid nictrace%lu argument '%s'", n, Utf8Str(a->argv[2]).raw());
|
---|
400 | rc = E_FAIL;
|
---|
401 | break;
|
---|
402 | }
|
---|
403 | }
|
---|
404 | else
|
---|
405 | {
|
---|
406 | RTPrintf("The NIC %d is currently disabled and thus can't change its tracefile\n", n);
|
---|
407 | }
|
---|
408 | }
|
---|
409 | }
|
---|
410 | else if (!strncmp(a->argv[1], "nic", 3))
|
---|
411 | {
|
---|
412 | /* Get the number of network adapters */
|
---|
413 | ULONG NetworkAdapterCount = 0;
|
---|
414 | ComPtr <ISystemProperties> info;
|
---|
415 | CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
|
---|
416 | CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
|
---|
417 |
|
---|
418 | unsigned n = parseNum(&a->argv[1][3], NetworkAdapterCount, "NIC");
|
---|
419 | if (!n)
|
---|
420 | {
|
---|
421 | rc = E_FAIL;
|
---|
422 | break;
|
---|
423 | }
|
---|
424 | if (a->argc <= 2)
|
---|
425 | {
|
---|
426 | errorArgument("Missing argument to '%s'", a->argv[1]);
|
---|
427 | rc = E_FAIL;
|
---|
428 | break;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /* get the corresponding network adapter */
|
---|
432 | ComPtr<INetworkAdapter> adapter;
|
---|
433 | CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
|
---|
434 | if (adapter)
|
---|
435 | {
|
---|
436 | BOOL fEnabled;
|
---|
437 | adapter->COMGETTER(Enabled)(&fEnabled);
|
---|
438 | if (fEnabled)
|
---|
439 | {
|
---|
440 | if (!strcmp(a->argv[2], "null"))
|
---|
441 | {
|
---|
442 | CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
|
---|
443 | CHECK_ERROR_RET(adapter, Detach(), 1);
|
---|
444 | }
|
---|
445 | else if (!strcmp(a->argv[2], "nat"))
|
---|
446 | {
|
---|
447 | CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
|
---|
448 | if (a->argc == 4)
|
---|
449 | CHECK_ERROR_RET(adapter, COMSETTER(NATNetwork)(Bstr(a->argv[3])), 1);
|
---|
450 | CHECK_ERROR_RET(adapter, AttachToNAT(), 1);
|
---|
451 | }
|
---|
452 | else if ( !strcmp(a->argv[2], "bridged")
|
---|
453 | || !strcmp(a->argv[2], "hostif")) /* backward compatibility */
|
---|
454 | {
|
---|
455 | if (a->argc <= 3)
|
---|
456 | {
|
---|
457 | errorArgument("Missing argument to '%s'", a->argv[2]);
|
---|
458 | rc = E_FAIL;
|
---|
459 | break;
|
---|
460 | }
|
---|
461 | CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
|
---|
462 | CHECK_ERROR_RET(adapter, COMSETTER(HostInterface)(Bstr(a->argv[3])), 1);
|
---|
463 | CHECK_ERROR_RET(adapter, AttachToBridgedInterface(), 1);
|
---|
464 | }
|
---|
465 | else if (!strcmp(a->argv[2], "intnet"))
|
---|
466 | {
|
---|
467 | if (a->argc <= 3)
|
---|
468 | {
|
---|
469 | errorArgument("Missing argument to '%s'", a->argv[2]);
|
---|
470 | rc = E_FAIL;
|
---|
471 | break;
|
---|
472 | }
|
---|
473 | CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
|
---|
474 | CHECK_ERROR_RET(adapter, COMSETTER(InternalNetwork)(Bstr(a->argv[3])), 1);
|
---|
475 | CHECK_ERROR_RET(adapter, AttachToInternalNetwork(), 1);
|
---|
476 | }
|
---|
477 | #if defined(VBOX_WITH_NETFLT)
|
---|
478 | else if (!strcmp(a->argv[2], "hostonly"))
|
---|
479 | {
|
---|
480 | if (a->argc <= 3)
|
---|
481 | {
|
---|
482 | errorArgument("Missing argument to '%s'", a->argv[2]);
|
---|
483 | rc = E_FAIL;
|
---|
484 | break;
|
---|
485 | }
|
---|
486 | CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
|
---|
487 | CHECK_ERROR_RET(adapter, COMSETTER(HostInterface)(Bstr(a->argv[3])), 1);
|
---|
488 | CHECK_ERROR_RET(adapter, AttachToHostOnlyInterface(), 1);
|
---|
489 | }
|
---|
490 | #endif
|
---|
491 | else
|
---|
492 | {
|
---|
493 | errorArgument("Invalid type '%s' specfied for NIC %lu", Utf8Str(a->argv[2]).raw(), n);
|
---|
494 | rc = E_FAIL;
|
---|
495 | break;
|
---|
496 | }
|
---|
497 | }
|
---|
498 | else
|
---|
499 | {
|
---|
500 | RTPrintf("The NIC %d is currently disabled and thus can't change its attachment type\n", n);
|
---|
501 | }
|
---|
502 | }
|
---|
503 | }
|
---|
504 | #endif /* VBOX_DYNAMIC_NET_ATTACH */
|
---|
505 | #ifdef VBOX_WITH_VRDP
|
---|
506 | else if (!strcmp(a->argv[1], "vrdp"))
|
---|
507 | {
|
---|
508 | if (a->argc <= 1 + 1)
|
---|
509 | {
|
---|
510 | errorArgument("Missing argument to '%s'", a->argv[1]);
|
---|
511 | rc = E_FAIL;
|
---|
512 | break;
|
---|
513 | }
|
---|
514 | /* get the corresponding VRDP server */
|
---|
515 | ComPtr<IVRDPServer> vrdpServer;
|
---|
516 | sessionMachine->COMGETTER(VRDPServer)(vrdpServer.asOutParam());
|
---|
517 | ASSERT(vrdpServer);
|
---|
518 | if (vrdpServer)
|
---|
519 | {
|
---|
520 | if (!strcmp(a->argv[2], "on"))
|
---|
521 | {
|
---|
522 | CHECK_ERROR_BREAK(vrdpServer, COMSETTER(Enabled)(TRUE));
|
---|
523 | }
|
---|
524 | else if (!strcmp(a->argv[2], "off"))
|
---|
525 | {
|
---|
526 | CHECK_ERROR_BREAK(vrdpServer, COMSETTER(Enabled)(FALSE));
|
---|
527 | }
|
---|
528 | else
|
---|
529 | {
|
---|
530 | errorArgument("Invalid vrdp server state '%s'", Utf8Str(a->argv[2]).raw());
|
---|
531 | rc = E_FAIL;
|
---|
532 | break;
|
---|
533 | }
|
---|
534 | }
|
---|
535 | }
|
---|
536 | else if (!strcmp(a->argv[1], "vrdpport"))
|
---|
537 | {
|
---|
538 | if (a->argc <= 1 + 1)
|
---|
539 | {
|
---|
540 | errorArgument("Missing argument to '%s'", a->argv[1]);
|
---|
541 | rc = E_FAIL;
|
---|
542 | break;
|
---|
543 | }
|
---|
544 | /* get the corresponding VRDP server */
|
---|
545 | ComPtr<IVRDPServer> vrdpServer;
|
---|
546 | sessionMachine->COMGETTER(VRDPServer)(vrdpServer.asOutParam());
|
---|
547 | ASSERT(vrdpServer);
|
---|
548 | if (vrdpServer)
|
---|
549 | {
|
---|
550 | Bstr vrdpports;
|
---|
551 |
|
---|
552 | if (!strcmp(a->argv[2], "default"))
|
---|
553 | vrdpports = "0";
|
---|
554 | else
|
---|
555 | vrdpports = a->argv [2];
|
---|
556 |
|
---|
557 | CHECK_ERROR_BREAK(vrdpServer, COMSETTER(Ports)(vrdpports));
|
---|
558 | }
|
---|
559 | }
|
---|
560 | else if (!strcmp(a->argv[1], "vrdpvideochannelquality"))
|
---|
561 | {
|
---|
562 | if (a->argc <= 1 + 1)
|
---|
563 | {
|
---|
564 | errorArgument("Missing argument to '%s'", a->argv[1]);
|
---|
565 | rc = E_FAIL;
|
---|
566 | break;
|
---|
567 | }
|
---|
568 | /* get the corresponding VRDP server */
|
---|
569 | ComPtr<IVRDPServer> vrdpServer;
|
---|
570 | sessionMachine->COMGETTER(VRDPServer)(vrdpServer.asOutParam());
|
---|
571 | ASSERT(vrdpServer);
|
---|
572 | if (vrdpServer)
|
---|
573 | {
|
---|
574 | unsigned n = parseNum(a->argv[2], 100, "VRDP video channel quality in percent");
|
---|
575 |
|
---|
576 | CHECK_ERROR(vrdpServer, COMSETTER(VideoChannelQuality)(n));
|
---|
577 | }
|
---|
578 | }
|
---|
579 | #endif /* VBOX_WITH_VRDP */
|
---|
580 | else if ( !strcmp(a->argv[1], "usbattach")
|
---|
581 | || !strcmp(a->argv[1], "usbdetach"))
|
---|
582 | {
|
---|
583 | if (a->argc < 3)
|
---|
584 | {
|
---|
585 | errorSyntax(USAGE_CONTROLVM, "Not enough parameters");
|
---|
586 | rc = E_FAIL;
|
---|
587 | break;
|
---|
588 | }
|
---|
589 |
|
---|
590 | bool attach = !strcmp(a->argv[1], "usbattach");
|
---|
591 |
|
---|
592 | Bstr usbId = a->argv [2];
|
---|
593 | if (Guid(usbId).isEmpty())
|
---|
594 | {
|
---|
595 | // assume address
|
---|
596 | if (attach)
|
---|
597 | {
|
---|
598 | ComPtr <IHost> host;
|
---|
599 | CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
|
---|
600 | SafeIfaceArray <IHostUSBDevice> coll;
|
---|
601 | CHECK_ERROR_BREAK(host, COMGETTER(USBDevices)(ComSafeArrayAsOutParam(coll)));
|
---|
602 | ComPtr <IHostUSBDevice> dev;
|
---|
603 | CHECK_ERROR_BREAK(host, FindUSBDeviceByAddress(Bstr(a->argv [2]), dev.asOutParam()));
|
---|
604 | CHECK_ERROR_BREAK(dev, COMGETTER(Id)(usbId.asOutParam()));
|
---|
605 | }
|
---|
606 | else
|
---|
607 | {
|
---|
608 | SafeIfaceArray <IUSBDevice> coll;
|
---|
609 | CHECK_ERROR_BREAK(console, COMGETTER(USBDevices)(ComSafeArrayAsOutParam(coll)));
|
---|
610 | ComPtr <IUSBDevice> dev;
|
---|
611 | CHECK_ERROR_BREAK(console, FindUSBDeviceByAddress(Bstr(a->argv [2]),
|
---|
612 | dev.asOutParam()));
|
---|
613 | CHECK_ERROR_BREAK(dev, COMGETTER(Id)(usbId.asOutParam()));
|
---|
614 | }
|
---|
615 | }
|
---|
616 |
|
---|
617 | if (attach)
|
---|
618 | CHECK_ERROR_BREAK(console, AttachUSBDevice(usbId));
|
---|
619 | else
|
---|
620 | {
|
---|
621 | ComPtr <IUSBDevice> dev;
|
---|
622 | CHECK_ERROR_BREAK(console, DetachUSBDevice(usbId, dev.asOutParam()));
|
---|
623 | }
|
---|
624 | }
|
---|
625 | else if (!strcmp(a->argv[1], "setvideomodehint"))
|
---|
626 | {
|
---|
627 | if (a->argc != 5 && a->argc != 6)
|
---|
628 | {
|
---|
629 | errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
|
---|
630 | rc = E_FAIL;
|
---|
631 | break;
|
---|
632 | }
|
---|
633 | uint32_t xres = RTStrToUInt32(a->argv[2]);
|
---|
634 | uint32_t yres = RTStrToUInt32(a->argv[3]);
|
---|
635 | uint32_t bpp = RTStrToUInt32(a->argv[4]);
|
---|
636 | uint32_t displayIdx = 0;
|
---|
637 | if (a->argc == 6)
|
---|
638 | displayIdx = RTStrToUInt32(a->argv[5]);
|
---|
639 |
|
---|
640 | ComPtr<IDisplay> display;
|
---|
641 | CHECK_ERROR_BREAK(console, COMGETTER(Display)(display.asOutParam()));
|
---|
642 | CHECK_ERROR_BREAK(display, SetVideoModeHint(xres, yres, bpp, displayIdx));
|
---|
643 | }
|
---|
644 | else if (!strcmp(a->argv[1], "setcredentials"))
|
---|
645 | {
|
---|
646 | bool fAllowLocalLogon = true;
|
---|
647 | if (a->argc == 7)
|
---|
648 | {
|
---|
649 | if ( strcmp(a->argv[5], "--allowlocallogon")
|
---|
650 | && strcmp(a->argv[5], "-allowlocallogon"))
|
---|
651 | {
|
---|
652 | errorArgument("Invalid parameter '%s'", a->argv[5]);
|
---|
653 | rc = E_FAIL;
|
---|
654 | break;
|
---|
655 | }
|
---|
656 | if (!strcmp(a->argv[6], "no"))
|
---|
657 | fAllowLocalLogon = false;
|
---|
658 | }
|
---|
659 | else if (a->argc != 5)
|
---|
660 | {
|
---|
661 | errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
|
---|
662 | rc = E_FAIL;
|
---|
663 | break;
|
---|
664 | }
|
---|
665 |
|
---|
666 | ComPtr<IGuest> guest;
|
---|
667 | CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
|
---|
668 | CHECK_ERROR_BREAK(guest, SetCredentials(Bstr(a->argv[2]), Bstr(a->argv[3]), Bstr(a->argv[4]), fAllowLocalLogon));
|
---|
669 | }
|
---|
670 | #if 0 /* TODO: review & remove */
|
---|
671 | else if (!strcmp(a->argv[1], "dvdattach"))
|
---|
672 | {
|
---|
673 | Bstr uuid;
|
---|
674 | if (a->argc != 3)
|
---|
675 | {
|
---|
676 | errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
|
---|
677 | rc = E_FAIL;
|
---|
678 | break;
|
---|
679 | }
|
---|
680 |
|
---|
681 | ComPtr<IMedium> dvdMedium;
|
---|
682 |
|
---|
683 | /* unmount? */
|
---|
684 | if (!strcmp(a->argv[2], "none"))
|
---|
685 | {
|
---|
686 | /* nothing to do, NULL object will cause unmount */
|
---|
687 | }
|
---|
688 | /* host drive? */
|
---|
689 | else if (!strncmp(a->argv[2], "host:", 5))
|
---|
690 | {
|
---|
691 | ComPtr<IHost> host;
|
---|
692 | CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
|
---|
693 |
|
---|
694 | rc = host->FindHostDVDDrive(Bstr(a->argv[2] + 5), dvdMedium.asOutParam());
|
---|
695 | if (!dvdMedium)
|
---|
696 | {
|
---|
697 | errorArgument("Invalid host DVD drive name \"%s\"",
|
---|
698 | a->argv[2] + 5);
|
---|
699 | rc = E_FAIL;
|
---|
700 | break;
|
---|
701 | }
|
---|
702 | }
|
---|
703 | else
|
---|
704 | {
|
---|
705 | /* first assume it's a UUID */
|
---|
706 | uuid = a->argv[2];
|
---|
707 | rc = a->virtualBox->GetDVDImage(uuid, dvdMedium.asOutParam());
|
---|
708 | if (FAILED(rc) || !dvdMedium)
|
---|
709 | {
|
---|
710 | /* must be a filename, check if it's in the collection */
|
---|
711 | rc = a->virtualBox->FindDVDImage(Bstr(a->argv[2]), dvdMedium.asOutParam());
|
---|
712 | /* not registered, do that on the fly */
|
---|
713 | if (!dvdMedium)
|
---|
714 | {
|
---|
715 | Bstr emptyUUID;
|
---|
716 | CHECK_ERROR(a->virtualBox, OpenDVDImage(Bstr(a->argv[2]), emptyUUID, dvdMedium.asOutParam()));
|
---|
717 | }
|
---|
718 | }
|
---|
719 | if (!dvdMedium)
|
---|
720 | {
|
---|
721 | rc = E_FAIL;
|
---|
722 | break;
|
---|
723 | }
|
---|
724 | }
|
---|
725 |
|
---|
726 | /** @todo generalize this, allow arbitrary number of DVD drives
|
---|
727 | * and as a consequence multiple attachments and different
|
---|
728 | * storage controllers. */
|
---|
729 | if (dvdMedium)
|
---|
730 | dvdMedium->COMGETTER(Id)(uuid.asOutParam());
|
---|
731 | else
|
---|
732 | uuid = Guid().toString();
|
---|
733 | CHECK_ERROR(machine, MountMedium(Bstr("IDE Controller"), 1, 0, uuid, FALSE /* aForce */));
|
---|
734 | }
|
---|
735 | else if (!strcmp(a->argv[1], "floppyattach"))
|
---|
736 | {
|
---|
737 | Bstr uuid;
|
---|
738 | if (a->argc != 3)
|
---|
739 | {
|
---|
740 | errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
|
---|
741 | rc = E_FAIL;
|
---|
742 | break;
|
---|
743 | }
|
---|
744 |
|
---|
745 | ComPtr<IMedium> floppyMedium;
|
---|
746 |
|
---|
747 | /* unmount? */
|
---|
748 | if (!strcmp(a->argv[2], "none"))
|
---|
749 | {
|
---|
750 | /* nothing to do, NULL object will cause unmount */
|
---|
751 | }
|
---|
752 | /* host drive? */
|
---|
753 | else if (!strncmp(a->argv[2], "host:", 5))
|
---|
754 | {
|
---|
755 | ComPtr<IHost> host;
|
---|
756 | CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
|
---|
757 | host->FindHostFloppyDrive(Bstr(a->argv[2] + 5), floppyMedium.asOutParam());
|
---|
758 | if (!floppyMedium)
|
---|
759 | {
|
---|
760 | errorArgument("Invalid host floppy drive name \"%s\"",
|
---|
761 | a->argv[2] + 5);
|
---|
762 | rc = E_FAIL;
|
---|
763 | break;
|
---|
764 | }
|
---|
765 | }
|
---|
766 | else
|
---|
767 | {
|
---|
768 | /* first assume it's a UUID */
|
---|
769 | uuid = a->argv[2];
|
---|
770 | rc = a->virtualBox->GetFloppyImage(uuid, floppyMedium.asOutParam());
|
---|
771 | if (FAILED(rc) || !floppyMedium)
|
---|
772 | {
|
---|
773 | /* must be a filename, check if it's in the collection */
|
---|
774 | rc = a->virtualBox->FindFloppyImage(Bstr(a->argv[2]), floppyMedium.asOutParam());
|
---|
775 | /* not registered, do that on the fly */
|
---|
776 | if (!floppyMedium)
|
---|
777 | {
|
---|
778 | Bstr emptyUUID;
|
---|
779 | CHECK_ERROR(a->virtualBox, OpenFloppyImage(Bstr(a->argv[2]), emptyUUID, floppyMedium.asOutParam()));
|
---|
780 | }
|
---|
781 | }
|
---|
782 | if (!floppyMedium)
|
---|
783 | {
|
---|
784 | rc = E_FAIL;
|
---|
785 | break;
|
---|
786 | }
|
---|
787 | }
|
---|
788 | floppyMedium->COMGETTER(Id)(uuid.asOutParam());
|
---|
789 | CHECK_ERROR(machine, MountMedium(Bstr("Floppy Controller"), 0, 0, uuid, FALSE /* aForce */));
|
---|
790 | }
|
---|
791 | #endif /* obsolete dvdattach/floppyattach */
|
---|
792 | else if (!strcmp(a->argv[1], "guestmemoryballoon"))
|
---|
793 | {
|
---|
794 | if (a->argc != 3)
|
---|
795 | {
|
---|
796 | errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
|
---|
797 | rc = E_FAIL;
|
---|
798 | break;
|
---|
799 | }
|
---|
800 | uint32_t uVal;
|
---|
801 | int vrc;
|
---|
802 | vrc = RTStrToUInt32Ex(a->argv[2], NULL, 0, &uVal);
|
---|
803 | if (vrc != VINF_SUCCESS)
|
---|
804 | {
|
---|
805 | errorArgument("Error parsing guest memory balloon size '%s'", a->argv[2]);
|
---|
806 | rc = E_FAIL;
|
---|
807 | break;
|
---|
808 | }
|
---|
809 | /* guest is running; update IGuest */
|
---|
810 | ComPtr <IGuest> guest;
|
---|
811 | rc = console->COMGETTER(Guest)(guest.asOutParam());
|
---|
812 | if (SUCCEEDED(rc))
|
---|
813 | CHECK_ERROR(guest, COMSETTER(MemoryBalloonSize)(uVal));
|
---|
814 | }
|
---|
815 | else if (!strcmp(a->argv[1], "teleport"))
|
---|
816 | {
|
---|
817 | Bstr bstrHostname;
|
---|
818 | uint32_t uMaxDowntime = 250 /*ms*/;
|
---|
819 | uint32_t uPort = UINT32_MAX;
|
---|
820 | uint32_t cMsTimeout = 0;
|
---|
821 | Bstr bstrPassword("");
|
---|
822 | static const RTGETOPTDEF s_aTeleportOptions[] =
|
---|
823 | {
|
---|
824 | { "--host", 'h', RTGETOPT_REQ_STRING }, /** @todo RTGETOPT_FLAG_MANDATORY */
|
---|
825 | { "--hostname", 'h', RTGETOPT_REQ_STRING }, /** @todo remove this */
|
---|
826 | { "--maxdowntime", 'd', RTGETOPT_REQ_UINT32 },
|
---|
827 | { "--port", 'p', RTGETOPT_REQ_UINT32 }, /** @todo RTGETOPT_FLAG_MANDATORY */
|
---|
828 | { "--password", 'P', RTGETOPT_REQ_STRING },
|
---|
829 | { "--timeout", 't', RTGETOPT_REQ_UINT32 },
|
---|
830 | { "--detailed-progress", 'D', RTGETOPT_REQ_NOTHING }
|
---|
831 | };
|
---|
832 | RTGETOPTSTATE GetOptState;
|
---|
833 | RTGetOptInit(&GetOptState, a->argc, a->argv, s_aTeleportOptions, RT_ELEMENTS(s_aTeleportOptions), 2, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
834 | int ch;
|
---|
835 | RTGETOPTUNION Value;
|
---|
836 | while ( SUCCEEDED(rc)
|
---|
837 | && (ch = RTGetOpt(&GetOptState, &Value)))
|
---|
838 | {
|
---|
839 | switch (ch)
|
---|
840 | {
|
---|
841 | case 'h': bstrHostname = Value.psz; break;
|
---|
842 | case 'd': uMaxDowntime = Value.u32; break;
|
---|
843 | case 'D': g_fDetailedProgress = true; break;
|
---|
844 | case 'p': uPort = Value.u32; break;
|
---|
845 | case 'P': bstrPassword = Value.psz; break;
|
---|
846 | case 't': cMsTimeout = Value.u32; break;
|
---|
847 | default:
|
---|
848 | errorGetOpt(USAGE_CONTROLVM, ch, &Value);
|
---|
849 | rc = E_FAIL;
|
---|
850 | break;
|
---|
851 | }
|
---|
852 | }
|
---|
853 | if (FAILED(rc))
|
---|
854 | break;
|
---|
855 |
|
---|
856 | ComPtr<IProgress> progress;
|
---|
857 | CHECK_ERROR_BREAK(console, Teleport(bstrHostname, uPort, bstrPassword, uMaxDowntime, progress.asOutParam()));
|
---|
858 |
|
---|
859 | if (cMsTimeout)
|
---|
860 | {
|
---|
861 | rc = progress->COMSETTER(Timeout)(cMsTimeout);
|
---|
862 | if (FAILED(rc) && rc != VBOX_E_INVALID_OBJECT_STATE)
|
---|
863 | CHECK_ERROR_BREAK(progress, COMSETTER(Timeout)(cMsTimeout)); /* lazyness */
|
---|
864 | }
|
---|
865 |
|
---|
866 | rc = showProgress(progress);
|
---|
867 | if (FAILED(rc))
|
---|
868 | {
|
---|
869 | com::ProgressErrorInfo info(progress);
|
---|
870 | if (info.isBasicAvailable())
|
---|
871 | RTPrintf("Error: teleportation failed. Error message: %lS\n", info.getText().raw());
|
---|
872 | else
|
---|
873 | RTPrintf("Error: teleportation failed. No error message available!\n");
|
---|
874 | }
|
---|
875 | }
|
---|
876 | else
|
---|
877 | {
|
---|
878 | errorSyntax(USAGE_CONTROLVM, "Invalid parameter '%s'", Utf8Str(a->argv[1]).raw());
|
---|
879 | rc = E_FAIL;
|
---|
880 | }
|
---|
881 | } while (0);
|
---|
882 |
|
---|
883 | a->session->Close();
|
---|
884 |
|
---|
885 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
886 | }
|
---|
887 |
|
---|