VirtualBox

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

最後變更 在這個檔案從32701是 32701,由 vboxsync 提交於 15 年 前

Frontends/VBoxManage: Error printing cleanup, use stderr and consistent formatting. Small cleanups (like using Keyboard::PutScancodes instead of the more clumsy Keyboard::PutScancode and fixing the incorrect comment which attracted my attention).

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

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