VBoxManage.cpp revision 44f5ccf2fcaeb82406c544f69d7fe951e3ca5167
/* $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/initterm.h>
#include "VBoxManage.h"
#ifndef VBOX_ONLY_DOCS
using namespace com;
/** command handler type */
typedef int (*PFNHANDLER)(HandlerArg *a);
#endif /* !VBOX_ONLY_DOCS */
// funcs
///////////////////////////////////////////////////////////////////////////////
void showLogo(void)
{
static bool fShown; /* show only once */
if (!fShown)
{
RTPrintf("VirtualBox Command Line Management Interface Version "
VBOX_VERSION_STRING "\n"
"(C) 2005-2009 Sun Microsystems, Inc.\n"
"All rights reserved.\n"
"\n");
fShown = true;
}
}
#ifndef VBOX_ONLY_DOCS
static int handleRegisterVM(HandlerArg *a)
{
if (a->argc != 1)
{
}
}
static int handleUnregisterVM(HandlerArg *a)
{
/* assume it's a UUID */
{
/* must be a name */
}
if (machine)
{
{
/* are we supposed to delete the config file? */
{
}
}
}
}
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);
}
/**
* Parses a number.
*
* @returns Valid number on success.
* @returns 0 if invalid number. All necesary bitching has been done.
* @param psz Pointer to the nic number.
*/
{
char *pszNext;
if ( RT_SUCCESS(rc)
&& *pszNext == '\0'
&& u32 >= 1
return (unsigned)u32;
return 0;
}
/** @todo refine this after HDD changes; MSC 8.0/64 has trouble with handleModifyVM. */
#if defined(_MSC_VER)
#endif
static int handleStartVM(HandlerArg *a)
{
if (a->argc < 1)
/* assume it's a UUID */
{
/* must be a name */
}
if (machine)
{
/* default to GUI session type */
/* has a session type been specified? */
{
{
sessionType = "gui";
}
{
sessionType = "vrdp";
}
{
sessionType = "capture";
}
else
}
#ifdef RT_OS_LINUX
/* make sure the VM process will start on the same display as VBoxManage */
{
if (display)
}
#endif
RTPrintf("Waiting for the remote session to open...\n");
if (FAILED(resultCode))
{
}
else
{
RTPrintf("Remote session has been successfully opened.\n");
}
}
/* it's important to always close sessions */
}
static int handleControlVM(HandlerArg *a)
{
if (a->argc < 2)
/* try to find the given machine */
{
}
else
{
}
return 1;
/* open a session for the VM */
do
{
/* get the associated console */
/* ... and session machine */
/* which command? */
{
}
{
}
{
}
{
}
{
{
if (info.isBasicAvailable())
{
}
else
{
RTPrintf("Error: failed to save machine state. No error message available!\n");
}
}
}
{
}
{
}
{
/* get the machine debugger. */
}
{
{
errorArgument("Missing argument to '%s'. Expected IBM PC AT set 2 keyboard scancode(s) as hex byte(s).", a->argv[1]);
break;
}
/* Arbitrary restrict the length of a sequence of scancodes to 1024. */
int cScancodes = 0;
/* Process the command line. */
int i;
{
if ( RT_C_IS_XDIGIT (a->argv[i][0])
&& a->argv[i][2] == 0)
{
if (RT_FAILURE (rc))
{
break;
}
}
else
{
break;
}
}
break;
&& i < a->argc)
{
break;
}
/* Send scancodes to the VM.
* Note: 'PutScancodes' did not work here. Only the first scancode was transmitted.
*/
for (i = 0; i < cScancodes; i++)
{
}
}
{
/* Get the number of network adapters */
ULONG NetworkAdapterCount = 0;
if (!n)
{
break;
}
{
break;
}
/* get the corresponding network adapter */
if (adapter)
{
{
}
{
}
else
{
break;
}
}
}
#ifdef VBOX_WITH_VRDP
{
{
break;
}
/* get the corresponding VRDP server */
if (vrdpServer)
{
{
}
{
}
else
{
break;
}
}
}
#endif /* VBOX_WITH_VRDP */
{
if (a->argc < 3)
{
break;
}
{
// assume address
if (attach)
{
}
else
{
}
}
if (attach)
else
{
}
}
{
{
break;
}
uint32_t displayIdx = 0;
if (a->argc == 6)
}
{
bool fAllowLocalLogon = true;
if (a->argc == 7)
{
{
break;
}
fAllowLocalLogon = false;
}
else if (a->argc != 5)
{
break;
}
CHECK_ERROR_BREAK(guest, SetCredentials(Bstr(a->argv[2]), Bstr(a->argv[3]), Bstr(a->argv[4]), fAllowLocalLogon));
}
{
if (a->argc != 3)
{
break;
}
/* unmount? */
{
}
/* host drive? */
{
if (!hostDVDDrive)
{
errorArgument("Invalid host DVD drive name");
break;
}
}
else
{
/* first assume it's a UUID */
{
/* must be a filename, check if it's in the collection */
/* not registered, do that on the fly */
if (!dvdImage)
{
}
}
if (!dvdImage)
{
break;
}
}
}
{
if (a->argc != 3)
{
break;
}
/* unmount? */
{
}
/* host drive? */
{
if (!hostFloppyDrive)
{
errorArgument("Invalid host floppy drive name");
break;
}
}
else
{
/* first assume it's a UUID */
{
/* must be a filename, check if it's in the collection */
/* not registered, do that on the fly */
if (!floppyImage)
{
}
}
if (!floppyImage)
{
break;
}
}
}
#ifdef VBOX_WITH_MEM_BALLOONING
{
if (a->argc != 3)
{
break;
}
int vrc;
if (vrc != VINF_SUCCESS)
{
break;
}
/* guest is running; update IGuest */
}
#endif
{
if (a->argc != 3)
{
break;
}
int vrc;
if (vrc != VINF_SUCCESS)
{
break;
}
/* guest is running; update IGuest */
}
else
{
}
}
while (0);
}
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? */
{
do
{
} while (extraDataKey);
}
else
{
if (value)
else
RTPrintf("No value set!\n");
}
}
else
{
/* assume it's a UUID */
{
/* must be a name */
}
if (machine)
{
/* enumeration? */
{
do
{
{
}
} while (extraDataKey);
}
else
{
if (value)
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
}
{
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 */
enum ConvertSettings
{
ConvertSettings_No = 0,
ConvertSettings_Yes = 1,
};
#ifndef VBOX_ONLY_DOCS
/**
* Checks if any of the settings files were auto-converted and informs the
* user if so.
*
* @return @false if the program should terminate and @true otherwise.
*/
{
/* return early if nothing to do */
return true;
do
{
bool isGlobalConverted = false;
{
if (!accessible)
continue;
if (version != formatVersion)
{
}
}
break;
if (version != formatVersion)
{
isGlobalConverted = true;
}
{
switch (fConvertSettings)
{
case ConvertSettings_No:
{
RTPrintf (
"WARNING! The following VirtualBox settings files have been automatically\n"
"converted to the new settings file format version '%ls':\n"
"\n",
formatVersion.raw());
RTPrintf (
"\n"
"The current command was aborted to prevent overwriting the above settings\n"
"files with the results of the auto-conversion without your permission.\n"
"Please put one of the following command line switches to the beginning of\n"
"the VBoxManage command line and repeat the command:\n"
"\n"
" -convertSettings - to save all auto-converted files (it will not\n"
" be possible to use these settings files with an\n"
" older version of VirtualBox in the future);\n"
" -convertSettingsBackup - to create backup copies of the settings files in\n"
" the old format before saving them in the new format;\n"
" -convertSettingsIgnore - to not save the auto-converted settings files.\n"
"\n"
"Note that if you use -convertSettingsIgnore, the auto-converted settings files\n"
"will be implicitly saved in the new format anyway once you change a setting or\n"
"start a virtual machine, but NO backup copies will be created in this case.\n");
return false;
}
case ConvertSettings_Yes:
case ConvertSettings_Backup:
{
break;
}
default:
AssertFailedReturn (false);
}
m != cvtMachines.end(); ++ m)
{
/* open a session for the VM */
else
break;
}
break;
if (isGlobalConverted)
{
else
}
break;
}
}
while (0);
}
#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++;
}
{
iCmd++;
}
{
iCmd++;
}
{
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;
}
/* create the event queue
* after the session is closed) */
#ifdef USE_XPCOM_QUEUE
#endif
break;
#ifdef USE_XPCOM_QUEUE
#else
#endif
/*
* All registered command handlers
*/
struct
{
const char *command;
} 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 },
{ "showhdinfo", handleShowHardDiskInfo },
{ "getextradata", handleGetExtraData },
{ "setextradata", handleSetExtraData },
{ "setproperty", handleSetProperty },
{ "usbfilter", handleUSBFilter },
{ "sharedfolder", handleSharedFolder },
{ "vmstatistics", handleVMStatistics },
#ifdef VBOX_WITH_GUEST_PROPS
{ "guestproperty", handleGuestProperty },
#endif /* VBOX_WITH_GUEST_PROPS defined */
{ "metrics", handleMetrics },
{ "import", handleImportAppliance },
{ "export", handleExportAppliance },
};
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). */
#ifdef USE_XPCOM_QUEUE
#endif
// end "all-stuff" scope
////////////////////////////////////////////////////////////////////////////
}
while (0);
#endif /* !VBOX_ONLY_DOCS */
/*
* Free converted argument vector
*/
return rc != 0;
}