VirtualBox

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

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

one more docs fix

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