VirtualBox

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

Last change on this file since 2685 was 2675, checked in by vboxsync, 18 years ago

Raw disk VMDK image creation stuff.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 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 InnoTek Systemberatung 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>]\n"
98 " [-register]\n"
99 " Creates a new VMDK image which gives access to an entite host disk or\n"
100 " some partitions of a host disk. The diskname is on Linux e.g. /dev/sda,\n"
101 " and on Windows e.g. \\\\.\\PhysicalDisk0).\n"
102 " Optionally the created image can be immediately registered.\n"
103 " The necessary partition numbers can be queried with\n"
104 " VBoxManage internalcommands listpartitions\n"
105 "\n"
106 : ""
107 );
108}
109
110/** @todo this is no longer necessary, we can enumerate extra data */
111/**
112 * Finds a new unique key name.
113 *
114 * I don't think this is 100% race condition proof, but we assumes
115 * the user is not trying to push this point.
116 *
117 * @returns Result from the insert.
118 * @param pMachine The Machine object.
119 * @param pszKeyBase The base key.
120 * @param rKey Reference to the string object in which we will return the key.
121 */
122static HRESULT NewUniqueKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, Utf8Str &rKey)
123{
124 Bstr Keys;
125 HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
126 if (FAILED(hrc))
127 return hrc;
128
129 /* if there are no keys, it's simple. */
130 if (Keys.isEmpty())
131 {
132 rKey = "1";
133 return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr("1"));
134 }
135
136 /* find a unique number - brute force rulez. */
137 Utf8Str KeysUtf8(Keys);
138 const char *pszKeys = RTStrStripL(KeysUtf8.raw());
139 for (unsigned i = 1; i < 1000000; i++)
140 {
141 char szKey[32];
142 size_t cchKey = RTStrPrintf(szKey, sizeof(szKey), "%#x", i);
143 const char *psz = strstr(pszKeys, szKey);
144 while (psz)
145 {
146 if ( ( psz == pszKeys
147 || psz[-1] == ' ')
148 && ( psz[cchKey] == ' '
149 || !psz[cchKey])
150 )
151 break;
152 psz = strstr(psz + cchKey, szKey);
153 }
154 if (!psz)
155 {
156 rKey = szKey;
157 Utf8StrFmt NewKeysUtf8("%s %s", pszKeys, szKey);
158 return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(NewKeysUtf8));
159 }
160 }
161 RTPrintf("Error: Cannot find unique key for '%s'!\n", pszKeyBase);
162 return E_FAIL;
163}
164
165
166#if 0
167/**
168 * Remove a key.
169 *
170 * I don't think this isn't 100% race condition proof, but we assumes
171 * the user is not trying to push this point.
172 *
173 * @returns Result from the insert.
174 * @param pMachine The machine object.
175 * @param pszKeyBase The base key.
176 * @param pszKey The key to remove.
177 */
178static HRESULT RemoveKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey)
179{
180 Bstr Keys;
181 HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
182 if (FAILED(hrc))
183 return hrc;
184
185 /* if there are no keys, it's simple. */
186 if (Keys.isEmpty())
187 return S_OK;
188
189 char *pszKeys;
190 int rc = RTStrUcs2ToUtf8(&pszKeys, Keys.raw());
191 if (VBOX_SUCCESS(rc))
192 {
193 /* locate it */
194 size_t cchKey = strlen(pszKey);
195 char *psz = strstr(pszKeys, pszKey);
196 while (psz)
197 {
198 if ( ( psz == pszKeys
199 || psz[-1] == ' ')
200 && ( psz[cchKey] == ' '
201 || !psz[cchKey])
202 )
203 break;
204 psz = strstr(psz + cchKey, pszKey);
205 }
206 if (psz)
207 {
208 /* remove it */
209 char *pszNext = RTStrStripL(psz + cchKey);
210 if (*pszNext)
211 memmove(psz, pszNext, strlen(pszNext) + 1);
212 else
213 *psz = '\0';
214 psz = RTStrStrip(pszKeys);
215
216 /* update */
217 hrc = pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(psz));
218 }
219
220 RTStrFree(pszKeys);
221 return hrc;
222 }
223 else
224 RTPrintf("error: failed to delete key '%s' from '%s', string conversion error %Vrc!\n",
225 pszKey, pszKeyBase, rc);
226
227 return E_FAIL;
228}
229#endif
230
231
232/**
233 * Sets a key value, does necessary error bitching.
234 *
235 * @returns COM status code.
236 * @param pMachine The Machine object.
237 * @param pszKeyBase The key base.
238 * @param pszKey The key.
239 * @param pszAttribute The attribute name.
240 * @param pszValue The string value.
241 */
242static HRESULT SetString(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, const char *pszValue)
243{
244 HRESULT hrc = pMachine->SetExtraData(Bstr(Utf8StrFmt("%s/%s/%s", pszKeyBase, pszKey, pszAttribute)), Bstr(pszValue));
245 if (FAILED(hrc))
246 RTPrintf("error: Failed to set '%s/%s/%s' to '%s'! hrc=%#x\n",
247 pszKeyBase, pszKey, pszAttribute, pszValue, hrc);
248 return hrc;
249}
250
251
252/**
253 * Sets a key value, does necessary error bitching.
254 *
255 * @returns COM status code.
256 * @param pMachine The Machine object.
257 * @param pszKeyBase The key base.
258 * @param pszKey The key.
259 * @param pszAttribute The attribute name.
260 * @param u64Value The value.
261 */
262static HRESULT SetUInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, uint64_t u64Value)
263{
264 char szValue[64];
265 RTStrPrintf(szValue, sizeof(szValue), "%#RX64", u64Value);
266 return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
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 i64Value The value.
279 */
280static HRESULT SetInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, int64_t i64Value)
281{
282 char szValue[64];
283 RTStrPrintf(szValue, sizeof(szValue), "%RI64", i64Value);
284 return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
285}
286
287
288/**
289 * Identical to the 'loadsyms' command.
290 */
291static int CmdLoadSyms(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
292{
293 HRESULT rc;
294
295 /*
296 * Get the VM
297 */
298 ComPtr<IMachine> machine;
299 /* assume it's a UUID */
300 rc = aVirtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
301 if (FAILED(rc) || !machine)
302 {
303 /* must be a name */
304 CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()), 1);
305 }
306
307 /*
308 * Parse the command.
309 */
310 const char *pszFilename;
311 int64_t offDelta = 0;
312 const char *pszModule = NULL;
313 uint64_t ModuleAddress = ~0;
314 uint64_t ModuleSize = 0;
315
316 /* filename */
317 if (argc < 2)
318 return errorArgument("Missing the filename argument!\n");
319 pszFilename = argv[1];
320
321 /* offDelta */
322 if (argc >= 3)
323 {
324 int rc = RTStrToInt64Ex(argv[2], NULL, 0, &offDelta);
325 if (VBOX_FAILURE(rc))
326 return errorArgument(argv[0], "Failed to read delta '%s', rc=%Vrc\n", argv[2], rc);
327 }
328
329 /* pszModule */
330 if (argc >= 4)
331 pszModule = argv[3];
332
333 /* ModuleAddress */
334 if (argc >= 5)
335 {
336 int rc = RTStrToUInt64Ex(argv[4], NULL, 0, &ModuleAddress);
337 if (VBOX_FAILURE(rc))
338 return errorArgument(argv[0], "Failed to read module address '%s', rc=%Vrc\n", argv[4], rc);
339 }
340
341 /* ModuleSize */
342 if (argc >= 6)
343 {
344 int rc = RTStrToUInt64Ex(argv[5], NULL, 0, &ModuleSize);
345 if (VBOX_FAILURE(rc))
346 return errorArgument(argv[0], "Failed to read module size '%s', rc=%Vrc\n", argv[5], rc);
347 }
348
349 /*
350 * Add extra data.
351 */
352 Utf8Str KeyStr;
353 HRESULT hrc = NewUniqueKey(machine, "VBoxInternal/DBGF/loadsyms", KeyStr);
354 if (SUCCEEDED(hrc))
355 hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Filename", pszFilename);
356 if (SUCCEEDED(hrc) && argc >= 3)
357 hrc = SetInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Delta", offDelta);
358 if (SUCCEEDED(hrc) && argc >= 4)
359 hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Module", pszModule);
360 if (SUCCEEDED(hrc) && argc >= 5)
361 hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleAddress", ModuleAddress);
362 if (SUCCEEDED(hrc) && argc >= 6)
363 hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleSize", ModuleSize);
364
365 return FAILED(hrc);
366}
367
368static int handleSetVDIUUID(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
369{
370 /* we need exactly one parameter: the vdi file */
371 if (argc != 1)
372 {
373 return errorSyntax(USAGE_SETVDIUUID, "Not enough parameters");
374 }
375
376 /* generate a new UUID */
377 Guid uuid;
378 uuid.create();
379
380 /* just try it */
381 int rc = VDISetImageUUIDs(argv[0], uuid.raw(), NULL, NULL, NULL);
382 if (VBOX_FAILURE(rc))
383 {
384 RTPrintf("Error while setting a new UUID: %Vrc (%d)\n", rc, rc);
385 }
386 else
387 {
388 RTPrintf("UUID changed to: %s\n", uuid.toString().raw());
389 }
390
391 return 0;
392}
393
394/**
395 * Wrapper for handling internal commands
396 */
397int handleInternalCommands(int argc, char *argv[],
398 ComPtr <IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
399{
400 fInternalMode = true;
401
402 /* at least a command is required */
403 if (argc < 1)
404 return errorSyntax(USAGE_ALL, "Command missing");
405
406 /*
407 * The 'string switch' on command name.
408 */
409 const char *pszCmd = argv[0];
410 if (!strcmp(pszCmd, "loadsyms"))
411 return CmdLoadSyms(argc - 1, &argv[1], aVirtualBox, aSession);
412 //if (!strcmp(pszCmd, "unloadsyms"))
413 // return CmdUnloadSyms(argc - 1 , &argv[1]);
414 if (!strcmp(pszCmd, "setvdiuuid"))
415 return handleSetVDIUUID(argc - 1, &argv[1], aVirtualBox, aSession);
416#ifndef VBOX_OSE
417 if (!strcmp(pszCmd, "listpartitions"))
418 return CmdListPartitions(argc - 1, &argv[1], aVirtualBox, aSession);
419 if (!strcmp(pszCmd, "createrawvmdk"))
420 return CmdCreateRawVMDK(argc - 1, &argv[1], aVirtualBox, aSession);
421#endif /* !VBOX_OSE */
422
423 /* default: */
424 return errorSyntax(USAGE_ALL, "Invalid command '%s'", Utf8Str(argv[0]).raw());
425}
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