VBoxInternalManage.cpp revision 07bf154df97af02974bb89d4f1ad36afa2b45443
/* $Id$ */
/** @file
* VBoxManage - The 'internalcommands' command.
*
* VBoxInternalManage used to be a second CLI for doing special tricks,
* not intended for general usage, only for assisting VBox developers.
* It is now integrated into VBoxManage.
*/
/*
* Copyright (C) 2006-2007 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 *
*******************************************************************************/
#include <VBox/VBoxHDD-new.h>
#include <iprt/initterm.h>
#include "VBoxManage.h"
/* Includes for the raw disk stuff. */
#ifdef RT_OS_WINDOWS
# include <windows.h>
# include <winioctl.h>
# include <errno.h>
# include <fcntl.h>
# include <unistd.h>
#endif
#ifdef RT_OS_LINUX
# include <stdlib.h> /* atoi() */
#endif /* RT_OS_LINUX */
#ifdef RT_OS_DARWIN
#endif /* RT_OS_DARWIN */
#ifdef RT_OS_SOLARIS
# include <stropts.h>
#endif /* RT_OS_SOLARIS */
using namespace com;
/** Macro for checking whether a partition is of extended type or not. */
/* Maximum number of partitions we can deal with. Ridiculously large number,
* but the memory consumption is rather low so who cares about never using
* most entries. */
#define HOSTPARTITION_MAX 100
typedef struct HOSTPARTITION
{
unsigned uIndex;
unsigned uType;
unsigned uStartCylinder;
unsigned uStartHead;
unsigned uStartSector;
unsigned uEndCylinder;
unsigned uEndHead;
unsigned uEndSector;
typedef struct HOSTPARTITIONS
{
unsigned cPartitions;
/** flag whether we're in internal mode */
bool g_fInternalMode;
/**
* Print the usage info.
*/
{
RTPrintf("Usage: VBoxManage internalcommands <command> [command arguments]\n"
"\n"
"Commands:\n"
"\n"
"%s%s%s%s%s%s%s%s%s"
"WARNING: This is a development tool and shall only be used to analyse\n"
" problems. It is completely unsupported and will change in\n"
" incompatible ways without warning.\n",
(u64Cmd & USAGE_LOADSYMS) ?
" loadsyms <vmname>|<uuid> <symfile> [delta] [module] [module address]\n"
" This will instruct DBGF to load the given symbolfile\n"
" during initialization.\n"
"\n"
: "",
(u64Cmd & USAGE_UNLOADSYMS) ?
" unloadsyms <vmname>|<uuid> <symfile>\n"
" Removes <symfile> from the list of symbol files that\n"
" should be loaded during DBF initialization.\n"
"\n"
: "",
(u64Cmd & USAGE_SETHDUUID) ?
" sethduuid <filepath>\n"
" Assigns a new UUID to the given image file. This way, multiple copies\n"
" of a container can be registered.\n"
"\n"
: "",
(u64Cmd & USAGE_LISTPARTITIONS) ?
" listpartitions -rawdisk <diskname>\n"
" Lists all partitions on <diskname>.\n"
"\n"
: "",
(u64Cmd & USAGE_CREATERAWVMDK) ?
" createrawvmdk -filename <filename> -rawdisk <diskname>\n"
" [-partitions <list of partition numbers> [-mbr <filename>] ]\n"
" [-register] [-relative]\n"
" Creates a new VMDK image which gives access to an entite host disk (if\n"
" the parameter -partitions is not specified) or some partitions of a\n"
" host disk. If access to individual partitions is granted, then the\n"
" parameter -mbr can be used to specify an alternative MBR to be used\n"
" (the partitioning information in the MBR file is ignored).\n"
" \\\\.\\PhysicalDrive0).\n"
" On Linux host the parameter -relative causes a VMDK file to be created\n"
" which refers to individual partitions instead to the entire disk.\n"
" Optionally the created image can be immediately registered.\n"
" The necessary partition numbers can be queried with\n"
" VBoxManage internalcommands listpartitions\n"
"\n"
: "",
(u64Cmd & USAGE_RENAMEVMDK) ?
" renamevmdk -from <filename> -to <filename>\n"
" Renames an existing VMDK image, including the base file and all its extents.\n"
"\n"
: "",
(u64Cmd & USAGE_CONVERTTORAW) ?
" converttoraw [-format <fileformat>] <filename> <outputfile>"
#ifdef ENABLE_CONVERT_RAW_TO_STDOUT
"|stdout"
#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
"\n"
" Convert image to raw, writing to file"
#ifdef ENABLE_CONVERT_RAW_TO_STDOUT
" or stdout"
#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
".\n"
"\n"
: "",
(u64Cmd & USAGE_CONVERTHD) ?
" converthd [-srcformat VDI|VMDK|VHD|RAW]\n"
" [-dstformat VDI|VMDK|VHD|RAW]\n"
" <inputfile> <outputfile>\n"
" converts hard disk images between formats\n"
"\n"
: "",
#ifdef RT_OS_WINDOWS
(u64Cmd & USAGE_MODINSTALL) ?
" modinstall\n"
" Installs the neccessary driver for the host OS\n"
"\n"
: "",
(u64Cmd & USAGE_MODUNINSTALL) ?
" moduninstall\n"
" Deinstalls the driver\n"
"\n"
: ""
#else
"",
""
#endif
);
}
/** @todo this is no longer necessary, we can enumerate extra data */
/**
* Finds a new unique key name.
*
* I don't think this is 100% race condition proof, but we assumes
* the user is not trying to push this point.
*
* @returns Result from the insert.
* @param pMachine The Machine object.
* @param pszKeyBase The base key.
* @param rKey Reference to the string object in which we will return the key.
*/
{
return hrc;
/* if there are no keys, it's simple. */
{
rKey = "1";
}
/* find a unique number - brute force rulez. */
for (unsigned i = 1; i < 1000000; i++)
{
char szKey[32];
while (psz)
{
)
break;
}
if (!psz)
{
}
}
return E_FAIL;
}
#if 0
/**
* Remove a key.
*
* I don't think this isn't 100% race condition proof, but we assumes
* the user is not trying to push this point.
*
* @returns Result from the insert.
* @param pMachine The machine object.
* @param pszKeyBase The base key.
* @param pszKey The key to remove.
*/
{
return hrc;
/* if there are no keys, it's simple. */
return S_OK;
char *pszKeys;
if (RT_SUCCESS(rc))
{
/* locate it */
while (psz)
{
)
break;
}
if (psz)
{
/* remove it */
if (*pszNext)
else
*psz = '\0';
/* update */
}
return hrc;
}
else
RTPrintf("error: failed to delete key '%s' from '%s', string conversion error %Rrc!\n",
return E_FAIL;
}
#endif
/**
* Sets a key value, does necessary error bitching.
*
* @returns COM status code.
* @param pMachine The Machine object.
* @param pszKeyBase The key base.
* @param pszKey The key.
* @param pszAttribute The attribute name.
* @param pszValue The string value.
*/
static HRESULT SetString(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, const char *pszValue)
{
HRESULT hrc = pMachine->SetExtraData(Bstr(Utf8StrFmt("%s/%s/%s", pszKeyBase, pszKey, pszAttribute)), Bstr(pszValue));
RTPrintf("error: Failed to set '%s/%s/%s' to '%s'! hrc=%#x\n",
return hrc;
}
/**
* Sets a key value, does necessary error bitching.
*
* @returns COM status code.
* @param pMachine The Machine object.
* @param pszKeyBase The key base.
* @param pszKey The key.
* @param pszAttribute The attribute name.
* @param u64Value The value.
*/
static HRESULT SetUInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, uint64_t u64Value)
{
char szValue[64];
}
/**
* Sets a key value, does necessary error bitching.
*
* @returns COM status code.
* @param pMachine The Machine object.
* @param pszKeyBase The key base.
* @param pszKey The key.
* @param pszAttribute The attribute name.
* @param i64Value The value.
*/
static HRESULT SetInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, int64_t i64Value)
{
char szValue[64];
}
/**
* Identical to the 'loadsyms' command.
*/
static int CmdLoadSyms(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
/*
* Get the VM
*/
/* assume it's a UUID */
{
/* must be a name */
}
/*
* Parse the command.
*/
const char *pszFilename;
uint64_t ModuleAddress = ~0;
uint64_t ModuleSize = 0;
/* filename */
if (argc < 2)
return errorArgument("Missing the filename argument!\n");
/* offDelta */
if (argc >= 3)
{
if (RT_FAILURE(rc))
}
/* pszModule */
if (argc >= 4)
/* ModuleAddress */
if (argc >= 5)
{
if (RT_FAILURE(rc))
}
/* ModuleSize */
if (argc >= 6)
{
if (RT_FAILURE(rc))
}
/*
* Add extra data.
*/
}
static DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
{
RTPrintf("ERROR: ");
RTPrintf("\n");
}
static int handleSetHDUUID(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
/* we need exactly one parameter: the image file */
if (argc != 1)
{
}
/* generate a new UUID */
/* just try it */
if (RT_FAILURE(rc))
{
return 1;
}
if (RT_FAILURE(rc))
{
return 1;
}
/* Open the image */
if (RT_FAILURE(rc))
{
return 1;
}
if (RT_FAILURE(rc))
else
return RT_FAILURE(rc);
}
{
int rc;
pPart->cPartitions = 0;
if (RT_FAILURE(rc))
return rc;
return VERR_INVALID_PARAMETER;
unsigned uExtended = (unsigned)-1;
for (unsigned i = 0; i < 4; i++)
{
if (p[4] == 0)
continue;
pCP->cPartDataSectors = 0;
if (PARTTYPE_IS_EXTENDED(p[4]))
{
if (uExtended == (unsigned)-1)
else
{
RTPrintf("More than one extended partition. Aborting\n");
return VERR_INVALID_PARAMETER;
}
}
}
if (uExtended != (unsigned)-1)
{
unsigned uIndex = 5;
if (!uStart)
{
RTPrintf("Inconsistency for logical partition start. Aborting\n");
return VERR_INVALID_PARAMETER;
}
do
{
if (RT_FAILURE(rc))
return rc;
{
RTPrintf("Logical partition without magic. Aborting\n");
return VERR_INVALID_PARAMETER;
}
if (p[4] == 0)
{
RTPrintf("Logical partition with type 0 encountered. Aborting\n");
return VERR_INVALID_PARAMETER;
}
if (!uStartOffset)
{
RTPrintf("Invalid partition start offset. Aborting\n");
return VERR_INVALID_PARAMETER;
}
/* Fill out partitioning location info for EBR. */
p += 16;
if (p[4] == 0)
uExtended = (unsigned)-1;
else if (PARTTYPE_IS_EXTENDED(p[4]))
{
}
else
{
RTPrintf("Logical partition chain broken. Aborting\n");
return VERR_INVALID_PARAMETER;
}
} while (uExtended != (unsigned)-1);
}
/* Sort partitions in ascending order of start sector, plus a trivial
* bit of consistency checking. */
{
unsigned uMinIdx = i;
{
{
uMinIdx = j;
}
{
RTPrintf("Two partitions start at the same place. Aborting\n");
return VERR_INVALID_PARAMETER;
{
RTPrintf("Partition starts at sector 0. Aborting\n");
return VERR_INVALID_PARAMETER;
}
}
if (uMinIdx != i)
{
/* Swap entries at index i and uMinIdx. */
}
}
/* Now do a lot of consistency checking. */
{
{
{
RTPrintf("Overlapping partition description areas. Aborting\n");
return VERR_INVALID_PARAMETER;
}
}
{
RTPrintf("Overlapping partitions. Aborting\n");
return VERR_INVALID_PARAMETER;
}
}
/* Fill out partitioning location info for MBR. */
return VINF_SUCCESS;
}
static int CmdListPartitions(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
/* let's have a closer look at the arguments */
for (int i = 0; i < argc; i++)
{
{
if (argc <= i + 1)
{
}
i++;
}
else
{
}
}
if (RT_FAILURE(vrc))
{
return vrc;
}
if (RT_FAILURE(vrc))
return vrc;
RTPrintf("Number Type StartCHS EndCHS Size (MiB) Start (Sect)\n");
for (unsigned i = 0; i < partitions.cPartitions; i++)
{
/* Suppress printing the extended partition. Otherwise people
* might add it to the list of partitions for raw partition
* access (which is not good). */
continue;
RTPrintf("%-7u %#04x %-4u/%-3u/%-2u %-4u/%-3u/%-2u %10llu %10llu\n",
}
return 0;
}
static int CmdCreateRawVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
const char *pszMBRFilename = NULL;
const char *pszPartitions = NULL;
bool fRegister = false;
bool fRelative = false;
uint32_t uPartitions = 0;
/* let's have a closer look at the arguments */
for (int i = 0; i < argc; i++)
{
{
if (argc <= i + 1)
{
}
i++;
}
{
if (argc <= i + 1)
{
}
i++;
pszMBRFilename = argv[i];
}
{
if (argc <= i + 1)
{
}
i++;
}
{
if (argc <= i + 1)
{
}
i++;
pszPartitions = argv[i];
}
{
fRegister = true;
}
#ifdef RT_OS_LINUX
{
fRelative = true;
}
#endif /* RT_OS_LINUX */
else
{
}
}
if (!pszPartitions && pszMBRFilename)
return errorSyntax(USAGE_CREATERAWVMDK, "The parameter -mbr is only valid when the parameter -partitions is also present");
if (RT_FAILURE(vrc))
{
goto out;
}
#ifdef RT_OS_WINDOWS
/* Windows NT has no IOCTL_DISK_GET_LENGTH_INFORMATION ioctl. This was
* added to Windows XP, so we have to use the available info from DriveGeo.
* Note that we cannot simply use IOCTL_DISK_GET_DRIVE_GEOMETRY as it
* yields a slightly different result than IOCTL_DISK_GET_LENGTH_INFO.
* We call IOCTL_DISK_GET_DRIVE_GEOMETRY first as we need to check the media
* type anyway, and if IOCTL_DISK_GET_LENGTH_INFORMATION is supported
* we will later override cbSize.
*/
{
{
}
else
{
goto out;
}
{
/* IOCTL_DISK_GET_LENGTH_INFO is supported -- override cbSize. */
}
}
else
{
goto out;
}
#elif defined(RT_OS_LINUX)
{
#ifdef BLKGETSIZE64
/* BLKGETSIZE64 is broken up to 2.4.17 and in many 2.5.x. In 2.6.0
* it works without problems. */
{
}
#endif /* BLKGETSIZE64 */
if (!cbSize)
{
long cBlocks;
else
{
goto out;
}
}
}
else
{
goto out;
}
#elif defined(RT_OS_DARWIN)
{
{
else
{
goto out;
}
}
else
{
goto out;
}
}
else
{
goto out;
}
#elif defined(RT_OS_SOLARIS)
{
else
{
goto out;
}
}
else
{
goto out;
}
#else /* all unrecognized OSes */
/* Hopefully this works on all other hosts. If it doesn't, it'll just fail
* creating the VMDK, so no real harm done. */
if (RT_FAILURE(vrc))
{
goto out;
}
#endif
/* Check whether cbSize is actually sensible. */
{
goto out;
}
if (!pszPartitions)
{
RawDescriptor.fRawDisk = true;
}
else
{
RawDescriptor.fRawDisk = false;
RawDescriptor.cPartitions = 0;
const char *p = pszPartitions;
char *pszNext;
while (*p != '\0')
{
if (RT_FAILURE(vrc))
{
RTPrintf("Incorrect value in partitions parameter\n");
goto out;
}
p = pszNext;
if (*p == ',')
p++;
else if (*p != '\0')
{
RTPrintf("Incorrect separator in partitions parameter\n");
goto out;
}
}
if (RT_FAILURE(vrc))
{
goto out;
}
for (unsigned i = 0; i < partitions.cPartitions; i++)
{
{
/* Some ignorant user specified an extended partition.
* Bad idea, as this would trigger an overlapping
* partitions error later during VMDK creation. So warn
* here and ignore what the user requested. */
RTPrintf("Warning: it is not possible (and necessary) to explicitly give access to the\n"
" extended partition %u. If required, enable access to all logical\n"
}
}
RawDescriptor.pPartitions = (PVBOXHDDRAWPART)RTMemAllocZ(partitions.cPartitions * sizeof(VBOXHDDRAWPART));
if (!RawDescriptor.pPartitions)
{
goto out;
}
for (unsigned i = 0; i < partitions.cPartitions; i++)
{
{
if (fRelative)
{
#ifdef RT_OS_LINUX
/* Refer to the correct partition and use offset 0. */
char *pszRawName;
if (RT_FAILURE(vrc))
{
RTPrintf("Error creating reference to individual partition %u, rc=%Rrc\n",
goto out;
}
#else
/** @todo not implemented yet for Windows host. Treat just
* like not specified (this code is actually never reached). */
#endif
}
else
{
/* This is the "everything refers to the base raw device"
* variant. This requires opening the base device in RW
* mode even for creation. */
}
}
else
{
/* Suppress access to this partition. */
/* This is used in the plausibility check in the creation
* code. In theory it's a dummy, but I don't want to make
* the VMDK creatiion any more complicated than what it needs
* to be. */
}
{
/* Suppress exporting the actual extended partition. Only
* logical partitions should be processed. However completely
* ignoring it leads to leaving out the MBR data. */
}
else
{
if (!pPartData)
{
goto out;
}
vrc = RTFileReadAt(RawFile, partitions.aPartitions[i].uPartDataStart * 512, pPartData, (size_t)RawDescriptor.pPartitions[i].cbPartitionData, NULL);
if (RT_FAILURE(vrc))
{
goto out;
}
/* Splice in the replacement MBR code if specified. */
&& pszMBRFilename)
{
if (RT_FAILURE(vrc))
{
goto out;
}
if (RT_FAILURE(vrc))
{
goto out;
}
}
}
}
}
if (RT_FAILURE(vrc))
{
goto out;
}
LCHS.cCylinders = 0;
VD_VMDK_IMAGE_FLAGS_RAWDISK, (char *)&RawDescriptor,
if (RT_FAILURE(vrc))
{
goto out;
}
/* Clean up allocated memory etc. */
if (pszPartitions)
{
for (unsigned i = 0; i < partitions.cPartitions; i++)
{
{
if (fRelative)
{
#ifdef RT_OS_LINUX
/* Free memory allocated above. */
#endif /* RT_OS_LINUX */
}
}
}
}
if (fRegister)
{
}
out:
RTPrintf("The raw disk vmdk file was not created\n");
}
static int CmdRenameVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
/* Parse the arguments. */
for (int i = 0; i < argc; i++)
{
{
if (argc <= i + 1)
{
}
i++;
}
{
if (argc <= i + 1)
{
}
i++;
}
else
{
}
}
if (RT_FAILURE(vrc))
{
return vrc;
}
else
{
if (RT_FAILURE(vrc))
{
}
else
{
if (RT_FAILURE(vrc))
{
}
}
}
return vrc;
}
static int CmdConvertToRaw(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
bool fWriteToStdOut = false;
/* Parse the arguments. */
for (int i = 0; i < argc; i++)
{
{
if (argc <= i + 1)
{
}
i++;
}
{
}
{
#ifdef ENABLE_CONVERT_RAW_TO_STDOUT
fWriteToStdOut = true;
#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
}
else
{
}
}
if (RT_FAILURE(vrc))
{
return 1;
}
/* Open raw output file. */
vrc = VINF_SUCCESS;
if (fWriteToStdOut)
outFile = 1;
else
vrc = RTFileOpen(&outFile, Utf8Str(dst).raw(), RTFILE_O_OPEN | RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_ALL);
if (RT_FAILURE(vrc))
{
return 1;
}
{
if (RT_FAILURE(vrc))
{
if (!fWriteToStdOut)
{
}
return 1;
}
}
if (RT_FAILURE(vrc))
{
if (!fWriteToStdOut)
{
}
return 1;
}
#define RAW_BUFFER_SIZE _128K
if (pvBuf)
{
RTPrintf("Converting image \"%s\" with size %RU64 bytes (%RU64MB) to raw...\n", Utf8Str(src).raw(), cbSize, (cbSize + _1M - 1) / _1M);
{
if (RT_FAILURE(vrc))
break;
if (RT_FAILURE(vrc))
break;
}
if (RT_FAILURE(vrc))
{
if (!fWriteToStdOut)
{
}
return 1;
}
}
else
{
if (!fWriteToStdOut)
{
}
return 1;
}
if (!fWriteToStdOut)
return 0;
}
static int CmdConvertHardDisk(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
int vrc;
/* Parse the arguments. */
for (int i = 0; i < argc; i++)
{
{
if (argc <= i + 1)
{
}
i++;
}
{
if (argc <= i + 1)
{
}
i++;
}
{
}
{
}
else
{
}
}
do
{
/* Try to determine input image format */
{
if (RT_FAILURE(vrc))
{
break;
}
}
if (RT_FAILURE(vrc))
{
break;
}
/* Open the input image */
if (RT_FAILURE(vrc))
{
break;
}
/* Output format defaults to VDI */
dstformat = "VDI";
if (RT_FAILURE(vrc))
{
break;
}
RTPrintf("Converting image \"%s\" with size %RU64 bytes (%RU64MB)...\n", Utf8Str(src).raw(), cbSize, (cbSize + _1M - 1) / _1M);
/* Create the output image */
if (RT_FAILURE(vrc))
{
break;
}
}
while (0);
if (pDstDisk)
if (pSrcDisk)
}
/**
* Unloads the neccessary driver.
*
* @returns VBox status code
*/
int CmdModUninstall(void)
{
int rc;
rc = SUPUninstall();
if (RT_SUCCESS(rc))
return 0;
if (rc == VERR_NOT_IMPLEMENTED)
return 0;
return E_FAIL;
}
/**
* Loads the neccessary driver.
*
* @returns VBox status code
*/
int CmdModInstall(void)
{
int rc;
rc = SUPInstall();
if (RT_SUCCESS(rc))
return 0;
if (rc == VERR_NOT_IMPLEMENTED)
return 0;
return E_FAIL;
}
/**
* Wrapper for handling internal commands
*/
int handleInternalCommands(HandlerArg *a)
{
g_fInternalMode = true;
/* at least a command is required */
if (a->argc < 1)
/*
* The 'string switch' on command name.
*/
//if (!strcmp(pszCmd, "unloadsyms"))
// return CmdUnloadSyms(argc - 1 , &a->argv[1]);
return CmdModInstall();
return CmdModUninstall();
/* default: */
}