VBoxInternalManage.cpp revision 824ae3158a8b8f8233fec3f5a12c81f139933698
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/* $Id$ */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @file
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * VBoxManage - The 'internalcommands' command.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * VBoxInternalManage used to be a second CLI for doing special tricks,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * not intended for general usage, only for assisting VBox developers.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * It is now integrated into VBoxManage.
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Copyright (C) 2006-2010 Oracle Corporation
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * available from http://www.virtualbox.org. This file is free software;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * you can redistribute it and/or modify it under the terms of the GNU
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * General Public License (GPL) as published by the Free Software
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync/*******************************************************************************
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync* Header Files *
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync*******************************************************************************/
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/com/com.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/com/string.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/com/Guid.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/com/ErrorInfo.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/com/errorprint.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/com/VirtualBox.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/VBoxHDD.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/sup.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/err.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <VBox/log.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/file.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/getopt.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/stream.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/string.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/uuid.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include "VBoxManage.h"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/* Includes for the raw disk stuff. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef RT_OS_WINDOWS
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync# include <windows.h>
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync# include <winioctl.h>
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync#elif defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) \
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync || defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync# include <errno.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync# include <sys/ioctl.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync# include <sys/types.h>
acca7ab818eb8c2832aa27e27ce53133fba927dbvboxsync# include <sys/stat.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync# include <fcntl.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync# include <unistd.h>
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync#endif
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync#ifdef RT_OS_LINUX
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync# include <sys/utsname.h>
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync# include <linux/hdreg.h>
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync# include <linux/fs.h>
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync# include <stdlib.h> /* atoi() */
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync#endif /* RT_OS_LINUX */
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync#ifdef RT_OS_DARWIN
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync# include <sys/disk.h>
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync#endif /* RT_OS_DARWIN */
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync#ifdef RT_OS_SOLARIS
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync# include <stropts.h>
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync# include <sys/dkio.h>
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync# include <sys/vtoc.h>
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync#endif /* RT_OS_SOLARIS */
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync#ifdef RT_OS_FREEBSD
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync# include <sys/disk.h>
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync#endif /* RT_OS_FREEBSD */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncusing namespace com;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
5eb36887f6970e0033f63fa135f3bb8fbfd6059bvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Macro for checking whether a partition is of extended type or not. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#define PARTTYPE_IS_EXTENDED(x) ((x) == 0x05 || (x) == 0x0f || (x) == 0x85)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Maximum number of partitions we can deal with.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Ridiculously large number, but the memory consumption is rather low so who
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * cares about never using most entries. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#define HOSTPARTITION_MAX 100
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct HOSTPARTITION
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uIndex;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** partition type */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uType;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** CHS/cylinder of the first sector */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uStartCylinder;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** CHS/head of the first sector */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uStartHead;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** CHS/head of the first sector */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uStartSector;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync /** CHS/cylinder of the last sector */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uEndCylinder;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** CHS/head of the last sector */
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync unsigned uEndHead;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync /** CHS/sector of the last sector */
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync unsigned uEndSector;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync /** start sector of this partition relative to the beginning of the hard
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * disk or relative to the beginning of the extended partition table */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t uStart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** numer of sectors of the partition */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t uSize;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** start sector of this partition _table_ */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t uPartDataStart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** numer of sectors of this partition _table_ */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t cPartDataSectors;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} HOSTPARTITION, *PHOSTPARTITION;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct HOSTPARTITIONS
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync{
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync unsigned cPartitions;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync HOSTPARTITION aPartitions[HOSTPARTITION_MAX];
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync} HOSTPARTITIONS, *PHOSTPARTITIONS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync/** flag whether we're in internal mode */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsyncbool g_fInternalMode;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Print the usage info.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncvoid printUsageInternal(USAGECATEGORY u64Cmd, PRTSTREAM pStrm)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPrintf(pStrm,
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync "Usage: VBoxManage internalcommands <command> [command arguments]\n"
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "Commands:\n"
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync "\n"
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync "%s%s%s%s%s%s%s%s%s%s%s%s"
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync "WARNING: This is a development tool and shall only be used to analyse\n"
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync " problems. It is completely unsupported and will change in\n"
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync " incompatible ways without warning.\n",
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_LOADSYMS)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " loadsyms <vmname>|<uuid> <symfile> [delta] [module] [module address]\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " This will instruct DBGF to load the given symbolfile\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " during initialization.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_UNLOADSYMS)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " unloadsyms <vmname>|<uuid> <symfile>\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " Removes <symfile> from the list of symbol files that\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " should be loaded during DBF initialization.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_SETHDUUID)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " sethduuid <filepath> [<uuid>]\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " Assigns a new UUID to the given image file. This way, multiple copies\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " of a container can be registered.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_SETHDPARENTUUID)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " sethdparentuuid <filepath> <uuid>\n"
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync " Assigns a new parent UUID to the given image file.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_DUMPHDINFO)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " dumphdinfo <filepath>\n"
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync " Prints information about the image at the given location.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_LISTPARTITIONS)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " listpartitions -rawdisk <diskname>\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " Lists all partitions on <diskname>.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_CREATERAWVMDK)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " createrawvmdk -filename <filename> -rawdisk <diskname>\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " [-partitions <list of partition numbers> [-mbr <filename>] ]\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " [-register] [-relative]\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " Creates a new VMDK image which gives access to an entite host disk (if\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " the parameter -partitions is not specified) or some partitions of a\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " host disk. If access to individual partitions is granted, then the\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " parameter -mbr can be used to specify an alternative MBR to be used\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " (the partitioning information in the MBR file is ignored).\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " The diskname is on Linux e.g. /dev/sda, and on Windows e.g.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " \\\\.\\PhysicalDrive0).\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " On Linux host the parameter -relative causes a VMDK file to be created\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " which refers to individual partitions instead to the entire disk.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " Optionally the created image can be immediately registered.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " The necessary partition numbers can be queried with\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " VBoxManage internalcommands listpartitions\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_RENAMEVMDK)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " renamevmdk -from <filename> -to <filename>\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " Renames an existing VMDK image, including the base file and all its extents.\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_CONVERTTORAW)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " converttoraw [-format <fileformat>] <filename> <outputfile>"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef ENABLE_CONVERT_RAW_TO_STDOUT
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "|stdout"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync " Convert image to raw, writing to file"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef ENABLE_CONVERT_RAW_TO_STDOUT
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " or stdout"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ".\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_CONVERTHD)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " converthd [-srcformat VDI|VMDK|VHD|RAW]\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " [-dstformat VDI|VMDK|VHD|RAW]\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " <inputfile> <outputfile>\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " converts hard disk images between formats\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef RT_OS_WINDOWS
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_MODINSTALL)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " modinstall\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " Installs the neccessary driver for the host OS\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
7755c0a4b7b215f612d9d27848c2584ffafe7a66vboxsync : "",
7755c0a4b7b215f612d9d27848c2584ffafe7a66vboxsync (u64Cmd & USAGE_MODUNINSTALL)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " moduninstall\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " Deinstalls the driver\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#endif
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (u64Cmd & USAGE_DEBUGLOG)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? " debuglog <vmname>|<uuid> [--enable|--disable] [--flags todo]\n"
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync " [--groups todo] [--destinations todo]\n"
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync " Controls debug logging.\n"
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync "\n"
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync : ""
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync );
602e0e27740395dba64bee2e0a8aef023ebd7650vboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @todo this is no longer necessary, we can enumerate extra data */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Finds a new unique key name.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * I don't think this is 100% race condition proof, but we assumes
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * the user is not trying to push this point.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns Result from the insert.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pMachine The Machine object.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszKeyBase The base key.
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * @param rKey Reference to the string object in which we will return the key.
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsyncstatic HRESULT NewUniqueKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, Utf8Str &rKey)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync{
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync Bstr Keys;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (FAILED(hrc))
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync return hrc;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /* if there are no keys, it's simple. */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (Keys.isEmpty())
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync rKey = "1";
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr("1"));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /* find a unique number - brute force rulez. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Utf8Str KeysUtf8(Keys);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync const char *pszKeys = RTStrStripL(KeysUtf8.c_str());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned i = 1; i < 1000000; i++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char szKey[32];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchKey = RTStrPrintf(szKey, sizeof(szKey), "%#x", i);
78df65edff21c11c537f38e736707ea434ab5623vboxsync const char *psz = strstr(pszKeys, szKey);
78df65edff21c11c537f38e736707ea434ab5623vboxsync while (psz)
78df65edff21c11c537f38e736707ea434ab5623vboxsync {
78df65edff21c11c537f38e736707ea434ab5623vboxsync if ( ( psz == pszKeys
78df65edff21c11c537f38e736707ea434ab5623vboxsync || psz[-1] == ' ')
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync && ( psz[cchKey] == ' '
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync || !psz[cchKey])
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync )
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync break;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync psz = strstr(psz + cchKey, szKey);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (!psz)
78df65edff21c11c537f38e736707ea434ab5623vboxsync {
78df65edff21c11c537f38e736707ea434ab5623vboxsync rKey = szKey;
78df65edff21c11c537f38e736707ea434ab5623vboxsync Utf8StrFmt NewKeysUtf8("%s %s", pszKeys, szKey);
78df65edff21c11c537f38e736707ea434ab5623vboxsync return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(NewKeysUtf8));
78df65edff21c11c537f38e736707ea434ab5623vboxsync }
78df65edff21c11c537f38e736707ea434ab5623vboxsync }
78df65edff21c11c537f38e736707ea434ab5623vboxsync RTMsgError("Cannot find unique key for '%s'!", pszKeyBase);
78df65edff21c11c537f38e736707ea434ab5623vboxsync return E_FAIL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#if 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * Remove a key.
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync *
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * I don't think this isn't 100% race condition proof, but we assumes
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * the user is not trying to push this point.
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync *
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * @returns Result from the insert.
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * @param pMachine The machine object.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszKeyBase The base key.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszKey The key to remove.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic HRESULT RemoveKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Bstr Keys;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (FAILED(hrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return hrc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* if there are no keys, it's simple. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (Keys.isEmpty())
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return S_OK;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pszKeys;
78df65edff21c11c537f38e736707ea434ab5623vboxsync int rc = RTUtf16ToUtf8(Keys.raw(), &pszKeys);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_SUCCESS(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* locate it */
78df65edff21c11c537f38e736707ea434ab5623vboxsync size_t cchKey = strlen(pszKey);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync char *psz = strstr(pszKeys, pszKey);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync while (psz)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if ( ( psz == pszKeys
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || psz[-1] == ' ')
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && ( psz[cchKey] == ' '
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || !psz[cchKey])
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync )
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync psz = strstr(psz + cchKey, pszKey);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (psz)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /* remove it */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync char *pszNext = RTStrStripL(psz + cchKey);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (*pszNext)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync memmove(psz, pszNext, strlen(pszNext) + 1);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync else
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync *psz = '\0';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync psz = RTStrStrip(pszKeys);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* update */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync hrc = pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(psz));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrFree(pszKeys);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return hrc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Failed to delete key '%s' from '%s', string conversion error %Rrc!",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pszKey, pszKeyBase, rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return E_FAIL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#endif
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Sets a key value, does necessary error bitching.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns COM status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pMachine The Machine object.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszKeyBase The key base.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszKey The key.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszAttribute The attribute name.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszValue The string value.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic HRESULT SetString(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, const char *pszValue)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync HRESULT hrc = pMachine->SetExtraData(Bstr(Utf8StrFmt("%s/%s/%s", pszKeyBase, pszKey, pszAttribute)), Bstr(pszValue));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (FAILED(hrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Failed to set '%s/%s/%s' to '%s'! hrc=%#x",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pszKeyBase, pszKey, pszAttribute, pszValue, hrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return hrc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Sets a key value, does necessary error bitching.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns COM status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pMachine The Machine object.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszKeyBase The key base.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszKey The key.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszAttribute The attribute name.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param u64Value The value.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic HRESULT SetUInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, uint64_t u64Value)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync char szValue[64];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrPrintf(szValue, sizeof(szValue), "%#RX64", u64Value);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Sets a key value, does necessary error bitching.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns COM status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pMachine The Machine object.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszKeyBase The key base.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszKey The key.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszAttribute The attribute name.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param i64Value The value.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic HRESULT SetInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, int64_t i64Value)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char szValue[64];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrPrintf(szValue, sizeof(szValue), "%RI64", i64Value);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Identical to the 'loadsyms' command.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int CmdLoadSyms(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync HRESULT rc;
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Get the VM
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ComPtr<IMachine> machine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* assume it's a UUID */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = aVirtualBox->GetMachine(Bstr(argv[0]), machine.asOutParam());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (FAILED(rc) || !machine)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* must be a name */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()), 1);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Parse the command.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszFilename;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int64_t offDelta = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszModule = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t ModuleAddress = ~0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t ModuleSize = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* filename */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (argc < 2)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorArgument("Missing the filename argument!\n");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pszFilename = argv[1];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* offDelta */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (argc >= 3)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int irc = RTStrToInt64Ex(argv[2], NULL, 0, &offDelta);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(irc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorArgument(argv[0], "Failed to read delta '%s', rc=%Rrc\n", argv[2], rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* pszModule */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (argc >= 4)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pszModule = argv[3];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* ModuleAddress */
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync if (argc >= 5)
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int irc = RTStrToUInt64Ex(argv[4], NULL, 0, &ModuleAddress);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(irc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorArgument(argv[0], "Failed to read module address '%s', rc=%Rrc\n", argv[4], rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync /* ModuleSize */
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync if (argc >= 6)
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync {
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync int irc = RTStrToUInt64Ex(argv[5], NULL, 0, &ModuleSize);
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync if (RT_FAILURE(irc))
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync return errorArgument(argv[0], "Failed to read module size '%s', rc=%Rrc\n", argv[5], rc);
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync }
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync /*
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync * Add extra data.
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync */
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync Utf8Str KeyStr;
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync HRESULT hrc = NewUniqueKey(machine, "VBoxInternal/DBGF/loadsyms", KeyStr);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (SUCCEEDED(hrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "Filename", pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (SUCCEEDED(hrc) && argc >= 3)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync hrc = SetInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "Delta", offDelta);
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync if (SUCCEEDED(hrc) && argc >= 4)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "Module", pszModule);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (SUCCEEDED(hrc) && argc >= 5)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "ModuleAddress", ModuleAddress);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (SUCCEEDED(hrc) && argc >= 6)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "ModuleSize", ModuleSize);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return FAILED(hrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsyncstatic DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync{
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync RTMsgErrorV(pszFormat, va);
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync RTMsgError("Error code %Rrc at %s(%u) in function %s", rc, RT_SRC_POS_ARGS);
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync}
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsyncstatic int handleVDMessage(void *pvUser, const char *pszFormat, va_list va)
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync{
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync NOREF(pvUser);
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync return RTPrintfV(pszFormat, va);
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync}
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsyncstatic int CmdSetHDUUID(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Guid uuid;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTUUID rtuuid;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync enum eUuidType {
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync HDUUID,
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync HDPARENTUUID
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync } uuidType;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync if (!strcmp(argv[0], "sethduuid"))
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync {
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync uuidType = HDUUID;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync if (argc != 3 && argc != 2)
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync return errorSyntax(USAGE_SETHDUUID, "Not enough parameters");
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync /* if specified, take UUID, otherwise generate a new one */
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync if (argc == 3)
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync {
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync if (RT_FAILURE(RTUuidFromStr(&rtuuid, argv[2])))
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync return errorSyntax(USAGE_SETHDUUID, "Invalid UUID parameter");
a86135e41c89c7b599607649347a4240809c784bvboxsync uuid = argv[2];
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync } else
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync uuid.create();
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync }
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync else if (!strcmp(argv[0], "sethdparentuuid"))
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync {
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync uuidType = HDPARENTUUID;
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync if (argc != 3)
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync return errorSyntax(USAGE_SETHDPARENTUUID, "Not enough parameters");
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync if (RT_FAILURE(RTUuidFromStr(&rtuuid, argv[2])))
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync return errorSyntax(USAGE_SETHDPARENTUUID, "Invalid UUID parameter");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uuid = argv[2];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorSyntax(USAGE_SETHDUUID, "Invalid invocation");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* just try it */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pszFormat = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync argv[1], &pszFormat);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Format autodetect failed: %Rrc", rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return 1;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVBOXHDD pDisk = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDINTERFACE pVDIfs = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync VDINTERFACE vdInterfaceError;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync VDINTERFACEERROR vdInterfaceErrorCallbacks;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdInterfaceErrorCallbacks.pfnError = handleVDError;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &vdInterfaceErrorCallbacks, NULL, &pVDIfs);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertRC(rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VDCreate(pVDIfs, &pDisk);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot create the virtual disk container: %Rrc", rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return 1;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Open the image */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VDOpen(pDisk, pszFormat, argv[1], VD_OPEN_FLAGS_NORMAL, NULL);
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot open the image: %Rrc", rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return 1;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (uuidType == HDUUID)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VDSetUuid(pDisk, VD_LAST_IMAGE, uuid.raw());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VDSetParentUuid(pDisk, VD_LAST_IMAGE, uuid.raw());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot set a new UUID: %Rrc", rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTPrintf("UUID changed to: %s\n", uuid.toString().c_str());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync VDCloseAll(pDisk);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return RT_FAILURE(rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int CmdDumpHDInfo(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* we need exactly one parameter: the image file */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (argc != 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorSyntax(USAGE_DUMPHDINFO, "Not enough parameters");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* just try it */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pszFormat = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync argv[0], &pszFormat);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Format autodetect failed: %Rrc", rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return 1;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVBOXHDD pDisk = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDINTERFACE pVDIfs = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync VDINTERFACE vdInterfaceError;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync VDINTERFACEERROR vdInterfaceErrorCallbacks;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdInterfaceErrorCallbacks.pfnError = handleVDError;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &vdInterfaceErrorCallbacks, NULL, &pVDIfs);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertRC(rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VDCreate(pVDIfs, &pDisk);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot create the virtual disk container: %Rrc", rc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return 1;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Open the image */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = VDOpen(pDisk, pszFormat, argv[0], VD_OPEN_FLAGS_INFO, NULL);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (RT_FAILURE(rc))
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync RTMsgError("Cannot open the image: %Rrc", rc);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync return 1;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync VDDumpImages(pDisk);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync VDCloseAll(pDisk);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync return RT_FAILURE(rc);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync}
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsyncstatic int partRead(RTFILE File, PHOSTPARTITIONS pPart)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync{
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync uint8_t aBuffer[512];
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync int rc;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pPart->cPartitions = 0;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync memset(pPart->aPartitions, '\0', sizeof(pPart->aPartitions));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = RTFileReadAt(File, 0, &aBuffer, sizeof(aBuffer), NULL);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (RT_FAILURE(rc))
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync return rc;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (aBuffer[510] != 0x55 || aBuffer[511] != 0xaa)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync return VERR_INVALID_PARAMETER;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync unsigned uExtended = (unsigned)-1;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync for (unsigned i = 0; i < 4; i++)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync uint8_t *p = &aBuffer[0x1be + i * 16];
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (p[4] == 0)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync continue;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync PHOSTPARTITION pCP = &pPart->aPartitions[pPart->cPartitions++];
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uIndex = i + 1;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uType = p[4];
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uStartCylinder = (uint32_t)p[3] + ((uint32_t)(p[2] & 0xc0) << 2);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uStartHead = p[1];
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uStartSector = p[2] & 0x3f;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uEndCylinder = (uint32_t)p[7] + ((uint32_t)(p[6] & 0xc0) << 2);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pCP->uEndHead = p[5];
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uEndSector = p[6] & 0x3f;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uStart = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pCP->uSize = RT_MAKE_U32_FROM_U8(p[12], p[13], p[14], p[15]);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pCP->uPartDataStart = 0; /* will be filled out later properly. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pCP->cPartDataSectors = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (PARTTYPE_IS_EXTENDED(p[4]))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (uExtended == (unsigned)-1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uExtended = (unsigned)(pCP - pPart->aPartitions);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("More than one extended partition");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync if (uExtended != (unsigned)-1)
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync {
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync unsigned uIndex = 5;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync uint64_t uStart = pPart->aPartitions[uExtended].uStart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t uOffset = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!uStart)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Inconsistency for logical partition start");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync do
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileReadAt(File, (uStart + uOffset) * 512, &aBuffer, sizeof(aBuffer), NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (aBuffer[510] != 0x55 || aBuffer[511] != 0xaa)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Logical partition without magic");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint8_t *p = &aBuffer[0x1be];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (p[4] == 0)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync RTMsgError("Logical partition with type 0 encountered");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync PHOSTPARTITION pCP = &pPart->aPartitions[pPart->cPartitions++];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pCP->uIndex = uIndex;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pCP->uType = p[4];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pCP->uStartCylinder = (uint32_t)p[3] + ((uint32_t)(p[2] & 0xc0) << 2);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uStartHead = p[1];
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uStartSector = p[2] & 0x3f;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uEndCylinder = (uint32_t)p[7] + ((uint32_t)(p[6] & 0xc0) << 2);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uEndHead = p[5];
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uEndSector = p[6] & 0x3f;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync uint32_t uStartOffset = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (!uStartOffset)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync RTMsgError("Invalid partition start offset");
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync return VERR_INVALID_PARAMETER;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uStart = uStart + uOffset + uStartOffset;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uSize = RT_MAKE_U32_FROM_U8(p[12], p[13], p[14], p[15]);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Fill out partitioning location info for EBR. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->uPartDataStart = uStart + uOffset;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pCP->cPartDataSectors = uStartOffset;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync p += 16;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (p[4] == 0)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync uExtended = (unsigned)-1;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync else if (PARTTYPE_IS_EXTENDED(p[4]))
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync uExtended = uIndex++;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync uOffset = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Logical partition chain broken");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync } while (uExtended != (unsigned)-1);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Sort partitions in ascending order of start sector, plus a trivial
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * bit of consistency checking. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned i = 0; i < pPart->cPartitions-1; i++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uMinIdx = i;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t uMinVal = pPart->aPartitions[i].uStart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned j = i + 1; j < pPart->cPartitions; j++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pPart->aPartitions[j].uStart < uMinVal)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uMinIdx = j;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uMinVal = pPart->aPartitions[j].uStart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (pPart->aPartitions[j].uStart == uMinVal)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Two partitions start at the same place");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (pPart->aPartitions[j].uStart == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Partition starts at sector 0");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (uMinIdx != i)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Swap entries at index i and uMinIdx. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memcpy(&pPart->aPartitions[pPart->cPartitions],
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &pPart->aPartitions[i], sizeof(HOSTPARTITION));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memcpy(&pPart->aPartitions[i],
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &pPart->aPartitions[uMinIdx], sizeof(HOSTPARTITION));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memcpy(&pPart->aPartitions[uMinIdx],
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &pPart->aPartitions[pPart->cPartitions], sizeof(HOSTPARTITION));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Fill out partitioning location info for MBR. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPart->aPartitions[0].uPartDataStart = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPart->aPartitions[0].cPartDataSectors = pPart->aPartitions[0].uStart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync /* Now do a some partition table consistency checking, to reject the most
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * obvious garbage which can lead to trouble later. */
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync uint64_t uPrevEnd = 0;
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync for (unsigned i = 0; i < pPart->cPartitions-1; i++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pPart->aPartitions[i].cPartDataSectors)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uPrevEnd = pPart->aPartitions[i].uPartDataStart + pPart->aPartitions[i].cPartDataSectors;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pPart->aPartitions[i].uStart < uPrevEnd)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Overlapping partitions");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!PARTTYPE_IS_EXTENDED(pPart->aPartitions[i].uType))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uPrevEnd = pPart->aPartitions[i].uStart + pPart->aPartitions[i].uSize;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int CmdListPartitions(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Utf8Str rawdisk;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* let's have a closer look at the arguments */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (int i = 0; i < argc; i++)
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (strcmp(argv[i], "-rawdisk") == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (argc <= i + 1)
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync {
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync return errorArgument("Missing argument to '%s'", argv[i]);
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync }
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync i++;
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync rawdisk = argv[i];
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorSyntax(USAGE_LISTPARTITIONS, "Invalid parameter '%s'", argv[i]);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (rawdisk.isEmpty())
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorSyntax(USAGE_LISTPARTITIONS, "Mandatory parameter -rawdisk missing");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFILE RawFile;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int vrc = RTFileOpen(&RawFile, rawdisk.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(vrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannnot open the raw disk: %Rrc", vrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return vrc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync HOSTPARTITIONS partitions;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = partRead(RawFile, &partitions);
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync /* Don't bail out on errors, print the table and return the result code. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTPrintf("Number Type StartCHS EndCHS Size (MiB) Start (Sect)\n");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned i = 0; i < partitions.cPartitions; i++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Don't show the extended partition, otherwise users might think they
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * can add it to the list of partitions for raw partition access. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync continue;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTPrintf("%-7u %#04x %-4u/%-3u/%-2u %-4u/%-3u/%-2u %10llu %10llu\n",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync partitions.aPartitions[i].uIndex,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync partitions.aPartitions[i].uType,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync partitions.aPartitions[i].uStartCylinder,
29a65fa8b740a860f86812328cf900b8d68d93bevboxsync partitions.aPartitions[i].uStartHead,
37a7e3e6f9ce5f6cabeb3f734044e9e8ca9cb1bfvboxsync partitions.aPartitions[i].uStartSector,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync partitions.aPartitions[i].uEndCylinder,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync partitions.aPartitions[i].uEndHead,
29a65fa8b740a860f86812328cf900b8d68d93bevboxsync partitions.aPartitions[i].uEndSector,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync partitions.aPartitions[i].uSize / 2048,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync partitions.aPartitions[i].uStart);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return vrc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic PVBOXHDDRAWPARTDESC appendPartDesc(uint32_t *pcPartDescs, PVBOXHDDRAWPARTDESC *ppPartDescs)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync (*pcPartDescs)++;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVBOXHDDRAWPARTDESC p;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync p = (PVBOXHDDRAWPARTDESC)RTMemRealloc(*ppPartDescs,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *pcPartDescs * sizeof(VBOXHDDRAWPARTDESC));
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync *ppPartDescs = p;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (p)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync p = p + *pcPartDescs - 1;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memset(p, '\0', sizeof(VBOXHDDRAWPARTDESC));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return p;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int CmdCreateRawVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync HRESULT rc = S_OK;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Utf8Str filename;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszMBRFilename = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Utf8Str rawdisk;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszPartitions = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fRegister = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fRelative = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t cbSize = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVBOXHDD pDisk = NULL;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync VBOXHDDRAW RawDescriptor;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDINTERFACE pVDIfs = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* let's have a closer look at the arguments */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (int i = 0; i < argc; i++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (strcmp(argv[i], "-filename") == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (argc <= i + 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync return errorArgument("Missing argument to '%s'", argv[i]);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync i++;
af0a09edb4c1431b606fe207d4138da008f67f13vboxsync filename = argv[i];
af0a09edb4c1431b606fe207d4138da008f67f13vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (strcmp(argv[i], "-mbr") == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (argc <= i + 1)
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync {
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync return errorArgument("Missing argument to '%s'", argv[i]);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync }
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync i++;
602e0e27740395dba64bee2e0a8aef023ebd7650vboxsync pszMBRFilename = argv[i];
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync }
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync else if (strcmp(argv[i], "-rawdisk") == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (argc <= i + 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorArgument("Missing argument to '%s'", argv[i]);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync i++;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rawdisk = argv[i];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (strcmp(argv[i], "-partitions") == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (argc <= i + 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorArgument("Missing argument to '%s'", argv[i]);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync i++;
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync pszPartitions = argv[i];
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync }
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync else if (strcmp(argv[i], "-register") == 0)
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync {
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync fRegister = true;
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync }
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync#ifdef RT_OS_LINUX
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (strcmp(argv[i], "-relative") == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync fRelative = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#endif /* RT_OS_LINUX */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorSyntax(USAGE_CREATERAWVMDK, "Invalid parameter '%s'", argv[i]);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (filename.isEmpty())
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorSyntax(USAGE_CREATERAWVMDK, "Mandatory parameter -filename missing");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (rawdisk.isEmpty())
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorSyntax(USAGE_CREATERAWVMDK, "Mandatory parameter -rawdisk missing");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pszPartitions && pszMBRFilename)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return errorSyntax(USAGE_CREATERAWVMDK, "The parameter -mbr is only valid when the parameter -partitions is also present");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef RT_OS_DARWIN
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync fRelative = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#endif /* RT_OS_DARWIN */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFILE RawFile;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int vrc = RTFileOpen(&RawFile, rawdisk.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(vrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot open the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef RT_OS_WINDOWS
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Windows NT has no IOCTL_DISK_GET_LENGTH_INFORMATION ioctl. This was
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * added to Windows XP, so we have to use the available info from DriveGeo.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Note that we cannot simply use IOCTL_DISK_GET_DRIVE_GEOMETRY as it
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync * yields a slightly different result than IOCTL_DISK_GET_LENGTH_INFO.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * We call IOCTL_DISK_GET_DRIVE_GEOMETRY first as we need to check the media
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync * type anyway, and if IOCTL_DISK_GET_LENGTH_INFORMATION is supported
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * we will later override cbSize.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync DISK_GEOMETRY DriveGeo;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync DWORD cbDriveGeo;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (DeviceIoControl((HANDLE)RawFile,
8d7d8eab781262b9db094374b166806c03786fa2vboxsync IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &DriveGeo, sizeof(DriveGeo), &cbDriveGeo, NULL))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( DriveGeo.MediaType == FixedMedia
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || DriveGeo.MediaType == RemovableMedia)
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbSize = DriveGeo.Cylinders.QuadPart
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * DriveGeo.TracksPerCylinder
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * DriveGeo.SectorsPerTrack
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * DriveGeo.BytesPerSector;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("File '%s' is no fixed/removable medium device", rawdisk.c_str());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
bc5c5b5cd56ce8e64ada076691018f1dff17b2c2vboxsync }
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync GET_LENGTH_INFORMATION DiskLenInfo;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync DWORD junk;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync if (DeviceIoControl((HANDLE)RawFile,
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync IOCTL_DISK_GET_LENGTH_INFO, NULL, 0,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &DiskLenInfo, sizeof(DiskLenInfo), &junk, (LPOVERLAPPED)NULL))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* IOCTL_DISK_GET_LENGTH_INFO is supported -- override cbSize. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbSize = DiskLenInfo.Length.QuadPart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = RTErrConvertFromWin32(GetLastError());
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync RTMsgError("Cannot get the geometry of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#elif defined(RT_OS_LINUX)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync struct stat DevStat;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef BLKGETSIZE64
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* BLKGETSIZE64 is broken up to 2.4.17 and in many 2.5.x. In 2.6.0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * it works without problems. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync struct utsname utsname;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( uname(&utsname) == 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && ( (strncmp(utsname.release, "2.5.", 4) == 0 && atoi(&utsname.release[4]) >= 18)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || (strncmp(utsname.release, "2.", 2) == 0 && atoi(&utsname.release[2]) >= 6)))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t cbBlk;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!ioctl(RawFile, BLKGETSIZE64, &cbBlk))
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync cbSize = cbBlk;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync#endif /* BLKGETSIZE64 */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!cbSize)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync long cBlocks;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!ioctl(RawFile, BLKGETSIZE, &cBlocks))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbSize = (uint64_t)cBlocks << 9;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync else
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync {
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync vrc = RTErrConvertFromErrno(errno);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync RTMsgError("Cannot get the size of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync goto out;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync else
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync {
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync RTMsgError("File '%s' is no block device", rawdisk.c_str());
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync vrc = VERR_INVALID_PARAMETER;
11c2b573e2625474a51ae55ee1f3f82936f125davboxsync goto out;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync#elif defined(RT_OS_DARWIN)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync struct stat DevStat;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync {
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync uint64_t cBlocks;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync uint32_t cbBlock;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (!ioctl(RawFile, DKIOCGETBLOCKCOUNT, &cBlocks))
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync {
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (!ioctl(RawFile, DKIOCGETBLOCKSIZE, &cbBlock))
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync cbSize = cBlocks * cbBlock;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync else
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync {
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync RTMsgError("Cannot get the block size for file '%s': %Rrc", rawdisk.c_str(), vrc);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync vrc = RTErrConvertFromErrno(errno);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync goto out;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
db07105a44f53e9e2377ade02cd3dbe482647cb9vboxsync else
db07105a44f53e9e2377ade02cd3dbe482647cb9vboxsync {
db07105a44f53e9e2377ade02cd3dbe482647cb9vboxsync vrc = RTErrConvertFromErrno(errno);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync RTMsgError("Cannot get the block count for file '%s': %Rrc", rawdisk.c_str(), vrc);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync goto out;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync else
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync {
04b02ffb8824a60fd37777bc1f7d2f35104a274cvboxsync RTMsgError("File '%s' is no block device", rawdisk.c_str());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#elif defined(RT_OS_SOLARIS)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync struct stat DevStat;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!fstat(RawFile, &DevStat) && ( S_ISBLK(DevStat.st_mode)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || S_ISCHR(DevStat.st_mode)))
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync {
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync struct dk_minfo mediainfo;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync if (!ioctl(RawFile, DKIOCGMEDIAINFO, &mediainfo))
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync cbSize = mediainfo.dki_capacity * mediainfo.dki_lbsize;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync else
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync {
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync vrc = RTErrConvertFromErrno(errno);
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync RTMsgError("Cannot get the size of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync goto out;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync }
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync }
04b02ffb8824a60fd37777bc1f7d2f35104a274cvboxsync else
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync {
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync RTMsgError("File '%s' is no block or char device", rawdisk.c_str());
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync vrc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#elif defined(RT_OS_FREEBSD)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync struct stat DevStat;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!fstat(RawFile, &DevStat) && S_ISCHR(DevStat.st_mode))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync off_t cbMedia = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!ioctl(RawFile, DIOCGMEDIASIZE, &cbMedia))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync cbSize = cbMedia;
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync }
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync else
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync {
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync vrc = RTErrConvertFromErrno(errno);
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync RTMsgError("Cannot get the block count for file '%s': %Rrc", rawdisk.c_str(), vrc);
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("File '%s' is no character device", rawdisk.c_str());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#else /* all unrecognized OSes */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Hopefully this works on all other hosts. If it doesn't, it'll just fail
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync * creating the VMDK, so no real harm done. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = RTFileGetSize(RawFile, &cbSize);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(vrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot get the size of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync }
78df65edff21c11c537f38e736707ea434ab5623vboxsync#endif
78df65edff21c11c537f38e736707ea434ab5623vboxsync
78df65edff21c11c537f38e736707ea434ab5623vboxsync /* Check whether cbSize is actually sensible. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!cbSize || cbSize % 512)
78df65edff21c11c537f38e736707ea434ab5623vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Detected size of raw disk '%s' is %s, an invalid value", rawdisk.c_str(), cbSize);
78df65edff21c11c537f38e736707ea434ab5623vboxsync vrc = VERR_INVALID_PARAMETER;
78df65edff21c11c537f38e736707ea434ab5623vboxsync goto out;
78df65edff21c11c537f38e736707ea434ab5623vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.szSignature[0] = 'R';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.szSignature[1] = 'A';
78df65edff21c11c537f38e736707ea434ab5623vboxsync RawDescriptor.szSignature[2] = 'W';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.szSignature[3] = '\0';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pszPartitions)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.fRawDisk = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.pszRawDisk = rawdisk.c_str();
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.fRawDisk = false;
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync RawDescriptor.pszRawDisk = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.cPartDescs = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.pPartDescs = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint32_t uPartitions = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *p = pszPartitions;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync char *pszNext;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint32_t u32;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync while (*p != '\0')
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = RTStrToUInt32Ex(p, &pszNext, 0, &u32);
37a7e3e6f9ce5f6cabeb3f734044e9e8ca9cb1bfvboxsync if (RT_FAILURE(vrc))
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Incorrect value in partitions parameter");
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync goto out;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync }
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync uPartitions |= RT_BIT(u32);
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync p = pszNext;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync if (*p == ',')
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync p++;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync else if (*p != '\0')
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync RTMsgError("Incorrect separator in partitions parameter");
01283af355575e02820380d73a30a474f425fd3dvboxsync vrc = VERR_INVALID_PARAMETER;
01283af355575e02820380d73a30a474f425fd3dvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync HOSTPARTITIONS partitions;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = partRead(RawFile, &partitions);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(vrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot read the partition information from '%s'", rawdisk.c_str());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned i = 0; i < partitions.cPartitions; i++)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( uPartitions & RT_BIT(partitions.aPartitions[i].uIndex)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Some ignorant user specified an extended partition.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Bad idea, as this would trigger an overlapping
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * partitions error later during VMDK creation. So warn
37a7e3e6f9ce5f6cabeb3f734044e9e8ca9cb1bfvboxsync * here and ignore what the user requested. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgWarning("It is not possible (and necessary) to explicitly give access to the "
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync "extended partition %u. If required, enable access to all logical "
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync "partitions inside this extended partition.",
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync partitions.aPartitions[i].uIndex);
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync uPartitions &= ~RT_BIT(partitions.aPartitions[i].uIndex);
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync }
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync }
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync for (unsigned i = 0; i < partitions.cPartitions; i++)
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVBOXHDDRAWPARTDESC pPartDesc = NULL;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* first dump the MBR/EPT data area */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (partitions.aPartitions[i].cPartDataSectors)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pPartDesc = appendPartDesc(&RawDescriptor.cPartDescs,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &RawDescriptor.pPartDescs);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (!pPartDesc)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Out of memory allocating the partition list for '%s'", rawdisk.c_str());
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync vrc = VERR_NO_MEMORY;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync goto out;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /** @todo the clipping below isn't 100% accurate, as it should
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * actually clip to the track size. However that's easier said
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * than done as figuring out the track size is heuristics. In
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * any case the clipping is adjusted later after sorting, to
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * prevent overlapping data areas on the resulting image. */
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync pPartDesc->cbData = RT_MIN(partitions.aPartitions[i].cPartDataSectors, 63) * 512;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync pPartDesc->uStart = partitions.aPartitions[i].uPartDataStart * 512;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync Assert(pPartDesc->cbData - (size_t)pPartDesc->cbData == 0);
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync void *pPartData = RTMemAlloc((size_t)pPartDesc->cbData);
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync if (!pPartData)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Out of memory allocating the partition descriptor for '%s'", rawdisk.c_str());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = VERR_NO_MEMORY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = RTFileReadAt(RawFile, partitions.aPartitions[i].uPartDataStart * 512,
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pPartData, (size_t)pPartDesc->cbData, NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(vrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot read partition data from raw device '%s': %Rrc", rawdisk.c_str(), vrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Splice in the replacement MBR code if specified. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( partitions.aPartitions[i].uPartDataStart == 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && pszMBRFilename)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync RTFILE MBRFile;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync vrc = RTFileOpen(&MBRFile, pszMBRFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (RT_FAILURE(vrc))
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot open replacement MBR file '%s' specified with -mbr: %Rrc", pszMBRFilename, vrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = RTFileReadAt(MBRFile, 0, pPartData, 0x1be, NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFileClose(MBRFile);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(vrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot read replacement MBR file '%s': %Rrc", pszMBRFilename, vrc);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pPartDesc->pvPartitionData = pPartData;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /* Suppress exporting the actual extended partition. Only
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * logical partitions should be processed. However completely
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * ignoring it leads to leaving out the EBR data. */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync continue;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /* set up values for non-relative device names */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync const char *pszRawName = rawdisk.c_str();
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync uint64_t uStartOffset = partitions.aPartitions[i].uStart * 512;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pPartDesc = appendPartDesc(&RawDescriptor.cPartDescs,
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync &RawDescriptor.pPartDescs);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (!pPartDesc)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
03493c32c4a1ce2b083405efe6f3b070bead1be6vboxsync RTMsgError("Out of memory allocating the partition list for '%s'", rawdisk.c_str());
03493c32c4a1ce2b083405efe6f3b070bead1be6vboxsync vrc = VERR_NO_MEMORY;
03493c32c4a1ce2b083405efe6f3b070bead1be6vboxsync goto out;
03493c32c4a1ce2b083405efe6f3b070bead1be6vboxsync }
03493c32c4a1ce2b083405efe6f3b070bead1be6vboxsync
03493c32c4a1ce2b083405efe6f3b070bead1be6vboxsync if (uPartitions & RT_BIT(partitions.aPartitions[i].uIndex))
03493c32c4a1ce2b083405efe6f3b070bead1be6vboxsync {
03493c32c4a1ce2b083405efe6f3b070bead1be6vboxsync if (fRelative)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef RT_OS_LINUX
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Refer to the correct partition and use offset 0. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *psz;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = RTStrAPrintf(&psz, "%s%u", rawdisk.c_str(),
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync partitions.aPartitions[i].uIndex);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(vrc))
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot create reference to individual partition %u, rc=%Rrc",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync partitions.aPartitions[i].uIndex, vrc);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync goto out;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pszRawName = psz;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uStartOffset = 0;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync#elif defined(RT_OS_DARWIN)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /* Refer to the correct partition and use offset 0. */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync char *psz;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync vrc = RTStrAPrintf(&psz, "%ss%u", rawdisk.c_str(),
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync partitions.aPartitions[i].uIndex);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (RT_FAILURE(vrc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("Cannot create reference to individual partition %u, rc=%Rrc",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync partitions.aPartitions[i].uIndex, vrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pszRawName = psz;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uStartOffset = 0;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync#else
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync /** @todo not implemented for other hosts. Treat just like
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync * not specified (this code is actually never reached). */
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync#endif
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPartDesc->pszRawDevice = pszRawName;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPartDesc->uStartOffset = uStartOffset;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPartDesc->pszRawDevice = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPartDesc->uStartOffset = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPartDesc->uStart = partitions.aPartitions[i].uStart * 512;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPartDesc->cbData = partitions.aPartitions[i].uSize * 512;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Sort data areas in ascending order of start. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned i = 0; i < RawDescriptor.cPartDescs-1; i++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uMinIdx = i;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t uMinVal = RawDescriptor.pPartDescs[i].uStart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned j = i + 1; j < RawDescriptor.cPartDescs; j++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RawDescriptor.pPartDescs[j].uStart < uMinVal)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
8e29dabca04cf55fde8755871d14abde99ef7236vboxsync uMinIdx = j;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uMinVal = RawDescriptor.pPartDescs[j].uStart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (uMinIdx != i)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Swap entries at index i and uMinIdx. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync VBOXHDDRAWPARTDESC tmp;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memcpy(&tmp, &RawDescriptor.pPartDescs[i], sizeof(tmp));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memcpy(&RawDescriptor.pPartDescs[i], &RawDescriptor.pPartDescs[uMinIdx], sizeof(tmp));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memcpy(&RawDescriptor.pPartDescs[uMinIdx], &tmp, sizeof(tmp));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Have a second go at MBR/EPT area clipping. Now that the data areas
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * are sorted this is much easier to get 100% right. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned i = 0; i < RawDescriptor.cPartDescs-1; i++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RawDescriptor.pPartDescs[i].pvPartitionData)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.pPartDescs[i].cbData = RT_MIN(RawDescriptor.pPartDescs[i+1].uStart - RawDescriptor.pPartDescs[i].uStart, RawDescriptor.pPartDescs[i].cbData);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!RawDescriptor.pPartDescs[i].cbData)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMsgError("MBR/EPT overlaps with data area");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFileClose(RawFile);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef DEBUG_klaus
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTPrintf("# start length startoffset partdataptr device\n");
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync for (unsigned i = 0; i < RawDescriptor.cPartDescs; i++)
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTPrintf("%2u %14RU64 %14RU64 %14RU64 %#18p %s\n", i,
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync RawDescriptor.pPartDescs[i].uStart,
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync RawDescriptor.pPartDescs[i].cbData,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.pPartDescs[i].uStartOffset,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RawDescriptor.pPartDescs[i].pvPartitionData,
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync RawDescriptor.pPartDescs[i].pszRawDevice);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync }
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync#endif
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync VDINTERFACE vdInterfaceError;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync VDINTERFACEERROR vdInterfaceErrorCallbacks;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdInterfaceErrorCallbacks.pfnError = handleVDError;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync &vdInterfaceErrorCallbacks, NULL, &pVDIfs);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync AssertRC(vrc);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync vrc = VDCreate(pVDIfs, &pDisk);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync if (RT_FAILURE(vrc))
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync {
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync RTMsgError("Cannot create the virtual disk container: %Rrc", vrc);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync goto out;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync }
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync Assert(RT_MIN(cbSize / 512 / 16 / 63, 16383) -
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync (unsigned int)RT_MIN(cbSize / 512 / 16 / 63, 16383) == 0);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync VDGEOMETRY PCHS, LCHS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PCHS.cCylinders = (unsigned int)RT_MIN(cbSize / 512 / 16 / 63, 16383);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PCHS.cHeads = 16;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PCHS.cSectors = 63;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LCHS.cCylinders = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LCHS.cHeads = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LCHS.cSectors = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vrc = VDCreateBase(pDisk, "VMDK", filename.c_str(), cbSize,
VD_IMAGE_FLAGS_FIXED | VD_VMDK_IMAGE_FLAGS_RAWDISK,
(char *)&RawDescriptor, &PCHS, &LCHS, NULL,
VD_OPEN_FLAGS_NORMAL, NULL, NULL);
if (RT_FAILURE(vrc))
{
RTMsgError("Cannot create the raw disk VMDK: %Rrc", vrc);
goto out;
}
RTPrintf("RAW host disk access VMDK file %s created successfully.\n", filename.c_str());
VDCloseAll(pDisk);
/* Clean up allocated memory etc. */
if (pszPartitions)
{
for (unsigned i = 0; i < RawDescriptor.cPartDescs; i++)
{
/* Free memory allocated for relative device name. */
if (fRelative && RawDescriptor.pPartDescs[i].pszRawDevice)
RTStrFree((char *)(void *)RawDescriptor.pPartDescs[i].pszRawDevice);
if (RawDescriptor.pPartDescs[i].pvPartitionData)
RTMemFree((void *)RawDescriptor.pPartDescs[i].pvPartitionData);
}
if (RawDescriptor.pPartDescs)
RTMemFree(RawDescriptor.pPartDescs);
}
if (fRegister)
{
ComPtr<IMedium> hardDisk;
CHECK_ERROR(aVirtualBox, OpenMedium(Bstr(filename), DeviceType_HardDisk, AccessMode_ReadWrite, hardDisk.asOutParam()));
}
return SUCCEEDED(rc) ? 0 : 1;
out:
RTMsgError("The raw disk vmdk file was not created");
return RT_SUCCESS(vrc) ? 0 : 1;
}
static int CmdRenameVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
Utf8Str src;
Utf8Str dst;
/* Parse the arguments. */
for (int i = 0; i < argc; i++)
{
if (strcmp(argv[i], "-from") == 0)
{
if (argc <= i + 1)
{
return errorArgument("Missing argument to '%s'", argv[i]);
}
i++;
src = argv[i];
}
else if (strcmp(argv[i], "-to") == 0)
{
if (argc <= i + 1)
{
return errorArgument("Missing argument to '%s'", argv[i]);
}
i++;
dst = argv[i];
}
else
{
return errorSyntax(USAGE_RENAMEVMDK, "Invalid parameter '%s'", argv[i]);
}
}
if (src.isEmpty())
return errorSyntax(USAGE_RENAMEVMDK, "Mandatory parameter -from missing");
if (dst.isEmpty())
return errorSyntax(USAGE_RENAMEVMDK, "Mandatory parameter -to missing");
PVBOXHDD pDisk = NULL;
PVDINTERFACE pVDIfs = NULL;
VDINTERFACE vdInterfaceError;
VDINTERFACEERROR vdInterfaceErrorCallbacks;
vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
vdInterfaceErrorCallbacks.pfnError = handleVDError;
vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
int vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
&vdInterfaceErrorCallbacks, NULL, &pVDIfs);
AssertRC(vrc);
vrc = VDCreate(pVDIfs, &pDisk);
if (RT_FAILURE(vrc))
{
RTMsgError("Cannot create the virtual disk container: %Rrc", vrc);
return vrc;
}
else
{
vrc = VDOpen(pDisk, "VMDK", src.c_str(), VD_OPEN_FLAGS_NORMAL, NULL);
if (RT_FAILURE(vrc))
{
RTMsgError("Cannot create the source image: %Rrc", vrc);
}
else
{
vrc = VDCopy(pDisk, 0, pDisk, "VMDK", dst.c_str(), true, 0, VD_IMAGE_FLAGS_NONE, NULL, NULL, NULL, NULL);
if (RT_FAILURE(vrc))
{
RTMsgError("Cannot rename the image: %Rrc", vrc);
}
}
}
VDCloseAll(pDisk);
return vrc;
}
static int CmdConvertToRaw(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
Utf8Str srcformat;
Utf8Str src;
Utf8Str dst;
bool fWriteToStdOut = false;
/* Parse the arguments. */
for (int i = 0; i < argc; i++)
{
if (strcmp(argv[i], "-format") == 0)
{
if (argc <= i + 1)
{
return errorArgument("Missing argument to '%s'", argv[i]);
}
i++;
srcformat = argv[i];
}
else if (src.isEmpty())
{
src = argv[i];
}
else if (dst.isEmpty())
{
dst = argv[i];
#ifdef ENABLE_CONVERT_RAW_TO_STDOUT
if (!strcmp(argv[i], "stdout"))
fWriteToStdOut = true;
#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
}
else
{
return errorSyntax(USAGE_CONVERTTORAW, "Invalid parameter '%s'", argv[i]);
}
}
if (src.isEmpty())
return errorSyntax(USAGE_CONVERTTORAW, "Mandatory filename parameter missing");
if (dst.isEmpty())
return errorSyntax(USAGE_CONVERTTORAW, "Mandatory outputfile parameter missing");
PVBOXHDD pDisk = NULL;
PVDINTERFACE pVDIfs = NULL;
VDINTERFACE vdInterfaceError;
VDINTERFACEERROR vdInterfaceErrorCallbacks;
vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
vdInterfaceErrorCallbacks.pfnError = handleVDError;
vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
int vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
&vdInterfaceErrorCallbacks, NULL, &pVDIfs);
AssertRC(vrc);
vrc = VDCreate(pVDIfs, &pDisk);
if (RT_FAILURE(vrc))
{
RTMsgError("Cannot create the virtual disk container: %Rrc", vrc);
return 1;
}
/* Open raw output file. */
RTFILE outFile;
vrc = VINF_SUCCESS;
if (fWriteToStdOut)
outFile = 1;
else
vrc = RTFileOpen(&outFile, dst.c_str(), RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
if (RT_FAILURE(vrc))
{
VDCloseAll(pDisk);
RTMsgError("Cannot create destination file \"%s\": %Rrc", dst.c_str(), vrc);
return 1;
}
if (srcformat.isEmpty())
{
char *pszFormat = NULL;
vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
src.c_str(), &pszFormat);
if (RT_FAILURE(vrc))
{
VDCloseAll(pDisk);
if (!fWriteToStdOut)
{
RTFileClose(outFile);
RTFileDelete(dst.c_str());
}
RTMsgError("No file format specified and autodetect failed - please specify format: %Rrc", vrc);
return 1;
}
srcformat = pszFormat;
RTStrFree(pszFormat);
}
vrc = VDOpen(pDisk, srcformat.c_str(), src.c_str(), VD_OPEN_FLAGS_READONLY, NULL);
if (RT_FAILURE(vrc))
{
VDCloseAll(pDisk);
if (!fWriteToStdOut)
{
RTFileClose(outFile);
RTFileDelete(dst.c_str());
}
RTMsgError("Cannot open the source image: %Rrc", vrc);
return 1;
}
uint64_t cbSize = VDGetSize(pDisk, VD_LAST_IMAGE);
uint64_t offFile = 0;
#define RAW_BUFFER_SIZE _128K
size_t cbBuf = RAW_BUFFER_SIZE;
void *pvBuf = RTMemAlloc(cbBuf);
if (pvBuf)
{
RTStrmPrintf(g_pStdErr, "Converting image \"%s\" with size %RU64 bytes (%RU64MB) to raw...\n", src.c_str(), cbSize, (cbSize + _1M - 1) / _1M);
while (offFile < cbSize)
{
size_t cb = (size_t)RT_MIN(cbSize - offFile, cbBuf);
vrc = VDRead(pDisk, offFile, pvBuf, cb);
if (RT_FAILURE(vrc))
break;
vrc = RTFileWrite(outFile, pvBuf, cb, NULL);
if (RT_FAILURE(vrc))
break;
offFile += cb;
}
if (RT_FAILURE(vrc))
{
VDCloseAll(pDisk);
if (!fWriteToStdOut)
{
RTFileClose(outFile);
RTFileDelete(dst.c_str());
}
RTMsgError("Cannot copy image data: %Rrc", vrc);
return 1;
}
}
else
{
vrc = VERR_NO_MEMORY;
VDCloseAll(pDisk);
if (!fWriteToStdOut)
{
RTFileClose(outFile);
RTFileDelete(dst.c_str());
}
RTMsgError("Out of memory allocating read buffer");
return 1;
}
if (!fWriteToStdOut)
RTFileClose(outFile);
VDCloseAll(pDisk);
return 0;
}
static int CmdConvertHardDisk(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
Utf8Str srcformat;
Utf8Str dstformat;
Utf8Str src;
Utf8Str dst;
int vrc;
PVBOXHDD pSrcDisk = NULL;
PVBOXHDD pDstDisk = NULL;
/* Parse the arguments. */
for (int i = 0; i < argc; i++)
{
if (strcmp(argv[i], "-srcformat") == 0)
{
if (argc <= i + 1)
{
return errorArgument("Missing argument to '%s'", argv[i]);
}
i++;
srcformat = argv[i];
}
else if (strcmp(argv[i], "-dstformat") == 0)
{
if (argc <= i + 1)
{
return errorArgument("Missing argument to '%s'", argv[i]);
}
i++;
dstformat = argv[i];
}
else if (src.isEmpty())
{
src = argv[i];
}
else if (dst.isEmpty())
{
dst = argv[i];
}
else
{
return errorSyntax(USAGE_CONVERTHD, "Invalid parameter '%s'", argv[i]);
}
}
if (src.isEmpty())
return errorSyntax(USAGE_CONVERTHD, "Mandatory input image parameter missing");
if (dst.isEmpty())
return errorSyntax(USAGE_CONVERTHD, "Mandatory output image parameter missing");
PVDINTERFACE pVDIfs = NULL;
VDINTERFACE vdInterfaceError;
VDINTERFACEERROR vdInterfaceErrorCallbacks;
vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
vdInterfaceErrorCallbacks.pfnError = handleVDError;
vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
&vdInterfaceErrorCallbacks, NULL, &pVDIfs);
AssertRC(vrc);
do
{
/* Try to determine input image format */
if (srcformat.isEmpty())
{
char *pszFormat = NULL;
vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
src.c_str(), &pszFormat);
if (RT_FAILURE(vrc))
{
RTMsgError("No file format specified and autodetect failed - please specify format: %Rrc", vrc);
break;
}
srcformat = pszFormat;
RTStrFree(pszFormat);
}
vrc = VDCreate(pVDIfs, &pSrcDisk);
if (RT_FAILURE(vrc))
{
RTMsgError("Cannot create the source virtual disk container: %Rrc", vrc);
break;
}
/* Open the input image */
vrc = VDOpen(pSrcDisk, srcformat.c_str(), src.c_str(), VD_OPEN_FLAGS_READONLY, NULL);
if (RT_FAILURE(vrc))
{
RTMsgError("Cannot open the source image: %Rrc", vrc);
break;
}
/* Output format defaults to VDI */
if (dstformat.isEmpty())
dstformat = "VDI";
vrc = VDCreate(pVDIfs, &pDstDisk);
if (RT_FAILURE(vrc))
{
RTMsgError("Cannot create the destination virtual disk container: %Rrc", vrc);
break;
}
uint64_t cbSize = VDGetSize(pSrcDisk, VD_LAST_IMAGE);
RTStrmPrintf(g_pStdErr, "Converting image \"%s\" with size %RU64 bytes (%RU64MB)...\n", src.c_str(), cbSize, (cbSize + _1M - 1) / _1M);
/* Create the output image */
vrc = VDCopy(pSrcDisk, VD_LAST_IMAGE, pDstDisk, dstformat.c_str(),
dst.c_str(), false, 0, VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED,
NULL, NULL, NULL, NULL);
if (RT_FAILURE(vrc))
{
RTMsgError("Cannot copy the image: %Rrc", vrc);
break;
}
}
while (0);
if (pDstDisk)
VDCloseAll(pDstDisk);
if (pSrcDisk)
VDCloseAll(pSrcDisk);
return RT_SUCCESS(vrc) ? 0 : 1;
}
/**
* Unloads the neccessary driver.
*
* @returns VBox status code
*/
int CmdModUninstall(void)
{
int rc;
rc = SUPR3Uninstall();
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 = SUPR3Install();
if (RT_SUCCESS(rc))
return 0;
if (rc == VERR_NOT_IMPLEMENTED)
return 0;
return E_FAIL;
}
int CmdDebugLog(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
{
/*
* The first parameter is the name or UUID of a VM with a direct session
* that we wish to open.
*/
if (argc < 1)
return errorSyntax(USAGE_DEBUGLOG, "Missing VM name/UUID");
ComPtr<IMachine> ptrMachine;
HRESULT rc = aVirtualBox->GetMachine(Bstr(argv[0]), ptrMachine.asOutParam());
if (FAILED(rc) || ptrMachine.isNull())
CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]), ptrMachine.asOutParam()), 1);
CHECK_ERROR_RET(ptrMachine, LockMachine(aSession, LockType_Shared), 1);
/*
* Get the debugger interface.
*/
ComPtr<IConsole> ptrConsole;
CHECK_ERROR_RET(aSession, COMGETTER(Console)(ptrConsole.asOutParam()), 1);
ComPtr<IMachineDebugger> ptrDebugger;
CHECK_ERROR_RET(ptrConsole, COMGETTER(Debugger)(ptrDebugger.asOutParam()), 1);
/*
* Parse the command.
*/
bool fEnablePresent = false;
bool fEnable = false;
bool fFlagsPresent = false;
iprt::MiniString strFlags;
bool fGroupsPresent = false;
iprt::MiniString strGroups;
bool fDestsPresent = false;
iprt::MiniString strDests;
static const RTGETOPTDEF s_aOptions[] =
{
{ "--disable", 'E', RTGETOPT_REQ_NOTHING },
{ "--enable", 'e', RTGETOPT_REQ_NOTHING },
{ "--flags", 'f', RTGETOPT_REQ_STRING },
{ "--groups", 'g', RTGETOPT_REQ_STRING },
{ "--destinations", 'd', RTGETOPT_REQ_STRING }
};
int ch;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
while ((ch = RTGetOpt(&GetState, &ValueUnion)))
{
switch (ch)
{
case 'e':
fEnablePresent = true;
fEnable = true;
break;
case 'E':
fEnablePresent = true;
fEnable = false;
break;
case 'f':
fFlagsPresent = true;
if (*ValueUnion.psz)
{
if (strFlags.isNotEmpty())
strFlags.append(' ');
strFlags.append(ValueUnion.psz);
}
break;
case 'g':
fGroupsPresent = true;
if (*ValueUnion.psz)
{
if (strGroups.isNotEmpty())
strGroups.append(' ');
strGroups.append(ValueUnion.psz);
}
break;
case 'd':
fDestsPresent = true;
if (*ValueUnion.psz)
{
if (strDests.isNotEmpty())
strDests.append(' ');
strDests.append(ValueUnion.psz);
}
break;
default:
return errorGetOpt(USAGE_DEBUGLOG , ch, &ValueUnion);
}
}
/*
* Do the job.
*/
if (fEnablePresent && !fEnable)
CHECK_ERROR_RET(ptrDebugger, COMSETTER(LogEnabled)(FALSE), 1);
/** @todo flags, groups destination. */
if (fFlagsPresent || fGroupsPresent || fDestsPresent)
RTMsgWarning("One or more of the requested features are not implemented! Feel free to do this.");
if (fEnablePresent && fEnable)
CHECK_ERROR_RET(ptrDebugger, COMSETTER(LogEnabled)(TRUE), 1);
return 0;
}
/**
* Wrapper for handling internal commands
*/
int handleInternalCommands(HandlerArg *a)
{
g_fInternalMode = true;
/* at least a command is required */
if (a->argc < 1)
return errorSyntax(USAGE_ALL, "Command missing");
/*
* The 'string switch' on command name.
*/
const char *pszCmd = a->argv[0];
if (!strcmp(pszCmd, "loadsyms"))
return CmdLoadSyms(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
//if (!strcmp(pszCmd, "unloadsyms"))
// return CmdUnloadSyms(argc - 1 , &a->argv[1]);
if (!strcmp(pszCmd, "sethduuid") || !strcmp(pszCmd, "sethdparentuuid"))
return CmdSetHDUUID(a->argc, &a->argv[0], a->virtualBox, a->session);
if (!strcmp(pszCmd, "dumphdinfo"))
return CmdDumpHDInfo(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
if (!strcmp(pszCmd, "listpartitions"))
return CmdListPartitions(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
if (!strcmp(pszCmd, "createrawvmdk"))
return CmdCreateRawVMDK(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
if (!strcmp(pszCmd, "renamevmdk"))
return CmdRenameVMDK(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
if (!strcmp(pszCmd, "converttoraw"))
return CmdConvertToRaw(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
if (!strcmp(pszCmd, "converthd"))
return CmdConvertHardDisk(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
if (!strcmp(pszCmd, "modinstall"))
return CmdModInstall();
if (!strcmp(pszCmd, "moduninstall"))
return CmdModUninstall();
if (!strcmp(pszCmd, "debuglog"))
return CmdDebugLog(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
/* default: */
return errorSyntax(USAGE_ALL, "Invalid command '%s'", a->argv[0]);
}