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