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