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