VBoxManage.cpp revision 4328e87247f4a96449677e199c7e99ef516fc1ce
/* $Id$ */
/** @file
* VBoxManage - VirtualBox's command-line interface.
*/
/*
* Copyright (C) 2006-2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifndef VBOX_ONLY_DOCS
#include <vector>
#include <list>
#endif /* !VBOX_ONLY_DOCS */
#include <iprt/buildconfig.h>
#include <iprt/initterm.h>
#include "VBoxManage.h"
#ifndef VBOX_ONLY_DOCS
using namespace com;
#endif /* !VBOX_ONLY_DOCS */
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/*extern*/ bool g_fDetailedProgress = false;
////////////////////////////////////////////////////////////////////////////////
//
// functions
//
////////////////////////////////////////////////////////////////////////////////
#ifndef VBOX_ONLY_DOCS
/**
* Print out progress on the console
*/
{
ULONG ulLastPercent = 0;
if (!g_fDetailedProgress)
{
RTPrintf("0%%...");
}
{
if (g_fDetailedProgress)
{
if (ulLastOperation != ulOperation)
{
}
if ( (ulCurrentPercent != ulLastPercent)
)
{
RTPrintf("(%ld/%ld) %ls %ld%% => %ld%% (%d s remaining)\n", ulOperation + 1, cOperations, bstrOperationDescription.raw(), ulCurrentOperationPercent, ulCurrentPercent, lSecsRem);
}
}
else
{
/* did we cross a 10% mark? */
{
/* make sure to also print out missed steps */
for (ULONG curVal = (ulLastPercent / 10) * 10 + 10; curVal <= (ulCurrentPercent / 10) * 10; curVal += 10)
{
if (curVal < 100)
{
}
}
}
}
if (fCompleted)
break;
/* make sure the loop is not too tight */
}
/* complete the line. */
{
RTPrintf("100%%\n");
else
RTPrintf("FAILED\n");
}
else
RTPrintf("\n");
return iRc;
}
static int handleRegisterVM(HandlerArg *a)
{
if (a->argc != 1)
/** @todo Ugly hack to get both the API interpretation of relative paths
* and the client's interpretation of relative paths. Remove after the API
* has been redesigned. */
if (rc == VBOX_E_FILE_ERROR)
{
if (RT_FAILURE(vrc))
{
return 1;
}
}
{
}
}
static const RTGETOPTDEF g_aUnregisterVMOptions[] =
{
};
static int handleUnregisterVM(HandlerArg *a)
{
bool fDelete = false;
int c;
// start at 0 because main() has hacked both the argc and argv given to us
RTGetOptInit(&GetState, a->argc, a->argv, g_aUnregisterVMOptions, RT_ELEMENTS(g_aUnregisterVMOptions), 0, 0 /* fFlags */);
{
switch (c)
{
case 'd': // --delete
fDelete = true;
break;
case VINF_GETOPT_NOT_OPTION:
if (!VMName)
else
break;
default:
if (c > 0)
{
if (RT_C_IS_PRINT(c))
else
}
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
else if (ValueUnion.pDef)
else
}
}
/* check for required options */
if (!VMName)
/* assume it's a UUID */
{
/* must be a name */
}
if (machine)
{
}
}
static int handleCreateVM(HandlerArg *a)
{
bool fRegister = false;
RTUuidClear(&id);
for (int i = 0; i < a->argc; i++)
{
{
if (a->argc <= i + 1)
i++;
baseFolder = a->argv[i];
}
{
if (a->argc <= i + 1)
i++;
settingsFile = a->argv[i];
}
{
if (a->argc <= i + 1)
i++;
}
{
if (a->argc <= i + 1)
i++;
}
{
if (a->argc <= i + 1)
i++;
}
{
fRegister = true;
}
else
}
if (!name)
if (!!baseFolder && !!settingsFile)
do
{
if (!settingsFile)
else
if (fRegister)
{
}
RTPrintf("Virtual machine '%ls' is created%s.\n"
"UUID: %s\n"
"Settings file: '%ls'\n",
}
while (0);
}
static int handleStartVM(HandlerArg *a)
{
static const RTGETOPTDEF s_aStartVMOptions[] =
{
};
int c;
// start at 0 because main() has hacked both the argc and argv given to us
RTGetOptInit(&GetState, a->argc, a->argv, s_aStartVMOptions, RT_ELEMENTS(s_aStartVMOptions), 0, 0 /* fFlags */);
{
switch (c)
{
case 't': // --type
{
sessionType = "gui";
}
#ifdef VBOX_WITH_VBOXSDL
{
sessionType = "sdl";
}
#endif
#ifdef VBOX_WITH_VRDP
{
sessionType = "vrdp";
}
#endif
#ifdef VBOX_WITH_HEADLESS
{
sessionType = "capture";
}
{
sessionType = "headless";
}
#endif
else
break;
case VINF_GETOPT_NOT_OPTION:
if (!VMName)
else
break;
default:
if (c > 0)
{
if (RT_C_IS_PRINT(c))
else
}
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
else if (ValueUnion.pDef)
else
}
}
/* check for required options */
if (!VMName)
/* assume it's a UUID */
{
/* must be a name */
}
if (machine)
{
#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
/* make sure the VM process will start on the same display as VBoxManage */
if (pszDisplay)
if (pszXAuth)
#endif
RTPrintf("Waiting for the remote session to open...\n");
{
}
else
{
RTPrintf("Remote session has been successfully opened.\n");
}
}
/* it's important to always close sessions */
}
static int handleDiscardState(HandlerArg *a)
{
if (a->argc != 1)
/* assume it's a UUID */
{
/* must be a name */
}
if (machine)
{
do
{
/* we have to open a session for this task */
do
{
} while (0);
} while (0);
}
}
static int handleAdoptdState(HandlerArg *a)
{
if (a->argc != 2)
/* assume it's a UUID */
{
/* must be a name */
}
if (machine)
{
do
{
/* we have to open a session for this task */
do
{
} while (0);
} while (0);
}
}
static int handleGetExtraData(HandlerArg *a)
{
if (a->argc != 2)
/* global data? */
{
/* enumeration? */
{
for (size_t i = 0;
++i)
{
}
}
else
{
else
RTPrintf("No value set!\n");
}
}
else
{
/* assume it's a UUID */
{
/* must be a name */
}
if (machine)
{
/* enumeration? */
{
for (size_t i = 0;
++i)
{
}
}
else
{
else
RTPrintf("No value set!\n");
}
}
}
}
static int handleSetExtraData(HandlerArg *a)
{
if (a->argc < 2)
/* global data? */
{
if (a->argc < 3)
else if (a->argc == 3)
else
}
else
{
/* assume it's a UUID */
{
/* must be a name */
}
if (machine)
{
if (a->argc < 3)
else if (a->argc == 3)
else
}
}
}
static int handleSetProperty(HandlerArg *a)
{
/* there must be two arguments: property name and value */
if (a->argc != 2)
{
/* reset to default? */
else
}
{
/* reset to default? */
else
}
{
/* reset to default? */
else
}
{
/* reset to default? */
else
}
{
int vrc;
if (vrc != VINF_SUCCESS)
}
else
}
static int handleSharedFolder(HandlerArg *a)
{
/* we need at least a command and target */
if (a->argc < 2)
/* assume it's a UUID */
{
/* must be a name */
}
if (!machine)
return 1;
{
/* we need at least four more parameters */
if (a->argc < 5)
bool fTransient = false;
bool fWritable = true;
for (int i = 2; i < a->argc; i++)
{
{
i++;
}
{
i++;
}
{
fWritable = false;
}
{
fTransient = true;
}
else
}
/* required arguments */
{
}
if (fTransient)
{
/* open an existing session for the VM */
/* get the session machine */
/* get the session console */
if (console)
}
else
{
/* open a session for the VM */
/* get the mutable session machine */
}
}
{
/* we need at least two more parameters */
if (a->argc < 3)
bool fTransient = false;
for (int i = 2; i < a->argc; i++)
{
{
i++;
}
{
fTransient = true;
}
else
}
/* required arguments */
if (!name)
if (fTransient)
{
/* open an existing session for the VM */
/* get the session machine */
/* get the session console */
if (console)
}
else
{
/* open a session for the VM */
/* get the mutable session machine */
/* commit and close the session */
}
}
else
return 0;
}
static int handleVMStatistics(HandlerArg *a)
{
/* at least one option: the UUID or name of the VM */
if (a->argc < 1)
/* try to find the given machine */
else
{
}
return 1;
/* parse arguments. */
bool fReset = false;
bool fWithDescriptions = false;
for (int i = 1; i < a->argc; i++)
{
{
if (pszPattern)
if (i + 1 >= a->argc)
pszPattern = a->argv[++i];
}
fWithDescriptions = true;
/* add: --file <filename> and --formatted */
fReset = true;
else
}
if (fReset && fWithDescriptions)
/* open an existing session for the VM. */
{
/* get the session console. */
{
/* get the machine debugger. */
{
if (fReset)
else
{
{
/* if (fFormatted)
{ big mess }
else
*/
}
}
}
}
}
}
#endif /* !VBOX_ONLY_DOCS */
// main
///////////////////////////////////////////////////////////////////////////////
{
/*
* Before we do anything, init the runtime without loading
* the support driver.
*/
RTR3Init();
bool fShowLogo = true;
int iCmd = 1;
int iCmdArg;
/* global options */
{
{
showLogo();
return 0;
}
{
/* Print version number, and do nothing else. */
return 0;
}
{
/* Special option to dump really all commands,
* even the ones not understood on this platform. */
return 0;
}
{
/* suppress the logo */
fShowLogo = false;
iCmd++;
}
else
{
break;
}
}
if (fShowLogo)
showLogo();
#ifdef VBOX_ONLY_DOCS
int rc = 0;
#else /* !VBOX_ONLY_DOCS */
{
RTPrintf("ERROR: failed to initialize COM!\n");
return rc;
}
/*
* The input is in the host OS'es codepage (NT guarantees ACP).
* For VBox we use UTF-8 and convert to UCS-2 when calling (XP)COM APIs.
* For simplicity, just convert the argv[] array here.
*/
{
char *converted;
}
do
{
// scopes all the stuff till shutdown
////////////////////////////////////////////////////////////////////////////
/* convertfromraw: does not need a VirtualBox instantiation. */
{
break;
}
RTPrintf("ERROR: failed to create the VirtualBox object!\n");
else
{
RTPrintf("ERROR: failed to create a session object!\n");
}
{
{
RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
}
else
break;
}
/*
* All registered command handlers
*/
static const struct
{
const char *command;
int (*handler)(HandlerArg *a);
} s_commandHandlers[] =
{
{ "internalcommands", handleInternalCommands },
{ "list", handleList },
{ "showvminfo", handleShowVMInfo },
{ "registervm", handleRegisterVM },
{ "unregistervm", handleUnregisterVM },
{ "createhd", handleCreateHardDisk },
{ "modifyhd", handleModifyHardDisk },
{ "clonehd", handleCloneHardDisk },
{ "addiscsidisk", handleAddiSCSIDisk },
{ "createvm", handleCreateVM },
{ "modifyvm", handleModifyVM },
{ "startvm", handleStartVM },
{ "controlvm", handleControlVM },
{ "discardstate", handleDiscardState },
{ "adoptstate", handleAdoptdState },
{ "snapshot", handleSnapshot },
{ "openmedium", handleOpenMedium },
{ "closemedium", handleCloseMedium },
{ "storageattach", handleStorageAttach },
{ "storagectl", handleStorageController },
{ "showhdinfo", handleShowHardDiskInfo },
{ "getextradata", handleGetExtraData },
{ "setextradata", handleSetExtraData },
{ "setproperty", handleSetProperty },
{ "usbfilter", handleUSBFilter },
{ "sharedfolder", handleSharedFolder },
{ "vmstatistics", handleVMStatistics },
#ifdef VBOX_WITH_GUEST_PROPS
{ "guestproperty", handleGuestProperty },
#endif
{ "metrics", handleMetrics },
{ "import", handleImportAppliance },
{ "export", handleExportAppliance },
#ifdef VBOX_WITH_NETFLT
{ "hostonlyif", handleHostonlyIf },
#endif
{ "dhcpserver", handleDHCPServer},
};
int commandIndex;
{
{
break;
}
}
{
}
/* Although all handlers should always close the session if they open it,
* we do it here just in case if some of the handlers contains a bug --
* leaving the direct session not closed will turn the machine state to
* Aborted which may have unwanted side effects like killing the saved
* state file (if the machine was in the Saved state before). */
// end "all-stuff" scope
////////////////////////////////////////////////////////////////////////////
} while (0);
#endif /* !VBOX_ONLY_DOCS */
/*
* Free converted argument vector
*/
return rc != 0;
}