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