VirtualBox

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

Last change on this file since 13068 was 12668, checked in by vboxsync, 16 years ago

PerfAPI: Added setupMetricsEx(), enableMetricsEx() and disableMetricsEx(). Added -list option to VBoxManage metrics setup. Updated docs and tstAPI.

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