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