VirtualBox

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

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

InnoTek -> innotek: all the headers and comments.

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