1 | /* $Id: vbox-img.cpp 62729 2016-07-30 01:54:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Standalone image manipulation tool
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <VBox/vd.h>
|
---|
23 | #include <VBox/err.h>
|
---|
24 | #include <VBox/version.h>
|
---|
25 | #include <iprt/initterm.h>
|
---|
26 | #include <iprt/buildconfig.h>
|
---|
27 | #include <iprt/path.h>
|
---|
28 | #include <iprt/string.h>
|
---|
29 | #include <iprt/uuid.h>
|
---|
30 | #include <iprt/stream.h>
|
---|
31 | #include <iprt/message.h>
|
---|
32 | #include <iprt/getopt.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/dvm.h>
|
---|
35 | #include <iprt/filesystem.h>
|
---|
36 | #include <iprt/vfs.h>
|
---|
37 |
|
---|
38 | static const char *g_pszProgName = "";
|
---|
39 | static void printUsage(PRTSTREAM pStrm)
|
---|
40 | {
|
---|
41 | RTStrmPrintf(pStrm,
|
---|
42 | "Usage: %s\n"
|
---|
43 | " setuuid --filename <filename>\n"
|
---|
44 | " [--format VDI|VMDK|VHD|...]\n"
|
---|
45 | " [--uuid <uuid>]\n"
|
---|
46 | " [--parentuuid <uuid>]\n"
|
---|
47 | " [--zeroparentuuid]\n"
|
---|
48 | "\n"
|
---|
49 | " geometry --filename <filename>\n"
|
---|
50 | " [--format VDI|VMDK|VHD|...]\n"
|
---|
51 | " [--clearchs]\n"
|
---|
52 | " [--cylinders <number>]\n"
|
---|
53 | " [--heads <number>]\n"
|
---|
54 | " [--sectors <number>]\n"
|
---|
55 | "\n"
|
---|
56 | " convert --srcfilename <filename>\n"
|
---|
57 | " --dstfilename <filename>\n"
|
---|
58 | " [--stdin]|[--stdout]\n"
|
---|
59 | " [--srcformat VDI|VMDK|VHD|RAW|..]\n"
|
---|
60 | " [--dstformat VDI|VMDK|VHD|RAW|..]\n"
|
---|
61 | " [--variant Standard,Fixed,Split2G,Stream,ESX]\n"
|
---|
62 | "\n"
|
---|
63 | " info --filename <filename>\n"
|
---|
64 | "\n"
|
---|
65 | " compact --filename <filename>\n"
|
---|
66 | " [--filesystemaware]\n"
|
---|
67 | "\n"
|
---|
68 | " createcache --filename <filename>\n"
|
---|
69 | " --size <cache size>\n"
|
---|
70 | "\n"
|
---|
71 | " createbase --filename <filename>\n"
|
---|
72 | " --size <size in bytes>\n"
|
---|
73 | " [--format VDI|VMDK|VHD] (default: VDI)\n"
|
---|
74 | " [--variant Standard,Fixed,Split2G,Stream,ESX]\n"
|
---|
75 | " [--dataalignment <alignment in bytes>]\n"
|
---|
76 | "\n"
|
---|
77 | " repair --filename <filename>\n"
|
---|
78 | " [--dry-run]\n"
|
---|
79 | " [--format VDI|VMDK|VHD] (default: autodetect)\n"
|
---|
80 | "\n"
|
---|
81 | " clearcomment --filename <filename>\n"
|
---|
82 | "\n"
|
---|
83 | " resize --filename <filename>\n"
|
---|
84 | " --size <new size>\n",
|
---|
85 | g_pszProgName);
|
---|
86 | }
|
---|
87 |
|
---|
88 | static void showLogo(PRTSTREAM pStrm)
|
---|
89 | {
|
---|
90 | static bool s_fShown; /* show only once */
|
---|
91 |
|
---|
92 | if (!s_fShown)
|
---|
93 | {
|
---|
94 | RTStrmPrintf(pStrm, VBOX_PRODUCT " Disk Utility " VBOX_VERSION_STRING "\n"
|
---|
95 | "(C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
96 | "All rights reserved.\n"
|
---|
97 | "\n");
|
---|
98 | s_fShown = true;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** command handler argument */
|
---|
103 | struct HandlerArg
|
---|
104 | {
|
---|
105 | int argc;
|
---|
106 | char **argv;
|
---|
107 | };
|
---|
108 |
|
---|
109 | static PVDINTERFACE pVDIfs;
|
---|
110 |
|
---|
111 | static DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
|
---|
112 | {
|
---|
113 | RT_NOREF2(pvUser, rc);
|
---|
114 | RT_SRC_POS_NOREF();
|
---|
115 | RTMsgErrorV(pszFormat, va);
|
---|
116 | }
|
---|
117 |
|
---|
118 | static DECLCALLBACK(int) handleVDMessage(void *pvUser, const char *pszFormat, va_list va)
|
---|
119 | {
|
---|
120 | NOREF(pvUser);
|
---|
121 | RTPrintfV(pszFormat, va);
|
---|
122 | return VINF_SUCCESS;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Print a usage synopsis and the syntax error message.
|
---|
127 | */
|
---|
128 | static int errorSyntax(const char *pszFormat, ...)
|
---|
129 | {
|
---|
130 | va_list args;
|
---|
131 | showLogo(g_pStdErr); // show logo even if suppressed
|
---|
132 | va_start(args, pszFormat);
|
---|
133 | RTStrmPrintf(g_pStdErr, "\nSyntax error: %N\n", pszFormat, &args);
|
---|
134 | va_end(args);
|
---|
135 | printUsage(g_pStdErr);
|
---|
136 | return 1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | static int errorRuntime(const char *pszFormat, ...)
|
---|
140 | {
|
---|
141 | va_list args;
|
---|
142 |
|
---|
143 | va_start(args, pszFormat);
|
---|
144 | RTMsgErrorV(pszFormat, args);
|
---|
145 | va_end(args);
|
---|
146 | return 1;
|
---|
147 | }
|
---|
148 |
|
---|
149 | static int parseDiskVariant(const char *psz, unsigned *puImageFlags)
|
---|
150 | {
|
---|
151 | int rc = VINF_SUCCESS;
|
---|
152 | unsigned uImageFlags = *puImageFlags;
|
---|
153 |
|
---|
154 | while (psz && *psz && RT_SUCCESS(rc))
|
---|
155 | {
|
---|
156 | size_t len;
|
---|
157 | const char *pszComma = strchr(psz, ',');
|
---|
158 | if (pszComma)
|
---|
159 | len = pszComma - psz;
|
---|
160 | else
|
---|
161 | len = strlen(psz);
|
---|
162 | if (len > 0)
|
---|
163 | {
|
---|
164 | /*
|
---|
165 | * Parsing is intentionally inconsistent: "standard" resets the
|
---|
166 | * variant, whereas the other flags are cumulative.
|
---|
167 | */
|
---|
168 | if (!RTStrNICmp(psz, "standard", len))
|
---|
169 | uImageFlags = VD_IMAGE_FLAGS_NONE;
|
---|
170 | else if ( !RTStrNICmp(psz, "fixed", len)
|
---|
171 | || !RTStrNICmp(psz, "static", len))
|
---|
172 | uImageFlags |= VD_IMAGE_FLAGS_FIXED;
|
---|
173 | else if (!RTStrNICmp(psz, "Diff", len))
|
---|
174 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
175 | else if (!RTStrNICmp(psz, "split2g", len))
|
---|
176 | uImageFlags |= VD_VMDK_IMAGE_FLAGS_SPLIT_2G;
|
---|
177 | else if ( !RTStrNICmp(psz, "stream", len)
|
---|
178 | || !RTStrNICmp(psz, "streamoptimized", len))
|
---|
179 | uImageFlags |= VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED;
|
---|
180 | else if (!RTStrNICmp(psz, "esx", len))
|
---|
181 | uImageFlags |= VD_VMDK_IMAGE_FLAGS_ESX;
|
---|
182 | else
|
---|
183 | rc = VERR_PARSE_ERROR;
|
---|
184 | }
|
---|
185 | if (pszComma)
|
---|
186 | psz += len + 1;
|
---|
187 | else
|
---|
188 | psz += len;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (RT_SUCCESS(rc))
|
---|
192 | *puImageFlags = uImageFlags;
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | static int handleSetUUID(HandlerArg *a)
|
---|
198 | {
|
---|
199 | const char *pszFilename = NULL;
|
---|
200 | char *pszFormat = NULL;
|
---|
201 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
202 | RTUUID imageUuid;
|
---|
203 | RTUUID parentUuid;
|
---|
204 | bool fSetImageUuid = false;
|
---|
205 | bool fSetParentUuid = false;
|
---|
206 | RTUuidClear(&imageUuid);
|
---|
207 | RTUuidClear(&parentUuid);
|
---|
208 | int rc;
|
---|
209 |
|
---|
210 | /* Parse the command line. */
|
---|
211 | static const RTGETOPTDEF s_aOptions[] =
|
---|
212 | {
|
---|
213 | { "--filename", 'f', RTGETOPT_REQ_STRING },
|
---|
214 | { "--format", 'o', RTGETOPT_REQ_STRING },
|
---|
215 | { "--uuid", 'u', RTGETOPT_REQ_UUID },
|
---|
216 | { "--parentuuid", 'p', RTGETOPT_REQ_UUID },
|
---|
217 | { "--zeroparentuuid", 'P', RTGETOPT_REQ_NOTHING }
|
---|
218 | };
|
---|
219 | int ch;
|
---|
220 | RTGETOPTUNION ValueUnion;
|
---|
221 | RTGETOPTSTATE GetState;
|
---|
222 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
223 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
224 | {
|
---|
225 | switch (ch)
|
---|
226 | {
|
---|
227 | case 'f': // --filename
|
---|
228 | pszFilename = ValueUnion.psz;
|
---|
229 | break;
|
---|
230 | case 'o': // --format
|
---|
231 | pszFormat = RTStrDup(ValueUnion.psz);
|
---|
232 | break;
|
---|
233 | case 'u': // --uuid
|
---|
234 | imageUuid = ValueUnion.Uuid;
|
---|
235 | fSetImageUuid = true;
|
---|
236 | break;
|
---|
237 | case 'p': // --parentuuid
|
---|
238 | parentUuid = ValueUnion.Uuid;
|
---|
239 | fSetParentUuid = true;
|
---|
240 | break;
|
---|
241 | case 'P': // --zeroparentuuid
|
---|
242 | RTUuidClear(&parentUuid);
|
---|
243 | fSetParentUuid = true;
|
---|
244 | break;
|
---|
245 |
|
---|
246 | default:
|
---|
247 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
248 | printUsage(g_pStdErr);
|
---|
249 | return ch;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | /* Check for mandatory parameters. */
|
---|
254 | if (!pszFilename)
|
---|
255 | return errorSyntax("Mandatory --filename option missing\n");
|
---|
256 |
|
---|
257 | /* Check for consistency of optional parameters. */
|
---|
258 | if (fSetImageUuid && RTUuidIsNull(&imageUuid))
|
---|
259 | return errorSyntax("Invalid parameter to --uuid option\n");
|
---|
260 |
|
---|
261 | /* Autodetect image format. */
|
---|
262 | if (!pszFormat)
|
---|
263 | {
|
---|
264 | /* Don't pass error interface, as that would triggers error messages
|
---|
265 | * because some backends fail to open the image. */
|
---|
266 | rc = VDGetFormat(NULL, NULL, pszFilename, &pszFormat, &enmType);
|
---|
267 | if (RT_FAILURE(rc))
|
---|
268 | return errorRuntime("Format autodetect failed: %Rrc\n", rc);
|
---|
269 | }
|
---|
270 |
|
---|
271 | PVBOXHDD pVD = NULL;
|
---|
272 | rc = VDCreate(pVDIfs, enmType, &pVD);
|
---|
273 | if (RT_FAILURE(rc))
|
---|
274 | return errorRuntime("Cannot create the virtual disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
275 |
|
---|
276 | /* Open in info mode to be able to open diff images without their parent. */
|
---|
277 | rc = VDOpen(pVD, pszFormat, pszFilename, VD_OPEN_FLAGS_INFO, NULL);
|
---|
278 | if (RT_FAILURE(rc))
|
---|
279 | return errorRuntime("Cannot open the virtual disk image \"%s\": %Rrf (%Rrc)\n",
|
---|
280 | pszFilename, rc, rc);
|
---|
281 |
|
---|
282 | RTUUID oldImageUuid;
|
---|
283 | rc = VDGetUuid(pVD, VD_LAST_IMAGE, &oldImageUuid);
|
---|
284 | if (RT_FAILURE(rc))
|
---|
285 | return errorRuntime("Cannot get UUID of virtual disk image \"%s\": %Rrc\n",
|
---|
286 | pszFilename, rc);
|
---|
287 |
|
---|
288 | RTPrintf("Old image UUID: %RTuuid\n", &oldImageUuid);
|
---|
289 |
|
---|
290 | RTUUID oldParentUuid;
|
---|
291 | rc = VDGetParentUuid(pVD, VD_LAST_IMAGE, &oldParentUuid);
|
---|
292 | if (RT_FAILURE(rc))
|
---|
293 | return errorRuntime("Cannot get parent UUID of virtual disk image \"%s\": %Rrc\n",
|
---|
294 | pszFilename, rc);
|
---|
295 |
|
---|
296 | RTPrintf("Old parent UUID: %RTuuid\n", &oldParentUuid);
|
---|
297 |
|
---|
298 | if (fSetImageUuid)
|
---|
299 | {
|
---|
300 | RTPrintf("New image UUID: %RTuuid\n", &imageUuid);
|
---|
301 | rc = VDSetUuid(pVD, VD_LAST_IMAGE, &imageUuid);
|
---|
302 | if (RT_FAILURE(rc))
|
---|
303 | return errorRuntime("Cannot set UUID of virtual disk image \"%s\": %Rrf (%Rrc)\n",
|
---|
304 | pszFilename, rc, rc);
|
---|
305 | }
|
---|
306 |
|
---|
307 | if (fSetParentUuid)
|
---|
308 | {
|
---|
309 | RTPrintf("New parent UUID: %RTuuid\n", &parentUuid);
|
---|
310 | rc = VDSetParentUuid(pVD, VD_LAST_IMAGE, &parentUuid);
|
---|
311 | if (RT_FAILURE(rc))
|
---|
312 | return errorRuntime("Cannot set parent UUID of virtual disk image \"%s\": %Rrf (%Rrc)\n",
|
---|
313 | pszFilename, rc, rc);
|
---|
314 | }
|
---|
315 |
|
---|
316 | VDDestroy(pVD);
|
---|
317 |
|
---|
318 | if (pszFormat)
|
---|
319 | {
|
---|
320 | RTStrFree(pszFormat);
|
---|
321 | pszFormat = NULL;
|
---|
322 | }
|
---|
323 |
|
---|
324 | return 0;
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 | static int handleGeometry(HandlerArg *a)
|
---|
329 | {
|
---|
330 | const char *pszFilename = NULL;
|
---|
331 | char *pszFormat = NULL;
|
---|
332 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
333 | uint16_t cCylinders = 0;
|
---|
334 | uint8_t cHeads = 0;
|
---|
335 | uint8_t cSectors = 0;
|
---|
336 | bool fCylinders = false;
|
---|
337 | bool fHeads = false;
|
---|
338 | bool fSectors = false;
|
---|
339 | int rc;
|
---|
340 |
|
---|
341 | /* Parse the command line. */
|
---|
342 | static const RTGETOPTDEF s_aOptions[] =
|
---|
343 | {
|
---|
344 | { "--filename", 'f', RTGETOPT_REQ_STRING },
|
---|
345 | { "--format", 'o', RTGETOPT_REQ_STRING },
|
---|
346 | { "--clearchs", 'C', RTGETOPT_REQ_NOTHING },
|
---|
347 | { "--cylinders", 'c', RTGETOPT_REQ_UINT16 },
|
---|
348 | { "--heads", 'e', RTGETOPT_REQ_UINT8 },
|
---|
349 | { "--sectors", 's', RTGETOPT_REQ_UINT8 }
|
---|
350 | };
|
---|
351 | int ch;
|
---|
352 | RTGETOPTUNION ValueUnion;
|
---|
353 | RTGETOPTSTATE GetState;
|
---|
354 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
355 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
356 | {
|
---|
357 | switch (ch)
|
---|
358 | {
|
---|
359 | case 'f': // --filename
|
---|
360 | pszFilename = ValueUnion.psz;
|
---|
361 | break;
|
---|
362 | case 'o': // --format
|
---|
363 | pszFormat = RTStrDup(ValueUnion.psz);
|
---|
364 | break;
|
---|
365 | case 'C': // --clearchs
|
---|
366 | cCylinders = 0;
|
---|
367 | cHeads = 0;
|
---|
368 | cSectors = 0;
|
---|
369 | fCylinders = true;
|
---|
370 | fHeads = true;
|
---|
371 | fSectors = true;
|
---|
372 | break;
|
---|
373 | case 'c': // --cylinders
|
---|
374 | cCylinders = ValueUnion.u16;
|
---|
375 | fCylinders = true;
|
---|
376 | break;
|
---|
377 | case 'e': // --heads
|
---|
378 | cHeads = ValueUnion.u8;
|
---|
379 | fHeads = true;
|
---|
380 | break;
|
---|
381 | case 's': // --sectors
|
---|
382 | cSectors = ValueUnion.u8;
|
---|
383 | fSectors = true;
|
---|
384 | break;
|
---|
385 |
|
---|
386 | default:
|
---|
387 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
388 | printUsage(g_pStdErr);
|
---|
389 | return ch;
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | /* Check for mandatory parameters. */
|
---|
394 | if (!pszFilename)
|
---|
395 | return errorSyntax("Mandatory --filename option missing\n");
|
---|
396 |
|
---|
397 | /* Autodetect image format. */
|
---|
398 | if (!pszFormat)
|
---|
399 | {
|
---|
400 | /* Don't pass error interface, as that would triggers error messages
|
---|
401 | * because some backends fail to open the image. */
|
---|
402 | rc = VDGetFormat(NULL, NULL, pszFilename, &pszFormat, &enmType);
|
---|
403 | if (RT_FAILURE(rc))
|
---|
404 | return errorRuntime("Format autodetect failed: %Rrc\n", rc);
|
---|
405 | }
|
---|
406 |
|
---|
407 | PVBOXHDD pVD = NULL;
|
---|
408 | rc = VDCreate(pVDIfs, enmType, &pVD);
|
---|
409 | if (RT_FAILURE(rc))
|
---|
410 | return errorRuntime("Cannot create the virtual disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
411 |
|
---|
412 | /* Open in info mode to be able to open diff images without their parent. */
|
---|
413 | rc = VDOpen(pVD, pszFormat, pszFilename, VD_OPEN_FLAGS_INFO, NULL);
|
---|
414 | if (RT_FAILURE(rc))
|
---|
415 | return errorRuntime("Cannot open the virtual disk image \"%s\": %Rrf (%Rrc)\n",
|
---|
416 | pszFilename, rc, rc);
|
---|
417 |
|
---|
418 | VDGEOMETRY oldLCHSGeometry;
|
---|
419 | rc = VDGetLCHSGeometry(pVD, VD_LAST_IMAGE, &oldLCHSGeometry);
|
---|
420 | if (rc == VERR_VD_GEOMETRY_NOT_SET)
|
---|
421 | {
|
---|
422 | memset(&oldLCHSGeometry, 0, sizeof(oldLCHSGeometry));
|
---|
423 | rc = VINF_SUCCESS;
|
---|
424 | }
|
---|
425 | if (RT_FAILURE(rc))
|
---|
426 | return errorRuntime("Cannot get LCHS geometry of virtual disk image \"%s\": %Rrc\n",
|
---|
427 | pszFilename, rc);
|
---|
428 |
|
---|
429 | VDGEOMETRY newLCHSGeometry = oldLCHSGeometry;
|
---|
430 | if (fCylinders)
|
---|
431 | newLCHSGeometry.cCylinders = cCylinders;
|
---|
432 | if (fHeads)
|
---|
433 | newLCHSGeometry.cHeads = cHeads;
|
---|
434 | if (fSectors)
|
---|
435 | newLCHSGeometry.cSectors = cSectors;
|
---|
436 |
|
---|
437 | if (fCylinders || fHeads || fSectors)
|
---|
438 | {
|
---|
439 | RTPrintf("Old image LCHS: %u/%u/%u\n", oldLCHSGeometry.cCylinders, oldLCHSGeometry.cHeads, oldLCHSGeometry.cSectors);
|
---|
440 | RTPrintf("New image LCHS: %u/%u/%u\n", newLCHSGeometry.cCylinders, newLCHSGeometry.cHeads, newLCHSGeometry.cSectors);
|
---|
441 |
|
---|
442 | rc = VDSetLCHSGeometry(pVD, VD_LAST_IMAGE, &newLCHSGeometry);
|
---|
443 | if (RT_FAILURE(rc))
|
---|
444 | return errorRuntime("Cannot set LCHS geometry of virtual disk image \"%s\": %Rrf (%Rrc)\n",
|
---|
445 | pszFilename, rc, rc);
|
---|
446 | }
|
---|
447 | else
|
---|
448 | RTPrintf("Current image LCHS: %u/%u/%u\n", oldLCHSGeometry.cCylinders, oldLCHSGeometry.cHeads, oldLCHSGeometry.cSectors);
|
---|
449 |
|
---|
450 |
|
---|
451 | VDDestroy(pVD);
|
---|
452 |
|
---|
453 | if (pszFormat)
|
---|
454 | {
|
---|
455 | RTStrFree(pszFormat);
|
---|
456 | pszFormat = NULL;
|
---|
457 | }
|
---|
458 |
|
---|
459 | return 0;
|
---|
460 | }
|
---|
461 |
|
---|
462 |
|
---|
463 | typedef struct FILEIOSTATE
|
---|
464 | {
|
---|
465 | RTFILE file;
|
---|
466 | /** Offset in the file. */
|
---|
467 | uint64_t off;
|
---|
468 | /** Offset where the buffer contents start. UINT64_MAX=buffer invalid. */
|
---|
469 | uint64_t offBuffer;
|
---|
470 | /** Size of valid data in the buffer. */
|
---|
471 | uint32_t cbBuffer;
|
---|
472 | /** Buffer for efficient I/O */
|
---|
473 | uint8_t abBuffer[16 *_1M];
|
---|
474 | } FILEIOSTATE, *PFILEIOSTATE;
|
---|
475 |
|
---|
476 | static DECLCALLBACK(int) convInOpen(void *pvUser, const char *pszLocation, uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
|
---|
477 | void **ppStorage)
|
---|
478 | {
|
---|
479 | RT_NOREF2(pvUser, pszLocation);
|
---|
480 |
|
---|
481 | /* Validate input. */
|
---|
482 | AssertPtrReturn(ppStorage, VERR_INVALID_POINTER);
|
---|
483 | AssertPtrNullReturn(pfnCompleted, VERR_INVALID_PARAMETER);
|
---|
484 | AssertReturn((fOpen & RTFILE_O_ACCESS_MASK) == RTFILE_O_READ, VERR_INVALID_PARAMETER);
|
---|
485 | RTFILE file;
|
---|
486 | int rc = RTFileFromNative(&file, RTFILE_NATIVE_STDIN);
|
---|
487 | if (RT_FAILURE(rc))
|
---|
488 | return rc;
|
---|
489 |
|
---|
490 | /* No need to clear the buffer, the data will be read from disk. */
|
---|
491 | PFILEIOSTATE pFS = (PFILEIOSTATE)RTMemAlloc(sizeof(FILEIOSTATE));
|
---|
492 | if (!pFS)
|
---|
493 | return VERR_NO_MEMORY;
|
---|
494 |
|
---|
495 | pFS->file = file;
|
---|
496 | pFS->off = 0;
|
---|
497 | pFS->offBuffer = UINT64_MAX;
|
---|
498 | pFS->cbBuffer = 0;
|
---|
499 |
|
---|
500 | *ppStorage = pFS;
|
---|
501 | return VINF_SUCCESS;
|
---|
502 | }
|
---|
503 |
|
---|
504 | static DECLCALLBACK(int) convInClose(void *pvUser, void *pStorage)
|
---|
505 | {
|
---|
506 | NOREF(pvUser);
|
---|
507 | AssertPtrReturn(pStorage, VERR_INVALID_POINTER);
|
---|
508 | PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage;
|
---|
509 |
|
---|
510 | RTMemFree(pFS);
|
---|
511 |
|
---|
512 | return VINF_SUCCESS;
|
---|
513 | }
|
---|
514 |
|
---|
515 | static DECLCALLBACK(int) convInDelete(void *pvUser, const char *pcszFilename)
|
---|
516 | {
|
---|
517 | NOREF(pvUser);
|
---|
518 | NOREF(pcszFilename);
|
---|
519 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
520 | }
|
---|
521 |
|
---|
522 | static DECLCALLBACK(int) convInMove(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
|
---|
523 | {
|
---|
524 | NOREF(pvUser);
|
---|
525 | NOREF(pcszSrc);
|
---|
526 | NOREF(pcszDst);
|
---|
527 | NOREF(fMove);
|
---|
528 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
529 | }
|
---|
530 |
|
---|
531 | static DECLCALLBACK(int) convInGetFreeSpace(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
|
---|
532 | {
|
---|
533 | NOREF(pvUser);
|
---|
534 | NOREF(pcszFilename);
|
---|
535 | AssertPtrReturn(pcbFreeSpace, VERR_INVALID_POINTER);
|
---|
536 | *pcbFreeSpace = 0;
|
---|
537 | return VINF_SUCCESS;
|
---|
538 | }
|
---|
539 |
|
---|
540 | static DECLCALLBACK(int) convInGetModificationTime(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
|
---|
541 | {
|
---|
542 | NOREF(pvUser);
|
---|
543 | NOREF(pcszFilename);
|
---|
544 | AssertPtrReturn(pModificationTime, VERR_INVALID_POINTER);
|
---|
545 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
546 | }
|
---|
547 |
|
---|
548 | static DECLCALLBACK(int) convInGetSize(void *pvUser, void *pStorage, uint64_t *pcbSize)
|
---|
549 | {
|
---|
550 | NOREF(pvUser);
|
---|
551 | NOREF(pStorage);
|
---|
552 | AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
|
---|
553 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
554 | }
|
---|
555 |
|
---|
556 | static DECLCALLBACK(int) convInSetSize(void *pvUser, void *pStorage, uint64_t cbSize)
|
---|
557 | {
|
---|
558 | NOREF(pvUser);
|
---|
559 | NOREF(pStorage);
|
---|
560 | NOREF(cbSize);
|
---|
561 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
562 | }
|
---|
563 |
|
---|
564 | static DECLCALLBACK(int) convInRead(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
565 | void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
566 | {
|
---|
567 | NOREF(pvUser);
|
---|
568 | AssertPtrReturn(pStorage, VERR_INVALID_POINTER);
|
---|
569 | AssertPtrReturn(pvBuffer, VERR_INVALID_POINTER);
|
---|
570 | PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage;
|
---|
571 | AssertReturn(uOffset >= pFS->off, VERR_INVALID_PARAMETER);
|
---|
572 | int rc;
|
---|
573 |
|
---|
574 | /* Fill buffer if it is empty. */
|
---|
575 | if (pFS->offBuffer == UINT64_MAX)
|
---|
576 | {
|
---|
577 | /* Repeat reading until buffer is full or EOF. */
|
---|
578 | size_t cbRead;
|
---|
579 | size_t cbSumRead = 0;
|
---|
580 | uint8_t *pbTmp = (uint8_t *)&pFS->abBuffer[0];
|
---|
581 | size_t cbTmp = sizeof(pFS->abBuffer);
|
---|
582 | do
|
---|
583 | {
|
---|
584 | rc = RTFileRead(pFS->file, pbTmp, cbTmp, &cbRead);
|
---|
585 | if (RT_FAILURE(rc))
|
---|
586 | return rc;
|
---|
587 | pbTmp += cbRead;
|
---|
588 | cbTmp -= cbRead;
|
---|
589 | cbSumRead += cbRead;
|
---|
590 | } while (cbTmp && cbRead);
|
---|
591 |
|
---|
592 | pFS->offBuffer = 0;
|
---|
593 | pFS->cbBuffer = (uint32_t)cbSumRead;
|
---|
594 | if (!cbSumRead && !pcbRead) /* Caller can't handle partial reads. */
|
---|
595 | return VERR_EOF;
|
---|
596 | }
|
---|
597 |
|
---|
598 | /* Read several blocks and assemble the result if necessary */
|
---|
599 | size_t cbTotalRead = 0;
|
---|
600 | do
|
---|
601 | {
|
---|
602 | /* Skip over areas no one wants to read. */
|
---|
603 | while (uOffset > pFS->offBuffer + pFS->cbBuffer - 1)
|
---|
604 | {
|
---|
605 | if (pFS->cbBuffer < sizeof(pFS->abBuffer))
|
---|
606 | {
|
---|
607 | if (pcbRead)
|
---|
608 | *pcbRead = cbTotalRead;
|
---|
609 | return VERR_EOF;
|
---|
610 | }
|
---|
611 |
|
---|
612 | /* Repeat reading until buffer is full or EOF. */
|
---|
613 | size_t cbRead;
|
---|
614 | size_t cbSumRead = 0;
|
---|
615 | uint8_t *pbTmp = (uint8_t *)&pFS->abBuffer[0];
|
---|
616 | size_t cbTmp = sizeof(pFS->abBuffer);
|
---|
617 | do
|
---|
618 | {
|
---|
619 | rc = RTFileRead(pFS->file, pbTmp, cbTmp, &cbRead);
|
---|
620 | if (RT_FAILURE(rc))
|
---|
621 | return rc;
|
---|
622 | pbTmp += cbRead;
|
---|
623 | cbTmp -= cbRead;
|
---|
624 | cbSumRead += cbRead;
|
---|
625 | } while (cbTmp && cbRead);
|
---|
626 |
|
---|
627 | pFS->offBuffer += pFS->cbBuffer;
|
---|
628 | pFS->cbBuffer = (uint32_t)cbSumRead;
|
---|
629 | }
|
---|
630 |
|
---|
631 | uint32_t cbThisRead = (uint32_t)RT_MIN(cbBuffer,
|
---|
632 | pFS->cbBuffer - uOffset % sizeof(pFS->abBuffer));
|
---|
633 | memcpy(pvBuffer, &pFS->abBuffer[uOffset % sizeof(pFS->abBuffer)],
|
---|
634 | cbThisRead);
|
---|
635 | uOffset += cbThisRead;
|
---|
636 | pvBuffer = (uint8_t *)pvBuffer + cbThisRead;
|
---|
637 | cbBuffer -= cbThisRead;
|
---|
638 | cbTotalRead += cbThisRead;
|
---|
639 | if (!cbTotalRead && !pcbRead) /* Caller can't handle partial reads. */
|
---|
640 | return VERR_EOF;
|
---|
641 | } while (cbBuffer > 0);
|
---|
642 |
|
---|
643 | if (pcbRead)
|
---|
644 | *pcbRead = cbTotalRead;
|
---|
645 |
|
---|
646 | pFS->off = uOffset;
|
---|
647 |
|
---|
648 | return VINF_SUCCESS;
|
---|
649 | }
|
---|
650 |
|
---|
651 | static DECLCALLBACK(int) convInWrite(void *pvUser, void *pStorage, uint64_t uOffset, const void *pvBuffer, size_t cbBuffer,
|
---|
652 | size_t *pcbWritten)
|
---|
653 | {
|
---|
654 | NOREF(pvUser);
|
---|
655 | NOREF(pStorage);
|
---|
656 | NOREF(uOffset);
|
---|
657 | NOREF(cbBuffer);
|
---|
658 | NOREF(pcbWritten);
|
---|
659 | AssertPtrReturn(pvBuffer, VERR_INVALID_POINTER);
|
---|
660 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
661 | }
|
---|
662 |
|
---|
663 | static DECLCALLBACK(int) convInFlush(void *pvUser, void *pStorage)
|
---|
664 | {
|
---|
665 | NOREF(pvUser);
|
---|
666 | NOREF(pStorage);
|
---|
667 | return VINF_SUCCESS;
|
---|
668 | }
|
---|
669 |
|
---|
670 | static DECLCALLBACK(int) convOutOpen(void *pvUser, const char *pszLocation, uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
|
---|
671 | void **ppStorage)
|
---|
672 | {
|
---|
673 | RT_NOREF2(pvUser, pszLocation);
|
---|
674 |
|
---|
675 | /* Validate input. */
|
---|
676 | AssertPtrReturn(ppStorage, VERR_INVALID_POINTER);
|
---|
677 | AssertPtrNullReturn(pfnCompleted, VERR_INVALID_PARAMETER);
|
---|
678 | AssertReturn((fOpen & RTFILE_O_ACCESS_MASK) == RTFILE_O_WRITE, VERR_INVALID_PARAMETER);
|
---|
679 | RTFILE file;
|
---|
680 | int rc = RTFileFromNative(&file, RTFILE_NATIVE_STDOUT);
|
---|
681 | if (RT_FAILURE(rc))
|
---|
682 | return rc;
|
---|
683 |
|
---|
684 | /* Must clear buffer, so that skipped over data is initialized properly. */
|
---|
685 | PFILEIOSTATE pFS = (PFILEIOSTATE)RTMemAllocZ(sizeof(FILEIOSTATE));
|
---|
686 | if (!pFS)
|
---|
687 | return VERR_NO_MEMORY;
|
---|
688 |
|
---|
689 | pFS->file = file;
|
---|
690 | pFS->off = 0;
|
---|
691 | pFS->offBuffer = 0;
|
---|
692 | pFS->cbBuffer = sizeof(FILEIOSTATE);
|
---|
693 |
|
---|
694 | *ppStorage = pFS;
|
---|
695 | return VINF_SUCCESS;
|
---|
696 | }
|
---|
697 |
|
---|
698 | static DECLCALLBACK(int) convOutClose(void *pvUser, void *pStorage)
|
---|
699 | {
|
---|
700 | NOREF(pvUser);
|
---|
701 | AssertPtrReturn(pStorage, VERR_INVALID_POINTER);
|
---|
702 | PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage;
|
---|
703 | int rc = VINF_SUCCESS;
|
---|
704 |
|
---|
705 | /* Flush any remaining buffer contents. */
|
---|
706 | if (pFS->cbBuffer)
|
---|
707 | rc = RTFileWrite(pFS->file, &pFS->abBuffer[0], pFS->cbBuffer, NULL);
|
---|
708 |
|
---|
709 | RTMemFree(pFS);
|
---|
710 |
|
---|
711 | return rc;
|
---|
712 | }
|
---|
713 |
|
---|
714 | static DECLCALLBACK(int) convOutDelete(void *pvUser, const char *pcszFilename)
|
---|
715 | {
|
---|
716 | NOREF(pvUser);
|
---|
717 | NOREF(pcszFilename);
|
---|
718 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
719 | }
|
---|
720 |
|
---|
721 | static DECLCALLBACK(int) convOutMove(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
|
---|
722 | {
|
---|
723 | NOREF(pvUser);
|
---|
724 | NOREF(pcszSrc);
|
---|
725 | NOREF(pcszDst);
|
---|
726 | NOREF(fMove);
|
---|
727 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
728 | }
|
---|
729 |
|
---|
730 | static DECLCALLBACK(int) convOutGetFreeSpace(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
|
---|
731 | {
|
---|
732 | NOREF(pvUser);
|
---|
733 | NOREF(pcszFilename);
|
---|
734 | AssertPtrReturn(pcbFreeSpace, VERR_INVALID_POINTER);
|
---|
735 | *pcbFreeSpace = INT64_MAX;
|
---|
736 | return VINF_SUCCESS;
|
---|
737 | }
|
---|
738 |
|
---|
739 | static DECLCALLBACK(int) convOutGetModificationTime(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
|
---|
740 | {
|
---|
741 | NOREF(pvUser);
|
---|
742 | NOREF(pcszFilename);
|
---|
743 | AssertPtrReturn(pModificationTime, VERR_INVALID_POINTER);
|
---|
744 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
745 | }
|
---|
746 |
|
---|
747 | static DECLCALLBACK(int) convOutGetSize(void *pvUser, void *pStorage, uint64_t *pcbSize)
|
---|
748 | {
|
---|
749 | NOREF(pvUser);
|
---|
750 | NOREF(pStorage);
|
---|
751 | AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
|
---|
752 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
753 | }
|
---|
754 |
|
---|
755 | static DECLCALLBACK(int) convOutSetSize(void *pvUser, void *pStorage, uint64_t cbSize)
|
---|
756 | {
|
---|
757 | NOREF(pvUser);
|
---|
758 | NOREF(pStorage);
|
---|
759 | NOREF(cbSize);
|
---|
760 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
761 | }
|
---|
762 |
|
---|
763 | static DECLCALLBACK(int) convOutRead(void *pvUser, void *pStorage, uint64_t uOffset, void *pvBuffer, size_t cbBuffer,
|
---|
764 | size_t *pcbRead)
|
---|
765 | {
|
---|
766 | NOREF(pvUser);
|
---|
767 | NOREF(pStorage);
|
---|
768 | NOREF(uOffset);
|
---|
769 | NOREF(cbBuffer);
|
---|
770 | NOREF(pcbRead);
|
---|
771 | AssertPtrReturn(pvBuffer, VERR_INVALID_POINTER);
|
---|
772 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
773 | }
|
---|
774 |
|
---|
775 | static DECLCALLBACK(int) convOutWrite(void *pvUser, void *pStorage, uint64_t uOffset, const void *pvBuffer, size_t cbBuffer,
|
---|
776 | size_t *pcbWritten)
|
---|
777 | {
|
---|
778 | NOREF(pvUser);
|
---|
779 | AssertPtrReturn(pStorage, VERR_INVALID_POINTER);
|
---|
780 | AssertPtrReturn(pvBuffer, VERR_INVALID_POINTER);
|
---|
781 | PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage;
|
---|
782 | AssertReturn(uOffset >= pFS->off, VERR_INVALID_PARAMETER);
|
---|
783 | int rc;
|
---|
784 |
|
---|
785 | /* Write the data to the buffer, flushing as required. */
|
---|
786 | size_t cbTotalWritten = 0;
|
---|
787 | do
|
---|
788 | {
|
---|
789 | /* Flush the buffer if we need a new one. */
|
---|
790 | while (uOffset > pFS->offBuffer + sizeof(pFS->abBuffer) - 1)
|
---|
791 | {
|
---|
792 | rc = RTFileWrite(pFS->file, &pFS->abBuffer[0],
|
---|
793 | sizeof(pFS->abBuffer), NULL);
|
---|
794 | RT_ZERO(pFS->abBuffer);
|
---|
795 | pFS->offBuffer += sizeof(pFS->abBuffer);
|
---|
796 | pFS->cbBuffer = 0;
|
---|
797 | }
|
---|
798 |
|
---|
799 | uint32_t cbThisWrite = (uint32_t)RT_MIN(cbBuffer,
|
---|
800 | sizeof(pFS->abBuffer) - uOffset % sizeof(pFS->abBuffer));
|
---|
801 | memcpy(&pFS->abBuffer[uOffset % sizeof(pFS->abBuffer)], pvBuffer,
|
---|
802 | cbThisWrite);
|
---|
803 | uOffset += cbThisWrite;
|
---|
804 | pvBuffer = (uint8_t *)pvBuffer + cbThisWrite;
|
---|
805 | cbBuffer -= cbThisWrite;
|
---|
806 | cbTotalWritten += cbThisWrite;
|
---|
807 | } while (cbBuffer > 0);
|
---|
808 |
|
---|
809 | if (pcbWritten)
|
---|
810 | *pcbWritten = cbTotalWritten;
|
---|
811 |
|
---|
812 | pFS->cbBuffer = uOffset % sizeof(pFS->abBuffer);
|
---|
813 | if (!pFS->cbBuffer)
|
---|
814 | pFS->cbBuffer = sizeof(pFS->abBuffer);
|
---|
815 | pFS->off = uOffset;
|
---|
816 |
|
---|
817 | return VINF_SUCCESS;
|
---|
818 | }
|
---|
819 |
|
---|
820 | static DECLCALLBACK(int) convOutFlush(void *pvUser, void *pStorage)
|
---|
821 | {
|
---|
822 | NOREF(pvUser);
|
---|
823 | NOREF(pStorage);
|
---|
824 | return VINF_SUCCESS;
|
---|
825 | }
|
---|
826 |
|
---|
827 | static int handleConvert(HandlerArg *a)
|
---|
828 | {
|
---|
829 | const char *pszSrcFilename = NULL;
|
---|
830 | const char *pszDstFilename = NULL;
|
---|
831 | bool fStdIn = false;
|
---|
832 | bool fStdOut = false;
|
---|
833 | const char *pszSrcFormat = NULL;
|
---|
834 | VDTYPE enmSrcType = VDTYPE_HDD;
|
---|
835 | const char *pszDstFormat = NULL;
|
---|
836 | const char *pszVariant = NULL;
|
---|
837 | PVBOXHDD pSrcDisk = NULL;
|
---|
838 | PVBOXHDD pDstDisk = NULL;
|
---|
839 | unsigned uImageFlags = VD_IMAGE_FLAGS_NONE;
|
---|
840 | PVDINTERFACE pIfsImageInput = NULL;
|
---|
841 | PVDINTERFACE pIfsImageOutput = NULL;
|
---|
842 | VDINTERFACEIO IfsInputIO;
|
---|
843 | VDINTERFACEIO IfsOutputIO;
|
---|
844 | int rc = VINF_SUCCESS;
|
---|
845 |
|
---|
846 | /* Parse the command line. */
|
---|
847 | static const RTGETOPTDEF s_aOptions[] =
|
---|
848 | {
|
---|
849 | { "--srcfilename", 'i', RTGETOPT_REQ_STRING },
|
---|
850 | { "--dstfilename", 'o', RTGETOPT_REQ_STRING },
|
---|
851 | { "--stdin", 'p', RTGETOPT_REQ_NOTHING },
|
---|
852 | { "--stdout", 'P', RTGETOPT_REQ_NOTHING },
|
---|
853 | { "--srcformat", 's', RTGETOPT_REQ_STRING },
|
---|
854 | { "--dstformat", 'd', RTGETOPT_REQ_STRING },
|
---|
855 | { "--variant", 'v', RTGETOPT_REQ_STRING }
|
---|
856 | };
|
---|
857 | int ch;
|
---|
858 | RTGETOPTUNION ValueUnion;
|
---|
859 | RTGETOPTSTATE GetState;
|
---|
860 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
861 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
862 | {
|
---|
863 | switch (ch)
|
---|
864 | {
|
---|
865 | case 'i': // --srcfilename
|
---|
866 | pszSrcFilename = ValueUnion.psz;
|
---|
867 | break;
|
---|
868 | case 'o': // --dstfilename
|
---|
869 | pszDstFilename = ValueUnion.psz;
|
---|
870 | break;
|
---|
871 | case 'p': // --stdin
|
---|
872 | fStdIn = true;
|
---|
873 | break;
|
---|
874 | case 'P': // --stdout
|
---|
875 | fStdOut = true;
|
---|
876 | break;
|
---|
877 | case 's': // --srcformat
|
---|
878 | pszSrcFormat = ValueUnion.psz;
|
---|
879 | break;
|
---|
880 | case 'd': // --dstformat
|
---|
881 | pszDstFormat = ValueUnion.psz;
|
---|
882 | break;
|
---|
883 | case 'v': // --variant
|
---|
884 | pszVariant = ValueUnion.psz;
|
---|
885 | break;
|
---|
886 |
|
---|
887 | default:
|
---|
888 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
889 | printUsage(g_pStdErr);
|
---|
890 | return ch;
|
---|
891 | }
|
---|
892 | }
|
---|
893 |
|
---|
894 | /* Check for mandatory parameters and handle dummies/defaults. */
|
---|
895 | if (fStdIn && !pszSrcFormat)
|
---|
896 | return errorSyntax("Mandatory --srcformat option missing\n");
|
---|
897 | if (!pszDstFormat)
|
---|
898 | pszDstFormat = "VDI";
|
---|
899 | if (fStdIn && !pszSrcFilename)
|
---|
900 | {
|
---|
901 | /* Complete dummy, will be just passed to various calls to fulfill
|
---|
902 | * the "must be non-NULL" requirement, and is completely ignored
|
---|
903 | * otherwise. It shown in the stderr message below. */
|
---|
904 | pszSrcFilename = "stdin";
|
---|
905 | }
|
---|
906 | if (fStdOut && !pszDstFilename)
|
---|
907 | {
|
---|
908 | /* Will be stored in the destination image if it is a streamOptimized
|
---|
909 | * VMDK, but it isn't really relevant - use it for "branding". */
|
---|
910 | if (!RTStrICmp(pszDstFormat, "VMDK"))
|
---|
911 | pszDstFilename = "VirtualBoxStream.vmdk";
|
---|
912 | else
|
---|
913 | pszDstFilename = "stdout";
|
---|
914 | }
|
---|
915 | if (!pszSrcFilename)
|
---|
916 | return errorSyntax("Mandatory --srcfilename option missing\n");
|
---|
917 | if (!pszDstFilename)
|
---|
918 | return errorSyntax("Mandatory --dstfilename option missing\n");
|
---|
919 |
|
---|
920 | if (fStdIn)
|
---|
921 | {
|
---|
922 | IfsInputIO.pfnOpen = convInOpen;
|
---|
923 | IfsInputIO.pfnClose = convInClose;
|
---|
924 | IfsInputIO.pfnDelete = convInDelete;
|
---|
925 | IfsInputIO.pfnMove = convInMove;
|
---|
926 | IfsInputIO.pfnGetFreeSpace = convInGetFreeSpace;
|
---|
927 | IfsInputIO.pfnGetModificationTime = convInGetModificationTime;
|
---|
928 | IfsInputIO.pfnGetSize = convInGetSize;
|
---|
929 | IfsInputIO.pfnSetSize = convInSetSize;
|
---|
930 | IfsInputIO.pfnReadSync = convInRead;
|
---|
931 | IfsInputIO.pfnWriteSync = convInWrite;
|
---|
932 | IfsInputIO.pfnFlushSync = convInFlush;
|
---|
933 | VDInterfaceAdd(&IfsInputIO.Core, "stdin", VDINTERFACETYPE_IO,
|
---|
934 | NULL, sizeof(VDINTERFACEIO), &pIfsImageInput);
|
---|
935 | }
|
---|
936 | if (fStdOut)
|
---|
937 | {
|
---|
938 | IfsOutputIO.pfnOpen = convOutOpen;
|
---|
939 | IfsOutputIO.pfnClose = convOutClose;
|
---|
940 | IfsOutputIO.pfnDelete = convOutDelete;
|
---|
941 | IfsOutputIO.pfnMove = convOutMove;
|
---|
942 | IfsOutputIO.pfnGetFreeSpace = convOutGetFreeSpace;
|
---|
943 | IfsOutputIO.pfnGetModificationTime = convOutGetModificationTime;
|
---|
944 | IfsOutputIO.pfnGetSize = convOutGetSize;
|
---|
945 | IfsOutputIO.pfnSetSize = convOutSetSize;
|
---|
946 | IfsOutputIO.pfnReadSync = convOutRead;
|
---|
947 | IfsOutputIO.pfnWriteSync = convOutWrite;
|
---|
948 | IfsOutputIO.pfnFlushSync = convOutFlush;
|
---|
949 | VDInterfaceAdd(&IfsOutputIO.Core, "stdout", VDINTERFACETYPE_IO,
|
---|
950 | NULL, sizeof(VDINTERFACEIO), &pIfsImageOutput);
|
---|
951 | }
|
---|
952 |
|
---|
953 | /* check the variant parameter */
|
---|
954 | if (pszVariant)
|
---|
955 | {
|
---|
956 | char *psz = (char*)pszVariant;
|
---|
957 | while (psz && *psz && RT_SUCCESS(rc))
|
---|
958 | {
|
---|
959 | size_t len;
|
---|
960 | const char *pszComma = strchr(psz, ',');
|
---|
961 | if (pszComma)
|
---|
962 | len = pszComma - psz;
|
---|
963 | else
|
---|
964 | len = strlen(psz);
|
---|
965 | if (len > 0)
|
---|
966 | {
|
---|
967 | if (!RTStrNICmp(pszVariant, "standard", len))
|
---|
968 | uImageFlags |= VD_IMAGE_FLAGS_NONE;
|
---|
969 | else if (!RTStrNICmp(pszVariant, "fixed", len))
|
---|
970 | uImageFlags |= VD_IMAGE_FLAGS_FIXED;
|
---|
971 | else if (!RTStrNICmp(pszVariant, "split2g", len))
|
---|
972 | uImageFlags |= VD_VMDK_IMAGE_FLAGS_SPLIT_2G;
|
---|
973 | else if (!RTStrNICmp(pszVariant, "stream", len))
|
---|
974 | uImageFlags |= VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED;
|
---|
975 | else if (!RTStrNICmp(pszVariant, "esx", len))
|
---|
976 | uImageFlags |= VD_VMDK_IMAGE_FLAGS_ESX;
|
---|
977 | else
|
---|
978 | return errorSyntax("Invalid --variant option\n");
|
---|
979 | }
|
---|
980 | if (pszComma)
|
---|
981 | psz += len + 1;
|
---|
982 | else
|
---|
983 | psz += len;
|
---|
984 | }
|
---|
985 | }
|
---|
986 |
|
---|
987 | do
|
---|
988 | {
|
---|
989 | /* try to determine input format if not specified */
|
---|
990 | if (!pszSrcFormat)
|
---|
991 | {
|
---|
992 | char *pszFormat = NULL;
|
---|
993 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
994 | rc = VDGetFormat(NULL, NULL, pszSrcFilename, &pszFormat, &enmType);
|
---|
995 | if (RT_FAILURE(rc))
|
---|
996 | {
|
---|
997 | errorSyntax("No file format specified, please specify format: %Rrc\n", rc);
|
---|
998 | break;
|
---|
999 | }
|
---|
1000 | pszSrcFormat = pszFormat;
|
---|
1001 | enmSrcType = enmType;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | rc = VDCreate(pVDIfs, enmSrcType, &pSrcDisk);
|
---|
1005 | if (RT_FAILURE(rc))
|
---|
1006 | {
|
---|
1007 | errorRuntime("Error while creating source disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
1008 | break;
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | rc = VDOpen(pSrcDisk, pszSrcFormat, pszSrcFilename,
|
---|
1012 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
1013 | pIfsImageInput);
|
---|
1014 | if (RT_FAILURE(rc))
|
---|
1015 | {
|
---|
1016 | errorRuntime("Error while opening source image: %Rrf (%Rrc)\n", rc, rc);
|
---|
1017 | break;
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | rc = VDCreate(pVDIfs, VDTYPE_HDD, &pDstDisk);
|
---|
1021 | if (RT_FAILURE(rc))
|
---|
1022 | {
|
---|
1023 | errorRuntime("Error while creating the destination disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
1024 | break;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | uint64_t cbSize = VDGetSize(pSrcDisk, VD_LAST_IMAGE);
|
---|
1028 | RTStrmPrintf(g_pStdErr, "Converting image \"%s\" with size %RU64 bytes (%RU64MB)...\n", pszSrcFilename, cbSize, (cbSize + _1M - 1) / _1M);
|
---|
1029 |
|
---|
1030 | /* Create the output image */
|
---|
1031 | rc = VDCopy(pSrcDisk, VD_LAST_IMAGE, pDstDisk, pszDstFormat,
|
---|
1032 | pszDstFilename, false, 0, uImageFlags, NULL,
|
---|
1033 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL, NULL,
|
---|
1034 | pIfsImageOutput, NULL);
|
---|
1035 | if (RT_FAILURE(rc))
|
---|
1036 | {
|
---|
1037 | errorRuntime("Error while copying the image: %Rrf (%Rrc)\n", rc, rc);
|
---|
1038 | break;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | }
|
---|
1042 | while (0);
|
---|
1043 |
|
---|
1044 | if (pDstDisk)
|
---|
1045 | VDDestroy(pDstDisk);
|
---|
1046 | if (pSrcDisk)
|
---|
1047 | VDDestroy(pSrcDisk);
|
---|
1048 |
|
---|
1049 | return RT_SUCCESS(rc) ? 0 : 1;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 |
|
---|
1053 | static int handleInfo(HandlerArg *a)
|
---|
1054 | {
|
---|
1055 | int rc = VINF_SUCCESS;
|
---|
1056 | PVBOXHDD pDisk = NULL;
|
---|
1057 | const char *pszFilename = NULL;
|
---|
1058 |
|
---|
1059 | /* Parse the command line. */
|
---|
1060 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1061 | {
|
---|
1062 | { "--filename", 'f', RTGETOPT_REQ_STRING }
|
---|
1063 | };
|
---|
1064 | int ch;
|
---|
1065 | RTGETOPTUNION ValueUnion;
|
---|
1066 | RTGETOPTSTATE GetState;
|
---|
1067 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
1068 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1069 | {
|
---|
1070 | switch (ch)
|
---|
1071 | {
|
---|
1072 | case 'f': // --filename
|
---|
1073 | pszFilename = ValueUnion.psz;
|
---|
1074 | break;
|
---|
1075 |
|
---|
1076 | default:
|
---|
1077 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
1078 | printUsage(g_pStdErr);
|
---|
1079 | return ch;
|
---|
1080 | }
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | /* Check for mandatory parameters. */
|
---|
1084 | if (!pszFilename)
|
---|
1085 | return errorSyntax("Mandatory --filename option missing\n");
|
---|
1086 |
|
---|
1087 | /* just try it */
|
---|
1088 | char *pszFormat = NULL;
|
---|
1089 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
1090 | rc = VDGetFormat(NULL, NULL, pszFilename, &pszFormat, &enmType);
|
---|
1091 | if (RT_FAILURE(rc))
|
---|
1092 | return errorSyntax("Format autodetect failed: %Rrc\n", rc);
|
---|
1093 |
|
---|
1094 | rc = VDCreate(pVDIfs, enmType, &pDisk);
|
---|
1095 | if (RT_FAILURE(rc))
|
---|
1096 | return errorRuntime("Error while creating the virtual disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
1097 |
|
---|
1098 | /* Open the image */
|
---|
1099 | rc = VDOpen(pDisk, pszFormat, pszFilename, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY, NULL);
|
---|
1100 | if (RT_FAILURE(rc))
|
---|
1101 | return errorRuntime("Error while opening the image: %Rrf (%Rrc)\n", rc, rc);
|
---|
1102 |
|
---|
1103 | VDDumpImages(pDisk);
|
---|
1104 |
|
---|
1105 | VDDestroy(pDisk);
|
---|
1106 |
|
---|
1107 | return rc;
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 |
|
---|
1111 | static DECLCALLBACK(int) vboximgDvmRead(void *pvUser, uint64_t off, void *pvBuf, size_t cbRead)
|
---|
1112 | {
|
---|
1113 | int rc = VINF_SUCCESS;
|
---|
1114 | PVBOXHDD pDisk = (PVBOXHDD)pvUser;
|
---|
1115 |
|
---|
1116 | /* Take shortcut if possible. */
|
---|
1117 | if ( off % 512 == 0
|
---|
1118 | && cbRead % 512 == 0)
|
---|
1119 | rc = VDRead(pDisk, off, pvBuf, cbRead);
|
---|
1120 | else
|
---|
1121 | {
|
---|
1122 | uint8_t *pbBuf = (uint8_t *)pvBuf;
|
---|
1123 | uint8_t abBuf[512];
|
---|
1124 |
|
---|
1125 | /* Unaligned access, make it aligned. */
|
---|
1126 | if (off % 512 != 0)
|
---|
1127 | {
|
---|
1128 | uint64_t offAligned = off & ~(uint64_t)(512 - 1);
|
---|
1129 | size_t cbToCopy = 512 - (off - offAligned);
|
---|
1130 | rc = VDRead(pDisk, offAligned, abBuf, 512);
|
---|
1131 | if (RT_SUCCESS(rc))
|
---|
1132 | {
|
---|
1133 | memcpy(pbBuf, &abBuf[off - offAligned], cbToCopy);
|
---|
1134 | pbBuf += cbToCopy;
|
---|
1135 | off += cbToCopy;
|
---|
1136 | cbRead -= cbToCopy;
|
---|
1137 | }
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | if ( RT_SUCCESS(rc)
|
---|
1141 | && (cbRead & ~(uint64_t)(512 - 1)))
|
---|
1142 | {
|
---|
1143 | size_t cbReadAligned = cbRead & ~(uint64_t)(512 - 1);
|
---|
1144 |
|
---|
1145 | Assert(!(off % 512));
|
---|
1146 | rc = VDRead(pDisk, off, pbBuf, cbReadAligned);
|
---|
1147 | if (RT_SUCCESS(rc))
|
---|
1148 | {
|
---|
1149 | pbBuf += cbReadAligned;
|
---|
1150 | off += cbReadAligned;
|
---|
1151 | cbRead -= cbReadAligned;
|
---|
1152 | }
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | if ( RT_SUCCESS(rc)
|
---|
1156 | && cbRead)
|
---|
1157 | {
|
---|
1158 | Assert(cbRead < 512);
|
---|
1159 | Assert(!(off % 512));
|
---|
1160 |
|
---|
1161 | rc = VDRead(pDisk, off, abBuf, 512);
|
---|
1162 | if (RT_SUCCESS(rc))
|
---|
1163 | memcpy(pbBuf, abBuf, cbRead);
|
---|
1164 | }
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | return rc;
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 |
|
---|
1171 | static DECLCALLBACK(int) vboximgDvmWrite(void *pvUser, uint64_t off, const void *pvBuf, size_t cbWrite)
|
---|
1172 | {
|
---|
1173 | PVBOXHDD pDisk = (PVBOXHDD)pvUser;
|
---|
1174 | return VDWrite(pDisk, off, pvBuf, cbWrite);
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 |
|
---|
1178 | static DECLCALLBACK(int) vboximgQueryBlockStatus(void *pvUser, uint64_t off,
|
---|
1179 | uint64_t cb, bool *pfAllocated)
|
---|
1180 | {
|
---|
1181 | RTVFS hVfs = (RTVFS)pvUser;
|
---|
1182 | return RTVfsIsRangeInUse(hVfs, off, cb, pfAllocated);
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 |
|
---|
1186 | static DECLCALLBACK(int) vboximgQueryRangeUse(void *pvUser, uint64_t off, uint64_t cb,
|
---|
1187 | bool *pfUsed)
|
---|
1188 | {
|
---|
1189 | RTDVM hVolMgr = (RTDVM)pvUser;
|
---|
1190 | return RTDvmMapQueryBlockStatus(hVolMgr, off, cb, pfUsed);
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 |
|
---|
1194 | typedef struct VBOXIMGVFS
|
---|
1195 | {
|
---|
1196 | /** Pointer to the next VFS handle. */
|
---|
1197 | struct VBOXIMGVFS *pNext;
|
---|
1198 | /** VFS handle. */
|
---|
1199 | RTVFS hVfs;
|
---|
1200 | } VBOXIMGVFS, *PVBOXIMGVFS;
|
---|
1201 |
|
---|
1202 | static int handleCompact(HandlerArg *a)
|
---|
1203 | {
|
---|
1204 | int rc = VINF_SUCCESS;
|
---|
1205 | PVBOXHDD pDisk = NULL;
|
---|
1206 | const char *pszFilename = NULL;
|
---|
1207 | bool fFilesystemAware = false;
|
---|
1208 | VDINTERFACEQUERYRANGEUSE VDIfQueryRangeUse;
|
---|
1209 | PVDINTERFACE pIfsCompact = NULL;
|
---|
1210 | RTDVM hDvm = NIL_RTDVM;
|
---|
1211 | PVBOXIMGVFS pVBoxImgVfsHead = NULL;
|
---|
1212 |
|
---|
1213 | /* Parse the command line. */
|
---|
1214 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1215 | {
|
---|
1216 | { "--filename", 'f', RTGETOPT_REQ_STRING },
|
---|
1217 | { "--filesystemaware", 'a', RTGETOPT_REQ_NOTHING }
|
---|
1218 | };
|
---|
1219 | int ch;
|
---|
1220 | RTGETOPTUNION ValueUnion;
|
---|
1221 | RTGETOPTSTATE GetState;
|
---|
1222 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
1223 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1224 | {
|
---|
1225 | switch (ch)
|
---|
1226 | {
|
---|
1227 | case 'f': // --filename
|
---|
1228 | pszFilename = ValueUnion.psz;
|
---|
1229 | break;
|
---|
1230 |
|
---|
1231 | case 'a':
|
---|
1232 | fFilesystemAware = true;
|
---|
1233 | break;
|
---|
1234 |
|
---|
1235 | default:
|
---|
1236 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
1237 | printUsage(g_pStdErr);
|
---|
1238 | return ch;
|
---|
1239 | }
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | /* Check for mandatory parameters. */
|
---|
1243 | if (!pszFilename)
|
---|
1244 | return errorSyntax("Mandatory --filename option missing\n");
|
---|
1245 |
|
---|
1246 | /* just try it */
|
---|
1247 | char *pszFormat = NULL;
|
---|
1248 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
1249 | rc = VDGetFormat(NULL, NULL, pszFilename, &pszFormat, &enmType);
|
---|
1250 | if (RT_FAILURE(rc))
|
---|
1251 | return errorSyntax("Format autodetect failed: %Rrc\n", rc);
|
---|
1252 |
|
---|
1253 | rc = VDCreate(pVDIfs, enmType, &pDisk);
|
---|
1254 | if (RT_FAILURE(rc))
|
---|
1255 | return errorRuntime("Error while creating the virtual disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
1256 |
|
---|
1257 | /* Open the image */
|
---|
1258 | rc = VDOpen(pDisk, pszFormat, pszFilename, VD_OPEN_FLAGS_NORMAL, NULL);
|
---|
1259 | if (RT_FAILURE(rc))
|
---|
1260 | return errorRuntime("Error while opening the image: %Rrf (%Rrc)\n", rc, rc);
|
---|
1261 |
|
---|
1262 | if ( RT_SUCCESS(rc)
|
---|
1263 | && fFilesystemAware)
|
---|
1264 | {
|
---|
1265 | uint64_t cbDisk = 0;
|
---|
1266 |
|
---|
1267 | cbDisk = VDGetSize(pDisk, 0);
|
---|
1268 | if (cbDisk > 0)
|
---|
1269 | {
|
---|
1270 | rc = RTDvmCreate(&hDvm, vboximgDvmRead, vboximgDvmWrite, cbDisk, 512,
|
---|
1271 | 0 /* fFlags*/, pDisk);
|
---|
1272 | if (RT_SUCCESS(rc))
|
---|
1273 | {
|
---|
1274 | rc = RTDvmMapOpen(hDvm);
|
---|
1275 | if ( RT_SUCCESS(rc)
|
---|
1276 | && RTDvmMapGetValidVolumes(hDvm))
|
---|
1277 | {
|
---|
1278 | RTDVMVOLUME hVol;
|
---|
1279 |
|
---|
1280 | /* Get all volumes and set the block query status callback. */
|
---|
1281 | rc = RTDvmMapQueryFirstVolume(hDvm, &hVol);
|
---|
1282 | AssertRC(rc);
|
---|
1283 |
|
---|
1284 | do
|
---|
1285 | {
|
---|
1286 | RTVFSFILE hVfsFile;
|
---|
1287 | rc = RTDvmVolumeCreateVfsFile(hVol, &hVfsFile);
|
---|
1288 | if (RT_FAILURE(rc))
|
---|
1289 | break;
|
---|
1290 |
|
---|
1291 | /* Try to detect the filesystem in this volume. */
|
---|
1292 | RTVFS hVfs;
|
---|
1293 | rc = RTFilesystemVfsFromFile(hVfsFile, &hVfs);
|
---|
1294 | if (rc == VERR_NOT_SUPPORTED)
|
---|
1295 | {
|
---|
1296 | /* Release the file handle and continue.*/
|
---|
1297 | RTVfsFileRelease(hVfsFile);
|
---|
1298 | }
|
---|
1299 | else if (RT_FAILURE(rc))
|
---|
1300 | break;
|
---|
1301 | else
|
---|
1302 | {
|
---|
1303 | PVBOXIMGVFS pVBoxImgVfs = (PVBOXIMGVFS)RTMemAllocZ(sizeof(VBOXIMGVFS));
|
---|
1304 | if (!pVBoxImgVfs)
|
---|
1305 | rc = VERR_NO_MEMORY;
|
---|
1306 | else
|
---|
1307 | {
|
---|
1308 | pVBoxImgVfs->hVfs = hVfs;
|
---|
1309 | pVBoxImgVfs->pNext = pVBoxImgVfsHead;
|
---|
1310 | pVBoxImgVfsHead = pVBoxImgVfs;
|
---|
1311 | RTDvmVolumeSetQueryBlockStatusCallback(hVol, vboximgQueryBlockStatus, hVfs);
|
---|
1312 | }
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | RTDVMVOLUME hVolNext = NIL_RTDVMVOLUME;
|
---|
1316 | if (RT_SUCCESS(rc))
|
---|
1317 | rc = RTDvmMapQueryNextVolume(hDvm, hVol, &hVolNext);
|
---|
1318 |
|
---|
1319 | /*
|
---|
1320 | * Release the volume handle, the file handle has a reference
|
---|
1321 | * to keep it open.
|
---|
1322 | */
|
---|
1323 | RTDvmVolumeRelease(hVol);
|
---|
1324 | hVol = hVolNext;
|
---|
1325 | } while (RT_SUCCESS(rc));
|
---|
1326 |
|
---|
1327 | if (rc == VERR_DVM_MAP_NO_VOLUME)
|
---|
1328 | rc = VINF_SUCCESS;
|
---|
1329 |
|
---|
1330 | if (RT_SUCCESS(rc))
|
---|
1331 | {
|
---|
1332 | VDIfQueryRangeUse.pfnQueryRangeUse = vboximgQueryRangeUse;
|
---|
1333 | VDInterfaceAdd(&VDIfQueryRangeUse.Core, "QueryRangeUse", VDINTERFACETYPE_QUERYRANGEUSE,
|
---|
1334 | hDvm, sizeof(VDINTERFACEQUERYRANGEUSE), &pIfsCompact);
|
---|
1335 | }
|
---|
1336 | }
|
---|
1337 | else if (RT_SUCCESS(rc))
|
---|
1338 | RTPrintf("There are no partitions in the volume map\n");
|
---|
1339 | else if (rc == VERR_NOT_FOUND)
|
---|
1340 | {
|
---|
1341 | rc = VINF_SUCCESS;
|
---|
1342 | RTPrintf("No known volume format on disk found\n");
|
---|
1343 | }
|
---|
1344 | else
|
---|
1345 | errorRuntime("Error while opening the volume manager: %Rrf (%Rrc)\n", rc, rc);
|
---|
1346 | }
|
---|
1347 | else
|
---|
1348 | errorRuntime("Error creating the volume manager: %Rrf (%Rrc)\n", rc, rc);
|
---|
1349 | }
|
---|
1350 | else
|
---|
1351 | {
|
---|
1352 | rc = VERR_INVALID_STATE;
|
---|
1353 | errorRuntime("Error while getting the disk size\n");
|
---|
1354 | }
|
---|
1355 | }
|
---|
1356 |
|
---|
1357 | if (RT_SUCCESS(rc))
|
---|
1358 | {
|
---|
1359 | rc = VDCompact(pDisk, 0, pIfsCompact);
|
---|
1360 | if (RT_FAILURE(rc))
|
---|
1361 | errorRuntime("Error while compacting image: %Rrf (%Rrc)\n", rc, rc);
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | while (pVBoxImgVfsHead)
|
---|
1365 | {
|
---|
1366 | PVBOXIMGVFS pVBoxImgVfsFree = pVBoxImgVfsHead;
|
---|
1367 |
|
---|
1368 | pVBoxImgVfsHead = pVBoxImgVfsHead->pNext;
|
---|
1369 | RTVfsRelease(pVBoxImgVfsFree->hVfs);
|
---|
1370 | RTMemFree(pVBoxImgVfsFree);
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | if (hDvm)
|
---|
1374 | RTDvmRelease(hDvm);
|
---|
1375 |
|
---|
1376 | VDDestroy(pDisk);
|
---|
1377 |
|
---|
1378 | return rc;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 |
|
---|
1382 | static int handleCreateCache(HandlerArg *a)
|
---|
1383 | {
|
---|
1384 | int rc = VINF_SUCCESS;
|
---|
1385 | PVBOXHDD pDisk = NULL;
|
---|
1386 | const char *pszFilename = NULL;
|
---|
1387 | uint64_t cbSize = 0;
|
---|
1388 |
|
---|
1389 | /* Parse the command line. */
|
---|
1390 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1391 | {
|
---|
1392 | { "--filename", 'f', RTGETOPT_REQ_STRING },
|
---|
1393 | { "--size", 's', RTGETOPT_REQ_UINT64 }
|
---|
1394 | };
|
---|
1395 | int ch;
|
---|
1396 | RTGETOPTUNION ValueUnion;
|
---|
1397 | RTGETOPTSTATE GetState;
|
---|
1398 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
1399 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1400 | {
|
---|
1401 | switch (ch)
|
---|
1402 | {
|
---|
1403 | case 'f': // --filename
|
---|
1404 | pszFilename = ValueUnion.psz;
|
---|
1405 | break;
|
---|
1406 |
|
---|
1407 | case 's': // --size
|
---|
1408 | cbSize = ValueUnion.u64;
|
---|
1409 | break;
|
---|
1410 |
|
---|
1411 | default:
|
---|
1412 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
1413 | printUsage(g_pStdErr);
|
---|
1414 | return ch;
|
---|
1415 | }
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | /* Check for mandatory parameters. */
|
---|
1419 | if (!pszFilename)
|
---|
1420 | return errorSyntax("Mandatory --filename option missing\n");
|
---|
1421 |
|
---|
1422 | if (!cbSize)
|
---|
1423 | return errorSyntax("Mandatory --size option missing\n");
|
---|
1424 |
|
---|
1425 | /* just try it */
|
---|
1426 | rc = VDCreate(pVDIfs, VDTYPE_HDD, &pDisk);
|
---|
1427 | if (RT_FAILURE(rc))
|
---|
1428 | return errorRuntime("Error while creating the virtual disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
1429 |
|
---|
1430 | rc = VDCreateCache(pDisk, "VCI", pszFilename, cbSize, VD_IMAGE_FLAGS_DEFAULT,
|
---|
1431 | NULL, NULL, VD_OPEN_FLAGS_NORMAL, NULL, NULL);
|
---|
1432 | if (RT_FAILURE(rc))
|
---|
1433 | return errorRuntime("Error while creating the virtual disk cache: %Rrf (%Rrc)\n", rc, rc);
|
---|
1434 |
|
---|
1435 | VDDestroy(pDisk);
|
---|
1436 |
|
---|
1437 | return rc;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | static DECLCALLBACK(bool) vdIfCfgCreateBaseAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
1441 | {
|
---|
1442 | RT_NOREF2(pvUser, pszzValid);
|
---|
1443 | return VINF_SUCCESS; /** @todo Implement. */
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | static DECLCALLBACK(int) vdIfCfgCreateBaseQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
1447 | {
|
---|
1448 | AssertReturn(VALID_PTR(pcbValue), VERR_INVALID_POINTER);
|
---|
1449 |
|
---|
1450 | AssertPtrReturn(pvUser, VERR_GENERAL_FAILURE);
|
---|
1451 |
|
---|
1452 | if (RTStrCmp(pszName, "DataAlignment"))
|
---|
1453 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
1454 |
|
---|
1455 | *pcbValue = strlen((const char *)pvUser) + 1 /* include terminator */;
|
---|
1456 |
|
---|
1457 | return VINF_SUCCESS;
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | static DECLCALLBACK(int) vdIfCfgCreateBaseQuery(void *pvUser, const char *pszName, char *pszValue, size_t cchValue)
|
---|
1461 | {
|
---|
1462 | AssertReturn(VALID_PTR(pszValue), VERR_INVALID_POINTER);
|
---|
1463 |
|
---|
1464 | AssertPtrReturn(pvUser, VERR_GENERAL_FAILURE);
|
---|
1465 |
|
---|
1466 | if (RTStrCmp(pszName, "DataAlignment"))
|
---|
1467 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
1468 |
|
---|
1469 | if (strlen((const char *)pvUser) >= cchValue)
|
---|
1470 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
1471 |
|
---|
1472 | memcpy(pszValue, pvUser, strlen((const char *)pvUser) + 1);
|
---|
1473 |
|
---|
1474 | return VINF_SUCCESS;
|
---|
1475 |
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | static int handleCreateBase(HandlerArg *a)
|
---|
1479 | {
|
---|
1480 | int rc = VINF_SUCCESS;
|
---|
1481 | PVBOXHDD pDisk = NULL;
|
---|
1482 | const char *pszFilename = NULL;
|
---|
1483 | const char *pszBackend = "VDI";
|
---|
1484 | const char *pszVariant = NULL;
|
---|
1485 | unsigned uImageFlags = VD_IMAGE_FLAGS_NONE;
|
---|
1486 | uint64_t cbSize = 0;
|
---|
1487 | const char *pszDataAlignment = NULL;
|
---|
1488 | VDGEOMETRY LCHSGeometry, PCHSGeometry;
|
---|
1489 | PVDINTERFACE pVDIfsOperation = NULL;
|
---|
1490 | VDINTERFACECONFIG vdIfCfg;
|
---|
1491 |
|
---|
1492 | memset(&LCHSGeometry, 0, sizeof(LCHSGeometry));
|
---|
1493 | memset(&PCHSGeometry, 0, sizeof(PCHSGeometry));
|
---|
1494 |
|
---|
1495 | /* Parse the command line. */
|
---|
1496 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1497 | {
|
---|
1498 | { "--filename", 'f', RTGETOPT_REQ_STRING },
|
---|
1499 | { "--size", 's', RTGETOPT_REQ_UINT64 },
|
---|
1500 | { "--format", 'b', RTGETOPT_REQ_STRING },
|
---|
1501 | { "--variant", 'v', RTGETOPT_REQ_STRING },
|
---|
1502 | { "--dataalignment", 'a', RTGETOPT_REQ_STRING }
|
---|
1503 | };
|
---|
1504 | int ch;
|
---|
1505 | RTGETOPTUNION ValueUnion;
|
---|
1506 | RTGETOPTSTATE GetState;
|
---|
1507 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
1508 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1509 | {
|
---|
1510 | switch (ch)
|
---|
1511 | {
|
---|
1512 | case 'f': // --filename
|
---|
1513 | pszFilename = ValueUnion.psz;
|
---|
1514 | break;
|
---|
1515 |
|
---|
1516 | case 's': // --size
|
---|
1517 | cbSize = ValueUnion.u64;
|
---|
1518 | break;
|
---|
1519 |
|
---|
1520 | case 'b': // --format
|
---|
1521 | pszBackend = ValueUnion.psz;
|
---|
1522 | break;
|
---|
1523 |
|
---|
1524 | case 'v': // --variant
|
---|
1525 | pszVariant = ValueUnion.psz;
|
---|
1526 | break;
|
---|
1527 |
|
---|
1528 | case 'a': // --dataalignment
|
---|
1529 | pszDataAlignment = ValueUnion.psz;
|
---|
1530 | break;
|
---|
1531 |
|
---|
1532 | default:
|
---|
1533 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
1534 | printUsage(g_pStdErr);
|
---|
1535 | return ch;
|
---|
1536 | }
|
---|
1537 | }
|
---|
1538 |
|
---|
1539 | /* Check for mandatory parameters. */
|
---|
1540 | if (!pszFilename)
|
---|
1541 | return errorSyntax("Mandatory --filename option missing\n");
|
---|
1542 |
|
---|
1543 | if (!cbSize)
|
---|
1544 | return errorSyntax("Mandatory --size option missing\n");
|
---|
1545 |
|
---|
1546 | if (pszVariant)
|
---|
1547 | {
|
---|
1548 | rc = parseDiskVariant(pszVariant, &uImageFlags);
|
---|
1549 | if (RT_FAILURE(rc))
|
---|
1550 | return errorSyntax("Invalid variant %s given\n", pszVariant);
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | /* Setup the config interface if required. */
|
---|
1554 | if (pszDataAlignment)
|
---|
1555 | {
|
---|
1556 | vdIfCfg.pfnAreKeysValid = vdIfCfgCreateBaseAreKeysValid;
|
---|
1557 | vdIfCfg.pfnQuerySize = vdIfCfgCreateBaseQuerySize;
|
---|
1558 | vdIfCfg.pfnQuery = vdIfCfgCreateBaseQuery;
|
---|
1559 | VDInterfaceAdd(&vdIfCfg.Core, "Config", VDINTERFACETYPE_CONFIG, (void *)pszDataAlignment,
|
---|
1560 | sizeof(vdIfCfg), &pVDIfsOperation);
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | /* just try it */
|
---|
1564 | rc = VDCreate(pVDIfs, VDTYPE_HDD, &pDisk);
|
---|
1565 | if (RT_FAILURE(rc))
|
---|
1566 | return errorRuntime("Error while creating the virtual disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
1567 |
|
---|
1568 | rc = VDCreateBase(pDisk, pszBackend, pszFilename, cbSize, uImageFlags,
|
---|
1569 | NULL, &PCHSGeometry, &LCHSGeometry, NULL, VD_OPEN_FLAGS_NORMAL,
|
---|
1570 | NULL, pVDIfsOperation);
|
---|
1571 | if (RT_FAILURE(rc))
|
---|
1572 | return errorRuntime("Error while creating the virtual disk: %Rrf (%Rrc)\n", rc, rc);
|
---|
1573 |
|
---|
1574 | VDDestroy(pDisk);
|
---|
1575 |
|
---|
1576 | return rc;
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 |
|
---|
1580 | static int handleRepair(HandlerArg *a)
|
---|
1581 | {
|
---|
1582 | int rc = VINF_SUCCESS;
|
---|
1583 | const char *pszFilename = NULL;
|
---|
1584 | char *pszBackend = NULL;
|
---|
1585 | const char *pszFormat = NULL;
|
---|
1586 | bool fDryRun = false;
|
---|
1587 | VDTYPE enmType = VDTYPE_HDD;
|
---|
1588 |
|
---|
1589 | /* Parse the command line. */
|
---|
1590 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1591 | {
|
---|
1592 | { "--filename", 'f', RTGETOPT_REQ_STRING },
|
---|
1593 | { "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
|
---|
1594 | { "--format", 'b', RTGETOPT_REQ_STRING }
|
---|
1595 | };
|
---|
1596 | int ch;
|
---|
1597 | RTGETOPTUNION ValueUnion;
|
---|
1598 | RTGETOPTSTATE GetState;
|
---|
1599 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
1600 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1601 | {
|
---|
1602 | switch (ch)
|
---|
1603 | {
|
---|
1604 | case 'f': // --filename
|
---|
1605 | pszFilename = ValueUnion.psz;
|
---|
1606 | break;
|
---|
1607 |
|
---|
1608 | case 'd': // --dry-run
|
---|
1609 | fDryRun = true;
|
---|
1610 | break;
|
---|
1611 |
|
---|
1612 | case 'b': // --format
|
---|
1613 | pszFormat = ValueUnion.psz;
|
---|
1614 | break;
|
---|
1615 |
|
---|
1616 | default:
|
---|
1617 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
1618 | printUsage(g_pStdErr);
|
---|
1619 | return ch;
|
---|
1620 | }
|
---|
1621 | }
|
---|
1622 |
|
---|
1623 | /* Check for mandatory parameters. */
|
---|
1624 | if (!pszFilename)
|
---|
1625 | return errorSyntax("Mandatory --filename option missing\n");
|
---|
1626 |
|
---|
1627 | /* just try it */
|
---|
1628 | if (!pszFormat)
|
---|
1629 | {
|
---|
1630 | rc = VDGetFormat(NULL, NULL, pszFilename, &pszBackend, &enmType);
|
---|
1631 | if (RT_FAILURE(rc))
|
---|
1632 | return errorSyntax("Format autodetect failed: %Rrc\n", rc);
|
---|
1633 | pszFormat = pszBackend;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | rc = VDRepair(pVDIfs, NULL, pszFilename, pszFormat, fDryRun ? VD_REPAIR_DRY_RUN : 0);
|
---|
1637 | if (RT_FAILURE(rc))
|
---|
1638 | rc = errorRuntime("Error while repairing the virtual disk: %Rrf (%Rrc)\n", rc, rc);
|
---|
1639 |
|
---|
1640 | if (pszBackend)
|
---|
1641 | RTStrFree(pszBackend);
|
---|
1642 | return rc;
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 |
|
---|
1646 | static int handleClearComment(HandlerArg *a)
|
---|
1647 | {
|
---|
1648 | int rc = VINF_SUCCESS;
|
---|
1649 | PVBOXHDD pDisk = NULL;
|
---|
1650 | const char *pszFilename = NULL;
|
---|
1651 |
|
---|
1652 | /* Parse the command line. */
|
---|
1653 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1654 | {
|
---|
1655 | { "--filename", 'f', RTGETOPT_REQ_STRING }
|
---|
1656 | };
|
---|
1657 | int ch;
|
---|
1658 | RTGETOPTUNION ValueUnion;
|
---|
1659 | RTGETOPTSTATE GetState;
|
---|
1660 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
1661 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1662 | {
|
---|
1663 | switch (ch)
|
---|
1664 | {
|
---|
1665 | case 'f': // --filename
|
---|
1666 | pszFilename = ValueUnion.psz;
|
---|
1667 | break;
|
---|
1668 |
|
---|
1669 | default:
|
---|
1670 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
1671 | printUsage(g_pStdErr);
|
---|
1672 | return ch;
|
---|
1673 | }
|
---|
1674 | }
|
---|
1675 |
|
---|
1676 | /* Check for mandatory parameters. */
|
---|
1677 | if (!pszFilename)
|
---|
1678 | return errorSyntax("Mandatory --filename option missing\n");
|
---|
1679 |
|
---|
1680 | /* just try it */
|
---|
1681 | char *pszFormat = NULL;
|
---|
1682 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
1683 | rc = VDGetFormat(NULL, NULL, pszFilename, &pszFormat, &enmType);
|
---|
1684 | if (RT_FAILURE(rc))
|
---|
1685 | return errorSyntax("Format autodetect failed: %Rrc\n", rc);
|
---|
1686 |
|
---|
1687 | rc = VDCreate(pVDIfs, enmType, &pDisk);
|
---|
1688 | if (RT_FAILURE(rc))
|
---|
1689 | return errorRuntime("Error while creating the virtual disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
1690 |
|
---|
1691 | /* Open the image */
|
---|
1692 | rc = VDOpen(pDisk, pszFormat, pszFilename, VD_OPEN_FLAGS_INFO, NULL);
|
---|
1693 | if (RT_FAILURE(rc))
|
---|
1694 | return errorRuntime("Error while opening the image: %Rrf (%Rrc)\n", rc, rc);
|
---|
1695 |
|
---|
1696 | VDSetComment(pDisk, 0, NULL);
|
---|
1697 |
|
---|
1698 | VDDestroy(pDisk);
|
---|
1699 | return rc;
|
---|
1700 | }
|
---|
1701 |
|
---|
1702 |
|
---|
1703 | static int handleClearResize(HandlerArg *a)
|
---|
1704 | {
|
---|
1705 | int rc = VINF_SUCCESS;
|
---|
1706 | PVBOXHDD pDisk = NULL;
|
---|
1707 | const char *pszFilename = NULL;
|
---|
1708 | uint64_t cbNew = 0;
|
---|
1709 | VDGEOMETRY LCHSGeometry, PCHSGeometry;
|
---|
1710 |
|
---|
1711 | memset(&LCHSGeometry, 0, sizeof(LCHSGeometry));
|
---|
1712 | memset(&PCHSGeometry, 0, sizeof(PCHSGeometry));
|
---|
1713 |
|
---|
1714 | /* Parse the command line. */
|
---|
1715 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1716 | {
|
---|
1717 | { "--filename", 'f', RTGETOPT_REQ_STRING },
|
---|
1718 | { "--size", 's', RTGETOPT_REQ_UINT64 }
|
---|
1719 | };
|
---|
1720 | int ch;
|
---|
1721 | RTGETOPTUNION ValueUnion;
|
---|
1722 | RTGETOPTSTATE GetState;
|
---|
1723 | RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
|
---|
1724 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1725 | {
|
---|
1726 | switch (ch)
|
---|
1727 | {
|
---|
1728 | case 'f': // --filename
|
---|
1729 | pszFilename = ValueUnion.psz;
|
---|
1730 | break;
|
---|
1731 |
|
---|
1732 | case 's': // --size
|
---|
1733 | cbNew = ValueUnion.u64;
|
---|
1734 | break;
|
---|
1735 |
|
---|
1736 | default:
|
---|
1737 | ch = RTGetOptPrintError(ch, &ValueUnion);
|
---|
1738 | printUsage(g_pStdErr);
|
---|
1739 | return ch;
|
---|
1740 | }
|
---|
1741 | }
|
---|
1742 |
|
---|
1743 | /* Check for mandatory parameters. */
|
---|
1744 | if (!pszFilename)
|
---|
1745 | return errorSyntax("Mandatory --filename option missing\n");
|
---|
1746 |
|
---|
1747 | if (!cbNew)
|
---|
1748 | return errorSyntax("Mandatory --size option missing or invalid\n");
|
---|
1749 |
|
---|
1750 | /* just try it */
|
---|
1751 | char *pszFormat = NULL;
|
---|
1752 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
1753 | rc = VDGetFormat(NULL, NULL, pszFilename, &pszFormat, &enmType);
|
---|
1754 | if (RT_FAILURE(rc))
|
---|
1755 | return errorSyntax("Format autodetect failed: %Rrc\n", rc);
|
---|
1756 |
|
---|
1757 | rc = VDCreate(pVDIfs, enmType, &pDisk);
|
---|
1758 | if (RT_FAILURE(rc))
|
---|
1759 | return errorRuntime("Error while creating the virtual disk container: %Rrf (%Rrc)\n", rc, rc);
|
---|
1760 |
|
---|
1761 | /* Open the image */
|
---|
1762 | rc = VDOpen(pDisk, pszFormat, pszFilename, VD_OPEN_FLAGS_NORMAL, NULL);
|
---|
1763 | if (RT_FAILURE(rc))
|
---|
1764 | return errorRuntime("Error while opening the image: %Rrf (%Rrc)\n", rc, rc);
|
---|
1765 |
|
---|
1766 | rc = VDResize(pDisk, cbNew, &PCHSGeometry, &LCHSGeometry, NULL);
|
---|
1767 | if (RT_FAILURE(rc))
|
---|
1768 | rc = errorRuntime("Error while resizing the virtual disk: %Rrf (%Rrc)\n", rc, rc);
|
---|
1769 |
|
---|
1770 | VDDestroy(pDisk);
|
---|
1771 | return rc;
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 |
|
---|
1775 | int main(int argc, char *argv[])
|
---|
1776 | {
|
---|
1777 | int exitcode = 0;
|
---|
1778 |
|
---|
1779 | int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_STANDALONE_APP);
|
---|
1780 | if (RT_FAILURE(rc))
|
---|
1781 | return RTMsgInitFailure(rc);
|
---|
1782 |
|
---|
1783 | g_pszProgName = RTPathFilename(argv[0]);
|
---|
1784 |
|
---|
1785 | bool fShowLogo = false;
|
---|
1786 | int iCmd = 1;
|
---|
1787 | int iCmdArg;
|
---|
1788 |
|
---|
1789 | /* global options */
|
---|
1790 | for (int i = 1; i < argc || argc <= iCmd; i++)
|
---|
1791 | {
|
---|
1792 | if ( argc <= iCmd
|
---|
1793 | || !strcmp(argv[i], "help")
|
---|
1794 | || !strcmp(argv[i], "-?")
|
---|
1795 | || !strcmp(argv[i], "-h")
|
---|
1796 | || !strcmp(argv[i], "-help")
|
---|
1797 | || !strcmp(argv[i], "--help"))
|
---|
1798 | {
|
---|
1799 | showLogo(g_pStdOut);
|
---|
1800 | printUsage(g_pStdOut);
|
---|
1801 | return 0;
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | if ( !strcmp(argv[i], "-v")
|
---|
1805 | || !strcmp(argv[i], "-version")
|
---|
1806 | || !strcmp(argv[i], "-Version")
|
---|
1807 | || !strcmp(argv[i], "--version"))
|
---|
1808 | {
|
---|
1809 | /* Print version number, and do nothing else. */
|
---|
1810 | RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
|
---|
1811 | return 0;
|
---|
1812 | }
|
---|
1813 |
|
---|
1814 | if ( !strcmp(argv[i], "--nologo")
|
---|
1815 | || !strcmp(argv[i], "-nologo")
|
---|
1816 | || !strcmp(argv[i], "-q"))
|
---|
1817 | {
|
---|
1818 | /* suppress the logo */
|
---|
1819 | fShowLogo = false;
|
---|
1820 | iCmd++;
|
---|
1821 | }
|
---|
1822 | else
|
---|
1823 | {
|
---|
1824 | break;
|
---|
1825 | }
|
---|
1826 | }
|
---|
1827 |
|
---|
1828 | iCmdArg = iCmd + 1;
|
---|
1829 |
|
---|
1830 | if (fShowLogo)
|
---|
1831 | showLogo(g_pStdOut);
|
---|
1832 |
|
---|
1833 | /* initialize the VD backend with dummy handlers */
|
---|
1834 | VDINTERFACEERROR vdInterfaceError;
|
---|
1835 | vdInterfaceError.pfnError = handleVDError;
|
---|
1836 | vdInterfaceError.pfnMessage = handleVDMessage;
|
---|
1837 |
|
---|
1838 | rc = VDInterfaceAdd(&vdInterfaceError.Core, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
|
---|
1839 | NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
|
---|
1840 |
|
---|
1841 | rc = VDInit();
|
---|
1842 | if (RT_FAILURE(rc))
|
---|
1843 | {
|
---|
1844 | errorSyntax("Initializing backends failed! rc=%Rrc\n", rc);
|
---|
1845 | return 1;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | /*
|
---|
1849 | * All registered command handlers
|
---|
1850 | */
|
---|
1851 | static const struct
|
---|
1852 | {
|
---|
1853 | const char *command;
|
---|
1854 | int (*handler)(HandlerArg *a);
|
---|
1855 | } s_commandHandlers[] =
|
---|
1856 | {
|
---|
1857 | { "setuuid", handleSetUUID },
|
---|
1858 | { "geometry", handleGeometry },
|
---|
1859 | { "convert", handleConvert },
|
---|
1860 | { "info", handleInfo },
|
---|
1861 | { "compact", handleCompact },
|
---|
1862 | { "createcache", handleCreateCache },
|
---|
1863 | { "createbase", handleCreateBase },
|
---|
1864 | { "repair", handleRepair },
|
---|
1865 | { "clearcomment", handleClearComment },
|
---|
1866 | { "resize", handleClearResize },
|
---|
1867 | { NULL, NULL }
|
---|
1868 | };
|
---|
1869 |
|
---|
1870 | HandlerArg handlerArg = { 0, NULL };
|
---|
1871 | int commandIndex;
|
---|
1872 | for (commandIndex = 0; s_commandHandlers[commandIndex].command != NULL; commandIndex++)
|
---|
1873 | {
|
---|
1874 | if (!strcmp(s_commandHandlers[commandIndex].command, argv[iCmd]))
|
---|
1875 | {
|
---|
1876 | handlerArg.argc = argc - iCmdArg;
|
---|
1877 | handlerArg.argv = &argv[iCmdArg];
|
---|
1878 |
|
---|
1879 | exitcode = s_commandHandlers[commandIndex].handler(&handlerArg);
|
---|
1880 | break;
|
---|
1881 | }
|
---|
1882 | }
|
---|
1883 | if (!s_commandHandlers[commandIndex].command)
|
---|
1884 | {
|
---|
1885 | errorSyntax("Invalid command '%s'", argv[iCmd]);
|
---|
1886 | return 1;
|
---|
1887 | }
|
---|
1888 |
|
---|
1889 | rc = VDShutdown();
|
---|
1890 | if (RT_FAILURE(rc))
|
---|
1891 | {
|
---|
1892 | errorSyntax("Unloading backends failed! rc=%Rrc\n", rc);
|
---|
1893 | return 1;
|
---|
1894 | }
|
---|
1895 |
|
---|
1896 | return exitcode;
|
---|
1897 | }
|
---|
1898 |
|
---|
1899 | /* dummy stub for RuntimeR3 */
|
---|
1900 | #ifndef RT_OS_WINDOWS
|
---|
1901 | RTDECL(bool) RTAssertShouldPanic(void)
|
---|
1902 | {
|
---|
1903 | return true;
|
---|
1904 | }
|
---|
1905 | #endif
|
---|