VBoxManageDisk.cpp revision 0bbcd928b2a3d73607f224687249c3cec733aee3
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * VBoxManage - The disk delated commands.
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2008 Sun Microsystems, Inc.
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * available from http://www.virtualbox.org. This file is free software;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * you can redistribute it and/or modify it under the terms of the GNU
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * General Public License (GPL) as published by the Free Software
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync * additional information or have any questions.
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync/*******************************************************************************
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync* Header Files *
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync*******************************************************************************/
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsyncusing namespace com;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync///////////////////////////////////////////////////////////////////////////////
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsyncstatic DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync RTPrintf("Error code %Rrc at %s(%u) in function %s\n", rc, RT_SRC_POS_ARGS);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsyncstatic int parseDiskVariant(const char *psz, HardDiskVariant_T *pDiskVariant)
10d739d22a5d5a13803f7e34de34de010099270cvboxsync // Parsing is intentionally inconsistent: "standard" resets the
10d739d22a5d5a13803f7e34de34de010099270cvboxsync // variant, whereas the other flags are cumulative.
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync DiskVariant |= HardDiskVariant_VmdkStreamOptimized;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsyncstatic int parseDiskType(const char *psz, HardDiskType_T *pDiskType)
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync/** @todo move this into getopt, as getting bool values is generic */
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsyncstatic const RTGETOPTDEF g_aCreateHardDiskOptions[] =
be196d173cf52fa33016912e4745dbe1170ac53avboxsync { "-filename", 'f', RTGETOPT_REQ_STRING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "-size", 's', RTGETOPT_REQ_UINT64 }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "-format", 'o', RTGETOPT_REQ_STRING }, // deprecated
be196d173cf52fa33016912e4745dbe1170ac53avboxsync { "-static", 'F', RTGETOPT_REQ_NOTHING }, // deprecated
be196d173cf52fa33016912e4745dbe1170ac53avboxsync { "-variant", 'm', RTGETOPT_REQ_STRING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "-type", 't', RTGETOPT_REQ_STRING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "-comment", 'c', RTGETOPT_REQ_STRING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "-remember", 'r', RTGETOPT_REQ_NOTHING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "--register", 'r', RTGETOPT_REQ_NOTHING }, // deprecated (inofficial)
be196d173cf52fa33016912e4745dbe1170ac53avboxsync { "-register", 'r', RTGETOPT_REQ_NOTHING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync HardDiskVariant_T DiskVariant = HardDiskVariant_Standard;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync bool fRemember = false;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync // start at 0 because main() has hacked both the argc and argv given to us
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync RTGetOptInit(&GetState, a->argc, a->argv, g_aCreateHardDiskOptions, RT_ELEMENTS(g_aCreateHardDiskOptions), 0, 0 /* fFlags */);
88acfa6629a7976c0583c1712d2b5b22a87a5121vboxsync vrc = parseDiskVariant(ValueUnion.psz, &DiskVariant);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorArgument("Invalid hard disk variant '%s'", ValueUnion.psz);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync || (DiskType != HardDiskType_Normal && DiskType != HardDiskType_Writethrough))
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorArgument("Invalid hard disk type '%s'", ValueUnion.psz);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_CREATEHD, "Invalid parameter '%s'", ValueUnion.psz);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_CREATEHD, "Invalid option -%c", c);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_CREATEHD, "Invalid option case %i", c);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_CREATEHD, "unknown option: %s\n", ValueUnion.psz);
be196d173cf52fa33016912e4745dbe1170ac53avboxsync return errorSyntax(USAGE_CREATEHD, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_CREATEHD, "error: %Rrs", c);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync /* check the outcome */
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_CREATEHD, "Parameters --filename and --size are required");
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync CHECK_ERROR(a->virtualBox, CreateHardDisk(format, filename, hardDisk.asOutParam()));
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync /* we will close the hard disk after the storage has been successfully
be196d173cf52fa33016912e4745dbe1170ac53avboxsync * created unless fRemember is set */
be196d173cf52fa33016912e4745dbe1170ac53avboxsync bool doClose = false;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync CHECK_ERROR(hardDisk,COMSETTER(Description)(comment));
10d739d22a5d5a13803f7e34de34de010099270cvboxsync CHECK_ERROR(hardDisk, CreateBaseStorage(sizeMB, DiskVariant, progress.asOutParam()));
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync RTPrintf("Error: failed to create hard disk. Error message: %lS\n", info.getText().raw());
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync RTPrintf("Error: failed to create hard disk. No error message available!\n");
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync CHECK_ERROR(hardDisk, COMGETTER(Id)(uuid.asOutParam()));
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync CHECK_ERROR(hardDisk, COMSETTER(Type)(HardDiskType_Writethrough));
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync RTPrintf("Disk image created. UUID: %s\n", Utf8Str(uuid).raw());
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync#if 0 /* disabled until disk shrinking is implemented based on VBoxHDD */
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsyncstatic DECLCALLBACK(int) hardDiskProgressCallback(PVM pVM, unsigned uPercent, void *pvUser)
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsyncstatic const RTGETOPTDEF g_aModifyHardDiskOptions[] =
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "-type", 't', RTGETOPT_REQ_STRING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "settype", 't', RTGETOPT_REQ_STRING }, // deprecated
be196d173cf52fa33016912e4745dbe1170ac53avboxsync { "-autoreset", 'z', RTGETOPT_REQ_STRING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "autoreset", 'z', RTGETOPT_REQ_STRING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "-compact", 'c', RTGETOPT_REQ_NOTHING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync { "compact", 'c', RTGETOPT_REQ_NOTHING }, // deprecated
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync bool AutoReset = false;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync bool fModifyDiskType = false, fModifyAutoReset = false, fModifyCompact = false;;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync // start at 0 because main() has hacked both the argc and argv given to us
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync RTGetOptInit(&GetState, a->argc, a->argv, g_aModifyHardDiskOptions, RT_ELEMENTS(g_aModifyHardDiskOptions), 0, 0 /* fFlags */);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorArgument("Invalid hard disk type '%s'", ValueUnion.psz);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorArgument("Invalid autoreset parameter '%s'", ValueUnion.psz);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_CREATEHD, "Invalid parameter '%s'", ValueUnion.psz);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_MODIFYHD, "Invalid option -%c", c);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_MODIFYHD, "Invalid option case %i", c);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_MODIFYHD, "unknown option: %s\n", ValueUnion.psz);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_MODIFYHD, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_MODIFYHD, "error: %Rrs", c);
10d739d22a5d5a13803f7e34de34de010099270cvboxsync return errorSyntax(USAGE_MODIFYHD, "Disk name or UUID required");
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync if (!fModifyDiskType && !fModifyAutoReset && !fModifyCompact)
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorSyntax(USAGE_MODIFYHD, "No operation specified");
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync /* first guess is that it's a UUID */
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync rc = a->virtualBox->GetHardDisk(uuid.toUtf16(), hardDisk.asOutParam());
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync /* no? then it must be a filename */
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync CHECK_ERROR(a->virtualBox, FindHardDisk(Bstr(FilenameOrUuid), hardDisk.asOutParam()));
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync /* hard disk must be registered */
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync return errorArgument("Hard disk image not registered");
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync CHECK_ERROR(hardDisk, COMSETTER(AutoReset)(AutoReset));
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync bool unknown = false;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync /* the hard disk image might not be registered */
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync rc = a->virtualBox->OpenHardDisk(Bstr(FilenameOrUuid), AccessMode_ReadWrite, hardDisk.asOutParam());
f2ffcc39cf4469a87fea28faf36a3918493bf7c1vboxsync int vrc = RTPathAbs(FilenameOrUuid, szFilenameAbs, sizeof(szFilenameAbs));
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync RTPrintf("Cannot convert filename \"%s\" to absolute path\n", FilenameOrUuid);
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync CHECK_ERROR(a->virtualBox, OpenHardDisk(Bstr(szFilenameAbs), AccessMode_ReadWrite, hardDisk.asOutParam()));
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync CHECK_ERROR(a->virtualBox, OpenHardDisk(Bstr(FilenameOrUuid), AccessMode_ReadWrite, hardDisk.asOutParam()));
b7c1d1e088392fc0698ab8732e2c24f54d92dcbdvboxsync CHECK_ERROR(hardDisk, Compact(progress.asOutParam()));
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync RTPrintf("Error: Compact hard disk operation is not implemented!\n");
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync RTPrintf("The functionality will be restored later.\n");
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync RTPrintf("Error: Compact hard disk operation for this format is not implemented yet!\n");
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsyncstatic const RTGETOPTDEF g_aCloneHardDiskOptions[] =
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync HardDiskVariant_T DiskVariant = HardDiskVariant_Standard;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync bool fRemember = false;
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync // start at 0 because main() has hacked both the argc and argv given to us
922f46b2f42ccb05e3c9cba34bbd1d2c19e04120vboxsync RTGetOptInit(&GetState, a->argc, a->argv, g_aCloneHardDiskOptions, RT_ELEMENTS(g_aCloneHardDiskOptions), 0, 0 /* fFlags */);
fRemember = true;
case VINF_GETOPT_NOT_OPTION:
if (RT_C_IS_GRAPH(c))
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
bool unknown = false;
CHECK_ERROR(a->virtualBox, OpenHardDisk(Bstr(szFilenameAbs), AccessMode_ReadWrite, srcDisk.asOutParam()));
unknown = true;
if (unknown)
bool fReadFromStdIn = false;
RTGetOptInit(&GetState, argc, argv, g_aConvertFromRawHardDiskOptions, RT_ELEMENTS(g_aConvertFromRawHardDiskOptions), 0, 0 /* fFlags */);
case VINF_GETOPT_NOT_OPTION:
if (!srcfilename)
else if (!dstfilename)
if (RT_C_IS_PRINT(c))
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
if (fReadFromStdIn)
File = 0;
goto out;
if (fReadFromStdIn)
goto out;
RTPrintf("Creating %s image with size %RU64 bytes (%RU64MB)...\n", (uImageFlags & VD_IMAGE_FLAGS_FIXED) ? "fixed" : "dynamic", cbFile, (cbFile + _1M - 1) / _1M);
goto out;
goto out;
if (!pvBuf)
goto out;
offFile = 0;
cbRead = 0;
goto out;
out:
if (pvBuf)
if (pDisk)
int vrc;
bool fIntNet = false;
RTGetOptInit(&GetState, a->argc, a->argv, g_aAddiSCSIDiskOptions, RT_ELEMENTS(g_aAddiSCSIDiskOptions), 0, 0 /* fFlags */);
fIntNet = true;
case VINF_GETOPT_NOT_OPTION:
if (RT_C_IS_PRINT(c))
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
if (fIntNet)
RTGetOptInit(&GetState, a->argc, a->argv, g_aShowHardDiskInfoOptions, RT_ELEMENTS(g_aShowHardDiskInfoOptions), 0, 0 /* fFlags */);
case VINF_GETOPT_NOT_OPTION:
if (!FilenameOrUuid)
if (RT_C_IS_PRINT(c))
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
if (!FilenameOrUuid)
bool unknown = false;
rc = a->virtualBox->OpenHardDisk(Bstr(FilenameOrUuid), AccessMode_ReadWrite, hardDisk.asOutParam());
CHECK_ERROR(a->virtualBox, OpenHardDisk(Bstr(szFilenameAbs), AccessMode_ReadWrite, hardDisk.asOutParam()));
CHECK_ERROR(a->virtualBox, OpenHardDisk(Bstr(FilenameOrUuid), AccessMode_ReadWrite, hardDisk.asOutParam()));
unknown = true;
if (description)
switch (type)
case HardDiskType_Normal:
case HardDiskType_Immutable:
if (!unknown)
if (unknown)
int vrc;
bool fDiskType = false;
RTGetOptInit(&GetState, a->argc, a->argv, g_aOpenMediumOptions, RT_ELEMENTS(g_aOpenMediumOptions), 0, 0 /* fFlags */);
fDiskType = true;
case VINF_GETOPT_NOT_OPTION:
if (!Filename)
if (RT_C_IS_PRINT(c))
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
if (!Filename)
CHECK_ERROR(a->virtualBox, OpenHardDisk(Bstr(szFilenameAbs), AccessMode_ReadWrite, hardDisk.asOutParam()));
CHECK_ERROR(a->virtualBox, OpenHardDisk(Bstr(Filename), AccessMode_ReadWrite, hardDisk.asOutParam()));
if (fDiskType)
if (fDiskType)
RTGetOptInit(&GetState, a->argc, a->argv, g_aCloseMediumOptions, RT_ELEMENTS(g_aCloseMediumOptions), 0, 0 /* fFlags */);
case VINF_GETOPT_NOT_OPTION:
if (!FilenameOrUuid)
if (RT_C_IS_PRINT(c))
else if (c == VERR_GETOPT_UNKNOWN_OPTION)
if (!FilenameOrUuid)
if (!hardDisk)
if (!dvdImage)
if (!floppyImage)