VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxInternalManage.cpp@ 7218

Last change on this file since 7218 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 KB
Line 
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 innotek GmbH
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
23
24
25/*******************************************************************************
26* Header Files *
27*******************************************************************************/
28#include <VBox/com/com.h>
29#include <VBox/com/string.h>
30#include <VBox/com/Guid.h>
31#include <VBox/com/ErrorInfo.h>
32
33#include <VBox/com/VirtualBox.h>
34
35#include <iprt/runtime.h>
36#include <iprt/stream.h>
37#include <iprt/string.h>
38#include <iprt/uuid.h>
39#include <VBox/err.h>
40
41#include <VBox/VBoxHDD.h>
42#include <VBox/sup.h>
43
44#include "VBoxManage.h"
45
46#ifndef VBOX_OSE
47HRESULT CmdListPartitions(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession);
48HRESULT CmdCreateRawVMDK(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession);
49#endif /* !VBOX_OSE */
50
51using namespace com;
52
53/** flag whether we're in internal mode */
54bool g_fInternalMode;
55
56/**
57 * Print the usage info.
58 */
59void printUsageInternal(USAGECATEGORY u64Cmd)
60{
61 RTPrintf("Usage: VBoxManage internalcommands <command> [command arguments]\n"
62 "\n"
63 "Commands:\n"
64 "\n"
65 "%s%s%s%s%s%s%s"
66 "WARNING: This is a development tool and shall only be used to analyse\n"
67 " problems. It is completely unsupported and will change in\n"
68 " incompatible ways without warning.\n",
69 (u64Cmd & USAGE_LOADSYMS) ?
70 " loadsyms <vmname>|<uuid> <symfile> [delta] [module] [module address]\n"
71 " This will instruct DBGF to load the given symbolfile\n"
72 " during initialization.\n"
73 "\n"
74 : "",
75 (u64Cmd & USAGE_UNLOADSYMS) ?
76 " unloadsyms <vmname>|<uuid> <symfile>\n"
77 " Removes <symfile> from the list of symbol files that\n"
78 " should be loaded during DBF initialization.\n"
79 "\n"
80 : "",
81 (u64Cmd & USAGE_SETVDIUUID) ?
82 " setvdiuuid <filepath>\n"
83 " Assigns a new UUID to the given VDI file. This way, multiple copies\n"
84 " of VDI containers can be registered.\n"
85 "\n"
86 : "",
87 (u64Cmd & USAGE_LISTPARTITIONS) ?
88 " listpartitions -rawdisk <diskname>\n"
89 " Lists all partitions on <diskname>.\n"
90 "\n"
91 : "",
92 (u64Cmd & USAGE_CREATERAWVMDK) ?
93 " createrawvmdk -filename <filename> -rawdisk <diskname>\n"
94 " [-partitions <list of partition numbers> [-mbr <filename>] ]\n"
95 " [-register] [-relative]\n"
96 " Creates a new VMDK image which gives access to an entite host disk (if\n"
97 " the parameter -partitions is not specified) or some partitions of a\n"
98 " host disk. If access to individual partitions is granted, then the\n"
99 " parameter -mbr can be used to specify an alternative MBR to be used\n"
100 " (the partitioning information in the MBR file is ignored).\n"
101 " The diskname is on Linux e.g. /dev/sda, and on Windows e.g.\n"
102 " \\\\.\\PhysicalDisk0).\n"
103 " On Linux host the parameter -relative causes a VMDK file to be created\n"
104 " which refers to individual partitions instead to the entire disk.\n"
105 " Optionally the created image can be immediately registered.\n"
106 " The necessary partition numbers can be queried with\n"
107 " VBoxManage internalcommands listpartitions\n"
108 "\n"
109 : "",
110#ifdef RT_OS_WINDOWS
111 (u64Cmd & USAGE_MODINSTALL) ?
112 " modinstall\n"
113 " Installs the neccessary driver for the host OS\n"
114 "\n"
115 : "",
116 (u64Cmd & USAGE_MODUNINSTALL) ?
117 " moduninstall\n"
118 " Deinstalls the driver\n"
119 "\n"
120 : ""
121#else
122 "",
123 ""
124#endif
125 );
126}
127
128/** @todo this is no longer necessary, we can enumerate extra data */
129/**
130 * Finds a new unique key name.
131 *
132 * I don't think this is 100% race condition proof, but we assumes
133 * the user is not trying to push this point.
134 *
135 * @returns Result from the insert.
136 * @param pMachine The Machine object.
137 * @param pszKeyBase The base key.
138 * @param rKey Reference to the string object in which we will return the key.
139 */
140static HRESULT NewUniqueKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, Utf8Str &rKey)
141{
142 Bstr Keys;
143 HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
144 if (FAILED(hrc))
145 return hrc;
146
147 /* if there are no keys, it's simple. */
148 if (Keys.isEmpty())
149 {
150 rKey = "1";
151 return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr("1"));
152 }
153
154 /* find a unique number - brute force rulez. */
155 Utf8Str KeysUtf8(Keys);
156 const char *pszKeys = RTStrStripL(KeysUtf8.raw());
157 for (unsigned i = 1; i < 1000000; i++)
158 {
159 char szKey[32];
160 size_t cchKey = RTStrPrintf(szKey, sizeof(szKey), "%#x", i);
161 const char *psz = strstr(pszKeys, szKey);
162 while (psz)
163 {
164 if ( ( psz == pszKeys
165 || psz[-1] == ' ')
166 && ( psz[cchKey] == ' '
167 || !psz[cchKey])
168 )
169 break;
170 psz = strstr(psz + cchKey, szKey);
171 }
172 if (!psz)
173 {
174 rKey = szKey;
175 Utf8StrFmt NewKeysUtf8("%s %s", pszKeys, szKey);
176 return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(NewKeysUtf8));
177 }
178 }
179 RTPrintf("Error: Cannot find unique key for '%s'!\n", pszKeyBase);
180 return E_FAIL;
181}
182
183
184#if 0
185/**
186 * Remove a key.
187 *
188 * I don't think this isn't 100% race condition proof, but we assumes
189 * the user is not trying to push this point.
190 *
191 * @returns Result from the insert.
192 * @param pMachine The machine object.
193 * @param pszKeyBase The base key.
194 * @param pszKey The key to remove.
195 */
196static HRESULT RemoveKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey)
197{
198 Bstr Keys;
199 HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
200 if (FAILED(hrc))
201 return hrc;
202
203 /* if there are no keys, it's simple. */
204 if (Keys.isEmpty())
205 return S_OK;
206
207 char *pszKeys;
208 int rc = RTStrUcs2ToUtf8(&pszKeys, Keys.raw());
209 if (VBOX_SUCCESS(rc))
210 {
211 /* locate it */
212 size_t cchKey = strlen(pszKey);
213 char *psz = strstr(pszKeys, pszKey);
214 while (psz)
215 {
216 if ( ( psz == pszKeys
217 || psz[-1] == ' ')
218 && ( psz[cchKey] == ' '
219 || !psz[cchKey])
220 )
221 break;
222 psz = strstr(psz + cchKey, pszKey);
223 }
224 if (psz)
225 {
226 /* remove it */
227 char *pszNext = RTStrStripL(psz + cchKey);
228 if (*pszNext)
229 memmove(psz, pszNext, strlen(pszNext) + 1);
230 else
231 *psz = '\0';
232 psz = RTStrStrip(pszKeys);
233
234 /* update */
235 hrc = pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(psz));
236 }
237
238 RTStrFree(pszKeys);
239 return hrc;
240 }
241 else
242 RTPrintf("error: failed to delete key '%s' from '%s', string conversion error %Vrc!\n",
243 pszKey, pszKeyBase, rc);
244
245 return E_FAIL;
246}
247#endif
248
249
250/**
251 * Sets a key value, does necessary error bitching.
252 *
253 * @returns COM status code.
254 * @param pMachine The Machine object.
255 * @param pszKeyBase The key base.
256 * @param pszKey The key.
257 * @param pszAttribute The attribute name.
258 * @param pszValue The string value.
259 */
260static HRESULT SetString(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, const char *pszValue)
261{
262 HRESULT hrc = pMachine->SetExtraData(Bstr(Utf8StrFmt("%s/%s/%s", pszKeyBase, pszKey, pszAttribute)), Bstr(pszValue));
263 if (FAILED(hrc))
264 RTPrintf("error: Failed to set '%s/%s/%s' to '%s'! hrc=%#x\n",
265 pszKeyBase, pszKey, pszAttribute, pszValue, hrc);
266 return hrc;
267}
268
269
270/**
271 * Sets a key value, does necessary error bitching.
272 *
273 * @returns COM status code.
274 * @param pMachine The Machine object.
275 * @param pszKeyBase The key base.
276 * @param pszKey The key.
277 * @param pszAttribute The attribute name.
278 * @param u64Value The value.
279 */
280static HRESULT SetUInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, uint64_t u64Value)
281{
282 char szValue[64];
283 RTStrPrintf(szValue, sizeof(szValue), "%#RX64", u64Value);
284 return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
285}
286
287
288/**
289 * Sets a key value, does necessary error bitching.
290 *
291 * @returns COM status code.
292 * @param pMachine The Machine object.
293 * @param pszKeyBase The key base.
294 * @param pszKey The key.
295 * @param pszAttribute The attribute name.
296 * @param i64Value The value.
297 */
298static HRESULT SetInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, int64_t i64Value)
299{
300 char szValue[64];
301 RTStrPrintf(szValue, sizeof(szValue), "%RI64", i64Value);
302 return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
303}
304
305
306/**
307 * Identical to the 'loadsyms' command.
308 */
309static int CmdLoadSyms(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
310{
311 HRESULT rc;
312
313 /*
314 * Get the VM
315 */
316 ComPtr<IMachine> machine;
317 /* assume it's a UUID */
318 rc = aVirtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
319 if (FAILED(rc) || !machine)
320 {
321 /* must be a name */
322 CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()), 1);
323 }
324
325 /*
326 * Parse the command.
327 */
328 const char *pszFilename;
329 int64_t offDelta = 0;
330 const char *pszModule = NULL;
331 uint64_t ModuleAddress = ~0;
332 uint64_t ModuleSize = 0;
333
334 /* filename */
335 if (argc < 2)
336 return errorArgument("Missing the filename argument!\n");
337 pszFilename = argv[1];
338
339 /* offDelta */
340 if (argc >= 3)
341 {
342 int rc = RTStrToInt64Ex(argv[2], NULL, 0, &offDelta);
343 if (VBOX_FAILURE(rc))
344 return errorArgument(argv[0], "Failed to read delta '%s', rc=%Vrc\n", argv[2], rc);
345 }
346
347 /* pszModule */
348 if (argc >= 4)
349 pszModule = argv[3];
350
351 /* ModuleAddress */
352 if (argc >= 5)
353 {
354 int rc = RTStrToUInt64Ex(argv[4], NULL, 0, &ModuleAddress);
355 if (VBOX_FAILURE(rc))
356 return errorArgument(argv[0], "Failed to read module address '%s', rc=%Vrc\n", argv[4], rc);
357 }
358
359 /* ModuleSize */
360 if (argc >= 6)
361 {
362 int rc = RTStrToUInt64Ex(argv[5], NULL, 0, &ModuleSize);
363 if (VBOX_FAILURE(rc))
364 return errorArgument(argv[0], "Failed to read module size '%s', rc=%Vrc\n", argv[5], rc);
365 }
366
367 /*
368 * Add extra data.
369 */
370 Utf8Str KeyStr;
371 HRESULT hrc = NewUniqueKey(machine, "VBoxInternal/DBGF/loadsyms", KeyStr);
372 if (SUCCEEDED(hrc))
373 hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Filename", pszFilename);
374 if (SUCCEEDED(hrc) && argc >= 3)
375 hrc = SetInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Delta", offDelta);
376 if (SUCCEEDED(hrc) && argc >= 4)
377 hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Module", pszModule);
378 if (SUCCEEDED(hrc) && argc >= 5)
379 hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleAddress", ModuleAddress);
380 if (SUCCEEDED(hrc) && argc >= 6)
381 hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleSize", ModuleSize);
382
383 return FAILED(hrc);
384}
385
386static int handleSetVDIUUID(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
387{
388 /* we need exactly one parameter: the vdi file */
389 if (argc != 1)
390 {
391 return errorSyntax(USAGE_SETVDIUUID, "Not enough parameters");
392 }
393
394 /* generate a new UUID */
395 Guid uuid;
396 uuid.create();
397
398 /* just try it */
399 int rc = VDISetImageUUIDs(argv[0], uuid.raw(), NULL, NULL, NULL);
400 if (VBOX_FAILURE(rc))
401 {
402 RTPrintf("Error while setting a new UUID: %Vrc (%d)\n", rc, rc);
403 }
404 else
405 {
406 RTPrintf("UUID changed to: %s\n", uuid.toString().raw());
407 }
408
409 return 0;
410}
411
412/**
413 * Unloads the neccessary driver.
414 *
415 * @returns VBox status code
416 */
417int CmdModUninstall(void)
418{
419 int rc = SUPUninstall();
420
421 rc = SUPInstall();
422 if (VBOX_SUCCESS(rc))
423 return 0;
424 else if (rc == VERR_NOT_IMPLEMENTED)
425 return 0;
426 else
427 return E_FAIL;
428}
429
430/**
431 * Loads the neccessary driver.
432 *
433 * @returns VBox status code
434 */
435int CmdModInstall(void)
436{
437 int rc = SUPInstall();
438
439 rc = SUPInstall();
440 if (VBOX_SUCCESS(rc))
441 return 0;
442 else if (rc == VERR_NOT_IMPLEMENTED)
443 return 0;
444 else
445 return E_FAIL;
446}
447
448/**
449 * Wrapper for handling internal commands
450 */
451int handleInternalCommands(int argc, char *argv[],
452 ComPtr <IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
453{
454 g_fInternalMode = true;
455
456 /* at least a command is required */
457 if (argc < 1)
458 return errorSyntax(USAGE_ALL, "Command missing");
459
460 /*
461 * The 'string switch' on command name.
462 */
463 const char *pszCmd = argv[0];
464 if (!strcmp(pszCmd, "loadsyms"))
465 return CmdLoadSyms(argc - 1, &argv[1], aVirtualBox, aSession);
466 //if (!strcmp(pszCmd, "unloadsyms"))
467 // return CmdUnloadSyms(argc - 1 , &argv[1]);
468 if (!strcmp(pszCmd, "setvdiuuid"))
469 return handleSetVDIUUID(argc - 1, &argv[1], aVirtualBox, aSession);
470#ifndef VBOX_OSE
471 if (!strcmp(pszCmd, "listpartitions"))
472 return CmdListPartitions(argc - 1, &argv[1], aVirtualBox, aSession);
473 if (!strcmp(pszCmd, "createrawvmdk"))
474 return CmdCreateRawVMDK(argc - 1, &argv[1], aVirtualBox, aSession);
475#endif /* !VBOX_OSE */
476
477 if (!strcmp(pszCmd, "modinstall"))
478 return CmdModInstall();
479 if (!strcmp(pszCmd, "moduninstall"))
480 return CmdModUninstall();
481
482 /* default: */
483 return errorSyntax(USAGE_ALL, "Invalid command '%s'", Utf8Str(argv[0]).raw());
484}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette