VirtualBox

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

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

The Big Sun Rebranding Header Change

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