VirtualBox

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

Last change on this file since 13325 was 13221, checked in by vboxsync, 16 years ago

Enabled VPID (VT-x tagged TLB); default off

  • Added machine & machine debugger property
  • Added -vtxvpid option to VBoxManage
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 308.8 KB
Line 
1/* $Id: VBoxManage.cpp 13221 2008-10-13 14:43:54Z vboxsync $ */
2/** @file
3 * VBoxManage - VirtualBox's command-line interface.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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#ifndef VBOX_ONLY_DOCS
27#include <VBox/com/com.h>
28#include <VBox/com/string.h>
29#include <VBox/com/Guid.h>
30#include <VBox/com/array.h>
31#include <VBox/com/ErrorInfo.h>
32#include <VBox/com/EventQueue.h>
33
34#include <VBox/com/VirtualBox.h>
35
36#include <stdlib.h>
37#include <stdarg.h>
38
39#include <vector>
40#include <list>
41#endif /* !VBOX_ONLY_DOCS */
42
43#include <iprt/runtime.h>
44#include <iprt/stream.h>
45#include <iprt/string.h>
46#include <iprt/asm.h>
47#include <iprt/uuid.h>
48#include <iprt/thread.h>
49#include <iprt/path.h>
50#include <iprt/param.h>
51#include <iprt/dir.h>
52#include <iprt/file.h>
53#include <iprt/env.h>
54#include <iprt/cidr.h>
55#include <VBox/err.h>
56#include <VBox/version.h>
57#include <VBox/VBoxHDD.h>
58
59#include "VBoxManage.h"
60
61#ifndef VBOX_ONLY_DOCS
62using namespace com;
63
64/* missing XPCOM <-> COM wrappers */
65#ifndef STDMETHOD_
66# define STDMETHOD_(ret, meth) NS_IMETHOD_(ret) meth
67#endif
68#ifndef NS_GET_IID
69# define NS_GET_IID(I) IID_##I
70#endif
71#ifndef RT_OS_WINDOWS
72#define IUnknown nsISupports
73#endif
74
75/** command handler type */
76typedef DECLCALLBACK(int) FNHANDLER(int argc, char *argv[], ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession);
77typedef FNHANDLER *PFNHANDLER;
78
79/**
80 * Quick IUSBDevice implementation for detaching / attaching
81 * devices to the USB Controller.
82 */
83class MyUSBDevice : public IUSBDevice
84{
85public:
86 // public initializer/uninitializer for internal purposes only
87 MyUSBDevice(uint16_t a_u16VendorId, uint16_t a_u16ProductId, uint16_t a_bcdRevision, uint64_t a_u64SerialHash, const char *a_pszComment)
88 : m_usVendorId(a_u16VendorId), m_usProductId(a_u16ProductId),
89 m_bcdRevision(a_bcdRevision), m_u64SerialHash(a_u64SerialHash),
90 m_bstrComment(a_pszComment),
91 m_cRefs(0)
92 {
93 }
94
95 STDMETHOD_(ULONG, AddRef)(void)
96 {
97 return ASMAtomicIncU32(&m_cRefs);
98 }
99 STDMETHOD_(ULONG, Release)(void)
100 {
101 ULONG cRefs = ASMAtomicDecU32(&m_cRefs);
102 if (!cRefs)
103 delete this;
104 return cRefs;
105 }
106 STDMETHOD(QueryInterface)(const IID &iid, void **ppvObject)
107 {
108 Guid guid(iid);
109 if (guid == Guid(NS_GET_IID(IUnknown)))
110 *ppvObject = (IUnknown *)this;
111 else if (guid == Guid(NS_GET_IID(IUSBDevice)))
112 *ppvObject = (IUSBDevice *)this;
113 else
114 return E_NOINTERFACE;
115 AddRef();
116 return S_OK;
117 }
118
119 STDMETHOD(COMGETTER(Id))(GUIDPARAMOUT a_pId) { return E_NOTIMPL; }
120 STDMETHOD(COMGETTER(VendorId))(USHORT *a_pusVendorId) { *a_pusVendorId = m_usVendorId; return S_OK; }
121 STDMETHOD(COMGETTER(ProductId))(USHORT *a_pusProductId) { *a_pusProductId = m_usProductId; return S_OK; }
122 STDMETHOD(COMGETTER(Revision))(USHORT *a_pusRevision) { *a_pusRevision = m_bcdRevision; return S_OK; }
123 STDMETHOD(COMGETTER(SerialHash))(ULONG64 *a_pullSerialHash) { *a_pullSerialHash = m_u64SerialHash; return S_OK; }
124 STDMETHOD(COMGETTER(Manufacturer))(BSTR *a_pManufacturer) { return E_NOTIMPL; }
125 STDMETHOD(COMGETTER(Product))(BSTR *a_pProduct) { return E_NOTIMPL; }
126 STDMETHOD(COMGETTER(SerialNumber))(BSTR *a_pSerialNumber) { return E_NOTIMPL; }
127 STDMETHOD(COMGETTER(Address))(BSTR *a_pAddress) { return E_NOTIMPL; }
128
129private:
130 /** The vendor id of this USB device. */
131 USHORT m_usVendorId;
132 /** The product id of this USB device. */
133 USHORT m_usProductId;
134 /** The product revision number of this USB device.
135 * (high byte = integer; low byte = decimal) */
136 USHORT m_bcdRevision;
137 /** The USB serial hash of the device. */
138 uint64_t m_u64SerialHash;
139 /** The user comment string. */
140 Bstr m_bstrComment;
141 /** Reference counter. */
142 uint32_t volatile m_cRefs;
143};
144
145
146// types
147///////////////////////////////////////////////////////////////////////////////
148
149template <typename T>
150class Nullable
151{
152public:
153
154 Nullable() : mIsNull (true) {}
155 Nullable (const T &aValue, bool aIsNull = false)
156 : mIsNull (aIsNull), mValue (aValue) {}
157
158 bool isNull() const { return mIsNull; };
159 void setNull (bool aIsNull = true) { mIsNull = aIsNull; }
160
161 operator const T&() const { return mValue; }
162
163 Nullable &operator= (const T &aValue)
164 {
165 mValue = aValue;
166 mIsNull = false;
167 return *this;
168 }
169
170private:
171
172 bool mIsNull;
173 T mValue;
174};
175
176/** helper structure to encapsulate USB filter manipulation commands */
177struct USBFilterCmd
178{
179 struct USBFilter
180 {
181 USBFilter ()
182 : mAction (USBDeviceFilterAction_Null)
183 {}
184
185 Bstr mName;
186 Nullable <bool> mActive;
187 Bstr mVendorId;
188 Bstr mProductId;
189 Bstr mRevision;
190 Bstr mManufacturer;
191 Bstr mProduct;
192 Bstr mRemote;
193 Bstr mSerialNumber;
194 Nullable <ULONG> mMaskedInterfaces;
195 USBDeviceFilterAction_T mAction;
196 };
197
198 enum Action { Invalid, Add, Modify, Remove };
199
200 USBFilterCmd() : mAction (Invalid), mIndex (0), mGlobal (false) {}
201
202 Action mAction;
203 ULONG mIndex;
204 /** flag whether the command target is a global filter */
205 bool mGlobal;
206 /** machine this command is targeted at (null for global filters) */
207 ComPtr<IMachine> mMachine;
208 USBFilter mFilter;
209};
210#endif /* !VBOX_ONLY_DOCS */
211
212// funcs
213///////////////////////////////////////////////////////////////////////////////
214
215static void showLogo(void)
216{
217 static bool fShown; /* show only once */
218
219 if (!fShown)
220 {
221 RTPrintf("VirtualBox Command Line Management Interface Version "
222 VBOX_VERSION_STRING "\n"
223 "(C) 2005-2008 Sun Microsystems, Inc.\n"
224 "All rights reserved.\n"
225 "\n");
226 fShown = true;
227 }
228}
229
230static void printUsage(USAGECATEGORY u64Cmd)
231{
232#ifdef RT_OS_LINUX
233 bool fLinux = true;
234#else
235 bool fLinux = false;
236#endif
237#ifdef RT_OS_WINDOWS
238 bool fWin = true;
239#else
240 bool fWin = false;
241#endif
242#ifdef RT_OS_SOLARIS
243 bool fSolaris = true;
244#else
245 bool fSolaris = false;
246#endif
247#ifdef RT_OS_DARWIN
248 bool fDarwin = true;
249#else
250 bool fDarwin = false;
251#endif
252#ifdef VBOX_WITH_VRDP
253 bool fVRDP = true;
254#else
255 bool fVRDP = false;
256#endif
257
258 if (u64Cmd == USAGE_DUMPOPTS)
259 {
260 fLinux = true;
261 fWin = true;
262 fSolaris = true;
263 fDarwin = true;
264 fVRDP = true;
265 u64Cmd = USAGE_ALL;
266 }
267
268 RTPrintf("Usage:\n"
269 "\n");
270
271 if (u64Cmd == USAGE_ALL)
272 {
273 RTPrintf("VBoxManage [-v|-version] print version number and exit\n"
274 "VBoxManage -nologo ... suppress the logo\n"
275 "\n"
276 "VBoxManage -convertSettings ... allow to auto-convert settings files\n"
277 "VBoxManage -convertSettingsBackup ... allow to auto-convert settings files\n"
278 " but create backup copies before\n"
279 "VBoxManage -convertSettingsIgnore ... allow to auto-convert settings files\n"
280 " but don't explicitly save the results\n"
281 "\n");
282 }
283
284 if (u64Cmd & USAGE_LIST)
285 {
286 RTPrintf("VBoxManage list vms|runningvms|ostypes|hostdvds|hostfloppies|\n"
287 " hostifs|hostinfo|hdds|dvds|floppies|\n"
288 " usbhost|usbfilters|systemproperties\n"
289 "\n");
290 }
291
292 if (u64Cmd & USAGE_SHOWVMINFO)
293 {
294 RTPrintf("VBoxManage showvminfo <uuid>|<name>\n"
295 " [-details]\n"
296 " [-statistics]\n"
297 " [-machinereadable]\n"
298 "\n");
299 }
300
301 if (u64Cmd & USAGE_REGISTERVM)
302 {
303 RTPrintf("VBoxManage registervm <filename>\n"
304 "\n");
305 }
306
307 if (u64Cmd & USAGE_UNREGISTERVM)
308 {
309 RTPrintf("VBoxManage unregistervm <uuid>|<name>\n"
310 " [-delete]\n"
311 "\n");
312 }
313
314 if (u64Cmd & USAGE_CREATEVM)
315 {
316 RTPrintf("VBoxManage createvm -name <name>\n"
317 " [-register]\n"
318 " [-basefolder <path> | -settingsfile <path>]\n"
319 " [-uuid <uuid>]\n"
320 " \n"
321 "\n");
322 }
323
324 if (u64Cmd & USAGE_MODIFYVM)
325 {
326 RTPrintf("VBoxManage modifyvm <uuid|name>\n"
327 " [-name <name>]\n"
328 " [-ostype <ostype>]\n"
329 " [-memory <memorysize>]\n"
330 " [-vram <vramsize>]\n"
331 " [-acpi on|off]\n"
332 " [-ioapic on|off]\n"
333 " [-pae on|off]\n"
334 " [-hwvirtex on|off|default]\n"
335 " [-nestedpaging on|off]\n"
336 " [-vtxvpid on|off]\n"
337 " [-monitorcount <number>]\n"
338 " [-bioslogofadein on|off]\n"
339 " [-bioslogofadeout on|off]\n"
340 " [-bioslogodisplaytime <msec>]\n"
341 " [-bioslogoimagepath <imagepath>]\n"
342 " [-biosbootmenu disabled|menuonly|messageandmenu]\n"
343 " [-biossystemtimeoffset <msec>]\n"
344 " [-biospxedebug on|off]\n"
345 " [-boot<1-4> none|floppy|dvd|disk|net>]\n"
346 " [-hd<a|b|d> none|<uuid>|<filename>]\n"
347 " [-idecontroller PIIX3|PIIX4]\n"
348#ifdef VBOX_WITH_AHCI
349 " [-sata on|off]\n"
350 " [-sataportcount <1-30>]\n"
351 " [-sataport<1-30> none|<uuid>|<filename>]\n"
352 " [-sataideemulation<1-4> <1-30>]\n"
353#endif
354 " [-dvd none|<uuid>|<filename>|host:<drive>]\n"
355 " [-dvdpassthrough on|off]\n"
356 " [-floppy disabled|empty|<uuid>|\n"
357 " <filename>|host:<drive>]\n"
358 " [-nic<1-N> none|null|nat|hostif|intnet]\n"
359 " [-nictype<1-N> Am79C970A|Am79C973"
360#ifdef VBOX_WITH_E1000
361 "|82540EM|82543GC"
362#endif
363 "]\n"
364 " [-cableconnected<1-N> on|off]\n"
365 " [-nictrace<1-N> on|off]\n"
366 " [-nictracefile<1-N> <filename>]\n"
367 " [-nicspeed<1-N> <kbps>]\n"
368 " [-hostifdev<1-N> none|<devicename>]\n"
369 " [-intnet<1-N> <network name>]\n"
370 " [-natnet<1-N> <network>|default]\n"
371 " [-macaddress<1-N> auto|<mac>]\n"
372 " [-uart<1-N> off|<I/O base> <IRQ>]\n"
373 " [-uartmode<1-N> disconnected|\n"
374 " server <pipe>|\n"
375 " client <pipe>|\n"
376 " <devicename>]\n"
377#ifdef VBOX_WITH_MEM_BALLOONING
378 " [-guestmemoryballoon <balloonsize>]\n"
379#endif
380 " [-gueststatisticsinterval <seconds>]\n"
381 );
382 if (fLinux)
383 {
384 RTPrintf(" [-tapsetup<1-N> none|<application>]\n"
385 " [-tapterminate<1-N> none|<application>]\n");
386 }
387 RTPrintf(" [-audio none|null");
388 if (fWin)
389 {
390#ifdef VBOX_WITH_WINMM
391 RTPrintf( "|winmm|dsound");
392#else
393 RTPrintf( "|dsound");
394#endif
395 }
396 if (fSolaris)
397 {
398 RTPrintf( "|solaudio");
399 }
400 if (fLinux)
401 {
402 RTPrintf( "|oss"
403#ifdef VBOX_WITH_ALSA
404 "|alsa"
405#endif
406#ifdef VBOX_WITH_PULSE
407 "|pulse"
408#endif
409 );
410 }
411 if (fDarwin)
412 {
413 RTPrintf( "|coreaudio");
414 }
415 RTPrintf( "]\n");
416 RTPrintf(" [-audiocontroller ac97|sb16]\n"
417 " [-clipboard disabled|hosttoguest|guesttohost|\n"
418 " bidirectional]\n");
419 if (fVRDP)
420 {
421 RTPrintf(" [-vrdp on|off]\n"
422 " [-vrdpport default|<port>]\n"
423 " [-vrdpaddress <host>]\n"
424 " [-vrdpauthtype null|external|guest]\n"
425 " [-vrdpmulticon on|off]\n"
426 " [-vrdpreusecon on|off]\n");
427 }
428 RTPrintf(" [-usb on|off]\n"
429 " [-usbehci on|off]\n"
430 " [-snapshotfolder default|<path>]\n");
431 RTPrintf("\n");
432 }
433
434 if (u64Cmd & USAGE_STARTVM)
435 {
436 RTPrintf("VBoxManage startvm <uuid>|<name>\n");
437 if (fVRDP)
438 RTPrintf(" [-type gui|vrdp]\n");
439 RTPrintf("\n");
440 }
441
442 if (u64Cmd & USAGE_CONTROLVM)
443 {
444 RTPrintf("VBoxManage controlvm <uuid>|<name>\n"
445 " pause|resume|reset|poweroff|savestate|\n"
446 " acpipowerbutton|acpisleepbutton|\n"
447 " keyboardputscancode <hex> [<hex> ...]|\n"
448 " setlinkstate<1-4> on|off |\n"
449 " usbattach <uuid>|<address> |\n"
450 " usbdetach <uuid>|<address> |\n"
451 " dvdattach none|<uuid>|<filename>|host:<drive> |\n"
452 " floppyattach none|<uuid>|<filename>|host:<drive> |\n"
453 " setvideomodehint <xres> <yres> <bpp> [display]|\n"
454 " setcredentials <username> <password> <domain>\n"
455 " [-allowlocallogon <yes|no>]\n"
456 "\n");
457 }
458
459 if (u64Cmd & USAGE_DISCARDSTATE)
460 {
461 RTPrintf("VBoxManage discardstate <uuid>|<name>\n"
462 "\n");
463 }
464
465 if (u64Cmd & USAGE_ADOPTSTATE)
466 {
467 RTPrintf("VBoxManage adoptstate <uuid>|<name> <state_file>\n"
468 "\n");
469 }
470
471 if (u64Cmd & USAGE_SNAPSHOT)
472 {
473 RTPrintf("VBoxManage snapshot <uuid>|<name>\n"
474 " take <name> [-desc <desc>] |\n"
475 " discard <uuid>|<name> |\n"
476 " discardcurrent -state|-all |\n"
477 " edit <uuid>|<name>|-current\n"
478 " [-newname <name>]\n"
479 " [-newdesc <desc>] |\n"
480 " showvminfo <uuid>|<name>\n"
481 "\n");
482 }
483
484 if (u64Cmd & USAGE_REGISTERIMAGE)
485 {
486 RTPrintf("VBoxManage registerimage disk|dvd|floppy <filename>\n"
487 " [-type normal|immutable|writethrough] (disk only)\n"
488 "\n");
489 }
490
491 if (u64Cmd & USAGE_UNREGISTERIMAGE)
492 {
493 RTPrintf("VBoxManage unregisterimage disk|dvd|floppy <uuid>|<filename>\n"
494 "\n");
495 }
496
497 if (u64Cmd & USAGE_SHOWVDIINFO)
498 {
499 RTPrintf("VBoxManage showvdiinfo <uuid>|<filename>\n"
500 "\n");
501 }
502
503 if (u64Cmd & USAGE_CREATEVDI)
504 {
505 RTPrintf("VBoxManage createvdi -filename <filename>\n"
506 " -size <megabytes>\n"
507 " [-static]\n"
508 " [-comment <comment>]\n"
509 " [-register]\n"
510 " [-type normal|writethrough] (default: normal)\n"
511 "\n");
512 }
513
514 if (u64Cmd & USAGE_MODIFYVDI)
515 {
516 RTPrintf("VBoxManage modifyvdi <uuid>|<filename>\n"
517#if 0 /* doesn't currently work */
518 " settype normal|writethrough|immutable |\n"
519#endif
520 " compact\n"
521 "\n");
522 }
523
524 if (u64Cmd & USAGE_CLONEVDI)
525 {
526 RTPrintf("VBoxManage clonevdi <uuid>|<filename> <outputfile>\n"
527 "\n");
528 }
529
530 if (u64Cmd & USAGE_CONVERTDD)
531 {
532 RTPrintf("VBoxManage convertdd [-static] <filename> <outputfile>\n"
533 "VBoxManage convertdd [-static] stdin <outputfile> <bytes>\n"
534 "\n");
535 }
536
537 if (u64Cmd & USAGE_ADDISCSIDISK)
538 {
539 RTPrintf("VBoxManage addiscsidisk -server <name>|<ip>\n"
540 " -target <target>\n"
541 " [-port <port>]\n"
542 " [-lun <lun>]\n"
543 " [-encodedlun <lun>]\n"
544 " [-username <username>]\n"
545 " [-password <password>]\n"
546 " [-comment <comment>]\n"
547 "\n");
548 }
549
550 if (u64Cmd & USAGE_CREATEHOSTIF && fWin)
551 {
552 RTPrintf("VBoxManage createhostif <name>\n"
553 "\n");
554 }
555
556 if (u64Cmd & USAGE_REMOVEHOSTIF && fWin)
557 {
558 RTPrintf("VBoxManage removehostif <uuid>|<name>\n"
559 "\n");
560 }
561
562 if (u64Cmd & USAGE_GETEXTRADATA)
563 {
564 RTPrintf("VBoxManage getextradata global|<uuid>|<name>\n"
565 " <key>|enumerate\n"
566 "\n");
567 }
568
569 if (u64Cmd & USAGE_SETEXTRADATA)
570 {
571 RTPrintf("VBoxManage setextradata global|<uuid>|<name>\n"
572 " <key>\n"
573 " [<value>] (no value deletes key)\n"
574 "\n");
575 }
576
577 if (u64Cmd & USAGE_SETPROPERTY)
578 {
579 RTPrintf("VBoxManage setproperty vdifolder default|<folder> |\n"
580 " machinefolder default|<folder> |\n"
581 " vrdpauthlibrary default|<library> |\n"
582 " websrvauthlibrary default|null|<library> |\n"
583 " hwvirtexenabled yes|no\n"
584 " loghistorycount <value>\n"
585 "\n");
586 }
587
588 if (u64Cmd & USAGE_USBFILTER_ADD)
589 {
590 RTPrintf("VBoxManage usbfilter add <index,0-N>\n"
591 " -target <uuid>|<name>|global\n"
592 " -name <string>\n"
593 " -action ignore|hold (global filters only)\n"
594 " [-active yes|no] (yes)\n"
595 " [-vendorid <XXXX>] (null)\n"
596 " [-productid <XXXX>] (null)\n"
597 " [-revision <IIFF>] (null)\n"
598 " [-manufacturer <string>] (null)\n"
599 " [-product <string>] (null)\n"
600 " [-remote yes|no] (null, VM filters only)\n"
601 " [-serialnumber <string>] (null)\n"
602 " [-maskedinterfaces <XXXXXXXX>]\n"
603 "\n");
604 }
605
606 if (u64Cmd & USAGE_USBFILTER_MODIFY)
607 {
608 RTPrintf("VBoxManage usbfilter modify <index,0-N>\n"
609 " -target <uuid>|<name>|global\n"
610 " [-name <string>]\n"
611 " [-action ignore|hold] (global filters only)\n"
612 " [-active yes|no]\n"
613 " [-vendorid <XXXX>|\"\"]\n"
614 " [-productid <XXXX>|\"\"]\n"
615 " [-revision <IIFF>|\"\"]\n"
616 " [-manufacturer <string>|\"\"]\n"
617 " [-product <string>|\"\"]\n"
618 " [-remote yes|no] (null, VM filters only)\n"
619 " [-serialnumber <string>|\"\"]\n"
620 " [-maskedinterfaces <XXXXXXXX>]\n"
621 "\n");
622 }
623
624 if (u64Cmd & USAGE_USBFILTER_REMOVE)
625 {
626 RTPrintf("VBoxManage usbfilter remove <index,0-N>\n"
627 " -target <uuid>|<name>|global\n"
628 "\n");
629 }
630
631 if (u64Cmd & USAGE_SHAREDFOLDER_ADD)
632 {
633 RTPrintf("VBoxManage sharedfolder add <vmname>|<uuid>\n"
634 " -name <name> -hostpath <hostpath>\n"
635 " [-transient] [-readonly]\n"
636 "\n");
637 }
638
639 if (u64Cmd & USAGE_SHAREDFOLDER_REMOVE)
640 {
641 RTPrintf("VBoxManage sharedfolder remove <vmname>|<uuid>\n"
642 " -name <name> [-transient]\n"
643 "\n");
644 }
645
646 if (u64Cmd & USAGE_VM_STATISTICS)
647 {
648 RTPrintf("VBoxManage vmstatistics <vmname>|<uuid> [-reset]\n"
649 " [-pattern <pattern>] [-descriptions]\n"
650 "\n");
651 }
652
653#ifdef VBOX_WITH_GUEST_PROPS
654 if (u64Cmd & USAGE_GUESTPROPERTY)
655 usageGuestProperty();
656#endif /* VBOX_WITH_GUEST_PROPS defined */
657
658 if (u64Cmd & USAGE_METRICS)
659 {
660 RTPrintf("VBoxManage metrics list [*|host|<vmname> [<metric_list>]] (comma-separated)\n\n"
661 "VBoxManage metrics setup\n"
662 " [-period <seconds>]\n"
663 " [-samples <count>]\n"
664 " [-list]\n"
665 " [*|host|<vmname> [<metric_list>]]\n\n"
666 "VBoxManage metrics query [*|host|<vmname> [<metric_list>]]\n\n"
667 "VBoxManage metrics collect\n"
668 " [-period <seconds>]\n"
669 " [-samples <count>]\n"
670 " [-list]\n"
671 " [-detach]\n"
672 " [*|host|<vmname> [<metric_list>]]\n"
673 "\n");
674 }
675
676}
677
678/**
679 * Print a usage synopsis and the syntax error message.
680 */
681int errorSyntax(USAGECATEGORY u64Cmd, const char *pszFormat, ...)
682{
683 va_list args;
684 showLogo(); // show logo even if suppressed
685#ifndef VBOX_ONLY_DOCS
686 if (g_fInternalMode)
687 printUsageInternal(u64Cmd);
688 else
689 printUsage(u64Cmd);
690#endif /* !VBOX_ONLY_DOCS */
691 va_start(args, pszFormat);
692 RTPrintf("\n"
693 "Syntax error: %N\n", pszFormat, &args);
694 va_end(args);
695 return 1;
696}
697
698/**
699 * Print an error message without the syntax stuff.
700 */
701int errorArgument(const char *pszFormat, ...)
702{
703 va_list args;
704 va_start(args, pszFormat);
705 RTPrintf("error: %N\n", pszFormat, &args);
706 va_end(args);
707 return 1;
708}
709
710#ifndef VBOX_ONLY_DOCS
711/**
712 * Print out progress on the console
713 */
714static void showProgress(ComPtr<IProgress> progress)
715{
716 BOOL fCompleted;
717 LONG currentPercent;
718 LONG lastPercent = 0;
719
720 RTPrintf("0%%...");
721 RTStrmFlush(g_pStdOut);
722 while (SUCCEEDED(progress->COMGETTER(Completed(&fCompleted))))
723 {
724 progress->COMGETTER(Percent(&currentPercent));
725
726 /* did we cross a 10% mark? */
727 if (((currentPercent / 10) > (lastPercent / 10)))
728 {
729 /* make sure to also print out missed steps */
730 for (LONG curVal = (lastPercent / 10) * 10 + 10; curVal <= (currentPercent / 10) * 10; curVal += 10)
731 {
732 if (curVal < 100)
733 {
734 RTPrintf("%ld%%...", curVal);
735 RTStrmFlush(g_pStdOut);
736 }
737 }
738 lastPercent = (currentPercent / 10) * 10;
739 }
740 if (fCompleted)
741 break;
742
743 /* make sure the loop is not too tight */
744 progress->WaitForCompletion(100);
745 }
746
747 /* complete the line. */
748 HRESULT rc;
749 if (SUCCEEDED(progress->COMGETTER(ResultCode)(&rc)))
750 {
751 if (SUCCEEDED(rc))
752 RTPrintf("100%%\n");
753 else
754 RTPrintf("FAILED\n");
755 }
756 else
757 RTPrintf("\n");
758 RTStrmFlush(g_pStdOut);
759}
760
761static void showSnapshots(ComPtr<ISnapshot> rootSnapshot, VMINFO_DETAILS details, const Bstr &prefix = "", int level = 0)
762{
763 /* start with the root */
764 Bstr name;
765 Guid uuid;
766 rootSnapshot->COMGETTER(Name)(name.asOutParam());
767 rootSnapshot->COMGETTER(Id)(uuid.asOutParam());
768 if (details == VMINFO_MACHINEREADABLE)
769 {
770 /* print with hierarchical numbering */
771 RTPrintf("SnapshotName%lS=\"%lS\"\n", prefix.raw(), name.raw());
772 RTPrintf("SnapshotUUID%lS=\"%s\"\n", prefix.raw(), uuid.toString().raw());
773 }
774 else
775 {
776 /* print with indentation */
777 RTPrintf(" %lSName: %lS (UUID: %s)\n", prefix.raw(), name.raw(), uuid.toString().raw());
778 }
779
780 /* get the children */
781 ComPtr<ISnapshotCollection> coll;
782 rootSnapshot->COMGETTER(Children)(coll.asOutParam());
783 if (coll)
784 {
785 ComPtr<ISnapshotEnumerator> enumerator;
786 coll->Enumerate(enumerator.asOutParam());
787 ULONG index = 0;
788 BOOL hasMore = FALSE;
789 while (enumerator->HasMore(&hasMore), hasMore)
790 {
791 ComPtr<ISnapshot> snapshot;
792 enumerator->GetNext(snapshot.asOutParam());
793 if (snapshot)
794 {
795 Bstr newPrefix;
796 if (details == VMINFO_MACHINEREADABLE)
797 newPrefix = Utf8StrFmt("%lS-%d", prefix.raw(), index + 1);
798 else
799 newPrefix = Utf8StrFmt("%lS ", prefix.raw());
800 /* recursive call */
801 showSnapshots(snapshot, details, newPrefix, level + 1);
802 }
803 index++;
804 }
805 }
806}
807
808static void makeTimeStr (char *s, int cb, int64_t millies)
809{
810 RTTIME t;
811 RTTIMESPEC ts;
812
813 RTTimeSpecSetMilli(&ts, millies);
814
815 RTTimeExplode (&t, &ts);
816
817 RTStrPrintf(s, cb, "%04d/%02d/%02d %02d:%02d:%02d UTC",
818 t.i32Year, t.u8Month, t.u8MonthDay,
819 t.u8Hour, t.u8Minute, t.u8Second);
820}
821
822static HRESULT showVMInfo (ComPtr <IVirtualBox> virtualBox, ComPtr<IMachine> machine,
823 ComPtr <IConsole> console = ComPtr <IConsole> (),
824 VMINFO_DETAILS details = VMINFO_NONE)
825{
826 HRESULT rc;
827
828 /*
829 * The rules for output in -argdump format:
830 * 1) the key part (the [0-9a-zA-Z_]+ string before the '=' delimiter)
831 * is all lowercase for "VBoxManage modifyvm" parameters. Any
832 * other values printed are in CamelCase.
833 * 2) strings (anything non-decimal) are printed surrounded by
834 * double quotes '"'. If the strings themselves contain double
835 * quotes, these characters are escaped by '\'. Any '\' character
836 * in the original string is also escaped by '\'.
837 * 3) numbers (containing just [0-9\-]) are written out unchanged.
838 */
839
840 /** @todo the quoting is not yet implemented! */
841
842 BOOL accessible = FALSE;
843 CHECK_ERROR (machine, COMGETTER(Accessible) (&accessible));
844 CheckComRCReturnRC (rc);
845
846 if (!accessible)
847 {
848 if (details == VMINFO_MACHINEREADABLE)
849 RTPrintf("name=\"<inaccessible>\"\n");
850 else
851 RTPrintf ("Name: <inaccessible!>\n");
852 Guid uuid;
853 rc = machine->COMGETTER(Id) (uuid.asOutParam());
854 if (details == VMINFO_MACHINEREADABLE)
855 RTPrintf ("UUID=\"%s\"\n", uuid.toString().raw());
856 else
857 RTPrintf ("UUID: %s\n", uuid.toString().raw());
858 if (details != VMINFO_MACHINEREADABLE)
859 {
860 Bstr settingsFilePath;
861 rc = machine->COMGETTER(SettingsFilePath) (settingsFilePath.asOutParam());
862 RTPrintf ("Config file: %lS\n", settingsFilePath.raw());
863 ComPtr<IVirtualBoxErrorInfo> accessError;
864 rc = machine->COMGETTER(AccessError) (accessError.asOutParam());
865 RTPrintf ("Access error details:\n");
866 ErrorInfo ei (accessError);
867 PRINT_ERROR_INFO (ei);
868 RTPrintf ("\n");
869 }
870 return S_OK;
871 }
872
873 Bstr machineName;
874 rc = machine->COMGETTER(Name)(machineName.asOutParam());
875 if (details == VMINFO_MACHINEREADABLE)
876 RTPrintf("name=\"%lS\"\n", machineName.raw());
877 else
878 RTPrintf("Name: %lS\n", machineName.raw());
879
880 Bstr osTypeId;
881 rc = machine->COMGETTER(OSTypeId)(osTypeId.asOutParam());
882 ComPtr<IGuestOSType> osType;
883 rc = virtualBox->GetGuestOSType (osTypeId, osType.asOutParam());
884 Bstr osName;
885 rc = osType->COMGETTER(Description)(osName.asOutParam());
886 if (details == VMINFO_MACHINEREADABLE)
887 RTPrintf("ostype=\"%lS\"\n", osTypeId.raw());
888 else
889 RTPrintf("Guest OS: %lS\n", osName.raw());
890
891 Guid uuid;
892 rc = machine->COMGETTER(Id)(uuid.asOutParam());
893 if (details == VMINFO_MACHINEREADABLE)
894 RTPrintf("UUID=\"%s\"\n", uuid.toString().raw());
895 else
896 RTPrintf("UUID: %s\n", uuid.toString().raw());
897
898 Bstr settingsFilePath;
899 rc = machine->COMGETTER(SettingsFilePath)(settingsFilePath.asOutParam());
900 if (details == VMINFO_MACHINEREADABLE)
901 RTPrintf("CfgFile=\"%lS\"\n", settingsFilePath.raw());
902 else
903 RTPrintf("Config file: %lS\n", settingsFilePath.raw());
904
905 ULONG memorySize;
906 rc = machine->COMGETTER(MemorySize)(&memorySize);
907 if (details == VMINFO_MACHINEREADABLE)
908 RTPrintf("memory=%u\n", memorySize);
909 else
910 RTPrintf("Memory size: %uMB\n", memorySize);
911
912 ULONG vramSize;
913 rc = machine->COMGETTER(VRAMSize)(&vramSize);
914 if (details == VMINFO_MACHINEREADABLE)
915 RTPrintf("vram=%u\n", vramSize);
916 else
917 RTPrintf("VRAM size: %uMB\n", vramSize);
918
919 ComPtr <IBIOSSettings> biosSettings;
920 machine->COMGETTER(BIOSSettings)(biosSettings.asOutParam());
921
922 BIOSBootMenuMode_T bootMenuMode;
923 biosSettings->COMGETTER(BootMenuMode)(&bootMenuMode);
924 const char *pszBootMenu = NULL;
925 switch (bootMenuMode)
926 {
927 case BIOSBootMenuMode_Disabled:
928 pszBootMenu = "disabled";
929 break;
930 case BIOSBootMenuMode_MenuOnly:
931 if (details == VMINFO_MACHINEREADABLE)
932 pszBootMenu = "menuonly";
933 else
934 pszBootMenu = "menu only";
935 break;
936 default:
937 if (details == VMINFO_MACHINEREADABLE)
938 pszBootMenu = "messageandmenu";
939 else
940 pszBootMenu = "message and menu";
941 }
942 if (details == VMINFO_MACHINEREADABLE)
943 RTPrintf("bootmenu=\"%s\"\n", pszBootMenu);
944 else
945 RTPrintf("Boot menu mode: %s\n", pszBootMenu);
946
947 BOOL acpiEnabled;
948 biosSettings->COMGETTER(ACPIEnabled)(&acpiEnabled);
949 if (details == VMINFO_MACHINEREADABLE)
950 RTPrintf("acpi=\"%s\"\n", acpiEnabled ? "on" : "off");
951 else
952 RTPrintf("ACPI: %s\n", acpiEnabled ? "on" : "off");
953
954 BOOL ioapicEnabled;
955 biosSettings->COMGETTER(IOAPICEnabled)(&ioapicEnabled);
956 if (details == VMINFO_MACHINEREADABLE)
957 RTPrintf("ioapic=\"%s\"\n", ioapicEnabled ? "on" : "off");
958 else
959 RTPrintf("IOAPIC: %s\n", ioapicEnabled ? "on" : "off");
960
961 BOOL PAEEnabled;
962 machine->COMGETTER(PAEEnabled)(&PAEEnabled);
963 if (details == VMINFO_MACHINEREADABLE)
964 RTPrintf("pae=\"%s\"\n", PAEEnabled ? "on" : "off");
965 else
966 RTPrintf("PAE: %s\n", PAEEnabled ? "on" : "off");
967
968 LONG64 timeOffset;
969 biosSettings->COMGETTER(TimeOffset)(&timeOffset);
970 if (details == VMINFO_MACHINEREADABLE)
971 RTPrintf("biossystemtimeoffset=%lld\n", timeOffset);
972 else
973 RTPrintf("Time offset: %lld ms\n", timeOffset);
974
975 TSBool_T hwVirtExEnabled;
976 machine->COMGETTER(HWVirtExEnabled)(&hwVirtExEnabled);
977 if (hwVirtExEnabled == TSBool_Default)
978 {
979 BOOL fHWVirtExEnabled;
980 ComPtr<ISystemProperties> systemProperties;
981 virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
982 systemProperties->COMGETTER(HWVirtExEnabled)(&fHWVirtExEnabled);
983 if (details == VMINFO_MACHINEREADABLE)
984 RTPrintf("hwvirtex=\"default\"\n");
985 else
986 RTPrintf("Hardw. virt.ext: Default (%s)\n", fHWVirtExEnabled ? "on" : "off");
987 }
988 else
989 {
990 if (details == VMINFO_MACHINEREADABLE)
991 RTPrintf("hwvirtex=\"%s\"\n", hwVirtExEnabled == TSBool_True ? "on" : "off");
992 else
993 RTPrintf("Hardw. virt.ext: %s\n", hwVirtExEnabled == TSBool_True ? "on" : "off");
994 }
995 BOOL HWVirtExNestedPagingEnabled;
996 machine->COMGETTER(HWVirtExNestedPagingEnabled)(&HWVirtExNestedPagingEnabled);
997 if (details == VMINFO_MACHINEREADABLE)
998 RTPrintf("nestedpaging=\"%s\"\n", HWVirtExNestedPagingEnabled ? "on" : "off");
999 else
1000 RTPrintf("Nested Paging: %s\n", HWVirtExNestedPagingEnabled ? "on" : "off");
1001
1002 BOOL HWVirtExVPIDEnabled;
1003 machine->COMGETTER(HWVirtExVPIDEnabled)(&HWVirtExVPIDEnabled);
1004 if (details == VMINFO_MACHINEREADABLE)
1005 RTPrintf("vtxvpid=\"%s\"\n", HWVirtExVPIDEnabled ? "on" : "off");
1006 else
1007 RTPrintf("VT-x VPID: %s\n", HWVirtExVPIDEnabled ? "on" : "off");
1008
1009 MachineState_T machineState;
1010 const char *pszState = NULL;
1011 rc = machine->COMGETTER(State)(&machineState);
1012 switch (machineState)
1013 {
1014 case MachineState_PoweredOff:
1015 if (details == VMINFO_MACHINEREADABLE)
1016 pszState = "poweroff";
1017 else
1018 pszState = "powered off";
1019 break;
1020 case MachineState_Saved:
1021 pszState = "saved";
1022 break;
1023 case MachineState_Aborted:
1024 pszState = "aborted";
1025 break;
1026 case MachineState_Running:
1027 pszState = "running";
1028 break;
1029 case MachineState_Paused:
1030 pszState = "paused";
1031 break;
1032 case MachineState_Starting:
1033 pszState = "starting";
1034 break;
1035 case MachineState_Stopping:
1036 pszState = "stopping";
1037 break;
1038 case MachineState_Saving:
1039 pszState = "saving";
1040 break;
1041 case MachineState_Restoring:
1042 pszState = "restoring";
1043 break;
1044 default:
1045 pszState = "unknown";
1046 break;
1047 }
1048 LONG64 stateSince;
1049 machine->COMGETTER(LastStateChange)(&stateSince);
1050 RTTIMESPEC timeSpec;
1051 RTTimeSpecSetMilli(&timeSpec, stateSince);
1052 char pszTime[30] = {0};
1053 RTTimeSpecToString(&timeSpec, pszTime, sizeof(pszTime));
1054 if (details == VMINFO_MACHINEREADABLE)
1055 {
1056 RTPrintf("VMState=\"%s\"\n", pszState);
1057 RTPrintf("VMStateChangeTime=\"%s\"\n", pszTime);
1058 }
1059 else
1060 RTPrintf("State: %s (since %s)\n", pszState, pszTime);
1061
1062 ULONG numMonitors;
1063 machine->COMGETTER(MonitorCount)(&numMonitors);
1064 if (details == VMINFO_MACHINEREADABLE)
1065 RTPrintf("monitorcount=%d\n", numMonitors);
1066 else
1067 RTPrintf("Monitor count: %d\n", numMonitors);
1068
1069 ComPtr<IFloppyDrive> floppyDrive;
1070 rc = machine->COMGETTER(FloppyDrive)(floppyDrive.asOutParam());
1071 if (SUCCEEDED(rc) && floppyDrive)
1072 {
1073 BOOL fFloppyEnabled;
1074 floppyDrive->COMGETTER(Enabled)(&fFloppyEnabled);
1075 Utf8Str pszFloppy = "invalid";
1076 if (fFloppyEnabled)
1077 {
1078 DriveState_T floppyState;
1079 floppyDrive->COMGETTER(State)(&floppyState);
1080 switch (floppyState)
1081 {
1082 case DriveState_ImageMounted:
1083 {
1084 ComPtr<IFloppyImage> floppyImage;
1085 rc = floppyDrive->GetImage(floppyImage.asOutParam());
1086 if (SUCCEEDED(rc) && floppyImage)
1087 {
1088 Bstr imagePath;
1089 floppyImage->COMGETTER(FilePath)(imagePath.asOutParam());
1090 Guid imageGuid;
1091 floppyImage->COMGETTER(Id)(imageGuid.asOutParam());
1092 if (details == VMINFO_MACHINEREADABLE)
1093 {
1094 RTPrintf("FloppyImageUUID=\"%s\"\n", imageGuid.toString().raw());
1095 pszFloppy = Utf8StrFmt("%lS", imagePath.raw());
1096 }
1097 else
1098 pszFloppy = Utf8StrFmt("%lS (UUID: %s)", imagePath.raw(), imageGuid.toString().raw());
1099 }
1100 break;
1101 }
1102
1103 case DriveState_HostDriveCaptured:
1104 {
1105 ComPtr<IHostFloppyDrive> hostFloppyDrive;
1106 rc = floppyDrive->GetHostDrive(hostFloppyDrive.asOutParam());
1107 if (SUCCEEDED(rc) && floppyDrive)
1108 {
1109 Bstr driveName;
1110 hostFloppyDrive->COMGETTER(Name)(driveName.asOutParam());
1111 if (details == VMINFO_MACHINEREADABLE)
1112 pszFloppy = Utf8StrFmt("host:%lS", driveName.raw());
1113 else
1114 pszFloppy = Utf8StrFmt("Host drive %lS", driveName.raw());
1115 }
1116 break;
1117 }
1118
1119 case DriveState_NotMounted:
1120 {
1121 pszFloppy = "empty";
1122 break;
1123 }
1124 }
1125 }
1126 else
1127 {
1128 pszFloppy = "disabled";
1129 }
1130 if (details == VMINFO_MACHINEREADABLE)
1131 RTPrintf("floppy=\"%s\"\n", pszFloppy.raw());
1132 else
1133 RTPrintf("Floppy: %s\n", pszFloppy.raw());
1134 }
1135
1136 /*
1137 * SATA.
1138 */
1139 ComPtr<ISATAController> SATACtl;
1140 rc = machine->COMGETTER(SATAController)(SATACtl.asOutParam());
1141 if (SUCCEEDED(rc))
1142 {
1143 BOOL fEnabled;
1144 rc = SATACtl->COMGETTER(Enabled)(&fEnabled);
1145 if (FAILED(rc))
1146 fEnabled = false;
1147 if (details == VMINFO_MACHINEREADABLE)
1148 RTPrintf("sata=\"%s\"\n", fEnabled ? "on" : "off");
1149 else
1150 RTPrintf("SATA: %s\n", fEnabled ? "enabled" : "disabled");
1151 }
1152
1153 /*
1154 * Hard disks
1155 */
1156 ComPtr<IHardDisk> hardDisk;
1157 Bstr filePath;
1158 rc = machine->GetHardDisk(StorageBus_IDE, 0, 0, hardDisk.asOutParam());
1159 if (SUCCEEDED(rc) && hardDisk)
1160 {
1161 /// @todo (dmik) we temporarily use the location property to
1162 // determine the image file name. This is subject to change
1163 // when iSCSI disks are here (we should either query a
1164 // storage-specific interface from IHardDisk, or "standardize"
1165 // the location property)
1166 hardDisk->COMGETTER(Location)(filePath.asOutParam());
1167 hardDisk->COMGETTER(Id)(uuid.asOutParam());
1168 if (details == VMINFO_MACHINEREADABLE)
1169 {
1170 RTPrintf("hda=\"%lS\"\n", filePath.raw());
1171 RTPrintf("HdaImageUUID=\"%s\"\n", uuid.toString().raw());
1172 }
1173 else
1174 RTPrintf("Primary master: %lS (UUID: %s)\n", filePath.raw(), uuid.toString().raw());
1175 }
1176 else
1177 {
1178 if (details == VMINFO_MACHINEREADABLE)
1179 RTPrintf("hda=\"none\"\n");
1180 }
1181 rc = machine->GetHardDisk(StorageBus_IDE, 0, 1, hardDisk.asOutParam());
1182 if (SUCCEEDED(rc) && hardDisk)
1183 {
1184 /// @todo (dmik) we temporarily use the location property to
1185 // determine the image file name. This is subject to change
1186 // when iSCSI disks are here (we should either query a
1187 // storage-specific interface from IHardDisk, or "standardize"
1188 // the location property)
1189 hardDisk->COMGETTER(Location)(filePath.asOutParam());
1190 hardDisk->COMGETTER(Id)(uuid.asOutParam());
1191 if (details == VMINFO_MACHINEREADABLE)
1192 {
1193 RTPrintf("hdb=\"%lS\"\n", filePath.raw());
1194 RTPrintf("HdbImageUUID=\"%s\"\n", uuid.toString().raw());
1195 }
1196 else
1197 RTPrintf("Primary slave: %lS (UUID: %s)\n", filePath.raw(), uuid.toString().raw());
1198 }
1199 else
1200 {
1201 if (details == VMINFO_MACHINEREADABLE)
1202 RTPrintf("hdb=\"none\"\n");
1203 }
1204 rc = machine->GetHardDisk(StorageBus_IDE, 1, 1, hardDisk.asOutParam());
1205 if (SUCCEEDED(rc) && hardDisk)
1206 {
1207 /// @todo (dmik) we temporarily use the location property to
1208 // determine the image file name. This is subject to change
1209 // when iSCSI disks are here (we should either query a
1210 // storage-specific interface from IHardDisk, or "standardize"
1211 // the location property)
1212 hardDisk->COMGETTER(Location)(filePath.asOutParam());
1213 hardDisk->COMGETTER(Id)(uuid.asOutParam());
1214 if (details == VMINFO_MACHINEREADABLE)
1215 {
1216 RTPrintf("hdd=\"%lS\"\n", filePath.raw());
1217 RTPrintf("HddImageUUID=\"%s\"\n", uuid.toString().raw());
1218 }
1219 else
1220 RTPrintf("Secondary slave: %lS (UUID: %s)\n", filePath.raw(), uuid.toString().raw());
1221 }
1222 else
1223 {
1224 if (details == VMINFO_MACHINEREADABLE)
1225 RTPrintf("hdd=\"none\"\n");
1226 }
1227 ComPtr<IDVDDrive> dvdDrive;
1228 rc = machine->COMGETTER(DVDDrive)(dvdDrive.asOutParam());
1229 if (SUCCEEDED(rc) && dvdDrive)
1230 {
1231 ComPtr<IDVDImage> dvdImage;
1232 rc = dvdDrive->GetImage(dvdImage.asOutParam());
1233 if (SUCCEEDED(rc) && dvdImage)
1234 {
1235 rc = dvdImage->COMGETTER(FilePath)(filePath.asOutParam());
1236 if (SUCCEEDED(rc) && filePath)
1237 {
1238 rc = dvdImage->COMGETTER(Id)(uuid.asOutParam());
1239 if (details == VMINFO_MACHINEREADABLE)
1240 {
1241 RTPrintf("dvd=\"%lS\"\n", filePath.raw());
1242 RTPrintf("DvdImageUUID=\"%s\"\n", uuid.toString().raw());
1243 }
1244 else
1245 RTPrintf("DVD: %lS (UUID: %s)\n", filePath.raw(), uuid.toString().raw());
1246 }
1247 }
1248 else
1249 {
1250 ComPtr<IHostDVDDrive> hostDVDDrive;
1251 rc = dvdDrive->GetHostDrive(hostDVDDrive.asOutParam());
1252 if (SUCCEEDED(rc) && hostDVDDrive)
1253 {
1254 Bstr name;
1255 hostDVDDrive->COMGETTER(Name)(name.asOutParam());
1256 if (details == VMINFO_MACHINEREADABLE)
1257 RTPrintf("dvd=\"host:%lS\"\n", name.raw());
1258 else
1259 RTPrintf("DVD: Host drive %lS", name.raw());
1260 }
1261 else
1262 {
1263 if (details == VMINFO_MACHINEREADABLE)
1264 RTPrintf("dvd=\"none\"\n");
1265 else
1266 RTPrintf("DVD: empty");
1267 }
1268 BOOL fPassthrough;
1269 dvdDrive->COMGETTER(Passthrough)(&fPassthrough);
1270 if (details == VMINFO_MACHINEREADABLE)
1271 {
1272 RTPrintf("dvdpassthrough=\"%s\"\n", fPassthrough ? "on" : "off");
1273 }
1274 else
1275 {
1276 if (fPassthrough)
1277 RTPrintf(" (passthrough enabled)");
1278 RTPrintf("\n");
1279 }
1280 }
1281 }
1282
1283 /* get the maximum amount of NICS */
1284 ComPtr<ISystemProperties> sysProps;
1285 virtualBox->COMGETTER(SystemProperties)(sysProps.asOutParam());
1286 ULONG maxNICs = 0;
1287 sysProps->COMGETTER(NetworkAdapterCount)(&maxNICs);
1288 for (ULONG currentNIC = 0; currentNIC < maxNICs; currentNIC++)
1289 {
1290 ComPtr<INetworkAdapter> nic;
1291 rc = machine->GetNetworkAdapter(currentNIC, nic.asOutParam());
1292 if (SUCCEEDED(rc) && nic)
1293 {
1294 BOOL fEnabled;
1295 nic->COMGETTER(Enabled)(&fEnabled);
1296 if (!fEnabled)
1297 {
1298 if (details == VMINFO_MACHINEREADABLE)
1299 RTPrintf("nic%d=\"none\"\n", currentNIC + 1);
1300 else
1301 RTPrintf("NIC %d: disabled\n", currentNIC + 1);
1302 }
1303 else
1304 {
1305 Bstr strMACAddress;
1306 nic->COMGETTER(MACAddress)(strMACAddress.asOutParam());
1307 Utf8Str strAttachment;
1308 NetworkAttachmentType_T attachment;
1309 nic->COMGETTER(AttachmentType)(&attachment);
1310 switch (attachment)
1311 {
1312 case NetworkAttachmentType_Null:
1313 if (details == VMINFO_MACHINEREADABLE)
1314 strAttachment = "null";
1315 else
1316 strAttachment = "none";
1317 break;
1318 case NetworkAttachmentType_NAT:
1319 {
1320 Bstr strNetwork;
1321 nic->COMGETTER(NATNetwork)(strNetwork.asOutParam());
1322 if (details == VMINFO_MACHINEREADABLE)
1323 {
1324 RTPrintf("natnet%d=\"%lS\"\n", currentNIC + 1, strNetwork.raw());
1325 strAttachment = "nat";
1326 }
1327 else if (!strNetwork.isEmpty())
1328 strAttachment = Utf8StrFmt("NAT (%lS)", strNetwork.raw());
1329 else
1330 strAttachment = "NAT";
1331 break;
1332 }
1333 case NetworkAttachmentType_HostInterface:
1334 {
1335 Bstr strHostIfDev;
1336 nic->COMGETTER(HostInterface)(strHostIfDev.asOutParam());
1337 if (details == VMINFO_MACHINEREADABLE)
1338 {
1339 RTPrintf("hostifdev%d=\"%lS\"\n", currentNIC + 1, strHostIfDev.raw());
1340 strAttachment = "hostif";
1341 }
1342 else
1343 strAttachment = Utf8StrFmt("Host Interface '%lS'", strHostIfDev.raw());
1344 break;
1345 }
1346 case NetworkAttachmentType_Internal:
1347 {
1348 Bstr strNetwork;
1349 nic->COMGETTER(InternalNetwork)(strNetwork.asOutParam());
1350 if (details == VMINFO_MACHINEREADABLE)
1351 {
1352 RTPrintf("intnet%d=\"%lS\"\n", currentNIC + 1, strNetwork.raw());
1353 strAttachment = "intnet";
1354 }
1355 else
1356 strAttachment = Utf8StrFmt("Internal Network '%s'", Utf8Str(strNetwork).raw());
1357 break;
1358 }
1359 default:
1360 strAttachment = "unknown";
1361 break;
1362 }
1363
1364 /* cable connected */
1365 BOOL fConnected;
1366 nic->COMGETTER(CableConnected)(&fConnected);
1367
1368 /* trace stuff */
1369 BOOL fTraceEnabled;
1370 nic->COMGETTER(TraceEnabled)(&fTraceEnabled);
1371 Bstr traceFile;
1372 nic->COMGETTER(TraceFile)(traceFile.asOutParam());
1373
1374 /* NIC type */
1375 Utf8Str strNICType;
1376 NetworkAdapterType_T NICType;
1377 nic->COMGETTER(AdapterType)(&NICType);
1378 switch (NICType) {
1379 case NetworkAdapterType_Am79C970A:
1380 strNICType = "Am79C970A";
1381 break;
1382 case NetworkAdapterType_Am79C973:
1383 strNICType = "Am79C973";
1384 break;
1385#ifdef VBOX_WITH_E1000
1386 case NetworkAdapterType_I82540EM:
1387 strNICType = "82540EM";
1388 break;
1389 case NetworkAdapterType_I82543GC:
1390 strNICType = "82543GC";
1391 break;
1392#endif
1393 default:
1394 strNICType = "unknown";
1395 break;
1396 }
1397
1398 /* reported line speed */
1399 ULONG ulLineSpeed;
1400 nic->COMGETTER(LineSpeed)(&ulLineSpeed);
1401
1402 if (details == VMINFO_MACHINEREADABLE)
1403 {
1404 RTPrintf("macaddress%d=\"%lS\"\n", currentNIC + 1, strMACAddress.raw());
1405 RTPrintf("cableconnected%d=\"%s\"\n", currentNIC + 1, fConnected ? "on" : "off");
1406 RTPrintf("nic%d=\"%s\"\n", currentNIC + 1, strAttachment.raw());
1407 }
1408 else
1409 RTPrintf("NIC %d: MAC: %lS, Attachment: %s, Cable connected: %s, Trace: %s (file: %lS), Type: %s, Reported speed: %d Mbps\n",
1410 currentNIC + 1, strMACAddress.raw(), strAttachment.raw(),
1411 fConnected ? "on" : "off",
1412 fTraceEnabled ? "on" : "off",
1413 traceFile.isEmpty() ? Bstr("none").raw() : traceFile.raw(),
1414 strNICType.raw(),
1415 ulLineSpeed / 1000);
1416 }
1417 }
1418 }
1419
1420 /* get the maximum amount of UARTs */
1421 ULONG maxUARTs = 0;
1422 sysProps->COMGETTER(SerialPortCount)(&maxUARTs);
1423 for (ULONG currentUART = 0; currentUART < maxUARTs; currentUART++)
1424 {
1425 ComPtr<ISerialPort> uart;
1426 rc = machine->GetSerialPort(currentUART, uart.asOutParam());
1427 if (SUCCEEDED(rc) && uart)
1428 {
1429 BOOL fEnabled;
1430 uart->COMGETTER(Enabled)(&fEnabled);
1431 if (!fEnabled)
1432 {
1433 if (details == VMINFO_MACHINEREADABLE)
1434 RTPrintf("uart%d=\"off\"\n", currentUART + 1);
1435 else
1436 RTPrintf("UART %d: disabled\n", currentUART + 1);
1437 }
1438 else
1439 {
1440 ULONG ulIRQ, ulIOBase;
1441 PortMode_T HostMode;
1442 Bstr path;
1443 BOOL fServer;
1444 uart->COMGETTER(IRQ)(&ulIRQ);
1445 uart->COMGETTER(IOBase)(&ulIOBase);
1446 uart->COMGETTER(Path)(path.asOutParam());
1447 uart->COMGETTER(Server)(&fServer);
1448 uart->COMGETTER(HostMode)(&HostMode);
1449
1450 if (details == VMINFO_MACHINEREADABLE)
1451 RTPrintf("uart%d=\"%#06x,%d\"\n", currentUART + 1,
1452 ulIOBase, ulIRQ);
1453 else
1454 RTPrintf("UART %d: I/O base: 0x%04x, IRQ: %d",
1455 currentUART + 1, ulIOBase, ulIRQ);
1456 switch (HostMode)
1457 {
1458 default:
1459 case PortMode_Disconnected:
1460 if (details == VMINFO_MACHINEREADABLE)
1461 RTPrintf("uartmode%d=\"disconnected\"\n", currentUART + 1);
1462 else
1463 RTPrintf(", disconnected\n");
1464 break;
1465 case PortMode_HostPipe:
1466 if (details == VMINFO_MACHINEREADABLE)
1467 RTPrintf("uartmode%d=\"%s,%lS\"\n", currentUART + 1,
1468 fServer ? "server" : "client", path.raw());
1469 else
1470 RTPrintf(", attached to pipe (%s) '%lS'\n",
1471 fServer ? "server" : "client", path.raw());
1472 break;
1473 case PortMode_HostDevice:
1474 if (details == VMINFO_MACHINEREADABLE)
1475 RTPrintf("uartmode%d=\"%lS\"\n", currentUART + 1,
1476 path.raw());
1477 else
1478 RTPrintf(", attached to device '%lS'\n", path.raw());
1479 break;
1480 }
1481 }
1482 }
1483 }
1484
1485 ComPtr<IAudioAdapter> AudioAdapter;
1486 rc = machine->COMGETTER(AudioAdapter)(AudioAdapter.asOutParam());
1487 if (SUCCEEDED(rc))
1488 {
1489 const char *pszDrv = "Unknown";
1490 const char *pszCtrl = "Unknown";
1491 BOOL fEnabled;
1492 rc = AudioAdapter->COMGETTER(Enabled)(&fEnabled);
1493 if (SUCCEEDED(rc) && fEnabled)
1494 {
1495 AudioDriverType_T enmDrvType;
1496 rc = AudioAdapter->COMGETTER(AudioDriver)(&enmDrvType);
1497 switch (enmDrvType)
1498 {
1499 case AudioDriverType_Null:
1500 if (details == VMINFO_MACHINEREADABLE)
1501 pszDrv = "null";
1502 else
1503 pszDrv = "Null";
1504 break;
1505 case AudioDriverType_WinMM:
1506 if (details == VMINFO_MACHINEREADABLE)
1507 pszDrv = "winmm";
1508 else
1509 pszDrv = "WINMM";
1510 break;
1511 case AudioDriverType_DirectSound:
1512 if (details == VMINFO_MACHINEREADABLE)
1513 pszDrv = "dsound";
1514 else
1515 pszDrv = "DSOUND";
1516 break;
1517 case AudioDriverType_OSS:
1518 if (details == VMINFO_MACHINEREADABLE)
1519 pszDrv = "oss";
1520 else
1521 pszDrv = "OSS";
1522 break;
1523 case AudioDriverType_ALSA:
1524 if (details == VMINFO_MACHINEREADABLE)
1525 pszDrv = "alsa";
1526 else
1527 pszDrv = "ALSA";
1528 break;
1529 case AudioDriverType_Pulse:
1530 if (details == VMINFO_MACHINEREADABLE)
1531 pszDrv = "pulse";
1532 else
1533 pszDrv = "PulseAudio";
1534 break;
1535 case AudioDriverType_CoreAudio:
1536 if (details == VMINFO_MACHINEREADABLE)
1537 pszDrv = "coreaudio";
1538 else
1539 pszDrv = "CoreAudio";
1540 break;
1541 case AudioDriverType_SolAudio:
1542 if (details == VMINFO_MACHINEREADABLE)
1543 pszDrv = "solaudio";
1544 else
1545 pszDrv = "SolAudio";
1546 break;
1547 default:
1548 if (details == VMINFO_MACHINEREADABLE)
1549 pszDrv = "unknown";
1550 break;
1551 }
1552 AudioControllerType_T enmCtrlType;
1553 rc = AudioAdapter->COMGETTER(AudioController)(&enmCtrlType);
1554 switch (enmCtrlType)
1555 {
1556 case AudioControllerType_AC97:
1557 if (details == VMINFO_MACHINEREADABLE)
1558 pszCtrl = "ac97";
1559 else
1560 pszCtrl = "AC97";
1561 break;
1562 case AudioControllerType_SB16:
1563 if (details == VMINFO_MACHINEREADABLE)
1564 pszCtrl = "sb16";
1565 else
1566 pszCtrl = "SB16";
1567 break;
1568 }
1569 }
1570 else
1571 fEnabled = FALSE;
1572 if (details == VMINFO_MACHINEREADABLE)
1573 {
1574 if (fEnabled)
1575 RTPrintf("audio=\"%s\"\n", pszDrv);
1576 else
1577 RTPrintf("audio=\"none\"\n");
1578 }
1579 else
1580 RTPrintf("Audio: %s (Driver: %s, Controller: %s)\n",
1581 fEnabled ? "enabled" : "disabled", pszDrv, pszCtrl);
1582 }
1583
1584 /* Shared clipboard */
1585 {
1586 const char *psz = "Unknown";
1587 ClipboardMode_T enmMode;
1588 rc = machine->COMGETTER(ClipboardMode)(&enmMode);
1589 switch (enmMode)
1590 {
1591 case ClipboardMode_Disabled:
1592 if (details == VMINFO_MACHINEREADABLE)
1593 psz = "disabled";
1594 else
1595 psz = "disabled";
1596 break;
1597 case ClipboardMode_HostToGuest:
1598 if (details == VMINFO_MACHINEREADABLE)
1599 psz = "hosttoguest";
1600 else
1601 psz = "HostToGuest";
1602 break;
1603 case ClipboardMode_GuestToHost:
1604 if (details == VMINFO_MACHINEREADABLE)
1605 psz = "guesttohost";
1606 else
1607 psz = "GuestToHost";
1608 break;
1609 case ClipboardMode_Bidirectional:
1610 if (details == VMINFO_MACHINEREADABLE)
1611 psz = "bidirectional";
1612 else
1613 psz = "Bidirectional";
1614 break;
1615 default:
1616 if (details == VMINFO_MACHINEREADABLE)
1617 psz = "unknown";
1618 break;
1619 }
1620 if (details == VMINFO_MACHINEREADABLE)
1621 RTPrintf("clipboard=\"%s\"\n", psz);
1622 else
1623 RTPrintf("Clipboard Mode: %s\n", psz);
1624 }
1625
1626 if (console)
1627 {
1628 ComPtr<IDisplay> display;
1629 CHECK_ERROR_RET(console, COMGETTER(Display)(display.asOutParam()), rc);
1630 do
1631 {
1632 ULONG xRes, yRes, bpp;
1633 rc = display->COMGETTER(Width)(&xRes);
1634 if (rc == E_ACCESSDENIED)
1635 break; /* VM not powered up */
1636 if (FAILED(rc))
1637 {
1638 com::ErrorInfo info (display);
1639 PRINT_ERROR_INFO (info);
1640 return rc;
1641 }
1642 rc = display->COMGETTER(Height)(&yRes);
1643 if (rc == E_ACCESSDENIED)
1644 break; /* VM not powered up */
1645 if (FAILED(rc))
1646 {
1647 com::ErrorInfo info (display);
1648 PRINT_ERROR_INFO (info);
1649 return rc;
1650 }
1651 rc = display->COMGETTER(BitsPerPixel)(&bpp);
1652 if (rc == E_ACCESSDENIED)
1653 break; /* VM not powered up */
1654 if (FAILED(rc))
1655 {
1656 com::ErrorInfo info (display);
1657 PRINT_ERROR_INFO (info);
1658 return rc;
1659 }
1660 if (details == VMINFO_MACHINEREADABLE)
1661 RTPrintf("VideoMode=\"%d,%d,%d\"\n", xRes, yRes, bpp);
1662 else
1663 RTPrintf("Video mode: %dx%dx%d\n", xRes, yRes, bpp);
1664 }
1665 while (0);
1666 }
1667
1668 /*
1669 * VRDP
1670 */
1671 ComPtr<IVRDPServer> vrdpServer;
1672 rc = machine->COMGETTER(VRDPServer)(vrdpServer.asOutParam());
1673 if (SUCCEEDED(rc) && vrdpServer)
1674 {
1675 BOOL fEnabled = false;
1676 vrdpServer->COMGETTER(Enabled)(&fEnabled);
1677 if (fEnabled)
1678 {
1679 ULONG port;
1680 vrdpServer->COMGETTER(Port)(&port);
1681 Bstr address;
1682 vrdpServer->COMGETTER(NetAddress)(address.asOutParam());
1683 BOOL fMultiCon;
1684 vrdpServer->COMGETTER(AllowMultiConnection)(&fMultiCon);
1685 BOOL fReuseCon;
1686 vrdpServer->COMGETTER(ReuseSingleConnection)(&fReuseCon);
1687 VRDPAuthType_T vrdpAuthType;
1688 const char *strAuthType;
1689 vrdpServer->COMGETTER(AuthType)(&vrdpAuthType);
1690 switch (vrdpAuthType)
1691 {
1692 case VRDPAuthType_Null:
1693 strAuthType = "null";
1694 break;
1695 case VRDPAuthType_External:
1696 strAuthType = "external";
1697 break;
1698 case VRDPAuthType_Guest:
1699 strAuthType = "guest";
1700 break;
1701 default:
1702 strAuthType = "unknown";
1703 break;
1704 }
1705 if (details == VMINFO_MACHINEREADABLE)
1706 {
1707 RTPrintf("vrdp=\"on\"\n");
1708 RTPrintf("vrdpport=%d\n", port);
1709 RTPrintf("vrdpaddress=\"%lS\"\n", address.raw());
1710 RTPrintf("vrdpauthtype=\"%s\"\n", strAuthType);
1711 RTPrintf("vrdpmulticon=\"%s\"\n", fMultiCon ? "on" : "off");
1712 RTPrintf("vrdpreusecon=\"%s\"\n", fReuseCon ? "on" : "off");
1713 }
1714 else
1715 {
1716 if (address.isEmpty())
1717 address = "0.0.0.0";
1718 RTPrintf("VRDP: enabled (Address %lS, Port %d, MultiConn: %s, ReuseSingleConn: %s, Authentication type: %s)\n", address.raw(), port, fMultiCon ? "on" : "off", fReuseCon ? "on" : "off", strAuthType);
1719 }
1720 }
1721 else
1722 {
1723 if (details == VMINFO_MACHINEREADABLE)
1724 RTPrintf("vrdp=\"off\"\n");
1725 else
1726 RTPrintf("VRDP: disabled\n");
1727 }
1728 }
1729
1730 /*
1731 * USB.
1732 */
1733 ComPtr<IUSBController> USBCtl;
1734 rc = machine->COMGETTER(USBController)(USBCtl.asOutParam());
1735 if (SUCCEEDED(rc))
1736 {
1737 BOOL fEnabled;
1738 rc = USBCtl->COMGETTER(Enabled)(&fEnabled);
1739 if (FAILED(rc))
1740 fEnabled = false;
1741 if (details == VMINFO_MACHINEREADABLE)
1742 RTPrintf("usb=\"%s\"\n", fEnabled ? "on" : "off");
1743 else
1744 RTPrintf("USB: %s\n", fEnabled ? "enabled" : "disabled");
1745
1746 if (details != VMINFO_MACHINEREADABLE)
1747 RTPrintf("\nUSB Device Filters:\n\n");
1748
1749 ComPtr<IUSBDeviceFilterCollection> Coll;
1750 CHECK_ERROR_RET (USBCtl, COMGETTER(DeviceFilters)(Coll.asOutParam()), rc);
1751
1752 ComPtr<IUSBDeviceFilterEnumerator> Enum;
1753 CHECK_ERROR_RET (Coll, Enumerate(Enum.asOutParam()), rc);
1754
1755 ULONG index = 0;
1756 BOOL fMore = FALSE;
1757 rc = Enum->HasMore (&fMore);
1758 ASSERT_RET (SUCCEEDED (rc), rc);
1759
1760 if (!fMore)
1761 {
1762 if (details != VMINFO_MACHINEREADABLE)
1763 RTPrintf("<none>\n\n");
1764 }
1765 else
1766 while (fMore)
1767 {
1768 ComPtr<IUSBDeviceFilter> DevPtr;
1769 rc = Enum->GetNext(DevPtr.asOutParam());
1770 ASSERT_RET (SUCCEEDED (rc), rc);
1771
1772 /* Query info. */
1773
1774 if (details != VMINFO_MACHINEREADABLE)
1775 RTPrintf("Index: %lu\n", index);
1776
1777 BOOL bActive = FALSE;
1778 CHECK_ERROR_RET (DevPtr, COMGETTER (Active) (&bActive), rc);
1779 if (details == VMINFO_MACHINEREADABLE)
1780 RTPrintf("USBFilterActive%d=\"%s\"\n", index + 1, bActive ? "on" : "off");
1781 else
1782 RTPrintf("Active: %s\n", bActive ? "yes" : "no");
1783
1784 Bstr bstr;
1785 CHECK_ERROR_RET (DevPtr, COMGETTER (Name) (bstr.asOutParam()), rc);
1786 if (details == VMINFO_MACHINEREADABLE)
1787 RTPrintf("USBFilterName%d=\"%lS\"\n", index + 1, bstr.raw());
1788 else
1789 RTPrintf("Name: %lS\n", bstr.raw());
1790 CHECK_ERROR_RET (DevPtr, COMGETTER (VendorId) (bstr.asOutParam()), rc);
1791 if (details == VMINFO_MACHINEREADABLE)
1792 RTPrintf("USBFilterVendorId%d=\"%lS\"\n", index + 1, bstr.raw());
1793 else
1794 RTPrintf("VendorId: %lS\n", bstr.raw());
1795 CHECK_ERROR_RET (DevPtr, COMGETTER (ProductId) (bstr.asOutParam()), rc);
1796 if (details == VMINFO_MACHINEREADABLE)
1797 RTPrintf("USBFilterProductId%d=\"%lS\"\n", index + 1, bstr.raw());
1798 else
1799 RTPrintf("ProductId: %lS\n", bstr.raw());
1800 CHECK_ERROR_RET (DevPtr, COMGETTER (Revision) (bstr.asOutParam()), rc);
1801 if (details == VMINFO_MACHINEREADABLE)
1802 RTPrintf("USBFilterRevision%d=\"%lS\"\n", index + 1, bstr.raw());
1803 else
1804 RTPrintf("Revision: %lS\n", bstr.raw());
1805 CHECK_ERROR_RET (DevPtr, COMGETTER (Manufacturer) (bstr.asOutParam()), rc);
1806 if (details == VMINFO_MACHINEREADABLE)
1807 RTPrintf("USBFilterManufacturer%d=\"%lS\"\n", index + 1, bstr.raw());
1808 else
1809 RTPrintf("Manufacturer: %lS\n", bstr.raw());
1810 CHECK_ERROR_RET (DevPtr, COMGETTER (Product) (bstr.asOutParam()), rc);
1811 if (details == VMINFO_MACHINEREADABLE)
1812 RTPrintf("USBFilterProduct%d=\"%lS\"\n", index + 1, bstr.raw());
1813 else
1814 RTPrintf("Product: %lS\n", bstr.raw());
1815 CHECK_ERROR_RET (DevPtr, COMGETTER (Remote) (bstr.asOutParam()), rc);
1816 if (details == VMINFO_MACHINEREADABLE)
1817 RTPrintf("USBFilterRemote%d=\"%lS\"\n", index + 1, bstr.raw());
1818 else
1819 RTPrintf("Remote: %lS\n", bstr.raw());
1820 CHECK_ERROR_RET (DevPtr, COMGETTER (SerialNumber) (bstr.asOutParam()), rc);
1821 if (details == VMINFO_MACHINEREADABLE)
1822 RTPrintf("USBFilterSerialNumber%d=\"%lS\"\n", index + 1, bstr.raw());
1823 else
1824 RTPrintf("Serial Number: %lS\n", bstr.raw());
1825 if (details != VMINFO_MACHINEREADABLE)
1826 {
1827 ULONG fMaskedIfs;
1828 CHECK_ERROR_RET (DevPtr, COMGETTER (MaskedInterfaces) (&fMaskedIfs), rc);
1829 if (fMaskedIfs)
1830 RTPrintf("Masked Interfaces: 0x%08x\n", fMaskedIfs);
1831 RTPrintf("\n");
1832 }
1833
1834 rc = Enum->HasMore (&fMore);
1835 ASSERT_RET (SUCCEEDED (rc), rc);
1836
1837 index ++;
1838 }
1839
1840 if (console)
1841 {
1842 index = 0;
1843 /* scope */
1844 {
1845 if (details != VMINFO_MACHINEREADABLE)
1846 RTPrintf("Available remote USB devices:\n\n");
1847
1848 ComPtr<IHostUSBDeviceCollection> coll;
1849 CHECK_ERROR_RET (console, COMGETTER(RemoteUSBDevices) (coll.asOutParam()), rc);
1850
1851 ComPtr <IHostUSBDeviceEnumerator> en;
1852 CHECK_ERROR_RET (coll, Enumerate (en.asOutParam()), rc);
1853
1854 BOOL more = FALSE;
1855 rc = en->HasMore (&more);
1856 ASSERT_RET (SUCCEEDED (rc), rc);
1857
1858 if (!more)
1859 {
1860 if (details != VMINFO_MACHINEREADABLE)
1861 RTPrintf("<none>\n\n");
1862 }
1863 else
1864 while (more)
1865 {
1866 ComPtr <IHostUSBDevice> dev;
1867 rc = en->GetNext (dev.asOutParam());
1868 ASSERT_RET (SUCCEEDED (rc), rc);
1869
1870 /* Query info. */
1871 Guid id;
1872 CHECK_ERROR_RET (dev, COMGETTER(Id)(id.asOutParam()), rc);
1873 USHORT usVendorId;
1874 CHECK_ERROR_RET (dev, COMGETTER(VendorId)(&usVendorId), rc);
1875 USHORT usProductId;
1876 CHECK_ERROR_RET (dev, COMGETTER(ProductId)(&usProductId), rc);
1877 USHORT bcdRevision;
1878 CHECK_ERROR_RET (dev, COMGETTER(Revision)(&bcdRevision), rc);
1879
1880 if (details == VMINFO_MACHINEREADABLE)
1881 RTPrintf("USBRemoteUUID%d=\"%S\"\n"
1882 "USBRemoteVendorId%d=\"%#06x\"\n"
1883 "USBRemoteProductId%d=\"%#06x\"\n"
1884 "USBRemoteRevision%d=\"%#04x%02x\"\n",
1885 index + 1, id.toString().raw(),
1886 index + 1, usVendorId,
1887 index + 1, usProductId,
1888 index + 1, bcdRevision >> 8, bcdRevision & 0xff);
1889 else
1890 RTPrintf("UUID: %S\n"
1891 "VendorId: 0x%04x (%04X)\n"
1892 "ProductId: 0x%04x (%04X)\n"
1893 "Revision: %u.%u (%02u%02u)\n",
1894 id.toString().raw(),
1895 usVendorId, usVendorId, usProductId, usProductId,
1896 bcdRevision >> 8, bcdRevision & 0xff,
1897 bcdRevision >> 8, bcdRevision & 0xff);
1898
1899 /* optional stuff. */
1900 Bstr bstr;
1901 CHECK_ERROR_RET (dev, COMGETTER(Manufacturer)(bstr.asOutParam()), rc);
1902 if (!bstr.isEmpty())
1903 {
1904 if (details == VMINFO_MACHINEREADABLE)
1905 RTPrintf("USBRemoteManufacturer%d=\"%lS\"\n", index + 1, bstr.raw());
1906 else
1907 RTPrintf("Manufacturer: %lS\n", bstr.raw());
1908 }
1909 CHECK_ERROR_RET (dev, COMGETTER(Product)(bstr.asOutParam()), rc);
1910 if (!bstr.isEmpty())
1911 {
1912 if (details == VMINFO_MACHINEREADABLE)
1913 RTPrintf("USBRemoteProduct%d=\"%lS\"\n", index + 1, bstr.raw());
1914 else
1915 RTPrintf("Product: %lS\n", bstr.raw());
1916 }
1917 CHECK_ERROR_RET (dev, COMGETTER(SerialNumber)(bstr.asOutParam()), rc);
1918 if (!bstr.isEmpty())
1919 {
1920 if (details == VMINFO_MACHINEREADABLE)
1921 RTPrintf("USBRemoteSerialNumber%d=\"%lS\"\n", index + 1, bstr.raw());
1922 else
1923 RTPrintf("SerialNumber: %lS\n", bstr.raw());
1924 }
1925 CHECK_ERROR_RET (dev, COMGETTER(Address)(bstr.asOutParam()), rc);
1926 if (!bstr.isEmpty())
1927 {
1928 if (details == VMINFO_MACHINEREADABLE)
1929 RTPrintf("USBRemoteAddress%d=\"%lS\"\n", index + 1, bstr.raw());
1930 else
1931 RTPrintf("Address: %lS\n", bstr.raw());
1932 }
1933
1934 if (details != VMINFO_MACHINEREADABLE)
1935 RTPrintf("\n");
1936
1937 rc = en->HasMore (&more);
1938 ASSERT_RET (SUCCEEDED (rc), rc);
1939
1940 index ++;
1941 }
1942 }
1943
1944 index = 0;
1945 /* scope */
1946 {
1947 if (details != VMINFO_MACHINEREADABLE)
1948 RTPrintf ("Currently Attached USB Devices:\n\n");
1949
1950 ComPtr <IUSBDeviceCollection> coll;
1951 CHECK_ERROR_RET (console, COMGETTER(USBDevices) (coll.asOutParam()), rc);
1952
1953 ComPtr <IUSBDeviceEnumerator> en;
1954 CHECK_ERROR_RET (coll, Enumerate (en.asOutParam()), rc);
1955
1956 BOOL more = FALSE;
1957 rc = en->HasMore (&more);
1958 ASSERT_RET (SUCCEEDED (rc), rc);
1959
1960 if (!more)
1961 {
1962 if (details != VMINFO_MACHINEREADABLE)
1963 RTPrintf("<none>\n\n");
1964 }
1965 else
1966 while (more)
1967 {
1968 ComPtr <IUSBDevice> dev;
1969 rc = en->GetNext (dev.asOutParam());
1970 ASSERT_RET (SUCCEEDED (rc), rc);
1971
1972 /* Query info. */
1973 Guid id;
1974 CHECK_ERROR_RET (dev, COMGETTER(Id)(id.asOutParam()), rc);
1975 USHORT usVendorId;
1976 CHECK_ERROR_RET (dev, COMGETTER(VendorId)(&usVendorId), rc);
1977 USHORT usProductId;
1978 CHECK_ERROR_RET (dev, COMGETTER(ProductId)(&usProductId), rc);
1979 USHORT bcdRevision;
1980 CHECK_ERROR_RET (dev, COMGETTER(Revision)(&bcdRevision), rc);
1981
1982 if (details == VMINFO_MACHINEREADABLE)
1983 RTPrintf("USBAttachedUUID%d=\"%S\"\n"
1984 "USBAttachedVendorId%d=\"%#06x\"\n"
1985 "USBAttachedProductId%d=\"%#06x\"\n"
1986 "USBAttachedRevision%d=\"%#04x%02x\"\n",
1987 index + 1, id.toString().raw(),
1988 index + 1, usVendorId,
1989 index + 1, usProductId,
1990 index + 1, bcdRevision >> 8, bcdRevision & 0xff);
1991 else
1992 RTPrintf("UUID: %S\n"
1993 "VendorId: 0x%04x (%04X)\n"
1994 "ProductId: 0x%04x (%04X)\n"
1995 "Revision: %u.%u (%02u%02u)\n",
1996 id.toString().raw(),
1997 usVendorId, usVendorId, usProductId, usProductId,
1998 bcdRevision >> 8, bcdRevision & 0xff,
1999 bcdRevision >> 8, bcdRevision & 0xff);
2000
2001 /* optional stuff. */
2002 Bstr bstr;
2003 CHECK_ERROR_RET (dev, COMGETTER(Manufacturer)(bstr.asOutParam()), rc);
2004 if (!bstr.isEmpty())
2005 {
2006 if (details == VMINFO_MACHINEREADABLE)
2007 RTPrintf("USBAttachedManufacturer%d=\"%lS\"\n", index + 1, bstr.raw());
2008 else
2009 RTPrintf("Manufacturer: %lS\n", bstr.raw());
2010 }
2011 CHECK_ERROR_RET (dev, COMGETTER(Product)(bstr.asOutParam()), rc);
2012 if (!bstr.isEmpty())
2013 {
2014 if (details == VMINFO_MACHINEREADABLE)
2015 RTPrintf("USBAttachedProduct%d=\"%lS\"\n", index + 1, bstr.raw());
2016 else
2017 RTPrintf("Product: %lS\n", bstr.raw());
2018 }
2019 CHECK_ERROR_RET (dev, COMGETTER(SerialNumber)(bstr.asOutParam()), rc);
2020 if (!bstr.isEmpty())
2021 {
2022 if (details == VMINFO_MACHINEREADABLE)
2023 RTPrintf("USBAttachedSerialNumber%d=\"%lS\"\n", index + 1, bstr.raw());
2024 else
2025 RTPrintf("SerialNumber: %lS\n", bstr.raw());
2026 }
2027 CHECK_ERROR_RET (dev, COMGETTER(Address)(bstr.asOutParam()), rc);
2028 if (!bstr.isEmpty())
2029 {
2030 if (details == VMINFO_MACHINEREADABLE)
2031 RTPrintf("USBAttachedAddress%d=\"%lS\"\n", index + 1, bstr.raw());
2032 else
2033 RTPrintf("Address: %lS\n", bstr.raw());
2034 }
2035
2036 if (details != VMINFO_MACHINEREADABLE)
2037 RTPrintf("\n");
2038
2039 rc = en->HasMore (&more);
2040 ASSERT_RET (SUCCEEDED (rc), rc);
2041
2042 index ++;
2043 }
2044 }
2045 }
2046 } /* USB */
2047
2048 /*
2049 * Shared folders
2050 */
2051 if (details != VMINFO_MACHINEREADABLE)
2052 RTPrintf("Shared folders: ");
2053 uint32_t numSharedFolders = 0;
2054#if 0 // not yet implemented
2055 /* globally shared folders first */
2056 {
2057 ComPtr<ISharedFolderCollection> sfColl;
2058 ComPtr<ISharedFolderEnumerator> sfEnum;
2059 CHECK_ERROR_RET(virtualBox, COMGETTER(SharedFolders)(sfColl.asOutParam()), rc);
2060 CHECK_ERROR_RET(sfColl, Enumerate(sfEnum.asOutParam()), rc);
2061 BOOL fMore;
2062 sfEnum->HasMore(&fMore);
2063 while (fMore)
2064 {
2065 ComPtr<ISharedFolder> sf;
2066 CHECK_ERROR_RET(sfEnum, GetNext(sf.asOutParam()), rc);
2067 Bstr name, hostPath;
2068 sf->COMGETTER(Name)(name.asOutParam());
2069 sf->COMGETTER(HostPath)(hostPath.asOutParam());
2070 RTPrintf("Name: '%lS', Host path: '%lS' (global mapping)\n", name.raw(), hostPath.raw());
2071 ++numSharedFolders;
2072 CHECK_ERROR_RET(sfEnum, HasMore(&fMore), rc);
2073 }
2074 }
2075#endif
2076 /* now VM mappings */
2077 {
2078 ComPtr<ISharedFolderCollection> sfColl;
2079 ComPtr<ISharedFolderEnumerator> sfEnum;
2080 CHECK_ERROR_RET(machine, COMGETTER(SharedFolders)(sfColl.asOutParam()), rc);
2081 CHECK_ERROR_RET(sfColl, Enumerate(sfEnum.asOutParam()), rc);
2082 ULONG index = 0;
2083 BOOL fMore;
2084 sfEnum->HasMore(&fMore);
2085 while (fMore)
2086 {
2087 ComPtr<ISharedFolder> sf;
2088 CHECK_ERROR_RET(sfEnum, GetNext(sf.asOutParam()), rc);
2089 Bstr name, hostPath;
2090 BOOL writable;
2091 sf->COMGETTER(Name)(name.asOutParam());
2092 sf->COMGETTER(HostPath)(hostPath.asOutParam());
2093 sf->COMGETTER(Writable)(&writable);
2094 if (!numSharedFolders && details != VMINFO_MACHINEREADABLE)
2095 RTPrintf("\n\n");
2096 if (details == VMINFO_MACHINEREADABLE)
2097 {
2098 RTPrintf("SharedFolderNameMachineMapping%d=\"%lS\"\n", index + 1,
2099 name.raw());
2100 RTPrintf("SharedFolderPathMachineMapping%d=\"%lS\"\n", index + 1,
2101 hostPath.raw());
2102 }
2103 else
2104 RTPrintf("Name: '%lS', Host path: '%lS' (machine mapping), %s\n",
2105 name.raw(), hostPath.raw(), writable ? "writable" : "readonly");
2106 ++numSharedFolders;
2107 CHECK_ERROR_RET(sfEnum, HasMore(&fMore), rc);
2108 }
2109 }
2110 /* transient mappings */
2111 if (console)
2112 {
2113 ComPtr<ISharedFolderCollection> sfColl;
2114 ComPtr<ISharedFolderEnumerator> sfEnum;
2115 CHECK_ERROR_RET(console, COMGETTER(SharedFolders)(sfColl.asOutParam()), rc);
2116 CHECK_ERROR_RET(sfColl, Enumerate(sfEnum.asOutParam()), rc);
2117 ULONG index = 0;
2118 BOOL fMore;
2119 sfEnum->HasMore(&fMore);
2120 while (fMore)
2121 {
2122 ComPtr<ISharedFolder> sf;
2123 CHECK_ERROR_RET(sfEnum, GetNext(sf.asOutParam()), rc);
2124 Bstr name, hostPath;
2125 sf->COMGETTER(Name)(name.asOutParam());
2126 sf->COMGETTER(HostPath)(hostPath.asOutParam());
2127 if (!numSharedFolders && details != VMINFO_MACHINEREADABLE)
2128 RTPrintf("\n\n");
2129 if (details == VMINFO_MACHINEREADABLE)
2130 {
2131 RTPrintf("SharedFolderNameTransientMapping%d=\"%lS\"\n", index + 1,
2132 name.raw());
2133 RTPrintf("SharedFolderPathTransientMapping%d=\"%lS\"\n", index + 1,
2134 hostPath.raw());
2135 }
2136 else
2137 RTPrintf("Name: '%lS', Host path: '%lS' (transient mapping)\n", name.raw(), hostPath.raw());
2138 ++numSharedFolders;
2139 CHECK_ERROR_RET(sfEnum, HasMore(&fMore), rc);
2140 }
2141 }
2142 if (!numSharedFolders && details != VMINFO_MACHINEREADABLE)
2143 RTPrintf("<none>\n");
2144 if (details != VMINFO_MACHINEREADABLE)
2145 RTPrintf("\n");
2146
2147 if (console)
2148 {
2149 /*
2150 * Live VRDP info.
2151 */
2152 ComPtr<IRemoteDisplayInfo> remoteDisplayInfo;
2153 CHECK_ERROR_RET(console, COMGETTER(RemoteDisplayInfo)(remoteDisplayInfo.asOutParam()), rc);
2154 BOOL Active;
2155 ULONG NumberOfClients;
2156 LONG64 BeginTime;
2157 LONG64 EndTime;
2158 ULONG64 BytesSent;
2159 ULONG64 BytesSentTotal;
2160 ULONG64 BytesReceived;
2161 ULONG64 BytesReceivedTotal;
2162 Bstr User;
2163 Bstr Domain;
2164 Bstr ClientName;
2165 Bstr ClientIP;
2166 ULONG ClientVersion;
2167 ULONG EncryptionStyle;
2168
2169 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(Active) (&Active), rc);
2170 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(NumberOfClients) (&NumberOfClients), rc);
2171 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(BeginTime) (&BeginTime), rc);
2172 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(EndTime) (&EndTime), rc);
2173 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(BytesSent) (&BytesSent), rc);
2174 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(BytesSentTotal) (&BytesSentTotal), rc);
2175 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(BytesReceived) (&BytesReceived), rc);
2176 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(BytesReceivedTotal) (&BytesReceivedTotal), rc);
2177 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(User) (User.asOutParam ()), rc);
2178 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(Domain) (Domain.asOutParam ()), rc);
2179 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(ClientName) (ClientName.asOutParam ()), rc);
2180 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(ClientIP) (ClientIP.asOutParam ()), rc);
2181 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(ClientVersion) (&ClientVersion), rc);
2182 CHECK_ERROR_RET(remoteDisplayInfo, COMGETTER(EncryptionStyle) (&EncryptionStyle), rc);
2183
2184 if (details == VMINFO_MACHINEREADABLE)
2185 RTPrintf("VRDPActiveConnection=\"%s\"\n", Active ? "on": "off");
2186 else
2187 RTPrintf("VRDP Connection: %s\n", Active? "active": "not active");
2188
2189 if (details == VMINFO_MACHINEREADABLE)
2190 RTPrintf("VRDPClients=%d\n", NumberOfClients);
2191 else
2192 RTPrintf("Clients so far: %d\n", NumberOfClients);
2193
2194 if (NumberOfClients > 0)
2195 {
2196 char timestr[128];
2197
2198 if (Active)
2199 {
2200 makeTimeStr (timestr, sizeof (timestr), BeginTime);
2201 if (details == VMINFO_MACHINEREADABLE)
2202 RTPrintf("VRDPStartTime=\"%s\"\n", timestr);
2203 else
2204 RTPrintf("Start time: %s\n", timestr);
2205 }
2206 else
2207 {
2208 makeTimeStr (timestr, sizeof (timestr), BeginTime);
2209 if (details == VMINFO_MACHINEREADABLE)
2210 RTPrintf("VRDPLastStartTime=\"%s\"\n", timestr);
2211 else
2212 RTPrintf("Last started: %s\n", timestr);
2213 makeTimeStr (timestr, sizeof (timestr), EndTime);
2214 if (details == VMINFO_MACHINEREADABLE)
2215 RTPrintf("VRDPLastEndTime=\"%s\"\n", timestr);
2216 else
2217 RTPrintf("Last ended: %s\n", timestr);
2218 }
2219
2220 if (details == VMINFO_MACHINEREADABLE)
2221 {
2222 RTPrintf("VRDPBytesSent=%llu\n", BytesSent);
2223 RTPrintf("VRDPThroughputSend=%llu\n", (BytesSent * 1000) / (EndTime - BeginTime) );
2224 RTPrintf("VRDPBytesSentTotal=%llu\n", BytesSentTotal);
2225
2226 RTPrintf("VRDPBytesReceived=%llu\n", BytesReceived);
2227 RTPrintf("VRDPThroughputReceive=%llu\n", (BytesReceived * 1000) / (EndTime - BeginTime) );
2228 RTPrintf("VRDPBytesReceivedTotal=%llu\n", BytesReceivedTotal);
2229 }
2230 else
2231 {
2232 RTPrintf("Sent: %llu Bytes\n", BytesSent);
2233 RTPrintf("Average speed: %llu B/s\n", (BytesSent * 1000) / (EndTime - BeginTime) );
2234 RTPrintf("Sent total: %llu Bytes\n", BytesSentTotal);
2235
2236 RTPrintf("Received: %llu Bytes\n", BytesReceived);
2237 RTPrintf("Speed: %llu B/s\n", (BytesReceived * 1000) / (EndTime - BeginTime) );
2238 RTPrintf("Received total: %llu Bytes\n", BytesReceivedTotal);
2239 }
2240
2241 if (Active)
2242 {
2243 if (details == VMINFO_MACHINEREADABLE)
2244 {
2245 RTPrintf("VRDPUserName=\"%lS\"\n", User.raw());
2246 RTPrintf("VRDPDomain=\"%lS\"\n", Domain.raw());
2247 RTPrintf("VRDPClientName=\"%lS\"\n", ClientName.raw());
2248 RTPrintf("VRDPClientIP=\"%lS\"\n", ClientIP.raw());
2249 RTPrintf("VRDPClientVersion=%d\n", ClientVersion);
2250 RTPrintf("VRDPEncryption=\"%s\"\n", EncryptionStyle == 0? "RDP4": "RDP5 (X.509)");
2251 }
2252 else
2253 {
2254 RTPrintf("User name: %lS\n", User.raw());
2255 RTPrintf("Domain: %lS\n", Domain.raw());
2256 RTPrintf("Client name: %lS\n", ClientName.raw());
2257 RTPrintf("Client IP: %lS\n", ClientIP.raw());
2258 RTPrintf("Client version: %d\n", ClientVersion);
2259 RTPrintf("Encryption: %s\n", EncryptionStyle == 0? "RDP4": "RDP5 (X.509)");
2260 }
2261 }
2262 }
2263
2264 if (details != VMINFO_MACHINEREADABLE)
2265 RTPrintf("\n");
2266 }
2267
2268 if ( details == VMINFO_STANDARD
2269 || details == VMINFO_FULL
2270 || details == VMINFO_MACHINEREADABLE)
2271 {
2272 Bstr description;
2273 machine->COMGETTER(Description)(description.asOutParam());
2274 if (!description.isEmpty())
2275 {
2276 if (details == VMINFO_MACHINEREADABLE)
2277 RTPrintf("description=\"%lS\"\n", description.raw());
2278 else
2279 RTPrintf("Description:\n%lS\n", description.raw());
2280 }
2281 }
2282
2283 ULONG guestVal;
2284 if (details != VMINFO_MACHINEREADABLE)
2285 RTPrintf("Guest:\n\n");
2286
2287#ifdef VBOX_WITH_MEM_BALLOONING
2288 rc = machine->COMGETTER(MemoryBalloonSize)(&guestVal);
2289 if (SUCCEEDED(rc))
2290 {
2291 if (details == VMINFO_MACHINEREADABLE)
2292 RTPrintf("GuestMemoryBalloon=%d\n", guestVal);
2293 else
2294 RTPrintf("Configured memory balloon size: %d MB\n", guestVal);
2295 }
2296#endif
2297 rc = machine->COMGETTER(StatisticsUpdateInterval)(&guestVal);
2298 if (SUCCEEDED(rc))
2299 {
2300 if (details == VMINFO_MACHINEREADABLE)
2301 RTPrintf("GuestStatisticsUpdateInterval=%d\n", guestVal);
2302 else
2303 {
2304 if (guestVal == 0)
2305 RTPrintf("Statistics update: disabled\n");
2306 else
2307 RTPrintf("Statistics update interval: %d seconds\n", guestVal);
2308 }
2309 }
2310 if (details != VMINFO_MACHINEREADABLE)
2311 RTPrintf("\n");
2312
2313 if ( console
2314 && ( details == VMINFO_STATISTICS
2315 || details == VMINFO_FULL
2316 || details == VMINFO_MACHINEREADABLE))
2317 {
2318 ComPtr <IGuest> guest;
2319
2320 rc = console->COMGETTER(Guest)(guest.asOutParam());
2321 if (SUCCEEDED(rc))
2322 {
2323 ULONG statVal;
2324
2325 rc = guest->GetStatistic(0, GuestStatisticType_SampleNumber, &statVal);
2326 if (details == VMINFO_MACHINEREADABLE)
2327 RTPrintf("StatGuestSample=%d\n", statVal);
2328 else
2329 RTPrintf("Guest statistics for sample %d:\n\n", statVal);
2330
2331 rc = guest->GetStatistic(0, GuestStatisticType_CPULoad_Idle, &statVal);
2332 if (SUCCEEDED(rc))
2333 {
2334 if (details == VMINFO_MACHINEREADABLE)
2335 RTPrintf("StatGuestLoadIdleCPU%d=%d\n", 0, statVal);
2336 else
2337 RTPrintf("CPU%d: CPU Load Idle %-3d%%\n", 0, statVal);
2338 }
2339
2340 rc = guest->GetStatistic(0, GuestStatisticType_CPULoad_Kernel, &statVal);
2341 if (SUCCEEDED(rc))
2342 {
2343 if (details == VMINFO_MACHINEREADABLE)
2344 RTPrintf("StatGuestLoadKernelCPU%d=%d\n", 0, statVal);
2345 else
2346 RTPrintf("CPU%d: CPU Load Kernel %-3d%%\n", 0, statVal);
2347 }
2348
2349 rc = guest->GetStatistic(0, GuestStatisticType_CPULoad_User, &statVal);
2350 if (SUCCEEDED(rc))
2351 {
2352 if (details == VMINFO_MACHINEREADABLE)
2353 RTPrintf("StatGuestLoadUserCPU%d=%d\n", 0, statVal);
2354 else
2355 RTPrintf("CPU%d: CPU Load User %-3d%%\n", 0, statVal);
2356 }
2357
2358 rc = guest->GetStatistic(0, GuestStatisticType_Threads, &statVal);
2359 if (SUCCEEDED(rc))
2360 {
2361 if (details == VMINFO_MACHINEREADABLE)
2362 RTPrintf("StatGuestThreadsCPU%d=%d\n", 0, statVal);
2363 else
2364 RTPrintf("CPU%d: Threads %d\n", 0, statVal);
2365 }
2366
2367 rc = guest->GetStatistic(0, GuestStatisticType_Processes, &statVal);
2368 if (SUCCEEDED(rc))
2369 {
2370 if (details == VMINFO_MACHINEREADABLE)
2371 RTPrintf("StatGuestProcessesCPU%d=%d\n", 0, statVal);
2372 else
2373 RTPrintf("CPU%d: Processes %d\n", 0, statVal);
2374 }
2375
2376 rc = guest->GetStatistic(0, GuestStatisticType_Handles, &statVal);
2377 if (SUCCEEDED(rc))
2378 {
2379 if (details == VMINFO_MACHINEREADABLE)
2380 RTPrintf("StatGuestHandlesCPU%d=%d\n", 0, statVal);
2381 else
2382 RTPrintf("CPU%d: Handles %d\n", 0, statVal);
2383 }
2384
2385 rc = guest->GetStatistic(0, GuestStatisticType_MemoryLoad, &statVal);
2386 if (SUCCEEDED(rc))
2387 {
2388 if (details == VMINFO_MACHINEREADABLE)
2389 RTPrintf("StatGuestMemoryLoadCPU%d=%d\n", 0, statVal);
2390 else
2391 RTPrintf("CPU%d: Memory Load %d%%\n", 0, statVal);
2392 }
2393
2394 rc = guest->GetStatistic(0, GuestStatisticType_PhysMemTotal, &statVal);
2395 if (SUCCEEDED(rc))
2396 {
2397 if (details == VMINFO_MACHINEREADABLE)
2398 RTPrintf("StatGuestMemoryTotalPhysCPU%d=%d\n", 0, statVal);
2399 else
2400 RTPrintf("CPU%d: Total physical memory %-4d MB\n", 0, statVal);
2401 }
2402
2403 rc = guest->GetStatistic(0, GuestStatisticType_PhysMemAvailable, &statVal);
2404 if (SUCCEEDED(rc))
2405 {
2406 if (details == VMINFO_MACHINEREADABLE)
2407 RTPrintf("StatGuestMemoryFreePhysCPU%d=%d\n", 0, statVal);
2408 else
2409 RTPrintf("CPU%d: Free physical memory %-4d MB\n", 0, statVal);
2410 }
2411
2412#ifdef VBOX_WITH_MEM_BALLOONING
2413 rc = guest->GetStatistic(0, GuestStatisticType_PhysMemBalloon, &statVal);
2414 if (SUCCEEDED(rc))
2415 {
2416 if (details == VMINFO_MACHINEREADABLE)
2417 RTPrintf("StatGuestMemoryBalloonCPU%d=%d\n", 0, statVal);
2418 else
2419 RTPrintf("CPU%d: Memory balloon size %-4d MB\n", 0, statVal);
2420 }
2421#endif
2422 rc = guest->GetStatistic(0, GuestStatisticType_MemCommitTotal, &statVal);
2423 if (SUCCEEDED(rc))
2424 {
2425 if (details == VMINFO_MACHINEREADABLE)
2426 RTPrintf("StatGuestMemoryCommittedCPU%d=%d\n", 0, statVal);
2427 else
2428 RTPrintf("CPU%d: Committed memory %-4d MB\n", 0, statVal);
2429 }
2430
2431 rc = guest->GetStatistic(0, GuestStatisticType_MemKernelTotal, &statVal);
2432 if (SUCCEEDED(rc))
2433 {
2434 if (details == VMINFO_MACHINEREADABLE)
2435 RTPrintf("StatGuestMemoryTotalKernelCPU%d=%d\n", 0, statVal);
2436 else
2437 RTPrintf("CPU%d: Total kernel memory %-4d MB\n", 0, statVal);
2438 }
2439
2440 rc = guest->GetStatistic(0, GuestStatisticType_MemKernelPaged, &statVal);
2441 if (SUCCEEDED(rc))
2442 {
2443 if (details == VMINFO_MACHINEREADABLE)
2444 RTPrintf("StatGuestMemoryPagedKernelCPU%d=%d\n", 0, statVal);
2445 else
2446 RTPrintf("CPU%d: Paged kernel memory %-4d MB\n", 0, statVal);
2447 }
2448
2449 rc = guest->GetStatistic(0, GuestStatisticType_MemKernelNonpaged, &statVal);
2450 if (SUCCEEDED(rc))
2451 {
2452 if (details == VMINFO_MACHINEREADABLE)
2453 RTPrintf("StatGuestMemoryNonpagedKernelCPU%d=%d\n", 0, statVal);
2454 else
2455 RTPrintf("CPU%d: Nonpaged kernel memory %-4d MB\n", 0, statVal);
2456 }
2457
2458 rc = guest->GetStatistic(0, GuestStatisticType_MemSystemCache, &statVal);
2459 if (SUCCEEDED(rc))
2460 {
2461 if (details == VMINFO_MACHINEREADABLE)
2462 RTPrintf("StatGuestSystemCacheSizeCPU%d=%d\n", 0, statVal);
2463 else
2464 RTPrintf("CPU%d: System cache size %-4d MB\n", 0, statVal);
2465 }
2466
2467 rc = guest->GetStatistic(0, GuestStatisticType_PageFileSize, &statVal);
2468 if (SUCCEEDED(rc))
2469 {
2470 if (details == VMINFO_MACHINEREADABLE)
2471 RTPrintf("StatGuestPageFileSizeCPU%d=%d\n", 0, statVal);
2472 else
2473 RTPrintf("CPU%d: Page file size %-4d MB\n", 0, statVal);
2474 }
2475
2476 RTPrintf("\n");
2477 }
2478 else
2479 {
2480 if (details != VMINFO_MACHINEREADABLE)
2481 {
2482 RTPrintf("[!] FAILED calling console->getGuest at line %d!\n", __LINE__);
2483 PRINT_RC_MESSAGE(rc);
2484 }
2485 }
2486 }
2487
2488 /*
2489 * snapshots
2490 */
2491 ComPtr<ISnapshot> snapshot;
2492 rc = machine->GetSnapshot(Guid(), snapshot.asOutParam());
2493 if (SUCCEEDED(rc) && snapshot)
2494 {
2495 if (details != VMINFO_MACHINEREADABLE)
2496 RTPrintf("Snapshots:\n\n");
2497 showSnapshots(snapshot, details);
2498 }
2499
2500 if (details != VMINFO_MACHINEREADABLE)
2501 RTPrintf("\n");
2502 return S_OK;
2503}
2504
2505static int handleShowVMInfo(int argc, char *argv[],
2506 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
2507{
2508 HRESULT rc;
2509
2510 /* at least one option: the UUID or name of the VM */
2511 if (argc < 1)
2512 return errorSyntax(USAGE_SHOWVMINFO, "Incorrect number of parameters");
2513
2514 /* try to find the given machine */
2515 ComPtr <IMachine> machine;
2516 Guid uuid (argv[0]);
2517 if (!uuid.isEmpty())
2518 {
2519 CHECK_ERROR (virtualBox, GetMachine (uuid, machine.asOutParam()));
2520 }
2521 else
2522 {
2523 CHECK_ERROR (virtualBox, FindMachine (Bstr(argv[0]), machine.asOutParam()));
2524 if (SUCCEEDED (rc))
2525 machine->COMGETTER(Id) (uuid.asOutParam());
2526 }
2527 if (FAILED (rc))
2528 return 1;
2529
2530 /* 2nd option can be -details, -statistics or -argdump */
2531 VMINFO_DETAILS details = VMINFO_NONE;
2532 bool fDetails = false;
2533 bool fStatistics = false;
2534 bool fMachinereadable = false;
2535 for (int i=1;i<argc;i++)
2536 {
2537 if (!strcmp(argv[i], "-details"))
2538 fDetails = true;
2539 else
2540 if (!strcmp(argv[i], "-statistics"))
2541 fStatistics = true;
2542 if (!strcmp(argv[1], "-machinereadable"))
2543 fMachinereadable = true;
2544 }
2545 if (fMachinereadable)
2546 details = VMINFO_MACHINEREADABLE;
2547 else
2548 if (fDetails && fStatistics)
2549 details = VMINFO_FULL;
2550 else
2551 if (fDetails)
2552 details = VMINFO_STANDARD;
2553 else
2554 if (fStatistics)
2555 details = VMINFO_STATISTICS;
2556
2557 ComPtr <IConsole> console;
2558
2559 /* open an existing session for the VM */
2560 rc = virtualBox->OpenExistingSession (session, uuid);
2561 if (SUCCEEDED(rc))
2562 /* get the session machine */
2563 rc = session->COMGETTER(Machine)(machine.asOutParam());
2564 if (SUCCEEDED(rc))
2565 /* get the session console */
2566 rc = session->COMGETTER(Console)(console.asOutParam());
2567
2568 rc = showVMInfo (virtualBox, machine, console, details);
2569
2570 if (console)
2571 session->Close();
2572
2573 return SUCCEEDED (rc) ? 0 : 1;
2574}
2575
2576
2577static int handleList(int argc, char *argv[],
2578 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
2579{
2580 HRESULT rc = S_OK;
2581
2582 /* exactly one option: the object */
2583 if (argc != 1)
2584 return errorSyntax(USAGE_LIST, "Incorrect number of parameters");
2585
2586 /* which object? */
2587 if (strcmp(argv[0], "vms") == 0)
2588 {
2589 /*
2590 * Get the list of all registered VMs
2591 */
2592 ComPtr<IMachineCollection> collection;
2593 rc = virtualBox->COMGETTER(Machines)(collection.asOutParam());
2594 ComPtr<IMachineEnumerator> enumerator;
2595 if (SUCCEEDED(rc))
2596 rc = collection->Enumerate(enumerator.asOutParam());
2597 if (SUCCEEDED(rc))
2598 {
2599 /*
2600 * Iterate through the collection
2601 */
2602 BOOL hasMore = FALSE;
2603 while (enumerator->HasMore(&hasMore), hasMore)
2604 {
2605 ComPtr<IMachine> machine;
2606 rc = enumerator->GetNext(machine.asOutParam());
2607 if ((SUCCEEDED(rc)) && machine)
2608 {
2609 rc = showVMInfo(virtualBox, machine);
2610 }
2611 }
2612 }
2613 }
2614 else
2615 if (strcmp(argv[0], "runningvms") == 0)
2616 {
2617 /*
2618 * Get the list of all _running_ VMs
2619 */
2620 ComPtr<IMachineCollection> collection;
2621 rc = virtualBox->COMGETTER(Machines)(collection.asOutParam());
2622 ComPtr<IMachineEnumerator> enumerator;
2623 if (SUCCEEDED(rc))
2624 rc = collection->Enumerate(enumerator.asOutParam());
2625 if (SUCCEEDED(rc))
2626 {
2627 /*
2628 * Iterate through the collection
2629 */
2630 BOOL hasMore = FALSE;
2631 while (enumerator->HasMore(&hasMore), hasMore)
2632 {
2633 ComPtr<IMachine> machine;
2634 rc = enumerator->GetNext(machine.asOutParam());
2635 if ((SUCCEEDED(rc)) && machine)
2636 {
2637 MachineState_T machineState;
2638 rc = machine->COMGETTER(State)(&machineState);
2639 if (SUCCEEDED(rc))
2640 {
2641 switch (machineState)
2642 {
2643 case MachineState_Running:
2644 case MachineState_Paused:
2645 {
2646 Guid uuid;
2647 rc = machine->COMGETTER(Id) (uuid.asOutParam());
2648 if (SUCCEEDED(rc))
2649 RTPrintf ("%s\n", uuid.toString().raw());
2650 break;
2651 }
2652 }
2653 }
2654 }
2655 }
2656 }
2657 }
2658 else
2659 if (strcmp(argv[0], "ostypes") == 0)
2660 {
2661 ComPtr<IGuestOSTypeCollection> coll;
2662 ComPtr<IGuestOSTypeEnumerator> enumerator;
2663 CHECK_ERROR(virtualBox, COMGETTER(GuestOSTypes)(coll.asOutParam()));
2664 if (SUCCEEDED(rc) && coll)
2665 {
2666 CHECK_ERROR(coll, Enumerate(enumerator.asOutParam()));
2667 BOOL hasMore;
2668 while (SUCCEEDED(enumerator->HasMore(&hasMore)) && hasMore)
2669 {
2670 ComPtr<IGuestOSType> guestOS;
2671 CHECK_RC_BREAK(enumerator->GetNext(guestOS.asOutParam()));
2672 Bstr guestId;
2673 guestOS->COMGETTER(Id)(guestId.asOutParam());
2674 RTPrintf("ID: %lS\n", guestId.raw());
2675 Bstr guestDescription;
2676 guestOS->COMGETTER(Description)(guestDescription.asOutParam());
2677 RTPrintf("Description: %lS\n\n", guestDescription.raw());
2678 }
2679 }
2680 }
2681 else
2682 if (strcmp(argv[0], "hostdvds") == 0)
2683 {
2684 ComPtr<IHost> host;
2685 CHECK_ERROR(virtualBox, COMGETTER(Host)(host.asOutParam()));
2686 ComPtr<IHostDVDDriveCollection> coll;
2687 ComPtr<IHostDVDDriveEnumerator> enumerator;
2688 CHECK_ERROR(host, COMGETTER(DVDDrives)(coll.asOutParam()));
2689 if (SUCCEEDED(rc) && coll)
2690 {
2691 CHECK_ERROR(coll, Enumerate(enumerator.asOutParam()));
2692 BOOL hasMore;
2693 while (SUCCEEDED(enumerator->HasMore(&hasMore)) && hasMore)
2694 {
2695 ComPtr<IHostDVDDrive> dvdDrive;
2696 CHECK_RC_BREAK(enumerator->GetNext(dvdDrive.asOutParam()));
2697 Bstr name;
2698 dvdDrive->COMGETTER(Name)(name.asOutParam());
2699 RTPrintf("Name: %lS\n\n", name.raw());
2700 }
2701 }
2702 }
2703 else
2704 if (strcmp(argv[0], "hostfloppies") == 0)
2705 {
2706 ComPtr<IHost> host;
2707 CHECK_ERROR(virtualBox, COMGETTER(Host)(host.asOutParam()));
2708 ComPtr<IHostFloppyDriveCollection> coll;
2709 ComPtr<IHostFloppyDriveEnumerator> enumerator;
2710 CHECK_ERROR(host, COMGETTER(FloppyDrives)(coll.asOutParam()));
2711 if (SUCCEEDED(rc) && coll)
2712 {
2713 CHECK_ERROR(coll, Enumerate(enumerator.asOutParam()));
2714 BOOL hasMore;
2715 while (SUCCEEDED(enumerator->HasMore(&hasMore)) && hasMore)
2716 {
2717 ComPtr<IHostFloppyDrive> floppyDrive;
2718 CHECK_RC_BREAK(enumerator->GetNext(floppyDrive.asOutParam()));
2719 Bstr name;
2720 floppyDrive->COMGETTER(Name)(name.asOutParam());
2721 RTPrintf("Name: %lS\n\n", name.raw());
2722 }
2723 }
2724 }
2725 else
2726 if (strcmp(argv[0], "hostifs") == 0)
2727 {
2728 ComPtr<IHost> host;
2729 CHECK_ERROR(virtualBox, COMGETTER(Host)(host.asOutParam()));
2730 ComPtr<IHostNetworkInterfaceCollection> coll;
2731 ComPtr<IHostNetworkInterfaceEnumerator> enumerator;
2732 CHECK_ERROR(host, COMGETTER(NetworkInterfaces)(coll.asOutParam()));
2733 if (SUCCEEDED(rc) && coll)
2734 {
2735 CHECK_ERROR(coll, Enumerate(enumerator.asOutParam()));
2736 BOOL hasMore;
2737 while (SUCCEEDED(enumerator->HasMore(&hasMore)) && hasMore)
2738 {
2739 ComPtr<IHostNetworkInterface> networkInterface;
2740 CHECK_RC_BREAK(enumerator->GetNext(networkInterface.asOutParam()));
2741 Bstr interfaceName;
2742 networkInterface->COMGETTER(Name)(interfaceName.asOutParam());
2743 RTPrintf("Name: %lS\n", interfaceName.raw());
2744 Guid interfaceGuid;
2745 networkInterface->COMGETTER(Id)(interfaceGuid.asOutParam());
2746 RTPrintf("GUID: %lS\n\n", Bstr(interfaceGuid.toString()).raw());
2747 }
2748 }
2749 }
2750 else
2751 if (strcmp(argv[0], "hostinfo") == 0)
2752 {
2753 ComPtr<IHost> Host;
2754 CHECK_ERROR (virtualBox, COMGETTER(Host)(Host.asOutParam()));
2755
2756 RTPrintf("Host Information:\n\n");
2757
2758 LONG64 uTCTime = 0;
2759 CHECK_ERROR (Host, COMGETTER(UTCTime)(&uTCTime));
2760 RTTIMESPEC timeSpec;
2761 RTTimeSpecSetMilli(&timeSpec, uTCTime);
2762 char pszTime[30] = {0};
2763 RTTimeSpecToString(&timeSpec, pszTime, sizeof(pszTime));
2764 RTPrintf("Host time: %s\n", pszTime);
2765
2766 ULONG processorOnlineCount = 0;
2767 CHECK_ERROR (Host, COMGETTER(ProcessorOnlineCount)(&processorOnlineCount));
2768 RTPrintf("Processor online count: %lu\n", processorOnlineCount);
2769 ULONG processorCount = 0;
2770 CHECK_ERROR (Host, COMGETTER(ProcessorCount)(&processorCount));
2771 RTPrintf("Processor count: %lu\n", processorCount);
2772 ULONG processorSpeed = 0;
2773 Bstr processorDescription;
2774 for (ULONG i = 0; i < processorCount; i++)
2775 {
2776 CHECK_ERROR (Host, GetProcessorSpeed(i, &processorSpeed));
2777 if (processorSpeed)
2778 RTPrintf("Processor#%u speed: %lu MHz\n", i, processorSpeed);
2779 else
2780 RTPrintf("Processor#%u speed: unknown\n", i, processorSpeed);
2781 #if 0 /* not yet implemented in Main */
2782 CHECK_ERROR (Host, GetProcessorDescription(i, processorDescription.asOutParam()));
2783 RTPrintf("Processor#%u description: %lS\n", i, processorDescription.raw());
2784 #endif
2785 }
2786
2787 #if 0 /* not yet implemented in Main */
2788 ULONG memorySize = 0;
2789 CHECK_ERROR (Host, COMGETTER(MemorySize)(&memorySize));
2790 RTPrintf("Memory size: %lu MByte\n", memorySize);
2791
2792 ULONG memoryAvailable = 0;
2793 CHECK_ERROR (Host, COMGETTER(MemoryAvailable)(&memoryAvailable));
2794 RTPrintf("Memory available: %lu MByte\n", memoryAvailable);
2795
2796 Bstr operatingSystem;
2797 CHECK_ERROR (Host, COMGETTER(OperatingSystem)(operatingSystem.asOutParam()));
2798 RTPrintf("Operating system: %lS\n", operatingSystem.raw());
2799
2800 Bstr oSVersion;
2801 CHECK_ERROR (Host, COMGETTER(OSVersion)(oSVersion.asOutParam()));
2802 RTPrintf("Operating system version: %lS\n", oSVersion.raw());
2803 #endif
2804 }
2805 else
2806 if (strcmp(argv[0], "hdds") == 0)
2807 {
2808 ComPtr<IHardDiskCollection> hddColl;
2809 CHECK_ERROR(virtualBox, COMGETTER(HardDisks)(hddColl.asOutParam()));
2810 ComPtr<IHardDiskEnumerator> enumerator;
2811 CHECK_ERROR(hddColl, Enumerate(enumerator.asOutParam()));
2812 BOOL hasMore;
2813 while (SUCCEEDED(enumerator->HasMore(&hasMore)) && hasMore)
2814 {
2815 ComPtr<IHardDisk> hdd;
2816 CHECK_RC_BREAK(enumerator->GetNext(hdd.asOutParam()));
2817 Guid uuid;
2818 hdd->COMGETTER(Id)(uuid.asOutParam());
2819 RTPrintf("UUID: %s\n", uuid.toString().raw());
2820 HardDiskStorageType_T storageType;
2821 hdd->COMGETTER(StorageType)(&storageType);
2822 const char *storageTypeString = "unknown";
2823 switch (storageType)
2824 {
2825 case HardDiskStorageType_VirtualDiskImage:
2826 storageTypeString = "Virtual Disk Image";
2827 break;
2828 case HardDiskStorageType_ISCSIHardDisk:
2829 storageTypeString = "iSCSI hard disk";
2830 break;
2831 }
2832 RTPrintf("Storage type: %s\n", storageTypeString);
2833 Bstr filepath;
2834 /// @todo (dmik) we temporarily use the location property to
2835 // determine the image file name. This is subject to change
2836 // when iSCSI disks are here (we should either query a
2837 // storage-specific interface from IHardDisk, or "standardize"
2838 // the location property)
2839 hdd->COMGETTER(Location)(filepath.asOutParam());
2840 RTPrintf("Path: %lS\n", filepath.raw());
2841 BOOL fAccessible;
2842 hdd->COMGETTER(AllAccessible)(&fAccessible);
2843 RTPrintf("Accessible: %s\n", fAccessible ? "yes" : "no");
2844 Guid machineUUID;
2845 hdd->COMGETTER(MachineId)(machineUUID.asOutParam());
2846 if (!machineUUID.isEmpty())
2847 {
2848 ComPtr<IMachine> machine;
2849 CHECK_ERROR(virtualBox, GetMachine(machineUUID, machine.asOutParam()));
2850 ASSERT(machine);
2851 Bstr name;
2852 machine->COMGETTER(Name)(name.asOutParam());
2853 machine->COMGETTER(Id)(uuid.asOutParam());
2854 RTPrintf("Usage: %lS (UUID: %s)\n", name.raw(), uuid.toString().raw());
2855 }
2856 RTPrintf("\n");
2857 }
2858 }
2859 else
2860 if (strcmp(argv[0], "dvds") == 0)
2861 {
2862 ComPtr<IDVDImageCollection> dvdColl;
2863 CHECK_ERROR(virtualBox, COMGETTER(DVDImages)(dvdColl.asOutParam()));
2864 ComPtr<IDVDImageEnumerator> enumerator;
2865 CHECK_ERROR(dvdColl, Enumerate(enumerator.asOutParam()));
2866 BOOL hasMore;
2867 while (SUCCEEDED(enumerator->HasMore(&hasMore)) && hasMore)
2868 {
2869 ComPtr<IDVDImage> dvdImage;
2870 CHECK_RC_BREAK(enumerator->GetNext(dvdImage.asOutParam()));
2871 Guid uuid;
2872 dvdImage->COMGETTER(Id)(uuid.asOutParam());
2873 RTPrintf("UUID: %s\n", uuid.toString().raw());
2874 Bstr filePath;
2875 dvdImage->COMGETTER(FilePath)(filePath.asOutParam());
2876 RTPrintf("Path: %lS\n", filePath.raw());
2877 BOOL fAccessible;
2878 dvdImage->COMGETTER(Accessible)(&fAccessible);
2879 RTPrintf("Accessible: %s\n", fAccessible ? "yes" : "no");
2880 Bstr machineUUIDs;
2881 CHECK_ERROR(virtualBox, GetDVDImageUsage(uuid, ResourceUsage_All, machineUUIDs.asOutParam()));
2882 /** @todo usage */
2883 RTPrintf("\n");
2884 }
2885 }
2886 else
2887 if (strcmp(argv[0], "floppies") == 0)
2888 {
2889 ComPtr<IFloppyImageCollection> floppyColl;
2890 CHECK_ERROR(virtualBox, COMGETTER(FloppyImages)(floppyColl.asOutParam()));
2891 ComPtr<IFloppyImageEnumerator> enumerator;
2892 CHECK_ERROR(floppyColl, Enumerate(enumerator.asOutParam()));
2893 BOOL hasMore;
2894 while (SUCCEEDED(enumerator->HasMore(&hasMore)) && hasMore)
2895 {
2896 ComPtr<IFloppyImage> floppyImage;
2897 CHECK_RC_BREAK(enumerator->GetNext(floppyImage.asOutParam()));
2898 Guid uuid;
2899 floppyImage->COMGETTER(Id)(uuid.asOutParam());
2900 RTPrintf("UUID: %s\n", uuid.toString().raw());
2901 Bstr filePath;
2902 floppyImage->COMGETTER(FilePath)(filePath.asOutParam());
2903 RTPrintf("Path: %lS\n", filePath.raw());
2904 BOOL fAccessible;
2905 floppyImage->COMGETTER(Accessible)(&fAccessible);
2906 RTPrintf("Accessible: %s\n", fAccessible ? "yes" : "no");
2907 Bstr machineUUIDs;
2908 CHECK_ERROR(virtualBox, GetFloppyImageUsage(uuid, ResourceUsage_All, machineUUIDs.asOutParam()));
2909 /** @todo usage */
2910 RTPrintf("\n");
2911 }
2912 }
2913 else
2914 if (strcmp(argv[0], "usbhost") == 0)
2915 {
2916 ComPtr<IHost> Host;
2917 CHECK_ERROR_RET (virtualBox, COMGETTER(Host)(Host.asOutParam()), 1);
2918
2919 ComPtr<IHostUSBDeviceCollection> CollPtr;
2920 CHECK_ERROR_RET (Host, COMGETTER(USBDevices)(CollPtr.asOutParam()), 1);
2921
2922 ComPtr<IHostUSBDeviceEnumerator> EnumPtr;
2923 CHECK_ERROR_RET (CollPtr, Enumerate(EnumPtr.asOutParam()), 1);
2924
2925 RTPrintf("Host USB Devices:\n\n");
2926
2927 BOOL fMore = FALSE;
2928 rc = EnumPtr->HasMore (&fMore);
2929 ASSERT_RET (SUCCEEDED (rc), 1);
2930
2931 if (!fMore)
2932 {
2933 RTPrintf("<none>\n\n");
2934 }
2935 else
2936 while (fMore)
2937 {
2938 ComPtr <IHostUSBDevice> dev;
2939 rc = EnumPtr->GetNext (dev.asOutParam());
2940 ASSERT_RET (SUCCEEDED (rc), 1);
2941
2942 /* Query info. */
2943 Guid id;
2944 CHECK_ERROR_RET (dev, COMGETTER(Id)(id.asOutParam()), 1);
2945 USHORT usVendorId;
2946 CHECK_ERROR_RET (dev, COMGETTER(VendorId)(&usVendorId), 1);
2947 USHORT usProductId;
2948 CHECK_ERROR_RET (dev, COMGETTER(ProductId)(&usProductId), 1);
2949 USHORT bcdRevision;
2950 CHECK_ERROR_RET (dev, COMGETTER(Revision)(&bcdRevision), 1);
2951
2952 RTPrintf("UUID: %S\n"
2953 "VendorId: 0x%04x (%04X)\n"
2954 "ProductId: 0x%04x (%04X)\n"
2955 "Revision: %u.%u (%02u%02u)\n",
2956 id.toString().raw(),
2957 usVendorId, usVendorId, usProductId, usProductId,
2958 bcdRevision >> 8, bcdRevision & 0xff,
2959 bcdRevision >> 8, bcdRevision & 0xff);
2960
2961 /* optional stuff. */
2962 Bstr bstr;
2963 CHECK_ERROR_RET (dev, COMGETTER(Manufacturer)(bstr.asOutParam()), 1);
2964 if (!bstr.isEmpty())
2965 RTPrintf("Manufacturer: %lS\n", bstr.raw());
2966 CHECK_ERROR_RET (dev, COMGETTER(Product)(bstr.asOutParam()), 1);
2967 if (!bstr.isEmpty())
2968 RTPrintf("Product: %lS\n", bstr.raw());
2969 CHECK_ERROR_RET (dev, COMGETTER(SerialNumber)(bstr.asOutParam()), 1);
2970 if (!bstr.isEmpty())
2971 RTPrintf("SerialNumber: %lS\n", bstr.raw());
2972 CHECK_ERROR_RET (dev, COMGETTER(Address)(bstr.asOutParam()), 1);
2973 if (!bstr.isEmpty())
2974 RTPrintf("Address: %lS\n", bstr.raw());
2975
2976 /* current state */
2977 USBDeviceState_T state;
2978 CHECK_ERROR_RET (dev, COMGETTER(State)(&state), 1);
2979 const char *pszState = "?";
2980 switch (state)
2981 {
2982 case USBDeviceState_NotSupported:
2983 pszState = "Not supported"; break;
2984 case USBDeviceState_Unavailable:
2985 pszState = "Unavailable"; break;
2986 case USBDeviceState_Busy:
2987 pszState = "Busy"; break;
2988 case USBDeviceState_Available:
2989 pszState = "Available"; break;
2990 case USBDeviceState_Held:
2991 pszState = "Held"; break;
2992 case USBDeviceState_Captured:
2993 pszState = "Captured"; break;
2994 default:
2995 ASSERT (false);
2996 break;
2997 }
2998 RTPrintf("Current State: %s\n\n", pszState);
2999
3000 rc = EnumPtr->HasMore (&fMore);
3001 ASSERT_RET (SUCCEEDED (rc), rc);
3002 }
3003 }
3004 else
3005 if (strcmp(argv[0], "usbfilters") == 0)
3006 {
3007 RTPrintf("Global USB Device Filters:\n\n");
3008
3009 ComPtr <IHost> host;
3010 CHECK_ERROR_RET (virtualBox, COMGETTER(Host) (host.asOutParam()), 1);
3011
3012 ComPtr<IHostUSBDeviceFilterCollection> coll;
3013 CHECK_ERROR_RET (host, COMGETTER (USBDeviceFilters)(coll.asOutParam()), 1);
3014
3015 ComPtr<IHostUSBDeviceFilterEnumerator> en;
3016 CHECK_ERROR_RET (coll, Enumerate(en.asOutParam()), 1);
3017
3018 ULONG index = 0;
3019 BOOL more = FALSE;
3020 rc = en->HasMore (&more);
3021 ASSERT_RET (SUCCEEDED (rc), 1);
3022
3023 if (!more)
3024 {
3025 RTPrintf("<none>\n\n");
3026 }
3027 else
3028 while (more)
3029 {
3030 ComPtr<IHostUSBDeviceFilter> flt;
3031 rc = en->GetNext (flt.asOutParam());
3032 ASSERT_RET (SUCCEEDED (rc), 1);
3033
3034 /* Query info. */
3035
3036 RTPrintf("Index: %lu\n", index);
3037
3038 BOOL active = FALSE;
3039 CHECK_ERROR_RET (flt, COMGETTER (Active) (&active), 1);
3040 RTPrintf("Active: %s\n", active ? "yes" : "no");
3041
3042 USBDeviceFilterAction_T action;
3043 CHECK_ERROR_RET (flt, COMGETTER (Action) (&action), 1);
3044 const char *pszAction = "<invalid>";
3045 switch (action)
3046 {
3047 case USBDeviceFilterAction_Ignore:
3048 pszAction = "Ignore";
3049 break;
3050 case USBDeviceFilterAction_Hold:
3051 pszAction = "Hold";
3052 break;
3053 default:
3054 break;
3055 }
3056 RTPrintf("Action: %s\n", pszAction);
3057
3058 Bstr bstr;
3059 CHECK_ERROR_RET (flt, COMGETTER (Name) (bstr.asOutParam()), 1);
3060 RTPrintf("Name: %lS\n", bstr.raw());
3061 CHECK_ERROR_RET (flt, COMGETTER (VendorId) (bstr.asOutParam()), 1);
3062 RTPrintf("VendorId: %lS\n", bstr.raw());
3063 CHECK_ERROR_RET (flt, COMGETTER (ProductId) (bstr.asOutParam()), 1);
3064 RTPrintf("ProductId: %lS\n", bstr.raw());
3065 CHECK_ERROR_RET (flt, COMGETTER (Revision) (bstr.asOutParam()), 1);
3066 RTPrintf("Revision: %lS\n", bstr.raw());
3067 CHECK_ERROR_RET (flt, COMGETTER (Manufacturer) (bstr.asOutParam()), 1);
3068 RTPrintf("Manufacturer: %lS\n", bstr.raw());
3069 CHECK_ERROR_RET (flt, COMGETTER (Product) (bstr.asOutParam()), 1);
3070 RTPrintf("Product: %lS\n", bstr.raw());
3071 CHECK_ERROR_RET (flt, COMGETTER (SerialNumber) (bstr.asOutParam()), 1);
3072 RTPrintf("Serial Number: %lS\n\n", bstr.raw());
3073
3074 rc = en->HasMore (&more);
3075 ASSERT_RET (SUCCEEDED (rc), 1);
3076
3077 index ++;
3078 }
3079 }
3080 else if (strcmp(argv[0], "systemproperties") == 0)
3081 {
3082 ComPtr<ISystemProperties> systemProperties;
3083 virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
3084
3085 Bstr str;
3086 ULONG ulValue;
3087 ULONG64 ul64Value;
3088 BOOL flag;
3089
3090 systemProperties->COMGETTER(MinGuestRAM)(&ulValue);
3091 RTPrintf("Minimum guest RAM size: %u Megabytes\n", ulValue);
3092 systemProperties->COMGETTER(MaxGuestRAM)(&ulValue);
3093 RTPrintf("Maximum guest RAM size: %u Megabytes\n", ulValue);
3094 systemProperties->COMGETTER(MaxGuestVRAM)(&ulValue);
3095 RTPrintf("Maximum video RAM size: %u Megabytes\n", ulValue);
3096 systemProperties->COMGETTER(MaxVDISize)(&ul64Value);
3097 RTPrintf("Maximum VDI size: %lu Megabytes\n", ul64Value);
3098 systemProperties->COMGETTER(DefaultVDIFolder)(str.asOutParam());
3099 RTPrintf("Default VDI folder: %lS\n", str.raw());
3100 systemProperties->COMGETTER(DefaultMachineFolder)(str.asOutParam());
3101 RTPrintf("Default machine folder: %lS\n", str.raw());
3102 systemProperties->COMGETTER(RemoteDisplayAuthLibrary)(str.asOutParam());
3103 RTPrintf("VRDP authentication library: %lS\n", str.raw());
3104 systemProperties->COMGETTER(WebServiceAuthLibrary)(str.asOutParam());
3105 RTPrintf("Webservice auth. library: %lS\n", str.raw());
3106 systemProperties->COMGETTER(HWVirtExEnabled)(&flag);
3107 RTPrintf("Hardware virt. extensions: %s\n", flag ? "yes" : "no");
3108 systemProperties->COMGETTER(LogHistoryCount)(&ulValue);
3109 RTPrintf("Log history count: %u\n", ulValue);
3110
3111 }
3112 else
3113 return errorSyntax(USAGE_LIST, "Invalid parameter '%s'", Utf8Str(argv[0]).raw());
3114
3115 return SUCCEEDED(rc) ? 0 : 1;
3116}
3117
3118static int handleRegisterVM(int argc, char *argv[],
3119 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
3120{
3121 HRESULT rc;
3122
3123 if (argc != 1)
3124 return errorSyntax(USAGE_REGISTERVM, "Incorrect number of parameters");
3125
3126 ComPtr<IMachine> machine;
3127 CHECK_ERROR(virtualBox, OpenMachine(Bstr(argv[0]), machine.asOutParam()));
3128 if (SUCCEEDED(rc))
3129 {
3130 ASSERT(machine);
3131 CHECK_ERROR(virtualBox, RegisterMachine(machine));
3132 }
3133 return SUCCEEDED(rc) ? 0 : 1;
3134}
3135
3136static int handleUnregisterVM(int argc, char *argv[],
3137 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
3138{
3139 HRESULT rc;
3140
3141 if ((argc != 1) && (argc != 2))
3142 return errorSyntax(USAGE_UNREGISTERVM, "Incorrect number of parameters");
3143
3144 ComPtr<IMachine> machine;
3145 /* assume it's a UUID */
3146 rc = virtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
3147 if (FAILED(rc) || !machine)
3148 {
3149 /* must be a name */
3150 CHECK_ERROR(virtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
3151 }
3152 if (machine)
3153 {
3154 Guid uuid;
3155 machine->COMGETTER(Id)(uuid.asOutParam());
3156 machine = NULL;
3157 CHECK_ERROR(virtualBox, UnregisterMachine(uuid, machine.asOutParam()));
3158 if (SUCCEEDED(rc) && machine)
3159 {
3160 /* are we supposed to delete the config file? */
3161 if ((argc == 2) && (strcmp(argv[1], "-delete") == 0))
3162 {
3163 CHECK_ERROR(machine, DeleteSettings());
3164 }
3165 }
3166 }
3167 return SUCCEEDED(rc) ? 0 : 1;
3168}
3169
3170static int handleCreateVDI(int argc, char *argv[],
3171 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
3172{
3173 HRESULT rc;
3174 Bstr filename;
3175 uint64_t sizeMB = 0;
3176 bool fStatic = false;
3177 Bstr comment;
3178 bool fRegister = false;
3179 const char *type = "normal";
3180
3181 /* let's have a closer look at the arguments */
3182 for (int i = 0; i < argc; i++)
3183 {
3184 if (strcmp(argv[i], "-filename") == 0)
3185 {
3186 if (argc <= i + 1)
3187 return errorArgument("Missing argument to '%s'", argv[i]);
3188 i++;
3189 filename = argv[i];
3190 }
3191 else if (strcmp(argv[i], "-size") == 0)
3192 {
3193 if (argc <= i + 1)
3194 return errorArgument("Missing argument to '%s'", argv[i]);
3195 i++;
3196 sizeMB = RTStrToUInt64(argv[i]);
3197 }
3198 else if (strcmp(argv[i], "-static") == 0)
3199 {
3200 fStatic = true;
3201 }
3202 else if (strcmp(argv[i], "-comment") == 0)
3203 {
3204 if (argc <= i + 1)
3205 return errorArgument("Missing argument to '%s'", argv[i]);
3206 i++;
3207 comment = argv[i];
3208 }
3209 else if (strcmp(argv[i], "-register") == 0)
3210 {
3211 fRegister = true;
3212 }
3213 else if (strcmp(argv[i], "-type") == 0)
3214 {
3215 if (argc <= i + 1)
3216 return errorArgument("Missing argument to '%s'", argv[i]);
3217 i++;
3218 type = argv[i];
3219 }
3220 else
3221 return errorSyntax(USAGE_CREATEVDI, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
3222 }
3223 /* check the outcome */
3224 if (!filename || (sizeMB == 0))
3225 return errorSyntax(USAGE_CREATEVDI, "Parameters -filename and -size are required");
3226
3227 if (strcmp(type, "normal") && strcmp(type, "writethrough"))
3228 return errorArgument("Invalid VDI type '%s' specified", Utf8Str(type).raw());
3229
3230 ComPtr<IHardDisk> hardDisk;
3231 CHECK_ERROR(virtualBox, CreateHardDisk(HardDiskStorageType_VirtualDiskImage, hardDisk.asOutParam()));
3232 if (SUCCEEDED(rc) && hardDisk)
3233 {
3234 CHECK_ERROR(hardDisk,COMSETTER(Description)(comment));
3235 ComPtr<IVirtualDiskImage> vdi = hardDisk;
3236 CHECK_ERROR(vdi, COMSETTER(FilePath)(filename));
3237 ComPtr<IProgress> progress;
3238 if (fStatic)
3239 {
3240 CHECK_ERROR(vdi, CreateFixedImage(sizeMB, progress.asOutParam()));
3241 }
3242 else
3243 {
3244 CHECK_ERROR(vdi, CreateDynamicImage(sizeMB, progress.asOutParam()));
3245 }
3246 if (SUCCEEDED(rc) && progress)
3247 {
3248 if (fStatic)
3249 showProgress(progress);
3250 else
3251 CHECK_ERROR(progress, WaitForCompletion(-1));
3252 if (SUCCEEDED(rc))
3253 {
3254 progress->COMGETTER(ResultCode)(&rc);
3255 if (FAILED(rc))
3256 {
3257 com::ProgressErrorInfo info(progress);
3258 if (info.isBasicAvailable())
3259 RTPrintf("Error: failed to create disk image. Error message: %lS\n", info.getText().raw());
3260 else
3261 RTPrintf("Error: failed to create disk image. No error message available!\n");
3262 }
3263 else
3264 {
3265 Guid uuid;
3266 CHECK_ERROR(hardDisk, COMGETTER(Id)(uuid.asOutParam()));
3267
3268 if (strcmp(type, "normal") == 0)
3269 {
3270 /* nothing required, default */
3271 }
3272 else if (strcmp(type, "writethrough") == 0)
3273 {
3274 CHECK_ERROR(hardDisk, COMSETTER(Type)(HardDiskType_Writethrough));
3275 }
3276
3277 RTPrintf("Disk image created. UUID: %s\n", uuid.toString().raw());
3278 }
3279 }
3280 }
3281 if (SUCCEEDED(rc) && fRegister)
3282 {
3283 CHECK_ERROR(virtualBox, RegisterHardDisk(hardDisk));
3284 }
3285 }
3286 return SUCCEEDED(rc) ? 0 : 1;
3287}
3288
3289static DECLCALLBACK(int) vdiProgressCallback(PVM pVM, unsigned uPercent, void *pvUser)
3290{
3291 unsigned *pPercent = (unsigned *)pvUser;
3292
3293 if (*pPercent != uPercent)
3294 {
3295 *pPercent = uPercent;
3296 RTPrintf(".");
3297 if ((uPercent % 10) == 0 && uPercent)
3298 RTPrintf("%d%%", uPercent);
3299 RTStrmFlush(g_pStdOut);
3300 }
3301
3302 return VINF_SUCCESS;
3303}
3304
3305
3306static int handleModifyVDI(int argc, char *argv[],
3307 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
3308{
3309 HRESULT rc;
3310
3311 /* The uuid/filename and a command */
3312 if (argc < 2)
3313 return errorSyntax(USAGE_MODIFYVDI, "Incorrect number of parameters");
3314
3315 ComPtr<IHardDisk> hardDisk;
3316 ComPtr<IVirtualDiskImage> vdi;
3317 Bstr filepath;
3318
3319 /* first guess is that it's a UUID */
3320 Guid uuid(argv[0]);
3321 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
3322 /* no? then it must be a filename */
3323 if (!hardDisk)
3324 {
3325 filepath = argv[0];
3326 CHECK_ERROR(virtualBox, FindVirtualDiskImage(filepath, vdi.asOutParam()));
3327 hardDisk = vdi;
3328 }
3329 else
3330 {
3331 vdi = hardDisk;
3332 }
3333
3334 /* let's find out which command */
3335// doesn't currently work if (strcmp(argv[1], "settype") == 0)
3336 if (0)
3337 {
3338 /* hard disk must be registered */
3339 if (SUCCEEDED(rc) && hardDisk && vdi)
3340 {
3341 char *type = NULL;
3342
3343 if (argc <= 2)
3344 return errorArgument("Missing argument to for settype");
3345
3346 type = argv[2];
3347
3348 HardDiskType_T hddType;
3349 CHECK_ERROR(hardDisk, COMGETTER(Type)(&hddType));
3350
3351 if (strcmp(type, "normal") == 0)
3352 {
3353 if (hddType != HardDiskType_Normal)
3354 CHECK_ERROR(hardDisk, COMSETTER(Type)(HardDiskType_Normal));
3355 }
3356 else if (strcmp(type, "writethrough") == 0)
3357 {
3358 if (hddType != HardDiskType_Writethrough)
3359 CHECK_ERROR(hardDisk, COMSETTER(Type)(HardDiskType_Writethrough));
3360
3361 }
3362 else if (strcmp(type, "immutable") == 0)
3363 {
3364 if (hddType != HardDiskType_Immutable)
3365 CHECK_ERROR(hardDisk, COMSETTER(Type)(HardDiskType_Immutable));
3366 }
3367 else
3368 {
3369 return errorArgument("Invalid VDI type '%s' specified", Utf8Str(type).raw());
3370 }
3371 }
3372 else
3373 return errorArgument("Hard disk image not registered");
3374 }
3375 else if (strcmp(argv[1], "compact") == 0)
3376 {
3377 ComPtr<IVirtualDiskImage> vdi;
3378
3379 /* the hard disk image might not be registered */
3380 if (!hardDisk)
3381 {
3382 virtualBox->OpenVirtualDiskImage(Bstr(argv[0]), vdi.asOutParam());
3383 if (!vdi)
3384 return errorArgument("Hard disk image not found");
3385 }
3386 else
3387 vdi = hardDisk;
3388
3389 if (!vdi)
3390 return errorArgument("Invalid hard disk type. The command only works on VDI files\n");
3391
3392 Bstr fileName;
3393 vdi->COMGETTER(FilePath)(fileName.asOutParam());
3394
3395 /* close the file */
3396 hardDisk = NULL;
3397 vdi = NULL;
3398
3399 unsigned uProcent;
3400
3401 RTPrintf("Shrinking '%lS': 0%%", fileName.raw());
3402 int vrc = VDIShrinkImage(Utf8Str(fileName).raw(), vdiProgressCallback, &uProcent);
3403 if (VBOX_FAILURE(vrc))
3404 {
3405 RTPrintf("Error while shrinking hard disk image: %Vrc\n", vrc);
3406 rc = E_FAIL;
3407 }
3408 }
3409 else
3410 return errorSyntax(USAGE_MODIFYVDI, "Invalid parameter '%s'", Utf8Str(argv[1]).raw());
3411
3412 return SUCCEEDED(rc) ? 0 : 1;
3413}
3414
3415static int handleCloneVDI(int argc, char *argv[],
3416 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
3417{
3418 HRESULT rc;
3419
3420 /* source VDI and target path */
3421 if (argc != 2)
3422 return errorSyntax(USAGE_CLONEVDI, "Incorrect number of parameters");
3423
3424 /* first guess is that it's a UUID */
3425 Guid uuid(argv[0]);
3426 ComPtr<IHardDisk> hardDisk;
3427 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
3428 if (!hardDisk)
3429 {
3430 /* not successful? Then it must be a filename */
3431 ComPtr<IVirtualDiskImage> vdi;
3432 CHECK_ERROR(virtualBox, OpenVirtualDiskImage(Bstr(argv[0]), vdi.asOutParam()));
3433 hardDisk = vdi;
3434 }
3435 if (hardDisk)
3436 {
3437 ComPtr<IVirtualDiskImage> vdiOut;
3438 ComPtr<IProgress> progress;
3439 CHECK_ERROR(hardDisk, CloneToImage(Bstr(argv[1]), vdiOut.asOutParam(), progress.asOutParam()));
3440 if (SUCCEEDED(rc))
3441 {
3442 showProgress(progress);
3443 progress->COMGETTER(ResultCode)(&rc);
3444 if (FAILED(rc))
3445 {
3446 com::ProgressErrorInfo info(progress);
3447 if (info.isBasicAvailable())
3448 {
3449 RTPrintf("Error: failed to clone disk image. Error message: %lS\n", info.getText().raw());
3450 }
3451 else
3452 {
3453 RTPrintf("Error: failed to clone disk image. No error message available!\n");
3454 }
3455 }
3456 }
3457 }
3458 return SUCCEEDED(rc) ? 0 : 1;
3459}
3460
3461static int handleConvertDDImage(int argc, char *argv[])
3462{
3463 int arg = 0;
3464 VDIIMAGETYPE enmImgType = VDI_IMAGE_TYPE_NORMAL;
3465 if (argc >= 1 && !strcmp(argv[arg], "-static"))
3466 {
3467 arg++;
3468 enmImgType = VDI_IMAGE_TYPE_FIXED;
3469 }
3470
3471#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
3472 const bool fReadFromStdIn = (argc >= arg + 1) && !strcmp(argv[arg], "stdin");
3473#else
3474 const bool fReadFromStdIn = false;
3475#endif
3476
3477 if ((!fReadFromStdIn && argc != arg + 2) || (fReadFromStdIn && argc != arg + 3))
3478 return errorSyntax(USAGE_CONVERTDD, "Incorrect number of parameters");
3479
3480 RTPrintf("Converting VDI: from DD image file=\"%s\" to file=\"%s\"...\n",
3481 argv[arg], argv[arg + 1]);
3482
3483 /* open raw image file. */
3484 RTFILE File;
3485 int rc = VINF_SUCCESS;
3486 if (fReadFromStdIn)
3487 File = 0;
3488 else
3489 rc = RTFileOpen(&File, argv[arg], RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
3490 if (VBOX_FAILURE(rc))
3491 {
3492 RTPrintf("File=\"%s\" open error: %Rrf\n", argv[arg], rc);
3493 return rc;
3494 }
3495
3496 uint64_t cbFile;
3497 /* get image size. */
3498 if (fReadFromStdIn)
3499 cbFile = RTStrToUInt64(argv[arg + 2]);
3500 else
3501 rc = RTFileGetSize(File, &cbFile);
3502 if (VBOX_SUCCESS(rc))
3503 {
3504 RTPrintf("Creating %s image with size %RU64 bytes (%RU64MB)...\n", (enmImgType == VDI_IMAGE_TYPE_FIXED) ? "fixed" : "dynamic", cbFile, (cbFile + _1M - 1) / _1M);
3505 char pszComment[256];
3506 RTStrPrintf(pszComment, sizeof(pszComment), "Converted image from %s", argv[arg]);
3507 rc = VDICreateBaseImage(argv[arg + 1],
3508 enmImgType,
3509 cbFile,
3510 pszComment, NULL, NULL);
3511 if (VBOX_SUCCESS(rc))
3512 {
3513 PVDIDISK pVdi = VDIDiskCreate();
3514 rc = VDIDiskOpenImage(pVdi, argv[arg + 1], VDI_OPEN_FLAGS_NORMAL);
3515 if (VBOX_SUCCESS(rc))
3516 {
3517 /* alloc work buffer. */
3518 size_t cbBuffer = VDIDiskGetBufferSize(pVdi);
3519 void *pvBuf = RTMemAlloc(cbBuffer);
3520 if (pvBuf)
3521 {
3522 uint64_t offFile = 0;
3523 while (offFile < cbFile)
3524 {
3525 size_t cbRead = 0;
3526 size_t cbToRead = cbFile - offFile >= (uint64_t) cbBuffer ?
3527 cbBuffer : (size_t) (cbFile - offFile);
3528 rc = RTFileRead(File, pvBuf, cbToRead, &cbRead);
3529 if (VBOX_FAILURE(rc) || !cbRead)
3530 break;
3531 rc = VDIDiskWrite(pVdi, offFile, pvBuf, cbRead);
3532 if (VBOX_FAILURE(rc))
3533 break;
3534 offFile += cbRead;
3535 }
3536
3537 RTMemFree(pvBuf);
3538 }
3539 else
3540 rc = VERR_NO_MEMORY;
3541
3542 VDIDiskCloseImage(pVdi);
3543 }
3544
3545 if (VBOX_FAILURE(rc))
3546 {
3547 /* delete image on error */
3548 RTPrintf("Failed (%Vrc)!\n", rc);
3549 VDIDeleteImage(argv[arg + 1]);
3550 }
3551 }
3552 else
3553 RTPrintf("Failed to create output file (%Vrc)!\n", rc);
3554 }
3555 RTFileClose(File);
3556
3557 return rc;
3558}
3559
3560static int handleAddiSCSIDisk(int argc, char *argv[],
3561 ComPtr <IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
3562{
3563 HRESULT rc;
3564 Bstr server;
3565 Bstr target;
3566 uint16_t port = UINT16_MAX;
3567 uint64_t lun = UINT64_MAX;
3568 Bstr username;
3569 Bstr password;
3570 Bstr comment;
3571
3572 /* at least server and target */
3573 if (argc < 4)
3574 return errorSyntax(USAGE_ADDISCSIDISK, "Not enough parameters");
3575
3576 /* let's have a closer look at the arguments */
3577 for (int i = 0; i < argc; i++)
3578 {
3579 if (strcmp(argv[i], "-server") == 0)
3580 {
3581 if (argc <= i + 1)
3582 return errorArgument("Missing argument to '%s'", argv[i]);
3583 i++;
3584 server = argv[i];
3585 }
3586 else if (strcmp(argv[i], "-target") == 0)
3587 {
3588 if (argc <= i + 1)
3589 return errorArgument("Missing argument to '%s'", argv[i]);
3590 i++;
3591 target = argv[i];
3592 }
3593 else if (strcmp(argv[i], "-port") == 0)
3594 {
3595 if (argc <= i + 1)
3596 return errorArgument("Missing argument to '%s'", argv[i]);
3597 i++;
3598 port = atoi(argv[i]);
3599 }
3600 else if (strcmp(argv[i], "-lun") == 0)
3601 {
3602 /** @todo move the LUN encoding algorithm into IISCSIHardDisk, add decoding */
3603 if (argc <= i + 1)
3604 return errorArgument("Missing argument to '%s'", argv[i]);
3605 i++;
3606 char *pszNext;
3607 int rc = RTStrToUInt64Ex(argv[i], &pszNext, 0, &lun);
3608 if (VBOX_FAILURE(rc) || *pszNext != '\0' || lun >= 16384)
3609 return errorArgument("Invalid LUN number '%s'", argv[i]);
3610 if (lun <= 255)
3611 {
3612 /* Assume bus identifier = 0. */
3613 lun = (lun << 48); /* uses peripheral device addressing method */
3614 }
3615 else
3616 {
3617 /* Check above already limited the LUN to 14 bits. */
3618 lun = (lun << 48) | RT_BIT_64(62); /* uses flat space addressing method */
3619 }
3620 }
3621 else if (strcmp(argv[i], "-encodedlun") == 0)
3622 {
3623 if (argc <= i + 1)
3624 return errorArgument("Missing argument to '%s'", argv[i]);
3625 i++;
3626 char *pszNext;
3627 int rc = RTStrToUInt64Ex(argv[i], &pszNext, 0, &lun);
3628 if (VBOX_FAILURE(rc) || *pszNext != '\0')
3629 return errorArgument("Invalid encoded LUN number '%s'", argv[i]);
3630 }
3631 else if (strcmp(argv[i], "-username") == 0)
3632 {
3633 if (argc <= i + 1)
3634 return errorArgument("Missing argument to '%s'", argv[i]);
3635 i++;
3636 username = argv[i];
3637 }
3638 else if (strcmp(argv[i], "-password") == 0)
3639 {
3640 if (argc <= i + 1)
3641 return errorArgument("Missing argument to '%s'", argv[i]);
3642 i++;
3643 password = argv[i];
3644 }
3645 else if (strcmp(argv[i], "-comment") == 0)
3646 {
3647 if (argc <= i + 1)
3648 return errorArgument("Missing argument to '%s'", argv[i]);
3649 i++;
3650 comment = argv[i];
3651 }
3652 else
3653 return errorSyntax(USAGE_ADDISCSIDISK, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
3654 }
3655
3656 /* check for required options */
3657 if (!server || !target)
3658 return errorSyntax(USAGE_ADDISCSIDISK, "Parameters -server and -target are required");
3659
3660 ComPtr<IHardDisk> hardDisk;
3661 CHECK_ERROR(aVirtualBox, CreateHardDisk(HardDiskStorageType_ISCSIHardDisk, hardDisk.asOutParam()));
3662 if (SUCCEEDED(rc) && hardDisk)
3663 {
3664 CHECK_ERROR(hardDisk, COMSETTER(Description)(comment));
3665 ComPtr<IISCSIHardDisk> iSCSIDisk = hardDisk;
3666 CHECK_ERROR(iSCSIDisk, COMSETTER(Server)(server));
3667 if (port != UINT16_MAX)
3668 CHECK_ERROR(iSCSIDisk, COMSETTER(Port)(port));
3669 CHECK_ERROR(iSCSIDisk, COMSETTER(Target)(target));
3670 if (lun != UINT64_MAX)
3671 CHECK_ERROR(iSCSIDisk, COMSETTER(Lun)(lun));
3672 CHECK_ERROR(iSCSIDisk, COMSETTER(UserName)(username));
3673 CHECK_ERROR(iSCSIDisk, COMSETTER(Password)(password));
3674
3675 if (SUCCEEDED(rc))
3676 {
3677 CHECK_ERROR(aVirtualBox, RegisterHardDisk(hardDisk));
3678 }
3679
3680 if (SUCCEEDED(rc))
3681 {
3682 Guid guid;
3683 CHECK_ERROR(hardDisk, COMGETTER(Id)(guid.asOutParam()));
3684 RTPrintf("iSCSI disk created. UUID: %s\n", guid.toString().raw());
3685 }
3686 }
3687
3688 return SUCCEEDED(rc) ? 0 : 1;
3689}
3690
3691static int handleCreateVM(int argc, char *argv[],
3692 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
3693{
3694 HRESULT rc;
3695 Bstr baseFolder;
3696 Bstr settingsFile;
3697 Bstr name;
3698 RTUUID id;
3699 bool fRegister = false;
3700
3701 RTUuidClear(&id);
3702 for (int i = 0; i < argc; i++)
3703 {
3704 if (strcmp(argv[i], "-basefolder") == 0)
3705 {
3706 if (argc <= i + 1)
3707 return errorArgument("Missing argument to '%s'", argv[i]);
3708 i++;
3709 baseFolder = argv[i];
3710 }
3711 else if (strcmp(argv[i], "-settingsfile") == 0)
3712 {
3713 if (argc <= i + 1)
3714 return errorArgument("Missing argument to '%s'", argv[i]);
3715 i++;
3716 settingsFile = argv[i];
3717 }
3718 else if (strcmp(argv[i], "-name") == 0)
3719 {
3720 if (argc <= i + 1)
3721 return errorArgument("Missing argument to '%s'", argv[i]);
3722 i++;
3723 name = argv[i];
3724 }
3725 else if (strcmp(argv[i], "-uuid") == 0)
3726 {
3727 if (argc <= i + 1)
3728 return errorArgument("Missing argument to '%s'", argv[i]);
3729 i++;
3730 if (VBOX_FAILURE(RTUuidFromStr(&id, argv[i])))
3731 return errorArgument("Invalid UUID format %s\n", argv[i]);
3732 }
3733 else if (strcmp(argv[i], "-register") == 0)
3734 {
3735 fRegister = true;
3736 }
3737 else
3738 return errorSyntax(USAGE_CREATEVM, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
3739 }
3740 if (!name)
3741 return errorSyntax(USAGE_CREATEVM, "Parameter -name is required");
3742
3743 if (!!baseFolder && !!settingsFile)
3744 return errorSyntax(USAGE_CREATEVM, "Either -basefolder or -settingsfile must be specified");
3745
3746 do
3747 {
3748 ComPtr<IMachine> machine;
3749
3750 if (!settingsFile)
3751 CHECK_ERROR_BREAK(virtualBox,
3752 CreateMachine(baseFolder, name, Guid(id), machine.asOutParam()));
3753 else
3754 CHECK_ERROR_BREAK(virtualBox,
3755 CreateLegacyMachine(settingsFile, name, Guid(id), machine.asOutParam()));
3756
3757 CHECK_ERROR_BREAK(machine, SaveSettings());
3758 if (fRegister)
3759 {
3760 CHECK_ERROR_BREAK(virtualBox, RegisterMachine(machine));
3761 }
3762 Guid uuid;
3763 CHECK_ERROR_BREAK(machine, COMGETTER(Id)(uuid.asOutParam()));
3764 CHECK_ERROR_BREAK(machine, COMGETTER(SettingsFilePath)(settingsFile.asOutParam()));
3765 RTPrintf("Virtual machine '%ls' is created%s.\n"
3766 "UUID: %s\n"
3767 "Settings file: '%ls'\n",
3768 name.raw(), fRegister ? " and registered" : "",
3769 uuid.toString().raw(), settingsFile.raw());
3770 }
3771 while (0);
3772
3773 return SUCCEEDED(rc) ? 0 : 1;
3774}
3775
3776/**
3777 * Parses a number.
3778 *
3779 * @returns Valid number on success.
3780 * @returns 0 if invalid number. All necesary bitching has been done.
3781 * @param psz Pointer to the nic number.
3782 */
3783static unsigned parseNum(const char *psz, unsigned cMaxNum, const char *name)
3784{
3785 uint32_t u32;
3786 char *pszNext;
3787 int rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u32);
3788 if ( VBOX_SUCCESS(rc)
3789 && *pszNext == '\0'
3790 && u32 >= 1
3791 && u32 <= cMaxNum)
3792 return (unsigned)u32;
3793 errorArgument("Invalid %s number '%s'", name, psz);
3794 return 0;
3795}
3796
3797static int handleModifyVM(int argc, char *argv[],
3798 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
3799{
3800 HRESULT rc;
3801 Bstr name;
3802 Bstr ostype;
3803 ULONG memorySize = 0;
3804 ULONG vramSize = 0;
3805 char *acpi = NULL;
3806 char *hwvirtex = NULL;
3807 char *nestedpaging = NULL;
3808 char *vtxvpid = NULL;
3809 char *pae = NULL;
3810 char *ioapic = NULL;
3811 int monitorcount = -1;
3812 char *bioslogofadein = NULL;
3813 char *bioslogofadeout = NULL;
3814 uint32_t bioslogodisplaytime = ~0;
3815 char *bioslogoimagepath = NULL;
3816 char *biosbootmenumode = NULL;
3817 char *biossystemtimeoffset = NULL;
3818 char *biospxedebug = NULL;
3819 DeviceType_T bootDevice[4];
3820 int bootDeviceChanged[4] = { false };
3821 char *hdds[34] = {0};
3822 char *dvd = NULL;
3823 char *dvdpassthrough = NULL;
3824 char *idecontroller = NULL;
3825 char *floppy = NULL;
3826 char *audio = NULL;
3827 char *audiocontroller = NULL;
3828 char *clipboard = NULL;
3829#ifdef VBOX_WITH_VRDP
3830 char *vrdp = NULL;
3831 uint16_t vrdpport = UINT16_MAX;
3832 char *vrdpaddress = NULL;
3833 char *vrdpauthtype = NULL;
3834 char *vrdpmulticon = NULL;
3835 char *vrdpreusecon = NULL;
3836#endif
3837 int fUsbEnabled = -1;
3838 int fUsbEhciEnabled = -1;
3839 char *snapshotFolder = NULL;
3840 ULONG guestMemBalloonSize = (ULONG)-1;
3841 ULONG guestStatInterval = (ULONG)-1;
3842 int fSataEnabled = -1;
3843 int sataPortCount = -1;
3844 int sataBootDevices[4] = {-1,-1,-1,-1};
3845
3846 /* VM ID + at least one parameter. Parameter arguments are checked
3847 * individually. */
3848 if (argc < 2)
3849 return errorSyntax(USAGE_MODIFYVM, "Not enough parameters");
3850
3851 /* Get the number of network adapters */
3852 ULONG NetworkAdapterCount = 0;
3853 {
3854 ComPtr <ISystemProperties> info;
3855 CHECK_ERROR_RET (virtualBox, COMGETTER(SystemProperties) (info.asOutParam()), 1);
3856 CHECK_ERROR_RET (info, COMGETTER(NetworkAdapterCount) (&NetworkAdapterCount), 1);
3857 }
3858 ULONG SerialPortCount = 0;
3859 {
3860 ComPtr <ISystemProperties> info;
3861 CHECK_ERROR_RET (virtualBox, COMGETTER(SystemProperties) (info.asOutParam()), 1);
3862 CHECK_ERROR_RET (info, COMGETTER(SerialPortCount) (&SerialPortCount), 1);
3863 }
3864
3865 std::vector <char *> nics (NetworkAdapterCount, 0);
3866 std::vector <char *> nictype (NetworkAdapterCount, 0);
3867 std::vector <char *> cableconnected (NetworkAdapterCount, 0);
3868 std::vector <char *> nictrace (NetworkAdapterCount, 0);
3869 std::vector <char *> nictracefile (NetworkAdapterCount, 0);
3870 std::vector <char *> nicspeed (NetworkAdapterCount, 0);
3871 std::vector <char *> hostifdev (NetworkAdapterCount, 0);
3872 std::vector <const char *> intnet (NetworkAdapterCount, 0);
3873 std::vector <const char *> natnet (NetworkAdapterCount, 0);
3874#ifdef RT_OS_LINUX
3875 std::vector <char *> tapsetup (NetworkAdapterCount, 0);
3876 std::vector <char *> tapterm (NetworkAdapterCount, 0);
3877#endif
3878 std::vector <char *> macs (NetworkAdapterCount, 0);
3879 std::vector <char *> uarts_mode (SerialPortCount, 0);
3880 std::vector <ULONG> uarts_base (SerialPortCount, 0);
3881 std::vector <ULONG> uarts_irq (SerialPortCount, 0);
3882 std::vector <char *> uarts_path (SerialPortCount, 0);
3883
3884 for (int i = 1; i < argc; i++)
3885 {
3886 if (strcmp(argv[i], "-name") == 0)
3887 {
3888 if (argc <= i + 1)
3889 return errorArgument("Missing argument to '%s'", argv[i]);
3890 i++;
3891 name = argv[i];
3892 }
3893 else if (strcmp(argv[i], "-ostype") == 0)
3894 {
3895 if (argc <= i + 1)
3896 return errorArgument("Missing argument to '%s'", argv[i]);
3897 i++;
3898 ostype = argv[i];
3899 }
3900 else if (strcmp(argv[i], "-memory") == 0)
3901 {
3902 if (argc <= i + 1)
3903 return errorArgument("Missing argument to '%s'", argv[i]);
3904 i++;
3905 memorySize = atoi(argv[i]);
3906 }
3907 else if (strcmp(argv[i], "-vram") == 0)
3908 {
3909 if (argc <= i + 1)
3910 return errorArgument("Missing argument to '%s'", argv[i]);
3911 i++;
3912 vramSize = atoi(argv[i]);
3913 }
3914 else if (strcmp(argv[i], "-acpi") == 0)
3915 {
3916 if (argc <= i + 1)
3917 return errorArgument("Missing argument to '%s'", argv[i]);
3918 i++;
3919 acpi = argv[i];
3920 }
3921 else if (strcmp(argv[i], "-ioapic") == 0)
3922 {
3923 if (argc <= i + 1)
3924 return errorArgument("Missing argument to '%s'", argv[i]);
3925 i++;
3926 ioapic = argv[i];
3927 }
3928 else if (strcmp(argv[i], "-hwvirtex") == 0)
3929 {
3930 if (argc <= i + 1)
3931 return errorArgument("Missing argument to '%s'", argv[i]);
3932 i++;
3933 hwvirtex = argv[i];
3934 }
3935 else if (strcmp(argv[i], "-nestedpaging") == 0)
3936 {
3937 if (argc <= i + 1)
3938 return errorArgument("Missing argument to '%s'", argv[i]);
3939 i++;
3940 nestedpaging = argv[i];
3941 }
3942 else if (strcmp(argv[i], "-vtxvpid") == 0)
3943 {
3944 if (argc <= i + 1)
3945 return errorArgument("Missing argument to '%s'", argv[i]);
3946 i++;
3947 vtxvpid = argv[i];
3948 }
3949 else if (strcmp(argv[i], "-pae") == 0)
3950 {
3951 if (argc <= i + 1)
3952 return errorArgument("Missing argument to '%s'", argv[i]);
3953 i++;
3954 pae = argv[i];
3955 }
3956 else if (strcmp(argv[i], "-monitorcount") == 0)
3957 {
3958 if (argc <= i + 1)
3959 return errorArgument("Missing argument to '%s'", argv[i]);
3960 i++;
3961 monitorcount = atoi(argv[i]);
3962 }
3963 else if (strcmp(argv[i], "-bioslogofadein") == 0)
3964 {
3965 if (argc <= i + 1)
3966 return errorArgument("Missing argument to '%s'", argv[i]);
3967 i++;
3968 bioslogofadein = argv[i];
3969 }
3970 else if (strcmp(argv[i], "-bioslogofadeout") == 0)
3971 {
3972 if (argc <= i + 1)
3973 return errorArgument("Missing argument to '%s'", argv[i]);
3974 i++;
3975 bioslogofadeout = argv[i];
3976 }
3977 else if (strcmp(argv[i], "-bioslogodisplaytime") == 0)
3978 {
3979 if (argc <= i + 1)
3980 return errorArgument("Missing argument to '%s'", argv[i]);
3981 i++;
3982 bioslogodisplaytime = atoi(argv[i]);
3983 }
3984 else if (strcmp(argv[i], "-bioslogoimagepath") == 0)
3985 {
3986 if (argc <= i + 1)
3987 return errorArgument("Missing argument to '%s'", argv[i]);
3988 i++;
3989 bioslogoimagepath = argv[i];
3990 }
3991 else if (strcmp(argv[i], "-biosbootmenu") == 0)
3992 {
3993 if (argc <= i + 1)
3994 return errorArgument("Missing argument to '%s'", argv[i]);
3995 i++;
3996 biosbootmenumode = argv[i];
3997 }
3998 else if (strcmp(argv[i], "-biossystemtimeoffset") == 0)
3999 {
4000 if (argc <= i + 1)
4001 return errorArgument("Missing argument to '%s'", argv[i]);
4002 i++;
4003 biossystemtimeoffset = argv[i];
4004 }
4005 else if (strcmp(argv[i], "-biospxedebug") == 0)
4006 {
4007 if (argc <= i + 1)
4008 return errorArgument("Missing argument to '%s'", argv[i]);
4009 i++;
4010 biospxedebug = argv[i];
4011 }
4012 else if (strncmp(argv[i], "-boot", 5) == 0)
4013 {
4014 ULONG n = 0;
4015 if (!argv[i][5])
4016 return errorSyntax(USAGE_MODIFYVM, "Missing boot slot number in '%s'", argv[i]);
4017 if ((n = strtoul(&argv[i][5], NULL, 10)) < 1)
4018 return errorSyntax(USAGE_MODIFYVM, "Invalid boot slot number in '%s'", argv[i]);
4019 if (argc <= i + 1)
4020 return errorArgument("Missing argument to '%s'", argv[i]);
4021 i++;
4022 if (strcmp(argv[i], "none") == 0)
4023 {
4024 bootDevice[n - 1] = DeviceType_Null;
4025 }
4026 else if (strcmp(argv[i], "floppy") == 0)
4027 {
4028 bootDevice[n - 1] = DeviceType_Floppy;
4029 }
4030 else if (strcmp(argv[i], "dvd") == 0)
4031 {
4032 bootDevice[n - 1] = DeviceType_DVD;
4033 }
4034 else if (strcmp(argv[i], "disk") == 0)
4035 {
4036 bootDevice[n - 1] = DeviceType_HardDisk;
4037 }
4038 else if (strcmp(argv[i], "net") == 0)
4039 {
4040 bootDevice[n - 1] = DeviceType_Network;
4041 }
4042 else
4043 return errorArgument("Invalid boot device '%s'", argv[i]);
4044
4045 bootDeviceChanged[n - 1] = true;
4046 }
4047 else if (strcmp(argv[i], "-hda") == 0)
4048 {
4049 if (argc <= i + 1)
4050 return errorArgument("Missing argument to '%s'", argv[i]);
4051 i++;
4052 hdds[0] = argv[i];
4053 }
4054 else if (strcmp(argv[i], "-hdb") == 0)
4055 {
4056 if (argc <= i + 1)
4057 return errorArgument("Missing argument to '%s'", argv[i]);
4058 i++;
4059 hdds[1] = argv[i];
4060 }
4061 else if (strcmp(argv[i], "-hdd") == 0)
4062 {
4063 if (argc <= i + 1)
4064 return errorArgument("Missing argument to '%s'", argv[i]);
4065 i++;
4066 hdds[2] = argv[i];
4067 }
4068 else if (strcmp(argv[i], "-dvd") == 0)
4069 {
4070 if (argc <= i + 1)
4071 return errorArgument("Missing argument to '%s'", argv[i]);
4072 i++;
4073 dvd = argv[i];
4074 }
4075 else if (strcmp(argv[i], "-dvdpassthrough") == 0)
4076 {
4077 if (argc <= i + 1)
4078 return errorArgument("Missing argument to '%s'", argv[i]);
4079 i++;
4080 dvdpassthrough = argv[i];
4081 }
4082 else if (strcmp(argv[i], "-idecontroller") == 0)
4083 {
4084 if (argc <= i + 1)
4085 return errorArgument("Missing argument to '%s'", argv[i]);
4086 i++;
4087 idecontroller = argv[i];
4088 }
4089 else if (strcmp(argv[i], "-floppy") == 0)
4090 {
4091 if (argc <= i + 1)
4092 return errorArgument("Missing argument to '%s'", argv[i]);
4093 i++;
4094 floppy = argv[i];
4095 }
4096 else if (strcmp(argv[i], "-audio") == 0)
4097 {
4098 if (argc <= i + 1)
4099 return errorArgument("Missing argument to '%s'", argv[i]);
4100 i++;
4101 audio = argv[i];
4102 }
4103 else if (strcmp(argv[i], "-audiocontroller") == 0)
4104 {
4105 if (argc <= i + 1)
4106 return errorArgument("Missing argument to '%s'", argv[i]);
4107 i++;
4108 audiocontroller = argv[i];
4109 }
4110 else if (strcmp(argv[i], "-clipboard") == 0)
4111 {
4112 if (argc <= i + 1)
4113 return errorArgument("Missing argument to '%s'", argv[i]);
4114 i++;
4115 clipboard = argv[i];
4116 }
4117 else if (strncmp(argv[i], "-cableconnected", 15) == 0)
4118 {
4119 unsigned n = parseNum(&argv[i][15], NetworkAdapterCount, "NIC");
4120 if (!n)
4121 return 1;
4122
4123 if (argc <= i + 1)
4124 return errorArgument("Missing argument to '%s'", argv[i]);
4125
4126 cableconnected[n - 1] = argv[i + 1];
4127 i++;
4128 }
4129 /* watch for the right order of these -nic* comparisons! */
4130 else if (strncmp(argv[i], "-nictracefile", 13) == 0)
4131 {
4132 unsigned n = parseNum(&argv[i][13], NetworkAdapterCount, "NIC");
4133 if (!n)
4134 return 1;
4135 if (argc <= i + 1)
4136 {
4137 return errorArgument("Missing argument to '%s'", argv[i]);
4138 }
4139 nictracefile[n - 1] = argv[i + 1];
4140 i++;
4141 }
4142 else if (strncmp(argv[i], "-nictrace", 9) == 0)
4143 {
4144 unsigned n = parseNum(&argv[i][9], NetworkAdapterCount, "NIC");
4145 if (!n)
4146 return 1;
4147 if (argc <= i + 1)
4148 return errorArgument("Missing argument to '%s'", argv[i]);
4149 nictrace[n - 1] = argv[i + 1];
4150 i++;
4151 }
4152 else if (strncmp(argv[i], "-nictype", 8) == 0)
4153 {
4154 unsigned n = parseNum(&argv[i][8], NetworkAdapterCount, "NIC");
4155 if (!n)
4156 return 1;
4157 if (argc <= i + 1)
4158 return errorArgument("Missing argument to '%s'", argv[i]);
4159 nictype[n - 1] = argv[i + 1];
4160 i++;
4161 }
4162 else if (strncmp(argv[i], "-nicspeed", 9) == 0)
4163 {
4164 unsigned n = parseNum(&argv[i][9], NetworkAdapterCount, "NIC");
4165 if (!n)
4166 return 1;
4167 if (argc <= i + 1)
4168 return errorArgument("Missing argument to '%s'", argv[i]);
4169 nicspeed[n - 1] = argv[i + 1];
4170 i++;
4171 }
4172 else if (strncmp(argv[i], "-nic", 4) == 0)
4173 {
4174 unsigned n = parseNum(&argv[i][4], NetworkAdapterCount, "NIC");
4175 if (!n)
4176 return 1;
4177 if (argc <= i + 1)
4178 return errorArgument("Missing argument to '%s'", argv[i]);
4179 nics[n - 1] = argv[i + 1];
4180 i++;
4181 }
4182 else if (strncmp(argv[i], "-hostifdev", 10) == 0)
4183 {
4184 unsigned n = parseNum(&argv[i][10], NetworkAdapterCount, "NIC");
4185 if (!n)
4186 return 1;
4187 if (argc <= i + 1)
4188 return errorArgument("Missing argument to '%s'", argv[i]);
4189 hostifdev[n - 1] = argv[i + 1];
4190 i++;
4191 }
4192 else if (strncmp(argv[i], "-intnet", 7) == 0)
4193 {
4194 unsigned n = parseNum(&argv[i][7], NetworkAdapterCount, "NIC");
4195 if (!n)
4196 return 1;
4197 if (argc <= i + 1)
4198 return errorArgument("Missing argument to '%s'", argv[i]);
4199 intnet[n - 1] = argv[i + 1];
4200 i++;
4201 }
4202 else if (strncmp(argv[i], "-natnet", 7) == 0)
4203 {
4204 unsigned n = parseNum(&argv[i][7], NetworkAdapterCount, "NIC");
4205 if (!n)
4206 return 1;
4207 if (argc <= i + 1)
4208 return errorArgument("Missing argument to '%s'", argv[i]);
4209
4210 if (!strcmp(argv[i + 1], "default"))
4211 natnet[n - 1] = "";
4212 else
4213 {
4214 RTIPV4ADDR Network;
4215 RTIPV4ADDR Netmask;
4216 int rc = RTCidrStrToIPv4(argv[i + 1], &Network, &Netmask);
4217 if (RT_FAILURE(rc))
4218 return errorArgument("Invalid IPv4 network '%s' specified -- CIDR notation expected.\n", argv[i + 1]);
4219 if (Netmask & 0x1f)
4220 return errorArgument("Prefix length of the NAT network must be less than 28.\n");
4221 natnet[n - 1] = argv[i + 1];
4222 }
4223 i++;
4224 }
4225#ifdef RT_OS_LINUX
4226 else if (strncmp(argv[i], "-tapsetup", 9) == 0)
4227 {
4228 unsigned n = parseNum(&argv[i][9], NetworkAdapterCount, "NIC");
4229 if (!n)
4230 return 1;
4231 if (argc <= i + 1)
4232 return errorArgument("Missing argument to '%s'", argv[i]);
4233 tapsetup[n - 1] = argv[i + 1];
4234 i++;
4235 }
4236 else if (strncmp(argv[i], "-tapterminate", 13) == 0)
4237 {
4238 unsigned n = parseNum(&argv[i][13], NetworkAdapterCount, "NIC");
4239 if (!n)
4240 return 1;
4241 if (argc <= i + 1)
4242 return errorArgument("Missing argument to '%s'", argv[i]);
4243 tapterm[n - 1] = argv[i + 1];
4244 i++;
4245 }
4246#endif /* RT_OS_LINUX */
4247 else if (strncmp(argv[i], "-macaddress", 11) == 0)
4248 {
4249 unsigned n = parseNum(&argv[i][11], NetworkAdapterCount, "NIC");
4250 if (!n)
4251 return 1;
4252 if (argc <= i + 1)
4253 return errorArgument("Missing argument to '%s'", argv[i]);
4254 macs[n - 1] = argv[i + 1];
4255 i++;
4256 }
4257#ifdef VBOX_WITH_VRDP
4258 else if (strcmp(argv[i], "-vrdp") == 0)
4259 {
4260 if (argc <= i + 1)
4261 return errorArgument("Missing argument to '%s'", argv[i]);
4262 i++;
4263 vrdp = argv[i];
4264 }
4265 else if (strcmp(argv[i], "-vrdpport") == 0)
4266 {
4267 if (argc <= i + 1)
4268 return errorArgument("Missing argument to '%s'", argv[i]);
4269 i++;
4270 if (strcmp(argv[i], "default") == 0)
4271 vrdpport = 0;
4272 else
4273 vrdpport = atoi(argv[i]);
4274 }
4275 else if (strcmp(argv[i], "-vrdpaddress") == 0)
4276 {
4277 if (argc <= i + 1)
4278 return errorArgument("Missing argument to '%s'", argv[i]);
4279 i++;
4280 vrdpaddress = argv[i];
4281 }
4282 else if (strcmp(argv[i], "-vrdpauthtype") == 0)
4283 {
4284 if (argc <= i + 1)
4285 return errorArgument("Missing argument to '%s'", argv[i]);
4286 i++;
4287 vrdpauthtype = argv[i];
4288 }
4289 else if (strcmp(argv[i], "-vrdpmulticon") == 0)
4290 {
4291 if (argc <= i + 1)
4292 return errorArgument("Missing argument to '%s'", argv[i]);
4293 i++;
4294 vrdpmulticon = argv[i];
4295 }
4296 else if (strcmp(argv[i], "-vrdpreusecon") == 0)
4297 {
4298 if (argc <= i + 1)
4299 return errorArgument("Missing argument to '%s'", argv[i]);
4300 i++;
4301 vrdpreusecon = argv[i];
4302 }
4303#endif /* VBOX_WITH_VRDP */
4304 else if (strcmp(argv[i], "-usb") == 0)
4305 {
4306 if (argc <= i + 1)
4307 return errorArgument("Missing argument to '%s'", argv[i]);
4308 i++;
4309 if (strcmp(argv[i], "on") == 0 || strcmp(argv[i], "enable") == 0)
4310 fUsbEnabled = 1;
4311 else if (strcmp(argv[i], "off") == 0 || strcmp(argv[i], "disable") == 0)
4312 fUsbEnabled = 0;
4313 else
4314 return errorArgument("Invalid -usb argument '%s'", argv[i]);
4315 }
4316 else if (strcmp(argv[i], "-usbehci") == 0)
4317 {
4318 if (argc <= i + 1)
4319 return errorArgument("Missing argument to '%s'", argv[i]);
4320 i++;
4321 if (strcmp(argv[i], "on") == 0 || strcmp(argv[i], "enable") == 0)
4322 fUsbEhciEnabled = 1;
4323 else if (strcmp(argv[i], "off") == 0 || strcmp(argv[i], "disable") == 0)
4324 fUsbEhciEnabled = 0;
4325 else
4326 return errorArgument("Invalid -usbehci argument '%s'", argv[i]);
4327 }
4328 else if (strcmp(argv[i], "-snapshotfolder") == 0)
4329 {
4330 if (argc <= i + 1)
4331 return errorArgument("Missing argument to '%s'", argv[i]);
4332 i++;
4333 snapshotFolder = argv[i];
4334 }
4335 else if (strncmp(argv[i], "-uartmode", 9) == 0)
4336 {
4337 unsigned n = parseNum(&argv[i][9], SerialPortCount, "UART");
4338 if (!n)
4339 return 1;
4340 i++;
4341 if (strcmp(argv[i], "disconnected") == 0)
4342 {
4343 uarts_mode[n - 1] = argv[i];
4344 }
4345 else
4346 {
4347 if (strcmp(argv[i], "server") == 0 || strcmp(argv[i], "client") == 0)
4348 {
4349 uarts_mode[n - 1] = argv[i];
4350 i++;
4351#ifdef RT_OS_WINDOWS
4352 if (strncmp(argv[i], "\\\\.\\pipe\\", 9))
4353 return errorArgument("Uart pipe must start with \\\\.\\pipe\\");
4354#endif
4355 }
4356 else
4357 {
4358 uarts_mode[n - 1] = (char*)"device";
4359 }
4360 if (argc <= i)
4361 return errorArgument("Missing argument to -uartmode");
4362 uarts_path[n - 1] = argv[i];
4363 }
4364 }
4365 else if (strncmp(argv[i], "-uart", 5) == 0)
4366 {
4367 unsigned n = parseNum(&argv[i][5], SerialPortCount, "UART");
4368 if (!n)
4369 return 1;
4370 if (argc <= i + 1)
4371 return errorArgument("Missing argument to '%s'", argv[i]);
4372 i++;
4373 if (strcmp(argv[i], "off") == 0 || strcmp(argv[i], "disable") == 0)
4374 {
4375 uarts_base[n - 1] = (ULONG)-1;
4376 }
4377 else
4378 {
4379 if (argc <= i + 1)
4380 return errorArgument("Missing argument to '%s'", argv[i-1]);
4381 uint32_t uVal;
4382 int vrc;
4383 vrc = RTStrToUInt32Ex(argv[i], NULL, 0, &uVal);
4384 if (vrc != VINF_SUCCESS || uVal == 0)
4385 return errorArgument("Error parsing UART I/O base '%s'", argv[i]);
4386 uarts_base[n - 1] = uVal;
4387 i++;
4388 vrc = RTStrToUInt32Ex(argv[i], NULL, 0, &uVal);
4389 if (vrc != VINF_SUCCESS)
4390 return errorArgument("Error parsing UART IRQ '%s'", argv[i]);
4391 uarts_irq[n - 1] = uVal;
4392 }
4393 }
4394#ifdef VBOX_WITH_MEM_BALLOONING
4395 else if (strncmp(argv[i], "-guestmemoryballoon", 19) == 0)
4396 {
4397 if (argc <= i + 1)
4398 return errorArgument("Missing argument to '%s'", argv[i]);
4399 i++;
4400 uint32_t uVal;
4401 int vrc;
4402 vrc = RTStrToUInt32Ex(argv[i], NULL, 0, &uVal);
4403 if (vrc != VINF_SUCCESS)
4404 return errorArgument("Error parsing guest memory balloon size '%s'", argv[i]);
4405 guestMemBalloonSize = uVal;
4406 }
4407#endif
4408 else if (strncmp(argv[i], "-gueststatisticsinterval", 24) == 0)
4409 {
4410 if (argc <= i + 1)
4411 return errorArgument("Missing argument to '%s'", argv[i]);
4412 i++;
4413 uint32_t uVal;
4414 int vrc;
4415 vrc = RTStrToUInt32Ex(argv[i], NULL, 0, &uVal);
4416 if (vrc != VINF_SUCCESS)
4417 return errorArgument("Error parsing guest statistics interval '%s'", argv[i]);
4418 guestStatInterval = uVal;
4419 }
4420 else if (strcmp(argv[i], "-sata") == 0)
4421 {
4422 if (argc <= i + 1)
4423 return errorArgument("Missing argument to '%s'", argv[i]);
4424 i++;
4425 if (strcmp(argv[i], "on") == 0 || strcmp(argv[i], "enable") == 0)
4426 fSataEnabled = 1;
4427 else if (strcmp(argv[i], "off") == 0 || strcmp(argv[i], "disable") == 0)
4428 fSataEnabled = 0;
4429 else
4430 return errorArgument("Invalid -usb argument '%s'", argv[i]);
4431 }
4432 else if (strcmp(argv[i], "-sataportcount") == 0)
4433 {
4434 unsigned n;
4435
4436 if (argc <= i + 1)
4437 return errorArgument("Missing arguments to '%s'", argv[i]);
4438 i++;
4439
4440 n = parseNum(argv[i], 30, "SATA");
4441 if (!n)
4442 return 1;
4443 sataPortCount = n;
4444 }
4445 else if (strncmp(argv[i], "-sataport", 9) == 0)
4446 {
4447 unsigned n = parseNum(&argv[i][9], 30, "SATA");
4448 if (!n)
4449 return 1;
4450 if (argc <= i + 1)
4451 return errorArgument("Missing argument to '%s'", argv[i]);
4452 i++;
4453 hdds[n-1+4] = argv[i];
4454 }
4455 else if (strncmp(argv[i], "-sataideemulation", 17) == 0)
4456 {
4457 unsigned bootDevicePos = 0;
4458 unsigned n;
4459
4460 bootDevicePos = parseNum(&argv[i][17], 4, "SATA");
4461 if (!bootDevicePos)
4462 return 1;
4463 bootDevicePos--;
4464
4465 if (argc <= i + 1)
4466 return errorArgument("Missing arguments to '%s'", argv[i]);
4467 i++;
4468
4469 n = parseNum(argv[i], 30, "SATA");
4470 if (!n)
4471 return 1;
4472
4473 sataBootDevices[bootDevicePos] = n-1;
4474 }
4475 else
4476 return errorSyntax(USAGE_MODIFYVM, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
4477 }
4478
4479 /* try to find the given machine */
4480 ComPtr <IMachine> machine;
4481 Guid uuid (argv[0]);
4482 if (!uuid.isEmpty())
4483 {
4484 CHECK_ERROR (virtualBox, GetMachine (uuid, machine.asOutParam()));
4485 }
4486 else
4487 {
4488 CHECK_ERROR (virtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
4489 if (SUCCEEDED (rc))
4490 machine->COMGETTER(Id)(uuid.asOutParam());
4491 }
4492 if (FAILED (rc))
4493 return 1;
4494
4495 /* open a session for the VM */
4496 CHECK_ERROR_RET (virtualBox, OpenSession(session, uuid), 1);
4497
4498 do
4499 {
4500 /* get the mutable session machine */
4501 session->COMGETTER(Machine)(machine.asOutParam());
4502
4503 ComPtr <IBIOSSettings> biosSettings;
4504 machine->COMGETTER(BIOSSettings)(biosSettings.asOutParam());
4505
4506 if (name)
4507 CHECK_ERROR(machine, COMSETTER(Name)(name));
4508 if (ostype)
4509 {
4510 ComPtr<IGuestOSType> guestOSType;
4511 CHECK_ERROR(virtualBox, GetGuestOSType(ostype, guestOSType.asOutParam()));
4512 if (SUCCEEDED(rc) && guestOSType)
4513 {
4514 CHECK_ERROR(machine, COMSETTER(OSTypeId)(ostype));
4515 }
4516 else
4517 {
4518 errorArgument("Invalid guest OS type '%s'", Utf8Str(ostype).raw());
4519 rc = E_FAIL;
4520 break;
4521 }
4522 }
4523 if (memorySize > 0)
4524 CHECK_ERROR(machine, COMSETTER(MemorySize)(memorySize));
4525 if (vramSize > 0)
4526 CHECK_ERROR(machine, COMSETTER(VRAMSize)(vramSize));
4527 if (acpi)
4528 {
4529 if (strcmp(acpi, "on") == 0)
4530 {
4531 CHECK_ERROR(biosSettings, COMSETTER(ACPIEnabled)(true));
4532 }
4533 else if (strcmp(acpi, "off") == 0)
4534 {
4535 CHECK_ERROR(biosSettings, COMSETTER(ACPIEnabled)(false));
4536 }
4537 else
4538 {
4539 errorArgument("Invalid -acpi argument '%s'", acpi);
4540 rc = E_FAIL;
4541 break;
4542 }
4543 }
4544 if (ioapic)
4545 {
4546 if (strcmp(ioapic, "on") == 0)
4547 {
4548 CHECK_ERROR(biosSettings, COMSETTER(IOAPICEnabled)(true));
4549 }
4550 else if (strcmp(ioapic, "off") == 0)
4551 {
4552 CHECK_ERROR(biosSettings, COMSETTER(IOAPICEnabled)(false));
4553 }
4554 else
4555 {
4556 errorArgument("Invalid -ioapic argument '%s'", ioapic);
4557 rc = E_FAIL;
4558 break;
4559 }
4560 }
4561 if (hwvirtex)
4562 {
4563 if (strcmp(hwvirtex, "on") == 0)
4564 {
4565 CHECK_ERROR(machine, COMSETTER(HWVirtExEnabled)(TSBool_True));
4566 }
4567 else if (strcmp(hwvirtex, "off") == 0)
4568 {
4569 CHECK_ERROR(machine, COMSETTER(HWVirtExEnabled)(TSBool_False));
4570 }
4571 else if (strcmp(hwvirtex, "default") == 0)
4572 {
4573 CHECK_ERROR(machine, COMSETTER(HWVirtExEnabled)(TSBool_Default));
4574 }
4575 else
4576 {
4577 errorArgument("Invalid -hwvirtex argument '%s'", hwvirtex);
4578 rc = E_FAIL;
4579 break;
4580 }
4581 }
4582 if (nestedpaging)
4583 {
4584 if (strcmp(nestedpaging, "on") == 0)
4585 {
4586 CHECK_ERROR(machine, COMSETTER(HWVirtExNestedPagingEnabled)(true));
4587 }
4588 else if (strcmp(nestedpaging, "off") == 0)
4589 {
4590 CHECK_ERROR(machine, COMSETTER(HWVirtExNestedPagingEnabled)(false));
4591 }
4592 else
4593 {
4594 errorArgument("Invalid -nestedpaging argument '%s'", ioapic);
4595 rc = E_FAIL;
4596 break;
4597 }
4598 }
4599 if (vtxvpid)
4600 {
4601 if (strcmp(vtxvpid, "on") == 0)
4602 {
4603 CHECK_ERROR(machine, COMSETTER(HWVirtExVPIDEnabled)(true));
4604 }
4605 else if (strcmp(vtxvpid, "off") == 0)
4606 {
4607 CHECK_ERROR(machine, COMSETTER(HWVirtExVPIDEnabled)(false));
4608 }
4609 else
4610 {
4611 errorArgument("Invalid -vtxvpid argument '%s'", ioapic);
4612 rc = E_FAIL;
4613 break;
4614 }
4615 }
4616 if (pae)
4617 {
4618 if (strcmp(pae, "on") == 0)
4619 {
4620 CHECK_ERROR(machine, COMSETTER(PAEEnabled)(true));
4621 }
4622 else if (strcmp(pae, "off") == 0)
4623 {
4624 CHECK_ERROR(machine, COMSETTER(PAEEnabled)(false));
4625 }
4626 else
4627 {
4628 errorArgument("Invalid -pae argument '%s'", ioapic);
4629 rc = E_FAIL;
4630 break;
4631 }
4632 }
4633 if (monitorcount != -1)
4634 {
4635 CHECK_ERROR(machine, COMSETTER(MonitorCount)(monitorcount));
4636 }
4637 if (bioslogofadein)
4638 {
4639 if (strcmp(bioslogofadein, "on") == 0)
4640 {
4641 CHECK_ERROR(biosSettings, COMSETTER(LogoFadeIn)(true));
4642 }
4643 else if (strcmp(bioslogofadein, "off") == 0)
4644 {
4645 CHECK_ERROR(biosSettings, COMSETTER(LogoFadeIn)(false));
4646 }
4647 else
4648 {
4649 errorArgument("Invalid -bioslogofadein argument '%s'", bioslogofadein);
4650 rc = E_FAIL;
4651 break;
4652 }
4653 }
4654 if (bioslogofadeout)
4655 {
4656 if (strcmp(bioslogofadeout, "on") == 0)
4657 {
4658 CHECK_ERROR(biosSettings, COMSETTER(LogoFadeOut)(true));
4659 }
4660 else if (strcmp(bioslogofadeout, "off") == 0)
4661 {
4662 CHECK_ERROR(biosSettings, COMSETTER(LogoFadeOut)(false));
4663 }
4664 else
4665 {
4666 errorArgument("Invalid -bioslogofadeout argument '%s'", bioslogofadeout);
4667 rc = E_FAIL;
4668 break;
4669 }
4670 }
4671 if (bioslogodisplaytime != ~0U)
4672 {
4673 CHECK_ERROR(biosSettings, COMSETTER(LogoDisplayTime)(bioslogodisplaytime));
4674 }
4675 if (bioslogoimagepath)
4676 {
4677 CHECK_ERROR(biosSettings, COMSETTER(LogoImagePath)(Bstr(bioslogoimagepath)));
4678 }
4679 if (biosbootmenumode)
4680 {
4681 if (strcmp(biosbootmenumode, "disabled") == 0)
4682 CHECK_ERROR(biosSettings, COMSETTER(BootMenuMode)(BIOSBootMenuMode_Disabled));
4683 else if (strcmp(biosbootmenumode, "menuonly") == 0)
4684 CHECK_ERROR(biosSettings, COMSETTER(BootMenuMode)(BIOSBootMenuMode_MenuOnly));
4685 else if (strcmp(biosbootmenumode, "messageandmenu") == 0)
4686 CHECK_ERROR(biosSettings, COMSETTER(BootMenuMode)(BIOSBootMenuMode_MessageAndMenu));
4687 else
4688 {
4689 errorArgument("Invalid -biosbootmenu argument '%s'", biosbootmenumode);
4690 rc = E_FAIL;
4691 break;
4692 }
4693
4694 }
4695 if (biossystemtimeoffset)
4696 {
4697 LONG64 timeOffset = RTStrToInt64(biossystemtimeoffset);
4698 CHECK_ERROR(biosSettings, COMSETTER(TimeOffset)(timeOffset));
4699 }
4700 if (biospxedebug)
4701 {
4702 if (strcmp(biospxedebug, "on") == 0)
4703 {
4704 CHECK_ERROR(biosSettings, COMSETTER(PXEDebugEnabled)(true));
4705 }
4706 else if (strcmp(biospxedebug, "off") == 0)
4707 {
4708 CHECK_ERROR(biosSettings, COMSETTER(PXEDebugEnabled)(false));
4709 }
4710 else
4711 {
4712 errorArgument("Invalid -biospxedebug argument '%s'", biospxedebug);
4713 rc = E_FAIL;
4714 break;
4715 }
4716 }
4717 for (int curBootDev = 0; curBootDev < 4; curBootDev++)
4718 {
4719 if (bootDeviceChanged[curBootDev])
4720 CHECK_ERROR(machine, SetBootOrder (curBootDev + 1, bootDevice[curBootDev]));
4721 }
4722 if (hdds[0])
4723 {
4724 if (strcmp(hdds[0], "none") == 0)
4725 {
4726 machine->DetachHardDisk(StorageBus_IDE, 0, 0);
4727 }
4728 else
4729 {
4730 /* first guess is that it's a UUID */
4731 Guid uuid(hdds[0]);
4732 ComPtr<IHardDisk> hardDisk;
4733 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
4734 /* not successful? Then it must be a filename */
4735 if (!hardDisk)
4736 {
4737 CHECK_ERROR(virtualBox, OpenHardDisk(Bstr(hdds[0]), hardDisk.asOutParam()));
4738 if (SUCCEEDED(rc) && hardDisk)
4739 {
4740 /* first check if it's already registered */
4741 Guid hddUUID;
4742 hardDisk->COMGETTER(Id)(hddUUID.asOutParam());
4743 ComPtr<IHardDisk> registeredHDD;
4744 rc = virtualBox->GetHardDisk(hddUUID, registeredHDD.asOutParam());
4745 if (SUCCEEDED(rc) && registeredHDD)
4746 hardDisk = registeredHDD;
4747 else
4748 {
4749 /* it has to be registered */
4750 CHECK_ERROR(virtualBox, RegisterHardDisk(hardDisk));
4751 if (FAILED(rc))
4752 break;
4753 }
4754 }
4755 }
4756 if (hardDisk)
4757 {
4758 hardDisk->COMGETTER(Id)(uuid.asOutParam());
4759 CHECK_ERROR(machine, AttachHardDisk(uuid, StorageBus_IDE, 0, 0));
4760 }
4761 else
4762 rc = E_FAIL;
4763 if (FAILED(rc))
4764 break;
4765 }
4766 }
4767 if (hdds[1])
4768 {
4769 if (strcmp(hdds[1], "none") == 0)
4770 {
4771 machine->DetachHardDisk(StorageBus_IDE, 0, 1);
4772 }
4773 else
4774 {
4775 /* first guess is that it's a UUID */
4776 Guid uuid(hdds[1]);
4777 ComPtr<IHardDisk> hardDisk;
4778 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
4779 /* not successful? Then it must be a filename */
4780 if (!hardDisk)
4781 {
4782 CHECK_ERROR(virtualBox, OpenHardDisk(Bstr(hdds[1]), hardDisk.asOutParam()));
4783 if (SUCCEEDED(rc) && hardDisk)
4784 {
4785 /* first check if it's already registered */
4786 Guid hddUUID;
4787 hardDisk->COMGETTER(Id)(hddUUID.asOutParam());
4788 ComPtr<IHardDisk> registeredHDD;
4789 rc = virtualBox->GetHardDisk(hddUUID, registeredHDD.asOutParam());
4790 if (SUCCEEDED(rc) && registeredHDD)
4791 hardDisk = registeredHDD;
4792 else
4793 {
4794 /* it has to be registered */
4795 CHECK_ERROR(virtualBox, RegisterHardDisk(hardDisk));
4796 if (FAILED(rc))
4797 break;
4798 }
4799 }
4800 }
4801 if (hardDisk)
4802 {
4803 hardDisk->COMGETTER(Id)(uuid.asOutParam());
4804 CHECK_ERROR(machine, AttachHardDisk(uuid, StorageBus_IDE, 0, 1));
4805 }
4806 else
4807 rc = E_FAIL;
4808 if (FAILED(rc))
4809 break;
4810 }
4811 }
4812 if (hdds[2])
4813 {
4814 if (strcmp(hdds[2], "none") == 0)
4815 {
4816 machine->DetachHardDisk(StorageBus_IDE, 1, 1);
4817 }
4818 else
4819 {
4820 /* first guess is that it's a UUID */
4821 Guid uuid(hdds[2]);
4822 ComPtr<IHardDisk> hardDisk;
4823 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
4824 /* not successful? Then it must be a filename */
4825 if (!hardDisk)
4826 {
4827 CHECK_ERROR(virtualBox, OpenHardDisk(Bstr(hdds[2]), hardDisk.asOutParam()));
4828 if (SUCCEEDED(rc) && hardDisk)
4829 {
4830 /* first check if it's already registered */
4831 Guid hddUUID;
4832 hardDisk->COMGETTER(Id)(hddUUID.asOutParam());
4833 ComPtr<IHardDisk> registeredHDD;
4834 rc = virtualBox->GetHardDisk(hddUUID, registeredHDD.asOutParam());
4835 if (SUCCEEDED(rc) && registeredHDD)
4836 hardDisk = registeredHDD;
4837 else
4838 {
4839 /* it has to be registered */
4840 CHECK_ERROR(virtualBox, RegisterHardDisk(hardDisk));
4841 if (FAILED(rc))
4842 break;
4843 }
4844 }
4845 }
4846 if (hardDisk)
4847 {
4848 hardDisk->COMGETTER(Id)(uuid.asOutParam());
4849 CHECK_ERROR(machine, AttachHardDisk(uuid, StorageBus_IDE, 1, 1));
4850 }
4851 else
4852 rc = E_FAIL;
4853 if (FAILED(rc))
4854 break;
4855 }
4856 }
4857 if (dvd)
4858 {
4859 ComPtr<IDVDDrive> dvdDrive;
4860 machine->COMGETTER(DVDDrive)(dvdDrive.asOutParam());
4861 ASSERT(dvdDrive);
4862
4863 /* unmount? */
4864 if (strcmp(dvd, "none") == 0)
4865 {
4866 CHECK_ERROR(dvdDrive, Unmount());
4867 }
4868 /* host drive? */
4869 else if (strncmp(dvd, "host:", 5) == 0)
4870 {
4871 ComPtr<IHost> host;
4872 CHECK_ERROR(virtualBox, COMGETTER(Host)(host.asOutParam()));
4873 ComPtr<IHostDVDDriveCollection> hostDVDs;
4874 CHECK_ERROR(host, COMGETTER(DVDDrives)(hostDVDs.asOutParam()));
4875 ComPtr<IHostDVDDrive> hostDVDDrive;
4876 rc = hostDVDs->FindByName(Bstr(dvd + 5), hostDVDDrive.asOutParam());
4877 if (!hostDVDDrive)
4878 {
4879 /* 2nd try: try with the real name, important on Linux+libhal */
4880 char szPathReal[RTPATH_MAX];
4881 if (VBOX_FAILURE(RTPathReal(dvd + 5, szPathReal, sizeof(szPathReal))))
4882 {
4883 errorArgument("Invalid host DVD drive name");
4884 rc = E_FAIL;
4885 break;
4886 }
4887 rc = hostDVDs->FindByName(Bstr(szPathReal), hostDVDDrive.asOutParam());
4888 if (!hostDVDDrive)
4889 {
4890 errorArgument("Invalid host DVD drive name");
4891 rc = E_FAIL;
4892 break;
4893 }
4894 }
4895 CHECK_ERROR(dvdDrive, CaptureHostDrive(hostDVDDrive));
4896 }
4897 else
4898 {
4899 /* first assume it's a UUID */
4900 Guid uuid(dvd);
4901 ComPtr<IDVDImage> dvdImage;
4902 rc = virtualBox->GetDVDImage(uuid, dvdImage.asOutParam());
4903 if (FAILED(rc) || !dvdImage)
4904 {
4905 /* must be a filename, check if it's in the collection */
4906 ComPtr<IDVDImageCollection> dvdImages;
4907 virtualBox->COMGETTER(DVDImages)(dvdImages.asOutParam());
4908 rc = dvdImages->FindByPath(Bstr(dvd), dvdImage.asOutParam());
4909 /* not registered, do that on the fly */
4910 if (!dvdImage)
4911 {
4912 Guid emptyUUID;
4913 CHECK_ERROR(virtualBox, OpenDVDImage(Bstr(dvd), emptyUUID, dvdImage.asOutParam()));
4914 if (SUCCEEDED(rc) && dvdImage)
4915 {
4916 /* time to register the image */
4917 CHECK_ERROR(virtualBox, RegisterDVDImage(dvdImage));
4918 }
4919 }
4920 }
4921 if (!dvdImage)
4922 {
4923 rc = E_FAIL;
4924 break;
4925 }
4926
4927 dvdImage->COMGETTER(Id)(uuid.asOutParam());
4928 CHECK_ERROR(dvdDrive, MountImage(uuid));
4929 }
4930 }
4931 if (dvdpassthrough)
4932 {
4933 ComPtr<IDVDDrive> dvdDrive;
4934 machine->COMGETTER(DVDDrive)(dvdDrive.asOutParam());
4935 ASSERT(dvdDrive);
4936
4937 CHECK_ERROR(dvdDrive, COMSETTER(Passthrough)(strcmp(dvdpassthrough, "on") == 0));
4938 }
4939 if (idecontroller)
4940 {
4941 if (RTStrICmp(idecontroller, "PIIX3") == 0)
4942 {
4943 CHECK_ERROR(biosSettings, COMSETTER(IDEControllerType)(IDEControllerType_PIIX3));
4944 }
4945 else if (RTStrICmp(idecontroller, "PIIX4") == 0)
4946 {
4947 CHECK_ERROR(biosSettings, COMSETTER(IDEControllerType)(IDEControllerType_PIIX4));
4948 }
4949 else
4950 {
4951 errorArgument("Invalid -idecontroller argument '%s'", idecontroller);
4952 rc = E_FAIL;
4953 break;
4954 }
4955 }
4956 if (floppy)
4957 {
4958 ComPtr<IFloppyDrive> floppyDrive;
4959 machine->COMGETTER(FloppyDrive)(floppyDrive.asOutParam());
4960 ASSERT(floppyDrive);
4961
4962 /* disable? */
4963 if (strcmp(floppy, "disabled") == 0)
4964 {
4965 /* disable the controller */
4966 CHECK_ERROR(floppyDrive, COMSETTER(Enabled)(false));
4967 }
4968 else
4969 {
4970 /* enable the controller */
4971 CHECK_ERROR(floppyDrive, COMSETTER(Enabled)(true));
4972
4973 /* unmount? */
4974 if (strcmp(floppy, "empty") == 0)
4975 {
4976 CHECK_ERROR(floppyDrive, Unmount());
4977 }
4978 /* host drive? */
4979 else if (strncmp(floppy, "host:", 5) == 0)
4980 {
4981 ComPtr<IHost> host;
4982 CHECK_ERROR(virtualBox, COMGETTER(Host)(host.asOutParam()));
4983 ComPtr<IHostFloppyDriveCollection> hostFloppies;
4984 CHECK_ERROR(host, COMGETTER(FloppyDrives)(hostFloppies.asOutParam()));
4985 ComPtr<IHostFloppyDrive> hostFloppyDrive;
4986 rc = hostFloppies->FindByName(Bstr(floppy + 5), hostFloppyDrive.asOutParam());
4987 if (!hostFloppyDrive)
4988 {
4989 errorArgument("Invalid host floppy drive name");
4990 rc = E_FAIL;
4991 break;
4992 }
4993 CHECK_ERROR(floppyDrive, CaptureHostDrive(hostFloppyDrive));
4994 }
4995 else
4996 {
4997 /* first assume it's a UUID */
4998 Guid uuid(floppy);
4999 ComPtr<IFloppyImage> floppyImage;
5000 rc = virtualBox->GetFloppyImage(uuid, floppyImage.asOutParam());
5001 if (FAILED(rc) || !floppyImage)
5002 {
5003 /* must be a filename */
5004 Guid emptyUUID;
5005 CHECK_ERROR(virtualBox, OpenFloppyImage(Bstr(floppy), emptyUUID, floppyImage.asOutParam()));
5006 if (SUCCEEDED(rc) && floppyImage)
5007 {
5008 /** @todo first iterate through the collection and try to find the image */
5009 /* time to register the image */
5010 CHECK_ERROR(virtualBox, RegisterFloppyImage(floppyImage));
5011 }
5012 }
5013 if (!floppyImage)
5014 {
5015 rc = E_FAIL;
5016 break;
5017 }
5018
5019 floppyImage->COMGETTER(Id)(uuid.asOutParam());
5020 CHECK_ERROR(floppyDrive, MountImage(uuid));
5021 }
5022 }
5023 }
5024 if (audio || audiocontroller)
5025 {
5026 ComPtr<IAudioAdapter> audioAdapter;
5027 machine->COMGETTER(AudioAdapter)(audioAdapter.asOutParam());
5028 ASSERT(audioAdapter);
5029
5030 if (audio)
5031 {
5032 /* disable? */
5033 if (strcmp(audio, "none") == 0)
5034 {
5035 CHECK_ERROR(audioAdapter, COMSETTER(Enabled)(false));
5036 }
5037 else if (strcmp(audio, "null") == 0)
5038 {
5039 CHECK_ERROR(audioAdapter, COMSETTER(AudioDriver)(AudioDriverType_Null));
5040 CHECK_ERROR(audioAdapter, COMSETTER(Enabled)(true));
5041 }
5042#ifdef RT_OS_WINDOWS
5043#ifdef VBOX_WITH_WINMM
5044 else if (strcmp(audio, "winmm") == 0)
5045 {
5046 CHECK_ERROR(audioAdapter, COMSETTER(AudioDriver)(AudioDriverType_WinMM));
5047 CHECK_ERROR(audioAdapter, COMSETTER(Enabled)(true));
5048 }
5049#endif
5050 else if (strcmp(audio, "dsound") == 0)
5051 {
5052 CHECK_ERROR(audioAdapter, COMSETTER(AudioDriver)(AudioDriverType_DirectSound));
5053 CHECK_ERROR(audioAdapter, COMSETTER(Enabled)(true));
5054 }
5055#endif /* RT_OS_WINDOWS */
5056#ifdef RT_OS_LINUX
5057 else if (strcmp(audio, "oss") == 0)
5058 {
5059 CHECK_ERROR(audioAdapter, COMSETTER(AudioDriver)(AudioDriverType_OSS));
5060 CHECK_ERROR(audioAdapter, COMSETTER(Enabled)(true));
5061 }
5062# ifdef VBOX_WITH_ALSA
5063 else if (strcmp(audio, "alsa") == 0)
5064 {
5065 CHECK_ERROR(audioAdapter, COMSETTER(AudioDriver)(AudioDriverType_ALSA));
5066 CHECK_ERROR(audioAdapter, COMSETTER(Enabled)(true));
5067 }
5068# endif
5069# ifdef VBOX_WITH_PULSE
5070 else if (strcmp(audio, "pulse") == 0)
5071 {
5072 CHECK_ERROR(audioAdapter, COMSETTER(AudioDriver)(AudioDriverType_Pulse));
5073 CHECK_ERROR(audioAdapter, COMSETTER(Enabled)(true));
5074 }
5075# endif
5076#endif /* !RT_OS_LINUX */
5077#ifdef RT_OS_SOLARIS
5078 else if (strcmp(audio, "solaudio") == 0)
5079 {
5080 CHECK_ERROR(audioAdapter, COMSETTER(AudioDriver)(AudioDriverType_SolAudio));
5081 CHECK_ERROR(audioAdapter, COMSETTER(Enabled)(true));
5082 }
5083
5084#endif /* !RT_OS_SOLARIS */
5085#ifdef RT_OS_DARWIN
5086 else if (strcmp(audio, "coreaudio") == 0)
5087 {
5088 CHECK_ERROR(audioAdapter, COMSETTER(AudioDriver)(AudioDriverType_CoreAudio));
5089 CHECK_ERROR(audioAdapter, COMSETTER(Enabled)(true));
5090 }
5091
5092#endif /* !RT_OS_DARWIN */
5093 else
5094 {
5095 errorArgument("Invalid -audio argument '%s'", audio);
5096 rc = E_FAIL;
5097 break;
5098 }
5099 }
5100 if (audiocontroller)
5101 {
5102 if (strcmp(audiocontroller, "sb16") == 0)
5103 CHECK_ERROR(audioAdapter, COMSETTER(AudioController)(AudioControllerType_SB16));
5104 else if (strcmp(audiocontroller, "ac97") == 0)
5105 CHECK_ERROR(audioAdapter, COMSETTER(AudioController)(AudioControllerType_AC97));
5106 else
5107 {
5108 errorArgument("Invalid -audiocontroller argument '%s'", audiocontroller);
5109 rc = E_FAIL;
5110 break;
5111 }
5112 }
5113 }
5114 /* Shared clipboard state */
5115 if (clipboard)
5116 {
5117/* ComPtr<IClipboardMode> clipboardMode;
5118 machine->COMGETTER(ClipboardMode)(clipboardMode.asOutParam());
5119 ASSERT(clipboardMode);
5120*/
5121 if (strcmp(clipboard, "disabled") == 0)
5122 {
5123 CHECK_ERROR(machine, COMSETTER(ClipboardMode)(ClipboardMode_Disabled));
5124 }
5125 else if (strcmp(clipboard, "hosttoguest") == 0)
5126 {
5127 CHECK_ERROR(machine, COMSETTER(ClipboardMode)(ClipboardMode_HostToGuest));
5128 }
5129 else if (strcmp(clipboard, "guesttohost") == 0)
5130 {
5131 CHECK_ERROR(machine, COMSETTER(ClipboardMode)(ClipboardMode_GuestToHost));
5132 }
5133 else if (strcmp(clipboard, "bidirectional") == 0)
5134 {
5135 CHECK_ERROR(machine, COMSETTER(ClipboardMode)(ClipboardMode_Bidirectional));
5136 }
5137 else
5138 {
5139 errorArgument("Invalid -clipboard argument '%s'", clipboard);
5140 rc = E_FAIL;
5141 break;
5142 }
5143 }
5144 /* iterate through all possible NICs */
5145 for (ULONG n = 0; n < NetworkAdapterCount; n ++)
5146 {
5147 ComPtr<INetworkAdapter> nic;
5148 CHECK_ERROR_RET (machine, GetNetworkAdapter (n, nic.asOutParam()), 1);
5149
5150 ASSERT(nic);
5151
5152 /* something about the NIC? */
5153 if (nics[n])
5154 {
5155 if (strcmp(nics[n], "none") == 0)
5156 {
5157 CHECK_ERROR_RET(nic, COMSETTER(Enabled) (FALSE), 1);
5158 }
5159 else if (strcmp(nics[n], "null") == 0)
5160 {
5161 CHECK_ERROR_RET(nic, COMSETTER(Enabled) (TRUE), 1);
5162 CHECK_ERROR_RET(nic, Detach(), 1);
5163 }
5164 else if (strcmp(nics[n], "nat") == 0)
5165 {
5166 CHECK_ERROR_RET(nic, COMSETTER(Enabled) (TRUE), 1);
5167 CHECK_ERROR_RET(nic, AttachToNAT(), 1);
5168 }
5169 else if (strcmp(nics[n], "hostif") == 0)
5170 {
5171 CHECK_ERROR_RET(nic, COMSETTER(Enabled) (TRUE), 1);
5172 CHECK_ERROR_RET(nic, AttachToHostInterface(), 1);
5173 }
5174 else if (strcmp(nics[n], "intnet") == 0)
5175 {
5176 CHECK_ERROR_RET(nic, COMSETTER(Enabled) (TRUE), 1);
5177 CHECK_ERROR_RET(nic, AttachToInternalNetwork(), 1);
5178 }
5179 else
5180 {
5181 errorArgument("Invalid type '%s' specfied for NIC %lu", nics[n], n + 1);
5182 rc = E_FAIL;
5183 break;
5184 }
5185 }
5186
5187 /* something about the NIC type? */
5188 if (nictype[n])
5189 {
5190 if (strcmp(nictype[n], "Am79C970A") == 0)
5191 {
5192 CHECK_ERROR_RET(nic, COMSETTER(AdapterType)(NetworkAdapterType_Am79C970A), 1);
5193 }
5194 else if (strcmp(nictype[n], "Am79C973") == 0)
5195 {
5196 CHECK_ERROR_RET(nic, COMSETTER(AdapterType)(NetworkAdapterType_Am79C973), 1);
5197 }
5198#ifdef VBOX_WITH_E1000
5199 else if (strcmp(nictype[n], "82540EM") == 0)
5200 {
5201 CHECK_ERROR_RET(nic, COMSETTER(AdapterType)(NetworkAdapterType_I82540EM), 1);
5202 }
5203 else if (strcmp(nictype[n], "82543GC") == 0)
5204 {
5205 CHECK_ERROR_RET(nic, COMSETTER(AdapterType)(NetworkAdapterType_I82543GC), 1);
5206 }
5207#endif
5208 else
5209 {
5210 errorArgument("Invalid NIC type '%s' specified for NIC %lu", nictype[n], n + 1);
5211 rc = E_FAIL;
5212 break;
5213 }
5214 }
5215
5216 /* something about the MAC address? */
5217 if (macs[n])
5218 {
5219 /* generate one? */
5220 if (strcmp(macs[n], "auto") == 0)
5221 {
5222 CHECK_ERROR_RET(nic, COMSETTER(MACAddress)(NULL), 1);
5223 }
5224 else
5225 {
5226 CHECK_ERROR_RET(nic, COMSETTER(MACAddress)(Bstr(macs[n])), 1);
5227 }
5228 }
5229
5230 /* something about the reported link speed? */
5231 if (nicspeed[n])
5232 {
5233 uint32_t u32LineSpeed;
5234
5235 u32LineSpeed = atoi(nicspeed[n]);
5236
5237 if (u32LineSpeed < 1000 || u32LineSpeed > 4000000)
5238 {
5239 errorArgument("Invalid -nicspeed%lu argument '%s'", n + 1, nicspeed[n]);
5240 rc = E_FAIL;
5241 break;
5242 }
5243 CHECK_ERROR_RET(nic, COMSETTER(LineSpeed)(u32LineSpeed), 1);
5244 }
5245
5246 /* the link status flag? */
5247 if (cableconnected[n])
5248 {
5249 if (strcmp(cableconnected[n], "on") == 0)
5250 {
5251 CHECK_ERROR_RET(nic, COMSETTER(CableConnected)(TRUE), 1);
5252 }
5253 else if (strcmp(cableconnected[n], "off") == 0)
5254 {
5255 CHECK_ERROR_RET(nic, COMSETTER(CableConnected)(FALSE), 1);
5256 }
5257 else
5258 {
5259 errorArgument("Invalid -cableconnected%lu argument '%s'", n + 1, cableconnected[n]);
5260 rc = E_FAIL;
5261 break;
5262 }
5263 }
5264
5265 /* the trace flag? */
5266 if (nictrace[n])
5267 {
5268 if (strcmp(nictrace[n], "on") == 0)
5269 {
5270 CHECK_ERROR_RET(nic, COMSETTER(TraceEnabled)(TRUE), 1);
5271 }
5272 else if (strcmp(nictrace[n], "off") == 0)
5273 {
5274 CHECK_ERROR_RET(nic, COMSETTER(TraceEnabled)(FALSE), 1);
5275 }
5276 else
5277 {
5278 errorArgument("Invalid -nictrace%lu argument '%s'", n + 1, nictrace[n]);
5279 rc = E_FAIL;
5280 break;
5281 }
5282 }
5283
5284 /* the tracefile flag? */
5285 if (nictracefile[n])
5286 {
5287 CHECK_ERROR_RET(nic, COMSETTER(TraceFile)(Bstr(nictracefile[n])), 1);
5288 }
5289
5290 /* the host interface device? */
5291 if (hostifdev[n])
5292 {
5293 /* remove it? */
5294 if (strcmp(hostifdev[n], "none") == 0)
5295 {
5296 CHECK_ERROR_RET(nic, COMSETTER(HostInterface)(NULL), 1);
5297 }
5298 else
5299 {
5300 CHECK_ERROR_RET(nic, COMSETTER(HostInterface)(Bstr(hostifdev[n])), 1);
5301 }
5302 }
5303
5304 /* the internal network name? */
5305 if (intnet[n])
5306 {
5307 /* remove it? */
5308 if (strcmp(intnet[n], "none") == 0)
5309 {
5310 CHECK_ERROR_RET(nic, COMSETTER(InternalNetwork)(NULL), 1);
5311 }
5312 else
5313 {
5314 CHECK_ERROR_RET(nic, COMSETTER(InternalNetwork)(Bstr(intnet[n])), 1);
5315 }
5316 }
5317 /* the network of the NAT */
5318 if (natnet[n])
5319 {
5320 CHECK_ERROR_RET(nic, COMSETTER(NATNetwork)(Bstr(natnet[n])), 1);
5321 }
5322#ifdef RT_OS_LINUX
5323 /* the TAP setup application? */
5324 if (tapsetup[n])
5325 {
5326 /* remove it? */
5327 if (strcmp(tapsetup[n], "none") == 0)
5328 {
5329 CHECK_ERROR_RET(nic, COMSETTER(TAPSetupApplication)(NULL), 1);
5330 }
5331 else
5332 {
5333 CHECK_ERROR_RET(nic, COMSETTER(TAPSetupApplication)(Bstr(tapsetup[n])), 1);
5334 }
5335 }
5336
5337 /* the TAP terminate application? */
5338 if (tapterm[n])
5339 {
5340 /* remove it? */
5341 if (strcmp(tapterm[n], "none") == 0)
5342 {
5343 CHECK_ERROR_RET(nic, COMSETTER(TAPTerminateApplication)(NULL), 1);
5344 }
5345 else
5346 {
5347 CHECK_ERROR_RET(nic, COMSETTER(TAPTerminateApplication)(Bstr(tapterm[n])), 1);
5348 }
5349 }
5350#endif /* RT_OS_LINUX */
5351
5352 }
5353 if (FAILED(rc))
5354 break;
5355
5356 /* iterate through all possible serial ports */
5357 for (ULONG n = 0; n < SerialPortCount; n ++)
5358 {
5359 ComPtr<ISerialPort> uart;
5360 CHECK_ERROR_RET (machine, GetSerialPort (n, uart.asOutParam()), 1);
5361
5362 ASSERT(uart);
5363
5364 if (uarts_base[n])
5365 {
5366 if (uarts_base[n] == (ULONG)-1)
5367 {
5368 CHECK_ERROR_RET(uart, COMSETTER(Enabled) (FALSE), 1);
5369 }
5370 else
5371 {
5372 CHECK_ERROR_RET(uart, COMSETTER(IOBase) (uarts_base[n]), 1);
5373 CHECK_ERROR_RET(uart, COMSETTER(IRQ) (uarts_irq[n]), 1);
5374 CHECK_ERROR_RET(uart, COMSETTER(Enabled) (TRUE), 1);
5375 }
5376 }
5377 if (uarts_mode[n])
5378 {
5379 if (strcmp(uarts_mode[n], "disconnected") == 0)
5380 {
5381 CHECK_ERROR_RET(uart, COMSETTER(HostMode) (PortMode_Disconnected), 1);
5382 }
5383 else
5384 {
5385 if (strcmp(uarts_mode[n], "server") == 0)
5386 {
5387 CHECK_ERROR_RET(uart, COMSETTER(HostMode) (PortMode_HostPipe), 1);
5388 CHECK_ERROR_RET(uart, COMSETTER(Server) (TRUE), 1);
5389 }
5390 else if (strcmp(uarts_mode[n], "client") == 0)
5391 {
5392 CHECK_ERROR_RET(uart, COMSETTER(HostMode) (PortMode_HostPipe), 1);
5393 CHECK_ERROR_RET(uart, COMSETTER(Server) (FALSE), 1);
5394 }
5395 else
5396 {
5397 CHECK_ERROR_RET(uart, COMSETTER(HostMode) (PortMode_HostDevice), 1);
5398 }
5399 CHECK_ERROR_RET(uart, COMSETTER(Path) (Bstr(uarts_path[n])), 1);
5400 }
5401 }
5402 }
5403 if (FAILED(rc))
5404 break;
5405
5406#ifdef VBOX_WITH_VRDP
5407 if (vrdp || (vrdpport != UINT16_MAX) || vrdpaddress || vrdpauthtype || vrdpmulticon || vrdpreusecon)
5408 {
5409 ComPtr<IVRDPServer> vrdpServer;
5410 machine->COMGETTER(VRDPServer)(vrdpServer.asOutParam());
5411 ASSERT(vrdpServer);
5412 if (vrdpServer)
5413 {
5414 if (vrdp)
5415 {
5416 if (strcmp(vrdp, "on") == 0)
5417 {
5418 CHECK_ERROR(vrdpServer, COMSETTER(Enabled)(true));
5419 }
5420 else if (strcmp(vrdp, "off") == 0)
5421 {
5422 CHECK_ERROR(vrdpServer, COMSETTER(Enabled)(false));
5423 }
5424 else
5425 {
5426 errorArgument("Invalid -vrdp argument '%s'", vrdp);
5427 rc = E_FAIL;
5428 break;
5429 }
5430 }
5431 if (vrdpport != UINT16_MAX)
5432 {
5433 CHECK_ERROR(vrdpServer, COMSETTER(Port)(vrdpport));
5434 }
5435 if (vrdpaddress)
5436 {
5437 CHECK_ERROR(vrdpServer, COMSETTER(NetAddress)(Bstr(vrdpaddress)));
5438 }
5439 if (vrdpauthtype)
5440 {
5441 if (strcmp(vrdpauthtype, "null") == 0)
5442 {
5443 CHECK_ERROR(vrdpServer, COMSETTER(AuthType)(VRDPAuthType_Null));
5444 }
5445 else if (strcmp(vrdpauthtype, "external") == 0)
5446 {
5447 CHECK_ERROR(vrdpServer, COMSETTER(AuthType)(VRDPAuthType_External));
5448 }
5449 else if (strcmp(vrdpauthtype, "guest") == 0)
5450 {
5451 CHECK_ERROR(vrdpServer, COMSETTER(AuthType)(VRDPAuthType_Guest));
5452 }
5453 else
5454 {
5455 errorArgument("Invalid -vrdpauthtype argument '%s'", vrdpauthtype);
5456 rc = E_FAIL;
5457 break;
5458 }
5459 }
5460 if (vrdpmulticon)
5461 {
5462 if (strcmp(vrdpmulticon, "on") == 0)
5463 {
5464 CHECK_ERROR(vrdpServer, COMSETTER(AllowMultiConnection)(true));
5465 }
5466 else if (strcmp(vrdpmulticon, "off") == 0)
5467 {
5468 CHECK_ERROR(vrdpServer, COMSETTER(AllowMultiConnection)(false));
5469 }
5470 else
5471 {
5472 errorArgument("Invalid -vrdpmulticon argument '%s'", vrdpmulticon);
5473 rc = E_FAIL;
5474 break;
5475 }
5476 }
5477 if (vrdpreusecon)
5478 {
5479 if (strcmp(vrdpreusecon, "on") == 0)
5480 {
5481 CHECK_ERROR(vrdpServer, COMSETTER(ReuseSingleConnection)(true));
5482 }
5483 else if (strcmp(vrdpreusecon, "off") == 0)
5484 {
5485 CHECK_ERROR(vrdpServer, COMSETTER(ReuseSingleConnection)(false));
5486 }
5487 else
5488 {
5489 errorArgument("Invalid -vrdpreusecon argument '%s'", vrdpreusecon);
5490 rc = E_FAIL;
5491 break;
5492 }
5493 }
5494 }
5495 }
5496#endif /* VBOX_WITH_VRDP */
5497
5498 /*
5499 * USB enable/disable
5500 */
5501 if (fUsbEnabled != -1)
5502 {
5503 ComPtr<IUSBController> UsbCtl;
5504 CHECK_ERROR(machine, COMGETTER(USBController)(UsbCtl.asOutParam()));
5505 if (SUCCEEDED(rc))
5506 {
5507 CHECK_ERROR(UsbCtl, COMSETTER(Enabled)(!!fUsbEnabled));
5508 }
5509 }
5510 /*
5511 * USB EHCI enable/disable
5512 */
5513 if (fUsbEhciEnabled != -1)
5514 {
5515 ComPtr<IUSBController> UsbCtl;
5516 CHECK_ERROR(machine, COMGETTER(USBController)(UsbCtl.asOutParam()));
5517 if (SUCCEEDED(rc))
5518 {
5519 CHECK_ERROR(UsbCtl, COMSETTER(EnabledEhci)(!!fUsbEhciEnabled));
5520 }
5521 }
5522
5523 if (snapshotFolder)
5524 {
5525 if (strcmp(snapshotFolder, "default") == 0)
5526 {
5527 CHECK_ERROR(machine, COMSETTER(SnapshotFolder)(NULL));
5528 }
5529 else
5530 {
5531 CHECK_ERROR(machine, COMSETTER(SnapshotFolder)(Bstr(snapshotFolder)));
5532 }
5533 }
5534
5535 if (guestMemBalloonSize != (ULONG)-1)
5536 CHECK_ERROR(machine, COMSETTER(MemoryBalloonSize)(guestMemBalloonSize));
5537
5538 if (guestStatInterval != (ULONG)-1)
5539 CHECK_ERROR(machine, COMSETTER(StatisticsUpdateInterval)(guestStatInterval));
5540
5541 /*
5542 * SATA controller enable/disable
5543 */
5544 if (fSataEnabled != -1)
5545 {
5546 ComPtr<ISATAController> SataCtl;
5547 CHECK_ERROR(machine, COMGETTER(SATAController)(SataCtl.asOutParam()));
5548 if (SUCCEEDED(rc))
5549 {
5550 CHECK_ERROR(SataCtl, COMSETTER(Enabled)(!!fSataEnabled));
5551 }
5552 }
5553
5554 for (uint32_t i = 4; i < 34; i++)
5555 {
5556 if (hdds[i])
5557 {
5558 if (strcmp(hdds[i], "none") == 0)
5559 {
5560 machine->DetachHardDisk(StorageBus_SATA, i-4, 0);
5561 }
5562 else
5563 {
5564 /* first guess is that it's a UUID */
5565 Guid uuid(hdds[i]);
5566 ComPtr<IHardDisk> hardDisk;
5567 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
5568 /* not successful? Then it must be a filename */
5569 if (!hardDisk)
5570 {
5571 CHECK_ERROR(virtualBox, OpenHardDisk(Bstr(hdds[i]), hardDisk.asOutParam()));
5572 if (SUCCEEDED(rc) && hardDisk)
5573 {
5574 /* first check if it's already registered */
5575 Guid hddUUID;
5576 hardDisk->COMGETTER(Id)(hddUUID.asOutParam());
5577 ComPtr<IHardDisk> registeredHDD;
5578 rc = virtualBox->GetHardDisk(hddUUID, registeredHDD.asOutParam());
5579 if (SUCCEEDED(rc) && registeredHDD)
5580 hardDisk = registeredHDD;
5581 else
5582 {
5583 /* it has to be registered */
5584 CHECK_ERROR(virtualBox, RegisterHardDisk(hardDisk));
5585 if (FAILED(rc))
5586 break;
5587 }
5588 }
5589 }
5590 if (hardDisk)
5591 {
5592 hardDisk->COMGETTER(Id)(uuid.asOutParam());
5593 CHECK_ERROR(machine, AttachHardDisk(uuid, StorageBus_SATA, i-4, 0));
5594 }
5595 else
5596 rc = E_FAIL;
5597 if (FAILED(rc))
5598 break;
5599 }
5600 }
5601 }
5602
5603 for (uint32_t i = 0; i < 4; i++)
5604 {
5605 if (sataBootDevices[i] != -1)
5606 {
5607 ComPtr<ISATAController> SataCtl;
5608 CHECK_ERROR(machine, COMGETTER(SATAController)(SataCtl.asOutParam()));
5609 if (SUCCEEDED(rc))
5610 {
5611 CHECK_ERROR(SataCtl, SetIDEEmulationPort(i, sataBootDevices[i]));
5612 }
5613 }
5614 }
5615
5616 if (sataPortCount != -1)
5617 {
5618 ComPtr<ISATAController> SataCtl;
5619 CHECK_ERROR(machine, COMGETTER(SATAController)(SataCtl.asOutParam()));
5620 if (SUCCEEDED(rc))
5621 {
5622 CHECK_ERROR(SataCtl, COMSETTER(PortCount)(sataPortCount));
5623 }
5624 }
5625
5626 /* commit changes */
5627 CHECK_ERROR(machine, SaveSettings());
5628 }
5629 while (0);
5630
5631 /* it's important to always close sessions */
5632 session->Close();
5633
5634 return SUCCEEDED(rc) ? 0 : 1;
5635}
5636
5637static int handleStartVM(int argc, char *argv[],
5638 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
5639{
5640 HRESULT rc;
5641
5642 if (argc < 1)
5643 return errorSyntax(USAGE_STARTVM, "Not enough parameters");
5644
5645 ComPtr<IMachine> machine;
5646 /* assume it's a UUID */
5647 rc = virtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
5648 if (FAILED(rc) || !machine)
5649 {
5650 /* must be a name */
5651 CHECK_ERROR(virtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
5652 }
5653 if (machine)
5654 {
5655 Guid uuid;
5656 machine->COMGETTER(Id)(uuid.asOutParam());
5657
5658 /* default to GUI session type */
5659 Bstr sessionType = "gui";
5660 /* has a session type been specified? */
5661 if ((argc > 2) && (strcmp(argv[1], "-type") == 0))
5662 {
5663 if (strcmp(argv[2], "gui") == 0)
5664 {
5665 sessionType = "gui";
5666 }
5667 else if (strcmp(argv[2], "vrdp") == 0)
5668 {
5669 sessionType = "vrdp";
5670 }
5671 else if (strcmp(argv[2], "capture") == 0)
5672 {
5673 sessionType = "capture";
5674 }
5675 else
5676 return errorArgument("Invalid session type argument '%s'", argv[2]);
5677 }
5678
5679 Bstr env;
5680#ifdef RT_OS_LINUX
5681 /* make sure the VM process will start on the same display as VBoxManage */
5682 {
5683 const char *display = RTEnvGet ("DISPLAY");
5684 if (display)
5685 env = Utf8StrFmt ("DISPLAY=%s", display);
5686 }
5687#endif
5688 ComPtr<IProgress> progress;
5689 CHECK_ERROR_RET(virtualBox, OpenRemoteSession(session, uuid, sessionType,
5690 env, progress.asOutParam()), rc);
5691 RTPrintf("Waiting for the remote session to open...\n");
5692 CHECK_ERROR_RET(progress, WaitForCompletion (-1), 1);
5693
5694 BOOL completed;
5695 CHECK_ERROR_RET(progress, COMGETTER(Completed)(&completed), rc);
5696 ASSERT(completed);
5697
5698 HRESULT resultCode;
5699 CHECK_ERROR_RET(progress, COMGETTER(ResultCode)(&resultCode), rc);
5700 if (FAILED(resultCode))
5701 {
5702 ComPtr <IVirtualBoxErrorInfo> errorInfo;
5703 CHECK_ERROR_RET(progress, COMGETTER(ErrorInfo)(errorInfo.asOutParam()), 1);
5704 ErrorInfo info (errorInfo);
5705 PRINT_ERROR_INFO(info);
5706 }
5707 else
5708 {
5709 RTPrintf("Remote session has been successfully opened.\n");
5710 }
5711 }
5712
5713 /* it's important to always close sessions */
5714 session->Close();
5715
5716 return SUCCEEDED(rc) ? 0 : 1;
5717}
5718
5719static int handleControlVM(int argc, char *argv[],
5720 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
5721{
5722 HRESULT rc;
5723
5724 if (argc < 2)
5725 return errorSyntax(USAGE_CONTROLVM, "Not enough parameters");
5726
5727 /* try to find the given machine */
5728 ComPtr <IMachine> machine;
5729 Guid uuid (argv[0]);
5730 if (!uuid.isEmpty())
5731 {
5732 CHECK_ERROR (virtualBox, GetMachine (uuid, machine.asOutParam()));
5733 }
5734 else
5735 {
5736 CHECK_ERROR (virtualBox, FindMachine (Bstr(argv[0]), machine.asOutParam()));
5737 if (SUCCEEDED (rc))
5738 machine->COMGETTER(Id) (uuid.asOutParam());
5739 }
5740 if (FAILED (rc))
5741 return 1;
5742
5743 /* open a session for the VM */
5744 CHECK_ERROR_RET (virtualBox, OpenExistingSession (session, uuid), 1);
5745
5746 do
5747 {
5748 /* get the associated console */
5749 ComPtr<IConsole> console;
5750 CHECK_ERROR_BREAK (session, COMGETTER(Console)(console.asOutParam()));
5751 /* ... and session machine */
5752 ComPtr<IMachine> sessionMachine;
5753 CHECK_ERROR_BREAK (session, COMGETTER(Machine)(sessionMachine.asOutParam()));
5754
5755 /* which command? */
5756 if (strcmp(argv[1], "pause") == 0)
5757 {
5758 CHECK_ERROR_BREAK (console, Pause());
5759 }
5760 else if (strcmp(argv[1], "resume") == 0)
5761 {
5762 CHECK_ERROR_BREAK (console, Resume());
5763 }
5764 else if (strcmp(argv[1], "reset") == 0)
5765 {
5766 CHECK_ERROR_BREAK (console, Reset());
5767 }
5768 else if (strcmp(argv[1], "poweroff") == 0)
5769 {
5770 CHECK_ERROR_BREAK (console, PowerDown());
5771 }
5772 else if (strcmp(argv[1], "savestate") == 0)
5773 {
5774 ComPtr<IProgress> progress;
5775 CHECK_ERROR_BREAK (console, SaveState(progress.asOutParam()));
5776
5777 showProgress(progress);
5778
5779 progress->COMGETTER(ResultCode)(&rc);
5780 if (FAILED(rc))
5781 {
5782 com::ProgressErrorInfo info(progress);
5783 if (info.isBasicAvailable())
5784 {
5785 RTPrintf("Error: failed to save machine state. Error message: %lS\n", info.getText().raw());
5786 }
5787 else
5788 {
5789 RTPrintf("Error: failed to save machine state. No error message available!\n");
5790 }
5791 }
5792 }
5793 else if (strcmp(argv[1], "acpipowerbutton") == 0)
5794 {
5795 CHECK_ERROR_BREAK (console, PowerButton());
5796 }
5797 else if (strcmp(argv[1], "acpisleepbutton") == 0)
5798 {
5799 CHECK_ERROR_BREAK (console, SleepButton());
5800 }
5801 else if (strcmp(argv[1], "keyboardputscancode") == 0)
5802 {
5803 ComPtr<IKeyboard> keyboard;
5804 CHECK_ERROR_BREAK(console, COMGETTER(Keyboard)(keyboard.asOutParam()));
5805
5806 if (argc <= 1 + 1)
5807 {
5808 errorArgument("Missing argument to '%s'. Expected IBM PC AT set 2 keyboard scancode(s) as hex byte(s).", argv[1]);
5809 rc = E_FAIL;
5810 break;
5811 }
5812
5813 /* Arbitrary restrict the length of a sequence of scancodes to 1024. */
5814 LONG alScancodes[1024];
5815 int cScancodes = 0;
5816
5817 /* Process the command line. */
5818 int i;
5819 for (i = 1 + 1; i < argc && cScancodes < (int)RT_ELEMENTS(alScancodes); i++, cScancodes++)
5820 {
5821 if ( isxdigit (argv[i][0])
5822 && isxdigit (argv[i][1])
5823 && argv[i][2] == 0)
5824 {
5825 uint8_t u8Scancode;
5826 int rc = RTStrToUInt8Ex(argv[i], NULL, 16, &u8Scancode);
5827 if (RT_FAILURE (rc))
5828 {
5829 RTPrintf("Error: converting '%s' returned %Vrc!\n", argv[i], rc);
5830 rc = E_FAIL;
5831 break;
5832 }
5833
5834 alScancodes[cScancodes] = u8Scancode;
5835 }
5836 else
5837 {
5838 RTPrintf("Error: '%s' is not a hex byte!\n", argv[i]);
5839 rc = E_FAIL;
5840 break;
5841 }
5842 }
5843
5844 if (FAILED(rc))
5845 break;
5846
5847 if ( cScancodes == RT_ELEMENTS(alScancodes)
5848 && i < argc)
5849 {
5850 RTPrintf("Error: too many scancodes, maximum %d allowed!\n", RT_ELEMENTS(alScancodes));
5851 rc = E_FAIL;
5852 break;
5853 }
5854
5855 /* Send scancodes to the VM.
5856 * Note: 'PutScancodes' did not work here. Only the first scancode was transmitted.
5857 */
5858 for (i = 0; i < cScancodes; i++)
5859 {
5860 CHECK_ERROR_BREAK(keyboard, PutScancode(alScancodes[i]));
5861 RTPrintf("Scancode[%d]: 0x%02X\n", i, alScancodes[i]);
5862 }
5863 }
5864 else if (strncmp(argv[1], "setlinkstate", 12) == 0)
5865 {
5866 /* Get the number of network adapters */
5867 ULONG NetworkAdapterCount = 0;
5868 ComPtr <ISystemProperties> info;
5869 CHECK_ERROR_BREAK (virtualBox, COMGETTER(SystemProperties) (info.asOutParam()));
5870 CHECK_ERROR_BREAK (info, COMGETTER(NetworkAdapterCount) (&NetworkAdapterCount));
5871
5872 unsigned n = parseNum(&argv[1][12], NetworkAdapterCount, "NIC");
5873 if (!n)
5874 {
5875 rc = E_FAIL;
5876 break;
5877 }
5878 if (argc <= 1 + 1)
5879 {
5880 errorArgument("Missing argument to '%s'", argv[1]);
5881 rc = E_FAIL;
5882 break;
5883 }
5884 /* get the corresponding network adapter */
5885 ComPtr<INetworkAdapter> adapter;
5886 CHECK_ERROR_BREAK (sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
5887 if (adapter)
5888 {
5889 if (strcmp(argv[2], "on") == 0)
5890 {
5891 CHECK_ERROR_BREAK (adapter, COMSETTER(CableConnected)(TRUE));
5892 }
5893 else if (strcmp(argv[2], "off") == 0)
5894 {
5895 CHECK_ERROR_BREAK (adapter, COMSETTER(CableConnected)(FALSE));
5896 }
5897 else
5898 {
5899 errorArgument("Invalid link state '%s'", Utf8Str(argv[2]).raw());
5900 rc = E_FAIL;
5901 break;
5902 }
5903 }
5904 }
5905 else if (strcmp (argv[1], "usbattach") == 0 ||
5906 strcmp (argv[1], "usbdetach") == 0)
5907 {
5908 if (argc < 3)
5909 {
5910 errorSyntax(USAGE_CONTROLVM, "Not enough parameters");
5911 rc = E_FAIL;
5912 break;
5913 }
5914
5915 bool attach = strcmp (argv[1], "usbattach") == 0;
5916
5917 Guid usbId = argv [2];
5918 if (usbId.isEmpty())
5919 {
5920 // assume address
5921 if (attach)
5922 {
5923 ComPtr <IHost> host;
5924 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host) (host.asOutParam()));
5925 ComPtr <IHostUSBDeviceCollection> coll;
5926 CHECK_ERROR_BREAK (host, COMGETTER(USBDevices) (coll.asOutParam()));
5927 ComPtr <IHostUSBDevice> dev;
5928 CHECK_ERROR_BREAK (coll, FindByAddress (Bstr (argv [2]), dev.asOutParam()));
5929 CHECK_ERROR_BREAK (dev, COMGETTER(Id) (usbId.asOutParam()));
5930 }
5931 else
5932 {
5933 ComPtr <IUSBDeviceCollection> coll;
5934 CHECK_ERROR_BREAK (console, COMGETTER(USBDevices)(coll.asOutParam()));
5935 ComPtr <IUSBDevice> dev;
5936 CHECK_ERROR_BREAK (coll, FindByAddress (Bstr (argv [2]), dev.asOutParam()));
5937 CHECK_ERROR_BREAK (dev, COMGETTER(Id) (usbId.asOutParam()));
5938 }
5939 }
5940
5941 if (attach)
5942 CHECK_ERROR_BREAK (console, AttachUSBDevice (usbId));
5943 else
5944 {
5945 ComPtr <IUSBDevice> dev;
5946 CHECK_ERROR_BREAK (console, DetachUSBDevice (usbId, dev.asOutParam()));
5947 }
5948 }
5949 else if (strcmp(argv[1], "setvideomodehint") == 0)
5950 {
5951 if (argc != 5 && argc != 6)
5952 {
5953 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
5954 rc = E_FAIL;
5955 break;
5956 }
5957 uint32_t xres = atoi(argv[2]);
5958 uint32_t yres = atoi(argv[3]);
5959 uint32_t bpp = atoi(argv[4]);
5960 uint32_t displayIdx = 0;
5961 if (argc == 6)
5962 displayIdx = atoi(argv[5]);
5963
5964 ComPtr<IDisplay> display;
5965 CHECK_ERROR_BREAK(console, COMGETTER(Display)(display.asOutParam()));
5966 CHECK_ERROR_BREAK(display, SetVideoModeHint(xres, yres, bpp, displayIdx));
5967 }
5968 else if (strcmp(argv[1], "setcredentials") == 0)
5969 {
5970 bool fAllowLocalLogon = true;
5971 if (argc == 7)
5972 {
5973 if (strcmp(argv[5], "-allowlocallogon") != 0)
5974 {
5975 errorArgument("Invalid parameter '%s'", argv[5]);
5976 rc = E_FAIL;
5977 break;
5978 }
5979 if (strcmp(argv[6], "no") == 0)
5980 fAllowLocalLogon = false;
5981 }
5982 else if (argc != 5)
5983 {
5984 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
5985 rc = E_FAIL;
5986 break;
5987 }
5988
5989 ComPtr<IGuest> guest;
5990 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
5991 CHECK_ERROR_BREAK(guest, SetCredentials(Bstr(argv[2]), Bstr(argv[3]), Bstr(argv[4]), fAllowLocalLogon));
5992 }
5993 else if (strcmp(argv[1], "dvdattach") == 0)
5994 {
5995 if (argc != 3)
5996 {
5997 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
5998 rc = E_FAIL;
5999 break;
6000 }
6001 ComPtr<IDVDDrive> dvdDrive;
6002 sessionMachine->COMGETTER(DVDDrive)(dvdDrive.asOutParam());
6003 ASSERT(dvdDrive);
6004
6005 /* unmount? */
6006 if (strcmp(argv[2], "none") == 0)
6007 {
6008 CHECK_ERROR(dvdDrive, Unmount());
6009 }
6010 /* host drive? */
6011 else if (strncmp(argv[2], "host:", 5) == 0)
6012 {
6013 ComPtr<IHost> host;
6014 CHECK_ERROR(virtualBox, COMGETTER(Host)(host.asOutParam()));
6015 ComPtr<IHostDVDDriveCollection> hostDVDs;
6016 CHECK_ERROR(host, COMGETTER(DVDDrives)(hostDVDs.asOutParam()));
6017 ComPtr<IHostDVDDrive> hostDVDDrive;
6018 rc = hostDVDs->FindByName(Bstr(argv[2] + 5), hostDVDDrive.asOutParam());
6019 if (!hostDVDDrive)
6020 {
6021 errorArgument("Invalid host DVD drive name");
6022 rc = E_FAIL;
6023 break;
6024 }
6025 CHECK_ERROR(dvdDrive, CaptureHostDrive(hostDVDDrive));
6026 }
6027 else
6028 {
6029 /* first assume it's a UUID */
6030 Guid uuid(argv[2]);
6031 ComPtr<IDVDImage> dvdImage;
6032 rc = virtualBox->GetDVDImage(uuid, dvdImage.asOutParam());
6033 if (FAILED(rc) || !dvdImage)
6034 {
6035 /* must be a filename, check if it's in the collection */
6036 ComPtr<IDVDImageCollection> dvdImages;
6037 virtualBox->COMGETTER(DVDImages)(dvdImages.asOutParam());
6038 rc = dvdImages->FindByPath(Bstr(argv[2]), dvdImage.asOutParam());
6039 /* not registered, do that on the fly */
6040 if (!dvdImage)
6041 {
6042 Guid emptyUUID;
6043 CHECK_ERROR(virtualBox, OpenDVDImage(Bstr(argv[2]), emptyUUID, dvdImage.asOutParam()));
6044 if (SUCCEEDED(rc) && dvdImage)
6045 {
6046 /* time to register the image */
6047 CHECK_ERROR(virtualBox, RegisterDVDImage(dvdImage));
6048 }
6049 }
6050 }
6051 if (!dvdImage)
6052 {
6053 rc = E_FAIL;
6054 break;
6055 }
6056 dvdImage->COMGETTER(Id)(uuid.asOutParam());
6057 CHECK_ERROR(dvdDrive, MountImage(uuid));
6058 }
6059 }
6060 else if (strcmp(argv[1], "floppyattach") == 0)
6061 {
6062 if (argc != 3)
6063 {
6064 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
6065 rc = E_FAIL;
6066 break;
6067 }
6068
6069 ComPtr<IFloppyDrive> floppyDrive;
6070 sessionMachine->COMGETTER(FloppyDrive)(floppyDrive.asOutParam());
6071 ASSERT(floppyDrive);
6072
6073 /* unmount? */
6074 if (strcmp(argv[2], "none") == 0)
6075 {
6076 CHECK_ERROR(floppyDrive, Unmount());
6077 }
6078 /* host drive? */
6079 else if (strncmp(argv[2], "host:", 5) == 0)
6080 {
6081 ComPtr<IHost> host;
6082 CHECK_ERROR(virtualBox, COMGETTER(Host)(host.asOutParam()));
6083 ComPtr<IHostFloppyDriveCollection> hostFloppies;
6084 CHECK_ERROR(host, COMGETTER(FloppyDrives)(hostFloppies.asOutParam()));
6085 ComPtr<IHostFloppyDrive> hostFloppyDrive;
6086 rc = hostFloppies->FindByName(Bstr(argv[2] + 5), hostFloppyDrive.asOutParam());
6087 if (!hostFloppyDrive)
6088 {
6089 errorArgument("Invalid host floppy drive name");
6090 rc = E_FAIL;
6091 break;
6092 }
6093 CHECK_ERROR(floppyDrive, CaptureHostDrive(hostFloppyDrive));
6094 }
6095 else
6096 {
6097 /* first assume it's a UUID */
6098 Guid uuid(argv[2]);
6099 ComPtr<IFloppyImage> floppyImage;
6100 rc = virtualBox->GetFloppyImage(uuid, floppyImage.asOutParam());
6101 if (FAILED(rc) || !floppyImage)
6102 {
6103 /* must be a filename, check if it's in the collection */
6104 ComPtr<IFloppyImageCollection> floppyImages;
6105 virtualBox->COMGETTER(FloppyImages)(floppyImages.asOutParam());
6106 rc = floppyImages->FindByPath(Bstr(argv[2]), floppyImage.asOutParam());
6107 /* not registered, do that on the fly */
6108 if (!floppyImage)
6109 {
6110 Guid emptyUUID;
6111 CHECK_ERROR(virtualBox, OpenFloppyImage(Bstr(argv[2]), emptyUUID, floppyImage.asOutParam()));
6112 if (SUCCEEDED(rc) && floppyImage)
6113 {
6114 /* time to register the image */
6115 CHECK_ERROR(virtualBox, RegisterFloppyImage(floppyImage));
6116 }
6117 }
6118 }
6119 if (!floppyImage)
6120 {
6121 rc = E_FAIL;
6122 break;
6123 }
6124 floppyImage->COMGETTER(Id)(uuid.asOutParam());
6125 CHECK_ERROR(floppyDrive, MountImage(uuid));
6126 }
6127 }
6128#ifdef VBOX_WITH_MEM_BALLOONING
6129 else if (strncmp(argv[1], "-guestmemoryballoon", 19) == 0)
6130 {
6131 if (argc != 3)
6132 {
6133 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
6134 rc = E_FAIL;
6135 break;
6136 }
6137 uint32_t uVal;
6138 int vrc;
6139 vrc = RTStrToUInt32Ex(argv[2], NULL, 0, &uVal);
6140 if (vrc != VINF_SUCCESS)
6141 {
6142 errorArgument("Error parsing guest memory balloon size '%s'", argv[2]);
6143 rc = E_FAIL;
6144 break;
6145 }
6146
6147 /* guest is running; update IGuest */
6148 ComPtr <IGuest> guest;
6149
6150 rc = console->COMGETTER(Guest)(guest.asOutParam());
6151 if (SUCCEEDED(rc))
6152 CHECK_ERROR(guest, COMSETTER(MemoryBalloonSize)(uVal));
6153 }
6154#endif
6155 else if (strncmp(argv[1], "-gueststatisticsinterval", 24) == 0)
6156 {
6157 if (argc != 3)
6158 {
6159 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
6160 rc = E_FAIL;
6161 break;
6162 }
6163 uint32_t uVal;
6164 int vrc;
6165 vrc = RTStrToUInt32Ex(argv[2], NULL, 0, &uVal);
6166 if (vrc != VINF_SUCCESS)
6167 {
6168 errorArgument("Error parsing guest statistics interval '%s'", argv[2]);
6169 rc = E_FAIL;
6170 break;
6171 }
6172
6173 /* guest is running; update IGuest */
6174 ComPtr <IGuest> guest;
6175
6176 rc = console->COMGETTER(Guest)(guest.asOutParam());
6177 if (SUCCEEDED(rc))
6178 CHECK_ERROR(guest, COMSETTER(StatisticsUpdateInterval)(uVal));
6179 }
6180 else
6181 {
6182 errorSyntax(USAGE_CONTROLVM, "Invalid parameter '%s'", Utf8Str(argv[1]).raw());
6183 rc = E_FAIL;
6184 }
6185 }
6186 while (0);
6187
6188 session->Close();
6189
6190 return SUCCEEDED (rc) ? 0 : 1;
6191}
6192
6193static int handleDiscardState(int argc, char *argv[],
6194 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6195{
6196 HRESULT rc;
6197
6198 if (argc != 1)
6199 return errorSyntax(USAGE_DISCARDSTATE, "Incorrect number of parameters");
6200
6201 ComPtr<IMachine> machine;
6202 /* assume it's a UUID */
6203 rc = virtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
6204 if (FAILED(rc) || !machine)
6205 {
6206 /* must be a name */
6207 CHECK_ERROR(virtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
6208 }
6209 if (machine)
6210 {
6211 do
6212 {
6213 /* we have to open a session for this task */
6214 Guid guid;
6215 machine->COMGETTER(Id)(guid.asOutParam());
6216 CHECK_ERROR_BREAK(virtualBox, OpenSession(session, guid));
6217 do
6218 {
6219 ComPtr<IConsole> console;
6220 CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam()));
6221 CHECK_ERROR_BREAK(console, DiscardSavedState());
6222 }
6223 while (0);
6224 CHECK_ERROR_BREAK(session, Close());
6225 }
6226 while (0);
6227 }
6228
6229 return SUCCEEDED(rc) ? 0 : 1;
6230}
6231
6232static int handleAdoptdState(int argc, char *argv[],
6233 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6234{
6235 HRESULT rc;
6236
6237 if (argc != 2)
6238 return errorSyntax(USAGE_ADOPTSTATE, "Incorrect number of parameters");
6239
6240 ComPtr<IMachine> machine;
6241 /* assume it's a UUID */
6242 rc = virtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
6243 if (FAILED(rc) || !machine)
6244 {
6245 /* must be a name */
6246 CHECK_ERROR(virtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
6247 }
6248 if (machine)
6249 {
6250 do
6251 {
6252 /* we have to open a session for this task */
6253 Guid guid;
6254 machine->COMGETTER(Id)(guid.asOutParam());
6255 CHECK_ERROR_BREAK(virtualBox, OpenSession(session, guid));
6256 do
6257 {
6258 ComPtr<IConsole> console;
6259 CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam()));
6260 CHECK_ERROR_BREAK(console, AdoptSavedState (Bstr (argv[1])));
6261 }
6262 while (0);
6263 CHECK_ERROR_BREAK(session, Close());
6264 }
6265 while (0);
6266 }
6267
6268 return SUCCEEDED(rc) ? 0 : 1;
6269}
6270
6271static int handleSnapshot(int argc, char *argv[],
6272 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6273{
6274 HRESULT rc;
6275
6276 /* we need at least a VM and a command */
6277 if (argc < 2)
6278 return errorSyntax(USAGE_SNAPSHOT, "Not enough parameters");
6279
6280 /* the first argument must be the VM */
6281 ComPtr<IMachine> machine;
6282 /* assume it's a UUID */
6283 rc = virtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
6284 if (FAILED(rc) || !machine)
6285 {
6286 /* must be a name */
6287 CHECK_ERROR(virtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
6288 }
6289 if (!machine)
6290 return 1;
6291 Guid guid;
6292 machine->COMGETTER(Id)(guid.asOutParam());
6293
6294 do
6295 {
6296 /* we have to open a session for this task. First try an existing session */
6297 rc = virtualBox->OpenExistingSession(session, guid);
6298 if (FAILED(rc))
6299 CHECK_ERROR_BREAK(virtualBox, OpenSession(session, guid));
6300 ComPtr<IConsole> console;
6301 CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam()));
6302
6303 /* switch based on the command */
6304 if (strcmp(argv[1], "take") == 0)
6305 {
6306 /* there must be a name */
6307 if (argc < 3)
6308 {
6309 errorSyntax(USAGE_SNAPSHOT, "Missing snapshot name");
6310 rc = E_FAIL;
6311 break;
6312 }
6313 Bstr name(argv[2]);
6314 if ((argc > 3) && ((argc != 5) || (strcmp(argv[3], "-desc") != 0)))
6315 {
6316 errorSyntax(USAGE_SNAPSHOT, "Incorrect description format");
6317 rc = E_FAIL;
6318 break;
6319 }
6320 Bstr desc;
6321 if (argc == 5)
6322 desc = argv[4];
6323 ComPtr<IProgress> progress;
6324 CHECK_ERROR_BREAK(console, TakeSnapshot(name, desc, progress.asOutParam()));
6325
6326 showProgress(progress);
6327 progress->COMGETTER(ResultCode)(&rc);
6328 if (FAILED(rc))
6329 {
6330 com::ProgressErrorInfo info(progress);
6331 if (info.isBasicAvailable())
6332 RTPrintf("Error: failed to take snapshot. Error message: %lS\n", info.getText().raw());
6333 else
6334 RTPrintf("Error: failed to take snapshot. No error message available!\n");
6335 }
6336 }
6337 else if (strcmp(argv[1], "discard") == 0)
6338 {
6339 /* exactly one parameter: snapshot name */
6340 if (argc != 3)
6341 {
6342 errorSyntax(USAGE_SNAPSHOT, "Expecting snapshot name only");
6343 rc = E_FAIL;
6344 break;
6345 }
6346
6347 ComPtr<ISnapshot> snapshot;
6348
6349 /* assume it's a UUID */
6350 Guid guid(argv[2]);
6351 if (!guid.isEmpty())
6352 {
6353 CHECK_ERROR_BREAK(machine, GetSnapshot(guid, snapshot.asOutParam()));
6354 }
6355 else
6356 {
6357 /* then it must be a name */
6358 CHECK_ERROR_BREAK(machine, FindSnapshot(Bstr(argv[2]), snapshot.asOutParam()));
6359 }
6360
6361 snapshot->COMGETTER(Id)(guid.asOutParam());
6362
6363 ComPtr<IProgress> progress;
6364 CHECK_ERROR_BREAK(console, DiscardSnapshot(guid, progress.asOutParam()));
6365
6366 showProgress(progress);
6367 progress->COMGETTER(ResultCode)(&rc);
6368 if (FAILED(rc))
6369 {
6370 com::ProgressErrorInfo info(progress);
6371 if (info.isBasicAvailable())
6372 RTPrintf("Error: failed to discard snapshot. Error message: %lS\n", info.getText().raw());
6373 else
6374 RTPrintf("Error: failed to discard snapshot. No error message available!\n");
6375 }
6376 }
6377 else if (strcmp(argv[1], "discardcurrent") == 0)
6378 {
6379 if ( (argc != 3)
6380 || ( (strcmp(argv[2], "-state") != 0)
6381 && (strcmp(argv[2], "-all") != 0)))
6382 {
6383 errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(argv[2]).raw());
6384 rc = E_FAIL;
6385 break;
6386 }
6387 bool fAll = false;
6388 if (strcmp(argv[2], "-all") == 0)
6389 fAll = true;
6390
6391 ComPtr<IProgress> progress;
6392
6393 if (fAll)
6394 {
6395 CHECK_ERROR_BREAK(console, DiscardCurrentSnapshotAndState(progress.asOutParam()));
6396 }
6397 else
6398 {
6399 CHECK_ERROR_BREAK(console, DiscardCurrentState(progress.asOutParam()));
6400 }
6401
6402 showProgress(progress);
6403 progress->COMGETTER(ResultCode)(&rc);
6404 if (FAILED(rc))
6405 {
6406 com::ProgressErrorInfo info(progress);
6407 if (info.isBasicAvailable())
6408 RTPrintf("Error: failed to discard. Error message: %lS\n", info.getText().raw());
6409 else
6410 RTPrintf("Error: failed to discard. No error message available!\n");
6411 }
6412
6413 }
6414 else if (strcmp(argv[1], "edit") == 0)
6415 {
6416 if (argc < 3)
6417 {
6418 errorSyntax(USAGE_SNAPSHOT, "Missing snapshot name");
6419 rc = E_FAIL;
6420 break;
6421 }
6422
6423 ComPtr<ISnapshot> snapshot;
6424
6425 if (strcmp(argv[2], "-current") == 0)
6426 {
6427 CHECK_ERROR_BREAK(machine, COMGETTER(CurrentSnapshot)(snapshot.asOutParam()));
6428 }
6429 else
6430 {
6431 /* assume it's a UUID */
6432 Guid guid(argv[2]);
6433 if (!guid.isEmpty())
6434 {
6435 CHECK_ERROR_BREAK(machine, GetSnapshot(guid, snapshot.asOutParam()));
6436 }
6437 else
6438 {
6439 /* then it must be a name */
6440 CHECK_ERROR_BREAK(machine, FindSnapshot(Bstr(argv[2]), snapshot.asOutParam()));
6441 }
6442 }
6443
6444 /* parse options */
6445 for (int i = 3; i < argc; i++)
6446 {
6447 if (strcmp(argv[i], "-newname") == 0)
6448 {
6449 if (argc <= i + 1)
6450 {
6451 errorArgument("Missing argument to '%s'", argv[i]);
6452 rc = E_FAIL;
6453 break;
6454 }
6455 i++;
6456 snapshot->COMSETTER(Name)(Bstr(argv[i]));
6457 }
6458 else if (strcmp(argv[i], "-newdesc") == 0)
6459 {
6460 if (argc <= i + 1)
6461 {
6462 errorArgument("Missing argument to '%s'", argv[i]);
6463 rc = E_FAIL;
6464 break;
6465 }
6466 i++;
6467 snapshot->COMSETTER(Description)(Bstr(argv[i]));
6468 }
6469 else
6470 {
6471 errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
6472 rc = E_FAIL;
6473 break;
6474 }
6475 }
6476
6477 }
6478 else if (strcmp(argv[1], "showvminfo") == 0)
6479 {
6480 /* exactly one parameter: snapshot name */
6481 if (argc != 3)
6482 {
6483 errorSyntax(USAGE_SNAPSHOT, "Expecting snapshot name only");
6484 rc = E_FAIL;
6485 break;
6486 }
6487
6488 ComPtr<ISnapshot> snapshot;
6489
6490 /* assume it's a UUID */
6491 Guid guid(argv[2]);
6492 if (!guid.isEmpty())
6493 {
6494 CHECK_ERROR_BREAK(machine, GetSnapshot(guid, snapshot.asOutParam()));
6495 }
6496 else
6497 {
6498 /* then it must be a name */
6499 CHECK_ERROR_BREAK(machine, FindSnapshot(Bstr(argv[2]), snapshot.asOutParam()));
6500 }
6501
6502 /* get the machine of the given snapshot */
6503 ComPtr<IMachine> machine;
6504 snapshot->COMGETTER(Machine)(machine.asOutParam());
6505 showVMInfo(virtualBox, machine, console);
6506 }
6507 else
6508 {
6509 errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(argv[1]).raw());
6510 rc = E_FAIL;
6511 }
6512 } while (0);
6513
6514 session->Close();
6515
6516 return SUCCEEDED(rc) ? 0 : 1;
6517}
6518
6519static int handleShowVDIInfo(int argc, char *argv[],
6520 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6521{
6522 HRESULT rc;
6523
6524 if (argc != 1)
6525 return errorSyntax(USAGE_SHOWVDIINFO, "Incorrect number of parameters");
6526
6527 ComPtr<IHardDisk> hardDisk;
6528 Bstr filepath;
6529
6530 bool registered = true;
6531
6532 /* first guess is that it's a UUID */
6533 Guid uuid(argv[0]);
6534 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
6535 /* no? then it must be a filename */
6536 if (FAILED (rc))
6537 {
6538 filepath = argv[0];
6539 rc = virtualBox->FindHardDisk(filepath, hardDisk.asOutParam());
6540 /* no? well, then it's an unregistered image */
6541 if (FAILED (rc))
6542 {
6543 registered = false;
6544 CHECK_ERROR(virtualBox, OpenHardDisk(filepath, hardDisk.asOutParam()));
6545 }
6546 }
6547 if (SUCCEEDED(rc) && hardDisk)
6548 {
6549 /* query a VDI object (will remain null if it's not VDI) */
6550 ComPtr<IVirtualDiskImage> vdi = hardDisk;
6551
6552 hardDisk->COMGETTER(Id)(uuid.asOutParam());
6553 RTPrintf("UUID: %s\n", uuid.toString().raw());
6554
6555 RTPrintf("Registered: %s\n", registered ? "yes" : "no");
6556
6557 /* check for accessibility */
6558 BOOL accessible = FALSE;
6559 CHECK_ERROR_RET (hardDisk, COMGETTER(Accessible)(&accessible), 1);
6560 RTPrintf("Accessible: %s\n", accessible ? "yes" : "no");
6561
6562 if (!accessible)
6563 {
6564 Bstr err;
6565 CHECK_ERROR_RET (hardDisk, COMGETTER(LastAccessError)(err.asOutParam()), 1);
6566 RTPrintf("Access Error: %lS\n", err.raw());
6567 }
6568
6569 Bstr description;
6570 hardDisk->COMGETTER(Description)(description.asOutParam());
6571 if (description)
6572 {
6573 RTPrintf("Description: %lS\n", description.raw());
6574 }
6575
6576 ULONG64 size;
6577 hardDisk->COMGETTER(Size)(&size);
6578 RTPrintf("Size: %llu MBytes\n", size);
6579 ULONG64 actualSize;
6580 hardDisk->COMGETTER(ActualSize)(&actualSize);
6581 RTPrintf("Current size on disk: %llu MBytes\n", actualSize >> 20);
6582
6583 HardDiskType_T type;
6584 hardDisk->COMGETTER(Type)(&type);
6585 const char *typeStr = "unknown";
6586 switch (type)
6587 {
6588 case HardDiskType_Normal:
6589 typeStr = "standard";
6590 break;
6591 case HardDiskType_Immutable:
6592 typeStr = "immutable";
6593 break;
6594 case HardDiskType_Writethrough:
6595 typeStr = "writethrough";
6596 break;
6597 }
6598 RTPrintf("Type: %s\n", typeStr);
6599
6600 HardDiskStorageType_T storageType;
6601 const char *storageTypeStr = "unknown";
6602 hardDisk->COMGETTER(StorageType)(&storageType);
6603 switch (storageType)
6604 {
6605 case HardDiskStorageType_VirtualDiskImage:
6606 storageTypeStr = "Virtual Disk Image (VDI)";
6607 break;
6608 case HardDiskStorageType_VMDKImage:
6609 storageTypeStr = "VMDK Image";
6610 break;
6611 case HardDiskStorageType_ISCSIHardDisk:
6612 storageTypeStr = "iSCSI target";
6613 break;
6614 case HardDiskStorageType_VHDImage:
6615 storageTypeStr = "VHD Image";
6616 break;
6617 }
6618 RTPrintf("Storage type: %s\n", storageTypeStr);
6619
6620 if (registered)
6621 {
6622 hardDisk->COMGETTER(MachineId)(uuid.asOutParam());
6623 RTPrintf("In use by VM: %s\n", uuid ? uuid.toString().raw() : "<none>");
6624 }
6625
6626 if (vdi)
6627 {
6628 /* VDI specific information */
6629 vdi->COMGETTER(FilePath)(filepath.asOutParam());
6630 RTPrintf("Path: %lS\n", filepath.raw());
6631
6632 }
6633 else
6634 {
6635 /* Generic location information */
6636 Bstr loc;
6637 hardDisk->COMGETTER(Location)(loc.asOutParam());
6638 RTPrintf("Location: %lS\n", loc.raw());
6639 }
6640 }
6641 return SUCCEEDED(rc) ? 0 : 1;
6642}
6643
6644static int handleRegisterImage(int argc, char *argv[],
6645 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6646{
6647 HRESULT rc;
6648
6649 if (argc < 2)
6650 return errorSyntax(USAGE_REGISTERIMAGE, "Not enough parameters");
6651
6652 Bstr filepath(argv[1]);
6653
6654 if (strcmp(argv[0], "disk") == 0)
6655 {
6656 const char *type = NULL;
6657 /* there can be a type parameter */
6658 if ((argc > 2) && (argc != 4))
6659 return errorSyntax(USAGE_REGISTERIMAGE, "Incorrect number of parameters");
6660 if (argc == 4)
6661 {
6662 if (strcmp(argv[2], "-type") != 0)
6663 return errorSyntax(USAGE_REGISTERIMAGE, "Invalid parameter '%s'", Utf8Str(argv[2]).raw());
6664 if ( (strcmp(argv[3], "normal") != 0)
6665 && (strcmp(argv[3], "immutable") != 0)
6666 && (strcmp(argv[3], "writethrough") != 0))
6667 return errorArgument("Invalid VDI type '%s' specified", Utf8Str(argv[3]).raw());
6668 type = argv[3];
6669 }
6670
6671 ComPtr<IHardDisk> hardDisk;
6672 CHECK_ERROR(virtualBox, OpenHardDisk(filepath, hardDisk.asOutParam()));
6673 if (SUCCEEDED(rc) && hardDisk)
6674 {
6675 /* change the type if requested */
6676 if (type)
6677 {
6678 if (strcmp(type, "normal") == 0)
6679 CHECK_ERROR(hardDisk, COMSETTER(Type)(HardDiskType_Normal));
6680 else if (strcmp(type, "immutable") == 0)
6681 CHECK_ERROR(hardDisk, COMSETTER(Type)(HardDiskType_Immutable));
6682 else if (strcmp(type, "writethrough") == 0)
6683 CHECK_ERROR(hardDisk, COMSETTER(Type)(HardDiskType_Writethrough));
6684 }
6685 if (SUCCEEDED(rc))
6686 CHECK_ERROR(virtualBox, RegisterHardDisk(hardDisk));
6687 }
6688 }
6689 else if (strcmp(argv[0], "dvd") == 0)
6690 {
6691 ComPtr<IDVDImage> dvdImage;
6692 CHECK_ERROR(virtualBox, OpenDVDImage(filepath, Guid(), dvdImage.asOutParam()));
6693 if (SUCCEEDED(rc) && dvdImage)
6694 {
6695 CHECK_ERROR(virtualBox, RegisterDVDImage(dvdImage));
6696 }
6697 }
6698 else if (strcmp(argv[0], "floppy") == 0)
6699 {
6700 ComPtr<IFloppyImage> floppyImage;
6701 CHECK_ERROR(virtualBox, OpenFloppyImage(filepath, Guid(), floppyImage.asOutParam()));
6702 if (SUCCEEDED(rc) && floppyImage)
6703 {
6704 CHECK_ERROR(virtualBox, RegisterFloppyImage(floppyImage));
6705 }
6706 }
6707 else
6708 return errorSyntax(USAGE_REGISTERIMAGE, "Invalid parameter '%s'", Utf8Str(argv[1]).raw());
6709
6710 return SUCCEEDED(rc) ? 0 : 1;
6711}
6712
6713static int handleUnregisterImage(int argc, char *argv[],
6714 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6715{
6716 HRESULT rc;
6717
6718 if (argc != 2)
6719 return errorSyntax(USAGE_UNREGISTERIMAGE, "Incorrect number of parameters");
6720
6721 /* first guess is that it's a UUID */
6722 Guid uuid(argv[1]);
6723
6724 if (strcmp(argv[0], "disk") == 0)
6725 {
6726 ComPtr<IHardDisk> hardDisk;
6727 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
6728 /* not a UUID or not registered? Then it must be a filename */
6729 if (!hardDisk)
6730 {
6731 ComPtr<IVirtualDiskImage> vdi;
6732 CHECK_ERROR(virtualBox, FindVirtualDiskImage(Bstr(argv[1]), vdi.asOutParam()));
6733 hardDisk = vdi;
6734 }
6735 if (SUCCEEDED(rc) && hardDisk)
6736 {
6737 hardDisk->COMGETTER(Id)(uuid.asOutParam());
6738 CHECK_ERROR(virtualBox, UnregisterHardDisk(uuid, hardDisk.asOutParam()));
6739 }
6740 }
6741 else
6742 if (strcmp(argv[0], "dvd") == 0)
6743 {
6744 ComPtr<IDVDImage> dvdImage;
6745 rc = virtualBox->GetDVDImage(uuid, dvdImage.asOutParam());
6746 /* not a UUID or not registered? Then it must be a filename */
6747 if (!dvdImage)
6748 {
6749 ComPtr<IDVDImageCollection> dvdColl;
6750 virtualBox->COMGETTER(DVDImages)(dvdColl.asOutParam());
6751 CHECK_ERROR(dvdColl, FindByPath(Bstr(argv[1]), dvdImage.asOutParam()));
6752 }
6753 if (SUCCEEDED(rc) && dvdImage)
6754 {
6755 dvdImage->COMGETTER(Id)(uuid.asOutParam());
6756 CHECK_ERROR(virtualBox, UnregisterDVDImage(uuid, dvdImage.asOutParam()));
6757 }
6758 }
6759 else
6760 if (strcmp(argv[0], "floppy") == 0)
6761 {
6762 ComPtr<IFloppyImage> floppyImage;
6763 rc = virtualBox->GetFloppyImage(uuid, floppyImage.asOutParam());
6764 /* not a UUID or not registered? Then it must be a filename */
6765 if (!floppyImage)
6766 {
6767 ComPtr<IFloppyImageCollection> floppyColl;
6768 virtualBox->COMGETTER(FloppyImages)(floppyColl.asOutParam());
6769 CHECK_ERROR(floppyColl, FindByPath(Bstr(argv[1]), floppyImage.asOutParam()));
6770 }
6771 if (SUCCEEDED(rc) && floppyImage)
6772 {
6773 floppyImage->COMGETTER(Id)(uuid.asOutParam());
6774 CHECK_ERROR(virtualBox, UnregisterFloppyImage(uuid, floppyImage.asOutParam()));
6775 }
6776 }
6777 else
6778 return errorSyntax(USAGE_UNREGISTERIMAGE, "Invalid parameter '%s'", Utf8Str(argv[1]).raw());
6779
6780 return SUCCEEDED(rc) ? 0 : 1;
6781}
6782
6783#ifdef RT_OS_WINDOWS
6784static int handleCreateHostIF(int argc, char *argv[],
6785 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6786{
6787 if (argc != 1)
6788 return errorSyntax(USAGE_CREATEHOSTIF, "Incorrect number of parameters");
6789
6790 HRESULT rc = S_OK;
6791
6792 do
6793 {
6794 ComPtr<IHost> host;
6795 CHECK_ERROR_BREAK(virtualBox, COMGETTER(Host)(host.asOutParam()));
6796
6797 ComPtr<IHostNetworkInterface> hostif;
6798 ComPtr<IProgress> progress;
6799 CHECK_ERROR_BREAK(host,
6800 CreateHostNetworkInterface(Bstr(argv[0]),
6801 hostif.asOutParam(),
6802 progress.asOutParam()));
6803
6804 showProgress(progress);
6805 HRESULT result;
6806 CHECK_ERROR_BREAK(progress, COMGETTER(ResultCode)(&result));
6807 if (FAILED(result))
6808 {
6809 com::ProgressErrorInfo info(progress);
6810 PRINT_ERROR_INFO(info);
6811 rc = result;
6812 }
6813 }
6814 while (0);
6815
6816 return SUCCEEDED(rc) ? 0 : 1;
6817}
6818
6819static int handleRemoveHostIF(int argc, char *argv[],
6820 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6821{
6822 if (argc != 1)
6823 return errorSyntax(USAGE_REMOVEHOSTIF, "Incorrect number of parameters");
6824
6825 HRESULT rc = S_OK;
6826
6827 do
6828 {
6829 ComPtr<IHost> host;
6830 CHECK_ERROR_BREAK(virtualBox, COMGETTER(Host)(host.asOutParam()));
6831
6832 ComPtr<IHostNetworkInterface> hostif;
6833
6834 /* first guess is that it's a UUID */
6835 Guid uuid(argv[0]);
6836 if (uuid.isEmpty())
6837 {
6838 /* not a valid UUID, search for it */
6839 ComPtr<IHostNetworkInterfaceCollection> coll;
6840 CHECK_ERROR_BREAK(host, COMGETTER(NetworkInterfaces)(coll.asOutParam()));
6841 CHECK_ERROR_BREAK(coll, FindByName(Bstr(argv[0]), hostif.asOutParam()));
6842 CHECK_ERROR_BREAK(hostif, COMGETTER(Id)(uuid.asOutParam()));
6843 }
6844
6845 ComPtr<IProgress> progress;
6846 CHECK_ERROR_BREAK(host,
6847 RemoveHostNetworkInterface(uuid,
6848 hostif.asOutParam(),
6849 progress.asOutParam()));
6850
6851 showProgress(progress);
6852 HRESULT result;
6853 CHECK_ERROR_BREAK(progress, COMGETTER(ResultCode)(&result));
6854 if (FAILED(result))
6855 {
6856 com::ProgressErrorInfo info(progress);
6857 PRINT_ERROR_INFO(info);
6858 rc = result;
6859 }
6860 }
6861 while (0);
6862
6863 return SUCCEEDED(rc) ? 0 : 1;
6864}
6865#endif /* RT_OS_WINDOWS */
6866
6867static int handleGetExtraData(int argc, char *argv[],
6868 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6869{
6870 HRESULT rc = S_OK;
6871
6872 if (argc != 2)
6873 return errorSyntax(USAGE_GETEXTRADATA, "Incorrect number of parameters");
6874
6875 /* global data? */
6876 if (strcmp(argv[0], "global") == 0)
6877 {
6878 /* enumeration? */
6879 if (strcmp(argv[1], "enumerate") == 0)
6880 {
6881 Bstr extraDataKey;
6882
6883 do
6884 {
6885 Bstr nextExtraDataKey;
6886 Bstr nextExtraDataValue;
6887 HRESULT rcEnum = virtualBox->GetNextExtraDataKey(extraDataKey, nextExtraDataKey.asOutParam(),
6888 nextExtraDataValue.asOutParam());
6889 extraDataKey = nextExtraDataKey;
6890
6891 if (SUCCEEDED(rcEnum) && extraDataKey)
6892 RTPrintf("Key: %lS, Value: %lS\n", nextExtraDataKey.raw(), nextExtraDataValue.raw());
6893 } while (extraDataKey);
6894 }
6895 else
6896 {
6897 Bstr value;
6898 CHECK_ERROR(virtualBox, GetExtraData(Bstr(argv[1]), value.asOutParam()));
6899 if (value)
6900 RTPrintf("Value: %lS\n", value.raw());
6901 else
6902 RTPrintf("No value set!\n");
6903 }
6904 }
6905 else
6906 {
6907 ComPtr<IMachine> machine;
6908 /* assume it's a UUID */
6909 rc = virtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
6910 if (FAILED(rc) || !machine)
6911 {
6912 /* must be a name */
6913 CHECK_ERROR(virtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
6914 }
6915 if (machine)
6916 {
6917 /* enumeration? */
6918 if (strcmp(argv[1], "enumerate") == 0)
6919 {
6920 Bstr extraDataKey;
6921
6922 do
6923 {
6924 Bstr nextExtraDataKey;
6925 Bstr nextExtraDataValue;
6926 HRESULT rcEnum = machine->GetNextExtraDataKey(extraDataKey, nextExtraDataKey.asOutParam(),
6927 nextExtraDataValue.asOutParam());
6928 extraDataKey = nextExtraDataKey;
6929
6930 if (SUCCEEDED(rcEnum) && extraDataKey)
6931 {
6932 RTPrintf("Key: %lS, Value: %lS\n", nextExtraDataKey.raw(), nextExtraDataValue.raw());
6933 }
6934 } while (extraDataKey);
6935 }
6936 else
6937 {
6938 Bstr value;
6939 CHECK_ERROR(machine, GetExtraData(Bstr(argv[1]), value.asOutParam()));
6940 if (value)
6941 RTPrintf("Value: %lS\n", value.raw());
6942 else
6943 RTPrintf("No value set!\n");
6944 }
6945 }
6946 }
6947 return SUCCEEDED(rc) ? 0 : 1;
6948}
6949
6950static int handleSetExtraData(int argc, char *argv[],
6951 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6952{
6953 HRESULT rc = S_OK;
6954
6955 if (argc < 2)
6956 return errorSyntax(USAGE_SETEXTRADATA, "Not enough parameters");
6957
6958 /* global data? */
6959 if (strcmp(argv[0], "global") == 0)
6960 {
6961 if (argc < 3)
6962 CHECK_ERROR(virtualBox, SetExtraData(Bstr(argv[1]), NULL));
6963 else if (argc == 3)
6964 CHECK_ERROR(virtualBox, SetExtraData(Bstr(argv[1]), Bstr(argv[2])));
6965 else
6966 return errorSyntax(USAGE_SETEXTRADATA, "Too many parameters");
6967 }
6968 else
6969 {
6970 ComPtr<IMachine> machine;
6971 /* assume it's a UUID */
6972 rc = virtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
6973 if (FAILED(rc) || !machine)
6974 {
6975 /* must be a name */
6976 CHECK_ERROR(virtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
6977 }
6978 if (machine)
6979 {
6980 if (argc < 3)
6981 CHECK_ERROR(machine, SetExtraData(Bstr(argv[1]), NULL));
6982 else if (argc == 3)
6983 CHECK_ERROR(machine, SetExtraData(Bstr(argv[1]), Bstr(argv[2])));
6984 else
6985 return errorSyntax(USAGE_SETEXTRADATA, "Too many parameters");
6986 }
6987 }
6988 return SUCCEEDED(rc) ? 0 : 1;
6989}
6990
6991static int handleSetProperty(int argc, char *argv[],
6992 ComPtr<IVirtualBox> virtualBox, ComPtr<ISession> session)
6993{
6994 HRESULT rc;
6995
6996 /* there must be two arguments: property name and value */
6997 if (argc != 2)
6998 return errorSyntax(USAGE_SETPROPERTY, "Incorrect number of parameters");
6999
7000 ComPtr<ISystemProperties> systemProperties;
7001 virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
7002
7003 if (strcmp(argv[0], "vdifolder") == 0)
7004 {
7005 /* reset to default? */
7006 if (strcmp(argv[1], "default") == 0)
7007 CHECK_ERROR(systemProperties, COMSETTER(DefaultVDIFolder)(NULL));
7008 else
7009 CHECK_ERROR(systemProperties, COMSETTER(DefaultVDIFolder)(Bstr(argv[1])));
7010 }
7011 else if (strcmp(argv[0], "machinefolder") == 0)
7012 {
7013 /* reset to default? */
7014 if (strcmp(argv[1], "default") == 0)
7015 CHECK_ERROR(systemProperties, COMSETTER(DefaultMachineFolder)(NULL));
7016 else
7017 CHECK_ERROR(systemProperties, COMSETTER(DefaultMachineFolder)(Bstr(argv[1])));
7018 }
7019 else if (strcmp(argv[0], "vrdpauthlibrary") == 0)
7020 {
7021 /* reset to default? */
7022 if (strcmp(argv[1], "default") == 0)
7023 CHECK_ERROR(systemProperties, COMSETTER(RemoteDisplayAuthLibrary)(NULL));
7024 else
7025 CHECK_ERROR(systemProperties, COMSETTER(RemoteDisplayAuthLibrary)(Bstr(argv[1])));
7026 }
7027 else if (strcmp(argv[0], "websrvauthlibrary") == 0)
7028 {
7029 /* reset to default? */
7030 if (strcmp(argv[1], "default") == 0)
7031 CHECK_ERROR(systemProperties, COMSETTER(WebServiceAuthLibrary)(NULL));
7032 else
7033 CHECK_ERROR(systemProperties, COMSETTER(WebServiceAuthLibrary)(Bstr(argv[1])));
7034 }
7035 else if (strcmp(argv[0], "hwvirtexenabled") == 0)
7036 {
7037 if (strcmp(argv[1], "yes") == 0)
7038 CHECK_ERROR(systemProperties, COMSETTER(HWVirtExEnabled)(TRUE));
7039 else if (strcmp(argv[1], "no") == 0)
7040 CHECK_ERROR(systemProperties, COMSETTER(HWVirtExEnabled)(FALSE));
7041 else
7042 return errorArgument("Invalid value '%s' for hardware virtualization extension flag", argv[1]);
7043 }
7044 else if (strcmp(argv[0], "loghistorycount") == 0)
7045 {
7046 uint32_t uVal;
7047 int vrc;
7048 vrc = RTStrToUInt32Ex(argv[1], NULL, 0, &uVal);
7049 if (vrc != VINF_SUCCESS)
7050 return errorArgument("Error parsing Log history count '%s'", argv[1]);
7051 CHECK_ERROR(systemProperties, COMSETTER(LogHistoryCount)(uVal));
7052 }
7053 else
7054 return errorSyntax(USAGE_SETPROPERTY, "Invalid parameter '%s'", argv[0]);
7055
7056 return SUCCEEDED(rc) ? 0 : 1;
7057}
7058
7059static int handleUSBFilter (int argc, char *argv[],
7060 ComPtr <IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
7061{
7062 HRESULT rc = S_OK;
7063 USBFilterCmd cmd;
7064
7065 /* at least: 0: command, 1: index, 2: -target, 3: <target value> */
7066 if (argc < 4)
7067 return errorSyntax(USAGE_USBFILTER, "Not enough parameters");
7068
7069 /* which command? */
7070 cmd.mAction = USBFilterCmd::Invalid;
7071 if (strcmp (argv [0], "add") == 0) cmd.mAction = USBFilterCmd::Add;
7072 else if (strcmp (argv [0], "modify") == 0) cmd.mAction = USBFilterCmd::Modify;
7073 else if (strcmp (argv [0], "remove") == 0) cmd.mAction = USBFilterCmd::Remove;
7074
7075 if (cmd.mAction == USBFilterCmd::Invalid)
7076 return errorSyntax(USAGE_USBFILTER, "Invalid parameter '%s'", argv[0]);
7077
7078 /* which index? */
7079 char *endptr = NULL;
7080 cmd.mIndex = strtoul (argv[1], &endptr, 10);
7081 if (!endptr || *endptr)
7082 return errorSyntax(USAGE_USBFILTER, "Invalid index '%s'", argv[1]);
7083
7084 switch (cmd.mAction)
7085 {
7086 case USBFilterCmd::Add:
7087 case USBFilterCmd::Modify:
7088 {
7089 /* at least: 0: command, 1: index, 2: -target, 3: <target value>, 4: -name, 5: <name value> */
7090 if (argc < 6)
7091 {
7092 if (cmd.mAction == USBFilterCmd::Add)
7093 return errorSyntax(USAGE_USBFILTER_ADD, "Not enough parameters");
7094
7095 return errorSyntax(USAGE_USBFILTER_MODIFY, "Not enough parameters");
7096 }
7097
7098 // set Active to true by default
7099 // (assuming that the user sets up all necessary attributes
7100 // at once and wants the filter to be active immediately)
7101 if (cmd.mAction == USBFilterCmd::Add)
7102 cmd.mFilter.mActive = true;
7103
7104 for (int i = 2; i < argc; i++)
7105 {
7106 if (strcmp(argv [i], "-target") == 0)
7107 {
7108 if (argc <= i + 1 || !*argv[i+1])
7109 return errorArgument("Missing argument to '%s'", argv[i]);
7110 i++;
7111 if (strcmp (argv [i], "global") == 0)
7112 cmd.mGlobal = true;
7113 else
7114 {
7115 /* assume it's a UUID of a machine */
7116 rc = aVirtualBox->GetMachine(Guid(argv[i]), cmd.mMachine.asOutParam());
7117 if (FAILED(rc) || !cmd.mMachine)
7118 {
7119 /* must be a name */
7120 CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[i]), cmd.mMachine.asOutParam()), 1);
7121 }
7122 }
7123 }
7124 else if (strcmp(argv [i], "-name") == 0)
7125 {
7126 if (argc <= i + 1 || !*argv[i+1])
7127 return errorArgument("Missing argument to '%s'", argv[i]);
7128 i++;
7129 cmd.mFilter.mName = argv [i];
7130 }
7131 else if (strcmp(argv [i], "-active") == 0)
7132 {
7133 if (argc <= i + 1)
7134 return errorArgument("Missing argument to '%s'", argv[i]);
7135 i++;
7136 if (strcmp (argv [i], "yes") == 0)
7137 cmd.mFilter.mActive = true;
7138 else if (strcmp (argv [i], "no") == 0)
7139 cmd.mFilter.mActive = false;
7140 else
7141 return errorArgument("Invalid -active argument '%s'", argv[i]);
7142 }
7143 else if (strcmp(argv [i], "-vendorid") == 0)
7144 {
7145 if (argc <= i + 1)
7146 return errorArgument("Missing argument to '%s'", argv[i]);
7147 i++;
7148 cmd.mFilter.mVendorId = argv [i];
7149 }
7150 else if (strcmp(argv [i], "-productid") == 0)
7151 {
7152 if (argc <= i + 1)
7153 return errorArgument("Missing argument to '%s'", argv[i]);
7154 i++;
7155 cmd.mFilter.mProductId = argv [i];
7156 }
7157 else if (strcmp(argv [i], "-revision") == 0)
7158 {
7159 if (argc <= i + 1)
7160 return errorArgument("Missing argument to '%s'", argv[i]);
7161 i++;
7162 cmd.mFilter.mRevision = argv [i];
7163 }
7164 else if (strcmp(argv [i], "-manufacturer") == 0)
7165 {
7166 if (argc <= i + 1)
7167 return errorArgument("Missing argument to '%s'", argv[i]);
7168 i++;
7169 cmd.mFilter.mManufacturer = argv [i];
7170 }
7171 else if (strcmp(argv [i], "-product") == 0)
7172 {
7173 if (argc <= i + 1)
7174 return errorArgument("Missing argument to '%s'", argv[i]);
7175 i++;
7176 cmd.mFilter.mProduct = argv [i];
7177 }
7178 else if (strcmp(argv [i], "-remote") == 0)
7179 {
7180 if (argc <= i + 1)
7181 return errorArgument("Missing argument to '%s'", argv[i]);
7182 i++;
7183 cmd.mFilter.mRemote = argv[i];
7184 }
7185 else if (strcmp(argv [i], "-serialnumber") == 0)
7186 {
7187 if (argc <= i + 1)
7188 return errorArgument("Missing argument to '%s'", argv[i]);
7189 i++;
7190 cmd.mFilter.mSerialNumber = argv [i];
7191 }
7192 else if (strcmp(argv [i], "-maskedinterfaces") == 0)
7193 {
7194 if (argc <= i + 1)
7195 return errorArgument("Missing argument to '%s'", argv[i]);
7196 i++;
7197 uint32_t u32;
7198 rc = RTStrToUInt32Full(argv[i], 0, &u32);
7199 if (RT_FAILURE(rc))
7200 return errorArgument("Failed to convert the -maskedinterfaces value '%s' to a number, rc=%Rrc", argv[i], rc);
7201 cmd.mFilter.mMaskedInterfaces = u32;
7202 }
7203 else if (strcmp(argv [i], "-action") == 0)
7204 {
7205 if (argc <= i + 1)
7206 return errorArgument("Missing argument to '%s'", argv[i]);
7207 i++;
7208 if (strcmp (argv [i], "ignore") == 0)
7209 cmd.mFilter.mAction = USBDeviceFilterAction_Ignore;
7210 else if (strcmp (argv [i], "hold") == 0)
7211 cmd.mFilter.mAction = USBDeviceFilterAction_Hold;
7212 else
7213 return errorArgument("Invalid USB filter action '%s'", argv[i]);
7214 }
7215 else
7216 return errorSyntax(cmd.mAction == USBFilterCmd::Add ? USAGE_USBFILTER_ADD : USAGE_USBFILTER_MODIFY,
7217 "Unknown option '%s'", argv[i]);
7218 }
7219
7220 if (cmd.mAction == USBFilterCmd::Add)
7221 {
7222 // mandatory/forbidden options
7223 if ( cmd.mFilter.mName.isEmpty()
7224 ||
7225 ( cmd.mGlobal
7226 && cmd.mFilter.mAction == USBDeviceFilterAction_Null
7227 )
7228 || ( !cmd.mGlobal
7229 && !cmd.mMachine)
7230 || ( cmd.mGlobal
7231 && cmd.mFilter.mRemote)
7232 )
7233 {
7234 return errorSyntax(USAGE_USBFILTER_ADD, "Mandatory options not supplied");
7235 }
7236 }
7237 break;
7238 }
7239
7240 case USBFilterCmd::Remove:
7241 {
7242 /* at least: 0: command, 1: index, 2: -target, 3: <target value> */
7243 if (argc < 4)
7244 return errorSyntax(USAGE_USBFILTER_REMOVE, "Not enough parameters");
7245
7246 for (int i = 2; i < argc; i++)
7247 {
7248 if (strcmp(argv [i], "-target") == 0)
7249 {
7250 if (argc <= i + 1 || !*argv[i+1])
7251 return errorArgument("Missing argument to '%s'", argv[i]);
7252 i++;
7253 if (strcmp (argv [i], "global") == 0)
7254 cmd.mGlobal = true;
7255 else
7256 {
7257 /* assume it's a UUID of a machine */
7258 rc = aVirtualBox->GetMachine(Guid(argv[i]), cmd.mMachine.asOutParam());
7259 if (FAILED(rc) || !cmd.mMachine)
7260 {
7261 /* must be a name */
7262 CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[i]), cmd.mMachine.asOutParam()), 1);
7263 }
7264 }
7265 }
7266 }
7267
7268 // mandatory options
7269 if (!cmd.mGlobal && !cmd.mMachine)
7270 return errorSyntax(USAGE_USBFILTER_REMOVE, "Mandatory options not supplied");
7271
7272 break;
7273 }
7274
7275 default: break;
7276 }
7277
7278 USBFilterCmd::USBFilter &f = cmd.mFilter;
7279
7280 ComPtr <IHost> host;
7281 ComPtr <IUSBController> ctl;
7282 if (cmd.mGlobal)
7283 CHECK_ERROR_RET (aVirtualBox, COMGETTER(Host) (host.asOutParam()), 1);
7284 else
7285 {
7286 Guid uuid;
7287 cmd.mMachine->COMGETTER(Id)(uuid.asOutParam());
7288 /* open a session for the VM */
7289 CHECK_ERROR_RET (aVirtualBox, OpenSession(aSession, uuid), 1);
7290 /* get the mutable session machine */
7291 aSession->COMGETTER(Machine)(cmd.mMachine.asOutParam());
7292 /* and get the USB controller */
7293 CHECK_ERROR_RET (cmd.mMachine, COMGETTER(USBController) (ctl.asOutParam()), 1);
7294 }
7295
7296 switch (cmd.mAction)
7297 {
7298 case USBFilterCmd::Add:
7299 {
7300 if (cmd.mGlobal)
7301 {
7302 ComPtr <IHostUSBDeviceFilter> flt;
7303 CHECK_ERROR_BREAK (host, CreateUSBDeviceFilter (f.mName, flt.asOutParam()));
7304
7305 if (!f.mActive.isNull())
7306 CHECK_ERROR_BREAK (flt, COMSETTER(Active) (f.mActive));
7307 if (!f.mVendorId.isNull())
7308 CHECK_ERROR_BREAK (flt, COMSETTER(VendorId) (f.mVendorId.setNullIfEmpty()));
7309 if (!f.mProductId.isNull())
7310 CHECK_ERROR_BREAK (flt, COMSETTER(ProductId) (f.mProductId.setNullIfEmpty()));
7311 if (!f.mRevision.isNull())
7312 CHECK_ERROR_BREAK (flt, COMSETTER(Revision) (f.mRevision.setNullIfEmpty()));
7313 if (!f.mManufacturer.isNull())
7314 CHECK_ERROR_BREAK (flt, COMSETTER(Manufacturer) (f.mManufacturer.setNullIfEmpty()));
7315 if (!f.mSerialNumber.isNull())
7316 CHECK_ERROR_BREAK (flt, COMSETTER(SerialNumber) (f.mSerialNumber.setNullIfEmpty()));
7317 if (!f.mMaskedInterfaces.isNull())
7318 CHECK_ERROR_BREAK (flt, COMSETTER(MaskedInterfaces) (f.mMaskedInterfaces));
7319
7320 if (f.mAction != USBDeviceFilterAction_Null)
7321 CHECK_ERROR_BREAK (flt, COMSETTER(Action) (f.mAction));
7322
7323 CHECK_ERROR_BREAK (host, InsertUSBDeviceFilter (cmd.mIndex, flt));
7324 }
7325 else
7326 {
7327 ComPtr <IUSBDeviceFilter> flt;
7328 CHECK_ERROR_BREAK (ctl, CreateDeviceFilter (f.mName, flt.asOutParam()));
7329
7330 if (!f.mActive.isNull())
7331 CHECK_ERROR_BREAK (flt, COMSETTER(Active) (f.mActive));
7332 if (!f.mVendorId.isNull())
7333 CHECK_ERROR_BREAK (flt, COMSETTER(VendorId) (f.mVendorId.setNullIfEmpty()));
7334 if (!f.mProductId.isNull())
7335 CHECK_ERROR_BREAK (flt, COMSETTER(ProductId) (f.mProductId.setNullIfEmpty()));
7336 if (!f.mRevision.isNull())
7337 CHECK_ERROR_BREAK (flt, COMSETTER(Revision) (f.mRevision.setNullIfEmpty()));
7338 if (!f.mManufacturer.isNull())
7339 CHECK_ERROR_BREAK (flt, COMSETTER(Manufacturer) (f.mManufacturer.setNullIfEmpty()));
7340 if (!f.mRemote.isNull())
7341 CHECK_ERROR_BREAK (flt, COMSETTER(Remote) (f.mRemote.setNullIfEmpty()));
7342 if (!f.mSerialNumber.isNull())
7343 CHECK_ERROR_BREAK (flt, COMSETTER(SerialNumber) (f.mSerialNumber.setNullIfEmpty()));
7344 if (!f.mMaskedInterfaces.isNull())
7345 CHECK_ERROR_BREAK (flt, COMSETTER(MaskedInterfaces) (f.mMaskedInterfaces));
7346
7347 CHECK_ERROR_BREAK (ctl, InsertDeviceFilter (cmd.mIndex, flt));
7348 }
7349 break;
7350 }
7351 case USBFilterCmd::Modify:
7352 {
7353 if (cmd.mGlobal)
7354 {
7355 ComPtr <IHostUSBDeviceFilterCollection> coll;
7356 CHECK_ERROR_BREAK (host, COMGETTER(USBDeviceFilters) (coll.asOutParam()));
7357 ComPtr <IHostUSBDeviceFilter> flt;
7358 CHECK_ERROR_BREAK (coll, GetItemAt (cmd.mIndex, flt.asOutParam()));
7359
7360 if (!f.mName.isNull())
7361 CHECK_ERROR_BREAK (flt, COMSETTER(Name) (f.mName.setNullIfEmpty()));
7362 if (!f.mActive.isNull())
7363 CHECK_ERROR_BREAK (flt, COMSETTER(Active) (f.mActive));
7364 if (!f.mVendorId.isNull())
7365 CHECK_ERROR_BREAK (flt, COMSETTER(VendorId) (f.mVendorId.setNullIfEmpty()));
7366 if (!f.mProductId.isNull())
7367 CHECK_ERROR_BREAK (flt, COMSETTER(ProductId) (f.mProductId.setNullIfEmpty()));
7368 if (!f.mRevision.isNull())
7369 CHECK_ERROR_BREAK (flt, COMSETTER(Revision) (f.mRevision.setNullIfEmpty()));
7370 if (!f.mManufacturer.isNull())
7371 CHECK_ERROR_BREAK (flt, COMSETTER(Manufacturer) (f.mManufacturer.setNullIfEmpty()));
7372 if (!f.mSerialNumber.isNull())
7373 CHECK_ERROR_BREAK (flt, COMSETTER(SerialNumber) (f.mSerialNumber.setNullIfEmpty()));
7374 if (!f.mMaskedInterfaces.isNull())
7375 CHECK_ERROR_BREAK (flt, COMSETTER(MaskedInterfaces) (f.mMaskedInterfaces));
7376
7377 if (f.mAction != USBDeviceFilterAction_Null)
7378 CHECK_ERROR_BREAK (flt, COMSETTER(Action) (f.mAction));
7379 }
7380 else
7381 {
7382 ComPtr <IUSBDeviceFilterCollection> coll;
7383 CHECK_ERROR_BREAK (ctl, COMGETTER(DeviceFilters) (coll.asOutParam()));
7384
7385 ComPtr <IUSBDeviceFilter> flt;
7386 CHECK_ERROR_BREAK (coll, GetItemAt (cmd.mIndex, flt.asOutParam()));
7387
7388 if (!f.mName.isNull())
7389 CHECK_ERROR_BREAK (flt, COMSETTER(Name) (f.mName.setNullIfEmpty()));
7390 if (!f.mActive.isNull())
7391 CHECK_ERROR_BREAK (flt, COMSETTER(Active) (f.mActive));
7392 if (!f.mVendorId.isNull())
7393 CHECK_ERROR_BREAK (flt, COMSETTER(VendorId) (f.mVendorId.setNullIfEmpty()));
7394 if (!f.mProductId.isNull())
7395 CHECK_ERROR_BREAK (flt, COMSETTER(ProductId) (f.mProductId.setNullIfEmpty()));
7396 if (!f.mRevision.isNull())
7397 CHECK_ERROR_BREAK (flt, COMSETTER(Revision) (f.mRevision.setNullIfEmpty()));
7398 if (!f.mManufacturer.isNull())
7399 CHECK_ERROR_BREAK (flt, COMSETTER(Manufacturer) (f.mManufacturer.setNullIfEmpty()));
7400 if (!f.mRemote.isNull())
7401 CHECK_ERROR_BREAK (flt, COMSETTER(Remote) (f.mRemote.setNullIfEmpty()));
7402 if (!f.mSerialNumber.isNull())
7403 CHECK_ERROR_BREAK (flt, COMSETTER(SerialNumber) (f.mSerialNumber.setNullIfEmpty()));
7404 if (!f.mMaskedInterfaces.isNull())
7405 CHECK_ERROR_BREAK (flt, COMSETTER(MaskedInterfaces) (f.mMaskedInterfaces));
7406 }
7407 break;
7408 }
7409 case USBFilterCmd::Remove:
7410 {
7411 if (cmd.mGlobal)
7412 {
7413 ComPtr <IHostUSBDeviceFilter> flt;
7414 CHECK_ERROR_BREAK (host, RemoveUSBDeviceFilter (cmd.mIndex, flt.asOutParam()));
7415 }
7416 else
7417 {
7418 ComPtr <IUSBDeviceFilter> flt;
7419 CHECK_ERROR_BREAK (ctl, RemoveDeviceFilter (cmd.mIndex, flt.asOutParam()));
7420 }
7421 break;
7422 }
7423 default:
7424 break;
7425 }
7426
7427 if (cmd.mMachine)
7428 {
7429 /* commit and close the session */
7430 CHECK_ERROR(cmd.mMachine, SaveSettings());
7431 aSession->Close();
7432 }
7433
7434 return SUCCEEDED (rc) ? 0 : 1;
7435}
7436
7437static int handleSharedFolder (int argc, char *argv[],
7438 ComPtr <IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
7439{
7440 HRESULT rc;
7441
7442 /* we need at least a command and target */
7443 if (argc < 2)
7444 return errorSyntax(USAGE_SHAREDFOLDER, "Not enough parameters");
7445
7446 ComPtr<IMachine> machine;
7447 /* assume it's a UUID */
7448 rc = aVirtualBox->GetMachine(Guid(argv[1]), machine.asOutParam());
7449 if (FAILED(rc) || !machine)
7450 {
7451 /* must be a name */
7452 CHECK_ERROR(aVirtualBox, FindMachine(Bstr(argv[1]), machine.asOutParam()));
7453 }
7454 if (!machine)
7455 return 1;
7456 Guid uuid;
7457 machine->COMGETTER(Id)(uuid.asOutParam());
7458
7459 if (strcmp(argv[0], "add") == 0)
7460 {
7461 /* we need at least four more parameters */
7462 if (argc < 5)
7463 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Not enough parameters");
7464
7465 char *name = NULL;
7466 char *hostpath = NULL;
7467 bool fTransient = false;
7468 bool fWritable = true;
7469
7470 for (int i = 2; i < argc; i++)
7471 {
7472 if (strcmp(argv[i], "-name") == 0)
7473 {
7474 if (argc <= i + 1 || !*argv[i+1])
7475 return errorArgument("Missing argument to '%s'", argv[i]);
7476 i++;
7477 name = argv[i];
7478 }
7479 else if (strcmp(argv[i], "-hostpath") == 0)
7480 {
7481 if (argc <= i + 1 || !*argv[i+1])
7482 return errorArgument("Missing argument to '%s'", argv[i]);
7483 i++;
7484 hostpath = argv[i];
7485 }
7486 else if (strcmp(argv[i], "-readonly") == 0)
7487 {
7488 fWritable = false;
7489 }
7490 else if (strcmp(argv[i], "-transient") == 0)
7491 {
7492 fTransient = true;
7493 }
7494 else
7495 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
7496 }
7497
7498 if (NULL != strstr(name, " "))
7499 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "No spaces allowed in parameter '-name'!");
7500
7501 /* required arguments */
7502 if (!name || !hostpath)
7503 {
7504 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Parameters -name and -hostpath are required");
7505 }
7506
7507 if (fTransient)
7508 {
7509 ComPtr <IConsole> console;
7510
7511 /* open an existing session for the VM */
7512 CHECK_ERROR_RET(aVirtualBox, OpenExistingSession (aSession, uuid), 1);
7513 /* get the session machine */
7514 CHECK_ERROR_RET(aSession, COMGETTER(Machine)(machine.asOutParam()), 1);
7515 /* get the session console */
7516 CHECK_ERROR_RET(aSession, COMGETTER(Console)(console.asOutParam()), 1);
7517
7518 CHECK_ERROR(console, CreateSharedFolder(Bstr(name), Bstr(hostpath), fWritable));
7519
7520 if (console)
7521 aSession->Close();
7522 }
7523 else
7524 {
7525 /* open a session for the VM */
7526 CHECK_ERROR_RET (aVirtualBox, OpenSession(aSession, uuid), 1);
7527
7528 /* get the mutable session machine */
7529 aSession->COMGETTER(Machine)(machine.asOutParam());
7530
7531 CHECK_ERROR(machine, CreateSharedFolder(Bstr(name), Bstr(hostpath), fWritable));
7532
7533 if (SUCCEEDED(rc))
7534 CHECK_ERROR(machine, SaveSettings());
7535
7536 aSession->Close();
7537 }
7538 }
7539 else if (strcmp(argv[0], "remove") == 0)
7540 {
7541 /* we need at least two more parameters */
7542 if (argc < 3)
7543 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Not enough parameters");
7544
7545 char *name = NULL;
7546 bool fTransient = false;
7547
7548 for (int i = 2; i < argc; i++)
7549 {
7550 if (strcmp(argv[i], "-name") == 0)
7551 {
7552 if (argc <= i + 1 || !*argv[i+1])
7553 return errorArgument("Missing argument to '%s'", argv[i]);
7554 i++;
7555 name = argv[i];
7556 }
7557 else if (strcmp(argv[i], "-transient") == 0)
7558 {
7559 fTransient = true;
7560 }
7561 else
7562 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
7563 }
7564
7565 /* required arguments */
7566 if (!name)
7567 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Parameter -name is required");
7568
7569 if (fTransient)
7570 {
7571 ComPtr <IConsole> console;
7572
7573 /* open an existing session for the VM */
7574 CHECK_ERROR_RET(aVirtualBox, OpenExistingSession (aSession, uuid), 1);
7575 /* get the session machine */
7576 CHECK_ERROR_RET(aSession, COMGETTER(Machine)(machine.asOutParam()), 1);
7577 /* get the session console */
7578 CHECK_ERROR_RET(aSession, COMGETTER(Console)(console.asOutParam()), 1);
7579
7580 CHECK_ERROR(console, RemoveSharedFolder(Bstr(name)));
7581
7582 if (console)
7583 aSession->Close();
7584 }
7585 else
7586 {
7587 /* open a session for the VM */
7588 CHECK_ERROR_RET (aVirtualBox, OpenSession(aSession, uuid), 1);
7589
7590 /* get the mutable session machine */
7591 aSession->COMGETTER(Machine)(machine.asOutParam());
7592
7593 CHECK_ERROR(machine, RemoveSharedFolder(Bstr(name)));
7594
7595 /* commit and close the session */
7596 CHECK_ERROR(machine, SaveSettings());
7597 aSession->Close();
7598 }
7599 }
7600 else
7601 return errorSyntax(USAGE_SETPROPERTY, "Invalid parameter '%s'", Utf8Str(argv[0]).raw());
7602
7603 return 0;
7604}
7605
7606static int handleVMStatistics(int argc, char *argv[],
7607 ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
7608{
7609 HRESULT rc;
7610
7611 /* at least one option: the UUID or name of the VM */
7612 if (argc < 1)
7613 return errorSyntax(USAGE_VM_STATISTICS, "Incorrect number of parameters");
7614
7615 /* try to find the given machine */
7616 ComPtr <IMachine> machine;
7617 Guid uuid (argv[0]);
7618 if (!uuid.isEmpty())
7619 CHECK_ERROR(aVirtualBox, GetMachine(uuid, machine.asOutParam()));
7620 else
7621 {
7622 CHECK_ERROR(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
7623 if (SUCCEEDED (rc))
7624 machine->COMGETTER(Id)(uuid.asOutParam());
7625 }
7626 if (FAILED(rc))
7627 return 1;
7628
7629 /* parse arguments. */
7630 bool fReset = false;
7631 bool fWithDescriptions = false;
7632 const char *pszPattern = NULL; /* all */
7633 for (int i = 1; i < argc; i++)
7634 {
7635 if (!strcmp(argv[i], "-pattern"))
7636 {
7637 if (pszPattern)
7638 return errorSyntax(USAGE_VM_STATISTICS, "Multiple -patterns options is not permitted");
7639 if (i + 1 >= argc)
7640 return errorArgument("Missing argument to '%s'", argv[i]);
7641 pszPattern = argv[++i];
7642 }
7643 else if (!strcmp(argv[i], "-descriptions"))
7644 fWithDescriptions = true;
7645 /* add: -file <filename> and -formatted */
7646 else if (!strcmp(argv[i], "-reset"))
7647 fReset = true;
7648 else
7649 return errorSyntax(USAGE_VM_STATISTICS, "Unknown option '%s'", argv[i]);
7650 }
7651 if (fReset && fWithDescriptions)
7652 return errorSyntax(USAGE_VM_STATISTICS, "The -reset and -descriptions options does not mix");
7653
7654
7655 /* open an existing session for the VM. */
7656 CHECK_ERROR(aVirtualBox, OpenExistingSession(aSession, uuid));
7657 if (SUCCEEDED(rc))
7658 {
7659 /* get the session console. */
7660 ComPtr <IConsole> console;
7661 CHECK_ERROR(aSession, COMGETTER(Console)(console.asOutParam()));
7662 if (SUCCEEDED(rc))
7663 {
7664 /* get the machine debugger. */
7665 ComPtr <IMachineDebugger> debugger;
7666 CHECK_ERROR(console, COMGETTER(Debugger)(debugger.asOutParam()));
7667 if (SUCCEEDED(rc))
7668 {
7669 if (fReset)
7670 CHECK_ERROR(debugger, ResetStats(Bstr(pszPattern).raw()));
7671 else
7672 {
7673 Bstr stats;
7674 CHECK_ERROR(debugger, GetStats(Bstr(pszPattern).raw(), fWithDescriptions, stats.asOutParam()));
7675 if (SUCCEEDED(rc))
7676 {
7677 /* if (fFormatted)
7678 { big mess }
7679 else
7680 */
7681 RTPrintf("%ls\n", stats.raw());
7682 }
7683 }
7684 }
7685 aSession->Close();
7686 }
7687 }
7688
7689 return SUCCEEDED(rc) ? 0 : 1;
7690}
7691
7692static char *toBaseMetricNames(const char *metricList)
7693{
7694 char *newList = (char*)RTMemAlloc(strlen(metricList) + 1);
7695 int cSlashes = 0;
7696 bool fSkip = false;
7697 const char *src = metricList;
7698 char c, *dst = newList;
7699 while ((c = *src++))
7700 if (c == ':')
7701 fSkip = true;
7702 else if (c == '/' && ++cSlashes == 2)
7703 fSkip = true;
7704 else if (c == ',')
7705 {
7706 fSkip = false;
7707 cSlashes = 0;
7708 *dst++ = c;
7709 }
7710 else
7711 if (!fSkip)
7712 *dst++ = c;
7713 *dst = 0;
7714 return newList;
7715}
7716
7717static int parseFilterParameters(int argc, char *argv[],
7718 ComPtr<IVirtualBox> aVirtualBox,
7719 ComSafeArrayOut(BSTR, outMetrics),
7720 ComSafeArrayOut(BSTR, outBaseMetrics),
7721 ComSafeArrayOut(IUnknown *, outObjects))
7722{
7723 HRESULT rc = S_OK;
7724 com::SafeArray<BSTR> retMetrics(1);
7725 com::SafeArray<BSTR> retBaseMetrics(1);
7726 com::SafeIfaceArray <IUnknown> retObjects;
7727
7728 Bstr metricNames, baseNames;
7729
7730 /* Metric list */
7731 if (argc > 1)
7732 {
7733 metricNames = argv[1];
7734 char *tmp = toBaseMetricNames(argv[1]);
7735 if (!tmp)
7736 return VERR_NO_MEMORY;
7737 baseNames = tmp;
7738 RTMemFree(tmp);
7739 }
7740 else
7741 {
7742 metricNames = L"*";
7743 baseNames = L"*";
7744 }
7745 metricNames.cloneTo(&retMetrics[0]);
7746 baseNames.cloneTo(&retBaseMetrics[0]);
7747
7748 /* Object name */
7749 if (argc > 0 && strcmp(argv[0], "*"))
7750 {
7751 if (!strcmp(argv[0], "host"))
7752 {
7753 ComPtr<IHost> host;
7754 CHECK_ERROR(aVirtualBox, COMGETTER(Host)(host.asOutParam()));
7755 retObjects.reset(1);
7756 host.queryInterfaceTo(&retObjects[0]);
7757 }
7758 else
7759 {
7760 ComPtr <IMachine> machine;
7761 rc = aVirtualBox->FindMachine(Bstr(argv[0]), machine.asOutParam());
7762 if (SUCCEEDED (rc))
7763 {
7764 retObjects.reset(1);
7765 machine.queryInterfaceTo(&retObjects[0]);
7766 }
7767 else
7768 {
7769 errorArgument("Invalid machine name: '%s'", argv[0]);
7770 return rc;
7771 }
7772 }
7773
7774 }
7775
7776 retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
7777 retBaseMetrics.detachTo(ComSafeArrayOutArg(outBaseMetrics));
7778 retObjects.detachTo(ComSafeArrayOutArg(outObjects));
7779
7780 return rc;
7781}
7782
7783static Bstr getObjectName(ComPtr<IVirtualBox> aVirtualBox,
7784 ComPtr<IUnknown> aObject)
7785{
7786 HRESULT rc;
7787
7788 ComPtr<IHost> host = aObject;
7789 if (!host.isNull())
7790 return Bstr("host");
7791
7792 ComPtr<IMachine> machine = aObject;
7793 if (!machine.isNull())
7794 {
7795 Bstr name;
7796 CHECK_ERROR(machine, COMGETTER(Name)(name.asOutParam()));
7797 if (SUCCEEDED(rc))
7798 return name;
7799 }
7800 return Bstr("unknown");
7801}
7802
7803static void listAffectedMetrics(ComPtr<IVirtualBox> aVirtualBox,
7804 ComSafeArrayIn(IPerformanceMetric*, aMetrics))
7805{
7806 HRESULT rc;
7807 com::SafeIfaceArray<IPerformanceMetric> metrics(ComSafeArrayInArg(aMetrics));
7808 if (metrics.size())
7809 {
7810 ComPtr<IUnknown> object;
7811 Bstr metricName;
7812 RTPrintf("The following metrics were modified:\n\n"
7813 "Object Metric\n"
7814 "---------- --------------------\n");
7815 for (size_t i = 0; i < metrics.size(); i++)
7816 {
7817 CHECK_ERROR(metrics[i], COMGETTER(Object)(object.asOutParam()));
7818 CHECK_ERROR(metrics[i], COMGETTER(MetricName)(metricName.asOutParam()));
7819 RTPrintf("%-10ls %-20ls\n",
7820 getObjectName(aVirtualBox, object).raw(), metricName.raw());
7821 }
7822 RTPrintf("\n");
7823 }
7824 else
7825 {
7826 RTPrintf("No metrics match the specified filter!\n");
7827 }
7828}
7829
7830/**
7831 * list *
7832 */
7833static int handleMetricsList(int argc, char *argv[],
7834 ComPtr<IVirtualBox> aVirtualBox,
7835 ComPtr<IPerformanceCollector> performanceCollector)
7836{
7837 HRESULT rc;
7838 com::SafeArray<BSTR> metrics;
7839 com::SafeArray<BSTR> baseMetrics;
7840 com::SafeIfaceArray<IUnknown> objects;
7841
7842 rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
7843 ComSafeArrayAsOutParam(metrics),
7844 ComSafeArrayAsOutParam(baseMetrics),
7845 ComSafeArrayAsOutParam(objects));
7846 if (FAILED(rc))
7847 return 1;
7848
7849 com::SafeIfaceArray<IPerformanceMetric> metricInfo;
7850
7851 CHECK_ERROR(performanceCollector,
7852 GetMetrics(ComSafeArrayAsInParam(metrics),
7853 ComSafeArrayAsInParam(objects),
7854 ComSafeArrayAsOutParam(metricInfo)));
7855
7856 ComPtr<IUnknown> object;
7857 Bstr metricName, unit, description;
7858 ULONG period, count;
7859 LONG minimum, maximum;
7860 RTPrintf(
7861"Object Metric Unit Minimum Maximum Period Count Description\n"
7862"---------- -------------------- ---- ---------- ---------- ---------- ---------- -----------\n");
7863 for (size_t i = 0; i < metricInfo.size(); i++)
7864 {
7865 CHECK_ERROR(metricInfo[i], COMGETTER(Object)(object.asOutParam()));
7866 CHECK_ERROR(metricInfo[i], COMGETTER(MetricName)(metricName.asOutParam()));
7867 CHECK_ERROR(metricInfo[i], COMGETTER(Period)(&period));
7868 CHECK_ERROR(metricInfo[i], COMGETTER(Count)(&count));
7869 CHECK_ERROR(metricInfo[i], COMGETTER(MinimumValue)(&minimum));
7870 CHECK_ERROR(metricInfo[i], COMGETTER(MaximumValue)(&maximum));
7871 CHECK_ERROR(metricInfo[i], COMGETTER(Unit)(unit.asOutParam()));
7872 CHECK_ERROR(metricInfo[i], COMGETTER(Description)(description.asOutParam()));
7873 RTPrintf("%-10ls %-20ls %-4ls %10d %10d %10u %10u %ls\n",
7874 getObjectName(aVirtualBox, object).raw(), metricName.raw(), unit.raw(),
7875 minimum, maximum, period, count, description.raw());
7876 }
7877
7878 return 0;
7879}
7880
7881/**
7882 * Metics setup
7883 */
7884static int handleMetricsSetup(int argc, char *argv[],
7885 ComPtr<IVirtualBox> aVirtualBox,
7886 ComPtr<IPerformanceCollector> performanceCollector)
7887{
7888 HRESULT rc;
7889 com::SafeArray<BSTR> metrics;
7890 com::SafeArray<BSTR> baseMetrics;
7891 com::SafeIfaceArray<IUnknown> objects;
7892 ULONG period = 1, samples = 1;
7893 bool listMatches = false;
7894 int i;
7895
7896 for (i = 1; i < argc; i++)
7897 {
7898 if (strcmp(argv[i], "-period") == 0)
7899 {
7900 if (argc <= i + 1)
7901 return errorArgument("Missing argument to '%s'", argv[i]);
7902 char *endptr = NULL;
7903 period = strtoul (argv[++i], &endptr, 10);
7904 if (!endptr || *endptr || !period)
7905 return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
7906 }
7907 else if (strcmp(argv[i], "-samples") == 0)
7908 {
7909 if (argc <= i + 1)
7910 return errorArgument("Missing argument to '%s'", argv[i]);
7911 char *endptr = NULL;
7912 samples = strtoul (argv[++i], &endptr, 10);
7913 if (!endptr || *endptr)
7914 return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
7915 }
7916 else if (strcmp(argv[i], "-list") == 0)
7917 listMatches = true;
7918 else
7919 break; /* The rest of params should define the filter */
7920 }
7921
7922 rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
7923 ComSafeArrayAsOutParam(metrics),
7924 ComSafeArrayAsOutParam(baseMetrics),
7925 ComSafeArrayAsOutParam(objects));
7926 if (FAILED(rc))
7927 return 1;
7928
7929 com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
7930 CHECK_ERROR(performanceCollector,
7931 SetupMetrics(ComSafeArrayAsInParam(metrics),
7932 ComSafeArrayAsInParam(objects), period, samples,
7933 ComSafeArrayAsOutParam(affectedMetrics)));
7934 if (listMatches)
7935 listAffectedMetrics(aVirtualBox,
7936 ComSafeArrayAsInParam(affectedMetrics));
7937
7938 return 0;
7939}
7940
7941/**
7942 * metrics query
7943 */
7944static int handleMetricsQuery(int argc, char *argv[],
7945 ComPtr<IVirtualBox> aVirtualBox,
7946 ComPtr<IPerformanceCollector> performanceCollector)
7947{
7948 HRESULT rc;
7949 com::SafeArray<BSTR> metrics;
7950 com::SafeArray<BSTR> baseMetrics;
7951 com::SafeIfaceArray<IUnknown> objects;
7952
7953 rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
7954 ComSafeArrayAsOutParam(metrics),
7955 ComSafeArrayAsOutParam(baseMetrics),
7956 ComSafeArrayAsOutParam(objects));
7957 if (FAILED(rc))
7958 return 1;
7959
7960 com::SafeArray<BSTR> retNames;
7961 com::SafeIfaceArray<IUnknown> retObjects;
7962 com::SafeArray<BSTR> retUnits;
7963 com::SafeArray<ULONG> retScales;
7964 com::SafeArray<ULONG> retSequenceNumbers;
7965 com::SafeArray<ULONG> retIndices;
7966 com::SafeArray<ULONG> retLengths;
7967 com::SafeArray<LONG> retData;
7968 CHECK_ERROR (performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
7969 ComSafeArrayAsInParam(objects),
7970 ComSafeArrayAsOutParam(retNames),
7971 ComSafeArrayAsOutParam(retObjects),
7972 ComSafeArrayAsOutParam(retUnits),
7973 ComSafeArrayAsOutParam(retScales),
7974 ComSafeArrayAsOutParam(retSequenceNumbers),
7975 ComSafeArrayAsOutParam(retIndices),
7976 ComSafeArrayAsOutParam(retLengths),
7977 ComSafeArrayAsOutParam(retData)) );
7978
7979 RTPrintf("Object Metric Values\n"
7980 "---------- -------------------- --------------------------------------------\n");
7981 for (unsigned i = 0; i < retNames.size(); i++)
7982 {
7983 Bstr metricUnit(retUnits[i]);
7984 Bstr metricName(retNames[i]);
7985 RTPrintf("%-10ls %-20ls ", getObjectName(aVirtualBox, retObjects[i]).raw(), metricName.raw());
7986 const char *separator = "";
7987 for (unsigned j = 0; j < retLengths[i]; j++)
7988 {
7989 if (retScales[i] == 1)
7990 RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
7991 else
7992 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
7993 (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
7994 separator = ", ";
7995 }
7996 RTPrintf("\n");
7997 }
7998
7999 return 0;
8000}
8001
8002static void getTimestamp(char *pts, size_t tsSize)
8003{
8004 *pts = 0;
8005 AssertReturnVoid(tsSize >= 13); /* 3+3+3+3+1 */
8006 RTTIMESPEC TimeSpec;
8007 RTTIME Time;
8008 RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
8009 pts += RTStrFormatNumber(pts, Time.u8Hour, 10, 2, 0, RTSTR_F_ZEROPAD);
8010 *pts++ = ':';
8011 pts += RTStrFormatNumber(pts, Time.u8Minute, 10, 2, 0, RTSTR_F_ZEROPAD);
8012 *pts++ = ':';
8013 pts += RTStrFormatNumber(pts, Time.u8Second, 10, 2, 0, RTSTR_F_ZEROPAD);
8014 *pts++ = '.';
8015 pts += RTStrFormatNumber(pts, Time.u32Nanosecond / 1000000, 10, 3, 0, RTSTR_F_ZEROPAD);
8016 *pts = 0;
8017}
8018
8019/** Used by the handleMetricsCollect loop. */
8020static bool volatile g_fKeepGoing = true;
8021
8022#ifdef RT_OS_WINDOWS
8023/**
8024 * Handler routine for catching Ctrl-C, Ctrl-Break and closing of
8025 * the console.
8026 *
8027 * @returns true if handled, false if not handled.
8028 * @param dwCtrlType The type of control signal.
8029 *
8030 * @remarks This is called on a new thread.
8031 */
8032static BOOL WINAPI ctrlHandler(DWORD dwCtrlType)
8033{
8034 switch (dwCtrlType)
8035 {
8036 /* Ctrl-C or Ctrl-Break or Close */
8037 case CTRL_C_EVENT:
8038 case CTRL_BREAK_EVENT:
8039 case CTRL_CLOSE_EVENT:
8040 /* Let's shut down gracefully. */
8041 ASMAtomicWriteBool(&g_fKeepGoing, false);
8042 return TRUE;
8043 }
8044 /* Don't care about the rest -- let it die a horrible death. */
8045 return FALSE;
8046}
8047#endif /* RT_OS_WINDOWS */
8048
8049/**
8050 * collect
8051 */
8052static int handleMetricsCollect(int argc, char *argv[],
8053 ComPtr<IVirtualBox> aVirtualBox,
8054 ComPtr<IPerformanceCollector> performanceCollector)
8055{
8056 HRESULT rc;
8057 com::SafeArray<BSTR> metrics;
8058 com::SafeArray<BSTR> baseMetrics;
8059 com::SafeIfaceArray<IUnknown> objects;
8060 ULONG period = 1, samples = 1;
8061 bool isDetached = false, listMatches = false;
8062 int i;
8063 for (i = 1; i < argc; i++)
8064 {
8065 if (strcmp(argv[i], "-period") == 0)
8066 {
8067 if (argc <= i + 1)
8068 return errorArgument("Missing argument to '%s'", argv[i]);
8069 char *endptr = NULL;
8070 period = strtoul (argv[++i], &endptr, 10);
8071 if (!endptr || *endptr || !period)
8072 return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
8073 }
8074 else if (strcmp(argv[i], "-samples") == 0)
8075 {
8076 if (argc <= i + 1)
8077 return errorArgument("Missing argument to '%s'", argv[i]);
8078 char *endptr = NULL;
8079 samples = strtoul (argv[++i], &endptr, 10);
8080 if (!endptr || *endptr || !samples)
8081 return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
8082 }
8083 else if (strcmp(argv[i], "-list") == 0)
8084 listMatches = true;
8085 else if (strcmp(argv[i], "-detach") == 0)
8086 isDetached = true;
8087 else
8088 break; /* The rest of params should define the filter */
8089 }
8090
8091 rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
8092 ComSafeArrayAsOutParam(metrics),
8093 ComSafeArrayAsOutParam(baseMetrics),
8094 ComSafeArrayAsOutParam(objects));
8095 if (FAILED(rc))
8096 return 1;
8097
8098
8099 com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
8100 CHECK_ERROR(performanceCollector,
8101 SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
8102 ComSafeArrayAsInParam(objects), period, samples,
8103 ComSafeArrayAsOutParam(affectedMetrics)));
8104 if (listMatches)
8105 listAffectedMetrics(aVirtualBox,
8106 ComSafeArrayAsInParam(affectedMetrics));
8107 if (!affectedMetrics.size())
8108 return 1;
8109
8110 if (isDetached)
8111 {
8112 RTPrintf("Warning! The background process holding collected metrics will shutdown\n"
8113 "in few seconds, discarding all collected data and parameters.\n");
8114 return 0;
8115 }
8116
8117#ifdef RT_OS_WINDOWS
8118 SetConsoleCtrlHandler(ctrlHandler, true);
8119#endif /* RT_OS_WINDOWS */
8120
8121 RTPrintf("Time stamp Object Metric Value\n");
8122
8123 while (g_fKeepGoing)
8124 {
8125 RTPrintf("------------ ---------- -------------------- --------------------\n");
8126 RTThreadSleep(period * 1000); // Sleep for 'period' seconds
8127 char ts[15];
8128
8129 getTimestamp(ts, sizeof(ts));
8130 com::SafeArray<BSTR> retNames;
8131 com::SafeIfaceArray<IUnknown> retObjects;
8132 com::SafeArray<BSTR> retUnits;
8133 com::SafeArray<ULONG> retScales;
8134 com::SafeArray<ULONG> retSequenceNumbers;
8135 com::SafeArray<ULONG> retIndices;
8136 com::SafeArray<ULONG> retLengths;
8137 com::SafeArray<LONG> retData;
8138 CHECK_ERROR (performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
8139 ComSafeArrayAsInParam(objects),
8140 ComSafeArrayAsOutParam(retNames),
8141 ComSafeArrayAsOutParam(retObjects),
8142 ComSafeArrayAsOutParam(retUnits),
8143 ComSafeArrayAsOutParam(retScales),
8144 ComSafeArrayAsOutParam(retSequenceNumbers),
8145 ComSafeArrayAsOutParam(retIndices),
8146 ComSafeArrayAsOutParam(retLengths),
8147 ComSafeArrayAsOutParam(retData)) );
8148 for (unsigned i = 0; i < retNames.size(); i++)
8149 {
8150 Bstr metricUnit(retUnits[i]);
8151 Bstr metricName(retNames[i]);
8152 RTPrintf("%-12s %-10ls %-20ls ", ts, getObjectName(aVirtualBox, retObjects[i]).raw(), metricName.raw());
8153 const char *separator = "";
8154 for (unsigned j = 0; j < retLengths[i]; j++)
8155 {
8156 if (retScales[i] == 1)
8157 RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
8158 else
8159 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
8160 (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
8161 separator = ", ";
8162 }
8163 RTPrintf("\n");
8164 }
8165 }
8166
8167#ifdef RT_OS_WINDOWS
8168 SetConsoleCtrlHandler(ctrlHandler, false);
8169#endif /* RT_OS_WINDOWS */
8170
8171 return 0;
8172}
8173
8174static int handleMetrics(int argc, char *argv[],
8175 ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
8176{
8177 int rc;
8178
8179 /* at least one option: subcommand name */
8180 if (argc < 1)
8181 return errorSyntax(USAGE_METRICS, "Subcommand missing");
8182
8183 ComPtr<IPerformanceCollector> performanceCollector;
8184 CHECK_ERROR(aVirtualBox, COMGETTER(PerformanceCollector)(performanceCollector.asOutParam()));
8185
8186 if (!strcmp(argv[0], "list"))
8187 rc = handleMetricsList(argc, argv, aVirtualBox, performanceCollector);
8188 else if (!strcmp(argv[0], "setup"))
8189 rc = handleMetricsSetup(argc, argv, aVirtualBox, performanceCollector);
8190 else if (!strcmp(argv[0], "query"))
8191 rc = handleMetricsQuery(argc, argv, aVirtualBox, performanceCollector);
8192 else if (!strcmp(argv[0], "collect"))
8193 rc = handleMetricsCollect(argc, argv, aVirtualBox, performanceCollector);
8194 else
8195 return errorSyntax(USAGE_METRICS, "Invalid subcommand '%s'", argv[0]);
8196
8197 return rc;
8198}
8199#endif /* !VBOX_ONLY_DOCS */
8200
8201enum ConvertSettings
8202{
8203 ConvertSettings_No = 0,
8204 ConvertSettings_Yes = 1,
8205 ConvertSettings_Backup = 2,
8206 ConvertSettings_Ignore = 3,
8207};
8208
8209#ifndef VBOX_ONLY_DOCS
8210/**
8211 * Checks if any of the settings files were auto-converted and informs the
8212 * user if so.
8213 *
8214 * @return @false if the program should terminate and @true otherwise.
8215 */
8216static bool checkForAutoConvertedSettings (ComPtr<IVirtualBox> virtualBox,
8217 ComPtr<ISession> session,
8218 ConvertSettings fConvertSettings)
8219{
8220 /* return early if nothing to do */
8221 if (fConvertSettings == ConvertSettings_Ignore)
8222 return true;
8223
8224 HRESULT rc;
8225
8226 do
8227 {
8228 Bstr formatVersion;
8229 CHECK_RC_BREAK (virtualBox->
8230 COMGETTER(SettingsFormatVersion) (formatVersion.asOutParam()));
8231
8232 bool isGlobalConverted = false;
8233 std::list <ComPtr <IMachine> > cvtMachines;
8234 std::list <Utf8Str> fileList;
8235 Bstr version;
8236 Bstr filePath;
8237
8238 com::SafeIfaceArray <IMachine> machines;
8239 CHECK_RC_BREAK (virtualBox->
8240 COMGETTER(Machines2) (ComSafeArrayAsOutParam (machines)));
8241
8242 for (size_t i = 0; i < machines.size(); ++ i)
8243 {
8244 BOOL accessible;
8245 CHECK_RC_BREAK (machines [i]->
8246 COMGETTER(Accessible) (&accessible));
8247 if (!accessible)
8248 continue;
8249
8250 CHECK_RC_BREAK (machines [i]->
8251 COMGETTER(SettingsFileVersion) (version.asOutParam()));
8252
8253 if (version != formatVersion)
8254 {
8255 cvtMachines.push_back (machines [i]);
8256 Bstr filePath;
8257 CHECK_RC_BREAK (machines [i]->
8258 COMGETTER(SettingsFilePath) (filePath.asOutParam()));
8259 fileList.push_back (Utf8StrFmt ("%ls (%ls)", filePath.raw(),
8260 version.raw()));
8261 }
8262 }
8263
8264 CHECK_RC_BREAK (rc);
8265
8266 CHECK_RC_BREAK (virtualBox->
8267 COMGETTER(SettingsFileVersion) (version.asOutParam()));
8268 if (version != formatVersion)
8269 {
8270 isGlobalConverted = true;
8271 CHECK_RC_BREAK (virtualBox->
8272 COMGETTER(SettingsFilePath) (filePath.asOutParam()));
8273 fileList.push_back (Utf8StrFmt ("%ls (%ls)", filePath.raw(),
8274 version.raw()));
8275 }
8276
8277 if (fileList.size() > 0)
8278 {
8279 switch (fConvertSettings)
8280 {
8281 case ConvertSettings_No:
8282 {
8283 RTPrintf (
8284"WARNING! The following VirtualBox settings files have been automatically\n"
8285"converted to the new settings file format version '%ls':\n"
8286"\n",
8287 formatVersion.raw());
8288
8289 for (std::list <Utf8Str>::const_iterator f = fileList.begin();
8290 f != fileList.end(); ++ f)
8291 RTPrintf (" %S\n", (*f).raw());
8292 RTPrintf (
8293"\n"
8294"The current command was aborted to prevent overwriting the above settings\n"
8295"files with the results of the auto-conversion without your permission.\n"
8296"Please put one of the following command line switches to the beginning of\n"
8297"the VBoxManage command line and repeat the command:\n"
8298"\n"
8299" -convertSettings - to save all auto-converted files (it will not\n"
8300" be possible to use these settings files with an\n"
8301" older version of VirtualBox in the future);\n"
8302" -convertSettingsBackup - to create backup copies of the settings files in\n"
8303" the old format before saving them in the new format;\n"
8304" -convertSettingsIgnore - to not save the auto-converted settings files.\n"
8305"\n"
8306"Note that if you use -convertSettingsIgnore, the auto-converted settings files\n"
8307"will be implicitly saved in the new format anyway once you change a setting or\n"
8308"start a virtual machine, but NO backup copies will be created in this case.\n");
8309 return false;
8310 }
8311 case ConvertSettings_Yes:
8312 case ConvertSettings_Backup:
8313 {
8314 break;
8315 }
8316 default:
8317 AssertFailedReturn (false);
8318 }
8319
8320 for (std::list <ComPtr <IMachine> >::const_iterator m = cvtMachines.begin();
8321 m != cvtMachines.end(); ++ m)
8322 {
8323 Guid id;
8324 CHECK_RC_BREAK ((*m)->COMGETTER(Id) (id.asOutParam()));
8325
8326 /* open a session for the VM */
8327 CHECK_ERROR_BREAK (virtualBox, OpenSession (session, id));
8328
8329 ComPtr <IMachine> sm;
8330 CHECK_RC_BREAK (session->COMGETTER(Machine) (sm.asOutParam()));
8331
8332 Bstr bakFileName;
8333 if (fConvertSettings == ConvertSettings_Backup)
8334 CHECK_ERROR (sm, SaveSettingsWithBackup (bakFileName.asOutParam()));
8335 else
8336 CHECK_ERROR (sm, SaveSettings());
8337
8338 session->Close();
8339
8340 CHECK_RC_BREAK (rc);
8341 }
8342
8343 CHECK_RC_BREAK (rc);
8344
8345 if (isGlobalConverted)
8346 {
8347 Bstr bakFileName;
8348 if (fConvertSettings == ConvertSettings_Backup)
8349 CHECK_ERROR (virtualBox, SaveSettingsWithBackup (bakFileName.asOutParam()));
8350 else
8351 CHECK_ERROR (virtualBox, SaveSettings());
8352 }
8353
8354 CHECK_RC_BREAK (rc);
8355 }
8356 }
8357 while (0);
8358
8359 return SUCCEEDED (rc);
8360}
8361#endif /* !VBOX_ONLY_DOCS */
8362
8363// main
8364///////////////////////////////////////////////////////////////////////////////
8365
8366int main(int argc, char *argv[])
8367{
8368 /*
8369 * Before we do anything, init the runtime without loading
8370 * the support driver.
8371 */
8372 RTR3Init();
8373
8374 bool fShowLogo = true;
8375 int iCmd = 1;
8376 int iCmdArg;
8377
8378 ConvertSettings fConvertSettings = ConvertSettings_No;
8379
8380 /* global options */
8381 for (int i = 1; i < argc || argc <= iCmd; i++)
8382 {
8383 if ( argc <= iCmd
8384 || (strcmp(argv[i], "help") == 0)
8385 || (strcmp(argv[i], "-?") == 0)
8386 || (strcmp(argv[i], "-h") == 0)
8387 || (strcmp(argv[i], "-help") == 0)
8388 || (strcmp(argv[i], "--help") == 0))
8389 {
8390 showLogo();
8391 printUsage(USAGE_ALL);
8392 return 0;
8393 }
8394 else if ( strcmp(argv[i], "-v") == 0
8395 || strcmp(argv[i], "-version") == 0
8396 || strcmp(argv[i], "-Version") == 0
8397 || strcmp(argv[i], "--version") == 0)
8398 {
8399 /* Print version number, and do nothing else. */
8400 RTPrintf("%sr%d\n", VBOX_VERSION_STRING, VBoxSVNRev ());
8401 exit(0);
8402 }
8403 else if (strcmp(argv[i], "-dumpopts") == 0)
8404 {
8405 /* Special option to dump really all commands,
8406 * even the ones not understood on this platform. */
8407 printUsage(USAGE_DUMPOPTS);
8408 return 0;
8409 }
8410 else if (strcmp(argv[i], "-nologo") == 0)
8411 {
8412 /* suppress the logo */
8413 fShowLogo = false;
8414 iCmd++;
8415 }
8416 else if (strcmp(argv[i], "-convertSettings") == 0)
8417 {
8418 fConvertSettings = ConvertSettings_Yes;
8419 iCmd++;
8420 }
8421 else if (strcmp(argv[i], "-convertSettingsBackup") == 0)
8422 {
8423 fConvertSettings = ConvertSettings_Backup;
8424 iCmd++;
8425 }
8426 else if (strcmp(argv[i], "-convertSettingsIgnore") == 0)
8427 {
8428 fConvertSettings = ConvertSettings_Ignore;
8429 iCmd++;
8430 }
8431 else
8432 {
8433 break;
8434 }
8435 }
8436
8437 iCmdArg = iCmd + 1;
8438
8439 if (fShowLogo)
8440 showLogo();
8441
8442
8443#ifdef VBOX_ONLY_DOCS
8444 int rc = 0;
8445#else /* !VBOX_ONLY_DOCS */
8446 HRESULT rc = 0;
8447
8448 CHECK_RC_RET (com::Initialize());
8449
8450 /*
8451 * The input is in the host OS'es codepage (NT guarantees ACP).
8452 * For VBox we use UTF-8 and convert to UCS-2 when calling (XP)COM APIs.
8453 * For simplicity, just convert the argv[] array here.
8454 */
8455 for (int i = iCmdArg; i < argc; i++)
8456 {
8457 char *converted;
8458 RTStrCurrentCPToUtf8(&converted, argv[i]);
8459 argv[i] = converted;
8460 }
8461
8462 do
8463 {
8464 // scopes all the stuff till shutdown
8465 ////////////////////////////////////////////////////////////////////////////
8466
8467 /* convertdd: does not need a VirtualBox instantiation) */
8468 if (argc >= iCmdArg && (strcmp(argv[iCmd], "convertdd") == 0))
8469 {
8470 rc = handleConvertDDImage(argc - iCmdArg, argv + iCmdArg);
8471 break;
8472 }
8473
8474 ComPtr <IVirtualBox> virtualBox;
8475 ComPtr <ISession> session;
8476
8477 rc = virtualBox.createLocalObject (CLSID_VirtualBox);
8478 if (FAILED(rc))
8479 {
8480 RTPrintf ("[!] Failed to create the VirtualBox object!\n");
8481 PRINT_RC_MESSAGE (rc);
8482
8483 com::ErrorInfo info;
8484 if (!info.isFullAvailable() && !info.isBasicAvailable())
8485 RTPrintf ("[!] Most likely, the VirtualBox COM server is not running "
8486 "or failed to start.\n");
8487 else
8488 PRINT_ERROR_INFO (info);
8489 break;
8490 }
8491
8492 CHECK_RC_BREAK (session.createInprocObject (CLSID_Session));
8493
8494 /* create the event queue
8495 * (here it is necessary only to process remaining XPCOM/IPC events
8496 * after the session is closed) */
8497
8498 EventQueue eventQ;
8499
8500 if (!checkForAutoConvertedSettings (virtualBox, session, fConvertSettings))
8501 break;
8502
8503 /*
8504 * All registered command handlers
8505 */
8506 struct
8507 {
8508 const char *command;
8509 PFNHANDLER handler;
8510 } commandHandlers[] =
8511 {
8512 { "internalcommands", handleInternalCommands },
8513 { "list", handleList },
8514 { "showvminfo", handleShowVMInfo },
8515 { "registervm", handleRegisterVM },
8516 { "unregistervm", handleUnregisterVM },
8517 { "createvdi", handleCreateVDI },
8518 { "modifyvdi", handleModifyVDI },
8519 { "addiscsidisk", handleAddiSCSIDisk },
8520 { "createvm", handleCreateVM },
8521 { "modifyvm", handleModifyVM },
8522 { "clonevdi", handleCloneVDI },
8523 { "startvm", handleStartVM },
8524 { "controlvm", handleControlVM },
8525 { "discardstate", handleDiscardState },
8526 { "adoptstate", handleAdoptdState },
8527 { "snapshot", handleSnapshot },
8528 { "registerimage", handleRegisterImage },
8529 { "unregisterimage", handleUnregisterImage },
8530 { "showvdiinfo", handleShowVDIInfo },
8531#ifdef RT_OS_WINDOWS
8532 { "createhostif", handleCreateHostIF },
8533 { "removehostif", handleRemoveHostIF },
8534#endif
8535 { "getextradata", handleGetExtraData },
8536 { "setextradata", handleSetExtraData },
8537 { "setproperty", handleSetProperty },
8538 { "usbfilter", handleUSBFilter },
8539 { "sharedfolder", handleSharedFolder },
8540 { "vmstatistics", handleVMStatistics },
8541#ifdef VBOX_WITH_GUEST_PROPS
8542 { "guestproperty", handleGuestProperty },
8543#endif /* VBOX_WITH_GUEST_PROPS defined */
8544 { "metrics", handleMetrics },
8545 { NULL, NULL }
8546 };
8547
8548 int commandIndex;
8549 for (commandIndex = 0; commandHandlers[commandIndex].command != NULL; commandIndex++)
8550 {
8551 if (strcmp(commandHandlers[commandIndex].command, argv[iCmd]) == 0)
8552 {
8553 rc = commandHandlers[commandIndex].handler(argc - iCmdArg, &argv[iCmdArg], virtualBox, session);
8554 break;
8555 }
8556 }
8557 if (!commandHandlers[commandIndex].command)
8558 {
8559 rc = errorSyntax(USAGE_ALL, "Invalid command '%s'", Utf8Str(argv[iCmd]).raw());
8560 }
8561
8562 /* Although all handlers should always close the session if they open it,
8563 * we do it here just in case if some of the handlers contains a bug --
8564 * leaving the direct session not closed will turn the machine state to
8565 * Aborted which may have unwanted side effects like killing the saved
8566 * state file (if the machine was in the Saved state before). */
8567 session->Close();
8568
8569 // end "all-stuff" scope
8570 ////////////////////////////////////////////////////////////////////////////
8571 }
8572 while (0);
8573
8574 com::Shutdown();
8575#endif /* !VBOX_ONLY_DOCS */
8576
8577 /*
8578 * Free converted argument vector
8579 */
8580 for (int i = iCmdArg; i < argc; i++)
8581 RTStrFree(argv[i]);
8582
8583 return rc != 0;
8584}
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