VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageControlVM.cpp@ 33386

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

VRDE: API changes for the VRDP server separation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.5 KB
Line 
1/* $Id: VBoxManageControlVM.cpp 33386 2010-10-24 15:57:55Z 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 necesary bitching has been done.
50 * @param psz Pointer to the nic number.
51 */
52static 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
67int 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#ifdef VBOX_DYNAMIC_NET_ATTACH
298 /* here the order in which strncmp is called is important
299 * cause nictracefile can be very well compared with
300 * nictrace and nic and thus everything will always fail
301 * if the order is changed
302 */
303 else if (!strncmp(a->argv[1], "nictracefile", 12))
304 {
305 /* Get the number of network adapters */
306 ULONG NetworkAdapterCount = 0;
307 ComPtr <ISystemProperties> info;
308 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
309 CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
310
311 unsigned n = parseNum(&a->argv[1][12], NetworkAdapterCount, "NIC");
312 if (!n)
313 {
314 rc = E_FAIL;
315 break;
316 }
317 if (a->argc <= 2)
318 {
319 errorArgument("Missing argument to '%s'", a->argv[1]);
320 rc = E_FAIL;
321 break;
322 }
323
324 /* get the corresponding network adapter */
325 ComPtr<INetworkAdapter> adapter;
326 CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
327 if (adapter)
328 {
329 BOOL fEnabled;
330 adapter->COMGETTER(Enabled)(&fEnabled);
331 if (fEnabled)
332 {
333 if (a->argv[2])
334 {
335 CHECK_ERROR_RET(adapter, COMSETTER(TraceFile)(Bstr(a->argv[2]).raw()), 1);
336 }
337 else
338 {
339 errorArgument("Invalid filename or filename not specified for NIC %lu", n);
340 rc = E_FAIL;
341 break;
342 }
343 }
344 else
345 RTMsgError("The NIC %d is currently disabled and thus can't change its tracefile", n);
346 }
347 }
348 else if (!strncmp(a->argv[1], "nictrace", 8))
349 {
350 /* Get the number of network adapters */
351 ULONG NetworkAdapterCount = 0;
352 ComPtr <ISystemProperties> info;
353 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
354 CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
355
356 unsigned n = parseNum(&a->argv[1][8], NetworkAdapterCount, "NIC");
357 if (!n)
358 {
359 rc = E_FAIL;
360 break;
361 }
362 if (a->argc <= 2)
363 {
364 errorArgument("Missing argument to '%s'", a->argv[1]);
365 rc = E_FAIL;
366 break;
367 }
368
369 /* get the corresponding network adapter */
370 ComPtr<INetworkAdapter> adapter;
371 CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
372 if (adapter)
373 {
374 BOOL fEnabled;
375 adapter->COMGETTER(Enabled)(&fEnabled);
376 if (fEnabled)
377 {
378 if (!strcmp(a->argv[2], "on"))
379 {
380 CHECK_ERROR_RET(adapter, COMSETTER(TraceEnabled)(TRUE), 1);
381 }
382 else if (!strcmp(a->argv[2], "off"))
383 {
384 CHECK_ERROR_RET(adapter, COMSETTER(TraceEnabled)(FALSE), 1);
385 }
386 else
387 {
388 errorArgument("Invalid nictrace%lu argument '%s'", n, Utf8Str(a->argv[2]).c_str());
389 rc = E_FAIL;
390 break;
391 }
392 }
393 else
394 RTMsgError("The NIC %d is currently disabled and thus can't change its trace flag", n);
395 }
396 }
397 else if (!strncmp(a->argv[1], "nic", 3))
398 {
399 /* Get the number of network adapters */
400 ULONG NetworkAdapterCount = 0;
401 ComPtr <ISystemProperties> info;
402 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
403 CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
404
405 unsigned n = parseNum(&a->argv[1][3], NetworkAdapterCount, "NIC");
406 if (!n)
407 {
408 rc = E_FAIL;
409 break;
410 }
411 if (a->argc <= 2)
412 {
413 errorArgument("Missing argument to '%s'", a->argv[1]);
414 rc = E_FAIL;
415 break;
416 }
417
418 /* get the corresponding network adapter */
419 ComPtr<INetworkAdapter> adapter;
420 CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
421 if (adapter)
422 {
423 BOOL fEnabled;
424 adapter->COMGETTER(Enabled)(&fEnabled);
425 if (fEnabled)
426 {
427 if (!strcmp(a->argv[2], "null"))
428 {
429 CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
430 CHECK_ERROR_RET(adapter, Detach(), 1);
431 }
432 else if (!strcmp(a->argv[2], "nat"))
433 {
434 CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
435 if (a->argc == 4)
436 CHECK_ERROR_RET(adapter, COMSETTER(NATNetwork)(Bstr(a->argv[3]).raw()), 1);
437 CHECK_ERROR_RET(adapter, AttachToNAT(), 1);
438 }
439 else if ( !strcmp(a->argv[2], "bridged")
440 || !strcmp(a->argv[2], "hostif")) /* backward compatibility */
441 {
442 if (a->argc <= 3)
443 {
444 errorArgument("Missing argument to '%s'", a->argv[2]);
445 rc = E_FAIL;
446 break;
447 }
448 CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
449 CHECK_ERROR_RET(adapter, COMSETTER(HostInterface)(Bstr(a->argv[3]).raw()), 1);
450 CHECK_ERROR_RET(adapter, AttachToBridgedInterface(), 1);
451 }
452 else if (!strcmp(a->argv[2], "intnet"))
453 {
454 if (a->argc <= 3)
455 {
456 errorArgument("Missing argument to '%s'", a->argv[2]);
457 rc = E_FAIL;
458 break;
459 }
460 CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
461 CHECK_ERROR_RET(adapter, COMSETTER(InternalNetwork)(Bstr(a->argv[3]).raw()), 1);
462 CHECK_ERROR_RET(adapter, AttachToInternalNetwork(), 1);
463 }
464#if defined(VBOX_WITH_NETFLT)
465 else if (!strcmp(a->argv[2], "hostonly"))
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(HostInterface)(Bstr(a->argv[3]).raw()), 1);
475 CHECK_ERROR_RET(adapter, AttachToHostOnlyInterface(), 1);
476 }
477#endif
478 else
479 {
480 errorArgument("Invalid type '%s' specfied for NIC %lu", Utf8Str(a->argv[2]).c_str(), n);
481 rc = E_FAIL;
482 break;
483 }
484 }
485 else
486 RTMsgError("The NIC %d is currently disabled and thus can't change its attachment type", n);
487 }
488 }
489#endif /* VBOX_DYNAMIC_NET_ATTACH */
490#ifdef VBOX_WITH_VRDP
491 else if ( !strcmp(a->argv[1], "vrde")
492 || !strcmp(a->argv[1], "vrdp"))
493 {
494 if (!strcmp(a->argv[1], "vrdp"))
495 RTStrmPrintf(g_pStdErr, "Warning: 'vrdp' is deprecated. Use 'vrde'.\n");
496
497 if (a->argc <= 1 + 1)
498 {
499 errorArgument("Missing argument to '%s'", a->argv[1]);
500 rc = E_FAIL;
501 break;
502 }
503 ComPtr<IVRDEServer> vrdeServer;
504 sessionMachine->COMGETTER(VRDEServer)(vrdeServer.asOutParam());
505 ASSERT(vrdeServer);
506 if (vrdeServer)
507 {
508 if (!strcmp(a->argv[2], "on"))
509 {
510 CHECK_ERROR_BREAK(vrdeServer, COMSETTER(Enabled)(TRUE));
511 }
512 else if (!strcmp(a->argv[2], "off"))
513 {
514 CHECK_ERROR_BREAK(vrdeServer, COMSETTER(Enabled)(FALSE));
515 }
516 else
517 {
518 errorArgument("Invalid remote desktop server state '%s'", Utf8Str(a->argv[2]).c_str());
519 rc = E_FAIL;
520 break;
521 }
522 }
523 }
524 else if (!strcmp(a->argv[1], "vrdpport"))
525 {
526 RTStrmPrintf(g_pStdErr, "Warning: 'vrdpport' is deprecated. Use 'setvrdeproperty'.\n");
527
528 if (a->argc <= 1 + 1)
529 {
530 errorArgument("Missing argument to '%s'", a->argv[1]);
531 rc = E_FAIL;
532 break;
533 }
534 ComPtr<IVRDEServer> vrdeServer;
535 sessionMachine->COMGETTER(VRDEServer)(vrdeServer.asOutParam());
536 ASSERT(vrdeServer);
537 if (vrdeServer)
538 {
539 Bstr ports;
540
541 if (!strcmp(a->argv[2], "default"))
542 ports = "0";
543 else
544 ports = a->argv[2];
545
546 CHECK_ERROR_BREAK(vrdeServer, SetVRDEProperty(Bstr("TCP/Ports").raw(), ports.raw()));
547 }
548 }
549 else if ( !strcmp(a->argv[1], "vrdevideochannelquality")
550 || !strcmp(a->argv[1], "vrdpvideochannelquality"))
551 {
552 if (!strcmp(a->argv[1], "vrdpvideochannelquality"))
553 RTStrmPrintf(g_pStdErr, "Warning: 'vrdpvideochannelquality' is deprecated. Use 'vrdevideochannelquality'.\n");
554
555 if (a->argc <= 1 + 1)
556 {
557 errorArgument("Missing argument to '%s'", a->argv[1]);
558 rc = E_FAIL;
559 break;
560 }
561 ComPtr<IVRDEServer> vrdeServer;
562 sessionMachine->COMGETTER(VRDEServer)(vrdeServer.asOutParam());
563 ASSERT(vrdeServer);
564 if (vrdeServer)
565 {
566 unsigned n = parseNum(a->argv[2], 100, "VRDE video redirection quality in percent");
567
568 CHECK_ERROR(vrdeServer, COMSETTER(VideoChannelQuality)(n));
569 }
570 }
571 else if (!strcmp(a->argv[1], "vrdesetproperty"))
572 {
573 if (a->argc <= 1 + 1)
574 {
575 errorArgument("Missing argument to '%s'", a->argv[1]);
576 rc = E_FAIL;
577 break;
578 }
579 ComPtr<IVRDEServer> vrdeServer;
580 sessionMachine->COMGETTER(VRDEServer)(vrdeServer.asOutParam());
581 ASSERT(vrdeServer);
582 if (vrdeServer)
583 {
584 /* Parse 'name=value' */
585 char *pszProperty = RTStrDup(a->argv[2]);
586 if (pszProperty)
587 {
588 char *pDelimiter = strchr(pszProperty, '=');
589 if (pDelimiter)
590 {
591 *pDelimiter = '\0';
592
593 Bstr bstrName = pszProperty;
594 Bstr bstrValue = &pDelimiter[1];
595 CHECK_ERROR(vrdeServer, SetVRDEProperty(bstrName.raw(), bstrValue.raw()));
596 }
597 else
598 {
599 errorArgument("Invalid --vrdesetproperty argument '%s'", a->argv[2]);
600 rc = E_FAIL;
601 break;
602 }
603 RTStrFree(pszProperty);
604 }
605 else
606 {
607 RTStrmPrintf(g_pStdErr, "Error: Failed to allocate memory for VRDE property '%s'\n", a->argv[2]);
608 rc = E_FAIL;
609 }
610 }
611 if (FAILED(rc))
612 {
613 break;
614 }
615 }
616#endif /* VBOX_WITH_VRDP */
617 else if ( !strcmp(a->argv[1], "usbattach")
618 || !strcmp(a->argv[1], "usbdetach"))
619 {
620 if (a->argc < 3)
621 {
622 errorSyntax(USAGE_CONTROLVM, "Not enough parameters");
623 rc = E_FAIL;
624 break;
625 }
626
627 bool attach = !strcmp(a->argv[1], "usbattach");
628
629 Bstr usbId = a->argv[2];
630 if (Guid(usbId).isEmpty())
631 {
632 // assume address
633 if (attach)
634 {
635 ComPtr <IHost> host;
636 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
637 SafeIfaceArray <IHostUSBDevice> coll;
638 CHECK_ERROR_BREAK(host, COMGETTER(USBDevices)(ComSafeArrayAsOutParam(coll)));
639 ComPtr <IHostUSBDevice> dev;
640 CHECK_ERROR_BREAK(host, FindUSBDeviceByAddress(Bstr(a->argv[2]).raw(),
641 dev.asOutParam()));
642 CHECK_ERROR_BREAK(dev, COMGETTER(Id)(usbId.asOutParam()));
643 }
644 else
645 {
646 SafeIfaceArray <IUSBDevice> coll;
647 CHECK_ERROR_BREAK(console, COMGETTER(USBDevices)(ComSafeArrayAsOutParam(coll)));
648 ComPtr <IUSBDevice> dev;
649 CHECK_ERROR_BREAK(console, FindUSBDeviceByAddress(Bstr(a->argv[2]).raw(),
650 dev.asOutParam()));
651 CHECK_ERROR_BREAK(dev, COMGETTER(Id)(usbId.asOutParam()));
652 }
653 }
654
655 if (attach)
656 CHECK_ERROR_BREAK(console, AttachUSBDevice(usbId.raw()));
657 else
658 {
659 ComPtr <IUSBDevice> dev;
660 CHECK_ERROR_BREAK(console, DetachUSBDevice(usbId.raw(),
661 dev.asOutParam()));
662 }
663 }
664 else if (!strcmp(a->argv[1], "setvideomodehint"))
665 {
666 if (a->argc != 5 && a->argc != 6)
667 {
668 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
669 rc = E_FAIL;
670 break;
671 }
672 uint32_t xres = RTStrToUInt32(a->argv[2]);
673 uint32_t yres = RTStrToUInt32(a->argv[3]);
674 uint32_t bpp = RTStrToUInt32(a->argv[4]);
675 uint32_t displayIdx = 0;
676 if (a->argc == 6)
677 displayIdx = RTStrToUInt32(a->argv[5]);
678
679 ComPtr<IDisplay> display;
680 CHECK_ERROR_BREAK(console, COMGETTER(Display)(display.asOutParam()));
681 CHECK_ERROR_BREAK(display, SetVideoModeHint(xres, yres, bpp, displayIdx));
682 }
683 else if (!strcmp(a->argv[1], "setcredentials"))
684 {
685 bool fAllowLocalLogon = true;
686 if (a->argc == 7)
687 {
688 if ( strcmp(a->argv[5], "--allowlocallogon")
689 && strcmp(a->argv[5], "-allowlocallogon"))
690 {
691 errorArgument("Invalid parameter '%s'", a->argv[5]);
692 rc = E_FAIL;
693 break;
694 }
695 if (!strcmp(a->argv[6], "no"))
696 fAllowLocalLogon = false;
697 }
698 else if (a->argc != 5)
699 {
700 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
701 rc = E_FAIL;
702 break;
703 }
704
705 ComPtr<IGuest> guest;
706 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
707 CHECK_ERROR_BREAK(guest, SetCredentials(Bstr(a->argv[2]).raw(),
708 Bstr(a->argv[3]).raw(),
709 Bstr(a->argv[4]).raw(),
710 fAllowLocalLogon));
711 }
712#if 0 /* TODO: review & remove */
713 else if (!strcmp(a->argv[1], "dvdattach"))
714 {
715 Bstr uuid;
716 if (a->argc != 3)
717 {
718 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
719 rc = E_FAIL;
720 break;
721 }
722
723 ComPtr<IMedium> dvdMedium;
724
725 /* unmount? */
726 if (!strcmp(a->argv[2], "none"))
727 {
728 /* nothing to do, NULL object will cause unmount */
729 }
730 /* host drive? */
731 else if (!strncmp(a->argv[2], "host:", 5))
732 {
733 ComPtr<IHost> host;
734 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
735
736 rc = host->FindHostDVDDrive(Bstr(a->argv[2] + 5), dvdMedium.asOutParam());
737 if (!dvdMedium)
738 {
739 errorArgument("Invalid host DVD drive name \"%s\"",
740 a->argv[2] + 5);
741 rc = E_FAIL;
742 break;
743 }
744 }
745 else
746 {
747 /* first assume it's a UUID */
748 uuid = a->argv[2];
749 rc = a->virtualBox->GetDVDImage(uuid, dvdMedium.asOutParam());
750 if (FAILED(rc) || !dvdMedium)
751 {
752 /* must be a filename, check if it's in the collection */
753 rc = a->virtualBox->FindDVDImage(Bstr(a->argv[2]), dvdMedium.asOutParam());
754 /* not registered, do that on the fly */
755 if (!dvdMedium)
756 {
757 Bstr emptyUUID;
758 CHECK_ERROR(a->virtualBox, OpenDVDImage(Bstr(a->argv[2]), emptyUUID, dvdMedium.asOutParam()));
759 }
760 }
761 if (!dvdMedium)
762 {
763 rc = E_FAIL;
764 break;
765 }
766 }
767
768 /** @todo generalize this, allow arbitrary number of DVD drives
769 * and as a consequence multiple attachments and different
770 * storage controllers. */
771 if (dvdMedium)
772 dvdMedium->COMGETTER(Id)(uuid.asOutParam());
773 else
774 uuid = Guid().toString();
775 CHECK_ERROR(machine, MountMedium(Bstr("IDE Controller"), 1, 0, uuid, FALSE /* aForce */));
776 }
777 else if (!strcmp(a->argv[1], "floppyattach"))
778 {
779 Bstr uuid;
780 if (a->argc != 3)
781 {
782 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
783 rc = E_FAIL;
784 break;
785 }
786
787 ComPtr<IMedium> floppyMedium;
788
789 /* unmount? */
790 if (!strcmp(a->argv[2], "none"))
791 {
792 /* nothing to do, NULL object will cause unmount */
793 }
794 /* host drive? */
795 else if (!strncmp(a->argv[2], "host:", 5))
796 {
797 ComPtr<IHost> host;
798 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
799 host->FindHostFloppyDrive(Bstr(a->argv[2] + 5), floppyMedium.asOutParam());
800 if (!floppyMedium)
801 {
802 errorArgument("Invalid host floppy drive name \"%s\"",
803 a->argv[2] + 5);
804 rc = E_FAIL;
805 break;
806 }
807 }
808 else
809 {
810 /* first assume it's a UUID */
811 uuid = a->argv[2];
812 rc = a->virtualBox->GetFloppyImage(uuid, floppyMedium.asOutParam());
813 if (FAILED(rc) || !floppyMedium)
814 {
815 /* must be a filename, check if it's in the collection */
816 rc = a->virtualBox->FindFloppyImage(Bstr(a->argv[2]), floppyMedium.asOutParam());
817 /* not registered, do that on the fly */
818 if (!floppyMedium)
819 {
820 Bstr emptyUUID;
821 CHECK_ERROR(a->virtualBox, OpenFloppyImage(Bstr(a->argv[2]), emptyUUID, floppyMedium.asOutParam()));
822 }
823 }
824 if (!floppyMedium)
825 {
826 rc = E_FAIL;
827 break;
828 }
829 }
830 floppyMedium->COMGETTER(Id)(uuid.asOutParam());
831 CHECK_ERROR(machine, MountMedium(Bstr("Floppy Controller"), 0, 0, uuid, FALSE /* aForce */));
832 }
833#endif /* obsolete dvdattach/floppyattach */
834 else if (!strcmp(a->argv[1], "guestmemoryballoon"))
835 {
836 if (a->argc != 3)
837 {
838 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
839 rc = E_FAIL;
840 break;
841 }
842 uint32_t uVal;
843 int vrc;
844 vrc = RTStrToUInt32Ex(a->argv[2], NULL, 0, &uVal);
845 if (vrc != VINF_SUCCESS)
846 {
847 errorArgument("Error parsing guest memory balloon size '%s'", a->argv[2]);
848 rc = E_FAIL;
849 break;
850 }
851 /* guest is running; update IGuest */
852 ComPtr <IGuest> guest;
853 rc = console->COMGETTER(Guest)(guest.asOutParam());
854 if (SUCCEEDED(rc))
855 CHECK_ERROR(guest, COMSETTER(MemoryBalloonSize)(uVal));
856 }
857 else if (!strcmp(a->argv[1], "teleport"))
858 {
859 Bstr bstrHostname;
860 uint32_t uMaxDowntime = 250 /*ms*/;
861 uint32_t uPort = UINT32_MAX;
862 uint32_t cMsTimeout = 0;
863 Bstr bstrPassword("");
864 static const RTGETOPTDEF s_aTeleportOptions[] =
865 {
866 { "--host", 'h', RTGETOPT_REQ_STRING }, /** @todo RTGETOPT_FLAG_MANDATORY */
867 { "--hostname", 'h', RTGETOPT_REQ_STRING }, /** @todo remove this */
868 { "--maxdowntime", 'd', RTGETOPT_REQ_UINT32 },
869 { "--port", 'p', RTGETOPT_REQ_UINT32 }, /** @todo RTGETOPT_FLAG_MANDATORY */
870 { "--password", 'P', RTGETOPT_REQ_STRING },
871 { "--timeout", 't', RTGETOPT_REQ_UINT32 },
872 { "--detailed-progress", 'D', RTGETOPT_REQ_NOTHING }
873 };
874 RTGETOPTSTATE GetOptState;
875 RTGetOptInit(&GetOptState, a->argc, a->argv, s_aTeleportOptions, RT_ELEMENTS(s_aTeleportOptions), 2, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
876 int ch;
877 RTGETOPTUNION Value;
878 while ( SUCCEEDED(rc)
879 && (ch = RTGetOpt(&GetOptState, &Value)))
880 {
881 switch (ch)
882 {
883 case 'h': bstrHostname = Value.psz; break;
884 case 'd': uMaxDowntime = Value.u32; break;
885 case 'D': g_fDetailedProgress = true; break;
886 case 'p': uPort = Value.u32; break;
887 case 'P': bstrPassword = Value.psz; break;
888 case 't': cMsTimeout = Value.u32; break;
889 default:
890 errorGetOpt(USAGE_CONTROLVM, ch, &Value);
891 rc = E_FAIL;
892 break;
893 }
894 }
895 if (FAILED(rc))
896 break;
897
898 ComPtr<IProgress> progress;
899 CHECK_ERROR_BREAK(console, Teleport(bstrHostname.raw(), uPort,
900 bstrPassword.raw(),
901 uMaxDowntime,
902 progress.asOutParam()));
903
904 if (cMsTimeout)
905 {
906 rc = progress->COMSETTER(Timeout)(cMsTimeout);
907 if (FAILED(rc) && rc != VBOX_E_INVALID_OBJECT_STATE)
908 CHECK_ERROR_BREAK(progress, COMSETTER(Timeout)(cMsTimeout)); /* lazyness */
909 }
910
911 rc = showProgress(progress);
912 if (FAILED(rc))
913 {
914 com::ProgressErrorInfo info(progress);
915 if (info.isBasicAvailable())
916 RTMsgError("Teleportation failed. Error message: %lS", info.getText().raw());
917 else
918 RTMsgError("Teleportation failed. No error message available!");
919 }
920 }
921 else
922 {
923 errorSyntax(USAGE_CONTROLVM, "Invalid parameter '%s'", Utf8Str(a->argv[1]).c_str());
924 rc = E_FAIL;
925 }
926 } while (0);
927
928 a->session->UnlockMachine();
929
930 return SUCCEEDED(rc) ? 0 : 1;
931}
932
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette