1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: VBoxManage (command-line interface):
|
---|
4 | * VBoxInternalManage
|
---|
5 | *
|
---|
6 | * VBoxInternalManage used to be a second CLI for doing special tricks,
|
---|
7 | * not intended for general usage, only for assisting VBox developers.
|
---|
8 | * It is now integrated into VBoxManage.
|
---|
9 | */
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
13 | *
|
---|
14 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
15 | * available from http://www.virtualbox.org. This file is free software;
|
---|
16 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
17 | * General Public License (GPL) as published by the Free Software
|
---|
18 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
19 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
20 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
21 | *
|
---|
22 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
23 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
24 | * additional information or have any questions.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | /*******************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *******************************************************************************/
|
---|
32 | #include <VBox/com/com.h>
|
---|
33 | #include <VBox/com/string.h>
|
---|
34 | #include <VBox/com/Guid.h>
|
---|
35 | #include <VBox/com/ErrorInfo.h>
|
---|
36 |
|
---|
37 | #include <VBox/com/VirtualBox.h>
|
---|
38 |
|
---|
39 | #include <iprt/runtime.h>
|
---|
40 | #include <iprt/stream.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <iprt/uuid.h>
|
---|
43 | #include <VBox/err.h>
|
---|
44 |
|
---|
45 | #include <VBox/VBoxHDD.h>
|
---|
46 | #include <VBox/VBoxHDD-new.h>
|
---|
47 | #include <VBox/sup.h>
|
---|
48 |
|
---|
49 | #include "VBoxManage.h"
|
---|
50 |
|
---|
51 | /* Includes for the raw disk stuff. */
|
---|
52 | #ifdef RT_OS_WINDOWS
|
---|
53 | #include <windows.h>
|
---|
54 | #include <winioctl.h>
|
---|
55 | #elif defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
|
---|
56 | #include <errno.h>
|
---|
57 | #include <sys/ioctl.h>
|
---|
58 | #include <sys/types.h>
|
---|
59 | #include <sys/stat.h>
|
---|
60 | #include <fcntl.h>
|
---|
61 | #include <unistd.h>
|
---|
62 | #endif
|
---|
63 | #ifdef RT_OS_LINUX
|
---|
64 | #include <sys/utsname.h>
|
---|
65 | #include <linux/hdreg.h>
|
---|
66 | #include <linux/fs.h>
|
---|
67 | #endif /* RT_OS_LINUX */
|
---|
68 | #ifdef RT_OS_DARWIN
|
---|
69 | #include <sys/disk.h>
|
---|
70 | #endif /* RT_OS_DARWIN */
|
---|
71 | #ifdef RT_OS_SOLARIS
|
---|
72 | #include <stropts.h>
|
---|
73 | #include <sys/dkio.h>
|
---|
74 | #include <sys/vtoc.h>
|
---|
75 | #endif /* RT_OS_SOLARIS */
|
---|
76 |
|
---|
77 | using namespace com;
|
---|
78 |
|
---|
79 |
|
---|
80 | /** Macro for checking whether a partition is of extended type or not. */
|
---|
81 | #define PARTTYPE_IS_EXTENDED(x) ((x) == 0x05 || (x) == 0x0f || (x) == 0x85)
|
---|
82 |
|
---|
83 | /* Maximum number of partitions we can deal with. Ridiculously large number,
|
---|
84 | * but the memory consumption is rather low so who cares about never using
|
---|
85 | * most entries. */
|
---|
86 | #define HOSTPARTITION_MAX 100
|
---|
87 |
|
---|
88 |
|
---|
89 | typedef struct HOSTPARTITION
|
---|
90 | {
|
---|
91 | unsigned uIndex;
|
---|
92 | unsigned uType;
|
---|
93 | unsigned uStartCylinder;
|
---|
94 | unsigned uStartHead;
|
---|
95 | unsigned uStartSector;
|
---|
96 | unsigned uEndCylinder;
|
---|
97 | unsigned uEndHead;
|
---|
98 | unsigned uEndSector;
|
---|
99 | uint64_t uStart;
|
---|
100 | uint64_t uSize;
|
---|
101 | uint64_t uPartDataStart;
|
---|
102 | uint64_t cPartDataSectors;
|
---|
103 | } HOSTPARTITION, *PHOSTPARTITION;
|
---|
104 |
|
---|
105 | typedef struct HOSTPARTITIONS
|
---|
106 | {
|
---|
107 | unsigned cPartitions;
|
---|
108 | HOSTPARTITION aPartitions[HOSTPARTITION_MAX];
|
---|
109 | } HOSTPARTITIONS, *PHOSTPARTITIONS;
|
---|
110 |
|
---|
111 | /** flag whether we're in internal mode */
|
---|
112 | bool g_fInternalMode;
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Print the usage info.
|
---|
116 | */
|
---|
117 | void printUsageInternal(USAGECATEGORY u64Cmd)
|
---|
118 | {
|
---|
119 | RTPrintf("Usage: VBoxManage internalcommands <command> [command arguments]\n"
|
---|
120 | "\n"
|
---|
121 | "Commands:\n"
|
---|
122 | "\n"
|
---|
123 | "%s%s%s%s%s%s%s"
|
---|
124 | "WARNING: This is a development tool and shall only be used to analyse\n"
|
---|
125 | " problems. It is completely unsupported and will change in\n"
|
---|
126 | " incompatible ways without warning.\n",
|
---|
127 | (u64Cmd & USAGE_LOADSYMS) ?
|
---|
128 | " loadsyms <vmname>|<uuid> <symfile> [delta] [module] [module address]\n"
|
---|
129 | " This will instruct DBGF to load the given symbolfile\n"
|
---|
130 | " during initialization.\n"
|
---|
131 | "\n"
|
---|
132 | : "",
|
---|
133 | (u64Cmd & USAGE_UNLOADSYMS) ?
|
---|
134 | " unloadsyms <vmname>|<uuid> <symfile>\n"
|
---|
135 | " Removes <symfile> from the list of symbol files that\n"
|
---|
136 | " should be loaded during DBF initialization.\n"
|
---|
137 | "\n"
|
---|
138 | : "",
|
---|
139 | (u64Cmd & USAGE_SETVDIUUID) ?
|
---|
140 | " setvdiuuid <filepath>\n"
|
---|
141 | " Assigns a new UUID to the given VDI file. This way, multiple copies\n"
|
---|
142 | " of VDI containers can be registered.\n"
|
---|
143 | "\n"
|
---|
144 | : "",
|
---|
145 | (u64Cmd & USAGE_LISTPARTITIONS) ?
|
---|
146 | " listpartitions -rawdisk <diskname>\n"
|
---|
147 | " Lists all partitions on <diskname>.\n"
|
---|
148 | "\n"
|
---|
149 | : "",
|
---|
150 | (u64Cmd & USAGE_CREATERAWVMDK) ?
|
---|
151 | " createrawvmdk -filename <filename> -rawdisk <diskname>\n"
|
---|
152 | " [-partitions <list of partition numbers> [-mbr <filename>] ]\n"
|
---|
153 | " [-register] [-relative]\n"
|
---|
154 | " Creates a new VMDK image which gives access to an entite host disk (if\n"
|
---|
155 | " the parameter -partitions is not specified) or some partitions of a\n"
|
---|
156 | " host disk. If access to individual partitions is granted, then the\n"
|
---|
157 | " parameter -mbr can be used to specify an alternative MBR to be used\n"
|
---|
158 | " (the partitioning information in the MBR file is ignored).\n"
|
---|
159 | " The diskname is on Linux e.g. /dev/sda, and on Windows e.g.\n"
|
---|
160 | " \\\\.\\PhysicalDrive0).\n"
|
---|
161 | " On Linux host the parameter -relative causes a VMDK file to be created\n"
|
---|
162 | " which refers to individual partitions instead to the entire disk.\n"
|
---|
163 | " Optionally the created image can be immediately registered.\n"
|
---|
164 | " The necessary partition numbers can be queried with\n"
|
---|
165 | " VBoxManage internalcommands listpartitions\n"
|
---|
166 | "\n"
|
---|
167 | : "",
|
---|
168 | (u64Cmd & USAGE_RENAMEVMDK) ?
|
---|
169 | " renamevmdk -from <filename> -to <filename>\n"
|
---|
170 | " Renames an existing VMDK image, including the base file and all its extents.\n"
|
---|
171 | "\n"
|
---|
172 | : "",
|
---|
173 | #ifdef RT_OS_WINDOWS
|
---|
174 | (u64Cmd & USAGE_MODINSTALL) ?
|
---|
175 | " modinstall\n"
|
---|
176 | " Installs the neccessary driver for the host OS\n"
|
---|
177 | "\n"
|
---|
178 | : "",
|
---|
179 | (u64Cmd & USAGE_MODUNINSTALL) ?
|
---|
180 | " moduninstall\n"
|
---|
181 | " Deinstalls the driver\n"
|
---|
182 | "\n"
|
---|
183 | : ""
|
---|
184 | #else
|
---|
185 | "",
|
---|
186 | ""
|
---|
187 | #endif
|
---|
188 | );
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** @todo this is no longer necessary, we can enumerate extra data */
|
---|
192 | /**
|
---|
193 | * Finds a new unique key name.
|
---|
194 | *
|
---|
195 | * I don't think this is 100% race condition proof, but we assumes
|
---|
196 | * the user is not trying to push this point.
|
---|
197 | *
|
---|
198 | * @returns Result from the insert.
|
---|
199 | * @param pMachine The Machine object.
|
---|
200 | * @param pszKeyBase The base key.
|
---|
201 | * @param rKey Reference to the string object in which we will return the key.
|
---|
202 | */
|
---|
203 | static HRESULT NewUniqueKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, Utf8Str &rKey)
|
---|
204 | {
|
---|
205 | Bstr Keys;
|
---|
206 | HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
|
---|
207 | if (FAILED(hrc))
|
---|
208 | return hrc;
|
---|
209 |
|
---|
210 | /* if there are no keys, it's simple. */
|
---|
211 | if (Keys.isEmpty())
|
---|
212 | {
|
---|
213 | rKey = "1";
|
---|
214 | return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr("1"));
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* find a unique number - brute force rulez. */
|
---|
218 | Utf8Str KeysUtf8(Keys);
|
---|
219 | const char *pszKeys = RTStrStripL(KeysUtf8.raw());
|
---|
220 | for (unsigned i = 1; i < 1000000; i++)
|
---|
221 | {
|
---|
222 | char szKey[32];
|
---|
223 | size_t cchKey = RTStrPrintf(szKey, sizeof(szKey), "%#x", i);
|
---|
224 | const char *psz = strstr(pszKeys, szKey);
|
---|
225 | while (psz)
|
---|
226 | {
|
---|
227 | if ( ( psz == pszKeys
|
---|
228 | || psz[-1] == ' ')
|
---|
229 | && ( psz[cchKey] == ' '
|
---|
230 | || !psz[cchKey])
|
---|
231 | )
|
---|
232 | break;
|
---|
233 | psz = strstr(psz + cchKey, szKey);
|
---|
234 | }
|
---|
235 | if (!psz)
|
---|
236 | {
|
---|
237 | rKey = szKey;
|
---|
238 | Utf8StrFmt NewKeysUtf8("%s %s", pszKeys, szKey);
|
---|
239 | return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(NewKeysUtf8));
|
---|
240 | }
|
---|
241 | }
|
---|
242 | RTPrintf("Error: Cannot find unique key for '%s'!\n", pszKeyBase);
|
---|
243 | return E_FAIL;
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | #if 0
|
---|
248 | /**
|
---|
249 | * Remove a key.
|
---|
250 | *
|
---|
251 | * I don't think this isn't 100% race condition proof, but we assumes
|
---|
252 | * the user is not trying to push this point.
|
---|
253 | *
|
---|
254 | * @returns Result from the insert.
|
---|
255 | * @param pMachine The machine object.
|
---|
256 | * @param pszKeyBase The base key.
|
---|
257 | * @param pszKey The key to remove.
|
---|
258 | */
|
---|
259 | static HRESULT RemoveKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey)
|
---|
260 | {
|
---|
261 | Bstr Keys;
|
---|
262 | HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
|
---|
263 | if (FAILED(hrc))
|
---|
264 | return hrc;
|
---|
265 |
|
---|
266 | /* if there are no keys, it's simple. */
|
---|
267 | if (Keys.isEmpty())
|
---|
268 | return S_OK;
|
---|
269 |
|
---|
270 | char *pszKeys;
|
---|
271 | int rc = RTUtf16ToUtf8(Keys.raw(), &pszKeys);
|
---|
272 | if (RT_SUCCESS(rc))
|
---|
273 | {
|
---|
274 | /* locate it */
|
---|
275 | size_t cchKey = strlen(pszKey);
|
---|
276 | char *psz = strstr(pszKeys, pszKey);
|
---|
277 | while (psz)
|
---|
278 | {
|
---|
279 | if ( ( psz == pszKeys
|
---|
280 | || psz[-1] == ' ')
|
---|
281 | && ( psz[cchKey] == ' '
|
---|
282 | || !psz[cchKey])
|
---|
283 | )
|
---|
284 | break;
|
---|
285 | psz = strstr(psz + cchKey, pszKey);
|
---|
286 | }
|
---|
287 | if (psz)
|
---|
288 | {
|
---|
289 | /* remove it */
|
---|
290 | char *pszNext = RTStrStripL(psz + cchKey);
|
---|
291 | if (*pszNext)
|
---|
292 | memmove(psz, pszNext, strlen(pszNext) + 1);
|
---|
293 | else
|
---|
294 | *psz = '\0';
|
---|
295 | psz = RTStrStrip(pszKeys);
|
---|
296 |
|
---|
297 | /* update */
|
---|
298 | hrc = pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(psz));
|
---|
299 | }
|
---|
300 |
|
---|
301 | RTStrFree(pszKeys);
|
---|
302 | return hrc;
|
---|
303 | }
|
---|
304 | else
|
---|
305 | RTPrintf("error: failed to delete key '%s' from '%s', string conversion error %Vrc!\n",
|
---|
306 | pszKey, pszKeyBase, rc);
|
---|
307 |
|
---|
308 | return E_FAIL;
|
---|
309 | }
|
---|
310 | #endif
|
---|
311 |
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Sets a key value, does necessary error bitching.
|
---|
315 | *
|
---|
316 | * @returns COM status code.
|
---|
317 | * @param pMachine The Machine object.
|
---|
318 | * @param pszKeyBase The key base.
|
---|
319 | * @param pszKey The key.
|
---|
320 | * @param pszAttribute The attribute name.
|
---|
321 | * @param pszValue The string value.
|
---|
322 | */
|
---|
323 | static HRESULT SetString(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, const char *pszValue)
|
---|
324 | {
|
---|
325 | HRESULT hrc = pMachine->SetExtraData(Bstr(Utf8StrFmt("%s/%s/%s", pszKeyBase, pszKey, pszAttribute)), Bstr(pszValue));
|
---|
326 | if (FAILED(hrc))
|
---|
327 | RTPrintf("error: Failed to set '%s/%s/%s' to '%s'! hrc=%#x\n",
|
---|
328 | pszKeyBase, pszKey, pszAttribute, pszValue, hrc);
|
---|
329 | return hrc;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Sets a key value, does necessary error bitching.
|
---|
335 | *
|
---|
336 | * @returns COM status code.
|
---|
337 | * @param pMachine The Machine object.
|
---|
338 | * @param pszKeyBase The key base.
|
---|
339 | * @param pszKey The key.
|
---|
340 | * @param pszAttribute The attribute name.
|
---|
341 | * @param u64Value The value.
|
---|
342 | */
|
---|
343 | static HRESULT SetUInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, uint64_t u64Value)
|
---|
344 | {
|
---|
345 | char szValue[64];
|
---|
346 | RTStrPrintf(szValue, sizeof(szValue), "%#RX64", u64Value);
|
---|
347 | return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
|
---|
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 i64Value The value.
|
---|
360 | */
|
---|
361 | static HRESULT SetInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, int64_t i64Value)
|
---|
362 | {
|
---|
363 | char szValue[64];
|
---|
364 | RTStrPrintf(szValue, sizeof(szValue), "%RI64", i64Value);
|
---|
365 | return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Identical to the 'loadsyms' command.
|
---|
371 | */
|
---|
372 | static int CmdLoadSyms(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
373 | {
|
---|
374 | HRESULT rc;
|
---|
375 |
|
---|
376 | /*
|
---|
377 | * Get the VM
|
---|
378 | */
|
---|
379 | ComPtr<IMachine> machine;
|
---|
380 | /* assume it's a UUID */
|
---|
381 | rc = aVirtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
|
---|
382 | if (FAILED(rc) || !machine)
|
---|
383 | {
|
---|
384 | /* must be a name */
|
---|
385 | CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()), 1);
|
---|
386 | }
|
---|
387 |
|
---|
388 | /*
|
---|
389 | * Parse the command.
|
---|
390 | */
|
---|
391 | const char *pszFilename;
|
---|
392 | int64_t offDelta = 0;
|
---|
393 | const char *pszModule = NULL;
|
---|
394 | uint64_t ModuleAddress = ~0;
|
---|
395 | uint64_t ModuleSize = 0;
|
---|
396 |
|
---|
397 | /* filename */
|
---|
398 | if (argc < 2)
|
---|
399 | return errorArgument("Missing the filename argument!\n");
|
---|
400 | pszFilename = argv[1];
|
---|
401 |
|
---|
402 | /* offDelta */
|
---|
403 | if (argc >= 3)
|
---|
404 | {
|
---|
405 | int rc = RTStrToInt64Ex(argv[2], NULL, 0, &offDelta);
|
---|
406 | if (VBOX_FAILURE(rc))
|
---|
407 | return errorArgument(argv[0], "Failed to read delta '%s', rc=%Vrc\n", argv[2], rc);
|
---|
408 | }
|
---|
409 |
|
---|
410 | /* pszModule */
|
---|
411 | if (argc >= 4)
|
---|
412 | pszModule = argv[3];
|
---|
413 |
|
---|
414 | /* ModuleAddress */
|
---|
415 | if (argc >= 5)
|
---|
416 | {
|
---|
417 | int rc = RTStrToUInt64Ex(argv[4], NULL, 0, &ModuleAddress);
|
---|
418 | if (VBOX_FAILURE(rc))
|
---|
419 | return errorArgument(argv[0], "Failed to read module address '%s', rc=%Vrc\n", argv[4], rc);
|
---|
420 | }
|
---|
421 |
|
---|
422 | /* ModuleSize */
|
---|
423 | if (argc >= 6)
|
---|
424 | {
|
---|
425 | int rc = RTStrToUInt64Ex(argv[5], NULL, 0, &ModuleSize);
|
---|
426 | if (VBOX_FAILURE(rc))
|
---|
427 | return errorArgument(argv[0], "Failed to read module size '%s', rc=%Vrc\n", argv[5], rc);
|
---|
428 | }
|
---|
429 |
|
---|
430 | /*
|
---|
431 | * Add extra data.
|
---|
432 | */
|
---|
433 | Utf8Str KeyStr;
|
---|
434 | HRESULT hrc = NewUniqueKey(machine, "VBoxInternal/DBGF/loadsyms", KeyStr);
|
---|
435 | if (SUCCEEDED(hrc))
|
---|
436 | hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Filename", pszFilename);
|
---|
437 | if (SUCCEEDED(hrc) && argc >= 3)
|
---|
438 | hrc = SetInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Delta", offDelta);
|
---|
439 | if (SUCCEEDED(hrc) && argc >= 4)
|
---|
440 | hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Module", pszModule);
|
---|
441 | if (SUCCEEDED(hrc) && argc >= 5)
|
---|
442 | hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleAddress", ModuleAddress);
|
---|
443 | if (SUCCEEDED(hrc) && argc >= 6)
|
---|
444 | hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleSize", ModuleSize);
|
---|
445 |
|
---|
446 | return FAILED(hrc);
|
---|
447 | }
|
---|
448 |
|
---|
449 | static int handleSetVDIUUID(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
450 | {
|
---|
451 | /* we need exactly one parameter: the vdi file */
|
---|
452 | if (argc != 1)
|
---|
453 | {
|
---|
454 | return errorSyntax(USAGE_SETVDIUUID, "Not enough parameters");
|
---|
455 | }
|
---|
456 |
|
---|
457 | /* generate a new UUID */
|
---|
458 | Guid uuid;
|
---|
459 | uuid.create();
|
---|
460 |
|
---|
461 | /* just try it */
|
---|
462 | int rc = VDISetImageUUIDs(argv[0], uuid.raw(), NULL, NULL, NULL);
|
---|
463 | if (VBOX_FAILURE(rc))
|
---|
464 | {
|
---|
465 | RTPrintf("Error while setting a new UUID: %Vrc (%d)\n", rc, rc);
|
---|
466 | }
|
---|
467 | else
|
---|
468 | {
|
---|
469 | RTPrintf("UUID changed to: %s\n", uuid.toString().raw());
|
---|
470 | }
|
---|
471 |
|
---|
472 | return 0;
|
---|
473 | }
|
---|
474 |
|
---|
475 |
|
---|
476 | static DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
|
---|
477 | {
|
---|
478 | RTPrintf("ERROR: ");
|
---|
479 | RTPrintfV(pszFormat, va);
|
---|
480 | RTPrintf("\n");
|
---|
481 | RTPrintf("Error code %Vrc at %s(%u) in function %s\n", rc, RT_SRC_POS_ARGS);
|
---|
482 | }
|
---|
483 |
|
---|
484 | static int partRead(RTFILE File, PHOSTPARTITIONS pPart)
|
---|
485 | {
|
---|
486 | uint8_t aBuffer[512];
|
---|
487 | int rc;
|
---|
488 |
|
---|
489 | pPart->cPartitions = 0;
|
---|
490 | memset(pPart->aPartitions, '\0', sizeof(pPart->aPartitions));
|
---|
491 | rc = RTFileReadAt(File, 0, &aBuffer, sizeof(aBuffer), NULL);
|
---|
492 | if (VBOX_FAILURE(rc))
|
---|
493 | return rc;
|
---|
494 | if (aBuffer[510] != 0x55 || aBuffer[511] != 0xaa)
|
---|
495 | return VERR_INVALID_PARAMETER;
|
---|
496 |
|
---|
497 | unsigned uExtended = (unsigned)-1;
|
---|
498 |
|
---|
499 | for (unsigned i = 0; i < 4; i++)
|
---|
500 | {
|
---|
501 | uint8_t *p = &aBuffer[0x1be + i * 16];
|
---|
502 | if (p[4] == 0)
|
---|
503 | continue;
|
---|
504 | PHOSTPARTITION pCP = &pPart->aPartitions[pPart->cPartitions++];
|
---|
505 | pCP->uIndex = i + 1;
|
---|
506 | pCP->uType = p[4];
|
---|
507 | pCP->uStartCylinder = (uint32_t)p[3] + ((uint32_t)(p[2] & 0xc0) << 2);
|
---|
508 | pCP->uStartHead = p[1];
|
---|
509 | pCP->uStartSector = p[2] & 0x3f;
|
---|
510 | pCP->uEndCylinder = (uint32_t)p[7] + ((uint32_t)(p[6] & 0xc0) << 2);
|
---|
511 | pCP->uEndHead = p[5];
|
---|
512 | pCP->uEndSector = p[6] & 0x3f;
|
---|
513 | pCP->uStart = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
|
---|
514 | pCP->uSize = RT_MAKE_U32_FROM_U8(p[12], p[13], p[14], p[15]);
|
---|
515 | pCP->uPartDataStart = 0; /* will be filled out later properly. */
|
---|
516 | pCP->cPartDataSectors = 0;
|
---|
517 |
|
---|
518 | if (PARTTYPE_IS_EXTENDED(p[4]))
|
---|
519 | {
|
---|
520 | if (uExtended == (unsigned)-1)
|
---|
521 | uExtended = pCP - pPart->aPartitions;
|
---|
522 | else
|
---|
523 | {
|
---|
524 | RTPrintf("More than one extended partition. Aborting\n");
|
---|
525 | return VERR_INVALID_PARAMETER;
|
---|
526 | }
|
---|
527 | }
|
---|
528 | }
|
---|
529 |
|
---|
530 | if (uExtended != (unsigned)-1)
|
---|
531 | {
|
---|
532 | unsigned uIndex = 5;
|
---|
533 | uint64_t uStart = pPart->aPartitions[uExtended].uStart;
|
---|
534 | uint64_t uOffset = 0;
|
---|
535 | if (!uStart)
|
---|
536 | {
|
---|
537 | RTPrintf("Inconsistency for logical partition start. Aborting\n");
|
---|
538 | return VERR_INVALID_PARAMETER;
|
---|
539 | }
|
---|
540 |
|
---|
541 | do
|
---|
542 | {
|
---|
543 | rc = RTFileReadAt(File, (uStart + uOffset) * 512, &aBuffer, sizeof(aBuffer), NULL);
|
---|
544 | if (VBOX_FAILURE(rc))
|
---|
545 | return rc;
|
---|
546 |
|
---|
547 | if (aBuffer[510] != 0x55 || aBuffer[511] != 0xaa)
|
---|
548 | {
|
---|
549 | RTPrintf("Logical partition without magic. Aborting\n");
|
---|
550 | return VERR_INVALID_PARAMETER;
|
---|
551 | }
|
---|
552 | uint8_t *p = &aBuffer[0x1be];
|
---|
553 |
|
---|
554 | if (p[4] == 0)
|
---|
555 | {
|
---|
556 | RTPrintf("Logical partition with type 0 encountered. Aborting\n");
|
---|
557 | return VERR_INVALID_PARAMETER;
|
---|
558 | }
|
---|
559 |
|
---|
560 | PHOSTPARTITION pCP = &pPart->aPartitions[pPart->cPartitions++];
|
---|
561 | pCP->uIndex = uIndex;
|
---|
562 | pCP->uType = p[4];
|
---|
563 | pCP->uStartCylinder = (uint32_t)p[3] + ((uint32_t)(p[2] & 0xc0) << 2);
|
---|
564 | pCP->uStartHead = p[1];
|
---|
565 | pCP->uStartSector = p[2] & 0x3f;
|
---|
566 | pCP->uEndCylinder = (uint32_t)p[7] + ((uint32_t)(p[6] & 0xc0) << 2);
|
---|
567 | pCP->uEndHead = p[5];
|
---|
568 | pCP->uEndSector = p[6] & 0x3f;
|
---|
569 | uint32_t uStartOffset = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
|
---|
570 | pCP->uStart = uStart + uOffset + uStartOffset;
|
---|
571 | pCP->uSize = RT_MAKE_U32_FROM_U8(p[12], p[13], p[14], p[15]);
|
---|
572 | /* Fill out partitioning location info for EBR. */
|
---|
573 | pCP->uPartDataStart = uStart + uOffset;
|
---|
574 | pCP->cPartDataSectors = RT_MIN(uStartOffset, 63);
|
---|
575 | p += 16;
|
---|
576 | if (p[4] == 0)
|
---|
577 | uExtended = (unsigned)-1;
|
---|
578 | else if (PARTTYPE_IS_EXTENDED(p[4]))
|
---|
579 | {
|
---|
580 | uExtended = uIndex++;
|
---|
581 | uOffset = RT_MAKE_U32_FROM_U8(p[8], p[9], p[10], p[11]);
|
---|
582 | }
|
---|
583 | else
|
---|
584 | {
|
---|
585 | RTPrintf("Logical partition chain broken. Aborting\n");
|
---|
586 | return VERR_INVALID_PARAMETER;
|
---|
587 | }
|
---|
588 | } while (uExtended != (unsigned)-1);
|
---|
589 | }
|
---|
590 |
|
---|
591 | /* Sort partitions in ascending order of start sector. Also do a lot of
|
---|
592 | * consistency checking. */
|
---|
593 | uint64_t uPrevEnd = 0;
|
---|
594 | for (unsigned i = 0; i < pPart->cPartitions-1; i++)
|
---|
595 | {
|
---|
596 | unsigned uMinIdx = i;
|
---|
597 | uint64_t uMinVal = pPart->aPartitions[i].uStart;
|
---|
598 | for (unsigned j = i + 1; j < pPart->cPartitions; j++)
|
---|
599 | {
|
---|
600 | if (pPart->aPartitions[j].uStart < uMinVal)
|
---|
601 | {
|
---|
602 | uMinIdx = j;
|
---|
603 | uMinVal = pPart->aPartitions[j].uStart;
|
---|
604 | }
|
---|
605 | else if (pPart->aPartitions[j].uStart == uMinVal)
|
---|
606 | {
|
---|
607 | RTPrintf("Two partitions start at the same place. Aborting\n");
|
---|
608 | return VERR_INVALID_PARAMETER;
|
---|
609 | } else if (pPart->aPartitions[j].uStart == 0)
|
---|
610 | {
|
---|
611 | RTPrintf("Partition starts at sector 0. Aborting\n");
|
---|
612 | return VERR_INVALID_PARAMETER;
|
---|
613 | }
|
---|
614 | }
|
---|
615 | if (uMinIdx != i)
|
---|
616 | {
|
---|
617 | /* Swap entries at index i and uMinIdx. */
|
---|
618 | memcpy(&pPart->aPartitions[pPart->cPartitions],
|
---|
619 | &pPart->aPartitions[i], sizeof(HOSTPARTITION));
|
---|
620 | memcpy(&pPart->aPartitions[i],
|
---|
621 | &pPart->aPartitions[uMinIdx], sizeof(HOSTPARTITION));
|
---|
622 | memcpy(&pPart->aPartitions[uMinIdx],
|
---|
623 | &pPart->aPartitions[pPart->cPartitions], sizeof(HOSTPARTITION));
|
---|
624 | }
|
---|
625 | if (pPart->aPartitions[i].cPartDataSectors)
|
---|
626 | {
|
---|
627 | if (pPart->aPartitions[i].uPartDataStart < uPrevEnd)
|
---|
628 | {
|
---|
629 | RTPrintf("Overlapping partition description areas. Aborting\n");
|
---|
630 | return VERR_INVALID_PARAMETER;
|
---|
631 | }
|
---|
632 | uPrevEnd = pPart->aPartitions[i].uPartDataStart + pPart->aPartitions[i].cPartDataSectors;
|
---|
633 | }
|
---|
634 | if (pPart->aPartitions[i].uStart < uPrevEnd)
|
---|
635 | {
|
---|
636 | RTPrintf("Overlapping partitions. Aborting\n");
|
---|
637 | return VERR_INVALID_PARAMETER;
|
---|
638 | }
|
---|
639 | if (!PARTTYPE_IS_EXTENDED(pPart->aPartitions[i].uType))
|
---|
640 | uPrevEnd = pPart->aPartitions[i].uStart + pPart->aPartitions[i].uSize;
|
---|
641 | }
|
---|
642 |
|
---|
643 | /* Fill out partitioning location info for MBR. */
|
---|
644 | pPart->aPartitions[0].uPartDataStart = 0;
|
---|
645 | pPart->aPartitions[0].cPartDataSectors = RT_MIN(pPart->aPartitions[0].uStart, 63);
|
---|
646 |
|
---|
647 | return VINF_SUCCESS;
|
---|
648 | }
|
---|
649 |
|
---|
650 | HRESULT CmdListPartitions(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
651 | {
|
---|
652 | Utf8Str rawdisk;
|
---|
653 |
|
---|
654 | /* let's have a closer look at the arguments */
|
---|
655 | for (int i = 0; i < argc; i++)
|
---|
656 | {
|
---|
657 | if (strcmp(argv[i], "-rawdisk") == 0)
|
---|
658 | {
|
---|
659 | if (argc <= i + 1)
|
---|
660 | {
|
---|
661 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
662 | }
|
---|
663 | i++;
|
---|
664 | rawdisk = argv[i];
|
---|
665 | }
|
---|
666 | else
|
---|
667 | {
|
---|
668 | return errorSyntax(USAGE_LISTPARTITIONS, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | if (rawdisk.isEmpty())
|
---|
673 | return errorSyntax(USAGE_LISTPARTITIONS, "Mandatory parameter -rawdisk missing");
|
---|
674 |
|
---|
675 | RTFILE RawFile;
|
---|
676 | int vrc = RTFileOpen(&RawFile, rawdisk.raw(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
|
---|
677 | if (VBOX_FAILURE(vrc))
|
---|
678 | {
|
---|
679 | RTPrintf("Error opening the raw disk: %Vrc\n", vrc);
|
---|
680 | return vrc;
|
---|
681 | }
|
---|
682 |
|
---|
683 | HOSTPARTITIONS partitions;
|
---|
684 | vrc = partRead(RawFile, &partitions);
|
---|
685 | if (VBOX_FAILURE(vrc))
|
---|
686 | return vrc;
|
---|
687 |
|
---|
688 | RTPrintf("Number Type StartCHS EndCHS Size (MiB) Start (Sect)\n");
|
---|
689 | for (unsigned i = 0; i < partitions.cPartitions; i++)
|
---|
690 | {
|
---|
691 | /* Suppress printing the extended partition. Otherwise people
|
---|
692 | * might add it to the list of partitions for raw partition
|
---|
693 | * access (which is not good). */
|
---|
694 | if (PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
|
---|
695 | continue;
|
---|
696 |
|
---|
697 | RTPrintf("%-7u %#04x %-4u/%-3u/%-2u %-4u/%-3u/%-2u %10llu %10llu\n",
|
---|
698 | partitions.aPartitions[i].uIndex,
|
---|
699 | partitions.aPartitions[i].uType,
|
---|
700 | partitions.aPartitions[i].uStartCylinder,
|
---|
701 | partitions.aPartitions[i].uStartHead,
|
---|
702 | partitions.aPartitions[i].uStartSector,
|
---|
703 | partitions.aPartitions[i].uEndCylinder,
|
---|
704 | partitions.aPartitions[i].uEndHead,
|
---|
705 | partitions.aPartitions[i].uEndSector,
|
---|
706 | partitions.aPartitions[i].uSize / 2048,
|
---|
707 | partitions.aPartitions[i].uStart);
|
---|
708 | }
|
---|
709 |
|
---|
710 | return 0;
|
---|
711 | }
|
---|
712 |
|
---|
713 | HRESULT CmdCreateRawVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
714 | {
|
---|
715 | HRESULT rc = S_OK;
|
---|
716 | Bstr filename;
|
---|
717 | const char *pszMBRFilename = NULL;
|
---|
718 | Utf8Str rawdisk;
|
---|
719 | const char *pszPartitions = NULL;
|
---|
720 | bool fRegister = false;
|
---|
721 | bool fRelative = false;
|
---|
722 |
|
---|
723 | /* let's have a closer look at the arguments */
|
---|
724 | for (int i = 0; i < argc; i++)
|
---|
725 | {
|
---|
726 | if (strcmp(argv[i], "-filename") == 0)
|
---|
727 | {
|
---|
728 | if (argc <= i + 1)
|
---|
729 | {
|
---|
730 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
731 | }
|
---|
732 | i++;
|
---|
733 | filename = argv[i];
|
---|
734 | }
|
---|
735 | else if (strcmp(argv[i], "-mbr") == 0)
|
---|
736 | {
|
---|
737 | if (argc <= i + 1)
|
---|
738 | {
|
---|
739 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
740 | }
|
---|
741 | i++;
|
---|
742 | pszMBRFilename = argv[i];
|
---|
743 | }
|
---|
744 | else if (strcmp(argv[i], "-rawdisk") == 0)
|
---|
745 | {
|
---|
746 | if (argc <= i + 1)
|
---|
747 | {
|
---|
748 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
749 | }
|
---|
750 | i++;
|
---|
751 | rawdisk = argv[i];
|
---|
752 | }
|
---|
753 | else if (strcmp(argv[i], "-partitions") == 0)
|
---|
754 | {
|
---|
755 | if (argc <= i + 1)
|
---|
756 | {
|
---|
757 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
758 | }
|
---|
759 | i++;
|
---|
760 | pszPartitions = argv[i];
|
---|
761 | }
|
---|
762 | else if (strcmp(argv[i], "-register") == 0)
|
---|
763 | {
|
---|
764 | fRegister = true;
|
---|
765 | }
|
---|
766 | #ifdef RT_OS_LINUX
|
---|
767 | else if (strcmp(argv[i], "-relative") == 0)
|
---|
768 | {
|
---|
769 | fRelative = true;
|
---|
770 | }
|
---|
771 | #endif /* RT_OS_LINUX */
|
---|
772 | else
|
---|
773 | {
|
---|
774 | return errorSyntax(USAGE_CREATERAWVMDK, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | if (filename.isEmpty())
|
---|
779 | return errorSyntax(USAGE_CREATERAWVMDK, "Mandatory parameter -filename missing");
|
---|
780 | if (rawdisk.isEmpty())
|
---|
781 | return errorSyntax(USAGE_CREATERAWVMDK, "Mandatory parameter -rawdisk missing");
|
---|
782 | if (!pszPartitions && pszMBRFilename)
|
---|
783 | return errorSyntax(USAGE_CREATERAWVMDK, "The parameter -mbr is only valid when the parameter -partitions is also present");
|
---|
784 |
|
---|
785 | RTFILE RawFile;
|
---|
786 | int vrc = RTFileOpen(&RawFile, rawdisk.raw(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
|
---|
787 | if (VBOX_FAILURE(vrc))
|
---|
788 | {
|
---|
789 | RTPrintf("Error opening the raw disk: %Vrc\n", vrc);
|
---|
790 | return vrc;
|
---|
791 | }
|
---|
792 |
|
---|
793 | uint64_t cbSize = 0;
|
---|
794 | #ifdef RT_OS_WINDOWS
|
---|
795 | /* Windows NT has no IOCTL_DISK_GET_LENGTH_INFORMATION ioctl. This was
|
---|
796 | * added to Windows XP, so we have to use the available info from DriveGeo.
|
---|
797 | * Note that we cannot simply use IOCTL_DISK_GET_DRIVE_GEOMETRY as it
|
---|
798 | * yields a slightly different result than IOCTL_DISK_GET_LENGTH_INFO.
|
---|
799 | * We call IOCTL_DISK_GET_DRIVE_GEOMETRY first as we need to check the media
|
---|
800 | * type anyway, and if IOCTL_DISK_GET_LENGTH_INFORMATION is supported
|
---|
801 | * we will later override cbSize.
|
---|
802 | */
|
---|
803 | DISK_GEOMETRY DriveGeo;
|
---|
804 | DWORD cbDriveGeo;
|
---|
805 | if (DeviceIoControl((HANDLE)RawFile,
|
---|
806 | IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
|
---|
807 | &DriveGeo, sizeof(DriveGeo), &cbDriveGeo, NULL))
|
---|
808 | {
|
---|
809 | if (DriveGeo.MediaType == FixedMedia)
|
---|
810 | {
|
---|
811 | cbSize = DriveGeo.Cylinders.QuadPart
|
---|
812 | * DriveGeo.TracksPerCylinder
|
---|
813 | * DriveGeo.SectorsPerTrack
|
---|
814 | * DriveGeo.BytesPerSector;
|
---|
815 | }
|
---|
816 | else
|
---|
817 | return VERR_MEDIA_NOT_RECOGNIZED;
|
---|
818 |
|
---|
819 | GET_LENGTH_INFORMATION DiskLenInfo;
|
---|
820 | DWORD junk;
|
---|
821 | if (DeviceIoControl((HANDLE)RawFile,
|
---|
822 | IOCTL_DISK_GET_LENGTH_INFO, NULL, 0,
|
---|
823 | &DiskLenInfo, sizeof(DiskLenInfo), &junk, (LPOVERLAPPED)NULL))
|
---|
824 | {
|
---|
825 | /* IOCTL_DISK_GET_LENGTH_INFO is supported -- override cbSize. */
|
---|
826 | cbSize = DiskLenInfo.Length.QuadPart;
|
---|
827 | }
|
---|
828 | }
|
---|
829 | else
|
---|
830 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
831 | #elif defined(RT_OS_LINUX)
|
---|
832 | struct stat DevStat;
|
---|
833 | if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
|
---|
834 | {
|
---|
835 | #ifdef BLKGETSIZE64
|
---|
836 | /* BLKGETSIZE64 is broken up to 2.4.17 and in many 2.5.x. In 2.6.0
|
---|
837 | * it works without problems. */
|
---|
838 | struct utsname utsname;
|
---|
839 | if ( uname(&utsname) == 0
|
---|
840 | && ( (strncmp(utsname.release, "2.5.", 4) == 0 && atoi(&utsname.release[4]) >= 18)
|
---|
841 | || (strncmp(utsname.release, "2.", 2) == 0 && atoi(&utsname.release[2]) >= 6)))
|
---|
842 | {
|
---|
843 | uint64_t cbBlk;
|
---|
844 | if (!ioctl(RawFile, BLKGETSIZE64, &cbBlk))
|
---|
845 | cbSize = cbBlk;
|
---|
846 | }
|
---|
847 | #endif /* BLKGETSIZE64 */
|
---|
848 | if (!cbSize)
|
---|
849 | {
|
---|
850 | long cBlocks;
|
---|
851 | if (!ioctl(RawFile, BLKGETSIZE, &cBlocks))
|
---|
852 | cbSize = (uint64_t)cBlocks << 9;
|
---|
853 | else
|
---|
854 | return RTErrConvertFromErrno(errno);
|
---|
855 | }
|
---|
856 | }
|
---|
857 | else
|
---|
858 | {
|
---|
859 | RTPrintf("File '%s' is no block device\n", rawdisk.raw());
|
---|
860 | return VERR_INVALID_PARAMETER;
|
---|
861 | }
|
---|
862 | #elif defined(RT_OS_DARWIN)
|
---|
863 | struct stat DevStat;
|
---|
864 | if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
|
---|
865 | {
|
---|
866 | uint64_t cBlocks;
|
---|
867 | uint32_t cbBlock;
|
---|
868 | if (!ioctl(RawFile, DKIOCGETBLOCKCOUNT, &cBlocks))
|
---|
869 | {
|
---|
870 | if (!ioctl(RawFile, DKIOCGETBLOCKSIZE, &cbBlock))
|
---|
871 | cbSize = cBlocks * cbBlock;
|
---|
872 | else
|
---|
873 | return RTErrConvertFromErrno(errno);
|
---|
874 | }
|
---|
875 | else
|
---|
876 | return RTErrConvertFromErrno(errno);
|
---|
877 | }
|
---|
878 | else
|
---|
879 | {
|
---|
880 | RTPrintf("File '%s' is no block device\n", rawdisk.raw());
|
---|
881 | return VERR_INVALID_PARAMETER;
|
---|
882 | }
|
---|
883 | #elif defined(RT_OS_SOLARIS)
|
---|
884 | struct stat DevStat;
|
---|
885 | if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
|
---|
886 | {
|
---|
887 | struct dk_minfo mediainfo;
|
---|
888 | if (!ioctl(RawFile, DKIOCGMEDIAINFO, &mediainfo))
|
---|
889 | cbSize = mediainfo.dki_capacity * mediainfo.dki_lbsize;
|
---|
890 | else
|
---|
891 | return RTErrConvertFromErrno(errno);
|
---|
892 | }
|
---|
893 | else
|
---|
894 | {
|
---|
895 | RTPrintf("File '%s' is no block device\n", rawdisk.raw());
|
---|
896 | return VERR_INVALID_PARAMETER;
|
---|
897 | }
|
---|
898 | #else /* all unrecognized OSes */
|
---|
899 | /* Hopefully this works on all other hosts. If it doesn't, it'll just fail
|
---|
900 | * creating the VMDK, so no real harm done. */
|
---|
901 | vrc = RTFileGetSize(RawFile, &cbSize);
|
---|
902 | if (VBOX_FAILURE(vrc))
|
---|
903 | {
|
---|
904 | RTPrintf("Error getting the size of the raw disk: %Vrc\n", vrc);
|
---|
905 | return vrc;
|
---|
906 | }
|
---|
907 | #endif
|
---|
908 |
|
---|
909 | PVBOXHDD pDisk = NULL;
|
---|
910 | VBOXHDDRAW RawDescriptor;
|
---|
911 | HOSTPARTITIONS partitions;
|
---|
912 | uint32_t uPartitions = 0;
|
---|
913 |
|
---|
914 | RawDescriptor.szSignature[0] = 'R';
|
---|
915 | RawDescriptor.szSignature[1] = 'A';
|
---|
916 | RawDescriptor.szSignature[2] = 'W';
|
---|
917 | RawDescriptor.szSignature[3] = '\0';
|
---|
918 | if (!pszPartitions)
|
---|
919 | {
|
---|
920 | RawDescriptor.fRawDisk = true;
|
---|
921 | RawDescriptor.pszRawDisk = rawdisk.raw();
|
---|
922 | }
|
---|
923 | else
|
---|
924 | {
|
---|
925 | RawDescriptor.fRawDisk = false;
|
---|
926 | RawDescriptor.pszRawDisk = NULL;
|
---|
927 | RawDescriptor.cPartitions = 0;
|
---|
928 |
|
---|
929 | const char *p = pszPartitions;
|
---|
930 | char *pszNext;
|
---|
931 | uint32_t u32;
|
---|
932 | while (*p != '\0')
|
---|
933 | {
|
---|
934 | vrc = RTStrToUInt32Ex(p, &pszNext, 0, &u32);
|
---|
935 | if (VBOX_FAILURE(vrc))
|
---|
936 | {
|
---|
937 | RTPrintf("Incorrect value in partitions parameter\n");
|
---|
938 | return vrc;
|
---|
939 | }
|
---|
940 | uPartitions |= RT_BIT(u32);
|
---|
941 | p = pszNext;
|
---|
942 | if (*p == ',')
|
---|
943 | p++;
|
---|
944 | else if (*p != '\0')
|
---|
945 | {
|
---|
946 | RTPrintf("Incorrect separator in partitions parameter\n");
|
---|
947 | return VERR_INVALID_PARAMETER;
|
---|
948 | }
|
---|
949 | }
|
---|
950 |
|
---|
951 | vrc = partRead(RawFile, &partitions);
|
---|
952 | if (VBOX_FAILURE(vrc))
|
---|
953 | {
|
---|
954 | RTPrintf("Error reading the partition information from '%s'\n", rawdisk.raw());
|
---|
955 | return vrc;
|
---|
956 | }
|
---|
957 |
|
---|
958 | for (unsigned i = 0; i < partitions.cPartitions; i++)
|
---|
959 | {
|
---|
960 | if ( uPartitions & RT_BIT(partitions.aPartitions[i].uIndex)
|
---|
961 | && PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
|
---|
962 | {
|
---|
963 | /* Some ignorant user specified an extended partition.
|
---|
964 | * Bad idea, as this would trigger an overlapping
|
---|
965 | * partitions error later during VMDK creation. So warn
|
---|
966 | * here and ignore what the user requested. */
|
---|
967 | RTPrintf("Warning: it is not possible (and necessary) to explicitly give access to the\n"
|
---|
968 | " extended partition %u. If required, enable access to all logical\n"
|
---|
969 | " partitions inside this extended partition.\n", partitions.aPartitions[i].uIndex);
|
---|
970 | uPartitions &= ~RT_BIT(partitions.aPartitions[i].uIndex);
|
---|
971 | }
|
---|
972 | }
|
---|
973 |
|
---|
974 | RawDescriptor.cPartitions = partitions.cPartitions;
|
---|
975 | RawDescriptor.pPartitions = (PVBOXHDDRAWPART)RTMemAllocZ(partitions.cPartitions * sizeof(VBOXHDDRAWPART));
|
---|
976 | if (!RawDescriptor.pPartitions)
|
---|
977 | return VERR_NO_MEMORY;
|
---|
978 | for (unsigned i = 0; i < partitions.cPartitions; i++)
|
---|
979 | {
|
---|
980 | if (uPartitions & RT_BIT(partitions.aPartitions[i].uIndex))
|
---|
981 | {
|
---|
982 | if (fRelative)
|
---|
983 | {
|
---|
984 | #ifdef RT_OS_LINUX
|
---|
985 | /* Refer to the correct partition and use offset 0. */
|
---|
986 | char *pszRawName;
|
---|
987 | vrc = RTStrAPrintf(&pszRawName, "%s%u", rawdisk.raw(),
|
---|
988 | partitions.aPartitions[i].uIndex);
|
---|
989 | if (VBOX_FAILURE(vrc))
|
---|
990 | {
|
---|
991 | RTPrintf("Error creating reference to individual partition %u, rc=%Vrc\n",
|
---|
992 | partitions.aPartitions[i].uIndex, vrc);
|
---|
993 | return vrc;
|
---|
994 | }
|
---|
995 | RawDescriptor.pPartitions[i].pszRawDevice = pszRawName;
|
---|
996 | RawDescriptor.pPartitions[i].uPartitionStartOffset = 0;
|
---|
997 | RawDescriptor.pPartitions[i].uPartitionStart = partitions.aPartitions[i].uStart * 512;
|
---|
998 | #else
|
---|
999 | /** @todo not implemented yet for Windows host. Treat just
|
---|
1000 | * like not specified (this code is actually never reached). */
|
---|
1001 | RawDescriptor.pPartitions[i].pszRawDevice = rawdisk.raw();
|
---|
1002 | RawDescriptor.pPartitions[i].uPartitionStartOffset = partitions.aPartitions[i].uStart * 512;
|
---|
1003 | RawDescriptor.pPartitions[i].uPartitionStart = partitions.aPartitions[i].uStart * 512;
|
---|
1004 | #endif
|
---|
1005 | }
|
---|
1006 | else
|
---|
1007 | {
|
---|
1008 | /* This is the "everything refers to the base raw device"
|
---|
1009 | * variant. This requires opening the base device in RW
|
---|
1010 | * mode even for creation. */
|
---|
1011 | RawDescriptor.pPartitions[i].pszRawDevice = rawdisk.raw();
|
---|
1012 | RawDescriptor.pPartitions[i].uPartitionStartOffset = partitions.aPartitions[i].uStart * 512;
|
---|
1013 | RawDescriptor.pPartitions[i].uPartitionStart = partitions.aPartitions[i].uStart * 512;
|
---|
1014 | }
|
---|
1015 | }
|
---|
1016 | else
|
---|
1017 | {
|
---|
1018 | /* Suppress access to this partition. */
|
---|
1019 | RawDescriptor.pPartitions[i].pszRawDevice = NULL;
|
---|
1020 | RawDescriptor.pPartitions[i].uPartitionStartOffset = 0;
|
---|
1021 | /* This is used in the plausibility check in the creation
|
---|
1022 | * code. In theory it's a dummy, but I don't want to make
|
---|
1023 | * the VMDK creatiion any more complicated than what it needs
|
---|
1024 | * to be. */
|
---|
1025 | RawDescriptor.pPartitions[i].uPartitionStart = partitions.aPartitions[i].uStart * 512;
|
---|
1026 | }
|
---|
1027 | if (PARTTYPE_IS_EXTENDED(partitions.aPartitions[i].uType))
|
---|
1028 | {
|
---|
1029 | /* Suppress exporting the actual extended partition. Only
|
---|
1030 | * logical partitions should be processed. However completely
|
---|
1031 | * ignoring it leads to leaving out the MBR data. */
|
---|
1032 | RawDescriptor.pPartitions[i].cbPartition = 0;
|
---|
1033 | }
|
---|
1034 | else
|
---|
1035 | RawDescriptor.pPartitions[i].cbPartition = partitions.aPartitions[i].uSize * 512;
|
---|
1036 | RawDescriptor.pPartitions[i].uPartitionDataStart = partitions.aPartitions[i].uPartDataStart * 512;
|
---|
1037 | RawDescriptor.pPartitions[i].cbPartitionData = partitions.aPartitions[i].cPartDataSectors * 512;
|
---|
1038 | if (RawDescriptor.pPartitions[i].cbPartitionData)
|
---|
1039 | {
|
---|
1040 | Assert (RawDescriptor.pPartitions[i].cbPartitionData -
|
---|
1041 | (size_t)RawDescriptor.pPartitions[i].cbPartitionData == 0);
|
---|
1042 | void *pPartData = RTMemAlloc((size_t)RawDescriptor.pPartitions[i].cbPartitionData);
|
---|
1043 | if (!pPartData)
|
---|
1044 | return VERR_NO_MEMORY;
|
---|
1045 | vrc = RTFileReadAt(RawFile, partitions.aPartitions[i].uPartDataStart * 512, pPartData, (size_t)RawDescriptor.pPartitions[i].cbPartitionData, NULL);
|
---|
1046 | if (VBOX_FAILURE(vrc))
|
---|
1047 | {
|
---|
1048 | RTPrintf("Cannot read partition data from raw device '%s': %Vrc\n", rawdisk.raw(), vrc);
|
---|
1049 | return vrc;
|
---|
1050 | }
|
---|
1051 | /* Splice in the replacement MBR code if specified. */
|
---|
1052 | if ( partitions.aPartitions[i].uPartDataStart == 0
|
---|
1053 | && pszMBRFilename)
|
---|
1054 | {
|
---|
1055 | RTFILE MBRFile;
|
---|
1056 | vrc = RTFileOpen(&MBRFile, pszMBRFilename, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
|
---|
1057 | if (VBOX_FAILURE(vrc))
|
---|
1058 | {
|
---|
1059 | RTPrintf("Cannot open replacement MBR file '%s' specified with -mbr: %Vrc\n", pszMBRFilename, vrc);
|
---|
1060 | return vrc;
|
---|
1061 | }
|
---|
1062 | vrc = RTFileReadAt(MBRFile, 0, pPartData, 0x1be, NULL);
|
---|
1063 | RTFileClose(MBRFile);
|
---|
1064 | if (VBOX_FAILURE(vrc))
|
---|
1065 | {
|
---|
1066 | RTPrintf("Cannot read replacement MBR file '%s': %Vrc\n", pszMBRFilename, vrc);
|
---|
1067 | return vrc;
|
---|
1068 | }
|
---|
1069 | }
|
---|
1070 | RawDescriptor.pPartitions[i].pvPartitionData = pPartData;
|
---|
1071 | }
|
---|
1072 | }
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | RTFileClose(RawFile);
|
---|
1076 |
|
---|
1077 | vrc = VDCreate(handleVDError, NULL, &pDisk);
|
---|
1078 | if (VBOX_FAILURE(vrc))
|
---|
1079 | {
|
---|
1080 | RTPrintf("Error while creating the virtual disk container: %Vrc\n", vrc);
|
---|
1081 | return vrc;
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | Assert(RT_MIN(cbSize / 512 / 16 / 63, 16383) -
|
---|
1085 | (unsigned int)RT_MIN(cbSize / 512 / 16 / 63, 16383) == 0);
|
---|
1086 | PDMMEDIAGEOMETRY PCHS, LCHS;
|
---|
1087 | PCHS.cCylinders = (unsigned int)RT_MIN(cbSize / 512 / 16 / 63, 16383);
|
---|
1088 | PCHS.cHeads = 16;
|
---|
1089 | PCHS.cSectors = 63;
|
---|
1090 | LCHS.cCylinders = 0;
|
---|
1091 | LCHS.cHeads = 0;
|
---|
1092 | LCHS.cSectors = 0;
|
---|
1093 | vrc = VDCreateBase(pDisk, "VMDK", Utf8Str(filename).raw(),
|
---|
1094 | VD_IMAGE_TYPE_FIXED, cbSize,
|
---|
1095 | VD_VMDK_IMAGE_FLAGS_RAWDISK, (char *)&RawDescriptor,
|
---|
1096 | &PCHS, &LCHS, VD_OPEN_FLAGS_NORMAL, NULL, NULL);
|
---|
1097 | if (VBOX_FAILURE(vrc))
|
---|
1098 | {
|
---|
1099 | RTPrintf("Error while creating the raw disk VMDK: %Vrc\n", vrc);
|
---|
1100 | return vrc;
|
---|
1101 | }
|
---|
1102 | RTPrintf("RAW host disk access VMDK file %s created successfully.\n", Utf8Str(filename).raw());
|
---|
1103 |
|
---|
1104 | VDCloseAll(pDisk);
|
---|
1105 |
|
---|
1106 | /* Clean up allocated memory etc. */
|
---|
1107 | if (pszPartitions)
|
---|
1108 | {
|
---|
1109 | for (unsigned i = 0; i < partitions.cPartitions; i++)
|
---|
1110 | {
|
---|
1111 | if (uPartitions & RT_BIT(partitions.aPartitions[i].uIndex))
|
---|
1112 | {
|
---|
1113 | if (fRelative)
|
---|
1114 | {
|
---|
1115 | #ifdef RT_OS_LINUX
|
---|
1116 | /* Free memory allocated above. */
|
---|
1117 | RTStrFree((char *)(void *)RawDescriptor.pPartitions[i].pszRawDevice);
|
---|
1118 | #endif /* RT_OS_LINUX */
|
---|
1119 | }
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | if (fRegister)
|
---|
1125 | {
|
---|
1126 | ComPtr<IHardDisk> hardDisk;
|
---|
1127 | CHECK_ERROR(aVirtualBox, OpenHardDisk(filename, hardDisk.asOutParam()));
|
---|
1128 |
|
---|
1129 | if (SUCCEEDED(rc) && hardDisk)
|
---|
1130 | {
|
---|
1131 | CHECK_ERROR(aVirtualBox, RegisterHardDisk(hardDisk));
|
---|
1132 | }
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | HRESULT CmdRenameVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
1139 | {
|
---|
1140 | Bstr src;
|
---|
1141 | Bstr dst;
|
---|
1142 | /* Parse the arguments. */
|
---|
1143 | for (int i = 0; i < argc; i++)
|
---|
1144 | {
|
---|
1145 | if (strcmp(argv[i], "-from") == 0)
|
---|
1146 | {
|
---|
1147 | if (argc <= i + 1)
|
---|
1148 | {
|
---|
1149 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
1150 | }
|
---|
1151 | i++;
|
---|
1152 | src = argv[i];
|
---|
1153 | }
|
---|
1154 | else if (strcmp(argv[i], "-to") == 0)
|
---|
1155 | {
|
---|
1156 | if (argc <= i + 1)
|
---|
1157 | {
|
---|
1158 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
1159 | }
|
---|
1160 | i++;
|
---|
1161 | dst = argv[i];
|
---|
1162 | }
|
---|
1163 | else
|
---|
1164 | {
|
---|
1165 | return errorSyntax(USAGE_RENAMEVMDK, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
|
---|
1166 | }
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | if (src.isEmpty())
|
---|
1170 | return errorSyntax(USAGE_RENAMEVMDK, "Mandatory parameter -from missing");
|
---|
1171 | if (dst.isEmpty())
|
---|
1172 | return errorSyntax(USAGE_RENAMEVMDK, "Mandatory parameter -to missing");
|
---|
1173 |
|
---|
1174 | PVBOXHDD pDisk = NULL;
|
---|
1175 |
|
---|
1176 | int vrc = VDCreate(handleVDError, NULL, &pDisk);
|
---|
1177 | if (VBOX_FAILURE(vrc))
|
---|
1178 | {
|
---|
1179 | RTPrintf("Error while creating the virtual disk container: %Vrc\n", vrc);
|
---|
1180 | return vrc;
|
---|
1181 | }
|
---|
1182 | else
|
---|
1183 | {
|
---|
1184 | vrc = VDOpen(pDisk, "VMDK", Utf8Str(src).raw(), VD_OPEN_FLAGS_NORMAL);
|
---|
1185 | if (VBOX_FAILURE(vrc))
|
---|
1186 | {
|
---|
1187 | RTPrintf("Error while opening the source image: %Vrc\n", vrc);
|
---|
1188 | }
|
---|
1189 | else
|
---|
1190 | {
|
---|
1191 | vrc = VDCopy(pDisk, 0, pDisk, "VMDK", Utf8Str(dst).raw(), true, 0, NULL, NULL);
|
---|
1192 | if (VBOX_FAILURE(vrc))
|
---|
1193 | {
|
---|
1194 | RTPrintf("Error while renaming the image: %Vrc\n", vrc);
|
---|
1195 | }
|
---|
1196 | }
|
---|
1197 | }
|
---|
1198 | VDCloseAll(pDisk);
|
---|
1199 | return vrc;
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | /**
|
---|
1203 | * Unloads the neccessary driver.
|
---|
1204 | *
|
---|
1205 | * @returns VBox status code
|
---|
1206 | */
|
---|
1207 | int CmdModUninstall(void)
|
---|
1208 | {
|
---|
1209 | int rc;
|
---|
1210 |
|
---|
1211 | rc = SUPUninstall();
|
---|
1212 | if (RT_SUCCESS(rc))
|
---|
1213 | return 0;
|
---|
1214 | if (rc == VERR_NOT_IMPLEMENTED)
|
---|
1215 | return 0;
|
---|
1216 | return E_FAIL;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /**
|
---|
1220 | * Loads the neccessary driver.
|
---|
1221 | *
|
---|
1222 | * @returns VBox status code
|
---|
1223 | */
|
---|
1224 | int CmdModInstall(void)
|
---|
1225 | {
|
---|
1226 | int rc;
|
---|
1227 |
|
---|
1228 | rc = SUPInstall();
|
---|
1229 | if (RT_SUCCESS(rc))
|
---|
1230 | return 0;
|
---|
1231 | if (rc == VERR_NOT_IMPLEMENTED)
|
---|
1232 | return 0;
|
---|
1233 | return E_FAIL;
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | /**
|
---|
1237 | * Wrapper for handling internal commands
|
---|
1238 | */
|
---|
1239 | int handleInternalCommands(int argc, char *argv[],
|
---|
1240 | ComPtr <IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
1241 | {
|
---|
1242 | g_fInternalMode = true;
|
---|
1243 |
|
---|
1244 | /* at least a command is required */
|
---|
1245 | if (argc < 1)
|
---|
1246 | return errorSyntax(USAGE_ALL, "Command missing");
|
---|
1247 |
|
---|
1248 | /*
|
---|
1249 | * The 'string switch' on command name.
|
---|
1250 | */
|
---|
1251 | const char *pszCmd = argv[0];
|
---|
1252 | if (!strcmp(pszCmd, "loadsyms"))
|
---|
1253 | return CmdLoadSyms(argc - 1, &argv[1], aVirtualBox, aSession);
|
---|
1254 | //if (!strcmp(pszCmd, "unloadsyms"))
|
---|
1255 | // return CmdUnloadSyms(argc - 1 , &argv[1]);
|
---|
1256 | if (!strcmp(pszCmd, "setvdiuuid"))
|
---|
1257 | return handleSetVDIUUID(argc - 1, &argv[1], aVirtualBox, aSession);
|
---|
1258 | if (!strcmp(pszCmd, "listpartitions"))
|
---|
1259 | return CmdListPartitions(argc - 1, &argv[1], aVirtualBox, aSession);
|
---|
1260 | if (!strcmp(pszCmd, "createrawvmdk"))
|
---|
1261 | return CmdCreateRawVMDK(argc - 1, &argv[1], aVirtualBox, aSession);
|
---|
1262 | if (!strcmp(pszCmd, "renamevmdk"))
|
---|
1263 | return CmdRenameVMDK(argc - 1, &argv[1], aVirtualBox, aSession);
|
---|
1264 |
|
---|
1265 | if (!strcmp(pszCmd, "modinstall"))
|
---|
1266 | return CmdModInstall();
|
---|
1267 | if (!strcmp(pszCmd, "moduninstall"))
|
---|
1268 | return CmdModUninstall();
|
---|
1269 |
|
---|
1270 | /* default: */
|
---|
1271 | return errorSyntax(USAGE_ALL, "Invalid command '%s'", Utf8Str(argv[0]).raw());
|
---|
1272 | }
|
---|