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