VBoxInternalManage.cpp revision 23179f1443b03947d85eccc81cbc6b5153a4abf3
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * VBoxManage - The 'internalcommands' command.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * VBoxInternalManage used to be a second CLI for doing special tricks,
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * not intended for general usage, only for assisting VBox developers.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * It is now integrated into VBoxManage.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Copyright (C) 2006-2009 Sun Microsystems, Inc.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * available from http://www.virtualbox.org. This file is free software;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * General Public License (GPL) as published by the Free Software
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * additional information or have any questions.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync/*******************************************************************************
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync* Header Files *
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync*******************************************************************************/
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync/* Includes for the raw disk stuff. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#elif defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#endif /* RT_OS_LINUX */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#endif /* RT_OS_DARWIN */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#endif /* RT_OS_SOLARIS */
681bb0bf6e9ab5a590d8afcdf37634e2fc29f595vboxsyncusing namespace com;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync/** Macro for checking whether a partition is of extended type or not. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#define PARTTYPE_IS_EXTENDED(x) ((x) == 0x05 || (x) == 0x0f || (x) == 0x85)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync/* Maximum number of partitions we can deal with. Ridiculously large number,
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * but the memory consumption is rather low so who cares about never using
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * most entries. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** partition type */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** CHS/cylinder of the first sector */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** CHS/head of the first sector */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** CHS/head of the first sector */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** CHS/cylinder of the last sector */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** CHS/head of the last sector */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** CHS/sector of the last sector */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** start sector of this partition relative to the beginning of the hard
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * disk or relative to the beginning of the extended partition table */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** numer of sectors of the partition */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** start sector of this partition _table_ */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** numer of sectors of this partition _table_ */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync/** flag whether we're in internal mode */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Print the usage info.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Usage: VBoxManage internalcommands <command> [command arguments]\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync "Commands:\n"
dcc162af9a2a47325b89f457ed215957415d3d3avboxsync "%s%s%s%s%s%s%s%s%s"
dcc162af9a2a47325b89f457ed215957415d3d3avboxsync "WARNING: This is a development tool and shall only be used to analyse\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " problems. It is completely unsupported and will change in\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " incompatible ways without warning.\n",
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " loadsyms <vmname>|<uuid> <symfile> [delta] [module] [module address]\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " This will instruct DBGF to load the given symbolfile\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " during initialization.\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " unloadsyms <vmname>|<uuid> <symfile>\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " Removes <symfile> from the list of symbol files that\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " should be loaded during DBF initialization.\n"
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync " sethduuid <filepath>\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " Assigns a new UUID to the given image file. This way, multiple copies\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " of a container can be registered.\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " listpartitions -rawdisk <diskname>\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " Lists all partitions on <diskname>.\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " createrawvmdk -filename <filename> -rawdisk <diskname>\n"
681bb0bf6e9ab5a590d8afcdf37634e2fc29f595vboxsync " [-partitions <list of partition numbers> [-mbr <filename>] ]\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " [-register] [-relative]\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " Creates a new VMDK image which gives access to an entite host disk (if\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " the parameter -partitions is not specified) or some partitions of a\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " host disk. If access to individual partitions is granted, then the\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " parameter -mbr can be used to specify an alternative MBR to be used\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " (the partitioning information in the MBR file is ignored).\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " The diskname is on Linux e.g. /dev/sda, and on Windows e.g.\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " \\\\.\\PhysicalDrive0).\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " On Linux host the parameter -relative causes a VMDK file to be created\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " which refers to individual partitions instead to the entire disk.\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " Optionally the created image can be immediately registered.\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " The necessary partition numbers can be queried with\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " VBoxManage internalcommands listpartitions\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " renamevmdk -from <filename> -to <filename>\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " Renames an existing VMDK image, including the base file and all its extents.\n"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " converttoraw [-format <fileformat>] <filename> <outputfile>"
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
c9ffb32f7bfe2b467ba3ae35d640ac7b97e50a30vboxsync " Convert image to raw, writing to file"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " or stdout"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " converthd [-srcformat VDI|VMDK|VHD|RAW]\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " [-dstformat VDI|VMDK|VHD|RAW]\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " <inputfile> <outputfile>\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " converts hard disk images between formats\n"
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync " modinstall\n"
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync " Installs the neccessary driver for the host OS\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " moduninstall\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " Deinstalls the driver\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync/** @todo this is no longer necessary, we can enumerate extra data */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Finds a new unique key name.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * I don't think this is 100% race condition proof, but we assumes
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * the user is not trying to push this point.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @returns Result from the insert.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pMachine The Machine object.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszKeyBase The base key.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param rKey Reference to the string object in which we will return the key.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic HRESULT NewUniqueKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, Utf8Str &rKey)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* if there are no keys, it's simple. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr("1"));
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* find a unique number - brute force rulez. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync size_t cchKey = RTStrPrintf(szKey, sizeof(szKey), "%#x", i);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(NewKeysUtf8));
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error: Cannot find unique key for '%s'!\n", pszKeyBase);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Remove a key.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * I don't think this isn't 100% race condition proof, but we assumes
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * the user is not trying to push this point.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @returns Result from the insert.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pMachine The machine object.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszKeyBase The base key.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszKey The key to remove.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic HRESULT RemoveKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* if there are no keys, it's simple. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* locate it */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* remove it */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* update */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync hrc = pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(psz));
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("error: failed to delete key '%s' from '%s', string conversion error %Rrc!\n",
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Sets a key value, does necessary error bitching.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @returns COM status code.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pMachine The Machine object.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszKeyBase The key base.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszKey The key.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszAttribute The attribute name.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszValue The string value.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic HRESULT SetString(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, const char *pszValue)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync HRESULT hrc = pMachine->SetExtraData(Bstr(Utf8StrFmt("%s/%s/%s", pszKeyBase, pszKey, pszAttribute)), Bstr(pszValue));
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("error: Failed to set '%s/%s/%s' to '%s'! hrc=%#x\n",
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Sets a key value, does necessary error bitching.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @returns COM status code.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pMachine The Machine object.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszKeyBase The key base.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszKey The key.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszAttribute The attribute name.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param u64Value The value.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic HRESULT SetUInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, uint64_t u64Value)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTStrPrintf(szValue, sizeof(szValue), "%#RX64", u64Value);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Sets a key value, does necessary error bitching.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @returns COM status code.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pMachine The Machine object.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszKeyBase The key base.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszKey The key.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param pszAttribute The attribute name.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * @param i64Value The value.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic HRESULT SetInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, int64_t i64Value)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTStrPrintf(szValue, sizeof(szValue), "%RI64", i64Value);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Identical to the 'loadsyms' command.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic int CmdLoadSyms(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Get the VM
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* assume it's a UUID */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync rc = aVirtualBox->GetMachine(Bstr(argv[0]), machine.asOutParam());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* must be a name */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()), 1);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Parse the command.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* filename */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument("Missing the filename argument!\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* offDelta */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync int rc = RTStrToInt64Ex(argv[2], NULL, 0, &offDelta);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument(argv[0], "Failed to read delta '%s', rc=%Rrc\n", argv[2], rc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* pszModule */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* ModuleAddress */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync int rc = RTStrToUInt64Ex(argv[4], NULL, 0, &ModuleAddress);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument(argv[0], "Failed to read module address '%s', rc=%Rrc\n", argv[4], rc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* ModuleSize */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync int rc = RTStrToUInt64Ex(argv[5], NULL, 0, &ModuleSize);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument(argv[0], "Failed to read module size '%s', rc=%Rrc\n", argv[5], rc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Add extra data.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync HRESULT hrc = NewUniqueKey(machine, "VBoxInternal/DBGF/loadsyms", KeyStr);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Filename", pszFilename);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync hrc = SetInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Delta", offDelta);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Module", pszModule);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleAddress", ModuleAddress);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleSize", ModuleSize);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error code %Rrc at %s(%u) in function %s\n", rc, RT_SRC_POS_ARGS);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic int handleSetHDUUID(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* we need exactly one parameter: the image file */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_SETHDUUID, "Not enough parameters");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* generate a new UUID */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* just try it */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vdInterfaceErrorCallbacks.pfnError = handleVDError;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync rc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error while creating the virtual disk container: %Rrc\n", rc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Open the image */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync rc = VDOpen(pDisk, pszFormat, argv[0], VD_OPEN_FLAGS_NORMAL, NULL);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error while opening the image: %Rrc\n", rc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error while setting a new UUID: %Rrc\n", rc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("UUID changed to: %s\n", uuid.toString().raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic int partRead(RTFILE File, PHOSTPARTITIONS pPart)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync memset(pPart->aPartitions, '\0', sizeof(pPart->aPartitions));
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync rc = RTFileReadAt(File, 0, &aBuffer, sizeof(aBuffer), NULL);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (unsigned i = 0; i < 4; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if (p[4] == 0)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync PHOSTPARTITION pCP = &pPart->aPartitions[pPart->cPartitions++];
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync pCP->uStartCylinder = (uint32_t)p[3] + ((uint32_t)(p[2] & 0xc0) << 2);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync pCP->uEndCylinder = (uint32_t)p[7] + ((uint32_t)(p[6] & 0xc0) << 2);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync pCP->uStart = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync pCP->uSize = RT_MAKE_U32_FROM_U8(p[12], p[13], p[14], p[15]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync pCP->uPartDataStart = 0; /* will be filled out later properly. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("More than one extended partition. Aborting\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync uint64_t uStart = pPart->aPartitions[uExtended].uStart;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Inconsistency for logical partition start. Aborting\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync rc = RTFileReadAt(File, (uStart + uOffset) * 512, &aBuffer, sizeof(aBuffer), NULL);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Logical partition without magic. Aborting\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if (p[4] == 0)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Logical partition with type 0 encountered. Aborting\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync PHOSTPARTITION pCP = &pPart->aPartitions[pPart->cPartitions++];
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync pCP->uStartCylinder = (uint32_t)p[3] + ((uint32_t)(p[2] & 0xc0) << 2);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync pCP->uEndCylinder = (uint32_t)p[7] + ((uint32_t)(p[6] & 0xc0) << 2);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync uint32_t uStartOffset = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Invalid partition start offset. Aborting\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync pCP->uSize = RT_MAKE_U32_FROM_U8(p[12], p[13], p[14], p[15]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Fill out partitioning location info for EBR. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if (p[4] == 0)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync uOffset = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Logical partition chain broken. Aborting\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Sort partitions in ascending order of start sector, plus a trivial
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * bit of consistency checking. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (unsigned i = 0; i < pPart->cPartitions-1; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync unsigned uMinIdx = i;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (unsigned j = i + 1; j < pPart->cPartitions; j++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Two partitions start at the same place. Aborting\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Partition starts at sector 0. Aborting\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Swap entries at index i and uMinIdx. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync &pPart->aPartitions[uMinIdx], sizeof(HOSTPARTITION));
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync &pPart->aPartitions[pPart->cPartitions], sizeof(HOSTPARTITION));
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync /* Now do a lot of consistency checking. */
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync for (unsigned i = 0; i < pPart->cPartitions-1; i++)
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync if (pPart->aPartitions[i].uPartDataStart < uPrevEnd)
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync RTPrintf("Overlapping partition description areas. Aborting\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync uPrevEnd = pPart->aPartitions[i].uPartDataStart + pPart->aPartitions[i].cPartDataSectors;
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync if (!PARTTYPE_IS_EXTENDED(pPart->aPartitions[i].uType))
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync uPrevEnd = pPart->aPartitions[i].uStart + pPart->aPartitions[i].uSize;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Fill out partitioning location info for MBR. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync pPart->aPartitions[0].cPartDataSectors = pPart->aPartitions[0].uStart;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic int CmdListPartitions(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* let's have a closer look at the arguments */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (int i = 0; i < argc; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument("Missing argument to '%s'", argv[i]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_LISTPARTITIONS, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_LISTPARTITIONS, "Mandatory parameter -rawdisk missing");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync int vrc = RTFileOpen(&RawFile, rawdisk.raw(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error opening the raw disk: %Rrc\n", vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Number Type StartCHS EndCHS Size (MiB) Start (Sect)\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (unsigned i = 0; i < partitions.cPartitions; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Suppress printing the extended partition. Otherwise people
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * might add it to the list of partitions for raw partition
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync * access (which is not good). */
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync if (PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync RTPrintf("%-7u %#04x %-4u/%-3u/%-2u %-4u/%-3u/%-2u %10llu %10llu\n",
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic int CmdCreateRawVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync bool fRegister = false;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync bool fRelative = false;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* let's have a closer look at the arguments */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (int i = 0; i < argc; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument("Missing argument to '%s'", argv[i]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument("Missing argument to '%s'", argv[i]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument("Missing argument to '%s'", argv[i]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument("Missing argument to '%s'", argv[i]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#endif /* RT_OS_LINUX */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_CREATERAWVMDK, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_CREATERAWVMDK, "Mandatory parameter -filename missing");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_CREATERAWVMDK, "Mandatory parameter -rawdisk missing");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_CREATERAWVMDK, "The parameter -mbr is only valid when the parameter -partitions is also present");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync int vrc = RTFileOpen(&RawFile, rawdisk.raw(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error opening the raw disk '%s': %Rrc\n", rawdisk.raw(), vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Windows NT has no IOCTL_DISK_GET_LENGTH_INFORMATION ioctl. This was
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * added to Windows XP, so we have to use the available info from DriveGeo.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Note that we cannot simply use IOCTL_DISK_GET_DRIVE_GEOMETRY as it
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * yields a slightly different result than IOCTL_DISK_GET_LENGTH_INFO.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * We call IOCTL_DISK_GET_DRIVE_GEOMETRY first as we need to check the media
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * type anyway, and if IOCTL_DISK_GET_LENGTH_INFORMATION is supported
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * we will later override cbSize.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("File '%s' is no fixed/removable medium device\n", rawdisk.raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync &DiskLenInfo, sizeof(DiskLenInfo), &junk, (LPOVERLAPPED)NULL))
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* IOCTL_DISK_GET_LENGTH_INFO is supported -- override cbSize. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error getting the geometry of the raw disk '%s': %Rrc\n", rawdisk.raw(), vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* BLKGETSIZE64 is broken up to 2.4.17 and in many 2.5.x. In 2.6.0
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * it works without problems. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync && ( (strncmp(utsname.release, "2.5.", 4) == 0 && atoi(&utsname.release[4]) >= 18)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync || (strncmp(utsname.release, "2.", 2) == 0 && atoi(&utsname.release[2]) >= 6)))
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#endif /* BLKGETSIZE64 */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error getting the size of the raw disk '%s': %Rrc\n", rawdisk.raw(), vrc);
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync RTPrintf("File '%s' is no block device\n", rawdisk.raw());
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Cannot get the block size for file '%s': %Rrc", rawdisk.raw(), vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Cannot get the block count for file '%s': %Rrc", rawdisk.raw(), vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("File '%s' is no block device\n", rawdisk.raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if (!fstat(RawFile, &DevStat) && ( S_ISBLK(DevStat.st_mode)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync cbSize = mediainfo.dki_capacity * mediainfo.dki_lbsize;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error getting the size of the raw disk '%s': %Rrc\n", rawdisk.raw(), vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("File '%s' is no block or char device\n", rawdisk.raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#else /* all unrecognized OSes */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Hopefully this works on all other hosts. If it doesn't, it'll just fail
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * creating the VMDK, so no real harm done. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error getting the size of the raw disk '%s': %Rrc\n", rawdisk.raw(), vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Check whether cbSize is actually sensible. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Detected size of raw disk '%s' is %s, an invalid value\n", rawdisk.raw(), cbSize);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync const char *p = pszPartitions;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync while (*p != '\0')
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Incorrect value in partitions parameter\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if (*p == ',')
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync else if (*p != '\0')
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync RTPrintf("Incorrect separator in partitions parameter\n");
9f3a515a29d45728a5c5330459abd3429e35bda1vboxsync RTPrintf("Error reading the partition information from '%s'\n", rawdisk.raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (unsigned i = 0; i < partitions.cPartitions; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if ( uPartitions & RT_BIT(partitions.aPartitions[i].uIndex)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync && PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Some ignorant user specified an extended partition.
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * Bad idea, as this would trigger an overlapping
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * partitions error later during VMDK creation. So warn
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * here and ignore what the user requested. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Warning: it is not possible (and necessary) to explicitly give access to the\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " extended partition %u. If required, enable access to all logical\n"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync " partitions inside this extended partition.\n", partitions.aPartitions[i].uIndex);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync uPartitions &= ~RT_BIT(partitions.aPartitions[i].uIndex);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.cPartitions = partitions.cPartitions;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions = (PVBOXHDDRAWPART)RTMemAllocZ(partitions.cPartitions * sizeof(VBOXHDDRAWPART));
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Out of memory allocating the partition list for '%s'\n", rawdisk.raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (unsigned i = 0; i < partitions.cPartitions; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if (uPartitions & RT_BIT(partitions.aPartitions[i].uIndex))
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Refer to the correct partition and use offset 0. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vrc = RTStrAPrintf(&pszRawName, "%s%u", rawdisk.raw(),
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error creating reference to individual partition %u, rc=%Rrc\n",
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].pszRawDevice = pszRawName;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].uPartitionStartOffset = 0;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].uPartitionStart = partitions.aPartitions[i].uStart * 512;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** @todo not implemented yet for Windows host. Treat just
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * like not specified (this code is actually never reached). */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].pszRawDevice = rawdisk.raw();
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].uPartitionStartOffset = partitions.aPartitions[i].uStart * 512;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].uPartitionStart = partitions.aPartitions[i].uStart * 512;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* This is the "everything refers to the base raw device"
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * variant. This requires opening the base device in RW
681bb0bf6e9ab5a590d8afcdf37634e2fc29f595vboxsync * mode even for creation. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].pszRawDevice = rawdisk.raw();
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].uPartitionStartOffset = partitions.aPartitions[i].uStart * 512;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].uPartitionStart = partitions.aPartitions[i].uStart * 512;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Suppress access to this partition. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].uPartitionStartOffset = 0;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* This is used in the plausibility check in the creation
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * code. In theory it's a dummy, but I don't want to make
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * the VMDK creatiion any more complicated than what it needs
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * to be. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].uPartitionStart = partitions.aPartitions[i].uStart * 512;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if (PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Suppress exporting the actual extended partition. Only
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * logical partitions should be processed. However completely
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * ignoring it leads to leaving out the MBR data. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].cbPartition = partitions.aPartitions[i].uSize * 512;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].uPartitionDataStart = partitions.aPartitions[i].uPartDataStart * 512;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /** @todo the clipping below isn't 100% accurate, as it should
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * actually clip to the track size. However that's easier said
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync * than done as figuring out the track size is heuristics. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].cbPartitionData = RT_MIN(partitions.aPartitions[i].cPartDataSectors, 63) * 512;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync Assert (RawDescriptor.pPartitions[i].cbPartitionData -
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync (size_t)RawDescriptor.pPartitions[i].cbPartitionData == 0);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync void *pPartData = RTMemAlloc((size_t)RawDescriptor.pPartitions[i].cbPartitionData);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Out of memory allocating the partition descriptor for '%s'\n", rawdisk.raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vrc = RTFileReadAt(RawFile, partitions.aPartitions[i].uPartDataStart * 512, pPartData, (size_t)RawDescriptor.pPartitions[i].cbPartitionData, NULL);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Cannot read partition data from raw device '%s': %Rrc\n", rawdisk.raw(), vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Splice in the replacement MBR code if specified. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vrc = RTFileOpen(&MBRFile, pszMBRFilename, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Cannot open replacement MBR file '%s' specified with -mbr: %Rrc\n", pszMBRFilename, vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vrc = RTFileReadAt(MBRFile, 0, pPartData, 0x1be, NULL);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Cannot read replacement MBR file '%s': %Rrc\n", pszMBRFilename, vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RawDescriptor.pPartitions[i].pvPartitionData = pPartData;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vdInterfaceErrorCallbacks.pfnError = handleVDError;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error while creating the virtual disk container: %Rrc\n", vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync (unsigned int)RT_MIN(cbSize / 512 / 16 / 63, 16383) == 0);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync PCHS.cCylinders = (unsigned int)RT_MIN(cbSize / 512 / 16 / 63, 16383);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vrc = VDCreateBase(pDisk, "VMDK", Utf8Str(filename).raw(), cbSize,
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync VD_IMAGE_FLAGS_FIXED | VD_VMDK_IMAGE_FLAGS_RAWDISK,
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error while creating the raw disk VMDK: %Rrc\n", vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("RAW host disk access VMDK file %s created successfully.\n", Utf8Str(filename).raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Clean up allocated memory etc. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (unsigned i = 0; i < partitions.cPartitions; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync if (uPartitions & RT_BIT(partitions.aPartitions[i].uIndex))
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Free memory allocated above. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTStrFree((char *)(void *)RawDescriptor.pPartitions[i].pszRawDevice);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#endif /* RT_OS_LINUX */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync CHECK_ERROR(aVirtualBox, OpenHardDisk(filename, AccessMode_ReadWrite, false, Bstr(""), false, Bstr(""), hardDisk.asOutParam()));
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("The raw disk vmdk file was not created\n");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic int CmdRenameVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Parse the arguments. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (int i = 0; i < argc; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument("Missing argument to '%s'", argv[i]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument("Missing argument to '%s'", argv[i]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_RENAMEVMDK, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_RENAMEVMDK, "Mandatory parameter -from missing");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_RENAMEVMDK, "Mandatory parameter -to missing");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vdInterfaceErrorCallbacks.pfnError = handleVDError;
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync int vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error while creating the virtual disk container: %Rrc\n", vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vrc = VDOpen(pDisk, "VMDK", Utf8Str(src).raw(), VD_OPEN_FLAGS_NORMAL, NULL);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error while opening the source image: %Rrc\n", vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync vrc = VDCopy(pDisk, 0, pDisk, "VMDK", Utf8Str(dst).raw(), true, 0, VD_IMAGE_FLAGS_NONE, NULL, NULL, NULL, NULL);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync RTPrintf("Error while renaming the image: %Rrc\n", vrc);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsyncstatic int CmdConvertToRaw(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync /* Parse the arguments. */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync for (int i = 0; i < argc; i++)
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorArgument("Missing argument to '%s'", argv[i]);
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_CONVERTTORAW, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_CONVERTTORAW, "Mandatory filename parameter missing");
c0d22c23d3289cef0332f4d61ab15df48ed34979vboxsync return errorSyntax(USAGE_CONVERTTORAW, "Mandatory outputfile parameter missing");
if (fWriteToStdOut)
vrc = RTFileOpen(&outFile, Utf8Str(dst).raw(), RTFILE_O_OPEN | RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_ALL);
if (!fWriteToStdOut)
if (!fWriteToStdOut)
if (pvBuf)
RTPrintf("Converting image \"%s\" with size %RU64 bytes (%RU64MB) to raw...\n", Utf8Str(src).raw(), cbSize, (cbSize + _1M - 1) / _1M);
if (!fWriteToStdOut)
if (!fWriteToStdOut)
if (!fWriteToStdOut)
static int CmdConvertHardDisk(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
int vrc;
for (int i = 0; i < argc; i++)
RTPrintf("Converting image \"%s\" with size %RU64 bytes (%RU64MB)...\n", Utf8Str(src).raw(), cbSize, (cbSize + _1M - 1) / _1M);
if (pDstDisk)
if (pSrcDisk)
int CmdModUninstall(void)
int rc;
return E_FAIL;
int CmdModInstall(void)
int rc;
return E_FAIL;
g_fInternalMode = true;
return CmdModInstall();
return CmdModUninstall();