vbox-img.cpp revision 1189c7dde5d7f6c26f338ced3d40fc830b822e68
/* $Id$ */
/** @file
* Standalone image manipulation tool
*/
/*
* Copyright (C) 2010 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
#include <iprt/buildconfig.h>
const char *g_pszProgName = "";
static void showUsage(void)
{
"Usage: %s\n"
" setuuid --filename <filename>\n"
" [--format VDI|VMDK|VHD|...]\n"
" [--uuid <uuid>]\n"
" [--parentuuid <uuid>]\n"
" [--zeroparentuuid]\n"
"\n"
" convert --srcfilename <filename>\n"
" --dstfilename <filename>\n"
" [--srcformat VDI|VMDK|VHD|RAW|..]\n"
" [--dstformat VDI|VMDK|VHD|RAW|..]\n"
" [--variant Standard,Fixed,Split2G,Stream,ESX]\n"
"\n"
" info --filename <filename>\n"
"\n"
" compact --filename <filename>\n",
}
/** command handler argument */
struct HandlerArg
{
int argc;
char **argv;
};
{
}
{
return VINF_SUCCESS;
}
/**
* Print a usage synopsis and the syntax error message.
*/
int errorSyntax(const char *pszFormat, ...)
{
RTPrintf("\n"
showUsage();
return 1;
}
int errorRuntime(const char *pszFormat, ...)
{
RTPrintf("\n"
return 1;
}
int handleSetUUID(HandlerArg *a)
{
const char *pszFilename = NULL;
bool fSetImageUuid = false;
bool fSetParentUuid = false;
int rc;
/* Parse the command line. */
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
{
switch (ch)
{
case 'f': // --filename
break;
case 'o': // --format
break;
case 'u': // --uuid
fSetImageUuid = true;
break;
case 'p': // --parentuuid
fSetParentUuid = true;
break;
case 'P': // --zeroparentuuid
fSetParentUuid = true;
break;
default:
showUsage();
return ch;
}
}
/* Check for mandatory parameters. */
if (!pszFilename)
return errorSyntax("Mandatory --filename option missing\n");
/* Check for consistency of optional parameters. */
return errorSyntax("Invalid parameter to --uuid option\n");
/* Autodetect image format. */
if (!pszFormat)
{
/* Don't pass error interface, as that would triggers error messages
* because some backends fail to open the image. */
if (RT_FAILURE(rc))
}
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
return errorRuntime("Cannot open the virtual disk image \"%s\": %Rrc\n",
pszFilename, rc);
if (RT_FAILURE(rc))
return errorRuntime("Cannot get UUID of virtual disk image \"%s\": %Rrc\n",
pszFilename, rc);
if (RT_FAILURE(rc))
return errorRuntime("Cannot get parent UUID of virtual disk image \"%s\": %Rrc\n",
pszFilename, rc);
if (fSetImageUuid)
{
if (RT_FAILURE(rc))
return errorRuntime("Cannot set UUID of virtual disk image \"%s\": %Rrc\n",
pszFilename, rc);
}
if (fSetParentUuid)
{
if (RT_FAILURE(rc))
return errorRuntime("Cannot set parent UUID of virtual disk image \"%s\": %Rrc\n",
pszFilename, rc);
}
if (RT_FAILURE(rc))
if (pszFormat)
{
}
return 0;
}
int handleConvert(HandlerArg *a)
{
const char *pszSrcFilename = NULL;
const char *pszDstFilename = NULL;
char *pszSrcFormat = NULL;
const char *pszDstFormat = NULL;
const char *pszVariant = NULL;
unsigned uImageFlags = VD_IMAGE_FLAGS_NONE;
int rc = VINF_SUCCESS;
/* Parse the command line. */
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
{
switch (ch)
{
case 'i': // --srcfilename
break;
case 'o': // --dstfilename
break;
case 's': // --srcformat
break;
case 'd': // --dstformat
break;
case 'v': // --variant
break;
default:
showUsage();
return ch;
}
}
/* Check for mandatory parameters. */
if (!pszSrcFilename)
return errorSyntax("Mandatory --srcfilename option missing\n");
if (!pszDstFilename)
return errorSyntax("Mandatory --dstfilename option missing\n");
/* check the variant parameter */
if (pszVariant)
{
char *psz = (char*)pszVariant;
{
if (pszComma)
else
if (len > 0)
{
else
return errorSyntax("Invalid --variant option\n");
}
if (pszComma)
else
}
}
do
{
/* try to determine input format if not specified */
if (!pszSrcFormat)
{
if (RT_FAILURE(rc))
{
break;
}
}
if (RT_FAILURE(rc))
{
break;
}
if (RT_FAILURE(rc))
{
break;
}
/* output format defaults to VDI */
if (!pszDstFormat)
pszDstFormat = "VDI";
if (RT_FAILURE(rc))
{
break;
}
RTPrintf("Converting image \"%s\" with size %RU64 bytes (%RU64MB)...\n", pszSrcFilename, cbSize, (cbSize + _1M - 1) / _1M);
/* Create the output image */
if (RT_FAILURE(rc))
{
break;
}
}
while (0);
if (pDstDisk)
if (pSrcDisk)
}
int handleInfo(HandlerArg *a)
{
int rc = VINF_SUCCESS;
const char *pszFilename = NULL;
/* Parse the command line. */
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
{
switch (ch)
{
case 'f': // --filename
break;
default:
showUsage();
return ch;
}
}
/* Check for mandatory parameters. */
if (!pszFilename)
return errorSyntax("Mandatory --filename option missing\n");
/* just try it */
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
/* Open the image */
if (RT_FAILURE(rc))
return rc;
}
int handleCompact(HandlerArg *a)
{
int rc = VINF_SUCCESS;
const char *pszFilename = NULL;
/* Parse the command line. */
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
{
switch (ch)
{
case 'f': // --filename
break;
default:
showUsage();
return ch;
}
}
/* Check for mandatory parameters. */
if (!pszFilename)
return errorSyntax("Mandatory --filename option missing\n");
/* just try it */
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
/* Open the image */
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
return rc;
}
void showLogo()
{
VBOX_VERSION_STRING "\n"
"All rights reserved.\n"
"\n");
}
{
RTR3Init();
int rc;
bool fShowLogo = true;
int iCmd = 1;
int iCmdArg;
/* global options */
{
{
showLogo();
showUsage();
return 0;
}
{
/* Print version number, and do nothing else. */
return 0;
}
{
/* suppress the logo */
fShowLogo = false;
iCmd++;
}
else
{
break;
}
}
if (fShowLogo)
showLogo();
/* initialize the VD backend with dummy handlers */
if (RT_FAILURE(rc))
return errorSyntax("Initalizing backends failed! rc=%Rrc\n");
/*
* All registered command handlers
*/
static const struct
{
const char *command;
int (*handler)(HandlerArg *a);
} s_commandHandlers[] =
{
{ "setuuid", handleSetUUID },
{ "convert", handleConvert },
{ "info", handleInfo },
{ "compact", handleCompact },
};
int commandIndex;
{
{
break;
}
}
rc = VDShutdown();
if (RT_FAILURE(rc))
return rc;
}
/* dummy stub for RuntimeR3 */
#ifndef RT_OS_WINDOWS
RTDECL(bool) RTAssertShouldPanic(void)
{
return true;
}
#endif