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