VirtualBox

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

最後變更 在這個檔案從35036是 34971,由 vboxsync 提交於 14 年 前

VBoxManage: move vmstatistics to debugvm as a 'statistics' sub-command. Remove injectnmi completely from controlvm - the audience of this features is tiny but smart, so no need for compatibility stuff.

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

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