[14831] | 1 | /* $Id: VBoxInternalManage.cpp 35238 2010-12-20 11:18:40Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[14732] | 3 | * VBoxManage - The 'internalcommands' command.
|
---|
[1] | 4 | *
|
---|
| 5 | * VBoxInternalManage used to be a second CLI for doing special tricks,
|
---|
| 6 | * not intended for general usage, only for assisting VBox developers.
|
---|
| 7 | * It is now integrated into VBoxManage.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /*
|
---|
[28800] | 11 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
[1] | 12 | *
|
---|
| 13 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 14 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 15 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
[5999] | 16 | * General Public License (GPL) as published by the Free Software
|
---|
| 17 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 18 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 19 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[1] | 20 | */
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | /*******************************************************************************
|
---|
| 25 | * Header Files *
|
---|
| 26 | *******************************************************************************/
|
---|
| 27 | #include <VBox/com/com.h>
|
---|
| 28 | #include <VBox/com/string.h>
|
---|
| 29 | #include <VBox/com/Guid.h>
|
---|
| 30 | #include <VBox/com/ErrorInfo.h>
|
---|
[20928] | 31 | #include <VBox/com/errorprint.h>
|
---|
[1] | 32 |
|
---|
| 33 | #include <VBox/com/VirtualBox.h>
|
---|
| 34 |
|
---|
[33567] | 35 | #include <VBox/vd.h>
|
---|
[14831] | 36 | #include <VBox/sup.h>
|
---|
| 37 | #include <VBox/err.h>
|
---|
| 38 | #include <VBox/log.h>
|
---|
| 39 |
|
---|
| 40 | #include <iprt/file.h>
|
---|
[30319] | 41 | #include <iprt/getopt.h>
|
---|
[1] | 42 | #include <iprt/stream.h>
|
---|
| 43 | #include <iprt/string.h>
|
---|
| 44 | #include <iprt/uuid.h>
|
---|
[33228] | 45 | #include <iprt/sha.h>
|
---|
[1] | 46 |
|
---|
| 47 | #include "VBoxManage.h"
|
---|
| 48 |
|
---|
[8230] | 49 | /* Includes for the raw disk stuff. */
|
---|
| 50 | #ifdef RT_OS_WINDOWS
|
---|
[14732] | 51 | # include <windows.h>
|
---|
| 52 | # include <winioctl.h>
|
---|
[27133] | 53 | #elif defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) \
|
---|
| 54 | || defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
[14732] | 55 | # include <errno.h>
|
---|
| 56 | # include <sys/ioctl.h>
|
---|
| 57 | # include <sys/types.h>
|
---|
| 58 | # include <sys/stat.h>
|
---|
| 59 | # include <fcntl.h>
|
---|
| 60 | # include <unistd.h>
|
---|
[8811] | 61 | #endif
|
---|
| 62 | #ifdef RT_OS_LINUX
|
---|
[14732] | 63 | # include <sys/utsname.h>
|
---|
| 64 | # include <linux/hdreg.h>
|
---|
| 65 | # include <linux/fs.h>
|
---|
[14838] | 66 | # include <stdlib.h> /* atoi() */
|
---|
[8811] | 67 | #endif /* RT_OS_LINUX */
|
---|
| 68 | #ifdef RT_OS_DARWIN
|
---|
[14732] | 69 | # include <sys/disk.h>
|
---|
[8811] | 70 | #endif /* RT_OS_DARWIN */
|
---|
[8835] | 71 | #ifdef RT_OS_SOLARIS
|
---|
[14732] | 72 | # include <stropts.h>
|
---|
| 73 | # include <sys/dkio.h>
|
---|
| 74 | # include <sys/vtoc.h>
|
---|
[8835] | 75 | #endif /* RT_OS_SOLARIS */
|
---|
[27133] | 76 | #ifdef RT_OS_FREEBSD
|
---|
| 77 | # include <sys/disk.h>
|
---|
| 78 | #endif /* RT_OS_FREEBSD */
|
---|
[2675] | 79 |
|
---|
[1] | 80 | using namespace com;
|
---|
| 81 |
|
---|
[8230] | 82 |
|
---|
| 83 | /** Macro for checking whether a partition is of extended type or not. */
|
---|
| 84 | #define PARTTYPE_IS_EXTENDED(x) ((x) == 0x05 || (x) == 0x0f || (x) == 0x85)
|
---|
| 85 |
|
---|
[30319] | 86 | /** Maximum number of partitions we can deal with.
|
---|
| 87 | * Ridiculously large number, but the memory consumption is rather low so who
|
---|
| 88 | * cares about never using most entries. */
|
---|
[8230] | 89 | #define HOSTPARTITION_MAX 100
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | typedef struct HOSTPARTITION
|
---|
| 93 | {
|
---|
| 94 | unsigned uIndex;
|
---|
[18164] | 95 | /** partition type */
|
---|
[8230] | 96 | unsigned uType;
|
---|
[18164] | 97 | /** CHS/cylinder of the first sector */
|
---|
[8230] | 98 | unsigned uStartCylinder;
|
---|
[18164] | 99 | /** CHS/head of the first sector */
|
---|
[8230] | 100 | unsigned uStartHead;
|
---|
[18164] | 101 | /** CHS/head of the first sector */
|
---|
[8230] | 102 | unsigned uStartSector;
|
---|
[18164] | 103 | /** CHS/cylinder of the last sector */
|
---|
[8230] | 104 | unsigned uEndCylinder;
|
---|
[18164] | 105 | /** CHS/head of the last sector */
|
---|
[8230] | 106 | unsigned uEndHead;
|
---|
[18164] | 107 | /** CHS/sector of the last sector */
|
---|
[8230] | 108 | unsigned uEndSector;
|
---|
[18164] | 109 | /** start sector of this partition relative to the beginning of the hard
|
---|
| 110 | * disk or relative to the beginning of the extended partition table */
|
---|
[8230] | 111 | uint64_t uStart;
|
---|
[18164] | 112 | /** numer of sectors of the partition */
|
---|
[8230] | 113 | uint64_t uSize;
|
---|
[18164] | 114 | /** start sector of this partition _table_ */
|
---|
[8230] | 115 | uint64_t uPartDataStart;
|
---|
[18164] | 116 | /** numer of sectors of this partition _table_ */
|
---|
[8230] | 117 | uint64_t cPartDataSectors;
|
---|
| 118 | } HOSTPARTITION, *PHOSTPARTITION;
|
---|
| 119 |
|
---|
| 120 | typedef struct HOSTPARTITIONS
|
---|
| 121 | {
|
---|
| 122 | unsigned cPartitions;
|
---|
| 123 | HOSTPARTITION aPartitions[HOSTPARTITION_MAX];
|
---|
| 124 | } HOSTPARTITIONS, *PHOSTPARTITIONS;
|
---|
| 125 |
|
---|
[1] | 126 | /** flag whether we're in internal mode */
|
---|
[5204] | 127 | bool g_fInternalMode;
|
---|
[1] | 128 |
|
---|
| 129 | /**
|
---|
| 130 | * Print the usage info.
|
---|
| 131 | */
|
---|
[32709] | 132 | void printUsageInternal(USAGECATEGORY u64Cmd, PRTSTREAM pStrm)
|
---|
[1] | 133 | {
|
---|
[32709] | 134 | RTStrmPrintf(pStrm,
|
---|
[32701] | 135 | "Usage: VBoxManage internalcommands <command> [command arguments]\n"
|
---|
| 136 | "\n"
|
---|
| 137 | "Commands:\n"
|
---|
| 138 | "\n"
|
---|
[33228] | 139 | "%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
|
---|
[32701] | 140 | "WARNING: This is a development tool and shall only be used to analyse\n"
|
---|
| 141 | " problems. It is completely unsupported and will change in\n"
|
---|
| 142 | " incompatible ways without warning.\n",
|
---|
[30319] | 143 |
|
---|
[32701] | 144 | (u64Cmd & USAGE_LOADSYMS)
|
---|
| 145 | ? " loadsyms <vmname>|<uuid> <symfile> [delta] [module] [module address]\n"
|
---|
| 146 | " This will instruct DBGF to load the given symbolfile\n"
|
---|
| 147 | " during initialization.\n"
|
---|
| 148 | "\n"
|
---|
| 149 | : "",
|
---|
| 150 | (u64Cmd & USAGE_UNLOADSYMS)
|
---|
| 151 | ? " unloadsyms <vmname>|<uuid> <symfile>\n"
|
---|
| 152 | " Removes <symfile> from the list of symbol files that\n"
|
---|
| 153 | " should be loaded during DBF initialization.\n"
|
---|
| 154 | "\n"
|
---|
| 155 | : "",
|
---|
| 156 | (u64Cmd & USAGE_SETHDUUID)
|
---|
| 157 | ? " sethduuid <filepath> [<uuid>]\n"
|
---|
| 158 | " Assigns a new UUID to the given image file. This way, multiple copies\n"
|
---|
| 159 | " of a container can be registered.\n"
|
---|
| 160 | "\n"
|
---|
| 161 | : "",
|
---|
| 162 | (u64Cmd & USAGE_SETHDPARENTUUID)
|
---|
| 163 | ? " sethdparentuuid <filepath> <uuid>\n"
|
---|
| 164 | " Assigns a new parent UUID to the given image file.\n"
|
---|
| 165 | "\n"
|
---|
| 166 | : "",
|
---|
| 167 | (u64Cmd & USAGE_DUMPHDINFO)
|
---|
| 168 | ? " dumphdinfo <filepath>\n"
|
---|
| 169 | " Prints information about the image at the given location.\n"
|
---|
| 170 | "\n"
|
---|
| 171 | : "",
|
---|
| 172 | (u64Cmd & USAGE_LISTPARTITIONS)
|
---|
| 173 | ? " listpartitions -rawdisk <diskname>\n"
|
---|
| 174 | " Lists all partitions on <diskname>.\n"
|
---|
| 175 | "\n"
|
---|
| 176 | : "",
|
---|
| 177 | (u64Cmd & USAGE_CREATERAWVMDK)
|
---|
| 178 | ? " createrawvmdk -filename <filename> -rawdisk <diskname>\n"
|
---|
| 179 | " [-partitions <list of partition numbers> [-mbr <filename>] ]\n"
|
---|
[35238] | 180 | " [-relative]\n"
|
---|
[32701] | 181 | " Creates a new VMDK image which gives access to an entite host disk (if\n"
|
---|
| 182 | " the parameter -partitions is not specified) or some partitions of a\n"
|
---|
| 183 | " host disk. If access to individual partitions is granted, then the\n"
|
---|
| 184 | " parameter -mbr can be used to specify an alternative MBR to be used\n"
|
---|
| 185 | " (the partitioning information in the MBR file is ignored).\n"
|
---|
| 186 | " The diskname is on Linux e.g. /dev/sda, and on Windows e.g.\n"
|
---|
| 187 | " \\\\.\\PhysicalDrive0).\n"
|
---|
| 188 | " On Linux host the parameter -relative causes a VMDK file to be created\n"
|
---|
| 189 | " which refers to individual partitions instead to the entire disk.\n"
|
---|
| 190 | " The necessary partition numbers can be queried with\n"
|
---|
| 191 | " VBoxManage internalcommands listpartitions\n"
|
---|
| 192 | "\n"
|
---|
| 193 | : "",
|
---|
| 194 | (u64Cmd & USAGE_RENAMEVMDK)
|
---|
| 195 | ? " renamevmdk -from <filename> -to <filename>\n"
|
---|
| 196 | " Renames an existing VMDK image, including the base file and all its extents.\n"
|
---|
| 197 | "\n"
|
---|
| 198 | : "",
|
---|
| 199 | (u64Cmd & USAGE_CONVERTTORAW)
|
---|
| 200 | ? " converttoraw [-format <fileformat>] <filename> <outputfile>"
|
---|
[11066] | 201 | #ifdef ENABLE_CONVERT_RAW_TO_STDOUT
|
---|
[32701] | 202 | "|stdout"
|
---|
[11066] | 203 | #endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
|
---|
[32701] | 204 | "\n"
|
---|
| 205 | " Convert image to raw, writing to file"
|
---|
[11066] | 206 | #ifdef ENABLE_CONVERT_RAW_TO_STDOUT
|
---|
[32701] | 207 | " or stdout"
|
---|
[11066] | 208 | #endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
|
---|
[32701] | 209 | ".\n"
|
---|
| 210 | "\n"
|
---|
| 211 | : "",
|
---|
| 212 | (u64Cmd & USAGE_CONVERTHD)
|
---|
| 213 | ? " converthd [-srcformat VDI|VMDK|VHD|RAW]\n"
|
---|
| 214 | " [-dstformat VDI|VMDK|VHD|RAW]\n"
|
---|
| 215 | " <inputfile> <outputfile>\n"
|
---|
| 216 | " converts hard disk images between formats\n"
|
---|
| 217 | "\n"
|
---|
| 218 | : "",
|
---|
[5896] | 219 | #ifdef RT_OS_WINDOWS
|
---|
[32701] | 220 | (u64Cmd & USAGE_MODINSTALL)
|
---|
| 221 | ? " modinstall\n"
|
---|
[33550] | 222 | " Installs the necessary driver for the host OS\n"
|
---|
[32701] | 223 | "\n"
|
---|
| 224 | : "",
|
---|
| 225 | (u64Cmd & USAGE_MODUNINSTALL)
|
---|
| 226 | ? " moduninstall\n"
|
---|
| 227 | " Deinstalls the driver\n"
|
---|
| 228 | "\n"
|
---|
| 229 | : "",
|
---|
[5896] | 230 | #else
|
---|
[32701] | 231 | "",
|
---|
| 232 | "",
|
---|
[5896] | 233 | #endif
|
---|
[32701] | 234 | (u64Cmd & USAGE_DEBUGLOG)
|
---|
| 235 | ? " debuglog <vmname>|<uuid> [--enable|--disable] [--flags todo]\n"
|
---|
| 236 | " [--groups todo] [--destinations todo]\n"
|
---|
| 237 | " Controls debug logging.\n"
|
---|
| 238 | "\n"
|
---|
[33228] | 239 | : "",
|
---|
| 240 | (u64Cmd & USAGE_PASSWORDHASH)
|
---|
| 241 | ? " passwordhash <passsword>\n"
|
---|
| 242 | " Generates a password hash.\n"
|
---|
| 243 | "\n"
|
---|
| 244 | :
|
---|
| 245 | ""
|
---|
[32701] | 246 | );
|
---|
[1] | 247 | }
|
---|
| 248 |
|
---|
| 249 | /** @todo this is no longer necessary, we can enumerate extra data */
|
---|
| 250 | /**
|
---|
| 251 | * Finds a new unique key name.
|
---|
| 252 | *
|
---|
| 253 | * I don't think this is 100% race condition proof, but we assumes
|
---|
| 254 | * the user is not trying to push this point.
|
---|
| 255 | *
|
---|
| 256 | * @returns Result from the insert.
|
---|
| 257 | * @param pMachine The Machine object.
|
---|
| 258 | * @param pszKeyBase The base key.
|
---|
| 259 | * @param rKey Reference to the string object in which we will return the key.
|
---|
| 260 | */
|
---|
| 261 | static HRESULT NewUniqueKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, Utf8Str &rKey)
|
---|
| 262 | {
|
---|
[32718] | 263 | Bstr KeyBase(pszKeyBase);
|
---|
[1] | 264 | Bstr Keys;
|
---|
[32718] | 265 | HRESULT hrc = pMachine->GetExtraData(KeyBase.raw(), Keys.asOutParam());
|
---|
[1] | 266 | if (FAILED(hrc))
|
---|
| 267 | return hrc;
|
---|
| 268 |
|
---|
| 269 | /* if there are no keys, it's simple. */
|
---|
| 270 | if (Keys.isEmpty())
|
---|
| 271 | {
|
---|
| 272 | rKey = "1";
|
---|
[32718] | 273 | return pMachine->SetExtraData(KeyBase.raw(), Bstr(rKey).raw());
|
---|
[1] | 274 | }
|
---|
| 275 |
|
---|
| 276 | /* find a unique number - brute force rulez. */
|
---|
| 277 | Utf8Str KeysUtf8(Keys);
|
---|
[31539] | 278 | const char *pszKeys = RTStrStripL(KeysUtf8.c_str());
|
---|
[1] | 279 | for (unsigned i = 1; i < 1000000; i++)
|
---|
| 280 | {
|
---|
| 281 | char szKey[32];
|
---|
| 282 | size_t cchKey = RTStrPrintf(szKey, sizeof(szKey), "%#x", i);
|
---|
| 283 | const char *psz = strstr(pszKeys, szKey);
|
---|
| 284 | while (psz)
|
---|
| 285 | {
|
---|
| 286 | if ( ( psz == pszKeys
|
---|
| 287 | || psz[-1] == ' ')
|
---|
| 288 | && ( psz[cchKey] == ' '
|
---|
| 289 | || !psz[cchKey])
|
---|
| 290 | )
|
---|
| 291 | break;
|
---|
| 292 | psz = strstr(psz + cchKey, szKey);
|
---|
| 293 | }
|
---|
| 294 | if (!psz)
|
---|
| 295 | {
|
---|
| 296 | rKey = szKey;
|
---|
| 297 | Utf8StrFmt NewKeysUtf8("%s %s", pszKeys, szKey);
|
---|
[32718] | 298 | return pMachine->SetExtraData(KeyBase.raw(),
|
---|
| 299 | Bstr(NewKeysUtf8).raw());
|
---|
[1] | 300 | }
|
---|
| 301 | }
|
---|
[32701] | 302 | RTMsgError("Cannot find unique key for '%s'!", pszKeyBase);
|
---|
[1] | 303 | return E_FAIL;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 | #if 0
|
---|
| 308 | /**
|
---|
| 309 | * Remove a key.
|
---|
| 310 | *
|
---|
| 311 | * I don't think this isn't 100% race condition proof, but we assumes
|
---|
| 312 | * the user is not trying to push this point.
|
---|
| 313 | *
|
---|
| 314 | * @returns Result from the insert.
|
---|
| 315 | * @param pMachine The machine object.
|
---|
| 316 | * @param pszKeyBase The base key.
|
---|
| 317 | * @param pszKey The key to remove.
|
---|
| 318 | */
|
---|
| 319 | static HRESULT RemoveKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey)
|
---|
| 320 | {
|
---|
| 321 | Bstr Keys;
|
---|
| 322 | HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
|
---|
| 323 | if (FAILED(hrc))
|
---|
| 324 | return hrc;
|
---|
| 325 |
|
---|
| 326 | /* if there are no keys, it's simple. */
|
---|
| 327 | if (Keys.isEmpty())
|
---|
| 328 | return S_OK;
|
---|
| 329 |
|
---|
| 330 | char *pszKeys;
|
---|
[7418] | 331 | int rc = RTUtf16ToUtf8(Keys.raw(), &pszKeys);
|
---|
| 332 | if (RT_SUCCESS(rc))
|
---|
[1] | 333 | {
|
---|
| 334 | /* locate it */
|
---|
| 335 | size_t cchKey = strlen(pszKey);
|
---|
| 336 | char *psz = strstr(pszKeys, pszKey);
|
---|
| 337 | while (psz)
|
---|
| 338 | {
|
---|
| 339 | if ( ( psz == pszKeys
|
---|
| 340 | || psz[-1] == ' ')
|
---|
| 341 | && ( psz[cchKey] == ' '
|
---|
| 342 | || !psz[cchKey])
|
---|
| 343 | )
|
---|
| 344 | break;
|
---|
| 345 | psz = strstr(psz + cchKey, pszKey);
|
---|
| 346 | }
|
---|
| 347 | if (psz)
|
---|
| 348 | {
|
---|
| 349 | /* remove it */
|
---|
| 350 | char *pszNext = RTStrStripL(psz + cchKey);
|
---|
| 351 | if (*pszNext)
|
---|
| 352 | memmove(psz, pszNext, strlen(pszNext) + 1);
|
---|
| 353 | else
|
---|
| 354 | *psz = '\0';
|
---|
| 355 | psz = RTStrStrip(pszKeys);
|
---|
| 356 |
|
---|
| 357 | /* update */
|
---|
| 358 | hrc = pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(psz));
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | RTStrFree(pszKeys);
|
---|
| 362 | return hrc;
|
---|
| 363 | }
|
---|
| 364 | else
|
---|
[32701] | 365 | RTMsgError("Failed to delete key '%s' from '%s', string conversion error %Rrc!",
|
---|
| 366 | pszKey, pszKeyBase, rc);
|
---|
[1] | 367 |
|
---|
| 368 | return E_FAIL;
|
---|
| 369 | }
|
---|
| 370 | #endif
|
---|
| 371 |
|
---|
| 372 |
|
---|
| 373 | /**
|
---|
| 374 | * Sets a key value, does necessary error bitching.
|
---|
| 375 | *
|
---|
| 376 | * @returns COM status code.
|
---|
| 377 | * @param pMachine The Machine object.
|
---|
| 378 | * @param pszKeyBase The key base.
|
---|
| 379 | * @param pszKey The key.
|
---|
| 380 | * @param pszAttribute The attribute name.
|
---|
| 381 | * @param pszValue The string value.
|
---|
| 382 | */
|
---|
| 383 | static HRESULT SetString(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, const char *pszValue)
|
---|
| 384 | {
|
---|
[32718] | 385 | HRESULT hrc = pMachine->SetExtraData(BstrFmt("%s/%s/%s", pszKeyBase,
|
---|
| 386 | pszKey, pszAttribute).raw(),
|
---|
| 387 | Bstr(pszValue).raw());
|
---|
[1] | 388 | if (FAILED(hrc))
|
---|
[32701] | 389 | RTMsgError("Failed to set '%s/%s/%s' to '%s'! hrc=%#x",
|
---|
| 390 | pszKeyBase, pszKey, pszAttribute, pszValue, hrc);
|
---|
[1] | 391 | return hrc;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 |
|
---|
| 395 | /**
|
---|
| 396 | * Sets a key value, does necessary error bitching.
|
---|
| 397 | *
|
---|
| 398 | * @returns COM status code.
|
---|
| 399 | * @param pMachine The Machine object.
|
---|
| 400 | * @param pszKeyBase The key base.
|
---|
| 401 | * @param pszKey The key.
|
---|
| 402 | * @param pszAttribute The attribute name.
|
---|
| 403 | * @param u64Value The value.
|
---|
| 404 | */
|
---|
| 405 | static HRESULT SetUInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, uint64_t u64Value)
|
---|
| 406 | {
|
---|
| 407 | char szValue[64];
|
---|
| 408 | RTStrPrintf(szValue, sizeof(szValue), "%#RX64", u64Value);
|
---|
| 409 | return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 |
|
---|
| 413 | /**
|
---|
| 414 | * Sets a key value, does necessary error bitching.
|
---|
| 415 | *
|
---|
| 416 | * @returns COM status code.
|
---|
| 417 | * @param pMachine The Machine object.
|
---|
| 418 | * @param pszKeyBase The key base.
|
---|
| 419 | * @param pszKey The key.
|
---|
| 420 | * @param pszAttribute The attribute name.
|
---|
| 421 | * @param i64Value The value.
|
---|
| 422 | */
|
---|
| 423 | static HRESULT SetInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, int64_t i64Value)
|
---|
| 424 | {
|
---|
| 425 | char szValue[64];
|
---|
| 426 | RTStrPrintf(szValue, sizeof(szValue), "%RI64", i64Value);
|
---|
| 427 | return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 |
|
---|
| 431 | /**
|
---|
| 432 | * Identical to the 'loadsyms' command.
|
---|
| 433 | */
|
---|
| 434 | static int CmdLoadSyms(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
| 435 | {
|
---|
| 436 | HRESULT rc;
|
---|
| 437 |
|
---|
| 438 | /*
|
---|
| 439 | * Get the VM
|
---|
| 440 | */
|
---|
| 441 | ComPtr<IMachine> machine;
|
---|
[33294] | 442 | CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]).raw(),
|
---|
| 443 | machine.asOutParam()), 1);
|
---|
[1] | 444 |
|
---|
| 445 | /*
|
---|
| 446 | * Parse the command.
|
---|
| 447 | */
|
---|
| 448 | const char *pszFilename;
|
---|
| 449 | int64_t offDelta = 0;
|
---|
| 450 | const char *pszModule = NULL;
|
---|
| 451 | uint64_t ModuleAddress = ~0;
|
---|
| 452 | uint64_t ModuleSize = 0;
|
---|
| 453 |
|
---|
| 454 | /* filename */
|
---|
| 455 | if (argc < 2)
|
---|
| 456 | return errorArgument("Missing the filename argument!\n");
|
---|
| 457 | pszFilename = argv[1];
|
---|
| 458 |
|
---|
| 459 | /* offDelta */
|
---|
| 460 | if (argc >= 3)
|
---|
| 461 | {
|
---|
[24998] | 462 | int irc = RTStrToInt64Ex(argv[2], NULL, 0, &offDelta);
|
---|
| 463 | if (RT_FAILURE(irc))
|
---|
[13837] | 464 | return errorArgument(argv[0], "Failed to read delta '%s', rc=%Rrc\n", argv[2], rc);
|
---|
[1] | 465 | }
|
---|
| 466 |
|
---|
| 467 | /* pszModule */
|
---|
| 468 | if (argc >= 4)
|
---|
| 469 | pszModule = argv[3];
|
---|
| 470 |
|
---|
| 471 | /* ModuleAddress */
|
---|
| 472 | if (argc >= 5)
|
---|
| 473 | {
|
---|
[24998] | 474 | int irc = RTStrToUInt64Ex(argv[4], NULL, 0, &ModuleAddress);
|
---|
| 475 | if (RT_FAILURE(irc))
|
---|
[13837] | 476 | return errorArgument(argv[0], "Failed to read module address '%s', rc=%Rrc\n", argv[4], rc);
|
---|
[1] | 477 | }
|
---|
| 478 |
|
---|
| 479 | /* ModuleSize */
|
---|
| 480 | if (argc >= 6)
|
---|
| 481 | {
|
---|
[24998] | 482 | int irc = RTStrToUInt64Ex(argv[5], NULL, 0, &ModuleSize);
|
---|
| 483 | if (RT_FAILURE(irc))
|
---|
[13837] | 484 | return errorArgument(argv[0], "Failed to read module size '%s', rc=%Rrc\n", argv[5], rc);
|
---|
[1] | 485 | }
|
---|
| 486 |
|
---|
| 487 | /*
|
---|
| 488 | * Add extra data.
|
---|
| 489 | */
|
---|
| 490 | Utf8Str KeyStr;
|
---|
| 491 | HRESULT hrc = NewUniqueKey(machine, "VBoxInternal/DBGF/loadsyms", KeyStr);
|
---|
| 492 | if (SUCCEEDED(hrc))
|
---|
[22173] | 493 | hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "Filename", pszFilename);
|
---|
[1] | 494 | if (SUCCEEDED(hrc) && argc >= 3)
|
---|
[22173] | 495 | hrc = SetInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "Delta", offDelta);
|
---|
[1] | 496 | if (SUCCEEDED(hrc) && argc >= 4)
|
---|
[22173] | 497 | hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "Module", pszModule);
|
---|
[1] | 498 | if (SUCCEEDED(hrc) && argc >= 5)
|
---|
[22173] | 499 | hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "ModuleAddress", ModuleAddress);
|
---|
[1] | 500 | if (SUCCEEDED(hrc) && argc >= 6)
|
---|
[22173] | 501 | hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr.c_str(), "ModuleSize", ModuleSize);
|
---|
[1] | 502 |
|
---|
| 503 | return FAILED(hrc);
|
---|
| 504 | }
|
---|
| 505 |
|
---|
[15366] | 506 |
|
---|
| 507 | static DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
|
---|
[1] | 508 | {
|
---|
[32701] | 509 | RTMsgErrorV(pszFormat, va);
|
---|
| 510 | RTMsgError("Error code %Rrc at %s(%u) in function %s", rc, RT_SRC_POS_ARGS);
|
---|
[15366] | 511 | }
|
---|
| 512 |
|
---|
[32536] | 513 | static int handleVDMessage(void *pvUser, const char *pszFormat, va_list va)
|
---|
[29649] | 514 | {
|
---|
| 515 | NOREF(pvUser);
|
---|
[32536] | 516 | return RTPrintfV(pszFormat, va);
|
---|
[29649] | 517 | }
|
---|
| 518 |
|
---|
[21806] | 519 | static int CmdSetHDUUID(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
[15366] | 520 | {
|
---|
[31461] | 521 | Guid uuid;
|
---|
| 522 | RTUUID rtuuid;
|
---|
| 523 | enum eUuidType {
|
---|
| 524 | HDUUID,
|
---|
| 525 | HDPARENTUUID
|
---|
| 526 | } uuidType;
|
---|
[31539] | 527 |
|
---|
[31461] | 528 | if (!strcmp(argv[0], "sethduuid"))
|
---|
[1] | 529 | {
|
---|
[31461] | 530 | uuidType = HDUUID;
|
---|
| 531 | if (argc != 3 && argc != 2)
|
---|
| 532 | return errorSyntax(USAGE_SETHDUUID, "Not enough parameters");
|
---|
| 533 | /* if specified, take UUID, otherwise generate a new one */
|
---|
| 534 | if (argc == 3)
|
---|
| 535 | {
|
---|
| 536 | if (RT_FAILURE(RTUuidFromStr(&rtuuid, argv[2])))
|
---|
| 537 | return errorSyntax(USAGE_SETHDUUID, "Invalid UUID parameter");
|
---|
| 538 | uuid = argv[2];
|
---|
| 539 | } else
|
---|
| 540 | uuid.create();
|
---|
[1] | 541 | }
|
---|
[31461] | 542 | else if (!strcmp(argv[0], "sethdparentuuid"))
|
---|
| 543 | {
|
---|
| 544 | uuidType = HDPARENTUUID;
|
---|
| 545 | if (argc != 3)
|
---|
| 546 | return errorSyntax(USAGE_SETHDPARENTUUID, "Not enough parameters");
|
---|
| 547 | if (RT_FAILURE(RTUuidFromStr(&rtuuid, argv[2])))
|
---|
| 548 | return errorSyntax(USAGE_SETHDPARENTUUID, "Invalid UUID parameter");
|
---|
| 549 | uuid = argv[2];
|
---|
| 550 | }
|
---|
| 551 | else
|
---|
| 552 | return errorSyntax(USAGE_SETHDUUID, "Invalid invocation");
|
---|
[1] | 553 |
|
---|
| 554 | /* just try it */
|
---|
[15366] | 555 | char *pszFormat = NULL;
|
---|
[33524] | 556 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
[32536] | 557 | int rc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
[33524] | 558 | argv[1], &pszFormat, &enmType);
|
---|
[13835] | 559 | if (RT_FAILURE(rc))
|
---|
[1] | 560 | {
|
---|
[32701] | 561 | RTMsgError("Format autodetect failed: %Rrc", rc);
|
---|
[15366] | 562 | return 1;
|
---|
[1] | 563 | }
|
---|
[15366] | 564 |
|
---|
| 565 | PVBOXHDD pDisk = NULL;
|
---|
| 566 |
|
---|
| 567 | PVDINTERFACE pVDIfs = NULL;
|
---|
| 568 | VDINTERFACE vdInterfaceError;
|
---|
| 569 | VDINTERFACEERROR vdInterfaceErrorCallbacks;
|
---|
| 570 | vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
|
---|
| 571 | vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
| 572 | vdInterfaceErrorCallbacks.pfnError = handleVDError;
|
---|
[29649] | 573 | vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
|
---|
[15366] | 574 |
|
---|
| 575 | rc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
|
---|
| 576 | &vdInterfaceErrorCallbacks, NULL, &pVDIfs);
|
---|
| 577 | AssertRC(rc);
|
---|
| 578 |
|
---|
[33524] | 579 | rc = VDCreate(pVDIfs, enmType, &pDisk);
|
---|
[15366] | 580 | if (RT_FAILURE(rc))
|
---|
[1] | 581 | {
|
---|
[32701] | 582 | RTMsgError("Cannot create the virtual disk container: %Rrc", rc);
|
---|
[15366] | 583 | return 1;
|
---|
[1] | 584 | }
|
---|
| 585 |
|
---|
[15366] | 586 | /* Open the image */
|
---|
[31461] | 587 | rc = VDOpen(pDisk, pszFormat, argv[1], VD_OPEN_FLAGS_NORMAL, NULL);
|
---|
[15366] | 588 | if (RT_FAILURE(rc))
|
---|
| 589 | {
|
---|
[32701] | 590 | RTMsgError("Cannot open the image: %Rrc", rc);
|
---|
[15366] | 591 | return 1;
|
---|
| 592 | }
|
---|
[1] | 593 |
|
---|
[31461] | 594 | if (uuidType == HDUUID)
|
---|
| 595 | rc = VDSetUuid(pDisk, VD_LAST_IMAGE, uuid.raw());
|
---|
| 596 | else
|
---|
| 597 | rc = VDSetParentUuid(pDisk, VD_LAST_IMAGE, uuid.raw());
|
---|
[15366] | 598 | if (RT_FAILURE(rc))
|
---|
[32701] | 599 | RTMsgError("Cannot set a new UUID: %Rrc", rc);
|
---|
[15366] | 600 | else
|
---|
[31539] | 601 | RTPrintf("UUID changed to: %s\n", uuid.toString().c_str());
|
---|
[8230] | 602 |
|
---|
[15366] | 603 | VDCloseAll(pDisk);
|
---|
| 604 |
|
---|
| 605 | return RT_FAILURE(rc);
|
---|
[8230] | 606 | }
|
---|
| 607 |
|
---|
[21806] | 608 |
|
---|
| 609 | static int CmdDumpHDInfo(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
| 610 | {
|
---|
| 611 | /* we need exactly one parameter: the image file */
|
---|
| 612 | if (argc != 1)
|
---|
| 613 | {
|
---|
[31257] | 614 | return errorSyntax(USAGE_DUMPHDINFO, "Not enough parameters");
|
---|
[21806] | 615 | }
|
---|
| 616 |
|
---|
| 617 | /* just try it */
|
---|
| 618 | char *pszFormat = NULL;
|
---|
[33524] | 619 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
[32536] | 620 | int rc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
[33524] | 621 | argv[0], &pszFormat, &enmType);
|
---|
[21806] | 622 | if (RT_FAILURE(rc))
|
---|
| 623 | {
|
---|
[32701] | 624 | RTMsgError("Format autodetect failed: %Rrc", rc);
|
---|
[21806] | 625 | return 1;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | PVBOXHDD pDisk = NULL;
|
---|
| 629 |
|
---|
| 630 | PVDINTERFACE pVDIfs = NULL;
|
---|
| 631 | VDINTERFACE vdInterfaceError;
|
---|
| 632 | VDINTERFACEERROR vdInterfaceErrorCallbacks;
|
---|
| 633 | vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
|
---|
| 634 | vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
| 635 | vdInterfaceErrorCallbacks.pfnError = handleVDError;
|
---|
| 636 | vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
|
---|
| 637 |
|
---|
| 638 | rc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
|
---|
| 639 | &vdInterfaceErrorCallbacks, NULL, &pVDIfs);
|
---|
| 640 | AssertRC(rc);
|
---|
| 641 |
|
---|
[33524] | 642 | rc = VDCreate(pVDIfs, enmType, &pDisk);
|
---|
[21806] | 643 | if (RT_FAILURE(rc))
|
---|
| 644 | {
|
---|
[32701] | 645 | RTMsgError("Cannot create the virtual disk container: %Rrc", rc);
|
---|
[21806] | 646 | return 1;
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | /* Open the image */
|
---|
[27357] | 650 | rc = VDOpen(pDisk, pszFormat, argv[0], VD_OPEN_FLAGS_INFO, NULL);
|
---|
[21806] | 651 | if (RT_FAILURE(rc))
|
---|
| 652 | {
|
---|
[32701] | 653 | RTMsgError("Cannot open the image: %Rrc", rc);
|
---|
[21806] | 654 | return 1;
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | VDDumpImages(pDisk);
|
---|
| 658 |
|
---|
| 659 | VDCloseAll(pDisk);
|
---|
| 660 |
|
---|
| 661 | return RT_FAILURE(rc);
|
---|
| 662 | }
|
---|
| 663 |
|
---|
[8230] | 664 | static int partRead(RTFILE File, PHOSTPARTITIONS pPart)
|
---|
| 665 | {
|
---|
| 666 | uint8_t aBuffer[512];
|
---|
| 667 | int rc;
|
---|
| 668 |
|
---|
| 669 | pPart->cPartitions = 0;
|
---|
| 670 | memset(pPart->aPartitions, '\0', sizeof(pPart->aPartitions));
|
---|
| 671 | rc = RTFileReadAt(File, 0, &aBuffer, sizeof(aBuffer), NULL);
|
---|
[13835] | 672 | if (RT_FAILURE(rc))
|
---|
[8230] | 673 | return rc;
|
---|
| 674 | if (aBuffer[510] != 0x55 || aBuffer[511] != 0xaa)
|
---|
| 675 | return VERR_INVALID_PARAMETER;
|
---|
| 676 |
|
---|
| 677 | unsigned uExtended = (unsigned)-1;
|
---|
| 678 |
|
---|
| 679 | for (unsigned i = 0; i < 4; i++)
|
---|
| 680 | {
|
---|
| 681 | uint8_t *p = &aBuffer[0x1be + i * 16];
|
---|
| 682 | if (p[4] == 0)
|
---|
| 683 | continue;
|
---|
| 684 | PHOSTPARTITION pCP = &pPart->aPartitions[pPart->cPartitions++];
|
---|
| 685 | pCP->uIndex = i + 1;
|
---|
| 686 | pCP->uType = p[4];
|
---|
| 687 | pCP->uStartCylinder = (uint32_t)p[3] + ((uint32_t)(p[2] & 0xc0) << 2);
|
---|
| 688 | pCP->uStartHead = p[1];
|
---|
| 689 | pCP->uStartSector = p[2] & 0x3f;
|
---|
| 690 | pCP->uEndCylinder = (uint32_t)p[7] + ((uint32_t)(p[6] & 0xc0) << 2);
|
---|
| 691 | pCP->uEndHead = p[5];
|
---|
| 692 | pCP->uEndSector = p[6] & 0x3f;
|
---|
| 693 | pCP->uStart = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
|
---|
| 694 | pCP->uSize = RT_MAKE_U32_FROM_U8(p[12], p[13], p[14], p[15]);
|
---|
| 695 | pCP->uPartDataStart = 0; /* will be filled out later properly. */
|
---|
| 696 | pCP->cPartDataSectors = 0;
|
---|
| 697 |
|
---|
| 698 | if (PARTTYPE_IS_EXTENDED(p[4]))
|
---|
| 699 | {
|
---|
| 700 | if (uExtended == (unsigned)-1)
|
---|
[18491] | 701 | uExtended = (unsigned)(pCP - pPart->aPartitions);
|
---|
[8230] | 702 | else
|
---|
| 703 | {
|
---|
[32701] | 704 | RTMsgError("More than one extended partition");
|
---|
[8230] | 705 | return VERR_INVALID_PARAMETER;
|
---|
| 706 | }
|
---|
| 707 | }
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | if (uExtended != (unsigned)-1)
|
---|
| 711 | {
|
---|
| 712 | unsigned uIndex = 5;
|
---|
| 713 | uint64_t uStart = pPart->aPartitions[uExtended].uStart;
|
---|
| 714 | uint64_t uOffset = 0;
|
---|
| 715 | if (!uStart)
|
---|
| 716 | {
|
---|
[32701] | 717 | RTMsgError("Inconsistency for logical partition start");
|
---|
[8230] | 718 | return VERR_INVALID_PARAMETER;
|
---|
| 719 | }
|
---|
| 720 |
|
---|
| 721 | do
|
---|
| 722 | {
|
---|
| 723 | rc = RTFileReadAt(File, (uStart + uOffset) * 512, &aBuffer, sizeof(aBuffer), NULL);
|
---|
[13835] | 724 | if (RT_FAILURE(rc))
|
---|
[8230] | 725 | return rc;
|
---|
| 726 |
|
---|
| 727 | if (aBuffer[510] != 0x55 || aBuffer[511] != 0xaa)
|
---|
| 728 | {
|
---|
[32701] | 729 | RTMsgError("Logical partition without magic");
|
---|
[8230] | 730 | return VERR_INVALID_PARAMETER;
|
---|
| 731 | }
|
---|
| 732 | uint8_t *p = &aBuffer[0x1be];
|
---|
| 733 |
|
---|
| 734 | if (p[4] == 0)
|
---|
| 735 | {
|
---|
[32701] | 736 | RTMsgError("Logical partition with type 0 encountered");
|
---|
[8230] | 737 | return VERR_INVALID_PARAMETER;
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | PHOSTPARTITION pCP = &pPart->aPartitions[pPart->cPartitions++];
|
---|
| 741 | pCP->uIndex = uIndex;
|
---|
| 742 | pCP->uType = p[4];
|
---|
| 743 | pCP->uStartCylinder = (uint32_t)p[3] + ((uint32_t)(p[2] & 0xc0) << 2);
|
---|
| 744 | pCP->uStartHead = p[1];
|
---|
| 745 | pCP->uStartSector = p[2] & 0x3f;
|
---|
| 746 | pCP->uEndCylinder = (uint32_t)p[7] + ((uint32_t)(p[6] & 0xc0) << 2);
|
---|
| 747 | pCP->uEndHead = p[5];
|
---|
| 748 | pCP->uEndSector = p[6] & 0x3f;
|
---|
| 749 | uint32_t uStartOffset = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
|
---|
[9975] | 750 | if (!uStartOffset)
|
---|
| 751 | {
|
---|
[32701] | 752 | RTMsgError("Invalid partition start offset");
|
---|
[9975] | 753 | return VERR_INVALID_PARAMETER;
|
---|
| 754 | }
|
---|
[8230] | 755 | pCP->uStart = uStart + uOffset + uStartOffset;
|
---|
| 756 | pCP->uSize = RT_MAKE_U32_FROM_U8(p[12], p[13], p[14], p[15]);
|
---|
| 757 | /* Fill out partitioning location info for EBR. */
|
---|
| 758 | pCP->uPartDataStart = uStart + uOffset;
|
---|
[9975] | 759 | pCP->cPartDataSectors = uStartOffset;
|
---|
[8230] | 760 | p += 16;
|
---|
| 761 | if (p[4] == 0)
|
---|
| 762 | uExtended = (unsigned)-1;
|
---|
| 763 | else if (PARTTYPE_IS_EXTENDED(p[4]))
|
---|
| 764 | {
|
---|
| 765 | uExtended = uIndex++;
|
---|
| 766 | uOffset = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
|
---|
| 767 | }
|
---|
| 768 | else
|
---|
| 769 | {
|
---|
[32701] | 770 | RTMsgError("Logical partition chain broken");
|
---|
[8230] | 771 | return VERR_INVALID_PARAMETER;
|
---|
| 772 | }
|
---|
| 773 | } while (uExtended != (unsigned)-1);
|
---|
| 774 | }
|
---|
| 775 |
|
---|
[9975] | 776 | /* Sort partitions in ascending order of start sector, plus a trivial
|
---|
| 777 | * bit of consistency checking. */
|
---|
[8230] | 778 | for (unsigned i = 0; i < pPart->cPartitions-1; i++)
|
---|
| 779 | {
|
---|
| 780 | unsigned uMinIdx = i;
|
---|
| 781 | uint64_t uMinVal = pPart->aPartitions[i].uStart;
|
---|
| 782 | for (unsigned j = i + 1; j < pPart->cPartitions; j++)
|
---|
| 783 | {
|
---|
| 784 | if (pPart->aPartitions[j].uStart < uMinVal)
|
---|
| 785 | {
|
---|
| 786 | uMinIdx = j;
|
---|
| 787 | uMinVal = pPart->aPartitions[j].uStart;
|
---|
| 788 | }
|
---|
| 789 | else if (pPart->aPartitions[j].uStart == uMinVal)
|
---|
| 790 | {
|
---|
[32701] | 791 | RTMsgError("Two partitions start at the same place");
|
---|
[8230] | 792 | return VERR_INVALID_PARAMETER;
|
---|
[18164] | 793 | }
|
---|
| 794 | else if (pPart->aPartitions[j].uStart == 0)
|
---|
[8230] | 795 | {
|
---|
[32701] | 796 | RTMsgError("Partition starts at sector 0");
|
---|
[8230] | 797 | return VERR_INVALID_PARAMETER;
|
---|
| 798 | }
|
---|
| 799 | }
|
---|
| 800 | if (uMinIdx != i)
|
---|
| 801 | {
|
---|
| 802 | /* Swap entries at index i and uMinIdx. */
|
---|
| 803 | memcpy(&pPart->aPartitions[pPart->cPartitions],
|
---|
| 804 | &pPart->aPartitions[i], sizeof(HOSTPARTITION));
|
---|
| 805 | memcpy(&pPart->aPartitions[i],
|
---|
| 806 | &pPart->aPartitions[uMinIdx], sizeof(HOSTPARTITION));
|
---|
| 807 | memcpy(&pPart->aPartitions[uMinIdx],
|
---|
| 808 | &pPart->aPartitions[pPart->cPartitions], sizeof(HOSTPARTITION));
|
---|
| 809 | }
|
---|
[9975] | 810 | }
|
---|
| 811 |
|
---|
[29649] | 812 | /* Fill out partitioning location info for MBR. */
|
---|
| 813 | pPart->aPartitions[0].uPartDataStart = 0;
|
---|
| 814 | pPart->aPartitions[0].cPartDataSectors = pPart->aPartitions[0].uStart;
|
---|
| 815 |
|
---|
| 816 | /* Now do a some partition table consistency checking, to reject the most
|
---|
| 817 | * obvious garbage which can lead to trouble later. */
|
---|
[9975] | 818 | uint64_t uPrevEnd = 0;
|
---|
| 819 | for (unsigned i = 0; i < pPart->cPartitions-1; i++)
|
---|
| 820 | {
|
---|
[8230] | 821 | if (pPart->aPartitions[i].cPartDataSectors)
|
---|
| 822 | uPrevEnd = pPart->aPartitions[i].uPartDataStart + pPart->aPartitions[i].cPartDataSectors;
|
---|
| 823 | if (pPart->aPartitions[i].uStart < uPrevEnd)
|
---|
| 824 | {
|
---|
[32701] | 825 | RTMsgError("Overlapping partitions");
|
---|
[8230] | 826 | return VERR_INVALID_PARAMETER;
|
---|
| 827 | }
|
---|
| 828 | if (!PARTTYPE_IS_EXTENDED(pPart->aPartitions[i].uType))
|
---|
| 829 | uPrevEnd = pPart->aPartitions[i].uStart + pPart->aPartitions[i].uSize;
|
---|
| 830 | }
|
---|
| 831 |
|
---|
| 832 | return VINF_SUCCESS;
|
---|
| 833 | }
|
---|
| 834 |
|
---|
[9493] | 835 | static int CmdListPartitions(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
[8230] | 836 | {
|
---|
| 837 | Utf8Str rawdisk;
|
---|
| 838 |
|
---|
| 839 | /* let's have a closer look at the arguments */
|
---|
| 840 | for (int i = 0; i < argc; i++)
|
---|
| 841 | {
|
---|
| 842 | if (strcmp(argv[i], "-rawdisk") == 0)
|
---|
| 843 | {
|
---|
| 844 | if (argc <= i + 1)
|
---|
| 845 | {
|
---|
| 846 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 847 | }
|
---|
| 848 | i++;
|
---|
| 849 | rawdisk = argv[i];
|
---|
| 850 | }
|
---|
| 851 | else
|
---|
| 852 | {
|
---|
[32531] | 853 | return errorSyntax(USAGE_LISTPARTITIONS, "Invalid parameter '%s'", argv[i]);
|
---|
[8230] | 854 | }
|
---|
| 855 | }
|
---|
| 856 |
|
---|
[21394] | 857 | if (rawdisk.isEmpty())
|
---|
[8230] | 858 | return errorSyntax(USAGE_LISTPARTITIONS, "Mandatory parameter -rawdisk missing");
|
---|
| 859 |
|
---|
| 860 | RTFILE RawFile;
|
---|
[31539] | 861 | int vrc = RTFileOpen(&RawFile, rawdisk.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
[13835] | 862 | if (RT_FAILURE(vrc))
|
---|
[8230] | 863 | {
|
---|
[33595] | 864 | RTMsgError("Cannot open the raw disk: %Rrc", vrc);
|
---|
[8230] | 865 | return vrc;
|
---|
| 866 | }
|
---|
| 867 |
|
---|
| 868 | HOSTPARTITIONS partitions;
|
---|
| 869 | vrc = partRead(RawFile, &partitions);
|
---|
[29649] | 870 | /* Don't bail out on errors, print the table and return the result code. */
|
---|
[8230] | 871 |
|
---|
| 872 | RTPrintf("Number Type StartCHS EndCHS Size (MiB) Start (Sect)\n");
|
---|
| 873 | for (unsigned i = 0; i < partitions.cPartitions; i++)
|
---|
| 874 | {
|
---|
[29649] | 875 | /* Don't show the extended partition, otherwise users might think they
|
---|
| 876 | * can add it to the list of partitions for raw partition access. */
|
---|
[8230] | 877 | if (PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
|
---|
| 878 | continue;
|
---|
| 879 |
|
---|
| 880 | RTPrintf("%-7u %#04x %-4u/%-3u/%-2u %-4u/%-3u/%-2u %10llu %10llu\n",
|
---|
| 881 | partitions.aPartitions[i].uIndex,
|
---|
| 882 | partitions.aPartitions[i].uType,
|
---|
| 883 | partitions.aPartitions[i].uStartCylinder,
|
---|
| 884 | partitions.aPartitions[i].uStartHead,
|
---|
| 885 | partitions.aPartitions[i].uStartSector,
|
---|
| 886 | partitions.aPartitions[i].uEndCylinder,
|
---|
| 887 | partitions.aPartitions[i].uEndHead,
|
---|
| 888 | partitions.aPartitions[i].uEndSector,
|
---|
| 889 | partitions.aPartitions[i].uSize / 2048,
|
---|
| 890 | partitions.aPartitions[i].uStart);
|
---|
| 891 | }
|
---|
| 892 |
|
---|
[29649] | 893 | return vrc;
|
---|
[8230] | 894 | }
|
---|
| 895 |
|
---|
[29649] | 896 | static PVBOXHDDRAWPARTDESC appendPartDesc(uint32_t *pcPartDescs, PVBOXHDDRAWPARTDESC *ppPartDescs)
|
---|
| 897 | {
|
---|
| 898 | (*pcPartDescs)++;
|
---|
| 899 | PVBOXHDDRAWPARTDESC p;
|
---|
| 900 | p = (PVBOXHDDRAWPARTDESC)RTMemRealloc(*ppPartDescs,
|
---|
| 901 | *pcPartDescs * sizeof(VBOXHDDRAWPARTDESC));
|
---|
| 902 | *ppPartDescs = p;
|
---|
| 903 | if (p)
|
---|
| 904 | {
|
---|
| 905 | p = p + *pcPartDescs - 1;
|
---|
| 906 | memset(p, '\0', sizeof(VBOXHDDRAWPARTDESC));
|
---|
| 907 | }
|
---|
| 908 |
|
---|
| 909 | return p;
|
---|
| 910 | }
|
---|
| 911 |
|
---|
[9493] | 912 | static int CmdCreateRawVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
[8230] | 913 | {
|
---|
| 914 | HRESULT rc = S_OK;
|
---|
[32531] | 915 | Utf8Str filename;
|
---|
[8230] | 916 | const char *pszMBRFilename = NULL;
|
---|
| 917 | Utf8Str rawdisk;
|
---|
| 918 | const char *pszPartitions = NULL;
|
---|
| 919 | bool fRelative = false;
|
---|
| 920 |
|
---|
[11131] | 921 | uint64_t cbSize = 0;
|
---|
| 922 | PVBOXHDD pDisk = NULL;
|
---|
| 923 | VBOXHDDRAW RawDescriptor;
|
---|
[11435] | 924 | PVDINTERFACE pVDIfs = NULL;
|
---|
[11131] | 925 |
|
---|
[8230] | 926 | /* let's have a closer look at the arguments */
|
---|
| 927 | for (int i = 0; i < argc; i++)
|
---|
| 928 | {
|
---|
| 929 | if (strcmp(argv[i], "-filename") == 0)
|
---|
| 930 | {
|
---|
| 931 | if (argc <= i + 1)
|
---|
| 932 | {
|
---|
| 933 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 934 | }
|
---|
| 935 | i++;
|
---|
| 936 | filename = argv[i];
|
---|
| 937 | }
|
---|
| 938 | else if (strcmp(argv[i], "-mbr") == 0)
|
---|
| 939 | {
|
---|
| 940 | if (argc <= i + 1)
|
---|
| 941 | {
|
---|
| 942 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 943 | }
|
---|
| 944 | i++;
|
---|
| 945 | pszMBRFilename = argv[i];
|
---|
| 946 | }
|
---|
| 947 | else if (strcmp(argv[i], "-rawdisk") == 0)
|
---|
| 948 | {
|
---|
| 949 | if (argc <= i + 1)
|
---|
| 950 | {
|
---|
| 951 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 952 | }
|
---|
| 953 | i++;
|
---|
| 954 | rawdisk = argv[i];
|
---|
| 955 | }
|
---|
| 956 | else if (strcmp(argv[i], "-partitions") == 0)
|
---|
| 957 | {
|
---|
| 958 | if (argc <= i + 1)
|
---|
| 959 | {
|
---|
| 960 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 961 | }
|
---|
| 962 | i++;
|
---|
| 963 | pszPartitions = argv[i];
|
---|
| 964 | }
|
---|
| 965 | #ifdef RT_OS_LINUX
|
---|
| 966 | else if (strcmp(argv[i], "-relative") == 0)
|
---|
| 967 | {
|
---|
| 968 | fRelative = true;
|
---|
| 969 | }
|
---|
| 970 | #endif /* RT_OS_LINUX */
|
---|
| 971 | else
|
---|
[32531] | 972 | return errorSyntax(USAGE_CREATERAWVMDK, "Invalid parameter '%s'", argv[i]);
|
---|
[8230] | 973 | }
|
---|
| 974 |
|
---|
| 975 | if (filename.isEmpty())
|
---|
| 976 | return errorSyntax(USAGE_CREATERAWVMDK, "Mandatory parameter -filename missing");
|
---|
[21394] | 977 | if (rawdisk.isEmpty())
|
---|
[8230] | 978 | return errorSyntax(USAGE_CREATERAWVMDK, "Mandatory parameter -rawdisk missing");
|
---|
| 979 | if (!pszPartitions && pszMBRFilename)
|
---|
| 980 | return errorSyntax(USAGE_CREATERAWVMDK, "The parameter -mbr is only valid when the parameter -partitions is also present");
|
---|
| 981 |
|
---|
[21556] | 982 | #ifdef RT_OS_DARWIN
|
---|
| 983 | fRelative = true;
|
---|
| 984 | #endif /* RT_OS_DARWIN */
|
---|
[8230] | 985 | RTFILE RawFile;
|
---|
[31539] | 986 | int vrc = RTFileOpen(&RawFile, rawdisk.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
[13835] | 987 | if (RT_FAILURE(vrc))
|
---|
[8230] | 988 | {
|
---|
[32701] | 989 | RTMsgError("Cannot open the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
|
---|
[11131] | 990 | goto out;
|
---|
[8230] | 991 | }
|
---|
| 992 |
|
---|
| 993 | #ifdef RT_OS_WINDOWS
|
---|
[8969] | 994 | /* Windows NT has no IOCTL_DISK_GET_LENGTH_INFORMATION ioctl. This was
|
---|
| 995 | * added to Windows XP, so we have to use the available info from DriveGeo.
|
---|
| 996 | * Note that we cannot simply use IOCTL_DISK_GET_DRIVE_GEOMETRY as it
|
---|
| 997 | * yields a slightly different result than IOCTL_DISK_GET_LENGTH_INFO.
|
---|
| 998 | * We call IOCTL_DISK_GET_DRIVE_GEOMETRY first as we need to check the media
|
---|
| 999 | * type anyway, and if IOCTL_DISK_GET_LENGTH_INFORMATION is supported
|
---|
| 1000 | * we will later override cbSize.
|
---|
| 1001 | */
|
---|
[8230] | 1002 | DISK_GEOMETRY DriveGeo;
|
---|
| 1003 | DWORD cbDriveGeo;
|
---|
| 1004 | if (DeviceIoControl((HANDLE)RawFile,
|
---|
| 1005 | IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
|
---|
| 1006 | &DriveGeo, sizeof(DriveGeo), &cbDriveGeo, NULL))
|
---|
| 1007 | {
|
---|
[11136] | 1008 | if ( DriveGeo.MediaType == FixedMedia
|
---|
| 1009 | || DriveGeo.MediaType == RemovableMedia)
|
---|
[8230] | 1010 | {
|
---|
| 1011 | cbSize = DriveGeo.Cylinders.QuadPart
|
---|
| 1012 | * DriveGeo.TracksPerCylinder
|
---|
| 1013 | * DriveGeo.SectorsPerTrack
|
---|
| 1014 | * DriveGeo.BytesPerSector;
|
---|
| 1015 | }
|
---|
| 1016 | else
|
---|
[11131] | 1017 | {
|
---|
[32701] | 1018 | RTMsgError("File '%s' is no fixed/removable medium device", rawdisk.c_str());
|
---|
[11131] | 1019 | vrc = VERR_INVALID_PARAMETER;
|
---|
| 1020 | goto out;
|
---|
| 1021 | }
|
---|
[8969] | 1022 |
|
---|
| 1023 | GET_LENGTH_INFORMATION DiskLenInfo;
|
---|
| 1024 | DWORD junk;
|
---|
| 1025 | if (DeviceIoControl((HANDLE)RawFile,
|
---|
| 1026 | IOCTL_DISK_GET_LENGTH_INFO, NULL, 0,
|
---|
| 1027 | &DiskLenInfo, sizeof(DiskLenInfo), &junk, (LPOVERLAPPED)NULL))
|
---|
| 1028 | {
|
---|
| 1029 | /* IOCTL_DISK_GET_LENGTH_INFO is supported -- override cbSize. */
|
---|
| 1030 | cbSize = DiskLenInfo.Length.QuadPart;
|
---|
| 1031 | }
|
---|
[8230] | 1032 | }
|
---|
| 1033 | else
|
---|
[11131] | 1034 | {
|
---|
| 1035 | vrc = RTErrConvertFromWin32(GetLastError());
|
---|
[32701] | 1036 | RTMsgError("Cannot get the geometry of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
|
---|
[11131] | 1037 | goto out;
|
---|
| 1038 | }
|
---|
[8230] | 1039 | #elif defined(RT_OS_LINUX)
|
---|
| 1040 | struct stat DevStat;
|
---|
| 1041 | if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
|
---|
| 1042 | {
|
---|
[8811] | 1043 | #ifdef BLKGETSIZE64
|
---|
| 1044 | /* BLKGETSIZE64 is broken up to 2.4.17 and in many 2.5.x. In 2.6.0
|
---|
| 1045 | * it works without problems. */
|
---|
| 1046 | struct utsname utsname;
|
---|
| 1047 | if ( uname(&utsname) == 0
|
---|
| 1048 | && ( (strncmp(utsname.release, "2.5.", 4) == 0 && atoi(&utsname.release[4]) >= 18)
|
---|
| 1049 | || (strncmp(utsname.release, "2.", 2) == 0 && atoi(&utsname.release[2]) >= 6)))
|
---|
| 1050 | {
|
---|
| 1051 | uint64_t cbBlk;
|
---|
| 1052 | if (!ioctl(RawFile, BLKGETSIZE64, &cbBlk))
|
---|
| 1053 | cbSize = cbBlk;
|
---|
| 1054 | }
|
---|
| 1055 | #endif /* BLKGETSIZE64 */
|
---|
| 1056 | if (!cbSize)
|
---|
| 1057 | {
|
---|
| 1058 | long cBlocks;
|
---|
| 1059 | if (!ioctl(RawFile, BLKGETSIZE, &cBlocks))
|
---|
| 1060 | cbSize = (uint64_t)cBlocks << 9;
|
---|
| 1061 | else
|
---|
[11131] | 1062 | {
|
---|
| 1063 | vrc = RTErrConvertFromErrno(errno);
|
---|
[32701] | 1064 | RTMsgError("Cannot get the size of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
|
---|
[11131] | 1065 | goto out;
|
---|
| 1066 | }
|
---|
[8811] | 1067 | }
|
---|
| 1068 | }
|
---|
| 1069 | else
|
---|
| 1070 | {
|
---|
[32701] | 1071 | RTMsgError("File '%s' is no block device", rawdisk.c_str());
|
---|
[20290] | 1072 | vrc = VERR_INVALID_PARAMETER;
|
---|
| 1073 | goto out;
|
---|
[8811] | 1074 | }
|
---|
| 1075 | #elif defined(RT_OS_DARWIN)
|
---|
| 1076 | struct stat DevStat;
|
---|
| 1077 | if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
|
---|
| 1078 | {
|
---|
| 1079 | uint64_t cBlocks;
|
---|
| 1080 | uint32_t cbBlock;
|
---|
[8812] | 1081 | if (!ioctl(RawFile, DKIOCGETBLOCKCOUNT, &cBlocks))
|
---|
[8811] | 1082 | {
|
---|
[8812] | 1083 | if (!ioctl(RawFile, DKIOCGETBLOCKSIZE, &cbBlock))
|
---|
[8811] | 1084 | cbSize = cBlocks * cbBlock;
|
---|
| 1085 | else
|
---|
[11131] | 1086 | {
|
---|
[32701] | 1087 | RTMsgError("Cannot get the block size for file '%s': %Rrc", rawdisk.c_str(), vrc);
|
---|
[11131] | 1088 | vrc = RTErrConvertFromErrno(errno);
|
---|
| 1089 | goto out;
|
---|
| 1090 | }
|
---|
[8811] | 1091 | }
|
---|
[8230] | 1092 | else
|
---|
[11131] | 1093 | {
|
---|
| 1094 | vrc = RTErrConvertFromErrno(errno);
|
---|
[32701] | 1095 | RTMsgError("Cannot get the block count for file '%s': %Rrc", rawdisk.c_str(), vrc);
|
---|
[11131] | 1096 | goto out;
|
---|
| 1097 | }
|
---|
[8230] | 1098 | }
|
---|
| 1099 | else
|
---|
| 1100 | {
|
---|
[32701] | 1101 | RTMsgError("File '%s' is no block device", rawdisk.c_str());
|
---|
[11131] | 1102 | vrc = VERR_INVALID_PARAMETER;
|
---|
| 1103 | goto out;
|
---|
[8230] | 1104 | }
|
---|
[8835] | 1105 | #elif defined(RT_OS_SOLARIS)
|
---|
| 1106 | struct stat DevStat;
|
---|
[9856] | 1107 | if (!fstat(RawFile, &DevStat) && ( S_ISBLK(DevStat.st_mode)
|
---|
| 1108 | || S_ISCHR(DevStat.st_mode)))
|
---|
[8835] | 1109 | {
|
---|
| 1110 | struct dk_minfo mediainfo;
|
---|
| 1111 | if (!ioctl(RawFile, DKIOCGMEDIAINFO, &mediainfo))
|
---|
| 1112 | cbSize = mediainfo.dki_capacity * mediainfo.dki_lbsize;
|
---|
| 1113 | else
|
---|
[11131] | 1114 | {
|
---|
| 1115 | vrc = RTErrConvertFromErrno(errno);
|
---|
[32701] | 1116 | RTMsgError("Cannot get the size of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
|
---|
[11131] | 1117 | goto out;
|
---|
| 1118 | }
|
---|
[8835] | 1119 | }
|
---|
| 1120 | else
|
---|
| 1121 | {
|
---|
[32701] | 1122 | RTMsgError("File '%s' is no block or char device", rawdisk.c_str());
|
---|
[11131] | 1123 | vrc = VERR_INVALID_PARAMETER;
|
---|
| 1124 | goto out;
|
---|
[8835] | 1125 | }
|
---|
[27133] | 1126 | #elif defined(RT_OS_FREEBSD)
|
---|
| 1127 | struct stat DevStat;
|
---|
| 1128 | if (!fstat(RawFile, &DevStat) && S_ISCHR(DevStat.st_mode))
|
---|
| 1129 | {
|
---|
| 1130 | off_t cbMedia = 0;
|
---|
| 1131 | if (!ioctl(RawFile, DIOCGMEDIASIZE, &cbMedia))
|
---|
| 1132 | {
|
---|
| 1133 | cbSize = cbMedia;
|
---|
| 1134 | }
|
---|
| 1135 | else
|
---|
| 1136 | {
|
---|
| 1137 | vrc = RTErrConvertFromErrno(errno);
|
---|
[32701] | 1138 | RTMsgError("Cannot get the block count for file '%s': %Rrc", rawdisk.c_str(), vrc);
|
---|
[27133] | 1139 | goto out;
|
---|
| 1140 | }
|
---|
| 1141 | }
|
---|
| 1142 | else
|
---|
| 1143 | {
|
---|
[32701] | 1144 | RTMsgError("File '%s' is no character device", rawdisk.c_str());
|
---|
[27133] | 1145 | vrc = VERR_INVALID_PARAMETER;
|
---|
| 1146 | goto out;
|
---|
| 1147 | }
|
---|
[8811] | 1148 | #else /* all unrecognized OSes */
|
---|
[8230] | 1149 | /* Hopefully this works on all other hosts. If it doesn't, it'll just fail
|
---|
| 1150 | * creating the VMDK, so no real harm done. */
|
---|
| 1151 | vrc = RTFileGetSize(RawFile, &cbSize);
|
---|
[13835] | 1152 | if (RT_FAILURE(vrc))
|
---|
[8230] | 1153 | {
|
---|
[32701] | 1154 | RTMsgError("Cannot get the size of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
|
---|
[11131] | 1155 | goto out;
|
---|
[8230] | 1156 | }
|
---|
[8811] | 1157 | #endif
|
---|
[8230] | 1158 |
|
---|
[9891] | 1159 | /* Check whether cbSize is actually sensible. */
|
---|
| 1160 | if (!cbSize || cbSize % 512)
|
---|
| 1161 | {
|
---|
[32701] | 1162 | RTMsgError("Detected size of raw disk '%s' is %s, an invalid value", rawdisk.c_str(), cbSize);
|
---|
[11131] | 1163 | vrc = VERR_INVALID_PARAMETER;
|
---|
| 1164 | goto out;
|
---|
[9891] | 1165 | }
|
---|
| 1166 |
|
---|
[8230] | 1167 | RawDescriptor.szSignature[0] = 'R';
|
---|
| 1168 | RawDescriptor.szSignature[1] = 'A';
|
---|
| 1169 | RawDescriptor.szSignature[2] = 'W';
|
---|
| 1170 | RawDescriptor.szSignature[3] = '\0';
|
---|
| 1171 | if (!pszPartitions)
|
---|
| 1172 | {
|
---|
| 1173 | RawDescriptor.fRawDisk = true;
|
---|
[31539] | 1174 | RawDescriptor.pszRawDisk = rawdisk.c_str();
|
---|
[8230] | 1175 | }
|
---|
| 1176 | else
|
---|
| 1177 | {
|
---|
| 1178 | RawDescriptor.fRawDisk = false;
|
---|
| 1179 | RawDescriptor.pszRawDisk = NULL;
|
---|
[29649] | 1180 | RawDescriptor.cPartDescs = 0;
|
---|
| 1181 | RawDescriptor.pPartDescs = NULL;
|
---|
[8230] | 1182 |
|
---|
[29649] | 1183 | uint32_t uPartitions = 0;
|
---|
| 1184 |
|
---|
[8230] | 1185 | const char *p = pszPartitions;
|
---|
| 1186 | char *pszNext;
|
---|
| 1187 | uint32_t u32;
|
---|
| 1188 | while (*p != '\0')
|
---|
| 1189 | {
|
---|
| 1190 | vrc = RTStrToUInt32Ex(p, &pszNext, 0, &u32);
|
---|
[13835] | 1191 | if (RT_FAILURE(vrc))
|
---|
[8230] | 1192 | {
|
---|
[32701] | 1193 | RTMsgError("Incorrect value in partitions parameter");
|
---|
[11131] | 1194 | goto out;
|
---|
[8230] | 1195 | }
|
---|
| 1196 | uPartitions |= RT_BIT(u32);
|
---|
| 1197 | p = pszNext;
|
---|
| 1198 | if (*p == ',')
|
---|
| 1199 | p++;
|
---|
| 1200 | else if (*p != '\0')
|
---|
| 1201 | {
|
---|
[32701] | 1202 | RTMsgError("Incorrect separator in partitions parameter");
|
---|
[11131] | 1203 | vrc = VERR_INVALID_PARAMETER;
|
---|
| 1204 | goto out;
|
---|
[8230] | 1205 | }
|
---|
| 1206 | }
|
---|
| 1207 |
|
---|
[29649] | 1208 | HOSTPARTITIONS partitions;
|
---|
[8230] | 1209 | vrc = partRead(RawFile, &partitions);
|
---|
[13835] | 1210 | if (RT_FAILURE(vrc))
|
---|
[8230] | 1211 | {
|
---|
[32701] | 1212 | RTMsgError("Cannot read the partition information from '%s'", rawdisk.c_str());
|
---|
[11131] | 1213 | goto out;
|
---|
[8230] | 1214 | }
|
---|
| 1215 |
|
---|
| 1216 | for (unsigned i = 0; i < partitions.cPartitions; i++)
|
---|
| 1217 | {
|
---|
| 1218 | if ( uPartitions & RT_BIT(partitions.aPartitions[i].uIndex)
|
---|
| 1219 | && PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
|
---|
| 1220 | {
|
---|
| 1221 | /* Some ignorant user specified an extended partition.
|
---|
| 1222 | * Bad idea, as this would trigger an overlapping
|
---|
| 1223 | * partitions error later during VMDK creation. So warn
|
---|
| 1224 | * here and ignore what the user requested. */
|
---|
[32701] | 1225 | RTMsgWarning("It is not possible (and necessary) to explicitly give access to the "
|
---|
| 1226 | "extended partition %u. If required, enable access to all logical "
|
---|
| 1227 | "partitions inside this extended partition.",
|
---|
| 1228 | partitions.aPartitions[i].uIndex);
|
---|
[8230] | 1229 | uPartitions &= ~RT_BIT(partitions.aPartitions[i].uIndex);
|
---|
| 1230 | }
|
---|
| 1231 | }
|
---|
| 1232 |
|
---|
| 1233 | for (unsigned i = 0; i < partitions.cPartitions; i++)
|
---|
| 1234 | {
|
---|
[29649] | 1235 | PVBOXHDDRAWPARTDESC pPartDesc = NULL;
|
---|
| 1236 |
|
---|
| 1237 | /* first dump the MBR/EPT data area */
|
---|
| 1238 | if (partitions.aPartitions[i].cPartDataSectors)
|
---|
| 1239 | {
|
---|
| 1240 | pPartDesc = appendPartDesc(&RawDescriptor.cPartDescs,
|
---|
| 1241 | &RawDescriptor.pPartDescs);
|
---|
| 1242 | if (!pPartDesc)
|
---|
| 1243 | {
|
---|
[32701] | 1244 | RTMsgError("Out of memory allocating the partition list for '%s'", rawdisk.c_str());
|
---|
[29649] | 1245 | vrc = VERR_NO_MEMORY;
|
---|
| 1246 | goto out;
|
---|
| 1247 | }
|
---|
| 1248 |
|
---|
| 1249 | /** @todo the clipping below isn't 100% accurate, as it should
|
---|
| 1250 | * actually clip to the track size. However that's easier said
|
---|
| 1251 | * than done as figuring out the track size is heuristics. In
|
---|
| 1252 | * any case the clipping is adjusted later after sorting, to
|
---|
| 1253 | * prevent overlapping data areas on the resulting image. */
|
---|
| 1254 | pPartDesc->cbData = RT_MIN(partitions.aPartitions[i].cPartDataSectors, 63) * 512;
|
---|
| 1255 | pPartDesc->uStart = partitions.aPartitions[i].uPartDataStart * 512;
|
---|
| 1256 | Assert(pPartDesc->cbData - (size_t)pPartDesc->cbData == 0);
|
---|
| 1257 | void *pPartData = RTMemAlloc((size_t)pPartDesc->cbData);
|
---|
| 1258 | if (!pPartData)
|
---|
| 1259 | {
|
---|
[32701] | 1260 | RTMsgError("Out of memory allocating the partition descriptor for '%s'", rawdisk.c_str());
|
---|
[29649] | 1261 | vrc = VERR_NO_MEMORY;
|
---|
| 1262 | goto out;
|
---|
| 1263 | }
|
---|
| 1264 | vrc = RTFileReadAt(RawFile, partitions.aPartitions[i].uPartDataStart * 512,
|
---|
| 1265 | pPartData, (size_t)pPartDesc->cbData, NULL);
|
---|
| 1266 | if (RT_FAILURE(vrc))
|
---|
| 1267 | {
|
---|
[32701] | 1268 | RTMsgError("Cannot read partition data from raw device '%s': %Rrc", rawdisk.c_str(), vrc);
|
---|
[29649] | 1269 | goto out;
|
---|
| 1270 | }
|
---|
| 1271 | /* Splice in the replacement MBR code if specified. */
|
---|
| 1272 | if ( partitions.aPartitions[i].uPartDataStart == 0
|
---|
| 1273 | && pszMBRFilename)
|
---|
| 1274 | {
|
---|
| 1275 | RTFILE MBRFile;
|
---|
| 1276 | vrc = RTFileOpen(&MBRFile, pszMBRFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
| 1277 | if (RT_FAILURE(vrc))
|
---|
| 1278 | {
|
---|
[32701] | 1279 | RTMsgError("Cannot open replacement MBR file '%s' specified with -mbr: %Rrc", pszMBRFilename, vrc);
|
---|
[29649] | 1280 | goto out;
|
---|
| 1281 | }
|
---|
| 1282 | vrc = RTFileReadAt(MBRFile, 0, pPartData, 0x1be, NULL);
|
---|
| 1283 | RTFileClose(MBRFile);
|
---|
| 1284 | if (RT_FAILURE(vrc))
|
---|
| 1285 | {
|
---|
[32701] | 1286 | RTMsgError("Cannot read replacement MBR file '%s': %Rrc", pszMBRFilename, vrc);
|
---|
[29649] | 1287 | goto out;
|
---|
| 1288 | }
|
---|
| 1289 | }
|
---|
| 1290 | pPartDesc->pvPartitionData = pPartData;
|
---|
| 1291 | }
|
---|
| 1292 |
|
---|
| 1293 | if (PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
|
---|
| 1294 | {
|
---|
| 1295 | /* Suppress exporting the actual extended partition. Only
|
---|
| 1296 | * logical partitions should be processed. However completely
|
---|
| 1297 | * ignoring it leads to leaving out the EBR data. */
|
---|
| 1298 | continue;
|
---|
| 1299 | }
|
---|
| 1300 |
|
---|
| 1301 | /* set up values for non-relative device names */
|
---|
[31539] | 1302 | const char *pszRawName = rawdisk.c_str();
|
---|
[29649] | 1303 | uint64_t uStartOffset = partitions.aPartitions[i].uStart * 512;
|
---|
| 1304 |
|
---|
| 1305 | pPartDesc = appendPartDesc(&RawDescriptor.cPartDescs,
|
---|
| 1306 | &RawDescriptor.pPartDescs);
|
---|
| 1307 | if (!pPartDesc)
|
---|
| 1308 | {
|
---|
[32701] | 1309 | RTMsgError("Out of memory allocating the partition list for '%s'", rawdisk.c_str());
|
---|
[29649] | 1310 | vrc = VERR_NO_MEMORY;
|
---|
| 1311 | goto out;
|
---|
| 1312 | }
|
---|
| 1313 |
|
---|
[8230] | 1314 | if (uPartitions & RT_BIT(partitions.aPartitions[i].uIndex))
|
---|
| 1315 | {
|
---|
| 1316 | if (fRelative)
|
---|
| 1317 | {
|
---|
| 1318 | #ifdef RT_OS_LINUX
|
---|
| 1319 | /* Refer to the correct partition and use offset 0. */
|
---|
[29649] | 1320 | char *psz;
|
---|
[33464] | 1321 | RTStrAPrintf(&psz, "%s%u", rawdisk.c_str(),
|
---|
| 1322 | partitions.aPartitions[i].uIndex);
|
---|
| 1323 | if (!psz)
|
---|
[8230] | 1324 | {
|
---|
[33464] | 1325 | vrc = VERR_NO_STR_MEMORY;
|
---|
[32701] | 1326 | RTMsgError("Cannot create reference to individual partition %u, rc=%Rrc",
|
---|
[33464] | 1327 | partitions.aPartitions[i].uIndex, vrc);
|
---|
[11131] | 1328 | goto out;
|
---|
[8230] | 1329 | }
|
---|
[29649] | 1330 | pszRawName = psz;
|
---|
| 1331 | uStartOffset = 0;
|
---|
[21556] | 1332 | #elif defined(RT_OS_DARWIN)
|
---|
| 1333 | /* Refer to the correct partition and use offset 0. */
|
---|
[29649] | 1334 | char *psz;
|
---|
[33464] | 1335 | RTStrAPrintf(&psz, "%ss%u", rawdisk.c_str(),
|
---|
| 1336 | partitions.aPartitions[i].uIndex);
|
---|
| 1337 | if (!psz)
|
---|
[21556] | 1338 | {
|
---|
[33464] | 1339 | vrc = VERR_NO_STR_MEMORY;
|
---|
[32701] | 1340 | RTMsgError("Cannot create reference to individual partition %u, rc=%Rrc",
|
---|
[21556] | 1341 | partitions.aPartitions[i].uIndex, vrc);
|
---|
| 1342 | goto out;
|
---|
| 1343 | }
|
---|
[29649] | 1344 | pszRawName = psz;
|
---|
| 1345 | uStartOffset = 0;
|
---|
[8230] | 1346 | #else
|
---|
[29649] | 1347 | /** @todo not implemented for other hosts. Treat just like
|
---|
| 1348 | * not specified (this code is actually never reached). */
|
---|
[8230] | 1349 | #endif
|
---|
| 1350 | }
|
---|
[29649] | 1351 |
|
---|
| 1352 | pPartDesc->pszRawDevice = pszRawName;
|
---|
| 1353 | pPartDesc->uStartOffset = uStartOffset;
|
---|
[8230] | 1354 | }
|
---|
| 1355 | else
|
---|
| 1356 | {
|
---|
[29649] | 1357 | pPartDesc->pszRawDevice = NULL;
|
---|
| 1358 | pPartDesc->uStartOffset = 0;
|
---|
[8230] | 1359 | }
|
---|
[29649] | 1360 |
|
---|
| 1361 | pPartDesc->uStart = partitions.aPartitions[i].uStart * 512;
|
---|
| 1362 | pPartDesc->cbData = partitions.aPartitions[i].uSize * 512;
|
---|
| 1363 | }
|
---|
| 1364 |
|
---|
| 1365 | /* Sort data areas in ascending order of start. */
|
---|
| 1366 | for (unsigned i = 0; i < RawDescriptor.cPartDescs-1; i++)
|
---|
| 1367 | {
|
---|
| 1368 | unsigned uMinIdx = i;
|
---|
| 1369 | uint64_t uMinVal = RawDescriptor.pPartDescs[i].uStart;
|
---|
| 1370 | for (unsigned j = i + 1; j < RawDescriptor.cPartDescs; j++)
|
---|
[8230] | 1371 | {
|
---|
[29649] | 1372 | if (RawDescriptor.pPartDescs[j].uStart < uMinVal)
|
---|
| 1373 | {
|
---|
| 1374 | uMinIdx = j;
|
---|
| 1375 | uMinVal = RawDescriptor.pPartDescs[j].uStart;
|
---|
| 1376 | }
|
---|
[8230] | 1377 | }
|
---|
[29649] | 1378 | if (uMinIdx != i)
|
---|
[8230] | 1379 | {
|
---|
[29649] | 1380 | /* Swap entries at index i and uMinIdx. */
|
---|
| 1381 | VBOXHDDRAWPARTDESC tmp;
|
---|
| 1382 | memcpy(&tmp, &RawDescriptor.pPartDescs[i], sizeof(tmp));
|
---|
| 1383 | memcpy(&RawDescriptor.pPartDescs[i], &RawDescriptor.pPartDescs[uMinIdx], sizeof(tmp));
|
---|
| 1384 | memcpy(&RawDescriptor.pPartDescs[uMinIdx], &tmp, sizeof(tmp));
|
---|
| 1385 | }
|
---|
| 1386 | }
|
---|
| 1387 |
|
---|
| 1388 | /* Have a second go at MBR/EPT area clipping. Now that the data areas
|
---|
| 1389 | * are sorted this is much easier to get 100% right. */
|
---|
| 1390 | for (unsigned i = 0; i < RawDescriptor.cPartDescs-1; i++)
|
---|
| 1391 | {
|
---|
| 1392 | if (RawDescriptor.pPartDescs[i].pvPartitionData)
|
---|
| 1393 | {
|
---|
| 1394 | RawDescriptor.pPartDescs[i].cbData = RT_MIN(RawDescriptor.pPartDescs[i+1].uStart - RawDescriptor.pPartDescs[i].uStart, RawDescriptor.pPartDescs[i].cbData);
|
---|
| 1395 | if (!RawDescriptor.pPartDescs[i].cbData)
|
---|
[11131] | 1396 | {
|
---|
[32701] | 1397 | RTMsgError("MBR/EPT overlaps with data area");
|
---|
[29649] | 1398 | vrc = VERR_INVALID_PARAMETER;
|
---|
[11131] | 1399 | goto out;
|
---|
| 1400 | }
|
---|
[8230] | 1401 | }
|
---|
| 1402 | }
|
---|
| 1403 | }
|
---|
| 1404 |
|
---|
| 1405 | RTFileClose(RawFile);
|
---|
| 1406 |
|
---|
[29649] | 1407 | #ifdef DEBUG_klaus
|
---|
| 1408 | RTPrintf("# start length startoffset partdataptr device\n");
|
---|
| 1409 | for (unsigned i = 0; i < RawDescriptor.cPartDescs; i++)
|
---|
| 1410 | {
|
---|
| 1411 | RTPrintf("%2u %14RU64 %14RU64 %14RU64 %#18p %s\n", i,
|
---|
| 1412 | RawDescriptor.pPartDescs[i].uStart,
|
---|
| 1413 | RawDescriptor.pPartDescs[i].cbData,
|
---|
| 1414 | RawDescriptor.pPartDescs[i].uStartOffset,
|
---|
| 1415 | RawDescriptor.pPartDescs[i].pvPartitionData,
|
---|
| 1416 | RawDescriptor.pPartDescs[i].pszRawDevice);
|
---|
| 1417 | }
|
---|
| 1418 | #endif
|
---|
| 1419 |
|
---|
[10715] | 1420 | VDINTERFACE vdInterfaceError;
|
---|
| 1421 | VDINTERFACEERROR vdInterfaceErrorCallbacks;
|
---|
| 1422 | vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
|
---|
| 1423 | vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
| 1424 | vdInterfaceErrorCallbacks.pfnError = handleVDError;
|
---|
[29649] | 1425 | vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
|
---|
[10715] | 1426 |
|
---|
[11435] | 1427 | vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
|
---|
| 1428 | &vdInterfaceErrorCallbacks, NULL, &pVDIfs);
|
---|
[10715] | 1429 | AssertRC(vrc);
|
---|
| 1430 |
|
---|
[33524] | 1431 | vrc = VDCreate(pVDIfs, VDTYPE_HDD, &pDisk); /* Raw VMDK's are harddisk only. */
|
---|
[13835] | 1432 | if (RT_FAILURE(vrc))
|
---|
[8230] | 1433 | {
|
---|
[32701] | 1434 | RTMsgError("Cannot create the virtual disk container: %Rrc", vrc);
|
---|
[11131] | 1435 | goto out;
|
---|
[8230] | 1436 | }
|
---|
| 1437 |
|
---|
| 1438 | Assert(RT_MIN(cbSize / 512 / 16 / 63, 16383) -
|
---|
| 1439 | (unsigned int)RT_MIN(cbSize / 512 / 16 / 63, 16383) == 0);
|
---|
[32536] | 1440 | VDGEOMETRY PCHS, LCHS;
|
---|
[8230] | 1441 | PCHS.cCylinders = (unsigned int)RT_MIN(cbSize / 512 / 16 / 63, 16383);
|
---|
| 1442 | PCHS.cHeads = 16;
|
---|
| 1443 | PCHS.cSectors = 63;
|
---|
| 1444 | LCHS.cCylinders = 0;
|
---|
| 1445 | LCHS.cHeads = 0;
|
---|
| 1446 | LCHS.cSectors = 0;
|
---|
[32531] | 1447 | vrc = VDCreateBase(pDisk, "VMDK", filename.c_str(), cbSize,
|
---|
[17970] | 1448 | VD_IMAGE_FLAGS_FIXED | VD_VMDK_IMAGE_FLAGS_RAWDISK,
|
---|
| 1449 | (char *)&RawDescriptor, &PCHS, &LCHS, NULL,
|
---|
| 1450 | VD_OPEN_FLAGS_NORMAL, NULL, NULL);
|
---|
[13835] | 1451 | if (RT_FAILURE(vrc))
|
---|
[8230] | 1452 | {
|
---|
[32701] | 1453 | RTMsgError("Cannot create the raw disk VMDK: %Rrc", vrc);
|
---|
[11131] | 1454 | goto out;
|
---|
[8230] | 1455 | }
|
---|
[32531] | 1456 | RTPrintf("RAW host disk access VMDK file %s created successfully.\n", filename.c_str());
|
---|
[8230] | 1457 |
|
---|
| 1458 | VDCloseAll(pDisk);
|
---|
| 1459 |
|
---|
| 1460 | /* Clean up allocated memory etc. */
|
---|
| 1461 | if (pszPartitions)
|
---|
| 1462 | {
|
---|
[29649] | 1463 | for (unsigned i = 0; i < RawDescriptor.cPartDescs; i++)
|
---|
[8230] | 1464 | {
|
---|
[29649] | 1465 | /* Free memory allocated for relative device name. */
|
---|
[29651] | 1466 | if (fRelative && RawDescriptor.pPartDescs[i].pszRawDevice)
|
---|
[29649] | 1467 | RTStrFree((char *)(void *)RawDescriptor.pPartDescs[i].pszRawDevice);
|
---|
| 1468 | if (RawDescriptor.pPartDescs[i].pvPartitionData)
|
---|
[29650] | 1469 | RTMemFree((void *)RawDescriptor.pPartDescs[i].pvPartitionData);
|
---|
[8230] | 1470 | }
|
---|
[29649] | 1471 | if (RawDescriptor.pPartDescs)
|
---|
| 1472 | RTMemFree(RawDescriptor.pPartDescs);
|
---|
[8230] | 1473 | }
|
---|
| 1474 |
|
---|
| 1475 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
[11131] | 1476 |
|
---|
| 1477 | out:
|
---|
[32701] | 1478 | RTMsgError("The raw disk vmdk file was not created");
|
---|
[13835] | 1479 | return RT_SUCCESS(vrc) ? 0 : 1;
|
---|
[8230] | 1480 | }
|
---|
| 1481 |
|
---|
[9493] | 1482 | static int CmdRenameVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
[9047] | 1483 | {
|
---|
[32531] | 1484 | Utf8Str src;
|
---|
| 1485 | Utf8Str dst;
|
---|
[9047] | 1486 | /* Parse the arguments. */
|
---|
| 1487 | for (int i = 0; i < argc; i++)
|
---|
| 1488 | {
|
---|
| 1489 | if (strcmp(argv[i], "-from") == 0)
|
---|
| 1490 | {
|
---|
| 1491 | if (argc <= i + 1)
|
---|
| 1492 | {
|
---|
| 1493 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 1494 | }
|
---|
| 1495 | i++;
|
---|
| 1496 | src = argv[i];
|
---|
| 1497 | }
|
---|
| 1498 | else if (strcmp(argv[i], "-to") == 0)
|
---|
| 1499 | {
|
---|
| 1500 | if (argc <= i + 1)
|
---|
| 1501 | {
|
---|
| 1502 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 1503 | }
|
---|
| 1504 | i++;
|
---|
| 1505 | dst = argv[i];
|
---|
| 1506 | }
|
---|
| 1507 | else
|
---|
| 1508 | {
|
---|
[32531] | 1509 | return errorSyntax(USAGE_RENAMEVMDK, "Invalid parameter '%s'", argv[i]);
|
---|
[9047] | 1510 | }
|
---|
| 1511 | }
|
---|
| 1512 |
|
---|
| 1513 | if (src.isEmpty())
|
---|
| 1514 | return errorSyntax(USAGE_RENAMEVMDK, "Mandatory parameter -from missing");
|
---|
| 1515 | if (dst.isEmpty())
|
---|
| 1516 | return errorSyntax(USAGE_RENAMEVMDK, "Mandatory parameter -to missing");
|
---|
| 1517 |
|
---|
| 1518 | PVBOXHDD pDisk = NULL;
|
---|
| 1519 |
|
---|
[11435] | 1520 | PVDINTERFACE pVDIfs = NULL;
|
---|
[10715] | 1521 | VDINTERFACE vdInterfaceError;
|
---|
| 1522 | VDINTERFACEERROR vdInterfaceErrorCallbacks;
|
---|
| 1523 | vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
|
---|
| 1524 | vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
| 1525 | vdInterfaceErrorCallbacks.pfnError = handleVDError;
|
---|
[29649] | 1526 | vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
|
---|
[10715] | 1527 |
|
---|
[11435] | 1528 | int vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
|
---|
| 1529 | &vdInterfaceErrorCallbacks, NULL, &pVDIfs);
|
---|
[10715] | 1530 | AssertRC(vrc);
|
---|
| 1531 |
|
---|
[33524] | 1532 | vrc = VDCreate(pVDIfs, VDTYPE_HDD, &pDisk);
|
---|
[13835] | 1533 | if (RT_FAILURE(vrc))
|
---|
[9047] | 1534 | {
|
---|
[32701] | 1535 | RTMsgError("Cannot create the virtual disk container: %Rrc", vrc);
|
---|
[9047] | 1536 | return vrc;
|
---|
| 1537 | }
|
---|
| 1538 | else
|
---|
| 1539 | {
|
---|
[32531] | 1540 | vrc = VDOpen(pDisk, "VMDK", src.c_str(), VD_OPEN_FLAGS_NORMAL, NULL);
|
---|
[13835] | 1541 | if (RT_FAILURE(vrc))
|
---|
[9047] | 1542 | {
|
---|
[32701] | 1543 | RTMsgError("Cannot create the source image: %Rrc", vrc);
|
---|
[9047] | 1544 | }
|
---|
| 1545 | else
|
---|
| 1546 | {
|
---|
[33082] | 1547 | vrc = VDCopy(pDisk, 0, pDisk, "VMDK", dst.c_str(), true, 0,
|
---|
| 1548 | VD_IMAGE_FLAGS_NONE, NULL, VD_OPEN_FLAGS_NORMAL,
|
---|
| 1549 | NULL, NULL, NULL);
|
---|
[13835] | 1550 | if (RT_FAILURE(vrc))
|
---|
[9047] | 1551 | {
|
---|
[32701] | 1552 | RTMsgError("Cannot rename the image: %Rrc", vrc);
|
---|
[9047] | 1553 | }
|
---|
| 1554 | }
|
---|
| 1555 | }
|
---|
| 1556 | VDCloseAll(pDisk);
|
---|
| 1557 | return vrc;
|
---|
| 1558 | }
|
---|
| 1559 |
|
---|
[11066] | 1560 | static int CmdConvertToRaw(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
| 1561 | {
|
---|
[32531] | 1562 | Utf8Str srcformat;
|
---|
| 1563 | Utf8Str src;
|
---|
| 1564 | Utf8Str dst;
|
---|
[11066] | 1565 | bool fWriteToStdOut = false;
|
---|
| 1566 |
|
---|
| 1567 | /* Parse the arguments. */
|
---|
| 1568 | for (int i = 0; i < argc; i++)
|
---|
| 1569 | {
|
---|
| 1570 | if (strcmp(argv[i], "-format") == 0)
|
---|
| 1571 | {
|
---|
| 1572 | if (argc <= i + 1)
|
---|
| 1573 | {
|
---|
| 1574 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 1575 | }
|
---|
| 1576 | i++;
|
---|
| 1577 | srcformat = argv[i];
|
---|
| 1578 | }
|
---|
| 1579 | else if (src.isEmpty())
|
---|
| 1580 | {
|
---|
| 1581 | src = argv[i];
|
---|
| 1582 | }
|
---|
| 1583 | else if (dst.isEmpty())
|
---|
| 1584 | {
|
---|
| 1585 | dst = argv[i];
|
---|
| 1586 | #ifdef ENABLE_CONVERT_RAW_TO_STDOUT
|
---|
| 1587 | if (!strcmp(argv[i], "stdout"))
|
---|
| 1588 | fWriteToStdOut = true;
|
---|
| 1589 | #endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
|
---|
| 1590 | }
|
---|
| 1591 | else
|
---|
| 1592 | {
|
---|
[32531] | 1593 | return errorSyntax(USAGE_CONVERTTORAW, "Invalid parameter '%s'", argv[i]);
|
---|
[11066] | 1594 | }
|
---|
| 1595 | }
|
---|
| 1596 |
|
---|
| 1597 | if (src.isEmpty())
|
---|
| 1598 | return errorSyntax(USAGE_CONVERTTORAW, "Mandatory filename parameter missing");
|
---|
| 1599 | if (dst.isEmpty())
|
---|
| 1600 | return errorSyntax(USAGE_CONVERTTORAW, "Mandatory outputfile parameter missing");
|
---|
| 1601 |
|
---|
| 1602 | PVBOXHDD pDisk = NULL;
|
---|
| 1603 |
|
---|
[11435] | 1604 | PVDINTERFACE pVDIfs = NULL;
|
---|
[11066] | 1605 | VDINTERFACE vdInterfaceError;
|
---|
| 1606 | VDINTERFACEERROR vdInterfaceErrorCallbacks;
|
---|
| 1607 | vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
|
---|
| 1608 | vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
| 1609 | vdInterfaceErrorCallbacks.pfnError = handleVDError;
|
---|
[29649] | 1610 | vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
|
---|
[11066] | 1611 |
|
---|
[11435] | 1612 | int vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
|
---|
| 1613 | &vdInterfaceErrorCallbacks, NULL, &pVDIfs);
|
---|
[11066] | 1614 | AssertRC(vrc);
|
---|
| 1615 |
|
---|
[33524] | 1616 | /** @todo: Support convert to raw for floppy and DVD images too. */
|
---|
| 1617 | vrc = VDCreate(pVDIfs, VDTYPE_HDD, &pDisk);
|
---|
[13835] | 1618 | if (RT_FAILURE(vrc))
|
---|
[11066] | 1619 | {
|
---|
[32701] | 1620 | RTMsgError("Cannot create the virtual disk container: %Rrc", vrc);
|
---|
[11138] | 1621 | return 1;
|
---|
[11066] | 1622 | }
|
---|
| 1623 |
|
---|
| 1624 | /* Open raw output file. */
|
---|
| 1625 | RTFILE outFile;
|
---|
| 1626 | vrc = VINF_SUCCESS;
|
---|
| 1627 | if (fWriteToStdOut)
|
---|
| 1628 | outFile = 1;
|
---|
| 1629 | else
|
---|
[32531] | 1630 | vrc = RTFileOpen(&outFile, dst.c_str(), RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
|
---|
[13835] | 1631 | if (RT_FAILURE(vrc))
|
---|
[11066] | 1632 | {
|
---|
| 1633 | VDCloseAll(pDisk);
|
---|
[32701] | 1634 | RTMsgError("Cannot create destination file \"%s\": %Rrc", dst.c_str(), vrc);
|
---|
[11138] | 1635 | return 1;
|
---|
[11066] | 1636 | }
|
---|
| 1637 |
|
---|
| 1638 | if (srcformat.isEmpty())
|
---|
| 1639 | {
|
---|
| 1640 | char *pszFormat = NULL;
|
---|
[33524] | 1641 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
[32536] | 1642 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
[33524] | 1643 | src.c_str(), &pszFormat, &enmType);
|
---|
| 1644 | if (RT_FAILURE(vrc) || enmType != VDTYPE_HDD)
|
---|
[11066] | 1645 | {
|
---|
| 1646 | VDCloseAll(pDisk);
|
---|
| 1647 | if (!fWriteToStdOut)
|
---|
| 1648 | {
|
---|
| 1649 | RTFileClose(outFile);
|
---|
[32531] | 1650 | RTFileDelete(dst.c_str());
|
---|
[11066] | 1651 | }
|
---|
[33524] | 1652 | if (RT_FAILURE(vrc))
|
---|
| 1653 | RTMsgError("No file format specified and autodetect failed - please specify format: %Rrc", vrc);
|
---|
| 1654 | else
|
---|
| 1655 | RTMsgError("Only converting harddisk images is supported");
|
---|
[11138] | 1656 | return 1;
|
---|
[11066] | 1657 | }
|
---|
| 1658 | srcformat = pszFormat;
|
---|
| 1659 | RTStrFree(pszFormat);
|
---|
| 1660 | }
|
---|
[32531] | 1661 | vrc = VDOpen(pDisk, srcformat.c_str(), src.c_str(), VD_OPEN_FLAGS_READONLY, NULL);
|
---|
[13835] | 1662 | if (RT_FAILURE(vrc))
|
---|
[11066] | 1663 | {
|
---|
| 1664 | VDCloseAll(pDisk);
|
---|
| 1665 | if (!fWriteToStdOut)
|
---|
| 1666 | {
|
---|
| 1667 | RTFileClose(outFile);
|
---|
[32531] | 1668 | RTFileDelete(dst.c_str());
|
---|
[11066] | 1669 | }
|
---|
[32701] | 1670 | RTMsgError("Cannot open the source image: %Rrc", vrc);
|
---|
[11138] | 1671 | return 1;
|
---|
[11066] | 1672 | }
|
---|
| 1673 |
|
---|
| 1674 | uint64_t cbSize = VDGetSize(pDisk, VD_LAST_IMAGE);
|
---|
| 1675 | uint64_t offFile = 0;
|
---|
| 1676 | #define RAW_BUFFER_SIZE _128K
|
---|
[27797] | 1677 | size_t cbBuf = RAW_BUFFER_SIZE;
|
---|
[11066] | 1678 | void *pvBuf = RTMemAlloc(cbBuf);
|
---|
| 1679 | if (pvBuf)
|
---|
| 1680 | {
|
---|
[32701] | 1681 | RTStrmPrintf(g_pStdErr, "Converting image \"%s\" with size %RU64 bytes (%RU64MB) to raw...\n", src.c_str(), cbSize, (cbSize + _1M - 1) / _1M);
|
---|
[11066] | 1682 | while (offFile < cbSize)
|
---|
| 1683 | {
|
---|
[27797] | 1684 | size_t cb = (size_t)RT_MIN(cbSize - offFile, cbBuf);
|
---|
[11066] | 1685 | vrc = VDRead(pDisk, offFile, pvBuf, cb);
|
---|
[13835] | 1686 | if (RT_FAILURE(vrc))
|
---|
[11066] | 1687 | break;
|
---|
| 1688 | vrc = RTFileWrite(outFile, pvBuf, cb, NULL);
|
---|
[13835] | 1689 | if (RT_FAILURE(vrc))
|
---|
[11066] | 1690 | break;
|
---|
| 1691 | offFile += cb;
|
---|
| 1692 | }
|
---|
[13835] | 1693 | if (RT_FAILURE(vrc))
|
---|
[11066] | 1694 | {
|
---|
| 1695 | VDCloseAll(pDisk);
|
---|
| 1696 | if (!fWriteToStdOut)
|
---|
| 1697 | {
|
---|
| 1698 | RTFileClose(outFile);
|
---|
[32531] | 1699 | RTFileDelete(dst.c_str());
|
---|
[11066] | 1700 | }
|
---|
[32701] | 1701 | RTMsgError("Cannot copy image data: %Rrc", vrc);
|
---|
[11138] | 1702 | return 1;
|
---|
[11066] | 1703 | }
|
---|
| 1704 | }
|
---|
| 1705 | else
|
---|
| 1706 | {
|
---|
| 1707 | vrc = VERR_NO_MEMORY;
|
---|
| 1708 | VDCloseAll(pDisk);
|
---|
| 1709 | if (!fWriteToStdOut)
|
---|
| 1710 | {
|
---|
| 1711 | RTFileClose(outFile);
|
---|
[32531] | 1712 | RTFileDelete(dst.c_str());
|
---|
[11066] | 1713 | }
|
---|
[32701] | 1714 | RTMsgError("Out of memory allocating read buffer");
|
---|
[11138] | 1715 | return 1;
|
---|
[11066] | 1716 | }
|
---|
| 1717 |
|
---|
| 1718 | if (!fWriteToStdOut)
|
---|
| 1719 | RTFileClose(outFile);
|
---|
| 1720 | VDCloseAll(pDisk);
|
---|
[11138] | 1721 | return 0;
|
---|
[11066] | 1722 | }
|
---|
| 1723 |
|
---|
[15602] | 1724 | static int CmdConvertHardDisk(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
| 1725 | {
|
---|
[32531] | 1726 | Utf8Str srcformat;
|
---|
| 1727 | Utf8Str dstformat;
|
---|
| 1728 | Utf8Str src;
|
---|
| 1729 | Utf8Str dst;
|
---|
[15602] | 1730 | int vrc;
|
---|
| 1731 | PVBOXHDD pSrcDisk = NULL;
|
---|
| 1732 | PVBOXHDD pDstDisk = NULL;
|
---|
[33524] | 1733 | VDTYPE enmSrcType = VDTYPE_INVALID;
|
---|
[15602] | 1734 |
|
---|
| 1735 | /* Parse the arguments. */
|
---|
| 1736 | for (int i = 0; i < argc; i++)
|
---|
| 1737 | {
|
---|
| 1738 | if (strcmp(argv[i], "-srcformat") == 0)
|
---|
| 1739 | {
|
---|
| 1740 | if (argc <= i + 1)
|
---|
| 1741 | {
|
---|
| 1742 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 1743 | }
|
---|
| 1744 | i++;
|
---|
| 1745 | srcformat = argv[i];
|
---|
| 1746 | }
|
---|
| 1747 | else if (strcmp(argv[i], "-dstformat") == 0)
|
---|
| 1748 | {
|
---|
| 1749 | if (argc <= i + 1)
|
---|
| 1750 | {
|
---|
| 1751 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
| 1752 | }
|
---|
| 1753 | i++;
|
---|
| 1754 | dstformat = argv[i];
|
---|
| 1755 | }
|
---|
| 1756 | else if (src.isEmpty())
|
---|
| 1757 | {
|
---|
| 1758 | src = argv[i];
|
---|
| 1759 | }
|
---|
| 1760 | else if (dst.isEmpty())
|
---|
| 1761 | {
|
---|
| 1762 | dst = argv[i];
|
---|
| 1763 | }
|
---|
| 1764 | else
|
---|
| 1765 | {
|
---|
[32531] | 1766 | return errorSyntax(USAGE_CONVERTHD, "Invalid parameter '%s'", argv[i]);
|
---|
[15602] | 1767 | }
|
---|
| 1768 | }
|
---|
| 1769 |
|
---|
| 1770 | if (src.isEmpty())
|
---|
| 1771 | return errorSyntax(USAGE_CONVERTHD, "Mandatory input image parameter missing");
|
---|
| 1772 | if (dst.isEmpty())
|
---|
| 1773 | return errorSyntax(USAGE_CONVERTHD, "Mandatory output image parameter missing");
|
---|
| 1774 |
|
---|
| 1775 |
|
---|
| 1776 | PVDINTERFACE pVDIfs = NULL;
|
---|
| 1777 | VDINTERFACE vdInterfaceError;
|
---|
| 1778 | VDINTERFACEERROR vdInterfaceErrorCallbacks;
|
---|
| 1779 | vdInterfaceErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
|
---|
| 1780 | vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
| 1781 | vdInterfaceErrorCallbacks.pfnError = handleVDError;
|
---|
[29649] | 1782 | vdInterfaceErrorCallbacks.pfnMessage = handleVDMessage;
|
---|
[15602] | 1783 |
|
---|
| 1784 | vrc = VDInterfaceAdd(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
|
---|
| 1785 | &vdInterfaceErrorCallbacks, NULL, &pVDIfs);
|
---|
| 1786 | AssertRC(vrc);
|
---|
| 1787 |
|
---|
| 1788 | do
|
---|
| 1789 | {
|
---|
| 1790 | /* Try to determine input image format */
|
---|
| 1791 | if (srcformat.isEmpty())
|
---|
| 1792 | {
|
---|
| 1793 | char *pszFormat = NULL;
|
---|
[32536] | 1794 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
[33524] | 1795 | src.c_str(), &pszFormat, &enmSrcType);
|
---|
[15602] | 1796 | if (RT_FAILURE(vrc))
|
---|
| 1797 | {
|
---|
[32701] | 1798 | RTMsgError("No file format specified and autodetect failed - please specify format: %Rrc", vrc);
|
---|
[15602] | 1799 | break;
|
---|
| 1800 | }
|
---|
| 1801 | srcformat = pszFormat;
|
---|
| 1802 | RTStrFree(pszFormat);
|
---|
| 1803 | }
|
---|
| 1804 |
|
---|
[33524] | 1805 | vrc = VDCreate(pVDIfs, enmSrcType, &pSrcDisk);
|
---|
[15602] | 1806 | if (RT_FAILURE(vrc))
|
---|
| 1807 | {
|
---|
[32701] | 1808 | RTMsgError("Cannot create the source virtual disk container: %Rrc", vrc);
|
---|
[15602] | 1809 | break;
|
---|
| 1810 | }
|
---|
| 1811 |
|
---|
| 1812 | /* Open the input image */
|
---|
[32531] | 1813 | vrc = VDOpen(pSrcDisk, srcformat.c_str(), src.c_str(), VD_OPEN_FLAGS_READONLY, NULL);
|
---|
[15602] | 1814 | if (RT_FAILURE(vrc))
|
---|
| 1815 | {
|
---|
[32701] | 1816 | RTMsgError("Cannot open the source image: %Rrc", vrc);
|
---|
[15602] | 1817 | break;
|
---|
| 1818 | }
|
---|
| 1819 |
|
---|
| 1820 | /* Output format defaults to VDI */
|
---|
| 1821 | if (dstformat.isEmpty())
|
---|
| 1822 | dstformat = "VDI";
|
---|
| 1823 |
|
---|
[33524] | 1824 | vrc = VDCreate(pVDIfs, enmSrcType, &pDstDisk);
|
---|
[15602] | 1825 | if (RT_FAILURE(vrc))
|
---|
| 1826 | {
|
---|
[32701] | 1827 | RTMsgError("Cannot create the destination virtual disk container: %Rrc", vrc);
|
---|
[15602] | 1828 | break;
|
---|
| 1829 | }
|
---|
| 1830 |
|
---|
| 1831 | uint64_t cbSize = VDGetSize(pSrcDisk, VD_LAST_IMAGE);
|
---|
[32701] | 1832 | RTStrmPrintf(g_pStdErr, "Converting image \"%s\" with size %RU64 bytes (%RU64MB)...\n", src.c_str(), cbSize, (cbSize + _1M - 1) / _1M);
|
---|
[15602] | 1833 |
|
---|
| 1834 | /* Create the output image */
|
---|
[32531] | 1835 | vrc = VDCopy(pSrcDisk, VD_LAST_IMAGE, pDstDisk, dstformat.c_str(),
|
---|
| 1836 | dst.c_str(), false, 0, VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED,
|
---|
[33082] | 1837 | NULL, VD_OPEN_FLAGS_NORMAL, NULL, NULL, NULL);
|
---|
[15602] | 1838 | if (RT_FAILURE(vrc))
|
---|
| 1839 | {
|
---|
[32701] | 1840 | RTMsgError("Cannot copy the image: %Rrc", vrc);
|
---|
[15602] | 1841 | break;
|
---|
| 1842 | }
|
---|
| 1843 | }
|
---|
| 1844 | while (0);
|
---|
| 1845 | if (pDstDisk)
|
---|
| 1846 | VDCloseAll(pDstDisk);
|
---|
| 1847 | if (pSrcDisk)
|
---|
| 1848 | VDCloseAll(pSrcDisk);
|
---|
| 1849 |
|
---|
| 1850 | return RT_SUCCESS(vrc) ? 0 : 1;
|
---|
| 1851 | }
|
---|
| 1852 |
|
---|
[1] | 1853 | /**
|
---|
[33540] | 1854 | * Unloads the necessary driver.
|
---|
[5895] | 1855 | *
|
---|
| 1856 | * @returns VBox status code
|
---|
| 1857 | */
|
---|
| 1858 | int CmdModUninstall(void)
|
---|
| 1859 | {
|
---|
[7854] | 1860 | int rc;
|
---|
[5895] | 1861 |
|
---|
[20864] | 1862 | rc = SUPR3Uninstall();
|
---|
[7418] | 1863 | if (RT_SUCCESS(rc))
|
---|
[5895] | 1864 | return 0;
|
---|
[7418] | 1865 | if (rc == VERR_NOT_IMPLEMENTED)
|
---|
[5895] | 1866 | return 0;
|
---|
[7418] | 1867 | return E_FAIL;
|
---|
[5895] | 1868 | }
|
---|
| 1869 |
|
---|
| 1870 | /**
|
---|
[33540] | 1871 | * Loads the necessary driver.
|
---|
[5895] | 1872 | *
|
---|
| 1873 | * @returns VBox status code
|
---|
| 1874 | */
|
---|
| 1875 | int CmdModInstall(void)
|
---|
| 1876 | {
|
---|
[7854] | 1877 | int rc;
|
---|
[5895] | 1878 |
|
---|
[20864] | 1879 | rc = SUPR3Install();
|
---|
[7418] | 1880 | if (RT_SUCCESS(rc))
|
---|
[5895] | 1881 | return 0;
|
---|
[7418] | 1882 | if (rc == VERR_NOT_IMPLEMENTED)
|
---|
[5895] | 1883 | return 0;
|
---|
[7418] | 1884 | return E_FAIL;
|
---|
[5895] | 1885 | }
|
---|
| 1886 |
|
---|
[30319] | 1887 | int CmdDebugLog(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
| 1888 | {
|
---|
| 1889 | /*
|
---|
| 1890 | * The first parameter is the name or UUID of a VM with a direct session
|
---|
| 1891 | * that we wish to open.
|
---|
| 1892 | */
|
---|
| 1893 | if (argc < 1)
|
---|
| 1894 | return errorSyntax(USAGE_DEBUGLOG, "Missing VM name/UUID");
|
---|
| 1895 |
|
---|
| 1896 | ComPtr<IMachine> ptrMachine;
|
---|
[33294] | 1897 | HRESULT rc;
|
---|
| 1898 | CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]).raw(),
|
---|
| 1899 | ptrMachine.asOutParam()), 1);
|
---|
[30319] | 1900 |
|
---|
[31019] | 1901 | CHECK_ERROR_RET(ptrMachine, LockMachine(aSession, LockType_Shared), 1);
|
---|
[30319] | 1902 |
|
---|
| 1903 | /*
|
---|
| 1904 | * Get the debugger interface.
|
---|
| 1905 | */
|
---|
| 1906 | ComPtr<IConsole> ptrConsole;
|
---|
| 1907 | CHECK_ERROR_RET(aSession, COMGETTER(Console)(ptrConsole.asOutParam()), 1);
|
---|
| 1908 |
|
---|
| 1909 | ComPtr<IMachineDebugger> ptrDebugger;
|
---|
| 1910 | CHECK_ERROR_RET(ptrConsole, COMGETTER(Debugger)(ptrDebugger.asOutParam()), 1);
|
---|
| 1911 |
|
---|
| 1912 | /*
|
---|
| 1913 | * Parse the command.
|
---|
| 1914 | */
|
---|
| 1915 | bool fEnablePresent = false;
|
---|
| 1916 | bool fEnable = false;
|
---|
| 1917 | bool fFlagsPresent = false;
|
---|
| 1918 | iprt::MiniString strFlags;
|
---|
| 1919 | bool fGroupsPresent = false;
|
---|
| 1920 | iprt::MiniString strGroups;
|
---|
| 1921 | bool fDestsPresent = false;
|
---|
| 1922 | iprt::MiniString strDests;
|
---|
| 1923 |
|
---|
| 1924 | static const RTGETOPTDEF s_aOptions[] =
|
---|
| 1925 | {
|
---|
| 1926 | { "--disable", 'E', RTGETOPT_REQ_NOTHING },
|
---|
| 1927 | { "--enable", 'e', RTGETOPT_REQ_NOTHING },
|
---|
| 1928 | { "--flags", 'f', RTGETOPT_REQ_STRING },
|
---|
| 1929 | { "--groups", 'g', RTGETOPT_REQ_STRING },
|
---|
| 1930 | { "--destinations", 'd', RTGETOPT_REQ_STRING }
|
---|
| 1931 | };
|
---|
| 1932 |
|
---|
| 1933 | int ch;
|
---|
| 1934 | RTGETOPTUNION ValueUnion;
|
---|
| 1935 | RTGETOPTSTATE GetState;
|
---|
| 1936 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
|
---|
| 1937 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
| 1938 | {
|
---|
| 1939 | switch (ch)
|
---|
| 1940 | {
|
---|
| 1941 | case 'e':
|
---|
| 1942 | fEnablePresent = true;
|
---|
| 1943 | fEnable = true;
|
---|
| 1944 | break;
|
---|
| 1945 |
|
---|
| 1946 | case 'E':
|
---|
| 1947 | fEnablePresent = true;
|
---|
| 1948 | fEnable = false;
|
---|
| 1949 | break;
|
---|
| 1950 |
|
---|
| 1951 | case 'f':
|
---|
| 1952 | fFlagsPresent = true;
|
---|
| 1953 | if (*ValueUnion.psz)
|
---|
| 1954 | {
|
---|
| 1955 | if (strFlags.isNotEmpty())
|
---|
| 1956 | strFlags.append(' ');
|
---|
| 1957 | strFlags.append(ValueUnion.psz);
|
---|
| 1958 | }
|
---|
| 1959 | break;
|
---|
| 1960 |
|
---|
| 1961 | case 'g':
|
---|
| 1962 | fGroupsPresent = true;
|
---|
| 1963 | if (*ValueUnion.psz)
|
---|
| 1964 | {
|
---|
| 1965 | if (strGroups.isNotEmpty())
|
---|
| 1966 | strGroups.append(' ');
|
---|
| 1967 | strGroups.append(ValueUnion.psz);
|
---|
| 1968 | }
|
---|
| 1969 | break;
|
---|
| 1970 |
|
---|
| 1971 | case 'd':
|
---|
| 1972 | fDestsPresent = true;
|
---|
| 1973 | if (*ValueUnion.psz)
|
---|
| 1974 | {
|
---|
| 1975 | if (strDests.isNotEmpty())
|
---|
| 1976 | strDests.append(' ');
|
---|
| 1977 | strDests.append(ValueUnion.psz);
|
---|
| 1978 | }
|
---|
| 1979 | break;
|
---|
| 1980 |
|
---|
| 1981 | default:
|
---|
| 1982 | return errorGetOpt(USAGE_DEBUGLOG , ch, &ValueUnion);
|
---|
| 1983 | }
|
---|
| 1984 | }
|
---|
| 1985 |
|
---|
| 1986 | /*
|
---|
| 1987 | * Do the job.
|
---|
| 1988 | */
|
---|
| 1989 | if (fEnablePresent && !fEnable)
|
---|
| 1990 | CHECK_ERROR_RET(ptrDebugger, COMSETTER(LogEnabled)(FALSE), 1);
|
---|
| 1991 |
|
---|
| 1992 | /** @todo flags, groups destination. */
|
---|
| 1993 | if (fFlagsPresent || fGroupsPresent || fDestsPresent)
|
---|
[32701] | 1994 | RTMsgWarning("One or more of the requested features are not implemented! Feel free to do this.");
|
---|
[30319] | 1995 |
|
---|
| 1996 | if (fEnablePresent && fEnable)
|
---|
[30321] | 1997 | CHECK_ERROR_RET(ptrDebugger, COMSETTER(LogEnabled)(TRUE), 1);
|
---|
[30319] | 1998 | return 0;
|
---|
| 1999 | }
|
---|
| 2000 |
|
---|
[5895] | 2001 | /**
|
---|
[33228] | 2002 | * Generate a SHA-256 password hash
|
---|
| 2003 | */
|
---|
| 2004 | int CmdGeneratePasswordHash(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
| 2005 | {
|
---|
| 2006 | /* one parameter, the password to hash */
|
---|
| 2007 | if (argc != 1)
|
---|
| 2008 | return errorSyntax(USAGE_PASSWORDHASH, "password to hash required");
|
---|
| 2009 |
|
---|
| 2010 | uint8_t abDigest[RTSHA256_HASH_SIZE];
|
---|
| 2011 | RTSha256(argv[0], strlen(argv[0]), abDigest);
|
---|
[33229] | 2012 | char pszDigest[RTSHA256_DIGEST_LEN + 1];
|
---|
[33228] | 2013 | RTSha256ToString(abDigest, pszDigest, sizeof(pszDigest));
|
---|
| 2014 | RTPrintf("Password hash: %s\n", pszDigest);
|
---|
[33294] | 2015 |
|
---|
[33228] | 2016 | return 0;
|
---|
| 2017 | }
|
---|
| 2018 |
|
---|
| 2019 | /**
|
---|
[1] | 2020 | * Wrapper for handling internal commands
|
---|
| 2021 | */
|
---|
[16052] | 2022 | int handleInternalCommands(HandlerArg *a)
|
---|
[1] | 2023 | {
|
---|
[5204] | 2024 | g_fInternalMode = true;
|
---|
[1] | 2025 |
|
---|
| 2026 | /* at least a command is required */
|
---|
[16052] | 2027 | if (a->argc < 1)
|
---|
[1] | 2028 | return errorSyntax(USAGE_ALL, "Command missing");
|
---|
| 2029 |
|
---|
| 2030 | /*
|
---|
| 2031 | * The 'string switch' on command name.
|
---|
| 2032 | */
|
---|
[16052] | 2033 | const char *pszCmd = a->argv[0];
|
---|
[1] | 2034 | if (!strcmp(pszCmd, "loadsyms"))
|
---|
[16052] | 2035 | return CmdLoadSyms(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
|
---|
[1] | 2036 | //if (!strcmp(pszCmd, "unloadsyms"))
|
---|
[16052] | 2037 | // return CmdUnloadSyms(argc - 1 , &a->argv[1]);
|
---|
[31461] | 2038 | if (!strcmp(pszCmd, "sethduuid") || !strcmp(pszCmd, "sethdparentuuid"))
|
---|
| 2039 | return CmdSetHDUUID(a->argc, &a->argv[0], a->virtualBox, a->session);
|
---|
[21806] | 2040 | if (!strcmp(pszCmd, "dumphdinfo"))
|
---|
| 2041 | return CmdDumpHDInfo(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
|
---|
[2675] | 2042 | if (!strcmp(pszCmd, "listpartitions"))
|
---|
[16052] | 2043 | return CmdListPartitions(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
|
---|
[2675] | 2044 | if (!strcmp(pszCmd, "createrawvmdk"))
|
---|
[16052] | 2045 | return CmdCreateRawVMDK(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
|
---|
[9047] | 2046 | if (!strcmp(pszCmd, "renamevmdk"))
|
---|
[16052] | 2047 | return CmdRenameVMDK(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
|
---|
[11066] | 2048 | if (!strcmp(pszCmd, "converttoraw"))
|
---|
[16052] | 2049 | return CmdConvertToRaw(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
|
---|
[15602] | 2050 | if (!strcmp(pszCmd, "converthd"))
|
---|
[16052] | 2051 | return CmdConvertHardDisk(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
|
---|
[5895] | 2052 | if (!strcmp(pszCmd, "modinstall"))
|
---|
| 2053 | return CmdModInstall();
|
---|
| 2054 | if (!strcmp(pszCmd, "moduninstall"))
|
---|
| 2055 | return CmdModUninstall();
|
---|
[30319] | 2056 | if (!strcmp(pszCmd, "debuglog"))
|
---|
| 2057 | return CmdDebugLog(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
|
---|
[33228] | 2058 | if (!strcmp(pszCmd, "passwordhash"))
|
---|
| 2059 | return CmdGeneratePasswordHash(a->argc - 1, &a->argv[1], a->virtualBox, a->session);
|
---|
[5895] | 2060 |
|
---|
[1] | 2061 | /* default: */
|
---|
[32531] | 2062 | return errorSyntax(USAGE_ALL, "Invalid command '%s'", a->argv[0]);
|
---|
[1] | 2063 | }
|
---|
[14732] | 2064 |
|
---|