VirtualBox

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

Last change on this file since 26956 was 26941, checked in by vboxsync, 15 years ago

VBoxManage: --guestmemoryballoon => guestmemoryballoon; --showgueststats => showgueststats

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